Don't erase empty indices in DWARF reader
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
47 #include "bfd.h"
48 #include "elf-bfd.h"
49 #include "symtab.h"
50 #include "gdbtypes.h"
51 #include "objfiles.h"
52 #include "dwarf2.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "c-lang.h"
68 #include "go-lang.h"
69 #include "valprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
72 #include "gdb_bfd.h"
73 #include "f-lang.h"
74 #include "source.h"
75 #include "build-id.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
82 #include "producer.h"
83 #include <fcntl.h>
84 #include <algorithm>
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
96
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug = 0;
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
103
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
109
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
112 ##__VA_ARGS__)
113
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug = 0;
116
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug = 0;
119
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname = false;
122
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections = false;
125
126 /* This is used to store the data that is always per objfile. */
127 static const registry<objfile>::key<dwarf2_per_objfile>
128 dwarf2_objfile_data_key;
129
130 /* These are used to store the dwarf2_per_bfd objects.
131
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
134
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
137 version. */
138 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
139 static const registry<objfile>::key<dwarf2_per_bfd>
140 dwarf2_per_bfd_objfile_data_key;
141
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
143
144 static int dwarf2_locexpr_index;
145 static int dwarf2_loclist_index;
146 static int dwarf2_locexpr_block_index;
147 static int dwarf2_loclist_block_index;
148
149 /* Size of .debug_loclists section header for 32-bit DWARF format. */
150 #define LOCLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_loclists section header for 64-bit DWARF format. */
153 #define LOCLIST_HEADER_SIZE64 20
154
155 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
156 #define RNGLIST_HEADER_SIZE32 12
157
158 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
159 #define RNGLIST_HEADER_SIZE64 20
160
161 /* This is a view into the index that converts from bytes to an
162 offset_type, and allows indexing. Unaligned bytes are specifically
163 allowed here, and handled via unpacking. */
164
165 class offset_view
166 {
167 public:
168 offset_view () = default;
169
170 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
171 : m_bytes (bytes)
172 {
173 }
174
175 /* Extract the INDEXth offset_type from the array. */
176 offset_type operator[] (size_t index) const
177 {
178 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
179 return (offset_type) extract_unsigned_integer (bytes,
180 sizeof (offset_type),
181 BFD_ENDIAN_LITTLE);
182 }
183
184 /* Return the number of offset_types in this array. */
185 size_t size () const
186 {
187 return m_bytes.size () / sizeof (offset_type);
188 }
189
190 /* Return true if this view is empty. */
191 bool empty () const
192 {
193 return m_bytes.empty ();
194 }
195
196 private:
197 /* The underlying bytes. */
198 gdb::array_view<const gdb_byte> m_bytes;
199 };
200
201 /* A description of the mapped index. The file format is described in
202 a comment by the code that writes the index. */
203 struct mapped_index final : public mapped_index_base
204 {
205 /* Index data format version. */
206 int version = 0;
207
208 /* The address table data. */
209 gdb::array_view<const gdb_byte> address_table;
210
211 /* The symbol table, implemented as a hash table. */
212 offset_view symbol_table;
213
214 /* A pointer to the constant pool. */
215 gdb::array_view<const gdb_byte> constant_pool;
216
217 /* Return the index into the constant pool of the name of the IDXth
218 symbol in the symbol table. */
219 offset_type symbol_name_index (offset_type idx) const
220 {
221 return symbol_table[2 * idx];
222 }
223
224 /* Return the index into the constant pool of the CU vector of the
225 IDXth symbol in the symbol table. */
226 offset_type symbol_vec_index (offset_type idx) const
227 {
228 return symbol_table[2 * idx + 1];
229 }
230
231 bool symbol_name_slot_invalid (offset_type idx) const override
232 {
233 return (symbol_name_index (idx) == 0
234 && symbol_vec_index (idx) == 0);
235 }
236
237 /* Convenience method to get at the name of the symbol at IDX in the
238 symbol table. */
239 const char *symbol_name_at
240 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
241 {
242 return (const char *) (this->constant_pool.data ()
243 + symbol_name_index (idx));
244 }
245
246 size_t symbol_name_count () const override
247 { return this->symbol_table.size () / 2; }
248
249 quick_symbol_functions_up make_quick_functions () const override;
250
251 bool version_check () const override
252 {
253 return version >= 8;
254 }
255 };
256
257 /* A description of the mapped .debug_names.
258 Uninitialized map has CU_COUNT 0. */
259 struct mapped_debug_names final : public mapped_index_base
260 {
261 bfd_endian dwarf5_byte_order;
262 bool dwarf5_is_dwarf64;
263 bool augmentation_is_gdb;
264 uint8_t offset_size;
265 uint32_t cu_count = 0;
266 uint32_t tu_count, bucket_count, name_count;
267 const gdb_byte *cu_table_reordered, *tu_table_reordered;
268 const uint32_t *bucket_table_reordered, *hash_table_reordered;
269 const gdb_byte *name_table_string_offs_reordered;
270 const gdb_byte *name_table_entry_offs_reordered;
271 const gdb_byte *entry_pool;
272
273 struct index_val
274 {
275 ULONGEST dwarf_tag;
276 struct attr
277 {
278 /* Attribute name DW_IDX_*. */
279 ULONGEST dw_idx;
280
281 /* Attribute form DW_FORM_*. */
282 ULONGEST form;
283
284 /* Value if FORM is DW_FORM_implicit_const. */
285 LONGEST implicit_const;
286 };
287 std::vector<attr> attr_vec;
288 };
289
290 std::unordered_map<ULONGEST, index_val> abbrev_map;
291
292 const char *namei_to_name
293 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
294
295 /* Implementation of the mapped_index_base virtual interface, for
296 the name_components cache. */
297
298 const char *symbol_name_at
299 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
300 { return namei_to_name (idx, per_objfile); }
301
302 size_t symbol_name_count () const override
303 { return this->name_count; }
304
305 quick_symbol_functions_up make_quick_functions () const override;
306 };
307
308 /* See dwarf2/read.h. */
309
310 dwarf2_per_objfile *
311 get_dwarf2_per_objfile (struct objfile *objfile)
312 {
313 return dwarf2_objfile_data_key.get (objfile);
314 }
315
316 /* Default names of the debugging sections. */
317
318 /* Note that if the debugging section has been compressed, it might
319 have a name like .zdebug_info. */
320
321 const struct dwarf2_debug_sections dwarf2_elf_names =
322 {
323 { ".debug_info", ".zdebug_info" },
324 { ".debug_abbrev", ".zdebug_abbrev" },
325 { ".debug_line", ".zdebug_line" },
326 { ".debug_loc", ".zdebug_loc" },
327 { ".debug_loclists", ".zdebug_loclists" },
328 { ".debug_macinfo", ".zdebug_macinfo" },
329 { ".debug_macro", ".zdebug_macro" },
330 { ".debug_str", ".zdebug_str" },
331 { ".debug_str_offsets", ".zdebug_str_offsets" },
332 { ".debug_line_str", ".zdebug_line_str" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_rnglists", ".zdebug_rnglists" },
335 { ".debug_types", ".zdebug_types" },
336 { ".debug_addr", ".zdebug_addr" },
337 { ".debug_frame", ".zdebug_frame" },
338 { ".eh_frame", NULL },
339 { ".gdb_index", ".zgdb_index" },
340 { ".debug_names", ".zdebug_names" },
341 { ".debug_aranges", ".zdebug_aranges" },
342 23
343 };
344
345 /* List of DWO/DWP sections. */
346
347 static const struct dwop_section_names
348 {
349 struct dwarf2_section_names abbrev_dwo;
350 struct dwarf2_section_names info_dwo;
351 struct dwarf2_section_names line_dwo;
352 struct dwarf2_section_names loc_dwo;
353 struct dwarf2_section_names loclists_dwo;
354 struct dwarf2_section_names macinfo_dwo;
355 struct dwarf2_section_names macro_dwo;
356 struct dwarf2_section_names rnglists_dwo;
357 struct dwarf2_section_names str_dwo;
358 struct dwarf2_section_names str_offsets_dwo;
359 struct dwarf2_section_names types_dwo;
360 struct dwarf2_section_names cu_index;
361 struct dwarf2_section_names tu_index;
362 }
363 dwop_section_names =
364 {
365 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
366 { ".debug_info.dwo", ".zdebug_info.dwo" },
367 { ".debug_line.dwo", ".zdebug_line.dwo" },
368 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
369 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
372 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
373 { ".debug_str.dwo", ".zdebug_str.dwo" },
374 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
375 { ".debug_types.dwo", ".zdebug_types.dwo" },
376 { ".debug_cu_index", ".zdebug_cu_index" },
377 { ".debug_tu_index", ".zdebug_tu_index" },
378 };
379
380 /* local data types */
381
382 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
383 begin with a header, which contains the following information. */
384 struct loclists_rnglists_header
385 {
386 /* A 4-byte or 12-byte length containing the length of the
387 set of entries for this compilation unit, not including the
388 length field itself. */
389 unsigned int length;
390
391 /* A 2-byte version identifier. */
392 short version;
393
394 /* A 1-byte unsigned integer containing the size in bytes of an address on
395 the target system. */
396 unsigned char addr_size;
397
398 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
399 on the target system. */
400 unsigned char segment_collector_size;
401
402 /* A 4-byte count of the number of offsets that follow the header. */
403 unsigned int offset_entry_count;
404 };
405
406 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
407 This includes type_unit_group and quick_file_names. */
408
409 struct stmt_list_hash
410 {
411 /* The DWO unit this table is from or NULL if there is none. */
412 struct dwo_unit *dwo_unit;
413
414 /* Offset in .debug_line or .debug_line.dwo. */
415 sect_offset line_sect_off;
416 };
417
418 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
419 an object of this type. This contains elements of type unit groups
420 that can be shared across objfiles. The non-shareable parts are in
421 type_unit_group_unshareable. */
422
423 struct type_unit_group
424 {
425 /* The data used to construct the hash key. */
426 struct stmt_list_hash hash {};
427 };
428
429 /* These sections are what may appear in a (real or virtual) DWO file. */
430
431 struct dwo_sections
432 {
433 struct dwarf2_section_info abbrev;
434 struct dwarf2_section_info line;
435 struct dwarf2_section_info loc;
436 struct dwarf2_section_info loclists;
437 struct dwarf2_section_info macinfo;
438 struct dwarf2_section_info macro;
439 struct dwarf2_section_info rnglists;
440 struct dwarf2_section_info str;
441 struct dwarf2_section_info str_offsets;
442 /* In the case of a virtual DWO file, these two are unused. */
443 struct dwarf2_section_info info;
444 std::vector<dwarf2_section_info> types;
445 };
446
447 /* CUs/TUs in DWP/DWO files. */
448
449 struct dwo_unit
450 {
451 /* Backlink to the containing struct dwo_file. */
452 struct dwo_file *dwo_file;
453
454 /* The "id" that distinguishes this CU/TU.
455 .debug_info calls this "dwo_id", .debug_types calls this "signature".
456 Since signatures came first, we stick with it for consistency. */
457 ULONGEST signature;
458
459 /* The section this CU/TU lives in, in the DWO file. */
460 struct dwarf2_section_info *section;
461
462 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
463 sect_offset sect_off;
464 unsigned int length;
465
466 /* For types, offset in the type's DIE of the type defined by this TU. */
467 cu_offset type_offset_in_tu;
468 };
469
470 /* include/dwarf2.h defines the DWP section codes.
471 It defines a max value but it doesn't define a min value, which we
472 use for error checking, so provide one. */
473
474 enum dwp_v2_section_ids
475 {
476 DW_SECT_MIN = 1
477 };
478
479 /* Data for one DWO file.
480
481 This includes virtual DWO files (a virtual DWO file is a DWO file as it
482 appears in a DWP file). DWP files don't really have DWO files per se -
483 comdat folding of types "loses" the DWO file they came from, and from
484 a high level view DWP files appear to contain a mass of random types.
485 However, to maintain consistency with the non-DWP case we pretend DWP
486 files contain virtual DWO files, and we assign each TU with one virtual
487 DWO file (generally based on the line and abbrev section offsets -
488 a heuristic that seems to work in practice). */
489
490 struct dwo_file
491 {
492 dwo_file () = default;
493 DISABLE_COPY_AND_ASSIGN (dwo_file);
494
495 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
496 For virtual DWO files the name is constructed from the section offsets
497 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
498 from related CU+TUs. */
499 const char *dwo_name = nullptr;
500
501 /* The DW_AT_comp_dir attribute. */
502 const char *comp_dir = nullptr;
503
504 /* The bfd, when the file is open. Otherwise this is NULL.
505 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
506 gdb_bfd_ref_ptr dbfd;
507
508 /* The sections that make up this DWO file.
509 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
510 sections (for lack of a better name). */
511 struct dwo_sections sections {};
512
513 /* The CUs in the file.
514 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
515 an extension to handle LLVM's Link Time Optimization output (where
516 multiple source files may be compiled into a single object/dwo pair). */
517 htab_up cus;
518
519 /* Table of TUs in the file.
520 Each element is a struct dwo_unit. */
521 htab_up tus;
522 };
523
524 /* These sections are what may appear in a DWP file. */
525
526 struct dwp_sections
527 {
528 /* These are used by all DWP versions (1, 2 and 5). */
529 struct dwarf2_section_info str;
530 struct dwarf2_section_info cu_index;
531 struct dwarf2_section_info tu_index;
532
533 /* These are only used by DWP version 2 and version 5 files.
534 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
535 sections are referenced by section number, and are not recorded here.
536 In DWP version 2 or 5 there is at most one copy of all these sections,
537 each section being (effectively) comprised of the concatenation of all of
538 the individual sections that exist in the version 1 format.
539 To keep the code simple we treat each of these concatenated pieces as a
540 section itself (a virtual section?). */
541 struct dwarf2_section_info abbrev;
542 struct dwarf2_section_info info;
543 struct dwarf2_section_info line;
544 struct dwarf2_section_info loc;
545 struct dwarf2_section_info loclists;
546 struct dwarf2_section_info macinfo;
547 struct dwarf2_section_info macro;
548 struct dwarf2_section_info rnglists;
549 struct dwarf2_section_info str_offsets;
550 struct dwarf2_section_info types;
551 };
552
553 /* These sections are what may appear in a virtual DWO file in DWP version 1.
554 A virtual DWO file is a DWO file as it appears in a DWP file. */
555
556 struct virtual_v1_dwo_sections
557 {
558 struct dwarf2_section_info abbrev;
559 struct dwarf2_section_info line;
560 struct dwarf2_section_info loc;
561 struct dwarf2_section_info macinfo;
562 struct dwarf2_section_info macro;
563 struct dwarf2_section_info str_offsets;
564 /* Each DWP hash table entry records one CU or one TU.
565 That is recorded here, and copied to dwo_unit.section. */
566 struct dwarf2_section_info info_or_types;
567 };
568
569 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
570 In version 2, the sections of the DWO files are concatenated together
571 and stored in one section of that name. Thus each ELF section contains
572 several "virtual" sections. */
573
574 struct virtual_v2_or_v5_dwo_sections
575 {
576 bfd_size_type abbrev_offset;
577 bfd_size_type abbrev_size;
578
579 bfd_size_type line_offset;
580 bfd_size_type line_size;
581
582 bfd_size_type loc_offset;
583 bfd_size_type loc_size;
584
585 bfd_size_type loclists_offset;
586 bfd_size_type loclists_size;
587
588 bfd_size_type macinfo_offset;
589 bfd_size_type macinfo_size;
590
591 bfd_size_type macro_offset;
592 bfd_size_type macro_size;
593
594 bfd_size_type rnglists_offset;
595 bfd_size_type rnglists_size;
596
597 bfd_size_type str_offsets_offset;
598 bfd_size_type str_offsets_size;
599
600 /* Each DWP hash table entry records one CU or one TU.
601 That is recorded here, and copied to dwo_unit.section. */
602 bfd_size_type info_or_types_offset;
603 bfd_size_type info_or_types_size;
604 };
605
606 /* Contents of DWP hash tables. */
607
608 struct dwp_hash_table
609 {
610 uint32_t version, nr_columns;
611 uint32_t nr_units, nr_slots;
612 const gdb_byte *hash_table, *unit_table;
613 union
614 {
615 struct
616 {
617 const gdb_byte *indices;
618 } v1;
619 struct
620 {
621 /* This is indexed by column number and gives the id of the section
622 in that column. */
623 #define MAX_NR_V2_DWO_SECTIONS \
624 (1 /* .debug_info or .debug_types */ \
625 + 1 /* .debug_abbrev */ \
626 + 1 /* .debug_line */ \
627 + 1 /* .debug_loc */ \
628 + 1 /* .debug_str_offsets */ \
629 + 1 /* .debug_macro or .debug_macinfo */)
630 int section_ids[MAX_NR_V2_DWO_SECTIONS];
631 const gdb_byte *offsets;
632 const gdb_byte *sizes;
633 } v2;
634 struct
635 {
636 /* This is indexed by column number and gives the id of the section
637 in that column. */
638 #define MAX_NR_V5_DWO_SECTIONS \
639 (1 /* .debug_info */ \
640 + 1 /* .debug_abbrev */ \
641 + 1 /* .debug_line */ \
642 + 1 /* .debug_loclists */ \
643 + 1 /* .debug_str_offsets */ \
644 + 1 /* .debug_macro */ \
645 + 1 /* .debug_rnglists */)
646 int section_ids[MAX_NR_V5_DWO_SECTIONS];
647 const gdb_byte *offsets;
648 const gdb_byte *sizes;
649 } v5;
650 } section_pool;
651 };
652
653 /* Data for one DWP file. */
654
655 struct dwp_file
656 {
657 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
658 : name (name_),
659 dbfd (std::move (abfd))
660 {
661 }
662
663 /* Name of the file. */
664 const char *name;
665
666 /* File format version. */
667 int version = 0;
668
669 /* The bfd. */
670 gdb_bfd_ref_ptr dbfd;
671
672 /* Section info for this file. */
673 struct dwp_sections sections {};
674
675 /* Table of CUs in the file. */
676 const struct dwp_hash_table *cus = nullptr;
677
678 /* Table of TUs in the file. */
679 const struct dwp_hash_table *tus = nullptr;
680
681 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
682 htab_up loaded_cus;
683 htab_up loaded_tus;
684
685 /* Table to map ELF section numbers to their sections.
686 This is only needed for the DWP V1 file format. */
687 unsigned int num_sections = 0;
688 asection **elf_sections = nullptr;
689 };
690
691 /* Struct used to pass misc. parameters to read_die_and_children, et
692 al. which are used for both .debug_info and .debug_types dies.
693 All parameters here are unchanging for the life of the call. This
694 struct exists to abstract away the constant parameters of die reading. */
695
696 struct die_reader_specs
697 {
698 /* The bfd of die_section. */
699 bfd *abfd;
700
701 /* The CU of the DIE we are parsing. */
702 struct dwarf2_cu *cu;
703
704 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
705 struct dwo_file *dwo_file;
706
707 /* The section the die comes from.
708 This is either .debug_info or .debug_types, or the .dwo variants. */
709 struct dwarf2_section_info *die_section;
710
711 /* die_section->buffer. */
712 const gdb_byte *buffer;
713
714 /* The end of the buffer. */
715 const gdb_byte *buffer_end;
716
717 /* The abbreviation table to use when reading the DIEs. */
718 struct abbrev_table *abbrev_table;
719 };
720
721 /* A subclass of die_reader_specs that holds storage and has complex
722 constructor and destructor behavior. */
723
724 class cutu_reader : public die_reader_specs
725 {
726 public:
727
728 cutu_reader (dwarf2_per_cu_data *this_cu,
729 dwarf2_per_objfile *per_objfile,
730 struct abbrev_table *abbrev_table,
731 dwarf2_cu *existing_cu,
732 bool skip_partial,
733 abbrev_cache *cache = nullptr);
734
735 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
736 dwarf2_per_objfile *per_objfile,
737 struct dwarf2_cu *parent_cu = nullptr,
738 struct dwo_file *dwo_file = nullptr);
739
740 DISABLE_COPY_AND_ASSIGN (cutu_reader);
741
742 cutu_reader (cutu_reader &&) = default;
743
744 const gdb_byte *info_ptr = nullptr;
745 struct die_info *comp_unit_die = nullptr;
746 bool dummy_p = false;
747
748 /* Release the new CU, putting it on the chain. This cannot be done
749 for dummy CUs. */
750 void keep ();
751
752 /* Release the abbrev table, transferring ownership to the
753 caller. */
754 abbrev_table_up release_abbrev_table ()
755 {
756 return std::move (m_abbrev_table_holder);
757 }
758
759 private:
760 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
761 dwarf2_per_objfile *per_objfile,
762 dwarf2_cu *existing_cu);
763
764 struct dwarf2_per_cu_data *m_this_cu;
765 std::unique_ptr<dwarf2_cu> m_new_cu;
766
767 /* The ordinary abbreviation table. */
768 abbrev_table_up m_abbrev_table_holder;
769
770 /* The DWO abbreviation table. */
771 abbrev_table_up m_dwo_abbrev_table;
772 };
773
774 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
775 but this would require a corresponding change in unpack_field_as_long
776 and friends. */
777 static int bits_per_byte = 8;
778
779 struct variant_part_builder;
780
781 /* When reading a variant, we track a bit more information about the
782 field, and store it in an object of this type. */
783
784 struct variant_field
785 {
786 int first_field = -1;
787 int last_field = -1;
788
789 /* A variant can contain other variant parts. */
790 std::vector<variant_part_builder> variant_parts;
791
792 /* If we see a DW_TAG_variant, then this will be set if this is the
793 default branch. */
794 bool default_branch = false;
795 /* If we see a DW_AT_discr_value, then this will be the discriminant
796 value. */
797 ULONGEST discriminant_value = 0;
798 /* If we see a DW_AT_discr_list, then this is a pointer to the list
799 data. */
800 struct dwarf_block *discr_list_data = nullptr;
801 };
802
803 /* This represents a DW_TAG_variant_part. */
804
805 struct variant_part_builder
806 {
807 /* The offset of the discriminant field. */
808 sect_offset discriminant_offset {};
809
810 /* Variants that are direct children of this variant part. */
811 std::vector<variant_field> variants;
812
813 /* True if we're currently reading a variant. */
814 bool processing_variant = false;
815 };
816
817 struct nextfield
818 {
819 int accessibility = 0;
820 int virtuality = 0;
821 /* Variant parts need to find the discriminant, which is a DIE
822 reference. We track the section offset of each field to make
823 this link. */
824 sect_offset offset;
825 struct field field {};
826 };
827
828 struct fnfieldlist
829 {
830 const char *name = nullptr;
831 std::vector<struct fn_field> fnfields;
832 };
833
834 /* The routines that read and process dies for a C struct or C++ class
835 pass lists of data member fields and lists of member function fields
836 in an instance of a field_info structure, as defined below. */
837 struct field_info
838 {
839 /* List of data member and baseclasses fields. */
840 std::vector<struct nextfield> fields;
841 std::vector<struct nextfield> baseclasses;
842
843 /* Set if the accessibility of one of the fields is not public. */
844 bool non_public_fields = false;
845
846 /* Member function fieldlist array, contains name of possibly overloaded
847 member function, number of overloaded member functions and a pointer
848 to the head of the member function field chain. */
849 std::vector<struct fnfieldlist> fnfieldlists;
850
851 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
852 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
853 std::vector<struct decl_field> typedef_field_list;
854
855 /* Nested types defined by this class and the number of elements in this
856 list. */
857 std::vector<struct decl_field> nested_types_list;
858
859 /* If non-null, this is the variant part we are currently
860 reading. */
861 variant_part_builder *current_variant_part = nullptr;
862 /* This holds all the top-level variant parts attached to the type
863 we're reading. */
864 std::vector<variant_part_builder> variant_parts;
865
866 /* Return the total number of fields (including baseclasses). */
867 int nfields () const
868 {
869 return fields.size () + baseclasses.size ();
870 }
871 };
872
873 /* Loaded secondary compilation units are kept in memory until they
874 have not been referenced for the processing of this many
875 compilation units. Set this to zero to disable caching. Cache
876 sizes of up to at least twenty will improve startup time for
877 typical inter-CU-reference binaries, at an obvious memory cost. */
878 static int dwarf_max_cache_age = 5;
879 static void
880 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
881 struct cmd_list_element *c, const char *value)
882 {
883 gdb_printf (file, _("The upper bound on the age of cached "
884 "DWARF compilation units is %s.\n"),
885 value);
886 }
887 \f
888 /* local function prototypes */
889
890 static void dwarf2_find_base_address (struct die_info *die,
891 struct dwarf2_cu *cu);
892
893 class cooked_index_storage;
894 static void build_type_psymtabs_reader (cutu_reader *reader,
895 cooked_index_storage *storage);
896
897 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
898
899 static void var_decode_location (struct attribute *attr,
900 struct symbol *sym,
901 struct dwarf2_cu *cu);
902
903 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
904
905 static const gdb_byte *read_attribute (const struct die_reader_specs *,
906 struct attribute *,
907 const struct attr_abbrev *,
908 const gdb_byte *);
909
910 static void read_attribute_reprocess (const struct die_reader_specs *reader,
911 struct attribute *attr, dwarf_tag tag);
912
913 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
914
915 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
916 dwarf2_section_info *, sect_offset);
917
918 static const char *read_indirect_string
919 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
920 const struct comp_unit_head *, unsigned int *);
921
922 static const char *read_indirect_string_at_offset
923 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
924
925 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
926 const gdb_byte *,
927 unsigned int *);
928
929 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
930 ULONGEST str_index);
931
932 static const char *read_stub_str_index (struct dwarf2_cu *cu,
933 ULONGEST str_index);
934
935 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
936 struct dwarf2_cu *);
937
938 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
939 struct dwarf2_cu *cu);
940
941 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
942
943 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
944 struct dwarf2_cu *cu);
945
946 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
947
948 static struct die_info *die_specification (struct die_info *die,
949 struct dwarf2_cu **);
950
951 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
952 struct dwarf2_cu *cu,
953 const char *comp_dir);
954
955 static void dwarf_decode_lines (struct line_header *,
956 struct dwarf2_cu *,
957 CORE_ADDR, int decode_mapping);
958
959 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
960 const line_header &lh);
961
962 static struct symbol *new_symbol (struct die_info *, struct type *,
963 struct dwarf2_cu *, struct symbol * = NULL);
964
965 static void dwarf2_const_value (const struct attribute *, struct symbol *,
966 struct dwarf2_cu *);
967
968 static void dwarf2_const_value_attr (const struct attribute *attr,
969 struct type *type,
970 const char *name,
971 struct obstack *obstack,
972 struct dwarf2_cu *cu, LONGEST *value,
973 const gdb_byte **bytes,
974 struct dwarf2_locexpr_baton **baton);
975
976 static struct type *read_subrange_index_type (struct die_info *die,
977 struct dwarf2_cu *cu);
978
979 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
980
981 static int need_gnat_info (struct dwarf2_cu *);
982
983 static struct type *die_descriptive_type (struct die_info *,
984 struct dwarf2_cu *);
985
986 static void set_descriptive_type (struct type *, struct die_info *,
987 struct dwarf2_cu *);
988
989 static struct type *die_containing_type (struct die_info *,
990 struct dwarf2_cu *);
991
992 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
993 struct dwarf2_cu *);
994
995 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
996
997 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
998
999 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1000
1001 static char *typename_concat (struct obstack *obs, const char *prefix,
1002 const char *suffix, int physname,
1003 struct dwarf2_cu *cu);
1004
1005 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1006
1007 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1008
1009 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1010
1011 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1012
1013 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1014
1015 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1016
1017 /* Return the .debug_loclists section to use for cu. */
1018 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1019
1020 /* Return the .debug_rnglists section to use for cu. */
1021 static struct dwarf2_section_info *cu_debug_rnglists_section
1022 (struct dwarf2_cu *cu, dwarf_tag tag);
1023
1024 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1025 values. Keep the items ordered with increasing constraints compliance. */
1026 enum pc_bounds_kind
1027 {
1028 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1029 PC_BOUNDS_NOT_PRESENT,
1030
1031 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1032 were present but they do not form a valid range of PC addresses. */
1033 PC_BOUNDS_INVALID,
1034
1035 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1036 PC_BOUNDS_RANGES,
1037
1038 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1039 PC_BOUNDS_HIGH_LOW,
1040 };
1041
1042 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1043 CORE_ADDR *, CORE_ADDR *,
1044 struct dwarf2_cu *,
1045 addrmap *,
1046 void *);
1047
1048 static void get_scope_pc_bounds (struct die_info *,
1049 CORE_ADDR *, CORE_ADDR *,
1050 struct dwarf2_cu *);
1051
1052 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1053 CORE_ADDR, struct dwarf2_cu *);
1054
1055 static void dwarf2_add_field (struct field_info *, struct die_info *,
1056 struct dwarf2_cu *);
1057
1058 static void dwarf2_attach_fields_to_type (struct field_info *,
1059 struct type *, struct dwarf2_cu *);
1060
1061 static void dwarf2_add_member_fn (struct field_info *,
1062 struct die_info *, struct type *,
1063 struct dwarf2_cu *);
1064
1065 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1066 struct type *,
1067 struct dwarf2_cu *);
1068
1069 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1070
1071 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1072
1073 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1074
1075 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1076
1077 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1078
1079 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1080
1081 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1085
1086 static const char *namespace_name (struct die_info *die,
1087 int *is_anonymous, struct dwarf2_cu *);
1088
1089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1090
1091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1092 bool * = nullptr);
1093
1094 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1095 struct dwarf2_cu *);
1096
1097 static struct die_info *read_die_and_siblings_1
1098 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1099 struct die_info *);
1100
1101 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1102 const gdb_byte *info_ptr,
1103 const gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1105
1106 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1107 struct die_info **, const gdb_byte *,
1108 int);
1109
1110 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1111 struct die_info **, const gdb_byte *);
1112
1113 static void process_die (struct die_info *, struct dwarf2_cu *);
1114
1115 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1116 struct objfile *);
1117
1118 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1119
1120 static const char *dwarf2_full_name (const char *name,
1121 struct die_info *die,
1122 struct dwarf2_cu *cu);
1123
1124 static const char *dwarf2_physname (const char *name, struct die_info *die,
1125 struct dwarf2_cu *cu);
1126
1127 static struct die_info *dwarf2_extension (struct die_info *die,
1128 struct dwarf2_cu **);
1129
1130 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1131
1132 static void dump_die_for_error (struct die_info *);
1133
1134 static void dump_die_1 (struct ui_file *, int level, int max_level,
1135 struct die_info *);
1136
1137 /*static*/ void dump_die (struct die_info *, int max_level);
1138
1139 static void store_in_ref_table (struct die_info *,
1140 struct dwarf2_cu *);
1141
1142 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1143 const struct attribute *,
1144 struct dwarf2_cu **);
1145
1146 static struct die_info *follow_die_ref (struct die_info *,
1147 const struct attribute *,
1148 struct dwarf2_cu **);
1149
1150 static struct die_info *follow_die_sig (struct die_info *,
1151 const struct attribute *,
1152 struct dwarf2_cu **);
1153
1154 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1155 struct dwarf2_cu *);
1156
1157 static struct type *get_DW_AT_signature_type (struct die_info *,
1158 const struct attribute *,
1159 struct dwarf2_cu *);
1160
1161 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1162 dwarf2_per_objfile *per_objfile);
1163
1164 static void read_signatured_type (signatured_type *sig_type,
1165 dwarf2_per_objfile *per_objfile);
1166
1167 static int attr_to_dynamic_prop (const struct attribute *attr,
1168 struct die_info *die, struct dwarf2_cu *cu,
1169 struct dynamic_prop *prop, struct type *type);
1170
1171 /* memory allocation interface */
1172
1173 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1174
1175 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1176
1177 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1178
1179 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1180 struct dwarf2_loclist_baton *baton,
1181 const struct attribute *attr);
1182
1183 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1184 struct symbol *sym,
1185 struct dwarf2_cu *cu,
1186 int is_block);
1187
1188 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1189 const gdb_byte *info_ptr,
1190 const struct abbrev_info *abbrev,
1191 bool do_skip_children = true);
1192
1193 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1194 (sect_offset sect_off, unsigned int offset_in_dwz,
1195 dwarf2_per_bfd *per_bfd);
1196
1197 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1198 struct die_info *comp_unit_die,
1199 enum language pretend_language);
1200
1201 static struct type *set_die_type (struct die_info *, struct type *,
1202 struct dwarf2_cu *, bool = false);
1203
1204 static void create_all_units (dwarf2_per_objfile *per_objfile);
1205
1206 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1207 dwarf2_per_objfile *per_objfile,
1208 dwarf2_cu *existing_cu,
1209 bool skip_partial,
1210 enum language pretend_language);
1211
1212 static void process_full_comp_unit (dwarf2_cu *cu,
1213 enum language pretend_language);
1214
1215 static void process_full_type_unit (dwarf2_cu *cu,
1216 enum language pretend_language);
1217
1218 static struct type *get_die_type_at_offset (sect_offset,
1219 dwarf2_per_cu_data *per_cu,
1220 dwarf2_per_objfile *per_objfile);
1221
1222 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1223
1224 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1225 dwarf2_per_objfile *per_objfile,
1226 enum language pretend_language);
1227
1228 static void process_queue (dwarf2_per_objfile *per_objfile);
1229
1230 /* Class, the destructor of which frees all allocated queue entries. This
1231 will only have work to do if an error was thrown while processing the
1232 dwarf. If no error was thrown then the queue entries should have all
1233 been processed, and freed, as we went along. */
1234
1235 class dwarf2_queue_guard
1236 {
1237 public:
1238 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1239 : m_per_objfile (per_objfile)
1240 {
1241 gdb_assert (!m_per_objfile->queue.has_value ());
1242
1243 m_per_objfile->queue.emplace ();
1244 }
1245
1246 /* Free any entries remaining on the queue. There should only be
1247 entries left if we hit an error while processing the dwarf. */
1248 ~dwarf2_queue_guard ()
1249 {
1250 gdb_assert (m_per_objfile->queue.has_value ());
1251
1252 m_per_objfile->queue.reset ();
1253 }
1254
1255 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1256
1257 private:
1258 dwarf2_per_objfile *m_per_objfile;
1259 };
1260
1261 dwarf2_queue_item::~dwarf2_queue_item ()
1262 {
1263 /* Anything still marked queued is likely to be in an
1264 inconsistent state, so discard it. */
1265 if (per_cu->queued)
1266 {
1267 per_objfile->remove_cu (per_cu);
1268 per_cu->queued = 0;
1269 }
1270 }
1271
1272 /* See dwarf2/read.h. */
1273
1274 void
1275 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1276 {
1277 if (data->is_debug_types)
1278 delete static_cast<signatured_type *> (data);
1279 else
1280 delete data;
1281 }
1282
1283 static file_and_directory &find_file_and_directory
1284 (struct die_info *die, struct dwarf2_cu *cu);
1285
1286 static const char *compute_include_file_name
1287 (const struct line_header *lh,
1288 const file_entry &fe,
1289 const file_and_directory &cu_info,
1290 std::string &name_holder);
1291
1292 static htab_up allocate_signatured_type_table ();
1293
1294 static htab_up allocate_dwo_unit_table ();
1295
1296 static struct dwo_unit *lookup_dwo_unit_in_dwp
1297 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1298 const char *comp_dir, ULONGEST signature, int is_debug_types);
1299
1300 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1301
1302 static struct dwo_unit *lookup_dwo_comp_unit
1303 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1304 ULONGEST signature);
1305
1306 static struct dwo_unit *lookup_dwo_type_unit
1307 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1308
1309 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1310
1311 /* A unique pointer to a dwo_file. */
1312
1313 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1314
1315 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1316
1317 static void check_producer (struct dwarf2_cu *cu);
1318 \f
1319 /* Various complaints about symbol reading that don't abort the process. */
1320
1321 static void
1322 dwarf2_debug_line_missing_file_complaint (void)
1323 {
1324 complaint (_(".debug_line section has line data without a file"));
1325 }
1326
1327 static void
1328 dwarf2_debug_line_missing_end_sequence_complaint (void)
1329 {
1330 complaint (_(".debug_line section has line "
1331 "program sequence without an end"));
1332 }
1333
1334 static void
1335 dwarf2_complex_location_expr_complaint (void)
1336 {
1337 complaint (_("location expression too complex"));
1338 }
1339
1340 static void
1341 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1342 int arg3)
1343 {
1344 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1345 arg1, arg2, arg3);
1346 }
1347
1348 static void
1349 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1350 {
1351 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1352 arg1, arg2);
1353 }
1354
1355 /* Hash function for line_header_hash. */
1356
1357 static hashval_t
1358 line_header_hash (const struct line_header *ofs)
1359 {
1360 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1361 }
1362
1363 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1364
1365 static hashval_t
1366 line_header_hash_voidp (const void *item)
1367 {
1368 const struct line_header *ofs = (const struct line_header *) item;
1369
1370 return line_header_hash (ofs);
1371 }
1372
1373 /* Equality function for line_header_hash. */
1374
1375 static int
1376 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1377 {
1378 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1379 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1380
1381 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1382 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1383 }
1384
1385 \f
1386
1387 /* An iterator for all_units that is based on index. This
1388 approach makes it possible to iterate over all_units safely,
1389 when some caller in the loop may add new units. */
1390
1391 class all_units_iterator
1392 {
1393 public:
1394
1395 all_units_iterator (dwarf2_per_bfd *per_bfd, bool start)
1396 : m_per_bfd (per_bfd),
1397 m_index (start ? 0 : per_bfd->all_units.size ())
1398 {
1399 }
1400
1401 all_units_iterator &operator++ ()
1402 {
1403 ++m_index;
1404 return *this;
1405 }
1406
1407 dwarf2_per_cu_data *operator* () const
1408 {
1409 return m_per_bfd->get_cu (m_index);
1410 }
1411
1412 bool operator== (const all_units_iterator &other) const
1413 {
1414 return m_index == other.m_index;
1415 }
1416
1417
1418 bool operator!= (const all_units_iterator &other) const
1419 {
1420 return m_index != other.m_index;
1421 }
1422
1423 private:
1424
1425 dwarf2_per_bfd *m_per_bfd;
1426 size_t m_index;
1427 };
1428
1429 /* A range adapter for the all_units_iterator. */
1430 class all_units_range
1431 {
1432 public:
1433
1434 all_units_range (dwarf2_per_bfd *per_bfd)
1435 : m_per_bfd (per_bfd)
1436 {
1437 }
1438
1439 all_units_iterator begin ()
1440 {
1441 return all_units_iterator (m_per_bfd, true);
1442 }
1443
1444 all_units_iterator end ()
1445 {
1446 return all_units_iterator (m_per_bfd, false);
1447 }
1448
1449 private:
1450
1451 dwarf2_per_bfd *m_per_bfd;
1452 };
1453
1454 /* See declaration. */
1455
1456 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1457 bool can_copy_)
1458 : obfd (obfd),
1459 can_copy (can_copy_)
1460 {
1461 if (names == NULL)
1462 names = &dwarf2_elf_names;
1463
1464 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1465 locate_sections (obfd, sec, *names);
1466 }
1467
1468 dwarf2_per_bfd::~dwarf2_per_bfd ()
1469 {
1470 for (auto &per_cu : all_units)
1471 {
1472 per_cu->imported_symtabs_free ();
1473 per_cu->free_cached_file_names ();
1474 }
1475
1476 /* Everything else should be on this->obstack. */
1477 }
1478
1479 /* See read.h. */
1480
1481 void
1482 dwarf2_per_objfile::remove_all_cus ()
1483 {
1484 gdb_assert (!queue.has_value ());
1485
1486 m_dwarf2_cus.clear ();
1487 }
1488
1489 /* A helper class that calls free_cached_comp_units on
1490 destruction. */
1491
1492 class free_cached_comp_units
1493 {
1494 public:
1495
1496 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1497 : m_per_objfile (per_objfile)
1498 {
1499 }
1500
1501 ~free_cached_comp_units ()
1502 {
1503 m_per_objfile->remove_all_cus ();
1504 }
1505
1506 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1507
1508 private:
1509
1510 dwarf2_per_objfile *m_per_objfile;
1511 };
1512
1513 /* See read.h. */
1514
1515 bool
1516 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1517 {
1518 if (per_cu->index < this->m_symtabs.size ())
1519 return this->m_symtabs[per_cu->index] != nullptr;
1520 return false;
1521 }
1522
1523 /* See read.h. */
1524
1525 compunit_symtab *
1526 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1527 {
1528 if (per_cu->index < this->m_symtabs.size ())
1529 return this->m_symtabs[per_cu->index];
1530 return nullptr;
1531 }
1532
1533 /* See read.h. */
1534
1535 void
1536 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1537 compunit_symtab *symtab)
1538 {
1539 if (per_cu->index >= this->m_symtabs.size ())
1540 this->m_symtabs.resize (per_cu->index + 1);
1541 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1542 this->m_symtabs[per_cu->index] = symtab;
1543 }
1544
1545 /* Try to locate the sections we need for DWARF 2 debugging
1546 information and return true if we have enough to do something.
1547 NAMES points to the dwarf2 section names, or is NULL if the standard
1548 ELF names are used. CAN_COPY is true for formats where symbol
1549 interposition is possible and so symbol values must follow copy
1550 relocation rules. */
1551
1552 bool
1553 dwarf2_has_info (struct objfile *objfile,
1554 const struct dwarf2_debug_sections *names,
1555 bool can_copy)
1556 {
1557 if (objfile->flags & OBJF_READNEVER)
1558 return false;
1559
1560 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1561
1562 if (per_objfile == NULL)
1563 {
1564 dwarf2_per_bfd *per_bfd;
1565
1566 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1567 BFD doesn't require relocations.
1568
1569 We don't share with objfiles for which -readnow was requested,
1570 because it would complicate things when loading the same BFD with
1571 -readnow and then without -readnow. */
1572 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1573 && (objfile->flags & OBJF_READNOW) == 0)
1574 {
1575 /* See if one has been created for this BFD yet. */
1576 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1577
1578 if (per_bfd == nullptr)
1579 {
1580 /* No, create it now. */
1581 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1582 can_copy);
1583 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1584 }
1585 }
1586 else
1587 {
1588 /* No sharing possible, create one specifically for this objfile. */
1589 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1590 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1591 }
1592
1593 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1594 }
1595
1596 return (!per_objfile->per_bfd->info.is_virtual
1597 && per_objfile->per_bfd->info.s.section != NULL
1598 && !per_objfile->per_bfd->abbrev.is_virtual
1599 && per_objfile->per_bfd->abbrev.s.section != NULL);
1600 }
1601
1602 /* See declaration. */
1603
1604 void
1605 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1606 const dwarf2_debug_sections &names)
1607 {
1608 flagword aflag = bfd_section_flags (sectp);
1609
1610 if ((aflag & SEC_HAS_CONTENTS) == 0)
1611 {
1612 }
1613 else if (elf_section_data (sectp)->this_hdr.sh_size
1614 > bfd_get_file_size (abfd))
1615 {
1616 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1617 warning (_("Discarding section %s which has a section size (%s"
1618 ") larger than the file size [in module %s]"),
1619 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1620 bfd_get_filename (abfd));
1621 }
1622 else if (names.info.matches (sectp->name))
1623 {
1624 this->info.s.section = sectp;
1625 this->info.size = bfd_section_size (sectp);
1626 }
1627 else if (names.abbrev.matches (sectp->name))
1628 {
1629 this->abbrev.s.section = sectp;
1630 this->abbrev.size = bfd_section_size (sectp);
1631 }
1632 else if (names.line.matches (sectp->name))
1633 {
1634 this->line.s.section = sectp;
1635 this->line.size = bfd_section_size (sectp);
1636 }
1637 else if (names.loc.matches (sectp->name))
1638 {
1639 this->loc.s.section = sectp;
1640 this->loc.size = bfd_section_size (sectp);
1641 }
1642 else if (names.loclists.matches (sectp->name))
1643 {
1644 this->loclists.s.section = sectp;
1645 this->loclists.size = bfd_section_size (sectp);
1646 }
1647 else if (names.macinfo.matches (sectp->name))
1648 {
1649 this->macinfo.s.section = sectp;
1650 this->macinfo.size = bfd_section_size (sectp);
1651 }
1652 else if (names.macro.matches (sectp->name))
1653 {
1654 this->macro.s.section = sectp;
1655 this->macro.size = bfd_section_size (sectp);
1656 }
1657 else if (names.str.matches (sectp->name))
1658 {
1659 this->str.s.section = sectp;
1660 this->str.size = bfd_section_size (sectp);
1661 }
1662 else if (names.str_offsets.matches (sectp->name))
1663 {
1664 this->str_offsets.s.section = sectp;
1665 this->str_offsets.size = bfd_section_size (sectp);
1666 }
1667 else if (names.line_str.matches (sectp->name))
1668 {
1669 this->line_str.s.section = sectp;
1670 this->line_str.size = bfd_section_size (sectp);
1671 }
1672 else if (names.addr.matches (sectp->name))
1673 {
1674 this->addr.s.section = sectp;
1675 this->addr.size = bfd_section_size (sectp);
1676 }
1677 else if (names.frame.matches (sectp->name))
1678 {
1679 this->frame.s.section = sectp;
1680 this->frame.size = bfd_section_size (sectp);
1681 }
1682 else if (names.eh_frame.matches (sectp->name))
1683 {
1684 this->eh_frame.s.section = sectp;
1685 this->eh_frame.size = bfd_section_size (sectp);
1686 }
1687 else if (names.ranges.matches (sectp->name))
1688 {
1689 this->ranges.s.section = sectp;
1690 this->ranges.size = bfd_section_size (sectp);
1691 }
1692 else if (names.rnglists.matches (sectp->name))
1693 {
1694 this->rnglists.s.section = sectp;
1695 this->rnglists.size = bfd_section_size (sectp);
1696 }
1697 else if (names.types.matches (sectp->name))
1698 {
1699 struct dwarf2_section_info type_section;
1700
1701 memset (&type_section, 0, sizeof (type_section));
1702 type_section.s.section = sectp;
1703 type_section.size = bfd_section_size (sectp);
1704
1705 this->types.push_back (type_section);
1706 }
1707 else if (names.gdb_index.matches (sectp->name))
1708 {
1709 this->gdb_index.s.section = sectp;
1710 this->gdb_index.size = bfd_section_size (sectp);
1711 }
1712 else if (names.debug_names.matches (sectp->name))
1713 {
1714 this->debug_names.s.section = sectp;
1715 this->debug_names.size = bfd_section_size (sectp);
1716 }
1717 else if (names.debug_aranges.matches (sectp->name))
1718 {
1719 this->debug_aranges.s.section = sectp;
1720 this->debug_aranges.size = bfd_section_size (sectp);
1721 }
1722
1723 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1724 && bfd_section_vma (sectp) == 0)
1725 this->has_section_at_zero = true;
1726 }
1727
1728 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1729 SECTION_NAME. */
1730
1731 void
1732 dwarf2_get_section_info (struct objfile *objfile,
1733 enum dwarf2_section_enum sect,
1734 asection **sectp, const gdb_byte **bufp,
1735 bfd_size_type *sizep)
1736 {
1737 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1738 struct dwarf2_section_info *info;
1739
1740 /* We may see an objfile without any DWARF, in which case we just
1741 return nothing. */
1742 if (per_objfile == NULL)
1743 {
1744 *sectp = NULL;
1745 *bufp = NULL;
1746 *sizep = 0;
1747 return;
1748 }
1749 switch (sect)
1750 {
1751 case DWARF2_DEBUG_FRAME:
1752 info = &per_objfile->per_bfd->frame;
1753 break;
1754 case DWARF2_EH_FRAME:
1755 info = &per_objfile->per_bfd->eh_frame;
1756 break;
1757 default:
1758 gdb_assert_not_reached ("unexpected section");
1759 }
1760
1761 info->read (objfile);
1762
1763 *sectp = info->get_bfd_section ();
1764 *bufp = info->buffer;
1765 *sizep = info->size;
1766 }
1767
1768 /* See dwarf2/read.h. */
1769
1770 void
1771 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1772 {
1773 info.read (objfile);
1774 abbrev.read (objfile);
1775 line.read (objfile);
1776 str.read (objfile);
1777 str_offsets.read (objfile);
1778 line_str.read (objfile);
1779 ranges.read (objfile);
1780 rnglists.read (objfile);
1781 addr.read (objfile);
1782
1783 for (auto &section : types)
1784 section.read (objfile);
1785 }
1786
1787 \f
1788 /* DWARF quick_symbol_functions support. */
1789
1790 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1791 unique line tables, so we maintain a separate table of all .debug_line
1792 derived entries to support the sharing.
1793 All the quick functions need is the list of file names. We discard the
1794 line_header when we're done and don't need to record it here. */
1795 struct quick_file_names
1796 {
1797 /* The data used to construct the hash key. */
1798 struct stmt_list_hash hash;
1799
1800 /* The number of entries in file_names, real_names. */
1801 unsigned int num_file_names;
1802
1803 /* The CU directory, as given by DW_AT_comp_dir. May be
1804 nullptr. */
1805 const char *comp_dir;
1806
1807 /* The file names from the line table, after being run through
1808 file_full_name. */
1809 const char **file_names;
1810
1811 /* The file names from the line table after being run through
1812 gdb_realpath. These are computed lazily. */
1813 const char **real_names;
1814 };
1815
1816 struct dwarf2_base_index_functions : public quick_symbol_functions
1817 {
1818 bool has_symbols (struct objfile *objfile) override;
1819
1820 bool has_unexpanded_symtabs (struct objfile *objfile) override;
1821
1822 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
1823
1824 void forget_cached_source_info (struct objfile *objfile) override;
1825
1826 enum language lookup_global_symbol_language (struct objfile *objfile,
1827 const char *name,
1828 domain_enum domain,
1829 bool *symbol_found_p) override
1830 {
1831 *symbol_found_p = false;
1832 return language_unknown;
1833 }
1834
1835 void print_stats (struct objfile *objfile, bool print_bcache) override;
1836
1837 void expand_all_symtabs (struct objfile *objfile) override;
1838
1839 /* A helper function that finds the per-cu object from an "adjusted"
1840 PC -- a PC with the base text offset removed. */
1841 virtual dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
1842 CORE_ADDR adjusted_pc);
1843
1844 struct compunit_symtab *find_pc_sect_compunit_symtab
1845 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
1846 CORE_ADDR pc, struct obj_section *section, int warn_if_readin)
1847 override final;
1848
1849 struct compunit_symtab *find_compunit_symtab_by_address
1850 (struct objfile *objfile, CORE_ADDR address) override
1851 {
1852 return nullptr;
1853 }
1854
1855 void map_symbol_filenames (struct objfile *objfile,
1856 gdb::function_view<symbol_filename_ftype> fun,
1857 bool need_fullname) override;
1858 };
1859
1860 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1861 It's handy in this case to have an empty implementation of the
1862 quick symbol functions, to avoid special cases in the rest of the
1863 code. */
1864
1865 struct readnow_functions : public dwarf2_base_index_functions
1866 {
1867 void dump (struct objfile *objfile) override
1868 {
1869 }
1870
1871 void expand_matching_symbols
1872 (struct objfile *,
1873 const lookup_name_info &lookup_name,
1874 domain_enum domain,
1875 int global,
1876 symbol_compare_ftype *ordered_compare) override
1877 {
1878 }
1879
1880 bool expand_symtabs_matching
1881 (struct objfile *objfile,
1882 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1883 const lookup_name_info *lookup_name,
1884 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1885 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1886 block_search_flags search_flags,
1887 domain_enum domain,
1888 enum search_domain kind) override
1889 {
1890 return true;
1891 }
1892 };
1893
1894 struct dwarf2_gdb_index : public dwarf2_base_index_functions
1895 {
1896 void dump (struct objfile *objfile) override;
1897
1898 void expand_matching_symbols
1899 (struct objfile *,
1900 const lookup_name_info &lookup_name,
1901 domain_enum domain,
1902 int global,
1903 symbol_compare_ftype *ordered_compare) override;
1904
1905 bool expand_symtabs_matching
1906 (struct objfile *objfile,
1907 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1908 const lookup_name_info *lookup_name,
1909 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1910 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1911 block_search_flags search_flags,
1912 domain_enum domain,
1913 enum search_domain kind) override;
1914 };
1915
1916 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
1917 {
1918 void dump (struct objfile *objfile) override;
1919
1920 void expand_matching_symbols
1921 (struct objfile *,
1922 const lookup_name_info &lookup_name,
1923 domain_enum domain,
1924 int global,
1925 symbol_compare_ftype *ordered_compare) override;
1926
1927 bool expand_symtabs_matching
1928 (struct objfile *objfile,
1929 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1930 const lookup_name_info *lookup_name,
1931 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1932 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1933 block_search_flags search_flags,
1934 domain_enum domain,
1935 enum search_domain kind) override;
1936 };
1937
1938 quick_symbol_functions_up
1939 mapped_index::make_quick_functions () const
1940 {
1941 return quick_symbol_functions_up (new dwarf2_gdb_index);
1942 }
1943
1944 quick_symbol_functions_up
1945 mapped_debug_names::make_quick_functions () const
1946 {
1947 return quick_symbol_functions_up (new dwarf2_debug_names_index);
1948 }
1949
1950 /* Utility hash function for a stmt_list_hash. */
1951
1952 static hashval_t
1953 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1954 {
1955 hashval_t v = 0;
1956
1957 if (stmt_list_hash->dwo_unit != NULL)
1958 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1959 v += to_underlying (stmt_list_hash->line_sect_off);
1960 return v;
1961 }
1962
1963 /* Utility equality function for a stmt_list_hash. */
1964
1965 static int
1966 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1967 const struct stmt_list_hash *rhs)
1968 {
1969 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1970 return 0;
1971 if (lhs->dwo_unit != NULL
1972 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1973 return 0;
1974
1975 return lhs->line_sect_off == rhs->line_sect_off;
1976 }
1977
1978 /* Hash function for a quick_file_names. */
1979
1980 static hashval_t
1981 hash_file_name_entry (const void *e)
1982 {
1983 const struct quick_file_names *file_data
1984 = (const struct quick_file_names *) e;
1985
1986 return hash_stmt_list_entry (&file_data->hash);
1987 }
1988
1989 /* Equality function for a quick_file_names. */
1990
1991 static int
1992 eq_file_name_entry (const void *a, const void *b)
1993 {
1994 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1995 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1996
1997 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1998 }
1999
2000 /* Create a quick_file_names hash table. */
2001
2002 static htab_up
2003 create_quick_file_names_table (unsigned int nr_initial_entries)
2004 {
2005 return htab_up (htab_create_alloc (nr_initial_entries,
2006 hash_file_name_entry, eq_file_name_entry,
2007 nullptr, xcalloc, xfree));
2008 }
2009
2010 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2011 function is unrelated to symtabs, symtab would have to be created afterwards.
2012 You should call age_cached_comp_units after processing the CU. */
2013
2014 static dwarf2_cu *
2015 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2016 bool skip_partial)
2017 {
2018 if (per_cu->is_debug_types)
2019 load_full_type_unit (per_cu, per_objfile);
2020 else
2021 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2022 skip_partial, language_minimal);
2023
2024 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2025 if (cu == nullptr)
2026 return nullptr; /* Dummy CU. */
2027
2028 dwarf2_find_base_address (cu->dies, cu);
2029
2030 return cu;
2031 }
2032
2033 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2034
2035 static void
2036 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2037 dwarf2_per_objfile *per_objfile, bool skip_partial)
2038 {
2039 {
2040 /* The destructor of dwarf2_queue_guard frees any entries left on
2041 the queue. After this point we're guaranteed to leave this function
2042 with the dwarf queue empty. */
2043 dwarf2_queue_guard q_guard (per_objfile);
2044
2045 if (!per_objfile->symtab_set_p (per_cu))
2046 {
2047 queue_comp_unit (per_cu, per_objfile, language_minimal);
2048 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2049
2050 /* If we just loaded a CU from a DWO, and we're working with an index
2051 that may badly handle TUs, load all the TUs in that DWO as well.
2052 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2053 if (!per_cu->is_debug_types
2054 && cu != NULL
2055 && cu->dwo_unit != NULL
2056 && per_objfile->per_bfd->index_table != NULL
2057 && !per_objfile->per_bfd->index_table->version_check ()
2058 /* DWP files aren't supported yet. */
2059 && get_dwp_file (per_objfile) == NULL)
2060 queue_and_load_all_dwo_tus (cu);
2061 }
2062
2063 process_queue (per_objfile);
2064 }
2065
2066 /* Age the cache, releasing compilation units that have not
2067 been used recently. */
2068 per_objfile->age_comp_units ();
2069 }
2070
2071 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2072 the per-objfile for which this symtab is instantiated.
2073
2074 Returns the resulting symbol table. */
2075
2076 static struct compunit_symtab *
2077 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2078 dwarf2_per_objfile *per_objfile,
2079 bool skip_partial)
2080 {
2081 if (!per_objfile->symtab_set_p (per_cu))
2082 {
2083 free_cached_comp_units freer (per_objfile);
2084 scoped_restore decrementer = increment_reading_symtab ();
2085 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2086 process_cu_includes (per_objfile);
2087 }
2088
2089 return per_objfile->get_symtab (per_cu);
2090 }
2091
2092 /* See read.h. */
2093
2094 dwarf2_per_cu_data_up
2095 dwarf2_per_bfd::allocate_per_cu ()
2096 {
2097 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
2098 result->per_bfd = this;
2099 result->index = all_units.size ();
2100 return result;
2101 }
2102
2103 /* See read.h. */
2104
2105 signatured_type_up
2106 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
2107 {
2108 signatured_type_up result (new signatured_type (signature));
2109 result->per_bfd = this;
2110 result->index = all_units.size ();
2111 result->is_debug_types = true;
2112 tu_stats.nr_tus++;
2113 return result;
2114 }
2115
2116 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2117 obstack, and constructed with the specified field values. */
2118
2119 static dwarf2_per_cu_data_up
2120 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2121 struct dwarf2_section_info *section,
2122 int is_dwz,
2123 sect_offset sect_off, ULONGEST length)
2124 {
2125 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
2126 the_cu->sect_off = sect_off;
2127 the_cu->set_length (length);
2128 the_cu->section = section;
2129 the_cu->is_dwz = is_dwz;
2130 return the_cu;
2131 }
2132
2133 /* A helper for create_cus_from_index that handles a given list of
2134 CUs. */
2135
2136 static void
2137 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2138 const gdb_byte *cu_list, offset_type n_elements,
2139 struct dwarf2_section_info *section,
2140 int is_dwz)
2141 {
2142 for (offset_type i = 0; i < n_elements; i += 2)
2143 {
2144 gdb_static_assert (sizeof (ULONGEST) >= 8);
2145
2146 sect_offset sect_off
2147 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2148 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2149 cu_list += 2 * 8;
2150
2151 dwarf2_per_cu_data_up per_cu
2152 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2153 length);
2154 per_bfd->all_units.push_back (std::move (per_cu));
2155 }
2156 }
2157
2158 /* Read the CU list from the mapped index, and use it to create all
2159 the CU objects for PER_BFD. */
2160
2161 static void
2162 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2163 const gdb_byte *cu_list, offset_type cu_list_elements,
2164 const gdb_byte *dwz_list, offset_type dwz_elements)
2165 {
2166 gdb_assert (per_bfd->all_units.empty ());
2167 per_bfd->all_units.reserve ((cu_list_elements + dwz_elements) / 2);
2168
2169 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2170 &per_bfd->info, 0);
2171
2172 if (dwz_elements == 0)
2173 return;
2174
2175 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2176 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2177 &dwz->info, 1);
2178 }
2179
2180 /* Create the signatured type hash table from the index. */
2181
2182 static void
2183 create_signatured_type_table_from_index
2184 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2185 const gdb_byte *bytes, offset_type elements)
2186 {
2187 htab_up sig_types_hash = allocate_signatured_type_table ();
2188
2189 for (offset_type i = 0; i < elements; i += 3)
2190 {
2191 signatured_type_up sig_type;
2192 ULONGEST signature;
2193 void **slot;
2194 cu_offset type_offset_in_tu;
2195
2196 gdb_static_assert (sizeof (ULONGEST) >= 8);
2197 sect_offset sect_off
2198 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2199 type_offset_in_tu
2200 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2201 BFD_ENDIAN_LITTLE);
2202 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2203 bytes += 3 * 8;
2204
2205 sig_type = per_bfd->allocate_signatured_type (signature);
2206 sig_type->type_offset_in_tu = type_offset_in_tu;
2207 sig_type->section = section;
2208 sig_type->sect_off = sect_off;
2209
2210 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2211 *slot = sig_type.get ();
2212
2213 per_bfd->all_units.emplace_back (sig_type.release ());
2214 }
2215
2216 per_bfd->signatured_types = std::move (sig_types_hash);
2217 }
2218
2219 /* Create the signatured type hash table from .debug_names. */
2220
2221 static void
2222 create_signatured_type_table_from_debug_names
2223 (dwarf2_per_objfile *per_objfile,
2224 const mapped_debug_names &map,
2225 struct dwarf2_section_info *section,
2226 struct dwarf2_section_info *abbrev_section)
2227 {
2228 struct objfile *objfile = per_objfile->objfile;
2229
2230 section->read (objfile);
2231 abbrev_section->read (objfile);
2232
2233 htab_up sig_types_hash = allocate_signatured_type_table ();
2234
2235 for (uint32_t i = 0; i < map.tu_count; ++i)
2236 {
2237 signatured_type_up sig_type;
2238 void **slot;
2239
2240 sect_offset sect_off
2241 = (sect_offset) (extract_unsigned_integer
2242 (map.tu_table_reordered + i * map.offset_size,
2243 map.offset_size,
2244 map.dwarf5_byte_order));
2245
2246 comp_unit_head cu_header;
2247 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2248 abbrev_section,
2249 section->buffer + to_underlying (sect_off),
2250 rcuh_kind::TYPE);
2251
2252 sig_type = per_objfile->per_bfd->allocate_signatured_type
2253 (cu_header.signature);
2254 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2255 sig_type->section = section;
2256 sig_type->sect_off = sect_off;
2257
2258 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2259 *slot = sig_type.get ();
2260
2261 per_objfile->per_bfd->all_units.emplace_back (sig_type.release ());
2262 }
2263
2264 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2265 }
2266
2267 /* Read the address map data from the mapped index, and use it to
2268 populate the index_addrmap. */
2269
2270 static void
2271 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2272 struct mapped_index *index)
2273 {
2274 struct objfile *objfile = per_objfile->objfile;
2275 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2276 struct gdbarch *gdbarch = objfile->arch ();
2277 const gdb_byte *iter, *end;
2278 CORE_ADDR baseaddr;
2279
2280 addrmap_mutable mutable_map;
2281
2282 iter = index->address_table.data ();
2283 end = iter + index->address_table.size ();
2284
2285 baseaddr = objfile->text_section_offset ();
2286
2287 while (iter < end)
2288 {
2289 ULONGEST hi, lo, cu_index;
2290 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2291 iter += 8;
2292 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2293 iter += 8;
2294 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2295 iter += 4;
2296
2297 if (lo > hi)
2298 {
2299 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2300 hex_string (lo), hex_string (hi));
2301 continue;
2302 }
2303
2304 if (cu_index >= per_bfd->all_units.size ())
2305 {
2306 complaint (_(".gdb_index address table has invalid CU number %u"),
2307 (unsigned) cu_index);
2308 continue;
2309 }
2310
2311 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2312 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2313 mutable_map.set_empty (lo, hi - 1, per_bfd->get_cu (cu_index));
2314 }
2315
2316 per_bfd->index_addrmap
2317 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack, &mutable_map);
2318 }
2319
2320 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2321 to populate given addrmap. Returns true on success, false on
2322 failure. */
2323
2324 static bool
2325 read_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2326 struct dwarf2_section_info *section,
2327 addrmap *mutable_map)
2328 {
2329 struct objfile *objfile = per_objfile->objfile;
2330 bfd *abfd = objfile->obfd.get ();
2331 struct gdbarch *gdbarch = objfile->arch ();
2332 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2333 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2334
2335 std::unordered_map<sect_offset,
2336 dwarf2_per_cu_data *,
2337 gdb::hash_enum<sect_offset>>
2338 debug_info_offset_to_per_cu;
2339 for (const auto &per_cu : per_bfd->all_units)
2340 {
2341 /* A TU will not need aranges, and skipping them here is an easy
2342 way of ignoring .debug_types -- and possibly seeing a
2343 duplicate section offset -- entirely. The same applies to
2344 units coming from a dwz file. */
2345 if (per_cu->is_debug_types || per_cu->is_dwz)
2346 continue;
2347
2348 const auto insertpair
2349 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2350 per_cu.get ());
2351
2352 /* Assume no duplicate offsets in all_units. */
2353 gdb_assert (insertpair.second);
2354 }
2355
2356 std::set<sect_offset> debug_info_offset_seen;
2357
2358 section->read (objfile);
2359
2360 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2361
2362 const gdb_byte *addr = section->buffer;
2363
2364 while (addr < section->buffer + section->size)
2365 {
2366 const gdb_byte *const entry_addr = addr;
2367 unsigned int bytes_read;
2368
2369 const LONGEST entry_length = read_initial_length (abfd, addr,
2370 &bytes_read);
2371 addr += bytes_read;
2372
2373 const gdb_byte *const entry_end = addr + entry_length;
2374 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2375 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2376 if (addr + entry_length > section->buffer + section->size)
2377 {
2378 warning (_("Section .debug_aranges in %s entry at offset %s "
2379 "length %s exceeds section length %s, "
2380 "ignoring .debug_aranges."),
2381 objfile_name (objfile),
2382 plongest (entry_addr - section->buffer),
2383 plongest (bytes_read + entry_length),
2384 pulongest (section->size));
2385 return false;
2386 }
2387
2388 /* The version number. */
2389 const uint16_t version = read_2_bytes (abfd, addr);
2390 addr += 2;
2391 if (version != 2)
2392 {
2393 warning (_("Section .debug_aranges in %s entry at offset %s "
2394 "has unsupported version %d, ignoring .debug_aranges."),
2395 objfile_name (objfile),
2396 plongest (entry_addr - section->buffer), version);
2397 return false;
2398 }
2399
2400 const uint64_t debug_info_offset
2401 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2402 addr += offset_size;
2403 const auto per_cu_it
2404 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2405 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2406 {
2407 warning (_("Section .debug_aranges in %s entry at offset %s "
2408 "debug_info_offset %s does not exists, "
2409 "ignoring .debug_aranges."),
2410 objfile_name (objfile),
2411 plongest (entry_addr - section->buffer),
2412 pulongest (debug_info_offset));
2413 return false;
2414 }
2415 const auto insertpair
2416 = debug_info_offset_seen.insert (sect_offset (debug_info_offset));
2417 if (!insertpair.second)
2418 {
2419 warning (_("Section .debug_aranges in %s has duplicate "
2420 "debug_info_offset %s, ignoring .debug_aranges."),
2421 objfile_name (objfile),
2422 sect_offset_str (sect_offset (debug_info_offset)));
2423 return false;
2424 }
2425 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2426
2427 const uint8_t address_size = *addr++;
2428 if (address_size < 1 || address_size > 8)
2429 {
2430 warning (_("Section .debug_aranges in %s entry at offset %s "
2431 "address_size %u is invalid, ignoring .debug_aranges."),
2432 objfile_name (objfile),
2433 plongest (entry_addr - section->buffer), address_size);
2434 return false;
2435 }
2436
2437 const uint8_t segment_selector_size = *addr++;
2438 if (segment_selector_size != 0)
2439 {
2440 warning (_("Section .debug_aranges in %s entry at offset %s "
2441 "segment_selector_size %u is not supported, "
2442 "ignoring .debug_aranges."),
2443 objfile_name (objfile),
2444 plongest (entry_addr - section->buffer),
2445 segment_selector_size);
2446 return false;
2447 }
2448
2449 /* Must pad to an alignment boundary that is twice the address
2450 size. It is undocumented by the DWARF standard but GCC does
2451 use it. However, not every compiler does this. We can see
2452 whether it has happened by looking at the total length of the
2453 contents of the aranges for this CU -- it if isn't a multiple
2454 of twice the address size, then we skip any leftover
2455 bytes. */
2456 addr += (entry_end - addr) % (2 * address_size);
2457
2458 while (addr < entry_end)
2459 {
2460 if (addr + 2 * address_size > entry_end)
2461 {
2462 warning (_("Section .debug_aranges in %s entry at offset %s "
2463 "address list is not properly terminated, "
2464 "ignoring .debug_aranges."),
2465 objfile_name (objfile),
2466 plongest (entry_addr - section->buffer));
2467 return false;
2468 }
2469 ULONGEST start = extract_unsigned_integer (addr, address_size,
2470 dwarf5_byte_order);
2471 addr += address_size;
2472 ULONGEST length = extract_unsigned_integer (addr, address_size,
2473 dwarf5_byte_order);
2474 addr += address_size;
2475 if (start == 0 && length == 0)
2476 {
2477 /* This can happen on some targets with --gc-sections.
2478 This pair of values is also used to mark the end of
2479 the entries for a given CU, but we ignore it and
2480 instead handle termination using the check at the top
2481 of the loop. */
2482 continue;
2483 }
2484 if (start == 0 && !per_bfd->has_section_at_zero)
2485 {
2486 /* Symbol was eliminated due to a COMDAT group. */
2487 continue;
2488 }
2489 ULONGEST end = start + length;
2490 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2491 - baseaddr);
2492 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2493 - baseaddr);
2494 mutable_map->set_empty (start, end - 1, per_cu);
2495 }
2496
2497 per_cu->addresses_seen = true;
2498 }
2499
2500 return true;
2501 }
2502
2503 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2504 populate the index_addrmap. */
2505
2506 static void
2507 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2508 struct dwarf2_section_info *section)
2509 {
2510 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2511
2512 addrmap_mutable mutable_map;
2513
2514 if (read_addrmap_from_aranges (per_objfile, section, &mutable_map))
2515 per_bfd->index_addrmap
2516 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack,
2517 &mutable_map);
2518 }
2519
2520 /* A helper function that reads the .gdb_index from BUFFER and fills
2521 in MAP. FILENAME is the name of the file containing the data;
2522 it is used for error reporting. DEPRECATED_OK is true if it is
2523 ok to use deprecated sections.
2524
2525 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2526 out parameters that are filled in with information about the CU and
2527 TU lists in the section.
2528
2529 Returns true if all went well, false otherwise. */
2530
2531 static bool
2532 read_gdb_index_from_buffer (const char *filename,
2533 bool deprecated_ok,
2534 gdb::array_view<const gdb_byte> buffer,
2535 struct mapped_index *map,
2536 const gdb_byte **cu_list,
2537 offset_type *cu_list_elements,
2538 const gdb_byte **types_list,
2539 offset_type *types_list_elements)
2540 {
2541 const gdb_byte *addr = &buffer[0];
2542 offset_view metadata (buffer);
2543
2544 /* Version check. */
2545 offset_type version = metadata[0];
2546 /* Versions earlier than 3 emitted every copy of a psymbol. This
2547 causes the index to behave very poorly for certain requests. Version 3
2548 contained incomplete addrmap. So, it seems better to just ignore such
2549 indices. */
2550 if (version < 4)
2551 {
2552 static int warning_printed = 0;
2553 if (!warning_printed)
2554 {
2555 warning (_("Skipping obsolete .gdb_index section in %s."),
2556 filename);
2557 warning_printed = 1;
2558 }
2559 return 0;
2560 }
2561 /* Index version 4 uses a different hash function than index version
2562 5 and later.
2563
2564 Versions earlier than 6 did not emit psymbols for inlined
2565 functions. Using these files will cause GDB not to be able to
2566 set breakpoints on inlined functions by name, so we ignore these
2567 indices unless the user has done
2568 "set use-deprecated-index-sections on". */
2569 if (version < 6 && !deprecated_ok)
2570 {
2571 static int warning_printed = 0;
2572 if (!warning_printed)
2573 {
2574 warning (_("\
2575 Skipping deprecated .gdb_index section in %s.\n\
2576 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2577 to use the section anyway."),
2578 filename);
2579 warning_printed = 1;
2580 }
2581 return 0;
2582 }
2583 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2584 of the TU (for symbols coming from TUs),
2585 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2586 Plus gold-generated indices can have duplicate entries for global symbols,
2587 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2588 These are just performance bugs, and we can't distinguish gdb-generated
2589 indices from gold-generated ones, so issue no warning here. */
2590
2591 /* Indexes with higher version than the one supported by GDB may be no
2592 longer backward compatible. */
2593 if (version > 8)
2594 return 0;
2595
2596 map->version = version;
2597
2598 int i = 1;
2599 *cu_list = addr + metadata[i];
2600 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2601 ++i;
2602
2603 *types_list = addr + metadata[i];
2604 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
2605 ++i;
2606
2607 const gdb_byte *address_table = addr + metadata[i];
2608 const gdb_byte *address_table_end = addr + metadata[i + 1];
2609 map->address_table
2610 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2611 ++i;
2612
2613 const gdb_byte *symbol_table = addr + metadata[i];
2614 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
2615 map->symbol_table
2616 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2617 symbol_table_end));
2618
2619 ++i;
2620 map->constant_pool = buffer.slice (metadata[i]);
2621
2622 if (map->constant_pool.empty () && !map->symbol_table.empty ())
2623 {
2624 /* An empty constant pool implies that all symbol table entries are
2625 empty. Make map->symbol_table.empty () == true. */
2626 map->symbol_table
2627 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
2628 symbol_table));
2629 }
2630
2631 return 1;
2632 }
2633
2634 static void finalize_all_units (dwarf2_per_bfd *per_bfd);
2635
2636 /* Callback types for dwarf2_read_gdb_index. */
2637
2638 typedef gdb::function_view
2639 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
2640 get_gdb_index_contents_ftype;
2641 typedef gdb::function_view
2642 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2643 get_gdb_index_contents_dwz_ftype;
2644
2645 /* Read .gdb_index. If everything went ok, initialize the "quick"
2646 elements of all the CUs and return 1. Otherwise, return 0. */
2647
2648 static int
2649 dwarf2_read_gdb_index
2650 (dwarf2_per_objfile *per_objfile,
2651 get_gdb_index_contents_ftype get_gdb_index_contents,
2652 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2653 {
2654 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2655 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2656 struct dwz_file *dwz;
2657 struct objfile *objfile = per_objfile->objfile;
2658 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2659
2660 gdb::array_view<const gdb_byte> main_index_contents
2661 = get_gdb_index_contents (objfile, per_bfd);
2662
2663 if (main_index_contents.empty ())
2664 return 0;
2665
2666 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2667 if (!read_gdb_index_from_buffer (objfile_name (objfile),
2668 use_deprecated_index_sections,
2669 main_index_contents, map.get (), &cu_list,
2670 &cu_list_elements, &types_list,
2671 &types_list_elements))
2672 return 0;
2673
2674 /* Don't use the index if it's empty. */
2675 if (map->symbol_table.empty ())
2676 return 0;
2677
2678 /* If there is a .dwz file, read it so we can get its CU list as
2679 well. */
2680 dwz = dwarf2_get_dwz_file (per_bfd);
2681 if (dwz != NULL)
2682 {
2683 struct mapped_index dwz_map;
2684 const gdb_byte *dwz_types_ignore;
2685 offset_type dwz_types_elements_ignore;
2686
2687 gdb::array_view<const gdb_byte> dwz_index_content
2688 = get_gdb_index_contents_dwz (objfile, dwz);
2689
2690 if (dwz_index_content.empty ())
2691 return 0;
2692
2693 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
2694 1, dwz_index_content, &dwz_map,
2695 &dwz_list, &dwz_list_elements,
2696 &dwz_types_ignore,
2697 &dwz_types_elements_ignore))
2698 {
2699 warning (_("could not read '.gdb_index' section from %s; skipping"),
2700 bfd_get_filename (dwz->dwz_bfd.get ()));
2701 return 0;
2702 }
2703 }
2704
2705 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
2706 dwz_list_elements);
2707
2708 if (types_list_elements)
2709 {
2710 /* We can only handle a single .debug_types when we have an
2711 index. */
2712 if (per_bfd->types.size () > 1)
2713 {
2714 per_bfd->all_units.clear ();
2715 return 0;
2716 }
2717
2718 dwarf2_section_info *section
2719 = (per_bfd->types.size () == 1
2720 ? &per_bfd->types[0]
2721 : &per_bfd->info);
2722
2723 create_signatured_type_table_from_index (per_bfd, section, types_list,
2724 types_list_elements);
2725 }
2726
2727 finalize_all_units (per_bfd);
2728
2729 create_addrmap_from_index (per_objfile, map.get ());
2730
2731 per_bfd->index_table = std::move (map);
2732 per_bfd->quick_file_names_table =
2733 create_quick_file_names_table (per_bfd->all_units.size ());
2734
2735 return 1;
2736 }
2737
2738 /* die_reader_func for dw2_get_file_names. */
2739
2740 static void
2741 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2742 struct die_info *comp_unit_die)
2743 {
2744 struct dwarf2_cu *cu = reader->cu;
2745 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2746 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2747 struct dwarf2_per_cu_data *lh_cu;
2748 struct attribute *attr;
2749 void **slot;
2750 struct quick_file_names *qfn;
2751
2752 gdb_assert (! this_cu->is_debug_types);
2753
2754 this_cu->files_read = true;
2755 /* Our callers never want to match partial units -- instead they
2756 will match the enclosing full CU. */
2757 if (comp_unit_die->tag == DW_TAG_partial_unit)
2758 return;
2759
2760 lh_cu = this_cu;
2761 slot = NULL;
2762
2763 line_header_up lh;
2764 sect_offset line_offset {};
2765
2766 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
2767
2768 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2769 if (attr != nullptr && attr->form_is_unsigned ())
2770 {
2771 struct quick_file_names find_entry;
2772
2773 line_offset = (sect_offset) attr->as_unsigned ();
2774
2775 /* We may have already read in this line header (TU line header sharing).
2776 If we have we're done. */
2777 find_entry.hash.dwo_unit = cu->dwo_unit;
2778 find_entry.hash.line_sect_off = line_offset;
2779 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
2780 &find_entry, INSERT);
2781 if (*slot != NULL)
2782 {
2783 lh_cu->file_names = (struct quick_file_names *) *slot;
2784 return;
2785 }
2786
2787 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
2788 }
2789
2790 int offset = 0;
2791 if (!fnd.is_unknown ())
2792 ++offset;
2793 else if (lh == nullptr)
2794 return;
2795
2796 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
2797 qfn->hash.dwo_unit = cu->dwo_unit;
2798 qfn->hash.line_sect_off = line_offset;
2799 /* There may not be a DW_AT_stmt_list. */
2800 if (slot != nullptr)
2801 *slot = qfn;
2802
2803 std::vector<const char *> include_names;
2804 if (lh != nullptr)
2805 {
2806 for (const auto &entry : lh->file_names ())
2807 {
2808 std::string name_holder;
2809 const char *include_name =
2810 compute_include_file_name (lh.get (), entry, fnd, name_holder);
2811 if (include_name != nullptr)
2812 {
2813 include_name = per_objfile->objfile->intern (include_name);
2814 include_names.push_back (include_name);
2815 }
2816 }
2817 }
2818
2819 qfn->num_file_names = offset + include_names.size ();
2820 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
2821 qfn->file_names =
2822 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
2823 qfn->num_file_names);
2824 if (offset != 0)
2825 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
2826
2827 if (!include_names.empty ())
2828 memcpy (&qfn->file_names[offset], include_names.data (),
2829 include_names.size () * sizeof (const char *));
2830
2831 qfn->real_names = NULL;
2832
2833 lh_cu->file_names = qfn;
2834 }
2835
2836 /* A helper for the "quick" functions which attempts to read the line
2837 table for THIS_CU. */
2838
2839 static struct quick_file_names *
2840 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
2841 dwarf2_per_objfile *per_objfile)
2842 {
2843 /* This should never be called for TUs. */
2844 gdb_assert (! this_cu->is_debug_types);
2845
2846 if (this_cu->files_read)
2847 return this_cu->file_names;
2848
2849 cutu_reader reader (this_cu, per_objfile);
2850 if (!reader.dummy_p)
2851 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
2852
2853 return this_cu->file_names;
2854 }
2855
2856 /* A helper for the "quick" functions which computes and caches the
2857 real path for a given file name from the line table. */
2858
2859 static const char *
2860 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
2861 struct quick_file_names *qfn, int index)
2862 {
2863 if (qfn->real_names == NULL)
2864 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
2865 qfn->num_file_names, const char *);
2866
2867 if (qfn->real_names[index] == NULL)
2868 {
2869 const char *dirname = nullptr;
2870
2871 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
2872 dirname = qfn->comp_dir;
2873
2874 gdb::unique_xmalloc_ptr<char> fullname;
2875 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
2876
2877 qfn->real_names[index] = fullname.release ();
2878 }
2879
2880 return qfn->real_names[index];
2881 }
2882
2883 struct symtab *
2884 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
2885 {
2886 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2887 dwarf2_per_cu_data *dwarf_cu
2888 = per_objfile->per_bfd->all_units.back ().get ();
2889 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2890
2891 if (cust == NULL)
2892 return NULL;
2893
2894 return cust->primary_filetab ();
2895 }
2896
2897 /* See read.h. */
2898
2899 void
2900 dwarf2_per_cu_data::free_cached_file_names ()
2901 {
2902 if (fnd != nullptr)
2903 fnd->forget_fullname ();
2904
2905 if (per_bfd == nullptr)
2906 return;
2907
2908 struct quick_file_names *file_data = file_names;
2909 if (file_data != nullptr && file_data->real_names != nullptr)
2910 {
2911 for (int i = 0; i < file_data->num_file_names; ++i)
2912 {
2913 xfree ((void *) file_data->real_names[i]);
2914 file_data->real_names[i] = nullptr;
2915 }
2916 }
2917 }
2918
2919 void
2920 dwarf2_base_index_functions::forget_cached_source_info
2921 (struct objfile *objfile)
2922 {
2923 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2924
2925 for (auto &per_cu : per_objfile->per_bfd->all_units)
2926 per_cu->free_cached_file_names ();
2927 }
2928
2929 /* Struct used to manage iterating over all CUs looking for a symbol. */
2930
2931 struct dw2_symtab_iterator
2932 {
2933 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2934 dwarf2_per_objfile *per_objfile;
2935 /* If set, only look for symbols that match that block. Valid values are
2936 GLOBAL_BLOCK and STATIC_BLOCK. */
2937 gdb::optional<block_enum> block_index;
2938 /* The kind of symbol we're looking for. */
2939 domain_enum domain;
2940 /* The list of CUs from the index entry of the symbol,
2941 or NULL if not found. */
2942 offset_view vec;
2943 /* The next element in VEC to look at. */
2944 int next;
2945 /* The number of elements in VEC, or zero if there is no match. */
2946 int length;
2947 /* Have we seen a global version of the symbol?
2948 If so we can ignore all further global instances.
2949 This is to work around gold/15646, inefficient gold-generated
2950 indices. */
2951 int global_seen;
2952 };
2953
2954 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2955
2956 static void
2957 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
2958 dwarf2_per_objfile *per_objfile,
2959 gdb::optional<block_enum> block_index,
2960 domain_enum domain, offset_type namei,
2961 mapped_index &index)
2962 {
2963 iter->per_objfile = per_objfile;
2964 iter->block_index = block_index;
2965 iter->domain = domain;
2966 iter->next = 0;
2967 iter->global_seen = 0;
2968 iter->vec = {};
2969 iter->length = 0;
2970
2971 gdb_assert (!index.symbol_name_slot_invalid (namei));
2972 offset_type vec_idx = index.symbol_vec_index (namei);
2973
2974 iter->vec = offset_view (index.constant_pool.slice (vec_idx));
2975 iter->length = iter->vec[0];
2976 }
2977
2978 /* Return the next matching CU or NULL if there are no more. */
2979
2980 static struct dwarf2_per_cu_data *
2981 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter,
2982 mapped_index &index)
2983 {
2984 dwarf2_per_objfile *per_objfile = iter->per_objfile;
2985
2986 for ( ; iter->next < iter->length; ++iter->next)
2987 {
2988 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
2989 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
2990 gdb_index_symbol_kind symbol_kind =
2991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2992 /* Only check the symbol attributes if they're present.
2993 Indices prior to version 7 don't record them,
2994 and indices >= 7 may elide them for certain symbols
2995 (gold does this). */
2996 int attrs_valid = (index.version >= 7
2997 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
2998
2999 /* Don't crash on bad data. */
3000 if (cu_index >= per_objfile->per_bfd->all_units.size ())
3001 {
3002 complaint (_(".gdb_index entry has bad CU index"
3003 " [in module %s]"), objfile_name (per_objfile->objfile));
3004 continue;
3005 }
3006
3007 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3008
3009 /* Skip if already read in. */
3010 if (per_objfile->symtab_set_p (per_cu))
3011 continue;
3012
3013 /* Check static vs global. */
3014 if (attrs_valid)
3015 {
3016 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3017
3018 if (iter->block_index.has_value ())
3019 {
3020 bool want_static = *iter->block_index == STATIC_BLOCK;
3021
3022 if (is_static != want_static)
3023 continue;
3024 }
3025
3026 /* Work around gold/15646. */
3027 if (!is_static
3028 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3029 {
3030 if (iter->global_seen)
3031 continue;
3032
3033 iter->global_seen = 1;
3034 }
3035 }
3036
3037 /* Only check the symbol's kind if it has one. */
3038 if (attrs_valid)
3039 {
3040 switch (iter->domain)
3041 {
3042 case VAR_DOMAIN:
3043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3044 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3045 /* Some types are also in VAR_DOMAIN. */
3046 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3047 continue;
3048 break;
3049 case STRUCT_DOMAIN:
3050 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3051 continue;
3052 break;
3053 case LABEL_DOMAIN:
3054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3055 continue;
3056 break;
3057 case MODULE_DOMAIN:
3058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3059 continue;
3060 break;
3061 default:
3062 break;
3063 }
3064 }
3065
3066 ++iter->next;
3067 return per_cu;
3068 }
3069
3070 return NULL;
3071 }
3072
3073 void
3074 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3075 bool print_bcache)
3076 {
3077 if (print_bcache)
3078 return;
3079
3080 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3081 int total = per_objfile->per_bfd->all_units.size ();
3082 int count = 0;
3083
3084 for (int i = 0; i < total; ++i)
3085 {
3086 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3087
3088 if (!per_objfile->symtab_set_p (per_cu))
3089 ++count;
3090 }
3091 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
3092 gdb_printf (_(" Number of unread CUs: %d\n"), count);
3093 }
3094
3095 /* This dumps minimal information about the index.
3096 It is called via "mt print objfiles".
3097 One use is to verify .gdb_index has been loaded by the
3098 gdb.dwarf2/gdb-index.exp testcase. */
3099
3100 void
3101 dwarf2_gdb_index::dump (struct objfile *objfile)
3102 {
3103 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3104
3105 mapped_index *index = (gdb::checked_static_cast<mapped_index *>
3106 (per_objfile->per_bfd->index_table.get ()));
3107 gdb_printf (".gdb_index: version %d\n", index->version);
3108 gdb_printf ("\n");
3109 }
3110
3111 void
3112 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3113 {
3114 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3115 int total_units = per_objfile->per_bfd->all_units.size ();
3116
3117 for (int i = 0; i < total_units; ++i)
3118 {
3119 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3120
3121 /* We don't want to directly expand a partial CU, because if we
3122 read it with the wrong language, then assertion failures can
3123 be triggered later on. See PR symtab/23010. So, tell
3124 dw2_instantiate_symtab to skip partial CUs -- any important
3125 partial CU will be read via DW_TAG_imported_unit anyway. */
3126 dw2_instantiate_symtab (per_cu, per_objfile, true);
3127 }
3128 }
3129
3130 static bool
3131 dw2_expand_symtabs_matching_symbol
3132 (mapped_index_base &index,
3133 const lookup_name_info &lookup_name_in,
3134 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3135 gdb::function_view<bool (offset_type)> match_callback,
3136 dwarf2_per_objfile *per_objfile);
3137
3138 static bool
3139 dw2_expand_symtabs_matching_one
3140 (dwarf2_per_cu_data *per_cu,
3141 dwarf2_per_objfile *per_objfile,
3142 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3143 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3144
3145 void
3146 dwarf2_gdb_index::expand_matching_symbols
3147 (struct objfile *objfile,
3148 const lookup_name_info &name, domain_enum domain,
3149 int global,
3150 symbol_compare_ftype *ordered_compare)
3151 {
3152 /* Used for Ada. */
3153 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3154
3155 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3156
3157 mapped_index &index
3158 = *(gdb::checked_static_cast<mapped_index *>
3159 (per_objfile->per_bfd->index_table.get ()));
3160
3161 const char *match_name = name.ada ().lookup_name ().c_str ();
3162 auto matcher = [&] (const char *symname)
3163 {
3164 if (ordered_compare == nullptr)
3165 return true;
3166 return ordered_compare (symname, match_name) == 0;
3167 };
3168
3169 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3170 [&] (offset_type namei)
3171 {
3172 struct dw2_symtab_iterator iter;
3173 struct dwarf2_per_cu_data *per_cu;
3174
3175 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain, namei,
3176 index);
3177 while ((per_cu = dw2_symtab_iter_next (&iter, index)) != NULL)
3178 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3179 nullptr);
3180 return true;
3181 }, per_objfile);
3182 }
3183
3184 /* Starting from a search name, return the string that finds the upper
3185 bound of all strings that start with SEARCH_NAME in a sorted name
3186 list. Returns the empty string to indicate that the upper bound is
3187 the end of the list. */
3188
3189 static std::string
3190 make_sort_after_prefix_name (const char *search_name)
3191 {
3192 /* When looking to complete "func", we find the upper bound of all
3193 symbols that start with "func" by looking for where we'd insert
3194 the closest string that would follow "func" in lexicographical
3195 order. Usually, that's "func"-with-last-character-incremented,
3196 i.e. "fund". Mind non-ASCII characters, though. Usually those
3197 will be UTF-8 multi-byte sequences, but we can't be certain.
3198 Especially mind the 0xff character, which is a valid character in
3199 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3200 rule out compilers allowing it in identifiers. Note that
3201 conveniently, strcmp/strcasecmp are specified to compare
3202 characters interpreted as unsigned char. So what we do is treat
3203 the whole string as a base 256 number composed of a sequence of
3204 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3205 to 0, and carries 1 to the following more-significant position.
3206 If the very first character in SEARCH_NAME ends up incremented
3207 and carries/overflows, then the upper bound is the end of the
3208 list. The string after the empty string is also the empty
3209 string.
3210
3211 Some examples of this operation:
3212
3213 SEARCH_NAME => "+1" RESULT
3214
3215 "abc" => "abd"
3216 "ab\xff" => "ac"
3217 "\xff" "a" "\xff" => "\xff" "b"
3218 "\xff" => ""
3219 "\xff\xff" => ""
3220 "" => ""
3221
3222 Then, with these symbols for example:
3223
3224 func
3225 func1
3226 fund
3227
3228 completing "func" looks for symbols between "func" and
3229 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3230 which finds "func" and "func1", but not "fund".
3231
3232 And with:
3233
3234 funcÿ (Latin1 'ÿ' [0xff])
3235 funcÿ1
3236 fund
3237
3238 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3239 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3240
3241 And with:
3242
3243 ÿÿ (Latin1 'ÿ' [0xff])
3244 ÿÿ1
3245
3246 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3247 the end of the list.
3248 */
3249 std::string after = search_name;
3250 while (!after.empty () && (unsigned char) after.back () == 0xff)
3251 after.pop_back ();
3252 if (!after.empty ())
3253 after.back () = (unsigned char) after.back () + 1;
3254 return after;
3255 }
3256
3257 /* See declaration. */
3258
3259 std::pair<std::vector<name_component>::const_iterator,
3260 std::vector<name_component>::const_iterator>
3261 mapped_index_base::find_name_components_bounds
3262 (const lookup_name_info &lookup_name_without_params, language lang,
3263 dwarf2_per_objfile *per_objfile) const
3264 {
3265 auto *name_cmp
3266 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3267
3268 const char *lang_name
3269 = lookup_name_without_params.language_lookup_name (lang);
3270
3271 /* Comparison function object for lower_bound that matches against a
3272 given symbol name. */
3273 auto lookup_compare_lower = [&] (const name_component &elem,
3274 const char *name)
3275 {
3276 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3277 const char *elem_name = elem_qualified + elem.name_offset;
3278 return name_cmp (elem_name, name) < 0;
3279 };
3280
3281 /* Comparison function object for upper_bound that matches against a
3282 given symbol name. */
3283 auto lookup_compare_upper = [&] (const char *name,
3284 const name_component &elem)
3285 {
3286 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3287 const char *elem_name = elem_qualified + elem.name_offset;
3288 return name_cmp (name, elem_name) < 0;
3289 };
3290
3291 auto begin = this->name_components.begin ();
3292 auto end = this->name_components.end ();
3293
3294 /* Find the lower bound. */
3295 auto lower = [&] ()
3296 {
3297 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3298 return begin;
3299 else
3300 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3301 } ();
3302
3303 /* Find the upper bound. */
3304 auto upper = [&] ()
3305 {
3306 if (lookup_name_without_params.completion_mode ())
3307 {
3308 /* In completion mode, we want UPPER to point past all
3309 symbols names that have the same prefix. I.e., with
3310 these symbols, and completing "func":
3311
3312 function << lower bound
3313 function1
3314 other_function << upper bound
3315
3316 We find the upper bound by looking for the insertion
3317 point of "func"-with-last-character-incremented,
3318 i.e. "fund". */
3319 std::string after = make_sort_after_prefix_name (lang_name);
3320 if (after.empty ())
3321 return end;
3322 return std::lower_bound (lower, end, after.c_str (),
3323 lookup_compare_lower);
3324 }
3325 else
3326 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3327 } ();
3328
3329 return {lower, upper};
3330 }
3331
3332 /* See declaration. */
3333
3334 void
3335 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3336 {
3337 if (!this->name_components.empty ())
3338 return;
3339
3340 this->name_components_casing = case_sensitivity;
3341 auto *name_cmp
3342 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3343
3344 /* The code below only knows how to break apart components of C++
3345 symbol names (and other languages that use '::' as
3346 namespace/module separator) and Ada symbol names. */
3347 auto count = this->symbol_name_count ();
3348 for (offset_type idx = 0; idx < count; idx++)
3349 {
3350 if (this->symbol_name_slot_invalid (idx))
3351 continue;
3352
3353 const char *name = this->symbol_name_at (idx, per_objfile);
3354
3355 /* Add each name component to the name component table. */
3356 unsigned int previous_len = 0;
3357
3358 if (strstr (name, "::") != nullptr)
3359 {
3360 for (unsigned int current_len = cp_find_first_component (name);
3361 name[current_len] != '\0';
3362 current_len += cp_find_first_component (name + current_len))
3363 {
3364 gdb_assert (name[current_len] == ':');
3365 this->name_components.push_back ({previous_len, idx});
3366 /* Skip the '::'. */
3367 current_len += 2;
3368 previous_len = current_len;
3369 }
3370 }
3371 else
3372 {
3373 /* Handle the Ada encoded (aka mangled) form here. */
3374 for (const char *iter = strstr (name, "__");
3375 iter != nullptr;
3376 iter = strstr (iter, "__"))
3377 {
3378 this->name_components.push_back ({previous_len, idx});
3379 iter += 2;
3380 previous_len = iter - name;
3381 }
3382 }
3383
3384 this->name_components.push_back ({previous_len, idx});
3385 }
3386
3387 /* Sort name_components elements by name. */
3388 auto name_comp_compare = [&] (const name_component &left,
3389 const name_component &right)
3390 {
3391 const char *left_qualified
3392 = this->symbol_name_at (left.idx, per_objfile);
3393 const char *right_qualified
3394 = this->symbol_name_at (right.idx, per_objfile);
3395
3396 const char *left_name = left_qualified + left.name_offset;
3397 const char *right_name = right_qualified + right.name_offset;
3398
3399 return name_cmp (left_name, right_name) < 0;
3400 };
3401
3402 std::sort (this->name_components.begin (),
3403 this->name_components.end (),
3404 name_comp_compare);
3405 }
3406
3407 /* Helper for dw2_expand_symtabs_matching that works with a
3408 mapped_index_base instead of the containing objfile. This is split
3409 to a separate function in order to be able to unit test the
3410 name_components matching using a mock mapped_index_base. For each
3411 symbol name that matches, calls MATCH_CALLBACK, passing it the
3412 symbol's index in the mapped_index_base symbol table. */
3413
3414 static bool
3415 dw2_expand_symtabs_matching_symbol
3416 (mapped_index_base &index,
3417 const lookup_name_info &lookup_name_in,
3418 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3419 gdb::function_view<bool (offset_type)> match_callback,
3420 dwarf2_per_objfile *per_objfile)
3421 {
3422 lookup_name_info lookup_name_without_params
3423 = lookup_name_in.make_ignore_params ();
3424
3425 /* Build the symbol name component sorted vector, if we haven't
3426 yet. */
3427 index.build_name_components (per_objfile);
3428
3429 /* The same symbol may appear more than once in the range though.
3430 E.g., if we're looking for symbols that complete "w", and we have
3431 a symbol named "w1::w2", we'll find the two name components for
3432 that same symbol in the range. To be sure we only call the
3433 callback once per symbol, we first collect the symbol name
3434 indexes that matched in a temporary vector and ignore
3435 duplicates. */
3436 std::vector<offset_type> matches;
3437
3438 struct name_and_matcher
3439 {
3440 symbol_name_matcher_ftype *matcher;
3441 const char *name;
3442
3443 bool operator== (const name_and_matcher &other) const
3444 {
3445 return matcher == other.matcher && strcmp (name, other.name) == 0;
3446 }
3447 };
3448
3449 /* A vector holding all the different symbol name matchers, for all
3450 languages. */
3451 std::vector<name_and_matcher> matchers;
3452
3453 for (int i = 0; i < nr_languages; i++)
3454 {
3455 enum language lang_e = (enum language) i;
3456
3457 const language_defn *lang = language_def (lang_e);
3458 symbol_name_matcher_ftype *name_matcher
3459 = lang->get_symbol_name_matcher (lookup_name_without_params);
3460
3461 name_and_matcher key {
3462 name_matcher,
3463 lookup_name_without_params.language_lookup_name (lang_e)
3464 };
3465
3466 /* Don't insert the same comparison routine more than once.
3467 Note that we do this linear walk. This is not a problem in
3468 practice because the number of supported languages is
3469 low. */
3470 if (std::find (matchers.begin (), matchers.end (), key)
3471 != matchers.end ())
3472 continue;
3473 matchers.push_back (std::move (key));
3474
3475 auto bounds
3476 = index.find_name_components_bounds (lookup_name_without_params,
3477 lang_e, per_objfile);
3478
3479 /* Now for each symbol name in range, check to see if we have a name
3480 match, and if so, call the MATCH_CALLBACK callback. */
3481
3482 for (; bounds.first != bounds.second; ++bounds.first)
3483 {
3484 const char *qualified
3485 = index.symbol_name_at (bounds.first->idx, per_objfile);
3486
3487 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3488 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3489 continue;
3490
3491 matches.push_back (bounds.first->idx);
3492 }
3493 }
3494
3495 std::sort (matches.begin (), matches.end ());
3496
3497 /* Finally call the callback, once per match. */
3498 ULONGEST prev = -1;
3499 bool result = true;
3500 for (offset_type idx : matches)
3501 {
3502 if (prev != idx)
3503 {
3504 if (!match_callback (idx))
3505 {
3506 result = false;
3507 break;
3508 }
3509 prev = idx;
3510 }
3511 }
3512
3513 /* Above we use a type wider than idx's for 'prev', since 0 and
3514 (offset_type)-1 are both possible values. */
3515 static_assert (sizeof (prev) > sizeof (offset_type), "");
3516
3517 return result;
3518 }
3519
3520 #if GDB_SELF_TEST
3521
3522 namespace selftests { namespace dw2_expand_symtabs_matching {
3523
3524 /* A mock .gdb_index/.debug_names-like name index table, enough to
3525 exercise dw2_expand_symtabs_matching_symbol, which works with the
3526 mapped_index_base interface. Builds an index from the symbol list
3527 passed as parameter to the constructor. */
3528 class mock_mapped_index : public mapped_index_base
3529 {
3530 public:
3531 mock_mapped_index (gdb::array_view<const char *> symbols)
3532 : m_symbol_table (symbols)
3533 {}
3534
3535 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3536
3537 /* Return the number of names in the symbol table. */
3538 size_t symbol_name_count () const override
3539 {
3540 return m_symbol_table.size ();
3541 }
3542
3543 /* Get the name of the symbol at IDX in the symbol table. */
3544 const char *symbol_name_at
3545 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3546 {
3547 return m_symbol_table[idx];
3548 }
3549
3550 quick_symbol_functions_up make_quick_functions () const override
3551 {
3552 return nullptr;
3553 }
3554
3555 private:
3556 gdb::array_view<const char *> m_symbol_table;
3557 };
3558
3559 /* Convenience function that converts a NULL pointer to a "<null>"
3560 string, to pass to print routines. */
3561
3562 static const char *
3563 string_or_null (const char *str)
3564 {
3565 return str != NULL ? str : "<null>";
3566 }
3567
3568 /* Check if a lookup_name_info built from
3569 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3570 index. EXPECTED_LIST is the list of expected matches, in expected
3571 matching order. If no match expected, then an empty list is
3572 specified. Returns true on success. On failure prints a warning
3573 indicating the file:line that failed, and returns false. */
3574
3575 static bool
3576 check_match (const char *file, int line,
3577 mock_mapped_index &mock_index,
3578 const char *name, symbol_name_match_type match_type,
3579 bool completion_mode,
3580 std::initializer_list<const char *> expected_list,
3581 dwarf2_per_objfile *per_objfile)
3582 {
3583 lookup_name_info lookup_name (name, match_type, completion_mode);
3584
3585 bool matched = true;
3586
3587 auto mismatch = [&] (const char *expected_str,
3588 const char *got)
3589 {
3590 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3591 "expected=\"%s\", got=\"%s\"\n"),
3592 file, line,
3593 (match_type == symbol_name_match_type::FULL
3594 ? "FULL" : "WILD"),
3595 name, string_or_null (expected_str), string_or_null (got));
3596 matched = false;
3597 };
3598
3599 auto expected_it = expected_list.begin ();
3600 auto expected_end = expected_list.end ();
3601
3602 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3603 nullptr,
3604 [&] (offset_type idx)
3605 {
3606 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3607 const char *expected_str
3608 = expected_it == expected_end ? NULL : *expected_it++;
3609
3610 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3611 mismatch (expected_str, matched_name);
3612 return true;
3613 }, per_objfile);
3614
3615 const char *expected_str
3616 = expected_it == expected_end ? NULL : *expected_it++;
3617 if (expected_str != NULL)
3618 mismatch (expected_str, NULL);
3619
3620 return matched;
3621 }
3622
3623 /* The symbols added to the mock mapped_index for testing (in
3624 canonical form). */
3625 static const char *test_symbols[] = {
3626 "function",
3627 "std::bar",
3628 "std::zfunction",
3629 "std::zfunction2",
3630 "w1::w2",
3631 "ns::foo<char*>",
3632 "ns::foo<int>",
3633 "ns::foo<long>",
3634 "ns2::tmpl<int>::foo2",
3635 "(anonymous namespace)::A::B::C",
3636
3637 /* These are used to check that the increment-last-char in the
3638 matching algorithm for completion doesn't match "t1_fund" when
3639 completing "t1_func". */
3640 "t1_func",
3641 "t1_func1",
3642 "t1_fund",
3643 "t1_fund1",
3644
3645 /* A UTF-8 name with multi-byte sequences to make sure that
3646 cp-name-parser understands this as a single identifier ("função"
3647 is "function" in PT). */
3648 u8"u8função",
3649
3650 /* Test a symbol name that ends with a 0xff character, which is a
3651 valid character in non-UTF-8 source character sets (e.g. Latin1
3652 'ÿ'), and we can't rule out compilers allowing it in identifiers.
3653 We test this because the completion algorithm finds the upper
3654 bound of symbols by looking for the insertion point of
3655 "func"-with-last-character-incremented, i.e. "fund", and adding 1
3656 to 0xff should wraparound and carry to the previous character.
3657 See comments in make_sort_after_prefix_name. */
3658 "yfunc\377",
3659
3660 /* Some more symbols with \377 (0xff). See above. */
3661 "\377",
3662 "\377\377123",
3663
3664 /* A name with all sorts of complications. Starts with "z" to make
3665 it easier for the completion tests below. */
3666 #define Z_SYM_NAME \
3667 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3668 "::tuple<(anonymous namespace)::ui*, " \
3669 "std::default_delete<(anonymous namespace)::ui>, void>"
3670
3671 Z_SYM_NAME
3672 };
3673
3674 /* Returns true if the mapped_index_base::find_name_component_bounds
3675 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3676 in completion mode. */
3677
3678 static bool
3679 check_find_bounds_finds (mapped_index_base &index,
3680 const char *search_name,
3681 gdb::array_view<const char *> expected_syms,
3682 dwarf2_per_objfile *per_objfile)
3683 {
3684 lookup_name_info lookup_name (search_name,
3685 symbol_name_match_type::FULL, true);
3686
3687 auto bounds = index.find_name_components_bounds (lookup_name,
3688 language_cplus,
3689 per_objfile);
3690
3691 size_t distance = std::distance (bounds.first, bounds.second);
3692 if (distance != expected_syms.size ())
3693 return false;
3694
3695 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
3696 {
3697 auto nc_elem = bounds.first + exp_elem;
3698 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
3699 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
3700 return false;
3701 }
3702
3703 return true;
3704 }
3705
3706 /* Test the lower-level mapped_index::find_name_component_bounds
3707 method. */
3708
3709 static void
3710 test_mapped_index_find_name_component_bounds ()
3711 {
3712 mock_mapped_index mock_index (test_symbols);
3713
3714 mock_index.build_name_components (NULL /* per_objfile */);
3715
3716 /* Test the lower-level mapped_index::find_name_component_bounds
3717 method in completion mode. */
3718 {
3719 static const char *expected_syms[] = {
3720 "t1_func",
3721 "t1_func1",
3722 };
3723
3724 SELF_CHECK (check_find_bounds_finds
3725 (mock_index, "t1_func", expected_syms,
3726 NULL /* per_objfile */));
3727 }
3728
3729 /* Check that the increment-last-char in the name matching algorithm
3730 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
3731 make_sort_after_prefix_name. */
3732 {
3733 static const char *expected_syms1[] = {
3734 "\377",
3735 "\377\377123",
3736 };
3737 SELF_CHECK (check_find_bounds_finds
3738 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
3739
3740 static const char *expected_syms2[] = {
3741 "\377\377123",
3742 };
3743 SELF_CHECK (check_find_bounds_finds
3744 (mock_index, "\377\377", expected_syms2,
3745 NULL /* per_objfile */));
3746 }
3747 }
3748
3749 /* Test dw2_expand_symtabs_matching_symbol. */
3750
3751 static void
3752 test_dw2_expand_symtabs_matching_symbol ()
3753 {
3754 mock_mapped_index mock_index (test_symbols);
3755
3756 /* We let all tests run until the end even if some fails, for debug
3757 convenience. */
3758 bool any_mismatch = false;
3759
3760 /* Create the expected symbols list (an initializer_list). Needed
3761 because lists have commas, and we need to pass them to CHECK,
3762 which is a macro. */
3763 #define EXPECT(...) { __VA_ARGS__ }
3764
3765 /* Wrapper for check_match that passes down the current
3766 __FILE__/__LINE__. */
3767 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3768 any_mismatch |= !check_match (__FILE__, __LINE__, \
3769 mock_index, \
3770 NAME, MATCH_TYPE, COMPLETION_MODE, \
3771 EXPECTED_LIST, NULL)
3772
3773 /* Identity checks. */
3774 for (const char *sym : test_symbols)
3775 {
3776 /* Should be able to match all existing symbols. */
3777 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
3778 EXPECT (sym));
3779
3780 /* Should be able to match all existing symbols with
3781 parameters. */
3782 std::string with_params = std::string (sym) + "(int)";
3783 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3784 EXPECT (sym));
3785
3786 /* Should be able to match all existing symbols with
3787 parameters and qualifiers. */
3788 with_params = std::string (sym) + " ( int ) const";
3789 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3790 EXPECT (sym));
3791
3792 /* This should really find sym, but cp-name-parser.y doesn't
3793 know about lvalue/rvalue qualifiers yet. */
3794 with_params = std::string (sym) + " ( int ) &&";
3795 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
3796 {});
3797 }
3798
3799 /* Check that the name matching algorithm for completion doesn't get
3800 confused with Latin1 'ÿ' / 0xff. See
3801 make_sort_after_prefix_name. */
3802 {
3803 static const char str[] = "\377";
3804 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3805 EXPECT ("\377", "\377\377123"));
3806 }
3807
3808 /* Check that the increment-last-char in the matching algorithm for
3809 completion doesn't match "t1_fund" when completing "t1_func". */
3810 {
3811 static const char str[] = "t1_func";
3812 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
3813 EXPECT ("t1_func", "t1_func1"));
3814 }
3815
3816 /* Check that completion mode works at each prefix of the expected
3817 symbol name. */
3818 {
3819 static const char str[] = "function(int)";
3820 size_t len = strlen (str);
3821 std::string lookup;
3822
3823 for (size_t i = 1; i < len; i++)
3824 {
3825 lookup.assign (str, i);
3826 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3827 EXPECT ("function"));
3828 }
3829 }
3830
3831 /* While "w" is a prefix of both components, the match function
3832 should still only be called once. */
3833 {
3834 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
3835 EXPECT ("w1::w2"));
3836 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
3837 EXPECT ("w1::w2"));
3838 }
3839
3840 /* Same, with a "complicated" symbol. */
3841 {
3842 static const char str[] = Z_SYM_NAME;
3843 size_t len = strlen (str);
3844 std::string lookup;
3845
3846 for (size_t i = 1; i < len; i++)
3847 {
3848 lookup.assign (str, i);
3849 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
3850 EXPECT (Z_SYM_NAME));
3851 }
3852 }
3853
3854 /* In FULL mode, an incomplete symbol doesn't match. */
3855 {
3856 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
3857 {});
3858 }
3859
3860 /* A complete symbol with parameters matches any overload, since the
3861 index has no overload info. */
3862 {
3863 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
3864 EXPECT ("std::zfunction", "std::zfunction2"));
3865 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
3866 EXPECT ("std::zfunction", "std::zfunction2"));
3867 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
3868 EXPECT ("std::zfunction", "std::zfunction2"));
3869 }
3870
3871 /* Check that whitespace is ignored appropriately. A symbol with a
3872 template argument list. */
3873 {
3874 static const char expected[] = "ns::foo<int>";
3875 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
3876 EXPECT (expected));
3877 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
3878 EXPECT (expected));
3879 }
3880
3881 /* Check that whitespace is ignored appropriately. A symbol with a
3882 template argument list that includes a pointer. */
3883 {
3884 static const char expected[] = "ns::foo<char*>";
3885 /* Try both completion and non-completion modes. */
3886 static const bool completion_mode[2] = {false, true};
3887 for (size_t i = 0; i < 2; i++)
3888 {
3889 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
3890 completion_mode[i], EXPECT (expected));
3891 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
3892 completion_mode[i], EXPECT (expected));
3893
3894 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
3895 completion_mode[i], EXPECT (expected));
3896 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
3897 completion_mode[i], EXPECT (expected));
3898 }
3899 }
3900
3901 {
3902 /* Check method qualifiers are ignored. */
3903 static const char expected[] = "ns::foo<char*>";
3904 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3905 symbol_name_match_type::FULL, true, EXPECT (expected));
3906 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3907 symbol_name_match_type::FULL, true, EXPECT (expected));
3908 CHECK_MATCH ("foo < char * > ( int ) const",
3909 symbol_name_match_type::WILD, true, EXPECT (expected));
3910 CHECK_MATCH ("foo < char * > ( int ) &&",
3911 symbol_name_match_type::WILD, true, EXPECT (expected));
3912 }
3913
3914 /* Test lookup names that don't match anything. */
3915 {
3916 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
3917 {});
3918
3919 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
3920 {});
3921 }
3922
3923 /* Some wild matching tests, exercising "(anonymous namespace)",
3924 which should not be confused with a parameter list. */
3925 {
3926 static const char *syms[] = {
3927 "A::B::C",
3928 "B::C",
3929 "C",
3930 "A :: B :: C ( int )",
3931 "B :: C ( int )",
3932 "C ( int )",
3933 };
3934
3935 for (const char *s : syms)
3936 {
3937 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
3938 EXPECT ("(anonymous namespace)::A::B::C"));
3939 }
3940 }
3941
3942 {
3943 static const char expected[] = "ns2::tmpl<int>::foo2";
3944 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
3945 EXPECT (expected));
3946 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
3947 EXPECT (expected));
3948 }
3949
3950 SELF_CHECK (!any_mismatch);
3951
3952 #undef EXPECT
3953 #undef CHECK_MATCH
3954 }
3955
3956 static void
3957 run_test ()
3958 {
3959 test_mapped_index_find_name_component_bounds ();
3960 test_dw2_expand_symtabs_matching_symbol ();
3961 }
3962
3963 }} // namespace selftests::dw2_expand_symtabs_matching
3964
3965 #endif /* GDB_SELF_TEST */
3966
3967 /* If FILE_MATCHER is NULL or if PER_CU has
3968 dwarf2_per_cu_quick_data::MARK set (see
3969 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3970 EXPANSION_NOTIFY on it. */
3971
3972 static bool
3973 dw2_expand_symtabs_matching_one
3974 (dwarf2_per_cu_data *per_cu,
3975 dwarf2_per_objfile *per_objfile,
3976 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3977 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
3978 {
3979 if (file_matcher == NULL || per_cu->mark)
3980 {
3981 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
3982
3983 compunit_symtab *symtab
3984 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3985 gdb_assert (symtab != nullptr);
3986
3987 if (expansion_notify != NULL && symtab_was_null)
3988 return expansion_notify (symtab);
3989 }
3990 return true;
3991 }
3992
3993 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3994 matched, to expand corresponding CUs that were marked. IDX is the
3995 index of the symbol name that matched. */
3996
3997 static bool
3998 dw2_expand_marked_cus
3999 (dwarf2_per_objfile *per_objfile, offset_type idx,
4000 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4001 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4002 block_search_flags search_flags,
4003 search_domain kind)
4004 {
4005 offset_type vec_len, vec_idx;
4006 bool global_seen = false;
4007 mapped_index &index
4008 = *(gdb::checked_static_cast<mapped_index *>
4009 (per_objfile->per_bfd->index_table.get ()));
4010
4011 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4012 vec_len = vec[0];
4013 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4014 {
4015 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4016 /* This value is only valid for index versions >= 7. */
4017 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4018 gdb_index_symbol_kind symbol_kind =
4019 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4020 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4021 /* Only check the symbol attributes if they're present.
4022 Indices prior to version 7 don't record them,
4023 and indices >= 7 may elide them for certain symbols
4024 (gold does this). */
4025 int attrs_valid =
4026 (index.version >= 7
4027 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4028
4029 /* Work around gold/15646. */
4030 if (attrs_valid
4031 && !is_static
4032 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4033 {
4034 if (global_seen)
4035 continue;
4036
4037 global_seen = true;
4038 }
4039
4040 /* Only check the symbol's kind if it has one. */
4041 if (attrs_valid)
4042 {
4043 if (is_static)
4044 {
4045 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4046 continue;
4047 }
4048 else
4049 {
4050 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4051 continue;
4052 }
4053
4054 switch (kind)
4055 {
4056 case VARIABLES_DOMAIN:
4057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4058 continue;
4059 break;
4060 case FUNCTIONS_DOMAIN:
4061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4062 continue;
4063 break;
4064 case TYPES_DOMAIN:
4065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4066 continue;
4067 break;
4068 case MODULES_DOMAIN:
4069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4070 continue;
4071 break;
4072 default:
4073 break;
4074 }
4075 }
4076
4077 /* Don't crash on bad data. */
4078 if (cu_index >= per_objfile->per_bfd->all_units.size ())
4079 {
4080 complaint (_(".gdb_index entry has bad CU index"
4081 " [in module %s]"), objfile_name (per_objfile->objfile));
4082 continue;
4083 }
4084
4085 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4086 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4087 expansion_notify))
4088 return false;
4089 }
4090
4091 return true;
4092 }
4093
4094 /* If FILE_MATCHER is non-NULL, set all the
4095 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4096 that match FILE_MATCHER. */
4097
4098 static void
4099 dw_expand_symtabs_matching_file_matcher
4100 (dwarf2_per_objfile *per_objfile,
4101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4102 {
4103 if (file_matcher == NULL)
4104 return;
4105
4106 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4107 htab_eq_pointer,
4108 NULL, xcalloc, xfree));
4109 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4110 htab_eq_pointer,
4111 NULL, xcalloc, xfree));
4112
4113 /* The rule is CUs specify all the files, including those used by
4114 any TU, so there's no need to scan TUs here. */
4115
4116 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4117 {
4118 QUIT;
4119
4120 if (per_cu->is_debug_types)
4121 continue;
4122 per_cu->mark = 0;
4123
4124 /* We only need to look at symtabs not already expanded. */
4125 if (per_objfile->symtab_set_p (per_cu.get ()))
4126 continue;
4127
4128 if (per_cu->fnd != nullptr)
4129 {
4130 file_and_directory *fnd = per_cu->fnd.get ();
4131
4132 if (file_matcher (fnd->get_name (), false))
4133 {
4134 per_cu->mark = 1;
4135 continue;
4136 }
4137
4138 /* Before we invoke realpath, which can get expensive when many
4139 files are involved, do a quick comparison of the basenames. */
4140 if ((basenames_may_differ
4141 || file_matcher (lbasename (fnd->get_name ()), true))
4142 && file_matcher (fnd->get_fullname (), false))
4143 {
4144 per_cu->mark = 1;
4145 continue;
4146 }
4147 }
4148
4149 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4150 per_objfile);
4151 if (file_data == NULL)
4152 continue;
4153
4154 if (htab_find (visited_not_found.get (), file_data) != NULL)
4155 continue;
4156 else if (htab_find (visited_found.get (), file_data) != NULL)
4157 {
4158 per_cu->mark = 1;
4159 continue;
4160 }
4161
4162 for (int j = 0; j < file_data->num_file_names; ++j)
4163 {
4164 const char *this_real_name;
4165
4166 if (file_matcher (file_data->file_names[j], false))
4167 {
4168 per_cu->mark = 1;
4169 break;
4170 }
4171
4172 /* Before we invoke realpath, which can get expensive when many
4173 files are involved, do a quick comparison of the basenames. */
4174 if (!basenames_may_differ
4175 && !file_matcher (lbasename (file_data->file_names[j]),
4176 true))
4177 continue;
4178
4179 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4180 if (file_matcher (this_real_name, false))
4181 {
4182 per_cu->mark = 1;
4183 break;
4184 }
4185 }
4186
4187 void **slot = htab_find_slot (per_cu->mark
4188 ? visited_found.get ()
4189 : visited_not_found.get (),
4190 file_data, INSERT);
4191 *slot = file_data;
4192 }
4193 }
4194
4195 bool
4196 dwarf2_gdb_index::expand_symtabs_matching
4197 (struct objfile *objfile,
4198 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4199 const lookup_name_info *lookup_name,
4200 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4201 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4202 block_search_flags search_flags,
4203 domain_enum domain,
4204 enum search_domain kind)
4205 {
4206 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4207
4208 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4209
4210 /* This invariant is documented in quick-functions.h. */
4211 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
4212 if (lookup_name == nullptr)
4213 {
4214 for (dwarf2_per_cu_data *per_cu
4215 : all_units_range (per_objfile->per_bfd))
4216 {
4217 QUIT;
4218
4219 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4220 file_matcher,
4221 expansion_notify))
4222 return false;
4223 }
4224 return true;
4225 }
4226
4227 mapped_index &index
4228 = *(gdb::checked_static_cast<mapped_index *>
4229 (per_objfile->per_bfd->index_table.get ()));
4230
4231 bool result
4232 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4233 symbol_matcher,
4234 [&] (offset_type idx)
4235 {
4236 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4237 expansion_notify, search_flags, kind))
4238 return false;
4239 return true;
4240 }, per_objfile);
4241
4242 return result;
4243 }
4244
4245 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4246 symtab. */
4247
4248 static struct compunit_symtab *
4249 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4250 CORE_ADDR pc)
4251 {
4252 int i;
4253
4254 if (cust->blockvector () != nullptr
4255 && blockvector_contains_pc (cust->blockvector (), pc))
4256 return cust;
4257
4258 if (cust->includes == NULL)
4259 return NULL;
4260
4261 for (i = 0; cust->includes[i]; ++i)
4262 {
4263 struct compunit_symtab *s = cust->includes[i];
4264
4265 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4266 if (s != NULL)
4267 return s;
4268 }
4269
4270 return NULL;
4271 }
4272
4273 dwarf2_per_cu_data *
4274 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
4275 CORE_ADDR adjusted_pc)
4276 {
4277 if (per_bfd->index_addrmap == nullptr)
4278 return nullptr;
4279 return ((struct dwarf2_per_cu_data *)
4280 per_bfd->index_addrmap->find (adjusted_pc));
4281 }
4282
4283 struct compunit_symtab *
4284 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4285 (struct objfile *objfile,
4286 struct bound_minimal_symbol msymbol,
4287 CORE_ADDR pc,
4288 struct obj_section *section,
4289 int warn_if_readin)
4290 {
4291 struct compunit_symtab *result;
4292
4293 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4294
4295 CORE_ADDR baseaddr = objfile->text_section_offset ();
4296 struct dwarf2_per_cu_data *data = find_per_cu (per_objfile->per_bfd,
4297 pc - baseaddr);
4298 if (data == nullptr)
4299 return nullptr;
4300
4301 if (warn_if_readin && per_objfile->symtab_set_p (data))
4302 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4303 paddress (objfile->arch (), pc));
4304
4305 result = recursively_find_pc_sect_compunit_symtab
4306 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4307
4308 if (warn_if_readin && result == nullptr)
4309 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4310 paddress (objfile->arch (), pc));
4311
4312 return result;
4313 }
4314
4315 void
4316 dwarf2_base_index_functions::map_symbol_filenames
4317 (struct objfile *objfile,
4318 gdb::function_view<symbol_filename_ftype> fun,
4319 bool need_fullname)
4320 {
4321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4322
4323 /* Use caches to ensure we only call FUN once for each filename. */
4324 filename_seen_cache filenames_cache;
4325 std::unordered_set<quick_file_names *> qfn_cache;
4326
4327 /* The rule is CUs specify all the files, including those used by any TU,
4328 so there's no need to scan TUs here. We can ignore file names coming
4329 from already-expanded CUs. It is possible that an expanded CU might
4330 reuse the file names data from a currently unexpanded CU, in this
4331 case we don't want to report the files from the unexpanded CU. */
4332
4333 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4334 {
4335 if (!per_cu->is_debug_types
4336 && per_objfile->symtab_set_p (per_cu.get ()))
4337 {
4338 if (per_cu->file_names != nullptr)
4339 qfn_cache.insert (per_cu->file_names);
4340 }
4341 }
4342
4343 for (dwarf2_per_cu_data *per_cu
4344 : all_units_range (per_objfile->per_bfd))
4345 {
4346 /* We only need to look at symtabs not already expanded. */
4347 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
4348 continue;
4349
4350 if (per_cu->fnd != nullptr)
4351 {
4352 file_and_directory *fnd = per_cu->fnd.get ();
4353
4354 const char *filename = fnd->get_name ();
4355 const char *key = filename;
4356 const char *fullname = nullptr;
4357
4358 if (need_fullname)
4359 {
4360 fullname = fnd->get_fullname ();
4361 key = fullname;
4362 }
4363
4364 if (!filenames_cache.seen (key))
4365 fun (filename, fullname);
4366 }
4367
4368 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4369 if (file_data == nullptr
4370 || qfn_cache.find (file_data) != qfn_cache.end ())
4371 continue;
4372
4373 for (int j = 0; j < file_data->num_file_names; ++j)
4374 {
4375 const char *filename = file_data->file_names[j];
4376 const char *key = filename;
4377 const char *fullname = nullptr;
4378
4379 if (need_fullname)
4380 {
4381 fullname = dw2_get_real_path (per_objfile, file_data, j);
4382 key = fullname;
4383 }
4384
4385 if (!filenames_cache.seen (key))
4386 fun (filename, fullname);
4387 }
4388 }
4389 }
4390
4391 bool
4392 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4393 {
4394 return true;
4395 }
4396
4397 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4398
4399 bool
4400 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
4401 {
4402 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4403
4404 for (const auto &per_cu : per_objfile->per_bfd->all_units)
4405 {
4406 /* Is this already expanded? */
4407 if (per_objfile->symtab_set_p (per_cu.get ()))
4408 continue;
4409
4410 /* It has not yet been expanded. */
4411 return true;
4412 }
4413
4414 return false;
4415 }
4416
4417 /* DWARF-5 debug_names reader. */
4418
4419 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4420 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4421
4422 /* A helper function that reads the .debug_names section in SECTION
4423 and fills in MAP. FILENAME is the name of the file containing the
4424 section; it is used for error reporting.
4425
4426 Returns true if all went well, false otherwise. */
4427
4428 static bool
4429 read_debug_names_from_section (struct objfile *objfile,
4430 const char *filename,
4431 struct dwarf2_section_info *section,
4432 mapped_debug_names &map)
4433 {
4434 if (section->empty ())
4435 return false;
4436
4437 /* Older elfutils strip versions could keep the section in the main
4438 executable while splitting it for the separate debug info file. */
4439 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4440 return false;
4441
4442 section->read (objfile);
4443
4444 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4445
4446 const gdb_byte *addr = section->buffer;
4447
4448 bfd *const abfd = section->get_bfd_owner ();
4449
4450 unsigned int bytes_read;
4451 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4452 addr += bytes_read;
4453
4454 map.dwarf5_is_dwarf64 = bytes_read != 4;
4455 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4456 if (bytes_read + length != section->size)
4457 {
4458 /* There may be multiple per-CU indices. */
4459 warning (_("Section .debug_names in %s length %s does not match "
4460 "section length %s, ignoring .debug_names."),
4461 filename, plongest (bytes_read + length),
4462 pulongest (section->size));
4463 return false;
4464 }
4465
4466 /* The version number. */
4467 uint16_t version = read_2_bytes (abfd, addr);
4468 addr += 2;
4469 if (version != 5)
4470 {
4471 warning (_("Section .debug_names in %s has unsupported version %d, "
4472 "ignoring .debug_names."),
4473 filename, version);
4474 return false;
4475 }
4476
4477 /* Padding. */
4478 uint16_t padding = read_2_bytes (abfd, addr);
4479 addr += 2;
4480 if (padding != 0)
4481 {
4482 warning (_("Section .debug_names in %s has unsupported padding %d, "
4483 "ignoring .debug_names."),
4484 filename, padding);
4485 return false;
4486 }
4487
4488 /* comp_unit_count - The number of CUs in the CU list. */
4489 map.cu_count = read_4_bytes (abfd, addr);
4490 addr += 4;
4491
4492 /* local_type_unit_count - The number of TUs in the local TU
4493 list. */
4494 map.tu_count = read_4_bytes (abfd, addr);
4495 addr += 4;
4496
4497 /* foreign_type_unit_count - The number of TUs in the foreign TU
4498 list. */
4499 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4500 addr += 4;
4501 if (foreign_tu_count != 0)
4502 {
4503 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4504 "ignoring .debug_names."),
4505 filename, static_cast<unsigned long> (foreign_tu_count));
4506 return false;
4507 }
4508
4509 /* bucket_count - The number of hash buckets in the hash lookup
4510 table. */
4511 map.bucket_count = read_4_bytes (abfd, addr);
4512 addr += 4;
4513
4514 /* name_count - The number of unique names in the index. */
4515 map.name_count = read_4_bytes (abfd, addr);
4516 addr += 4;
4517
4518 /* abbrev_table_size - The size in bytes of the abbreviations
4519 table. */
4520 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4521 addr += 4;
4522
4523 /* augmentation_string_size - The size in bytes of the augmentation
4524 string. This value is rounded up to a multiple of 4. */
4525 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4526 addr += 4;
4527 map.augmentation_is_gdb = ((augmentation_string_size
4528 == sizeof (dwarf5_augmentation))
4529 && memcmp (addr, dwarf5_augmentation,
4530 sizeof (dwarf5_augmentation)) == 0);
4531 augmentation_string_size += (-augmentation_string_size) & 3;
4532 addr += augmentation_string_size;
4533
4534 /* List of CUs */
4535 map.cu_table_reordered = addr;
4536 addr += map.cu_count * map.offset_size;
4537
4538 /* List of Local TUs */
4539 map.tu_table_reordered = addr;
4540 addr += map.tu_count * map.offset_size;
4541
4542 /* Hash Lookup Table */
4543 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4544 addr += map.bucket_count * 4;
4545 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4546 addr += map.name_count * 4;
4547
4548 /* Name Table */
4549 map.name_table_string_offs_reordered = addr;
4550 addr += map.name_count * map.offset_size;
4551 map.name_table_entry_offs_reordered = addr;
4552 addr += map.name_count * map.offset_size;
4553
4554 const gdb_byte *abbrev_table_start = addr;
4555 for (;;)
4556 {
4557 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4558 addr += bytes_read;
4559 if (index_num == 0)
4560 break;
4561
4562 const auto insertpair
4563 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4564 if (!insertpair.second)
4565 {
4566 warning (_("Section .debug_names in %s has duplicate index %s, "
4567 "ignoring .debug_names."),
4568 filename, pulongest (index_num));
4569 return false;
4570 }
4571 mapped_debug_names::index_val &indexval = insertpair.first->second;
4572 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4573 addr += bytes_read;
4574
4575 for (;;)
4576 {
4577 mapped_debug_names::index_val::attr attr;
4578 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4579 addr += bytes_read;
4580 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4581 addr += bytes_read;
4582 if (attr.form == DW_FORM_implicit_const)
4583 {
4584 attr.implicit_const = read_signed_leb128 (abfd, addr,
4585 &bytes_read);
4586 addr += bytes_read;
4587 }
4588 if (attr.dw_idx == 0 && attr.form == 0)
4589 break;
4590 indexval.attr_vec.push_back (std::move (attr));
4591 }
4592 }
4593 if (addr != abbrev_table_start + abbrev_table_size)
4594 {
4595 warning (_("Section .debug_names in %s has abbreviation_table "
4596 "of size %s vs. written as %u, ignoring .debug_names."),
4597 filename, plongest (addr - abbrev_table_start),
4598 abbrev_table_size);
4599 return false;
4600 }
4601 map.entry_pool = addr;
4602
4603 return true;
4604 }
4605
4606 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4607 list. */
4608
4609 static bool
4610 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4611 const mapped_debug_names &map,
4612 dwarf2_section_info &section,
4613 bool is_dwz)
4614 {
4615 if (!map.augmentation_is_gdb)
4616 {
4617 for (uint32_t i = 0; i < map.cu_count; ++i)
4618 {
4619 sect_offset sect_off
4620 = (sect_offset) (extract_unsigned_integer
4621 (map.cu_table_reordered + i * map.offset_size,
4622 map.offset_size,
4623 map.dwarf5_byte_order));
4624 /* We don't know the length of the CU, because the CU list in a
4625 .debug_names index can be incomplete, so we can't use the start
4626 of the next CU as end of this CU. We create the CUs here with
4627 length 0, and in cutu_reader::cutu_reader we'll fill in the
4628 actual length. */
4629 dwarf2_per_cu_data_up per_cu
4630 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4631 sect_off, 0);
4632 per_bfd->all_units.push_back (std::move (per_cu));
4633 }
4634 return true;
4635 }
4636
4637 sect_offset sect_off_prev;
4638 for (uint32_t i = 0; i <= map.cu_count; ++i)
4639 {
4640 sect_offset sect_off_next;
4641 if (i < map.cu_count)
4642 {
4643 sect_off_next
4644 = (sect_offset) (extract_unsigned_integer
4645 (map.cu_table_reordered + i * map.offset_size,
4646 map.offset_size,
4647 map.dwarf5_byte_order));
4648 }
4649 else
4650 sect_off_next = (sect_offset) section.size;
4651 if (i >= 1)
4652 {
4653 if (sect_off_next == sect_off_prev)
4654 {
4655 warning (_("Section .debug_names has duplicate entry in CU table,"
4656 " ignoring .debug_names."));
4657 return false;
4658 }
4659 if (sect_off_next < sect_off_prev)
4660 {
4661 warning (_("Section .debug_names has non-ascending CU table,"
4662 " ignoring .debug_names."));
4663 return false;
4664 }
4665 /* Note: we're not using length = sect_off_next - sect_off_prev,
4666 to gracefully handle an incomplete CU list. */
4667 const ULONGEST length = 0;
4668 dwarf2_per_cu_data_up per_cu
4669 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4670 sect_off_prev, length);
4671 per_bfd->all_units.push_back (std::move (per_cu));
4672 }
4673 sect_off_prev = sect_off_next;
4674 }
4675
4676 return true;
4677 }
4678
4679 /* Read the CU list from the mapped index, and use it to create all
4680 the CU objects for this dwarf2_per_objfile. */
4681
4682 static bool
4683 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4684 const mapped_debug_names &map,
4685 const mapped_debug_names &dwz_map)
4686 {
4687 gdb_assert (per_bfd->all_units.empty ());
4688 per_bfd->all_units.reserve (map.cu_count + dwz_map.cu_count);
4689
4690 if (!create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4691 false /* is_dwz */))
4692 return false;
4693
4694 if (dwz_map.cu_count == 0)
4695 return true;
4696
4697 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4698 return create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4699 true /* is_dwz */);
4700 }
4701
4702 /* Read .debug_names. If everything went ok, initialize the "quick"
4703 elements of all the CUs and return true. Otherwise, return false. */
4704
4705 static bool
4706 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4707 {
4708 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4709 mapped_debug_names dwz_map;
4710 struct objfile *objfile = per_objfile->objfile;
4711 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4712
4713 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4714 &per_bfd->debug_names, *map))
4715 return false;
4716
4717 /* Don't use the index if it's empty. */
4718 if (map->name_count == 0)
4719 return false;
4720
4721 /* If there is a .dwz file, read it so we can get its CU list as
4722 well. */
4723 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4724 if (dwz != NULL)
4725 {
4726 if (!read_debug_names_from_section (objfile,
4727 bfd_get_filename (dwz->dwz_bfd.get ()),
4728 &dwz->debug_names, dwz_map))
4729 {
4730 warning (_("could not read '.debug_names' section from %s; skipping"),
4731 bfd_get_filename (dwz->dwz_bfd.get ()));
4732 return false;
4733 }
4734 }
4735
4736 if (!create_cus_from_debug_names (per_bfd, *map, dwz_map))
4737 {
4738 per_bfd->all_units.clear ();
4739 return false;
4740 }
4741
4742 if (map->tu_count != 0)
4743 {
4744 /* We can only handle a single .debug_types when we have an
4745 index. */
4746 if (per_bfd->types.size () > 1)
4747 {
4748 per_bfd->all_units.clear ();
4749 return false;
4750 }
4751
4752 dwarf2_section_info *section
4753 = (per_bfd->types.size () == 1
4754 ? &per_bfd->types[0]
4755 : &per_bfd->info);
4756
4757 create_signatured_type_table_from_debug_names
4758 (per_objfile, *map, section, &per_bfd->abbrev);
4759 }
4760
4761 finalize_all_units (per_bfd);
4762
4763 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
4764
4765 per_bfd->index_table = std::move (map);
4766 per_bfd->quick_file_names_table =
4767 create_quick_file_names_table (per_bfd->all_units.size ());
4768
4769 return true;
4770 }
4771
4772 /* Type used to manage iterating over all CUs looking for a symbol for
4773 .debug_names. */
4774
4775 class dw2_debug_names_iterator
4776 {
4777 public:
4778 dw2_debug_names_iterator (const mapped_debug_names &map,
4779 block_search_flags block_index,
4780 domain_enum domain,
4781 const char *name, dwarf2_per_objfile *per_objfile)
4782 : m_map (map), m_block_index (block_index), m_domain (domain),
4783 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
4784 m_per_objfile (per_objfile)
4785 {}
4786
4787 dw2_debug_names_iterator (const mapped_debug_names &map,
4788 search_domain search, uint32_t namei,
4789 dwarf2_per_objfile *per_objfile,
4790 domain_enum domain = UNDEF_DOMAIN)
4791 : m_map (map),
4792 m_domain (domain),
4793 m_search (search),
4794 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4795 m_per_objfile (per_objfile)
4796 {}
4797
4798 dw2_debug_names_iterator (const mapped_debug_names &map,
4799 block_search_flags block_index, domain_enum domain,
4800 uint32_t namei, dwarf2_per_objfile *per_objfile)
4801 : m_map (map), m_block_index (block_index), m_domain (domain),
4802 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
4803 m_per_objfile (per_objfile)
4804 {}
4805
4806 /* Return the next matching CU or NULL if there are no more. */
4807 dwarf2_per_cu_data *next ();
4808
4809 private:
4810 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4811 const char *name,
4812 dwarf2_per_objfile *per_objfile);
4813 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
4814 uint32_t namei,
4815 dwarf2_per_objfile *per_objfile);
4816
4817 /* The internalized form of .debug_names. */
4818 const mapped_debug_names &m_map;
4819
4820 /* Restrict the search to these blocks. */
4821 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
4822 | SEARCH_STATIC_BLOCK);
4823
4824 /* The kind of symbol we're looking for. */
4825 const domain_enum m_domain = UNDEF_DOMAIN;
4826 const search_domain m_search = ALL_DOMAIN;
4827
4828 /* The list of CUs from the index entry of the symbol, or NULL if
4829 not found. */
4830 const gdb_byte *m_addr;
4831
4832 dwarf2_per_objfile *m_per_objfile;
4833 };
4834
4835 const char *
4836 mapped_debug_names::namei_to_name
4837 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
4838 {
4839 const ULONGEST namei_string_offs
4840 = extract_unsigned_integer ((name_table_string_offs_reordered
4841 + namei * offset_size),
4842 offset_size,
4843 dwarf5_byte_order);
4844 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
4845 }
4846
4847 /* Find a slot in .debug_names for the object named NAME. If NAME is
4848 found, return pointer to its pool data. If NAME cannot be found,
4849 return NULL. */
4850
4851 const gdb_byte *
4852 dw2_debug_names_iterator::find_vec_in_debug_names
4853 (const mapped_debug_names &map, const char *name,
4854 dwarf2_per_objfile *per_objfile)
4855 {
4856 int (*cmp) (const char *, const char *);
4857
4858 gdb::unique_xmalloc_ptr<char> without_params;
4859 if (current_language->la_language == language_cplus
4860 || current_language->la_language == language_fortran
4861 || current_language->la_language == language_d)
4862 {
4863 /* NAME is already canonical. Drop any qualifiers as
4864 .debug_names does not contain any. */
4865
4866 if (strchr (name, '(') != NULL)
4867 {
4868 without_params = cp_remove_params (name);
4869 if (without_params != NULL)
4870 name = without_params.get ();
4871 }
4872 }
4873
4874 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
4875
4876 const uint32_t full_hash = dwarf5_djb_hash (name);
4877 uint32_t namei
4878 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4879 (map.bucket_table_reordered
4880 + (full_hash % map.bucket_count)), 4,
4881 map.dwarf5_byte_order);
4882 if (namei == 0)
4883 return NULL;
4884 --namei;
4885 if (namei >= map.name_count)
4886 {
4887 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4888 "[in module %s]"),
4889 namei, map.name_count,
4890 objfile_name (per_objfile->objfile));
4891 return NULL;
4892 }
4893
4894 for (;;)
4895 {
4896 const uint32_t namei_full_hash
4897 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
4898 (map.hash_table_reordered + namei), 4,
4899 map.dwarf5_byte_order);
4900 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
4901 return NULL;
4902
4903 if (full_hash == namei_full_hash)
4904 {
4905 const char *const namei_string = map.namei_to_name (namei, per_objfile);
4906
4907 #if 0 /* An expensive sanity check. */
4908 if (namei_full_hash != dwarf5_djb_hash (namei_string))
4909 {
4910 complaint (_("Wrong .debug_names hash for string at index %u "
4911 "[in module %s]"),
4912 namei, objfile_name (dwarf2_per_objfile->objfile));
4913 return NULL;
4914 }
4915 #endif
4916
4917 if (cmp (namei_string, name) == 0)
4918 {
4919 const ULONGEST namei_entry_offs
4920 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4921 + namei * map.offset_size),
4922 map.offset_size, map.dwarf5_byte_order);
4923 return map.entry_pool + namei_entry_offs;
4924 }
4925 }
4926
4927 ++namei;
4928 if (namei >= map.name_count)
4929 return NULL;
4930 }
4931 }
4932
4933 const gdb_byte *
4934 dw2_debug_names_iterator::find_vec_in_debug_names
4935 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
4936 {
4937 if (namei >= map.name_count)
4938 {
4939 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4940 "[in module %s]"),
4941 namei, map.name_count,
4942 objfile_name (per_objfile->objfile));
4943 return NULL;
4944 }
4945
4946 const ULONGEST namei_entry_offs
4947 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
4948 + namei * map.offset_size),
4949 map.offset_size, map.dwarf5_byte_order);
4950 return map.entry_pool + namei_entry_offs;
4951 }
4952
4953 /* See dw2_debug_names_iterator. */
4954
4955 dwarf2_per_cu_data *
4956 dw2_debug_names_iterator::next ()
4957 {
4958 if (m_addr == NULL)
4959 return NULL;
4960
4961 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4962 struct objfile *objfile = m_per_objfile->objfile;
4963 bfd *const abfd = objfile->obfd.get ();
4964
4965 again:
4966
4967 unsigned int bytes_read;
4968 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
4969 m_addr += bytes_read;
4970 if (abbrev == 0)
4971 return NULL;
4972
4973 const auto indexval_it = m_map.abbrev_map.find (abbrev);
4974 if (indexval_it == m_map.abbrev_map.cend ())
4975 {
4976 complaint (_("Wrong .debug_names undefined abbrev code %s "
4977 "[in module %s]"),
4978 pulongest (abbrev), objfile_name (objfile));
4979 return NULL;
4980 }
4981 const mapped_debug_names::index_val &indexval = indexval_it->second;
4982 enum class symbol_linkage {
4983 unknown,
4984 static_,
4985 extern_,
4986 } symbol_linkage_ = symbol_linkage::unknown;
4987 dwarf2_per_cu_data *per_cu = NULL;
4988 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
4989 {
4990 ULONGEST ull;
4991 switch (attr.form)
4992 {
4993 case DW_FORM_implicit_const:
4994 ull = attr.implicit_const;
4995 break;
4996 case DW_FORM_flag_present:
4997 ull = 1;
4998 break;
4999 case DW_FORM_udata:
5000 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5001 m_addr += bytes_read;
5002 break;
5003 case DW_FORM_ref4:
5004 ull = read_4_bytes (abfd, m_addr);
5005 m_addr += 4;
5006 break;
5007 case DW_FORM_ref8:
5008 ull = read_8_bytes (abfd, m_addr);
5009 m_addr += 8;
5010 break;
5011 case DW_FORM_ref_sig8:
5012 ull = read_8_bytes (abfd, m_addr);
5013 m_addr += 8;
5014 break;
5015 default:
5016 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5017 dwarf_form_name (attr.form),
5018 objfile_name (objfile));
5019 return NULL;
5020 }
5021 switch (attr.dw_idx)
5022 {
5023 case DW_IDX_compile_unit:
5024 {
5025 /* Don't crash on bad data. */
5026 if (ull >= per_bfd->all_comp_units.size ())
5027 {
5028 complaint (_(".debug_names entry has bad CU index %s"
5029 " [in module %s]"),
5030 pulongest (ull),
5031 objfile_name (objfile));
5032 continue;
5033 }
5034 }
5035 per_cu = per_bfd->get_cu (ull);
5036 break;
5037 case DW_IDX_type_unit:
5038 /* Don't crash on bad data. */
5039 if (ull >= per_bfd->all_type_units.size ())
5040 {
5041 complaint (_(".debug_names entry has bad TU index %s"
5042 " [in module %s]"),
5043 pulongest (ull),
5044 objfile_name (objfile));
5045 continue;
5046 }
5047 {
5048 int nr_cus = per_bfd->all_comp_units.size ();
5049 per_cu = per_bfd->get_cu (nr_cus + ull);
5050 }
5051 break;
5052 case DW_IDX_die_offset:
5053 /* In a per-CU index (as opposed to a per-module index), index
5054 entries without CU attribute implicitly refer to the single CU. */
5055 if (per_cu == NULL)
5056 per_cu = per_bfd->get_cu (0);
5057 break;
5058 case DW_IDX_GNU_internal:
5059 if (!m_map.augmentation_is_gdb)
5060 break;
5061 symbol_linkage_ = symbol_linkage::static_;
5062 break;
5063 case DW_IDX_GNU_external:
5064 if (!m_map.augmentation_is_gdb)
5065 break;
5066 symbol_linkage_ = symbol_linkage::extern_;
5067 break;
5068 }
5069 }
5070
5071 /* Skip if we couldn't find a valid CU/TU index. */
5072 if (per_cu == nullptr)
5073 goto again;
5074
5075 /* Skip if already read in. */
5076 if (m_per_objfile->symtab_set_p (per_cu))
5077 goto again;
5078
5079 /* Check static vs global. */
5080 if (symbol_linkage_ != symbol_linkage::unknown)
5081 {
5082 if (symbol_linkage_ == symbol_linkage::static_)
5083 {
5084 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5085 goto again;
5086 }
5087 else
5088 {
5089 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5090 goto again;
5091 }
5092 }
5093
5094 /* Match dw2_symtab_iter_next, symbol_kind
5095 and debug_names::psymbol_tag. */
5096 switch (m_domain)
5097 {
5098 case VAR_DOMAIN:
5099 switch (indexval.dwarf_tag)
5100 {
5101 case DW_TAG_variable:
5102 case DW_TAG_subprogram:
5103 /* Some types are also in VAR_DOMAIN. */
5104 case DW_TAG_typedef:
5105 case DW_TAG_structure_type:
5106 break;
5107 default:
5108 goto again;
5109 }
5110 break;
5111 case STRUCT_DOMAIN:
5112 switch (indexval.dwarf_tag)
5113 {
5114 case DW_TAG_typedef:
5115 case DW_TAG_structure_type:
5116 break;
5117 default:
5118 goto again;
5119 }
5120 break;
5121 case LABEL_DOMAIN:
5122 switch (indexval.dwarf_tag)
5123 {
5124 case 0:
5125 case DW_TAG_variable:
5126 break;
5127 default:
5128 goto again;
5129 }
5130 break;
5131 case MODULE_DOMAIN:
5132 switch (indexval.dwarf_tag)
5133 {
5134 case DW_TAG_module:
5135 break;
5136 default:
5137 goto again;
5138 }
5139 break;
5140 default:
5141 break;
5142 }
5143
5144 /* Match dw2_expand_symtabs_matching, symbol_kind and
5145 debug_names::psymbol_tag. */
5146 switch (m_search)
5147 {
5148 case VARIABLES_DOMAIN:
5149 switch (indexval.dwarf_tag)
5150 {
5151 case DW_TAG_variable:
5152 break;
5153 default:
5154 goto again;
5155 }
5156 break;
5157 case FUNCTIONS_DOMAIN:
5158 switch (indexval.dwarf_tag)
5159 {
5160 case DW_TAG_subprogram:
5161 break;
5162 default:
5163 goto again;
5164 }
5165 break;
5166 case TYPES_DOMAIN:
5167 switch (indexval.dwarf_tag)
5168 {
5169 case DW_TAG_typedef:
5170 case DW_TAG_structure_type:
5171 break;
5172 default:
5173 goto again;
5174 }
5175 break;
5176 case MODULES_DOMAIN:
5177 switch (indexval.dwarf_tag)
5178 {
5179 case DW_TAG_module:
5180 break;
5181 default:
5182 goto again;
5183 }
5184 default:
5185 break;
5186 }
5187
5188 return per_cu;
5189 }
5190
5191 /* This dumps minimal information about .debug_names. It is called
5192 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5193 uses this to verify that .debug_names has been loaded. */
5194
5195 void
5196 dwarf2_debug_names_index::dump (struct objfile *objfile)
5197 {
5198 gdb_printf (".debug_names: exists\n");
5199 }
5200
5201 void
5202 dwarf2_debug_names_index::expand_matching_symbols
5203 (struct objfile *objfile,
5204 const lookup_name_info &name, domain_enum domain,
5205 int global,
5206 symbol_compare_ftype *ordered_compare)
5207 {
5208 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5209
5210 mapped_debug_names &map
5211 = *(gdb::checked_static_cast<mapped_debug_names *>
5212 (per_objfile->per_bfd->index_table.get ()));
5213 const block_search_flags block_flags
5214 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5215
5216 const char *match_name = name.ada ().lookup_name ().c_str ();
5217 auto matcher = [&] (const char *symname)
5218 {
5219 if (ordered_compare == nullptr)
5220 return true;
5221 return ordered_compare (symname, match_name) == 0;
5222 };
5223
5224 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5225 [&] (offset_type namei)
5226 {
5227 /* The name was matched, now expand corresponding CUs that were
5228 marked. */
5229 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5230 per_objfile);
5231
5232 struct dwarf2_per_cu_data *per_cu;
5233 while ((per_cu = iter.next ()) != NULL)
5234 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5235 nullptr);
5236 return true;
5237 }, per_objfile);
5238 }
5239
5240 bool
5241 dwarf2_debug_names_index::expand_symtabs_matching
5242 (struct objfile *objfile,
5243 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5244 const lookup_name_info *lookup_name,
5245 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5246 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5247 block_search_flags search_flags,
5248 domain_enum domain,
5249 enum search_domain kind)
5250 {
5251 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5252
5253 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5254
5255 /* This invariant is documented in quick-functions.h. */
5256 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
5257 if (lookup_name == nullptr)
5258 {
5259 for (dwarf2_per_cu_data *per_cu
5260 : all_units_range (per_objfile->per_bfd))
5261 {
5262 QUIT;
5263
5264 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5265 file_matcher,
5266 expansion_notify))
5267 return false;
5268 }
5269 return true;
5270 }
5271
5272 mapped_debug_names &map
5273 = *(gdb::checked_static_cast<mapped_debug_names *>
5274 (per_objfile->per_bfd->index_table.get ()));
5275
5276 bool result
5277 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5278 symbol_matcher,
5279 [&] (offset_type namei)
5280 {
5281 /* The name was matched, now expand corresponding CUs that were
5282 marked. */
5283 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5284
5285 struct dwarf2_per_cu_data *per_cu;
5286 while ((per_cu = iter.next ()) != NULL)
5287 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5288 file_matcher,
5289 expansion_notify))
5290 return false;
5291 return true;
5292 }, per_objfile);
5293
5294 return result;
5295 }
5296
5297 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5298 to either a dwarf2_per_bfd or dwz_file object. */
5299
5300 template <typename T>
5301 static gdb::array_view<const gdb_byte>
5302 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5303 {
5304 dwarf2_section_info *section = &section_owner->gdb_index;
5305
5306 if (section->empty ())
5307 return {};
5308
5309 /* Older elfutils strip versions could keep the section in the main
5310 executable while splitting it for the separate debug info file. */
5311 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5312 return {};
5313
5314 section->read (obj);
5315
5316 /* dwarf2_section_info::size is a bfd_size_type, while
5317 gdb::array_view works with size_t. On 32-bit hosts, with
5318 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5319 is 32-bit. So we need an explicit narrowing conversion here.
5320 This is fine, because it's impossible to allocate or mmap an
5321 array/buffer larger than what size_t can represent. */
5322 return gdb::make_array_view (section->buffer, section->size);
5323 }
5324
5325 /* Lookup the index cache for the contents of the index associated to
5326 DWARF2_OBJ. */
5327
5328 static gdb::array_view<const gdb_byte>
5329 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5330 {
5331 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
5332 if (build_id == nullptr)
5333 return {};
5334
5335 return global_index_cache.lookup_gdb_index (build_id,
5336 &dwarf2_per_bfd->index_cache_res);
5337 }
5338
5339 /* Same as the above, but for DWZ. */
5340
5341 static gdb::array_view<const gdb_byte>
5342 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5343 {
5344 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5345 if (build_id == nullptr)
5346 return {};
5347
5348 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5349 }
5350
5351 static quick_symbol_functions_up make_cooked_index_funcs ();
5352
5353 /* See dwarf2/public.h. */
5354
5355 void
5356 dwarf2_initialize_objfile (struct objfile *objfile)
5357 {
5358 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5359 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5360
5361 dwarf_read_debug_printf ("called");
5362
5363 /* If we're about to read full symbols, don't bother with the
5364 indices. In this case we also don't care if some other debug
5365 format is making psymtabs, because they are all about to be
5366 expanded anyway. */
5367 if ((objfile->flags & OBJF_READNOW))
5368 {
5369 dwarf_read_debug_printf ("readnow requested");
5370
5371 create_all_units (per_objfile);
5372 per_bfd->quick_file_names_table
5373 = create_quick_file_names_table (per_bfd->all_units.size ());
5374
5375 objfile->qf.emplace_front (new readnow_functions);
5376 return;
5377 }
5378
5379 /* Was a GDB index already read when we processed an objfile sharing
5380 PER_BFD? */
5381 if (per_bfd->index_table != nullptr)
5382 {
5383 dwarf_read_debug_printf ("re-using symbols");
5384 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5385 return;
5386 }
5387
5388 if (dwarf2_read_debug_names (per_objfile))
5389 {
5390 dwarf_read_debug_printf ("found debug names");
5391 objfile->qf.push_front
5392 (per_bfd->index_table->make_quick_functions ());
5393 return;
5394 }
5395
5396 if (dwarf2_read_gdb_index (per_objfile,
5397 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5398 get_gdb_index_contents_from_section<dwz_file>))
5399 {
5400 dwarf_read_debug_printf ("found gdb index from file");
5401 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5402 return;
5403 }
5404
5405 /* ... otherwise, try to find the index in the index cache. */
5406 if (dwarf2_read_gdb_index (per_objfile,
5407 get_gdb_index_contents_from_cache,
5408 get_gdb_index_contents_from_cache_dwz))
5409 {
5410 dwarf_read_debug_printf ("found gdb index from cache");
5411 global_index_cache.hit ();
5412 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
5413 return;
5414 }
5415
5416 global_index_cache.miss ();
5417 objfile->qf.push_front (make_cooked_index_funcs ());
5418 }
5419
5420 \f
5421
5422 /* Build a partial symbol table. */
5423
5424 static void
5425 dwarf2_build_psymtabs (struct objfile *objfile)
5426 {
5427 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5428
5429 if (per_objfile->per_bfd->index_table != nullptr)
5430 return;
5431
5432 try
5433 {
5434 dwarf2_build_psymtabs_hard (per_objfile);
5435
5436 /* (maybe) store an index in the cache. */
5437 global_index_cache.store (per_objfile);
5438 }
5439 catch (const gdb_exception_error &except)
5440 {
5441 exception_print (gdb_stderr, except);
5442 }
5443 }
5444
5445 /* Find the base address of the compilation unit for range lists and
5446 location lists. It will normally be specified by DW_AT_low_pc.
5447 In DWARF-3 draft 4, the base address could be overridden by
5448 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5449 compilation units with discontinuous ranges. */
5450
5451 static void
5452 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5453 {
5454 struct attribute *attr;
5455
5456 cu->base_address.reset ();
5457
5458 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5459 if (attr != nullptr)
5460 cu->base_address = attr->as_address ();
5461 else
5462 {
5463 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5464 if (attr != nullptr)
5465 cu->base_address = attr->as_address ();
5466 }
5467 }
5468
5469 /* Helper function that returns the proper abbrev section for
5470 THIS_CU. */
5471
5472 static struct dwarf2_section_info *
5473 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5474 {
5475 struct dwarf2_section_info *abbrev;
5476 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5477
5478 if (this_cu->is_dwz)
5479 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5480 else
5481 abbrev = &per_bfd->abbrev;
5482
5483 return abbrev;
5484 }
5485
5486 /* Fetch the abbreviation table offset from a comp or type unit header. */
5487
5488 static sect_offset
5489 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5490 struct dwarf2_section_info *section,
5491 sect_offset sect_off)
5492 {
5493 bfd *abfd = section->get_bfd_owner ();
5494 const gdb_byte *info_ptr;
5495 unsigned int initial_length_size, offset_size;
5496 uint16_t version;
5497
5498 section->read (per_objfile->objfile);
5499 info_ptr = section->buffer + to_underlying (sect_off);
5500 read_initial_length (abfd, info_ptr, &initial_length_size);
5501 offset_size = initial_length_size == 4 ? 4 : 8;
5502 info_ptr += initial_length_size;
5503
5504 version = read_2_bytes (abfd, info_ptr);
5505 info_ptr += 2;
5506 if (version >= 5)
5507 {
5508 /* Skip unit type and address size. */
5509 info_ptr += 2;
5510 }
5511
5512 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5513 }
5514
5515 static hashval_t
5516 hash_signatured_type (const void *item)
5517 {
5518 const struct signatured_type *sig_type
5519 = (const struct signatured_type *) item;
5520
5521 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5522 return sig_type->signature;
5523 }
5524
5525 static int
5526 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5527 {
5528 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5529 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5530
5531 return lhs->signature == rhs->signature;
5532 }
5533
5534 /* Allocate a hash table for signatured types. */
5535
5536 static htab_up
5537 allocate_signatured_type_table ()
5538 {
5539 return htab_up (htab_create_alloc (41,
5540 hash_signatured_type,
5541 eq_signatured_type,
5542 NULL, xcalloc, xfree));
5543 }
5544
5545 /* A helper for create_debug_types_hash_table. Read types from SECTION
5546 and fill them into TYPES_HTAB. It will process only type units,
5547 therefore DW_UT_type. */
5548
5549 static void
5550 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
5551 struct dwo_file *dwo_file,
5552 dwarf2_section_info *section, htab_up &types_htab,
5553 rcuh_kind section_kind)
5554 {
5555 struct objfile *objfile = per_objfile->objfile;
5556 struct dwarf2_section_info *abbrev_section;
5557 bfd *abfd;
5558 const gdb_byte *info_ptr, *end_ptr;
5559
5560 abbrev_section = &dwo_file->sections.abbrev;
5561
5562 dwarf_read_debug_printf ("Reading %s for %s",
5563 section->get_name (),
5564 abbrev_section->get_file_name ());
5565
5566 section->read (objfile);
5567 info_ptr = section->buffer;
5568
5569 if (info_ptr == NULL)
5570 return;
5571
5572 /* We can't set abfd until now because the section may be empty or
5573 not present, in which case the bfd is unknown. */
5574 abfd = section->get_bfd_owner ();
5575
5576 /* We don't use cutu_reader here because we don't need to read
5577 any dies: the signature is in the header. */
5578
5579 end_ptr = info_ptr + section->size;
5580 while (info_ptr < end_ptr)
5581 {
5582 signatured_type_up sig_type;
5583 struct dwo_unit *dwo_tu;
5584 void **slot;
5585 const gdb_byte *ptr = info_ptr;
5586 struct comp_unit_head header;
5587 unsigned int length;
5588
5589 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
5590
5591 /* Initialize it due to a false compiler warning. */
5592 header.signature = -1;
5593 header.type_cu_offset_in_tu = (cu_offset) -1;
5594
5595 /* We need to read the type's signature in order to build the hash
5596 table, but we don't need anything else just yet. */
5597
5598 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
5599 abbrev_section, ptr, section_kind);
5600
5601 length = header.get_length_with_initial ();
5602
5603 /* Skip dummy type units. */
5604 if (ptr >= info_ptr + length
5605 || peek_abbrev_code (abfd, ptr) == 0
5606 || (header.unit_type != DW_UT_type
5607 && header.unit_type != DW_UT_split_type))
5608 {
5609 info_ptr += length;
5610 continue;
5611 }
5612
5613 if (types_htab == NULL)
5614 types_htab = allocate_dwo_unit_table ();
5615
5616 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
5617 dwo_tu->dwo_file = dwo_file;
5618 dwo_tu->signature = header.signature;
5619 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
5620 dwo_tu->section = section;
5621 dwo_tu->sect_off = sect_off;
5622 dwo_tu->length = length;
5623
5624 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
5625 gdb_assert (slot != NULL);
5626 if (*slot != NULL)
5627 complaint (_("debug type entry at offset %s is duplicate to"
5628 " the entry at offset %s, signature %s"),
5629 sect_offset_str (sect_off),
5630 sect_offset_str (dwo_tu->sect_off),
5631 hex_string (header.signature));
5632 *slot = dwo_tu;
5633
5634 dwarf_read_debug_printf_v (" offset %s, signature %s",
5635 sect_offset_str (sect_off),
5636 hex_string (header.signature));
5637
5638 info_ptr += length;
5639 }
5640 }
5641
5642 /* Create the hash table of all entries in the .debug_types
5643 (or .debug_types.dwo) section(s).
5644 DWO_FILE is a pointer to the DWO file object.
5645
5646 The result is a pointer to the hash table or NULL if there are no types.
5647
5648 Note: This function processes DWO files only, not DWP files. */
5649
5650 static void
5651 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
5652 struct dwo_file *dwo_file,
5653 gdb::array_view<dwarf2_section_info> type_sections,
5654 htab_up &types_htab)
5655 {
5656 for (dwarf2_section_info &section : type_sections)
5657 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
5658 rcuh_kind::TYPE);
5659 }
5660
5661 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5662 If SLOT is non-NULL, it is the entry to use in the hash table.
5663 Otherwise we find one. */
5664
5665 static struct signatured_type *
5666 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
5667 {
5668 if (per_objfile->per_bfd->all_units.size ()
5669 == per_objfile->per_bfd->all_units.capacity ())
5670 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
5671
5672 signatured_type_up sig_type_holder
5673 = per_objfile->per_bfd->allocate_signatured_type (sig);
5674 signatured_type *sig_type = sig_type_holder.get ();
5675
5676 per_objfile->per_bfd->all_units.emplace_back
5677 (sig_type_holder.release ());
5678
5679 if (slot == NULL)
5680 {
5681 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5682 sig_type, INSERT);
5683 }
5684 gdb_assert (*slot == NULL);
5685 *slot = sig_type;
5686 /* The rest of sig_type must be filled in by the caller. */
5687 return sig_type;
5688 }
5689
5690 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5691 Fill in SIG_ENTRY with DWO_ENTRY. */
5692
5693 static void
5694 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
5695 struct signatured_type *sig_entry,
5696 struct dwo_unit *dwo_entry)
5697 {
5698 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5699
5700 /* Make sure we're not clobbering something we don't expect to. */
5701 gdb_assert (! sig_entry->queued);
5702 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
5703 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
5704 gdb_assert (sig_entry->signature == dwo_entry->signature);
5705 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
5706 || (to_underlying (sig_entry->type_offset_in_section)
5707 == to_underlying (dwo_entry->type_offset_in_tu)));
5708 gdb_assert (sig_entry->type_unit_group == NULL);
5709 gdb_assert (sig_entry->dwo_unit == NULL
5710 || sig_entry->dwo_unit == dwo_entry);
5711
5712 sig_entry->section = dwo_entry->section;
5713 sig_entry->sect_off = dwo_entry->sect_off;
5714 sig_entry->set_length (dwo_entry->length, false);
5715 sig_entry->reading_dwo_directly = 1;
5716 sig_entry->per_bfd = per_bfd;
5717 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5718 sig_entry->dwo_unit = dwo_entry;
5719 }
5720
5721 /* Subroutine of lookup_signatured_type.
5722 If we haven't read the TU yet, create the signatured_type data structure
5723 for a TU to be read in directly from a DWO file, bypassing the stub.
5724 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5725 using .gdb_index, then when reading a CU we want to stay in the DWO file
5726 containing that CU. Otherwise we could end up reading several other DWO
5727 files (due to comdat folding) to process the transitive closure of all the
5728 mentioned TUs, and that can be slow. The current DWO file will have every
5729 type signature that it needs.
5730 We only do this for .gdb_index because in the psymtab case we already have
5731 to read all the DWOs to build the type unit groups. */
5732
5733 static struct signatured_type *
5734 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5735 {
5736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5737 struct dwo_file *dwo_file;
5738 struct dwo_unit find_dwo_entry, *dwo_entry;
5739 void **slot;
5740
5741 gdb_assert (cu->dwo_unit);
5742
5743 /* If TU skeletons have been removed then we may not have read in any
5744 TUs yet. */
5745 if (per_objfile->per_bfd->signatured_types == NULL)
5746 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5747
5748 /* We only ever need to read in one copy of a signatured type.
5749 Use the global signatured_types array to do our own comdat-folding
5750 of types. If this is the first time we're reading this TU, and
5751 the TU has an entry in .gdb_index, replace the recorded data from
5752 .gdb_index with this TU. */
5753
5754 signatured_type find_sig_entry (sig);
5755 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5756 &find_sig_entry, INSERT);
5757 signatured_type *sig_entry = (struct signatured_type *) *slot;
5758
5759 /* We can get here with the TU already read, *or* in the process of being
5760 read. Don't reassign the global entry to point to this DWO if that's
5761 the case. Also note that if the TU is already being read, it may not
5762 have come from a DWO, the program may be a mix of Fission-compiled
5763 code and non-Fission-compiled code. */
5764
5765 /* Have we already tried to read this TU?
5766 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5767 needn't exist in the global table yet). */
5768 if (sig_entry != NULL && sig_entry->tu_read)
5769 return sig_entry;
5770
5771 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5772 dwo_unit of the TU itself. */
5773 dwo_file = cu->dwo_unit->dwo_file;
5774
5775 /* Ok, this is the first time we're reading this TU. */
5776 if (dwo_file->tus == NULL)
5777 return NULL;
5778 find_dwo_entry.signature = sig;
5779 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
5780 &find_dwo_entry);
5781 if (dwo_entry == NULL)
5782 return NULL;
5783
5784 /* If the global table doesn't have an entry for this TU, add one. */
5785 if (sig_entry == NULL)
5786 sig_entry = add_type_unit (per_objfile, sig, slot);
5787
5788 if (sig_entry->dwo_unit == nullptr)
5789 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5790 sig_entry->tu_read = 1;
5791 return sig_entry;
5792 }
5793
5794 /* Subroutine of lookup_signatured_type.
5795 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5796 then try the DWP file. If the TU stub (skeleton) has been removed then
5797 it won't be in .gdb_index. */
5798
5799 static struct signatured_type *
5800 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5801 {
5802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5803 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
5804 struct dwo_unit *dwo_entry;
5805 void **slot;
5806
5807 gdb_assert (cu->dwo_unit);
5808 gdb_assert (dwp_file != NULL);
5809
5810 /* If TU skeletons have been removed then we may not have read in any
5811 TUs yet. */
5812 if (per_objfile->per_bfd->signatured_types == NULL)
5813 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
5814
5815 signatured_type find_sig_entry (sig);
5816 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
5817 &find_sig_entry, INSERT);
5818 signatured_type *sig_entry = (struct signatured_type *) *slot;
5819
5820 /* Have we already tried to read this TU?
5821 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5822 needn't exist in the global table yet). */
5823 if (sig_entry != NULL)
5824 return sig_entry;
5825
5826 if (dwp_file->tus == NULL)
5827 return NULL;
5828 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
5829 1 /* is_debug_types */);
5830 if (dwo_entry == NULL)
5831 return NULL;
5832
5833 sig_entry = add_type_unit (per_objfile, sig, slot);
5834 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
5835
5836 return sig_entry;
5837 }
5838
5839 /* Lookup a signature based type for DW_FORM_ref_sig8.
5840 Returns NULL if signature SIG is not present in the table.
5841 It is up to the caller to complain about this. */
5842
5843 static struct signatured_type *
5844 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5845 {
5846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5847
5848 if (cu->dwo_unit)
5849 {
5850 /* We're in a DWO/DWP file, and we're using .gdb_index.
5851 These cases require special processing. */
5852 if (get_dwp_file (per_objfile) == NULL)
5853 return lookup_dwo_signatured_type (cu, sig);
5854 else
5855 return lookup_dwp_signatured_type (cu, sig);
5856 }
5857 else
5858 {
5859 if (per_objfile->per_bfd->signatured_types == NULL)
5860 return NULL;
5861 signatured_type find_entry (sig);
5862 return ((struct signatured_type *)
5863 htab_find (per_objfile->per_bfd->signatured_types.get (),
5864 &find_entry));
5865 }
5866 }
5867
5868 /* Low level DIE reading support. */
5869
5870 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5871
5872 static void
5873 init_cu_die_reader (struct die_reader_specs *reader,
5874 struct dwarf2_cu *cu,
5875 struct dwarf2_section_info *section,
5876 struct dwo_file *dwo_file,
5877 struct abbrev_table *abbrev_table)
5878 {
5879 gdb_assert (section->readin && section->buffer != NULL);
5880 reader->abfd = section->get_bfd_owner ();
5881 reader->cu = cu;
5882 reader->dwo_file = dwo_file;
5883 reader->die_section = section;
5884 reader->buffer = section->buffer;
5885 reader->buffer_end = section->buffer + section->size;
5886 reader->abbrev_table = abbrev_table;
5887 }
5888
5889 /* Subroutine of cutu_reader to simplify it.
5890 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5891 There's just a lot of work to do, and cutu_reader is big enough
5892 already.
5893
5894 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5895 from it to the DIE in the DWO. If NULL we are skipping the stub.
5896 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5897 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5898 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5899 STUB_COMP_DIR may be non-NULL.
5900 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5901 are filled in with the info of the DIE from the DWO file.
5902 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5903 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5904 kept around for at least as long as *RESULT_READER.
5905
5906 The result is non-zero if a valid (non-dummy) DIE was found. */
5907
5908 static int
5909 read_cutu_die_from_dwo (dwarf2_cu *cu,
5910 struct dwo_unit *dwo_unit,
5911 struct die_info *stub_comp_unit_die,
5912 const char *stub_comp_dir,
5913 struct die_reader_specs *result_reader,
5914 const gdb_byte **result_info_ptr,
5915 struct die_info **result_comp_unit_die,
5916 abbrev_table_up *result_dwo_abbrev_table)
5917 {
5918 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5919 dwarf2_per_cu_data *per_cu = cu->per_cu;
5920 struct objfile *objfile = per_objfile->objfile;
5921 bfd *abfd;
5922 const gdb_byte *begin_info_ptr, *info_ptr;
5923 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5924 int i,num_extra_attrs;
5925 struct dwarf2_section_info *dwo_abbrev_section;
5926 struct die_info *comp_unit_die;
5927
5928 /* At most one of these may be provided. */
5929 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5930
5931 /* These attributes aren't processed until later:
5932 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5933 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5934 referenced later. However, these attributes are found in the stub
5935 which we won't have later. In order to not impose this complication
5936 on the rest of the code, we read them here and copy them to the
5937 DWO CU/TU die. */
5938
5939 stmt_list = NULL;
5940 low_pc = NULL;
5941 high_pc = NULL;
5942 ranges = NULL;
5943 comp_dir = NULL;
5944
5945 if (stub_comp_unit_die != NULL)
5946 {
5947 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5948 DWO file. */
5949 if (!per_cu->is_debug_types)
5950 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5951 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5952 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5953 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5954 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5955
5956 cu->addr_base = stub_comp_unit_die->addr_base ();
5957
5958 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5959 We need the value before we can process DW_AT_ranges values from the
5960 DWO. */
5961 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
5962
5963 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5964 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5965 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5966 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5967 section. */
5968 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
5969 }
5970 else if (stub_comp_dir != NULL)
5971 {
5972 /* Reconstruct the comp_dir attribute to simplify the code below. */
5973 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
5974 comp_dir->name = DW_AT_comp_dir;
5975 comp_dir->form = DW_FORM_string;
5976 comp_dir->set_string_noncanonical (stub_comp_dir);
5977 }
5978
5979 /* Set up for reading the DWO CU/TU. */
5980 cu->dwo_unit = dwo_unit;
5981 dwarf2_section_info *section = dwo_unit->section;
5982 section->read (objfile);
5983 abfd = section->get_bfd_owner ();
5984 begin_info_ptr = info_ptr = (section->buffer
5985 + to_underlying (dwo_unit->sect_off));
5986 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5987
5988 if (per_cu->is_debug_types)
5989 {
5990 signatured_type *sig_type = (struct signatured_type *) per_cu;
5991
5992 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
5993 section, dwo_abbrev_section,
5994 info_ptr, rcuh_kind::TYPE);
5995 /* This is not an assert because it can be caused by bad debug info. */
5996 if (sig_type->signature != cu->header.signature)
5997 {
5998 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5999 " TU at offset %s [in module %s]"),
6000 hex_string (sig_type->signature),
6001 hex_string (cu->header.signature),
6002 sect_offset_str (dwo_unit->sect_off),
6003 bfd_get_filename (abfd));
6004 }
6005 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6006 /* For DWOs coming from DWP files, we don't know the CU length
6007 nor the type's offset in the TU until now. */
6008 dwo_unit->length = cu->header.get_length_with_initial ();
6009 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6010
6011 /* Establish the type offset that can be used to lookup the type.
6012 For DWO files, we don't know it until now. */
6013 sig_type->type_offset_in_section
6014 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6015 }
6016 else
6017 {
6018 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6019 section, dwo_abbrev_section,
6020 info_ptr, rcuh_kind::COMPILE);
6021 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6022 /* For DWOs coming from DWP files, we don't know the CU length
6023 until now. */
6024 dwo_unit->length = cu->header.get_length_with_initial ();
6025 }
6026
6027 dwo_abbrev_section->read (objfile);
6028 *result_dwo_abbrev_table
6029 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6030 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6031 result_dwo_abbrev_table->get ());
6032
6033 /* Read in the die, but leave space to copy over the attributes
6034 from the stub. This has the benefit of simplifying the rest of
6035 the code - all the work to maintain the illusion of a single
6036 DW_TAG_{compile,type}_unit DIE is done here. */
6037 num_extra_attrs = ((stmt_list != NULL)
6038 + (low_pc != NULL)
6039 + (high_pc != NULL)
6040 + (ranges != NULL)
6041 + (comp_dir != NULL));
6042 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6043 num_extra_attrs);
6044
6045 /* Copy over the attributes from the stub to the DIE we just read in. */
6046 comp_unit_die = *result_comp_unit_die;
6047 i = comp_unit_die->num_attrs;
6048 if (stmt_list != NULL)
6049 comp_unit_die->attrs[i++] = *stmt_list;
6050 if (low_pc != NULL)
6051 comp_unit_die->attrs[i++] = *low_pc;
6052 if (high_pc != NULL)
6053 comp_unit_die->attrs[i++] = *high_pc;
6054 if (ranges != NULL)
6055 comp_unit_die->attrs[i++] = *ranges;
6056 if (comp_dir != NULL)
6057 comp_unit_die->attrs[i++] = *comp_dir;
6058 comp_unit_die->num_attrs += num_extra_attrs;
6059
6060 if (dwarf_die_debug)
6061 {
6062 gdb_printf (gdb_stdlog,
6063 "Read die from %s@0x%x of %s:\n",
6064 section->get_name (),
6065 (unsigned) (begin_info_ptr - section->buffer),
6066 bfd_get_filename (abfd));
6067 dump_die (comp_unit_die, dwarf_die_debug);
6068 }
6069
6070 /* Skip dummy compilation units. */
6071 if (info_ptr >= begin_info_ptr + dwo_unit->length
6072 || peek_abbrev_code (abfd, info_ptr) == 0)
6073 return 0;
6074
6075 *result_info_ptr = info_ptr;
6076 return 1;
6077 }
6078
6079 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6080 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6081 signature is part of the header. */
6082 static gdb::optional<ULONGEST>
6083 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6084 {
6085 if (cu->header.version >= 5)
6086 return cu->header.signature;
6087 struct attribute *attr;
6088 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6089 if (attr == nullptr || !attr->form_is_unsigned ())
6090 return gdb::optional<ULONGEST> ();
6091 return attr->as_unsigned ();
6092 }
6093
6094 /* Subroutine of cutu_reader to simplify it.
6095 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6096 Returns NULL if the specified DWO unit cannot be found. */
6097
6098 static struct dwo_unit *
6099 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6100 {
6101 #if CXX_STD_THREAD
6102 /* We need a lock here both to handle the DWO hash table, and BFD,
6103 which is not thread-safe. */
6104 static std::mutex dwo_lock;
6105
6106 std::lock_guard<std::mutex> guard (dwo_lock);
6107 #endif
6108
6109 dwarf2_per_cu_data *per_cu = cu->per_cu;
6110 struct dwo_unit *dwo_unit;
6111 const char *comp_dir;
6112
6113 gdb_assert (cu != NULL);
6114
6115 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6116 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6117 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6118
6119 if (per_cu->is_debug_types)
6120 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6121 else
6122 {
6123 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6124
6125 if (!signature.has_value ())
6126 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6127 " [in module %s]"),
6128 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6129
6130 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6131 }
6132
6133 return dwo_unit;
6134 }
6135
6136 /* Subroutine of cutu_reader to simplify it.
6137 See it for a description of the parameters.
6138 Read a TU directly from a DWO file, bypassing the stub. */
6139
6140 void
6141 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6142 dwarf2_per_objfile *per_objfile,
6143 dwarf2_cu *existing_cu)
6144 {
6145 struct signatured_type *sig_type;
6146
6147 /* Verify we can do the following downcast, and that we have the
6148 data we need. */
6149 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6150 sig_type = (struct signatured_type *) this_cu;
6151 gdb_assert (sig_type->dwo_unit != NULL);
6152
6153 dwarf2_cu *cu;
6154
6155 if (existing_cu != nullptr)
6156 {
6157 cu = existing_cu;
6158 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6159 /* There's no need to do the rereading_dwo_cu handling that
6160 cutu_reader does since we don't read the stub. */
6161 }
6162 else
6163 {
6164 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6165 in per_objfile yet. */
6166 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6167 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6168 cu = m_new_cu.get ();
6169 }
6170
6171 /* A future optimization, if needed, would be to use an existing
6172 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6173 could share abbrev tables. */
6174
6175 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6176 NULL /* stub_comp_unit_die */,
6177 sig_type->dwo_unit->dwo_file->comp_dir,
6178 this, &info_ptr,
6179 &comp_unit_die,
6180 &m_dwo_abbrev_table) == 0)
6181 {
6182 /* Dummy die. */
6183 dummy_p = true;
6184 }
6185 }
6186
6187 /* Initialize a CU (or TU) and read its DIEs.
6188 If the CU defers to a DWO file, read the DWO file as well.
6189
6190 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6191 Otherwise the table specified in the comp unit header is read in and used.
6192 This is an optimization for when we already have the abbrev table.
6193
6194 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6195 allocated. */
6196
6197 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6198 dwarf2_per_objfile *per_objfile,
6199 struct abbrev_table *abbrev_table,
6200 dwarf2_cu *existing_cu,
6201 bool skip_partial,
6202 abbrev_cache *cache)
6203 : die_reader_specs {},
6204 m_this_cu (this_cu)
6205 {
6206 struct objfile *objfile = per_objfile->objfile;
6207 struct dwarf2_section_info *section = this_cu->section;
6208 bfd *abfd = section->get_bfd_owner ();
6209 const gdb_byte *begin_info_ptr;
6210 struct signatured_type *sig_type = NULL;
6211 struct dwarf2_section_info *abbrev_section;
6212 /* Non-zero if CU currently points to a DWO file and we need to
6213 reread it. When this happens we need to reread the skeleton die
6214 before we can reread the DWO file (this only applies to CUs, not TUs). */
6215 int rereading_dwo_cu = 0;
6216
6217 if (dwarf_die_debug)
6218 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6219 this_cu->is_debug_types ? "type" : "comp",
6220 sect_offset_str (this_cu->sect_off));
6221
6222 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6223 file (instead of going through the stub), short-circuit all of this. */
6224 if (this_cu->reading_dwo_directly)
6225 {
6226 /* Narrow down the scope of possibilities to have to understand. */
6227 gdb_assert (this_cu->is_debug_types);
6228 gdb_assert (abbrev_table == NULL);
6229 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6230 return;
6231 }
6232
6233 /* This is cheap if the section is already read in. */
6234 section->read (objfile);
6235
6236 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6237
6238 abbrev_section = get_abbrev_section_for_cu (this_cu);
6239
6240 dwarf2_cu *cu;
6241
6242 if (existing_cu != nullptr)
6243 {
6244 cu = existing_cu;
6245 /* If this CU is from a DWO file we need to start over, we need to
6246 refetch the attributes from the skeleton CU.
6247 This could be optimized by retrieving those attributes from when we
6248 were here the first time: the previous comp_unit_die was stored in
6249 comp_unit_obstack. But there's no data yet that we need this
6250 optimization. */
6251 if (cu->dwo_unit != NULL)
6252 rereading_dwo_cu = 1;
6253 }
6254 else
6255 {
6256 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6257 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6258 let us know that the CU is being scanned using the parallel
6259 indexer. This assert is avoided in this case because (1) it
6260 is irrelevant, and (2) the get_cu method is not
6261 thread-safe. */
6262 gdb_assert (cache != nullptr
6263 || per_objfile->get_cu (this_cu) == nullptr);
6264 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6265 cu = m_new_cu.get ();
6266 }
6267
6268 /* Get the header. */
6269 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6270 {
6271 /* We already have the header, there's no need to read it in again. */
6272 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6273 }
6274 else
6275 {
6276 if (this_cu->is_debug_types)
6277 {
6278 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6279 section, abbrev_section,
6280 info_ptr, rcuh_kind::TYPE);
6281
6282 /* Since per_cu is the first member of struct signatured_type,
6283 we can go from a pointer to one to a pointer to the other. */
6284 sig_type = (struct signatured_type *) this_cu;
6285 gdb_assert (sig_type->signature == cu->header.signature);
6286 gdb_assert (sig_type->type_offset_in_tu
6287 == cu->header.type_cu_offset_in_tu);
6288 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6289
6290 /* LENGTH has not been set yet for type units if we're
6291 using .gdb_index. */
6292 this_cu->set_length (cu->header.get_length_with_initial ());
6293
6294 /* Establish the type offset that can be used to lookup the type. */
6295 sig_type->type_offset_in_section =
6296 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6297
6298 this_cu->set_version (cu->header.version);
6299 }
6300 else
6301 {
6302 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6303 section, abbrev_section,
6304 info_ptr,
6305 rcuh_kind::COMPILE);
6306
6307 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6308 this_cu->set_length (cu->header.get_length_with_initial ());
6309 this_cu->set_version (cu->header.version);
6310 }
6311 }
6312
6313 /* Skip dummy compilation units. */
6314 if (info_ptr >= begin_info_ptr + this_cu->length ()
6315 || peek_abbrev_code (abfd, info_ptr) == 0)
6316 {
6317 dummy_p = true;
6318 return;
6319 }
6320
6321 /* If we don't have them yet, read the abbrevs for this compilation unit.
6322 And if we need to read them now, make sure they're freed when we're
6323 done. */
6324 if (abbrev_table != NULL)
6325 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6326 else
6327 {
6328 if (cache != nullptr)
6329 abbrev_table = cache->find (abbrev_section,
6330 cu->header.abbrev_sect_off);
6331 if (abbrev_table == nullptr)
6332 {
6333 abbrev_section->read (objfile);
6334 m_abbrev_table_holder
6335 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6336 abbrev_table = m_abbrev_table_holder.get ();
6337 }
6338 }
6339
6340 /* Read the top level CU/TU die. */
6341 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6342 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6343
6344 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6345 {
6346 dummy_p = true;
6347 return;
6348 }
6349
6350 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6351 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6352 table from the DWO file and pass the ownership over to us. It will be
6353 referenced from READER, so we must make sure to free it after we're done
6354 with READER.
6355
6356 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6357 DWO CU, that this test will fail (the attribute will not be present). */
6358 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6359 if (dwo_name != nullptr)
6360 {
6361 struct dwo_unit *dwo_unit;
6362 struct die_info *dwo_comp_unit_die;
6363
6364 if (comp_unit_die->has_children)
6365 {
6366 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6367 " has children (offset %s) [in module %s]"),
6368 sect_offset_str (this_cu->sect_off),
6369 bfd_get_filename (abfd));
6370 }
6371 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6372 if (dwo_unit != NULL)
6373 {
6374 if (read_cutu_die_from_dwo (cu, dwo_unit,
6375 comp_unit_die, NULL,
6376 this, &info_ptr,
6377 &dwo_comp_unit_die,
6378 &m_dwo_abbrev_table) == 0)
6379 {
6380 /* Dummy die. */
6381 dummy_p = true;
6382 return;
6383 }
6384 comp_unit_die = dwo_comp_unit_die;
6385 }
6386 else
6387 {
6388 /* Yikes, we couldn't find the rest of the DIE, we only have
6389 the stub. A complaint has already been logged. There's
6390 not much more we can do except pass on the stub DIE to
6391 die_reader_func. We don't want to throw an error on bad
6392 debug info. */
6393 }
6394 }
6395 }
6396
6397 void
6398 cutu_reader::keep ()
6399 {
6400 /* Done, clean up. */
6401 gdb_assert (!dummy_p);
6402 if (m_new_cu != NULL)
6403 {
6404 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6405 now. */
6406 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6407 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
6408 }
6409 }
6410
6411 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6412 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6413 assumed to have already done the lookup to find the DWO file).
6414
6415 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6416 THIS_CU->is_debug_types, but nothing else.
6417
6418 We fill in THIS_CU->length.
6419
6420 THIS_CU->cu is always freed when done.
6421 This is done in order to not leave THIS_CU->cu in a state where we have
6422 to care whether it refers to the "main" CU or the DWO CU.
6423
6424 When parent_cu is passed, it is used to provide a default value for
6425 str_offsets_base and addr_base from the parent. */
6426
6427 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6428 dwarf2_per_objfile *per_objfile,
6429 struct dwarf2_cu *parent_cu,
6430 struct dwo_file *dwo_file)
6431 : die_reader_specs {},
6432 m_this_cu (this_cu)
6433 {
6434 struct objfile *objfile = per_objfile->objfile;
6435 struct dwarf2_section_info *section = this_cu->section;
6436 bfd *abfd = section->get_bfd_owner ();
6437 struct dwarf2_section_info *abbrev_section;
6438 const gdb_byte *begin_info_ptr, *info_ptr;
6439
6440 if (dwarf_die_debug)
6441 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
6442 this_cu->is_debug_types ? "type" : "comp",
6443 sect_offset_str (this_cu->sect_off));
6444
6445 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6446
6447 abbrev_section = (dwo_file != NULL
6448 ? &dwo_file->sections.abbrev
6449 : get_abbrev_section_for_cu (this_cu));
6450
6451 /* This is cheap if the section is already read in. */
6452 section->read (objfile);
6453
6454 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6455
6456 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6457 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6458 section, abbrev_section, info_ptr,
6459 (this_cu->is_debug_types
6460 ? rcuh_kind::TYPE
6461 : rcuh_kind::COMPILE));
6462
6463 if (parent_cu != nullptr)
6464 {
6465 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6466 m_new_cu->addr_base = parent_cu->addr_base;
6467 }
6468 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
6469
6470 /* Skip dummy compilation units. */
6471 if (info_ptr >= begin_info_ptr + this_cu->length ()
6472 || peek_abbrev_code (abfd, info_ptr) == 0)
6473 {
6474 dummy_p = true;
6475 return;
6476 }
6477
6478 abbrev_section->read (objfile);
6479 m_abbrev_table_holder
6480 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6481
6482 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6483 m_abbrev_table_holder.get ());
6484 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6485 }
6486
6487 \f
6488 /* Type Unit Groups.
6489
6490 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6491 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6492 so that all types coming from the same compilation (.o file) are grouped
6493 together. A future step could be to put the types in the same symtab as
6494 the CU the types ultimately came from. */
6495
6496 static hashval_t
6497 hash_type_unit_group (const void *item)
6498 {
6499 const struct type_unit_group *tu_group
6500 = (const struct type_unit_group *) item;
6501
6502 return hash_stmt_list_entry (&tu_group->hash);
6503 }
6504
6505 static int
6506 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6507 {
6508 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6509 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6510
6511 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6512 }
6513
6514 /* Allocate a hash table for type unit groups. */
6515
6516 static htab_up
6517 allocate_type_unit_groups_table ()
6518 {
6519 return htab_up (htab_create_alloc (3,
6520 hash_type_unit_group,
6521 eq_type_unit_group,
6522 htab_delete_entry<type_unit_group>,
6523 xcalloc, xfree));
6524 }
6525
6526 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6527 partial symtabs. We combine several TUs per psymtab to not let the size
6528 of any one psymtab grow too big. */
6529 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6530 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6531
6532 /* Helper routine for get_type_unit_group.
6533 Create the type_unit_group object used to hold one or more TUs. */
6534
6535 static std::unique_ptr<type_unit_group>
6536 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6537 {
6538 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6539
6540 tu_group->hash.dwo_unit = cu->dwo_unit;
6541 tu_group->hash.line_sect_off = line_offset_struct;
6542
6543 return tu_group;
6544 }
6545
6546 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6547 STMT_LIST is a DW_AT_stmt_list attribute. */
6548
6549 static struct type_unit_group *
6550 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6551 {
6552 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6553 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6554 struct type_unit_group *tu_group;
6555 void **slot;
6556 unsigned int line_offset;
6557 struct type_unit_group type_unit_group_for_lookup;
6558
6559 if (per_objfile->per_bfd->type_unit_groups == NULL)
6560 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
6561
6562 /* Do we need to create a new group, or can we use an existing one? */
6563
6564 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
6565 {
6566 line_offset = stmt_list->as_unsigned ();
6567 ++tu_stats->nr_symtab_sharers;
6568 }
6569 else
6570 {
6571 /* Ugh, no stmt_list. Rare, but we have to handle it.
6572 We can do various things here like create one group per TU or
6573 spread them over multiple groups to split up the expansion work.
6574 To avoid worst case scenarios (too many groups or too large groups)
6575 we, umm, group them in bunches. */
6576 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6577 | (tu_stats->nr_stmt_less_type_units
6578 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6579 ++tu_stats->nr_stmt_less_type_units;
6580 }
6581
6582 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6583 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
6584 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
6585 &type_unit_group_for_lookup, INSERT);
6586 if (*slot == nullptr)
6587 {
6588 sect_offset line_offset_struct = (sect_offset) line_offset;
6589 std::unique_ptr<type_unit_group> grp
6590 = create_type_unit_group (cu, line_offset_struct);
6591 *slot = grp.release ();
6592 ++tu_stats->nr_symtabs;
6593 }
6594
6595 tu_group = (struct type_unit_group *) *slot;
6596 gdb_assert (tu_group != nullptr);
6597 return tu_group;
6598 }
6599 \f
6600
6601 /* An instance of this is created when scanning DWARF to create a
6602 cooked index. */
6603
6604 class cooked_index_storage
6605 {
6606 public:
6607
6608 cooked_index_storage ()
6609 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
6610 eq_cutu_reader,
6611 htab_delete_entry<cutu_reader>,
6612 xcalloc, xfree)),
6613 m_index (new cooked_index)
6614 {
6615 }
6616
6617 DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
6618
6619 /* Return the current abbrev cache. */
6620 abbrev_cache *get_abbrev_cache ()
6621 {
6622 return &m_abbrev_cache;
6623 }
6624
6625 /* Return the DIE reader corresponding to PER_CU. If no such reader
6626 has been registered, return NULL. */
6627 cutu_reader *get_reader (dwarf2_per_cu_data *per_cu)
6628 {
6629 int index = per_cu->index;
6630 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
6631 &index, index);
6632 }
6633
6634 /* Preserve READER by storing it in the local hash table. */
6635 cutu_reader *preserve (std::unique_ptr<cutu_reader> reader)
6636 {
6637 m_abbrev_cache.add (reader->release_abbrev_table ());
6638
6639 int index = reader->cu->per_cu->index;
6640 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
6641 index, INSERT);
6642 gdb_assert (*slot == nullptr);
6643 cutu_reader *result = reader.get ();
6644 *slot = reader.release ();
6645 return result;
6646 }
6647
6648 /* Add an entry to the index. The arguments describe the entry; see
6649 cooked-index.h. The new entry is returned. */
6650 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
6651 cooked_index_flag flags,
6652 const char *name,
6653 const cooked_index_entry *parent_entry,
6654 dwarf2_per_cu_data *per_cu)
6655 {
6656 return m_index->add (die_offset, tag, flags, name, parent_entry, per_cu);
6657 }
6658
6659 /* Install the current addrmap into the index being constructed,
6660 then transfer ownership of the index to the caller. */
6661 std::unique_ptr<cooked_index> release ()
6662 {
6663 m_index->install_addrmap (&m_addrmap);
6664 return std::move (m_index);
6665 }
6666
6667 /* Return the mutable addrmap that is currently being created. */
6668 addrmap_mutable *get_addrmap ()
6669 {
6670 return &m_addrmap;
6671 }
6672
6673 private:
6674
6675 /* Hash function for a cutu_reader. */
6676 static hashval_t hash_cutu_reader (const void *a)
6677 {
6678 const cutu_reader *reader = (const cutu_reader *) a;
6679 return reader->cu->per_cu->index;
6680 }
6681
6682 /* Equality function for cutu_reader. */
6683 static int eq_cutu_reader (const void *a, const void *b)
6684 {
6685 const cutu_reader *ra = (const cutu_reader *) a;
6686 const int *rb = (const int *) b;
6687 return ra->cu->per_cu->index == *rb;
6688 }
6689
6690 /* The abbrev cache used by this indexer. */
6691 abbrev_cache m_abbrev_cache;
6692 /* A hash table of cutu_reader objects. */
6693 htab_up m_reader_hash;
6694 /* The index that is being constructed. */
6695 std::unique_ptr<cooked_index> m_index;
6696
6697 /* A writeable addrmap being constructed by this scanner. */
6698 addrmap_mutable m_addrmap;
6699 };
6700
6701 /* An instance of this is created to index a CU. */
6702
6703 class cooked_indexer
6704 {
6705 public:
6706
6707 cooked_indexer (cooked_index_storage *storage,
6708 dwarf2_per_cu_data *per_cu,
6709 enum language language)
6710 : m_index_storage (storage),
6711 m_per_cu (per_cu),
6712 m_language (language)
6713 {
6714 }
6715
6716 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
6717
6718 /* Index the given CU. */
6719 void make_index (cutu_reader *reader);
6720
6721 private:
6722
6723 /* A helper function to turn a section offset into an address that
6724 can be used in an addrmap. */
6725 CORE_ADDR form_addr (sect_offset offset, bool is_dwz)
6726 {
6727 CORE_ADDR value = to_underlying (offset);
6728 if (is_dwz)
6729 value |= ((CORE_ADDR) 1) << (8 * sizeof (CORE_ADDR) - 1);
6730 return value;
6731 }
6732
6733 /* A helper function to scan the PC bounds of READER and record them
6734 in the storage's addrmap. */
6735 void check_bounds (cutu_reader *reader);
6736
6737 /* Ensure that the indicated CU exists. The cutu_reader for it is
6738 returned. FOR_SCANNING is true if the caller intends to scan all
6739 the DIEs in the CU; when false, this use is assumed to be to look
6740 up just a single DIE. */
6741 cutu_reader *ensure_cu_exists (cutu_reader *reader,
6742 dwarf2_per_objfile *per_objfile,
6743 sect_offset sect_off,
6744 bool is_dwz,
6745 bool for_scanning);
6746
6747 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6748 the entry for the enclosing scope (nullptr at top level). FULLY
6749 is true when a full scan must be done -- in some languages,
6750 function scopes must be fully explored in order to find nested
6751 functions. This returns a pointer to just after the spot where
6752 reading stopped. */
6753 const gdb_byte *index_dies (cutu_reader *reader,
6754 const gdb_byte *info_ptr,
6755 const cooked_index_entry *parent_entry,
6756 bool fully);
6757
6758 /* Scan the attributes for a given DIE and update the out
6759 parameters. Returns a pointer to the byte after the DIE. */
6760 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
6761 cutu_reader *reader,
6762 const gdb_byte *watermark_ptr,
6763 const gdb_byte *info_ptr,
6764 const abbrev_info *abbrev,
6765 const char **name,
6766 const char **linkage_name,
6767 cooked_index_flag *flags,
6768 sect_offset *sibling_offset,
6769 const cooked_index_entry **parent_entry,
6770 CORE_ADDR *maybe_defer,
6771 bool for_specification);
6772
6773 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6774 DW_AT_import, and then scanning the referenced CU. Returns a
6775 pointer to the byte after the DIE. */
6776 const gdb_byte *index_imported_unit (cutu_reader *reader,
6777 const gdb_byte *info_ptr,
6778 const abbrev_info *abbrev);
6779
6780 /* Recursively read DIEs, recording the section offsets in
6781 m_die_range_map and then calling index_dies. */
6782 const gdb_byte *recurse (cutu_reader *reader,
6783 const gdb_byte *info_ptr,
6784 const cooked_index_entry *parent_entry,
6785 bool fully);
6786
6787 /* The storage object, where the results are kept. */
6788 cooked_index_storage *m_index_storage;
6789 /* The CU that we are reading on behalf of. This object might be
6790 asked to index one CU but to treat the results as if they come
6791 from some including CU; in this case the including CU would be
6792 recorded here. */
6793 dwarf2_per_cu_data *m_per_cu;
6794 /* The language that we're assuming when reading. */
6795 enum language m_language;
6796
6797 /* An addrmap that maps from section offsets (see the form_addr
6798 method) to newly-created entries. See m_deferred_entries to
6799 understand this. */
6800 addrmap_mutable m_die_range_map;
6801
6802 /* A single deferred entry. */
6803 struct deferred_entry
6804 {
6805 sect_offset die_offset;
6806 const char *name;
6807 CORE_ADDR spec_offset;
6808 dwarf_tag tag;
6809 cooked_index_flag flags;
6810 };
6811
6812 /* The generated DWARF can sometimes have the declaration for a
6813 method in a class (or perhaps namespace) scope, with the
6814 definition appearing outside this scope... just one of the many
6815 bad things about DWARF. In order to handle this situation, we
6816 defer certain entries until the end of scanning, at which point
6817 we'll know the containing context of all the DIEs that we might
6818 have scanned. This vector stores these deferred entries. */
6819 std::vector<deferred_entry> m_deferred_entries;
6820 };
6821
6822 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6823 Process compilation unit THIS_CU for a psymtab. */
6824
6825 static void
6826 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
6827 dwarf2_per_objfile *per_objfile,
6828 cooked_index_storage *storage)
6829 {
6830 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false,
6831 storage->get_abbrev_cache ());
6832
6833 if (reader.comp_unit_die == nullptr)
6834 return;
6835
6836 if (reader.dummy_p)
6837 {
6838 /* Nothing. */
6839 }
6840 else if (this_cu->is_debug_types)
6841 build_type_psymtabs_reader (&reader, storage);
6842 else if (reader.comp_unit_die->tag != DW_TAG_partial_unit)
6843 {
6844 bool nope = false;
6845 if (this_cu->scanned.compare_exchange_strong (nope, true))
6846 {
6847 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
6848 language_minimal);
6849 gdb_assert (storage != nullptr);
6850 cooked_indexer indexer (storage, this_cu, reader.cu->lang ());
6851 indexer.make_index (&reader);
6852 }
6853 }
6854 }
6855
6856 /* Reader function for build_type_psymtabs. */
6857
6858 static void
6859 build_type_psymtabs_reader (cutu_reader *reader,
6860 cooked_index_storage *storage)
6861 {
6862 struct dwarf2_cu *cu = reader->cu;
6863 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6864 struct die_info *type_unit_die = reader->comp_unit_die;
6865
6866 gdb_assert (per_cu->is_debug_types);
6867
6868 if (! type_unit_die->has_children)
6869 return;
6870
6871 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6872
6873 gdb_assert (storage != nullptr);
6874 cooked_indexer indexer (storage, per_cu, cu->lang ());
6875 indexer.make_index (reader);
6876 }
6877
6878 /* Struct used to sort TUs by their abbreviation table offset. */
6879
6880 struct tu_abbrev_offset
6881 {
6882 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
6883 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
6884 {}
6885
6886 /* This is used when sorting. */
6887 bool operator< (const tu_abbrev_offset &other) const
6888 {
6889 return abbrev_offset < other.abbrev_offset;
6890 }
6891
6892 signatured_type *sig_type;
6893 sect_offset abbrev_offset;
6894 };
6895
6896 /* Efficiently read all the type units.
6897
6898 The efficiency is because we sort TUs by the abbrev table they use and
6899 only read each abbrev table once. In one program there are 200K TUs
6900 sharing 8K abbrev tables.
6901
6902 The main purpose of this function is to support building the
6903 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6904 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6905 can collapse the search space by grouping them by stmt_list.
6906 The savings can be significant, in the same program from above the 200K TUs
6907 share 8K stmt_list tables.
6908
6909 FUNC is expected to call get_type_unit_group, which will create the
6910 struct type_unit_group if necessary and add it to
6911 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6912
6913 static void
6914 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
6915 cooked_index_storage *storage)
6916 {
6917 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6918 abbrev_table_up abbrev_table;
6919 sect_offset abbrev_offset;
6920
6921 /* It's up to the caller to not call us multiple times. */
6922 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
6923
6924 if (per_objfile->per_bfd->all_type_units.size () == 0)
6925 return;
6926
6927 /* TUs typically share abbrev tables, and there can be way more TUs than
6928 abbrev tables. Sort by abbrev table to reduce the number of times we
6929 read each abbrev table in.
6930 Alternatives are to punt or to maintain a cache of abbrev tables.
6931 This is simpler and efficient enough for now.
6932
6933 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6934 symtab to use). Typically TUs with the same abbrev offset have the same
6935 stmt_list value too so in practice this should work well.
6936
6937 The basic algorithm here is:
6938
6939 sort TUs by abbrev table
6940 for each TU with same abbrev table:
6941 read abbrev table if first user
6942 read TU top level DIE
6943 [IWBN if DWO skeletons had DW_AT_stmt_list]
6944 call FUNC */
6945
6946 dwarf_read_debug_printf ("Building type unit groups ...");
6947
6948 /* Sort in a separate table to maintain the order of all_units
6949 for .gdb_index: TU indices directly index all_type_units. */
6950 std::vector<tu_abbrev_offset> sorted_by_abbrev;
6951 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
6952
6953 for (const auto &cu : per_objfile->per_bfd->all_units)
6954 {
6955 if (cu->is_debug_types)
6956 {
6957 auto sig_type = static_cast<signatured_type *> (cu.get ());
6958 sorted_by_abbrev.emplace_back
6959 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
6960 sig_type->sect_off));
6961 }
6962 }
6963
6964 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
6965
6966 abbrev_offset = (sect_offset) ~(unsigned) 0;
6967
6968 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
6969 {
6970 /* Switch to the next abbrev table if necessary. */
6971 if (abbrev_table == NULL
6972 || tu.abbrev_offset != abbrev_offset)
6973 {
6974 abbrev_offset = tu.abbrev_offset;
6975 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
6976 abbrev_table =
6977 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
6978 ++tu_stats->nr_uniq_abbrev_tables;
6979 }
6980
6981 cutu_reader reader (tu.sig_type, per_objfile,
6982 abbrev_table.get (), nullptr, false);
6983 if (!reader.dummy_p)
6984 build_type_psymtabs_reader (&reader, storage);
6985 }
6986 }
6987
6988 /* Print collected type unit statistics. */
6989
6990 static void
6991 print_tu_stats (dwarf2_per_objfile *per_objfile)
6992 {
6993 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6994
6995 dwarf_read_debug_printf ("Type unit statistics:");
6996 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
6997 dwarf_read_debug_printf (" %d uniq abbrev tables",
6998 tu_stats->nr_uniq_abbrev_tables);
6999 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7000 tu_stats->nr_symtabs);
7001 dwarf_read_debug_printf (" %d symtab sharers",
7002 tu_stats->nr_symtab_sharers);
7003 dwarf_read_debug_printf (" %d type units without a stmt_list",
7004 tu_stats->nr_stmt_less_type_units);
7005 dwarf_read_debug_printf (" %d all_type_units reallocs",
7006 tu_stats->nr_all_type_units_reallocs);
7007 }
7008
7009 struct skeleton_data
7010 {
7011 dwarf2_per_objfile *per_objfile;
7012 cooked_index_storage *storage;
7013 };
7014
7015 /* Traversal function for process_skeletonless_type_unit.
7016 Read a TU in a DWO file and build partial symbols for it. */
7017
7018 static int
7019 process_skeletonless_type_unit (void **slot, void *info)
7020 {
7021 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7022 skeleton_data *data = (skeleton_data *) info;
7023
7024 /* If this TU doesn't exist in the global table, add it and read it in. */
7025
7026 if (data->per_objfile->per_bfd->signatured_types == NULL)
7027 data->per_objfile->per_bfd->signatured_types
7028 = allocate_signatured_type_table ();
7029
7030 signatured_type find_entry (dwo_unit->signature);
7031 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
7032 &find_entry, INSERT);
7033 /* If we've already seen this type there's nothing to do. What's happening
7034 is we're doing our own version of comdat-folding here. */
7035 if (*slot != NULL)
7036 return 1;
7037
7038 /* This does the job that create_all_units would have done for
7039 this TU. */
7040 signatured_type *entry
7041 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
7042 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
7043 *slot = entry;
7044
7045 /* This does the job that build_type_psymtabs would have done. */
7046 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
7047 if (!reader.dummy_p)
7048 build_type_psymtabs_reader (&reader, data->storage);
7049
7050 return 1;
7051 }
7052
7053 /* Traversal function for process_skeletonless_type_units. */
7054
7055 static int
7056 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7057 {
7058 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7059
7060 if (dwo_file->tus != NULL)
7061 htab_traverse_noresize (dwo_file->tus.get (),
7062 process_skeletonless_type_unit, info);
7063
7064 return 1;
7065 }
7066
7067 /* Scan all TUs of DWO files, verifying we've processed them.
7068 This is needed in case a TU was emitted without its skeleton.
7069 Note: This can't be done until we know what all the DWO files are. */
7070
7071 static void
7072 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
7073 cooked_index_storage *storage)
7074 {
7075 skeleton_data data { per_objfile, storage };
7076
7077 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7078 if (get_dwp_file (per_objfile) == NULL
7079 && per_objfile->per_bfd->dwo_files != NULL)
7080 {
7081 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7082 process_dwo_file_for_skeletonless_type_units,
7083 &data);
7084 }
7085 }
7086
7087 /* Build the partial symbol table by doing a quick pass through the
7088 .debug_info and .debug_abbrev sections. */
7089
7090 static void
7091 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7092 {
7093 struct objfile *objfile = per_objfile->objfile;
7094 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7095
7096 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7097 objfile_name (objfile));
7098
7099 per_bfd->map_info_sections (objfile);
7100
7101 cooked_index_storage index_storage;
7102 create_all_units (per_objfile);
7103 build_type_psymtabs (per_objfile, &index_storage);
7104 std::vector<std::unique_ptr<cooked_index>> indexes;
7105
7106 per_bfd->quick_file_names_table
7107 = create_quick_file_names_table (per_bfd->all_units.size ());
7108 if (!per_bfd->debug_aranges.empty ())
7109 read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
7110 index_storage.get_addrmap ());
7111
7112 {
7113 /* Ensure that complaints are handled correctly. */
7114 complaint_interceptor complaint_handler;
7115
7116 using iter_type = decltype (per_bfd->all_units.begin ());
7117
7118 auto task_size_ = [] (iter_type iter)
7119 {
7120 dwarf2_per_cu_data *per_cu = iter->get ();
7121 return (size_t)per_cu->length ();
7122 };
7123 auto task_size = gdb::make_function_view (task_size_);
7124
7125 /* Each thread returns a pair holding a cooked index, and a vector
7126 of errors that should be printed. The latter is done because
7127 GDB's I/O system is not thread-safe. run_on_main_thread could be
7128 used, but that would mean the messages are printed after the
7129 prompt, which looks weird. */
7130 using result_type = std::pair<std::unique_ptr<cooked_index>,
7131 std::vector<gdb_exception>>;
7132 std::vector<result_type> results
7133 = gdb::parallel_for_each (1, per_bfd->all_units.begin (),
7134 per_bfd->all_units.end (),
7135 [=] (iter_type iter, iter_type end)
7136 {
7137 std::vector<gdb_exception> errors;
7138 cooked_index_storage thread_storage;
7139 for (; iter != end; ++iter)
7140 {
7141 dwarf2_per_cu_data *per_cu = iter->get ();
7142 try
7143 {
7144 process_psymtab_comp_unit (per_cu, per_objfile,
7145 &thread_storage);
7146 }
7147 catch (gdb_exception &except)
7148 {
7149 errors.push_back (std::move (except));
7150 }
7151 }
7152 return result_type (thread_storage.release (), std::move (errors));
7153 }, task_size);
7154
7155 /* Only show a given exception a single time. */
7156 std::unordered_set<gdb_exception> seen_exceptions;
7157 for (auto &one_result : results)
7158 {
7159 indexes.push_back (std::move (one_result.first));
7160 for (auto &one_exc : one_result.second)
7161 if (seen_exceptions.insert (one_exc).second)
7162 exception_print (gdb_stderr, one_exc);
7163 }
7164 }
7165
7166 /* This has to wait until we read the CUs, we need the list of DWOs. */
7167 process_skeletonless_type_units (per_objfile, &index_storage);
7168
7169 if (dwarf_read_debug > 0)
7170 print_tu_stats (per_objfile);
7171
7172 indexes.push_back (index_storage.release ());
7173 indexes.shrink_to_fit ();
7174
7175 cooked_index_vector *vec = new cooked_index_vector (std::move (indexes));
7176 per_bfd->index_table.reset (vec);
7177
7178 const cooked_index_entry *main_entry = vec->get_main ();
7179 if (main_entry != nullptr)
7180 set_objfile_main_name (objfile, main_entry->name,
7181 main_entry->per_cu->lang ());
7182
7183 dwarf_read_debug_printf ("Done building psymtabs of %s",
7184 objfile_name (objfile));
7185 }
7186
7187 static void
7188 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7189 struct dwarf2_section_info *section,
7190 struct dwarf2_section_info *abbrev_section,
7191 unsigned int is_dwz,
7192 htab_up &types_htab,
7193 rcuh_kind section_kind)
7194 {
7195 const gdb_byte *info_ptr;
7196 struct objfile *objfile = per_objfile->objfile;
7197
7198 dwarf_read_debug_printf ("Reading %s for %s",
7199 section->get_name (),
7200 section->get_file_name ());
7201
7202 section->read (objfile);
7203
7204 info_ptr = section->buffer;
7205
7206 while (info_ptr < section->buffer + section->size)
7207 {
7208 dwarf2_per_cu_data_up this_cu;
7209
7210 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7211
7212 comp_unit_head cu_header;
7213 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7214 abbrev_section, info_ptr,
7215 section_kind);
7216
7217 /* Save the compilation unit for later lookup. */
7218 if (cu_header.unit_type != DW_UT_type)
7219 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7220 else
7221 {
7222 if (types_htab == nullptr)
7223 types_htab = allocate_signatured_type_table ();
7224
7225 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
7226 (cu_header.signature);
7227 signatured_type *sig_ptr = sig_type.get ();
7228 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7229 this_cu.reset (sig_type.release ());
7230
7231 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7232 gdb_assert (slot != nullptr);
7233 if (*slot != nullptr)
7234 complaint (_("debug type entry at offset %s is duplicate to"
7235 " the entry at offset %s, signature %s"),
7236 sect_offset_str (sect_off),
7237 sect_offset_str (sig_ptr->sect_off),
7238 hex_string (sig_ptr->signature));
7239 *slot = sig_ptr;
7240 }
7241 this_cu->sect_off = sect_off;
7242 this_cu->set_length (cu_header.get_length_with_initial ());
7243 this_cu->is_dwz = is_dwz;
7244 this_cu->section = section;
7245 /* Init this asap, to avoid a data race in the set_version in
7246 cutu_reader::cutu_reader (which may be run in parallel for the cooked
7247 index case). */
7248 this_cu->set_version (cu_header.version);
7249
7250 info_ptr = info_ptr + this_cu->length ();
7251 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
7252 }
7253 }
7254
7255 /* Initialize the views on all_units. */
7256
7257 static void
7258 finalize_all_units (dwarf2_per_bfd *per_bfd)
7259 {
7260 size_t nr_tus = per_bfd->tu_stats.nr_tus;
7261 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
7262 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
7263 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
7264 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
7265 }
7266
7267 /* Create a list of all compilation units in OBJFILE.
7268 This is only done for -readnow and building partial symtabs. */
7269
7270 static void
7271 create_all_units (dwarf2_per_objfile *per_objfile)
7272 {
7273 htab_up types_htab;
7274 gdb_assert (per_objfile->per_bfd->all_units.empty ());
7275
7276 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7277 &per_objfile->per_bfd->abbrev, 0,
7278 types_htab, rcuh_kind::COMPILE);
7279 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7280 read_comp_units_from_section (per_objfile, &section,
7281 &per_objfile->per_bfd->abbrev, 0,
7282 types_htab, rcuh_kind::TYPE);
7283
7284 dwz_file *dwz;
7285 try
7286 {
7287 dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7288 }
7289 catch (const gdb_exception_error &)
7290 {
7291 per_objfile->per_bfd->all_units.clear ();
7292 throw;
7293 }
7294 if (dwz != NULL)
7295 {
7296 /* Pre-read the sections we'll need to construct an index. */
7297 struct objfile *objfile = per_objfile->objfile;
7298 dwz->abbrev.read (objfile);
7299 dwz->info.read (objfile);
7300 dwz->str.read (objfile);
7301 dwz->line.read (objfile);
7302 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7303 types_htab, rcuh_kind::COMPILE);
7304 }
7305
7306 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7307
7308 finalize_all_units (per_objfile->per_bfd);
7309 }
7310
7311 /* Return the initial uleb128 in the die at INFO_PTR. */
7312
7313 static unsigned int
7314 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7315 {
7316 unsigned int bytes_read;
7317
7318 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7319 }
7320
7321 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7322 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7323
7324 Return the corresponding abbrev, or NULL if the number is zero (indicating
7325 an empty DIE). In either case *BYTES_READ will be set to the length of
7326 the initial number. */
7327
7328 static const struct abbrev_info *
7329 peek_die_abbrev (const die_reader_specs &reader,
7330 const gdb_byte *info_ptr, unsigned int *bytes_read)
7331 {
7332 dwarf2_cu *cu = reader.cu;
7333 bfd *abfd = reader.abfd;
7334 unsigned int abbrev_number
7335 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7336
7337 if (abbrev_number == 0)
7338 return NULL;
7339
7340 const abbrev_info *abbrev
7341 = reader.abbrev_table->lookup_abbrev (abbrev_number);
7342 if (!abbrev)
7343 {
7344 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7345 " at offset %s [in module %s]"),
7346 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7347 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
7348 }
7349
7350 return abbrev;
7351 }
7352
7353 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7354 Returns a pointer to the end of a series of DIEs, terminated by an empty
7355 DIE. Any children of the skipped DIEs will also be skipped. */
7356
7357 static const gdb_byte *
7358 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7359 {
7360 while (1)
7361 {
7362 unsigned int bytes_read;
7363 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
7364 &bytes_read);
7365
7366 if (abbrev == NULL)
7367 return info_ptr + bytes_read;
7368 else
7369 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7370 }
7371 }
7372
7373 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7374 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7375 abbrev corresponding to that skipped uleb128 should be passed in
7376 ABBREV.
7377
7378 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7379 returns a pointer to this DIE's sibling, skipping any children.
7380 Otherwise, returns a pointer to the DIE's first child. */
7381
7382 static const gdb_byte *
7383 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7384 const struct abbrev_info *abbrev, bool do_skip_children)
7385 {
7386 unsigned int bytes_read;
7387 struct attribute attr;
7388 bfd *abfd = reader->abfd;
7389 struct dwarf2_cu *cu = reader->cu;
7390 const gdb_byte *buffer = reader->buffer;
7391 const gdb_byte *buffer_end = reader->buffer_end;
7392 unsigned int form, i;
7393
7394 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
7395 {
7396 /* We only handle DW_FORM_ref4 here. */
7397 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
7398 unsigned int offset = read_4_bytes (abfd, sibling_data);
7399 const gdb_byte *sibling_ptr
7400 = buffer + to_underlying (cu->header.sect_off) + offset;
7401 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
7402 return sibling_ptr;
7403 /* Fall through to the slow way. */
7404 }
7405 else if (abbrev->size_if_constant != 0)
7406 {
7407 info_ptr += abbrev->size_if_constant;
7408 if (do_skip_children && abbrev->has_children)
7409 return skip_children (reader, info_ptr);
7410 return info_ptr;
7411 }
7412
7413 for (i = 0; i < abbrev->num_attrs; i++)
7414 {
7415 /* The only abbrev we care about is DW_AT_sibling. */
7416 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
7417 {
7418 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7419 if (attr.form == DW_FORM_ref_addr)
7420 complaint (_("ignoring absolute DW_AT_sibling"));
7421 else
7422 {
7423 sect_offset off = attr.get_ref_die_offset ();
7424 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
7425
7426 if (sibling_ptr < info_ptr)
7427 complaint (_("DW_AT_sibling points backwards"));
7428 else if (sibling_ptr > reader->buffer_end)
7429 reader->die_section->overflow_complaint ();
7430 else
7431 return sibling_ptr;
7432 }
7433 }
7434
7435 /* If it isn't DW_AT_sibling, skip this attribute. */
7436 form = abbrev->attrs[i].form;
7437 skip_attribute:
7438 switch (form)
7439 {
7440 case DW_FORM_ref_addr:
7441 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7442 and later it is offset sized. */
7443 if (cu->header.version == 2)
7444 info_ptr += cu->header.addr_size;
7445 else
7446 info_ptr += cu->header.offset_size;
7447 break;
7448 case DW_FORM_GNU_ref_alt:
7449 info_ptr += cu->header.offset_size;
7450 break;
7451 case DW_FORM_addr:
7452 info_ptr += cu->header.addr_size;
7453 break;
7454 case DW_FORM_data1:
7455 case DW_FORM_ref1:
7456 case DW_FORM_flag:
7457 case DW_FORM_strx1:
7458 info_ptr += 1;
7459 break;
7460 case DW_FORM_flag_present:
7461 case DW_FORM_implicit_const:
7462 break;
7463 case DW_FORM_data2:
7464 case DW_FORM_ref2:
7465 case DW_FORM_strx2:
7466 info_ptr += 2;
7467 break;
7468 case DW_FORM_strx3:
7469 info_ptr += 3;
7470 break;
7471 case DW_FORM_data4:
7472 case DW_FORM_ref4:
7473 case DW_FORM_strx4:
7474 info_ptr += 4;
7475 break;
7476 case DW_FORM_data8:
7477 case DW_FORM_ref8:
7478 case DW_FORM_ref_sig8:
7479 info_ptr += 8;
7480 break;
7481 case DW_FORM_data16:
7482 info_ptr += 16;
7483 break;
7484 case DW_FORM_string:
7485 read_direct_string (abfd, info_ptr, &bytes_read);
7486 info_ptr += bytes_read;
7487 break;
7488 case DW_FORM_sec_offset:
7489 case DW_FORM_strp:
7490 case DW_FORM_GNU_strp_alt:
7491 info_ptr += cu->header.offset_size;
7492 break;
7493 case DW_FORM_exprloc:
7494 case DW_FORM_block:
7495 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7496 info_ptr += bytes_read;
7497 break;
7498 case DW_FORM_block1:
7499 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7500 break;
7501 case DW_FORM_block2:
7502 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7503 break;
7504 case DW_FORM_block4:
7505 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7506 break;
7507 case DW_FORM_addrx:
7508 case DW_FORM_strx:
7509 case DW_FORM_sdata:
7510 case DW_FORM_udata:
7511 case DW_FORM_ref_udata:
7512 case DW_FORM_GNU_addr_index:
7513 case DW_FORM_GNU_str_index:
7514 case DW_FORM_rnglistx:
7515 case DW_FORM_loclistx:
7516 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7517 break;
7518 case DW_FORM_indirect:
7519 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7520 info_ptr += bytes_read;
7521 /* We need to continue parsing from here, so just go back to
7522 the top. */
7523 goto skip_attribute;
7524
7525 default:
7526 error (_("Dwarf Error: Cannot handle %s "
7527 "in DWARF reader [in module %s]"),
7528 dwarf_form_name (form),
7529 bfd_get_filename (abfd));
7530 }
7531 }
7532
7533 if (do_skip_children && abbrev->has_children)
7534 return skip_children (reader, info_ptr);
7535 else
7536 return info_ptr;
7537 }
7538 \f
7539 /* Reading in full CUs. */
7540
7541 /* Add PER_CU to the queue. */
7542
7543 static void
7544 queue_comp_unit (dwarf2_per_cu_data *per_cu,
7545 dwarf2_per_objfile *per_objfile,
7546 enum language pretend_language)
7547 {
7548 per_cu->queued = 1;
7549
7550 gdb_assert (per_objfile->queue.has_value ());
7551 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
7552 }
7553
7554 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7555
7556 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7557 dependency.
7558
7559 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7560 DIEs, false otherwise.
7561
7562 Explanation: there is an invariant that if a CU is queued for expansion
7563 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7564 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7565 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7566 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7567 invariant is respected.
7568
7569 The caller is therefore not required to load the CU's DIEs (we return false)
7570 if:
7571
7572 - the CU is already expanded, and therefore does not get enqueued
7573 - the CU gets enqueued for expansion, but its DIEs are already loaded
7574
7575 Note that the caller should not use this function's return value as an
7576 indicator of whether the CU's DIEs are loaded right now, it should check
7577 that by calling `dwarf2_per_objfile::get_cu` instead. */
7578
7579 static int
7580 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7581 dwarf2_per_cu_data *per_cu,
7582 dwarf2_per_objfile *per_objfile,
7583 enum language pretend_language)
7584 {
7585 /* Mark the dependence relation so that we don't flush PER_CU
7586 too early. */
7587 if (dependent_cu != NULL)
7588 dependent_cu->add_dependence (per_cu);
7589
7590 /* If it's already on the queue, we have nothing to do. */
7591 if (per_cu->queued)
7592 {
7593 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7594 loaded. */
7595 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
7596
7597 /* If the CU is queued for expansion, it should not already be
7598 expanded. */
7599 gdb_assert (!per_objfile->symtab_set_p (per_cu));
7600
7601 /* The DIEs are already loaded, the caller doesn't need to do it. */
7602 return 0;
7603 }
7604
7605 bool queued = false;
7606 if (!per_objfile->symtab_set_p (per_cu))
7607 {
7608 /* Add it to the queue. */
7609 queue_comp_unit (per_cu, per_objfile, pretend_language);
7610 queued = true;
7611 }
7612
7613 /* If the compilation unit is already loaded, just mark it as
7614 used. */
7615 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7616 if (cu != nullptr)
7617 cu->last_used = 0;
7618
7619 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7620 and the DIEs are not already loaded. */
7621 return queued && cu == nullptr;
7622 }
7623
7624 /* Process the queue. */
7625
7626 static void
7627 process_queue (dwarf2_per_objfile *per_objfile)
7628 {
7629 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7630 objfile_name (per_objfile->objfile));
7631
7632 /* The queue starts out with one item, but following a DIE reference
7633 may load a new CU, adding it to the end of the queue. */
7634 while (!per_objfile->queue->empty ())
7635 {
7636 dwarf2_queue_item &item = per_objfile->queue->front ();
7637 dwarf2_per_cu_data *per_cu = item.per_cu;
7638
7639 if (!per_objfile->symtab_set_p (per_cu))
7640 {
7641 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
7642
7643 /* Skip dummy CUs. */
7644 if (cu != nullptr)
7645 {
7646 unsigned int debug_print_threshold;
7647 char buf[100];
7648
7649 if (per_cu->is_debug_types)
7650 {
7651 struct signatured_type *sig_type =
7652 (struct signatured_type *) per_cu;
7653
7654 sprintf (buf, "TU %s at offset %s",
7655 hex_string (sig_type->signature),
7656 sect_offset_str (per_cu->sect_off));
7657 /* There can be 100s of TUs.
7658 Only print them in verbose mode. */
7659 debug_print_threshold = 2;
7660 }
7661 else
7662 {
7663 sprintf (buf, "CU at offset %s",
7664 sect_offset_str (per_cu->sect_off));
7665 debug_print_threshold = 1;
7666 }
7667
7668 if (dwarf_read_debug >= debug_print_threshold)
7669 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
7670
7671 if (per_cu->is_debug_types)
7672 process_full_type_unit (cu, item.pretend_language);
7673 else
7674 process_full_comp_unit (cu, item.pretend_language);
7675
7676 if (dwarf_read_debug >= debug_print_threshold)
7677 dwarf_read_debug_printf ("Done expanding %s", buf);
7678 }
7679 }
7680
7681 per_cu->queued = 0;
7682 per_objfile->queue->pop ();
7683 }
7684
7685 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7686 objfile_name (per_objfile->objfile));
7687 }
7688
7689 /* Trivial hash function for die_info: the hash value of a DIE
7690 is its offset in .debug_info for this objfile. */
7691
7692 static hashval_t
7693 die_hash (const void *item)
7694 {
7695 const struct die_info *die = (const struct die_info *) item;
7696
7697 return to_underlying (die->sect_off);
7698 }
7699
7700 /* Trivial comparison function for die_info structures: two DIEs
7701 are equal if they have the same offset. */
7702
7703 static int
7704 die_eq (const void *item_lhs, const void *item_rhs)
7705 {
7706 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7707 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7708
7709 return die_lhs->sect_off == die_rhs->sect_off;
7710 }
7711
7712 /* Load the DIEs associated with PER_CU into memory.
7713
7714 In some cases, the caller, while reading partial symbols, will need to load
7715 the full symbols for the CU for some reason. It will already have a
7716 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7717 rather than creating a new one. */
7718
7719 static void
7720 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
7721 dwarf2_per_objfile *per_objfile,
7722 dwarf2_cu *existing_cu,
7723 bool skip_partial,
7724 enum language pretend_language)
7725 {
7726 gdb_assert (! this_cu->is_debug_types);
7727
7728 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
7729 if (reader.dummy_p)
7730 return;
7731
7732 struct dwarf2_cu *cu = reader.cu;
7733 const gdb_byte *info_ptr = reader.info_ptr;
7734
7735 gdb_assert (cu->die_hash == NULL);
7736 cu->die_hash =
7737 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
7738 die_hash,
7739 die_eq,
7740 NULL,
7741 &cu->comp_unit_obstack,
7742 hashtab_obstack_allocate,
7743 dummy_obstack_deallocate);
7744
7745 if (reader.comp_unit_die->has_children)
7746 reader.comp_unit_die->child
7747 = read_die_and_siblings (&reader, reader.info_ptr,
7748 &info_ptr, reader.comp_unit_die);
7749 cu->dies = reader.comp_unit_die;
7750 /* comp_unit_die is not stored in die_hash, no need. */
7751
7752 /* We try not to read any attributes in this function, because not
7753 all CUs needed for references have been loaded yet, and symbol
7754 table processing isn't initialized. But we have to set the CU language,
7755 or we won't be able to build types correctly.
7756 Similarly, if we do not read the producer, we can not apply
7757 producer-specific interpretation. */
7758 prepare_one_comp_unit (cu, cu->dies, pretend_language);
7759
7760 reader.keep ();
7761 }
7762
7763 /* Add a DIE to the delayed physname list. */
7764
7765 static void
7766 add_to_method_list (struct type *type, int fnfield_index, int index,
7767 const char *name, struct die_info *die,
7768 struct dwarf2_cu *cu)
7769 {
7770 struct delayed_method_info mi;
7771 mi.type = type;
7772 mi.fnfield_index = fnfield_index;
7773 mi.index = index;
7774 mi.name = name;
7775 mi.die = die;
7776 cu->method_list.push_back (mi);
7777 }
7778
7779 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7780 "const" / "volatile". If so, decrements LEN by the length of the
7781 modifier and return true. Otherwise return false. */
7782
7783 template<size_t N>
7784 static bool
7785 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
7786 {
7787 size_t mod_len = sizeof (mod) - 1;
7788 if (len > mod_len && startswith (physname + (len - mod_len), mod))
7789 {
7790 len -= mod_len;
7791 return true;
7792 }
7793 return false;
7794 }
7795
7796 /* Compute the physnames of any methods on the CU's method list.
7797
7798 The computation of method physnames is delayed in order to avoid the
7799 (bad) condition that one of the method's formal parameters is of an as yet
7800 incomplete type. */
7801
7802 static void
7803 compute_delayed_physnames (struct dwarf2_cu *cu)
7804 {
7805 /* Only C++ delays computing physnames. */
7806 if (cu->method_list.empty ())
7807 return;
7808 gdb_assert (cu->lang () == language_cplus);
7809
7810 for (const delayed_method_info &mi : cu->method_list)
7811 {
7812 const char *physname;
7813 struct fn_fieldlist *fn_flp
7814 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
7815 physname = dwarf2_physname (mi.name, mi.die, cu);
7816 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
7817 = physname ? physname : "";
7818
7819 /* Since there's no tag to indicate whether a method is a
7820 const/volatile overload, extract that information out of the
7821 demangled name. */
7822 if (physname != NULL)
7823 {
7824 size_t len = strlen (physname);
7825
7826 while (1)
7827 {
7828 if (physname[len] == ')') /* shortcut */
7829 break;
7830 else if (check_modifier (physname, len, " const"))
7831 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
7832 else if (check_modifier (physname, len, " volatile"))
7833 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
7834 else
7835 break;
7836 }
7837 }
7838 }
7839
7840 /* The list is no longer needed. */
7841 cu->method_list.clear ();
7842 }
7843
7844 /* Go objects should be embedded in a DW_TAG_module DIE,
7845 and it's not clear if/how imported objects will appear.
7846 To keep Go support simple until that's worked out,
7847 go back through what we've read and create something usable.
7848 We could do this while processing each DIE, and feels kinda cleaner,
7849 but that way is more invasive.
7850 This is to, for example, allow the user to type "p var" or "b main"
7851 without having to specify the package name, and allow lookups
7852 of module.object to work in contexts that use the expression
7853 parser. */
7854
7855 static void
7856 fixup_go_packaging (struct dwarf2_cu *cu)
7857 {
7858 gdb::unique_xmalloc_ptr<char> package_name;
7859 struct pending *list;
7860 int i;
7861
7862 for (list = *cu->get_builder ()->get_global_symbols ();
7863 list != NULL;
7864 list = list->next)
7865 {
7866 for (i = 0; i < list->nsyms; ++i)
7867 {
7868 struct symbol *sym = list->symbol[i];
7869
7870 if (sym->language () == language_go
7871 && sym->aclass () == LOC_BLOCK)
7872 {
7873 gdb::unique_xmalloc_ptr<char> this_package_name
7874 (go_symbol_package_name (sym));
7875
7876 if (this_package_name == NULL)
7877 continue;
7878 if (package_name == NULL)
7879 package_name = std::move (this_package_name);
7880 else
7881 {
7882 struct objfile *objfile = cu->per_objfile->objfile;
7883 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
7884 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7885 (sym->symtab () != NULL
7886 ? symtab_to_filename_for_display
7887 (sym->symtab ())
7888 : objfile_name (objfile)),
7889 this_package_name.get (), package_name.get ());
7890 }
7891 }
7892 }
7893 }
7894
7895 if (package_name != NULL)
7896 {
7897 struct objfile *objfile = cu->per_objfile->objfile;
7898 const char *saved_package_name = objfile->intern (package_name.get ());
7899 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
7900 saved_package_name);
7901 struct symbol *sym;
7902
7903 sym = new (&objfile->objfile_obstack) symbol;
7904 sym->set_language (language_go, &objfile->objfile_obstack);
7905 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
7906 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7907 e.g., "main" finds the "main" module and not C's main(). */
7908 sym->set_domain (STRUCT_DOMAIN);
7909 sym->set_aclass_index (LOC_TYPEDEF);
7910 sym->set_type (type);
7911
7912 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
7913 }
7914 }
7915
7916 /* Allocate a fully-qualified name consisting of the two parts on the
7917 obstack. */
7918
7919 static const char *
7920 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
7921 {
7922 return obconcat (obstack, p1, "::", p2, (char *) NULL);
7923 }
7924
7925 /* A helper that allocates a variant part to attach to a Rust enum
7926 type. OBSTACK is where the results should be allocated. TYPE is
7927 the type we're processing. DISCRIMINANT_INDEX is the index of the
7928 discriminant. It must be the index of one of the fields of TYPE,
7929 or -1 to mean there is no discriminant (univariant enum).
7930 DEFAULT_INDEX is the index of the default field; or -1 if there is
7931 no default. RANGES is indexed by "effective" field number (the
7932 field index, but omitting the discriminant and default fields) and
7933 must hold the discriminant values used by the variants. Note that
7934 RANGES must have a lifetime at least as long as OBSTACK -- either
7935 already allocated on it, or static. */
7936
7937 static void
7938 alloc_rust_variant (struct obstack *obstack, struct type *type,
7939 int discriminant_index, int default_index,
7940 gdb::array_view<discriminant_range> ranges)
7941 {
7942 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7943 gdb_assert (discriminant_index == -1
7944 || (discriminant_index >= 0
7945 && discriminant_index < type->num_fields ()));
7946 gdb_assert (default_index == -1
7947 || (default_index >= 0 && default_index < type->num_fields ()));
7948
7949 /* We have one variant for each non-discriminant field. */
7950 int n_variants = type->num_fields ();
7951 if (discriminant_index != -1)
7952 --n_variants;
7953
7954 variant *variants = new (obstack) variant[n_variants];
7955 int var_idx = 0;
7956 int range_idx = 0;
7957 for (int i = 0; i < type->num_fields (); ++i)
7958 {
7959 if (i == discriminant_index)
7960 continue;
7961
7962 variants[var_idx].first_field = i;
7963 variants[var_idx].last_field = i + 1;
7964
7965 /* The default field does not need a range, but other fields do.
7966 We skipped the discriminant above. */
7967 if (i != default_index)
7968 {
7969 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
7970 ++range_idx;
7971 }
7972
7973 ++var_idx;
7974 }
7975
7976 gdb_assert (range_idx == ranges.size ());
7977 gdb_assert (var_idx == n_variants);
7978
7979 variant_part *part = new (obstack) variant_part;
7980 part->discriminant_index = discriminant_index;
7981 /* If there is no discriminant, then whether it is signed is of no
7982 consequence. */
7983 part->is_unsigned
7984 = (discriminant_index == -1
7985 ? false
7986 : type->field (discriminant_index).type ()->is_unsigned ());
7987 part->variants = gdb::array_view<variant> (variants, n_variants);
7988
7989 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
7990 gdb::array_view<variant_part> *prop_value
7991 = new (storage) gdb::array_view<variant_part> (part, 1);
7992
7993 struct dynamic_prop prop;
7994 prop.set_variant_parts (prop_value);
7995
7996 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
7997 }
7998
7999 /* Some versions of rustc emitted enums in an unusual way.
8000
8001 Ordinary enums were emitted as unions. The first element of each
8002 structure in the union was named "RUST$ENUM$DISR". This element
8003 held the discriminant.
8004
8005 These versions of Rust also implemented the "non-zero"
8006 optimization. When the enum had two values, and one is empty and
8007 the other holds a pointer that cannot be zero, the pointer is used
8008 as the discriminant, with a zero value meaning the empty variant.
8009 Here, the union's first member is of the form
8010 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8011 where the fieldnos are the indices of the fields that should be
8012 traversed in order to find the field (which may be several fields deep)
8013 and the variantname is the name of the variant of the case when the
8014 field is zero.
8015
8016 This function recognizes whether TYPE is of one of these forms,
8017 and, if so, smashes it to be a variant type. */
8018
8019 static void
8020 quirk_rust_enum (struct type *type, struct objfile *objfile)
8021 {
8022 gdb_assert (type->code () == TYPE_CODE_UNION);
8023
8024 /* We don't need to deal with empty enums. */
8025 if (type->num_fields () == 0)
8026 return;
8027
8028 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8029 if (type->num_fields () == 1
8030 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
8031 {
8032 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
8033
8034 /* Decode the field name to find the offset of the
8035 discriminant. */
8036 ULONGEST bit_offset = 0;
8037 struct type *field_type = type->field (0).type ();
8038 while (name[0] >= '0' && name[0] <= '9')
8039 {
8040 char *tail;
8041 unsigned long index = strtoul (name, &tail, 10);
8042 name = tail;
8043 if (*name != '$'
8044 || index >= field_type->num_fields ()
8045 || (field_type->field (index).loc_kind ()
8046 != FIELD_LOC_KIND_BITPOS))
8047 {
8048 complaint (_("Could not parse Rust enum encoding string \"%s\""
8049 "[in module %s]"),
8050 type->field (0).name (),
8051 objfile_name (objfile));
8052 return;
8053 }
8054 ++name;
8055
8056 bit_offset += field_type->field (index).loc_bitpos ();
8057 field_type = field_type->field (index).type ();
8058 }
8059
8060 /* Smash this type to be a structure type. We have to do this
8061 because the type has already been recorded. */
8062 type->set_code (TYPE_CODE_STRUCT);
8063 type->set_num_fields (3);
8064 /* Save the field we care about. */
8065 struct field saved_field = type->field (0);
8066 type->set_fields
8067 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
8068
8069 /* Put the discriminant at index 0. */
8070 type->field (0).set_type (field_type);
8071 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8072 type->field (0).set_name ("<<discriminant>>");
8073 type->field (0).set_loc_bitpos (bit_offset);
8074
8075 /* The order of fields doesn't really matter, so put the real
8076 field at index 1 and the data-less field at index 2. */
8077 type->field (1) = saved_field;
8078 type->field (1).set_name
8079 (rust_last_path_segment (type->field (1).type ()->name ()));
8080 type->field (1).type ()->set_name
8081 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8082 type->field (1).name ()));
8083
8084 const char *dataless_name
8085 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
8086 name);
8087 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
8088 dataless_name);
8089 type->field (2).set_type (dataless_type);
8090 /* NAME points into the original discriminant name, which
8091 already has the correct lifetime. */
8092 type->field (2).set_name (name);
8093 type->field (2).set_loc_bitpos (0);
8094
8095 /* Indicate that this is a variant type. */
8096 static discriminant_range ranges[1] = { { 0, 0 } };
8097 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
8098 }
8099 /* A union with a single anonymous field is probably an old-style
8100 univariant enum. */
8101 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
8102 {
8103 /* Smash this type to be a structure type. We have to do this
8104 because the type has already been recorded. */
8105 type->set_code (TYPE_CODE_STRUCT);
8106
8107 struct type *field_type = type->field (0).type ();
8108 const char *variant_name
8109 = rust_last_path_segment (field_type->name ());
8110 type->field (0).set_name (variant_name);
8111 field_type->set_name
8112 (rust_fully_qualify (&objfile->objfile_obstack,
8113 type->name (), variant_name));
8114
8115 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
8116 }
8117 else
8118 {
8119 struct type *disr_type = nullptr;
8120 for (int i = 0; i < type->num_fields (); ++i)
8121 {
8122 disr_type = type->field (i).type ();
8123
8124 if (disr_type->code () != TYPE_CODE_STRUCT)
8125 {
8126 /* All fields of a true enum will be structs. */
8127 return;
8128 }
8129 else if (disr_type->num_fields () == 0)
8130 {
8131 /* Could be data-less variant, so keep going. */
8132 disr_type = nullptr;
8133 }
8134 else if (strcmp (disr_type->field (0).name (),
8135 "RUST$ENUM$DISR") != 0)
8136 {
8137 /* Not a Rust enum. */
8138 return;
8139 }
8140 else
8141 {
8142 /* Found one. */
8143 break;
8144 }
8145 }
8146
8147 /* If we got here without a discriminant, then it's probably
8148 just a union. */
8149 if (disr_type == nullptr)
8150 return;
8151
8152 /* Smash this type to be a structure type. We have to do this
8153 because the type has already been recorded. */
8154 type->set_code (TYPE_CODE_STRUCT);
8155
8156 /* Make space for the discriminant field. */
8157 struct field *disr_field = &disr_type->field (0);
8158 field *new_fields
8159 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
8160 * sizeof (struct field)));
8161 memcpy (new_fields + 1, type->fields (),
8162 type->num_fields () * sizeof (struct field));
8163 type->set_fields (new_fields);
8164 type->set_num_fields (type->num_fields () + 1);
8165
8166 /* Install the discriminant at index 0 in the union. */
8167 type->field (0) = *disr_field;
8168 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
8169 type->field (0).set_name ("<<discriminant>>");
8170
8171 /* We need a way to find the correct discriminant given a
8172 variant name. For convenience we build a map here. */
8173 struct type *enum_type = disr_field->type ();
8174 std::unordered_map<std::string, ULONGEST> discriminant_map;
8175 for (int i = 0; i < enum_type->num_fields (); ++i)
8176 {
8177 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
8178 {
8179 const char *name
8180 = rust_last_path_segment (enum_type->field (i).name ());
8181 discriminant_map[name] = enum_type->field (i).loc_enumval ();
8182 }
8183 }
8184
8185 int n_fields = type->num_fields ();
8186 /* We don't need a range entry for the discriminant, but we do
8187 need one for every other field, as there is no default
8188 variant. */
8189 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
8190 discriminant_range,
8191 n_fields - 1);
8192 /* Skip the discriminant here. */
8193 for (int i = 1; i < n_fields; ++i)
8194 {
8195 /* Find the final word in the name of this variant's type.
8196 That name can be used to look up the correct
8197 discriminant. */
8198 const char *variant_name
8199 = rust_last_path_segment (type->field (i).type ()->name ());
8200
8201 auto iter = discriminant_map.find (variant_name);
8202 if (iter != discriminant_map.end ())
8203 {
8204 ranges[i - 1].low = iter->second;
8205 ranges[i - 1].high = iter->second;
8206 }
8207
8208 /* In Rust, each element should have the size of the
8209 enclosing enum. */
8210 type->field (i).type ()->set_length (type->length ());
8211
8212 /* Remove the discriminant field, if it exists. */
8213 struct type *sub_type = type->field (i).type ();
8214 if (sub_type->num_fields () > 0)
8215 {
8216 sub_type->set_num_fields (sub_type->num_fields () - 1);
8217 sub_type->set_fields (sub_type->fields () + 1);
8218 }
8219 type->field (i).set_name (variant_name);
8220 sub_type->set_name
8221 (rust_fully_qualify (&objfile->objfile_obstack,
8222 type->name (), variant_name));
8223 }
8224
8225 /* Indicate that this is a variant type. */
8226 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
8227 gdb::array_view<discriminant_range> (ranges,
8228 n_fields - 1));
8229 }
8230 }
8231
8232 /* Rewrite some Rust unions to be structures with variants parts. */
8233
8234 static void
8235 rust_union_quirks (struct dwarf2_cu *cu)
8236 {
8237 gdb_assert (cu->lang () == language_rust);
8238 for (type *type_ : cu->rust_unions)
8239 quirk_rust_enum (type_, cu->per_objfile->objfile);
8240 /* We don't need this any more. */
8241 cu->rust_unions.clear ();
8242 }
8243
8244 /* See read.h. */
8245
8246 type_unit_group_unshareable *
8247 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
8248 {
8249 auto iter = this->m_type_units.find (tu_group);
8250 if (iter != this->m_type_units.end ())
8251 return iter->second.get ();
8252
8253 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
8254 type_unit_group_unshareable *result = uniq.get ();
8255 this->m_type_units[tu_group] = std::move (uniq);
8256 return result;
8257 }
8258
8259 struct type *
8260 dwarf2_per_objfile::get_type_for_signatured_type
8261 (signatured_type *sig_type) const
8262 {
8263 auto iter = this->m_type_map.find (sig_type);
8264 if (iter == this->m_type_map.end ())
8265 return nullptr;
8266
8267 return iter->second;
8268 }
8269
8270 void dwarf2_per_objfile::set_type_for_signatured_type
8271 (signatured_type *sig_type, struct type *type)
8272 {
8273 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
8274
8275 this->m_type_map[sig_type] = type;
8276 }
8277
8278 /* A helper function for computing the list of all symbol tables
8279 included by PER_CU. */
8280
8281 static void
8282 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
8283 htab_t all_children, htab_t all_type_symtabs,
8284 dwarf2_per_cu_data *per_cu,
8285 dwarf2_per_objfile *per_objfile,
8286 struct compunit_symtab *immediate_parent)
8287 {
8288 void **slot = htab_find_slot (all_children, per_cu, INSERT);
8289 if (*slot != NULL)
8290 {
8291 /* This inclusion and its children have been processed. */
8292 return;
8293 }
8294
8295 *slot = per_cu;
8296
8297 /* Only add a CU if it has a symbol table. */
8298 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8299 if (cust != NULL)
8300 {
8301 /* If this is a type unit only add its symbol table if we haven't
8302 seen it yet (type unit per_cu's can share symtabs). */
8303 if (per_cu->is_debug_types)
8304 {
8305 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8306 if (*slot == NULL)
8307 {
8308 *slot = cust;
8309 result->push_back (cust);
8310 if (cust->user == NULL)
8311 cust->user = immediate_parent;
8312 }
8313 }
8314 else
8315 {
8316 result->push_back (cust);
8317 if (cust->user == NULL)
8318 cust->user = immediate_parent;
8319 }
8320 }
8321
8322 if (!per_cu->imported_symtabs_empty ())
8323 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8324 {
8325 recursively_compute_inclusions (result, all_children,
8326 all_type_symtabs, ptr, per_objfile,
8327 cust);
8328 }
8329 }
8330
8331 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8332 PER_CU. */
8333
8334 static void
8335 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
8336 dwarf2_per_objfile *per_objfile)
8337 {
8338 gdb_assert (! per_cu->is_debug_types);
8339
8340 if (!per_cu->imported_symtabs_empty ())
8341 {
8342 int len;
8343 std::vector<compunit_symtab *> result_symtabs;
8344 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
8345
8346 /* If we don't have a symtab, we can just skip this case. */
8347 if (cust == NULL)
8348 return;
8349
8350 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
8351 htab_eq_pointer,
8352 NULL, xcalloc, xfree));
8353 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
8354 htab_eq_pointer,
8355 NULL, xcalloc, xfree));
8356
8357 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
8358 {
8359 recursively_compute_inclusions (&result_symtabs, all_children.get (),
8360 all_type_symtabs.get (), ptr,
8361 per_objfile, cust);
8362 }
8363
8364 /* Now we have a transitive closure of all the included symtabs. */
8365 len = result_symtabs.size ();
8366 cust->includes
8367 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8368 struct compunit_symtab *, len + 1);
8369 memcpy (cust->includes, result_symtabs.data (),
8370 len * sizeof (compunit_symtab *));
8371 cust->includes[len] = NULL;
8372 }
8373 }
8374
8375 /* Compute the 'includes' field for the symtabs of all the CUs we just
8376 read. */
8377
8378 static void
8379 process_cu_includes (dwarf2_per_objfile *per_objfile)
8380 {
8381 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
8382 {
8383 if (! iter->is_debug_types)
8384 compute_compunit_symtab_includes (iter, per_objfile);
8385 }
8386
8387 per_objfile->per_bfd->just_read_cus.clear ();
8388 }
8389
8390 /* Generate full symbol information for CU, whose DIEs have
8391 already been loaded into memory. */
8392
8393 static void
8394 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
8395 {
8396 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8397 struct objfile *objfile = per_objfile->objfile;
8398 struct gdbarch *gdbarch = objfile->arch ();
8399 CORE_ADDR lowpc, highpc;
8400 struct compunit_symtab *cust;
8401 CORE_ADDR baseaddr;
8402 struct block *static_block;
8403 CORE_ADDR addr;
8404
8405 baseaddr = objfile->text_section_offset ();
8406
8407 /* Clear the list here in case something was left over. */
8408 cu->method_list.clear ();
8409
8410 dwarf2_find_base_address (cu->dies, cu);
8411
8412 /* Before we start reading the top-level DIE, ensure it has a valid tag
8413 type. */
8414 switch (cu->dies->tag)
8415 {
8416 case DW_TAG_compile_unit:
8417 case DW_TAG_partial_unit:
8418 case DW_TAG_type_unit:
8419 break;
8420 default:
8421 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8422 dwarf_tag_name (cu->dies->tag),
8423 sect_offset_str (cu->per_cu->sect_off),
8424 objfile_name (per_objfile->objfile));
8425 }
8426
8427 /* Do line number decoding in read_file_scope () */
8428 process_die (cu->dies, cu);
8429
8430 /* For now fudge the Go package. */
8431 if (cu->lang () == language_go)
8432 fixup_go_packaging (cu);
8433
8434 /* Now that we have processed all the DIEs in the CU, all the types
8435 should be complete, and it should now be safe to compute all of the
8436 physnames. */
8437 compute_delayed_physnames (cu);
8438
8439 if (cu->lang () == language_rust)
8440 rust_union_quirks (cu);
8441
8442 /* Some compilers don't define a DW_AT_high_pc attribute for the
8443 compilation unit. If the DW_AT_high_pc is missing, synthesize
8444 it, by scanning the DIE's below the compilation unit. */
8445 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8446
8447 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8448 static_block
8449 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
8450
8451 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8452 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8453 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8454 addrmap to help ensure it has an accurate map of pc values belonging to
8455 this comp unit. */
8456 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8457
8458 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
8459 (static_block, SECT_OFF_TEXT (objfile), 0);
8460
8461 if (cust != NULL)
8462 {
8463 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8464
8465 /* Set symtab language to language from DW_AT_language. If the
8466 compilation is from a C file generated by language preprocessors, do
8467 not set the language if it was already deduced by start_subfile. */
8468 if (!(cu->lang () == language_c
8469 && cust->primary_filetab ()->language () != language_unknown))
8470 cust->primary_filetab ()->set_language (cu->lang ());
8471
8472 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8473 produce DW_AT_location with location lists but it can be possibly
8474 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8475 there were bugs in prologue debug info, fixed later in GCC-4.5
8476 by "unwind info for epilogues" patch (which is not directly related).
8477
8478 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8479 needed, it would be wrong due to missing DW_AT_producer there.
8480
8481 Still one can confuse GDB by using non-standard GCC compilation
8482 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8483 */
8484 if (cu->has_loclist && gcc_4_minor >= 5)
8485 cust->set_locations_valid (true);
8486
8487 if (gcc_4_minor >= 5)
8488 cust->set_epilogue_unwind_valid (true);
8489
8490 cust->set_call_site_htab (cu->call_site_htab);
8491 }
8492
8493 per_objfile->set_symtab (cu->per_cu, cust);
8494
8495 /* Push it for inclusion processing later. */
8496 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
8497
8498 /* Not needed any more. */
8499 cu->reset_builder ();
8500 }
8501
8502 /* Generate full symbol information for type unit CU, whose DIEs have
8503 already been loaded into memory. */
8504
8505 static void
8506 process_full_type_unit (dwarf2_cu *cu,
8507 enum language pretend_language)
8508 {
8509 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8510 struct objfile *objfile = per_objfile->objfile;
8511 struct compunit_symtab *cust;
8512 struct signatured_type *sig_type;
8513
8514 gdb_assert (cu->per_cu->is_debug_types);
8515 sig_type = (struct signatured_type *) cu->per_cu;
8516
8517 /* Clear the list here in case something was left over. */
8518 cu->method_list.clear ();
8519
8520 /* The symbol tables are set up in read_type_unit_scope. */
8521 process_die (cu->dies, cu);
8522
8523 /* For now fudge the Go package. */
8524 if (cu->lang () == language_go)
8525 fixup_go_packaging (cu);
8526
8527 /* Now that we have processed all the DIEs in the CU, all the types
8528 should be complete, and it should now be safe to compute all of the
8529 physnames. */
8530 compute_delayed_physnames (cu);
8531
8532 if (cu->lang () == language_rust)
8533 rust_union_quirks (cu);
8534
8535 /* TUs share symbol tables.
8536 If this is the first TU to use this symtab, complete the construction
8537 of it with end_expandable_symtab. Otherwise, complete the addition of
8538 this TU's symbols to the existing symtab. */
8539 type_unit_group_unshareable *tug_unshare =
8540 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8541 if (tug_unshare->compunit_symtab == NULL)
8542 {
8543 buildsym_compunit *builder = cu->get_builder ();
8544 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8545 tug_unshare->compunit_symtab = cust;
8546
8547 if (cust != NULL)
8548 {
8549 /* Set symtab language to language from DW_AT_language. If the
8550 compilation is from a C file generated by language preprocessors,
8551 do not set the language if it was already deduced by
8552 start_subfile. */
8553 if (!(cu->lang () == language_c
8554 && cust->primary_filetab ()->language () != language_c))
8555 cust->primary_filetab ()->set_language (cu->lang ());
8556 }
8557 }
8558 else
8559 {
8560 cu->get_builder ()->augment_type_symtab ();
8561 cust = tug_unshare->compunit_symtab;
8562 }
8563
8564 per_objfile->set_symtab (cu->per_cu, cust);
8565
8566 /* Not needed any more. */
8567 cu->reset_builder ();
8568 }
8569
8570 /* Process an imported unit DIE. */
8571
8572 static void
8573 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8574 {
8575 struct attribute *attr;
8576
8577 /* For now we don't handle imported units in type units. */
8578 if (cu->per_cu->is_debug_types)
8579 {
8580 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8581 " supported in type units [in module %s]"),
8582 objfile_name (cu->per_objfile->objfile));
8583 }
8584
8585 attr = dwarf2_attr (die, DW_AT_import, cu);
8586 if (attr != NULL)
8587 {
8588 sect_offset sect_off = attr->get_ref_die_offset ();
8589 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8590 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8591 dwarf2_per_cu_data *per_cu
8592 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
8593 per_objfile->per_bfd);
8594
8595 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8596 into another compilation unit, at root level. Regard this as a hint,
8597 and ignore it. This is a best effort, it only works if unit_type and
8598 lang are already set. */
8599 if (die->parent && die->parent->parent == NULL
8600 && per_cu->unit_type (false) == DW_UT_compile
8601 && per_cu->lang (false) == language_cplus)
8602 return;
8603
8604 /* If necessary, add it to the queue and load its DIEs. */
8605 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
8606 cu->lang ()))
8607 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
8608 false, cu->lang ());
8609
8610 cu->per_cu->imported_symtabs_push (per_cu);
8611 }
8612 }
8613
8614 /* RAII object that represents a process_die scope: i.e.,
8615 starts/finishes processing a DIE. */
8616 class process_die_scope
8617 {
8618 public:
8619 process_die_scope (die_info *die, dwarf2_cu *cu)
8620 : m_die (die), m_cu (cu)
8621 {
8622 /* We should only be processing DIEs not already in process. */
8623 gdb_assert (!m_die->in_process);
8624 m_die->in_process = true;
8625 }
8626
8627 ~process_die_scope ()
8628 {
8629 m_die->in_process = false;
8630
8631 /* If we're done processing the DIE for the CU that owns the line
8632 header, we don't need the line header anymore. */
8633 if (m_cu->line_header_die_owner == m_die)
8634 {
8635 delete m_cu->line_header;
8636 m_cu->line_header = NULL;
8637 m_cu->line_header_die_owner = NULL;
8638 }
8639 }
8640
8641 private:
8642 die_info *m_die;
8643 dwarf2_cu *m_cu;
8644 };
8645
8646 /* Process a die and its children. */
8647
8648 static void
8649 process_die (struct die_info *die, struct dwarf2_cu *cu)
8650 {
8651 process_die_scope scope (die, cu);
8652
8653 switch (die->tag)
8654 {
8655 case DW_TAG_padding:
8656 break;
8657 case DW_TAG_compile_unit:
8658 case DW_TAG_partial_unit:
8659 read_file_scope (die, cu);
8660 break;
8661 case DW_TAG_type_unit:
8662 read_type_unit_scope (die, cu);
8663 break;
8664 case DW_TAG_subprogram:
8665 /* Nested subprograms in Fortran get a prefix. */
8666 if (cu->lang () == language_fortran
8667 && die->parent != NULL
8668 && die->parent->tag == DW_TAG_subprogram)
8669 cu->processing_has_namespace_info = true;
8670 /* Fall through. */
8671 case DW_TAG_inlined_subroutine:
8672 read_func_scope (die, cu);
8673 break;
8674 case DW_TAG_lexical_block:
8675 case DW_TAG_try_block:
8676 case DW_TAG_catch_block:
8677 read_lexical_block_scope (die, cu);
8678 break;
8679 case DW_TAG_call_site:
8680 case DW_TAG_GNU_call_site:
8681 read_call_site_scope (die, cu);
8682 break;
8683 case DW_TAG_class_type:
8684 case DW_TAG_interface_type:
8685 case DW_TAG_structure_type:
8686 case DW_TAG_union_type:
8687 case DW_TAG_namelist:
8688 process_structure_scope (die, cu);
8689 break;
8690 case DW_TAG_enumeration_type:
8691 process_enumeration_scope (die, cu);
8692 break;
8693
8694 /* These dies have a type, but processing them does not create
8695 a symbol or recurse to process the children. Therefore we can
8696 read them on-demand through read_type_die. */
8697 case DW_TAG_subroutine_type:
8698 case DW_TAG_set_type:
8699 case DW_TAG_pointer_type:
8700 case DW_TAG_ptr_to_member_type:
8701 case DW_TAG_reference_type:
8702 case DW_TAG_rvalue_reference_type:
8703 case DW_TAG_string_type:
8704 break;
8705
8706 case DW_TAG_array_type:
8707 /* We only need to handle this case for Ada -- in other
8708 languages, it's normal for the compiler to emit a typedef
8709 instead. */
8710 if (cu->lang () != language_ada)
8711 break;
8712 /* FALLTHROUGH */
8713 case DW_TAG_base_type:
8714 case DW_TAG_subrange_type:
8715 case DW_TAG_generic_subrange:
8716 case DW_TAG_typedef:
8717 /* Add a typedef symbol for the type definition, if it has a
8718 DW_AT_name. */
8719 new_symbol (die, read_type_die (die, cu), cu);
8720 break;
8721 case DW_TAG_common_block:
8722 read_common_block (die, cu);
8723 break;
8724 case DW_TAG_common_inclusion:
8725 break;
8726 case DW_TAG_namespace:
8727 cu->processing_has_namespace_info = true;
8728 read_namespace (die, cu);
8729 break;
8730 case DW_TAG_module:
8731 cu->processing_has_namespace_info = true;
8732 read_module (die, cu);
8733 break;
8734 case DW_TAG_imported_declaration:
8735 cu->processing_has_namespace_info = true;
8736 if (read_alias (die, cu))
8737 break;
8738 /* The declaration is neither a global namespace nor a variable
8739 alias. */
8740 /* Fall through. */
8741 case DW_TAG_imported_module:
8742 cu->processing_has_namespace_info = true;
8743 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8744 || cu->lang () != language_fortran))
8745 complaint (_("Tag '%s' has unexpected children"),
8746 dwarf_tag_name (die->tag));
8747 read_import_statement (die, cu);
8748 break;
8749
8750 case DW_TAG_imported_unit:
8751 process_imported_unit_die (die, cu);
8752 break;
8753
8754 case DW_TAG_variable:
8755 read_variable (die, cu);
8756 break;
8757
8758 default:
8759 new_symbol (die, NULL, cu);
8760 break;
8761 }
8762 }
8763 \f
8764 /* DWARF name computation. */
8765
8766 /* A helper function for dwarf2_compute_name which determines whether DIE
8767 needs to have the name of the scope prepended to the name listed in the
8768 die. */
8769
8770 static int
8771 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8772 {
8773 struct attribute *attr;
8774
8775 switch (die->tag)
8776 {
8777 case DW_TAG_namespace:
8778 case DW_TAG_typedef:
8779 case DW_TAG_class_type:
8780 case DW_TAG_interface_type:
8781 case DW_TAG_structure_type:
8782 case DW_TAG_union_type:
8783 case DW_TAG_enumeration_type:
8784 case DW_TAG_enumerator:
8785 case DW_TAG_subprogram:
8786 case DW_TAG_inlined_subroutine:
8787 case DW_TAG_member:
8788 case DW_TAG_imported_declaration:
8789 return 1;
8790
8791 case DW_TAG_variable:
8792 case DW_TAG_constant:
8793 /* We only need to prefix "globally" visible variables. These include
8794 any variable marked with DW_AT_external or any variable that
8795 lives in a namespace. [Variables in anonymous namespaces
8796 require prefixing, but they are not DW_AT_external.] */
8797
8798 if (dwarf2_attr (die, DW_AT_specification, cu))
8799 {
8800 struct dwarf2_cu *spec_cu = cu;
8801
8802 return die_needs_namespace (die_specification (die, &spec_cu),
8803 spec_cu);
8804 }
8805
8806 attr = dwarf2_attr (die, DW_AT_external, cu);
8807 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8808 && die->parent->tag != DW_TAG_module)
8809 return 0;
8810 /* A variable in a lexical block of some kind does not need a
8811 namespace, even though in C++ such variables may be external
8812 and have a mangled name. */
8813 if (die->parent->tag == DW_TAG_lexical_block
8814 || die->parent->tag == DW_TAG_try_block
8815 || die->parent->tag == DW_TAG_catch_block
8816 || die->parent->tag == DW_TAG_subprogram)
8817 return 0;
8818 return 1;
8819
8820 default:
8821 return 0;
8822 }
8823 }
8824
8825 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8826 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8827 defined for the given DIE. */
8828
8829 static struct attribute *
8830 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
8831 {
8832 struct attribute *attr;
8833
8834 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8835 if (attr == NULL)
8836 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8837
8838 return attr;
8839 }
8840
8841 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8842 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8843 defined for the given DIE. */
8844
8845 static const char *
8846 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
8847 {
8848 const char *linkage_name;
8849
8850 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8851 if (linkage_name == NULL)
8852 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8853
8854 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8855 See https://github.com/rust-lang/rust/issues/32925. */
8856 if (cu->lang () == language_rust && linkage_name != NULL
8857 && strchr (linkage_name, '{') != NULL)
8858 linkage_name = NULL;
8859
8860 return linkage_name;
8861 }
8862
8863 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8864 compute the physname for the object, which include a method's:
8865 - formal parameters (C++),
8866 - receiver type (Go),
8867
8868 The term "physname" is a bit confusing.
8869 For C++, for example, it is the demangled name.
8870 For Go, for example, it's the mangled name.
8871
8872 For Ada, return the DIE's linkage name rather than the fully qualified
8873 name. PHYSNAME is ignored..
8874
8875 The result is allocated on the objfile->per_bfd's obstack and
8876 canonicalized. */
8877
8878 static const char *
8879 dwarf2_compute_name (const char *name,
8880 struct die_info *die, struct dwarf2_cu *cu,
8881 int physname)
8882 {
8883 struct objfile *objfile = cu->per_objfile->objfile;
8884
8885 if (name == NULL)
8886 name = dwarf2_name (die, cu);
8887
8888 enum language lang = cu->lang ();
8889
8890 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8891 but otherwise compute it by typename_concat inside GDB.
8892 FIXME: Actually this is not really true, or at least not always true.
8893 It's all very confusing. compute_and_set_names doesn't try to demangle
8894 Fortran names because there is no mangling standard. So new_symbol
8895 will set the demangled name to the result of dwarf2_full_name, and it is
8896 the demangled name that GDB uses if it exists. */
8897 if (lang == language_ada
8898 || (lang == language_fortran && physname))
8899 {
8900 /* For Ada unit, we prefer the linkage name over the name, as
8901 the former contains the exported name, which the user expects
8902 to be able to reference. Ideally, we want the user to be able
8903 to reference this entity using either natural or linkage name,
8904 but we haven't started looking at this enhancement yet. */
8905 const char *linkage_name = dw2_linkage_name (die, cu);
8906
8907 if (linkage_name != NULL)
8908 return linkage_name;
8909 }
8910
8911 /* These are the only languages we know how to qualify names in. */
8912 if (name != NULL
8913 && (lang == language_cplus
8914 || lang == language_fortran || lang == language_d
8915 || lang == language_rust))
8916 {
8917 if (die_needs_namespace (die, cu))
8918 {
8919 const char *prefix;
8920
8921 string_file buf;
8922
8923 prefix = determine_prefix (die, cu);
8924 if (*prefix != '\0')
8925 {
8926 gdb::unique_xmalloc_ptr<char> prefixed_name
8927 (typename_concat (NULL, prefix, name, physname, cu));
8928
8929 buf.puts (prefixed_name.get ());
8930 }
8931 else
8932 buf.puts (name);
8933
8934 /* Template parameters may be specified in the DIE's DW_AT_name, or
8935 as children with DW_TAG_template_type_param or
8936 DW_TAG_value_type_param. If the latter, add them to the name
8937 here. If the name already has template parameters, then
8938 skip this step; some versions of GCC emit both, and
8939 it is more efficient to use the pre-computed name.
8940
8941 Something to keep in mind about this process: it is very
8942 unlikely, or in some cases downright impossible, to produce
8943 something that will match the mangled name of a function.
8944 If the definition of the function has the same debug info,
8945 we should be able to match up with it anyway. But fallbacks
8946 using the minimal symbol, for instance to find a method
8947 implemented in a stripped copy of libstdc++, will not work.
8948 If we do not have debug info for the definition, we will have to
8949 match them up some other way.
8950
8951 When we do name matching there is a related problem with function
8952 templates; two instantiated function templates are allowed to
8953 differ only by their return types, which we do not add here. */
8954
8955 if (lang == language_cplus && strchr (name, '<') == NULL)
8956 {
8957 struct attribute *attr;
8958 struct die_info *child;
8959 int first = 1;
8960
8961 die->building_fullname = 1;
8962
8963 for (child = die->child; child != NULL; child = child->sibling)
8964 {
8965 struct type *type;
8966 LONGEST value;
8967 const gdb_byte *bytes;
8968 struct dwarf2_locexpr_baton *baton;
8969 struct value *v;
8970
8971 if (child->tag != DW_TAG_template_type_param
8972 && child->tag != DW_TAG_template_value_param)
8973 continue;
8974
8975 if (first)
8976 {
8977 buf.puts ("<");
8978 first = 0;
8979 }
8980 else
8981 buf.puts (", ");
8982
8983 attr = dwarf2_attr (child, DW_AT_type, cu);
8984 if (attr == NULL)
8985 {
8986 complaint (_("template parameter missing DW_AT_type"));
8987 buf.puts ("UNKNOWN_TYPE");
8988 continue;
8989 }
8990 type = die_type (child, cu);
8991
8992 if (child->tag == DW_TAG_template_type_param)
8993 {
8994 cu->language_defn->print_type (type, "", &buf, -1, 0,
8995 &type_print_raw_options);
8996 continue;
8997 }
8998
8999 attr = dwarf2_attr (child, DW_AT_const_value, cu);
9000 if (attr == NULL)
9001 {
9002 complaint (_("template parameter missing "
9003 "DW_AT_const_value"));
9004 buf.puts ("UNKNOWN_VALUE");
9005 continue;
9006 }
9007
9008 dwarf2_const_value_attr (attr, type, name,
9009 &cu->comp_unit_obstack, cu,
9010 &value, &bytes, &baton);
9011
9012 if (type->has_no_signedness ())
9013 /* GDB prints characters as NUMBER 'CHAR'. If that's
9014 changed, this can use value_print instead. */
9015 cu->language_defn->printchar (value, type, &buf);
9016 else
9017 {
9018 struct value_print_options opts;
9019
9020 if (baton != NULL)
9021 v = dwarf2_evaluate_loc_desc (type, NULL,
9022 baton->data,
9023 baton->size,
9024 baton->per_cu,
9025 baton->per_objfile);
9026 else if (bytes != NULL)
9027 {
9028 v = allocate_value (type);
9029 memcpy (value_contents_writeable (v).data (), bytes,
9030 type->length ());
9031 }
9032 else
9033 v = value_from_longest (type, value);
9034
9035 /* Specify decimal so that we do not depend on
9036 the radix. */
9037 get_formatted_print_options (&opts, 'd');
9038 opts.raw = true;
9039 value_print (v, &buf, &opts);
9040 release_value (v);
9041 }
9042 }
9043
9044 die->building_fullname = 0;
9045
9046 if (!first)
9047 {
9048 /* Close the argument list, with a space if necessary
9049 (nested templates). */
9050 if (!buf.empty () && buf.string ().back () == '>')
9051 buf.puts (" >");
9052 else
9053 buf.puts (">");
9054 }
9055 }
9056
9057 /* For C++ methods, append formal parameter type
9058 information, if PHYSNAME. */
9059
9060 if (physname && die->tag == DW_TAG_subprogram
9061 && lang == language_cplus)
9062 {
9063 struct type *type = read_type_die (die, cu);
9064
9065 c_type_print_args (type, &buf, 1, lang,
9066 &type_print_raw_options);
9067
9068 if (lang == language_cplus)
9069 {
9070 /* Assume that an artificial first parameter is
9071 "this", but do not crash if it is not. RealView
9072 marks unnamed (and thus unused) parameters as
9073 artificial; there is no way to differentiate
9074 the two cases. */
9075 if (type->num_fields () > 0
9076 && TYPE_FIELD_ARTIFICIAL (type, 0)
9077 && type->field (0).type ()->code () == TYPE_CODE_PTR
9078 && TYPE_CONST (type->field (0).type ()->target_type ()))
9079 buf.puts (" const");
9080 }
9081 }
9082
9083 const std::string &intermediate_name = buf.string ();
9084
9085 const char *canonical_name
9086 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
9087 objfile);
9088
9089 /* If we only computed INTERMEDIATE_NAME, or if
9090 INTERMEDIATE_NAME is already canonical, then we need to
9091 intern it. */
9092 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
9093 name = objfile->intern (intermediate_name);
9094 else
9095 name = canonical_name;
9096 }
9097 }
9098
9099 return name;
9100 }
9101
9102 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9103 If scope qualifiers are appropriate they will be added. The result
9104 will be allocated on the storage_obstack, or NULL if the DIE does
9105 not have a name. NAME may either be from a previous call to
9106 dwarf2_name or NULL.
9107
9108 The output string will be canonicalized (if C++). */
9109
9110 static const char *
9111 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9112 {
9113 return dwarf2_compute_name (name, die, cu, 0);
9114 }
9115
9116 /* Construct a physname for the given DIE in CU. NAME may either be
9117 from a previous call to dwarf2_name or NULL. The result will be
9118 allocated on the objfile_objstack or NULL if the DIE does not have a
9119 name.
9120
9121 The output string will be canonicalized (if C++). */
9122
9123 static const char *
9124 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
9125 {
9126 struct objfile *objfile = cu->per_objfile->objfile;
9127 const char *retval, *mangled = NULL, *canon = NULL;
9128 int need_copy = 1;
9129
9130 /* In this case dwarf2_compute_name is just a shortcut not building anything
9131 on its own. */
9132 if (!die_needs_namespace (die, cu))
9133 return dwarf2_compute_name (name, die, cu, 1);
9134
9135 if (cu->lang () != language_rust)
9136 mangled = dw2_linkage_name (die, cu);
9137
9138 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9139 has computed. */
9140 gdb::unique_xmalloc_ptr<char> demangled;
9141 if (mangled != NULL)
9142 {
9143 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
9144 {
9145 /* Do nothing (do not demangle the symbol name). */
9146 }
9147 else
9148 {
9149 /* Use DMGL_RET_DROP for C++ template functions to suppress
9150 their return type. It is easier for GDB users to search
9151 for such functions as `name(params)' than `long name(params)'.
9152 In such case the minimal symbol names do not match the full
9153 symbol names but for template functions there is never a need
9154 to look up their definition from their declaration so
9155 the only disadvantage remains the minimal symbol variant
9156 `long name(params)' does not have the proper inferior type. */
9157 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
9158 | DMGL_RET_DROP));
9159 }
9160 if (demangled)
9161 canon = demangled.get ();
9162 else
9163 {
9164 canon = mangled;
9165 need_copy = 0;
9166 }
9167 }
9168
9169 if (canon == NULL || check_physname)
9170 {
9171 const char *physname = dwarf2_compute_name (name, die, cu, 1);
9172
9173 if (canon != NULL && strcmp (physname, canon) != 0)
9174 {
9175 /* It may not mean a bug in GDB. The compiler could also
9176 compute DW_AT_linkage_name incorrectly. But in such case
9177 GDB would need to be bug-to-bug compatible. */
9178
9179 complaint (_("Computed physname <%s> does not match demangled <%s> "
9180 "(from linkage <%s>) - DIE at %s [in module %s]"),
9181 physname, canon, mangled, sect_offset_str (die->sect_off),
9182 objfile_name (objfile));
9183
9184 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9185 is available here - over computed PHYSNAME. It is safer
9186 against both buggy GDB and buggy compilers. */
9187
9188 retval = canon;
9189 }
9190 else
9191 {
9192 retval = physname;
9193 need_copy = 0;
9194 }
9195 }
9196 else
9197 retval = canon;
9198
9199 if (need_copy)
9200 retval = objfile->intern (retval);
9201
9202 return retval;
9203 }
9204
9205 /* Inspect DIE in CU for a namespace alias or a variable with alias
9206 attribute. If one exists, record a new symbol for it.
9207
9208 Returns true if an alias was recorded, false otherwise. */
9209
9210 static bool
9211 read_alias (struct die_info *die, struct dwarf2_cu *cu)
9212 {
9213 struct attribute *attr;
9214
9215 /* If the die does not have a name, this is neither a namespace
9216 alias nor a variable alias. */
9217 attr = dwarf2_attr (die, DW_AT_name, cu);
9218 if (attr != NULL)
9219 {
9220 int num;
9221 struct die_info *d = die;
9222 struct dwarf2_cu *imported_cu = cu;
9223
9224 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9225 keep inspecting DIEs until we hit the underlying import. */
9226 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9227 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
9228 {
9229 attr = dwarf2_attr (d, DW_AT_import, cu);
9230 if (attr == NULL)
9231 break;
9232
9233 d = follow_die_ref (d, attr, &imported_cu);
9234 if (d->tag != DW_TAG_imported_declaration)
9235 break;
9236 }
9237
9238 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
9239 {
9240 complaint (_("DIE at %s has too many recursively imported "
9241 "declarations"), sect_offset_str (d->sect_off));
9242 return false;
9243 }
9244
9245 if (attr != NULL)
9246 {
9247 struct type *type;
9248 if (d->tag == DW_TAG_variable)
9249 {
9250 /* This declaration is a C/C++ global variable alias.
9251 Add a symbol for it whose type is the same as the
9252 aliased variable's. */
9253 type = die_type (d, imported_cu);
9254 struct symbol *sym = new_symbol (die, type, cu);
9255 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
9256 sym->set_aclass_index (LOC_UNRESOLVED);
9257 if (attr != nullptr)
9258 var_decode_location (attr, sym, cu);
9259 return true;
9260 }
9261 else
9262 {
9263 sect_offset sect_off = attr->get_ref_die_offset ();
9264 type = get_die_type_at_offset (sect_off, cu->per_cu,
9265 cu->per_objfile);
9266 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
9267 {
9268 /* This declaration is a global namespace alias. Add
9269 a symbol for it whose type is the aliased
9270 namespace. */
9271 new_symbol (die, type, cu);
9272 return true;
9273 }
9274 }
9275 }
9276 }
9277 return false;
9278 }
9279
9280 /* Return the using directives repository (global or local?) to use in the
9281 current context for CU.
9282
9283 For Ada, imported declarations can materialize renamings, which *may* be
9284 global. However it is impossible (for now?) in DWARF to distinguish
9285 "external" imported declarations and "static" ones. As all imported
9286 declarations seem to be static in all other languages, make them all CU-wide
9287 global only in Ada. */
9288
9289 static struct using_direct **
9290 using_directives (struct dwarf2_cu *cu)
9291 {
9292 if (cu->lang () == language_ada
9293 && cu->get_builder ()->outermost_context_p ())
9294 return cu->get_builder ()->get_global_using_directives ();
9295 else
9296 return cu->get_builder ()->get_local_using_directives ();
9297 }
9298
9299 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
9300 given CU. If the format is not recognized or the attribute is
9301 not present, set it to 0. */
9302
9303 static unsigned int
9304 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
9305 {
9306 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
9307 if (decl_line == nullptr)
9308 return 0;
9309 if (decl_line->form_is_constant ())
9310 {
9311 LONGEST val = decl_line->constant_value (0);
9312 if (0 <= val && val <= UINT_MAX)
9313 return (unsigned int) val;
9314
9315 complaint (_("Declared line for using directive is too large"));
9316 return 0;
9317 }
9318
9319 complaint (_("Declared line for using directive is of incorrect format"));
9320 return 0;
9321 }
9322
9323 /* Read the import statement specified by the given die and record it. */
9324
9325 static void
9326 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9327 {
9328 struct objfile *objfile = cu->per_objfile->objfile;
9329 struct attribute *import_attr;
9330 struct die_info *imported_die, *child_die;
9331 struct dwarf2_cu *imported_cu;
9332 const char *imported_name;
9333 const char *imported_name_prefix;
9334 const char *canonical_name;
9335 const char *import_alias;
9336 const char *imported_declaration = NULL;
9337 const char *import_prefix;
9338 std::vector<const char *> excludes;
9339
9340 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9341 if (import_attr == NULL)
9342 {
9343 complaint (_("Tag '%s' has no DW_AT_import"),
9344 dwarf_tag_name (die->tag));
9345 return;
9346 }
9347
9348 imported_cu = cu;
9349 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9350 imported_name = dwarf2_name (imported_die, imported_cu);
9351 if (imported_name == NULL)
9352 {
9353 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9354
9355 The import in the following code:
9356 namespace A
9357 {
9358 typedef int B;
9359 }
9360
9361 int main ()
9362 {
9363 using A::B;
9364 B b;
9365 return b;
9366 }
9367
9368 ...
9369 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9370 <52> DW_AT_decl_file : 1
9371 <53> DW_AT_decl_line : 6
9372 <54> DW_AT_import : <0x75>
9373 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9374 <59> DW_AT_name : B
9375 <5b> DW_AT_decl_file : 1
9376 <5c> DW_AT_decl_line : 2
9377 <5d> DW_AT_type : <0x6e>
9378 ...
9379 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9380 <76> DW_AT_byte_size : 4
9381 <77> DW_AT_encoding : 5 (signed)
9382
9383 imports the wrong die ( 0x75 instead of 0x58 ).
9384 This case will be ignored until the gcc bug is fixed. */
9385 return;
9386 }
9387
9388 /* Figure out the local name after import. */
9389 import_alias = dwarf2_name (die, cu);
9390
9391 /* Figure out where the statement is being imported to. */
9392 import_prefix = determine_prefix (die, cu);
9393
9394 /* Figure out what the scope of the imported die is and prepend it
9395 to the name of the imported die. */
9396 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9397
9398 if (imported_die->tag != DW_TAG_namespace
9399 && imported_die->tag != DW_TAG_module)
9400 {
9401 imported_declaration = imported_name;
9402 canonical_name = imported_name_prefix;
9403 }
9404 else if (strlen (imported_name_prefix) > 0)
9405 canonical_name = obconcat (&objfile->objfile_obstack,
9406 imported_name_prefix,
9407 (cu->lang () == language_d
9408 ? "."
9409 : "::"),
9410 imported_name, (char *) NULL);
9411 else
9412 canonical_name = imported_name;
9413
9414 if (die->tag == DW_TAG_imported_module
9415 && cu->lang () == language_fortran)
9416 for (child_die = die->child; child_die && child_die->tag;
9417 child_die = child_die->sibling)
9418 {
9419 /* DWARF-4: A Fortran use statement with a “rename list” may be
9420 represented by an imported module entry with an import attribute
9421 referring to the module and owned entries corresponding to those
9422 entities that are renamed as part of being imported. */
9423
9424 if (child_die->tag != DW_TAG_imported_declaration)
9425 {
9426 complaint (_("child DW_TAG_imported_declaration expected "
9427 "- DIE at %s [in module %s]"),
9428 sect_offset_str (child_die->sect_off),
9429 objfile_name (objfile));
9430 continue;
9431 }
9432
9433 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9434 if (import_attr == NULL)
9435 {
9436 complaint (_("Tag '%s' has no DW_AT_import"),
9437 dwarf_tag_name (child_die->tag));
9438 continue;
9439 }
9440
9441 imported_cu = cu;
9442 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9443 &imported_cu);
9444 imported_name = dwarf2_name (imported_die, imported_cu);
9445 if (imported_name == NULL)
9446 {
9447 complaint (_("child DW_TAG_imported_declaration has unknown "
9448 "imported name - DIE at %s [in module %s]"),
9449 sect_offset_str (child_die->sect_off),
9450 objfile_name (objfile));
9451 continue;
9452 }
9453
9454 excludes.push_back (imported_name);
9455
9456 process_die (child_die, cu);
9457 }
9458
9459 add_using_directive (using_directives (cu),
9460 import_prefix,
9461 canonical_name,
9462 import_alias,
9463 imported_declaration,
9464 excludes,
9465 read_decl_line (die, cu),
9466 0,
9467 &objfile->objfile_obstack);
9468 }
9469
9470 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9471 types, but gives them a size of zero. Starting with version 14,
9472 ICC is compatible with GCC. */
9473
9474 static bool
9475 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
9476 {
9477 if (!cu->checked_producer)
9478 check_producer (cu);
9479
9480 return cu->producer_is_icc_lt_14;
9481 }
9482
9483 /* ICC generates a DW_AT_type for C void functions. This was observed on
9484 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9485 which says that void functions should not have a DW_AT_type. */
9486
9487 static bool
9488 producer_is_icc (struct dwarf2_cu *cu)
9489 {
9490 if (!cu->checked_producer)
9491 check_producer (cu);
9492
9493 return cu->producer_is_icc;
9494 }
9495
9496 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9497 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9498 this, it was first present in GCC release 4.3.0. */
9499
9500 static bool
9501 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9502 {
9503 if (!cu->checked_producer)
9504 check_producer (cu);
9505
9506 return cu->producer_is_gcc_lt_4_3;
9507 }
9508
9509 /* See dwarf2/read.h. */
9510 bool
9511 producer_is_clang (struct dwarf2_cu *cu)
9512 {
9513 if (!cu->checked_producer)
9514 check_producer (cu);
9515
9516 return cu->producer_is_clang;
9517 }
9518
9519 static file_and_directory &
9520 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9521 {
9522 if (cu->per_cu->fnd != nullptr)
9523 return *cu->per_cu->fnd;
9524
9525 /* Find the filename. Do not use dwarf2_name here, since the filename
9526 is not a source language identifier. */
9527 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
9528 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
9529
9530 if (res.get_comp_dir () == nullptr
9531 && producer_is_gcc_lt_4_3 (cu)
9532 && res.get_name () != nullptr
9533 && IS_ABSOLUTE_PATH (res.get_name ()))
9534 {
9535 res.set_comp_dir (ldirname (res.get_name ()));
9536 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
9537 }
9538
9539 cu->per_cu->fnd.reset (new file_and_directory (std::move (res)));
9540 return *cu->per_cu->fnd;
9541 }
9542
9543 /* Handle DW_AT_stmt_list for a compilation unit.
9544 DIE is the DW_TAG_compile_unit die for CU.
9545 COMP_DIR is the compilation directory. LOWPC is passed to
9546 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9547
9548 static void
9549 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9550 const file_and_directory &fnd, CORE_ADDR lowpc,
9551 bool have_code) /* ARI: editCase function */
9552 {
9553 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9554 struct attribute *attr;
9555 hashval_t line_header_local_hash;
9556 void **slot;
9557 int decode_mapping;
9558
9559 gdb_assert (! cu->per_cu->is_debug_types);
9560
9561 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9562 if (attr == NULL || !attr->form_is_unsigned ())
9563 return;
9564
9565 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9566
9567 /* The line header hash table is only created if needed (it exists to
9568 prevent redundant reading of the line table for partial_units).
9569 If we're given a partial_unit, we'll need it. If we're given a
9570 compile_unit, then use the line header hash table if it's already
9571 created, but don't create one just yet. */
9572
9573 if (per_objfile->line_header_hash == NULL
9574 && die->tag == DW_TAG_partial_unit)
9575 {
9576 per_objfile->line_header_hash
9577 .reset (htab_create_alloc (127, line_header_hash_voidp,
9578 line_header_eq_voidp,
9579 htab_delete_entry<line_header>,
9580 xcalloc, xfree));
9581 }
9582
9583 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
9584 line_header_local_hash = line_header_hash (&line_header_local);
9585 if (per_objfile->line_header_hash != NULL)
9586 {
9587 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9588 &line_header_local,
9589 line_header_local_hash, NO_INSERT);
9590
9591 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9592 is not present in *SLOT (since if there is something in *SLOT then
9593 it will be for a partial_unit). */
9594 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9595 {
9596 gdb_assert (*slot != NULL);
9597 cu->line_header = (struct line_header *) *slot;
9598 return;
9599 }
9600 }
9601
9602 /* dwarf_decode_line_header does not yet provide sufficient information.
9603 We always have to call also dwarf_decode_lines for it. */
9604 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
9605 fnd.get_comp_dir ());
9606 if (lh == NULL)
9607 return;
9608
9609 cu->line_header = lh.release ();
9610 cu->line_header_die_owner = die;
9611
9612 if (per_objfile->line_header_hash == NULL)
9613 slot = NULL;
9614 else
9615 {
9616 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
9617 &line_header_local,
9618 line_header_local_hash, INSERT);
9619 gdb_assert (slot != NULL);
9620 }
9621 if (slot != NULL && *slot == NULL)
9622 {
9623 /* This newly decoded line number information unit will be owned
9624 by line_header_hash hash table. */
9625 *slot = cu->line_header;
9626 cu->line_header_die_owner = NULL;
9627 }
9628 else
9629 {
9630 /* We cannot free any current entry in (*slot) as that struct line_header
9631 may be already used by multiple CUs. Create only temporary decoded
9632 line_header for this CU - it may happen at most once for each line
9633 number information unit. And if we're not using line_header_hash
9634 then this is what we want as well. */
9635 gdb_assert (die->tag != DW_TAG_partial_unit);
9636 }
9637 decode_mapping = (die->tag != DW_TAG_partial_unit);
9638 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
9639 then there won't be any interesting code in the CU, but a check later on
9640 (in lnp_state_machine::check_line_address) will fail to properly exclude
9641 an entry that was removed via --gc-sections. */
9642 if (have_code)
9643 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping);
9644 }
9645
9646 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9647
9648 static void
9649 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9650 {
9651 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9652 struct objfile *objfile = per_objfile->objfile;
9653 struct gdbarch *gdbarch = objfile->arch ();
9654 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9655 CORE_ADDR highpc = ((CORE_ADDR) 0);
9656 struct attribute *attr;
9657 struct die_info *child_die;
9658 CORE_ADDR baseaddr;
9659
9660 prepare_one_comp_unit (cu, die, cu->lang ());
9661 baseaddr = objfile->text_section_offset ();
9662
9663 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9664
9665 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9666 from finish_block. */
9667 if (lowpc == ((CORE_ADDR) -1))
9668 lowpc = highpc;
9669 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9670
9671 file_and_directory &fnd = find_file_and_directory (die, cu);
9672
9673 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
9674 lowpc);
9675
9676 gdb_assert (per_objfile->sym_cu == nullptr);
9677 scoped_restore restore_sym_cu
9678 = make_scoped_restore (&per_objfile->sym_cu, cu);
9679
9680 /* Decode line number information if present. We do this before
9681 processing child DIEs, so that the line header table is available
9682 for DW_AT_decl_file. */
9683 handle_DW_AT_stmt_list (die, cu, fnd, lowpc, lowpc != highpc);
9684
9685 /* Process all dies in compilation unit. */
9686 if (die->child != NULL)
9687 {
9688 child_die = die->child;
9689 while (child_die && child_die->tag)
9690 {
9691 process_die (child_die, cu);
9692 child_die = child_die->sibling;
9693 }
9694 }
9695 per_objfile->sym_cu = nullptr;
9696
9697 /* Decode macro information, if present. Dwarf 2 macro information
9698 refers to information in the line number info statement program
9699 header, so we can only read it if we've read the header
9700 successfully. */
9701 attr = dwarf2_attr (die, DW_AT_macros, cu);
9702 if (attr == NULL)
9703 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9704 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9705 {
9706 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9707 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9708
9709 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
9710 }
9711 else
9712 {
9713 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9714 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
9715 {
9716 unsigned int macro_offset = attr->as_unsigned ();
9717
9718 dwarf_decode_macros (cu, macro_offset, 0);
9719 }
9720 }
9721 }
9722
9723 void
9724 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
9725 {
9726 struct type_unit_group *tu_group;
9727 int first_time;
9728 struct attribute *attr;
9729 unsigned int i;
9730 struct signatured_type *sig_type;
9731
9732 gdb_assert (per_cu->is_debug_types);
9733 sig_type = (struct signatured_type *) per_cu;
9734
9735 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
9736
9737 /* If we're using .gdb_index (includes -readnow) then
9738 per_cu->type_unit_group may not have been set up yet. */
9739 if (sig_type->type_unit_group == NULL)
9740 sig_type->type_unit_group = get_type_unit_group (this, attr);
9741 tu_group = sig_type->type_unit_group;
9742
9743 /* If we've already processed this stmt_list there's no real need to
9744 do it again, we could fake it and just recreate the part we need
9745 (file name,index -> symtab mapping). If data shows this optimization
9746 is useful we can do it then. */
9747 type_unit_group_unshareable *tug_unshare
9748 = per_objfile->get_type_unit_group_unshareable (tu_group);
9749 first_time = tug_unshare->compunit_symtab == NULL;
9750
9751 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9752 debug info. */
9753 line_header_up lh;
9754 if (attr != NULL && attr->form_is_unsigned ())
9755 {
9756 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
9757 lh = dwarf_decode_line_header (line_offset, this, nullptr);
9758 }
9759 if (lh == NULL)
9760 {
9761 if (first_time)
9762 start_compunit_symtab ("", NULL, 0);
9763 else
9764 {
9765 gdb_assert (tug_unshare->symtabs == NULL);
9766 gdb_assert (m_builder == nullptr);
9767 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9768 m_builder.reset (new struct buildsym_compunit
9769 (cust->objfile (), "",
9770 cust->dirname (),
9771 cust->language (),
9772 0, cust));
9773 list_in_scope = get_builder ()->get_file_symbols ();
9774 }
9775 return;
9776 }
9777
9778 line_header = lh.release ();
9779 line_header_die_owner = die;
9780
9781 if (first_time)
9782 {
9783 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
9784
9785 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9786 still initializing it, and our caller (a few levels up)
9787 process_full_type_unit still needs to know if this is the first
9788 time. */
9789
9790 tug_unshare->symtabs
9791 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
9792 struct symtab *, line_header->file_names_size ());
9793
9794 auto &file_names = line_header->file_names ();
9795 for (i = 0; i < file_names.size (); ++i)
9796 {
9797 file_entry &fe = file_names[i];
9798 dwarf2_start_subfile (this, fe, *line_header);
9799 buildsym_compunit *b = get_builder ();
9800 subfile *sf = b->get_current_subfile ();
9801
9802 if (sf->symtab == nullptr)
9803 {
9804 /* NOTE: start_subfile will recognize when it's been
9805 passed a file it has already seen. So we can't
9806 assume there's a simple mapping from
9807 cu->line_header->file_names to subfiles, plus
9808 cu->line_header->file_names may contain dups. */
9809 const char *name = sf->name.c_str ();
9810 const char *name_for_id = sf->name_for_id.c_str ();
9811 sf->symtab = allocate_symtab (cust, name, name_for_id);
9812 }
9813
9814 fe.symtab = b->get_current_subfile ()->symtab;
9815 tug_unshare->symtabs[i] = fe.symtab;
9816 }
9817 }
9818 else
9819 {
9820 gdb_assert (m_builder == nullptr);
9821 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
9822 m_builder.reset (new struct buildsym_compunit
9823 (cust->objfile (), "",
9824 cust->dirname (),
9825 cust->language (),
9826 0, cust));
9827 list_in_scope = get_builder ()->get_file_symbols ();
9828
9829 auto &file_names = line_header->file_names ();
9830 for (i = 0; i < file_names.size (); ++i)
9831 {
9832 file_entry &fe = file_names[i];
9833 fe.symtab = tug_unshare->symtabs[i];
9834 }
9835 }
9836
9837 /* The main symtab is allocated last. Type units don't have DW_AT_name
9838 so they don't have a "real" (so to speak) symtab anyway.
9839 There is later code that will assign the main symtab to all symbols
9840 that don't have one. We need to handle the case of a symbol with a
9841 missing symtab (DW_AT_decl_file) anyway. */
9842 }
9843
9844 /* Process DW_TAG_type_unit.
9845 For TUs we want to skip the first top level sibling if it's not the
9846 actual type being defined by this TU. In this case the first top
9847 level sibling is there to provide context only. */
9848
9849 static void
9850 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9851 {
9852 struct die_info *child_die;
9853
9854 prepare_one_comp_unit (cu, die, language_minimal);
9855
9856 /* Initialize (or reinitialize) the machinery for building symtabs.
9857 We do this before processing child DIEs, so that the line header table
9858 is available for DW_AT_decl_file. */
9859 cu->setup_type_unit_groups (die);
9860
9861 if (die->child != NULL)
9862 {
9863 child_die = die->child;
9864 while (child_die && child_die->tag)
9865 {
9866 process_die (child_die, cu);
9867 child_die = child_die->sibling;
9868 }
9869 }
9870 }
9871 \f
9872 /* DWO/DWP files.
9873
9874 http://gcc.gnu.org/wiki/DebugFission
9875 http://gcc.gnu.org/wiki/DebugFissionDWP
9876
9877 To simplify handling of both DWO files ("object" files with the DWARF info)
9878 and DWP files (a file with the DWOs packaged up into one file), we treat
9879 DWP files as having a collection of virtual DWO files. */
9880
9881 static hashval_t
9882 hash_dwo_file (const void *item)
9883 {
9884 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9885 hashval_t hash;
9886
9887 hash = htab_hash_string (dwo_file->dwo_name);
9888 if (dwo_file->comp_dir != NULL)
9889 hash += htab_hash_string (dwo_file->comp_dir);
9890 return hash;
9891 }
9892
9893 static int
9894 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9895 {
9896 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9897 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9898
9899 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9900 return 0;
9901 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9902 return lhs->comp_dir == rhs->comp_dir;
9903 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9904 }
9905
9906 /* Allocate a hash table for DWO files. */
9907
9908 static htab_up
9909 allocate_dwo_file_hash_table ()
9910 {
9911 return htab_up (htab_create_alloc (41,
9912 hash_dwo_file,
9913 eq_dwo_file,
9914 htab_delete_entry<dwo_file>,
9915 xcalloc, xfree));
9916 }
9917
9918 /* Lookup DWO file DWO_NAME. */
9919
9920 static void **
9921 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
9922 const char *dwo_name,
9923 const char *comp_dir)
9924 {
9925 struct dwo_file find_entry;
9926 void **slot;
9927
9928 if (per_objfile->per_bfd->dwo_files == NULL)
9929 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
9930
9931 find_entry.dwo_name = dwo_name;
9932 find_entry.comp_dir = comp_dir;
9933 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
9934 INSERT);
9935
9936 return slot;
9937 }
9938
9939 static hashval_t
9940 hash_dwo_unit (const void *item)
9941 {
9942 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9943
9944 /* This drops the top 32 bits of the id, but is ok for a hash. */
9945 return dwo_unit->signature;
9946 }
9947
9948 static int
9949 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9950 {
9951 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9952 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9953
9954 /* The signature is assumed to be unique within the DWO file.
9955 So while object file CU dwo_id's always have the value zero,
9956 that's OK, assuming each object file DWO file has only one CU,
9957 and that's the rule for now. */
9958 return lhs->signature == rhs->signature;
9959 }
9960
9961 /* Allocate a hash table for DWO CUs,TUs.
9962 There is one of these tables for each of CUs,TUs for each DWO file. */
9963
9964 static htab_up
9965 allocate_dwo_unit_table ()
9966 {
9967 /* Start out with a pretty small number.
9968 Generally DWO files contain only one CU and maybe some TUs. */
9969 return htab_up (htab_create_alloc (3,
9970 hash_dwo_unit,
9971 eq_dwo_unit,
9972 NULL, xcalloc, xfree));
9973 }
9974
9975 /* die_reader_func for create_dwo_cu. */
9976
9977 static void
9978 create_dwo_cu_reader (const struct die_reader_specs *reader,
9979 const gdb_byte *info_ptr,
9980 struct die_info *comp_unit_die,
9981 struct dwo_file *dwo_file,
9982 struct dwo_unit *dwo_unit)
9983 {
9984 struct dwarf2_cu *cu = reader->cu;
9985 sect_offset sect_off = cu->per_cu->sect_off;
9986 struct dwarf2_section_info *section = cu->per_cu->section;
9987
9988 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
9989 if (!signature.has_value ())
9990 {
9991 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9992 " its dwo_id [in module %s]"),
9993 sect_offset_str (sect_off), dwo_file->dwo_name);
9994 return;
9995 }
9996
9997 dwo_unit->dwo_file = dwo_file;
9998 dwo_unit->signature = *signature;
9999 dwo_unit->section = section;
10000 dwo_unit->sect_off = sect_off;
10001 dwo_unit->length = cu->per_cu->length ();
10002
10003 dwarf_read_debug_printf (" offset %s, dwo_id %s",
10004 sect_offset_str (sect_off),
10005 hex_string (dwo_unit->signature));
10006 }
10007
10008 /* Create the dwo_units for the CUs in a DWO_FILE.
10009 Note: This function processes DWO files only, not DWP files. */
10010
10011 static void
10012 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
10013 dwarf2_cu *cu, struct dwo_file &dwo_file,
10014 dwarf2_section_info &section, htab_up &cus_htab)
10015 {
10016 struct objfile *objfile = per_objfile->objfile;
10017 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
10018 const gdb_byte *info_ptr, *end_ptr;
10019
10020 section.read (objfile);
10021 info_ptr = section.buffer;
10022
10023 if (info_ptr == NULL)
10024 return;
10025
10026 dwarf_read_debug_printf ("Reading %s for %s:",
10027 section.get_name (),
10028 section.get_file_name ());
10029
10030 end_ptr = info_ptr + section.size;
10031 while (info_ptr < end_ptr)
10032 {
10033 struct dwarf2_per_cu_data per_cu;
10034 struct dwo_unit read_unit {};
10035 struct dwo_unit *dwo_unit;
10036 void **slot;
10037 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
10038
10039 per_cu.per_bfd = per_bfd;
10040 per_cu.is_debug_types = 0;
10041 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
10042 per_cu.section = &section;
10043
10044 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
10045 if (!reader.dummy_p)
10046 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
10047 &dwo_file, &read_unit);
10048 info_ptr += per_cu.length ();
10049
10050 // If the unit could not be parsed, skip it.
10051 if (read_unit.dwo_file == NULL)
10052 continue;
10053
10054 if (cus_htab == NULL)
10055 cus_htab = allocate_dwo_unit_table ();
10056
10057 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
10058 struct dwo_unit);
10059 *dwo_unit = read_unit;
10060 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
10061 gdb_assert (slot != NULL);
10062 if (*slot != NULL)
10063 {
10064 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
10065 sect_offset dup_sect_off = dup_cu->sect_off;
10066
10067 complaint (_("debug cu entry at offset %s is duplicate to"
10068 " the entry at offset %s, signature %s"),
10069 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
10070 hex_string (dwo_unit->signature));
10071 }
10072 *slot = (void *)dwo_unit;
10073 }
10074 }
10075
10076 /* DWP file .debug_{cu,tu}_index section format:
10077 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10078 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10079
10080 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10081 officially standard DWP format was published with DWARF v5 and is called
10082 Version 5. There are no versions 3 or 4.
10083
10084 DWP Version 1:
10085
10086 Both index sections have the same format, and serve to map a 64-bit
10087 signature to a set of section numbers. Each section begins with a header,
10088 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10089 indexes, and a pool of 32-bit section numbers. The index sections will be
10090 aligned at 8-byte boundaries in the file.
10091
10092 The index section header consists of:
10093
10094 V, 32 bit version number
10095 -, 32 bits unused
10096 N, 32 bit number of compilation units or type units in the index
10097 M, 32 bit number of slots in the hash table
10098
10099 Numbers are recorded using the byte order of the application binary.
10100
10101 The hash table begins at offset 16 in the section, and consists of an array
10102 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10103 order of the application binary). Unused slots in the hash table are 0.
10104 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10105
10106 The parallel table begins immediately after the hash table
10107 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10108 array of 32-bit indexes (using the byte order of the application binary),
10109 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10110 table contains a 32-bit index into the pool of section numbers. For unused
10111 hash table slots, the corresponding entry in the parallel table will be 0.
10112
10113 The pool of section numbers begins immediately following the hash table
10114 (at offset 16 + 12 * M from the beginning of the section). The pool of
10115 section numbers consists of an array of 32-bit words (using the byte order
10116 of the application binary). Each item in the array is indexed starting
10117 from 0. The hash table entry provides the index of the first section
10118 number in the set. Additional section numbers in the set follow, and the
10119 set is terminated by a 0 entry (section number 0 is not used in ELF).
10120
10121 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10122 section must be the first entry in the set, and the .debug_abbrev.dwo must
10123 be the second entry. Other members of the set may follow in any order.
10124
10125 ---
10126
10127 DWP Versions 2 and 5:
10128
10129 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10130 and the entries in the index tables are now offsets into these sections.
10131 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10132 section.
10133
10134 Index Section Contents:
10135 Header
10136 Hash Table of Signatures dwp_hash_table.hash_table
10137 Parallel Table of Indices dwp_hash_table.unit_table
10138 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10139 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10140
10141 The index section header consists of:
10142
10143 V, 32 bit version number
10144 L, 32 bit number of columns in the table of section offsets
10145 N, 32 bit number of compilation units or type units in the index
10146 M, 32 bit number of slots in the hash table
10147
10148 Numbers are recorded using the byte order of the application binary.
10149
10150 The hash table has the same format as version 1.
10151 The parallel table of indices has the same format as version 1,
10152 except that the entries are origin-1 indices into the table of sections
10153 offsets and the table of section sizes.
10154
10155 The table of offsets begins immediately following the parallel table
10156 (at offset 16 + 12 * M from the beginning of the section). The table is
10157 a two-dimensional array of 32-bit words (using the byte order of the
10158 application binary), with L columns and N+1 rows, in row-major order.
10159 Each row in the array is indexed starting from 0. The first row provides
10160 a key to the remaining rows: each column in this row provides an identifier
10161 for a debug section, and the offsets in the same column of subsequent rows
10162 refer to that section. The section identifiers for Version 2 are:
10163
10164 DW_SECT_INFO 1 .debug_info.dwo
10165 DW_SECT_TYPES 2 .debug_types.dwo
10166 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10167 DW_SECT_LINE 4 .debug_line.dwo
10168 DW_SECT_LOC 5 .debug_loc.dwo
10169 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10170 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10171 DW_SECT_MACRO 8 .debug_macro.dwo
10172
10173 The section identifiers for Version 5 are:
10174
10175 DW_SECT_INFO_V5 1 .debug_info.dwo
10176 DW_SECT_RESERVED_V5 2 --
10177 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10178 DW_SECT_LINE_V5 4 .debug_line.dwo
10179 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10180 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10181 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10182 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10183
10184 The offsets provided by the CU and TU index sections are the base offsets
10185 for the contributions made by each CU or TU to the corresponding section
10186 in the package file. Each CU and TU header contains an abbrev_offset
10187 field, used to find the abbreviations table for that CU or TU within the
10188 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10189 be interpreted as relative to the base offset given in the index section.
10190 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10191 should be interpreted as relative to the base offset for .debug_line.dwo,
10192 and offsets into other debug sections obtained from DWARF attributes should
10193 also be interpreted as relative to the corresponding base offset.
10194
10195 The table of sizes begins immediately following the table of offsets.
10196 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10197 with L columns and N rows, in row-major order. Each row in the array is
10198 indexed starting from 1 (row 0 is shared by the two tables).
10199
10200 ---
10201
10202 Hash table lookup is handled the same in version 1 and 2:
10203
10204 We assume that N and M will not exceed 2^32 - 1.
10205 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10206
10207 Given a 64-bit compilation unit signature or a type signature S, an entry
10208 in the hash table is located as follows:
10209
10210 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10211 the low-order k bits all set to 1.
10212
10213 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10214
10215 3) If the hash table entry at index H matches the signature, use that
10216 entry. If the hash table entry at index H is unused (all zeroes),
10217 terminate the search: the signature is not present in the table.
10218
10219 4) Let H = (H + H') modulo M. Repeat at Step 3.
10220
10221 Because M > N and H' and M are relatively prime, the search is guaranteed
10222 to stop at an unused slot or find the match. */
10223
10224 /* Create a hash table to map DWO IDs to their CU/TU entry in
10225 .debug_{info,types}.dwo in DWP_FILE.
10226 Returns NULL if there isn't one.
10227 Note: This function processes DWP files only, not DWO files. */
10228
10229 static struct dwp_hash_table *
10230 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
10231 struct dwp_file *dwp_file, int is_debug_types)
10232 {
10233 struct objfile *objfile = per_objfile->objfile;
10234 bfd *dbfd = dwp_file->dbfd.get ();
10235 const gdb_byte *index_ptr, *index_end;
10236 struct dwarf2_section_info *index;
10237 uint32_t version, nr_columns, nr_units, nr_slots;
10238 struct dwp_hash_table *htab;
10239
10240 if (is_debug_types)
10241 index = &dwp_file->sections.tu_index;
10242 else
10243 index = &dwp_file->sections.cu_index;
10244
10245 if (index->empty ())
10246 return NULL;
10247 index->read (objfile);
10248
10249 index_ptr = index->buffer;
10250 index_end = index_ptr + index->size;
10251
10252 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10253 For now it's safe to just read 4 bytes (particularly as it's difficult to
10254 tell if you're dealing with Version 5 before you've read the version). */
10255 version = read_4_bytes (dbfd, index_ptr);
10256 index_ptr += 4;
10257 if (version == 2 || version == 5)
10258 nr_columns = read_4_bytes (dbfd, index_ptr);
10259 else
10260 nr_columns = 0;
10261 index_ptr += 4;
10262 nr_units = read_4_bytes (dbfd, index_ptr);
10263 index_ptr += 4;
10264 nr_slots = read_4_bytes (dbfd, index_ptr);
10265 index_ptr += 4;
10266
10267 if (version != 1 && version != 2 && version != 5)
10268 {
10269 error (_("Dwarf Error: unsupported DWP file version (%s)"
10270 " [in module %s]"),
10271 pulongest (version), dwp_file->name);
10272 }
10273 if (nr_slots != (nr_slots & -nr_slots))
10274 {
10275 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10276 " is not power of 2 [in module %s]"),
10277 pulongest (nr_slots), dwp_file->name);
10278 }
10279
10280 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
10281 htab->version = version;
10282 htab->nr_columns = nr_columns;
10283 htab->nr_units = nr_units;
10284 htab->nr_slots = nr_slots;
10285 htab->hash_table = index_ptr;
10286 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
10287
10288 /* Exit early if the table is empty. */
10289 if (nr_slots == 0 || nr_units == 0
10290 || (version == 2 && nr_columns == 0)
10291 || (version == 5 && nr_columns == 0))
10292 {
10293 /* All must be zero. */
10294 if (nr_slots != 0 || nr_units != 0
10295 || (version == 2 && nr_columns != 0)
10296 || (version == 5 && nr_columns != 0))
10297 {
10298 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10299 " all zero [in modules %s]"),
10300 dwp_file->name);
10301 }
10302 return htab;
10303 }
10304
10305 if (version == 1)
10306 {
10307 htab->section_pool.v1.indices =
10308 htab->unit_table + sizeof (uint32_t) * nr_slots;
10309 /* It's harder to decide whether the section is too small in v1.
10310 V1 is deprecated anyway so we punt. */
10311 }
10312 else if (version == 2)
10313 {
10314 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10315 int *ids = htab->section_pool.v2.section_ids;
10316 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
10317 /* Reverse map for error checking. */
10318 int ids_seen[DW_SECT_MAX + 1];
10319 int i;
10320
10321 if (nr_columns < 2)
10322 {
10323 error (_("Dwarf Error: bad DWP hash table, too few columns"
10324 " in section table [in module %s]"),
10325 dwp_file->name);
10326 }
10327 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10328 {
10329 error (_("Dwarf Error: bad DWP hash table, too many columns"
10330 " in section table [in module %s]"),
10331 dwp_file->name);
10332 }
10333 memset (ids, 255, sizeof_ids);
10334 memset (ids_seen, 255, sizeof (ids_seen));
10335 for (i = 0; i < nr_columns; ++i)
10336 {
10337 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10338
10339 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10340 {
10341 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10342 " in section table [in module %s]"),
10343 id, dwp_file->name);
10344 }
10345 if (ids_seen[id] != -1)
10346 {
10347 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10348 " id %d in section table [in module %s]"),
10349 id, dwp_file->name);
10350 }
10351 ids_seen[id] = i;
10352 ids[i] = id;
10353 }
10354 /* Must have exactly one info or types section. */
10355 if (((ids_seen[DW_SECT_INFO] != -1)
10356 + (ids_seen[DW_SECT_TYPES] != -1))
10357 != 1)
10358 {
10359 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10360 " DWO info/types section [in module %s]"),
10361 dwp_file->name);
10362 }
10363 /* Must have an abbrev section. */
10364 if (ids_seen[DW_SECT_ABBREV] == -1)
10365 {
10366 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10367 " section [in module %s]"),
10368 dwp_file->name);
10369 }
10370 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10371 htab->section_pool.v2.sizes =
10372 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10373 * nr_units * nr_columns);
10374 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10375 * nr_units * nr_columns))
10376 > index_end)
10377 {
10378 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10379 " [in module %s]"),
10380 dwp_file->name);
10381 }
10382 }
10383 else /* version == 5 */
10384 {
10385 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
10386 int *ids = htab->section_pool.v5.section_ids;
10387 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
10388 /* Reverse map for error checking. */
10389 int ids_seen[DW_SECT_MAX_V5 + 1];
10390
10391 if (nr_columns < 2)
10392 {
10393 error (_("Dwarf Error: bad DWP hash table, too few columns"
10394 " in section table [in module %s]"),
10395 dwp_file->name);
10396 }
10397 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
10398 {
10399 error (_("Dwarf Error: bad DWP hash table, too many columns"
10400 " in section table [in module %s]"),
10401 dwp_file->name);
10402 }
10403 memset (ids, 255, sizeof_ids);
10404 memset (ids_seen, 255, sizeof (ids_seen));
10405 for (int i = 0; i < nr_columns; ++i)
10406 {
10407 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10408
10409 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
10410 {
10411 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10412 " in section table [in module %s]"),
10413 id, dwp_file->name);
10414 }
10415 if (ids_seen[id] != -1)
10416 {
10417 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10418 " id %d in section table [in module %s]"),
10419 id, dwp_file->name);
10420 }
10421 ids_seen[id] = i;
10422 ids[i] = id;
10423 }
10424 /* Must have seen an info section. */
10425 if (ids_seen[DW_SECT_INFO_V5] == -1)
10426 {
10427 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10428 " DWO info/types section [in module %s]"),
10429 dwp_file->name);
10430 }
10431 /* Must have an abbrev section. */
10432 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
10433 {
10434 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10435 " section [in module %s]"),
10436 dwp_file->name);
10437 }
10438 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10439 htab->section_pool.v5.sizes
10440 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
10441 * nr_units * nr_columns);
10442 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
10443 * nr_units * nr_columns))
10444 > index_end)
10445 {
10446 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10447 " [in module %s]"),
10448 dwp_file->name);
10449 }
10450 }
10451
10452 return htab;
10453 }
10454
10455 /* Update SECTIONS with the data from SECTP.
10456
10457 This function is like the other "locate" section routines, but in
10458 this context the sections to read comes from the DWP V1 hash table,
10459 not the full ELF section table.
10460
10461 The result is non-zero for success, or zero if an error was found. */
10462
10463 static int
10464 locate_v1_virtual_dwo_sections (asection *sectp,
10465 struct virtual_v1_dwo_sections *sections)
10466 {
10467 const struct dwop_section_names *names = &dwop_section_names;
10468
10469 if (names->abbrev_dwo.matches (sectp->name))
10470 {
10471 /* There can be only one. */
10472 if (sections->abbrev.s.section != NULL)
10473 return 0;
10474 sections->abbrev.s.section = sectp;
10475 sections->abbrev.size = bfd_section_size (sectp);
10476 }
10477 else if (names->info_dwo.matches (sectp->name)
10478 || names->types_dwo.matches (sectp->name))
10479 {
10480 /* There can be only one. */
10481 if (sections->info_or_types.s.section != NULL)
10482 return 0;
10483 sections->info_or_types.s.section = sectp;
10484 sections->info_or_types.size = bfd_section_size (sectp);
10485 }
10486 else if (names->line_dwo.matches (sectp->name))
10487 {
10488 /* There can be only one. */
10489 if (sections->line.s.section != NULL)
10490 return 0;
10491 sections->line.s.section = sectp;
10492 sections->line.size = bfd_section_size (sectp);
10493 }
10494 else if (names->loc_dwo.matches (sectp->name))
10495 {
10496 /* There can be only one. */
10497 if (sections->loc.s.section != NULL)
10498 return 0;
10499 sections->loc.s.section = sectp;
10500 sections->loc.size = bfd_section_size (sectp);
10501 }
10502 else if (names->macinfo_dwo.matches (sectp->name))
10503 {
10504 /* There can be only one. */
10505 if (sections->macinfo.s.section != NULL)
10506 return 0;
10507 sections->macinfo.s.section = sectp;
10508 sections->macinfo.size = bfd_section_size (sectp);
10509 }
10510 else if (names->macro_dwo.matches (sectp->name))
10511 {
10512 /* There can be only one. */
10513 if (sections->macro.s.section != NULL)
10514 return 0;
10515 sections->macro.s.section = sectp;
10516 sections->macro.size = bfd_section_size (sectp);
10517 }
10518 else if (names->str_offsets_dwo.matches (sectp->name))
10519 {
10520 /* There can be only one. */
10521 if (sections->str_offsets.s.section != NULL)
10522 return 0;
10523 sections->str_offsets.s.section = sectp;
10524 sections->str_offsets.size = bfd_section_size (sectp);
10525 }
10526 else
10527 {
10528 /* No other kind of section is valid. */
10529 return 0;
10530 }
10531
10532 return 1;
10533 }
10534
10535 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10536 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10537 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10538 This is for DWP version 1 files. */
10539
10540 static struct dwo_unit *
10541 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
10542 struct dwp_file *dwp_file,
10543 uint32_t unit_index,
10544 const char *comp_dir,
10545 ULONGEST signature, int is_debug_types)
10546 {
10547 const struct dwp_hash_table *dwp_htab =
10548 is_debug_types ? dwp_file->tus : dwp_file->cus;
10549 bfd *dbfd = dwp_file->dbfd.get ();
10550 const char *kind = is_debug_types ? "TU" : "CU";
10551 struct dwo_file *dwo_file;
10552 struct dwo_unit *dwo_unit;
10553 struct virtual_v1_dwo_sections sections;
10554 void **dwo_file_slot;
10555 int i;
10556
10557 gdb_assert (dwp_file->version == 1);
10558
10559 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10560 kind, pulongest (unit_index), hex_string (signature),
10561 dwp_file->name);
10562
10563 /* Fetch the sections of this DWO unit.
10564 Put a limit on the number of sections we look for so that bad data
10565 doesn't cause us to loop forever. */
10566
10567 #define MAX_NR_V1_DWO_SECTIONS \
10568 (1 /* .debug_info or .debug_types */ \
10569 + 1 /* .debug_abbrev */ \
10570 + 1 /* .debug_line */ \
10571 + 1 /* .debug_loc */ \
10572 + 1 /* .debug_str_offsets */ \
10573 + 1 /* .debug_macro or .debug_macinfo */ \
10574 + 1 /* trailing zero */)
10575
10576 memset (&sections, 0, sizeof (sections));
10577
10578 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10579 {
10580 asection *sectp;
10581 uint32_t section_nr =
10582 read_4_bytes (dbfd,
10583 dwp_htab->section_pool.v1.indices
10584 + (unit_index + i) * sizeof (uint32_t));
10585
10586 if (section_nr == 0)
10587 break;
10588 if (section_nr >= dwp_file->num_sections)
10589 {
10590 error (_("Dwarf Error: bad DWP hash table, section number too large"
10591 " [in module %s]"),
10592 dwp_file->name);
10593 }
10594
10595 sectp = dwp_file->elf_sections[section_nr];
10596 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10597 {
10598 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10599 " [in module %s]"),
10600 dwp_file->name);
10601 }
10602 }
10603
10604 if (i < 2
10605 || sections.info_or_types.empty ()
10606 || sections.abbrev.empty ())
10607 {
10608 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10609 " [in module %s]"),
10610 dwp_file->name);
10611 }
10612 if (i == MAX_NR_V1_DWO_SECTIONS)
10613 {
10614 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10615 " [in module %s]"),
10616 dwp_file->name);
10617 }
10618
10619 /* It's easier for the rest of the code if we fake a struct dwo_file and
10620 have dwo_unit "live" in that. At least for now.
10621
10622 The DWP file can be made up of a random collection of CUs and TUs.
10623 However, for each CU + set of TUs that came from the same original DWO
10624 file, we can combine them back into a virtual DWO file to save space
10625 (fewer struct dwo_file objects to allocate). Remember that for really
10626 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10627
10628 std::string virtual_dwo_name =
10629 string_printf ("virtual-dwo/%d-%d-%d-%d",
10630 sections.abbrev.get_id (),
10631 sections.line.get_id (),
10632 sections.loc.get_id (),
10633 sections.str_offsets.get_id ());
10634 /* Can we use an existing virtual DWO file? */
10635 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10636 comp_dir);
10637 /* Create one if necessary. */
10638 if (*dwo_file_slot == NULL)
10639 {
10640 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10641 virtual_dwo_name.c_str ());
10642
10643 dwo_file = new struct dwo_file;
10644 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10645 dwo_file->comp_dir = comp_dir;
10646 dwo_file->sections.abbrev = sections.abbrev;
10647 dwo_file->sections.line = sections.line;
10648 dwo_file->sections.loc = sections.loc;
10649 dwo_file->sections.macinfo = sections.macinfo;
10650 dwo_file->sections.macro = sections.macro;
10651 dwo_file->sections.str_offsets = sections.str_offsets;
10652 /* The "str" section is global to the entire DWP file. */
10653 dwo_file->sections.str = dwp_file->sections.str;
10654 /* The info or types section is assigned below to dwo_unit,
10655 there's no need to record it in dwo_file.
10656 Also, we can't simply record type sections in dwo_file because
10657 we record a pointer into the vector in dwo_unit. As we collect more
10658 types we'll grow the vector and eventually have to reallocate space
10659 for it, invalidating all copies of pointers into the previous
10660 contents. */
10661 *dwo_file_slot = dwo_file;
10662 }
10663 else
10664 {
10665 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10666 virtual_dwo_name.c_str ());
10667
10668 dwo_file = (struct dwo_file *) *dwo_file_slot;
10669 }
10670
10671 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10672 dwo_unit->dwo_file = dwo_file;
10673 dwo_unit->signature = signature;
10674 dwo_unit->section =
10675 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10676 *dwo_unit->section = sections.info_or_types;
10677 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10678
10679 return dwo_unit;
10680 }
10681
10682 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10683 simplify them. Given a pointer to the containing section SECTION, and
10684 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10685 virtual section of just that piece. */
10686
10687 static struct dwarf2_section_info
10688 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
10689 struct dwarf2_section_info *section,
10690 bfd_size_type offset, bfd_size_type size)
10691 {
10692 struct dwarf2_section_info result;
10693 asection *sectp;
10694
10695 gdb_assert (section != NULL);
10696 gdb_assert (!section->is_virtual);
10697
10698 memset (&result, 0, sizeof (result));
10699 result.s.containing_section = section;
10700 result.is_virtual = true;
10701
10702 if (size == 0)
10703 return result;
10704
10705 sectp = section->get_bfd_section ();
10706
10707 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10708 bounds of the real section. This is a pretty-rare event, so just
10709 flag an error (easier) instead of a warning and trying to cope. */
10710 if (sectp == NULL
10711 || offset + size > bfd_section_size (sectp))
10712 {
10713 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10714 " in section %s [in module %s]"),
10715 sectp ? bfd_section_name (sectp) : "<unknown>",
10716 objfile_name (per_objfile->objfile));
10717 }
10718
10719 result.virtual_offset = offset;
10720 result.size = size;
10721 return result;
10722 }
10723
10724 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10725 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10726 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10727 This is for DWP version 2 files. */
10728
10729 static struct dwo_unit *
10730 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
10731 struct dwp_file *dwp_file,
10732 uint32_t unit_index,
10733 const char *comp_dir,
10734 ULONGEST signature, int is_debug_types)
10735 {
10736 const struct dwp_hash_table *dwp_htab =
10737 is_debug_types ? dwp_file->tus : dwp_file->cus;
10738 bfd *dbfd = dwp_file->dbfd.get ();
10739 const char *kind = is_debug_types ? "TU" : "CU";
10740 struct dwo_file *dwo_file;
10741 struct dwo_unit *dwo_unit;
10742 struct virtual_v2_or_v5_dwo_sections sections;
10743 void **dwo_file_slot;
10744 int i;
10745
10746 gdb_assert (dwp_file->version == 2);
10747
10748 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10749 kind, pulongest (unit_index), hex_string (signature),
10750 dwp_file->name);
10751
10752 /* Fetch the section offsets of this DWO unit. */
10753
10754 memset (&sections, 0, sizeof (sections));
10755
10756 for (i = 0; i < dwp_htab->nr_columns; ++i)
10757 {
10758 uint32_t offset = read_4_bytes (dbfd,
10759 dwp_htab->section_pool.v2.offsets
10760 + (((unit_index - 1) * dwp_htab->nr_columns
10761 + i)
10762 * sizeof (uint32_t)));
10763 uint32_t size = read_4_bytes (dbfd,
10764 dwp_htab->section_pool.v2.sizes
10765 + (((unit_index - 1) * dwp_htab->nr_columns
10766 + i)
10767 * sizeof (uint32_t)));
10768
10769 switch (dwp_htab->section_pool.v2.section_ids[i])
10770 {
10771 case DW_SECT_INFO:
10772 case DW_SECT_TYPES:
10773 sections.info_or_types_offset = offset;
10774 sections.info_or_types_size = size;
10775 break;
10776 case DW_SECT_ABBREV:
10777 sections.abbrev_offset = offset;
10778 sections.abbrev_size = size;
10779 break;
10780 case DW_SECT_LINE:
10781 sections.line_offset = offset;
10782 sections.line_size = size;
10783 break;
10784 case DW_SECT_LOC:
10785 sections.loc_offset = offset;
10786 sections.loc_size = size;
10787 break;
10788 case DW_SECT_STR_OFFSETS:
10789 sections.str_offsets_offset = offset;
10790 sections.str_offsets_size = size;
10791 break;
10792 case DW_SECT_MACINFO:
10793 sections.macinfo_offset = offset;
10794 sections.macinfo_size = size;
10795 break;
10796 case DW_SECT_MACRO:
10797 sections.macro_offset = offset;
10798 sections.macro_size = size;
10799 break;
10800 }
10801 }
10802
10803 /* It's easier for the rest of the code if we fake a struct dwo_file and
10804 have dwo_unit "live" in that. At least for now.
10805
10806 The DWP file can be made up of a random collection of CUs and TUs.
10807 However, for each CU + set of TUs that came from the same original DWO
10808 file, we can combine them back into a virtual DWO file to save space
10809 (fewer struct dwo_file objects to allocate). Remember that for really
10810 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10811
10812 std::string virtual_dwo_name =
10813 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10814 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10815 (long) (sections.line_size ? sections.line_offset : 0),
10816 (long) (sections.loc_size ? sections.loc_offset : 0),
10817 (long) (sections.str_offsets_size
10818 ? sections.str_offsets_offset : 0));
10819 /* Can we use an existing virtual DWO file? */
10820 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
10821 comp_dir);
10822 /* Create one if necessary. */
10823 if (*dwo_file_slot == NULL)
10824 {
10825 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10826 virtual_dwo_name.c_str ());
10827
10828 dwo_file = new struct dwo_file;
10829 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
10830 dwo_file->comp_dir = comp_dir;
10831 dwo_file->sections.abbrev =
10832 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
10833 sections.abbrev_offset,
10834 sections.abbrev_size);
10835 dwo_file->sections.line =
10836 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
10837 sections.line_offset,
10838 sections.line_size);
10839 dwo_file->sections.loc =
10840 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
10841 sections.loc_offset, sections.loc_size);
10842 dwo_file->sections.macinfo =
10843 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
10844 sections.macinfo_offset,
10845 sections.macinfo_size);
10846 dwo_file->sections.macro =
10847 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
10848 sections.macro_offset,
10849 sections.macro_size);
10850 dwo_file->sections.str_offsets =
10851 create_dwp_v2_or_v5_section (per_objfile,
10852 &dwp_file->sections.str_offsets,
10853 sections.str_offsets_offset,
10854 sections.str_offsets_size);
10855 /* The "str" section is global to the entire DWP file. */
10856 dwo_file->sections.str = dwp_file->sections.str;
10857 /* The info or types section is assigned below to dwo_unit,
10858 there's no need to record it in dwo_file.
10859 Also, we can't simply record type sections in dwo_file because
10860 we record a pointer into the vector in dwo_unit. As we collect more
10861 types we'll grow the vector and eventually have to reallocate space
10862 for it, invalidating all copies of pointers into the previous
10863 contents. */
10864 *dwo_file_slot = dwo_file;
10865 }
10866 else
10867 {
10868 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10869 virtual_dwo_name.c_str ());
10870
10871 dwo_file = (struct dwo_file *) *dwo_file_slot;
10872 }
10873
10874 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
10875 dwo_unit->dwo_file = dwo_file;
10876 dwo_unit->signature = signature;
10877 dwo_unit->section =
10878 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
10879 *dwo_unit->section = create_dwp_v2_or_v5_section
10880 (per_objfile,
10881 is_debug_types
10882 ? &dwp_file->sections.types
10883 : &dwp_file->sections.info,
10884 sections.info_or_types_offset,
10885 sections.info_or_types_size);
10886 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10887
10888 return dwo_unit;
10889 }
10890
10891 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10892 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10893 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10894 This is for DWP version 5 files. */
10895
10896 static struct dwo_unit *
10897 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
10898 struct dwp_file *dwp_file,
10899 uint32_t unit_index,
10900 const char *comp_dir,
10901 ULONGEST signature, int is_debug_types)
10902 {
10903 const struct dwp_hash_table *dwp_htab
10904 = is_debug_types ? dwp_file->tus : dwp_file->cus;
10905 bfd *dbfd = dwp_file->dbfd.get ();
10906 const char *kind = is_debug_types ? "TU" : "CU";
10907 struct dwo_file *dwo_file;
10908 struct dwo_unit *dwo_unit;
10909 struct virtual_v2_or_v5_dwo_sections sections {};
10910 void **dwo_file_slot;
10911
10912 gdb_assert (dwp_file->version == 5);
10913
10914 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10915 kind, pulongest (unit_index), hex_string (signature),
10916 dwp_file->name);
10917
10918 /* Fetch the section offsets of this DWO unit. */
10919
10920 /* memset (&sections, 0, sizeof (sections)); */
10921
10922 for (int i = 0; i < dwp_htab->nr_columns; ++i)
10923 {
10924 uint32_t offset = read_4_bytes (dbfd,
10925 dwp_htab->section_pool.v5.offsets
10926 + (((unit_index - 1)
10927 * dwp_htab->nr_columns
10928 + i)
10929 * sizeof (uint32_t)));
10930 uint32_t size = read_4_bytes (dbfd,
10931 dwp_htab->section_pool.v5.sizes
10932 + (((unit_index - 1) * dwp_htab->nr_columns
10933 + i)
10934 * sizeof (uint32_t)));
10935
10936 switch (dwp_htab->section_pool.v5.section_ids[i])
10937 {
10938 case DW_SECT_ABBREV_V5:
10939 sections.abbrev_offset = offset;
10940 sections.abbrev_size = size;
10941 break;
10942 case DW_SECT_INFO_V5:
10943 sections.info_or_types_offset = offset;
10944 sections.info_or_types_size = size;
10945 break;
10946 case DW_SECT_LINE_V5:
10947 sections.line_offset = offset;
10948 sections.line_size = size;
10949 break;
10950 case DW_SECT_LOCLISTS_V5:
10951 sections.loclists_offset = offset;
10952 sections.loclists_size = size;
10953 break;
10954 case DW_SECT_MACRO_V5:
10955 sections.macro_offset = offset;
10956 sections.macro_size = size;
10957 break;
10958 case DW_SECT_RNGLISTS_V5:
10959 sections.rnglists_offset = offset;
10960 sections.rnglists_size = size;
10961 break;
10962 case DW_SECT_STR_OFFSETS_V5:
10963 sections.str_offsets_offset = offset;
10964 sections.str_offsets_size = size;
10965 break;
10966 case DW_SECT_RESERVED_V5:
10967 default:
10968 break;
10969 }
10970 }
10971
10972 /* It's easier for the rest of the code if we fake a struct dwo_file and
10973 have dwo_unit "live" in that. At least for now.
10974
10975 The DWP file can be made up of a random collection of CUs and TUs.
10976 However, for each CU + set of TUs that came from the same original DWO
10977 file, we can combine them back into a virtual DWO file to save space
10978 (fewer struct dwo_file objects to allocate). Remember that for really
10979 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10980
10981 std::string virtual_dwo_name =
10982 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10983 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10984 (long) (sections.line_size ? sections.line_offset : 0),
10985 (long) (sections.loclists_size ? sections.loclists_offset : 0),
10986 (long) (sections.str_offsets_size
10987 ? sections.str_offsets_offset : 0),
10988 (long) (sections.macro_size ? sections.macro_offset : 0),
10989 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
10990 /* Can we use an existing virtual DWO file? */
10991 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
10992 virtual_dwo_name.c_str (),
10993 comp_dir);
10994 /* Create one if necessary. */
10995 if (*dwo_file_slot == NULL)
10996 {
10997 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10998 virtual_dwo_name.c_str ());
10999
11000 dwo_file = new struct dwo_file;
11001 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11002 dwo_file->comp_dir = comp_dir;
11003 dwo_file->sections.abbrev =
11004 create_dwp_v2_or_v5_section (per_objfile,
11005 &dwp_file->sections.abbrev,
11006 sections.abbrev_offset,
11007 sections.abbrev_size);
11008 dwo_file->sections.line =
11009 create_dwp_v2_or_v5_section (per_objfile,
11010 &dwp_file->sections.line,
11011 sections.line_offset, sections.line_size);
11012 dwo_file->sections.macro =
11013 create_dwp_v2_or_v5_section (per_objfile,
11014 &dwp_file->sections.macro,
11015 sections.macro_offset,
11016 sections.macro_size);
11017 dwo_file->sections.loclists =
11018 create_dwp_v2_or_v5_section (per_objfile,
11019 &dwp_file->sections.loclists,
11020 sections.loclists_offset,
11021 sections.loclists_size);
11022 dwo_file->sections.rnglists =
11023 create_dwp_v2_or_v5_section (per_objfile,
11024 &dwp_file->sections.rnglists,
11025 sections.rnglists_offset,
11026 sections.rnglists_size);
11027 dwo_file->sections.str_offsets =
11028 create_dwp_v2_or_v5_section (per_objfile,
11029 &dwp_file->sections.str_offsets,
11030 sections.str_offsets_offset,
11031 sections.str_offsets_size);
11032 /* The "str" section is global to the entire DWP file. */
11033 dwo_file->sections.str = dwp_file->sections.str;
11034 /* The info or types section is assigned below to dwo_unit,
11035 there's no need to record it in dwo_file.
11036 Also, we can't simply record type sections in dwo_file because
11037 we record a pointer into the vector in dwo_unit. As we collect more
11038 types we'll grow the vector and eventually have to reallocate space
11039 for it, invalidating all copies of pointers into the previous
11040 contents. */
11041 *dwo_file_slot = dwo_file;
11042 }
11043 else
11044 {
11045 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11046 virtual_dwo_name.c_str ());
11047
11048 dwo_file = (struct dwo_file *) *dwo_file_slot;
11049 }
11050
11051 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11052 dwo_unit->dwo_file = dwo_file;
11053 dwo_unit->signature = signature;
11054 dwo_unit->section
11055 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11056 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
11057 &dwp_file->sections.info,
11058 sections.info_or_types_offset,
11059 sections.info_or_types_size);
11060 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11061
11062 return dwo_unit;
11063 }
11064
11065 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11066 Returns NULL if the signature isn't found. */
11067
11068 static struct dwo_unit *
11069 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
11070 struct dwp_file *dwp_file, const char *comp_dir,
11071 ULONGEST signature, int is_debug_types)
11072 {
11073 const struct dwp_hash_table *dwp_htab =
11074 is_debug_types ? dwp_file->tus : dwp_file->cus;
11075 bfd *dbfd = dwp_file->dbfd.get ();
11076 uint32_t mask = dwp_htab->nr_slots - 1;
11077 uint32_t hash = signature & mask;
11078 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11079 unsigned int i;
11080 void **slot;
11081 struct dwo_unit find_dwo_cu;
11082
11083 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11084 find_dwo_cu.signature = signature;
11085 slot = htab_find_slot (is_debug_types
11086 ? dwp_file->loaded_tus.get ()
11087 : dwp_file->loaded_cus.get (),
11088 &find_dwo_cu, INSERT);
11089
11090 if (*slot != NULL)
11091 return (struct dwo_unit *) *slot;
11092
11093 /* Use a for loop so that we don't loop forever on bad debug info. */
11094 for (i = 0; i < dwp_htab->nr_slots; ++i)
11095 {
11096 ULONGEST signature_in_table;
11097
11098 signature_in_table =
11099 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11100 if (signature_in_table == signature)
11101 {
11102 uint32_t unit_index =
11103 read_4_bytes (dbfd,
11104 dwp_htab->unit_table + hash * sizeof (uint32_t));
11105
11106 if (dwp_file->version == 1)
11107 {
11108 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
11109 unit_index, comp_dir,
11110 signature, is_debug_types);
11111 }
11112 else if (dwp_file->version == 2)
11113 {
11114 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
11115 unit_index, comp_dir,
11116 signature, is_debug_types);
11117 }
11118 else /* version == 5 */
11119 {
11120 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
11121 unit_index, comp_dir,
11122 signature, is_debug_types);
11123 }
11124 return (struct dwo_unit *) *slot;
11125 }
11126 if (signature_in_table == 0)
11127 return NULL;
11128 hash = (hash + hash2) & mask;
11129 }
11130
11131 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11132 " [in module %s]"),
11133 dwp_file->name);
11134 }
11135
11136 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11137 Open the file specified by FILE_NAME and hand it off to BFD for
11138 preliminary analysis. Return a newly initialized bfd *, which
11139 includes a canonicalized copy of FILE_NAME.
11140 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11141 SEARCH_CWD is true if the current directory is to be searched.
11142 It will be searched before debug-file-directory.
11143 If successful, the file is added to the bfd include table of the
11144 objfile's bfd (see gdb_bfd_record_inclusion).
11145 If unable to find/open the file, return NULL.
11146 NOTE: This function is derived from symfile_bfd_open. */
11147
11148 static gdb_bfd_ref_ptr
11149 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
11150 const char *file_name, int is_dwp, int search_cwd)
11151 {
11152 int desc;
11153 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11154 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11155 to debug_file_directory. */
11156 const char *search_path;
11157 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11158
11159 gdb::unique_xmalloc_ptr<char> search_path_holder;
11160 if (search_cwd)
11161 {
11162 if (!debug_file_directory.empty ())
11163 {
11164 search_path_holder.reset (concat (".", dirname_separator_string,
11165 debug_file_directory.c_str (),
11166 (char *) NULL));
11167 search_path = search_path_holder.get ();
11168 }
11169 else
11170 search_path = ".";
11171 }
11172 else
11173 search_path = debug_file_directory.c_str ();
11174
11175 /* Add the path for the executable binary to the list of search paths. */
11176 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
11177 search_path_holder.reset (concat (objfile_dir.c_str (),
11178 dirname_separator_string,
11179 search_path, nullptr));
11180 search_path = search_path_holder.get ();
11181
11182 openp_flags flags = OPF_RETURN_REALPATH;
11183 if (is_dwp)
11184 flags |= OPF_SEARCH_IN_PATH;
11185
11186 gdb::unique_xmalloc_ptr<char> absolute_name;
11187 desc = openp (search_path, flags, file_name,
11188 O_RDONLY | O_BINARY, &absolute_name);
11189 if (desc < 0)
11190 return NULL;
11191
11192 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11193 gnutarget, desc));
11194 if (sym_bfd == NULL)
11195 return NULL;
11196 bfd_set_cacheable (sym_bfd.get (), 1);
11197
11198 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11199 return NULL;
11200
11201 /* Success. Record the bfd as having been included by the objfile's bfd.
11202 This is important because things like demangled_names_hash lives in the
11203 objfile's per_bfd space and may have references to things like symbol
11204 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11205 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
11206
11207 return sym_bfd;
11208 }
11209
11210 /* Try to open DWO file FILE_NAME.
11211 COMP_DIR is the DW_AT_comp_dir attribute.
11212 The result is the bfd handle of the file.
11213 If there is a problem finding or opening the file, return NULL.
11214 Upon success, the canonicalized path of the file is stored in the bfd,
11215 same as symfile_bfd_open. */
11216
11217 static gdb_bfd_ref_ptr
11218 open_dwo_file (dwarf2_per_objfile *per_objfile,
11219 const char *file_name, const char *comp_dir)
11220 {
11221 if (IS_ABSOLUTE_PATH (file_name))
11222 return try_open_dwop_file (per_objfile, file_name,
11223 0 /*is_dwp*/, 0 /*search_cwd*/);
11224
11225 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11226
11227 if (comp_dir != NULL)
11228 {
11229 std::string path_to_try = path_join (comp_dir, file_name);
11230
11231 /* NOTE: If comp_dir is a relative path, this will also try the
11232 search path, which seems useful. */
11233 gdb_bfd_ref_ptr abfd (try_open_dwop_file
11234 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11235
11236 if (abfd != NULL)
11237 return abfd;
11238 }
11239
11240 /* That didn't work, try debug-file-directory, which, despite its name,
11241 is a list of paths. */
11242
11243 if (debug_file_directory.empty ())
11244 return NULL;
11245
11246 return try_open_dwop_file (per_objfile, file_name,
11247 0 /*is_dwp*/, 1 /*search_cwd*/);
11248 }
11249
11250 /* This function is mapped across the sections and remembers the offset and
11251 size of each of the DWO debugging sections we are interested in. */
11252
11253 static void
11254 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
11255 asection *sectp, dwo_sections *dwo_sections)
11256 {
11257 const struct dwop_section_names *names = &dwop_section_names;
11258
11259 struct dwarf2_section_info *dw_sect = nullptr;
11260
11261 if (names->abbrev_dwo.matches (sectp->name))
11262 dw_sect = &dwo_sections->abbrev;
11263 else if (names->info_dwo.matches (sectp->name))
11264 dw_sect = &dwo_sections->info;
11265 else if (names->line_dwo.matches (sectp->name))
11266 dw_sect = &dwo_sections->line;
11267 else if (names->loc_dwo.matches (sectp->name))
11268 dw_sect = &dwo_sections->loc;
11269 else if (names->loclists_dwo.matches (sectp->name))
11270 dw_sect = &dwo_sections->loclists;
11271 else if (names->macinfo_dwo.matches (sectp->name))
11272 dw_sect = &dwo_sections->macinfo;
11273 else if (names->macro_dwo.matches (sectp->name))
11274 dw_sect = &dwo_sections->macro;
11275 else if (names->rnglists_dwo.matches (sectp->name))
11276 dw_sect = &dwo_sections->rnglists;
11277 else if (names->str_dwo.matches (sectp->name))
11278 dw_sect = &dwo_sections->str;
11279 else if (names->str_offsets_dwo.matches (sectp->name))
11280 dw_sect = &dwo_sections->str_offsets;
11281 else if (names->types_dwo.matches (sectp->name))
11282 {
11283 struct dwarf2_section_info type_section;
11284
11285 memset (&type_section, 0, sizeof (type_section));
11286 dwo_sections->types.push_back (type_section);
11287 dw_sect = &dwo_sections->types.back ();
11288 }
11289
11290 if (dw_sect != nullptr)
11291 {
11292 dw_sect->s.section = sectp;
11293 dw_sect->size = bfd_section_size (sectp);
11294 dw_sect->read (objfile);
11295 }
11296 }
11297
11298 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11299 by PER_CU. This is for the non-DWP case.
11300 The result is NULL if DWO_NAME can't be found. */
11301
11302 static struct dwo_file *
11303 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
11304 const char *comp_dir)
11305 {
11306 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11307
11308 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
11309 if (dbfd == NULL)
11310 {
11311 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
11312
11313 return NULL;
11314 }
11315
11316 dwo_file_up dwo_file (new struct dwo_file);
11317 dwo_file->dwo_name = dwo_name;
11318 dwo_file->comp_dir = comp_dir;
11319 dwo_file->dbfd = std::move (dbfd);
11320
11321 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
11322 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
11323 sec, &dwo_file->sections);
11324
11325 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
11326 dwo_file->cus);
11327
11328 if (cu->per_cu->version () < 5)
11329 {
11330 create_debug_types_hash_table (per_objfile, dwo_file.get (),
11331 dwo_file->sections.types, dwo_file->tus);
11332 }
11333 else
11334 {
11335 create_debug_type_hash_table (per_objfile, dwo_file.get (),
11336 &dwo_file->sections.info, dwo_file->tus,
11337 rcuh_kind::COMPILE);
11338 }
11339
11340 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
11341
11342 return dwo_file.release ();
11343 }
11344
11345 /* This function is mapped across the sections and remembers the offset and
11346 size of each of the DWP debugging sections common to version 1 and 2 that
11347 we are interested in. */
11348
11349 static void
11350 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
11351 asection *sectp, dwp_file *dwp_file)
11352 {
11353 const struct dwop_section_names *names = &dwop_section_names;
11354 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11355
11356 /* Record the ELF section number for later lookup: this is what the
11357 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11358 gdb_assert (elf_section_nr < dwp_file->num_sections);
11359 dwp_file->elf_sections[elf_section_nr] = sectp;
11360
11361 /* Look for specific sections that we need. */
11362 struct dwarf2_section_info *dw_sect = nullptr;
11363 if (names->str_dwo.matches (sectp->name))
11364 dw_sect = &dwp_file->sections.str;
11365 else if (names->cu_index.matches (sectp->name))
11366 dw_sect = &dwp_file->sections.cu_index;
11367 else if (names->tu_index.matches (sectp->name))
11368 dw_sect = &dwp_file->sections.tu_index;
11369
11370 if (dw_sect != nullptr)
11371 {
11372 dw_sect->s.section = sectp;
11373 dw_sect->size = bfd_section_size (sectp);
11374 dw_sect->read (objfile);
11375 }
11376 }
11377
11378 /* This function is mapped across the sections and remembers the offset and
11379 size of each of the DWP version 2 debugging sections that we are interested
11380 in. This is split into a separate function because we don't know if we
11381 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11382
11383 static void
11384 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
11385 asection *sectp, void *dwp_file_ptr)
11386 {
11387 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11388 const struct dwop_section_names *names = &dwop_section_names;
11389 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11390
11391 /* Record the ELF section number for later lookup: this is what the
11392 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11393 gdb_assert (elf_section_nr < dwp_file->num_sections);
11394 dwp_file->elf_sections[elf_section_nr] = sectp;
11395
11396 /* Look for specific sections that we need. */
11397 struct dwarf2_section_info *dw_sect = nullptr;
11398 if (names->abbrev_dwo.matches (sectp->name))
11399 dw_sect = &dwp_file->sections.abbrev;
11400 else if (names->info_dwo.matches (sectp->name))
11401 dw_sect = &dwp_file->sections.info;
11402 else if (names->line_dwo.matches (sectp->name))
11403 dw_sect = &dwp_file->sections.line;
11404 else if (names->loc_dwo.matches (sectp->name))
11405 dw_sect = &dwp_file->sections.loc;
11406 else if (names->macinfo_dwo.matches (sectp->name))
11407 dw_sect = &dwp_file->sections.macinfo;
11408 else if (names->macro_dwo.matches (sectp->name))
11409 dw_sect = &dwp_file->sections.macro;
11410 else if (names->str_offsets_dwo.matches (sectp->name))
11411 dw_sect = &dwp_file->sections.str_offsets;
11412 else if (names->types_dwo.matches (sectp->name))
11413 dw_sect = &dwp_file->sections.types;
11414
11415 if (dw_sect != nullptr)
11416 {
11417 dw_sect->s.section = sectp;
11418 dw_sect->size = bfd_section_size (sectp);
11419 dw_sect->read (objfile);
11420 }
11421 }
11422
11423 /* This function is mapped across the sections and remembers the offset and
11424 size of each of the DWP version 5 debugging sections that we are interested
11425 in. This is split into a separate function because we don't know if we
11426 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11427
11428 static void
11429 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
11430 asection *sectp, void *dwp_file_ptr)
11431 {
11432 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
11433 const struct dwop_section_names *names = &dwop_section_names;
11434 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
11435
11436 /* Record the ELF section number for later lookup: this is what the
11437 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11438 gdb_assert (elf_section_nr < dwp_file->num_sections);
11439 dwp_file->elf_sections[elf_section_nr] = sectp;
11440
11441 /* Look for specific sections that we need. */
11442 struct dwarf2_section_info *dw_sect = nullptr;
11443 if (names->abbrev_dwo.matches (sectp->name))
11444 dw_sect = &dwp_file->sections.abbrev;
11445 else if (names->info_dwo.matches (sectp->name))
11446 dw_sect = &dwp_file->sections.info;
11447 else if (names->line_dwo.matches (sectp->name))
11448 dw_sect = &dwp_file->sections.line;
11449 else if (names->loclists_dwo.matches (sectp->name))
11450 dw_sect = &dwp_file->sections.loclists;
11451 else if (names->macro_dwo.matches (sectp->name))
11452 dw_sect = &dwp_file->sections.macro;
11453 else if (names->rnglists_dwo.matches (sectp->name))
11454 dw_sect = &dwp_file->sections.rnglists;
11455 else if (names->str_offsets_dwo.matches (sectp->name))
11456 dw_sect = &dwp_file->sections.str_offsets;
11457
11458 if (dw_sect != nullptr)
11459 {
11460 dw_sect->s.section = sectp;
11461 dw_sect->size = bfd_section_size (sectp);
11462 dw_sect->read (objfile);
11463 }
11464 }
11465
11466 /* Hash function for dwp_file loaded CUs/TUs. */
11467
11468 static hashval_t
11469 hash_dwp_loaded_cutus (const void *item)
11470 {
11471 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11472
11473 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11474 return dwo_unit->signature;
11475 }
11476
11477 /* Equality function for dwp_file loaded CUs/TUs. */
11478
11479 static int
11480 eq_dwp_loaded_cutus (const void *a, const void *b)
11481 {
11482 const struct dwo_unit *dua = (const struct dwo_unit *) a;
11483 const struct dwo_unit *dub = (const struct dwo_unit *) b;
11484
11485 return dua->signature == dub->signature;
11486 }
11487
11488 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11489
11490 static htab_up
11491 allocate_dwp_loaded_cutus_table ()
11492 {
11493 return htab_up (htab_create_alloc (3,
11494 hash_dwp_loaded_cutus,
11495 eq_dwp_loaded_cutus,
11496 NULL, xcalloc, xfree));
11497 }
11498
11499 /* Try to open DWP file FILE_NAME.
11500 The result is the bfd handle of the file.
11501 If there is a problem finding or opening the file, return NULL.
11502 Upon success, the canonicalized path of the file is stored in the bfd,
11503 same as symfile_bfd_open. */
11504
11505 static gdb_bfd_ref_ptr
11506 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
11507 {
11508 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
11509 1 /*is_dwp*/,
11510 1 /*search_cwd*/));
11511 if (abfd != NULL)
11512 return abfd;
11513
11514 /* Work around upstream bug 15652.
11515 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11516 [Whether that's a "bug" is debatable, but it is getting in our way.]
11517 We have no real idea where the dwp file is, because gdb's realpath-ing
11518 of the executable's path may have discarded the needed info.
11519 [IWBN if the dwp file name was recorded in the executable, akin to
11520 .gnu_debuglink, but that doesn't exist yet.]
11521 Strip the directory from FILE_NAME and search again. */
11522 if (!debug_file_directory.empty ())
11523 {
11524 /* Don't implicitly search the current directory here.
11525 If the user wants to search "." to handle this case,
11526 it must be added to debug-file-directory. */
11527 return try_open_dwop_file (per_objfile, lbasename (file_name),
11528 1 /*is_dwp*/,
11529 0 /*search_cwd*/);
11530 }
11531
11532 return NULL;
11533 }
11534
11535 /* Initialize the use of the DWP file for the current objfile.
11536 By convention the name of the DWP file is ${objfile}.dwp.
11537 The result is NULL if it can't be found. */
11538
11539 static std::unique_ptr<struct dwp_file>
11540 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
11541 {
11542 struct objfile *objfile = per_objfile->objfile;
11543
11544 /* Try to find first .dwp for the binary file before any symbolic links
11545 resolving. */
11546
11547 /* If the objfile is a debug file, find the name of the real binary
11548 file and get the name of dwp file from there. */
11549 std::string dwp_name;
11550 if (objfile->separate_debug_objfile_backlink != NULL)
11551 {
11552 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
11553 const char *backlink_basename = lbasename (backlink->original_name);
11554
11555 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
11556 }
11557 else
11558 dwp_name = objfile->original_name;
11559
11560 dwp_name += ".dwp";
11561
11562 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
11563 if (dbfd == NULL
11564 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
11565 {
11566 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11567 dwp_name = objfile_name (objfile);
11568 dwp_name += ".dwp";
11569 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
11570 }
11571
11572 if (dbfd == NULL)
11573 {
11574 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
11575
11576 return std::unique_ptr<dwp_file> ();
11577 }
11578
11579 const char *name = bfd_get_filename (dbfd.get ());
11580 std::unique_ptr<struct dwp_file> dwp_file
11581 (new struct dwp_file (name, std::move (dbfd)));
11582
11583 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
11584 dwp_file->elf_sections =
11585 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
11586 dwp_file->num_sections, asection *);
11587
11588 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11589 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11590 dwp_file.get ());
11591
11592 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
11593
11594 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
11595
11596 /* The DWP file version is stored in the hash table. Oh well. */
11597 if (dwp_file->cus && dwp_file->tus
11598 && dwp_file->cus->version != dwp_file->tus->version)
11599 {
11600 /* Technically speaking, we should try to limp along, but this is
11601 pretty bizarre. We use pulongest here because that's the established
11602 portability solution (e.g, we cannot use %u for uint32_t). */
11603 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11604 " TU version %s [in DWP file %s]"),
11605 pulongest (dwp_file->cus->version),
11606 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11607 }
11608
11609 if (dwp_file->cus)
11610 dwp_file->version = dwp_file->cus->version;
11611 else if (dwp_file->tus)
11612 dwp_file->version = dwp_file->tus->version;
11613 else
11614 dwp_file->version = 2;
11615
11616 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
11617 {
11618 if (dwp_file->version == 2)
11619 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11620 dwp_file.get ());
11621 else
11622 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
11623 dwp_file.get ());
11624 }
11625
11626 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
11627 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
11628
11629 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
11630 dwarf_read_debug_printf (" %s CUs, %s TUs",
11631 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11632 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11633
11634 return dwp_file;
11635 }
11636
11637 /* Wrapper around open_and_init_dwp_file, only open it once. */
11638
11639 static struct dwp_file *
11640 get_dwp_file (dwarf2_per_objfile *per_objfile)
11641 {
11642 if (!per_objfile->per_bfd->dwp_checked)
11643 {
11644 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
11645 per_objfile->per_bfd->dwp_checked = 1;
11646 }
11647 return per_objfile->per_bfd->dwp_file.get ();
11648 }
11649
11650 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11651 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11652 or in the DWP file for the objfile, referenced by THIS_UNIT.
11653 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11654 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11655
11656 This is called, for example, when wanting to read a variable with a
11657 complex location. Therefore we don't want to do file i/o for every call.
11658 Therefore we don't want to look for a DWO file on every call.
11659 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11660 then we check if we've already seen DWO_NAME, and only THEN do we check
11661 for a DWO file.
11662
11663 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11664 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11665
11666 static struct dwo_unit *
11667 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11668 ULONGEST signature, int is_debug_types)
11669 {
11670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11671 struct objfile *objfile = per_objfile->objfile;
11672 const char *kind = is_debug_types ? "TU" : "CU";
11673 void **dwo_file_slot;
11674 struct dwo_file *dwo_file;
11675 struct dwp_file *dwp_file;
11676
11677 /* First see if there's a DWP file.
11678 If we have a DWP file but didn't find the DWO inside it, don't
11679 look for the original DWO file. It makes gdb behave differently
11680 depending on whether one is debugging in the build tree. */
11681
11682 dwp_file = get_dwp_file (per_objfile);
11683 if (dwp_file != NULL)
11684 {
11685 const struct dwp_hash_table *dwp_htab =
11686 is_debug_types ? dwp_file->tus : dwp_file->cus;
11687
11688 if (dwp_htab != NULL)
11689 {
11690 struct dwo_unit *dwo_cutu =
11691 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
11692 is_debug_types);
11693
11694 if (dwo_cutu != NULL)
11695 {
11696 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11697 kind, hex_string (signature),
11698 host_address_to_string (dwo_cutu));
11699
11700 return dwo_cutu;
11701 }
11702 }
11703 }
11704 else
11705 {
11706 /* No DWP file, look for the DWO file. */
11707
11708 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
11709 if (*dwo_file_slot == NULL)
11710 {
11711 /* Read in the file and build a table of the CUs/TUs it contains. */
11712 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
11713 }
11714 /* NOTE: This will be NULL if unable to open the file. */
11715 dwo_file = (struct dwo_file *) *dwo_file_slot;
11716
11717 if (dwo_file != NULL)
11718 {
11719 struct dwo_unit *dwo_cutu = NULL;
11720
11721 if (is_debug_types && dwo_file->tus)
11722 {
11723 struct dwo_unit find_dwo_cutu;
11724
11725 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11726 find_dwo_cutu.signature = signature;
11727 dwo_cutu
11728 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
11729 &find_dwo_cutu);
11730 }
11731 else if (!is_debug_types && dwo_file->cus)
11732 {
11733 struct dwo_unit find_dwo_cutu;
11734
11735 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11736 find_dwo_cutu.signature = signature;
11737 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
11738 &find_dwo_cutu);
11739 }
11740
11741 if (dwo_cutu != NULL)
11742 {
11743 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11744 kind, dwo_name, hex_string (signature),
11745 host_address_to_string (dwo_cutu));
11746
11747 return dwo_cutu;
11748 }
11749 }
11750 }
11751
11752 /* We didn't find it. This could mean a dwo_id mismatch, or
11753 someone deleted the DWO/DWP file, or the search path isn't set up
11754 correctly to find the file. */
11755
11756 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11757 kind, dwo_name, hex_string (signature));
11758
11759 /* This is a warning and not a complaint because it can be caused by
11760 pilot error (e.g., user accidentally deleting the DWO). */
11761 {
11762 /* Print the name of the DWP file if we looked there, helps the user
11763 better diagnose the problem. */
11764 std::string dwp_text;
11765
11766 if (dwp_file != NULL)
11767 dwp_text = string_printf (" [in DWP file %s]",
11768 lbasename (dwp_file->name));
11769
11770 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11771 " [in module %s]"),
11772 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
11773 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
11774 }
11775 return NULL;
11776 }
11777
11778 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11779 See lookup_dwo_cutu_unit for details. */
11780
11781 static struct dwo_unit *
11782 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
11783 ULONGEST signature)
11784 {
11785 gdb_assert (!cu->per_cu->is_debug_types);
11786
11787 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
11788 }
11789
11790 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11791 See lookup_dwo_cutu_unit for details. */
11792
11793 static struct dwo_unit *
11794 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
11795 {
11796 gdb_assert (cu->per_cu->is_debug_types);
11797
11798 signatured_type *sig_type = (signatured_type *) cu->per_cu;
11799
11800 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
11801 }
11802
11803 /* Traversal function for queue_and_load_all_dwo_tus. */
11804
11805 static int
11806 queue_and_load_dwo_tu (void **slot, void *info)
11807 {
11808 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11809 dwarf2_cu *cu = (dwarf2_cu *) info;
11810 ULONGEST signature = dwo_unit->signature;
11811 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
11812
11813 if (sig_type != NULL)
11814 {
11815 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11816 a real dependency of PER_CU on SIG_TYPE. That is detected later
11817 while processing PER_CU. */
11818 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
11819 cu->lang ()))
11820 load_full_type_unit (sig_type, cu->per_objfile);
11821 cu->per_cu->imported_symtabs_push (sig_type);
11822 }
11823
11824 return 1;
11825 }
11826
11827 /* Queue all TUs contained in the DWO of CU to be read in.
11828 The DWO may have the only definition of the type, though it may not be
11829 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11830 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11831
11832 static void
11833 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
11834 {
11835 struct dwo_unit *dwo_unit;
11836 struct dwo_file *dwo_file;
11837
11838 gdb_assert (cu != nullptr);
11839 gdb_assert (!cu->per_cu->is_debug_types);
11840 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
11841
11842 dwo_unit = cu->dwo_unit;
11843 gdb_assert (dwo_unit != NULL);
11844
11845 dwo_file = dwo_unit->dwo_file;
11846 if (dwo_file->tus != NULL)
11847 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
11848 }
11849
11850 /* Read in various DIEs. */
11851
11852 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11853 Inherit only the children of the DW_AT_abstract_origin DIE not being
11854 already referenced by DW_AT_abstract_origin from the children of the
11855 current DIE. */
11856
11857 static void
11858 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11859 {
11860 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11861 if (attr == nullptr)
11862 return;
11863
11864 /* Note that following die references may follow to a die in a
11865 different CU. */
11866 dwarf2_cu *origin_cu = cu;
11867
11868 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11869 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
11870
11871 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11872 symbols in. */
11873 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
11874 origin_cu->list_in_scope = cu->list_in_scope;
11875
11876 if (die->tag != origin_die->tag
11877 && !(die->tag == DW_TAG_inlined_subroutine
11878 && origin_die->tag == DW_TAG_subprogram))
11879 complaint (_("DIE %s and its abstract origin %s have different tags"),
11880 sect_offset_str (die->sect_off),
11881 sect_offset_str (origin_die->sect_off));
11882
11883 /* Find if the concrete and abstract trees are structurally the
11884 same. This is a shallow traversal and it is not bullet-proof;
11885 the compiler can trick the debugger into believing that the trees
11886 are isomorphic, whereas they actually are not. However, the
11887 likelyhood of this happening is pretty low, and a full-fledged
11888 check would be an overkill. */
11889 bool are_isomorphic = true;
11890 die_info *concrete_child = die->child;
11891 die_info *abstract_child = origin_die->child;
11892 while (concrete_child != nullptr || abstract_child != nullptr)
11893 {
11894 if (concrete_child == nullptr
11895 || abstract_child == nullptr
11896 || concrete_child->tag != abstract_child->tag)
11897 {
11898 are_isomorphic = false;
11899 break;
11900 }
11901
11902 concrete_child = concrete_child->sibling;
11903 abstract_child = abstract_child->sibling;
11904 }
11905
11906 /* Walk the origin's children in parallel to the concrete children.
11907 This helps match an origin child in case the debug info misses
11908 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11909 origin tree may not have the same tree structure as the concrete
11910 DIE, though. */
11911 die_info *corresponding_abstract_child
11912 = are_isomorphic ? origin_die->child : nullptr;
11913
11914 std::vector<sect_offset> offsets;
11915
11916 for (die_info *child_die = die->child;
11917 child_die && child_die->tag;
11918 child_die = child_die->sibling)
11919 {
11920 /* We are trying to process concrete instance entries:
11921 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11922 it's not relevant to our analysis here. i.e. detecting DIEs that are
11923 present in the abstract instance but not referenced in the concrete
11924 one. */
11925 if (child_die->tag == DW_TAG_call_site
11926 || child_die->tag == DW_TAG_GNU_call_site)
11927 {
11928 if (are_isomorphic)
11929 corresponding_abstract_child
11930 = corresponding_abstract_child->sibling;
11931 continue;
11932 }
11933
11934 /* For each CHILD_DIE, find the corresponding child of
11935 ORIGIN_DIE. If there is more than one layer of
11936 DW_AT_abstract_origin, follow them all; there shouldn't be,
11937 but GCC versions at least through 4.4 generate this (GCC PR
11938 40573). */
11939 die_info *child_origin_die = child_die;
11940 dwarf2_cu *child_origin_cu = cu;
11941 while (true)
11942 {
11943 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11944 child_origin_cu);
11945 if (attr == nullptr)
11946 break;
11947
11948 child_origin_die = follow_die_ref (child_origin_die, attr,
11949 &child_origin_cu);
11950 }
11951
11952 /* If missing DW_AT_abstract_origin, try the corresponding child
11953 of the origin. Clang emits such lexical scopes. */
11954 if (child_origin_die == child_die
11955 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
11956 && are_isomorphic
11957 && child_die->tag == DW_TAG_lexical_block)
11958 child_origin_die = corresponding_abstract_child;
11959
11960 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11961 counterpart may exist. */
11962 if (child_origin_die != child_die)
11963 {
11964 if (child_die->tag != child_origin_die->tag
11965 && !(child_die->tag == DW_TAG_inlined_subroutine
11966 && child_origin_die->tag == DW_TAG_subprogram))
11967 complaint (_("Child DIE %s and its abstract origin %s have "
11968 "different tags"),
11969 sect_offset_str (child_die->sect_off),
11970 sect_offset_str (child_origin_die->sect_off));
11971 if (child_origin_die->parent != origin_die)
11972 complaint (_("Child DIE %s and its abstract origin %s have "
11973 "different parents"),
11974 sect_offset_str (child_die->sect_off),
11975 sect_offset_str (child_origin_die->sect_off));
11976 else
11977 offsets.push_back (child_origin_die->sect_off);
11978 }
11979
11980 if (are_isomorphic)
11981 corresponding_abstract_child = corresponding_abstract_child->sibling;
11982 }
11983
11984 if (!offsets.empty ())
11985 {
11986 std::sort (offsets.begin (), offsets.end ());
11987
11988 for (auto offsets_it = offsets.begin () + 1;
11989 offsets_it < offsets.end ();
11990 ++offsets_it)
11991 if (*(offsets_it - 1) == *offsets_it)
11992 complaint (_("Multiple children of DIE %s refer "
11993 "to DIE %s as their abstract origin"),
11994 sect_offset_str (die->sect_off),
11995 sect_offset_str (*offsets_it));
11996 }
11997
11998 auto offsets_it = offsets.begin ();
11999 die_info *origin_child_die = origin_die->child;
12000 while (origin_child_die != nullptr && origin_child_die->tag != 0)
12001 {
12002 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12003 while (offsets_it < offsets.end ()
12004 && *offsets_it < origin_child_die->sect_off)
12005 ++offsets_it;
12006
12007 if (offsets_it == offsets.end ()
12008 || *offsets_it > origin_child_die->sect_off)
12009 {
12010 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12011 Check whether we're already processing ORIGIN_CHILD_DIE.
12012 This can happen with mutually referenced abstract_origins.
12013 PR 16581. */
12014 if (!origin_child_die->in_process)
12015 process_die (origin_child_die, origin_cu);
12016 }
12017
12018 origin_child_die = origin_child_die->sibling;
12019 }
12020
12021 origin_cu->list_in_scope = origin_previous_list_in_scope;
12022
12023 if (cu != origin_cu)
12024 compute_delayed_physnames (origin_cu);
12025 }
12026
12027 static void
12028 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12029 {
12030 struct objfile *objfile = cu->per_objfile->objfile;
12031 struct gdbarch *gdbarch = objfile->arch ();
12032 struct context_stack *newobj;
12033 CORE_ADDR lowpc;
12034 CORE_ADDR highpc;
12035 struct die_info *child_die;
12036 struct attribute *attr, *call_line, *call_file;
12037 const char *name;
12038 CORE_ADDR baseaddr;
12039 struct block *block;
12040 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12041 std::vector<struct symbol *> template_args;
12042 struct template_symbol *templ_func = NULL;
12043
12044 if (inlined_func)
12045 {
12046 /* If we do not have call site information, we can't show the
12047 caller of this inlined function. That's too confusing, so
12048 only use the scope for local variables. */
12049 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12050 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12051 if (call_line == NULL || call_file == NULL)
12052 {
12053 read_lexical_block_scope (die, cu);
12054 return;
12055 }
12056 }
12057
12058 baseaddr = objfile->text_section_offset ();
12059
12060 name = dwarf2_name (die, cu);
12061 if (name == nullptr)
12062 name = dw2_linkage_name (die, cu);
12063
12064 /* Ignore functions with missing or empty names. These are actually
12065 illegal according to the DWARF standard. */
12066 if (name == NULL)
12067 {
12068 complaint (_("missing name for subprogram DIE at %s"),
12069 sect_offset_str (die->sect_off));
12070 return;
12071 }
12072
12073 /* Ignore functions with missing or invalid low and high pc attributes. */
12074 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr)
12075 <= PC_BOUNDS_INVALID)
12076 {
12077 if (have_complaint ())
12078 {
12079 attr = dwarf2_attr (die, DW_AT_external, cu);
12080 bool external_p = attr != nullptr && attr->as_boolean ();
12081 attr = dwarf2_attr (die, DW_AT_inline, cu);
12082 bool inlined_p
12083 = (attr != nullptr
12084 && attr->is_nonnegative ()
12085 && (attr->as_nonnegative () == DW_INL_inlined
12086 || attr->as_nonnegative () == DW_INL_declared_inlined));
12087 attr = dwarf2_attr (die, DW_AT_declaration, cu);
12088 bool decl_p = attr != nullptr && attr->as_boolean ();
12089 if (!external_p && !inlined_p && !decl_p)
12090 complaint (_("cannot get low and high bounds "
12091 "for subprogram DIE at %s"),
12092 sect_offset_str (die->sect_off));
12093 }
12094 return;
12095 }
12096
12097 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12098 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12099
12100 /* If we have any template arguments, then we must allocate a
12101 different sort of symbol. */
12102 for (child_die = die->child; child_die; child_die = child_die->sibling)
12103 {
12104 if (child_die->tag == DW_TAG_template_type_param
12105 || child_die->tag == DW_TAG_template_value_param)
12106 {
12107 templ_func = new (&objfile->objfile_obstack) template_symbol;
12108 templ_func->subclass = SYMBOL_TEMPLATE;
12109 break;
12110 }
12111 }
12112
12113 gdb_assert (cu->get_builder () != nullptr);
12114 newobj = cu->get_builder ()->push_context (0, lowpc);
12115 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12116 (struct symbol *) templ_func);
12117
12118 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12119 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12120 cu->lang ());
12121
12122 /* If there is a location expression for DW_AT_frame_base, record
12123 it. */
12124 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12125 if (attr != nullptr)
12126 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12127
12128 /* If there is a location for the static link, record it. */
12129 newobj->static_link = NULL;
12130 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12131 if (attr != nullptr)
12132 {
12133 newobj->static_link
12134 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12135 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12136 cu->addr_type ());
12137 }
12138
12139 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12140
12141 if (die->child != NULL)
12142 {
12143 child_die = die->child;
12144 while (child_die && child_die->tag)
12145 {
12146 if (child_die->tag == DW_TAG_template_type_param
12147 || child_die->tag == DW_TAG_template_value_param)
12148 {
12149 struct symbol *arg = new_symbol (child_die, NULL, cu);
12150
12151 if (arg != NULL)
12152 template_args.push_back (arg);
12153 }
12154 else
12155 process_die (child_die, cu);
12156 child_die = child_die->sibling;
12157 }
12158 }
12159
12160 inherit_abstract_dies (die, cu);
12161
12162 /* If we have a DW_AT_specification, we might need to import using
12163 directives from the context of the specification DIE. See the
12164 comment in determine_prefix. */
12165 if (cu->lang () == language_cplus
12166 && dwarf2_attr (die, DW_AT_specification, cu))
12167 {
12168 struct dwarf2_cu *spec_cu = cu;
12169 struct die_info *spec_die = die_specification (die, &spec_cu);
12170
12171 while (spec_die)
12172 {
12173 child_die = spec_die->child;
12174 while (child_die && child_die->tag)
12175 {
12176 if (child_die->tag == DW_TAG_imported_module)
12177 process_die (child_die, spec_cu);
12178 child_die = child_die->sibling;
12179 }
12180
12181 /* In some cases, GCC generates specification DIEs that
12182 themselves contain DW_AT_specification attributes. */
12183 spec_die = die_specification (spec_die, &spec_cu);
12184 }
12185 }
12186
12187 struct context_stack cstk = cu->get_builder ()->pop_context ();
12188 /* Make a block for the local symbols within. */
12189 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12190 cstk.static_link, lowpc, highpc);
12191
12192 /* For C++, set the block's scope. */
12193 if ((cu->lang () == language_cplus
12194 || cu->lang () == language_fortran
12195 || cu->lang () == language_d
12196 || cu->lang () == language_rust)
12197 && cu->processing_has_namespace_info)
12198 block_set_scope (block, determine_prefix (die, cu),
12199 &objfile->objfile_obstack);
12200
12201 /* If we have address ranges, record them. */
12202 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12203
12204 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12205
12206 /* Attach template arguments to function. */
12207 if (!template_args.empty ())
12208 {
12209 gdb_assert (templ_func != NULL);
12210
12211 templ_func->n_template_arguments = template_args.size ();
12212 templ_func->template_arguments
12213 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12214 templ_func->n_template_arguments);
12215 memcpy (templ_func->template_arguments,
12216 template_args.data (),
12217 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12218
12219 /* Make sure that the symtab is set on the new symbols. Even
12220 though they don't appear in this symtab directly, other parts
12221 of gdb assume that symbols do, and this is reasonably
12222 true. */
12223 for (symbol *sym : template_args)
12224 sym->set_symtab (templ_func->symtab ());
12225 }
12226
12227 /* In C++, we can have functions nested inside functions (e.g., when
12228 a function declares a class that has methods). This means that
12229 when we finish processing a function scope, we may need to go
12230 back to building a containing block's symbol lists. */
12231 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12232 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12233
12234 /* If we've finished processing a top-level function, subsequent
12235 symbols go in the file symbol list. */
12236 if (cu->get_builder ()->outermost_context_p ())
12237 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12238 }
12239
12240 /* Process all the DIES contained within a lexical block scope. Start
12241 a new scope, process the dies, and then close the scope. */
12242
12243 static void
12244 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12245 {
12246 struct objfile *objfile = cu->per_objfile->objfile;
12247 struct gdbarch *gdbarch = objfile->arch ();
12248 CORE_ADDR lowpc, highpc;
12249 struct die_info *child_die;
12250 CORE_ADDR baseaddr;
12251
12252 baseaddr = objfile->text_section_offset ();
12253
12254 /* Ignore blocks with missing or invalid low and high pc attributes. */
12255 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12256 as multiple lexical blocks? Handling children in a sane way would
12257 be nasty. Might be easier to properly extend generic blocks to
12258 describe ranges. */
12259 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, nullptr, nullptr))
12260 {
12261 case PC_BOUNDS_NOT_PRESENT:
12262 /* DW_TAG_lexical_block has no attributes, process its children as if
12263 there was no wrapping by that DW_TAG_lexical_block.
12264 GCC does no longer produces such DWARF since GCC r224161. */
12265 for (child_die = die->child;
12266 child_die != NULL && child_die->tag;
12267 child_die = child_die->sibling)
12268 {
12269 /* We might already be processing this DIE. This can happen
12270 in an unusual circumstance -- where a subroutine A
12271 appears lexically in another subroutine B, but A actually
12272 inlines B. The recursion is broken here, rather than in
12273 inherit_abstract_dies, because it seems better to simply
12274 drop concrete children here. */
12275 if (!child_die->in_process)
12276 process_die (child_die, cu);
12277 }
12278 return;
12279 case PC_BOUNDS_INVALID:
12280 return;
12281 }
12282 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12283 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12284
12285 cu->get_builder ()->push_context (0, lowpc);
12286 if (die->child != NULL)
12287 {
12288 child_die = die->child;
12289 while (child_die && child_die->tag)
12290 {
12291 process_die (child_die, cu);
12292 child_die = child_die->sibling;
12293 }
12294 }
12295 inherit_abstract_dies (die, cu);
12296 struct context_stack cstk = cu->get_builder ()->pop_context ();
12297
12298 if (*cu->get_builder ()->get_local_symbols () != NULL
12299 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12300 {
12301 struct block *block
12302 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12303 cstk.start_addr, highpc);
12304
12305 /* Note that recording ranges after traversing children, as we
12306 do here, means that recording a parent's ranges entails
12307 walking across all its children's ranges as they appear in
12308 the address map, which is quadratic behavior.
12309
12310 It would be nicer to record the parent's ranges before
12311 traversing its children, simply overriding whatever you find
12312 there. But since we don't even decide whether to create a
12313 block until after we've traversed its children, that's hard
12314 to do. */
12315 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12316 }
12317 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12318 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12319 }
12320
12321 static void dwarf2_ranges_read_low_addrs (unsigned offset,
12322 struct dwarf2_cu *cu,
12323 dwarf_tag tag,
12324 std::vector<CORE_ADDR> &result);
12325
12326 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12327
12328 static void
12329 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
12330 {
12331 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12332 struct objfile *objfile = per_objfile->objfile;
12333 struct gdbarch *gdbarch = objfile->arch ();
12334 CORE_ADDR pc, baseaddr;
12335 struct attribute *attr;
12336 void **slot;
12337 int nparams;
12338 struct die_info *child_die;
12339
12340 baseaddr = objfile->text_section_offset ();
12341
12342 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
12343 if (attr == NULL)
12344 {
12345 /* This was a pre-DWARF-5 GNU extension alias
12346 for DW_AT_call_return_pc. */
12347 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12348 }
12349 if (!attr)
12350 {
12351 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12352 "DIE %s [in module %s]"),
12353 sect_offset_str (die->sect_off), objfile_name (objfile));
12354 return;
12355 }
12356 pc = attr->as_address () + baseaddr;
12357 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
12358 pc -= baseaddr;
12359
12360 if (cu->call_site_htab == NULL)
12361 cu->call_site_htab = htab_create_alloc_ex (16, call_site::hash,
12362 call_site::eq, NULL,
12363 &objfile->objfile_obstack,
12364 hashtab_obstack_allocate, NULL);
12365 struct call_site call_site_local (pc, nullptr, nullptr);
12366 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
12367 if (*slot != NULL)
12368 {
12369 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12370 "DIE %s [in module %s]"),
12371 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
12372 objfile_name (objfile));
12373 return;
12374 }
12375
12376 /* Count parameters at the caller. */
12377
12378 nparams = 0;
12379 for (child_die = die->child; child_die && child_die->tag;
12380 child_die = child_die->sibling)
12381 {
12382 if (child_die->tag != DW_TAG_call_site_parameter
12383 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12384 {
12385 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12386 "DW_TAG_call_site child DIE %s [in module %s]"),
12387 child_die->tag, sect_offset_str (child_die->sect_off),
12388 objfile_name (objfile));
12389 continue;
12390 }
12391
12392 nparams++;
12393 }
12394
12395 struct call_site *call_site
12396 = new (XOBNEWVAR (&objfile->objfile_obstack,
12397 struct call_site,
12398 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
12399 struct call_site (pc, cu->per_cu, per_objfile);
12400 *slot = call_site;
12401
12402 /* We never call the destructor of call_site, so we must ensure it is
12403 trivially destructible. */
12404 gdb_static_assert(std::is_trivially_destructible<struct call_site>::value);
12405
12406 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
12407 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
12408 {
12409 struct die_info *func_die;
12410
12411 /* Skip also over DW_TAG_inlined_subroutine. */
12412 for (func_die = die->parent;
12413 func_die && func_die->tag != DW_TAG_subprogram
12414 && func_die->tag != DW_TAG_subroutine_type;
12415 func_die = func_die->parent);
12416
12417 /* DW_AT_call_all_calls is a superset
12418 of DW_AT_call_all_tail_calls. */
12419 if (func_die
12420 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
12421 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
12422 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
12423 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
12424 {
12425 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12426 not complete. But keep CALL_SITE for look ups via call_site_htab,
12427 both the initial caller containing the real return address PC and
12428 the final callee containing the current PC of a chain of tail
12429 calls do not need to have the tail call list complete. But any
12430 function candidate for a virtual tail call frame searched via
12431 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12432 determined unambiguously. */
12433 }
12434 else
12435 {
12436 struct type *func_type = NULL;
12437
12438 if (func_die)
12439 func_type = get_die_type (func_die, cu);
12440 if (func_type != NULL)
12441 {
12442 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
12443
12444 /* Enlist this call site to the function. */
12445 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
12446 TYPE_TAIL_CALL_LIST (func_type) = call_site;
12447 }
12448 else
12449 complaint (_("Cannot find function owning DW_TAG_call_site "
12450 "DIE %s [in module %s]"),
12451 sect_offset_str (die->sect_off), objfile_name (objfile));
12452 }
12453 }
12454
12455 attr = dwarf2_attr (die, DW_AT_call_target, cu);
12456 if (attr == NULL)
12457 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
12458 if (attr == NULL)
12459 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
12460 if (attr == NULL)
12461 {
12462 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12463 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12464 }
12465
12466 call_site->target.set_loc_dwarf_block (nullptr);
12467 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
12468 /* Keep NULL DWARF_BLOCK. */;
12469 else if (attr->form_is_block ())
12470 {
12471 struct dwarf2_locexpr_baton *dlbaton;
12472 struct dwarf_block *block = attr->as_block ();
12473
12474 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
12475 dlbaton->data = block->data;
12476 dlbaton->size = block->size;
12477 dlbaton->per_objfile = per_objfile;
12478 dlbaton->per_cu = cu->per_cu;
12479
12480 call_site->target.set_loc_dwarf_block (dlbaton);
12481 }
12482 else if (attr->form_is_ref ())
12483 {
12484 struct dwarf2_cu *target_cu = cu;
12485 struct die_info *target_die;
12486
12487 target_die = follow_die_ref (die, attr, &target_cu);
12488 gdb_assert (target_cu->per_objfile->objfile == objfile);
12489
12490 struct attribute *ranges_attr
12491 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
12492
12493 if (die_is_declaration (target_die, target_cu))
12494 {
12495 const char *target_physname;
12496
12497 /* Prefer the mangled name; otherwise compute the demangled one. */
12498 target_physname = dw2_linkage_name (target_die, target_cu);
12499 if (target_physname == NULL)
12500 target_physname = dwarf2_physname (NULL, target_die, target_cu);
12501 if (target_physname == NULL)
12502 complaint (_("DW_AT_call_target target DIE has invalid "
12503 "physname, for referencing DIE %s [in module %s]"),
12504 sect_offset_str (die->sect_off), objfile_name (objfile));
12505 else
12506 call_site->target.set_loc_physname (target_physname);
12507 }
12508 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
12509 {
12510 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
12511 + target_cu->gnu_ranges_base);
12512 std::vector<CORE_ADDR> addresses;
12513 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
12514 target_die->tag, addresses);
12515 CORE_ADDR *saved = XOBNEWVAR (&objfile->objfile_obstack, CORE_ADDR,
12516 addresses.size ());
12517 std::copy (addresses.begin (), addresses.end (), saved);
12518 call_site->target.set_loc_array (addresses.size (), saved);
12519 }
12520 else
12521 {
12522 CORE_ADDR lowpc;
12523
12524 /* DW_AT_entry_pc should be preferred. */
12525 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
12526 nullptr, nullptr)
12527 <= PC_BOUNDS_INVALID)
12528 complaint (_("DW_AT_call_target target DIE has invalid "
12529 "low pc, for referencing DIE %s [in module %s]"),
12530 sect_offset_str (die->sect_off), objfile_name (objfile));
12531 else
12532 {
12533 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr)
12534 - baseaddr);
12535 call_site->target.set_loc_physaddr (lowpc);
12536 }
12537 }
12538 }
12539 else
12540 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12541 "block nor reference, for DIE %s [in module %s]"),
12542 sect_offset_str (die->sect_off), objfile_name (objfile));
12543
12544 for (child_die = die->child;
12545 child_die && child_die->tag;
12546 child_die = child_die->sibling)
12547 {
12548 struct call_site_parameter *parameter;
12549 struct attribute *loc, *origin;
12550
12551 if (child_die->tag != DW_TAG_call_site_parameter
12552 && child_die->tag != DW_TAG_GNU_call_site_parameter)
12553 {
12554 /* Already printed the complaint above. */
12555 continue;
12556 }
12557
12558 gdb_assert (call_site->parameter_count < nparams);
12559 parameter = &call_site->parameter[call_site->parameter_count];
12560
12561 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12562 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12563 register is contained in DW_AT_call_value. */
12564
12565 loc = dwarf2_attr (child_die, DW_AT_location, cu);
12566 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
12567 if (origin == NULL)
12568 {
12569 /* This was a pre-DWARF-5 GNU extension alias
12570 for DW_AT_call_parameter. */
12571 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
12572 }
12573 if (loc == NULL && origin != NULL && origin->form_is_ref ())
12574 {
12575 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
12576
12577 sect_offset sect_off = origin->get_ref_die_offset ();
12578 if (!cu->header.offset_in_cu_p (sect_off))
12579 {
12580 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12581 binding can be done only inside one CU. Such referenced DIE
12582 therefore cannot be even moved to DW_TAG_partial_unit. */
12583 complaint (_("DW_AT_call_parameter offset is not in CU for "
12584 "DW_TAG_call_site child DIE %s [in module %s]"),
12585 sect_offset_str (child_die->sect_off),
12586 objfile_name (objfile));
12587 continue;
12588 }
12589 parameter->u.param_cu_off
12590 = (cu_offset) (sect_off - cu->header.sect_off);
12591 }
12592 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
12593 {
12594 complaint (_("No DW_FORM_block* DW_AT_location for "
12595 "DW_TAG_call_site child DIE %s [in module %s]"),
12596 sect_offset_str (child_die->sect_off), objfile_name (objfile));
12597 continue;
12598 }
12599 else
12600 {
12601 struct dwarf_block *block = loc->as_block ();
12602
12603 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
12604 (block->data, &block->data[block->size]);
12605 if (parameter->u.dwarf_reg != -1)
12606 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12607 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
12608 &block->data[block->size],
12609 &parameter->u.fb_offset))
12610 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12611 else
12612 {
12613 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12614 "for DW_FORM_block* DW_AT_location is supported for "
12615 "DW_TAG_call_site child DIE %s "
12616 "[in module %s]"),
12617 sect_offset_str (child_die->sect_off),
12618 objfile_name (objfile));
12619 continue;
12620 }
12621 }
12622
12623 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12624 if (attr == NULL)
12625 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12626 if (attr == NULL || !attr->form_is_block ())
12627 {
12628 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12629 "DW_TAG_call_site child DIE %s [in module %s]"),
12630 sect_offset_str (child_die->sect_off),
12631 objfile_name (objfile));
12632 continue;
12633 }
12634
12635 struct dwarf_block *block = attr->as_block ();
12636 parameter->value = block->data;
12637 parameter->value_size = block->size;
12638
12639 /* Parameters are not pre-cleared by memset above. */
12640 parameter->data_value = NULL;
12641 parameter->data_value_size = 0;
12642 call_site->parameter_count++;
12643
12644 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12645 if (attr == NULL)
12646 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12647 if (attr != nullptr)
12648 {
12649 if (!attr->form_is_block ())
12650 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12651 "DW_TAG_call_site child DIE %s [in module %s]"),
12652 sect_offset_str (child_die->sect_off),
12653 objfile_name (objfile));
12654 else
12655 {
12656 block = attr->as_block ();
12657 parameter->data_value = block->data;
12658 parameter->data_value_size = block->size;
12659 }
12660 }
12661 }
12662 }
12663
12664 /* Helper function for read_variable. If DIE represents a virtual
12665 table, then return the type of the concrete object that is
12666 associated with the virtual table. Otherwise, return NULL. */
12667
12668 static struct type *
12669 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12670 {
12671 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
12672 if (attr == NULL)
12673 return NULL;
12674
12675 /* Find the type DIE. */
12676 struct die_info *type_die = NULL;
12677 struct dwarf2_cu *type_cu = cu;
12678
12679 if (attr->form_is_ref ())
12680 type_die = follow_die_ref (die, attr, &type_cu);
12681 if (type_die == NULL)
12682 return NULL;
12683
12684 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
12685 return NULL;
12686 return die_containing_type (type_die, type_cu);
12687 }
12688
12689 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12690
12691 static void
12692 read_variable (struct die_info *die, struct dwarf2_cu *cu)
12693 {
12694 struct rust_vtable_symbol *storage = NULL;
12695
12696 if (cu->lang () == language_rust)
12697 {
12698 struct type *containing_type = rust_containing_type (die, cu);
12699
12700 if (containing_type != NULL)
12701 {
12702 struct objfile *objfile = cu->per_objfile->objfile;
12703
12704 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
12705 storage->concrete_type = containing_type;
12706 storage->subclass = SYMBOL_RUST_VTABLE;
12707 }
12708 }
12709
12710 struct symbol *res = new_symbol (die, NULL, cu, storage);
12711 struct attribute *abstract_origin
12712 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12713 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
12714 if (res == NULL && loc && abstract_origin)
12715 {
12716 /* We have a variable without a name, but with a location and an abstract
12717 origin. This may be a concrete instance of an abstract variable
12718 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12719 later. */
12720 struct dwarf2_cu *origin_cu = cu;
12721 struct die_info *origin_die
12722 = follow_die_ref (die, abstract_origin, &origin_cu);
12723 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12724 per_objfile->per_bfd->abstract_to_concrete
12725 [origin_die->sect_off].push_back (die->sect_off);
12726 }
12727 }
12728
12729 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12730 reading .debug_rnglists.
12731 Callback's type should be:
12732 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12733 Return true if the attributes are present and valid, otherwise,
12734 return false. */
12735
12736 template <typename Callback>
12737 static bool
12738 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12739 dwarf_tag tag, Callback &&callback)
12740 {
12741 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12742 struct objfile *objfile = per_objfile->objfile;
12743 bfd *obfd = objfile->obfd.get ();
12744 /* Base address selection entry. */
12745 gdb::optional<CORE_ADDR> base;
12746 const gdb_byte *buffer;
12747 bool overflow = false;
12748 ULONGEST addr_index;
12749 struct dwarf2_section_info *rnglists_section;
12750
12751 base = cu->base_address;
12752 rnglists_section = cu_debug_rnglists_section (cu, tag);
12753 rnglists_section->read (objfile);
12754
12755 if (offset >= rnglists_section->size)
12756 {
12757 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12758 offset);
12759 return false;
12760 }
12761 buffer = rnglists_section->buffer + offset;
12762
12763 while (1)
12764 {
12765 /* Initialize it due to a false compiler warning. */
12766 CORE_ADDR range_beginning = 0, range_end = 0;
12767 const gdb_byte *buf_end = (rnglists_section->buffer
12768 + rnglists_section->size);
12769 unsigned int bytes_read;
12770
12771 if (buffer == buf_end)
12772 {
12773 overflow = true;
12774 break;
12775 }
12776 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12777 switch (rlet)
12778 {
12779 case DW_RLE_end_of_list:
12780 break;
12781 case DW_RLE_base_address:
12782 if (buffer + cu->header.addr_size > buf_end)
12783 {
12784 overflow = true;
12785 break;
12786 }
12787 base = cu->header.read_address (obfd, buffer, &bytes_read);
12788 buffer += bytes_read;
12789 break;
12790 case DW_RLE_base_addressx:
12791 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12792 buffer += bytes_read;
12793 base = read_addr_index (cu, addr_index);
12794 break;
12795 case DW_RLE_start_length:
12796 if (buffer + cu->header.addr_size > buf_end)
12797 {
12798 overflow = true;
12799 break;
12800 }
12801 range_beginning = cu->header.read_address (obfd, buffer,
12802 &bytes_read);
12803 buffer += bytes_read;
12804 range_end = (range_beginning
12805 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12806 buffer += bytes_read;
12807 if (buffer > buf_end)
12808 {
12809 overflow = true;
12810 break;
12811 }
12812 break;
12813 case DW_RLE_startx_length:
12814 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12815 buffer += bytes_read;
12816 range_beginning = read_addr_index (cu, addr_index);
12817 if (buffer > buf_end)
12818 {
12819 overflow = true;
12820 break;
12821 }
12822 range_end = (range_beginning
12823 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12824 buffer += bytes_read;
12825 break;
12826 case DW_RLE_offset_pair:
12827 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12828 buffer += bytes_read;
12829 if (buffer > buf_end)
12830 {
12831 overflow = true;
12832 break;
12833 }
12834 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12835 buffer += bytes_read;
12836 if (buffer > buf_end)
12837 {
12838 overflow = true;
12839 break;
12840 }
12841 break;
12842 case DW_RLE_start_end:
12843 if (buffer + 2 * cu->header.addr_size > buf_end)
12844 {
12845 overflow = true;
12846 break;
12847 }
12848 range_beginning = cu->header.read_address (obfd, buffer,
12849 &bytes_read);
12850 buffer += bytes_read;
12851 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
12852 buffer += bytes_read;
12853 break;
12854 case DW_RLE_startx_endx:
12855 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12856 buffer += bytes_read;
12857 range_beginning = read_addr_index (cu, addr_index);
12858 if (buffer > buf_end)
12859 {
12860 overflow = true;
12861 break;
12862 }
12863 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12864 buffer += bytes_read;
12865 range_end = read_addr_index (cu, addr_index);
12866 break;
12867 default:
12868 complaint (_("Invalid .debug_rnglists data (no base address)"));
12869 return false;
12870 }
12871 if (rlet == DW_RLE_end_of_list || overflow)
12872 break;
12873 if (rlet == DW_RLE_base_address)
12874 continue;
12875
12876 if (range_beginning > range_end)
12877 {
12878 /* Inverted range entries are invalid. */
12879 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12880 return false;
12881 }
12882
12883 /* Empty range entries have no effect. */
12884 if (range_beginning == range_end)
12885 continue;
12886
12887 /* Only DW_RLE_offset_pair needs the base address added. */
12888 if (rlet == DW_RLE_offset_pair)
12889 {
12890 if (!base.has_value ())
12891 {
12892 /* We have no valid base address for the DW_RLE_offset_pair. */
12893 complaint (_("Invalid .debug_rnglists data (no base address for "
12894 "DW_RLE_offset_pair)"));
12895 return false;
12896 }
12897
12898 range_beginning += *base;
12899 range_end += *base;
12900 }
12901
12902 /* A not-uncommon case of bad debug info.
12903 Don't pollute the addrmap with bad data. */
12904 if (range_beginning == 0
12905 && !per_objfile->per_bfd->has_section_at_zero)
12906 {
12907 complaint (_(".debug_rnglists entry has start address of zero"
12908 " [in module %s]"), objfile_name (objfile));
12909 continue;
12910 }
12911
12912 callback (range_beginning, range_end);
12913 }
12914
12915 if (overflow)
12916 {
12917 complaint (_("Offset %d is not terminated "
12918 "for DW_AT_ranges attribute"),
12919 offset);
12920 return false;
12921 }
12922
12923 return true;
12924 }
12925
12926 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12927 Callback's type should be:
12928 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12929 Return 1 if the attributes are present and valid, otherwise, return 0. */
12930
12931 template <typename Callback>
12932 static int
12933 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
12934 Callback &&callback)
12935 {
12936 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12937 struct objfile *objfile = per_objfile->objfile;
12938 struct comp_unit_head *cu_header = &cu->header;
12939 bfd *obfd = objfile->obfd.get ();
12940 unsigned int addr_size = cu_header->addr_size;
12941 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12942 /* Base address selection entry. */
12943 gdb::optional<CORE_ADDR> base;
12944 unsigned int dummy;
12945 const gdb_byte *buffer;
12946
12947 if (cu_header->version >= 5)
12948 return dwarf2_rnglists_process (offset, cu, tag, callback);
12949
12950 base = cu->base_address;
12951
12952 per_objfile->per_bfd->ranges.read (objfile);
12953 if (offset >= per_objfile->per_bfd->ranges.size)
12954 {
12955 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12956 offset);
12957 return 0;
12958 }
12959 buffer = per_objfile->per_bfd->ranges.buffer + offset;
12960
12961 while (1)
12962 {
12963 CORE_ADDR range_beginning, range_end;
12964
12965 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
12966 buffer += addr_size;
12967 range_end = cu->header.read_address (obfd, buffer, &dummy);
12968 buffer += addr_size;
12969 offset += 2 * addr_size;
12970
12971 /* An end of list marker is a pair of zero addresses. */
12972 if (range_beginning == 0 && range_end == 0)
12973 /* Found the end of list entry. */
12974 break;
12975
12976 /* Each base address selection entry is a pair of 2 values.
12977 The first is the largest possible address, the second is
12978 the base address. Check for a base address here. */
12979 if ((range_beginning & mask) == mask)
12980 {
12981 /* If we found the largest possible address, then we already
12982 have the base address in range_end. */
12983 base = range_end;
12984 continue;
12985 }
12986
12987 if (!base.has_value ())
12988 {
12989 /* We have no valid base address for the ranges
12990 data. */
12991 complaint (_("Invalid .debug_ranges data (no base address)"));
12992 return 0;
12993 }
12994
12995 if (range_beginning > range_end)
12996 {
12997 /* Inverted range entries are invalid. */
12998 complaint (_("Invalid .debug_ranges data (inverted range)"));
12999 return 0;
13000 }
13001
13002 /* Empty range entries have no effect. */
13003 if (range_beginning == range_end)
13004 continue;
13005
13006 range_beginning += *base;
13007 range_end += *base;
13008
13009 /* A not-uncommon case of bad debug info.
13010 Don't pollute the addrmap with bad data. */
13011 if (range_beginning == 0
13012 && !per_objfile->per_bfd->has_section_at_zero)
13013 {
13014 complaint (_(".debug_ranges entry has start address of zero"
13015 " [in module %s]"), objfile_name (objfile));
13016 continue;
13017 }
13018
13019 callback (range_beginning, range_end);
13020 }
13021
13022 return 1;
13023 }
13024
13025 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13026 Return 1 if the attributes are present and valid, otherwise, return 0.
13027 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
13028 ranges in MAP are set, using DATUM as the value. */
13029
13030 static int
13031 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13032 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13033 addrmap *map, void *datum, dwarf_tag tag)
13034 {
13035 struct objfile *objfile = cu->per_objfile->objfile;
13036 struct gdbarch *gdbarch = objfile->arch ();
13037 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13038 int low_set = 0;
13039 CORE_ADDR low = 0;
13040 CORE_ADDR high = 0;
13041 int retval;
13042
13043 retval = dwarf2_ranges_process (offset, cu, tag,
13044 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13045 {
13046 if (map != nullptr)
13047 {
13048 CORE_ADDR lowpc;
13049 CORE_ADDR highpc;
13050
13051 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13052 range_beginning + baseaddr)
13053 - baseaddr);
13054 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13055 range_end + baseaddr)
13056 - baseaddr);
13057 map->set_empty (lowpc, highpc - 1, datum);
13058 }
13059
13060 /* FIXME: This is recording everything as a low-high
13061 segment of consecutive addresses. We should have a
13062 data structure for discontiguous block ranges
13063 instead. */
13064 if (! low_set)
13065 {
13066 low = range_beginning;
13067 high = range_end;
13068 low_set = 1;
13069 }
13070 else
13071 {
13072 if (range_beginning < low)
13073 low = range_beginning;
13074 if (range_end > high)
13075 high = range_end;
13076 }
13077 });
13078 if (!retval)
13079 return 0;
13080
13081 if (! low_set)
13082 /* If the first entry is an end-of-list marker, the range
13083 describes an empty scope, i.e. no instructions. */
13084 return 0;
13085
13086 if (low_return)
13087 *low_return = low;
13088 if (high_return)
13089 *high_return = high;
13090 return 1;
13091 }
13092
13093 /* Process ranges and fill in a vector of the low PC values only. */
13094
13095 static void
13096 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
13097 dwarf_tag tag,
13098 std::vector<CORE_ADDR> &result)
13099 {
13100 dwarf2_ranges_process (offset, cu, tag,
13101 [&] (CORE_ADDR start, CORE_ADDR end)
13102 {
13103 result.push_back (start);
13104 });
13105 }
13106
13107 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13108 definition for the return value. *LOWPC and *HIGHPC are set iff
13109 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13110
13111 static enum pc_bounds_kind
13112 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13113 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13114 addrmap *map, void *datum)
13115 {
13116 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13117 struct attribute *attr;
13118 struct attribute *attr_high;
13119 CORE_ADDR low = 0;
13120 CORE_ADDR high = 0;
13121 enum pc_bounds_kind ret;
13122
13123 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13124 if (attr_high)
13125 {
13126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13127 if (attr != nullptr)
13128 {
13129 low = attr->as_address ();
13130 high = attr_high->as_address ();
13131 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13132 high += low;
13133 }
13134 else
13135 /* Found high w/o low attribute. */
13136 return PC_BOUNDS_INVALID;
13137
13138 /* Found consecutive range of addresses. */
13139 ret = PC_BOUNDS_HIGH_LOW;
13140 }
13141 else
13142 {
13143 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13144 if (attr != nullptr && attr->form_is_unsigned ())
13145 {
13146 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13147 on DWARF version). */
13148 ULONGEST ranges_offset = attr->as_unsigned ();
13149
13150 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13151 this value. */
13152 if (die->tag != DW_TAG_compile_unit)
13153 ranges_offset += cu->gnu_ranges_base;
13154
13155 /* Value of the DW_AT_ranges attribute is the offset in the
13156 .debug_ranges section. */
13157 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu,
13158 map, datum, die->tag))
13159 return PC_BOUNDS_INVALID;
13160 /* Found discontinuous range of addresses. */
13161 ret = PC_BOUNDS_RANGES;
13162 }
13163 else
13164 return PC_BOUNDS_NOT_PRESENT;
13165 }
13166
13167 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13168 if (high <= low)
13169 return PC_BOUNDS_INVALID;
13170
13171 /* When using the GNU linker, .gnu.linkonce. sections are used to
13172 eliminate duplicate copies of functions and vtables and such.
13173 The linker will arbitrarily choose one and discard the others.
13174 The AT_*_pc values for such functions refer to local labels in
13175 these sections. If the section from that file was discarded, the
13176 labels are not in the output, so the relocs get a value of 0.
13177 If this is a discarded function, mark the pc bounds as invalid,
13178 so that GDB will ignore it. */
13179 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
13180 return PC_BOUNDS_INVALID;
13181
13182 *lowpc = low;
13183 if (highpc)
13184 *highpc = high;
13185 return ret;
13186 }
13187
13188 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13189 its low and high PC addresses. Do nothing if these addresses could not
13190 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13191 and HIGHPC to the high address if greater than HIGHPC. */
13192
13193 static void
13194 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13195 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13196 struct dwarf2_cu *cu)
13197 {
13198 CORE_ADDR low, high;
13199 struct die_info *child = die->child;
13200
13201 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
13202 >= PC_BOUNDS_RANGES)
13203 {
13204 *lowpc = std::min (*lowpc, low);
13205 *highpc = std::max (*highpc, high);
13206 }
13207
13208 /* If the language does not allow nested subprograms (either inside
13209 subprograms or lexical blocks), we're done. */
13210 if (cu->lang () != language_ada)
13211 return;
13212
13213 /* Check all the children of the given DIE. If it contains nested
13214 subprograms, then check their pc bounds. Likewise, we need to
13215 check lexical blocks as well, as they may also contain subprogram
13216 definitions. */
13217 while (child && child->tag)
13218 {
13219 if (child->tag == DW_TAG_subprogram
13220 || child->tag == DW_TAG_lexical_block)
13221 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13222 child = child->sibling;
13223 }
13224 }
13225
13226 /* Get the low and high pc's represented by the scope DIE, and store
13227 them in *LOWPC and *HIGHPC. If the correct values can't be
13228 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13229
13230 static void
13231 get_scope_pc_bounds (struct die_info *die,
13232 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13233 struct dwarf2_cu *cu)
13234 {
13235 CORE_ADDR best_low = (CORE_ADDR) -1;
13236 CORE_ADDR best_high = (CORE_ADDR) 0;
13237 CORE_ADDR current_low, current_high;
13238
13239 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
13240 nullptr, nullptr)
13241 >= PC_BOUNDS_RANGES)
13242 {
13243 best_low = current_low;
13244 best_high = current_high;
13245 }
13246 else
13247 {
13248 struct die_info *child = die->child;
13249
13250 while (child && child->tag)
13251 {
13252 switch (child->tag) {
13253 case DW_TAG_subprogram:
13254 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13255 break;
13256 case DW_TAG_namespace:
13257 case DW_TAG_module:
13258 /* FIXME: carlton/2004-01-16: Should we do this for
13259 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13260 that current GCC's always emit the DIEs corresponding
13261 to definitions of methods of classes as children of a
13262 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13263 the DIEs giving the declarations, which could be
13264 anywhere). But I don't see any reason why the
13265 standards says that they have to be there. */
13266 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13267
13268 if (current_low != ((CORE_ADDR) -1))
13269 {
13270 best_low = std::min (best_low, current_low);
13271 best_high = std::max (best_high, current_high);
13272 }
13273 break;
13274 default:
13275 /* Ignore. */
13276 break;
13277 }
13278
13279 child = child->sibling;
13280 }
13281 }
13282
13283 *lowpc = best_low;
13284 *highpc = best_high;
13285 }
13286
13287 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13288 in DIE. */
13289
13290 static void
13291 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13292 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13293 {
13294 struct objfile *objfile = cu->per_objfile->objfile;
13295 struct gdbarch *gdbarch = objfile->arch ();
13296 struct attribute *attr;
13297 struct attribute *attr_high;
13298
13299 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13300 if (attr_high)
13301 {
13302 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13303 if (attr != nullptr)
13304 {
13305 CORE_ADDR low = attr->as_address ();
13306 CORE_ADDR high = attr_high->as_address ();
13307
13308 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13309 high += low;
13310
13311 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13312 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13313 cu->get_builder ()->record_block_range (block, low, high - 1);
13314 }
13315 }
13316
13317 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13318 if (attr != nullptr && attr->form_is_unsigned ())
13319 {
13320 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13321 on DWARF version). */
13322 ULONGEST ranges_offset = attr->as_unsigned ();
13323
13324 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13325 this value. */
13326 if (die->tag != DW_TAG_compile_unit)
13327 ranges_offset += cu->gnu_ranges_base;
13328
13329 std::vector<blockrange> blockvec;
13330 dwarf2_ranges_process (ranges_offset, cu, die->tag,
13331 [&] (CORE_ADDR start, CORE_ADDR end)
13332 {
13333 start += baseaddr;
13334 end += baseaddr;
13335 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13336 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13337 cu->get_builder ()->record_block_range (block, start, end - 1);
13338 blockvec.emplace_back (start, end);
13339 });
13340
13341 block->set_ranges (make_blockranges (objfile, blockvec));
13342 }
13343 }
13344
13345 /* Check whether the producer field indicates either of GCC < 4.6, or the
13346 Intel C/C++ compiler, and cache the result in CU. */
13347
13348 static void
13349 check_producer (struct dwarf2_cu *cu)
13350 {
13351 int major, minor;
13352
13353 if (cu->producer == NULL)
13354 {
13355 /* For unknown compilers expect their behavior is DWARF version
13356 compliant.
13357
13358 GCC started to support .debug_types sections by -gdwarf-4 since
13359 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13360 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13361 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13362 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13363 }
13364 else if (producer_is_gcc (cu->producer, &major, &minor))
13365 {
13366 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13367 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13368 cu->producer_is_gcc_11 = major == 11;
13369 }
13370 else if (producer_is_icc (cu->producer, &major, &minor))
13371 {
13372 cu->producer_is_icc = true;
13373 cu->producer_is_icc_lt_14 = major < 14;
13374 }
13375 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13376 cu->producer_is_codewarrior = true;
13377 else if (producer_is_clang (cu->producer, &major, &minor))
13378 cu->producer_is_clang = true;
13379 else
13380 {
13381 /* For other non-GCC compilers, expect their behavior is DWARF version
13382 compliant. */
13383 }
13384
13385 cu->checked_producer = true;
13386 }
13387
13388 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13389 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13390 during 4.6.0 experimental. */
13391
13392 static bool
13393 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13394 {
13395 if (!cu->checked_producer)
13396 check_producer (cu);
13397
13398 return cu->producer_is_gxx_lt_4_6;
13399 }
13400
13401
13402 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13403 with incorrect is_stmt attributes. */
13404
13405 static bool
13406 producer_is_codewarrior (struct dwarf2_cu *cu)
13407 {
13408 if (!cu->checked_producer)
13409 check_producer (cu);
13410
13411 return cu->producer_is_codewarrior;
13412 }
13413
13414 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13415 If that attribute is not available, return the appropriate
13416 default. */
13417
13418 static enum dwarf_access_attribute
13419 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
13420 {
13421 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13422 if (attr != nullptr)
13423 {
13424 LONGEST value = attr->constant_value (-1);
13425 if (value == DW_ACCESS_public
13426 || value == DW_ACCESS_protected
13427 || value == DW_ACCESS_private)
13428 return (dwarf_access_attribute) value;
13429 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13430 plongest (value));
13431 }
13432
13433 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
13434 {
13435 /* The default DWARF 2 accessibility for members is public, the default
13436 accessibility for inheritance is private. */
13437
13438 if (die->tag != DW_TAG_inheritance)
13439 return DW_ACCESS_public;
13440 else
13441 return DW_ACCESS_private;
13442 }
13443 else
13444 {
13445 /* DWARF 3+ defines the default accessibility a different way. The same
13446 rules apply now for DW_TAG_inheritance as for the members and it only
13447 depends on the container kind. */
13448
13449 if (die->parent->tag == DW_TAG_class_type)
13450 return DW_ACCESS_private;
13451 else
13452 return DW_ACCESS_public;
13453 }
13454 }
13455
13456 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13457 *OFFSET to the byte offset. If the attribute was not found return
13458 0, otherwise return 1. If it was found but could not properly be
13459 handled, set *OFFSET to 0. */
13460
13461 static int
13462 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13463 LONGEST *offset)
13464 {
13465 struct attribute *attr;
13466
13467 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13468 if (attr != NULL)
13469 {
13470 *offset = 0;
13471
13472 /* Note that we do not check for a section offset first here.
13473 This is because DW_AT_data_member_location is new in DWARF 4,
13474 so if we see it, we can assume that a constant form is really
13475 a constant and not a section offset. */
13476 if (attr->form_is_constant ())
13477 *offset = attr->constant_value (0);
13478 else if (attr->form_is_section_offset ())
13479 dwarf2_complex_location_expr_complaint ();
13480 else if (attr->form_is_block ())
13481 *offset = decode_locdesc (attr->as_block (), cu);
13482 else
13483 dwarf2_complex_location_expr_complaint ();
13484
13485 return 1;
13486 }
13487 else
13488 {
13489 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13490 if (attr != nullptr)
13491 {
13492 *offset = attr->constant_value (0);
13493 return 1;
13494 }
13495 }
13496
13497 return 0;
13498 }
13499
13500 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13501 store the results in FIELD. */
13502
13503 static void
13504 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
13505 struct field *field)
13506 {
13507 struct attribute *attr;
13508
13509 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
13510 if (attr != NULL)
13511 {
13512 if (attr->form_is_constant ())
13513 {
13514 LONGEST offset = attr->constant_value (0);
13515
13516 /* Work around this GCC 11 bug, where it would erroneously use -1
13517 data member locations, instead of 0:
13518
13519 Negative DW_AT_data_member_location
13520 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13521 */
13522 if (offset == -1 && cu->producer_is_gcc_11)
13523 {
13524 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13525 offset = 0;
13526 }
13527
13528 field->set_loc_bitpos (offset * bits_per_byte);
13529 }
13530 else if (attr->form_is_section_offset ())
13531 dwarf2_complex_location_expr_complaint ();
13532 else if (attr->form_is_block ())
13533 {
13534 bool handled;
13535 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
13536 if (handled)
13537 field->set_loc_bitpos (offset * bits_per_byte);
13538 else
13539 {
13540 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13541 struct objfile *objfile = per_objfile->objfile;
13542 struct dwarf2_locexpr_baton *dlbaton
13543 = XOBNEW (&objfile->objfile_obstack,
13544 struct dwarf2_locexpr_baton);
13545 dlbaton->data = attr->as_block ()->data;
13546 dlbaton->size = attr->as_block ()->size;
13547 /* When using this baton, we want to compute the address
13548 of the field, not the value. This is why
13549 is_reference is set to false here. */
13550 dlbaton->is_reference = false;
13551 dlbaton->per_objfile = per_objfile;
13552 dlbaton->per_cu = cu->per_cu;
13553
13554 field->set_loc_dwarf_block (dlbaton);
13555 }
13556 }
13557 else
13558 dwarf2_complex_location_expr_complaint ();
13559 }
13560 else
13561 {
13562 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
13563 if (attr != nullptr)
13564 field->set_loc_bitpos (attr->constant_value (0));
13565 }
13566 }
13567
13568 /* Add an aggregate field to the field list. */
13569
13570 static void
13571 dwarf2_add_field (struct field_info *fip, struct die_info *die,
13572 struct dwarf2_cu *cu)
13573 {
13574 struct objfile *objfile = cu->per_objfile->objfile;
13575 struct gdbarch *gdbarch = objfile->arch ();
13576 struct nextfield *new_field;
13577 struct attribute *attr;
13578 struct field *fp;
13579 const char *fieldname = "";
13580
13581 if (die->tag == DW_TAG_inheritance)
13582 {
13583 fip->baseclasses.emplace_back ();
13584 new_field = &fip->baseclasses.back ();
13585 }
13586 else
13587 {
13588 fip->fields.emplace_back ();
13589 new_field = &fip->fields.back ();
13590 }
13591
13592 new_field->offset = die->sect_off;
13593
13594 new_field->accessibility = dwarf2_access_attribute (die, cu);
13595 if (new_field->accessibility != DW_ACCESS_public)
13596 fip->non_public_fields = true;
13597
13598 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13599 if (attr != nullptr)
13600 new_field->virtuality = attr->as_virtuality ();
13601 else
13602 new_field->virtuality = DW_VIRTUALITY_none;
13603
13604 fp = &new_field->field;
13605
13606 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
13607 && !die_is_declaration (die, cu))
13608 {
13609 if (die->tag == DW_TAG_namelist_item)
13610 {
13611 /* Typically, DW_TAG_namelist_item are references to namelist items.
13612 If so, follow that reference. */
13613 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
13614 struct die_info *item_die = nullptr;
13615 struct dwarf2_cu *item_cu = cu;
13616 if (attr1->form_is_ref ())
13617 item_die = follow_die_ref (die, attr1, &item_cu);
13618 if (item_die != nullptr)
13619 die = item_die;
13620 }
13621 /* Data member other than a C++ static data member. */
13622
13623 /* Get type of field. */
13624 fp->set_type (die_type (die, cu));
13625
13626 fp->set_loc_bitpos (0);
13627
13628 /* Get bit size of field (zero if none). */
13629 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
13630 if (attr != nullptr)
13631 {
13632 FIELD_BITSIZE (*fp) = attr->constant_value (0);
13633 }
13634 else
13635 {
13636 FIELD_BITSIZE (*fp) = 0;
13637 }
13638
13639 /* Get bit offset of field. */
13640 handle_member_location (die, cu, fp);
13641 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
13642 if (attr != nullptr && attr->form_is_constant ())
13643 {
13644 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
13645 {
13646 /* For big endian bits, the DW_AT_bit_offset gives the
13647 additional bit offset from the MSB of the containing
13648 anonymous object to the MSB of the field. We don't
13649 have to do anything special since we don't need to
13650 know the size of the anonymous object. */
13651 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
13652 }
13653 else
13654 {
13655 /* For little endian bits, compute the bit offset to the
13656 MSB of the anonymous object, subtract off the number of
13657 bits from the MSB of the field to the MSB of the
13658 object, and then subtract off the number of bits of
13659 the field itself. The result is the bit offset of
13660 the LSB of the field. */
13661 int anonymous_size;
13662 int bit_offset = attr->constant_value (0);
13663
13664 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13665 if (attr != nullptr && attr->form_is_constant ())
13666 {
13667 /* The size of the anonymous object containing
13668 the bit field is explicit, so use the
13669 indicated size (in bytes). */
13670 anonymous_size = attr->constant_value (0);
13671 }
13672 else
13673 {
13674 /* The size of the anonymous object containing
13675 the bit field must be inferred from the type
13676 attribute of the data member containing the
13677 bit field. */
13678 anonymous_size = fp->type ()->length ();
13679 }
13680 fp->set_loc_bitpos (fp->loc_bitpos ()
13681 + anonymous_size * bits_per_byte
13682 - bit_offset - FIELD_BITSIZE (*fp));
13683 }
13684 }
13685
13686 /* Get name of field. */
13687 fieldname = dwarf2_name (die, cu);
13688 if (fieldname == NULL)
13689 fieldname = "";
13690
13691 /* The name is already allocated along with this objfile, so we don't
13692 need to duplicate it for the type. */
13693 fp->set_name (fieldname);
13694
13695 /* Change accessibility for artificial fields (e.g. virtual table
13696 pointer or virtual base class pointer) to private. */
13697 if (dwarf2_attr (die, DW_AT_artificial, cu))
13698 {
13699 FIELD_ARTIFICIAL (*fp) = 1;
13700 new_field->accessibility = DW_ACCESS_private;
13701 fip->non_public_fields = true;
13702 }
13703 }
13704 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
13705 {
13706 /* C++ static member. */
13707
13708 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13709 is a declaration, but all versions of G++ as of this writing
13710 (so through at least 3.2.1) incorrectly generate
13711 DW_TAG_variable tags. */
13712
13713 const char *physname;
13714
13715 /* Get name of field. */
13716 fieldname = dwarf2_name (die, cu);
13717 if (fieldname == NULL)
13718 return;
13719
13720 attr = dwarf2_attr (die, DW_AT_const_value, cu);
13721 if (attr
13722 /* Only create a symbol if this is an external value.
13723 new_symbol checks this and puts the value in the global symbol
13724 table, which we want. If it is not external, new_symbol
13725 will try to put the value in cu->list_in_scope which is wrong. */
13726 && dwarf2_flag_true_p (die, DW_AT_external, cu))
13727 {
13728 /* A static const member, not much different than an enum as far as
13729 we're concerned, except that we can support more types. */
13730 new_symbol (die, NULL, cu);
13731 }
13732
13733 /* Get physical name. */
13734 physname = dwarf2_physname (fieldname, die, cu);
13735
13736 /* The name is already allocated along with this objfile, so we don't
13737 need to duplicate it for the type. */
13738 fp->set_loc_physname (physname ? physname : "");
13739 fp->set_type (die_type (die, cu));
13740 fp->set_name (fieldname);
13741 }
13742 else if (die->tag == DW_TAG_inheritance)
13743 {
13744 /* C++ base class field. */
13745 handle_member_location (die, cu, fp);
13746 FIELD_BITSIZE (*fp) = 0;
13747 fp->set_type (die_type (die, cu));
13748 fp->set_name (fp->type ()->name ());
13749 }
13750 else
13751 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13752 }
13753
13754 /* Can the type given by DIE define another type? */
13755
13756 static bool
13757 type_can_define_types (const struct die_info *die)
13758 {
13759 switch (die->tag)
13760 {
13761 case DW_TAG_typedef:
13762 case DW_TAG_class_type:
13763 case DW_TAG_structure_type:
13764 case DW_TAG_union_type:
13765 case DW_TAG_enumeration_type:
13766 return true;
13767
13768 default:
13769 return false;
13770 }
13771 }
13772
13773 /* Add a type definition defined in the scope of the FIP's class. */
13774
13775 static void
13776 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
13777 struct dwarf2_cu *cu)
13778 {
13779 struct decl_field fp;
13780 memset (&fp, 0, sizeof (fp));
13781
13782 gdb_assert (type_can_define_types (die));
13783
13784 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13785 fp.name = dwarf2_name (die, cu);
13786 fp.type = read_type_die (die, cu);
13787
13788 /* Save accessibility. */
13789 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
13790 switch (accessibility)
13791 {
13792 case DW_ACCESS_public:
13793 /* The assumed value if neither private nor protected. */
13794 break;
13795 case DW_ACCESS_private:
13796 fp.is_private = 1;
13797 break;
13798 case DW_ACCESS_protected:
13799 fp.is_protected = 1;
13800 break;
13801 }
13802
13803 if (die->tag == DW_TAG_typedef)
13804 fip->typedef_field_list.push_back (fp);
13805 else
13806 fip->nested_types_list.push_back (fp);
13807 }
13808
13809 /* A convenience typedef that's used when finding the discriminant
13810 field for a variant part. */
13811 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
13812 offset_map_type;
13813
13814 /* Compute the discriminant range for a given variant. OBSTACK is
13815 where the results will be stored. VARIANT is the variant to
13816 process. IS_UNSIGNED indicates whether the discriminant is signed
13817 or unsigned. */
13818
13819 static const gdb::array_view<discriminant_range>
13820 convert_variant_range (struct obstack *obstack, const variant_field &variant,
13821 bool is_unsigned)
13822 {
13823 std::vector<discriminant_range> ranges;
13824
13825 if (variant.default_branch)
13826 return {};
13827
13828 if (variant.discr_list_data == nullptr)
13829 {
13830 discriminant_range r
13831 = {variant.discriminant_value, variant.discriminant_value};
13832 ranges.push_back (r);
13833 }
13834 else
13835 {
13836 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
13837 variant.discr_list_data->size);
13838 while (!data.empty ())
13839 {
13840 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
13841 {
13842 complaint (_("invalid discriminant marker: %d"), data[0]);
13843 break;
13844 }
13845 bool is_range = data[0] == DW_DSC_range;
13846 data = data.slice (1);
13847
13848 ULONGEST low, high;
13849 unsigned int bytes_read;
13850
13851 if (data.empty ())
13852 {
13853 complaint (_("DW_AT_discr_list missing low value"));
13854 break;
13855 }
13856 if (is_unsigned)
13857 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
13858 else
13859 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
13860 &bytes_read);
13861 data = data.slice (bytes_read);
13862
13863 if (is_range)
13864 {
13865 if (data.empty ())
13866 {
13867 complaint (_("DW_AT_discr_list missing high value"));
13868 break;
13869 }
13870 if (is_unsigned)
13871 high = read_unsigned_leb128 (nullptr, data.data (),
13872 &bytes_read);
13873 else
13874 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
13875 &bytes_read);
13876 data = data.slice (bytes_read);
13877 }
13878 else
13879 high = low;
13880
13881 ranges.push_back ({ low, high });
13882 }
13883 }
13884
13885 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
13886 ranges.size ());
13887 std::copy (ranges.begin (), ranges.end (), result);
13888 return gdb::array_view<discriminant_range> (result, ranges.size ());
13889 }
13890
13891 static const gdb::array_view<variant_part> create_variant_parts
13892 (struct obstack *obstack,
13893 const offset_map_type &offset_map,
13894 struct field_info *fi,
13895 const std::vector<variant_part_builder> &variant_parts);
13896
13897 /* Fill in a "struct variant" for a given variant field. RESULT is
13898 the variant to fill in. OBSTACK is where any needed allocations
13899 will be done. OFFSET_MAP holds the mapping from section offsets to
13900 fields for the type. FI describes the fields of the type we're
13901 processing. FIELD is the variant field we're converting. */
13902
13903 static void
13904 create_one_variant (variant &result, struct obstack *obstack,
13905 const offset_map_type &offset_map,
13906 struct field_info *fi, const variant_field &field)
13907 {
13908 result.discriminants = convert_variant_range (obstack, field, false);
13909 result.first_field = field.first_field + fi->baseclasses.size ();
13910 result.last_field = field.last_field + fi->baseclasses.size ();
13911 result.parts = create_variant_parts (obstack, offset_map, fi,
13912 field.variant_parts);
13913 }
13914
13915 /* Fill in a "struct variant_part" for a given variant part. RESULT
13916 is the variant part to fill in. OBSTACK is where any needed
13917 allocations will be done. OFFSET_MAP holds the mapping from
13918 section offsets to fields for the type. FI describes the fields of
13919 the type we're processing. BUILDER is the variant part to be
13920 converted. */
13921
13922 static void
13923 create_one_variant_part (variant_part &result,
13924 struct obstack *obstack,
13925 const offset_map_type &offset_map,
13926 struct field_info *fi,
13927 const variant_part_builder &builder)
13928 {
13929 auto iter = offset_map.find (builder.discriminant_offset);
13930 if (iter == offset_map.end ())
13931 {
13932 result.discriminant_index = -1;
13933 /* Doesn't matter. */
13934 result.is_unsigned = false;
13935 }
13936 else
13937 {
13938 result.discriminant_index = iter->second;
13939 result.is_unsigned
13940 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
13941 }
13942
13943 size_t n = builder.variants.size ();
13944 variant *output = new (obstack) variant[n];
13945 for (size_t i = 0; i < n; ++i)
13946 create_one_variant (output[i], obstack, offset_map, fi,
13947 builder.variants[i]);
13948
13949 result.variants = gdb::array_view<variant> (output, n);
13950 }
13951
13952 /* Create a vector of variant parts that can be attached to a type.
13953 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13954 holds the mapping from section offsets to fields for the type. FI
13955 describes the fields of the type we're processing. VARIANT_PARTS
13956 is the vector to convert. */
13957
13958 static const gdb::array_view<variant_part>
13959 create_variant_parts (struct obstack *obstack,
13960 const offset_map_type &offset_map,
13961 struct field_info *fi,
13962 const std::vector<variant_part_builder> &variant_parts)
13963 {
13964 if (variant_parts.empty ())
13965 return {};
13966
13967 size_t n = variant_parts.size ();
13968 variant_part *result = new (obstack) variant_part[n];
13969 for (size_t i = 0; i < n; ++i)
13970 create_one_variant_part (result[i], obstack, offset_map, fi,
13971 variant_parts[i]);
13972
13973 return gdb::array_view<variant_part> (result, n);
13974 }
13975
13976 /* Compute the variant part vector for FIP, attaching it to TYPE when
13977 done. */
13978
13979 static void
13980 add_variant_property (struct field_info *fip, struct type *type,
13981 struct dwarf2_cu *cu)
13982 {
13983 /* Map section offsets of fields to their field index. Note the
13984 field index here does not take the number of baseclasses into
13985 account. */
13986 offset_map_type offset_map;
13987 for (int i = 0; i < fip->fields.size (); ++i)
13988 offset_map[fip->fields[i].offset] = i;
13989
13990 struct objfile *objfile = cu->per_objfile->objfile;
13991 gdb::array_view<const variant_part> parts
13992 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
13993 fip->variant_parts);
13994
13995 struct dynamic_prop prop;
13996 prop.set_variant_parts ((gdb::array_view<variant_part> *)
13997 obstack_copy (&objfile->objfile_obstack, &parts,
13998 sizeof (parts)));
13999
14000 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14001 }
14002
14003 /* Create the vector of fields, and attach it to the type. */
14004
14005 static void
14006 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14007 struct dwarf2_cu *cu)
14008 {
14009 int nfields = fip->nfields ();
14010
14011 /* Record the field count, allocate space for the array of fields,
14012 and create blank accessibility bitfields if necessary. */
14013 type->set_num_fields (nfields);
14014 type->set_fields
14015 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14016
14017 if (fip->non_public_fields && cu->lang () != language_ada)
14018 {
14019 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14020
14021 TYPE_FIELD_PRIVATE_BITS (type) =
14022 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14023 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14024
14025 TYPE_FIELD_PROTECTED_BITS (type) =
14026 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14027 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14028
14029 TYPE_FIELD_IGNORE_BITS (type) =
14030 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14031 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14032 }
14033
14034 /* If the type has baseclasses, allocate and clear a bit vector for
14035 TYPE_FIELD_VIRTUAL_BITS. */
14036 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
14037 {
14038 int num_bytes = B_BYTES (fip->baseclasses.size ());
14039 unsigned char *pointer;
14040
14041 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14042 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14043 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14044 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14045 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14046 }
14047
14048 if (!fip->variant_parts.empty ())
14049 add_variant_property (fip, type, cu);
14050
14051 /* Copy the saved-up fields into the field vector. */
14052 for (int i = 0; i < nfields; ++i)
14053 {
14054 struct nextfield &field
14055 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14056 : fip->fields[i - fip->baseclasses.size ()]);
14057
14058 type->field (i) = field.field;
14059 switch (field.accessibility)
14060 {
14061 case DW_ACCESS_private:
14062 if (cu->lang () != language_ada)
14063 SET_TYPE_FIELD_PRIVATE (type, i);
14064 break;
14065
14066 case DW_ACCESS_protected:
14067 if (cu->lang () != language_ada)
14068 SET_TYPE_FIELD_PROTECTED (type, i);
14069 break;
14070
14071 case DW_ACCESS_public:
14072 break;
14073
14074 default:
14075 /* Unknown accessibility. Complain and treat it as public. */
14076 {
14077 complaint (_("unsupported accessibility %d"),
14078 field.accessibility);
14079 }
14080 break;
14081 }
14082 if (i < fip->baseclasses.size ())
14083 {
14084 switch (field.virtuality)
14085 {
14086 case DW_VIRTUALITY_virtual:
14087 case DW_VIRTUALITY_pure_virtual:
14088 if (cu->lang () == language_ada)
14089 error (_("unexpected virtuality in component of Ada type"));
14090 SET_TYPE_FIELD_VIRTUAL (type, i);
14091 break;
14092 }
14093 }
14094 }
14095 }
14096
14097 /* Return true if this member function is a constructor, false
14098 otherwise. */
14099
14100 static int
14101 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14102 {
14103 const char *fieldname;
14104 const char *type_name;
14105 int len;
14106
14107 if (die->parent == NULL)
14108 return 0;
14109
14110 if (die->parent->tag != DW_TAG_structure_type
14111 && die->parent->tag != DW_TAG_union_type
14112 && die->parent->tag != DW_TAG_class_type)
14113 return 0;
14114
14115 fieldname = dwarf2_name (die, cu);
14116 type_name = dwarf2_name (die->parent, cu);
14117 if (fieldname == NULL || type_name == NULL)
14118 return 0;
14119
14120 len = strlen (fieldname);
14121 return (strncmp (fieldname, type_name, len) == 0
14122 && (type_name[len] == '\0' || type_name[len] == '<'));
14123 }
14124
14125 /* Add a member function to the proper fieldlist. */
14126
14127 static void
14128 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14129 struct type *type, struct dwarf2_cu *cu)
14130 {
14131 struct objfile *objfile = cu->per_objfile->objfile;
14132 struct attribute *attr;
14133 int i;
14134 struct fnfieldlist *flp = nullptr;
14135 struct fn_field *fnp;
14136 const char *fieldname;
14137 struct type *this_type;
14138
14139 if (cu->lang () == language_ada)
14140 error (_("unexpected member function in Ada type"));
14141
14142 /* Get name of member function. */
14143 fieldname = dwarf2_name (die, cu);
14144 if (fieldname == NULL)
14145 return;
14146
14147 /* Look up member function name in fieldlist. */
14148 for (i = 0; i < fip->fnfieldlists.size (); i++)
14149 {
14150 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14151 {
14152 flp = &fip->fnfieldlists[i];
14153 break;
14154 }
14155 }
14156
14157 /* Create a new fnfieldlist if necessary. */
14158 if (flp == nullptr)
14159 {
14160 fip->fnfieldlists.emplace_back ();
14161 flp = &fip->fnfieldlists.back ();
14162 flp->name = fieldname;
14163 i = fip->fnfieldlists.size () - 1;
14164 }
14165
14166 /* Create a new member function field and add it to the vector of
14167 fnfieldlists. */
14168 flp->fnfields.emplace_back ();
14169 fnp = &flp->fnfields.back ();
14170
14171 /* Delay processing of the physname until later. */
14172 if (cu->lang () == language_cplus)
14173 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14174 die, cu);
14175 else
14176 {
14177 const char *physname = dwarf2_physname (fieldname, die, cu);
14178 fnp->physname = physname ? physname : "";
14179 }
14180
14181 fnp->type = alloc_type (objfile);
14182 this_type = read_type_die (die, cu);
14183 if (this_type && this_type->code () == TYPE_CODE_FUNC)
14184 {
14185 int nparams = this_type->num_fields ();
14186
14187 /* TYPE is the domain of this method, and THIS_TYPE is the type
14188 of the method itself (TYPE_CODE_METHOD). */
14189 smash_to_method_type (fnp->type, type,
14190 this_type->target_type (),
14191 this_type->fields (),
14192 this_type->num_fields (),
14193 this_type->has_varargs ());
14194
14195 /* Handle static member functions.
14196 Dwarf2 has no clean way to discern C++ static and non-static
14197 member functions. G++ helps GDB by marking the first
14198 parameter for non-static member functions (which is the this
14199 pointer) as artificial. We obtain this information from
14200 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14201 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14202 fnp->voffset = VOFFSET_STATIC;
14203 }
14204 else
14205 complaint (_("member function type missing for '%s'"),
14206 dwarf2_full_name (fieldname, die, cu));
14207
14208 /* Get fcontext from DW_AT_containing_type if present. */
14209 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14210 fnp->fcontext = die_containing_type (die, cu);
14211
14212 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14213 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14214
14215 /* Get accessibility. */
14216 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14217 switch (accessibility)
14218 {
14219 case DW_ACCESS_private:
14220 fnp->is_private = 1;
14221 break;
14222 case DW_ACCESS_protected:
14223 fnp->is_protected = 1;
14224 break;
14225 }
14226
14227 /* Check for artificial methods. */
14228 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14229 if (attr && attr->as_boolean ())
14230 fnp->is_artificial = 1;
14231
14232 /* Check for defaulted methods. */
14233 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14234 if (attr != nullptr)
14235 fnp->defaulted = attr->defaulted ();
14236
14237 /* Check for deleted methods. */
14238 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14239 if (attr != nullptr && attr->as_boolean ())
14240 fnp->is_deleted = 1;
14241
14242 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14243
14244 /* Get index in virtual function table if it is a virtual member
14245 function. For older versions of GCC, this is an offset in the
14246 appropriate virtual table, as specified by DW_AT_containing_type.
14247 For everyone else, it is an expression to be evaluated relative
14248 to the object address. */
14249
14250 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14251 if (attr != nullptr)
14252 {
14253 if (attr->form_is_block () && attr->as_block ()->size > 0)
14254 {
14255 struct dwarf_block *block = attr->as_block ();
14256
14257 if (block->data[0] == DW_OP_constu)
14258 {
14259 /* Old-style GCC. */
14260 fnp->voffset = decode_locdesc (block, cu) + 2;
14261 }
14262 else if (block->data[0] == DW_OP_deref
14263 || (block->size > 1
14264 && block->data[0] == DW_OP_deref_size
14265 && block->data[1] == cu->header.addr_size))
14266 {
14267 fnp->voffset = decode_locdesc (block, cu);
14268 if ((fnp->voffset % cu->header.addr_size) != 0)
14269 dwarf2_complex_location_expr_complaint ();
14270 else
14271 fnp->voffset /= cu->header.addr_size;
14272 fnp->voffset += 2;
14273 }
14274 else
14275 dwarf2_complex_location_expr_complaint ();
14276
14277 if (!fnp->fcontext)
14278 {
14279 /* If there is no `this' field and no DW_AT_containing_type,
14280 we cannot actually find a base class context for the
14281 vtable! */
14282 if (this_type->num_fields () == 0
14283 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14284 {
14285 complaint (_("cannot determine context for virtual member "
14286 "function \"%s\" (offset %s)"),
14287 fieldname, sect_offset_str (die->sect_off));
14288 }
14289 else
14290 {
14291 fnp->fcontext = this_type->field (0).type ()->target_type ();
14292 }
14293 }
14294 }
14295 else if (attr->form_is_section_offset ())
14296 {
14297 dwarf2_complex_location_expr_complaint ();
14298 }
14299 else
14300 {
14301 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14302 fieldname);
14303 }
14304 }
14305 else
14306 {
14307 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14308 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
14309 {
14310 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14311 complaint (_("Member function \"%s\" (offset %s) is virtual "
14312 "but the vtable offset is not specified"),
14313 fieldname, sect_offset_str (die->sect_off));
14314 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14315 TYPE_CPLUS_DYNAMIC (type) = 1;
14316 }
14317 }
14318 }
14319
14320 /* Create the vector of member function fields, and attach it to the type. */
14321
14322 static void
14323 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14324 struct dwarf2_cu *cu)
14325 {
14326 if (cu->lang () == language_ada)
14327 error (_("unexpected member functions in Ada type"));
14328
14329 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14330 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14331 TYPE_ALLOC (type,
14332 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14333
14334 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14335 {
14336 struct fnfieldlist &nf = fip->fnfieldlists[i];
14337 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14338
14339 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14340 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14341 fn_flp->fn_fields = (struct fn_field *)
14342 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14343
14344 for (int k = 0; k < nf.fnfields.size (); ++k)
14345 fn_flp->fn_fields[k] = nf.fnfields[k];
14346 }
14347
14348 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14349 }
14350
14351 /* Returns non-zero if NAME is the name of a vtable member in CU's
14352 language, zero otherwise. */
14353 static int
14354 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14355 {
14356 static const char vptr[] = "_vptr";
14357
14358 /* Look for the C++ form of the vtable. */
14359 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14360 return 1;
14361
14362 return 0;
14363 }
14364
14365 /* GCC outputs unnamed structures that are really pointers to member
14366 functions, with the ABI-specified layout. If TYPE describes
14367 such a structure, smash it into a member function type.
14368
14369 GCC shouldn't do this; it should just output pointer to member DIEs.
14370 This is GCC PR debug/28767. */
14371
14372 static void
14373 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14374 {
14375 struct type *pfn_type, *self_type, *new_type;
14376
14377 /* Check for a structure with no name and two children. */
14378 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14379 return;
14380
14381 /* Check for __pfn and __delta members. */
14382 if (type->field (0).name () == NULL
14383 || strcmp (type->field (0).name (), "__pfn") != 0
14384 || type->field (1).name () == NULL
14385 || strcmp (type->field (1).name (), "__delta") != 0)
14386 return;
14387
14388 /* Find the type of the method. */
14389 pfn_type = type->field (0).type ();
14390 if (pfn_type == NULL
14391 || pfn_type->code () != TYPE_CODE_PTR
14392 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
14393 return;
14394
14395 /* Look for the "this" argument. */
14396 pfn_type = pfn_type->target_type ();
14397 if (pfn_type->num_fields () == 0
14398 /* || pfn_type->field (0).type () == NULL */
14399 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
14400 return;
14401
14402 self_type = pfn_type->field (0).type ()->target_type ();
14403 new_type = alloc_type (objfile);
14404 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
14405 pfn_type->fields (), pfn_type->num_fields (),
14406 pfn_type->has_varargs ());
14407 smash_to_methodptr_type (type, new_type);
14408 }
14409
14410 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14411 requires rewriting, then copy it and return the updated copy.
14412 Otherwise return nullptr. */
14413
14414 static struct type *
14415 rewrite_array_type (struct type *type)
14416 {
14417 if (type->code () != TYPE_CODE_ARRAY)
14418 return nullptr;
14419
14420 struct type *index_type = type->index_type ();
14421 range_bounds *current_bounds = index_type->bounds ();
14422
14423 /* Handle multi-dimensional arrays. */
14424 struct type *new_target = rewrite_array_type (type->target_type ());
14425 if (new_target == nullptr)
14426 {
14427 /* Maybe we don't need to rewrite this array. */
14428 if (current_bounds->low.kind () == PROP_CONST
14429 && current_bounds->high.kind () == PROP_CONST)
14430 return nullptr;
14431 }
14432
14433 /* Either the target type was rewritten, or the bounds have to be
14434 updated. Either way we want to copy the type and update
14435 everything. */
14436 struct type *copy = copy_type (type);
14437 int nfields = copy->num_fields ();
14438 field *new_fields
14439 = ((struct field *) TYPE_ZALLOC (copy,
14440 nfields * sizeof (struct field)));
14441 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
14442 copy->set_fields (new_fields);
14443 if (new_target != nullptr)
14444 copy->set_target_type (new_target);
14445
14446 struct type *index_copy = copy_type (index_type);
14447 range_bounds *bounds
14448 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
14449 sizeof (range_bounds));
14450 *bounds = *current_bounds;
14451 bounds->low.set_const_val (1);
14452 bounds->high.set_const_val (0);
14453 index_copy->set_bounds (bounds);
14454 copy->set_index_type (index_copy);
14455
14456 return copy;
14457 }
14458
14459 /* While some versions of GCC will generate complicated DWARF for an
14460 array (see quirk_ada_thick_pointer), more recent versions were
14461 modified to emit an explicit thick pointer structure. However, in
14462 this case, the array still has DWARF expressions for its ranges,
14463 and these must be ignored. */
14464
14465 static void
14466 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
14467 struct type *type)
14468 {
14469 gdb_assert (cu->lang () == language_ada);
14470
14471 /* Check for a structure with two children. */
14472 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
14473 return;
14474
14475 /* Check for P_ARRAY and P_BOUNDS members. */
14476 if (type->field (0).name () == NULL
14477 || strcmp (type->field (0).name (), "P_ARRAY") != 0
14478 || type->field (1).name () == NULL
14479 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
14480 return;
14481
14482 /* Make sure we're looking at a pointer to an array. */
14483 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
14484 return;
14485
14486 /* The Ada code already knows how to handle these types, so all that
14487 we need to do is turn the bounds into static bounds. However, we
14488 don't want to rewrite existing array or index types in-place,
14489 because those may be referenced in other contexts where this
14490 rewriting is undesirable. */
14491 struct type *new_ary_type
14492 = rewrite_array_type (type->field (0).type ()->target_type ());
14493 if (new_ary_type != nullptr)
14494 type->field (0).set_type (lookup_pointer_type (new_ary_type));
14495 }
14496
14497 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14498 appropriate error checking and issuing complaints if there is a
14499 problem. */
14500
14501 static ULONGEST
14502 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14503 {
14504 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14505
14506 if (attr == nullptr)
14507 return 0;
14508
14509 if (!attr->form_is_constant ())
14510 {
14511 complaint (_("DW_AT_alignment must have constant form"
14512 " - DIE at %s [in module %s]"),
14513 sect_offset_str (die->sect_off),
14514 objfile_name (cu->per_objfile->objfile));
14515 return 0;
14516 }
14517
14518 LONGEST val = attr->constant_value (0);
14519 if (val < 0)
14520 {
14521 complaint (_("DW_AT_alignment value must not be negative"
14522 " - DIE at %s [in module %s]"),
14523 sect_offset_str (die->sect_off),
14524 objfile_name (cu->per_objfile->objfile));
14525 return 0;
14526 }
14527 ULONGEST align = val;
14528
14529 if (align == 0)
14530 {
14531 complaint (_("DW_AT_alignment value must not be zero"
14532 " - DIE at %s [in module %s]"),
14533 sect_offset_str (die->sect_off),
14534 objfile_name (cu->per_objfile->objfile));
14535 return 0;
14536 }
14537 if ((align & (align - 1)) != 0)
14538 {
14539 complaint (_("DW_AT_alignment value must be a power of 2"
14540 " - DIE at %s [in module %s]"),
14541 sect_offset_str (die->sect_off),
14542 objfile_name (cu->per_objfile->objfile));
14543 return 0;
14544 }
14545
14546 return align;
14547 }
14548
14549 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14550 the alignment for TYPE. */
14551
14552 static void
14553 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14554 struct type *type)
14555 {
14556 if (!set_type_align (type, get_alignment (cu, die)))
14557 complaint (_("DW_AT_alignment value too large"
14558 " - DIE at %s [in module %s]"),
14559 sect_offset_str (die->sect_off),
14560 objfile_name (cu->per_objfile->objfile));
14561 }
14562
14563 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14564 constant for a type, according to DWARF5 spec, Table 5.5. */
14565
14566 static bool
14567 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14568 {
14569 switch (value)
14570 {
14571 case DW_CC_normal:
14572 case DW_CC_pass_by_reference:
14573 case DW_CC_pass_by_value:
14574 return true;
14575
14576 default:
14577 complaint (_("unrecognized DW_AT_calling_convention value "
14578 "(%s) for a type"), pulongest (value));
14579 return false;
14580 }
14581 }
14582
14583 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14584 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14585 also according to GNU-specific values (see include/dwarf2.h). */
14586
14587 static bool
14588 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14589 {
14590 switch (value)
14591 {
14592 case DW_CC_normal:
14593 case DW_CC_program:
14594 case DW_CC_nocall:
14595 return true;
14596
14597 case DW_CC_GNU_renesas_sh:
14598 case DW_CC_GNU_borland_fastcall_i386:
14599 case DW_CC_GDB_IBM_OpenCL:
14600 return true;
14601
14602 default:
14603 complaint (_("unrecognized DW_AT_calling_convention value "
14604 "(%s) for a subroutine"), pulongest (value));
14605 return false;
14606 }
14607 }
14608
14609 /* Called when we find the DIE that starts a structure or union scope
14610 (definition) to create a type for the structure or union. Fill in
14611 the type's name and general properties; the members will not be
14612 processed until process_structure_scope. A symbol table entry for
14613 the type will also not be done until process_structure_scope (assuming
14614 the type has a name).
14615
14616 NOTE: we need to call these functions regardless of whether or not the
14617 DIE has a DW_AT_name attribute, since it might be an anonymous
14618 structure or union. This gets the type entered into our set of
14619 user defined types. */
14620
14621 static struct type *
14622 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14623 {
14624 struct objfile *objfile = cu->per_objfile->objfile;
14625 struct type *type;
14626 struct attribute *attr;
14627 const char *name;
14628
14629 /* If the definition of this type lives in .debug_types, read that type.
14630 Don't follow DW_AT_specification though, that will take us back up
14631 the chain and we want to go down. */
14632 attr = die->attr (DW_AT_signature);
14633 if (attr != nullptr)
14634 {
14635 type = get_DW_AT_signature_type (die, attr, cu);
14636
14637 /* The type's CU may not be the same as CU.
14638 Ensure TYPE is recorded with CU in die_type_hash. */
14639 return set_die_type (die, type, cu);
14640 }
14641
14642 type = alloc_type (objfile);
14643 INIT_CPLUS_SPECIFIC (type);
14644
14645 name = dwarf2_name (die, cu);
14646 if (name != NULL)
14647 {
14648 if (cu->lang () == language_cplus
14649 || cu->lang () == language_d
14650 || cu->lang () == language_rust)
14651 {
14652 const char *full_name = dwarf2_full_name (name, die, cu);
14653
14654 /* dwarf2_full_name might have already finished building the DIE's
14655 type. If so, there is no need to continue. */
14656 if (get_die_type (die, cu) != NULL)
14657 return get_die_type (die, cu);
14658
14659 type->set_name (full_name);
14660 }
14661 else
14662 {
14663 /* The name is already allocated along with this objfile, so
14664 we don't need to duplicate it for the type. */
14665 type->set_name (name);
14666 }
14667 }
14668
14669 if (die->tag == DW_TAG_structure_type)
14670 {
14671 type->set_code (TYPE_CODE_STRUCT);
14672 }
14673 else if (die->tag == DW_TAG_union_type)
14674 {
14675 type->set_code (TYPE_CODE_UNION);
14676 }
14677 else if (die->tag == DW_TAG_namelist)
14678 {
14679 type->set_code (TYPE_CODE_NAMELIST);
14680 }
14681 else
14682 {
14683 type->set_code (TYPE_CODE_STRUCT);
14684 }
14685
14686 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
14687 type->set_is_declared_class (true);
14688
14689 /* Store the calling convention in the type if it's available in
14690 the die. Otherwise the calling convention remains set to
14691 the default value DW_CC_normal. */
14692 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14693 if (attr != nullptr
14694 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
14695 {
14696 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14697 TYPE_CPLUS_CALLING_CONVENTION (type)
14698 = (enum dwarf_calling_convention) (attr->constant_value (0));
14699 }
14700
14701 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14702 if (attr != nullptr)
14703 {
14704 if (attr->form_is_constant ())
14705 type->set_length (attr->constant_value (0));
14706 else
14707 {
14708 struct dynamic_prop prop;
14709 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
14710 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
14711
14712 type->set_length (0);
14713 }
14714 }
14715 else
14716 type->set_length (0);
14717
14718 maybe_set_alignment (cu, die, type);
14719
14720 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
14721 {
14722 /* ICC<14 does not output the required DW_AT_declaration on
14723 incomplete types, but gives them a size of zero. */
14724 type->set_is_stub (true);
14725 }
14726 else
14727 type->set_stub_is_supported (true);
14728
14729 if (die_is_declaration (die, cu))
14730 type->set_is_stub (true);
14731 else if (attr == NULL && die->child == NULL
14732 && producer_is_realview (cu->producer))
14733 /* RealView does not output the required DW_AT_declaration
14734 on incomplete types. */
14735 type->set_is_stub (true);
14736
14737 /* We need to add the type field to the die immediately so we don't
14738 infinitely recurse when dealing with pointers to the structure
14739 type within the structure itself. */
14740 set_die_type (die, type, cu);
14741
14742 /* set_die_type should be already done. */
14743 set_descriptive_type (type, die, cu);
14744
14745 return type;
14746 }
14747
14748 static void handle_struct_member_die
14749 (struct die_info *child_die,
14750 struct type *type,
14751 struct field_info *fi,
14752 std::vector<struct symbol *> *template_args,
14753 struct dwarf2_cu *cu);
14754
14755 /* A helper for handle_struct_member_die that handles
14756 DW_TAG_variant_part. */
14757
14758 static void
14759 handle_variant_part (struct die_info *die, struct type *type,
14760 struct field_info *fi,
14761 std::vector<struct symbol *> *template_args,
14762 struct dwarf2_cu *cu)
14763 {
14764 variant_part_builder *new_part;
14765 if (fi->current_variant_part == nullptr)
14766 {
14767 fi->variant_parts.emplace_back ();
14768 new_part = &fi->variant_parts.back ();
14769 }
14770 else if (!fi->current_variant_part->processing_variant)
14771 {
14772 complaint (_("nested DW_TAG_variant_part seen "
14773 "- DIE at %s [in module %s]"),
14774 sect_offset_str (die->sect_off),
14775 objfile_name (cu->per_objfile->objfile));
14776 return;
14777 }
14778 else
14779 {
14780 variant_field &current = fi->current_variant_part->variants.back ();
14781 current.variant_parts.emplace_back ();
14782 new_part = &current.variant_parts.back ();
14783 }
14784
14785 /* When we recurse, we want callees to add to this new variant
14786 part. */
14787 scoped_restore save_current_variant_part
14788 = make_scoped_restore (&fi->current_variant_part, new_part);
14789
14790 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
14791 if (discr == NULL)
14792 {
14793 /* It's a univariant form, an extension we support. */
14794 }
14795 else if (discr->form_is_ref ())
14796 {
14797 struct dwarf2_cu *target_cu = cu;
14798 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
14799
14800 new_part->discriminant_offset = target_die->sect_off;
14801 }
14802 else
14803 {
14804 complaint (_("DW_AT_discr does not have DIE reference form"
14805 " - DIE at %s [in module %s]"),
14806 sect_offset_str (die->sect_off),
14807 objfile_name (cu->per_objfile->objfile));
14808 }
14809
14810 for (die_info *child_die = die->child;
14811 child_die != NULL;
14812 child_die = child_die->sibling)
14813 handle_struct_member_die (child_die, type, fi, template_args, cu);
14814 }
14815
14816 /* A helper for handle_struct_member_die that handles
14817 DW_TAG_variant. */
14818
14819 static void
14820 handle_variant (struct die_info *die, struct type *type,
14821 struct field_info *fi,
14822 std::vector<struct symbol *> *template_args,
14823 struct dwarf2_cu *cu)
14824 {
14825 if (fi->current_variant_part == nullptr)
14826 {
14827 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14828 "- DIE at %s [in module %s]"),
14829 sect_offset_str (die->sect_off),
14830 objfile_name (cu->per_objfile->objfile));
14831 return;
14832 }
14833 if (fi->current_variant_part->processing_variant)
14834 {
14835 complaint (_("nested DW_TAG_variant seen "
14836 "- DIE at %s [in module %s]"),
14837 sect_offset_str (die->sect_off),
14838 objfile_name (cu->per_objfile->objfile));
14839 return;
14840 }
14841
14842 scoped_restore save_processing_variant
14843 = make_scoped_restore (&fi->current_variant_part->processing_variant,
14844 true);
14845
14846 fi->current_variant_part->variants.emplace_back ();
14847 variant_field &variant = fi->current_variant_part->variants.back ();
14848 variant.first_field = fi->fields.size ();
14849
14850 /* In a variant we want to get the discriminant and also add a
14851 field for our sole member child. */
14852 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
14853 if (discr == nullptr || !discr->form_is_constant ())
14854 {
14855 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
14856 if (discr == nullptr || discr->as_block ()->size == 0)
14857 variant.default_branch = true;
14858 else
14859 variant.discr_list_data = discr->as_block ();
14860 }
14861 else
14862 variant.discriminant_value = discr->constant_value (0);
14863
14864 for (die_info *variant_child = die->child;
14865 variant_child != NULL;
14866 variant_child = variant_child->sibling)
14867 handle_struct_member_die (variant_child, type, fi, template_args, cu);
14868
14869 variant.last_field = fi->fields.size ();
14870 }
14871
14872 /* A helper for process_structure_scope that handles a single member
14873 DIE. */
14874
14875 static void
14876 handle_struct_member_die (struct die_info *child_die, struct type *type,
14877 struct field_info *fi,
14878 std::vector<struct symbol *> *template_args,
14879 struct dwarf2_cu *cu)
14880 {
14881 if (child_die->tag == DW_TAG_member
14882 || child_die->tag == DW_TAG_variable
14883 || child_die->tag == DW_TAG_namelist_item)
14884 {
14885 /* NOTE: carlton/2002-11-05: A C++ static data member
14886 should be a DW_TAG_member that is a declaration, but
14887 all versions of G++ as of this writing (so through at
14888 least 3.2.1) incorrectly generate DW_TAG_variable
14889 tags for them instead. */
14890 dwarf2_add_field (fi, child_die, cu);
14891 }
14892 else if (child_die->tag == DW_TAG_subprogram)
14893 {
14894 /* Rust doesn't have member functions in the C++ sense.
14895 However, it does emit ordinary functions as children
14896 of a struct DIE. */
14897 if (cu->lang () == language_rust)
14898 read_func_scope (child_die, cu);
14899 else
14900 {
14901 /* C++ member function. */
14902 dwarf2_add_member_fn (fi, child_die, type, cu);
14903 }
14904 }
14905 else if (child_die->tag == DW_TAG_inheritance)
14906 {
14907 /* C++ base class field. */
14908 dwarf2_add_field (fi, child_die, cu);
14909 }
14910 else if (type_can_define_types (child_die))
14911 dwarf2_add_type_defn (fi, child_die, cu);
14912 else if (child_die->tag == DW_TAG_template_type_param
14913 || child_die->tag == DW_TAG_template_value_param)
14914 {
14915 struct symbol *arg = new_symbol (child_die, NULL, cu);
14916
14917 if (arg != NULL)
14918 template_args->push_back (arg);
14919 }
14920 else if (child_die->tag == DW_TAG_variant_part)
14921 handle_variant_part (child_die, type, fi, template_args, cu);
14922 else if (child_die->tag == DW_TAG_variant)
14923 handle_variant (child_die, type, fi, template_args, cu);
14924 }
14925
14926 /* Finish creating a structure or union type, including filling in its
14927 members and creating a symbol for it. This function also handles Fortran
14928 namelist variables, their items or members and creating a symbol for
14929 them. */
14930
14931 static void
14932 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
14933 {
14934 struct objfile *objfile = cu->per_objfile->objfile;
14935 struct die_info *child_die;
14936 struct type *type;
14937
14938 type = get_die_type (die, cu);
14939 if (type == NULL)
14940 type = read_structure_type (die, cu);
14941
14942 bool has_template_parameters = false;
14943 if (die->child != NULL && ! die_is_declaration (die, cu))
14944 {
14945 struct field_info fi;
14946 std::vector<struct symbol *> template_args;
14947
14948 child_die = die->child;
14949
14950 while (child_die && child_die->tag)
14951 {
14952 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
14953 child_die = child_die->sibling;
14954 }
14955
14956 /* Attach template arguments to type. */
14957 if (!template_args.empty ())
14958 {
14959 has_template_parameters = true;
14960 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14961 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
14962 TYPE_TEMPLATE_ARGUMENTS (type)
14963 = XOBNEWVEC (&objfile->objfile_obstack,
14964 struct symbol *,
14965 TYPE_N_TEMPLATE_ARGUMENTS (type));
14966 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
14967 template_args.data (),
14968 (TYPE_N_TEMPLATE_ARGUMENTS (type)
14969 * sizeof (struct symbol *)));
14970 }
14971
14972 /* Attach fields and member functions to the type. */
14973 if (fi.nfields () > 0)
14974 dwarf2_attach_fields_to_type (&fi, type, cu);
14975 if (!fi.fnfieldlists.empty ())
14976 {
14977 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
14978
14979 /* Get the type which refers to the base class (possibly this
14980 class itself) which contains the vtable pointer for the current
14981 class from the DW_AT_containing_type attribute. This use of
14982 DW_AT_containing_type is a GNU extension. */
14983
14984 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14985 {
14986 struct type *t = die_containing_type (die, cu);
14987
14988 set_type_vptr_basetype (type, t);
14989 if (type == t)
14990 {
14991 int i;
14992
14993 /* Our own class provides vtbl ptr. */
14994 for (i = t->num_fields () - 1;
14995 i >= TYPE_N_BASECLASSES (t);
14996 --i)
14997 {
14998 const char *fieldname = t->field (i).name ();
14999
15000 if (is_vtable_name (fieldname, cu))
15001 {
15002 set_type_vptr_fieldno (type, i);
15003 break;
15004 }
15005 }
15006
15007 /* Complain if virtual function table field not found. */
15008 if (i < TYPE_N_BASECLASSES (t))
15009 complaint (_("virtual function table pointer "
15010 "not found when defining class '%s'"),
15011 type->name () ? type->name () : "");
15012 }
15013 else
15014 {
15015 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15016 }
15017 }
15018 else if (cu->producer
15019 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15020 {
15021 /* The IBM XLC compiler does not provide direct indication
15022 of the containing type, but the vtable pointer is
15023 always named __vfp. */
15024
15025 int i;
15026
15027 for (i = type->num_fields () - 1;
15028 i >= TYPE_N_BASECLASSES (type);
15029 --i)
15030 {
15031 if (strcmp (type->field (i).name (), "__vfp") == 0)
15032 {
15033 set_type_vptr_fieldno (type, i);
15034 set_type_vptr_basetype (type, type);
15035 break;
15036 }
15037 }
15038 }
15039 }
15040
15041 /* Copy fi.typedef_field_list linked list elements content into the
15042 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15043 if (!fi.typedef_field_list.empty ())
15044 {
15045 int count = fi.typedef_field_list.size ();
15046
15047 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15048 TYPE_TYPEDEF_FIELD_ARRAY (type)
15049 = ((struct decl_field *)
15050 TYPE_ALLOC (type,
15051 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15052 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15053
15054 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15055 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15056 }
15057
15058 /* Copy fi.nested_types_list linked list elements content into the
15059 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15060 if (!fi.nested_types_list.empty ()
15061 && cu->lang () != language_ada)
15062 {
15063 int count = fi.nested_types_list.size ();
15064
15065 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15066 TYPE_NESTED_TYPES_ARRAY (type)
15067 = ((struct decl_field *)
15068 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15069 TYPE_NESTED_TYPES_COUNT (type) = count;
15070
15071 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15072 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15073 }
15074 }
15075
15076 quirk_gcc_member_function_pointer (type, objfile);
15077 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
15078 cu->rust_unions.push_back (type);
15079 else if (cu->lang () == language_ada)
15080 quirk_ada_thick_pointer_struct (die, cu, type);
15081
15082 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15083 snapshots) has been known to create a die giving a declaration
15084 for a class that has, as a child, a die giving a definition for a
15085 nested class. So we have to process our children even if the
15086 current die is a declaration. Normally, of course, a declaration
15087 won't have any children at all. */
15088
15089 child_die = die->child;
15090
15091 while (child_die != NULL && child_die->tag)
15092 {
15093 if (child_die->tag == DW_TAG_member
15094 || child_die->tag == DW_TAG_variable
15095 || child_die->tag == DW_TAG_inheritance
15096 || child_die->tag == DW_TAG_template_value_param
15097 || child_die->tag == DW_TAG_template_type_param)
15098 {
15099 /* Do nothing. */
15100 }
15101 else
15102 process_die (child_die, cu);
15103
15104 child_die = child_die->sibling;
15105 }
15106
15107 /* Do not consider external references. According to the DWARF standard,
15108 these DIEs are identified by the fact that they have no byte_size
15109 attribute, and a declaration attribute. */
15110 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15111 || !die_is_declaration (die, cu)
15112 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15113 {
15114 struct symbol *sym = new_symbol (die, type, cu);
15115
15116 if (has_template_parameters)
15117 {
15118 struct symtab *symtab;
15119 if (sym != nullptr)
15120 symtab = sym->symtab ();
15121 else if (cu->line_header != nullptr)
15122 {
15123 /* Any related symtab will do. */
15124 symtab
15125 = cu->line_header->file_names ()[0].symtab;
15126 }
15127 else
15128 {
15129 symtab = nullptr;
15130 complaint (_("could not find suitable "
15131 "symtab for template parameter"
15132 " - DIE at %s [in module %s]"),
15133 sect_offset_str (die->sect_off),
15134 objfile_name (objfile));
15135 }
15136
15137 if (symtab != nullptr)
15138 {
15139 /* Make sure that the symtab is set on the new symbols.
15140 Even though they don't appear in this symtab directly,
15141 other parts of gdb assume that symbols do, and this is
15142 reasonably true. */
15143 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15144 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
15145 }
15146 }
15147 }
15148 }
15149
15150 /* Assuming DIE is an enumeration type, and TYPE is its associated
15151 type, update TYPE using some information only available in DIE's
15152 children. In particular, the fields are computed. */
15153
15154 static void
15155 update_enumeration_type_from_children (struct die_info *die,
15156 struct type *type,
15157 struct dwarf2_cu *cu)
15158 {
15159 struct die_info *child_die;
15160 int unsigned_enum = 1;
15161 int flag_enum = 1;
15162
15163 auto_obstack obstack;
15164 std::vector<struct field> fields;
15165
15166 for (child_die = die->child;
15167 child_die != NULL && child_die->tag;
15168 child_die = child_die->sibling)
15169 {
15170 struct attribute *attr;
15171 LONGEST value;
15172 const gdb_byte *bytes;
15173 struct dwarf2_locexpr_baton *baton;
15174 const char *name;
15175
15176 if (child_die->tag != DW_TAG_enumerator)
15177 continue;
15178
15179 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15180 if (attr == NULL)
15181 continue;
15182
15183 name = dwarf2_name (child_die, cu);
15184 if (name == NULL)
15185 name = "<anonymous enumerator>";
15186
15187 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15188 &value, &bytes, &baton);
15189 if (value < 0)
15190 {
15191 unsigned_enum = 0;
15192 flag_enum = 0;
15193 }
15194 else
15195 {
15196 if (count_one_bits_ll (value) >= 2)
15197 flag_enum = 0;
15198 }
15199
15200 fields.emplace_back ();
15201 struct field &field = fields.back ();
15202 field.set_name (dwarf2_physname (name, child_die, cu));
15203 field.set_loc_enumval (value);
15204 }
15205
15206 if (!fields.empty ())
15207 {
15208 type->set_num_fields (fields.size ());
15209 type->set_fields
15210 ((struct field *)
15211 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
15212 memcpy (type->fields (), fields.data (),
15213 sizeof (struct field) * fields.size ());
15214 }
15215
15216 if (unsigned_enum)
15217 type->set_is_unsigned (true);
15218
15219 if (flag_enum)
15220 type->set_is_flag_enum (true);
15221 }
15222
15223 /* Given a DW_AT_enumeration_type die, set its type. We do not
15224 complete the type's fields yet, or create any symbols. */
15225
15226 static struct type *
15227 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15228 {
15229 struct objfile *objfile = cu->per_objfile->objfile;
15230 struct type *type;
15231 struct attribute *attr;
15232 const char *name;
15233
15234 /* If the definition of this type lives in .debug_types, read that type.
15235 Don't follow DW_AT_specification though, that will take us back up
15236 the chain and we want to go down. */
15237 attr = die->attr (DW_AT_signature);
15238 if (attr != nullptr)
15239 {
15240 type = get_DW_AT_signature_type (die, attr, cu);
15241
15242 /* The type's CU may not be the same as CU.
15243 Ensure TYPE is recorded with CU in die_type_hash. */
15244 return set_die_type (die, type, cu);
15245 }
15246
15247 type = alloc_type (objfile);
15248
15249 type->set_code (TYPE_CODE_ENUM);
15250 name = dwarf2_full_name (NULL, die, cu);
15251 if (name != NULL)
15252 type->set_name (name);
15253
15254 attr = dwarf2_attr (die, DW_AT_type, cu);
15255 if (attr != NULL)
15256 {
15257 struct type *underlying_type = die_type (die, cu);
15258
15259 type->set_target_type (underlying_type);
15260 }
15261
15262 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15263 if (attr != nullptr)
15264 type->set_length (attr->constant_value (0));
15265 else
15266 type->set_length (0);
15267
15268 maybe_set_alignment (cu, die, type);
15269
15270 /* The enumeration DIE can be incomplete. In Ada, any type can be
15271 declared as private in the package spec, and then defined only
15272 inside the package body. Such types are known as Taft Amendment
15273 Types. When another package uses such a type, an incomplete DIE
15274 may be generated by the compiler. */
15275 if (die_is_declaration (die, cu))
15276 type->set_is_stub (true);
15277
15278 /* If this type has an underlying type that is not a stub, then we
15279 may use its attributes. We always use the "unsigned" attribute
15280 in this situation, because ordinarily we guess whether the type
15281 is unsigned -- but the guess can be wrong and the underlying type
15282 can tell us the reality. However, we defer to a local size
15283 attribute if one exists, because this lets the compiler override
15284 the underlying type if needed. */
15285 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
15286 {
15287 struct type *underlying_type = type->target_type ();
15288 underlying_type = check_typedef (underlying_type);
15289
15290 type->set_is_unsigned (underlying_type->is_unsigned ());
15291
15292 if (type->length () == 0)
15293 type->set_length (underlying_type->length ());
15294
15295 if (TYPE_RAW_ALIGN (type) == 0
15296 && TYPE_RAW_ALIGN (underlying_type) != 0)
15297 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15298 }
15299
15300 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
15301
15302 set_die_type (die, type, cu);
15303
15304 /* Finish the creation of this type by using the enum's children.
15305 Note that, as usual, this must come after set_die_type to avoid
15306 infinite recursion when trying to compute the names of the
15307 enumerators. */
15308 update_enumeration_type_from_children (die, type, cu);
15309
15310 return type;
15311 }
15312
15313 /* Given a pointer to a die which begins an enumeration, process all
15314 the dies that define the members of the enumeration, and create the
15315 symbol for the enumeration type.
15316
15317 NOTE: We reverse the order of the element list. */
15318
15319 static void
15320 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15321 {
15322 struct type *this_type;
15323
15324 this_type = get_die_type (die, cu);
15325 if (this_type == NULL)
15326 this_type = read_enumeration_type (die, cu);
15327
15328 if (die->child != NULL)
15329 {
15330 struct die_info *child_die;
15331 const char *name;
15332
15333 child_die = die->child;
15334 while (child_die && child_die->tag)
15335 {
15336 if (child_die->tag != DW_TAG_enumerator)
15337 {
15338 process_die (child_die, cu);
15339 }
15340 else
15341 {
15342 name = dwarf2_name (child_die, cu);
15343 if (name)
15344 new_symbol (child_die, this_type, cu);
15345 }
15346
15347 child_die = child_die->sibling;
15348 }
15349 }
15350
15351 /* If we are reading an enum from a .debug_types unit, and the enum
15352 is a declaration, and the enum is not the signatured type in the
15353 unit, then we do not want to add a symbol for it. Adding a
15354 symbol would in some cases obscure the true definition of the
15355 enum, giving users an incomplete type when the definition is
15356 actually available. Note that we do not want to do this for all
15357 enums which are just declarations, because C++0x allows forward
15358 enum declarations. */
15359 if (cu->per_cu->is_debug_types
15360 && die_is_declaration (die, cu))
15361 {
15362 struct signatured_type *sig_type;
15363
15364 sig_type = (struct signatured_type *) cu->per_cu;
15365 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15366 if (sig_type->type_offset_in_section != die->sect_off)
15367 return;
15368 }
15369
15370 new_symbol (die, this_type, cu);
15371 }
15372
15373 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15374 expression for an index type and finds the corresponding field
15375 offset in the hidden "P_BOUNDS" structure. Returns true on success
15376 and updates *FIELD, false if it fails to recognize an
15377 expression. */
15378
15379 static bool
15380 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
15381 int *bounds_offset, struct field *field,
15382 struct dwarf2_cu *cu)
15383 {
15384 struct attribute *attr = dwarf2_attr (die, name, cu);
15385 if (attr == nullptr || !attr->form_is_block ())
15386 return false;
15387
15388 const struct dwarf_block *block = attr->as_block ();
15389 const gdb_byte *start = block->data;
15390 const gdb_byte *end = block->data + block->size;
15391
15392 /* The expression to recognize generally looks like:
15393
15394 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15395 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15396
15397 However, the second "plus_uconst" may be missing:
15398
15399 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15400 DW_OP_deref_size: 4)
15401
15402 This happens when the field is at the start of the structure.
15403
15404 Also, the final deref may not be sized:
15405
15406 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15407 DW_OP_deref)
15408
15409 This happens when the size of the index type happens to be the
15410 same as the architecture's word size. This can occur with or
15411 without the second plus_uconst. */
15412
15413 if (end - start < 2)
15414 return false;
15415 if (*start++ != DW_OP_push_object_address)
15416 return false;
15417 if (*start++ != DW_OP_plus_uconst)
15418 return false;
15419
15420 uint64_t this_bound_off;
15421 start = gdb_read_uleb128 (start, end, &this_bound_off);
15422 if (start == nullptr || (int) this_bound_off != this_bound_off)
15423 return false;
15424 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15425 is consistent among all bounds. */
15426 if (*bounds_offset == -1)
15427 *bounds_offset = this_bound_off;
15428 else if (*bounds_offset != this_bound_off)
15429 return false;
15430
15431 if (start == end || *start++ != DW_OP_deref)
15432 return false;
15433
15434 int offset = 0;
15435 if (start ==end)
15436 return false;
15437 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
15438 {
15439 /* This means an offset of 0. */
15440 }
15441 else if (*start++ != DW_OP_plus_uconst)
15442 return false;
15443 else
15444 {
15445 /* The size is the parameter to DW_OP_plus_uconst. */
15446 uint64_t val;
15447 start = gdb_read_uleb128 (start, end, &val);
15448 if (start == nullptr)
15449 return false;
15450 if ((int) val != val)
15451 return false;
15452 offset = val;
15453 }
15454
15455 if (start == end)
15456 return false;
15457
15458 uint64_t size;
15459 if (*start == DW_OP_deref_size)
15460 {
15461 start = gdb_read_uleb128 (start + 1, end, &size);
15462 if (start == nullptr)
15463 return false;
15464 }
15465 else if (*start == DW_OP_deref)
15466 {
15467 size = cu->header.addr_size;
15468 ++start;
15469 }
15470 else
15471 return false;
15472
15473 field->set_loc_bitpos (8 * offset);
15474 if (size != field->type ()->length ())
15475 FIELD_BITSIZE (*field) = 8 * size;
15476
15477 return true;
15478 }
15479
15480 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15481 some kinds of Ada arrays:
15482
15483 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15484 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15485 <11e0> DW_AT_data_location: 2 byte block: 97 6
15486 (DW_OP_push_object_address; DW_OP_deref)
15487 <11e3> DW_AT_type : <0x1173>
15488 <11e7> DW_AT_sibling : <0x1201>
15489 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15490 <11ec> DW_AT_type : <0x1206>
15491 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15492 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15493 DW_OP_deref_size: 4)
15494 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15495 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15496 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15497
15498 This actually represents a "thick pointer", which is a structure
15499 with two elements: one that is a pointer to the array data, and one
15500 that is a pointer to another structure; this second structure holds
15501 the array bounds.
15502
15503 This returns a new type on success, or nullptr if this didn't
15504 recognize the type. */
15505
15506 static struct type *
15507 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
15508 struct type *type)
15509 {
15510 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
15511 /* So far we've only seen this with block form. */
15512 if (attr == nullptr || !attr->form_is_block ())
15513 return nullptr;
15514
15515 /* Note that this will fail if the structure layout is changed by
15516 the compiler. However, we have no good way to recognize some
15517 other layout, because we don't know what expression the compiler
15518 might choose to emit should this happen. */
15519 struct dwarf_block *blk = attr->as_block ();
15520 if (blk->size != 2
15521 || blk->data[0] != DW_OP_push_object_address
15522 || blk->data[1] != DW_OP_deref)
15523 return nullptr;
15524
15525 int bounds_offset = -1;
15526 int max_align = -1;
15527 std::vector<struct field> range_fields;
15528 for (struct die_info *child_die = die->child;
15529 child_die;
15530 child_die = child_die->sibling)
15531 {
15532 if (child_die->tag == DW_TAG_subrange_type)
15533 {
15534 struct type *underlying = read_subrange_index_type (child_die, cu);
15535
15536 int this_align = type_align (underlying);
15537 if (this_align > max_align)
15538 max_align = this_align;
15539
15540 range_fields.emplace_back ();
15541 range_fields.emplace_back ();
15542
15543 struct field &lower = range_fields[range_fields.size () - 2];
15544 struct field &upper = range_fields[range_fields.size () - 1];
15545
15546 lower.set_type (underlying);
15547 FIELD_ARTIFICIAL (lower) = 1;
15548
15549 upper.set_type (underlying);
15550 FIELD_ARTIFICIAL (upper) = 1;
15551
15552 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
15553 &bounds_offset, &lower, cu)
15554 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
15555 &bounds_offset, &upper, cu))
15556 return nullptr;
15557 }
15558 }
15559
15560 /* This shouldn't really happen, but double-check that we found
15561 where the bounds are stored. */
15562 if (bounds_offset == -1)
15563 return nullptr;
15564
15565 struct objfile *objfile = cu->per_objfile->objfile;
15566 for (int i = 0; i < range_fields.size (); i += 2)
15567 {
15568 char name[20];
15569
15570 /* Set the name of each field in the bounds. */
15571 xsnprintf (name, sizeof (name), "LB%d", i / 2);
15572 range_fields[i].set_name (objfile->intern (name));
15573 xsnprintf (name, sizeof (name), "UB%d", i / 2);
15574 range_fields[i + 1].set_name (objfile->intern (name));
15575 }
15576
15577 struct type *bounds = alloc_type (objfile);
15578 bounds->set_code (TYPE_CODE_STRUCT);
15579
15580 bounds->set_num_fields (range_fields.size ());
15581 bounds->set_fields
15582 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
15583 * sizeof (struct field))));
15584 memcpy (bounds->fields (), range_fields.data (),
15585 bounds->num_fields () * sizeof (struct field));
15586
15587 int last_fieldno = range_fields.size () - 1;
15588 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
15589 + bounds->field (last_fieldno).type ()->length ());
15590 bounds->set_length (align_up (bounds_size, max_align));
15591
15592 /* Rewrite the existing array type in place. Specifically, we
15593 remove any dynamic properties we might have read, and we replace
15594 the index types. */
15595 struct type *iter = type;
15596 for (int i = 0; i < range_fields.size (); i += 2)
15597 {
15598 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
15599 iter->main_type->dyn_prop_list = nullptr;
15600 iter->set_index_type
15601 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
15602 iter = iter->target_type ();
15603 }
15604
15605 struct type *result = alloc_type (objfile);
15606 result->set_code (TYPE_CODE_STRUCT);
15607
15608 result->set_num_fields (2);
15609 result->set_fields
15610 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
15611 * sizeof (struct field))));
15612
15613 /* The names are chosen to coincide with what the compiler does with
15614 -fgnat-encodings=all, which the Ada code in gdb already
15615 understands. */
15616 result->field (0).set_name ("P_ARRAY");
15617 result->field (0).set_type (lookup_pointer_type (type));
15618
15619 result->field (1).set_name ("P_BOUNDS");
15620 result->field (1).set_type (lookup_pointer_type (bounds));
15621 result->field (1).set_loc_bitpos (8 * bounds_offset);
15622
15623 result->set_name (type->name ());
15624 result->set_length (result->field (0).type ()->length ()
15625 + result->field (1).type ()->length ());
15626
15627 return result;
15628 }
15629
15630 /* Extract all information from a DW_TAG_array_type DIE and put it in
15631 the DIE's type field. For now, this only handles one dimensional
15632 arrays. */
15633
15634 static struct type *
15635 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15636 {
15637 struct objfile *objfile = cu->per_objfile->objfile;
15638 struct die_info *child_die;
15639 struct type *type;
15640 struct type *element_type, *range_type, *index_type;
15641 struct attribute *attr;
15642 const char *name;
15643 struct dynamic_prop *byte_stride_prop = NULL;
15644 unsigned int bit_stride = 0;
15645
15646 element_type = die_type (die, cu);
15647
15648 /* The die_type call above may have already set the type for this DIE. */
15649 type = get_die_type (die, cu);
15650 if (type)
15651 return type;
15652
15653 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15654 if (attr != NULL)
15655 {
15656 int stride_ok;
15657 struct type *prop_type = cu->addr_sized_int_type (false);
15658
15659 byte_stride_prop
15660 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15661 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15662 prop_type);
15663 if (!stride_ok)
15664 {
15665 complaint (_("unable to read array DW_AT_byte_stride "
15666 " - DIE at %s [in module %s]"),
15667 sect_offset_str (die->sect_off),
15668 objfile_name (cu->per_objfile->objfile));
15669 /* Ignore this attribute. We will likely not be able to print
15670 arrays of this type correctly, but there is little we can do
15671 to help if we cannot read the attribute's value. */
15672 byte_stride_prop = NULL;
15673 }
15674 }
15675
15676 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15677 if (attr != NULL)
15678 bit_stride = attr->constant_value (0);
15679
15680 /* Irix 6.2 native cc creates array types without children for
15681 arrays with unspecified length. */
15682 if (die->child == NULL)
15683 {
15684 index_type = objfile_type (objfile)->builtin_int;
15685 range_type = create_static_range_type (NULL, index_type, 0, -1);
15686 type = create_array_type_with_stride (NULL, element_type, range_type,
15687 byte_stride_prop, bit_stride);
15688 return set_die_type (die, type, cu);
15689 }
15690
15691 std::vector<struct type *> range_types;
15692 child_die = die->child;
15693 while (child_die && child_die->tag)
15694 {
15695 if (child_die->tag == DW_TAG_subrange_type
15696 || child_die->tag == DW_TAG_generic_subrange)
15697 {
15698 struct type *child_type = read_type_die (child_die, cu);
15699
15700 if (child_type != NULL)
15701 {
15702 /* The range type was succesfully read. Save it for the
15703 array type creation. */
15704 range_types.push_back (child_type);
15705 }
15706 }
15707 child_die = child_die->sibling;
15708 }
15709
15710 if (range_types.empty ())
15711 {
15712 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15713 sect_offset_str (die->sect_off),
15714 objfile_name (cu->per_objfile->objfile));
15715 return NULL;
15716 }
15717
15718 /* Dwarf2 dimensions are output from left to right, create the
15719 necessary array types in backwards order. */
15720
15721 type = element_type;
15722
15723 if (read_array_order (die, cu) == DW_ORD_col_major)
15724 {
15725 int i = 0;
15726
15727 while (i < range_types.size ())
15728 {
15729 type = create_array_type_with_stride (NULL, type, range_types[i++],
15730 byte_stride_prop, bit_stride);
15731 type->set_is_multi_dimensional (true);
15732 bit_stride = 0;
15733 byte_stride_prop = nullptr;
15734 }
15735 }
15736 else
15737 {
15738 size_t ndim = range_types.size ();
15739 while (ndim-- > 0)
15740 {
15741 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15742 byte_stride_prop, bit_stride);
15743 type->set_is_multi_dimensional (true);
15744 bit_stride = 0;
15745 byte_stride_prop = nullptr;
15746 }
15747 }
15748
15749 /* Clear the flag on the outermost array type. */
15750 type->set_is_multi_dimensional (false);
15751 gdb_assert (type != element_type);
15752
15753 /* Understand Dwarf2 support for vector types (like they occur on
15754 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15755 array type. This is not part of the Dwarf2/3 standard yet, but a
15756 custom vendor extension. The main difference between a regular
15757 array and the vector variant is that vectors are passed by value
15758 to functions. */
15759 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15760 if (attr != nullptr)
15761 make_vector_type (type);
15762
15763 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15764 implementation may choose to implement triple vectors using this
15765 attribute. */
15766 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15767 if (attr != nullptr && attr->form_is_unsigned ())
15768 {
15769 if (attr->as_unsigned () >= type->length ())
15770 type->set_length (attr->as_unsigned ());
15771 else
15772 complaint (_("DW_AT_byte_size for array type smaller "
15773 "than the total size of elements"));
15774 }
15775
15776 name = dwarf2_name (die, cu);
15777 if (name)
15778 type->set_name (name);
15779
15780 maybe_set_alignment (cu, die, type);
15781
15782 struct type *replacement_type = nullptr;
15783 if (cu->lang () == language_ada)
15784 {
15785 replacement_type = quirk_ada_thick_pointer (die, cu, type);
15786 if (replacement_type != nullptr)
15787 type = replacement_type;
15788 }
15789
15790 /* Install the type in the die. */
15791 set_die_type (die, type, cu, replacement_type != nullptr);
15792
15793 /* set_die_type should be already done. */
15794 set_descriptive_type (type, die, cu);
15795
15796 return type;
15797 }
15798
15799 static enum dwarf_array_dim_ordering
15800 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15801 {
15802 struct attribute *attr;
15803
15804 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15805
15806 if (attr != nullptr)
15807 {
15808 LONGEST val = attr->constant_value (-1);
15809 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
15810 return (enum dwarf_array_dim_ordering) val;
15811 }
15812
15813 /* GNU F77 is a special case, as at 08/2004 array type info is the
15814 opposite order to the dwarf2 specification, but data is still
15815 laid out as per normal fortran.
15816
15817 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15818 version checking. */
15819
15820 if (cu->lang () == language_fortran
15821 && cu->producer && strstr (cu->producer, "GNU F77"))
15822 {
15823 return DW_ORD_row_major;
15824 }
15825
15826 switch (cu->language_defn->array_ordering ())
15827 {
15828 case array_column_major:
15829 return DW_ORD_col_major;
15830 case array_row_major:
15831 default:
15832 return DW_ORD_row_major;
15833 };
15834 }
15835
15836 /* Extract all information from a DW_TAG_set_type DIE and put it in
15837 the DIE's type field. */
15838
15839 static struct type *
15840 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15841 {
15842 struct type *domain_type, *set_type;
15843 struct attribute *attr;
15844
15845 domain_type = die_type (die, cu);
15846
15847 /* The die_type call above may have already set the type for this DIE. */
15848 set_type = get_die_type (die, cu);
15849 if (set_type)
15850 return set_type;
15851
15852 set_type = create_set_type (NULL, domain_type);
15853
15854 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15855 if (attr != nullptr && attr->form_is_unsigned ())
15856 set_type->set_length (attr->as_unsigned ());
15857
15858 maybe_set_alignment (cu, die, set_type);
15859
15860 return set_die_type (die, set_type, cu);
15861 }
15862
15863 /* A helper for read_common_block that creates a locexpr baton.
15864 SYM is the symbol which we are marking as computed.
15865 COMMON_DIE is the DIE for the common block.
15866 COMMON_LOC is the location expression attribute for the common
15867 block itself.
15868 MEMBER_LOC is the location expression attribute for the particular
15869 member of the common block that we are processing.
15870 CU is the CU from which the above come. */
15871
15872 static void
15873 mark_common_block_symbol_computed (struct symbol *sym,
15874 struct die_info *common_die,
15875 struct attribute *common_loc,
15876 struct attribute *member_loc,
15877 struct dwarf2_cu *cu)
15878 {
15879 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15880 struct objfile *objfile = per_objfile->objfile;
15881 struct dwarf2_locexpr_baton *baton;
15882 gdb_byte *ptr;
15883 unsigned int cu_off;
15884 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
15885 LONGEST offset = 0;
15886
15887 gdb_assert (common_loc && member_loc);
15888 gdb_assert (common_loc->form_is_block ());
15889 gdb_assert (member_loc->form_is_block ()
15890 || member_loc->form_is_constant ());
15891
15892 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15893 baton->per_objfile = per_objfile;
15894 baton->per_cu = cu->per_cu;
15895 gdb_assert (baton->per_cu);
15896
15897 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15898
15899 if (member_loc->form_is_constant ())
15900 {
15901 offset = member_loc->constant_value (0);
15902 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15903 }
15904 else
15905 baton->size += member_loc->as_block ()->size;
15906
15907 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15908 baton->data = ptr;
15909
15910 *ptr++ = DW_OP_call4;
15911 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15912 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15913 ptr += 4;
15914
15915 if (member_loc->form_is_constant ())
15916 {
15917 *ptr++ = DW_OP_addr;
15918 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15919 ptr += cu->header.addr_size;
15920 }
15921 else
15922 {
15923 /* We have to copy the data here, because DW_OP_call4 will only
15924 use a DW_AT_location attribute. */
15925 struct dwarf_block *block = member_loc->as_block ();
15926 memcpy (ptr, block->data, block->size);
15927 ptr += block->size;
15928 }
15929
15930 *ptr++ = DW_OP_plus;
15931 gdb_assert (ptr - baton->data == baton->size);
15932
15933 SYMBOL_LOCATION_BATON (sym) = baton;
15934 sym->set_aclass_index (dwarf2_locexpr_index);
15935 }
15936
15937 /* Create appropriate locally-scoped variables for all the
15938 DW_TAG_common_block entries. Also create a struct common_block
15939 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15940 is used to separate the common blocks name namespace from regular
15941 variable names. */
15942
15943 static void
15944 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15945 {
15946 struct attribute *attr;
15947
15948 attr = dwarf2_attr (die, DW_AT_location, cu);
15949 if (attr != nullptr)
15950 {
15951 /* Support the .debug_loc offsets. */
15952 if (attr->form_is_block ())
15953 {
15954 /* Ok. */
15955 }
15956 else if (attr->form_is_section_offset ())
15957 {
15958 dwarf2_complex_location_expr_complaint ();
15959 attr = NULL;
15960 }
15961 else
15962 {
15963 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15964 "common block member");
15965 attr = NULL;
15966 }
15967 }
15968
15969 if (die->child != NULL)
15970 {
15971 struct objfile *objfile = cu->per_objfile->objfile;
15972 struct die_info *child_die;
15973 size_t n_entries = 0, size;
15974 struct common_block *common_block;
15975 struct symbol *sym;
15976
15977 for (child_die = die->child;
15978 child_die && child_die->tag;
15979 child_die = child_die->sibling)
15980 ++n_entries;
15981
15982 size = (sizeof (struct common_block)
15983 + (n_entries - 1) * sizeof (struct symbol *));
15984 common_block
15985 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15986 size);
15987 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15988 common_block->n_entries = 0;
15989
15990 for (child_die = die->child;
15991 child_die && child_die->tag;
15992 child_die = child_die->sibling)
15993 {
15994 /* Create the symbol in the DW_TAG_common_block block in the current
15995 symbol scope. */
15996 sym = new_symbol (child_die, NULL, cu);
15997 if (sym != NULL)
15998 {
15999 struct attribute *member_loc;
16000
16001 common_block->contents[common_block->n_entries++] = sym;
16002
16003 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16004 cu);
16005 if (member_loc)
16006 {
16007 /* GDB has handled this for a long time, but it is
16008 not specified by DWARF. It seems to have been
16009 emitted by gfortran at least as recently as:
16010 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16011 complaint (_("Variable in common block has "
16012 "DW_AT_data_member_location "
16013 "- DIE at %s [in module %s]"),
16014 sect_offset_str (child_die->sect_off),
16015 objfile_name (objfile));
16016
16017 if (member_loc->form_is_section_offset ())
16018 dwarf2_complex_location_expr_complaint ();
16019 else if (member_loc->form_is_constant ()
16020 || member_loc->form_is_block ())
16021 {
16022 if (attr != nullptr)
16023 mark_common_block_symbol_computed (sym, die, attr,
16024 member_loc, cu);
16025 }
16026 else
16027 dwarf2_complex_location_expr_complaint ();
16028 }
16029 }
16030 }
16031
16032 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16033 sym->set_value_common_block (common_block);
16034 }
16035 }
16036
16037 /* Create a type for a C++ namespace. */
16038
16039 static struct type *
16040 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16041 {
16042 struct objfile *objfile = cu->per_objfile->objfile;
16043 const char *previous_prefix, *name;
16044 int is_anonymous;
16045 struct type *type;
16046
16047 /* For extensions, reuse the type of the original namespace. */
16048 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16049 {
16050 struct die_info *ext_die;
16051 struct dwarf2_cu *ext_cu = cu;
16052
16053 ext_die = dwarf2_extension (die, &ext_cu);
16054 type = read_type_die (ext_die, ext_cu);
16055
16056 /* EXT_CU may not be the same as CU.
16057 Ensure TYPE is recorded with CU in die_type_hash. */
16058 return set_die_type (die, type, cu);
16059 }
16060
16061 name = namespace_name (die, &is_anonymous, cu);
16062
16063 /* Now build the name of the current namespace. */
16064
16065 previous_prefix = determine_prefix (die, cu);
16066 if (previous_prefix[0] != '\0')
16067 name = typename_concat (&objfile->objfile_obstack,
16068 previous_prefix, name, 0, cu);
16069
16070 /* Create the type. */
16071 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16072
16073 return set_die_type (die, type, cu);
16074 }
16075
16076 /* Read a namespace scope. */
16077
16078 static void
16079 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16080 {
16081 struct objfile *objfile = cu->per_objfile->objfile;
16082 int is_anonymous;
16083
16084 /* Add a symbol associated to this if we haven't seen the namespace
16085 before. Also, add a using directive if it's an anonymous
16086 namespace. */
16087
16088 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16089 {
16090 struct type *type;
16091
16092 type = read_type_die (die, cu);
16093 new_symbol (die, type, cu);
16094
16095 namespace_name (die, &is_anonymous, cu);
16096 if (is_anonymous)
16097 {
16098 const char *previous_prefix = determine_prefix (die, cu);
16099
16100 std::vector<const char *> excludes;
16101 add_using_directive (using_directives (cu),
16102 previous_prefix, type->name (), NULL,
16103 NULL, excludes,
16104 read_decl_line (die, cu),
16105 0, &objfile->objfile_obstack);
16106 }
16107 }
16108
16109 if (die->child != NULL)
16110 {
16111 struct die_info *child_die = die->child;
16112
16113 while (child_die && child_die->tag)
16114 {
16115 process_die (child_die, cu);
16116 child_die = child_die->sibling;
16117 }
16118 }
16119 }
16120
16121 /* Read a Fortran module as type. This DIE can be only a declaration used for
16122 imported module. Still we need that type as local Fortran "use ... only"
16123 declaration imports depend on the created type in determine_prefix. */
16124
16125 static struct type *
16126 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16127 {
16128 struct objfile *objfile = cu->per_objfile->objfile;
16129 const char *module_name;
16130 struct type *type;
16131
16132 module_name = dwarf2_name (die, cu);
16133 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16134
16135 return set_die_type (die, type, cu);
16136 }
16137
16138 /* Read a Fortran module. */
16139
16140 static void
16141 read_module (struct die_info *die, struct dwarf2_cu *cu)
16142 {
16143 struct die_info *child_die = die->child;
16144 struct type *type;
16145
16146 type = read_type_die (die, cu);
16147 new_symbol (die, type, cu);
16148
16149 while (child_die && child_die->tag)
16150 {
16151 process_die (child_die, cu);
16152 child_die = child_die->sibling;
16153 }
16154 }
16155
16156 /* Return the name of the namespace represented by DIE. Set
16157 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16158 namespace. */
16159
16160 static const char *
16161 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16162 {
16163 struct die_info *current_die;
16164 const char *name = NULL;
16165
16166 /* Loop through the extensions until we find a name. */
16167
16168 for (current_die = die;
16169 current_die != NULL;
16170 current_die = dwarf2_extension (die, &cu))
16171 {
16172 /* We don't use dwarf2_name here so that we can detect the absence
16173 of a name -> anonymous namespace. */
16174 name = dwarf2_string_attr (die, DW_AT_name, cu);
16175
16176 if (name != NULL)
16177 break;
16178 }
16179
16180 /* Is it an anonymous namespace? */
16181
16182 *is_anonymous = (name == NULL);
16183 if (*is_anonymous)
16184 name = CP_ANONYMOUS_NAMESPACE_STR;
16185
16186 return name;
16187 }
16188
16189 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16190 the user defined type vector. */
16191
16192 static struct type *
16193 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16194 {
16195 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16196 struct comp_unit_head *cu_header = &cu->header;
16197 struct type *type;
16198 struct attribute *attr_byte_size;
16199 struct attribute *attr_address_class;
16200 int byte_size, addr_class;
16201 struct type *target_type;
16202
16203 target_type = die_type (die, cu);
16204
16205 /* The die_type call above may have already set the type for this DIE. */
16206 type = get_die_type (die, cu);
16207 if (type)
16208 return type;
16209
16210 type = lookup_pointer_type (target_type);
16211
16212 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16213 if (attr_byte_size)
16214 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
16215 else
16216 byte_size = cu_header->addr_size;
16217
16218 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16219 if (attr_address_class)
16220 addr_class = attr_address_class->constant_value (DW_ADDR_none);
16221 else
16222 addr_class = DW_ADDR_none;
16223
16224 ULONGEST alignment = get_alignment (cu, die);
16225
16226 /* If the pointer size, alignment, or address class is different
16227 than the default, create a type variant marked as such and set
16228 the length accordingly. */
16229 if (type->length () != byte_size
16230 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16231 && alignment != TYPE_RAW_ALIGN (type))
16232 || addr_class != DW_ADDR_none)
16233 {
16234 if (gdbarch_address_class_type_flags_p (gdbarch))
16235 {
16236 type_instance_flags type_flags
16237 = gdbarch_address_class_type_flags (gdbarch, byte_size,
16238 addr_class);
16239 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16240 == 0);
16241 type = make_type_with_address_space (type, type_flags);
16242 }
16243 else if (type->length () != byte_size)
16244 {
16245 complaint (_("invalid pointer size %d"), byte_size);
16246 }
16247 else if (TYPE_RAW_ALIGN (type) != alignment)
16248 {
16249 complaint (_("Invalid DW_AT_alignment"
16250 " - DIE at %s [in module %s]"),
16251 sect_offset_str (die->sect_off),
16252 objfile_name (cu->per_objfile->objfile));
16253 }
16254 else
16255 {
16256 /* Should we also complain about unhandled address classes? */
16257 }
16258 }
16259
16260 type->set_length (byte_size);
16261 set_type_align (type, alignment);
16262 return set_die_type (die, type, cu);
16263 }
16264
16265 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16266 the user defined type vector. */
16267
16268 static struct type *
16269 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16270 {
16271 struct type *type;
16272 struct type *to_type;
16273 struct type *domain;
16274
16275 to_type = die_type (die, cu);
16276 domain = die_containing_type (die, cu);
16277
16278 /* The calls above may have already set the type for this DIE. */
16279 type = get_die_type (die, cu);
16280 if (type)
16281 return type;
16282
16283 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16284 type = lookup_methodptr_type (to_type);
16285 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16286 {
16287 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16288
16289 smash_to_method_type (new_type, domain, to_type->target_type (),
16290 to_type->fields (), to_type->num_fields (),
16291 to_type->has_varargs ());
16292 type = lookup_methodptr_type (new_type);
16293 }
16294 else
16295 type = lookup_memberptr_type (to_type, domain);
16296
16297 return set_die_type (die, type, cu);
16298 }
16299
16300 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16301 the user defined type vector. */
16302
16303 static struct type *
16304 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16305 enum type_code refcode)
16306 {
16307 struct comp_unit_head *cu_header = &cu->header;
16308 struct type *type, *target_type;
16309 struct attribute *attr;
16310
16311 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16312
16313 target_type = die_type (die, cu);
16314
16315 /* The die_type call above may have already set the type for this DIE. */
16316 type = get_die_type (die, cu);
16317 if (type)
16318 return type;
16319
16320 type = lookup_reference_type (target_type, refcode);
16321 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16322 if (attr != nullptr)
16323 type->set_length (attr->constant_value (cu_header->addr_size));
16324 else
16325 type->set_length (cu_header->addr_size);
16326
16327 maybe_set_alignment (cu, die, type);
16328 return set_die_type (die, type, cu);
16329 }
16330
16331 /* Add the given cv-qualifiers to the element type of the array. GCC
16332 outputs DWARF type qualifiers that apply to an array, not the
16333 element type. But GDB relies on the array element type to carry
16334 the cv-qualifiers. This mimics section 6.7.3 of the C99
16335 specification. */
16336
16337 static struct type *
16338 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16339 struct type *base_type, int cnst, int voltl)
16340 {
16341 struct type *el_type, *inner_array;
16342
16343 base_type = copy_type (base_type);
16344 inner_array = base_type;
16345
16346 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
16347 {
16348 inner_array->set_target_type (copy_type (inner_array->target_type ()));
16349 inner_array = inner_array->target_type ();
16350 }
16351
16352 el_type = inner_array->target_type ();
16353 cnst |= TYPE_CONST (el_type);
16354 voltl |= TYPE_VOLATILE (el_type);
16355 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
16356
16357 return set_die_type (die, base_type, cu);
16358 }
16359
16360 static struct type *
16361 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16362 {
16363 struct type *base_type, *cv_type;
16364
16365 base_type = die_type (die, cu);
16366
16367 /* The die_type call above may have already set the type for this DIE. */
16368 cv_type = get_die_type (die, cu);
16369 if (cv_type)
16370 return cv_type;
16371
16372 /* In case the const qualifier is applied to an array type, the element type
16373 is so qualified, not the array type (section 6.7.3 of C99). */
16374 if (base_type->code () == TYPE_CODE_ARRAY)
16375 return add_array_cv_type (die, cu, base_type, 1, 0);
16376
16377 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16378 return set_die_type (die, cv_type, cu);
16379 }
16380
16381 static struct type *
16382 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16383 {
16384 struct type *base_type, *cv_type;
16385
16386 base_type = die_type (die, cu);
16387
16388 /* The die_type call above may have already set the type for this DIE. */
16389 cv_type = get_die_type (die, cu);
16390 if (cv_type)
16391 return cv_type;
16392
16393 /* In case the volatile qualifier is applied to an array type, the
16394 element type is so qualified, not the array type (section 6.7.3
16395 of C99). */
16396 if (base_type->code () == TYPE_CODE_ARRAY)
16397 return add_array_cv_type (die, cu, base_type, 0, 1);
16398
16399 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16400 return set_die_type (die, cv_type, cu);
16401 }
16402
16403 /* Handle DW_TAG_restrict_type. */
16404
16405 static struct type *
16406 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16407 {
16408 struct type *base_type, *cv_type;
16409
16410 base_type = die_type (die, cu);
16411
16412 /* The die_type call above may have already set the type for this DIE. */
16413 cv_type = get_die_type (die, cu);
16414 if (cv_type)
16415 return cv_type;
16416
16417 cv_type = make_restrict_type (base_type);
16418 return set_die_type (die, cv_type, cu);
16419 }
16420
16421 /* Handle DW_TAG_atomic_type. */
16422
16423 static struct type *
16424 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16425 {
16426 struct type *base_type, *cv_type;
16427
16428 base_type = die_type (die, cu);
16429
16430 /* The die_type call above may have already set the type for this DIE. */
16431 cv_type = get_die_type (die, cu);
16432 if (cv_type)
16433 return cv_type;
16434
16435 cv_type = make_atomic_type (base_type);
16436 return set_die_type (die, cv_type, cu);
16437 }
16438
16439 /* Extract all information from a DW_TAG_string_type DIE and add to
16440 the user defined type vector. It isn't really a user defined type,
16441 but it behaves like one, with other DIE's using an AT_user_def_type
16442 attribute to reference it. */
16443
16444 static struct type *
16445 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16446 {
16447 struct objfile *objfile = cu->per_objfile->objfile;
16448 struct gdbarch *gdbarch = objfile->arch ();
16449 struct type *type, *range_type, *index_type, *char_type;
16450 struct attribute *attr;
16451 struct dynamic_prop prop;
16452 bool length_is_constant = true;
16453 LONGEST length;
16454
16455 /* There are a couple of places where bit sizes might be made use of
16456 when parsing a DW_TAG_string_type, however, no producer that we know
16457 of make use of these. Handling bit sizes that are a multiple of the
16458 byte size is easy enough, but what about other bit sizes? Lets deal
16459 with that problem when we have to. Warn about these attributes being
16460 unsupported, then parse the type and ignore them like we always
16461 have. */
16462 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16463 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16464 {
16465 static bool warning_printed = false;
16466 if (!warning_printed)
16467 {
16468 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16469 "currently supported on DW_TAG_string_type."));
16470 warning_printed = true;
16471 }
16472 }
16473
16474 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16475 if (attr != nullptr && !attr->form_is_constant ())
16476 {
16477 /* The string length describes the location at which the length of
16478 the string can be found. The size of the length field can be
16479 specified with one of the attributes below. */
16480 struct type *prop_type;
16481 struct attribute *len
16482 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16483 if (len == nullptr)
16484 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16485 if (len != nullptr && len->form_is_constant ())
16486 {
16487 /* Pass 0 as the default as we know this attribute is constant
16488 and the default value will not be returned. */
16489 LONGEST sz = len->constant_value (0);
16490 prop_type = objfile_int_type (objfile, sz, true);
16491 }
16492 else
16493 {
16494 /* If the size is not specified then we assume it is the size of
16495 an address on this target. */
16496 prop_type = cu->addr_sized_int_type (true);
16497 }
16498
16499 /* Convert the attribute into a dynamic property. */
16500 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16501 length = 1;
16502 else
16503 length_is_constant = false;
16504 }
16505 else if (attr != nullptr)
16506 {
16507 /* This DW_AT_string_length just contains the length with no
16508 indirection. There's no need to create a dynamic property in this
16509 case. Pass 0 for the default value as we know it will not be
16510 returned in this case. */
16511 length = attr->constant_value (0);
16512 }
16513 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16514 {
16515 /* We don't currently support non-constant byte sizes for strings. */
16516 length = attr->constant_value (1);
16517 }
16518 else
16519 {
16520 /* Use 1 as a fallback length if we have nothing else. */
16521 length = 1;
16522 }
16523
16524 index_type = objfile_type (objfile)->builtin_int;
16525 if (length_is_constant)
16526 range_type = create_static_range_type (NULL, index_type, 1, length);
16527 else
16528 {
16529 struct dynamic_prop low_bound;
16530
16531 low_bound.set_const_val (1);
16532 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16533 }
16534 char_type = language_string_char_type (cu->language_defn, gdbarch);
16535 type = create_string_type (NULL, char_type, range_type);
16536
16537 return set_die_type (die, type, cu);
16538 }
16539
16540 /* Assuming that DIE corresponds to a function, returns nonzero
16541 if the function is prototyped. */
16542
16543 static int
16544 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16545 {
16546 struct attribute *attr;
16547
16548 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16549 if (attr && attr->as_boolean ())
16550 return 1;
16551
16552 /* The DWARF standard implies that the DW_AT_prototyped attribute
16553 is only meaningful for C, but the concept also extends to other
16554 languages that allow unprototyped functions (Eg: Objective C).
16555 For all other languages, assume that functions are always
16556 prototyped. */
16557 if (cu->lang () != language_c
16558 && cu->lang () != language_objc
16559 && cu->lang () != language_opencl)
16560 return 1;
16561
16562 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16563 prototyped and unprototyped functions; default to prototyped,
16564 since that is more common in modern code (and RealView warns
16565 about unprototyped functions). */
16566 if (producer_is_realview (cu->producer))
16567 return 1;
16568
16569 return 0;
16570 }
16571
16572 /* Handle DIES due to C code like:
16573
16574 struct foo
16575 {
16576 int (*funcp)(int a, long l);
16577 int b;
16578 };
16579
16580 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16581
16582 static struct type *
16583 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16584 {
16585 struct objfile *objfile = cu->per_objfile->objfile;
16586 struct type *type; /* Type that this function returns. */
16587 struct type *ftype; /* Function that returns above type. */
16588 struct attribute *attr;
16589
16590 type = die_type (die, cu);
16591
16592 /* The die_type call above may have already set the type for this DIE. */
16593 ftype = get_die_type (die, cu);
16594 if (ftype)
16595 return ftype;
16596
16597 ftype = lookup_function_type (type);
16598
16599 if (prototyped_function_p (die, cu))
16600 ftype->set_is_prototyped (true);
16601
16602 /* Store the calling convention in the type if it's available in
16603 the subroutine die. Otherwise set the calling convention to
16604 the default value DW_CC_normal. */
16605 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16606 if (attr != nullptr
16607 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
16608 TYPE_CALLING_CONVENTION (ftype)
16609 = (enum dwarf_calling_convention) attr->constant_value (0);
16610 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16611 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16612 else
16613 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16614
16615 /* Record whether the function returns normally to its caller or not
16616 if the DWARF producer set that information. */
16617 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16618 if (attr && attr->as_boolean ())
16619 TYPE_NO_RETURN (ftype) = 1;
16620
16621 /* We need to add the subroutine type to the die immediately so
16622 we don't infinitely recurse when dealing with parameters
16623 declared as the same subroutine type. */
16624 set_die_type (die, ftype, cu);
16625
16626 if (die->child != NULL)
16627 {
16628 struct type *void_type = objfile_type (objfile)->builtin_void;
16629 struct die_info *child_die;
16630 int nparams, iparams;
16631
16632 /* Count the number of parameters.
16633 FIXME: GDB currently ignores vararg functions, but knows about
16634 vararg member functions. */
16635 nparams = 0;
16636 child_die = die->child;
16637 while (child_die && child_die->tag)
16638 {
16639 if (child_die->tag == DW_TAG_formal_parameter)
16640 nparams++;
16641 else if (child_die->tag == DW_TAG_unspecified_parameters)
16642 ftype->set_has_varargs (true);
16643
16644 child_die = child_die->sibling;
16645 }
16646
16647 /* Allocate storage for parameters and fill them in. */
16648 ftype->set_num_fields (nparams);
16649 ftype->set_fields
16650 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
16651
16652 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16653 even if we error out during the parameters reading below. */
16654 for (iparams = 0; iparams < nparams; iparams++)
16655 ftype->field (iparams).set_type (void_type);
16656
16657 iparams = 0;
16658 child_die = die->child;
16659 while (child_die && child_die->tag)
16660 {
16661 if (child_die->tag == DW_TAG_formal_parameter)
16662 {
16663 struct type *arg_type;
16664
16665 /* DWARF version 2 has no clean way to discern C++
16666 static and non-static member functions. G++ helps
16667 GDB by marking the first parameter for non-static
16668 member functions (which is the this pointer) as
16669 artificial. We pass this information to
16670 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16671
16672 DWARF version 3 added DW_AT_object_pointer, which GCC
16673 4.5 does not yet generate. */
16674 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16675 if (attr != nullptr)
16676 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
16677 else
16678 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16679 arg_type = die_type (child_die, cu);
16680
16681 /* RealView does not mark THIS as const, which the testsuite
16682 expects. GCC marks THIS as const in method definitions,
16683 but not in the class specifications (GCC PR 43053). */
16684 if (cu->lang () == language_cplus
16685 && !TYPE_CONST (arg_type)
16686 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16687 {
16688 int is_this = 0;
16689 struct dwarf2_cu *arg_cu = cu;
16690 const char *name = dwarf2_name (child_die, cu);
16691
16692 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16693 if (attr != nullptr)
16694 {
16695 /* If the compiler emits this, use it. */
16696 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16697 is_this = 1;
16698 }
16699 else if (name && strcmp (name, "this") == 0)
16700 /* Function definitions will have the argument names. */
16701 is_this = 1;
16702 else if (name == NULL && iparams == 0)
16703 /* Declarations may not have the names, so like
16704 elsewhere in GDB, assume an artificial first
16705 argument is "this". */
16706 is_this = 1;
16707
16708 if (is_this)
16709 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16710 arg_type, 0);
16711 }
16712
16713 ftype->field (iparams).set_type (arg_type);
16714 iparams++;
16715 }
16716 child_die = child_die->sibling;
16717 }
16718 }
16719
16720 return ftype;
16721 }
16722
16723 static struct type *
16724 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16725 {
16726 struct objfile *objfile = cu->per_objfile->objfile;
16727 const char *name = NULL;
16728 struct type *this_type, *target_type;
16729
16730 name = dwarf2_full_name (NULL, die, cu);
16731 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16732 this_type->set_target_is_stub (true);
16733 set_die_type (die, this_type, cu);
16734 target_type = die_type (die, cu);
16735 if (target_type != this_type)
16736 this_type->set_target_type (target_type);
16737 else
16738 {
16739 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16740 spec and cause infinite loops in GDB. */
16741 complaint (_("Self-referential DW_TAG_typedef "
16742 "- DIE at %s [in module %s]"),
16743 sect_offset_str (die->sect_off), objfile_name (objfile));
16744 this_type->set_target_type (nullptr);
16745 }
16746 if (name == NULL)
16747 {
16748 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16749 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16750 Handle these by just returning the target type, rather than
16751 constructing an anonymous typedef type and trying to handle this
16752 elsewhere. */
16753 set_die_type (die, target_type, cu);
16754 return target_type;
16755 }
16756 return this_type;
16757 }
16758
16759 /* Helper for get_dwarf2_rational_constant that computes the value of
16760 a given gmp_mpz given an attribute. */
16761
16762 static void
16763 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
16764 {
16765 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16766 location expression that pushes an implicit value. */
16767 if (attr->form == DW_FORM_exprloc)
16768 {
16769 dwarf_block *blk = attr->as_block ();
16770 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
16771 {
16772 uint64_t len;
16773 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
16774 blk->data + blk->size,
16775 &len);
16776 if (ptr - blk->data + len <= blk->size)
16777 {
16778 mpz_import (value->val, len,
16779 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
16780 ? 1 : -1,
16781 1, 0, 0, ptr);
16782 return;
16783 }
16784 }
16785
16786 /* On failure set it to 1. */
16787 *value = gdb_mpz (1);
16788 }
16789 else if (attr->form_is_block ())
16790 {
16791 dwarf_block *blk = attr->as_block ();
16792 mpz_import (value->val, blk->size,
16793 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
16794 ? 1 : -1,
16795 1, 0, 0, blk->data);
16796 }
16797 else
16798 *value = gdb_mpz (attr->constant_value (1));
16799 }
16800
16801 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16802 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16803
16804 If the numerator and/or numerator attribute is missing,
16805 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16806 untouched. */
16807
16808 static void
16809 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
16810 gdb_mpz *numerator, gdb_mpz *denominator)
16811 {
16812 struct attribute *num_attr, *denom_attr;
16813
16814 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
16815 if (num_attr == nullptr)
16816 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16817 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16818
16819 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
16820 if (denom_attr == nullptr)
16821 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16822 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
16823
16824 if (num_attr == nullptr || denom_attr == nullptr)
16825 return;
16826
16827 get_mpz (cu, numerator, num_attr);
16828 get_mpz (cu, denominator, denom_attr);
16829 }
16830
16831 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16832 rational constant, rather than a signed one.
16833
16834 If the rational constant has a negative value, a complaint
16835 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16836
16837 static void
16838 get_dwarf2_unsigned_rational_constant (struct die_info *die,
16839 struct dwarf2_cu *cu,
16840 gdb_mpz *numerator,
16841 gdb_mpz *denominator)
16842 {
16843 gdb_mpz num (1);
16844 gdb_mpz denom (1);
16845
16846 get_dwarf2_rational_constant (die, cu, &num, &denom);
16847 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
16848 {
16849 mpz_neg (num.val, num.val);
16850 mpz_neg (denom.val, denom.val);
16851 }
16852 else if (mpz_sgn (num.val) == -1)
16853 {
16854 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16855 " in DIE at %s"),
16856 sect_offset_str (die->sect_off));
16857 return;
16858 }
16859 else if (mpz_sgn (denom.val) == -1)
16860 {
16861 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16862 " in DIE at %s"),
16863 sect_offset_str (die->sect_off));
16864 return;
16865 }
16866
16867 *numerator = std::move (num);
16868 *denominator = std::move (denom);
16869 }
16870
16871 /* Assuming that ENCODING is a string whose contents starting at the
16872 K'th character is "_nn" where "nn" is a decimal number, scan that
16873 number and set RESULT to the value. K is updated to point to the
16874 character immediately following the number.
16875
16876 If the string does not conform to the format described above, false
16877 is returned, and K may or may not be changed. */
16878
16879 static bool
16880 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
16881 {
16882 /* The next character should be an underscore ('_') followed
16883 by a digit. */
16884 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
16885 return false;
16886
16887 /* Skip the underscore. */
16888 k++;
16889 int start = k;
16890
16891 /* Determine the number of digits for our number. */
16892 while (isdigit (encoding[k]))
16893 k++;
16894 if (k == start)
16895 return false;
16896
16897 std::string copy (&encoding[start], k - start);
16898 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
16899 return false;
16900
16901 return true;
16902 }
16903
16904 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16905 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16906 DENOM, update OFFSET, and return true on success. Return false on
16907 failure. */
16908
16909 static bool
16910 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
16911 gdb_mpz *num, gdb_mpz *denom)
16912 {
16913 if (!ada_get_gnat_encoded_number (encoding, offset, num))
16914 return false;
16915 return ada_get_gnat_encoded_number (encoding, offset, denom);
16916 }
16917
16918 /* Assuming DIE corresponds to a fixed point type, finish the creation
16919 of the corresponding TYPE by setting its type-specific data. CU is
16920 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16921 encodings. It is nullptr if the GNAT encoding should be
16922 ignored. */
16923
16924 static void
16925 finish_fixed_point_type (struct type *type, const char *suffix,
16926 struct die_info *die, struct dwarf2_cu *cu)
16927 {
16928 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
16929 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
16930
16931 /* If GNAT encodings are preferred, don't examine the
16932 attributes. */
16933 struct attribute *attr = nullptr;
16934 if (suffix == nullptr)
16935 {
16936 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
16937 if (attr == nullptr)
16938 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
16939 if (attr == nullptr)
16940 attr = dwarf2_attr (die, DW_AT_small, cu);
16941 }
16942
16943 /* Numerator and denominator of our fixed-point type's scaling factor.
16944 The default is a scaling factor of 1, which we use as a fallback
16945 when we are not able to decode it (problem with the debugging info,
16946 unsupported forms, bug in GDB, etc...). Using that as the default
16947 allows us to at least print the unscaled value, which might still
16948 be useful to a user. */
16949 gdb_mpz scale_num (1);
16950 gdb_mpz scale_denom (1);
16951
16952 if (attr == nullptr)
16953 {
16954 int offset = 0;
16955 if (suffix != nullptr
16956 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16957 &scale_denom)
16958 /* The number might be encoded as _nn_dd_nn_dd, where the
16959 second ratio is the 'small value. In this situation, we
16960 want the second value. */
16961 && (suffix[offset] != '_'
16962 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
16963 &scale_denom)))
16964 {
16965 /* Found it. */
16966 }
16967 else
16968 {
16969 /* Scaling factor not found. Assume a scaling factor of 1,
16970 and hope for the best. At least the user will be able to
16971 see the encoded value. */
16972 scale_num = 1;
16973 scale_denom = 1;
16974 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16975 sect_offset_str (die->sect_off));
16976 }
16977 }
16978 else if (attr->name == DW_AT_binary_scale)
16979 {
16980 LONGEST scale_exp = attr->constant_value (0);
16981 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16982
16983 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
16984 }
16985 else if (attr->name == DW_AT_decimal_scale)
16986 {
16987 LONGEST scale_exp = attr->constant_value (0);
16988 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
16989
16990 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
16991 }
16992 else if (attr->name == DW_AT_small)
16993 {
16994 struct die_info *scale_die;
16995 struct dwarf2_cu *scale_cu = cu;
16996
16997 scale_die = follow_die_ref (die, attr, &scale_cu);
16998 if (scale_die->tag == DW_TAG_constant)
16999 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
17000 &scale_num, &scale_denom);
17001 else
17002 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
17003 " (DIE at %s)"),
17004 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17005 }
17006 else
17007 {
17008 complaint (_("unsupported scale attribute %s for fixed-point type"
17009 " (DIE at %s)"),
17010 dwarf_attr_name (attr->name),
17011 sect_offset_str (die->sect_off));
17012 }
17013
17014 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
17015 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
17016 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
17017 mpq_canonicalize (scaling_factor.val);
17018 }
17019
17020 /* The gnat-encoding suffix for fixed point. */
17021
17022 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
17023
17024 /* If NAME encodes an Ada fixed-point type, return a pointer to the
17025 "XF" suffix of the name. The text after this is what encodes the
17026 'small and 'delta information. Otherwise, return nullptr. */
17027
17028 static const char *
17029 gnat_encoded_fixed_point_type_info (const char *name)
17030 {
17031 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
17032 }
17033
17034 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17035 (which may be different from NAME) to the architecture back-end to allow
17036 it to guess the correct format if necessary. */
17037
17038 static struct type *
17039 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17040 const char *name_hint, enum bfd_endian byte_order)
17041 {
17042 struct gdbarch *gdbarch = objfile->arch ();
17043 const struct floatformat **format;
17044 struct type *type;
17045
17046 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17047 if (format)
17048 type = init_float_type (objfile, bits, name, format, byte_order);
17049 else
17050 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17051
17052 return type;
17053 }
17054
17055 /* Allocate an integer type of size BITS and name NAME. */
17056
17057 static struct type *
17058 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17059 int bits, int unsigned_p, const char *name)
17060 {
17061 struct type *type;
17062
17063 /* Versions of Intel's C Compiler generate an integer type called "void"
17064 instead of using DW_TAG_unspecified_type. This has been seen on
17065 at least versions 14, 17, and 18. */
17066 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17067 && strcmp (name, "void") == 0)
17068 type = objfile_type (objfile)->builtin_void;
17069 else
17070 type = init_integer_type (objfile, bits, unsigned_p, name);
17071
17072 return type;
17073 }
17074
17075 /* Return true if DIE has a DW_AT_small attribute whose value is
17076 a constant rational, where both the numerator and denominator
17077 are equal to zero.
17078
17079 CU is the DIE's Compilation Unit. */
17080
17081 static bool
17082 has_zero_over_zero_small_attribute (struct die_info *die,
17083 struct dwarf2_cu *cu)
17084 {
17085 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
17086 if (attr == nullptr)
17087 return false;
17088
17089 struct dwarf2_cu *scale_cu = cu;
17090 struct die_info *scale_die
17091 = follow_die_ref (die, attr, &scale_cu);
17092
17093 if (scale_die->tag != DW_TAG_constant)
17094 return false;
17095
17096 gdb_mpz num (1), denom (1);
17097 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
17098 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
17099 }
17100
17101 /* Initialise and return a floating point type of size BITS suitable for
17102 use as a component of a complex number. The NAME_HINT is passed through
17103 when initialising the floating point type and is the name of the complex
17104 type.
17105
17106 As DWARF doesn't currently provide an explicit name for the components
17107 of a complex number, but it can be helpful to have these components
17108 named, we try to select a suitable name based on the size of the
17109 component. */
17110 static struct type *
17111 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17112 struct objfile *objfile,
17113 int bits, const char *name_hint,
17114 enum bfd_endian byte_order)
17115 {
17116 gdbarch *gdbarch = objfile->arch ();
17117 struct type *tt = nullptr;
17118
17119 /* Try to find a suitable floating point builtin type of size BITS.
17120 We're going to use the name of this type as the name for the complex
17121 target type that we are about to create. */
17122 switch (cu->lang ())
17123 {
17124 case language_fortran:
17125 switch (bits)
17126 {
17127 case 32:
17128 tt = builtin_f_type (gdbarch)->builtin_real;
17129 break;
17130 case 64:
17131 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17132 break;
17133 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17134 case 128:
17135 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17136 break;
17137 }
17138 break;
17139 default:
17140 switch (bits)
17141 {
17142 case 32:
17143 tt = builtin_type (gdbarch)->builtin_float;
17144 break;
17145 case 64:
17146 tt = builtin_type (gdbarch)->builtin_double;
17147 break;
17148 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17149 case 128:
17150 tt = builtin_type (gdbarch)->builtin_long_double;
17151 break;
17152 }
17153 break;
17154 }
17155
17156 /* If the type we found doesn't match the size we were looking for, then
17157 pretend we didn't find a type at all, the complex target type we
17158 create will then be nameless. */
17159 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
17160 tt = nullptr;
17161
17162 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17163 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17164 }
17165
17166 /* Find a representation of a given base type and install
17167 it in the TYPE field of the die. */
17168
17169 static struct type *
17170 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17171 {
17172 struct objfile *objfile = cu->per_objfile->objfile;
17173 struct type *type;
17174 struct attribute *attr;
17175 int encoding = 0, bits = 0;
17176 const char *name;
17177 gdbarch *arch;
17178
17179 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17180 if (attr != nullptr && attr->form_is_constant ())
17181 encoding = attr->constant_value (0);
17182 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17183 if (attr != nullptr)
17184 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
17185 name = dwarf2_name (die, cu);
17186 if (!name)
17187 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17188
17189 arch = objfile->arch ();
17190 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17191
17192 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17193 if (attr != nullptr && attr->form_is_constant ())
17194 {
17195 int endianity = attr->constant_value (0);
17196
17197 switch (endianity)
17198 {
17199 case DW_END_big:
17200 byte_order = BFD_ENDIAN_BIG;
17201 break;
17202 case DW_END_little:
17203 byte_order = BFD_ENDIAN_LITTLE;
17204 break;
17205 default:
17206 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17207 break;
17208 }
17209 }
17210
17211 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
17212 && cu->lang () == language_ada
17213 && has_zero_over_zero_small_attribute (die, cu))
17214 {
17215 /* brobecker/2018-02-24: This is a fixed point type for which
17216 the scaling factor is represented as fraction whose value
17217 does not make sense (zero divided by zero), so we should
17218 normally never see these. However, there is a small category
17219 of fixed point types for which GNAT is unable to provide
17220 the scaling factor via the standard DWARF mechanisms, and
17221 for which the info is provided via the GNAT encodings instead.
17222 This is likely what this DIE is about. */
17223 encoding = (encoding == DW_ATE_signed_fixed
17224 ? DW_ATE_signed
17225 : DW_ATE_unsigned);
17226 }
17227
17228 /* With GNAT encodings, fixed-point information will be encoded in
17229 the type name. Note that this can also occur with the above
17230 zero-over-zero case, which is why this is a separate "if" rather
17231 than an "else if". */
17232 const char *gnat_encoding_suffix = nullptr;
17233 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
17234 && cu->lang () == language_ada
17235 && name != nullptr)
17236 {
17237 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
17238 if (gnat_encoding_suffix != nullptr)
17239 {
17240 gdb_assert (startswith (gnat_encoding_suffix,
17241 GNAT_FIXED_POINT_SUFFIX));
17242 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
17243 name, gnat_encoding_suffix - name);
17244 /* Use -1 here so that SUFFIX points at the "_" after the
17245 "XF". */
17246 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
17247
17248 encoding = (encoding == DW_ATE_signed
17249 ? DW_ATE_signed_fixed
17250 : DW_ATE_unsigned_fixed);
17251 }
17252 }
17253
17254 switch (encoding)
17255 {
17256 case DW_ATE_address:
17257 /* Turn DW_ATE_address into a void * pointer. */
17258 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17259 type = init_pointer_type (objfile, bits, name, type);
17260 break;
17261 case DW_ATE_boolean:
17262 type = init_boolean_type (objfile, bits, 1, name);
17263 break;
17264 case DW_ATE_complex_float:
17265 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17266 byte_order);
17267 if (type->code () == TYPE_CODE_ERROR)
17268 {
17269 if (name == nullptr)
17270 {
17271 struct obstack *obstack
17272 = &cu->per_objfile->objfile->objfile_obstack;
17273 name = obconcat (obstack, "_Complex ", type->name (),
17274 nullptr);
17275 }
17276 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17277 }
17278 else
17279 type = init_complex_type (name, type);
17280 break;
17281 case DW_ATE_decimal_float:
17282 type = init_decfloat_type (objfile, bits, name);
17283 break;
17284 case DW_ATE_float:
17285 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17286 break;
17287 case DW_ATE_signed:
17288 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17289 break;
17290 case DW_ATE_unsigned:
17291 if (cu->lang () == language_fortran
17292 && name
17293 && startswith (name, "character("))
17294 type = init_character_type (objfile, bits, 1, name);
17295 else
17296 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17297 break;
17298 case DW_ATE_signed_char:
17299 if (cu->lang () == language_ada
17300 || cu->lang () == language_m2
17301 || cu->lang () == language_pascal
17302 || cu->lang () == language_fortran)
17303 type = init_character_type (objfile, bits, 0, name);
17304 else
17305 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17306 break;
17307 case DW_ATE_unsigned_char:
17308 if (cu->lang () == language_ada
17309 || cu->lang () == language_m2
17310 || cu->lang () == language_pascal
17311 || cu->lang () == language_fortran
17312 || cu->lang () == language_rust)
17313 type = init_character_type (objfile, bits, 1, name);
17314 else
17315 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17316 break;
17317 case DW_ATE_UTF:
17318 {
17319 type = init_character_type (objfile, bits, 1, name);
17320 return set_die_type (die, type, cu);
17321 }
17322 break;
17323 case DW_ATE_signed_fixed:
17324 type = init_fixed_point_type (objfile, bits, 0, name);
17325 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17326 break;
17327 case DW_ATE_unsigned_fixed:
17328 type = init_fixed_point_type (objfile, bits, 1, name);
17329 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
17330 break;
17331
17332 default:
17333 complaint (_("unsupported DW_AT_encoding: '%s'"),
17334 dwarf_type_encoding_name (encoding));
17335 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17336 break;
17337 }
17338
17339 if (type->code () == TYPE_CODE_INT
17340 && name != nullptr
17341 && strcmp (name, "char") == 0)
17342 type->set_has_no_signedness (true);
17343
17344 maybe_set_alignment (cu, die, type);
17345
17346 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
17347
17348 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
17349 {
17350 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
17351 if (attr != nullptr && attr->as_unsigned () <= 8 * type->length ())
17352 {
17353 unsigned real_bit_size = attr->as_unsigned ();
17354 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
17355 /* Only use the attributes if they make sense together. */
17356 if (attr == nullptr
17357 || (attr->as_unsigned () + real_bit_size
17358 <= 8 * type->length ()))
17359 {
17360 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
17361 = real_bit_size;
17362 if (attr != nullptr)
17363 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
17364 = attr->as_unsigned ();
17365 }
17366 }
17367 }
17368
17369 return set_die_type (die, type, cu);
17370 }
17371
17372 /* A helper function that returns the name of DIE, if it refers to a
17373 variable declaration. */
17374
17375 static const char *
17376 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
17377 {
17378 if (die->tag != DW_TAG_variable)
17379 return nullptr;
17380
17381 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
17382 if (attr == nullptr || !attr->as_boolean ())
17383 return nullptr;
17384
17385 attr = dwarf2_attr (die, DW_AT_name, cu);
17386 if (attr == nullptr)
17387 return nullptr;
17388 return attr->as_string ();
17389 }
17390
17391 /* Parse dwarf attribute if it's a block, reference or constant and put the
17392 resulting value of the attribute into struct bound_prop.
17393 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17394
17395 static int
17396 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17397 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17398 struct type *default_type)
17399 {
17400 struct dwarf2_property_baton *baton;
17401 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17402 struct objfile *objfile = per_objfile->objfile;
17403 struct obstack *obstack = &objfile->objfile_obstack;
17404
17405 gdb_assert (default_type != NULL);
17406
17407 if (attr == NULL || prop == NULL)
17408 return 0;
17409
17410 if (attr->form_is_block ())
17411 {
17412 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17413 baton->property_type = default_type;
17414 baton->locexpr.per_cu = cu->per_cu;
17415 baton->locexpr.per_objfile = per_objfile;
17416
17417 struct dwarf_block *block;
17418 if (attr->form == DW_FORM_data16)
17419 {
17420 size_t data_size = 16;
17421 block = XOBNEW (obstack, struct dwarf_block);
17422 block->size = (data_size
17423 + 2 /* Extra bytes for DW_OP and arg. */);
17424 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block->size);
17425 data[0] = DW_OP_implicit_value;
17426 data[1] = data_size;
17427 memcpy (&data[2], attr->as_block ()->data, data_size);
17428 block->data = data;
17429 }
17430 else
17431 block = attr->as_block ();
17432
17433 baton->locexpr.size = block->size;
17434 baton->locexpr.data = block->data;
17435 switch (attr->name)
17436 {
17437 case DW_AT_string_length:
17438 baton->locexpr.is_reference = true;
17439 break;
17440 default:
17441 baton->locexpr.is_reference = false;
17442 break;
17443 }
17444
17445 prop->set_locexpr (baton);
17446 gdb_assert (prop->baton () != NULL);
17447 }
17448 else if (attr->form_is_ref ())
17449 {
17450 struct dwarf2_cu *target_cu = cu;
17451 struct die_info *target_die;
17452 struct attribute *target_attr;
17453
17454 target_die = follow_die_ref (die, attr, &target_cu);
17455 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17456 if (target_attr == NULL)
17457 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17458 target_cu);
17459 if (target_attr == nullptr)
17460 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
17461 target_cu);
17462 if (target_attr == NULL)
17463 {
17464 const char *name = var_decl_name (target_die, target_cu);
17465 if (name != nullptr)
17466 {
17467 prop->set_variable_name (name);
17468 return 1;
17469 }
17470 return 0;
17471 }
17472
17473 switch (target_attr->name)
17474 {
17475 case DW_AT_location:
17476 if (target_attr->form_is_section_offset ())
17477 {
17478 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17479 baton->property_type = die_type (target_die, target_cu);
17480 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17481 prop->set_loclist (baton);
17482 gdb_assert (prop->baton () != NULL);
17483 }
17484 else if (target_attr->form_is_block ())
17485 {
17486 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17487 baton->property_type = die_type (target_die, target_cu);
17488 baton->locexpr.per_cu = cu->per_cu;
17489 baton->locexpr.per_objfile = per_objfile;
17490 struct dwarf_block *block = target_attr->as_block ();
17491 baton->locexpr.size = block->size;
17492 baton->locexpr.data = block->data;
17493 baton->locexpr.is_reference = true;
17494 prop->set_locexpr (baton);
17495 gdb_assert (prop->baton () != NULL);
17496 }
17497 else
17498 {
17499 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17500 "dynamic property");
17501 return 0;
17502 }
17503 break;
17504 case DW_AT_data_member_location:
17505 case DW_AT_data_bit_offset:
17506 {
17507 LONGEST offset;
17508
17509 if (!handle_member_location (target_die, target_cu, &offset))
17510 return 0;
17511
17512 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17513 baton->property_type = read_type_die (target_die->parent,
17514 target_cu);
17515 baton->offset_info.offset = offset;
17516 baton->offset_info.type = die_type (target_die, target_cu);
17517 prop->set_addr_offset (baton);
17518 break;
17519 }
17520 }
17521 }
17522 else if (attr->form_is_constant ())
17523 prop->set_const_val (attr->constant_value (0));
17524 else if (attr->form_is_section_offset ())
17525 {
17526 switch (attr->name)
17527 {
17528 case DW_AT_string_length:
17529 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17530 baton->property_type = default_type;
17531 fill_in_loclist_baton (cu, &baton->loclist, attr);
17532 prop->set_loclist (baton);
17533 gdb_assert (prop->baton () != NULL);
17534 break;
17535 default:
17536 goto invalid;
17537 }
17538 }
17539 else
17540 goto invalid;
17541
17542 return 1;
17543
17544 invalid:
17545 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17546 dwarf2_name (die, cu));
17547 return 0;
17548 }
17549
17550 /* See read.h. */
17551
17552 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17553 present (which is valid) then compute the default type based on the
17554 compilation units address size. */
17555
17556 static struct type *
17557 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17558 {
17559 struct type *index_type = die_type (die, cu);
17560
17561 /* Dwarf-2 specifications explicitly allows to create subrange types
17562 without specifying a base type.
17563 In that case, the base type must be set to the type of
17564 the lower bound, upper bound or count, in that order, if any of these
17565 three attributes references an object that has a type.
17566 If no base type is found, the Dwarf-2 specifications say that
17567 a signed integer type of size equal to the size of an address should
17568 be used.
17569 For the following C code: `extern char gdb_int [];'
17570 GCC produces an empty range DIE.
17571 FIXME: muller/2010-05-28: Possible references to object for low bound,
17572 high bound or count are not yet handled by this code. */
17573 if (index_type->code () == TYPE_CODE_VOID)
17574 index_type = cu->addr_sized_int_type (false);
17575
17576 return index_type;
17577 }
17578
17579 /* Read the given DW_AT_subrange DIE. */
17580
17581 static struct type *
17582 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17583 {
17584 struct type *base_type, *orig_base_type;
17585 struct type *range_type;
17586 struct attribute *attr;
17587 struct dynamic_prop low, high;
17588 int low_default_is_valid;
17589 int high_bound_is_count = 0;
17590 const char *name;
17591 ULONGEST negative_mask;
17592
17593 orig_base_type = read_subrange_index_type (die, cu);
17594
17595 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17596 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17597 creating the range type, but we use the result of check_typedef
17598 when examining properties of the type. */
17599 base_type = check_typedef (orig_base_type);
17600
17601 /* The die_type call above may have already set the type for this DIE. */
17602 range_type = get_die_type (die, cu);
17603 if (range_type)
17604 return range_type;
17605
17606 high.set_const_val (0);
17607
17608 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17609 omitting DW_AT_lower_bound. */
17610 switch (cu->lang ())
17611 {
17612 case language_c:
17613 case language_cplus:
17614 low.set_const_val (0);
17615 low_default_is_valid = 1;
17616 break;
17617 case language_fortran:
17618 low.set_const_val (1);
17619 low_default_is_valid = 1;
17620 break;
17621 case language_d:
17622 case language_objc:
17623 case language_rust:
17624 low.set_const_val (0);
17625 low_default_is_valid = (cu->header.version >= 4);
17626 break;
17627 case language_ada:
17628 case language_m2:
17629 case language_pascal:
17630 low.set_const_val (1);
17631 low_default_is_valid = (cu->header.version >= 4);
17632 break;
17633 default:
17634 low.set_const_val (0);
17635 low_default_is_valid = 0;
17636 break;
17637 }
17638
17639 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17640 if (attr != nullptr)
17641 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17642 else if (!low_default_is_valid)
17643 complaint (_("Missing DW_AT_lower_bound "
17644 "- DIE at %s [in module %s]"),
17645 sect_offset_str (die->sect_off),
17646 objfile_name (cu->per_objfile->objfile));
17647
17648 struct attribute *attr_ub, *attr_count;
17649 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17650 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17651 {
17652 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17653 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17654 {
17655 /* If bounds are constant do the final calculation here. */
17656 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
17657 high.set_const_val (low.const_val () + high.const_val () - 1);
17658 else
17659 high_bound_is_count = 1;
17660 }
17661 else
17662 {
17663 if (attr_ub != NULL)
17664 complaint (_("Unresolved DW_AT_upper_bound "
17665 "- DIE at %s [in module %s]"),
17666 sect_offset_str (die->sect_off),
17667 objfile_name (cu->per_objfile->objfile));
17668 if (attr_count != NULL)
17669 complaint (_("Unresolved DW_AT_count "
17670 "- DIE at %s [in module %s]"),
17671 sect_offset_str (die->sect_off),
17672 objfile_name (cu->per_objfile->objfile));
17673 }
17674 }
17675
17676 LONGEST bias = 0;
17677 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17678 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17679 bias = bias_attr->constant_value (0);
17680
17681 /* Normally, the DWARF producers are expected to use a signed
17682 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17683 But this is unfortunately not always the case, as witnessed
17684 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17685 is used instead. To work around that ambiguity, we treat
17686 the bounds as signed, and thus sign-extend their values, when
17687 the base type is signed. */
17688 negative_mask =
17689 -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
17690 if (low.kind () == PROP_CONST
17691 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
17692 low.set_const_val (low.const_val () | negative_mask);
17693 if (high.kind () == PROP_CONST
17694 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
17695 high.set_const_val (high.const_val () | negative_mask);
17696
17697 /* Check for bit and byte strides. */
17698 struct dynamic_prop byte_stride_prop;
17699 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17700 if (attr_byte_stride != nullptr)
17701 {
17702 struct type *prop_type = cu->addr_sized_int_type (false);
17703 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17704 prop_type);
17705 }
17706
17707 struct dynamic_prop bit_stride_prop;
17708 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17709 if (attr_bit_stride != nullptr)
17710 {
17711 /* It only makes sense to have either a bit or byte stride. */
17712 if (attr_byte_stride != nullptr)
17713 {
17714 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17715 "- DIE at %s [in module %s]"),
17716 sect_offset_str (die->sect_off),
17717 objfile_name (cu->per_objfile->objfile));
17718 attr_bit_stride = nullptr;
17719 }
17720 else
17721 {
17722 struct type *prop_type = cu->addr_sized_int_type (false);
17723 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17724 prop_type);
17725 }
17726 }
17727
17728 if (attr_byte_stride != nullptr
17729 || attr_bit_stride != nullptr)
17730 {
17731 bool byte_stride_p = (attr_byte_stride != nullptr);
17732 struct dynamic_prop *stride
17733 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17734
17735 range_type
17736 = create_range_type_with_stride (NULL, orig_base_type, &low,
17737 &high, bias, stride, byte_stride_p);
17738 }
17739 else
17740 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17741
17742 if (high_bound_is_count)
17743 range_type->bounds ()->flag_upper_bound_is_count = 1;
17744
17745 /* Ada expects an empty array on no boundary attributes. */
17746 if (attr == NULL && cu->lang () != language_ada)
17747 range_type->bounds ()->high.set_undefined ();
17748
17749 name = dwarf2_name (die, cu);
17750 if (name)
17751 range_type->set_name (name);
17752
17753 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17754 if (attr != nullptr)
17755 range_type->set_length (attr->constant_value (0));
17756
17757 maybe_set_alignment (cu, die, range_type);
17758
17759 set_die_type (die, range_type, cu);
17760
17761 /* set_die_type should be already done. */
17762 set_descriptive_type (range_type, die, cu);
17763
17764 return range_type;
17765 }
17766
17767 static struct type *
17768 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17769 {
17770 struct type *type;
17771
17772 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17773 type->set_name (dwarf2_name (die, cu));
17774
17775 /* In Ada, an unspecified type is typically used when the description
17776 of the type is deferred to a different unit. When encountering
17777 such a type, we treat it as a stub, and try to resolve it later on,
17778 when needed.
17779 Mark this as a stub type for all languages though. */
17780 type->set_is_stub (true);
17781
17782 return set_die_type (die, type, cu);
17783 }
17784
17785 /* Read a single die and all its descendents. Set the die's sibling
17786 field to NULL; set other fields in the die correctly, and set all
17787 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17788 location of the info_ptr after reading all of those dies. PARENT
17789 is the parent of the die in question. */
17790
17791 static struct die_info *
17792 read_die_and_children (const struct die_reader_specs *reader,
17793 const gdb_byte *info_ptr,
17794 const gdb_byte **new_info_ptr,
17795 struct die_info *parent)
17796 {
17797 struct die_info *die;
17798 const gdb_byte *cur_ptr;
17799
17800 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17801 if (die == NULL)
17802 {
17803 *new_info_ptr = cur_ptr;
17804 return NULL;
17805 }
17806 store_in_ref_table (die, reader->cu);
17807
17808 if (die->has_children)
17809 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17810 else
17811 {
17812 die->child = NULL;
17813 *new_info_ptr = cur_ptr;
17814 }
17815
17816 die->sibling = NULL;
17817 die->parent = parent;
17818 return die;
17819 }
17820
17821 /* Read a die, all of its descendents, and all of its siblings; set
17822 all of the fields of all of the dies correctly. Arguments are as
17823 in read_die_and_children. */
17824
17825 static struct die_info *
17826 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17827 const gdb_byte *info_ptr,
17828 const gdb_byte **new_info_ptr,
17829 struct die_info *parent)
17830 {
17831 struct die_info *first_die, *last_sibling;
17832 const gdb_byte *cur_ptr;
17833
17834 cur_ptr = info_ptr;
17835 first_die = last_sibling = NULL;
17836
17837 while (1)
17838 {
17839 struct die_info *die
17840 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17841
17842 if (die == NULL)
17843 {
17844 *new_info_ptr = cur_ptr;
17845 return first_die;
17846 }
17847
17848 if (!first_die)
17849 first_die = die;
17850 else
17851 last_sibling->sibling = die;
17852
17853 last_sibling = die;
17854 }
17855 }
17856
17857 /* Read a die, all of its descendents, and all of its siblings; set
17858 all of the fields of all of the dies correctly. Arguments are as
17859 in read_die_and_children.
17860 This the main entry point for reading a DIE and all its children. */
17861
17862 static struct die_info *
17863 read_die_and_siblings (const struct die_reader_specs *reader,
17864 const gdb_byte *info_ptr,
17865 const gdb_byte **new_info_ptr,
17866 struct die_info *parent)
17867 {
17868 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17869 new_info_ptr, parent);
17870
17871 if (dwarf_die_debug)
17872 {
17873 gdb_printf (gdb_stdlog,
17874 "Read die from %s@0x%x of %s:\n",
17875 reader->die_section->get_name (),
17876 (unsigned) (info_ptr - reader->die_section->buffer),
17877 bfd_get_filename (reader->abfd));
17878 dump_die (die, dwarf_die_debug);
17879 }
17880
17881 return die;
17882 }
17883
17884 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17885 attributes.
17886 The caller is responsible for filling in the extra attributes
17887 and updating (*DIEP)->num_attrs.
17888 Set DIEP to point to a newly allocated die with its information,
17889 except for its child, sibling, and parent fields. */
17890
17891 static const gdb_byte *
17892 read_full_die_1 (const struct die_reader_specs *reader,
17893 struct die_info **diep, const gdb_byte *info_ptr,
17894 int num_extra_attrs)
17895 {
17896 unsigned int abbrev_number, bytes_read, i;
17897 const struct abbrev_info *abbrev;
17898 struct die_info *die;
17899 struct dwarf2_cu *cu = reader->cu;
17900 bfd *abfd = reader->abfd;
17901
17902 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17903 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17904 info_ptr += bytes_read;
17905 if (!abbrev_number)
17906 {
17907 *diep = NULL;
17908 return info_ptr;
17909 }
17910
17911 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17912 if (!abbrev)
17913 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17914 abbrev_number,
17915 bfd_get_filename (abfd));
17916
17917 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17918 die->sect_off = sect_off;
17919 die->tag = abbrev->tag;
17920 die->abbrev = abbrev_number;
17921 die->has_children = abbrev->has_children;
17922
17923 /* Make the result usable.
17924 The caller needs to update num_attrs after adding the extra
17925 attributes. */
17926 die->num_attrs = abbrev->num_attrs;
17927
17928 bool any_need_reprocess = false;
17929 for (i = 0; i < abbrev->num_attrs; ++i)
17930 {
17931 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17932 info_ptr);
17933 if (die->attrs[i].requires_reprocessing_p ())
17934 any_need_reprocess = true;
17935 }
17936
17937 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17938 if (attr != nullptr && attr->form_is_unsigned ())
17939 cu->str_offsets_base = attr->as_unsigned ();
17940
17941 attr = die->attr (DW_AT_loclists_base);
17942 if (attr != nullptr)
17943 cu->loclist_base = attr->as_unsigned ();
17944
17945 auto maybe_addr_base = die->addr_base ();
17946 if (maybe_addr_base.has_value ())
17947 cu->addr_base = *maybe_addr_base;
17948
17949 attr = die->attr (DW_AT_rnglists_base);
17950 if (attr != nullptr)
17951 cu->rnglists_base = attr->as_unsigned ();
17952
17953 if (any_need_reprocess)
17954 {
17955 for (i = 0; i < abbrev->num_attrs; ++i)
17956 {
17957 if (die->attrs[i].requires_reprocessing_p ())
17958 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
17959 }
17960 }
17961 *diep = die;
17962 return info_ptr;
17963 }
17964
17965 /* Read a die and all its attributes.
17966 Set DIEP to point to a newly allocated die with its information,
17967 except for its child, sibling, and parent fields. */
17968
17969 static const gdb_byte *
17970 read_full_die (const struct die_reader_specs *reader,
17971 struct die_info **diep, const gdb_byte *info_ptr)
17972 {
17973 const gdb_byte *result;
17974
17975 result = read_full_die_1 (reader, diep, info_ptr, 0);
17976
17977 if (dwarf_die_debug)
17978 {
17979 gdb_printf (gdb_stdlog,
17980 "Read die from %s@0x%x of %s:\n",
17981 reader->die_section->get_name (),
17982 (unsigned) (info_ptr - reader->die_section->buffer),
17983 bfd_get_filename (reader->abfd));
17984 dump_die (*diep, dwarf_die_debug);
17985 }
17986
17987 return result;
17988 }
17989 \f
17990
17991 void
17992 cooked_indexer::check_bounds (cutu_reader *reader)
17993 {
17994 if (reader->cu->per_cu->addresses_seen)
17995 return;
17996
17997 dwarf2_cu *cu = reader->cu;
17998
17999 CORE_ADDR best_lowpc = 0, best_highpc = 0;
18000 /* Possibly set the default values of LOWPC and HIGHPC from
18001 `DW_AT_ranges'. */
18002 dwarf2_find_base_address (reader->comp_unit_die, cu);
18003 enum pc_bounds_kind cu_bounds_kind
18004 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
18005 cu, m_index_storage->get_addrmap (), cu->per_cu);
18006 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
18007 {
18008 struct objfile *objfile = cu->per_objfile->objfile;
18009 CORE_ADDR baseaddr = objfile->text_section_offset ();
18010 struct gdbarch *gdbarch = objfile->arch ();
18011 CORE_ADDR low
18012 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
18013 - baseaddr);
18014 CORE_ADDR high
18015 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
18016 - baseaddr - 1);
18017 /* Store the contiguous range if it is not empty; it can be
18018 empty for CUs with no code. */
18019 m_index_storage->get_addrmap ()->set_empty (low, high, cu->per_cu);
18020
18021 cu->per_cu->addresses_seen = true;
18022 }
18023 }
18024
18025 /* Helper function that returns true if TAG can have a linkage
18026 name. */
18027
18028 static bool
18029 tag_can_have_linkage_name (enum dwarf_tag tag)
18030 {
18031 switch (tag)
18032 {
18033 case DW_TAG_variable:
18034 case DW_TAG_subprogram:
18035 return true;
18036
18037 default:
18038 return false;
18039 }
18040 }
18041
18042 cutu_reader *
18043 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
18044 dwarf2_per_objfile *per_objfile,
18045 sect_offset sect_off, bool is_dwz,
18046 bool for_scanning)
18047 {
18048 /* Lookups for type unit references are always in the CU, and
18049 cross-CU references will crash. */
18050 if (reader->cu->per_cu->is_dwz == is_dwz
18051 && reader->cu->header.offset_in_cu_p (sect_off))
18052 return reader;
18053
18054 dwarf2_per_cu_data *per_cu
18055 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
18056 per_objfile->per_bfd);
18057
18058 /* When scanning, we only want to visit a given CU a single time.
18059 Doing this check here avoids self-imports as well. */
18060 if (for_scanning)
18061 {
18062 bool nope = false;
18063 if (!per_cu->scanned.compare_exchange_strong (nope, true))
18064 return nullptr;
18065 }
18066 if (per_cu == m_per_cu)
18067 return reader;
18068
18069 cutu_reader *result = m_index_storage->get_reader (per_cu);
18070 if (result == nullptr)
18071 {
18072 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
18073 m_index_storage->get_abbrev_cache ());
18074
18075 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
18076 language_minimal);
18077 std::unique_ptr<cutu_reader> copy
18078 (new cutu_reader (std::move (new_reader)));
18079 result = m_index_storage->preserve (std::move (copy));
18080 }
18081
18082 if (result->dummy_p || !result->comp_unit_die->has_children)
18083 return nullptr;
18084
18085 if (for_scanning)
18086 check_bounds (result);
18087
18088 return result;
18089 }
18090
18091 const gdb_byte *
18092 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
18093 cutu_reader *reader,
18094 const gdb_byte *watermark_ptr,
18095 const gdb_byte *info_ptr,
18096 const abbrev_info *abbrev,
18097 const char **name,
18098 const char **linkage_name,
18099 cooked_index_flag *flags,
18100 sect_offset *sibling_offset,
18101 const cooked_index_entry **parent_entry,
18102 CORE_ADDR *maybe_defer,
18103 bool for_specification)
18104 {
18105 bool origin_is_dwz = false;
18106 bool is_declaration = false;
18107 sect_offset origin_offset {};
18108
18109 gdb::optional<CORE_ADDR> low_pc;
18110 gdb::optional<CORE_ADDR> high_pc;
18111 bool high_pc_relative = false;
18112
18113 for (int i = 0; i < abbrev->num_attrs; ++i)
18114 {
18115 attribute attr;
18116 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18117 if (attr.requires_reprocessing_p ())
18118 read_attribute_reprocess (reader, &attr, abbrev->tag);
18119
18120 /* Store the data if it is of an attribute we want to keep in a
18121 partial symbol table. */
18122 switch (attr.name)
18123 {
18124 case DW_AT_name:
18125 switch (abbrev->tag)
18126 {
18127 case DW_TAG_compile_unit:
18128 case DW_TAG_partial_unit:
18129 case DW_TAG_type_unit:
18130 /* Compilation units have a DW_AT_name that is a filename, not
18131 a source language identifier. */
18132 break;
18133
18134 default:
18135 if (*name == nullptr)
18136 *name = attr.as_string ();
18137 break;
18138 }
18139 break;
18140
18141 case DW_AT_linkage_name:
18142 case DW_AT_MIPS_linkage_name:
18143 /* Note that both forms of linkage name might appear. We
18144 assume they will be the same, and we only store the last
18145 one we see. */
18146 if (*linkage_name == nullptr)
18147 *linkage_name = attr.as_string ();
18148 break;
18149
18150 case DW_AT_main_subprogram:
18151 if (attr.as_boolean ())
18152 *flags |= IS_MAIN;
18153 break;
18154
18155 case DW_AT_declaration:
18156 is_declaration = attr.as_boolean ();
18157 break;
18158
18159 case DW_AT_sibling:
18160 if (sibling_offset != nullptr)
18161 *sibling_offset = attr.get_ref_die_offset ();
18162 break;
18163
18164 case DW_AT_specification:
18165 case DW_AT_abstract_origin:
18166 case DW_AT_extension:
18167 origin_offset = attr.get_ref_die_offset ();
18168 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
18169 break;
18170
18171 case DW_AT_external:
18172 if (attr.as_boolean ())
18173 *flags &= ~IS_STATIC;
18174 break;
18175
18176 case DW_AT_enum_class:
18177 if (attr.as_boolean ())
18178 *flags |= IS_ENUM_CLASS;
18179 break;
18180
18181 case DW_AT_low_pc:
18182 low_pc = attr.as_address ();
18183 break;
18184
18185 case DW_AT_high_pc:
18186 high_pc = attr.as_address ();
18187 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
18188 high_pc_relative = true;
18189 break;
18190
18191 case DW_AT_location:
18192 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
18193 {
18194 struct dwarf_block *locdesc = attr.as_block ();
18195 CORE_ADDR addr = decode_locdesc (locdesc, reader->cu);
18196 if (addr != 0
18197 || reader->cu->per_objfile->per_bfd->has_section_at_zero)
18198 {
18199 low_pc = addr;
18200 /* For variables, we don't want to try decoding the
18201 type just to find the size -- for gdb's purposes
18202 we only need the address of a variable. */
18203 high_pc = addr + 1;
18204 high_pc_relative = false;
18205 }
18206 }
18207 break;
18208
18209 case DW_AT_ranges:
18210 if (!scanning_per_cu->addresses_seen)
18211 {
18212 /* Offset in the .debug_ranges or .debug_rnglist section
18213 (depending on DWARF version). */
18214 ULONGEST ranges_offset = attr.as_unsigned ();
18215
18216 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18217 want to add this value. */
18218 ranges_offset += reader->cu->gnu_ranges_base;
18219
18220 CORE_ADDR lowpc, highpc;
18221 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
18222 m_index_storage->get_addrmap (),
18223 scanning_per_cu, abbrev->tag);
18224 }
18225 break;
18226 }
18227 }
18228
18229 /* We don't want to examine declarations, but if we found a
18230 declaration when handling DW_AT_specification or the like, then
18231 that is ok. Similarly, we allow an external variable without a
18232 location; those are resolved via minimal symbols. */
18233 if (is_declaration && !for_specification
18234 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
18235 {
18236 /* We always want to recurse into some types, but we may not
18237 want to treat them as definitions. */
18238 if ((abbrev->tag == DW_TAG_class_type
18239 || abbrev->tag == DW_TAG_structure_type
18240 || abbrev->tag == DW_TAG_union_type)
18241 && abbrev->has_children)
18242 *flags |= IS_TYPE_DECLARATION;
18243 else
18244 {
18245 *linkage_name = nullptr;
18246 *name = nullptr;
18247 }
18248 }
18249 else if ((*name == nullptr
18250 || (*linkage_name == nullptr
18251 && tag_can_have_linkage_name (abbrev->tag))
18252 || (*parent_entry == nullptr && m_language != language_c))
18253 && origin_offset != sect_offset (0))
18254 {
18255 cutu_reader *new_reader
18256 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
18257 origin_is_dwz, false);
18258 if (new_reader != nullptr)
18259 {
18260 const gdb_byte *new_info_ptr = (new_reader->buffer
18261 + to_underlying (origin_offset));
18262
18263 if (new_reader->cu == reader->cu
18264 && new_info_ptr > watermark_ptr
18265 && *parent_entry == nullptr)
18266 *maybe_defer = form_addr (origin_offset, origin_is_dwz);
18267 else if (*parent_entry == nullptr)
18268 {
18269 CORE_ADDR lookup = form_addr (origin_offset, origin_is_dwz);
18270 *parent_entry
18271 = (cooked_index_entry *) m_die_range_map.find (lookup);
18272 }
18273
18274 unsigned int bytes_read;
18275 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
18276 new_info_ptr,
18277 &bytes_read);
18278 new_info_ptr += bytes_read;
18279 scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
18280 new_abbrev, name, linkage_name, flags, nullptr,
18281 parent_entry, maybe_defer, true);
18282 }
18283 }
18284
18285 if (!for_specification)
18286 {
18287 if (m_language == language_ada
18288 && *linkage_name == nullptr)
18289 *linkage_name = *name;
18290
18291 if (!scanning_per_cu->addresses_seen
18292 && low_pc.has_value ()
18293 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
18294 || *low_pc != 0)
18295 && high_pc.has_value ())
18296 {
18297 if (high_pc_relative)
18298 high_pc = *high_pc + *low_pc;
18299
18300 if (*high_pc > *low_pc)
18301 {
18302 struct objfile *objfile = reader->cu->per_objfile->objfile;
18303 CORE_ADDR baseaddr = objfile->text_section_offset ();
18304 struct gdbarch *gdbarch = objfile->arch ();
18305 CORE_ADDR lo
18306 = (gdbarch_adjust_dwarf2_addr (gdbarch, *low_pc + baseaddr)
18307 - baseaddr);
18308 CORE_ADDR hi
18309 = (gdbarch_adjust_dwarf2_addr (gdbarch, *high_pc + baseaddr)
18310 - baseaddr);
18311 m_index_storage->get_addrmap ()->set_empty (lo, hi - 1,
18312 scanning_per_cu);
18313 }
18314 }
18315
18316 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
18317 *flags &= ~IS_STATIC;
18318
18319 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
18320 *name = "(anonymous namespace)";
18321
18322 if (m_language == language_cplus
18323 && (abbrev->tag == DW_TAG_class_type
18324 || abbrev->tag == DW_TAG_interface_type
18325 || abbrev->tag == DW_TAG_structure_type
18326 || abbrev->tag == DW_TAG_union_type
18327 || abbrev->tag == DW_TAG_enumeration_type
18328 || abbrev->tag == DW_TAG_enumerator))
18329 *flags &= ~IS_STATIC;
18330 }
18331
18332 return info_ptr;
18333 }
18334
18335 const gdb_byte *
18336 cooked_indexer::index_imported_unit (cutu_reader *reader,
18337 const gdb_byte *info_ptr,
18338 const abbrev_info *abbrev)
18339 {
18340 sect_offset sect_off {};
18341 bool is_dwz = false;
18342
18343 for (int i = 0; i < abbrev->num_attrs; ++i)
18344 {
18345 /* Note that we never need to reprocess attributes here. */
18346 attribute attr;
18347 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
18348
18349 if (attr.name == DW_AT_import)
18350 {
18351 sect_off = attr.get_ref_die_offset ();
18352 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18353 || reader->cu->per_cu->is_dwz);
18354 }
18355 }
18356
18357 /* Did not find DW_AT_import. */
18358 if (sect_off == sect_offset (0))
18359 return info_ptr;
18360
18361 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
18362 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
18363 is_dwz, true);
18364 if (new_reader != nullptr)
18365 {
18366 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
18367
18368 reader->cu->add_dependence (new_reader->cu->per_cu);
18369 }
18370
18371 return info_ptr;
18372 }
18373
18374 const gdb_byte *
18375 cooked_indexer::recurse (cutu_reader *reader,
18376 const gdb_byte *info_ptr,
18377 const cooked_index_entry *parent_entry,
18378 bool fully)
18379 {
18380 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18381
18382 if (parent_entry != nullptr)
18383 {
18384 CORE_ADDR start = form_addr (parent_entry->die_offset,
18385 reader->cu->per_cu->is_dwz);
18386 CORE_ADDR end = form_addr (sect_offset (info_ptr - 1 - reader->buffer),
18387 reader->cu->per_cu->is_dwz);
18388 m_die_range_map.set_empty (start, end, (void *) parent_entry);
18389 }
18390
18391 return info_ptr;
18392 }
18393
18394 const gdb_byte *
18395 cooked_indexer::index_dies (cutu_reader *reader,
18396 const gdb_byte *info_ptr,
18397 const cooked_index_entry *parent_entry,
18398 bool fully)
18399 {
18400 const gdb_byte *end_ptr = (reader->buffer
18401 + to_underlying (reader->cu->header.sect_off)
18402 + reader->cu->header.get_length_with_initial ());
18403
18404 while (info_ptr < end_ptr)
18405 {
18406 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
18407 unsigned int bytes_read;
18408 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
18409 &bytes_read);
18410 info_ptr += bytes_read;
18411 if (abbrev == nullptr)
18412 break;
18413
18414 if (abbrev->tag == DW_TAG_imported_unit)
18415 {
18416 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
18417 continue;
18418 }
18419
18420 if (!abbrev->interesting)
18421 {
18422 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
18423 if (fully && abbrev->has_children)
18424 info_ptr = index_dies (reader, info_ptr, parent_entry, fully);
18425 continue;
18426 }
18427
18428 const char *name = nullptr;
18429 const char *linkage_name = nullptr;
18430 CORE_ADDR defer = 0;
18431 cooked_index_flag flags = IS_STATIC;
18432 sect_offset sibling {};
18433 const cooked_index_entry *this_parent_entry = parent_entry;
18434 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
18435 info_ptr, abbrev, &name, &linkage_name,
18436 &flags, &sibling, &this_parent_entry,
18437 &defer, false);
18438
18439 if (abbrev->tag == DW_TAG_namespace
18440 && m_language == language_cplus
18441 && strcmp (name, "::") == 0)
18442 {
18443 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18444 generated bogus DW_TAG_namespace DIEs with a name of "::"
18445 for the global namespace. Work around this problem
18446 here. */
18447 name = nullptr;
18448 }
18449
18450 const cooked_index_entry *this_entry = nullptr;
18451 if (name != nullptr)
18452 {
18453 if (defer != 0)
18454 m_deferred_entries.push_back ({
18455 this_die, name, defer, abbrev->tag, flags
18456 });
18457 else
18458 this_entry = m_index_storage->add (this_die, abbrev->tag, flags,
18459 name, this_parent_entry,
18460 m_per_cu);
18461 }
18462
18463 if (linkage_name != nullptr)
18464 {
18465 /* We only want this to be "main" if it has a linkage name
18466 but not an ordinary name. */
18467 if (name != nullptr)
18468 flags = flags & ~IS_MAIN;
18469 /* Set the IS_LINKAGE on for everything except when functions
18470 have linkage name present but name is absent. */
18471 if (name != nullptr
18472 || (abbrev->tag != DW_TAG_subprogram
18473 && abbrev->tag != DW_TAG_inlined_subroutine
18474 && abbrev->tag != DW_TAG_entry_point))
18475 flags = flags | IS_LINKAGE;
18476 m_index_storage->add (this_die, abbrev->tag, flags,
18477 linkage_name, nullptr, m_per_cu);
18478 }
18479
18480 if (abbrev->has_children)
18481 {
18482 switch (abbrev->tag)
18483 {
18484 case DW_TAG_class_type:
18485 case DW_TAG_interface_type:
18486 case DW_TAG_structure_type:
18487 case DW_TAG_union_type:
18488 if (m_language != language_c && this_entry != nullptr)
18489 {
18490 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18491 continue;
18492 }
18493 break;
18494
18495 case DW_TAG_enumeration_type:
18496 /* We need to recurse even for an anonymous enumeration.
18497 Which scope we record as the parent scope depends on
18498 whether we're reading an "enum class". If so, we use
18499 the enum itself as the parent, yielding names like
18500 "enum_class::enumerator"; otherwise we inject the
18501 names into our own parent scope. */
18502 info_ptr = recurse (reader, info_ptr,
18503 ((flags & IS_ENUM_CLASS) == 0)
18504 ? parent_entry
18505 : this_entry,
18506 fully);
18507 continue;
18508
18509 case DW_TAG_module:
18510 if (this_entry == nullptr)
18511 break;
18512 /* FALLTHROUGH */
18513 case DW_TAG_namespace:
18514 /* We don't check THIS_ENTRY for a namespace, to handle
18515 the ancient G++ workaround pointed out above. */
18516 info_ptr = recurse (reader, info_ptr, this_entry, fully);
18517 continue;
18518
18519 case DW_TAG_subprogram:
18520 if ((m_language == language_fortran
18521 || m_language == language_ada)
18522 && this_entry != nullptr)
18523 {
18524 info_ptr = recurse (reader, info_ptr, this_entry, true);
18525 continue;
18526 }
18527 break;
18528 }
18529
18530 if (sibling != sect_offset (0))
18531 {
18532 const gdb_byte *sibling_ptr
18533 = reader->buffer + to_underlying (sibling);
18534
18535 if (sibling_ptr < info_ptr)
18536 complaint (_("DW_AT_sibling points backwards"));
18537 else if (sibling_ptr > reader->buffer_end)
18538 reader->die_section->overflow_complaint ();
18539 else
18540 info_ptr = sibling_ptr;
18541 }
18542 else
18543 info_ptr = skip_children (reader, info_ptr);
18544 }
18545 }
18546
18547 return info_ptr;
18548 }
18549
18550 void
18551 cooked_indexer::make_index (cutu_reader *reader)
18552 {
18553 check_bounds (reader);
18554 find_file_and_directory (reader->comp_unit_die, reader->cu);
18555 if (!reader->comp_unit_die->has_children)
18556 return;
18557 index_dies (reader, reader->info_ptr, nullptr, false);
18558
18559 for (const auto &entry : m_deferred_entries)
18560 {
18561 CORE_ADDR key = form_addr (entry.die_offset, m_per_cu->is_dwz);
18562 cooked_index_entry *parent
18563 = (cooked_index_entry *) m_die_range_map.find (key);
18564 m_index_storage->add (entry.die_offset, entry.tag, entry.flags,
18565 entry.name, parent, m_per_cu);
18566 }
18567 }
18568
18569 /* An implementation of quick_symbol_functions for the cooked DWARF
18570 index. */
18571
18572 struct cooked_index_functions : public dwarf2_base_index_functions
18573 {
18574 dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
18575 CORE_ADDR adjusted_pc) override;
18576
18577 struct compunit_symtab *find_compunit_symtab_by_address
18578 (struct objfile *objfile, CORE_ADDR address) override;
18579
18580 void dump (struct objfile *objfile) override
18581 {
18582 gdb_printf ("Cooked index in use\n");
18583 }
18584
18585 void expand_matching_symbols
18586 (struct objfile *,
18587 const lookup_name_info &lookup_name,
18588 domain_enum domain,
18589 int global,
18590 symbol_compare_ftype *ordered_compare) override;
18591
18592 bool expand_symtabs_matching
18593 (struct objfile *objfile,
18594 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18595 const lookup_name_info *lookup_name,
18596 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18597 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18598 block_search_flags search_flags,
18599 domain_enum domain,
18600 enum search_domain kind) override;
18601
18602 bool can_lazily_read_symbols () override
18603 {
18604 return true;
18605 }
18606
18607 void read_partial_symbols (struct objfile *objfile) override
18608 {
18609 if (dwarf2_has_info (objfile, nullptr))
18610 dwarf2_build_psymtabs (objfile);
18611 }
18612 };
18613
18614 dwarf2_per_cu_data *
18615 cooked_index_functions::find_per_cu (dwarf2_per_bfd *per_bfd,
18616 CORE_ADDR adjusted_pc)
18617 {
18618 if (per_bfd->index_table == nullptr)
18619 return nullptr;
18620 cooked_index_vector *table
18621 = (gdb::checked_static_cast<cooked_index_vector *>
18622 (per_bfd->index_table.get ()));
18623 return table->lookup (adjusted_pc);
18624 }
18625
18626 struct compunit_symtab *
18627 cooked_index_functions::find_compunit_symtab_by_address
18628 (struct objfile *objfile, CORE_ADDR address)
18629 {
18630 if (objfile->sect_index_data == -1)
18631 return nullptr;
18632
18633 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18634 if (per_objfile->per_bfd->index_table == nullptr)
18635 return nullptr;
18636
18637 CORE_ADDR baseaddr = objfile->data_section_offset ();
18638 cooked_index_vector *table
18639 = (gdb::checked_static_cast<cooked_index_vector *>
18640 (per_objfile->per_bfd->index_table.get ()));
18641 dwarf2_per_cu_data *per_cu = table->lookup (address - baseaddr);
18642 if (per_cu == nullptr)
18643 return nullptr;
18644
18645 return dw2_instantiate_symtab (per_cu, per_objfile, false);
18646 }
18647
18648 void
18649 cooked_index_functions::expand_matching_symbols
18650 (struct objfile *objfile,
18651 const lookup_name_info &lookup_name,
18652 domain_enum domain,
18653 int global,
18654 symbol_compare_ftype *ordered_compare)
18655 {
18656 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18657 if (per_objfile->per_bfd->index_table == nullptr)
18658 return;
18659 const block_search_flags search_flags = (global
18660 ? SEARCH_GLOBAL_BLOCK
18661 : SEARCH_STATIC_BLOCK);
18662 const language_defn *lang = language_def (language_ada);
18663 symbol_name_matcher_ftype *name_match
18664 = lang->get_symbol_name_matcher (lookup_name);
18665
18666 cooked_index_vector *table
18667 = (gdb::checked_static_cast<cooked_index_vector *>
18668 (per_objfile->per_bfd->index_table.get ()));
18669 for (const cooked_index_entry *entry : table->all_entries ())
18670 {
18671 QUIT;
18672
18673 if (entry->parent_entry != nullptr)
18674 continue;
18675
18676 if (!entry->matches (search_flags)
18677 || !entry->matches (domain))
18678 continue;
18679
18680 if (name_match (entry->canonical, lookup_name, nullptr))
18681 dw2_instantiate_symtab (entry->per_cu, per_objfile, false);
18682 }
18683 }
18684
18685 bool
18686 cooked_index_functions::expand_symtabs_matching
18687 (struct objfile *objfile,
18688 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
18689 const lookup_name_info *lookup_name,
18690 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
18691 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
18692 block_search_flags search_flags,
18693 domain_enum domain,
18694 enum search_domain kind)
18695 {
18696 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
18697 if (per_objfile->per_bfd->index_table == nullptr)
18698 return true;
18699
18700 cooked_index_vector *table
18701 = (gdb::checked_static_cast<cooked_index_vector *>
18702 (per_objfile->per_bfd->index_table.get ()));
18703 table->wait ();
18704
18705 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
18706
18707 /* This invariant is documented in quick-functions.h. */
18708 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
18709 if (lookup_name == nullptr)
18710 {
18711 for (dwarf2_per_cu_data *per_cu
18712 : all_units_range (per_objfile->per_bfd))
18713 {
18714 QUIT;
18715
18716 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
18717 file_matcher,
18718 expansion_notify))
18719 return false;
18720 }
18721 return true;
18722 }
18723
18724 lookup_name_info lookup_name_without_params
18725 = lookup_name->make_ignore_params ();
18726 bool completing = lookup_name->completion_mode ();
18727
18728 /* Unique styles of language splitting. */
18729 static const enum language unique_styles[] =
18730 {
18731 /* No splitting is also a style. */
18732 language_c,
18733 /* This includes Rust. */
18734 language_cplus,
18735 /* This includes Go. */
18736 language_d,
18737 language_ada
18738 };
18739
18740 for (enum language lang : unique_styles)
18741 {
18742 std::vector<gdb::string_view> name_vec
18743 = lookup_name_without_params.split_name (lang);
18744
18745 for (const cooked_index_entry *entry : table->find (name_vec.back (),
18746 completing))
18747 {
18748 QUIT;
18749
18750 /* No need to consider symbols from expanded CUs. */
18751 if (per_objfile->symtab_set_p (entry->per_cu))
18752 continue;
18753
18754 /* If file-matching was done, we don't need to consider
18755 symbols from unmarked CUs. */
18756 if (file_matcher != nullptr && !entry->per_cu->mark)
18757 continue;
18758
18759 /* See if the symbol matches the type filter. */
18760 if (!entry->matches (search_flags)
18761 || !entry->matches (domain)
18762 || !entry->matches (kind))
18763 continue;
18764
18765 /* We've found the base name of the symbol; now walk its
18766 parentage chain, ensuring that each component
18767 matches. */
18768 bool found = true;
18769
18770 const cooked_index_entry *parent = entry->parent_entry;
18771 for (int i = name_vec.size () - 1; i > 0; --i)
18772 {
18773 /* If we ran out of entries, or if this segment doesn't
18774 match, this did not match. */
18775 if (parent == nullptr
18776 || strncmp (parent->name, name_vec[i - 1].data (),
18777 name_vec[i - 1].length ()) != 0)
18778 {
18779 found = false;
18780 break;
18781 }
18782
18783 parent = parent->parent_entry;
18784 }
18785
18786 if (!found)
18787 continue;
18788
18789 /* Might have been looking for "a::b" and found
18790 "x::a::b". */
18791 if (symbol_matcher == nullptr)
18792 {
18793 symbol_name_match_type match_type
18794 = lookup_name_without_params.match_type ();
18795 if ((match_type == symbol_name_match_type::FULL
18796 || (lang != language_ada
18797 && match_type == symbol_name_match_type::EXPRESSION))
18798 && parent != nullptr)
18799 continue;
18800 }
18801 else
18802 {
18803 auto_obstack temp_storage;
18804 const char *full_name = entry->full_name (&temp_storage);
18805 if (!symbol_matcher (full_name))
18806 continue;
18807 }
18808
18809 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
18810 file_matcher,
18811 expansion_notify))
18812 return false;
18813 }
18814 }
18815
18816 return true;
18817 }
18818
18819 /* Return a new cooked_index_functions object. */
18820
18821 static quick_symbol_functions_up
18822 make_cooked_index_funcs ()
18823 {
18824 return quick_symbol_functions_up (new cooked_index_functions);
18825 }
18826
18827 quick_symbol_functions_up
18828 cooked_index_vector::make_quick_functions () const
18829 {
18830 return make_cooked_index_funcs ();
18831 }
18832
18833 \f
18834
18835 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18836 contents from the given SECTION in the HEADER.
18837
18838 HEADER_OFFSET is the offset of the header in the section. */
18839 static void
18840 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
18841 struct dwarf2_section_info *section,
18842 sect_offset header_offset)
18843 {
18844 unsigned int bytes_read;
18845 bfd *abfd = section->get_bfd_owner ();
18846 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
18847
18848 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18849 info_ptr += bytes_read;
18850
18851 header->version = read_2_bytes (abfd, info_ptr);
18852 info_ptr += 2;
18853
18854 header->addr_size = read_1_byte (abfd, info_ptr);
18855 info_ptr += 1;
18856
18857 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18858 info_ptr += 1;
18859
18860 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18861 }
18862
18863 /* Return the DW_AT_loclists_base value for the CU. */
18864 static ULONGEST
18865 lookup_loclist_base (struct dwarf2_cu *cu)
18866 {
18867 /* For the .dwo unit, the loclist_base points to the first offset following
18868 the header. The header consists of the following entities-
18869 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18870 bit format)
18871 2. version (2 bytes)
18872 3. address size (1 byte)
18873 4. segment selector size (1 byte)
18874 5. offset entry count (4 bytes)
18875 These sizes are derived as per the DWARFv5 standard. */
18876 if (cu->dwo_unit != nullptr)
18877 {
18878 if (cu->header.initial_length_size == 4)
18879 return LOCLIST_HEADER_SIZE32;
18880 return LOCLIST_HEADER_SIZE64;
18881 }
18882 return cu->loclist_base;
18883 }
18884
18885 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18886 array of offsets in the .debug_loclists section. */
18887
18888 static sect_offset
18889 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18890 {
18891 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18892 struct objfile *objfile = per_objfile->objfile;
18893 bfd *abfd = objfile->obfd.get ();
18894 ULONGEST loclist_header_size =
18895 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
18896 : LOCLIST_HEADER_SIZE64);
18897 ULONGEST loclist_base = lookup_loclist_base (cu);
18898
18899 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18900 ULONGEST start_offset =
18901 loclist_base + loclist_index * cu->header.offset_size;
18902
18903 /* Get loclists section. */
18904 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18905
18906 /* Read the loclists section content. */
18907 section->read (objfile);
18908 if (section->buffer == NULL)
18909 error (_("DW_FORM_loclistx used without .debug_loclists "
18910 "section [in module %s]"), objfile_name (objfile));
18911
18912 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18913 so if loclist_base is smaller than the header size, we have a problem. */
18914 if (loclist_base < loclist_header_size)
18915 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18916 objfile_name (objfile));
18917
18918 /* Read the header of the loclists contribution. */
18919 struct loclists_rnglists_header header;
18920 read_loclists_rnglists_header (&header, section,
18921 (sect_offset) (loclist_base - loclist_header_size));
18922
18923 /* Verify the loclist index is valid. */
18924 if (loclist_index >= header.offset_entry_count)
18925 error (_("DW_FORM_loclistx pointing outside of "
18926 ".debug_loclists offset array [in module %s]"),
18927 objfile_name (objfile));
18928
18929 /* Validate that reading won't go beyond the end of the section. */
18930 if (start_offset + cu->header.offset_size > section->size)
18931 error (_("Reading DW_FORM_loclistx index beyond end of"
18932 ".debug_loclists section [in module %s]"),
18933 objfile_name (objfile));
18934
18935 const gdb_byte *info_ptr = section->buffer + start_offset;
18936
18937 if (cu->header.offset_size == 4)
18938 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
18939 else
18940 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
18941 }
18942
18943 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18944 array of offsets in the .debug_rnglists section. */
18945
18946 static sect_offset
18947 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
18948 dwarf_tag tag)
18949 {
18950 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18951 struct objfile *objfile = dwarf2_per_objfile->objfile;
18952 bfd *abfd = objfile->obfd.get ();
18953 ULONGEST rnglist_header_size =
18954 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
18955 : RNGLIST_HEADER_SIZE64);
18956
18957 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18958 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18959 doesn't apply. */
18960 ULONGEST rnglist_base =
18961 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
18962
18963 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18964 ULONGEST start_offset =
18965 rnglist_base + rnglist_index * cu->header.offset_size;
18966
18967 /* Get rnglists section. */
18968 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
18969
18970 /* Read the rnglists section content. */
18971 section->read (objfile);
18972 if (section->buffer == nullptr)
18973 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18974 "[in module %s]"),
18975 objfile_name (objfile));
18976
18977 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18978 so if rnglist_base is smaller than the header size, we have a problem. */
18979 if (rnglist_base < rnglist_header_size)
18980 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18981 objfile_name (objfile));
18982
18983 /* Read the header of the rnglists contribution. */
18984 struct loclists_rnglists_header header;
18985 read_loclists_rnglists_header (&header, section,
18986 (sect_offset) (rnglist_base - rnglist_header_size));
18987
18988 /* Verify the rnglist index is valid. */
18989 if (rnglist_index >= header.offset_entry_count)
18990 error (_("DW_FORM_rnglistx index pointing outside of "
18991 ".debug_rnglists offset array [in module %s]"),
18992 objfile_name (objfile));
18993
18994 /* Validate that reading won't go beyond the end of the section. */
18995 if (start_offset + cu->header.offset_size > section->size)
18996 error (_("Reading DW_FORM_rnglistx index beyond end of"
18997 ".debug_rnglists section [in module %s]"),
18998 objfile_name (objfile));
18999
19000 const gdb_byte *info_ptr = section->buffer + start_offset;
19001
19002 if (cu->header.offset_size == 4)
19003 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
19004 else
19005 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
19006 }
19007
19008 /* Process the attributes that had to be skipped in the first round. These
19009 attributes are the ones that need str_offsets_base or addr_base attributes.
19010 They could not have been processed in the first round, because at the time
19011 the values of str_offsets_base or addr_base may not have been known. */
19012 static void
19013 read_attribute_reprocess (const struct die_reader_specs *reader,
19014 struct attribute *attr, dwarf_tag tag)
19015 {
19016 struct dwarf2_cu *cu = reader->cu;
19017 switch (attr->form)
19018 {
19019 case DW_FORM_addrx:
19020 case DW_FORM_GNU_addr_index:
19021 attr->set_address (read_addr_index (cu,
19022 attr->as_unsigned_reprocess ()));
19023 break;
19024 case DW_FORM_loclistx:
19025 {
19026 sect_offset loclists_sect_off
19027 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
19028
19029 attr->set_unsigned (to_underlying (loclists_sect_off));
19030 }
19031 break;
19032 case DW_FORM_rnglistx:
19033 {
19034 sect_offset rnglists_sect_off
19035 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
19036
19037 attr->set_unsigned (to_underlying (rnglists_sect_off));
19038 }
19039 break;
19040 case DW_FORM_strx:
19041 case DW_FORM_strx1:
19042 case DW_FORM_strx2:
19043 case DW_FORM_strx3:
19044 case DW_FORM_strx4:
19045 case DW_FORM_GNU_str_index:
19046 {
19047 unsigned int str_index = attr->as_unsigned_reprocess ();
19048 gdb_assert (!attr->canonical_string_p ());
19049 if (reader->dwo_file != NULL)
19050 attr->set_string_noncanonical (read_dwo_str_index (reader,
19051 str_index));
19052 else
19053 attr->set_string_noncanonical (read_stub_str_index (cu,
19054 str_index));
19055 break;
19056 }
19057 default:
19058 gdb_assert_not_reached ("Unexpected DWARF form.");
19059 }
19060 }
19061
19062 /* Read an attribute value described by an attribute form. */
19063
19064 static const gdb_byte *
19065 read_attribute_value (const struct die_reader_specs *reader,
19066 struct attribute *attr, unsigned form,
19067 LONGEST implicit_const, const gdb_byte *info_ptr)
19068 {
19069 struct dwarf2_cu *cu = reader->cu;
19070 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19071 struct objfile *objfile = per_objfile->objfile;
19072 bfd *abfd = reader->abfd;
19073 struct comp_unit_head *cu_header = &cu->header;
19074 unsigned int bytes_read;
19075 struct dwarf_block *blk;
19076
19077 attr->form = (enum dwarf_form) form;
19078 switch (form)
19079 {
19080 case DW_FORM_ref_addr:
19081 if (cu_header->version == 2)
19082 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
19083 &bytes_read));
19084 else
19085 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19086 &bytes_read));
19087 info_ptr += bytes_read;
19088 break;
19089 case DW_FORM_GNU_ref_alt:
19090 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19091 &bytes_read));
19092 info_ptr += bytes_read;
19093 break;
19094 case DW_FORM_addr:
19095 {
19096 struct gdbarch *gdbarch = objfile->arch ();
19097 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
19098 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19099 attr->set_address (addr);
19100 info_ptr += bytes_read;
19101 }
19102 break;
19103 case DW_FORM_block2:
19104 blk = dwarf_alloc_block (cu);
19105 blk->size = read_2_bytes (abfd, info_ptr);
19106 info_ptr += 2;
19107 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19108 info_ptr += blk->size;
19109 attr->set_block (blk);
19110 break;
19111 case DW_FORM_block4:
19112 blk = dwarf_alloc_block (cu);
19113 blk->size = read_4_bytes (abfd, info_ptr);
19114 info_ptr += 4;
19115 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19116 info_ptr += blk->size;
19117 attr->set_block (blk);
19118 break;
19119 case DW_FORM_data2:
19120 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
19121 info_ptr += 2;
19122 break;
19123 case DW_FORM_data4:
19124 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
19125 info_ptr += 4;
19126 break;
19127 case DW_FORM_data8:
19128 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
19129 info_ptr += 8;
19130 break;
19131 case DW_FORM_data16:
19132 blk = dwarf_alloc_block (cu);
19133 blk->size = 16;
19134 blk->data = read_n_bytes (abfd, info_ptr, 16);
19135 info_ptr += 16;
19136 attr->set_block (blk);
19137 break;
19138 case DW_FORM_sec_offset:
19139 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
19140 &bytes_read));
19141 info_ptr += bytes_read;
19142 break;
19143 case DW_FORM_loclistx:
19144 {
19145 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19146 &bytes_read));
19147 info_ptr += bytes_read;
19148 }
19149 break;
19150 case DW_FORM_string:
19151 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19152 &bytes_read));
19153 info_ptr += bytes_read;
19154 break;
19155 case DW_FORM_strp:
19156 if (!cu->per_cu->is_dwz)
19157 {
19158 attr->set_string_noncanonical
19159 (read_indirect_string (per_objfile,
19160 abfd, info_ptr, cu_header,
19161 &bytes_read));
19162 info_ptr += bytes_read;
19163 break;
19164 }
19165 /* FALLTHROUGH */
19166 case DW_FORM_line_strp:
19167 if (!cu->per_cu->is_dwz)
19168 {
19169 attr->set_string_noncanonical
19170 (per_objfile->read_line_string (info_ptr, cu_header,
19171 &bytes_read));
19172 info_ptr += bytes_read;
19173 break;
19174 }
19175 /* FALLTHROUGH */
19176 case DW_FORM_GNU_strp_alt:
19177 {
19178 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19179 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19180 &bytes_read);
19181
19182 attr->set_string_noncanonical
19183 (dwz->read_string (objfile, str_offset));
19184 info_ptr += bytes_read;
19185 }
19186 break;
19187 case DW_FORM_exprloc:
19188 case DW_FORM_block:
19189 blk = dwarf_alloc_block (cu);
19190 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19191 info_ptr += bytes_read;
19192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19193 info_ptr += blk->size;
19194 attr->set_block (blk);
19195 break;
19196 case DW_FORM_block1:
19197 blk = dwarf_alloc_block (cu);
19198 blk->size = read_1_byte (abfd, info_ptr);
19199 info_ptr += 1;
19200 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19201 info_ptr += blk->size;
19202 attr->set_block (blk);
19203 break;
19204 case DW_FORM_data1:
19205 case DW_FORM_flag:
19206 attr->set_unsigned (read_1_byte (abfd, info_ptr));
19207 info_ptr += 1;
19208 break;
19209 case DW_FORM_flag_present:
19210 attr->set_unsigned (1);
19211 break;
19212 case DW_FORM_sdata:
19213 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
19214 info_ptr += bytes_read;
19215 break;
19216 case DW_FORM_rnglistx:
19217 {
19218 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19219 &bytes_read));
19220 info_ptr += bytes_read;
19221 }
19222 break;
19223 case DW_FORM_udata:
19224 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19225 info_ptr += bytes_read;
19226 break;
19227 case DW_FORM_ref1:
19228 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19229 + read_1_byte (abfd, info_ptr)));
19230 info_ptr += 1;
19231 break;
19232 case DW_FORM_ref2:
19233 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19234 + read_2_bytes (abfd, info_ptr)));
19235 info_ptr += 2;
19236 break;
19237 case DW_FORM_ref4:
19238 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19239 + read_4_bytes (abfd, info_ptr)));
19240 info_ptr += 4;
19241 break;
19242 case DW_FORM_ref8:
19243 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19244 + read_8_bytes (abfd, info_ptr)));
19245 info_ptr += 8;
19246 break;
19247 case DW_FORM_ref_sig8:
19248 attr->set_signature (read_8_bytes (abfd, info_ptr));
19249 info_ptr += 8;
19250 break;
19251 case DW_FORM_ref_udata:
19252 attr->set_unsigned ((to_underlying (cu_header->sect_off)
19253 + read_unsigned_leb128 (abfd, info_ptr,
19254 &bytes_read)));
19255 info_ptr += bytes_read;
19256 break;
19257 case DW_FORM_indirect:
19258 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19259 info_ptr += bytes_read;
19260 if (form == DW_FORM_implicit_const)
19261 {
19262 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19263 info_ptr += bytes_read;
19264 }
19265 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19266 info_ptr);
19267 break;
19268 case DW_FORM_implicit_const:
19269 attr->set_signed (implicit_const);
19270 break;
19271 case DW_FORM_addrx:
19272 case DW_FORM_GNU_addr_index:
19273 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19274 &bytes_read));
19275 info_ptr += bytes_read;
19276 break;
19277 case DW_FORM_strx:
19278 case DW_FORM_strx1:
19279 case DW_FORM_strx2:
19280 case DW_FORM_strx3:
19281 case DW_FORM_strx4:
19282 case DW_FORM_GNU_str_index:
19283 {
19284 ULONGEST str_index;
19285 if (form == DW_FORM_strx1)
19286 {
19287 str_index = read_1_byte (abfd, info_ptr);
19288 info_ptr += 1;
19289 }
19290 else if (form == DW_FORM_strx2)
19291 {
19292 str_index = read_2_bytes (abfd, info_ptr);
19293 info_ptr += 2;
19294 }
19295 else if (form == DW_FORM_strx3)
19296 {
19297 str_index = read_3_bytes (abfd, info_ptr);
19298 info_ptr += 3;
19299 }
19300 else if (form == DW_FORM_strx4)
19301 {
19302 str_index = read_4_bytes (abfd, info_ptr);
19303 info_ptr += 4;
19304 }
19305 else
19306 {
19307 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19308 info_ptr += bytes_read;
19309 }
19310 attr->set_unsigned_reprocess (str_index);
19311 }
19312 break;
19313 default:
19314 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19315 dwarf_form_name (form),
19316 bfd_get_filename (abfd));
19317 }
19318
19319 /* Super hack. */
19320 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19321 attr->form = DW_FORM_GNU_ref_alt;
19322
19323 /* We have seen instances where the compiler tried to emit a byte
19324 size attribute of -1 which ended up being encoded as an unsigned
19325 0xffffffff. Although 0xffffffff is technically a valid size value,
19326 an object of this size seems pretty unlikely so we can relatively
19327 safely treat these cases as if the size attribute was invalid and
19328 treat them as zero by default. */
19329 if (attr->name == DW_AT_byte_size
19330 && form == DW_FORM_data4
19331 && attr->as_unsigned () >= 0xffffffff)
19332 {
19333 complaint
19334 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19335 hex_string (attr->as_unsigned ()));
19336 attr->set_unsigned (0);
19337 }
19338
19339 return info_ptr;
19340 }
19341
19342 /* Read an attribute described by an abbreviated attribute. */
19343
19344 static const gdb_byte *
19345 read_attribute (const struct die_reader_specs *reader,
19346 struct attribute *attr, const struct attr_abbrev *abbrev,
19347 const gdb_byte *info_ptr)
19348 {
19349 attr->name = abbrev->name;
19350 attr->string_is_canonical = 0;
19351 attr->requires_reprocessing = 0;
19352 return read_attribute_value (reader, attr, abbrev->form,
19353 abbrev->implicit_const, info_ptr);
19354 }
19355
19356 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19357
19358 static const char *
19359 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
19360 LONGEST str_offset)
19361 {
19362 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19363 str_offset, "DW_FORM_strp");
19364 }
19365
19366 /* Return pointer to string at .debug_str offset as read from BUF.
19367 BUF is assumed to be in a compilation unit described by CU_HEADER.
19368 Return *BYTES_READ_PTR count of bytes read from BUF. */
19369
19370 static const char *
19371 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
19372 const gdb_byte *buf,
19373 const struct comp_unit_head *cu_header,
19374 unsigned int *bytes_read_ptr)
19375 {
19376 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19377
19378 return read_indirect_string_at_offset (per_objfile, str_offset);
19379 }
19380
19381 /* See read.h. */
19382
19383 const char *
19384 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19385 unsigned int offset_size)
19386 {
19387 bfd *abfd = objfile->obfd.get ();
19388 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
19389
19390 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19391 }
19392
19393 /* See read.h. */
19394
19395 const char *
19396 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19397 const struct comp_unit_head *cu_header,
19398 unsigned int *bytes_read_ptr)
19399 {
19400 bfd *abfd = objfile->obfd.get ();
19401 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19402
19403 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19404 }
19405
19406 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19407 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19408 ADDR_SIZE is the size of addresses from the CU header. */
19409
19410 static CORE_ADDR
19411 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19412 gdb::optional<ULONGEST> addr_base, int addr_size)
19413 {
19414 struct objfile *objfile = per_objfile->objfile;
19415 bfd *abfd = objfile->obfd.get ();
19416 const gdb_byte *info_ptr;
19417 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19418
19419 per_objfile->per_bfd->addr.read (objfile);
19420 if (per_objfile->per_bfd->addr.buffer == NULL)
19421 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19422 objfile_name (objfile));
19423 if (addr_base_or_zero + addr_index * addr_size
19424 >= per_objfile->per_bfd->addr.size)
19425 error (_("DW_FORM_addr_index pointing outside of "
19426 ".debug_addr section [in module %s]"),
19427 objfile_name (objfile));
19428 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
19429 + addr_index * addr_size);
19430 if (addr_size == 4)
19431 return bfd_get_32 (abfd, info_ptr);
19432 else
19433 return bfd_get_64 (abfd, info_ptr);
19434 }
19435
19436 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19437
19438 static CORE_ADDR
19439 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19440 {
19441 return read_addr_index_1 (cu->per_objfile, addr_index,
19442 cu->addr_base, cu->header.addr_size);
19443 }
19444
19445 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19446
19447 static CORE_ADDR
19448 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19449 unsigned int *bytes_read)
19450 {
19451 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
19452 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19453
19454 return read_addr_index (cu, addr_index);
19455 }
19456
19457 /* See read.h. */
19458
19459 CORE_ADDR
19460 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19461 dwarf2_per_objfile *per_objfile,
19462 unsigned int addr_index)
19463 {
19464 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
19465 gdb::optional<ULONGEST> addr_base;
19466 int addr_size;
19467
19468 /* We need addr_base and addr_size.
19469 If we don't have PER_CU->cu, we have to get it.
19470 Nasty, but the alternative is storing the needed info in PER_CU,
19471 which at this point doesn't seem justified: it's not clear how frequently
19472 it would get used and it would increase the size of every PER_CU.
19473 Entry points like dwarf2_per_cu_addr_size do a similar thing
19474 so we're not in uncharted territory here.
19475 Alas we need to be a bit more complicated as addr_base is contained
19476 in the DIE.
19477
19478 We don't need to read the entire CU(/TU).
19479 We just need the header and top level die.
19480
19481 IWBN to use the aging mechanism to let us lazily later discard the CU.
19482 For now we skip this optimization. */
19483
19484 if (cu != NULL)
19485 {
19486 addr_base = cu->addr_base;
19487 addr_size = cu->header.addr_size;
19488 }
19489 else
19490 {
19491 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
19492 addr_base = reader.cu->addr_base;
19493 addr_size = reader.cu->header.addr_size;
19494 }
19495
19496 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
19497 }
19498
19499 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19500 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19501 DWO file. */
19502
19503 static const char *
19504 read_str_index (struct dwarf2_cu *cu,
19505 struct dwarf2_section_info *str_section,
19506 struct dwarf2_section_info *str_offsets_section,
19507 ULONGEST str_offsets_base, ULONGEST str_index,
19508 unsigned offset_size)
19509 {
19510 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19511 struct objfile *objfile = per_objfile->objfile;
19512 const char *objf_name = objfile_name (objfile);
19513 bfd *abfd = objfile->obfd.get ();
19514 const gdb_byte *info_ptr;
19515 ULONGEST str_offset;
19516 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19517
19518 str_section->read (objfile);
19519 str_offsets_section->read (objfile);
19520 if (str_section->buffer == NULL)
19521 error (_("%s used without %s section"
19522 " in CU at offset %s [in module %s]"),
19523 form_name, str_section->get_name (),
19524 sect_offset_str (cu->header.sect_off), objf_name);
19525 if (str_offsets_section->buffer == NULL)
19526 error (_("%s used without %s section"
19527 " in CU at offset %s [in module %s]"),
19528 form_name, str_section->get_name (),
19529 sect_offset_str (cu->header.sect_off), objf_name);
19530 info_ptr = (str_offsets_section->buffer
19531 + str_offsets_base
19532 + str_index * offset_size);
19533 if (offset_size == 4)
19534 str_offset = bfd_get_32 (abfd, info_ptr);
19535 else
19536 str_offset = bfd_get_64 (abfd, info_ptr);
19537 if (str_offset >= str_section->size)
19538 error (_("Offset from %s pointing outside of"
19539 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19540 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19541 return (const char *) (str_section->buffer + str_offset);
19542 }
19543
19544 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19545
19546 static const char *
19547 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19548 {
19549 unsigned offset_size;
19550 ULONGEST str_offsets_base;
19551 if (reader->cu->header.version >= 5)
19552 {
19553 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
19554 so assume the .debug_str_offsets section is DWARF5 as well, and
19555 parse the header. FIXME: Parse the header only once. */
19556 unsigned int bytes_read = 0;
19557 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
19558 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
19559
19560 /* Header: Initial length. */
19561 read_initial_length (abfd, p + bytes_read, &bytes_read);
19562
19563 /* Determine offset_size based on the .debug_str_offsets header. */
19564 const bool dwarf5_is_dwarf64 = bytes_read != 4;
19565 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
19566
19567 /* Header: Version. */
19568 unsigned version = read_2_bytes (abfd, p + bytes_read);
19569 bytes_read += 2;
19570
19571 if (version <= 4)
19572 {
19573 /* We'd like one warning here about ignoring the section, but
19574 because we parse the header more than once (see FIXME above)
19575 we'd have many warnings, so use a complaint instead, which at
19576 least has a limit. */
19577 complaint (_("Section .debug_str_offsets in %s has unsupported"
19578 " version %d, use empty string."),
19579 reader->dwo_file->dwo_name, version);
19580 return "";
19581 }
19582
19583 /* Header: Padding. */
19584 bytes_read += 2;
19585
19586 str_offsets_base = bytes_read;
19587 }
19588 else
19589 {
19590 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
19591 section, assume the .debug_str_offsets section is pre-DWARF5 as
19592 well, which doesn't have a header. */
19593 str_offsets_base = 0;
19594
19595 /* Determine offset_size based on the .debug_info header. */
19596 offset_size = reader->cu->header.offset_size;
19597 }
19598
19599 return read_str_index (reader->cu,
19600 &reader->dwo_file->sections.str,
19601 &reader->dwo_file->sections.str_offsets,
19602 str_offsets_base, str_index, offset_size);
19603 }
19604
19605 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19606
19607 static const char *
19608 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19609 {
19610 struct objfile *objfile = cu->per_objfile->objfile;
19611 const char *objf_name = objfile_name (objfile);
19612 static const char form_name[] = "DW_FORM_GNU_str_index";
19613 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19614
19615 if (!cu->str_offsets_base.has_value ())
19616 error (_("%s used in Fission stub without %s"
19617 " in CU at offset 0x%lx [in module %s]"),
19618 form_name, str_offsets_attr_name,
19619 (long) cu->header.offset_size, objf_name);
19620
19621 return read_str_index (cu,
19622 &cu->per_objfile->per_bfd->str,
19623 &cu->per_objfile->per_bfd->str_offsets,
19624 *cu->str_offsets_base, str_index,
19625 cu->header.offset_size);
19626 }
19627
19628 /* Return the length of an LEB128 number in BUF. */
19629
19630 static int
19631 leb128_size (const gdb_byte *buf)
19632 {
19633 const gdb_byte *begin = buf;
19634 gdb_byte byte;
19635
19636 while (1)
19637 {
19638 byte = *buf++;
19639 if ((byte & 128) == 0)
19640 return buf - begin;
19641 }
19642 }
19643
19644 static enum language
19645 dwarf_lang_to_enum_language (unsigned int lang)
19646 {
19647 enum language language;
19648
19649 switch (lang)
19650 {
19651 case DW_LANG_C89:
19652 case DW_LANG_C99:
19653 case DW_LANG_C11:
19654 case DW_LANG_C:
19655 case DW_LANG_UPC:
19656 language = language_c;
19657 break;
19658 case DW_LANG_Java:
19659 case DW_LANG_C_plus_plus:
19660 case DW_LANG_C_plus_plus_11:
19661 case DW_LANG_C_plus_plus_14:
19662 language = language_cplus;
19663 break;
19664 case DW_LANG_D:
19665 language = language_d;
19666 break;
19667 case DW_LANG_Fortran77:
19668 case DW_LANG_Fortran90:
19669 case DW_LANG_Fortran95:
19670 case DW_LANG_Fortran03:
19671 case DW_LANG_Fortran08:
19672 language = language_fortran;
19673 break;
19674 case DW_LANG_Go:
19675 language = language_go;
19676 break;
19677 case DW_LANG_Mips_Assembler:
19678 language = language_asm;
19679 break;
19680 case DW_LANG_Ada83:
19681 case DW_LANG_Ada95:
19682 language = language_ada;
19683 break;
19684 case DW_LANG_Modula2:
19685 language = language_m2;
19686 break;
19687 case DW_LANG_Pascal83:
19688 language = language_pascal;
19689 break;
19690 case DW_LANG_ObjC:
19691 language = language_objc;
19692 break;
19693 case DW_LANG_Rust:
19694 case DW_LANG_Rust_old:
19695 language = language_rust;
19696 break;
19697 case DW_LANG_OpenCL:
19698 language = language_opencl;
19699 break;
19700 case DW_LANG_Cobol74:
19701 case DW_LANG_Cobol85:
19702 default:
19703 language = language_minimal;
19704 break;
19705 }
19706
19707 return language;
19708 }
19709
19710 /* Return the named attribute or NULL if not there. */
19711
19712 static struct attribute *
19713 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19714 {
19715 for (;;)
19716 {
19717 unsigned int i;
19718 struct attribute *spec = NULL;
19719
19720 for (i = 0; i < die->num_attrs; ++i)
19721 {
19722 if (die->attrs[i].name == name)
19723 return &die->attrs[i];
19724 if (die->attrs[i].name == DW_AT_specification
19725 || die->attrs[i].name == DW_AT_abstract_origin)
19726 spec = &die->attrs[i];
19727 }
19728
19729 if (!spec)
19730 break;
19731
19732 die = follow_die_ref (die, spec, &cu);
19733 }
19734
19735 return NULL;
19736 }
19737
19738 /* Return the string associated with a string-typed attribute, or NULL if it
19739 is either not found or is of an incorrect type. */
19740
19741 static const char *
19742 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19743 {
19744 struct attribute *attr;
19745 const char *str = NULL;
19746
19747 attr = dwarf2_attr (die, name, cu);
19748
19749 if (attr != NULL)
19750 {
19751 str = attr->as_string ();
19752 if (str == nullptr)
19753 complaint (_("string type expected for attribute %s for "
19754 "DIE at %s in module %s"),
19755 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19756 objfile_name (cu->per_objfile->objfile));
19757 }
19758
19759 return str;
19760 }
19761
19762 /* Return the dwo name or NULL if not present. If present, it is in either
19763 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19764 static const char *
19765 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19766 {
19767 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19768 if (dwo_name == nullptr)
19769 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19770 return dwo_name;
19771 }
19772
19773 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19774 and holds a non-zero value. This function should only be used for
19775 DW_FORM_flag or DW_FORM_flag_present attributes. */
19776
19777 static int
19778 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19779 {
19780 struct attribute *attr = dwarf2_attr (die, name, cu);
19781
19782 return attr != nullptr && attr->as_boolean ();
19783 }
19784
19785 static int
19786 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19787 {
19788 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19789 which value is non-zero. However, we have to be careful with
19790 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19791 (via dwarf2_flag_true_p) follows this attribute. So we may
19792 end up accidently finding a declaration attribute that belongs
19793 to a different DIE referenced by the specification attribute,
19794 even though the given DIE does not have a declaration attribute. */
19795 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19796 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19797 }
19798
19799 /* Return the die giving the specification for DIE, if there is
19800 one. *SPEC_CU is the CU containing DIE on input, and the CU
19801 containing the return value on output. If there is no
19802 specification, but there is an abstract origin, that is
19803 returned. */
19804
19805 static struct die_info *
19806 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19807 {
19808 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19809 *spec_cu);
19810
19811 if (spec_attr == NULL)
19812 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19813
19814 if (spec_attr == NULL)
19815 return NULL;
19816 else
19817 return follow_die_ref (die, spec_attr, spec_cu);
19818 }
19819
19820 /* A convenience function to find the proper .debug_line section for a CU. */
19821
19822 static struct dwarf2_section_info *
19823 get_debug_line_section (struct dwarf2_cu *cu)
19824 {
19825 struct dwarf2_section_info *section;
19826 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19827
19828 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19829 DWO file. */
19830 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19831 section = &cu->dwo_unit->dwo_file->sections.line;
19832 else if (cu->per_cu->is_dwz)
19833 {
19834 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
19835
19836 section = &dwz->line;
19837 }
19838 else
19839 section = &per_objfile->per_bfd->line;
19840
19841 return section;
19842 }
19843
19844 /* Read the statement program header starting at OFFSET in
19845 .debug_line, or .debug_line.dwo. Return a pointer
19846 to a struct line_header, allocated using xmalloc.
19847 Returns NULL if there is a problem reading the header, e.g., if it
19848 has a version we don't understand.
19849
19850 NOTE: the strings in the include directory and file name tables of
19851 the returned object point into the dwarf line section buffer,
19852 and must not be freed. */
19853
19854 static line_header_up
19855 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
19856 const char *comp_dir)
19857 {
19858 struct dwarf2_section_info *section;
19859 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19860
19861 section = get_debug_line_section (cu);
19862 section->read (per_objfile->objfile);
19863 if (section->buffer == NULL)
19864 {
19865 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19866 complaint (_("missing .debug_line.dwo section"));
19867 else
19868 complaint (_("missing .debug_line section"));
19869 return 0;
19870 }
19871
19872 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19873 per_objfile, section, &cu->header,
19874 comp_dir);
19875 }
19876
19877 /* Subroutine of dwarf_decode_lines to simplify it.
19878 Return the file name for the given file_entry.
19879 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19880 If space for the result is malloc'd, *NAME_HOLDER will be set.
19881 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19882 equivalent to CU_INFO. */
19883
19884 static const char *
19885 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
19886 const file_and_directory &cu_info,
19887 std::string &name_holder)
19888 {
19889 const char *include_name = fe.name;
19890 const char *include_name_to_compare = include_name;
19891
19892 const char *dir_name = fe.include_dir (lh);
19893
19894 std::string hold_compare;
19895 if (!IS_ABSOLUTE_PATH (include_name)
19896 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
19897 {
19898 /* Avoid creating a duplicate name for CU_INFO.
19899 We do this by comparing INCLUDE_NAME and CU_INFO.
19900 Before we do the comparison, however, we need to account
19901 for DIR_NAME and COMP_DIR.
19902 First prepend dir_name (if non-NULL). If we still don't
19903 have an absolute path prepend comp_dir (if non-NULL).
19904 However, the directory we record in the include-file's
19905 psymtab does not contain COMP_DIR (to match the
19906 corresponding symtab(s)).
19907
19908 Example:
19909
19910 bash$ cd /tmp
19911 bash$ gcc -g ./hello.c
19912 include_name = "hello.c"
19913 dir_name = "."
19914 DW_AT_comp_dir = comp_dir = "/tmp"
19915 DW_AT_name = "./hello.c"
19916
19917 */
19918
19919 if (dir_name != NULL)
19920 {
19921 name_holder = path_join (dir_name, include_name);
19922 include_name = name_holder.c_str ();
19923 include_name_to_compare = include_name;
19924 }
19925 if (!IS_ABSOLUTE_PATH (include_name)
19926 && cu_info.get_comp_dir () != nullptr)
19927 {
19928 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
19929 include_name_to_compare = hold_compare.c_str ();
19930 }
19931 }
19932
19933 std::string copied_name;
19934 const char *cu_filename = cu_info.get_name ();
19935 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
19936 {
19937 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
19938 cu_filename = copied_name.c_str ();
19939 }
19940
19941 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
19942 return nullptr;
19943 return include_name;
19944 }
19945
19946 /* State machine to track the state of the line number program. */
19947
19948 class lnp_state_machine
19949 {
19950 public:
19951 /* Initialize a machine state for the start of a line number
19952 program. */
19953 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
19954
19955 file_entry *current_file ()
19956 {
19957 /* lh->file_names is 0-based, but the file name numbers in the
19958 statement program are 1-based. */
19959 return m_line_header->file_name_at (m_file);
19960 }
19961
19962 /* Record the line in the state machine. END_SEQUENCE is true if
19963 we're processing the end of a sequence. */
19964 void record_line (bool end_sequence);
19965
19966 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19967 nop-out rest of the lines in this sequence. */
19968 void check_line_address (struct dwarf2_cu *cu,
19969 const gdb_byte *line_ptr,
19970 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19971
19972 void handle_set_discriminator (unsigned int discriminator)
19973 {
19974 m_discriminator = discriminator;
19975 m_line_has_non_zero_discriminator |= discriminator != 0;
19976 }
19977
19978 /* Handle DW_LNE_set_address. */
19979 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19980 {
19981 m_op_index = 0;
19982 address += baseaddr;
19983 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19984 }
19985
19986 /* Handle DW_LNS_advance_pc. */
19987 void handle_advance_pc (CORE_ADDR adjust);
19988
19989 /* Handle a special opcode. */
19990 void handle_special_opcode (unsigned char op_code);
19991
19992 /* Handle DW_LNS_advance_line. */
19993 void handle_advance_line (int line_delta)
19994 {
19995 advance_line (line_delta);
19996 }
19997
19998 /* Handle DW_LNS_set_file. */
19999 void handle_set_file (file_name_index file);
20000
20001 /* Handle DW_LNS_negate_stmt. */
20002 void handle_negate_stmt ()
20003 {
20004 m_flags ^= LEF_IS_STMT;
20005 }
20006
20007 /* Handle DW_LNS_const_add_pc. */
20008 void handle_const_add_pc ();
20009
20010 /* Handle DW_LNS_fixed_advance_pc. */
20011 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20012 {
20013 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20014 m_op_index = 0;
20015 }
20016
20017 /* Handle DW_LNS_copy. */
20018 void handle_copy ()
20019 {
20020 record_line (false);
20021 m_discriminator = 0;
20022 m_flags &= ~LEF_PROLOGUE_END;
20023 }
20024
20025 /* Handle DW_LNE_end_sequence. */
20026 void handle_end_sequence ()
20027 {
20028 m_currently_recording_lines = true;
20029 }
20030
20031 /* Handle DW_LNS_set_prologue_end. */
20032 void handle_set_prologue_end ()
20033 {
20034 m_flags |= LEF_PROLOGUE_END;
20035 }
20036
20037 private:
20038 /* Advance the line by LINE_DELTA. */
20039 void advance_line (int line_delta)
20040 {
20041 m_line += line_delta;
20042
20043 if (line_delta != 0)
20044 m_line_has_non_zero_discriminator = m_discriminator != 0;
20045 }
20046
20047 struct dwarf2_cu *m_cu;
20048
20049 gdbarch *m_gdbarch;
20050
20051 /* The line number header. */
20052 line_header *m_line_header;
20053
20054 /* These are part of the standard DWARF line number state machine,
20055 and initialized according to the DWARF spec. */
20056
20057 unsigned char m_op_index = 0;
20058 /* The line table index of the current file. */
20059 file_name_index m_file = 1;
20060 unsigned int m_line = 1;
20061
20062 /* These are initialized in the constructor. */
20063
20064 CORE_ADDR m_address;
20065 linetable_entry_flags m_flags;
20066 unsigned int m_discriminator;
20067
20068 /* Additional bits of state we need to track. */
20069
20070 /* The last file that we called dwarf2_start_subfile for.
20071 This is only used for TLLs. */
20072 unsigned int m_last_file = 0;
20073 /* The last file a line number was recorded for. */
20074 struct subfile *m_last_subfile = NULL;
20075
20076 /* The address of the last line entry. */
20077 CORE_ADDR m_last_address;
20078
20079 /* Set to true when a previous line at the same address (using
20080 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
20081 when a line entry at a new address (m_address different to
20082 m_last_address) is processed. */
20083 bool m_stmt_at_address = false;
20084
20085 /* When true, record the lines we decode. */
20086 bool m_currently_recording_lines = false;
20087
20088 /* The last line number that was recorded, used to coalesce
20089 consecutive entries for the same line. This can happen, for
20090 example, when discriminators are present. PR 17276. */
20091 unsigned int m_last_line = 0;
20092 bool m_line_has_non_zero_discriminator = false;
20093 };
20094
20095 void
20096 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20097 {
20098 CORE_ADDR addr_adj = (((m_op_index + adjust)
20099 / m_line_header->maximum_ops_per_instruction)
20100 * m_line_header->minimum_instruction_length);
20101 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20102 m_op_index = ((m_op_index + adjust)
20103 % m_line_header->maximum_ops_per_instruction);
20104 }
20105
20106 void
20107 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20108 {
20109 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20110 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20111 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20112 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20113 / m_line_header->maximum_ops_per_instruction)
20114 * m_line_header->minimum_instruction_length);
20115 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20116 m_op_index = ((m_op_index + adj_opcode_d)
20117 % m_line_header->maximum_ops_per_instruction);
20118
20119 int line_delta = m_line_header->line_base + adj_opcode_r;
20120 advance_line (line_delta);
20121 record_line (false);
20122 m_discriminator = 0;
20123 m_flags &= ~LEF_PROLOGUE_END;
20124 }
20125
20126 void
20127 lnp_state_machine::handle_set_file (file_name_index file)
20128 {
20129 m_file = file;
20130
20131 const file_entry *fe = current_file ();
20132 if (fe == NULL)
20133 dwarf2_debug_line_missing_file_complaint ();
20134 else
20135 {
20136 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20137 m_line_has_non_zero_discriminator = m_discriminator != 0;
20138 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
20139 }
20140 }
20141
20142 void
20143 lnp_state_machine::handle_const_add_pc ()
20144 {
20145 CORE_ADDR adjust
20146 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20147
20148 CORE_ADDR addr_adj
20149 = (((m_op_index + adjust)
20150 / m_line_header->maximum_ops_per_instruction)
20151 * m_line_header->minimum_instruction_length);
20152
20153 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20154 m_op_index = ((m_op_index + adjust)
20155 % m_line_header->maximum_ops_per_instruction);
20156 }
20157
20158 /* Return non-zero if we should add LINE to the line number table.
20159 LINE is the line to add, LAST_LINE is the last line that was added,
20160 LAST_SUBFILE is the subfile for LAST_LINE.
20161 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20162 had a non-zero discriminator.
20163
20164 We have to be careful in the presence of discriminators.
20165 E.g., for this line:
20166
20167 for (i = 0; i < 100000; i++);
20168
20169 clang can emit four line number entries for that one line,
20170 each with a different discriminator.
20171 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20172
20173 However, we want gdb to coalesce all four entries into one.
20174 Otherwise the user could stepi into the middle of the line and
20175 gdb would get confused about whether the pc really was in the
20176 middle of the line.
20177
20178 Things are further complicated by the fact that two consecutive
20179 line number entries for the same line is a heuristic used by gcc
20180 to denote the end of the prologue. So we can't just discard duplicate
20181 entries, we have to be selective about it. The heuristic we use is
20182 that we only collapse consecutive entries for the same line if at least
20183 one of those entries has a non-zero discriminator. PR 17276.
20184
20185 Note: Addresses in the line number state machine can never go backwards
20186 within one sequence, thus this coalescing is ok. */
20187
20188 static int
20189 dwarf_record_line_p (struct dwarf2_cu *cu,
20190 unsigned int line, unsigned int last_line,
20191 int line_has_non_zero_discriminator,
20192 struct subfile *last_subfile)
20193 {
20194 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20195 return 1;
20196 if (line != last_line)
20197 return 1;
20198 /* Same line for the same file that we've seen already.
20199 As a last check, for pr 17276, only record the line if the line
20200 has never had a non-zero discriminator. */
20201 if (!line_has_non_zero_discriminator)
20202 return 1;
20203 return 0;
20204 }
20205
20206 /* Use the CU's builder to record line number LINE beginning at
20207 address ADDRESS in the line table of subfile SUBFILE. */
20208
20209 static void
20210 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20211 unsigned int line, CORE_ADDR address,
20212 linetable_entry_flags flags,
20213 struct dwarf2_cu *cu)
20214 {
20215 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20216
20217 if (dwarf_line_debug)
20218 {
20219 gdb_printf (gdb_stdlog,
20220 "Recording line %u, file %s, address %s\n",
20221 line, lbasename (subfile->name.c_str ()),
20222 paddress (gdbarch, address));
20223 }
20224
20225 if (cu != nullptr)
20226 cu->get_builder ()->record_line (subfile, line, addr, flags);
20227 }
20228
20229 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20230 Mark the end of a set of line number records.
20231 The arguments are the same as for dwarf_record_line_1.
20232 If SUBFILE is NULL the request is ignored. */
20233
20234 static void
20235 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20236 CORE_ADDR address, struct dwarf2_cu *cu)
20237 {
20238 if (subfile == NULL)
20239 return;
20240
20241 if (dwarf_line_debug)
20242 {
20243 gdb_printf (gdb_stdlog,
20244 "Finishing current line, file %s, address %s\n",
20245 lbasename (subfile->name.c_str ()),
20246 paddress (gdbarch, address));
20247 }
20248
20249 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
20250 }
20251
20252 void
20253 lnp_state_machine::record_line (bool end_sequence)
20254 {
20255 if (dwarf_line_debug)
20256 {
20257 gdb_printf (gdb_stdlog,
20258 "Processing actual line %u: file %u,"
20259 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20260 m_line, m_file,
20261 paddress (m_gdbarch, m_address),
20262 (m_flags & LEF_IS_STMT) != 0,
20263 (m_flags & LEF_PROLOGUE_END) != 0,
20264 m_discriminator,
20265 (end_sequence ? "\t(end sequence)" : ""));
20266 }
20267
20268 file_entry *fe = current_file ();
20269
20270 if (fe == NULL)
20271 dwarf2_debug_line_missing_file_complaint ();
20272 /* For now we ignore lines not starting on an instruction boundary.
20273 But not when processing end_sequence for compatibility with the
20274 previous version of the code. */
20275 else if (m_op_index == 0 || end_sequence)
20276 {
20277 /* When we switch files we insert an end maker in the first file,
20278 switch to the second file and add a new line entry. The
20279 problem is that the end marker inserted in the first file will
20280 discard any previous line entries at the same address. If the
20281 line entries in the first file are marked as is-stmt, while
20282 the new line in the second file is non-stmt, then this means
20283 the end marker will discard is-stmt lines so we can have a
20284 non-stmt line. This means that there are less addresses at
20285 which the user can insert a breakpoint.
20286
20287 To improve this we track the last address in m_last_address,
20288 and whether we have seen an is-stmt at this address. Then
20289 when switching files, if we have seen a stmt at the current
20290 address, and we are switching to create a non-stmt line, then
20291 discard the new line. */
20292 bool file_changed
20293 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20294 bool ignore_this_line
20295 = ((file_changed && !end_sequence && m_last_address == m_address
20296 && ((m_flags & LEF_IS_STMT) == 0)
20297 && m_stmt_at_address)
20298 || (!end_sequence && m_line == 0));
20299
20300 if ((file_changed && !ignore_this_line) || end_sequence)
20301 {
20302 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20303 m_currently_recording_lines ? m_cu : nullptr);
20304 }
20305
20306 if (!end_sequence && !ignore_this_line)
20307 {
20308 linetable_entry_flags lte_flags = m_flags;
20309 if (producer_is_codewarrior (m_cu))
20310 lte_flags |= LEF_IS_STMT;
20311
20312 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20313 m_line_has_non_zero_discriminator,
20314 m_last_subfile))
20315 {
20316 buildsym_compunit *builder = m_cu->get_builder ();
20317 dwarf_record_line_1 (m_gdbarch,
20318 builder->get_current_subfile (),
20319 m_line, m_address, lte_flags,
20320 m_currently_recording_lines ? m_cu : nullptr);
20321 }
20322 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20323 m_last_line = m_line;
20324 }
20325 }
20326
20327 /* Track whether we have seen any IS_STMT true at m_address in case we
20328 have multiple line table entries all at m_address. */
20329 if (m_last_address != m_address)
20330 {
20331 m_stmt_at_address = false;
20332 m_last_address = m_address;
20333 }
20334 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
20335 }
20336
20337 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20338 line_header *lh)
20339 {
20340 m_cu = cu;
20341 m_gdbarch = arch;
20342 m_line_header = lh;
20343
20344 m_currently_recording_lines = true;
20345
20346 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20347 was a line entry for it so that the backend has a chance to adjust it
20348 and also record it in case it needs it. This is currently used by MIPS
20349 code, cf. `mips_adjust_dwarf2_line'. */
20350 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20351 m_flags = 0;
20352 if (lh->default_is_stmt)
20353 m_flags |= LEF_IS_STMT;
20354 m_discriminator = 0;
20355
20356 m_last_address = m_address;
20357 m_stmt_at_address = false;
20358 }
20359
20360 void
20361 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20362 const gdb_byte *line_ptr,
20363 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20364 {
20365 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20366 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20367 located at 0x0. In this case, additionally check that if
20368 ADDRESS < UNRELOCATED_LOWPC. */
20369
20370 if ((address == 0 && address < unrelocated_lowpc)
20371 || address == (CORE_ADDR) -1)
20372 {
20373 /* This line table is for a function which has been
20374 GCd by the linker. Ignore it. PR gdb/12528 */
20375
20376 struct objfile *objfile = cu->per_objfile->objfile;
20377 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20378
20379 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20380 line_offset, objfile_name (objfile));
20381 m_currently_recording_lines = false;
20382 /* Note: m_currently_recording_lines is left as false until we see
20383 DW_LNE_end_sequence. */
20384 }
20385 }
20386
20387 /* Subroutine of dwarf_decode_lines to simplify it.
20388 Process the line number information in LH. */
20389
20390 static void
20391 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20392 CORE_ADDR lowpc)
20393 {
20394 const gdb_byte *line_ptr, *extended_end;
20395 const gdb_byte *line_end;
20396 unsigned int bytes_read, extended_len;
20397 unsigned char op_code, extended_op;
20398 CORE_ADDR baseaddr;
20399 struct objfile *objfile = cu->per_objfile->objfile;
20400 bfd *abfd = objfile->obfd.get ();
20401 struct gdbarch *gdbarch = objfile->arch ();
20402
20403 baseaddr = objfile->text_section_offset ();
20404
20405 line_ptr = lh->statement_program_start;
20406 line_end = lh->statement_program_end;
20407
20408 /* Read the statement sequences until there's nothing left. */
20409 while (line_ptr < line_end)
20410 {
20411 /* The DWARF line number program state machine. Reset the state
20412 machine at the start of each sequence. */
20413 lnp_state_machine state_machine (cu, gdbarch, lh);
20414 bool end_sequence = false;
20415
20416 /* Start a subfile for the current file of the state
20417 machine. */
20418 const file_entry *fe = state_machine.current_file ();
20419
20420 if (fe != NULL)
20421 dwarf2_start_subfile (cu, *fe, *lh);
20422
20423 /* Decode the table. */
20424 while (line_ptr < line_end && !end_sequence)
20425 {
20426 op_code = read_1_byte (abfd, line_ptr);
20427 line_ptr += 1;
20428
20429 if (op_code >= lh->opcode_base)
20430 {
20431 /* Special opcode. */
20432 state_machine.handle_special_opcode (op_code);
20433 }
20434 else switch (op_code)
20435 {
20436 case DW_LNS_extended_op:
20437 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20438 &bytes_read);
20439 line_ptr += bytes_read;
20440 extended_end = line_ptr + extended_len;
20441 extended_op = read_1_byte (abfd, line_ptr);
20442 line_ptr += 1;
20443 if (DW_LNE_lo_user <= extended_op
20444 && extended_op <= DW_LNE_hi_user)
20445 {
20446 /* Vendor extension, ignore. */
20447 line_ptr = extended_end;
20448 break;
20449 }
20450 switch (extended_op)
20451 {
20452 case DW_LNE_end_sequence:
20453 state_machine.handle_end_sequence ();
20454 end_sequence = true;
20455 break;
20456 case DW_LNE_set_address:
20457 {
20458 CORE_ADDR address
20459 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20460 line_ptr += bytes_read;
20461
20462 state_machine.check_line_address (cu, line_ptr,
20463 lowpc - baseaddr, address);
20464 state_machine.handle_set_address (baseaddr, address);
20465 }
20466 break;
20467 case DW_LNE_define_file:
20468 {
20469 const char *cur_file;
20470 unsigned int mod_time, length;
20471 dir_index dindex;
20472
20473 cur_file = read_direct_string (abfd, line_ptr,
20474 &bytes_read);
20475 line_ptr += bytes_read;
20476 dindex = (dir_index)
20477 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20478 line_ptr += bytes_read;
20479 mod_time =
20480 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20481 line_ptr += bytes_read;
20482 length =
20483 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20484 line_ptr += bytes_read;
20485 lh->add_file_name (cur_file, dindex, mod_time, length);
20486 }
20487 break;
20488 case DW_LNE_set_discriminator:
20489 {
20490 /* The discriminator is not interesting to the
20491 debugger; just ignore it. We still need to
20492 check its value though:
20493 if there are consecutive entries for the same
20494 (non-prologue) line we want to coalesce them.
20495 PR 17276. */
20496 unsigned int discr
20497 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20498 line_ptr += bytes_read;
20499
20500 state_machine.handle_set_discriminator (discr);
20501 }
20502 break;
20503 default:
20504 complaint (_("mangled .debug_line section"));
20505 return;
20506 }
20507 /* Make sure that we parsed the extended op correctly. If e.g.
20508 we expected a different address size than the producer used,
20509 we may have read the wrong number of bytes. */
20510 if (line_ptr != extended_end)
20511 {
20512 complaint (_("mangled .debug_line section"));
20513 return;
20514 }
20515 break;
20516 case DW_LNS_copy:
20517 state_machine.handle_copy ();
20518 break;
20519 case DW_LNS_advance_pc:
20520 {
20521 CORE_ADDR adjust
20522 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20523 line_ptr += bytes_read;
20524
20525 state_machine.handle_advance_pc (adjust);
20526 }
20527 break;
20528 case DW_LNS_advance_line:
20529 {
20530 int line_delta
20531 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20532 line_ptr += bytes_read;
20533
20534 state_machine.handle_advance_line (line_delta);
20535 }
20536 break;
20537 case DW_LNS_set_file:
20538 {
20539 file_name_index file
20540 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20541 &bytes_read);
20542 line_ptr += bytes_read;
20543
20544 state_machine.handle_set_file (file);
20545 }
20546 break;
20547 case DW_LNS_set_column:
20548 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20549 line_ptr += bytes_read;
20550 break;
20551 case DW_LNS_negate_stmt:
20552 state_machine.handle_negate_stmt ();
20553 break;
20554 case DW_LNS_set_basic_block:
20555 break;
20556 /* Add to the address register of the state machine the
20557 address increment value corresponding to special opcode
20558 255. I.e., this value is scaled by the minimum
20559 instruction length since special opcode 255 would have
20560 scaled the increment. */
20561 case DW_LNS_const_add_pc:
20562 state_machine.handle_const_add_pc ();
20563 break;
20564 case DW_LNS_fixed_advance_pc:
20565 {
20566 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20567 line_ptr += 2;
20568
20569 state_machine.handle_fixed_advance_pc (addr_adj);
20570 }
20571 break;
20572 case DW_LNS_set_prologue_end:
20573 state_machine.handle_set_prologue_end ();
20574 break;
20575 default:
20576 {
20577 /* Unknown standard opcode, ignore it. */
20578 int i;
20579
20580 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20581 {
20582 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20583 line_ptr += bytes_read;
20584 }
20585 }
20586 }
20587 }
20588
20589 if (!end_sequence)
20590 dwarf2_debug_line_missing_end_sequence_complaint ();
20591
20592 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20593 in which case we still finish recording the last line). */
20594 state_machine.record_line (true);
20595 }
20596 }
20597
20598 /* Decode the Line Number Program (LNP) for the given line_header
20599 structure and CU. The actual information extracted and the type
20600 of structures created from the LNP depends on the value of PST.
20601
20602 FND holds the CU file name and directory, if known.
20603 It is used for relative paths in the line table.
20604
20605 NOTE: It is important that psymtabs have the same file name (via
20606 strcmp) as the corresponding symtab. Since the directory is not
20607 used in the name of the symtab we don't use it in the name of the
20608 psymtabs we create. E.g. expand_line_sal requires this when
20609 finding psymtabs to expand. A good testcase for this is
20610 mb-inline.exp.
20611
20612 LOWPC is the lowest address in CU (or 0 if not known).
20613
20614 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20615 for its PC<->lines mapping information. Otherwise only the filename
20616 table is read in. */
20617
20618 static void
20619 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
20620 CORE_ADDR lowpc, int decode_mapping)
20621 {
20622 if (decode_mapping)
20623 dwarf_decode_lines_1 (lh, cu, lowpc);
20624
20625 /* Make sure a symtab is created for every file, even files
20626 which contain only variables (i.e. no code with associated
20627 line numbers). */
20628 buildsym_compunit *builder = cu->get_builder ();
20629 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20630
20631 for (auto &fe : lh->file_names ())
20632 {
20633 dwarf2_start_subfile (cu, fe, *lh);
20634 subfile *sf = builder->get_current_subfile ();
20635
20636 if (sf->symtab == nullptr)
20637 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
20638 sf->name_for_id.c_str ());
20639
20640 fe.symtab = sf->symtab;
20641 }
20642 }
20643
20644 /* Start a subfile for DWARF. FILENAME is the name of the file and
20645 DIRNAME the name of the source directory which contains FILENAME
20646 or NULL if not known.
20647 This routine tries to keep line numbers from identical absolute and
20648 relative file names in a common subfile.
20649
20650 Using the `list' example from the GDB testsuite, which resides in
20651 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20652 of /srcdir/list0.c yields the following debugging information for list0.c:
20653
20654 DW_AT_name: /srcdir/list0.c
20655 DW_AT_comp_dir: /compdir
20656 files.files[0].name: list0.h
20657 files.files[0].dir: /srcdir
20658 files.files[1].name: list0.c
20659 files.files[1].dir: /srcdir
20660
20661 The line number information for list0.c has to end up in a single
20662 subfile, so that `break /srcdir/list0.c:1' works as expected.
20663 start_subfile will ensure that this happens provided that we pass the
20664 concatenation of files.files[1].dir and files.files[1].name as the
20665 subfile's name. */
20666
20667 static void
20668 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
20669 const line_header &lh)
20670 {
20671 std::string filename_holder;
20672 const char *filename = fe.name;
20673 const char *dirname = lh.include_dir_at (fe.d_index);
20674
20675 /* In order not to lose the line information directory,
20676 we concatenate it to the filename when it makes sense.
20677 Note that the Dwarf3 standard says (speaking of filenames in line
20678 information): ``The directory index is ignored for file names
20679 that represent full path names''. Thus ignoring dirname in the
20680 `else' branch below isn't an issue. */
20681
20682 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20683 {
20684 filename_holder = path_join (dirname, filename);
20685 filename = filename_holder.c_str ();
20686 }
20687
20688 std::string filename_for_id = lh.file_file_name (fe);
20689 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
20690 }
20691
20692 static void
20693 var_decode_location (struct attribute *attr, struct symbol *sym,
20694 struct dwarf2_cu *cu)
20695 {
20696 struct objfile *objfile = cu->per_objfile->objfile;
20697 struct comp_unit_head *cu_header = &cu->header;
20698
20699 /* NOTE drow/2003-01-30: There used to be a comment and some special
20700 code here to turn a symbol with DW_AT_external and a
20701 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20702 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20703 with some versions of binutils) where shared libraries could have
20704 relocations against symbols in their debug information - the
20705 minimal symbol would have the right address, but the debug info
20706 would not. It's no longer necessary, because we will explicitly
20707 apply relocations when we read in the debug information now. */
20708
20709 /* A DW_AT_location attribute with no contents indicates that a
20710 variable has been optimized away. */
20711 if (attr->form_is_block () && attr->as_block ()->size == 0)
20712 {
20713 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20714 return;
20715 }
20716
20717 /* Handle one degenerate form of location expression specially, to
20718 preserve GDB's previous behavior when section offsets are
20719 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20720 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20721
20722 if (attr->form_is_block ())
20723 {
20724 struct dwarf_block *block = attr->as_block ();
20725
20726 if ((block->data[0] == DW_OP_addr
20727 && block->size == 1 + cu_header->addr_size)
20728 || ((block->data[0] == DW_OP_GNU_addr_index
20729 || block->data[0] == DW_OP_addrx)
20730 && (block->size
20731 == 1 + leb128_size (&block->data[1]))))
20732 {
20733 unsigned int dummy;
20734
20735 if (block->data[0] == DW_OP_addr)
20736 sym->set_value_address
20737 (cu->header.read_address (objfile->obfd.get (), block->data + 1,
20738 &dummy));
20739 else
20740 sym->set_value_address
20741 (read_addr_index_from_leb128 (cu, block->data + 1, &dummy));
20742 sym->set_aclass_index (LOC_STATIC);
20743 fixup_symbol_section (sym, objfile);
20744 sym->set_value_address
20745 (sym->value_address ()
20746 + objfile->section_offsets[sym->section_index ()]);
20747 return;
20748 }
20749 }
20750
20751 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20752 expression evaluator, and use LOC_COMPUTED only when necessary
20753 (i.e. when the value of a register or memory location is
20754 referenced, or a thread-local block, etc.). Then again, it might
20755 not be worthwhile. I'm assuming that it isn't unless performance
20756 or memory numbers show me otherwise. */
20757
20758 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20759
20760 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20761 cu->has_loclist = true;
20762 }
20763
20764 /* Given a pointer to a DWARF information entry, figure out if we need
20765 to make a symbol table entry for it, and if so, create a new entry
20766 and return a pointer to it.
20767 If TYPE is NULL, determine symbol type from the die, otherwise
20768 used the passed type.
20769 If SPACE is not NULL, use it to hold the new symbol. If it is
20770 NULL, allocate a new symbol on the objfile's obstack. */
20771
20772 static struct symbol *
20773 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20774 struct symbol *space)
20775 {
20776 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20777 struct objfile *objfile = per_objfile->objfile;
20778 struct gdbarch *gdbarch = objfile->arch ();
20779 struct symbol *sym = NULL;
20780 const char *name;
20781 struct attribute *attr = NULL;
20782 struct attribute *attr2 = NULL;
20783 CORE_ADDR baseaddr;
20784 struct pending **list_to_add = NULL;
20785
20786 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20787
20788 baseaddr = objfile->text_section_offset ();
20789
20790 name = dwarf2_name (die, cu);
20791 if (name == nullptr && (die->tag == DW_TAG_subprogram
20792 || die->tag == DW_TAG_inlined_subroutine
20793 || die->tag == DW_TAG_entry_point))
20794 name = dw2_linkage_name (die, cu);
20795
20796 if (name)
20797 {
20798 int suppress_add = 0;
20799
20800 if (space)
20801 sym = space;
20802 else
20803 sym = new (&objfile->objfile_obstack) symbol;
20804 OBJSTAT (objfile, n_syms++);
20805
20806 /* Cache this symbol's name and the name's demangled form (if any). */
20807 sym->set_language (cu->lang (), &objfile->objfile_obstack);
20808 /* Fortran does not have mangling standard and the mangling does differ
20809 between gfortran, iFort etc. */
20810 const char *physname
20811 = (cu->lang () == language_fortran
20812 ? dwarf2_full_name (name, die, cu)
20813 : dwarf2_physname (name, die, cu));
20814 const char *linkagename = dw2_linkage_name (die, cu);
20815
20816 if (linkagename == nullptr || cu->lang () == language_ada)
20817 sym->set_linkage_name (physname);
20818 else
20819 {
20820 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20821 sym->set_linkage_name (linkagename);
20822 }
20823
20824 /* Handle DW_AT_artificial. */
20825 attr = dwarf2_attr (die, DW_AT_artificial, cu);
20826 if (attr != nullptr)
20827 sym->set_is_artificial (attr->as_boolean ());
20828
20829 /* Default assumptions.
20830 Use the passed type or decode it from the die. */
20831 sym->set_domain (VAR_DOMAIN);
20832 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20833 if (type != NULL)
20834 sym->set_type (type);
20835 else
20836 sym->set_type (die_type (die, cu));
20837 attr = dwarf2_attr (die,
20838 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20839 cu);
20840 if (attr != nullptr)
20841 sym->set_line (attr->constant_value (0));
20842
20843 attr = dwarf2_attr (die,
20844 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20845 cu);
20846 if (attr != nullptr && attr->is_nonnegative ())
20847 {
20848 file_name_index file_index
20849 = (file_name_index) attr->as_nonnegative ();
20850 struct file_entry *fe;
20851
20852 if (cu->line_header != NULL)
20853 fe = cu->line_header->file_name_at (file_index);
20854 else
20855 fe = NULL;
20856
20857 if (fe == NULL)
20858 complaint (_("file index out of range"));
20859 else
20860 sym->set_symtab (fe->symtab);
20861 }
20862
20863 switch (die->tag)
20864 {
20865 case DW_TAG_label:
20866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20867 if (attr != nullptr)
20868 {
20869 CORE_ADDR addr;
20870
20871 addr = attr->as_address ();
20872 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20873 sym->set_value_address (addr);
20874 sym->set_aclass_index (LOC_LABEL);
20875 }
20876 else
20877 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
20878 sym->set_type (objfile_type (objfile)->builtin_core_addr);
20879 sym->set_domain (LABEL_DOMAIN);
20880 add_symbol_to_list (sym, cu->list_in_scope);
20881 break;
20882 case DW_TAG_subprogram:
20883 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20884 finish_block. */
20885 sym->set_aclass_index (LOC_BLOCK);
20886 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20887 if ((attr2 != nullptr && attr2->as_boolean ())
20888 || cu->lang () == language_ada
20889 || cu->lang () == language_fortran)
20890 {
20891 /* Subprograms marked external are stored as a global symbol.
20892 Ada and Fortran subprograms, whether marked external or
20893 not, are always stored as a global symbol, because we want
20894 to be able to access them globally. For instance, we want
20895 to be able to break on a nested subprogram without having
20896 to specify the context. */
20897 list_to_add = cu->get_builder ()->get_global_symbols ();
20898 }
20899 else
20900 {
20901 list_to_add = cu->list_in_scope;
20902 }
20903 break;
20904 case DW_TAG_inlined_subroutine:
20905 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20906 finish_block. */
20907 sym->set_aclass_index (LOC_BLOCK);
20908 sym->set_is_inlined (1);
20909 list_to_add = cu->list_in_scope;
20910 break;
20911 case DW_TAG_template_value_param:
20912 suppress_add = 1;
20913 /* Fall through. */
20914 case DW_TAG_constant:
20915 case DW_TAG_variable:
20916 case DW_TAG_member:
20917 /* Compilation with minimal debug info may result in
20918 variables with missing type entries. Change the
20919 misleading `void' type to something sensible. */
20920 if (sym->type ()->code () == TYPE_CODE_VOID)
20921 sym->set_type (objfile_type (objfile)->builtin_int);
20922
20923 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20924 /* In the case of DW_TAG_member, we should only be called for
20925 static const members. */
20926 if (die->tag == DW_TAG_member)
20927 {
20928 /* dwarf2_add_field uses die_is_declaration,
20929 so we do the same. */
20930 gdb_assert (die_is_declaration (die, cu));
20931 gdb_assert (attr);
20932 }
20933 if (attr != nullptr)
20934 {
20935 dwarf2_const_value (attr, sym, cu);
20936 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20937 if (!suppress_add)
20938 {
20939 if (attr2 != nullptr && attr2->as_boolean ())
20940 list_to_add = cu->get_builder ()->get_global_symbols ();
20941 else
20942 list_to_add = cu->list_in_scope;
20943 }
20944 break;
20945 }
20946 attr = dwarf2_attr (die, DW_AT_location, cu);
20947 if (attr != nullptr)
20948 {
20949 var_decode_location (attr, sym, cu);
20950 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20951
20952 /* Fortran explicitly imports any global symbols to the local
20953 scope by DW_TAG_common_block. */
20954 if (cu->lang () == language_fortran && die->parent
20955 && die->parent->tag == DW_TAG_common_block)
20956 attr2 = NULL;
20957
20958 if (sym->aclass () == LOC_STATIC
20959 && sym->value_address () == 0
20960 && !per_objfile->per_bfd->has_section_at_zero)
20961 {
20962 /* When a static variable is eliminated by the linker,
20963 the corresponding debug information is not stripped
20964 out, but the variable address is set to null;
20965 do not add such variables into symbol table. */
20966 }
20967 else if (attr2 != nullptr && attr2->as_boolean ())
20968 {
20969 if (sym->aclass () == LOC_STATIC
20970 && (objfile->flags & OBJF_MAINLINE) == 0
20971 && per_objfile->per_bfd->can_copy)
20972 {
20973 /* A global static variable might be subject to
20974 copy relocation. We first check for a local
20975 minsym, though, because maybe the symbol was
20976 marked hidden, in which case this would not
20977 apply. */
20978 bound_minimal_symbol found
20979 = (lookup_minimal_symbol_linkage
20980 (sym->linkage_name (), objfile));
20981 if (found.minsym != nullptr)
20982 sym->maybe_copied = 1;
20983 }
20984
20985 /* A variable with DW_AT_external is never static,
20986 but it may be block-scoped. */
20987 list_to_add
20988 = ((cu->list_in_scope
20989 == cu->get_builder ()->get_file_symbols ())
20990 ? cu->get_builder ()->get_global_symbols ()
20991 : cu->list_in_scope);
20992 }
20993 else
20994 list_to_add = cu->list_in_scope;
20995 }
20996 else
20997 {
20998 /* We do not know the address of this symbol.
20999 If it is an external symbol and we have type information
21000 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21001 The address of the variable will then be determined from
21002 the minimal symbol table whenever the variable is
21003 referenced. */
21004 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21005
21006 /* Fortran explicitly imports any global symbols to the local
21007 scope by DW_TAG_common_block. */
21008 if (cu->lang () == language_fortran && die->parent
21009 && die->parent->tag == DW_TAG_common_block)
21010 {
21011 /* SYMBOL_CLASS doesn't matter here because
21012 read_common_block is going to reset it. */
21013 if (!suppress_add)
21014 list_to_add = cu->list_in_scope;
21015 }
21016 else if (attr2 != nullptr && attr2->as_boolean ()
21017 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21018 {
21019 /* A variable with DW_AT_external is never static, but it
21020 may be block-scoped. */
21021 list_to_add
21022 = ((cu->list_in_scope
21023 == cu->get_builder ()->get_file_symbols ())
21024 ? cu->get_builder ()->get_global_symbols ()
21025 : cu->list_in_scope);
21026
21027 sym->set_aclass_index (LOC_UNRESOLVED);
21028 }
21029 else if (!die_is_declaration (die, cu))
21030 {
21031 /* Use the default LOC_OPTIMIZED_OUT class. */
21032 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
21033 if (!suppress_add)
21034 list_to_add = cu->list_in_scope;
21035 }
21036 }
21037 break;
21038 case DW_TAG_formal_parameter:
21039 {
21040 /* If we are inside a function, mark this as an argument. If
21041 not, we might be looking at an argument to an inlined function
21042 when we do not have enough information to show inlined frames;
21043 pretend it's a local variable in that case so that the user can
21044 still see it. */
21045 struct context_stack *curr
21046 = cu->get_builder ()->get_current_context_stack ();
21047 if (curr != nullptr && curr->name != nullptr)
21048 sym->set_is_argument (1);
21049 attr = dwarf2_attr (die, DW_AT_location, cu);
21050 if (attr != nullptr)
21051 {
21052 var_decode_location (attr, sym, cu);
21053 }
21054 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21055 if (attr != nullptr)
21056 {
21057 dwarf2_const_value (attr, sym, cu);
21058 }
21059
21060 list_to_add = cu->list_in_scope;
21061 }
21062 break;
21063 case DW_TAG_unspecified_parameters:
21064 /* From varargs functions; gdb doesn't seem to have any
21065 interest in this information, so just ignore it for now.
21066 (FIXME?) */
21067 break;
21068 case DW_TAG_template_type_param:
21069 suppress_add = 1;
21070 /* Fall through. */
21071 case DW_TAG_class_type:
21072 case DW_TAG_interface_type:
21073 case DW_TAG_structure_type:
21074 case DW_TAG_union_type:
21075 case DW_TAG_set_type:
21076 case DW_TAG_enumeration_type:
21077 case DW_TAG_namelist:
21078 if (die->tag == DW_TAG_namelist)
21079 {
21080 sym->set_aclass_index (LOC_STATIC);
21081 sym->set_domain (VAR_DOMAIN);
21082 }
21083 else
21084 {
21085 sym->set_aclass_index (LOC_TYPEDEF);
21086 sym->set_domain (STRUCT_DOMAIN);
21087 }
21088 {
21089 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21090 really ever be static objects: otherwise, if you try
21091 to, say, break of a class's method and you're in a file
21092 which doesn't mention that class, it won't work unless
21093 the check for all static symbols in lookup_symbol_aux
21094 saves you. See the OtherFileClass tests in
21095 gdb.c++/namespace.exp. */
21096
21097 if (!suppress_add)
21098 {
21099 buildsym_compunit *builder = cu->get_builder ();
21100 list_to_add
21101 = (cu->list_in_scope == builder->get_file_symbols ()
21102 && cu->lang () == language_cplus
21103 ? builder->get_global_symbols ()
21104 : cu->list_in_scope);
21105
21106 /* The semantics of C++ state that "struct foo {
21107 ... }" also defines a typedef for "foo". */
21108 if (cu->lang () == language_cplus
21109 || cu->lang () == language_ada
21110 || cu->lang () == language_d
21111 || cu->lang () == language_rust)
21112 {
21113 /* The symbol's name is already allocated along
21114 with this objfile, so we don't need to
21115 duplicate it for the type. */
21116 if (sym->type ()->name () == 0)
21117 sym->type ()->set_name (sym->search_name ());
21118 }
21119 }
21120 }
21121 break;
21122 case DW_TAG_typedef:
21123 sym->set_aclass_index (LOC_TYPEDEF);
21124 sym->set_domain (VAR_DOMAIN);
21125 list_to_add = cu->list_in_scope;
21126 break;
21127 case DW_TAG_array_type:
21128 case DW_TAG_base_type:
21129 case DW_TAG_subrange_type:
21130 case DW_TAG_generic_subrange:
21131 case DW_TAG_unspecified_type:
21132 sym->set_aclass_index (LOC_TYPEDEF);
21133 sym->set_domain (VAR_DOMAIN);
21134 list_to_add = cu->list_in_scope;
21135 break;
21136 case DW_TAG_enumerator:
21137 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21138 if (attr != nullptr)
21139 {
21140 dwarf2_const_value (attr, sym, cu);
21141 }
21142 {
21143 /* NOTE: carlton/2003-11-10: See comment above in the
21144 DW_TAG_class_type, etc. block. */
21145
21146 list_to_add
21147 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21148 && cu->lang () == language_cplus
21149 ? cu->get_builder ()->get_global_symbols ()
21150 : cu->list_in_scope);
21151 }
21152 break;
21153 case DW_TAG_imported_declaration:
21154 case DW_TAG_namespace:
21155 sym->set_aclass_index (LOC_TYPEDEF);
21156 list_to_add = cu->get_builder ()->get_global_symbols ();
21157 break;
21158 case DW_TAG_module:
21159 sym->set_aclass_index (LOC_TYPEDEF);
21160 sym->set_domain (MODULE_DOMAIN);
21161 list_to_add = cu->get_builder ()->get_global_symbols ();
21162 break;
21163 case DW_TAG_common_block:
21164 sym->set_aclass_index (LOC_COMMON_BLOCK);
21165 sym->set_domain (COMMON_BLOCK_DOMAIN);
21166 add_symbol_to_list (sym, cu->list_in_scope);
21167 break;
21168 default:
21169 /* Not a tag we recognize. Hopefully we aren't processing
21170 trash data, but since we must specifically ignore things
21171 we don't recognize, there is nothing else we should do at
21172 this point. */
21173 complaint (_("unsupported tag: '%s'"),
21174 dwarf_tag_name (die->tag));
21175 break;
21176 }
21177
21178 if (suppress_add)
21179 {
21180 sym->hash_next = objfile->template_symbols;
21181 objfile->template_symbols = sym;
21182 list_to_add = NULL;
21183 }
21184
21185 if (list_to_add != NULL)
21186 add_symbol_to_list (sym, list_to_add);
21187
21188 /* For the benefit of old versions of GCC, check for anonymous
21189 namespaces based on the demangled name. */
21190 if (!cu->processing_has_namespace_info
21191 && cu->lang () == language_cplus)
21192 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21193 }
21194 return (sym);
21195 }
21196
21197 /* Given an attr with a DW_FORM_dataN value in host byte order,
21198 zero-extend it as appropriate for the symbol's type. The DWARF
21199 standard (v4) is not entirely clear about the meaning of using
21200 DW_FORM_dataN for a constant with a signed type, where the type is
21201 wider than the data. The conclusion of a discussion on the DWARF
21202 list was that this is unspecified. We choose to always zero-extend
21203 because that is the interpretation long in use by GCC. */
21204
21205 static gdb_byte *
21206 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21207 struct dwarf2_cu *cu, LONGEST *value, int bits)
21208 {
21209 struct objfile *objfile = cu->per_objfile->objfile;
21210 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
21211 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21212 LONGEST l = attr->constant_value (0);
21213
21214 if (bits < sizeof (*value) * 8)
21215 {
21216 l &= ((LONGEST) 1 << bits) - 1;
21217 *value = l;
21218 }
21219 else if (bits == sizeof (*value) * 8)
21220 *value = l;
21221 else
21222 {
21223 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21224 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21225 return bytes;
21226 }
21227
21228 return NULL;
21229 }
21230
21231 /* Read a constant value from an attribute. Either set *VALUE, or if
21232 the value does not fit in *VALUE, set *BYTES - either already
21233 allocated on the objfile obstack, or newly allocated on OBSTACK,
21234 or, set *BATON, if we translated the constant to a location
21235 expression. */
21236
21237 static void
21238 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21239 const char *name, struct obstack *obstack,
21240 struct dwarf2_cu *cu,
21241 LONGEST *value, const gdb_byte **bytes,
21242 struct dwarf2_locexpr_baton **baton)
21243 {
21244 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21245 struct objfile *objfile = per_objfile->objfile;
21246 struct comp_unit_head *cu_header = &cu->header;
21247 struct dwarf_block *blk;
21248 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
21249 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21250
21251 *value = 0;
21252 *bytes = NULL;
21253 *baton = NULL;
21254
21255 switch (attr->form)
21256 {
21257 case DW_FORM_addr:
21258 case DW_FORM_addrx:
21259 case DW_FORM_GNU_addr_index:
21260 {
21261 gdb_byte *data;
21262
21263 if (type->length () != cu_header->addr_size)
21264 dwarf2_const_value_length_mismatch_complaint (name,
21265 cu_header->addr_size,
21266 type->length ());
21267 /* Symbols of this form are reasonably rare, so we just
21268 piggyback on the existing location code rather than writing
21269 a new implementation of symbol_computed_ops. */
21270 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21271 (*baton)->per_objfile = per_objfile;
21272 (*baton)->per_cu = cu->per_cu;
21273 gdb_assert ((*baton)->per_cu);
21274
21275 (*baton)->size = 2 + cu_header->addr_size;
21276 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21277 (*baton)->data = data;
21278
21279 data[0] = DW_OP_addr;
21280 store_unsigned_integer (&data[1], cu_header->addr_size,
21281 byte_order, attr->as_address ());
21282 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21283 }
21284 break;
21285 case DW_FORM_string:
21286 case DW_FORM_strp:
21287 case DW_FORM_strx:
21288 case DW_FORM_GNU_str_index:
21289 case DW_FORM_GNU_strp_alt:
21290 /* The string is already allocated on the objfile obstack, point
21291 directly to it. */
21292 *bytes = (const gdb_byte *) attr->as_string ();
21293 break;
21294 case DW_FORM_block1:
21295 case DW_FORM_block2:
21296 case DW_FORM_block4:
21297 case DW_FORM_block:
21298 case DW_FORM_exprloc:
21299 case DW_FORM_data16:
21300 blk = attr->as_block ();
21301 if (type->length () != blk->size)
21302 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21303 type->length ());
21304 *bytes = blk->data;
21305 break;
21306
21307 /* The DW_AT_const_value attributes are supposed to carry the
21308 symbol's value "represented as it would be on the target
21309 architecture." By the time we get here, it's already been
21310 converted to host endianness, so we just need to sign- or
21311 zero-extend it as appropriate. */
21312 case DW_FORM_data1:
21313 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21314 break;
21315 case DW_FORM_data2:
21316 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21317 break;
21318 case DW_FORM_data4:
21319 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21320 break;
21321 case DW_FORM_data8:
21322 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21323 break;
21324
21325 case DW_FORM_sdata:
21326 case DW_FORM_implicit_const:
21327 *value = attr->as_signed ();
21328 break;
21329
21330 case DW_FORM_udata:
21331 *value = attr->as_unsigned ();
21332 break;
21333
21334 default:
21335 complaint (_("unsupported const value attribute form: '%s'"),
21336 dwarf_form_name (attr->form));
21337 *value = 0;
21338 break;
21339 }
21340 }
21341
21342
21343 /* Copy constant value from an attribute to a symbol. */
21344
21345 static void
21346 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21347 struct dwarf2_cu *cu)
21348 {
21349 struct objfile *objfile = cu->per_objfile->objfile;
21350 LONGEST value;
21351 const gdb_byte *bytes;
21352 struct dwarf2_locexpr_baton *baton;
21353
21354 dwarf2_const_value_attr (attr, sym->type (),
21355 sym->print_name (),
21356 &objfile->objfile_obstack, cu,
21357 &value, &bytes, &baton);
21358
21359 if (baton != NULL)
21360 {
21361 SYMBOL_LOCATION_BATON (sym) = baton;
21362 sym->set_aclass_index (dwarf2_locexpr_index);
21363 }
21364 else if (bytes != NULL)
21365 {
21366 sym->set_value_bytes (bytes);
21367 sym->set_aclass_index (LOC_CONST_BYTES);
21368 }
21369 else
21370 {
21371 sym->set_value_longest (value);
21372 sym->set_aclass_index (LOC_CONST);
21373 }
21374 }
21375
21376 /* Return the type of the die in question using its DW_AT_type attribute. */
21377
21378 static struct type *
21379 die_type (struct die_info *die, struct dwarf2_cu *cu)
21380 {
21381 struct attribute *type_attr;
21382
21383 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21384 if (!type_attr)
21385 {
21386 struct objfile *objfile = cu->per_objfile->objfile;
21387 /* A missing DW_AT_type represents a void type. */
21388 return objfile_type (objfile)->builtin_void;
21389 }
21390
21391 return lookup_die_type (die, type_attr, cu);
21392 }
21393
21394 /* True iff CU's producer generates GNAT Ada auxiliary information
21395 that allows to find parallel types through that information instead
21396 of having to do expensive parallel lookups by type name. */
21397
21398 static int
21399 need_gnat_info (struct dwarf2_cu *cu)
21400 {
21401 /* Assume that the Ada compiler was GNAT, which always produces
21402 the auxiliary information. */
21403 return (cu->lang () == language_ada);
21404 }
21405
21406 /* Return the auxiliary type of the die in question using its
21407 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21408 attribute is not present. */
21409
21410 static struct type *
21411 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21412 {
21413 struct attribute *type_attr;
21414
21415 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21416 if (!type_attr)
21417 return NULL;
21418
21419 return lookup_die_type (die, type_attr, cu);
21420 }
21421
21422 /* If DIE has a descriptive_type attribute, then set the TYPE's
21423 descriptive type accordingly. */
21424
21425 static void
21426 set_descriptive_type (struct type *type, struct die_info *die,
21427 struct dwarf2_cu *cu)
21428 {
21429 struct type *descriptive_type = die_descriptive_type (die, cu);
21430
21431 if (descriptive_type)
21432 {
21433 ALLOCATE_GNAT_AUX_TYPE (type);
21434 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21435 }
21436 }
21437
21438 /* Return the containing type of the die in question using its
21439 DW_AT_containing_type attribute. */
21440
21441 static struct type *
21442 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21443 {
21444 struct attribute *type_attr;
21445 struct objfile *objfile = cu->per_objfile->objfile;
21446
21447 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21448 if (!type_attr)
21449 error (_("Dwarf Error: Problem turning containing type into gdb type "
21450 "[in module %s]"), objfile_name (objfile));
21451
21452 return lookup_die_type (die, type_attr, cu);
21453 }
21454
21455 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21456
21457 static struct type *
21458 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21459 {
21460 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21461 struct objfile *objfile = per_objfile->objfile;
21462 char *saved;
21463
21464 std::string message
21465 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21466 objfile_name (objfile),
21467 sect_offset_str (cu->header.sect_off),
21468 sect_offset_str (die->sect_off));
21469 saved = obstack_strdup (&objfile->objfile_obstack, message);
21470
21471 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21472 }
21473
21474 /* Look up the type of DIE in CU using its type attribute ATTR.
21475 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21476 DW_AT_containing_type.
21477 If there is no type substitute an error marker. */
21478
21479 static struct type *
21480 lookup_die_type (struct die_info *die, const struct attribute *attr,
21481 struct dwarf2_cu *cu)
21482 {
21483 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21484 struct objfile *objfile = per_objfile->objfile;
21485 struct type *this_type;
21486
21487 gdb_assert (attr->name == DW_AT_type
21488 || attr->name == DW_AT_GNAT_descriptive_type
21489 || attr->name == DW_AT_containing_type);
21490
21491 /* First see if we have it cached. */
21492
21493 if (attr->form == DW_FORM_GNU_ref_alt)
21494 {
21495 struct dwarf2_per_cu_data *per_cu;
21496 sect_offset sect_off = attr->get_ref_die_offset ();
21497
21498 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21499 per_objfile->per_bfd);
21500 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
21501 }
21502 else if (attr->form_is_ref ())
21503 {
21504 sect_offset sect_off = attr->get_ref_die_offset ();
21505
21506 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
21507 }
21508 else if (attr->form == DW_FORM_ref_sig8)
21509 {
21510 ULONGEST signature = attr->as_signature ();
21511
21512 return get_signatured_type (die, signature, cu);
21513 }
21514 else
21515 {
21516 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21517 " at %s [in module %s]"),
21518 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21519 objfile_name (objfile));
21520 return build_error_marker_type (cu, die);
21521 }
21522
21523 /* If not cached we need to read it in. */
21524
21525 if (this_type == NULL)
21526 {
21527 struct die_info *type_die = NULL;
21528 struct dwarf2_cu *type_cu = cu;
21529
21530 if (attr->form_is_ref ())
21531 type_die = follow_die_ref (die, attr, &type_cu);
21532 if (type_die == NULL)
21533 return build_error_marker_type (cu, die);
21534 /* If we find the type now, it's probably because the type came
21535 from an inter-CU reference and the type's CU got expanded before
21536 ours. */
21537 this_type = read_type_die (type_die, type_cu);
21538 }
21539
21540 /* If we still don't have a type use an error marker. */
21541
21542 if (this_type == NULL)
21543 return build_error_marker_type (cu, die);
21544
21545 return this_type;
21546 }
21547
21548 /* Return the type in DIE, CU.
21549 Returns NULL for invalid types.
21550
21551 This first does a lookup in die_type_hash,
21552 and only reads the die in if necessary.
21553
21554 NOTE: This can be called when reading in partial or full symbols. */
21555
21556 static struct type *
21557 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21558 {
21559 struct type *this_type;
21560
21561 this_type = get_die_type (die, cu);
21562 if (this_type)
21563 return this_type;
21564
21565 return read_type_die_1 (die, cu);
21566 }
21567
21568 /* Read the type in DIE, CU.
21569 Returns NULL for invalid types. */
21570
21571 static struct type *
21572 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21573 {
21574 struct type *this_type = NULL;
21575
21576 switch (die->tag)
21577 {
21578 case DW_TAG_class_type:
21579 case DW_TAG_interface_type:
21580 case DW_TAG_structure_type:
21581 case DW_TAG_union_type:
21582 this_type = read_structure_type (die, cu);
21583 break;
21584 case DW_TAG_enumeration_type:
21585 this_type = read_enumeration_type (die, cu);
21586 break;
21587 case DW_TAG_subprogram:
21588 case DW_TAG_subroutine_type:
21589 case DW_TAG_inlined_subroutine:
21590 this_type = read_subroutine_type (die, cu);
21591 break;
21592 case DW_TAG_array_type:
21593 this_type = read_array_type (die, cu);
21594 break;
21595 case DW_TAG_set_type:
21596 this_type = read_set_type (die, cu);
21597 break;
21598 case DW_TAG_pointer_type:
21599 this_type = read_tag_pointer_type (die, cu);
21600 break;
21601 case DW_TAG_ptr_to_member_type:
21602 this_type = read_tag_ptr_to_member_type (die, cu);
21603 break;
21604 case DW_TAG_reference_type:
21605 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21606 break;
21607 case DW_TAG_rvalue_reference_type:
21608 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21609 break;
21610 case DW_TAG_const_type:
21611 this_type = read_tag_const_type (die, cu);
21612 break;
21613 case DW_TAG_volatile_type:
21614 this_type = read_tag_volatile_type (die, cu);
21615 break;
21616 case DW_TAG_restrict_type:
21617 this_type = read_tag_restrict_type (die, cu);
21618 break;
21619 case DW_TAG_string_type:
21620 this_type = read_tag_string_type (die, cu);
21621 break;
21622 case DW_TAG_typedef:
21623 this_type = read_typedef (die, cu);
21624 break;
21625 case DW_TAG_generic_subrange:
21626 case DW_TAG_subrange_type:
21627 this_type = read_subrange_type (die, cu);
21628 break;
21629 case DW_TAG_base_type:
21630 this_type = read_base_type (die, cu);
21631 break;
21632 case DW_TAG_unspecified_type:
21633 this_type = read_unspecified_type (die, cu);
21634 break;
21635 case DW_TAG_namespace:
21636 this_type = read_namespace_type (die, cu);
21637 break;
21638 case DW_TAG_module:
21639 this_type = read_module_type (die, cu);
21640 break;
21641 case DW_TAG_atomic_type:
21642 this_type = read_tag_atomic_type (die, cu);
21643 break;
21644 default:
21645 complaint (_("unexpected tag in read_type_die: '%s'"),
21646 dwarf_tag_name (die->tag));
21647 break;
21648 }
21649
21650 return this_type;
21651 }
21652
21653 /* See if we can figure out if the class lives in a namespace. We do
21654 this by looking for a member function; its demangled name will
21655 contain namespace info, if there is any.
21656 Return the computed name or NULL.
21657 Space for the result is allocated on the objfile's obstack.
21658 This is the full-die version of guess_partial_die_structure_name.
21659 In this case we know DIE has no useful parent. */
21660
21661 static const char *
21662 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21663 {
21664 struct die_info *spec_die;
21665 struct dwarf2_cu *spec_cu;
21666 struct die_info *child;
21667 struct objfile *objfile = cu->per_objfile->objfile;
21668
21669 spec_cu = cu;
21670 spec_die = die_specification (die, &spec_cu);
21671 if (spec_die != NULL)
21672 {
21673 die = spec_die;
21674 cu = spec_cu;
21675 }
21676
21677 for (child = die->child;
21678 child != NULL;
21679 child = child->sibling)
21680 {
21681 if (child->tag == DW_TAG_subprogram)
21682 {
21683 const char *linkage_name = dw2_linkage_name (child, cu);
21684
21685 if (linkage_name != NULL)
21686 {
21687 gdb::unique_xmalloc_ptr<char> actual_name
21688 (cu->language_defn->class_name_from_physname (linkage_name));
21689 const char *name = NULL;
21690
21691 if (actual_name != NULL)
21692 {
21693 const char *die_name = dwarf2_name (die, cu);
21694
21695 if (die_name != NULL
21696 && strcmp (die_name, actual_name.get ()) != 0)
21697 {
21698 /* Strip off the class name from the full name.
21699 We want the prefix. */
21700 int die_name_len = strlen (die_name);
21701 int actual_name_len = strlen (actual_name.get ());
21702 const char *ptr = actual_name.get ();
21703
21704 /* Test for '::' as a sanity check. */
21705 if (actual_name_len > die_name_len + 2
21706 && ptr[actual_name_len - die_name_len - 1] == ':')
21707 name = obstack_strndup (
21708 &objfile->per_bfd->storage_obstack,
21709 ptr, actual_name_len - die_name_len - 2);
21710 }
21711 }
21712 return name;
21713 }
21714 }
21715 }
21716
21717 return NULL;
21718 }
21719
21720 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21721 prefix part in such case. See
21722 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21723
21724 static const char *
21725 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21726 {
21727 struct attribute *attr;
21728 const char *base;
21729
21730 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21731 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21732 return NULL;
21733
21734 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21735 return NULL;
21736
21737 attr = dw2_linkage_name_attr (die, cu);
21738 const char *attr_name = attr->as_string ();
21739 if (attr == NULL || attr_name == NULL)
21740 return NULL;
21741
21742 /* dwarf2_name had to be already called. */
21743 gdb_assert (attr->canonical_string_p ());
21744
21745 /* Strip the base name, keep any leading namespaces/classes. */
21746 base = strrchr (attr_name, ':');
21747 if (base == NULL || base == attr_name || base[-1] != ':')
21748 return "";
21749
21750 struct objfile *objfile = cu->per_objfile->objfile;
21751 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21752 attr_name,
21753 &base[-1] - attr_name);
21754 }
21755
21756 /* Return the name of the namespace/class that DIE is defined within,
21757 or "" if we can't tell. The caller should not xfree the result.
21758
21759 For example, if we're within the method foo() in the following
21760 code:
21761
21762 namespace N {
21763 class C {
21764 void foo () {
21765 }
21766 };
21767 }
21768
21769 then determine_prefix on foo's die will return "N::C". */
21770
21771 static const char *
21772 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21773 {
21774 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21775 struct die_info *parent, *spec_die;
21776 struct dwarf2_cu *spec_cu;
21777 struct type *parent_type;
21778 const char *retval;
21779
21780 if (cu->lang () != language_cplus
21781 && cu->lang () != language_fortran
21782 && cu->lang () != language_d
21783 && cu->lang () != language_rust)
21784 return "";
21785
21786 retval = anonymous_struct_prefix (die, cu);
21787 if (retval)
21788 return retval;
21789
21790 /* We have to be careful in the presence of DW_AT_specification.
21791 For example, with GCC 3.4, given the code
21792
21793 namespace N {
21794 void foo() {
21795 // Definition of N::foo.
21796 }
21797 }
21798
21799 then we'll have a tree of DIEs like this:
21800
21801 1: DW_TAG_compile_unit
21802 2: DW_TAG_namespace // N
21803 3: DW_TAG_subprogram // declaration of N::foo
21804 4: DW_TAG_subprogram // definition of N::foo
21805 DW_AT_specification // refers to die #3
21806
21807 Thus, when processing die #4, we have to pretend that we're in
21808 the context of its DW_AT_specification, namely the contex of die
21809 #3. */
21810 spec_cu = cu;
21811 spec_die = die_specification (die, &spec_cu);
21812 if (spec_die == NULL)
21813 parent = die->parent;
21814 else
21815 {
21816 parent = spec_die->parent;
21817 cu = spec_cu;
21818 }
21819
21820 if (parent == NULL)
21821 return "";
21822 else if (parent->building_fullname)
21823 {
21824 const char *name;
21825 const char *parent_name;
21826
21827 /* It has been seen on RealView 2.2 built binaries,
21828 DW_TAG_template_type_param types actually _defined_ as
21829 children of the parent class:
21830
21831 enum E {};
21832 template class <class Enum> Class{};
21833 Class<enum E> class_e;
21834
21835 1: DW_TAG_class_type (Class)
21836 2: DW_TAG_enumeration_type (E)
21837 3: DW_TAG_enumerator (enum1:0)
21838 3: DW_TAG_enumerator (enum2:1)
21839 ...
21840 2: DW_TAG_template_type_param
21841 DW_AT_type DW_FORM_ref_udata (E)
21842
21843 Besides being broken debug info, it can put GDB into an
21844 infinite loop. Consider:
21845
21846 When we're building the full name for Class<E>, we'll start
21847 at Class, and go look over its template type parameters,
21848 finding E. We'll then try to build the full name of E, and
21849 reach here. We're now trying to build the full name of E,
21850 and look over the parent DIE for containing scope. In the
21851 broken case, if we followed the parent DIE of E, we'd again
21852 find Class, and once again go look at its template type
21853 arguments, etc., etc. Simply don't consider such parent die
21854 as source-level parent of this die (it can't be, the language
21855 doesn't allow it), and break the loop here. */
21856 name = dwarf2_name (die, cu);
21857 parent_name = dwarf2_name (parent, cu);
21858 complaint (_("template param type '%s' defined within parent '%s'"),
21859 name ? name : "<unknown>",
21860 parent_name ? parent_name : "<unknown>");
21861 return "";
21862 }
21863 else
21864 switch (parent->tag)
21865 {
21866 case DW_TAG_namespace:
21867 parent_type = read_type_die (parent, cu);
21868 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21869 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21870 Work around this problem here. */
21871 if (cu->lang () == language_cplus
21872 && strcmp (parent_type->name (), "::") == 0)
21873 return "";
21874 /* We give a name to even anonymous namespaces. */
21875 return parent_type->name ();
21876 case DW_TAG_class_type:
21877 case DW_TAG_interface_type:
21878 case DW_TAG_structure_type:
21879 case DW_TAG_union_type:
21880 case DW_TAG_module:
21881 parent_type = read_type_die (parent, cu);
21882 if (parent_type->name () != NULL)
21883 return parent_type->name ();
21884 else
21885 /* An anonymous structure is only allowed non-static data
21886 members; no typedefs, no member functions, et cetera.
21887 So it does not need a prefix. */
21888 return "";
21889 case DW_TAG_compile_unit:
21890 case DW_TAG_partial_unit:
21891 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21892 if (cu->lang () == language_cplus
21893 && !per_objfile->per_bfd->types.empty ()
21894 && die->child != NULL
21895 && (die->tag == DW_TAG_class_type
21896 || die->tag == DW_TAG_structure_type
21897 || die->tag == DW_TAG_union_type))
21898 {
21899 const char *name = guess_full_die_structure_name (die, cu);
21900 if (name != NULL)
21901 return name;
21902 }
21903 return "";
21904 case DW_TAG_subprogram:
21905 /* Nested subroutines in Fortran get a prefix with the name
21906 of the parent's subroutine. */
21907 if (cu->lang () == language_fortran)
21908 {
21909 if ((die->tag == DW_TAG_subprogram)
21910 && (dwarf2_name (parent, cu) != NULL))
21911 return dwarf2_name (parent, cu);
21912 }
21913 return "";
21914 case DW_TAG_enumeration_type:
21915 parent_type = read_type_die (parent, cu);
21916 if (parent_type->is_declared_class ())
21917 {
21918 if (parent_type->name () != NULL)
21919 return parent_type->name ();
21920 return "";
21921 }
21922 /* Fall through. */
21923 default:
21924 return determine_prefix (parent, cu);
21925 }
21926 }
21927
21928 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21929 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21930 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21931 an obconcat, otherwise allocate storage for the result. The CU argument is
21932 used to determine the language and hence, the appropriate separator. */
21933
21934 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21935
21936 static char *
21937 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21938 int physname, struct dwarf2_cu *cu)
21939 {
21940 const char *lead = "";
21941 const char *sep;
21942
21943 if (suffix == NULL || suffix[0] == '\0'
21944 || prefix == NULL || prefix[0] == '\0')
21945 sep = "";
21946 else if (cu->lang () == language_d)
21947 {
21948 /* For D, the 'main' function could be defined in any module, but it
21949 should never be prefixed. */
21950 if (strcmp (suffix, "D main") == 0)
21951 {
21952 prefix = "";
21953 sep = "";
21954 }
21955 else
21956 sep = ".";
21957 }
21958 else if (cu->lang () == language_fortran && physname)
21959 {
21960 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21961 DW_AT_MIPS_linkage_name is preferred and used instead. */
21962
21963 lead = "__";
21964 sep = "_MOD_";
21965 }
21966 else
21967 sep = "::";
21968
21969 if (prefix == NULL)
21970 prefix = "";
21971 if (suffix == NULL)
21972 suffix = "";
21973
21974 if (obs == NULL)
21975 {
21976 char *retval
21977 = ((char *)
21978 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21979
21980 strcpy (retval, lead);
21981 strcat (retval, prefix);
21982 strcat (retval, sep);
21983 strcat (retval, suffix);
21984 return retval;
21985 }
21986 else
21987 {
21988 /* We have an obstack. */
21989 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21990 }
21991 }
21992
21993 /* Return a generic name for a DW_TAG_template_type_param or
21994 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
21995 per parent, so each function/class/struct template will have their own set
21996 of template parameters named <unnnamed0>, <unnamed1>, ... where the
21997 enumeration starts at 0 and represents the position of the template tag in
21998 the list of unnamed template tags for this parent, counting both, type and
21999 value tags. */
22000
22001 static const char *
22002 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
22003 {
22004 if (die->parent == nullptr)
22005 return nullptr;
22006
22007 /* Count the parent types unnamed template type and value children until, we
22008 arrive at our entry. */
22009 size_t nth_unnamed = 0;
22010
22011 die_info *child = die->parent->child;
22012 while (child != die)
22013 {
22014 gdb_assert (child != nullptr);
22015 if (child->tag == DW_TAG_template_type_param
22016 || child->tag == DW_TAG_template_value_param)
22017 {
22018 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
22019 ++nth_unnamed;
22020 }
22021 child = child->sibling;
22022 }
22023
22024 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
22025 return cu->per_objfile->objfile->intern (name_str.c_str ());
22026 }
22027
22028 /* Get name of a die, return NULL if not found. */
22029
22030 static const char *
22031 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22032 struct objfile *objfile)
22033 {
22034 if (name == nullptr)
22035 return name;
22036
22037 if (cu->lang () == language_cplus)
22038 {
22039 gdb::unique_xmalloc_ptr<char> canon_name
22040 = cp_canonicalize_string (name);
22041
22042 if (canon_name != nullptr)
22043 name = objfile->intern (canon_name.get ());
22044 }
22045 else if (cu->lang () == language_c)
22046 {
22047 gdb::unique_xmalloc_ptr<char> canon_name
22048 = c_canonicalize_name (name);
22049
22050 if (canon_name != nullptr)
22051 name = objfile->intern (canon_name.get ());
22052 }
22053
22054 return name;
22055 }
22056
22057 /* Get name of a die, return NULL if not found.
22058 Anonymous namespaces are converted to their magic string. */
22059
22060 static const char *
22061 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22062 {
22063 struct attribute *attr;
22064 struct objfile *objfile = cu->per_objfile->objfile;
22065
22066 attr = dwarf2_attr (die, DW_AT_name, cu);
22067 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22068 if (attr_name == nullptr
22069 && die->tag != DW_TAG_namespace
22070 && die->tag != DW_TAG_class_type
22071 && die->tag != DW_TAG_interface_type
22072 && die->tag != DW_TAG_structure_type
22073 && die->tag != DW_TAG_namelist
22074 && die->tag != DW_TAG_union_type
22075 && die->tag != DW_TAG_template_type_param
22076 && die->tag != DW_TAG_template_value_param)
22077 return NULL;
22078
22079 switch (die->tag)
22080 {
22081 /* A member's name should not be canonicalized. This is a bit
22082 of a hack, in that normally it should not be possible to run
22083 into this situation; however, the dw2-unusual-field-names.exp
22084 test creates custom DWARF that does. */
22085 case DW_TAG_member:
22086 case DW_TAG_compile_unit:
22087 case DW_TAG_partial_unit:
22088 /* Compilation units have a DW_AT_name that is a filename, not
22089 a source language identifier. */
22090 case DW_TAG_enumeration_type:
22091 case DW_TAG_enumerator:
22092 /* These tags always have simple identifiers already; no need
22093 to canonicalize them. */
22094 return attr_name;
22095
22096 case DW_TAG_namespace:
22097 if (attr_name != nullptr)
22098 return attr_name;
22099 return CP_ANONYMOUS_NAMESPACE_STR;
22100
22101 /* DWARF does not actually require template tags to have a name. */
22102 case DW_TAG_template_type_param:
22103 case DW_TAG_template_value_param:
22104 if (attr_name == nullptr)
22105 return unnamed_template_tag_name (die, cu);
22106 /* FALLTHROUGH. */
22107 case DW_TAG_class_type:
22108 case DW_TAG_interface_type:
22109 case DW_TAG_structure_type:
22110 case DW_TAG_union_type:
22111 case DW_TAG_namelist:
22112 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22113 structures or unions. These were of the form "._%d" in GCC 4.1,
22114 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22115 and GCC 4.4. We work around this problem by ignoring these. */
22116 if (attr_name != nullptr
22117 && (startswith (attr_name, "._")
22118 || startswith (attr_name, "<anonymous")))
22119 return NULL;
22120
22121 /* GCC might emit a nameless typedef that has a linkage name. See
22122 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22123 if (!attr || attr_name == NULL)
22124 {
22125 attr = dw2_linkage_name_attr (die, cu);
22126 attr_name = attr == nullptr ? nullptr : attr->as_string ();
22127 if (attr == NULL || attr_name == NULL)
22128 return NULL;
22129
22130 /* Avoid demangling attr_name the second time on a second
22131 call for the same DIE. */
22132 if (!attr->canonical_string_p ())
22133 {
22134 gdb::unique_xmalloc_ptr<char> demangled
22135 (gdb_demangle (attr_name, DMGL_TYPES));
22136 if (demangled == nullptr)
22137 return nullptr;
22138
22139 attr->set_string_canonical (objfile->intern (demangled.get ()));
22140 attr_name = attr->as_string ();
22141 }
22142
22143 /* Strip any leading namespaces/classes, keep only the
22144 base name. DW_AT_name for named DIEs does not
22145 contain the prefixes. */
22146 const char *base = strrchr (attr_name, ':');
22147 if (base && base > attr_name && base[-1] == ':')
22148 return &base[1];
22149 else
22150 return attr_name;
22151 }
22152 break;
22153
22154 default:
22155 break;
22156 }
22157
22158 if (!attr->canonical_string_p ())
22159 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22160 objfile));
22161 return attr->as_string ();
22162 }
22163
22164 /* Return the die that this die in an extension of, or NULL if there
22165 is none. *EXT_CU is the CU containing DIE on input, and the CU
22166 containing the return value on output. */
22167
22168 static struct die_info *
22169 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22170 {
22171 struct attribute *attr;
22172
22173 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22174 if (attr == NULL)
22175 return NULL;
22176
22177 return follow_die_ref (die, attr, ext_cu);
22178 }
22179
22180 static void
22181 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22182 {
22183 unsigned int i;
22184
22185 gdb_printf (f, "%*sDie: %s (abbrev %d, offset %s)\n",
22186 indent, "",
22187 dwarf_tag_name (die->tag), die->abbrev,
22188 sect_offset_str (die->sect_off));
22189
22190 if (die->parent != NULL)
22191 gdb_printf (f, "%*s parent at offset: %s\n",
22192 indent, "",
22193 sect_offset_str (die->parent->sect_off));
22194
22195 gdb_printf (f, "%*s has children: %s\n",
22196 indent, "",
22197 dwarf_bool_name (die->child != NULL));
22198
22199 gdb_printf (f, "%*s attributes:\n", indent, "");
22200
22201 for (i = 0; i < die->num_attrs; ++i)
22202 {
22203 gdb_printf (f, "%*s %s (%s) ",
22204 indent, "",
22205 dwarf_attr_name (die->attrs[i].name),
22206 dwarf_form_name (die->attrs[i].form));
22207
22208 switch (die->attrs[i].form)
22209 {
22210 case DW_FORM_addr:
22211 case DW_FORM_addrx:
22212 case DW_FORM_GNU_addr_index:
22213 gdb_printf (f, "address: ");
22214 gdb_puts (hex_string (die->attrs[i].as_address ()), f);
22215 break;
22216 case DW_FORM_block2:
22217 case DW_FORM_block4:
22218 case DW_FORM_block:
22219 case DW_FORM_block1:
22220 gdb_printf (f, "block: size %s",
22221 pulongest (die->attrs[i].as_block ()->size));
22222 break;
22223 case DW_FORM_exprloc:
22224 gdb_printf (f, "expression: size %s",
22225 pulongest (die->attrs[i].as_block ()->size));
22226 break;
22227 case DW_FORM_data16:
22228 gdb_printf (f, "constant of 16 bytes");
22229 break;
22230 case DW_FORM_ref_addr:
22231 gdb_printf (f, "ref address: ");
22232 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22233 break;
22234 case DW_FORM_GNU_ref_alt:
22235 gdb_printf (f, "alt ref address: ");
22236 gdb_puts (hex_string (die->attrs[i].as_unsigned ()), f);
22237 break;
22238 case DW_FORM_ref1:
22239 case DW_FORM_ref2:
22240 case DW_FORM_ref4:
22241 case DW_FORM_ref8:
22242 case DW_FORM_ref_udata:
22243 gdb_printf (f, "constant ref: 0x%lx (adjusted)",
22244 (long) (die->attrs[i].as_unsigned ()));
22245 break;
22246 case DW_FORM_data1:
22247 case DW_FORM_data2:
22248 case DW_FORM_data4:
22249 case DW_FORM_data8:
22250 case DW_FORM_udata:
22251 gdb_printf (f, "constant: %s",
22252 pulongest (die->attrs[i].as_unsigned ()));
22253 break;
22254 case DW_FORM_sec_offset:
22255 gdb_printf (f, "section offset: %s",
22256 pulongest (die->attrs[i].as_unsigned ()));
22257 break;
22258 case DW_FORM_ref_sig8:
22259 gdb_printf (f, "signature: %s",
22260 hex_string (die->attrs[i].as_signature ()));
22261 break;
22262 case DW_FORM_string:
22263 case DW_FORM_strp:
22264 case DW_FORM_line_strp:
22265 case DW_FORM_strx:
22266 case DW_FORM_GNU_str_index:
22267 case DW_FORM_GNU_strp_alt:
22268 gdb_printf (f, "string: \"%s\" (%s canonicalized)",
22269 die->attrs[i].as_string ()
22270 ? die->attrs[i].as_string () : "",
22271 die->attrs[i].canonical_string_p () ? "is" : "not");
22272 break;
22273 case DW_FORM_flag:
22274 if (die->attrs[i].as_boolean ())
22275 gdb_printf (f, "flag: TRUE");
22276 else
22277 gdb_printf (f, "flag: FALSE");
22278 break;
22279 case DW_FORM_flag_present:
22280 gdb_printf (f, "flag: TRUE");
22281 break;
22282 case DW_FORM_indirect:
22283 /* The reader will have reduced the indirect form to
22284 the "base form" so this form should not occur. */
22285 gdb_printf (f,
22286 "unexpected attribute form: DW_FORM_indirect");
22287 break;
22288 case DW_FORM_sdata:
22289 case DW_FORM_implicit_const:
22290 gdb_printf (f, "constant: %s",
22291 plongest (die->attrs[i].as_signed ()));
22292 break;
22293 default:
22294 gdb_printf (f, "unsupported attribute form: %d.",
22295 die->attrs[i].form);
22296 break;
22297 }
22298 gdb_printf (f, "\n");
22299 }
22300 }
22301
22302 static void
22303 dump_die_for_error (struct die_info *die)
22304 {
22305 dump_die_shallow (gdb_stderr, 0, die);
22306 }
22307
22308 static void
22309 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22310 {
22311 int indent = level * 4;
22312
22313 gdb_assert (die != NULL);
22314
22315 if (level >= max_level)
22316 return;
22317
22318 dump_die_shallow (f, indent, die);
22319
22320 if (die->child != NULL)
22321 {
22322 gdb_printf (f, "%*s Children:", indent, "");
22323 if (level + 1 < max_level)
22324 {
22325 gdb_printf (f, "\n");
22326 dump_die_1 (f, level + 1, max_level, die->child);
22327 }
22328 else
22329 {
22330 gdb_printf (f,
22331 " [not printed, max nesting level reached]\n");
22332 }
22333 }
22334
22335 if (die->sibling != NULL && level > 0)
22336 {
22337 dump_die_1 (f, level, max_level, die->sibling);
22338 }
22339 }
22340
22341 /* This is called from the pdie macro in gdbinit.in.
22342 It's not static so gcc will keep a copy callable from gdb. */
22343
22344 void
22345 dump_die (struct die_info *die, int max_level)
22346 {
22347 dump_die_1 (gdb_stdlog, 0, max_level, die);
22348 }
22349
22350 static void
22351 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22352 {
22353 void **slot;
22354
22355 slot = htab_find_slot_with_hash (cu->die_hash, die,
22356 to_underlying (die->sect_off),
22357 INSERT);
22358
22359 *slot = die;
22360 }
22361
22362 /* Follow reference or signature attribute ATTR of SRC_DIE.
22363 On entry *REF_CU is the CU of SRC_DIE.
22364 On exit *REF_CU is the CU of the result. */
22365
22366 static struct die_info *
22367 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22368 struct dwarf2_cu **ref_cu)
22369 {
22370 struct die_info *die;
22371
22372 if (attr->form_is_ref ())
22373 die = follow_die_ref (src_die, attr, ref_cu);
22374 else if (attr->form == DW_FORM_ref_sig8)
22375 die = follow_die_sig (src_die, attr, ref_cu);
22376 else
22377 {
22378 dump_die_for_error (src_die);
22379 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22380 objfile_name ((*ref_cu)->per_objfile->objfile));
22381 }
22382
22383 return die;
22384 }
22385
22386 /* Follow reference OFFSET.
22387 On entry *REF_CU is the CU of the source die referencing OFFSET.
22388 On exit *REF_CU is the CU of the result.
22389 Returns NULL if OFFSET is invalid. */
22390
22391 static struct die_info *
22392 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22393 struct dwarf2_cu **ref_cu)
22394 {
22395 struct die_info temp_die;
22396 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22397 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22398
22399 gdb_assert (cu->per_cu != NULL);
22400
22401 target_cu = cu;
22402
22403 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22404 "source CU contains target offset: %d",
22405 sect_offset_str (cu->per_cu->sect_off),
22406 sect_offset_str (sect_off),
22407 cu->header.offset_in_cu_p (sect_off));
22408
22409 if (cu->per_cu->is_debug_types)
22410 {
22411 /* .debug_types CUs cannot reference anything outside their CU.
22412 If they need to, they have to reference a signatured type via
22413 DW_FORM_ref_sig8. */
22414 if (!cu->header.offset_in_cu_p (sect_off))
22415 return NULL;
22416 }
22417 else if (offset_in_dwz != cu->per_cu->is_dwz
22418 || !cu->header.offset_in_cu_p (sect_off))
22419 {
22420 struct dwarf2_per_cu_data *per_cu;
22421
22422 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22423 per_objfile->per_bfd);
22424
22425 dwarf_read_debug_printf_v ("target CU offset: %s, "
22426 "target CU DIEs loaded: %d",
22427 sect_offset_str (per_cu->sect_off),
22428 per_objfile->get_cu (per_cu) != nullptr);
22429
22430 /* If necessary, add it to the queue and load its DIEs.
22431
22432 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22433 it doesn't mean they are currently loaded. Since we require them
22434 to be loaded, we must check for ourselves. */
22435 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
22436 || per_objfile->get_cu (per_cu) == nullptr)
22437 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22438 false, cu->lang ());
22439
22440 target_cu = per_objfile->get_cu (per_cu);
22441 gdb_assert (target_cu != nullptr);
22442 }
22443 else if (cu->dies == NULL)
22444 {
22445 /* We're loading full DIEs during partial symbol reading. */
22446 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22447 language_minimal);
22448 }
22449
22450 *ref_cu = target_cu;
22451 temp_die.sect_off = sect_off;
22452
22453 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22454 &temp_die,
22455 to_underlying (sect_off));
22456 }
22457
22458 /* Follow reference attribute ATTR of SRC_DIE.
22459 On entry *REF_CU is the CU of SRC_DIE.
22460 On exit *REF_CU is the CU of the result. */
22461
22462 static struct die_info *
22463 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22464 struct dwarf2_cu **ref_cu)
22465 {
22466 sect_offset sect_off = attr->get_ref_die_offset ();
22467 struct dwarf2_cu *cu = *ref_cu;
22468 struct die_info *die;
22469
22470 die = follow_die_offset (sect_off,
22471 (attr->form == DW_FORM_GNU_ref_alt
22472 || cu->per_cu->is_dwz),
22473 ref_cu);
22474 if (!die)
22475 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22476 "at %s [in module %s]"),
22477 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22478 objfile_name (cu->per_objfile->objfile));
22479
22480 return die;
22481 }
22482
22483 /* See read.h. */
22484
22485 struct dwarf2_locexpr_baton
22486 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22487 dwarf2_per_cu_data *per_cu,
22488 dwarf2_per_objfile *per_objfile,
22489 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22490 bool resolve_abstract_p)
22491 {
22492 struct die_info *die;
22493 struct attribute *attr;
22494 struct dwarf2_locexpr_baton retval;
22495 struct objfile *objfile = per_objfile->objfile;
22496
22497 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22498 if (cu == nullptr)
22499 cu = load_cu (per_cu, per_objfile, false);
22500
22501 if (cu == nullptr)
22502 {
22503 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22504 Instead just throw an error, not much else we can do. */
22505 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22506 sect_offset_str (sect_off), objfile_name (objfile));
22507 }
22508
22509 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22510 if (!die)
22511 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22512 sect_offset_str (sect_off), objfile_name (objfile));
22513
22514 attr = dwarf2_attr (die, DW_AT_location, cu);
22515 if (!attr && resolve_abstract_p
22516 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22517 != per_objfile->per_bfd->abstract_to_concrete.end ()))
22518 {
22519 CORE_ADDR pc = get_frame_pc ();
22520 CORE_ADDR baseaddr = objfile->text_section_offset ();
22521 struct gdbarch *gdbarch = objfile->arch ();
22522
22523 for (const auto &cand_off
22524 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22525 {
22526 struct dwarf2_cu *cand_cu = cu;
22527 struct die_info *cand
22528 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22529 if (!cand
22530 || !cand->parent
22531 || cand->parent->tag != DW_TAG_subprogram)
22532 continue;
22533
22534 CORE_ADDR pc_low, pc_high;
22535 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22536 if (pc_low == ((CORE_ADDR) -1))
22537 continue;
22538 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22539 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22540 if (!(pc_low <= pc && pc < pc_high))
22541 continue;
22542
22543 die = cand;
22544 attr = dwarf2_attr (die, DW_AT_location, cu);
22545 break;
22546 }
22547 }
22548
22549 if (!attr)
22550 {
22551 /* DWARF: "If there is no such attribute, then there is no effect.".
22552 DATA is ignored if SIZE is 0. */
22553
22554 retval.data = NULL;
22555 retval.size = 0;
22556 }
22557 else if (attr->form_is_section_offset ())
22558 {
22559 struct dwarf2_loclist_baton loclist_baton;
22560 CORE_ADDR pc = get_frame_pc ();
22561 size_t size;
22562
22563 fill_in_loclist_baton (cu, &loclist_baton, attr);
22564
22565 retval.data = dwarf2_find_location_expression (&loclist_baton,
22566 &size, pc);
22567 retval.size = size;
22568 }
22569 else
22570 {
22571 if (!attr->form_is_block ())
22572 error (_("Dwarf Error: DIE at %s referenced in module %s "
22573 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22574 sect_offset_str (sect_off), objfile_name (objfile));
22575
22576 struct dwarf_block *block = attr->as_block ();
22577 retval.data = block->data;
22578 retval.size = block->size;
22579 }
22580 retval.per_objfile = per_objfile;
22581 retval.per_cu = cu->per_cu;
22582
22583 per_objfile->age_comp_units ();
22584
22585 return retval;
22586 }
22587
22588 /* See read.h. */
22589
22590 struct dwarf2_locexpr_baton
22591 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22592 dwarf2_per_cu_data *per_cu,
22593 dwarf2_per_objfile *per_objfile,
22594 gdb::function_view<CORE_ADDR ()> get_frame_pc)
22595 {
22596 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22597
22598 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22599 get_frame_pc);
22600 }
22601
22602 /* Write a constant of a given type as target-ordered bytes into
22603 OBSTACK. */
22604
22605 static const gdb_byte *
22606 write_constant_as_bytes (struct obstack *obstack,
22607 enum bfd_endian byte_order,
22608 struct type *type,
22609 ULONGEST value,
22610 LONGEST *len)
22611 {
22612 gdb_byte *result;
22613
22614 *len = type->length ();
22615 result = (gdb_byte *) obstack_alloc (obstack, *len);
22616 store_unsigned_integer (result, *len, byte_order, value);
22617
22618 return result;
22619 }
22620
22621 /* See read.h. */
22622
22623 const gdb_byte *
22624 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22625 dwarf2_per_cu_data *per_cu,
22626 dwarf2_per_objfile *per_objfile,
22627 obstack *obstack,
22628 LONGEST *len)
22629 {
22630 struct die_info *die;
22631 struct attribute *attr;
22632 const gdb_byte *result = NULL;
22633 struct type *type;
22634 LONGEST value;
22635 enum bfd_endian byte_order;
22636 struct objfile *objfile = per_objfile->objfile;
22637
22638 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22639 if (cu == nullptr)
22640 cu = load_cu (per_cu, per_objfile, false);
22641
22642 if (cu == nullptr)
22643 {
22644 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22645 Instead just throw an error, not much else we can do. */
22646 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22647 sect_offset_str (sect_off), objfile_name (objfile));
22648 }
22649
22650 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22651 if (!die)
22652 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22653 sect_offset_str (sect_off), objfile_name (objfile));
22654
22655 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22656 if (attr == NULL)
22657 return NULL;
22658
22659 byte_order = (bfd_big_endian (objfile->obfd.get ())
22660 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22661
22662 switch (attr->form)
22663 {
22664 case DW_FORM_addr:
22665 case DW_FORM_addrx:
22666 case DW_FORM_GNU_addr_index:
22667 {
22668 gdb_byte *tem;
22669
22670 *len = cu->header.addr_size;
22671 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22672 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
22673 result = tem;
22674 }
22675 break;
22676 case DW_FORM_string:
22677 case DW_FORM_strp:
22678 case DW_FORM_strx:
22679 case DW_FORM_GNU_str_index:
22680 case DW_FORM_GNU_strp_alt:
22681 /* The string is already allocated on the objfile obstack, point
22682 directly to it. */
22683 {
22684 const char *attr_name = attr->as_string ();
22685 result = (const gdb_byte *) attr_name;
22686 *len = strlen (attr_name);
22687 }
22688 break;
22689 case DW_FORM_block1:
22690 case DW_FORM_block2:
22691 case DW_FORM_block4:
22692 case DW_FORM_block:
22693 case DW_FORM_exprloc:
22694 case DW_FORM_data16:
22695 {
22696 struct dwarf_block *block = attr->as_block ();
22697 result = block->data;
22698 *len = block->size;
22699 }
22700 break;
22701
22702 /* The DW_AT_const_value attributes are supposed to carry the
22703 symbol's value "represented as it would be on the target
22704 architecture." By the time we get here, it's already been
22705 converted to host endianness, so we just need to sign- or
22706 zero-extend it as appropriate. */
22707 case DW_FORM_data1:
22708 type = die_type (die, cu);
22709 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22710 if (result == NULL)
22711 result = write_constant_as_bytes (obstack, byte_order,
22712 type, value, len);
22713 break;
22714 case DW_FORM_data2:
22715 type = die_type (die, cu);
22716 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22717 if (result == NULL)
22718 result = write_constant_as_bytes (obstack, byte_order,
22719 type, value, len);
22720 break;
22721 case DW_FORM_data4:
22722 type = die_type (die, cu);
22723 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22724 if (result == NULL)
22725 result = write_constant_as_bytes (obstack, byte_order,
22726 type, value, len);
22727 break;
22728 case DW_FORM_data8:
22729 type = die_type (die, cu);
22730 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22731 if (result == NULL)
22732 result = write_constant_as_bytes (obstack, byte_order,
22733 type, value, len);
22734 break;
22735
22736 case DW_FORM_sdata:
22737 case DW_FORM_implicit_const:
22738 type = die_type (die, cu);
22739 result = write_constant_as_bytes (obstack, byte_order,
22740 type, attr->as_signed (), len);
22741 break;
22742
22743 case DW_FORM_udata:
22744 type = die_type (die, cu);
22745 result = write_constant_as_bytes (obstack, byte_order,
22746 type, attr->as_unsigned (), len);
22747 break;
22748
22749 default:
22750 complaint (_("unsupported const value attribute form: '%s'"),
22751 dwarf_form_name (attr->form));
22752 break;
22753 }
22754
22755 return result;
22756 }
22757
22758 /* See read.h. */
22759
22760 struct type *
22761 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22762 dwarf2_per_cu_data *per_cu,
22763 dwarf2_per_objfile *per_objfile,
22764 const char **var_name)
22765 {
22766 struct die_info *die;
22767
22768 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22769 if (cu == nullptr)
22770 cu = load_cu (per_cu, per_objfile, false);
22771
22772 if (cu == nullptr)
22773 return nullptr;
22774
22775 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22776 if (!die)
22777 return NULL;
22778
22779 if (var_name != nullptr)
22780 *var_name = var_decl_name (die, cu);
22781 return die_type (die, cu);
22782 }
22783
22784 /* See read.h. */
22785
22786 struct type *
22787 dwarf2_get_die_type (cu_offset die_offset,
22788 dwarf2_per_cu_data *per_cu,
22789 dwarf2_per_objfile *per_objfile)
22790 {
22791 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22792 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22793 }
22794
22795 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22796 On entry *REF_CU is the CU of SRC_DIE.
22797 On exit *REF_CU is the CU of the result.
22798 Returns NULL if the referenced DIE isn't found. */
22799
22800 static struct die_info *
22801 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22802 struct dwarf2_cu **ref_cu)
22803 {
22804 struct die_info temp_die;
22805 struct dwarf2_cu *sig_cu;
22806 struct die_info *die;
22807 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
22808
22809
22810 /* While it might be nice to assert sig_type->type == NULL here,
22811 we can get here for DW_AT_imported_declaration where we need
22812 the DIE not the type. */
22813
22814 /* If necessary, add it to the queue and load its DIEs.
22815
22816 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22817 it doesn't mean they are currently loaded. Since we require them
22818 to be loaded, we must check for ourselves. */
22819 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
22820 language_minimal)
22821 || per_objfile->get_cu (sig_type) == nullptr)
22822 read_signatured_type (sig_type, per_objfile);
22823
22824 sig_cu = per_objfile->get_cu (sig_type);
22825 gdb_assert (sig_cu != NULL);
22826 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22827 temp_die.sect_off = sig_type->type_offset_in_section;
22828 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22829 to_underlying (temp_die.sect_off));
22830 if (die)
22831 {
22832 /* For .gdb_index version 7 keep track of included TUs.
22833 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22834 if (per_objfile->per_bfd->index_table != NULL
22835 && !per_objfile->per_bfd->index_table->version_check ())
22836 {
22837 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22838 }
22839
22840 *ref_cu = sig_cu;
22841 return die;
22842 }
22843
22844 return NULL;
22845 }
22846
22847 /* Follow signatured type referenced by ATTR in SRC_DIE.
22848 On entry *REF_CU is the CU of SRC_DIE.
22849 On exit *REF_CU is the CU of the result.
22850 The result is the DIE of the type.
22851 If the referenced type cannot be found an error is thrown. */
22852
22853 static struct die_info *
22854 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22855 struct dwarf2_cu **ref_cu)
22856 {
22857 ULONGEST signature = attr->as_signature ();
22858 struct signatured_type *sig_type;
22859 struct die_info *die;
22860
22861 gdb_assert (attr->form == DW_FORM_ref_sig8);
22862
22863 sig_type = lookup_signatured_type (*ref_cu, signature);
22864 /* sig_type will be NULL if the signatured type is missing from
22865 the debug info. */
22866 if (sig_type == NULL)
22867 {
22868 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22869 " from DIE at %s [in module %s]"),
22870 hex_string (signature), sect_offset_str (src_die->sect_off),
22871 objfile_name ((*ref_cu)->per_objfile->objfile));
22872 }
22873
22874 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22875 if (die == NULL)
22876 {
22877 dump_die_for_error (src_die);
22878 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22879 " from DIE at %s [in module %s]"),
22880 hex_string (signature), sect_offset_str (src_die->sect_off),
22881 objfile_name ((*ref_cu)->per_objfile->objfile));
22882 }
22883
22884 return die;
22885 }
22886
22887 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22888 reading in and processing the type unit if necessary. */
22889
22890 static struct type *
22891 get_signatured_type (struct die_info *die, ULONGEST signature,
22892 struct dwarf2_cu *cu)
22893 {
22894 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22895 struct signatured_type *sig_type;
22896 struct dwarf2_cu *type_cu;
22897 struct die_info *type_die;
22898 struct type *type;
22899
22900 sig_type = lookup_signatured_type (cu, signature);
22901 /* sig_type will be NULL if the signatured type is missing from
22902 the debug info. */
22903 if (sig_type == NULL)
22904 {
22905 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22906 " from DIE at %s [in module %s]"),
22907 hex_string (signature), sect_offset_str (die->sect_off),
22908 objfile_name (per_objfile->objfile));
22909 return build_error_marker_type (cu, die);
22910 }
22911
22912 /* If we already know the type we're done. */
22913 type = per_objfile->get_type_for_signatured_type (sig_type);
22914 if (type != nullptr)
22915 return type;
22916
22917 type_cu = cu;
22918 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22919 if (type_die != NULL)
22920 {
22921 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22922 is created. This is important, for example, because for c++ classes
22923 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22924 type = read_type_die (type_die, type_cu);
22925 if (type == NULL)
22926 {
22927 complaint (_("Dwarf Error: Cannot build signatured type %s"
22928 " referenced from DIE at %s [in module %s]"),
22929 hex_string (signature), sect_offset_str (die->sect_off),
22930 objfile_name (per_objfile->objfile));
22931 type = build_error_marker_type (cu, die);
22932 }
22933 }
22934 else
22935 {
22936 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22937 " from DIE at %s [in module %s]"),
22938 hex_string (signature), sect_offset_str (die->sect_off),
22939 objfile_name (per_objfile->objfile));
22940 type = build_error_marker_type (cu, die);
22941 }
22942
22943 per_objfile->set_type_for_signatured_type (sig_type, type);
22944
22945 return type;
22946 }
22947
22948 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22949 reading in and processing the type unit if necessary. */
22950
22951 static struct type *
22952 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22953 struct dwarf2_cu *cu) /* ARI: editCase function */
22954 {
22955 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22956 if (attr->form_is_ref ())
22957 {
22958 struct dwarf2_cu *type_cu = cu;
22959 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22960
22961 return read_type_die (type_die, type_cu);
22962 }
22963 else if (attr->form == DW_FORM_ref_sig8)
22964 {
22965 return get_signatured_type (die, attr->as_signature (), cu);
22966 }
22967 else
22968 {
22969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22970
22971 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22972 " at %s [in module %s]"),
22973 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22974 objfile_name (per_objfile->objfile));
22975 return build_error_marker_type (cu, die);
22976 }
22977 }
22978
22979 /* Load the DIEs associated with type unit PER_CU into memory. */
22980
22981 static void
22982 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22983 dwarf2_per_objfile *per_objfile)
22984 {
22985 struct signatured_type *sig_type;
22986
22987 /* We have the per_cu, but we need the signatured_type.
22988 Fortunately this is an easy translation. */
22989 gdb_assert (per_cu->is_debug_types);
22990 sig_type = (struct signatured_type *) per_cu;
22991
22992 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22993
22994 read_signatured_type (sig_type, per_objfile);
22995
22996 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22997 }
22998
22999 /* Read in a signatured type and build its CU and DIEs.
23000 If the type is a stub for the real type in a DWO file,
23001 read in the real type from the DWO file as well. */
23002
23003 static void
23004 read_signatured_type (signatured_type *sig_type,
23005 dwarf2_per_objfile *per_objfile)
23006 {
23007 gdb_assert (sig_type->is_debug_types);
23008 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23009
23010 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23011
23012 if (!reader.dummy_p)
23013 {
23014 struct dwarf2_cu *cu = reader.cu;
23015 const gdb_byte *info_ptr = reader.info_ptr;
23016
23017 gdb_assert (cu->die_hash == NULL);
23018 cu->die_hash =
23019 htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
23020 die_hash,
23021 die_eq,
23022 NULL,
23023 &cu->comp_unit_obstack,
23024 hashtab_obstack_allocate,
23025 dummy_obstack_deallocate);
23026
23027 if (reader.comp_unit_die->has_children)
23028 reader.comp_unit_die->child
23029 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23030 reader.comp_unit_die);
23031 cu->dies = reader.comp_unit_die;
23032 /* comp_unit_die is not stored in die_hash, no need. */
23033
23034 /* We try not to read any attributes in this function, because
23035 not all CUs needed for references have been loaded yet, and
23036 symbol table processing isn't initialized. But we have to
23037 set the CU language, or we won't be able to build types
23038 correctly. Similarly, if we do not read the producer, we can
23039 not apply producer-specific interpretation. */
23040 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23041
23042 reader.keep ();
23043 }
23044
23045 sig_type->tu_read = 1;
23046 }
23047
23048 /* Decode simple location descriptions.
23049 Given a pointer to a dwarf block that defines a location, compute
23050 the location and return the value. If COMPUTED is non-null, it is
23051 set to true to indicate that decoding was successful, and false
23052 otherwise. If COMPUTED is null, then this function may emit a
23053 complaint. */
23054
23055 static CORE_ADDR
23056 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23057 {
23058 struct objfile *objfile = cu->per_objfile->objfile;
23059 size_t i;
23060 size_t size = blk->size;
23061 const gdb_byte *data = blk->data;
23062 CORE_ADDR stack[64];
23063 int stacki;
23064 unsigned int bytes_read, unsnd;
23065 gdb_byte op;
23066
23067 if (computed != nullptr)
23068 *computed = false;
23069
23070 i = 0;
23071 stacki = 0;
23072 stack[stacki] = 0;
23073 stack[++stacki] = 0;
23074
23075 while (i < size)
23076 {
23077 op = data[i++];
23078 switch (op)
23079 {
23080 case DW_OP_lit0:
23081 case DW_OP_lit1:
23082 case DW_OP_lit2:
23083 case DW_OP_lit3:
23084 case DW_OP_lit4:
23085 case DW_OP_lit5:
23086 case DW_OP_lit6:
23087 case DW_OP_lit7:
23088 case DW_OP_lit8:
23089 case DW_OP_lit9:
23090 case DW_OP_lit10:
23091 case DW_OP_lit11:
23092 case DW_OP_lit12:
23093 case DW_OP_lit13:
23094 case DW_OP_lit14:
23095 case DW_OP_lit15:
23096 case DW_OP_lit16:
23097 case DW_OP_lit17:
23098 case DW_OP_lit18:
23099 case DW_OP_lit19:
23100 case DW_OP_lit20:
23101 case DW_OP_lit21:
23102 case DW_OP_lit22:
23103 case DW_OP_lit23:
23104 case DW_OP_lit24:
23105 case DW_OP_lit25:
23106 case DW_OP_lit26:
23107 case DW_OP_lit27:
23108 case DW_OP_lit28:
23109 case DW_OP_lit29:
23110 case DW_OP_lit30:
23111 case DW_OP_lit31:
23112 stack[++stacki] = op - DW_OP_lit0;
23113 break;
23114
23115 case DW_OP_reg0:
23116 case DW_OP_reg1:
23117 case DW_OP_reg2:
23118 case DW_OP_reg3:
23119 case DW_OP_reg4:
23120 case DW_OP_reg5:
23121 case DW_OP_reg6:
23122 case DW_OP_reg7:
23123 case DW_OP_reg8:
23124 case DW_OP_reg9:
23125 case DW_OP_reg10:
23126 case DW_OP_reg11:
23127 case DW_OP_reg12:
23128 case DW_OP_reg13:
23129 case DW_OP_reg14:
23130 case DW_OP_reg15:
23131 case DW_OP_reg16:
23132 case DW_OP_reg17:
23133 case DW_OP_reg18:
23134 case DW_OP_reg19:
23135 case DW_OP_reg20:
23136 case DW_OP_reg21:
23137 case DW_OP_reg22:
23138 case DW_OP_reg23:
23139 case DW_OP_reg24:
23140 case DW_OP_reg25:
23141 case DW_OP_reg26:
23142 case DW_OP_reg27:
23143 case DW_OP_reg28:
23144 case DW_OP_reg29:
23145 case DW_OP_reg30:
23146 case DW_OP_reg31:
23147 stack[++stacki] = op - DW_OP_reg0;
23148 if (i < size)
23149 {
23150 if (computed == nullptr)
23151 dwarf2_complex_location_expr_complaint ();
23152 else
23153 return 0;
23154 }
23155 break;
23156
23157 case DW_OP_regx:
23158 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23159 i += bytes_read;
23160 stack[++stacki] = unsnd;
23161 if (i < size)
23162 {
23163 if (computed == nullptr)
23164 dwarf2_complex_location_expr_complaint ();
23165 else
23166 return 0;
23167 }
23168 break;
23169
23170 case DW_OP_addr:
23171 stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
23172 &data[i],
23173 &bytes_read);
23174 i += bytes_read;
23175 break;
23176
23177 case DW_OP_const1u:
23178 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
23179 i += 1;
23180 break;
23181
23182 case DW_OP_const1s:
23183 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
23184 i += 1;
23185 break;
23186
23187 case DW_OP_const2u:
23188 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
23189 i += 2;
23190 break;
23191
23192 case DW_OP_const2s:
23193 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
23194 i += 2;
23195 break;
23196
23197 case DW_OP_const4u:
23198 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
23199 i += 4;
23200 break;
23201
23202 case DW_OP_const4s:
23203 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
23204 i += 4;
23205 break;
23206
23207 case DW_OP_const8u:
23208 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
23209 i += 8;
23210 break;
23211
23212 case DW_OP_constu:
23213 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23214 &bytes_read);
23215 i += bytes_read;
23216 break;
23217
23218 case DW_OP_consts:
23219 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23220 i += bytes_read;
23221 break;
23222
23223 case DW_OP_dup:
23224 stack[stacki + 1] = stack[stacki];
23225 stacki++;
23226 break;
23227
23228 case DW_OP_plus:
23229 stack[stacki - 1] += stack[stacki];
23230 stacki--;
23231 break;
23232
23233 case DW_OP_plus_uconst:
23234 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23235 &bytes_read);
23236 i += bytes_read;
23237 break;
23238
23239 case DW_OP_minus:
23240 stack[stacki - 1] -= stack[stacki];
23241 stacki--;
23242 break;
23243
23244 case DW_OP_deref:
23245 /* If we're not the last op, then we definitely can't encode
23246 this using GDB's address_class enum. This is valid for partial
23247 global symbols, although the variable's address will be bogus
23248 in the psymtab. */
23249 if (i < size)
23250 {
23251 if (computed == nullptr)
23252 dwarf2_complex_location_expr_complaint ();
23253 else
23254 return 0;
23255 }
23256 break;
23257
23258 case DW_OP_GNU_push_tls_address:
23259 case DW_OP_form_tls_address:
23260 /* The top of the stack has the offset from the beginning
23261 of the thread control block at which the variable is located. */
23262 /* Nothing should follow this operator, so the top of stack would
23263 be returned. */
23264 /* This is valid for partial global symbols, but the variable's
23265 address will be bogus in the psymtab. Make it always at least
23266 non-zero to not look as a variable garbage collected by linker
23267 which have DW_OP_addr 0. */
23268 if (i < size)
23269 {
23270 if (computed == nullptr)
23271 dwarf2_complex_location_expr_complaint ();
23272 else
23273 return 0;
23274 }
23275 stack[stacki]++;
23276 break;
23277
23278 case DW_OP_GNU_uninit:
23279 if (computed != nullptr)
23280 return 0;
23281 break;
23282
23283 case DW_OP_addrx:
23284 case DW_OP_GNU_addr_index:
23285 case DW_OP_GNU_const_index:
23286 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23287 &bytes_read);
23288 i += bytes_read;
23289 break;
23290
23291 default:
23292 if (computed == nullptr)
23293 {
23294 const char *name = get_DW_OP_name (op);
23295
23296 if (name)
23297 complaint (_("unsupported stack op: '%s'"),
23298 name);
23299 else
23300 complaint (_("unsupported stack op: '%02x'"),
23301 op);
23302 }
23303
23304 return (stack[stacki]);
23305 }
23306
23307 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23308 outside of the allocated space. Also enforce minimum>0. */
23309 if (stacki >= ARRAY_SIZE (stack) - 1)
23310 {
23311 if (computed == nullptr)
23312 complaint (_("location description stack overflow"));
23313 return 0;
23314 }
23315
23316 if (stacki <= 0)
23317 {
23318 if (computed == nullptr)
23319 complaint (_("location description stack underflow"));
23320 return 0;
23321 }
23322 }
23323
23324 if (computed != nullptr)
23325 *computed = true;
23326 return (stack[stacki]);
23327 }
23328
23329 /* memory allocation interface */
23330
23331 static struct dwarf_block *
23332 dwarf_alloc_block (struct dwarf2_cu *cu)
23333 {
23334 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23335 }
23336
23337 static struct die_info *
23338 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23339 {
23340 struct die_info *die;
23341 size_t size = sizeof (struct die_info);
23342
23343 if (num_attrs > 1)
23344 size += (num_attrs - 1) * sizeof (struct attribute);
23345
23346 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23347 memset (die, 0, sizeof (struct die_info));
23348 return (die);
23349 }
23350
23351 \f
23352
23353 /* Macro support. */
23354
23355 /* An overload of dwarf_decode_macros that finds the correct section
23356 and ensures it is read in before calling the other overload. */
23357
23358 static void
23359 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23360 int section_is_gnu)
23361 {
23362 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23363 struct objfile *objfile = per_objfile->objfile;
23364 const struct line_header *lh = cu->line_header;
23365 unsigned int offset_size = cu->header.offset_size;
23366 struct dwarf2_section_info *section;
23367 const char *section_name;
23368
23369 if (cu->dwo_unit != nullptr)
23370 {
23371 if (section_is_gnu)
23372 {
23373 section = &cu->dwo_unit->dwo_file->sections.macro;
23374 section_name = ".debug_macro.dwo";
23375 }
23376 else
23377 {
23378 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23379 section_name = ".debug_macinfo.dwo";
23380 }
23381 }
23382 else
23383 {
23384 if (section_is_gnu)
23385 {
23386 section = &per_objfile->per_bfd->macro;
23387 section_name = ".debug_macro";
23388 }
23389 else
23390 {
23391 section = &per_objfile->per_bfd->macinfo;
23392 section_name = ".debug_macinfo";
23393 }
23394 }
23395
23396 section->read (objfile);
23397 if (section->buffer == nullptr)
23398 {
23399 complaint (_("missing %s section"), section_name);
23400 return;
23401 }
23402
23403 buildsym_compunit *builder = cu->get_builder ();
23404
23405 struct dwarf2_section_info *str_offsets_section;
23406 struct dwarf2_section_info *str_section;
23407 gdb::optional<ULONGEST> str_offsets_base;
23408
23409 if (cu->dwo_unit != nullptr)
23410 {
23411 str_offsets_section = &cu->dwo_unit->dwo_file
23412 ->sections.str_offsets;
23413 str_section = &cu->dwo_unit->dwo_file->sections.str;
23414 str_offsets_base = cu->header.addr_size;
23415 }
23416 else
23417 {
23418 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23419 str_section = &per_objfile->per_bfd->str;
23420 str_offsets_base = cu->str_offsets_base;
23421 }
23422
23423 dwarf_decode_macros (per_objfile, builder, section, lh,
23424 offset_size, offset, str_section, str_offsets_section,
23425 str_offsets_base, section_is_gnu, cu);
23426 }
23427
23428 /* Return the .debug_loc section to use for CU.
23429 For DWO files use .debug_loc.dwo. */
23430
23431 static struct dwarf2_section_info *
23432 cu_debug_loc_section (struct dwarf2_cu *cu)
23433 {
23434 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23435
23436 if (cu->dwo_unit)
23437 {
23438 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23439
23440 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23441 }
23442 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23443 : &per_objfile->per_bfd->loc);
23444 }
23445
23446 /* Return the .debug_rnglists section to use for CU. */
23447 static struct dwarf2_section_info *
23448 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23449 {
23450 if (cu->header.version < 5)
23451 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23452 cu->header.version);
23453 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23454
23455 /* Make sure we read the .debug_rnglists section from the file that
23456 contains the DW_AT_ranges attribute we are reading. Normally that
23457 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23458 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23459 program. */
23460 if (cu->dwo_unit != nullptr
23461 && tag != DW_TAG_compile_unit
23462 && tag != DW_TAG_skeleton_unit)
23463 {
23464 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23465
23466 if (sections->rnglists.size > 0)
23467 return &sections->rnglists;
23468 else
23469 error (_(".debug_rnglists section is missing from .dwo file."));
23470 }
23471 return &dwarf2_per_objfile->per_bfd->rnglists;
23472 }
23473
23474 /* A helper function that fills in a dwarf2_loclist_baton. */
23475
23476 static void
23477 fill_in_loclist_baton (struct dwarf2_cu *cu,
23478 struct dwarf2_loclist_baton *baton,
23479 const struct attribute *attr)
23480 {
23481 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23482 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23483
23484 section->read (per_objfile->objfile);
23485
23486 baton->per_objfile = per_objfile;
23487 baton->per_cu = cu->per_cu;
23488 gdb_assert (baton->per_cu);
23489 /* We don't know how long the location list is, but make sure we
23490 don't run off the edge of the section. */
23491 baton->size = section->size - attr->as_unsigned ();
23492 baton->data = section->buffer + attr->as_unsigned ();
23493 if (cu->base_address.has_value ())
23494 baton->base_address = *cu->base_address;
23495 else
23496 baton->base_address = 0;
23497 baton->from_dwo = cu->dwo_unit != NULL;
23498 }
23499
23500 static void
23501 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23502 struct dwarf2_cu *cu, int is_block)
23503 {
23504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23505 struct objfile *objfile = per_objfile->objfile;
23506 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23507
23508 if (attr->form_is_section_offset ()
23509 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23510 the section. If so, fall through to the complaint in the
23511 other branch. */
23512 && attr->as_unsigned () < section->get_size (objfile))
23513 {
23514 struct dwarf2_loclist_baton *baton;
23515
23516 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23517
23518 fill_in_loclist_baton (cu, baton, attr);
23519
23520 if (!cu->base_address.has_value ())
23521 complaint (_("Location list used without "
23522 "specifying the CU base address."));
23523
23524 sym->set_aclass_index ((is_block
23525 ? dwarf2_loclist_block_index
23526 : dwarf2_loclist_index));
23527 SYMBOL_LOCATION_BATON (sym) = baton;
23528 }
23529 else
23530 {
23531 struct dwarf2_locexpr_baton *baton;
23532
23533 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23534 baton->per_objfile = per_objfile;
23535 baton->per_cu = cu->per_cu;
23536 gdb_assert (baton->per_cu);
23537
23538 if (attr->form_is_block ())
23539 {
23540 /* Note that we're just copying the block's data pointer
23541 here, not the actual data. We're still pointing into the
23542 info_buffer for SYM's objfile; right now we never release
23543 that buffer, but when we do clean up properly this may
23544 need to change. */
23545 struct dwarf_block *block = attr->as_block ();
23546 baton->size = block->size;
23547 baton->data = block->data;
23548 }
23549 else
23550 {
23551 dwarf2_invalid_attrib_class_complaint ("location description",
23552 sym->natural_name ());
23553 baton->size = 0;
23554 }
23555
23556 sym->set_aclass_index ((is_block
23557 ? dwarf2_locexpr_block_index
23558 : dwarf2_locexpr_index));
23559 SYMBOL_LOCATION_BATON (sym) = baton;
23560 }
23561 }
23562
23563 /* See read.h. */
23564
23565 const comp_unit_head *
23566 dwarf2_per_cu_data::get_header () const
23567 {
23568 if (!m_header_read_in)
23569 {
23570 const gdb_byte *info_ptr
23571 = this->section->buffer + to_underlying (this->sect_off);
23572
23573 read_comp_unit_head (&m_header, info_ptr, this->section,
23574 rcuh_kind::COMPILE);
23575
23576 m_header_read_in = true;
23577 }
23578
23579 return &m_header;
23580 }
23581
23582 /* See read.h. */
23583
23584 int
23585 dwarf2_per_cu_data::addr_size () const
23586 {
23587 return this->get_header ()->addr_size;
23588 }
23589
23590 /* See read.h. */
23591
23592 int
23593 dwarf2_per_cu_data::offset_size () const
23594 {
23595 return this->get_header ()->offset_size;
23596 }
23597
23598 /* See read.h. */
23599
23600 int
23601 dwarf2_per_cu_data::ref_addr_size () const
23602 {
23603 const comp_unit_head *header = this->get_header ();
23604
23605 if (header->version == 2)
23606 return header->addr_size;
23607 else
23608 return header->offset_size;
23609 }
23610
23611 /* A helper function for dwarf2_find_containing_comp_unit that returns
23612 the index of the result, and that searches a vector. It will
23613 return a result even if the offset in question does not actually
23614 occur in any CU. This is separate so that it can be unit
23615 tested. */
23616
23617 static int
23618 dwarf2_find_containing_comp_unit
23619 (sect_offset sect_off,
23620 unsigned int offset_in_dwz,
23621 const std::vector<dwarf2_per_cu_data_up> &all_units)
23622 {
23623 int low, high;
23624
23625 low = 0;
23626 high = all_units.size () - 1;
23627 while (high > low)
23628 {
23629 struct dwarf2_per_cu_data *mid_cu;
23630 int mid = low + (high - low) / 2;
23631
23632 mid_cu = all_units[mid].get ();
23633 if (mid_cu->is_dwz > offset_in_dwz
23634 || (mid_cu->is_dwz == offset_in_dwz
23635 && mid_cu->sect_off + mid_cu->length () > sect_off))
23636 high = mid;
23637 else
23638 low = mid + 1;
23639 }
23640 gdb_assert (low == high);
23641 return low;
23642 }
23643
23644 /* Locate the .debug_info compilation unit from CU's objfile which contains
23645 the DIE at OFFSET. Raises an error on failure. */
23646
23647 static struct dwarf2_per_cu_data *
23648 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23649 unsigned int offset_in_dwz,
23650 dwarf2_per_bfd *per_bfd)
23651 {
23652 int low = dwarf2_find_containing_comp_unit
23653 (sect_off, offset_in_dwz, per_bfd->all_units);
23654 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
23655
23656 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23657 {
23658 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23659 error (_("Dwarf Error: could not find partial DIE containing "
23660 "offset %s [in module %s]"),
23661 sect_offset_str (sect_off),
23662 bfd_get_filename (per_bfd->obfd));
23663
23664 gdb_assert (per_bfd->all_units[low-1]->sect_off
23665 <= sect_off);
23666 return per_bfd->all_units[low - 1].get ();
23667 }
23668 else
23669 {
23670 if (low == per_bfd->all_units.size () - 1
23671 && sect_off >= this_cu->sect_off + this_cu->length ())
23672 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23673 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
23674 return this_cu;
23675 }
23676 }
23677
23678 #if GDB_SELF_TEST
23679
23680 namespace selftests {
23681 namespace find_containing_comp_unit {
23682
23683 static void
23684 run_test ()
23685 {
23686 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
23687 dwarf2_per_cu_data *one_ptr = one.get ();
23688 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
23689 dwarf2_per_cu_data *two_ptr = two.get ();
23690 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
23691 dwarf2_per_cu_data *three_ptr = three.get ();
23692 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
23693 dwarf2_per_cu_data *four_ptr = four.get ();
23694
23695 one->set_length (5);
23696 two->sect_off = sect_offset (one->length ());
23697 two->set_length (7);
23698
23699 three->set_length (5);
23700 three->is_dwz = 1;
23701 four->sect_off = sect_offset (three->length ());
23702 four->set_length (7);
23703 four->is_dwz = 1;
23704
23705 std::vector<dwarf2_per_cu_data_up> units;
23706 units.push_back (std::move (one));
23707 units.push_back (std::move (two));
23708 units.push_back (std::move (three));
23709 units.push_back (std::move (four));
23710
23711 int result;
23712
23713 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23714 SELF_CHECK (units[result].get () == one_ptr);
23715 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23716 SELF_CHECK (units[result].get () == one_ptr);
23717 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23718 SELF_CHECK (units[result].get () == two_ptr);
23719
23720 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23721 SELF_CHECK (units[result].get () == three_ptr);
23722 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23723 SELF_CHECK (units[result].get () == three_ptr);
23724 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23725 SELF_CHECK (units[result].get () == four_ptr);
23726 }
23727
23728 }
23729 }
23730
23731 #endif /* GDB_SELF_TEST */
23732
23733 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23734
23735 static void
23736 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23737 enum language pretend_language)
23738 {
23739 struct attribute *attr;
23740
23741 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23742
23743 /* Set the language we're debugging. */
23744 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23745 enum language lang;
23746 if (cu->producer != nullptr
23747 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
23748 {
23749 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23750 attribute is not standardised yet. As a workaround for the
23751 language detection we fall back to the DW_AT_producer
23752 string. */
23753 lang = language_opencl;
23754 }
23755 else if (cu->producer != nullptr
23756 && strstr (cu->producer, "GNU Go ") != NULL)
23757 {
23758 /* Similar hack for Go. */
23759 lang = language_go;
23760 }
23761 else if (attr != nullptr)
23762 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
23763 else
23764 lang = pretend_language;
23765
23766 cu->language_defn = language_def (lang);
23767
23768 switch (comp_unit_die->tag)
23769 {
23770 case DW_TAG_compile_unit:
23771 cu->per_cu->set_unit_type (DW_UT_compile);
23772 break;
23773 case DW_TAG_partial_unit:
23774 cu->per_cu->set_unit_type (DW_UT_partial);
23775 break;
23776 case DW_TAG_type_unit:
23777 cu->per_cu->set_unit_type (DW_UT_type);
23778 break;
23779 default:
23780 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
23781 dwarf_tag_name (comp_unit_die->tag),
23782 sect_offset_str (cu->per_cu->sect_off));
23783 }
23784
23785 cu->per_cu->set_lang (lang);
23786 }
23787
23788 /* See read.h. */
23789
23790 dwarf2_cu *
23791 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23792 {
23793 auto it = m_dwarf2_cus.find (per_cu);
23794 if (it == m_dwarf2_cus.end ())
23795 return nullptr;
23796
23797 return it->second.get ();
23798 }
23799
23800 /* See read.h. */
23801
23802 void
23803 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
23804 std::unique_ptr<dwarf2_cu> cu)
23805 {
23806 gdb_assert (this->get_cu (per_cu) == nullptr);
23807
23808 m_dwarf2_cus[per_cu] = std::move (cu);
23809 }
23810
23811 /* See read.h. */
23812
23813 void
23814 dwarf2_per_objfile::age_comp_units ()
23815 {
23816 dwarf_read_debug_printf_v ("running");
23817
23818 /* This is not expected to be called in the middle of CU expansion. There is
23819 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23820 loaded in memory. Calling age_comp_units while the queue is in use could
23821 make us free the DIEs for a CU that is in the queue and therefore break
23822 that invariant. */
23823 gdb_assert (!queue.has_value ());
23824
23825 /* Start by clearing all marks. */
23826 for (const auto &pair : m_dwarf2_cus)
23827 pair.second->clear_mark ();
23828
23829 /* Traverse all CUs, mark them and their dependencies if used recently
23830 enough. */
23831 for (const auto &pair : m_dwarf2_cus)
23832 {
23833 dwarf2_cu *cu = pair.second.get ();
23834
23835 cu->last_used++;
23836 if (cu->last_used <= dwarf_max_cache_age)
23837 cu->mark ();
23838 }
23839
23840 /* Delete all CUs still not marked. */
23841 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23842 {
23843 dwarf2_cu *cu = it->second.get ();
23844
23845 if (!cu->is_marked ())
23846 {
23847 dwarf_read_debug_printf_v ("deleting old CU %s",
23848 sect_offset_str (cu->per_cu->sect_off));
23849 it = m_dwarf2_cus.erase (it);
23850 }
23851 else
23852 it++;
23853 }
23854 }
23855
23856 /* See read.h. */
23857
23858 void
23859 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23860 {
23861 auto it = m_dwarf2_cus.find (per_cu);
23862 if (it == m_dwarf2_cus.end ())
23863 return;
23864
23865 m_dwarf2_cus.erase (it);
23866 }
23867
23868 dwarf2_per_objfile::~dwarf2_per_objfile ()
23869 {
23870 remove_all_cus ();
23871 }
23872
23873 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23874 We store these in a hash table separate from the DIEs, and preserve them
23875 when the DIEs are flushed out of cache.
23876
23877 The CU "per_cu" pointer is needed because offset alone is not enough to
23878 uniquely identify the type. A file may have multiple .debug_types sections,
23879 or the type may come from a DWO file. Furthermore, while it's more logical
23880 to use per_cu->section+offset, with Fission the section with the data is in
23881 the DWO file but we don't know that section at the point we need it.
23882 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23883 because we can enter the lookup routine, get_die_type_at_offset, from
23884 outside this file, and thus won't necessarily have PER_CU->cu.
23885 Fortunately, PER_CU is stable for the life of the objfile. */
23886
23887 struct dwarf2_per_cu_offset_and_type
23888 {
23889 const struct dwarf2_per_cu_data *per_cu;
23890 sect_offset sect_off;
23891 struct type *type;
23892 };
23893
23894 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23895
23896 static hashval_t
23897 per_cu_offset_and_type_hash (const void *item)
23898 {
23899 const struct dwarf2_per_cu_offset_and_type *ofs
23900 = (const struct dwarf2_per_cu_offset_and_type *) item;
23901
23902 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23903 }
23904
23905 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23906
23907 static int
23908 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23909 {
23910 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23911 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23912 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23913 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23914
23915 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23916 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23917 }
23918
23919 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23920 table if necessary. For convenience, return TYPE.
23921
23922 The DIEs reading must have careful ordering to:
23923 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23924 reading current DIE.
23925 * Not trying to dereference contents of still incompletely read in types
23926 while reading in other DIEs.
23927 * Enable referencing still incompletely read in types just by a pointer to
23928 the type without accessing its fields.
23929
23930 Therefore caller should follow these rules:
23931 * Try to fetch any prerequisite types we may need to build this DIE type
23932 before building the type and calling set_die_type.
23933 * After building type call set_die_type for current DIE as soon as
23934 possible before fetching more types to complete the current type.
23935 * Make the type as complete as possible before fetching more types. */
23936
23937 static struct type *
23938 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
23939 bool skip_data_location)
23940 {
23941 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23942 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23943 struct objfile *objfile = per_objfile->objfile;
23944 struct attribute *attr;
23945 struct dynamic_prop prop;
23946
23947 /* For Ada types, make sure that the gnat-specific data is always
23948 initialized (if not already set). There are a few types where
23949 we should not be doing so, because the type-specific area is
23950 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23951 where the type-specific area is used to store the floatformat).
23952 But this is not a problem, because the gnat-specific information
23953 is actually not needed for these types. */
23954 if (need_gnat_info (cu)
23955 && type->code () != TYPE_CODE_FUNC
23956 && type->code () != TYPE_CODE_FLT
23957 && type->code () != TYPE_CODE_METHODPTR
23958 && type->code () != TYPE_CODE_MEMBERPTR
23959 && type->code () != TYPE_CODE_METHOD
23960 && type->code () != TYPE_CODE_FIXED_POINT
23961 && !HAVE_GNAT_AUX_INFO (type))
23962 INIT_GNAT_SPECIFIC (type);
23963
23964 /* Read DW_AT_allocated and set in type. */
23965 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23966 if (attr != NULL)
23967 {
23968 struct type *prop_type = cu->addr_sized_int_type (false);
23969 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23970 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23971 }
23972
23973 /* Read DW_AT_associated and set in type. */
23974 attr = dwarf2_attr (die, DW_AT_associated, cu);
23975 if (attr != NULL)
23976 {
23977 struct type *prop_type = cu->addr_sized_int_type (false);
23978 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23979 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23980 }
23981
23982 /* Read DW_AT_rank and set in type. */
23983 attr = dwarf2_attr (die, DW_AT_rank, cu);
23984 if (attr != NULL)
23985 {
23986 struct type *prop_type = cu->addr_sized_int_type (false);
23987 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23988 type->add_dyn_prop (DYN_PROP_RANK, prop);
23989 }
23990
23991 /* Read DW_AT_data_location and set in type. */
23992 if (!skip_data_location)
23993 {
23994 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23995 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23996 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23997 }
23998
23999 if (per_objfile->die_type_hash == NULL)
24000 per_objfile->die_type_hash
24001 = htab_up (htab_create_alloc (127,
24002 per_cu_offset_and_type_hash,
24003 per_cu_offset_and_type_eq,
24004 NULL, xcalloc, xfree));
24005
24006 ofs.per_cu = cu->per_cu;
24007 ofs.sect_off = die->sect_off;
24008 ofs.type = type;
24009 slot = (struct dwarf2_per_cu_offset_and_type **)
24010 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24011 if (*slot)
24012 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24013 sect_offset_str (die->sect_off));
24014 *slot = XOBNEW (&objfile->objfile_obstack,
24015 struct dwarf2_per_cu_offset_and_type);
24016 **slot = ofs;
24017 return type;
24018 }
24019
24020 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24021 or return NULL if the die does not have a saved type. */
24022
24023 static struct type *
24024 get_die_type_at_offset (sect_offset sect_off,
24025 dwarf2_per_cu_data *per_cu,
24026 dwarf2_per_objfile *per_objfile)
24027 {
24028 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24029
24030 if (per_objfile->die_type_hash == NULL)
24031 return NULL;
24032
24033 ofs.per_cu = per_cu;
24034 ofs.sect_off = sect_off;
24035 slot = ((struct dwarf2_per_cu_offset_and_type *)
24036 htab_find (per_objfile->die_type_hash.get (), &ofs));
24037 if (slot)
24038 return slot->type;
24039 else
24040 return NULL;
24041 }
24042
24043 /* Look up the type for DIE in CU in die_type_hash,
24044 or return NULL if DIE does not have a saved type. */
24045
24046 static struct type *
24047 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24048 {
24049 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24050 }
24051
24052 struct cmd_list_element *set_dwarf_cmdlist;
24053 struct cmd_list_element *show_dwarf_cmdlist;
24054
24055 static void
24056 show_check_physname (struct ui_file *file, int from_tty,
24057 struct cmd_list_element *c, const char *value)
24058 {
24059 gdb_printf (file,
24060 _("Whether to check \"physname\" is %s.\n"),
24061 value);
24062 }
24063
24064 void _initialize_dwarf2_read ();
24065 void
24066 _initialize_dwarf2_read ()
24067 {
24068 add_setshow_prefix_cmd ("dwarf", class_maintenance,
24069 _("\
24070 Set DWARF specific variables.\n\
24071 Configure DWARF variables such as the cache size."),
24072 _("\
24073 Show DWARF specific variables.\n\
24074 Show DWARF variables such as the cache size."),
24075 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
24076 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
24077
24078 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24079 &dwarf_max_cache_age, _("\
24080 Set the upper bound on the age of cached DWARF compilation units."), _("\
24081 Show the upper bound on the age of cached DWARF compilation units."), _("\
24082 A higher limit means that cached compilation units will be stored\n\
24083 in memory longer, and more total memory will be used. Zero disables\n\
24084 caching, which can slow down startup."),
24085 NULL,
24086 show_dwarf_max_cache_age,
24087 &set_dwarf_cmdlist,
24088 &show_dwarf_cmdlist);
24089
24090 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24091 Set debugging of the DWARF reader."), _("\
24092 Show debugging of the DWARF reader."), _("\
24093 When enabled (non-zero), debugging messages are printed during DWARF\n\
24094 reading and symtab expansion. A value of 1 (one) provides basic\n\
24095 information. A value greater than 1 provides more verbose information."),
24096 NULL,
24097 NULL,
24098 &setdebuglist, &showdebuglist);
24099
24100 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24101 Set debugging of the DWARF DIE reader."), _("\
24102 Show debugging of the DWARF DIE reader."), _("\
24103 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24104 The value is the maximum depth to print."),
24105 NULL,
24106 NULL,
24107 &setdebuglist, &showdebuglist);
24108
24109 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24110 Set debugging of the dwarf line reader."), _("\
24111 Show debugging of the dwarf line reader."), _("\
24112 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24113 A value of 1 (one) provides basic information.\n\
24114 A value greater than 1 provides more verbose information."),
24115 NULL,
24116 NULL,
24117 &setdebuglist, &showdebuglist);
24118
24119 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24120 Set cross-checking of \"physname\" code against demangler."), _("\
24121 Show cross-checking of \"physname\" code against demangler."), _("\
24122 When enabled, GDB's internal \"physname\" code is checked against\n\
24123 the demangler."),
24124 NULL, show_check_physname,
24125 &setdebuglist, &showdebuglist);
24126
24127 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24128 no_class, &use_deprecated_index_sections, _("\
24129 Set whether to use deprecated gdb_index sections."), _("\
24130 Show whether to use deprecated gdb_index sections."), _("\
24131 When enabled, deprecated .gdb_index sections are used anyway.\n\
24132 Normally they are ignored either because of a missing feature or\n\
24133 performance issue.\n\
24134 Warning: This option must be enabled before gdb reads the file."),
24135 NULL,
24136 NULL,
24137 &setlist, &showlist);
24138
24139 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24140 &dwarf2_locexpr_funcs);
24141 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24142 &dwarf2_loclist_funcs);
24143
24144 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24145 &dwarf2_block_frame_base_locexpr_funcs);
24146 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24147 &dwarf2_block_frame_base_loclist_funcs);
24148
24149 #if GDB_SELF_TEST
24150 selftests::register_test ("dw2_expand_symtabs_matching",
24151 selftests::dw2_expand_symtabs_matching::run_test);
24152 selftests::register_test ("dwarf2_find_containing_comp_unit",
24153 selftests::find_containing_comp_unit::run_test);
24154 #endif
24155 }