Add translations for various sub-directories
[binutils-gdb.git] / gdb / dwarf2 / read.c
blob9e3d13c9875e75d3b945699b5244d2925f88ff96
1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2024 Free Software Foundation, Inc.
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.
12 This file is part of GDB.
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.
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.
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/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section. */
30 #include "dwarf2/read.h"
31 #include "dwarf2/abbrev.h"
32 #include "dwarf2/aranges.h"
33 #include "dwarf2/attribute.h"
34 #include "dwarf2/comp-unit-head.h"
35 #include "dwarf2/cu.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/read-debug-names.h"
44 #include "dwarf2/read-gdb-index.h"
45 #include "dwarf2/sect-names.h"
46 #include "dwarf2/stringify.h"
47 #include "dwarf2/public.h"
48 #include "bfd.h"
49 #include "elf-bfd.h"
50 #include "event-top.h"
51 #include "gdbsupport/task-group.h"
52 #include "symtab.h"
53 #include "gdbtypes.h"
54 #include "objfiles.h"
55 #include "dwarf2.h"
56 #include "demangle.h"
57 #include "gdb-demangle.h"
58 #include "filenames.h"
59 #include "language.h"
60 #include "complaints.h"
61 #include "dwarf2/expr.h"
62 #include "dwarf2/loc.h"
63 #include "cp-support.h"
64 #include "hashtab.h"
65 #include "command.h"
66 #include "cli/cli-cmds.h"
67 #include "block.h"
68 #include "addrmap.h"
69 #include "typeprint.h"
70 #include "c-lang.h"
71 #include "go-lang.h"
72 #include "valprint.h"
73 #include "gdbcore.h"
74 #include "gdb_bfd.h"
75 #include "f-lang.h"
76 #include "source.h"
77 #include "build-id.h"
78 #include "namespace.h"
79 #include "gdbsupport/function-view.h"
80 #include <optional>
81 #include "gdbsupport/underlying.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91 #include <unordered_set>
92 #include "dwarf2/abbrev-table-cache.h"
93 #include "cooked-index.h"
94 #include "gdbsupport/thread-pool.h"
95 #include "run-on-main-thread.h"
96 #include "dwarf2/parent-map.h"
97 #include "dwarf2/error.h"
98 #include <variant>
99 #include "gdbsupport/unordered_set.h"
100 #include "extract-store-integer.h"
102 /* When == 1, print basic high level tracing messages.
103 When > 1, be more verbose.
104 This is in contrast to the low level DIE reading of dwarf_die_debug. */
105 static unsigned int dwarf_read_debug = 0;
107 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
109 #define dwarf_read_debug_printf(fmt, ...) \
110 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
111 ##__VA_ARGS__)
113 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
115 #define dwarf_read_debug_printf_v(fmt, ...) \
116 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
117 ##__VA_ARGS__)
119 /* When non-zero, dump DIEs after they are read in. */
120 static unsigned int dwarf_die_debug = 0;
122 /* When non-zero, dump line number entries as they are read in. */
123 unsigned int dwarf_line_debug = 0;
125 /* When true, cross-check physname against demangler. */
126 static bool check_physname = false;
128 /* This is used to store the data that is always per objfile. */
129 static const registry<objfile>::key<dwarf2_per_objfile>
130 dwarf2_objfile_data_key;
132 /* These are used to store the dwarf2_per_bfd objects.
134 objfiles having the same BFD, which doesn't require relocations, are going to
135 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
137 Other objfiles are not going to share a dwarf2_per_bfd with any other
138 objfiles, so they'll have their own version kept in the _objfile_data_key
139 version. */
140 static const registry<bfd>::key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
141 static const registry<objfile>::key<dwarf2_per_bfd>
142 dwarf2_per_bfd_objfile_data_key;
144 /* The "aclass" indices for various kinds of computed DWARF symbols. */
146 static int dwarf2_locexpr_index;
147 static int dwarf2_loclist_index;
148 static int ada_imported_index;
149 static int dwarf2_locexpr_block_index;
150 static int dwarf2_loclist_block_index;
151 static int ada_block_index;
153 static bool producer_is_gas_lt_2_38 (struct dwarf2_cu *cu);
154 static bool producer_is_gas_ge_2_39 (struct dwarf2_cu *cu);
156 /* Size of .debug_loclists section header for 32-bit DWARF format. */
157 #define LOCLIST_HEADER_SIZE32 12
159 /* Size of .debug_loclists section header for 64-bit DWARF format. */
160 #define LOCLIST_HEADER_SIZE64 20
162 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
163 #define RNGLIST_HEADER_SIZE32 12
165 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
166 #define RNGLIST_HEADER_SIZE64 20
168 /* See dwarf2/read.h. */
170 dwarf2_per_objfile *
171 get_dwarf2_per_objfile (struct objfile *objfile)
173 return dwarf2_objfile_data_key.get (objfile);
176 /* Default names of the debugging sections. */
178 /* Note that if the debugging section has been compressed, it might
179 have a name like .zdebug_info. */
181 const struct dwarf2_debug_sections dwarf2_elf_names =
183 { ".debug_info", ".zdebug_info" },
184 { ".debug_abbrev", ".zdebug_abbrev" },
185 { ".debug_line", ".zdebug_line" },
186 { ".debug_loc", ".zdebug_loc" },
187 { ".debug_loclists", ".zdebug_loclists" },
188 { ".debug_macinfo", ".zdebug_macinfo" },
189 { ".debug_macro", ".zdebug_macro" },
190 { ".debug_str", ".zdebug_str" },
191 { ".debug_str_offsets", ".zdebug_str_offsets" },
192 { ".debug_line_str", ".zdebug_line_str" },
193 { ".debug_ranges", ".zdebug_ranges" },
194 { ".debug_rnglists", ".zdebug_rnglists" },
195 { ".debug_types", ".zdebug_types" },
196 { ".debug_addr", ".zdebug_addr" },
197 { ".debug_frame", ".zdebug_frame" },
198 { ".eh_frame", NULL },
199 { ".gdb_index", ".zgdb_index" },
200 { ".debug_names", ".zdebug_names" },
201 { ".debug_aranges", ".zdebug_aranges" },
205 /* List of DWO/DWP sections. */
207 static const struct dwop_section_names
209 struct dwarf2_section_names abbrev_dwo;
210 struct dwarf2_section_names info_dwo;
211 struct dwarf2_section_names line_dwo;
212 struct dwarf2_section_names loc_dwo;
213 struct dwarf2_section_names loclists_dwo;
214 struct dwarf2_section_names macinfo_dwo;
215 struct dwarf2_section_names macro_dwo;
216 struct dwarf2_section_names rnglists_dwo;
217 struct dwarf2_section_names str_dwo;
218 struct dwarf2_section_names str_offsets_dwo;
219 struct dwarf2_section_names types_dwo;
220 struct dwarf2_section_names cu_index;
221 struct dwarf2_section_names tu_index;
223 dwop_section_names =
225 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
226 { ".debug_info.dwo", ".zdebug_info.dwo" },
227 { ".debug_line.dwo", ".zdebug_line.dwo" },
228 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
229 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
230 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
231 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
232 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
233 { ".debug_str.dwo", ".zdebug_str.dwo" },
234 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
235 { ".debug_types.dwo", ".zdebug_types.dwo" },
236 { ".debug_cu_index", ".zdebug_cu_index" },
237 { ".debug_tu_index", ".zdebug_tu_index" },
240 /* local data types */
242 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
243 begin with a header, which contains the following information. */
244 struct loclists_rnglists_header
246 /* A 4-byte or 12-byte length containing the length of the
247 set of entries for this compilation unit, not including the
248 length field itself. */
249 unsigned int length;
251 /* A 2-byte version identifier. */
252 short version;
254 /* A 1-byte unsigned integer containing the size in bytes of an address on
255 the target system. */
256 unsigned char addr_size;
258 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
259 on the target system. */
260 unsigned char segment_collector_size;
262 /* A 4-byte count of the number of offsets that follow the header. */
263 unsigned int offset_entry_count;
266 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
267 This includes type_unit_group and quick_file_names. */
269 struct stmt_list_hash
271 /* The DWO unit this table is from or NULL if there is none. */
272 struct dwo_unit *dwo_unit;
274 /* Offset in .debug_line or .debug_line.dwo. */
275 sect_offset line_sect_off;
278 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
279 an object of this type. This contains elements of type unit groups
280 that can be shared across objfiles. The non-shareable parts are in
281 type_unit_group_unshareable. */
283 struct type_unit_group
285 /* The data used to construct the hash key. */
286 struct stmt_list_hash hash {};
289 /* These sections are what may appear in a (real or virtual) DWO file. */
291 struct dwo_sections
293 struct dwarf2_section_info abbrev;
294 struct dwarf2_section_info line;
295 struct dwarf2_section_info loc;
296 struct dwarf2_section_info loclists;
297 struct dwarf2_section_info macinfo;
298 struct dwarf2_section_info macro;
299 struct dwarf2_section_info rnglists;
300 struct dwarf2_section_info str;
301 struct dwarf2_section_info str_offsets;
302 /* In the case of a virtual DWO file, these two are unused. */
303 struct dwarf2_section_info info;
304 std::vector<dwarf2_section_info> types;
307 /* CUs/TUs in DWP/DWO files. */
309 struct dwo_unit
311 /* Backlink to the containing struct dwo_file. */
312 struct dwo_file *dwo_file;
314 /* The "id" that distinguishes this CU/TU.
315 .debug_info calls this "dwo_id", .debug_types calls this "signature".
316 Since signatures came first, we stick with it for consistency. */
317 ULONGEST signature;
319 /* The section this CU/TU lives in, in the DWO file. */
320 struct dwarf2_section_info *section;
322 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
323 sect_offset sect_off;
324 unsigned int length;
326 /* For types, offset in the type's DIE of the type defined by this TU. */
327 cu_offset type_offset_in_tu;
330 /* include/dwarf2.h defines the DWP section codes.
331 It defines a max value but it doesn't define a min value, which we
332 use for error checking, so provide one. */
334 enum dwp_v2_section_ids
336 DW_SECT_MIN = 1
339 /* Data for one DWO file.
341 This includes virtual DWO files (a virtual DWO file is a DWO file as it
342 appears in a DWP file). DWP files don't really have DWO files per se -
343 comdat folding of types "loses" the DWO file they came from, and from
344 a high level view DWP files appear to contain a mass of random types.
345 However, to maintain consistency with the non-DWP case we pretend DWP
346 files contain virtual DWO files, and we assign each TU with one virtual
347 DWO file (generally based on the line and abbrev section offsets -
348 a heuristic that seems to work in practice). */
350 struct dwo_file
352 dwo_file () = default;
353 DISABLE_COPY_AND_ASSIGN (dwo_file);
355 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
356 For virtual DWO files the name is constructed from the section offsets
357 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
358 from related CU+TUs. */
359 std::string dwo_name;
361 /* The DW_AT_comp_dir attribute. */
362 const char *comp_dir = nullptr;
364 /* The bfd, when the file is open. Otherwise this is NULL.
365 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
366 gdb_bfd_ref_ptr dbfd;
368 /* The sections that make up this DWO file.
369 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
370 sections (for lack of a better name). */
371 struct dwo_sections sections {};
373 /* The CUs in the file.
374 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
375 an extension to handle LLVM's Link Time Optimization output (where
376 multiple source files may be compiled into a single object/dwo pair). */
377 htab_up cus;
379 /* Table of TUs in the file.
380 Each element is a struct dwo_unit. */
381 htab_up tus;
384 /* These sections are what may appear in a DWP file. */
386 struct dwp_sections
388 /* These are used by all DWP versions (1, 2 and 5). */
389 struct dwarf2_section_info str;
390 struct dwarf2_section_info cu_index;
391 struct dwarf2_section_info tu_index;
393 /* These are only used by DWP version 2 and version 5 files.
394 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
395 sections are referenced by section number, and are not recorded here.
396 In DWP version 2 or 5 there is at most one copy of all these sections,
397 each section being (effectively) comprised of the concatenation of all of
398 the individual sections that exist in the version 1 format.
399 To keep the code simple we treat each of these concatenated pieces as a
400 section itself (a virtual section?). */
401 struct dwarf2_section_info abbrev;
402 struct dwarf2_section_info info;
403 struct dwarf2_section_info line;
404 struct dwarf2_section_info loc;
405 struct dwarf2_section_info loclists;
406 struct dwarf2_section_info macinfo;
407 struct dwarf2_section_info macro;
408 struct dwarf2_section_info rnglists;
409 struct dwarf2_section_info str_offsets;
410 struct dwarf2_section_info types;
413 /* These sections are what may appear in a virtual DWO file in DWP version 1.
414 A virtual DWO file is a DWO file as it appears in a DWP file. */
416 struct virtual_v1_dwo_sections
418 struct dwarf2_section_info abbrev;
419 struct dwarf2_section_info line;
420 struct dwarf2_section_info loc;
421 struct dwarf2_section_info macinfo;
422 struct dwarf2_section_info macro;
423 struct dwarf2_section_info str_offsets;
424 /* Each DWP hash table entry records one CU or one TU.
425 That is recorded here, and copied to dwo_unit.section. */
426 struct dwarf2_section_info info_or_types;
429 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
430 In version 2, the sections of the DWO files are concatenated together
431 and stored in one section of that name. Thus each ELF section contains
432 several "virtual" sections. */
434 struct virtual_v2_or_v5_dwo_sections
436 bfd_size_type abbrev_offset;
437 bfd_size_type abbrev_size;
439 bfd_size_type line_offset;
440 bfd_size_type line_size;
442 bfd_size_type loc_offset;
443 bfd_size_type loc_size;
445 bfd_size_type loclists_offset;
446 bfd_size_type loclists_size;
448 bfd_size_type macinfo_offset;
449 bfd_size_type macinfo_size;
451 bfd_size_type macro_offset;
452 bfd_size_type macro_size;
454 bfd_size_type rnglists_offset;
455 bfd_size_type rnglists_size;
457 bfd_size_type str_offsets_offset;
458 bfd_size_type str_offsets_size;
460 /* Each DWP hash table entry records one CU or one TU.
461 That is recorded here, and copied to dwo_unit.section. */
462 bfd_size_type info_or_types_offset;
463 bfd_size_type info_or_types_size;
466 /* Contents of DWP hash tables. */
468 struct dwp_hash_table
470 uint32_t version, nr_columns;
471 uint32_t nr_units, nr_slots;
472 const gdb_byte *hash_table, *unit_table;
473 union
475 struct
477 const gdb_byte *indices;
478 } v1;
479 struct
481 /* This is indexed by column number and gives the id of the section
482 in that column. */
483 #define MAX_NR_V2_DWO_SECTIONS \
484 (1 /* .debug_info or .debug_types */ \
485 + 1 /* .debug_abbrev */ \
486 + 1 /* .debug_line */ \
487 + 1 /* .debug_loc */ \
488 + 1 /* .debug_str_offsets */ \
489 + 1 /* .debug_macro or .debug_macinfo */)
490 int section_ids[MAX_NR_V2_DWO_SECTIONS];
491 const gdb_byte *offsets;
492 const gdb_byte *sizes;
493 } v2;
494 struct
496 /* This is indexed by column number and gives the id of the section
497 in that column. */
498 #define MAX_NR_V5_DWO_SECTIONS \
499 (1 /* .debug_info */ \
500 + 1 /* .debug_abbrev */ \
501 + 1 /* .debug_line */ \
502 + 1 /* .debug_loclists */ \
503 + 1 /* .debug_str_offsets */ \
504 + 1 /* .debug_macro */ \
505 + 1 /* .debug_rnglists */)
506 int section_ids[MAX_NR_V5_DWO_SECTIONS];
507 const gdb_byte *offsets;
508 const gdb_byte *sizes;
509 } v5;
510 } section_pool;
513 /* Data for one DWP file. */
515 struct dwp_file
517 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
518 : name (name_),
519 dbfd (std::move (abfd))
523 /* Name of the file. */
524 const char *name;
526 /* File format version. */
527 int version = 0;
529 /* The bfd. */
530 gdb_bfd_ref_ptr dbfd;
532 /* Section info for this file. */
533 struct dwp_sections sections {};
535 /* Table of CUs in the file. */
536 const struct dwp_hash_table *cus = nullptr;
538 /* Table of TUs in the file. */
539 const struct dwp_hash_table *tus = nullptr;
541 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
542 htab_up loaded_cus;
543 htab_up loaded_tus;
545 /* Table to map ELF section numbers to their sections.
546 This is only needed for the DWP V1 file format. */
547 unsigned int num_sections = 0;
548 asection **elf_sections = nullptr;
551 /* Struct used to pass misc. parameters to read_die_and_children, et
552 al. which are used for both .debug_info and .debug_types dies.
553 All parameters here are unchanging for the life of the call. This
554 struct exists to abstract away the constant parameters of die reading. */
556 struct die_reader_specs
558 /* The bfd of die_section. */
559 bfd *abfd;
561 /* The CU of the DIE we are parsing. */
562 struct dwarf2_cu *cu;
564 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
565 struct dwo_file *dwo_file;
567 /* The section the die comes from.
568 This is either .debug_info or .debug_types, or the .dwo variants. */
569 struct dwarf2_section_info *die_section;
571 /* die_section->buffer. */
572 const gdb_byte *buffer;
574 /* The end of the buffer. */
575 const gdb_byte *buffer_end;
577 /* The abbreviation table to use when reading the DIEs. */
578 const struct abbrev_table *abbrev_table;
581 /* A subclass of die_reader_specs that holds storage and has complex
582 constructor and destructor behavior. */
584 class cutu_reader : public die_reader_specs
586 public:
588 cutu_reader (dwarf2_per_cu_data *this_cu,
589 dwarf2_per_objfile *per_objfile,
590 const struct abbrev_table *abbrev_table,
591 dwarf2_cu *existing_cu,
592 bool skip_partial,
593 const abbrev_table_cache *cache = nullptr);
595 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
596 dwarf2_per_objfile *per_objfile,
597 struct dwarf2_cu *parent_cu = nullptr,
598 struct dwo_file *dwo_file = nullptr);
600 DISABLE_COPY_AND_ASSIGN (cutu_reader);
602 cutu_reader (cutu_reader &&) = default;
604 const gdb_byte *info_ptr = nullptr;
605 struct die_info *comp_unit_die = nullptr;
606 bool dummy_p = false;
608 /* Release the new CU, putting it on the chain. This cannot be done
609 for dummy CUs. */
610 void keep ();
612 /* Release the abbrev table, transferring ownership to the
613 caller. */
614 abbrev_table_up release_abbrev_table ()
616 return std::move (m_abbrev_table_holder);
619 private:
620 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
621 dwarf2_per_objfile *per_objfile,
622 dwarf2_cu *existing_cu);
624 struct dwarf2_per_cu_data *m_this_cu;
625 std::unique_ptr<dwarf2_cu> m_new_cu;
627 /* The ordinary abbreviation table. */
628 abbrev_table_up m_abbrev_table_holder;
630 /* The DWO abbreviation table. */
631 abbrev_table_up m_dwo_abbrev_table;
634 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
635 but this would require a corresponding change in unpack_field_as_long
636 and friends. */
637 static int bits_per_byte = 8;
639 struct variant_part_builder;
641 /* When reading a variant, we track a bit more information about the
642 field, and store it in an object of this type. */
644 struct variant_field
646 int first_field = -1;
647 int last_field = -1;
649 /* A variant can contain other variant parts. */
650 std::vector<variant_part_builder> variant_parts;
652 /* If we see a DW_TAG_variant, then this will be set if this is the
653 default branch. */
654 bool default_branch = false;
655 /* If we see a DW_AT_discr_value, then this will be the discriminant
656 value. */
657 ULONGEST discriminant_value = 0;
658 /* If we see a DW_AT_discr_list, then this is a pointer to the list
659 data. */
660 struct dwarf_block *discr_list_data = nullptr;
663 /* This represents a DW_TAG_variant_part. */
665 struct variant_part_builder
667 /* The offset of the discriminant field. */
668 sect_offset discriminant_offset {};
670 /* Variants that are direct children of this variant part. */
671 std::vector<variant_field> variants;
673 /* True if we're currently reading a variant. */
674 bool processing_variant = false;
677 struct nextfield
679 /* Variant parts need to find the discriminant, which is a DIE
680 reference. We track the section offset of each field to make
681 this link. */
682 sect_offset offset;
683 struct field field {};
686 struct fnfieldlist
688 const char *name = nullptr;
689 std::vector<struct fn_field> fnfields;
692 /* The routines that read and process dies for a C struct or C++ class
693 pass lists of data member fields and lists of member function fields
694 in an instance of a field_info structure, as defined below. */
695 struct field_info
697 /* List of data member and baseclasses fields. */
698 std::vector<struct nextfield> fields;
699 std::vector<struct nextfield> baseclasses;
701 /* Member function fieldlist array, contains name of possibly overloaded
702 member function, number of overloaded member functions and a pointer
703 to the head of the member function field chain. */
704 std::vector<struct fnfieldlist> fnfieldlists;
706 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
707 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
708 std::vector<struct decl_field> typedef_field_list;
710 /* Nested types defined by this class and the number of elements in this
711 list. */
712 std::vector<struct decl_field> nested_types_list;
714 /* If non-null, this is the variant part we are currently
715 reading. */
716 variant_part_builder *current_variant_part = nullptr;
717 /* This holds all the top-level variant parts attached to the type
718 we're reading. */
719 std::vector<variant_part_builder> variant_parts;
721 /* Return the total number of fields (including baseclasses). */
722 int nfields () const
724 return fields.size () + baseclasses.size ();
728 /* Loaded secondary compilation units are kept in memory until they
729 have not been referenced for the processing of this many
730 compilation units. Set this to zero to disable caching. Cache
731 sizes of up to at least twenty will improve startup time for
732 typical inter-CU-reference binaries, at an obvious memory cost. */
733 static int dwarf_max_cache_age = 5;
734 static void
735 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
736 struct cmd_list_element *c, const char *value)
738 gdb_printf (file, _("The upper bound on the age of cached "
739 "DWARF compilation units is %s.\n"),
740 value);
743 /* When true, wait for DWARF reading to be complete. */
744 static bool dwarf_synchronous = true;
746 /* "Show" callback for "maint set dwarf synchronous". */
747 static void
748 show_dwarf_synchronous (struct ui_file *file, int from_tty,
749 struct cmd_list_element *c, const char *value)
751 gdb_printf (file, _("Whether DWARF reading is synchronous is %s.\n"),
752 value);
755 /* local function prototypes */
757 static void dwarf2_find_base_address (struct die_info *die,
758 struct dwarf2_cu *cu);
760 static void build_type_psymtabs_reader (cutu_reader *reader,
761 cooked_index_storage *storage);
763 static void var_decode_location (struct attribute *attr,
764 struct symbol *sym,
765 struct dwarf2_cu *cu);
767 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
769 static const gdb_byte *read_attribute (const struct die_reader_specs *,
770 struct attribute *,
771 const struct attr_abbrev *,
772 const gdb_byte *,
773 bool allow_reprocess = true);
775 /* Note that the default for TAG is chosen because it only matters
776 when reading the top-level DIE, and that function is careful to
777 pass the correct tag. */
778 static void read_attribute_reprocess (const struct die_reader_specs *reader,
779 struct attribute *attr,
780 dwarf_tag tag = DW_TAG_padding);
782 static unrelocated_addr read_addr_index (struct dwarf2_cu *cu,
783 unsigned int addr_index);
785 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
786 dwarf2_section_info *, sect_offset);
788 static const char *read_indirect_string
789 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
790 const struct comp_unit_head *, unsigned int *);
792 static unrelocated_addr read_addr_index_from_leb128 (struct dwarf2_cu *,
793 const gdb_byte *,
794 unsigned int *);
796 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
797 ULONGEST str_index);
799 static const char *read_stub_str_index (struct dwarf2_cu *cu,
800 ULONGEST str_index);
802 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
803 struct dwarf2_cu *);
805 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
806 struct dwarf2_cu *cu);
808 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
810 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
811 struct dwarf2_cu *cu);
813 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
815 static struct die_info *die_specification (struct die_info *die,
816 struct dwarf2_cu **);
818 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
819 struct dwarf2_cu *cu,
820 const char *comp_dir);
822 static void dwarf_decode_lines (struct line_header *,
823 struct dwarf2_cu *,
824 unrelocated_addr, int decode_mapping);
826 static void dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
827 const line_header &lh);
829 static struct symbol *new_symbol (struct die_info *, struct type *,
830 struct dwarf2_cu *, struct symbol * = NULL);
832 static void dwarf2_const_value (const struct attribute *, struct symbol *,
833 struct dwarf2_cu *);
835 static void dwarf2_const_value_attr (const struct attribute *attr,
836 struct type *type,
837 const char *name,
838 struct obstack *obstack,
839 struct dwarf2_cu *cu, LONGEST *value,
840 const gdb_byte **bytes,
841 struct dwarf2_locexpr_baton **baton);
843 static struct type *read_subrange_index_type (struct die_info *die,
844 struct dwarf2_cu *cu);
846 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
848 static int need_gnat_info (struct dwarf2_cu *);
850 static struct type *die_descriptive_type (struct die_info *,
851 struct dwarf2_cu *);
853 static void set_descriptive_type (struct type *, struct die_info *,
854 struct dwarf2_cu *);
856 static struct type *die_containing_type (struct die_info *,
857 struct dwarf2_cu *);
859 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
860 struct dwarf2_cu *);
862 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
864 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
866 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
868 static gdb::unique_xmalloc_ptr<char> typename_concat (const char *prefix,
869 const char *suffix,
870 int physname,
871 struct dwarf2_cu *cu);
873 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
875 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
877 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
879 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
881 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
883 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
885 /* Return the .debug_loclists section to use for cu. */
886 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
888 /* Return the .debug_rnglists section to use for cu. */
889 static struct dwarf2_section_info *cu_debug_rnglists_section
890 (struct dwarf2_cu *cu, dwarf_tag tag);
892 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
893 values. Keep the items ordered with increasing constraints compliance. */
894 enum pc_bounds_kind
896 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
897 PC_BOUNDS_NOT_PRESENT,
899 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
900 were present but they do not form a valid range of PC addresses. */
901 PC_BOUNDS_INVALID,
903 /* Discontiguous range was found - that is DW_AT_ranges was found. */
904 PC_BOUNDS_RANGES,
906 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
907 PC_BOUNDS_HIGH_LOW,
910 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
911 unrelocated_addr *,
912 unrelocated_addr *,
913 struct dwarf2_cu *,
914 addrmap_mutable *,
915 void *);
917 static void get_scope_pc_bounds (struct die_info *,
918 unrelocated_addr *, unrelocated_addr *,
919 struct dwarf2_cu *);
921 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
922 struct dwarf2_cu *);
924 static void dwarf2_add_field (struct field_info *, struct die_info *,
925 struct dwarf2_cu *);
927 static void dwarf2_attach_fields_to_type (struct field_info *,
928 struct type *, struct dwarf2_cu *);
930 static void dwarf2_add_member_fn (struct field_info *,
931 struct die_info *, struct type *,
932 struct dwarf2_cu *);
934 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
935 struct type *,
936 struct dwarf2_cu *);
938 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
940 static void read_common_block (struct die_info *, struct dwarf2_cu *);
942 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
944 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
946 static struct using_direct **using_directives (struct dwarf2_cu *cu);
948 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
950 static bool read_alias (struct die_info *die, struct dwarf2_cu *cu);
952 static struct type *read_module_type (struct die_info *die,
953 struct dwarf2_cu *cu);
955 static const char *namespace_name (struct die_info *die,
956 int *is_anonymous, struct dwarf2_cu *);
958 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
960 static bool decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
961 CORE_ADDR *addr);
963 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
964 struct dwarf2_cu *);
966 static struct die_info *read_die_and_siblings_1
967 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
968 struct die_info *);
970 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
971 const gdb_byte *info_ptr,
972 const gdb_byte **new_info_ptr,
973 struct die_info *parent);
975 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
976 struct die_info **, const gdb_byte *,
977 int, bool);
979 static const gdb_byte *read_toplevel_die (const struct die_reader_specs *,
980 struct die_info **,
981 const gdb_byte *,
982 gdb::array_view<attribute *> = {});
984 static void process_die (struct die_info *, struct dwarf2_cu *);
986 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
987 struct objfile *);
989 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
991 static const char *dwarf2_full_name (const char *name,
992 struct die_info *die,
993 struct dwarf2_cu *cu);
995 static const char *dwarf2_physname (const char *name, struct die_info *die,
996 struct dwarf2_cu *cu);
998 static struct die_info *dwarf2_extension (struct die_info *die,
999 struct dwarf2_cu **);
1001 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1002 const struct attribute *,
1003 struct dwarf2_cu **);
1005 static struct die_info *follow_die_ref (struct die_info *,
1006 const struct attribute *,
1007 struct dwarf2_cu **);
1009 static struct die_info *follow_die_sig (struct die_info *,
1010 const struct attribute *,
1011 struct dwarf2_cu **);
1013 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1014 struct dwarf2_cu *);
1016 static struct type *get_DW_AT_signature_type (struct die_info *,
1017 const struct attribute *,
1018 struct dwarf2_cu *);
1020 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1021 dwarf2_per_objfile *per_objfile);
1023 static void read_signatured_type (signatured_type *sig_type,
1024 dwarf2_per_objfile *per_objfile);
1026 static int attr_to_dynamic_prop (const struct attribute *attr,
1027 struct die_info *die, struct dwarf2_cu *cu,
1028 struct dynamic_prop *prop, struct type *type);
1030 /* memory allocation interface */
1032 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1034 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1036 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1037 struct dwarf2_loclist_baton *baton,
1038 const struct attribute *attr);
1040 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1041 struct symbol *sym,
1042 struct dwarf2_cu *cu,
1043 int is_block);
1045 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1046 const gdb_byte *info_ptr,
1047 const struct abbrev_info *abbrev,
1048 bool do_skip_children = true);
1050 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1051 (sect_offset sect_off, unsigned int offset_in_dwz,
1052 dwarf2_per_bfd *per_bfd);
1054 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1055 struct die_info *comp_unit_die,
1056 enum language pretend_language);
1058 static struct type *set_die_type (struct die_info *, struct type *,
1059 struct dwarf2_cu *, bool = false);
1061 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1062 dwarf2_per_objfile *per_objfile,
1063 dwarf2_cu *existing_cu,
1064 bool skip_partial,
1065 enum language pretend_language);
1067 static void process_full_comp_unit (dwarf2_cu *cu,
1068 enum language pretend_language);
1070 static void process_full_type_unit (dwarf2_cu *cu,
1071 enum language pretend_language);
1073 static struct type *get_die_type_at_offset (sect_offset,
1074 dwarf2_per_cu_data *per_cu,
1075 dwarf2_per_objfile *per_objfile);
1077 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1079 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1080 dwarf2_per_objfile *per_objfile,
1081 enum language pretend_language);
1083 static void process_queue (dwarf2_per_objfile *per_objfile);
1085 static bool is_ada_import_or_export (dwarf2_cu *cu, const char *name,
1086 const char *linkagename);
1088 /* Class, the destructor of which frees all allocated queue entries. This
1089 will only have work to do if an error was thrown while processing the
1090 dwarf. If no error was thrown then the queue entries should have all
1091 been processed, and freed, as we went along. */
1093 class dwarf2_queue_guard
1095 public:
1096 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1097 : m_per_objfile (per_objfile)
1099 gdb_assert (!m_per_objfile->queue.has_value ());
1101 m_per_objfile->queue.emplace ();
1104 /* Free any entries remaining on the queue. There should only be
1105 entries left if we hit an error while processing the dwarf. */
1106 ~dwarf2_queue_guard ()
1108 gdb_assert (m_per_objfile->queue.has_value ());
1110 m_per_objfile->queue.reset ();
1113 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1115 private:
1116 dwarf2_per_objfile *m_per_objfile;
1119 dwarf2_queue_item::~dwarf2_queue_item ()
1121 /* Anything still marked queued is likely to be in an
1122 inconsistent state, so discard it. */
1123 if (per_cu->queued)
1125 per_objfile->remove_cu (per_cu);
1126 per_cu->queued = 0;
1130 /* See dwarf2/read.h. */
1132 void
1133 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data *data)
1135 if (data->is_debug_types)
1136 delete static_cast<signatured_type *> (data);
1137 else
1138 delete data;
1141 static file_and_directory &find_file_and_directory
1142 (struct die_info *die, struct dwarf2_cu *cu);
1144 static const char *compute_include_file_name
1145 (const struct line_header *lh,
1146 const file_entry &fe,
1147 const file_and_directory &cu_info,
1148 std::string &name_holder);
1150 static htab_up allocate_dwo_unit_table ();
1152 static struct dwo_unit *lookup_dwo_unit_in_dwp
1153 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1154 const char *comp_dir, ULONGEST signature, int is_debug_types);
1156 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1158 static struct dwo_unit *lookup_dwo_comp_unit
1159 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1160 ULONGEST signature);
1162 static struct dwo_unit *lookup_dwo_type_unit
1163 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1165 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1167 /* A unique pointer to a dwo_file. */
1169 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1171 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1173 static void check_producer (struct dwarf2_cu *cu);
1175 /* Various complaints about symbol reading that don't abort the process. */
1177 static void
1178 dwarf2_debug_line_missing_file_complaint (void)
1180 complaint (_(".debug_line section has line data without a file"));
1183 static void
1184 dwarf2_debug_line_missing_end_sequence_complaint (void)
1186 complaint (_(".debug_line section has line "
1187 "program sequence without an end"));
1190 static void
1191 dwarf2_complex_location_expr_complaint (void)
1193 complaint (_("location expression too complex"));
1196 static void
1197 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1198 int arg3)
1200 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1201 arg1, arg2, arg3);
1204 static void
1205 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1207 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1208 arg1, arg2);
1211 /* See read.h. */
1213 CORE_ADDR
1214 dwarf2_per_objfile::relocate (unrelocated_addr addr)
1216 CORE_ADDR baseaddr = objfile->text_section_offset ();
1217 CORE_ADDR tem = (CORE_ADDR) addr + baseaddr;
1218 return gdbarch_adjust_dwarf2_addr (objfile->arch (), tem);
1221 /* Hash function for line_header_hash. */
1223 static hashval_t
1224 line_header_hash (const struct line_header *ofs)
1226 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1229 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1231 static hashval_t
1232 line_header_hash_voidp (const void *item)
1234 const struct line_header *ofs = (const struct line_header *) item;
1236 return line_header_hash (ofs);
1239 /* Equality function for line_header_hash. */
1241 static int
1242 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1244 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1245 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1247 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1248 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1251 /* See declaration. */
1253 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1254 bool can_copy_)
1255 : obfd (obfd),
1256 can_copy (can_copy_),
1257 captured_cwd (current_directory),
1258 captured_debug_dir (debug_file_directory)
1260 if (names == NULL)
1261 names = &dwarf2_elf_names;
1263 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1264 locate_sections (obfd, sec, *names);
1267 dwarf2_per_bfd::~dwarf2_per_bfd ()
1269 /* Data from the per-BFD may be needed when finalizing the cooked
1270 index table, so wait here while this happens. */
1271 if (index_table != nullptr)
1272 index_table->wait_completely ();
1274 for (auto &per_cu : all_units)
1275 per_cu->free_cached_file_names ();
1277 /* Everything else should be on this->obstack. */
1280 /* See read.h. */
1282 void
1283 dwarf2_per_objfile::remove_all_cus ()
1285 gdb_assert (!queue.has_value ());
1287 m_dwarf2_cus.clear ();
1290 /* A helper class that calls free_cached_comp_units on
1291 destruction. */
1293 class free_cached_comp_units
1295 public:
1297 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1298 : m_per_objfile (per_objfile)
1302 ~free_cached_comp_units ()
1304 m_per_objfile->remove_all_cus ();
1307 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1309 private:
1311 dwarf2_per_objfile *m_per_objfile;
1314 /* See read.h. */
1316 bool
1317 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1319 if (per_cu->index < this->m_symtabs.size ())
1320 return this->m_symtabs[per_cu->index] != nullptr;
1321 return false;
1324 /* See read.h. */
1326 compunit_symtab *
1327 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1329 if (per_cu->index < this->m_symtabs.size ())
1330 return this->m_symtabs[per_cu->index];
1331 return nullptr;
1334 /* See read.h. */
1336 void
1337 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1338 compunit_symtab *symtab)
1340 if (per_cu->index >= this->m_symtabs.size ())
1341 this->m_symtabs.resize (per_cu->index + 1);
1342 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1343 this->m_symtabs[per_cu->index] = symtab;
1346 /* Helper function for dwarf2_initialize_objfile that creates the
1347 per-BFD object. */
1349 static bool
1350 dwarf2_has_info (struct objfile *objfile,
1351 const struct dwarf2_debug_sections *names,
1352 bool can_copy)
1354 if (objfile->flags & OBJF_READNEVER)
1355 return false;
1357 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1358 bool just_created = false;
1360 if (per_objfile == NULL)
1362 dwarf2_per_bfd *per_bfd;
1364 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1365 BFD doesn't require relocations.
1367 We don't share with objfiles for which -readnow was requested,
1368 because it would complicate things when loading the same BFD with
1369 -readnow and then without -readnow. */
1370 if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
1371 && (objfile->flags & OBJF_READNOW) == 0)
1373 /* See if one has been created for this BFD yet. */
1374 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
1376 if (per_bfd == nullptr)
1378 /* No, create it now. */
1379 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
1380 can_copy);
1381 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
1382 just_created = true;
1385 else
1387 /* No sharing possible, create one specifically for this objfile. */
1388 per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
1389 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1390 just_created = true;
1393 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1396 /* Virtual sections are created from DWP files. It's not clear those
1397 can occur here, so perhaps the is_virtual checks here are dead code. */
1398 const bool has_info = (!per_objfile->per_bfd->infos.empty ()
1399 && !per_objfile->per_bfd->infos[0].is_virtual
1400 && per_objfile->per_bfd->infos[0].s.section != nullptr
1401 && !per_objfile->per_bfd->abbrev.is_virtual
1402 && per_objfile->per_bfd->abbrev.s.section != nullptr);
1404 if (just_created && has_info)
1406 /* Try to fetch any potential dwz file early, while still on
1407 the main thread. Also, be sure to do it just once per
1408 BFD, to avoid races. */
1411 dwarf2_read_dwz_file (per_objfile);
1413 catch (const gdb_exception_error &err)
1415 warning (_("%s"), err.what ());
1419 return has_info;
1422 /* See declaration. */
1424 void
1425 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1426 const dwarf2_debug_sections &names)
1428 flagword aflag = bfd_section_flags (sectp);
1430 if ((aflag & SEC_HAS_CONTENTS) == 0)
1433 else if (bfd_section_size_insane (abfd, sectp))
1435 bfd_size_type size = sectp->size;
1436 warning (_("Discarding section %s which has an invalid size (%s) "
1437 "[in module %s]"),
1438 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1439 bfd_get_filename (abfd));
1441 else if (names.info.matches (sectp->name))
1443 struct dwarf2_section_info info_section;
1444 memset (&info_section, 0, sizeof (info_section));
1445 info_section.s.section = sectp;
1446 info_section.size = bfd_section_size (sectp);
1447 this->infos.push_back (info_section);
1449 else if (names.abbrev.matches (sectp->name))
1451 this->abbrev.s.section = sectp;
1452 this->abbrev.size = bfd_section_size (sectp);
1454 else if (names.line.matches (sectp->name))
1456 this->line.s.section = sectp;
1457 this->line.size = bfd_section_size (sectp);
1459 else if (names.loc.matches (sectp->name))
1461 this->loc.s.section = sectp;
1462 this->loc.size = bfd_section_size (sectp);
1464 else if (names.loclists.matches (sectp->name))
1466 this->loclists.s.section = sectp;
1467 this->loclists.size = bfd_section_size (sectp);
1469 else if (names.macinfo.matches (sectp->name))
1471 this->macinfo.s.section = sectp;
1472 this->macinfo.size = bfd_section_size (sectp);
1474 else if (names.macro.matches (sectp->name))
1476 this->macro.s.section = sectp;
1477 this->macro.size = bfd_section_size (sectp);
1479 else if (names.str.matches (sectp->name))
1481 this->str.s.section = sectp;
1482 this->str.size = bfd_section_size (sectp);
1484 else if (names.str_offsets.matches (sectp->name))
1486 this->str_offsets.s.section = sectp;
1487 this->str_offsets.size = bfd_section_size (sectp);
1489 else if (names.line_str.matches (sectp->name))
1491 this->line_str.s.section = sectp;
1492 this->line_str.size = bfd_section_size (sectp);
1494 else if (names.addr.matches (sectp->name))
1496 this->addr.s.section = sectp;
1497 this->addr.size = bfd_section_size (sectp);
1499 else if (names.frame.matches (sectp->name))
1501 this->frame.s.section = sectp;
1502 this->frame.size = bfd_section_size (sectp);
1504 else if (names.eh_frame.matches (sectp->name))
1506 this->eh_frame.s.section = sectp;
1507 this->eh_frame.size = bfd_section_size (sectp);
1509 else if (names.ranges.matches (sectp->name))
1511 this->ranges.s.section = sectp;
1512 this->ranges.size = bfd_section_size (sectp);
1514 else if (names.rnglists.matches (sectp->name))
1516 this->rnglists.s.section = sectp;
1517 this->rnglists.size = bfd_section_size (sectp);
1519 else if (names.types.matches (sectp->name))
1521 struct dwarf2_section_info type_section;
1523 memset (&type_section, 0, sizeof (type_section));
1524 type_section.s.section = sectp;
1525 type_section.size = bfd_section_size (sectp);
1527 this->types.push_back (type_section);
1529 else if (names.gdb_index.matches (sectp->name))
1531 this->gdb_index.s.section = sectp;
1532 this->gdb_index.size = bfd_section_size (sectp);
1534 else if (names.debug_names.matches (sectp->name))
1536 this->debug_names.s.section = sectp;
1537 this->debug_names.size = bfd_section_size (sectp);
1539 else if (names.debug_aranges.matches (sectp->name))
1541 this->debug_aranges.s.section = sectp;
1542 this->debug_aranges.size = bfd_section_size (sectp);
1545 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1546 && bfd_section_vma (sectp) == 0)
1547 this->has_section_at_zero = true;
1550 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1551 SECTION_NAME. */
1553 void
1554 dwarf2_get_section_info (struct objfile *objfile,
1555 enum dwarf2_section_enum sect,
1556 asection **sectp, const gdb_byte **bufp,
1557 bfd_size_type *sizep)
1559 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1560 struct dwarf2_section_info *info;
1562 /* We may see an objfile without any DWARF, in which case we just
1563 return nothing. */
1564 if (per_objfile == NULL)
1566 *sectp = NULL;
1567 *bufp = NULL;
1568 *sizep = 0;
1569 return;
1571 switch (sect)
1573 case DWARF2_DEBUG_FRAME:
1574 info = &per_objfile->per_bfd->frame;
1575 break;
1576 case DWARF2_EH_FRAME:
1577 info = &per_objfile->per_bfd->eh_frame;
1578 break;
1579 default:
1580 gdb_assert_not_reached ("unexpected section");
1583 info->read (objfile);
1585 *sectp = info->get_bfd_section ();
1586 *bufp = info->buffer;
1587 *sizep = info->size;
1590 /* See dwarf2/read.h. */
1592 void
1593 dwarf2_per_bfd::map_info_sections (struct objfile *objfile)
1595 for (auto &section : infos)
1596 section.read (objfile);
1598 abbrev.read (objfile);
1599 line.read (objfile);
1600 str.read (objfile);
1601 str_offsets.read (objfile);
1602 line_str.read (objfile);
1603 ranges.read (objfile);
1604 rnglists.read (objfile);
1605 addr.read (objfile);
1606 debug_aranges.read (objfile);
1608 for (auto &section : types)
1609 section.read (objfile);
1613 /* DWARF quick_symbol_functions support. */
1615 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1616 unique line tables, so we maintain a separate table of all .debug_line
1617 derived entries to support the sharing.
1618 All the quick functions need is the list of file names. We discard the
1619 line_header when we're done and don't need to record it here. */
1620 struct quick_file_names
1622 /* The data used to construct the hash key. */
1623 struct stmt_list_hash hash;
1625 /* The number of entries in file_names, real_names. */
1626 unsigned int num_file_names;
1628 /* The CU directory, as given by DW_AT_comp_dir. May be
1629 nullptr. */
1630 const char *comp_dir;
1632 /* The file names from the line table, after being run through
1633 file_full_name. */
1634 const char **file_names;
1636 /* The file names from the line table after being run through
1637 gdb_realpath. These are computed lazily. */
1638 const char **real_names;
1641 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1642 It's handy in this case to have an empty implementation of the
1643 quick symbol functions, to avoid special cases in the rest of the
1644 code. */
1646 struct readnow_functions : public dwarf2_base_index_functions
1648 void dump (struct objfile *objfile) override
1652 bool expand_symtabs_matching
1653 (struct objfile *objfile,
1654 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1655 const lookup_name_info *lookup_name,
1656 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1657 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1658 block_search_flags search_flags,
1659 domain_search_flags domain,
1660 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
1661 override
1663 return true;
1667 /* Utility hash function for a stmt_list_hash. */
1669 static hashval_t
1670 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
1672 hashval_t v = 0;
1674 if (stmt_list_hash->dwo_unit != NULL)
1675 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
1676 v += to_underlying (stmt_list_hash->line_sect_off);
1677 return v;
1680 /* Utility equality function for a stmt_list_hash. */
1682 static int
1683 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
1684 const struct stmt_list_hash *rhs)
1686 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
1687 return 0;
1688 if (lhs->dwo_unit != NULL
1689 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
1690 return 0;
1692 return lhs->line_sect_off == rhs->line_sect_off;
1695 /* Hash function for a quick_file_names. */
1697 static hashval_t
1698 hash_file_name_entry (const void *e)
1700 const struct quick_file_names *file_data
1701 = (const struct quick_file_names *) e;
1703 return hash_stmt_list_entry (&file_data->hash);
1706 /* Equality function for a quick_file_names. */
1708 static int
1709 eq_file_name_entry (const void *a, const void *b)
1711 const struct quick_file_names *ea = (const struct quick_file_names *) a;
1712 const struct quick_file_names *eb = (const struct quick_file_names *) b;
1714 return eq_stmt_list_entry (&ea->hash, &eb->hash);
1717 /* See read.h. */
1719 htab_up
1720 create_quick_file_names_table (unsigned int nr_initial_entries)
1722 return htab_up (htab_create_alloc (nr_initial_entries,
1723 hash_file_name_entry, eq_file_name_entry,
1724 nullptr, xcalloc, xfree));
1727 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1728 function is unrelated to symtabs, symtab would have to be created afterwards.
1729 You should call age_cached_comp_units after processing the CU. */
1731 static dwarf2_cu *
1732 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1733 bool skip_partial)
1735 if (per_cu->is_debug_types)
1736 load_full_type_unit (per_cu, per_objfile);
1737 else
1738 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
1739 skip_partial, language_minimal);
1741 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1742 if (cu == nullptr)
1743 return nullptr; /* Dummy CU. */
1745 dwarf2_find_base_address (cu->dies, cu);
1747 return cu;
1750 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1752 static void
1753 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1754 dwarf2_per_objfile *per_objfile, bool skip_partial)
1757 /* The destructor of dwarf2_queue_guard frees any entries left on
1758 the queue. After this point we're guaranteed to leave this function
1759 with the dwarf queue empty. */
1760 dwarf2_queue_guard q_guard (per_objfile);
1762 if (!per_objfile->symtab_set_p (per_cu))
1764 queue_comp_unit (per_cu, per_objfile, language_minimal);
1765 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
1767 /* If we just loaded a CU from a DWO, and we're working with an index
1768 that may badly handle TUs, load all the TUs in that DWO as well.
1769 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1770 if (!per_cu->is_debug_types
1771 && cu != NULL
1772 && cu->dwo_unit != NULL
1773 && per_objfile->per_bfd->index_table != NULL
1774 && !per_objfile->per_bfd->index_table->version_check ()
1775 /* DWP files aren't supported yet. */
1776 && get_dwp_file (per_objfile) == NULL)
1777 queue_and_load_all_dwo_tus (cu);
1780 process_queue (per_objfile);
1783 /* Age the cache, releasing compilation units that have not
1784 been used recently. */
1785 per_objfile->age_comp_units ();
1788 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1789 the per-objfile for which this symtab is instantiated.
1791 Returns the resulting symbol table. */
1793 static struct compunit_symtab *
1794 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
1795 dwarf2_per_objfile *per_objfile,
1796 bool skip_partial)
1798 if (!per_objfile->symtab_set_p (per_cu))
1800 free_cached_comp_units freer (per_objfile);
1801 scoped_restore decrementer = increment_reading_symtab ();
1802 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
1803 process_cu_includes (per_objfile);
1806 return per_objfile->get_symtab (per_cu);
1809 /* See read.h. */
1811 dwarf2_per_cu_data_up
1812 dwarf2_per_bfd::allocate_per_cu ()
1814 dwarf2_per_cu_data_up result (new dwarf2_per_cu_data);
1815 result->per_bfd = this;
1816 result->index = all_units.size ();
1817 return result;
1820 /* See read.h. */
1822 signatured_type_up
1823 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature)
1825 signatured_type_up result (new signatured_type (signature));
1826 result->per_bfd = this;
1827 result->index = all_units.size ();
1828 result->is_debug_types = true;
1829 tu_stats.nr_tus++;
1830 return result;
1833 /* See read.h. */
1835 dwarf2_per_cu_data_up
1836 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
1837 struct dwarf2_section_info *section,
1838 int is_dwz,
1839 sect_offset sect_off, ULONGEST length)
1841 dwarf2_per_cu_data_up the_cu = per_bfd->allocate_per_cu ();
1842 the_cu->sect_off = sect_off;
1843 the_cu->set_length (length);
1844 the_cu->section = section;
1845 the_cu->is_dwz = is_dwz;
1846 return the_cu;
1849 /* die_reader_func for dw2_get_file_names. */
1851 static void
1852 dw2_get_file_names_reader (const struct die_reader_specs *reader,
1853 struct die_info *comp_unit_die)
1855 struct dwarf2_cu *cu = reader->cu;
1856 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
1857 dwarf2_per_objfile *per_objfile = cu->per_objfile;
1858 struct dwarf2_per_cu_data *lh_cu;
1859 struct attribute *attr;
1860 void **slot;
1861 struct quick_file_names *qfn;
1863 gdb_assert (! this_cu->is_debug_types);
1865 this_cu->files_read = true;
1866 /* Our callers never want to match partial units -- instead they
1867 will match the enclosing full CU. */
1868 if (comp_unit_die->tag == DW_TAG_partial_unit)
1869 return;
1871 lh_cu = this_cu;
1872 slot = NULL;
1874 line_header_up lh;
1875 sect_offset line_offset {};
1877 file_and_directory &fnd = find_file_and_directory (comp_unit_die, cu);
1879 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
1880 if (attr != nullptr && attr->form_is_unsigned ())
1882 struct quick_file_names find_entry;
1884 line_offset = (sect_offset) attr->as_unsigned ();
1886 /* We may have already read in this line header (TU line header sharing).
1887 If we have we're done. */
1888 find_entry.hash.dwo_unit = cu->dwo_unit;
1889 find_entry.hash.line_sect_off = line_offset;
1890 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
1891 &find_entry, INSERT);
1892 if (*slot != NULL)
1894 lh_cu->file_names = (struct quick_file_names *) *slot;
1895 return;
1898 lh = dwarf_decode_line_header (line_offset, cu, fnd.get_comp_dir ());
1901 int offset = 0;
1902 if (!fnd.is_unknown ())
1903 ++offset;
1904 else if (lh == nullptr)
1905 return;
1907 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
1908 qfn->hash.dwo_unit = cu->dwo_unit;
1909 qfn->hash.line_sect_off = line_offset;
1910 /* There may not be a DW_AT_stmt_list. */
1911 if (slot != nullptr)
1912 *slot = qfn;
1914 std::vector<const char *> include_names;
1915 if (lh != nullptr)
1917 for (const auto &entry : lh->file_names ())
1919 std::string name_holder;
1920 const char *include_name =
1921 compute_include_file_name (lh.get (), entry, fnd, name_holder);
1922 if (include_name != nullptr)
1924 include_name = per_objfile->objfile->intern (include_name);
1925 include_names.push_back (include_name);
1930 qfn->num_file_names = offset + include_names.size ();
1931 qfn->comp_dir = fnd.intern_comp_dir (per_objfile->objfile);
1932 qfn->file_names =
1933 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
1934 qfn->num_file_names);
1935 if (offset != 0)
1936 qfn->file_names[0] = per_objfile->objfile->intern (fnd.get_name ());
1938 if (!include_names.empty ())
1939 memcpy (&qfn->file_names[offset], include_names.data (),
1940 include_names.size () * sizeof (const char *));
1942 qfn->real_names = NULL;
1944 lh_cu->file_names = qfn;
1947 /* A helper for the "quick" functions which attempts to read the line
1948 table for THIS_CU. */
1950 static struct quick_file_names *
1951 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
1952 dwarf2_per_objfile *per_objfile)
1954 /* This should never be called for TUs. */
1955 gdb_assert (! this_cu->is_debug_types);
1957 if (this_cu->files_read)
1958 return this_cu->file_names;
1960 cutu_reader reader (this_cu, per_objfile);
1961 if (!reader.dummy_p)
1962 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
1964 return this_cu->file_names;
1967 /* A helper for the "quick" functions which computes and caches the
1968 real path for a given file name from the line table. */
1970 static const char *
1971 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
1972 struct quick_file_names *qfn, int index)
1974 if (qfn->real_names == NULL)
1975 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
1976 qfn->num_file_names, const char *);
1978 if (qfn->real_names[index] == NULL)
1980 const char *dirname = nullptr;
1982 if (!IS_ABSOLUTE_PATH (qfn->file_names[index]))
1983 dirname = qfn->comp_dir;
1985 gdb::unique_xmalloc_ptr<char> fullname;
1986 fullname = find_source_or_rewrite (qfn->file_names[index], dirname);
1988 qfn->real_names[index] = fullname.release ();
1991 return qfn->real_names[index];
1994 struct symtab *
1995 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
1997 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1998 dwarf2_per_cu_data *dwarf_cu
1999 = per_objfile->per_bfd->all_units.back ().get ();
2000 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
2002 if (cust == NULL)
2003 return NULL;
2005 return cust->primary_filetab ();
2008 /* See read.h. */
2010 void
2011 dwarf2_per_cu_data::free_cached_file_names ()
2013 if (fnd != nullptr)
2014 fnd->forget_fullname ();
2016 if (per_bfd == nullptr)
2017 return;
2019 struct quick_file_names *file_data = file_names;
2020 if (file_data != nullptr && file_data->real_names != nullptr)
2022 for (int i = 0; i < file_data->num_file_names; ++i)
2024 xfree ((void *) file_data->real_names[i]);
2025 file_data->real_names[i] = nullptr;
2030 void
2031 dwarf2_base_index_functions::forget_cached_source_info
2032 (struct objfile *objfile)
2034 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2036 for (auto &per_cu : per_objfile->per_bfd->all_units)
2037 per_cu->free_cached_file_names ();
2040 void
2041 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
2042 bool print_bcache)
2044 if (print_bcache)
2045 return;
2047 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2048 int total = per_objfile->per_bfd->all_units.size ();
2049 int count = 0;
2051 for (int i = 0; i < total; ++i)
2053 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2055 if (!per_objfile->symtab_set_p (per_cu))
2056 ++count;
2058 gdb_printf (_(" Number of read CUs: %d\n"), total - count);
2059 gdb_printf (_(" Number of unread CUs: %d\n"), count);
2062 void
2063 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
2065 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2066 int total_units = per_objfile->per_bfd->all_units.size ();
2068 for (int i = 0; i < total_units; ++i)
2070 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
2072 /* We don't want to directly expand a partial CU, because if we
2073 read it with the wrong language, then assertion failures can
2074 be triggered later on. See PR symtab/23010. So, tell
2075 dw2_instantiate_symtab to skip partial CUs -- any important
2076 partial CU will be read via DW_TAG_imported_unit anyway. */
2077 dw2_instantiate_symtab (per_cu, per_objfile, true);
2081 /* See read.h. */
2083 bool
2084 dw2_expand_symtabs_matching_one
2085 (dwarf2_per_cu_data *per_cu,
2086 dwarf2_per_objfile *per_objfile,
2087 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2088 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2089 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
2091 if (file_matcher != nullptr && !per_cu->mark)
2092 return true;
2094 if (lang_matcher != nullptr)
2096 /* Try to skip CUs with non-matching language. */
2097 per_cu->ensure_lang (per_objfile);
2098 if (!per_cu->maybe_multi_language ()
2099 && !lang_matcher (per_cu->lang ()))
2100 return true;
2103 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
2104 compunit_symtab *symtab
2105 = dw2_instantiate_symtab (per_cu, per_objfile, false);
2106 gdb_assert (symtab != nullptr);
2108 if (expansion_notify != NULL && symtab_was_null)
2109 return expansion_notify (symtab);
2111 return true;
2114 /* See read.h. */
2116 void
2117 dw_expand_symtabs_matching_file_matcher
2118 (dwarf2_per_objfile *per_objfile,
2119 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
2121 if (file_matcher == NULL)
2122 return;
2124 gdb::unordered_set<quick_file_names *> visited_found;
2125 gdb::unordered_set<quick_file_names *> visited_not_found;
2127 /* The rule is CUs specify all the files, including those used by
2128 any TU, so there's no need to scan TUs here. */
2130 for (const auto &per_cu : per_objfile->per_bfd->all_units)
2132 QUIT;
2134 if (per_cu->is_debug_types)
2135 continue;
2136 per_cu->mark = 0;
2138 /* We only need to look at symtabs not already expanded. */
2139 if (per_objfile->symtab_set_p (per_cu.get ()))
2140 continue;
2142 if (per_cu->fnd != nullptr)
2144 file_and_directory *fnd = per_cu->fnd.get ();
2146 if (file_matcher (fnd->get_name (), false))
2148 per_cu->mark = 1;
2149 continue;
2152 /* Before we invoke realpath, which can get expensive when many
2153 files are involved, do a quick comparison of the basenames. */
2154 if ((basenames_may_differ
2155 || file_matcher (lbasename (fnd->get_name ()), true))
2156 && file_matcher (fnd->get_fullname (), false))
2158 per_cu->mark = 1;
2159 continue;
2163 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
2164 per_objfile);
2165 if (file_data == NULL)
2166 continue;
2168 if (visited_not_found.contains (file_data))
2169 continue;
2170 else if (visited_found.contains (file_data))
2172 per_cu->mark = 1;
2173 continue;
2176 for (int j = 0; j < file_data->num_file_names; ++j)
2178 const char *this_real_name;
2180 if (file_matcher (file_data->file_names[j], false))
2182 per_cu->mark = 1;
2183 break;
2186 /* Before we invoke realpath, which can get expensive when many
2187 files are involved, do a quick comparison of the basenames. */
2188 if (!basenames_may_differ
2189 && !file_matcher (lbasename (file_data->file_names[j]),
2190 true))
2191 continue;
2193 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
2194 if (file_matcher (this_real_name, false))
2196 per_cu->mark = 1;
2197 break;
2201 if (per_cu->mark)
2202 visited_found.insert (file_data);
2203 else
2204 visited_not_found.insert (file_data);
2209 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
2210 symtab. */
2212 static struct compunit_symtab *
2213 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
2214 CORE_ADDR pc)
2216 int i;
2218 if (cust->blockvector () != nullptr
2219 && blockvector_contains_pc (cust->blockvector (), pc))
2220 return cust;
2222 if (cust->includes == NULL)
2223 return NULL;
2225 for (i = 0; cust->includes[i]; ++i)
2227 struct compunit_symtab *s = cust->includes[i];
2229 s = recursively_find_pc_sect_compunit_symtab (s, pc);
2230 if (s != NULL)
2231 return s;
2234 return NULL;
2237 struct compunit_symtab *
2238 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
2239 (struct objfile *objfile,
2240 bound_minimal_symbol msymbol,
2241 CORE_ADDR pc,
2242 struct obj_section *section,
2243 int warn_if_readin)
2245 struct compunit_symtab *result;
2247 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2248 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2250 if (per_bfd->index_table == nullptr)
2251 return nullptr;
2253 CORE_ADDR baseaddr = objfile->text_section_offset ();
2254 struct dwarf2_per_cu_data *data
2255 = per_bfd->index_table->lookup ((unrelocated_addr) (pc - baseaddr));
2256 if (data == nullptr)
2257 return nullptr;
2259 if (warn_if_readin && per_objfile->symtab_set_p (data))
2260 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2261 paddress (objfile->arch (), pc));
2263 result = recursively_find_pc_sect_compunit_symtab
2264 (dw2_instantiate_symtab (data, per_objfile, false), pc);
2266 if (warn_if_readin && result == nullptr)
2267 warning (_("(Error: pc %s in address map, but not in symtab.)"),
2268 paddress (objfile->arch (), pc));
2270 return result;
2273 void
2274 dwarf2_base_index_functions::map_symbol_filenames
2275 (struct objfile *objfile,
2276 gdb::function_view<symbol_filename_ftype> fun,
2277 bool need_fullname)
2279 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2281 /* Use caches to ensure we only call FUN once for each filename. */
2282 filename_seen_cache filenames_cache;
2283 std::unordered_set<quick_file_names *> qfn_cache;
2285 /* The rule is CUs specify all the files, including those used by any TU,
2286 so there's no need to scan TUs here. We can ignore file names coming
2287 from already-expanded CUs. It is possible that an expanded CU might
2288 reuse the file names data from a currently unexpanded CU, in this
2289 case we don't want to report the files from the unexpanded CU. */
2291 for (const auto &per_cu : per_objfile->per_bfd->all_units)
2293 if (!per_cu->is_debug_types
2294 && per_objfile->symtab_set_p (per_cu.get ()))
2296 if (per_cu->file_names != nullptr)
2297 qfn_cache.insert (per_cu->file_names);
2301 for (dwarf2_per_cu_data *per_cu
2302 : all_units_range (per_objfile->per_bfd))
2304 /* We only need to look at symtabs not already expanded. */
2305 if (per_cu->is_debug_types || per_objfile->symtab_set_p (per_cu))
2306 continue;
2308 if (per_cu->fnd != nullptr)
2310 file_and_directory *fnd = per_cu->fnd.get ();
2312 const char *filename = fnd->get_name ();
2313 const char *key = filename;
2314 const char *fullname = nullptr;
2316 if (need_fullname)
2318 fullname = fnd->get_fullname ();
2319 key = fullname;
2322 if (!filenames_cache.seen (key))
2323 fun (filename, fullname);
2326 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
2327 if (file_data == nullptr
2328 || qfn_cache.find (file_data) != qfn_cache.end ())
2329 continue;
2331 for (int j = 0; j < file_data->num_file_names; ++j)
2333 const char *filename = file_data->file_names[j];
2334 const char *key = filename;
2335 const char *fullname = nullptr;
2337 if (need_fullname)
2339 fullname = dw2_get_real_path (per_objfile, file_data, j);
2340 key = fullname;
2343 if (!filenames_cache.seen (key))
2344 fun (filename, fullname);
2349 bool
2350 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
2352 return true;
2355 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
2357 bool
2358 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile *objfile)
2360 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2362 for (const auto &per_cu : per_objfile->per_bfd->all_units)
2364 /* Is this already expanded? */
2365 if (per_objfile->symtab_set_p (per_cu.get ()))
2366 continue;
2368 /* It has not yet been expanded. */
2369 return true;
2372 return false;
2375 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
2376 to either a dwarf2_per_bfd or dwz_file object. */
2378 template <typename T>
2379 static gdb::array_view<const gdb_byte>
2380 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
2382 dwarf2_section_info *section = &section_owner->gdb_index;
2384 if (section->empty ())
2385 return {};
2387 /* Older elfutils strip versions could keep the section in the main
2388 executable while splitting it for the separate debug info file. */
2389 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
2390 return {};
2392 section->read (obj);
2394 /* dwarf2_section_info::size is a bfd_size_type, while
2395 gdb::array_view works with size_t. On 32-bit hosts, with
2396 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
2397 is 32-bit. So we need an explicit narrowing conversion here.
2398 This is fine, because it's impossible to allocate or mmap an
2399 array/buffer larger than what size_t can represent. */
2400 return gdb::make_array_view (section->buffer, section->size);
2403 /* Lookup the index cache for the contents of the index associated to
2404 DWARF2_OBJ. */
2406 static gdb::array_view<const gdb_byte>
2407 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
2409 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
2410 if (build_id == nullptr)
2411 return {};
2413 return global_index_cache.lookup_gdb_index (build_id,
2414 &dwarf2_per_bfd->index_cache_res);
2417 /* Same as the above, but for DWZ. */
2419 static gdb::array_view<const gdb_byte>
2420 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
2422 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
2423 if (build_id == nullptr)
2424 return {};
2426 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
2429 static void start_debug_info_reader (dwarf2_per_objfile *);
2431 /* See dwarf2/public.h. */
2433 bool
2434 dwarf2_initialize_objfile (struct objfile *objfile,
2435 const struct dwarf2_debug_sections *names,
2436 bool can_copy)
2438 if (!dwarf2_has_info (objfile, names, can_copy))
2439 return false;
2441 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2442 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2444 dwarf_read_debug_printf ("called");
2446 /* If we're about to read full symbols, don't bother with the
2447 indices. In this case we also don't care if some other debug
2448 format is making psymtabs, because they are all about to be
2449 expanded anyway. */
2450 if ((objfile->flags & OBJF_READNOW))
2452 dwarf_read_debug_printf ("readnow requested");
2454 create_all_units (per_objfile);
2455 per_bfd->quick_file_names_table
2456 = create_quick_file_names_table (per_bfd->all_units.size ());
2458 objfile->qf.emplace_front (new readnow_functions);
2460 /* Was a GDB index already read when we processed an objfile sharing
2461 PER_BFD? */
2462 else if (per_bfd->index_table != nullptr)
2463 dwarf_read_debug_printf ("re-using symbols");
2464 else if (dwarf2_read_debug_names (per_objfile))
2465 dwarf_read_debug_printf ("found debug names");
2466 else if (dwarf2_read_gdb_index (per_objfile,
2467 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
2468 get_gdb_index_contents_from_section<dwz_file>))
2469 dwarf_read_debug_printf ("found gdb index from file");
2470 /* ... otherwise, try to find the index in the index cache. */
2471 else if (dwarf2_read_gdb_index (per_objfile,
2472 get_gdb_index_contents_from_cache,
2473 get_gdb_index_contents_from_cache_dwz))
2475 dwarf_read_debug_printf ("found gdb index from cache");
2476 global_index_cache.hit ();
2478 else
2480 global_index_cache.miss ();
2481 start_debug_info_reader (per_objfile);
2484 if (per_bfd->index_table != nullptr)
2486 if (dwarf_synchronous)
2487 per_bfd->index_table->wait_completely ();
2488 objfile->qf.push_front (per_bfd->index_table->make_quick_functions ());
2491 return true;
2496 /* Find the base address of the compilation unit for range lists and
2497 location lists. It will normally be specified by DW_AT_low_pc.
2498 In DWARF-3 draft 4, the base address could be overridden by
2499 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2500 compilation units with discontinuous ranges. */
2502 static void
2503 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2505 struct attribute *attr;
2507 cu->base_address.reset ();
2509 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2510 if (attr != nullptr)
2511 cu->base_address = attr->as_address ();
2512 else
2514 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2515 if (attr != nullptr)
2516 cu->base_address = attr->as_address ();
2520 /* Helper function that returns the proper abbrev section for
2521 THIS_CU. */
2523 static struct dwarf2_section_info *
2524 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
2526 struct dwarf2_section_info *abbrev;
2527 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
2529 if (this_cu->is_dwz)
2530 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
2531 else
2532 abbrev = &per_bfd->abbrev;
2534 return abbrev;
2537 /* Fetch the abbreviation table offset from a comp or type unit header. */
2539 static sect_offset
2540 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
2541 struct dwarf2_section_info *section,
2542 sect_offset sect_off)
2544 bfd *abfd = section->get_bfd_owner ();
2545 const gdb_byte *info_ptr;
2546 unsigned int initial_length_size, offset_size;
2547 uint16_t version;
2549 section->read (per_objfile->objfile);
2550 info_ptr = section->buffer + to_underlying (sect_off);
2551 read_initial_length (abfd, info_ptr, &initial_length_size);
2552 offset_size = initial_length_size == 4 ? 4 : 8;
2553 info_ptr += initial_length_size;
2555 version = read_2_bytes (abfd, info_ptr);
2556 info_ptr += 2;
2557 if (version >= 5)
2559 /* Skip unit type and address size. */
2560 info_ptr += 2;
2563 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
2566 static hashval_t
2567 hash_signatured_type (const void *item)
2569 const struct signatured_type *sig_type
2570 = (const struct signatured_type *) item;
2572 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2573 return sig_type->signature;
2576 static int
2577 eq_signatured_type (const void *item_lhs, const void *item_rhs)
2579 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
2580 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
2582 return lhs->signature == rhs->signature;
2585 /* See read.h. */
2587 htab_up
2588 allocate_signatured_type_table ()
2590 return htab_up (htab_create_alloc (41,
2591 hash_signatured_type,
2592 eq_signatured_type,
2593 NULL, xcalloc, xfree));
2596 /* A helper for create_debug_types_hash_table. Read types from SECTION
2597 and fill them into TYPES_HTAB. It will process only type units,
2598 therefore DW_UT_type. */
2600 static void
2601 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
2602 struct dwo_file *dwo_file,
2603 dwarf2_section_info *section, htab_up &types_htab,
2604 rcuh_kind section_kind)
2606 struct objfile *objfile = per_objfile->objfile;
2607 struct dwarf2_section_info *abbrev_section;
2608 bfd *abfd;
2609 const gdb_byte *info_ptr, *end_ptr;
2611 abbrev_section = &dwo_file->sections.abbrev;
2613 dwarf_read_debug_printf ("Reading %s for %s",
2614 section->get_name (),
2615 abbrev_section->get_file_name ());
2617 section->read (objfile);
2618 info_ptr = section->buffer;
2620 if (info_ptr == NULL)
2621 return;
2623 /* We can't set abfd until now because the section may be empty or
2624 not present, in which case the bfd is unknown. */
2625 abfd = section->get_bfd_owner ();
2627 /* We don't use cutu_reader here because we don't need to read
2628 any dies: the signature is in the header. */
2630 end_ptr = info_ptr + section->size;
2631 while (info_ptr < end_ptr)
2633 signatured_type_up sig_type;
2634 struct dwo_unit *dwo_tu;
2635 void **slot;
2636 const gdb_byte *ptr = info_ptr;
2637 struct comp_unit_head header;
2638 unsigned int length;
2640 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
2642 /* Initialize it due to a false compiler warning. */
2643 header.signature = -1;
2644 header.type_cu_offset_in_tu = (cu_offset) -1;
2646 /* We need to read the type's signature in order to build the hash
2647 table, but we don't need anything else just yet. */
2649 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
2650 abbrev_section, ptr, section_kind);
2652 length = header.get_length_with_initial ();
2654 /* Skip dummy type units. */
2655 if (ptr >= info_ptr + length
2656 || peek_abbrev_code (abfd, ptr) == 0
2657 || (header.unit_type != DW_UT_type
2658 && header.unit_type != DW_UT_split_type))
2660 info_ptr += length;
2661 continue;
2664 if (types_htab == NULL)
2665 types_htab = allocate_dwo_unit_table ();
2667 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
2668 dwo_tu->dwo_file = dwo_file;
2669 dwo_tu->signature = header.signature;
2670 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
2671 dwo_tu->section = section;
2672 dwo_tu->sect_off = sect_off;
2673 dwo_tu->length = length;
2675 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
2676 gdb_assert (slot != NULL);
2677 if (*slot != NULL)
2678 complaint (_("debug type entry at offset %s is duplicate to"
2679 " the entry at offset %s, signature %s"),
2680 sect_offset_str (sect_off),
2681 sect_offset_str (dwo_tu->sect_off),
2682 hex_string (header.signature));
2683 *slot = dwo_tu;
2685 dwarf_read_debug_printf_v (" offset %s, signature %s",
2686 sect_offset_str (sect_off),
2687 hex_string (header.signature));
2689 info_ptr += length;
2693 /* Create the hash table of all entries in the .debug_types
2694 (or .debug_types.dwo) section(s).
2695 DWO_FILE is a pointer to the DWO file object.
2697 The result is a pointer to the hash table or NULL if there are no types.
2699 Note: This function processes DWO files only, not DWP files. */
2701 static void
2702 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
2703 struct dwo_file *dwo_file,
2704 gdb::array_view<dwarf2_section_info> type_sections,
2705 htab_up &types_htab)
2707 for (dwarf2_section_info &section : type_sections)
2708 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
2709 rcuh_kind::TYPE);
2712 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
2713 If SLOT is non-NULL, it is the entry to use in the hash table.
2714 Otherwise we find one. */
2716 static struct signatured_type *
2717 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
2719 if (per_objfile->per_bfd->all_units.size ()
2720 == per_objfile->per_bfd->all_units.capacity ())
2721 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
2723 signatured_type_up sig_type_holder
2724 = per_objfile->per_bfd->allocate_signatured_type (sig);
2725 signatured_type *sig_type = sig_type_holder.get ();
2727 per_objfile->per_bfd->all_units.emplace_back
2728 (sig_type_holder.release ());
2730 if (slot == NULL)
2732 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
2733 sig_type, INSERT);
2735 gdb_assert (*slot == NULL);
2736 *slot = sig_type;
2737 /* The rest of sig_type must be filled in by the caller. */
2738 return sig_type;
2741 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
2742 Fill in SIG_ENTRY with DWO_ENTRY. */
2744 static void
2745 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
2746 struct signatured_type *sig_entry,
2747 struct dwo_unit *dwo_entry)
2749 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2751 /* Make sure we're not clobbering something we don't expect to. */
2752 gdb_assert (! sig_entry->queued);
2753 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
2754 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
2755 gdb_assert (sig_entry->signature == dwo_entry->signature);
2756 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0
2757 || (to_underlying (sig_entry->type_offset_in_section)
2758 == to_underlying (dwo_entry->type_offset_in_tu)));
2759 gdb_assert (sig_entry->type_unit_group == NULL);
2760 gdb_assert (sig_entry->dwo_unit == NULL
2761 || sig_entry->dwo_unit == dwo_entry);
2763 sig_entry->section = dwo_entry->section;
2764 sig_entry->sect_off = dwo_entry->sect_off;
2765 sig_entry->set_length (dwo_entry->length, false);
2766 sig_entry->reading_dwo_directly = 1;
2767 sig_entry->per_bfd = per_bfd;
2768 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
2769 sig_entry->dwo_unit = dwo_entry;
2772 /* Subroutine of lookup_signatured_type.
2773 If we haven't read the TU yet, create the signatured_type data structure
2774 for a TU to be read in directly from a DWO file, bypassing the stub.
2775 This is the "Stay in DWO Optimization": When there is no DWP file and we're
2776 using .gdb_index, then when reading a CU we want to stay in the DWO file
2777 containing that CU. Otherwise we could end up reading several other DWO
2778 files (due to comdat folding) to process the transitive closure of all the
2779 mentioned TUs, and that can be slow. The current DWO file will have every
2780 type signature that it needs.
2781 We only do this for .gdb_index because in the psymtab case we already have
2782 to read all the DWOs to build the type unit groups. */
2784 static struct signatured_type *
2785 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
2787 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2788 struct dwo_file *dwo_file;
2789 struct dwo_unit find_dwo_entry, *dwo_entry;
2790 void **slot;
2792 gdb_assert (cu->dwo_unit);
2794 /* If TU skeletons have been removed then we may not have read in any
2795 TUs yet. */
2796 if (per_objfile->per_bfd->signatured_types == NULL)
2797 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
2799 /* We only ever need to read in one copy of a signatured type.
2800 Use the global signatured_types array to do our own comdat-folding
2801 of types. If this is the first time we're reading this TU, and
2802 the TU has an entry in .gdb_index, replace the recorded data from
2803 .gdb_index with this TU. */
2805 signatured_type find_sig_entry (sig);
2806 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
2807 &find_sig_entry, INSERT);
2808 signatured_type *sig_entry = (struct signatured_type *) *slot;
2810 /* We can get here with the TU already read, *or* in the process of being
2811 read. Don't reassign the global entry to point to this DWO if that's
2812 the case. Also note that if the TU is already being read, it may not
2813 have come from a DWO, the program may be a mix of Fission-compiled
2814 code and non-Fission-compiled code. */
2816 /* Have we already tried to read this TU?
2817 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
2818 needn't exist in the global table yet). */
2819 if (sig_entry != NULL && sig_entry->tu_read)
2820 return sig_entry;
2822 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
2823 dwo_unit of the TU itself. */
2824 dwo_file = cu->dwo_unit->dwo_file;
2826 /* Ok, this is the first time we're reading this TU. */
2827 if (dwo_file->tus == NULL)
2828 return NULL;
2829 find_dwo_entry.signature = sig;
2830 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
2831 &find_dwo_entry);
2832 if (dwo_entry == NULL)
2833 return NULL;
2835 /* If the global table doesn't have an entry for this TU, add one. */
2836 if (sig_entry == NULL)
2837 sig_entry = add_type_unit (per_objfile, sig, slot);
2839 if (sig_entry->dwo_unit == nullptr)
2840 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
2841 sig_entry->tu_read = 1;
2842 return sig_entry;
2845 /* Subroutine of lookup_signatured_type.
2846 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
2847 then try the DWP file. If the TU stub (skeleton) has been removed then
2848 it won't be in .gdb_index. */
2850 static struct signatured_type *
2851 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
2853 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2854 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
2855 struct dwo_unit *dwo_entry;
2856 void **slot;
2858 gdb_assert (cu->dwo_unit);
2859 gdb_assert (dwp_file != NULL);
2861 /* If TU skeletons have been removed then we may not have read in any
2862 TUs yet. */
2863 if (per_objfile->per_bfd->signatured_types == NULL)
2864 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
2866 signatured_type find_sig_entry (sig);
2867 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
2868 &find_sig_entry, INSERT);
2869 signatured_type *sig_entry = (struct signatured_type *) *slot;
2871 /* Have we already tried to read this TU?
2872 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
2873 needn't exist in the global table yet). */
2874 if (sig_entry != NULL)
2875 return sig_entry;
2877 if (dwp_file->tus == NULL)
2878 return NULL;
2879 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
2880 1 /* is_debug_types */);
2881 if (dwo_entry == NULL)
2882 return NULL;
2884 sig_entry = add_type_unit (per_objfile, sig, slot);
2885 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
2887 return sig_entry;
2890 /* Lookup a signature based type for DW_FORM_ref_sig8.
2891 Returns NULL if signature SIG is not present in the table.
2892 It is up to the caller to complain about this. */
2894 static struct signatured_type *
2895 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
2897 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2899 if (cu->dwo_unit)
2901 /* We're in a DWO/DWP file, and we're using .gdb_index.
2902 These cases require special processing. */
2903 if (get_dwp_file (per_objfile) == NULL)
2904 return lookup_dwo_signatured_type (cu, sig);
2905 else
2906 return lookup_dwp_signatured_type (cu, sig);
2908 else
2910 if (per_objfile->per_bfd->signatured_types == NULL)
2911 return NULL;
2912 signatured_type find_entry (sig);
2913 return ((struct signatured_type *)
2914 htab_find (per_objfile->per_bfd->signatured_types.get (),
2915 &find_entry));
2919 /* Low level DIE reading support. */
2921 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2923 static void
2924 init_cu_die_reader (struct die_reader_specs *reader,
2925 struct dwarf2_cu *cu,
2926 struct dwarf2_section_info *section,
2927 struct dwo_file *dwo_file,
2928 const abbrev_table *abbrev_table)
2930 gdb_assert (section->readin && section->buffer != NULL);
2931 reader->abfd = section->get_bfd_owner ();
2932 reader->cu = cu;
2933 reader->dwo_file = dwo_file;
2934 reader->die_section = section;
2935 reader->buffer = section->buffer;
2936 reader->buffer_end = section->buffer + section->size;
2937 reader->abbrev_table = abbrev_table;
2940 /* Subroutine of cutu_reader to simplify it.
2941 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
2942 There's just a lot of work to do, and cutu_reader is big enough
2943 already.
2945 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
2946 from it to the DIE in the DWO. If NULL we are skipping the stub.
2947 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
2948 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
2949 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
2950 STUB_COMP_DIR may be non-NULL.
2951 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
2952 are filled in with the info of the DIE from the DWO file.
2953 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
2954 from the dwo. Since *RESULT_READER references this abbrev table, it must be
2955 kept around for at least as long as *RESULT_READER.
2957 The result is non-zero if a valid (non-dummy) DIE was found. */
2959 static int
2960 read_cutu_die_from_dwo (dwarf2_cu *cu,
2961 struct dwo_unit *dwo_unit,
2962 struct die_info *stub_comp_unit_die,
2963 const char *stub_comp_dir,
2964 struct die_reader_specs *result_reader,
2965 const gdb_byte **result_info_ptr,
2966 struct die_info **result_comp_unit_die,
2967 abbrev_table_up *result_dwo_abbrev_table)
2969 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2970 dwarf2_per_cu_data *per_cu = cu->per_cu;
2971 struct objfile *objfile = per_objfile->objfile;
2972 bfd *abfd;
2973 const gdb_byte *begin_info_ptr, *info_ptr;
2974 struct dwarf2_section_info *dwo_abbrev_section;
2976 /* At most one of these may be provided. */
2977 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
2979 /* These attributes aren't processed until later: DW_AT_stmt_list,
2980 DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_comp_dir.
2981 However, these attributes are found in the stub which we won't
2982 have later. In order to not impose this complication on the rest
2983 of the code, we read them here and copy them to the DWO CU/TU
2984 die. */
2986 /* We store them all in an array. */
2987 struct attribute *attributes[5] {};
2988 /* Next available element of the attributes array. */
2989 int next_attr_idx = 0;
2991 /* Push an element into ATTRIBUTES. */
2992 auto push_back = [&] (struct attribute *attr)
2994 gdb_assert (next_attr_idx < ARRAY_SIZE (attributes));
2995 if (attr != nullptr)
2996 attributes[next_attr_idx++] = attr;
2999 if (stub_comp_unit_die != NULL)
3001 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3002 DWO file. */
3003 if (!per_cu->is_debug_types)
3004 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu));
3005 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu));
3006 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu));
3007 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu));
3008 push_back (dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu));
3010 cu->addr_base = stub_comp_unit_die->addr_base ();
3012 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3013 We need the value before we can process DW_AT_ranges values from the
3014 DWO. */
3015 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
3017 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3018 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3019 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3020 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3021 section. */
3022 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
3024 else if (stub_comp_dir != NULL)
3026 /* Reconstruct the comp_dir attribute to simplify the code below. */
3027 struct attribute *comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack,
3028 struct attribute);
3029 comp_dir->name = DW_AT_comp_dir;
3030 comp_dir->form = DW_FORM_string;
3031 comp_dir->set_string_noncanonical (stub_comp_dir);
3032 push_back (comp_dir);
3035 /* Set up for reading the DWO CU/TU. */
3036 cu->dwo_unit = dwo_unit;
3037 dwarf2_section_info *section = dwo_unit->section;
3038 section->read (objfile);
3039 abfd = section->get_bfd_owner ();
3040 begin_info_ptr = info_ptr = (section->buffer
3041 + to_underlying (dwo_unit->sect_off));
3042 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3044 if (per_cu->is_debug_types)
3046 signatured_type *sig_type = (struct signatured_type *) per_cu;
3048 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3049 section, dwo_abbrev_section,
3050 info_ptr, rcuh_kind::TYPE);
3051 /* This is not an assert because it can be caused by bad debug info. */
3052 if (sig_type->signature != cu->header.signature)
3054 error (_(DWARF_ERROR_PREFIX
3055 "signature mismatch %s vs %s while reading TU at offset %s"
3056 " [in module %s]"),
3057 hex_string (sig_type->signature),
3058 hex_string (cu->header.signature),
3059 sect_offset_str (dwo_unit->sect_off),
3060 bfd_get_filename (abfd));
3062 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3063 /* For DWOs coming from DWP files, we don't know the CU length
3064 nor the type's offset in the TU until now. */
3065 dwo_unit->length = cu->header.get_length_with_initial ();
3066 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
3068 /* Establish the type offset that can be used to lookup the type.
3069 For DWO files, we don't know it until now. */
3070 sig_type->type_offset_in_section
3071 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
3073 else
3075 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3076 section, dwo_abbrev_section,
3077 info_ptr, rcuh_kind::COMPILE);
3078 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
3079 /* For DWOs coming from DWP files, we don't know the CU length
3080 until now. */
3081 dwo_unit->length = cu->header.get_length_with_initial ();
3084 dwo_abbrev_section->read (objfile);
3085 *result_dwo_abbrev_table
3086 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
3087 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
3088 result_dwo_abbrev_table->get ());
3090 /* Read in the die, filling in the attributes from the stub. This
3091 has the benefit of simplifying the rest of the code - all the
3092 work to maintain the illusion of a single
3093 DW_TAG_{compile,type}_unit DIE is done here. */
3094 info_ptr = read_toplevel_die (result_reader, result_comp_unit_die, info_ptr,
3095 gdb::make_array_view (attributes,
3096 next_attr_idx));
3098 /* Skip dummy compilation units. */
3099 if (info_ptr >= begin_info_ptr + dwo_unit->length
3100 || peek_abbrev_code (abfd, info_ptr) == 0)
3101 return 0;
3103 *result_info_ptr = info_ptr;
3104 return 1;
3107 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
3108 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
3109 signature is part of the header. */
3110 static std::optional<ULONGEST>
3111 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
3113 if (cu->header.version >= 5)
3114 return cu->header.signature;
3115 struct attribute *attr;
3116 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3117 if (attr == nullptr || !attr->form_is_unsigned ())
3118 return std::optional<ULONGEST> ();
3119 return attr->as_unsigned ();
3122 /* Subroutine of cutu_reader to simplify it.
3123 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
3124 Returns NULL if the specified DWO unit cannot be found. */
3126 static struct dwo_unit *
3127 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
3129 #if CXX_STD_THREAD
3130 /* We need a lock here to handle the DWO hash table. */
3131 static std::mutex dwo_lock;
3133 std::lock_guard<std::mutex> guard (dwo_lock);
3134 #endif
3136 dwarf2_per_cu_data *per_cu = cu->per_cu;
3137 struct dwo_unit *dwo_unit;
3138 const char *comp_dir;
3140 gdb_assert (cu != NULL);
3142 /* Yeah, we look dwo_name up again, but it simplifies the code. */
3143 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
3144 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
3146 if (per_cu->is_debug_types)
3147 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
3148 else
3150 std::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
3152 if (!signature.has_value ())
3153 error (_(DWARF_ERROR_PREFIX
3154 "missing dwo_id for dwo_name %s"
3155 " [in module %s]"),
3156 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
3158 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
3161 return dwo_unit;
3164 /* Subroutine of cutu_reader to simplify it.
3165 See it for a description of the parameters.
3166 Read a TU directly from a DWO file, bypassing the stub. */
3168 void
3169 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
3170 dwarf2_per_objfile *per_objfile,
3171 dwarf2_cu *existing_cu)
3173 struct signatured_type *sig_type;
3175 /* Verify we can do the following downcast, and that we have the
3176 data we need. */
3177 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
3178 sig_type = (struct signatured_type *) this_cu;
3179 gdb_assert (sig_type->dwo_unit != NULL);
3181 dwarf2_cu *cu;
3183 if (existing_cu != nullptr)
3185 cu = existing_cu;
3186 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
3187 /* There's no need to do the rereading_dwo_cu handling that
3188 cutu_reader does since we don't read the stub. */
3190 else
3192 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
3193 in per_objfile yet. */
3194 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
3195 m_new_cu = std::make_unique<dwarf2_cu> (this_cu, per_objfile);
3196 cu = m_new_cu.get ();
3199 /* A future optimization, if needed, would be to use an existing
3200 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
3201 could share abbrev tables. */
3203 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
3204 NULL /* stub_comp_unit_die */,
3205 sig_type->dwo_unit->dwo_file->comp_dir,
3206 this, &info_ptr,
3207 &comp_unit_die,
3208 &m_dwo_abbrev_table) == 0)
3210 /* Dummy die. */
3211 dummy_p = true;
3215 /* Initialize a CU (or TU) and read its DIEs.
3216 If the CU defers to a DWO file, read the DWO file as well.
3218 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
3219 Otherwise the table specified in the comp unit header is read in and used.
3220 This is an optimization for when we already have the abbrev table.
3222 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
3223 allocated. */
3225 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
3226 dwarf2_per_objfile *per_objfile,
3227 const struct abbrev_table *abbrev_table,
3228 dwarf2_cu *existing_cu,
3229 bool skip_partial,
3230 const abbrev_table_cache *cache)
3231 : die_reader_specs {},
3232 m_this_cu (this_cu)
3234 struct objfile *objfile = per_objfile->objfile;
3235 struct dwarf2_section_info *section = this_cu->section;
3236 bfd *abfd = section->get_bfd_owner ();
3237 const gdb_byte *begin_info_ptr;
3238 struct signatured_type *sig_type = NULL;
3239 struct dwarf2_section_info *abbrev_section;
3240 /* Non-zero if CU currently points to a DWO file and we need to
3241 reread it. When this happens we need to reread the skeleton die
3242 before we can reread the DWO file (this only applies to CUs, not TUs). */
3243 int rereading_dwo_cu = 0;
3245 if (dwarf_die_debug)
3246 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
3247 this_cu->is_debug_types ? "type" : "comp",
3248 sect_offset_str (this_cu->sect_off));
3250 /* If we're reading a TU directly from a DWO file, including a virtual DWO
3251 file (instead of going through the stub), short-circuit all of this. */
3252 if (this_cu->reading_dwo_directly)
3254 /* Narrow down the scope of possibilities to have to understand. */
3255 gdb_assert (this_cu->is_debug_types);
3256 gdb_assert (abbrev_table == NULL);
3257 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
3258 return;
3261 /* This is cheap if the section is already read in. */
3262 section->read (objfile);
3264 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
3266 abbrev_section = get_abbrev_section_for_cu (this_cu);
3268 dwarf2_cu *cu;
3270 if (existing_cu != nullptr)
3272 cu = existing_cu;
3273 /* If this CU is from a DWO file we need to start over, we need to
3274 refetch the attributes from the skeleton CU.
3275 This could be optimized by retrieving those attributes from when we
3276 were here the first time: the previous comp_unit_die was stored in
3277 comp_unit_obstack. But there's no data yet that we need this
3278 optimization. */
3279 if (cu->dwo_unit != NULL)
3280 rereading_dwo_cu = 1;
3282 else
3284 /* If an existing_cu is provided, a dwarf2_cu must not exist for
3285 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
3286 let us know that the CU is being scanned using the parallel
3287 indexer. This assert is avoided in this case because (1) it
3288 is irrelevant, and (2) the get_cu method is not
3289 thread-safe. */
3290 gdb_assert (cache != nullptr
3291 || per_objfile->get_cu (this_cu) == nullptr);
3292 m_new_cu = std::make_unique<dwarf2_cu> (this_cu, per_objfile);
3293 cu = m_new_cu.get ();
3296 /* Get the header. */
3297 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
3299 /* We already have the header, there's no need to read it in again. */
3300 info_ptr += to_underlying (cu->header.first_die_cu_offset);
3302 else
3304 if (this_cu->is_debug_types)
3306 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3307 section, abbrev_section,
3308 info_ptr, rcuh_kind::TYPE);
3310 /* Since per_cu is the first member of struct signatured_type,
3311 we can go from a pointer to one to a pointer to the other. */
3312 sig_type = (struct signatured_type *) this_cu;
3313 gdb_assert (sig_type->signature == cu->header.signature);
3314 gdb_assert (sig_type->type_offset_in_tu
3315 == cu->header.type_cu_offset_in_tu);
3316 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3318 /* LENGTH has not been set yet for type units if we're
3319 using .gdb_index. */
3320 this_cu->set_length (cu->header.get_length_with_initial ());
3322 /* Establish the type offset that can be used to lookup the type. */
3323 sig_type->type_offset_in_section =
3324 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
3326 this_cu->set_version (cu->header.version);
3328 else
3330 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
3331 section, abbrev_section,
3332 info_ptr,
3333 rcuh_kind::COMPILE);
3335 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3336 this_cu->set_length (cu->header.get_length_with_initial ());
3337 this_cu->set_version (cu->header.version);
3341 /* Skip dummy compilation units. */
3342 if (info_ptr >= begin_info_ptr + this_cu->length ()
3343 || peek_abbrev_code (abfd, info_ptr) == 0)
3345 dummy_p = true;
3346 return;
3349 /* If we don't have them yet, read the abbrevs for this compilation unit.
3350 And if we need to read them now, make sure they're freed when we're
3351 done. */
3352 if (abbrev_table != NULL)
3353 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
3354 else
3356 if (cache != nullptr)
3357 abbrev_table = cache->find (abbrev_section,
3358 cu->header.abbrev_sect_off);
3359 if (abbrev_table == nullptr)
3361 abbrev_section->read (objfile);
3362 m_abbrev_table_holder
3363 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
3364 abbrev_table = m_abbrev_table_holder.get ();
3368 /* Read the top level CU/TU die. */
3369 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3370 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
3372 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
3374 dummy_p = true;
3375 return;
3378 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
3379 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
3380 table from the DWO file and pass the ownership over to us. It will be
3381 referenced from READER, so we must make sure to free it after we're done
3382 with READER.
3384 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
3385 DWO CU, that this test will fail (the attribute will not be present). */
3386 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
3387 if (dwo_name != nullptr)
3389 struct dwo_unit *dwo_unit;
3390 struct die_info *dwo_comp_unit_die;
3392 if (comp_unit_die->has_children)
3394 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
3395 " has children (offset %s) [in module %s]"),
3396 sect_offset_str (this_cu->sect_off),
3397 bfd_get_filename (abfd));
3399 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
3400 if (dwo_unit != NULL)
3402 if (read_cutu_die_from_dwo (cu, dwo_unit,
3403 comp_unit_die, NULL,
3404 this, &info_ptr,
3405 &dwo_comp_unit_die,
3406 &m_dwo_abbrev_table) == 0)
3408 /* Dummy die. */
3409 dummy_p = true;
3410 return;
3412 comp_unit_die = dwo_comp_unit_die;
3414 else
3416 /* Yikes, we couldn't find the rest of the DIE, we only have
3417 the stub. A complaint has already been logged. There's
3418 not much more we can do except pass on the stub DIE to
3419 die_reader_func. We don't want to throw an error on bad
3420 debug info. */
3425 void
3426 cutu_reader::keep ()
3428 /* Done, clean up. */
3429 gdb_assert (!dummy_p);
3430 if (m_new_cu != NULL)
3432 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
3433 now. */
3434 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
3435 per_objfile->set_cu (m_this_cu, std::move (m_new_cu));
3439 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
3440 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
3441 assumed to have already done the lookup to find the DWO file).
3443 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3444 THIS_CU->is_debug_types, but nothing else.
3446 We fill in THIS_CU->length.
3448 THIS_CU->cu is always freed when done.
3449 This is done in order to not leave THIS_CU->cu in a state where we have
3450 to care whether it refers to the "main" CU or the DWO CU.
3452 When parent_cu is passed, it is used to provide a default value for
3453 str_offsets_base and addr_base from the parent. */
3455 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
3456 dwarf2_per_objfile *per_objfile,
3457 struct dwarf2_cu *parent_cu,
3458 struct dwo_file *dwo_file)
3459 : die_reader_specs {},
3460 m_this_cu (this_cu)
3462 struct objfile *objfile = per_objfile->objfile;
3463 struct dwarf2_section_info *section = this_cu->section;
3464 bfd *abfd = section->get_bfd_owner ();
3465 struct dwarf2_section_info *abbrev_section;
3466 const gdb_byte *begin_info_ptr, *info_ptr;
3468 if (dwarf_die_debug)
3469 gdb_printf (gdb_stdlog, "Reading %s unit at offset %s\n",
3470 this_cu->is_debug_types ? "type" : "comp",
3471 sect_offset_str (this_cu->sect_off));
3473 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
3475 abbrev_section = (dwo_file != NULL
3476 ? &dwo_file->sections.abbrev
3477 : get_abbrev_section_for_cu (this_cu));
3479 /* This is cheap if the section is already read in. */
3480 section->read (objfile);
3482 m_new_cu = std::make_unique<dwarf2_cu> (this_cu, per_objfile);
3484 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
3485 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
3486 section, abbrev_section, info_ptr,
3487 (this_cu->is_debug_types
3488 ? rcuh_kind::TYPE
3489 : rcuh_kind::COMPILE));
3491 if (parent_cu != nullptr)
3493 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
3494 m_new_cu->addr_base = parent_cu->addr_base;
3496 this_cu->set_length (m_new_cu->header.get_length_with_initial ());
3498 /* Skip dummy compilation units. */
3499 if (info_ptr >= begin_info_ptr + this_cu->length ()
3500 || peek_abbrev_code (abfd, info_ptr) == 0)
3502 dummy_p = true;
3503 return;
3506 abbrev_section->read (objfile);
3507 m_abbrev_table_holder
3508 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
3510 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
3511 m_abbrev_table_holder.get ());
3512 info_ptr = read_toplevel_die (this, &comp_unit_die, info_ptr);
3516 /* Type Unit Groups.
3518 Type Unit Groups are a way to collapse the set of all TUs (type units) into
3519 a more manageable set. The grouping is done by DW_AT_stmt_list entry
3520 so that all types coming from the same compilation (.o file) are grouped
3521 together. A future step could be to put the types in the same symtab as
3522 the CU the types ultimately came from. */
3524 static hashval_t
3525 hash_type_unit_group (const void *item)
3527 const struct type_unit_group *tu_group
3528 = (const struct type_unit_group *) item;
3530 return hash_stmt_list_entry (&tu_group->hash);
3533 static int
3534 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
3536 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
3537 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
3539 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
3542 /* Allocate a hash table for type unit groups. */
3544 static htab_up
3545 allocate_type_unit_groups_table ()
3547 return htab_up (htab_create_alloc (3,
3548 hash_type_unit_group,
3549 eq_type_unit_group,
3550 htab_delete_entry<type_unit_group>,
3551 xcalloc, xfree));
3554 /* Type units that don't have DW_AT_stmt_list are grouped into their own
3555 partial symtabs. We combine several TUs per psymtab to not let the size
3556 of any one psymtab grow too big. */
3557 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
3558 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
3560 /* Helper routine for get_type_unit_group.
3561 Create the type_unit_group object used to hold one or more TUs. */
3563 static std::unique_ptr<type_unit_group>
3564 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
3566 auto tu_group = std::make_unique<type_unit_group> ();
3568 tu_group->hash.dwo_unit = cu->dwo_unit;
3569 tu_group->hash.line_sect_off = line_offset_struct;
3571 return tu_group;
3574 /* Look up the type_unit_group for type unit CU, and create it if necessary.
3575 STMT_LIST is a DW_AT_stmt_list attribute. */
3577 static struct type_unit_group *
3578 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
3580 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3581 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
3582 struct type_unit_group *tu_group;
3583 void **slot;
3584 unsigned int line_offset;
3585 struct type_unit_group type_unit_group_for_lookup;
3587 if (per_objfile->per_bfd->type_unit_groups == NULL)
3588 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
3590 /* Do we need to create a new group, or can we use an existing one? */
3592 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
3594 line_offset = stmt_list->as_unsigned ();
3595 ++tu_stats->nr_symtab_sharers;
3597 else
3599 /* Ugh, no stmt_list. Rare, but we have to handle it.
3600 We can do various things here like create one group per TU or
3601 spread them over multiple groups to split up the expansion work.
3602 To avoid worst case scenarios (too many groups or too large groups)
3603 we, umm, group them in bunches. */
3604 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
3605 | (tu_stats->nr_stmt_less_type_units
3606 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
3607 ++tu_stats->nr_stmt_less_type_units;
3610 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
3611 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
3612 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
3613 &type_unit_group_for_lookup, INSERT);
3614 if (*slot == nullptr)
3616 sect_offset line_offset_struct = (sect_offset) line_offset;
3617 std::unique_ptr<type_unit_group> grp
3618 = create_type_unit_group (cu, line_offset_struct);
3619 *slot = grp.release ();
3620 ++tu_stats->nr_symtabs;
3623 tu_group = (struct type_unit_group *) *slot;
3624 gdb_assert (tu_group != nullptr);
3625 return tu_group;
3629 cooked_index_storage::cooked_index_storage ()
3630 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader,
3631 eq_cutu_reader,
3632 htab_delete_entry<cutu_reader>,
3633 xcalloc, xfree)),
3634 m_index (new cooked_index_shard)
3638 cutu_reader *
3639 cooked_index_storage::get_reader (dwarf2_per_cu_data *per_cu)
3641 int index = per_cu->index;
3642 return (cutu_reader *) htab_find_with_hash (m_reader_hash.get (),
3643 &index, index);
3646 cutu_reader *
3647 cooked_index_storage::preserve (std::unique_ptr<cutu_reader> reader)
3649 m_abbrev_table_cache.add (reader->release_abbrev_table ());
3651 int index = reader->cu->per_cu->index;
3652 void **slot = htab_find_slot_with_hash (m_reader_hash.get (), &index,
3653 index, INSERT);
3654 gdb_assert (*slot == nullptr);
3655 cutu_reader *result = reader.get ();
3656 *slot = reader.release ();
3657 return result;
3660 /* Hash function for a cutu_reader. */
3661 hashval_t
3662 cooked_index_storage::hash_cutu_reader (const void *a)
3664 const cutu_reader *reader = (const cutu_reader *) a;
3665 return reader->cu->per_cu->index;
3668 /* Equality function for cutu_reader. */
3670 cooked_index_storage::eq_cutu_reader (const void *a, const void *b)
3672 const cutu_reader *ra = (const cutu_reader *) a;
3673 const int *rb = (const int *) b;
3674 return ra->cu->per_cu->index == *rb;
3677 /* Dump MAP as parent_map. */
3679 static void
3680 dump_parent_map (dwarf2_per_bfd *per_bfd, const struct addrmap *map)
3682 auto_obstack temp_storage;
3684 auto annotate_cooked_index_entry
3685 = [&] (struct ui_file *outfile, CORE_ADDR start_addr, const void *value)
3687 const cooked_index_entry *parent_entry
3688 = (const cooked_index_entry *)value;
3690 gdb_printf (outfile, "\n\t");
3692 bool found = false;
3693 for (auto sections : {per_bfd->infos, per_bfd->types})
3694 for (auto section : sections)
3695 if ((CORE_ADDR)section.buffer <= start_addr
3696 && start_addr < (CORE_ADDR) (section.buffer + section.size))
3698 gdb_printf (outfile, "(section: %s, offset: 0x%" PRIx64 ")",
3699 section.get_name (),
3700 start_addr - (CORE_ADDR)section.buffer);
3701 found = true;
3702 break;
3705 if (!found)
3706 gdb_printf (outfile, "()");
3708 if (parent_entry == nullptr)
3710 gdb_printf (outfile, " -> ()");
3711 return;
3714 gdb_printf (outfile, " -> (0x%" PRIx64 ": %s)",
3715 to_underlying (parent_entry->die_offset),
3716 parent_entry->full_name (&temp_storage, false));
3719 addrmap_dump (const_cast<addrmap *> (map), gdb_stdlog, nullptr,
3720 annotate_cooked_index_entry);
3723 /* See parent-map.h. */
3725 void
3726 parent_map::dump (dwarf2_per_bfd *per_bfd) const
3728 dump_parent_map (per_bfd, &m_map);
3731 /* See parent-map.h. */
3733 void
3734 parent_map_map::dump (dwarf2_per_bfd *per_bfd) const
3736 for (const auto &iter : m_maps)
3738 gdb_printf (gdb_stdlog, "map start:\n");
3739 dump_parent_map (per_bfd, iter);
3743 /* An instance of this is created to index a CU. */
3745 class cooked_indexer
3747 public:
3749 cooked_indexer (cooked_index_storage *storage,
3750 dwarf2_per_cu_data *per_cu,
3751 enum language language)
3752 : m_index_storage (storage),
3753 m_per_cu (per_cu),
3754 m_language (language),
3755 m_die_range_map (storage->get_parent_map ())
3759 DISABLE_COPY_AND_ASSIGN (cooked_indexer);
3761 /* Index the given CU. */
3762 void make_index (cutu_reader *reader);
3764 private:
3766 /* A helper function to scan the PC bounds of READER and record them
3767 in the storage's addrmap. */
3768 void check_bounds (cutu_reader *reader);
3770 /* Ensure that the indicated CU exists. The cutu_reader for it is
3771 returned. FOR_SCANNING is true if the caller intends to scan all
3772 the DIEs in the CU; when false, this use is assumed to be to look
3773 up just a single DIE. */
3774 cutu_reader *ensure_cu_exists (cutu_reader *reader,
3775 dwarf2_per_objfile *per_objfile,
3776 sect_offset sect_off,
3777 bool is_dwz,
3778 bool for_scanning);
3780 /* Index DIEs in the READER starting at INFO_PTR. PARENT is
3781 the entry for the enclosing scope (nullptr at top level). FULLY
3782 is true when a full scan must be done -- in some languages,
3783 function scopes must be fully explored in order to find nested
3784 functions. This returns a pointer to just after the spot where
3785 reading stopped. */
3786 const gdb_byte *index_dies (cutu_reader *reader,
3787 const gdb_byte *info_ptr,
3788 std::variant<const cooked_index_entry *,
3789 parent_map::addr_type> parent,
3790 bool fully);
3792 /* Scan the attributes for a given DIE and update the out
3793 parameters. Returns a pointer to the byte after the DIE. */
3794 const gdb_byte *scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
3795 cutu_reader *reader,
3796 const gdb_byte *watermark_ptr,
3797 const gdb_byte *info_ptr,
3798 const abbrev_info *abbrev,
3799 const char **name,
3800 const char **linkage_name,
3801 cooked_index_flag *flags,
3802 sect_offset *sibling_offset,
3803 const cooked_index_entry **parent_entry,
3804 parent_map::addr_type *maybe_defer,
3805 bool *is_enum_class,
3806 bool for_specification);
3808 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
3809 DW_AT_import, and then scanning the referenced CU. Returns a
3810 pointer to the byte after the DIE. */
3811 const gdb_byte *index_imported_unit (cutu_reader *reader,
3812 const gdb_byte *info_ptr,
3813 const abbrev_info *abbrev);
3815 /* Recursively read DIEs, recording the section offsets in
3816 m_die_range_map and then calling index_dies. */
3817 const gdb_byte *recurse (cutu_reader *reader,
3818 const gdb_byte *info_ptr,
3819 std::variant<const cooked_index_entry *,
3820 parent_map::addr_type> parent_entry,
3821 bool fully);
3823 /* The storage object, where the results are kept. */
3824 cooked_index_storage *m_index_storage;
3825 /* The CU that we are reading on behalf of. This object might be
3826 asked to index one CU but to treat the results as if they come
3827 from some including CU; in this case the including CU would be
3828 recorded here. */
3829 dwarf2_per_cu_data *m_per_cu;
3830 /* The language that we're assuming when reading. */
3831 enum language m_language;
3833 /* Map from DIE ranges to newly-created entries. */
3834 parent_map *m_die_range_map;
3837 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3838 Process compilation unit THIS_CU for a psymtab. */
3840 static void
3841 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
3842 dwarf2_per_objfile *per_objfile,
3843 cooked_index_storage *storage)
3845 cutu_reader *reader = storage->get_reader (this_cu);
3846 if (reader == nullptr)
3848 cutu_reader new_reader (this_cu, per_objfile, nullptr, nullptr, false,
3849 &storage->get_abbrev_table_cache ());
3851 if (new_reader.comp_unit_die == nullptr || new_reader.dummy_p)
3852 return;
3854 std::unique_ptr<cutu_reader> copy
3855 (new cutu_reader (std::move (new_reader)));
3856 reader = storage->preserve (std::move (copy));
3859 if (reader->comp_unit_die == nullptr || reader->dummy_p)
3860 return;
3862 if (this_cu->is_debug_types)
3863 build_type_psymtabs_reader (reader, storage);
3864 else if (reader->comp_unit_die->tag != DW_TAG_partial_unit)
3866 bool nope = false;
3867 if (this_cu->scanned.compare_exchange_strong (nope, true))
3869 prepare_one_comp_unit (reader->cu, reader->comp_unit_die,
3870 language_minimal);
3871 gdb_assert (storage != nullptr);
3872 cooked_indexer indexer (storage, this_cu, reader->cu->lang ());
3873 indexer.make_index (reader);
3878 /* Reader function for build_type_psymtabs. */
3880 static void
3881 build_type_psymtabs_reader (cutu_reader *reader,
3882 cooked_index_storage *storage)
3884 struct dwarf2_cu *cu = reader->cu;
3885 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
3886 struct die_info *type_unit_die = reader->comp_unit_die;
3888 gdb_assert (per_cu->is_debug_types);
3890 if (! type_unit_die->has_children)
3891 return;
3893 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
3895 gdb_assert (storage != nullptr);
3896 cooked_indexer indexer (storage, per_cu, cu->lang ());
3897 indexer.make_index (reader);
3900 /* Struct used to sort TUs by their abbreviation table offset. */
3902 struct tu_abbrev_offset
3904 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
3905 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
3908 /* This is used when sorting. */
3909 bool operator< (const tu_abbrev_offset &other) const
3911 return abbrev_offset < other.abbrev_offset;
3914 signatured_type *sig_type;
3915 sect_offset abbrev_offset;
3918 /* Efficiently read all the type units.
3920 The efficiency is because we sort TUs by the abbrev table they use and
3921 only read each abbrev table once. In one program there are 200K TUs
3922 sharing 8K abbrev tables.
3924 The main purpose of this function is to support building the
3925 dwarf2_per_objfile->per_bfd->type_unit_groups table.
3926 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
3927 can collapse the search space by grouping them by stmt_list.
3928 The savings can be significant, in the same program from above the 200K TUs
3929 share 8K stmt_list tables.
3931 FUNC is expected to call get_type_unit_group, which will create the
3932 struct type_unit_group if necessary and add it to
3933 dwarf2_per_objfile->per_bfd->type_unit_groups. */
3935 static void
3936 build_type_psymtabs (dwarf2_per_objfile *per_objfile,
3937 cooked_index_storage *storage)
3939 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
3940 abbrev_table_up abbrev_table;
3941 sect_offset abbrev_offset;
3943 /* It's up to the caller to not call us multiple times. */
3944 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
3946 if (per_objfile->per_bfd->all_type_units.size () == 0)
3947 return;
3949 /* TUs typically share abbrev tables, and there can be way more TUs than
3950 abbrev tables. Sort by abbrev table to reduce the number of times we
3951 read each abbrev table in.
3952 Alternatives are to punt or to maintain a cache of abbrev tables.
3953 This is simpler and efficient enough for now.
3955 Later we group TUs by their DW_AT_stmt_list value (as this defines the
3956 symtab to use). Typically TUs with the same abbrev offset have the same
3957 stmt_list value too so in practice this should work well.
3959 The basic algorithm here is:
3961 sort TUs by abbrev table
3962 for each TU with same abbrev table:
3963 read abbrev table if first user
3964 read TU top level DIE
3965 [IWBN if DWO skeletons had DW_AT_stmt_list]
3966 call FUNC */
3968 dwarf_read_debug_printf ("Building type unit groups ...");
3970 /* Sort in a separate table to maintain the order of all_units
3971 for .gdb_index: TU indices directly index all_type_units. */
3972 std::vector<tu_abbrev_offset> sorted_by_abbrev;
3973 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
3975 for (const auto &cu : per_objfile->per_bfd->all_units)
3977 if (cu->is_debug_types)
3979 auto sig_type = static_cast<signatured_type *> (cu.get ());
3980 sorted_by_abbrev.emplace_back
3981 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
3982 sig_type->sect_off));
3986 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end ());
3988 abbrev_offset = (sect_offset) ~(unsigned) 0;
3990 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
3992 /* Switch to the next abbrev table if necessary. */
3993 if (abbrev_table == NULL
3994 || tu.abbrev_offset != abbrev_offset)
3996 abbrev_offset = tu.abbrev_offset;
3997 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
3998 abbrev_table =
3999 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
4000 ++tu_stats->nr_uniq_abbrev_tables;
4003 cutu_reader reader (tu.sig_type, per_objfile,
4004 abbrev_table.get (), nullptr, false);
4005 if (!reader.dummy_p)
4006 build_type_psymtabs_reader (&reader, storage);
4010 /* Print collected type unit statistics. */
4012 static void
4013 print_tu_stats (dwarf2_per_objfile *per_objfile)
4015 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
4017 dwarf_read_debug_printf ("Type unit statistics:");
4018 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
4019 dwarf_read_debug_printf (" %d uniq abbrev tables",
4020 tu_stats->nr_uniq_abbrev_tables);
4021 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4022 tu_stats->nr_symtabs);
4023 dwarf_read_debug_printf (" %d symtab sharers",
4024 tu_stats->nr_symtab_sharers);
4025 dwarf_read_debug_printf (" %d type units without a stmt_list",
4026 tu_stats->nr_stmt_less_type_units);
4027 dwarf_read_debug_printf (" %d all_type_units reallocs",
4028 tu_stats->nr_all_type_units_reallocs);
4031 struct skeleton_data
4033 dwarf2_per_objfile *per_objfile;
4034 cooked_index_storage *storage;
4037 /* Traversal function for process_skeletonless_type_unit.
4038 Read a TU in a DWO file and build partial symbols for it. */
4040 static int
4041 process_skeletonless_type_unit (void **slot, void *info)
4043 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
4044 skeleton_data *data = (skeleton_data *) info;
4046 /* If this TU doesn't exist in the global table, add it and read it in. */
4048 if (data->per_objfile->per_bfd->signatured_types == NULL)
4049 data->per_objfile->per_bfd->signatured_types
4050 = allocate_signatured_type_table ();
4052 signatured_type find_entry (dwo_unit->signature);
4053 slot = htab_find_slot (data->per_objfile->per_bfd->signatured_types.get (),
4054 &find_entry, INSERT);
4055 /* If we've already seen this type there's nothing to do. What's happening
4056 is we're doing our own version of comdat-folding here. */
4057 if (*slot != NULL)
4058 return 1;
4060 /* This does the job that create_all_units would have done for
4061 this TU. */
4062 signatured_type *entry
4063 = add_type_unit (data->per_objfile, dwo_unit->signature, slot);
4064 fill_in_sig_entry_from_dwo_entry (data->per_objfile, entry, dwo_unit);
4065 *slot = entry;
4067 /* This does the job that build_type_psymtabs would have done. */
4068 cutu_reader reader (entry, data->per_objfile, nullptr, nullptr, false);
4069 if (!reader.dummy_p)
4070 build_type_psymtabs_reader (&reader, data->storage);
4072 return 1;
4075 /* Traversal function for process_skeletonless_type_units. */
4077 static int
4078 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
4080 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
4082 if (dwo_file->tus != NULL)
4083 htab_traverse_noresize (dwo_file->tus.get (),
4084 process_skeletonless_type_unit, info);
4086 return 1;
4089 /* Scan all TUs of DWO files, verifying we've processed them.
4090 This is needed in case a TU was emitted without its skeleton.
4091 Note: This can't be done until we know what all the DWO files are. */
4093 static void
4094 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile,
4095 cooked_index_storage *storage)
4097 skeleton_data data { per_objfile, storage };
4099 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
4100 if (get_dwp_file (per_objfile) == NULL
4101 && per_objfile->per_bfd->dwo_files != NULL)
4103 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
4104 process_dwo_file_for_skeletonless_type_units,
4105 &data);
4109 /* A subclass of cooked_index_worker that handles scanning
4110 .debug_info. */
4112 class cooked_index_debug_info : public cooked_index_worker
4114 public:
4115 cooked_index_debug_info (dwarf2_per_objfile *per_objfile)
4116 : cooked_index_worker (per_objfile)
4118 gdb_assert (is_main_thread ());
4120 struct objfile *objfile = per_objfile->objfile;
4121 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4123 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
4124 objfile_name (objfile));
4126 per_bfd->map_info_sections (objfile);
4129 private:
4131 void do_reading () override;
4133 void print_stats () override
4135 if (dwarf_read_debug > 0)
4136 print_tu_stats (m_per_objfile);
4137 if (dwarf_read_debug > 1)
4139 dwarf_read_debug_printf_v ("Final m_all_parents_map:");
4140 m_all_parents_map.dump (m_per_objfile->per_bfd);
4144 /* After the last DWARF-reading task has finished, this function
4145 does the remaining work to finish the scan. */
4146 void done_reading ();
4148 /* An iterator for the comp units. */
4149 typedef std::vector<dwarf2_per_cu_data_up>::iterator unit_iterator;
4151 /* Process a batch of CUs. This may be called multiple times in
4152 separate threads. TASK_NUMBER indicates which task this is --
4153 the result is stored in that slot of M_RESULTS. */
4154 void process_cus (size_t task_number, unit_iterator first,
4155 unit_iterator end);
4157 /* A storage object for "leftovers" -- see the 'start' method, but
4158 essentially things not parsed during the normal CU parsing
4159 passes. */
4160 cooked_index_storage m_index_storage;
4163 void
4164 cooked_index_debug_info::process_cus (size_t task_number, unit_iterator first,
4165 unit_iterator end)
4167 SCOPE_EXIT { bfd_thread_cleanup (); };
4169 /* Ensure that complaints are handled correctly. */
4170 complaint_interceptor complaint_handler;
4172 std::vector<gdb_exception> errors;
4173 cooked_index_storage thread_storage;
4174 for (auto inner = first; inner != end; ++inner)
4176 dwarf2_per_cu_data *per_cu = inner->get ();
4179 process_psymtab_comp_unit (per_cu, m_per_objfile, &thread_storage);
4181 catch (gdb_exception &except)
4183 errors.push_back (std::move (except));
4187 m_results[task_number] = result_type (thread_storage.release (),
4188 complaint_handler.release (),
4189 std::move (errors),
4190 thread_storage.release_parent_map ());
4193 void
4194 cooked_index_debug_info::done_reading ()
4196 /* Only handle the scanning results here. Complaints and exceptions
4197 can only be dealt with on the main thread. */
4198 std::vector<std::unique_ptr<cooked_index_shard>> indexes;
4199 for (auto &one_result : m_results)
4201 indexes.push_back (std::move (std::get<0> (one_result)));
4202 m_all_parents_map.add_map (std::get<3> (one_result));
4205 /* This has to wait until we read the CUs, we need the list of DWOs. */
4206 process_skeletonless_type_units (m_per_objfile, &m_index_storage);
4208 indexes.push_back (m_index_storage.release ());
4209 indexes.shrink_to_fit ();
4211 m_all_parents_map.add_map (m_index_storage.release_parent_map ());
4213 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4214 cooked_index *table
4215 = (gdb::checked_static_cast<cooked_index *>
4216 (per_bfd->index_table.get ()));
4217 table->set_contents (std::move (indexes), &m_warnings,
4218 &m_all_parents_map);
4221 void
4222 cooked_index_debug_info::do_reading ()
4224 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
4226 create_all_units (m_per_objfile);
4227 build_type_psymtabs (m_per_objfile, &m_index_storage);
4229 per_bfd->quick_file_names_table
4230 = create_quick_file_names_table (per_bfd->all_units.size ());
4231 if (!per_bfd->debug_aranges.empty ())
4232 read_addrmap_from_aranges (m_per_objfile, &per_bfd->debug_aranges,
4233 m_index_storage.get_addrmap (),
4234 &m_warnings);
4236 /* We want to balance the load between the worker threads. This is
4237 done by using the size of each CU as a rough estimate of how
4238 difficult it will be to operate on. This isn't ideal -- for
4239 example if dwz is used, the early CUs will all tend to be
4240 "included" and won't be parsed independently. However, this
4241 heuristic works well for typical compiler output. */
4243 size_t total_size = 0;
4244 for (const auto &per_cu : per_bfd->all_units)
4245 total_size += per_cu->length ();
4247 /* How many worker threads we plan to use. We may not actually use
4248 this many. We use 1 as the minimum to avoid division by zero,
4249 and anyway in the N==0 case the work will be done
4250 synchronously. */
4251 const size_t n_worker_threads
4252 = std::max (gdb::thread_pool::g_thread_pool->thread_count (), (size_t) 1);
4254 /* How much effort should be put into each worker. */
4255 const size_t size_per_thread
4256 = std::max (total_size / n_worker_threads, (size_t) 1);
4258 /* Work is done in a task group. */
4259 gdb::task_group workers ([this] ()
4261 this->done_reading ();
4264 auto end = per_bfd->all_units.end ();
4265 size_t task_count = 0;
4266 for (auto iter = per_bfd->all_units.begin (); iter != end; )
4268 auto last = iter;
4269 /* Put all remaining CUs into the last task. */
4270 if (task_count == n_worker_threads - 1)
4271 last = end;
4272 else
4274 size_t chunk_size = 0;
4275 for (; last != end && chunk_size < size_per_thread; ++last)
4276 chunk_size += (*last)->length ();
4279 gdb_assert (iter != last);
4280 workers.add_task ([this, task_count, iter, last] ()
4282 process_cus (task_count, iter, last);
4285 ++task_count;
4286 iter = last;
4289 m_results.resize (task_count);
4290 workers.start ();
4293 static void
4294 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
4295 struct dwarf2_section_info *section,
4296 struct dwarf2_section_info *abbrev_section,
4297 unsigned int is_dwz,
4298 htab_up &types_htab,
4299 rcuh_kind section_kind)
4301 const gdb_byte *info_ptr;
4302 struct objfile *objfile = per_objfile->objfile;
4304 dwarf_read_debug_printf ("Reading %s for %s",
4305 section->get_name (),
4306 section->get_file_name ());
4308 section->read (objfile);
4310 info_ptr = section->buffer;
4312 while (info_ptr < section->buffer + section->size)
4314 dwarf2_per_cu_data_up this_cu;
4316 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
4318 comp_unit_head cu_header;
4319 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
4320 abbrev_section, info_ptr,
4321 section_kind);
4323 /* Save the compilation unit for later lookup. */
4324 if (cu_header.unit_type != DW_UT_type)
4325 this_cu = per_objfile->per_bfd->allocate_per_cu ();
4326 else
4328 if (types_htab == nullptr)
4329 types_htab = allocate_signatured_type_table ();
4331 auto sig_type = per_objfile->per_bfd->allocate_signatured_type
4332 (cu_header.signature);
4333 signatured_type *sig_ptr = sig_type.get ();
4334 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
4335 this_cu.reset (sig_type.release ());
4337 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
4338 gdb_assert (slot != nullptr);
4339 if (*slot != nullptr)
4340 complaint (_("debug type entry at offset %s is duplicate to"
4341 " the entry at offset %s, signature %s"),
4342 sect_offset_str (sect_off),
4343 sect_offset_str (sig_ptr->sect_off),
4344 hex_string (sig_ptr->signature));
4345 *slot = sig_ptr;
4347 this_cu->sect_off = sect_off;
4348 this_cu->set_length (cu_header.get_length_with_initial ());
4349 this_cu->is_dwz = is_dwz;
4350 this_cu->section = section;
4351 /* Init this asap, to avoid a data race in the set_version in
4352 cutu_reader::cutu_reader (which may be run in parallel for the cooked
4353 index case). */
4354 this_cu->set_version (cu_header.version);
4356 info_ptr = info_ptr + this_cu->length ();
4357 per_objfile->per_bfd->all_units.push_back (std::move (this_cu));
4361 /* Initialize the views on all_units. */
4363 void
4364 finalize_all_units (dwarf2_per_bfd *per_bfd)
4366 size_t nr_tus = per_bfd->tu_stats.nr_tus;
4367 size_t nr_cus = per_bfd->all_units.size () - nr_tus;
4368 gdb::array_view<dwarf2_per_cu_data_up> tmp = per_bfd->all_units;
4369 per_bfd->all_comp_units = tmp.slice (0, nr_cus);
4370 per_bfd->all_type_units = tmp.slice (nr_cus, nr_tus);
4373 /* See read.h. */
4375 void
4376 create_all_units (dwarf2_per_objfile *per_objfile)
4378 htab_up types_htab;
4379 gdb_assert (per_objfile->per_bfd->all_units.empty ());
4381 for (dwarf2_section_info &section : per_objfile->per_bfd->infos)
4382 read_comp_units_from_section (per_objfile, &section,
4383 &per_objfile->per_bfd->abbrev, 0,
4384 types_htab, rcuh_kind::COMPILE);
4385 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
4386 read_comp_units_from_section (per_objfile, &section,
4387 &per_objfile->per_bfd->abbrev, 0,
4388 types_htab, rcuh_kind::TYPE);
4390 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4391 if (dwz != NULL)
4393 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
4394 types_htab, rcuh_kind::COMPILE);
4396 if (!dwz->types.empty ())
4398 per_objfile->per_bfd->all_units.clear ();
4400 /* See enhancement PR symtab/30838. */
4401 error (_(DWARF_ERROR_PREFIX
4402 ".debug_types section not supported in dwz file"));
4406 per_objfile->per_bfd->signatured_types = std::move (types_htab);
4408 finalize_all_units (per_objfile->per_bfd);
4411 /* Return the initial uleb128 in the die at INFO_PTR. */
4413 static unsigned int
4414 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
4416 unsigned int bytes_read;
4418 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4421 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
4422 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
4424 Return the corresponding abbrev, or NULL if the number is zero (indicating
4425 an empty DIE). In either case *BYTES_READ will be set to the length of
4426 the initial number. */
4428 static const struct abbrev_info *
4429 peek_die_abbrev (const die_reader_specs &reader,
4430 const gdb_byte *info_ptr, unsigned int *bytes_read)
4432 dwarf2_cu *cu = reader.cu;
4433 bfd *abfd = reader.abfd;
4434 unsigned int abbrev_number
4435 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4437 if (abbrev_number == 0)
4438 return NULL;
4440 const abbrev_info *abbrev
4441 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4442 if (!abbrev)
4444 error (_(DWARF_ERROR_PREFIX
4445 "Could not find abbrev number %d in %s at offset %s"
4446 " [in module %s]"),
4447 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
4448 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4451 return abbrev;
4454 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4455 Returns a pointer to the end of a series of DIEs, terminated by an empty
4456 DIE. Any children of the skipped DIEs will also be skipped. */
4458 static const gdb_byte *
4459 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4461 while (1)
4463 unsigned int bytes_read;
4464 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
4465 &bytes_read);
4467 if (abbrev == NULL)
4468 return info_ptr + bytes_read;
4469 else
4470 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4474 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4475 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4476 abbrev corresponding to that skipped uleb128 should be passed in
4477 ABBREV.
4479 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
4480 returns a pointer to this DIE's sibling, skipping any children.
4481 Otherwise, returns a pointer to the DIE's first child. */
4483 static const gdb_byte *
4484 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
4485 const struct abbrev_info *abbrev, bool do_skip_children)
4487 unsigned int bytes_read;
4488 struct attribute attr;
4489 bfd *abfd = reader->abfd;
4490 struct dwarf2_cu *cu = reader->cu;
4491 const gdb_byte *buffer = reader->buffer;
4492 const gdb_byte *buffer_end = reader->buffer_end;
4493 unsigned int form, i;
4495 if (do_skip_children && abbrev->sibling_offset != (unsigned short) -1)
4497 /* We only handle DW_FORM_ref4 here. */
4498 const gdb_byte *sibling_data = info_ptr + abbrev->sibling_offset;
4499 unsigned int offset = read_4_bytes (abfd, sibling_data);
4500 const gdb_byte *sibling_ptr
4501 = buffer + to_underlying (cu->header.sect_off) + offset;
4502 if (sibling_ptr >= info_ptr && sibling_ptr < reader->buffer_end)
4503 return sibling_ptr;
4504 /* Fall through to the slow way. */
4506 else if (abbrev->size_if_constant != 0)
4508 info_ptr += abbrev->size_if_constant;
4509 if (do_skip_children && abbrev->has_children)
4510 return skip_children (reader, info_ptr);
4511 return info_ptr;
4514 for (i = 0; i < abbrev->num_attrs; i++)
4516 /* The only abbrev we care about is DW_AT_sibling. */
4517 if (do_skip_children && abbrev->attrs[i].name == DW_AT_sibling)
4519 /* Note there is no need for the extra work of
4520 "reprocessing" here, so we pass false for that
4521 argument. */
4522 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr, false);
4523 if (attr.form == DW_FORM_ref_addr)
4524 complaint (_("ignoring absolute DW_AT_sibling"));
4525 else
4527 sect_offset off = attr.get_ref_die_offset ();
4528 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
4530 if (sibling_ptr < info_ptr)
4531 complaint (_("DW_AT_sibling points backwards"));
4532 else if (sibling_ptr > reader->buffer_end)
4533 reader->die_section->overflow_complaint ();
4534 else
4535 return sibling_ptr;
4539 /* If it isn't DW_AT_sibling, skip this attribute. */
4540 form = abbrev->attrs[i].form;
4541 skip_attribute:
4542 switch (form)
4544 case DW_FORM_ref_addr:
4545 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4546 and later it is offset sized. */
4547 if (cu->header.version == 2)
4548 info_ptr += cu->header.addr_size;
4549 else
4550 info_ptr += cu->header.offset_size;
4551 break;
4552 case DW_FORM_GNU_ref_alt:
4553 info_ptr += cu->header.offset_size;
4554 break;
4555 case DW_FORM_addr:
4556 info_ptr += cu->header.addr_size;
4557 break;
4558 case DW_FORM_data1:
4559 case DW_FORM_ref1:
4560 case DW_FORM_flag:
4561 case DW_FORM_strx1:
4562 info_ptr += 1;
4563 break;
4564 case DW_FORM_flag_present:
4565 case DW_FORM_implicit_const:
4566 break;
4567 case DW_FORM_data2:
4568 case DW_FORM_ref2:
4569 case DW_FORM_strx2:
4570 info_ptr += 2;
4571 break;
4572 case DW_FORM_strx3:
4573 info_ptr += 3;
4574 break;
4575 case DW_FORM_data4:
4576 case DW_FORM_ref4:
4577 case DW_FORM_strx4:
4578 info_ptr += 4;
4579 break;
4580 case DW_FORM_data8:
4581 case DW_FORM_ref8:
4582 case DW_FORM_ref_sig8:
4583 info_ptr += 8;
4584 break;
4585 case DW_FORM_data16:
4586 info_ptr += 16;
4587 break;
4588 case DW_FORM_string:
4589 read_direct_string (abfd, info_ptr, &bytes_read);
4590 info_ptr += bytes_read;
4591 break;
4592 case DW_FORM_sec_offset:
4593 case DW_FORM_strp:
4594 case DW_FORM_GNU_strp_alt:
4595 info_ptr += cu->header.offset_size;
4596 break;
4597 case DW_FORM_exprloc:
4598 case DW_FORM_block:
4599 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4600 info_ptr += bytes_read;
4601 break;
4602 case DW_FORM_block1:
4603 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4604 break;
4605 case DW_FORM_block2:
4606 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4607 break;
4608 case DW_FORM_block4:
4609 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4610 break;
4611 case DW_FORM_addrx:
4612 case DW_FORM_strx:
4613 case DW_FORM_sdata:
4614 case DW_FORM_udata:
4615 case DW_FORM_ref_udata:
4616 case DW_FORM_GNU_addr_index:
4617 case DW_FORM_GNU_str_index:
4618 case DW_FORM_rnglistx:
4619 case DW_FORM_loclistx:
4620 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4621 break;
4622 case DW_FORM_indirect:
4623 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4624 info_ptr += bytes_read;
4625 /* We need to continue parsing from here, so just go back to
4626 the top. */
4627 goto skip_attribute;
4629 default:
4630 error (_(DWARF_ERROR_PREFIX
4631 "Cannot handle %s in DWARF reader [in module %s]"),
4632 dwarf_form_name (form),
4633 bfd_get_filename (abfd));
4637 if (do_skip_children && abbrev->has_children)
4638 return skip_children (reader, info_ptr);
4639 else
4640 return info_ptr;
4643 /* Reading in full CUs. */
4645 /* Add PER_CU to the queue. */
4647 static void
4648 queue_comp_unit (dwarf2_per_cu_data *per_cu,
4649 dwarf2_per_objfile *per_objfile,
4650 enum language pretend_language)
4652 per_cu->queued = 1;
4654 gdb_assert (per_objfile->queue.has_value ());
4655 per_objfile->queue->emplace (per_cu, per_objfile, pretend_language);
4658 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
4660 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
4661 dependency.
4663 Return true if maybe_queue_comp_unit requires the caller to load the CU's
4664 DIEs, false otherwise.
4666 Explanation: there is an invariant that if a CU is queued for expansion
4667 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
4668 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
4669 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
4670 are not yet loaded, the the caller must load the CU's DIEs to ensure the
4671 invariant is respected.
4673 The caller is therefore not required to load the CU's DIEs (we return false)
4676 - the CU is already expanded, and therefore does not get enqueued
4677 - the CU gets enqueued for expansion, but its DIEs are already loaded
4679 Note that the caller should not use this function's return value as an
4680 indicator of whether the CU's DIEs are loaded right now, it should check
4681 that by calling `dwarf2_per_objfile::get_cu` instead. */
4683 static int
4684 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
4685 dwarf2_per_cu_data *per_cu,
4686 dwarf2_per_objfile *per_objfile,
4687 enum language pretend_language)
4689 /* Mark the dependence relation so that we don't flush PER_CU
4690 too early. */
4691 if (dependent_cu != NULL)
4692 dependent_cu->add_dependence (per_cu);
4694 /* If it's already on the queue, we have nothing to do. */
4695 if (per_cu->queued)
4697 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
4698 loaded. */
4699 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
4701 /* If the CU is queued for expansion, it should not already be
4702 expanded. */
4703 gdb_assert (!per_objfile->symtab_set_p (per_cu));
4705 /* The DIEs are already loaded, the caller doesn't need to do it. */
4706 return 0;
4709 bool queued = false;
4710 if (!per_objfile->symtab_set_p (per_cu))
4712 /* Add it to the queue. */
4713 queue_comp_unit (per_cu, per_objfile, pretend_language);
4714 queued = true;
4717 /* If the compilation unit is already loaded, just mark it as
4718 used. */
4719 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
4720 if (cu != nullptr)
4721 cu->last_used = 0;
4723 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
4724 and the DIEs are not already loaded. */
4725 return queued && cu == nullptr;
4728 /* Process the queue. */
4730 static void
4731 process_queue (dwarf2_per_objfile *per_objfile)
4733 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
4734 objfile_name (per_objfile->objfile));
4736 /* The queue starts out with one item, but following a DIE reference
4737 may load a new CU, adding it to the end of the queue. */
4738 while (!per_objfile->queue->empty ())
4740 dwarf2_queue_item &item = per_objfile->queue->front ();
4741 dwarf2_per_cu_data *per_cu = item.per_cu;
4743 if (!per_objfile->symtab_set_p (per_cu))
4745 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
4747 /* Skip dummy CUs. */
4748 if (cu != nullptr)
4750 unsigned int debug_print_threshold;
4751 char buf[100];
4753 if (per_cu->is_debug_types)
4755 struct signatured_type *sig_type =
4756 (struct signatured_type *) per_cu;
4758 sprintf (buf, "TU %s at offset %s",
4759 hex_string (sig_type->signature),
4760 sect_offset_str (per_cu->sect_off));
4761 /* There can be 100s of TUs.
4762 Only print them in verbose mode. */
4763 debug_print_threshold = 2;
4765 else
4767 sprintf (buf, "CU at offset %s",
4768 sect_offset_str (per_cu->sect_off));
4769 debug_print_threshold = 1;
4772 if (dwarf_read_debug >= debug_print_threshold)
4773 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
4775 if (per_cu->is_debug_types)
4776 process_full_type_unit (cu, item.pretend_language);
4777 else
4778 process_full_comp_unit (cu, item.pretend_language);
4780 if (dwarf_read_debug >= debug_print_threshold)
4781 dwarf_read_debug_printf ("Done expanding %s", buf);
4785 per_cu->queued = 0;
4786 per_objfile->queue->pop ();
4789 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
4790 objfile_name (per_objfile->objfile));
4793 /* Load the DIEs associated with PER_CU into memory.
4795 In some cases, the caller, while reading partial symbols, will need to load
4796 the full symbols for the CU for some reason. It will already have a
4797 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
4798 rather than creating a new one. */
4800 static void
4801 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
4802 dwarf2_per_objfile *per_objfile,
4803 dwarf2_cu *existing_cu,
4804 bool skip_partial,
4805 enum language pretend_language)
4807 gdb_assert (! this_cu->is_debug_types);
4809 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
4810 if (reader.dummy_p)
4811 return;
4813 struct dwarf2_cu *cu = reader.cu;
4814 const gdb_byte *info_ptr = reader.info_ptr;
4816 gdb_assert (cu->die_hash.empty ());
4817 cu->die_hash.reserve (cu->header.get_length_without_initial () / 12);
4819 if (reader.comp_unit_die->has_children)
4820 reader.comp_unit_die->child
4821 = read_die_and_siblings (&reader, reader.info_ptr,
4822 &info_ptr, reader.comp_unit_die);
4823 cu->dies = reader.comp_unit_die;
4824 /* comp_unit_die is not stored in die_hash, no need. */
4826 /* We try not to read any attributes in this function, because not
4827 all CUs needed for references have been loaded yet, and symbol
4828 table processing isn't initialized. But we have to set the CU language,
4829 or we won't be able to build types correctly.
4830 Similarly, if we do not read the producer, we can not apply
4831 producer-specific interpretation. */
4832 prepare_one_comp_unit (cu, cu->dies, pretend_language);
4834 reader.keep ();
4837 /* Add a DIE to the delayed physname list. */
4839 static void
4840 add_to_method_list (struct type *type, int fnfield_index, int index,
4841 const char *name, struct die_info *die,
4842 struct dwarf2_cu *cu)
4844 struct delayed_method_info mi;
4845 mi.type = type;
4846 mi.fnfield_index = fnfield_index;
4847 mi.index = index;
4848 mi.name = name;
4849 mi.die = die;
4850 cu->method_list.push_back (mi);
4853 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
4854 "const" / "volatile". If so, decrements LEN by the length of the
4855 modifier and return true. Otherwise return false. */
4857 template<size_t N>
4858 static bool
4859 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
4861 size_t mod_len = sizeof (mod) - 1;
4862 if (len > mod_len && startswith (physname + (len - mod_len), mod))
4864 len -= mod_len;
4865 return true;
4867 return false;
4870 /* Compute the physnames of any methods on the CU's method list.
4872 The computation of method physnames is delayed in order to avoid the
4873 (bad) condition that one of the method's formal parameters is of an as yet
4874 incomplete type. */
4876 static void
4877 compute_delayed_physnames (struct dwarf2_cu *cu)
4879 /* Only C++ delays computing physnames. */
4880 if (cu->method_list.empty ())
4881 return;
4882 gdb_assert (cu->lang () == language_cplus);
4884 for (const delayed_method_info &mi : cu->method_list)
4886 const char *physname;
4887 struct fn_fieldlist *fn_flp
4888 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
4889 physname = dwarf2_physname (mi.name, mi.die, cu);
4890 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
4891 = physname ? physname : "";
4893 /* Since there's no tag to indicate whether a method is a
4894 const/volatile overload, extract that information out of the
4895 demangled name. */
4896 if (physname != NULL)
4898 size_t len = strlen (physname);
4900 while (1)
4902 if (physname[len - 1] == ')') /* shortcut */
4903 break;
4904 else if (check_modifier (physname, len, " const"))
4905 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
4906 else if (check_modifier (physname, len, " volatile"))
4907 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
4908 else
4909 break;
4914 /* The list is no longer needed. */
4915 cu->method_list.clear ();
4918 /* Go objects should be embedded in a DW_TAG_module DIE,
4919 and it's not clear if/how imported objects will appear.
4920 To keep Go support simple until that's worked out,
4921 go back through what we've read and create something usable.
4922 We could do this while processing each DIE, and feels kinda cleaner,
4923 but that way is more invasive.
4924 This is to, for example, allow the user to type "p var" or "b main"
4925 without having to specify the package name, and allow lookups
4926 of module.object to work in contexts that use the expression
4927 parser. */
4929 static void
4930 fixup_go_packaging (struct dwarf2_cu *cu)
4932 gdb::unique_xmalloc_ptr<char> package_name;
4933 struct pending *list;
4934 int i;
4936 for (list = *cu->get_builder ()->get_global_symbols ();
4937 list != NULL;
4938 list = list->next)
4940 for (i = 0; i < list->nsyms; ++i)
4942 struct symbol *sym = list->symbol[i];
4944 if (sym->language () == language_go
4945 && sym->aclass () == LOC_BLOCK)
4947 gdb::unique_xmalloc_ptr<char> this_package_name
4948 = go_symbol_package_name (sym);
4950 if (this_package_name == NULL)
4951 continue;
4952 if (package_name == NULL)
4953 package_name = std::move (this_package_name);
4954 else
4956 struct objfile *objfile = cu->per_objfile->objfile;
4957 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
4958 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
4959 (sym->symtab () != NULL
4960 ? symtab_to_filename_for_display
4961 (sym->symtab ())
4962 : objfile_name (objfile)),
4963 this_package_name.get (), package_name.get ());
4969 if (package_name != NULL)
4971 struct objfile *objfile = cu->per_objfile->objfile;
4972 const char *saved_package_name = objfile->intern (package_name.get ());
4973 struct type *type
4974 = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_MODULE, 0,
4975 saved_package_name);
4976 struct symbol *sym;
4978 sym = new (&objfile->objfile_obstack) symbol;
4979 sym->set_language (language_go, &objfile->objfile_obstack);
4980 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
4981 sym->set_domain (TYPE_DOMAIN);
4982 sym->set_aclass_index (LOC_TYPEDEF);
4983 sym->set_type (type);
4985 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
4989 /* Allocate a fully-qualified name consisting of the two parts on the
4990 obstack. */
4992 static const char *
4993 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
4995 return obconcat (obstack, p1, "::", p2, (char *) NULL);
4998 /* A helper that allocates a variant part to attach to a Rust enum
4999 type. OBSTACK is where the results should be allocated. TYPE is
5000 the type we're processing. DISCRIMINANT_INDEX is the index of the
5001 discriminant. It must be the index of one of the fields of TYPE,
5002 or -1 to mean there is no discriminant (univariant enum).
5003 DEFAULT_INDEX is the index of the default field; or -1 if there is
5004 no default. RANGES is indexed by "effective" field number (the
5005 field index, but omitting the discriminant and default fields) and
5006 must hold the discriminant values used by the variants. Note that
5007 RANGES must have a lifetime at least as long as OBSTACK -- either
5008 already allocated on it, or static. */
5010 static void
5011 alloc_rust_variant (struct obstack *obstack, struct type *type,
5012 int discriminant_index, int default_index,
5013 gdb::array_view<discriminant_range> ranges)
5015 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5016 gdb_assert (discriminant_index == -1
5017 || (discriminant_index >= 0
5018 && discriminant_index < type->num_fields ()));
5019 gdb_assert (default_index == -1
5020 || (default_index >= 0 && default_index < type->num_fields ()));
5022 /* We have one variant for each non-discriminant field. */
5023 int n_variants = type->num_fields ();
5024 if (discriminant_index != -1)
5025 --n_variants;
5027 variant *variants = new (obstack) variant[n_variants];
5028 int var_idx = 0;
5029 int range_idx = 0;
5030 for (int i = 0; i < type->num_fields (); ++i)
5032 if (i == discriminant_index)
5033 continue;
5035 variants[var_idx].first_field = i;
5036 variants[var_idx].last_field = i + 1;
5038 /* The default field does not need a range, but other fields do.
5039 We skipped the discriminant above. */
5040 if (i != default_index)
5042 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
5043 ++range_idx;
5046 ++var_idx;
5049 gdb_assert (range_idx == ranges.size ());
5050 gdb_assert (var_idx == n_variants);
5052 variant_part *part = new (obstack) variant_part;
5053 part->discriminant_index = discriminant_index;
5054 /* If there is no discriminant, then whether it is signed is of no
5055 consequence. */
5056 part->is_unsigned
5057 = (discriminant_index == -1
5058 ? false
5059 : type->field (discriminant_index).type ()->is_unsigned ());
5060 part->variants = gdb::array_view<variant> (variants, n_variants);
5062 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
5063 gdb::array_view<variant_part> *prop_value
5064 = new (storage) gdb::array_view<variant_part> (part, 1);
5066 struct dynamic_prop prop;
5067 prop.set_variant_parts (prop_value);
5069 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
5072 /* Some versions of rustc emitted enums in an unusual way.
5074 Ordinary enums were emitted as unions. The first element of each
5075 structure in the union was named "RUST$ENUM$DISR". This element
5076 held the discriminant.
5078 These versions of Rust also implemented the "non-zero"
5079 optimization. When the enum had two values, and one is empty and
5080 the other holds a pointer that cannot be zero, the pointer is used
5081 as the discriminant, with a zero value meaning the empty variant.
5082 Here, the union's first member is of the form
5083 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5084 where the fieldnos are the indices of the fields that should be
5085 traversed in order to find the field (which may be several fields deep)
5086 and the variantname is the name of the variant of the case when the
5087 field is zero.
5089 This function recognizes whether TYPE is of one of these forms,
5090 and, if so, smashes it to be a variant type. */
5092 static void
5093 quirk_rust_enum (struct type *type, struct objfile *objfile)
5095 gdb_assert (type->code () == TYPE_CODE_UNION);
5097 /* We don't need to deal with empty enums. */
5098 if (type->num_fields () == 0)
5099 return;
5101 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5102 if (type->num_fields () == 1
5103 && startswith (type->field (0).name (), RUST_ENUM_PREFIX))
5105 const char *name = type->field (0).name () + strlen (RUST_ENUM_PREFIX);
5107 /* Decode the field name to find the offset of the
5108 discriminant. */
5109 ULONGEST bit_offset = 0;
5110 struct type *field_type = type->field (0).type ();
5111 while (name[0] >= '0' && name[0] <= '9')
5113 char *tail;
5114 unsigned long index = strtoul (name, &tail, 10);
5115 name = tail;
5116 if (*name != '$'
5117 || index >= field_type->num_fields ()
5118 || (field_type->field (index).loc_kind ()
5119 != FIELD_LOC_KIND_BITPOS))
5121 complaint (_("Could not parse Rust enum encoding string \"%s\""
5122 "[in module %s]"),
5123 type->field (0).name (),
5124 objfile_name (objfile));
5125 return;
5127 ++name;
5129 bit_offset += field_type->field (index).loc_bitpos ();
5130 field_type = field_type->field (index).type ();
5133 /* Smash this type to be a structure type. We have to do this
5134 because the type has already been recorded. */
5135 type->set_code (TYPE_CODE_STRUCT);
5136 /* Save the field we care about. */
5137 struct field saved_field = type->field (0);
5138 type->alloc_fields (3);
5140 /* Put the discriminant at index 0. */
5141 type->field (0).set_type (field_type);
5142 type->field (0).set_is_artificial (true);
5143 type->field (0).set_name ("<<discriminant>>");
5144 type->field (0).set_loc_bitpos (bit_offset);
5146 /* The order of fields doesn't really matter, so put the real
5147 field at index 1 and the data-less field at index 2. */
5148 type->field (1) = saved_field;
5149 type->field (1).set_name
5150 (rust_last_path_segment (type->field (1).type ()->name ()));
5151 type->field (1).type ()->set_name
5152 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
5153 type->field (1).name ()));
5155 const char *dataless_name
5156 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
5157 name);
5158 struct type *dataless_type
5159 = type_allocator (type).new_type (TYPE_CODE_VOID, 0,
5160 dataless_name);
5161 type->field (2).set_type (dataless_type);
5162 /* NAME points into the original discriminant name, which
5163 already has the correct lifetime. */
5164 type->field (2).set_name (name);
5165 type->field (2).set_loc_bitpos (0);
5167 /* Indicate that this is a variant type. */
5168 static discriminant_range ranges[1] = { { 0, 0 } };
5169 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
5171 /* A union with a single anonymous field is probably an old-style
5172 univariant enum. */
5173 else if (type->num_fields () == 1 && streq (type->field (0).name (), ""))
5175 /* Smash this type to be a structure type. We have to do this
5176 because the type has already been recorded. */
5177 type->set_code (TYPE_CODE_STRUCT);
5179 struct type *field_type = type->field (0).type ();
5180 const char *variant_name
5181 = rust_last_path_segment (field_type->name ());
5182 type->field (0).set_name (variant_name);
5183 field_type->set_name
5184 (rust_fully_qualify (&objfile->objfile_obstack,
5185 type->name (), variant_name));
5187 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
5189 else
5191 struct type *disr_type = nullptr;
5192 for (int i = 0; i < type->num_fields (); ++i)
5194 disr_type = type->field (i).type ();
5196 if (disr_type->code () != TYPE_CODE_STRUCT)
5198 /* All fields of a true enum will be structs. */
5199 return;
5201 else if (disr_type->num_fields () == 0)
5203 /* Could be data-less variant, so keep going. */
5204 disr_type = nullptr;
5206 else if (strcmp (disr_type->field (0).name (),
5207 "RUST$ENUM$DISR") != 0)
5209 /* Not a Rust enum. */
5210 return;
5212 else
5214 /* Found one. */
5215 break;
5219 /* If we got here without a discriminant, then it's probably
5220 just a union. */
5221 if (disr_type == nullptr)
5222 return;
5224 /* Smash this type to be a structure type. We have to do this
5225 because the type has already been recorded. */
5226 type->set_code (TYPE_CODE_STRUCT);
5228 /* Make space for the discriminant field. */
5229 struct field *disr_field = &disr_type->field (0);
5230 field *new_fields
5231 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
5232 * sizeof (struct field)));
5233 memcpy (new_fields + 1, type->fields (),
5234 type->num_fields () * sizeof (struct field));
5235 type->set_fields (new_fields);
5236 type->set_num_fields (type->num_fields () + 1);
5238 /* Install the discriminant at index 0 in the union. */
5239 type->field (0) = *disr_field;
5240 type->field (0).set_is_artificial (true);
5241 type->field (0).set_name ("<<discriminant>>");
5243 /* We need a way to find the correct discriminant given a
5244 variant name. For convenience we build a map here. */
5245 struct type *enum_type = disr_field->type ();
5246 std::unordered_map<std::string_view, ULONGEST> discriminant_map;
5247 for (int i = 0; i < enum_type->num_fields (); ++i)
5249 if (enum_type->field (i).loc_kind () == FIELD_LOC_KIND_ENUMVAL)
5251 const char *name
5252 = rust_last_path_segment (enum_type->field (i).name ());
5253 discriminant_map[name] = enum_type->field (i).loc_enumval ();
5257 int n_fields = type->num_fields ();
5258 /* We don't need a range entry for the discriminant, but we do
5259 need one for every other field, as there is no default
5260 variant. */
5261 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
5262 discriminant_range,
5263 n_fields - 1);
5264 /* Skip the discriminant here. */
5265 for (int i = 1; i < n_fields; ++i)
5267 /* Find the final word in the name of this variant's type.
5268 That name can be used to look up the correct
5269 discriminant. */
5270 const char *variant_name
5271 = rust_last_path_segment (type->field (i).type ()->name ());
5273 auto iter = discriminant_map.find (variant_name);
5274 if (iter != discriminant_map.end ())
5276 ranges[i - 1].low = iter->second;
5277 ranges[i - 1].high = iter->second;
5280 /* In Rust, each element should have the size of the
5281 enclosing enum. */
5282 type->field (i).type ()->set_length (type->length ());
5284 /* Remove the discriminant field, if it exists. */
5285 struct type *sub_type = type->field (i).type ();
5286 if (sub_type->num_fields () > 0)
5288 sub_type->set_num_fields (sub_type->num_fields () - 1);
5289 sub_type->set_fields (sub_type->fields () + 1);
5291 type->field (i).set_name (variant_name);
5292 sub_type->set_name
5293 (rust_fully_qualify (&objfile->objfile_obstack,
5294 type->name (), variant_name));
5297 /* Indicate that this is a variant type. */
5298 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
5299 gdb::array_view<discriminant_range> (ranges,
5300 n_fields - 1));
5304 /* Rewrite some Rust unions to be structures with variants parts. */
5306 static void
5307 rust_union_quirks (struct dwarf2_cu *cu)
5309 gdb_assert (cu->lang () == language_rust);
5310 for (type *type_ : cu->rust_unions)
5311 quirk_rust_enum (type_, cu->per_objfile->objfile);
5312 /* We don't need this any more. */
5313 cu->rust_unions.clear ();
5316 /* See read.h. */
5318 type_unit_group_unshareable *
5319 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
5321 auto iter = this->m_type_units.find (tu_group);
5322 if (iter != this->m_type_units.end ())
5323 return iter->second.get ();
5325 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
5326 type_unit_group_unshareable *result = uniq.get ();
5327 this->m_type_units[tu_group] = std::move (uniq);
5328 return result;
5331 struct type *
5332 dwarf2_per_objfile::get_type_for_signatured_type
5333 (signatured_type *sig_type) const
5335 auto iter = this->m_type_map.find (sig_type);
5336 if (iter == this->m_type_map.end ())
5337 return nullptr;
5339 return iter->second;
5342 void dwarf2_per_objfile::set_type_for_signatured_type
5343 (signatured_type *sig_type, struct type *type)
5345 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
5347 this->m_type_map[sig_type] = type;
5350 /* A helper function for computing the list of all symbol tables
5351 included by PER_CU. */
5353 static void
5354 recursively_compute_inclusions
5355 (std::vector<compunit_symtab *> *result,
5356 gdb::unordered_set<dwarf2_per_cu_data *> &all_children,
5357 gdb::unordered_set<compunit_symtab *> &all_type_symtabs,
5358 dwarf2_per_cu_data *per_cu,
5359 dwarf2_per_objfile *per_objfile,
5360 struct compunit_symtab *immediate_parent)
5362 if (bool inserted = all_children.emplace (per_cu).second;
5363 !inserted)
5365 /* This inclusion and its children have been processed. */
5366 return;
5369 /* Only add a CU if it has a symbol table. */
5370 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
5371 if (cust != NULL)
5373 /* If this is a type unit only add its symbol table if we haven't
5374 seen it yet (type unit per_cu's can share symtabs). */
5375 if (per_cu->is_debug_types)
5377 if (bool inserted = all_type_symtabs.insert (cust).second;
5378 inserted)
5380 result->push_back (cust);
5381 if (cust->user == NULL)
5382 cust->user = immediate_parent;
5385 else
5387 result->push_back (cust);
5388 if (cust->user == NULL)
5389 cust->user = immediate_parent;
5393 for (dwarf2_per_cu_data *ptr : per_cu->imported_symtabs)
5394 recursively_compute_inclusions (result, all_children,
5395 all_type_symtabs, ptr, per_objfile,
5396 cust);
5399 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
5400 PER_CU. */
5402 static void
5403 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
5404 dwarf2_per_objfile *per_objfile)
5406 gdb_assert (! per_cu->is_debug_types);
5408 if (!per_cu->imported_symtabs.empty ())
5410 int len;
5411 std::vector<compunit_symtab *> result_symtabs;
5412 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
5414 /* If we don't have a symtab, we can just skip this case. */
5415 if (cust == NULL)
5416 return;
5418 gdb::unordered_set<dwarf2_per_cu_data *> all_children;
5419 gdb::unordered_set<compunit_symtab *> all_type_symtabs;
5421 for (dwarf2_per_cu_data *ptr : per_cu->imported_symtabs)
5422 recursively_compute_inclusions (&result_symtabs, all_children,
5423 all_type_symtabs, ptr,
5424 per_objfile, cust);
5426 /* Now we have a transitive closure of all the included symtabs. */
5427 len = result_symtabs.size ();
5428 cust->includes
5429 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
5430 struct compunit_symtab *, len + 1);
5431 memcpy (cust->includes, result_symtabs.data (),
5432 len * sizeof (compunit_symtab *));
5433 cust->includes[len] = NULL;
5437 /* Compute the 'includes' field for the symtabs of all the CUs we just
5438 read. */
5440 static void
5441 process_cu_includes (dwarf2_per_objfile *per_objfile)
5443 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
5445 if (! iter->is_debug_types)
5446 compute_compunit_symtab_includes (iter, per_objfile);
5449 per_objfile->per_bfd->just_read_cus.clear ();
5452 /* Generate full symbol information for CU, whose DIEs have
5453 already been loaded into memory. */
5455 static void
5456 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
5458 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5459 unrelocated_addr lowpc, highpc;
5460 struct compunit_symtab *cust;
5461 struct block *static_block;
5462 CORE_ADDR addr;
5464 /* Clear the list here in case something was left over. */
5465 cu->method_list.clear ();
5467 dwarf2_find_base_address (cu->dies, cu);
5469 /* Before we start reading the top-level DIE, ensure it has a valid tag
5470 type. */
5471 switch (cu->dies->tag)
5473 case DW_TAG_compile_unit:
5474 case DW_TAG_partial_unit:
5475 case DW_TAG_type_unit:
5476 break;
5477 default:
5478 error (_(DWARF_ERROR_PREFIX
5479 "unexpected tag '%s' at offset %s [in module %s]"),
5480 dwarf_tag_name (cu->dies->tag),
5481 sect_offset_str (cu->per_cu->sect_off),
5482 objfile_name (per_objfile->objfile));
5485 /* Do line number decoding in read_file_scope () */
5486 process_die (cu->dies, cu);
5488 /* For now fudge the Go package. */
5489 if (cu->lang () == language_go)
5490 fixup_go_packaging (cu);
5492 /* Now that we have processed all the DIEs in the CU, all the types
5493 should be complete, and it should now be safe to compute all of the
5494 physnames. */
5495 compute_delayed_physnames (cu);
5497 if (cu->lang () == language_rust)
5498 rust_union_quirks (cu);
5500 /* Some compilers don't define a DW_AT_high_pc attribute for the
5501 compilation unit. If the DW_AT_high_pc is missing, synthesize
5502 it, by scanning the DIE's below the compilation unit. */
5503 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
5505 addr = per_objfile->relocate (highpc);
5506 static_block
5507 = cu->get_builder ()->end_compunit_symtab_get_static_block (addr, 0, 1);
5509 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
5510 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
5511 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
5512 addrmap to help ensure it has an accurate map of pc values belonging to
5513 this comp unit. */
5514 dwarf2_record_block_ranges (cu->dies, static_block, cu);
5516 cust = cu->get_builder ()->end_compunit_symtab_from_static_block
5517 (static_block, 0);
5519 if (cust != NULL)
5521 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
5523 /* Set symtab language to language from DW_AT_language. If the
5524 compilation is from a C file generated by language preprocessors, do
5525 not set the language if it was already deduced by start_subfile. */
5526 if (!(cu->lang () == language_c
5527 && cust->primary_filetab ()->language () != language_unknown))
5528 cust->primary_filetab ()->set_language (cu->lang ());
5530 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5531 produce DW_AT_location with location lists but it can be possibly
5532 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5533 there were bugs in prologue debug info, fixed later in GCC-4.5
5534 by "unwind info for epilogues" patch (which is not directly related).
5536 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5537 needed, it would be wrong due to missing DW_AT_producer there.
5539 Still one can confuse GDB by using non-standard GCC compilation
5540 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5542 if (cu->has_loclist && gcc_4_minor >= 5)
5543 cust->set_locations_valid (true);
5545 int major, minor;
5546 if (cu->producer != nullptr
5547 && producer_is_gcc (cu->producer, &major, &minor)
5548 && (major < 4 || (major == 4 && minor < 5)))
5549 /* Don't trust gcc < 4.5.x. */
5550 cust->set_epilogue_unwind_valid (false);
5551 else
5552 cust->set_epilogue_unwind_valid (true);
5554 cust->set_call_site_htab (std::move (cu->call_site_htab));
5557 per_objfile->set_symtab (cu->per_cu, cust);
5559 /* Push it for inclusion processing later. */
5560 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
5562 /* Not needed any more. */
5563 cu->reset_builder ();
5566 /* Generate full symbol information for type unit CU, whose DIEs have
5567 already been loaded into memory. */
5569 static void
5570 process_full_type_unit (dwarf2_cu *cu,
5571 enum language pretend_language)
5573 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5574 struct compunit_symtab *cust;
5575 struct signatured_type *sig_type;
5577 gdb_assert (cu->per_cu->is_debug_types);
5578 sig_type = (struct signatured_type *) cu->per_cu;
5580 /* Clear the list here in case something was left over. */
5581 cu->method_list.clear ();
5583 /* The symbol tables are set up in read_type_unit_scope. */
5584 process_die (cu->dies, cu);
5586 /* For now fudge the Go package. */
5587 if (cu->lang () == language_go)
5588 fixup_go_packaging (cu);
5590 /* Now that we have processed all the DIEs in the CU, all the types
5591 should be complete, and it should now be safe to compute all of the
5592 physnames. */
5593 compute_delayed_physnames (cu);
5595 if (cu->lang () == language_rust)
5596 rust_union_quirks (cu);
5598 /* TUs share symbol tables.
5599 If this is the first TU to use this symtab, complete the construction
5600 of it with end_expandable_symtab. Otherwise, complete the addition of
5601 this TU's symbols to the existing symtab. */
5602 type_unit_group_unshareable *tug_unshare =
5603 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
5604 if (tug_unshare->compunit_symtab == NULL)
5606 buildsym_compunit *builder = cu->get_builder ();
5607 cust = builder->end_expandable_symtab (0);
5608 tug_unshare->compunit_symtab = cust;
5610 if (cust != NULL)
5612 /* Set symtab language to language from DW_AT_language. If the
5613 compilation is from a C file generated by language preprocessors,
5614 do not set the language if it was already deduced by
5615 start_subfile. */
5616 if (!(cu->lang () == language_c
5617 && cust->primary_filetab ()->language () != language_c))
5618 cust->primary_filetab ()->set_language (cu->lang ());
5621 else
5623 cu->get_builder ()->augment_type_symtab ();
5624 cust = tug_unshare->compunit_symtab;
5627 per_objfile->set_symtab (cu->per_cu, cust);
5629 /* Not needed any more. */
5630 cu->reset_builder ();
5633 /* Process an imported unit DIE. */
5635 static void
5636 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5638 struct attribute *attr;
5640 /* For now we don't handle imported units in type units. */
5641 if (cu->per_cu->is_debug_types)
5643 error (_(DWARF_ERROR_PREFIX
5644 "DW_TAG_imported_unit is not supported in type units"
5645 " [in module %s]"),
5646 objfile_name (cu->per_objfile->objfile));
5649 attr = dwarf2_attr (die, DW_AT_import, cu);
5650 if (attr != NULL)
5652 sect_offset sect_off = attr->get_ref_die_offset ();
5653 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
5654 dwarf2_per_objfile *per_objfile = cu->per_objfile;
5655 dwarf2_per_cu_data *per_cu
5656 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
5657 per_objfile->per_bfd);
5659 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
5660 into another compilation unit, at root level. Regard this as a hint,
5661 and ignore it. This is a best effort, it only works if unit_type and
5662 lang are already set. */
5663 if (die->parent && die->parent->parent == NULL
5664 && per_cu->unit_type (false) == DW_UT_compile
5665 && per_cu->lang (false) == language_cplus)
5666 return;
5668 /* If necessary, add it to the queue and load its DIEs. */
5669 if (maybe_queue_comp_unit (cu, per_cu, per_objfile,
5670 cu->lang ()))
5671 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
5672 false, cu->lang ());
5674 cu->per_cu->imported_symtabs.push_back (per_cu);
5678 /* RAII object that represents a process_die scope: i.e.,
5679 starts/finishes processing a DIE. */
5680 class process_die_scope
5682 public:
5683 process_die_scope (die_info *die, dwarf2_cu *cu)
5684 : m_die (die), m_cu (cu)
5686 /* We should only be processing DIEs not already in process. */
5687 gdb_assert (!m_die->in_process);
5688 m_die->in_process = true;
5691 ~process_die_scope ()
5693 m_die->in_process = false;
5695 /* If we're done processing the DIE for the CU that owns the line
5696 header, we don't need the line header anymore. */
5697 if (m_cu->line_header_die_owner == m_die)
5699 delete m_cu->line_header;
5700 m_cu->line_header = NULL;
5701 m_cu->line_header_die_owner = NULL;
5705 private:
5706 die_info *m_die;
5707 dwarf2_cu *m_cu;
5710 /* Process a die and its children. */
5712 static void
5713 process_die (struct die_info *die, struct dwarf2_cu *cu)
5715 process_die_scope scope (die, cu);
5717 switch (die->tag)
5719 case DW_TAG_padding:
5720 break;
5721 case DW_TAG_compile_unit:
5722 case DW_TAG_partial_unit:
5723 read_file_scope (die, cu);
5724 break;
5725 case DW_TAG_type_unit:
5726 read_type_unit_scope (die, cu);
5727 break;
5728 case DW_TAG_subprogram:
5729 /* Nested subprograms in Fortran get a prefix. */
5730 if (cu->lang () == language_fortran
5731 && die->parent != NULL
5732 && die->parent->tag == DW_TAG_subprogram)
5733 cu->processing_has_namespace_info = true;
5734 [[fallthrough]];
5735 /* Fall through. */
5736 case DW_TAG_entry_point:
5737 case DW_TAG_inlined_subroutine:
5738 read_func_scope (die, cu);
5739 break;
5740 case DW_TAG_lexical_block:
5741 case DW_TAG_try_block:
5742 case DW_TAG_catch_block:
5743 read_lexical_block_scope (die, cu);
5744 break;
5745 case DW_TAG_call_site:
5746 case DW_TAG_GNU_call_site:
5747 read_call_site_scope (die, cu);
5748 break;
5749 case DW_TAG_class_type:
5750 case DW_TAG_interface_type:
5751 case DW_TAG_structure_type:
5752 case DW_TAG_union_type:
5753 case DW_TAG_namelist:
5754 process_structure_scope (die, cu);
5755 break;
5756 case DW_TAG_enumeration_type:
5757 process_enumeration_scope (die, cu);
5758 break;
5760 /* These dies have a type, but processing them does not create
5761 a symbol or recurse to process the children. Therefore we can
5762 read them on-demand through read_type_die. */
5763 case DW_TAG_subroutine_type:
5764 case DW_TAG_set_type:
5765 case DW_TAG_pointer_type:
5766 case DW_TAG_ptr_to_member_type:
5767 case DW_TAG_reference_type:
5768 case DW_TAG_rvalue_reference_type:
5769 case DW_TAG_string_type:
5770 break;
5772 case DW_TAG_array_type:
5773 /* We only need to handle this case for Ada -- in other
5774 languages, it's normal for the compiler to emit a typedef
5775 instead. */
5776 if (cu->lang () != language_ada)
5777 break;
5778 [[fallthrough]];
5779 case DW_TAG_base_type:
5780 case DW_TAG_subrange_type:
5781 case DW_TAG_generic_subrange:
5782 case DW_TAG_typedef:
5783 case DW_TAG_unspecified_type:
5784 /* Add a typedef symbol for the type definition, if it has a
5785 DW_AT_name. */
5786 new_symbol (die, read_type_die (die, cu), cu);
5787 break;
5788 case DW_TAG_common_block:
5789 read_common_block (die, cu);
5790 break;
5791 case DW_TAG_common_inclusion:
5792 break;
5793 case DW_TAG_namespace:
5794 cu->processing_has_namespace_info = true;
5795 read_namespace (die, cu);
5796 break;
5797 case DW_TAG_module:
5798 cu->processing_has_namespace_info = true;
5799 read_module (die, cu);
5800 break;
5801 case DW_TAG_imported_declaration:
5802 cu->processing_has_namespace_info = true;
5803 if (read_alias (die, cu))
5804 break;
5805 /* The declaration is neither a global namespace nor a variable
5806 alias. */
5807 [[fallthrough]];
5808 case DW_TAG_imported_module:
5809 cu->processing_has_namespace_info = true;
5810 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5811 || cu->lang () != language_fortran))
5812 complaint (_("Tag '%s' has unexpected children"),
5813 dwarf_tag_name (die->tag));
5814 read_import_statement (die, cu);
5815 break;
5817 case DW_TAG_imported_unit:
5818 process_imported_unit_die (die, cu);
5819 break;
5821 case DW_TAG_variable:
5822 read_variable (die, cu);
5823 break;
5825 default:
5826 new_symbol (die, NULL, cu);
5827 break;
5831 /* DWARF name computation. */
5833 /* A helper function for dwarf2_compute_name which determines whether DIE
5834 needs to have the name of the scope prepended to the name listed in the
5835 die. */
5837 static int
5838 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5840 struct attribute *attr;
5842 switch (die->tag)
5844 case DW_TAG_namespace:
5845 case DW_TAG_typedef:
5846 case DW_TAG_class_type:
5847 case DW_TAG_interface_type:
5848 case DW_TAG_structure_type:
5849 case DW_TAG_union_type:
5850 case DW_TAG_enumeration_type:
5851 case DW_TAG_enumerator:
5852 case DW_TAG_subprogram:
5853 case DW_TAG_inlined_subroutine:
5854 case DW_TAG_entry_point:
5855 case DW_TAG_member:
5856 case DW_TAG_imported_declaration:
5857 return 1;
5859 case DW_TAG_variable:
5860 case DW_TAG_constant:
5861 /* We only need to prefix "globally" visible variables. These include
5862 any variable marked with DW_AT_external or any variable that
5863 lives in a namespace. [Variables in anonymous namespaces
5864 require prefixing, but they are not DW_AT_external.] */
5866 if (dwarf2_attr (die, DW_AT_specification, cu))
5868 struct dwarf2_cu *spec_cu = cu;
5870 return die_needs_namespace (die_specification (die, &spec_cu),
5871 spec_cu);
5874 attr = dwarf2_attr (die, DW_AT_external, cu);
5875 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5876 && die->parent->tag != DW_TAG_module)
5877 return 0;
5878 /* A variable in a lexical block of some kind does not need a
5879 namespace, even though in C++ such variables may be external
5880 and have a mangled name. */
5881 if (die->parent->tag == DW_TAG_lexical_block
5882 || die->parent->tag == DW_TAG_try_block
5883 || die->parent->tag == DW_TAG_catch_block
5884 || die->parent->tag == DW_TAG_subprogram)
5885 return 0;
5886 return 1;
5888 default:
5889 return 0;
5893 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
5894 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
5895 defined for the given DIE. */
5897 static struct attribute *
5898 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
5900 struct attribute *attr;
5902 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5903 if (attr == NULL)
5904 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5906 return attr;
5909 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
5910 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
5911 defined for the given DIE. */
5913 static const char *
5914 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
5916 const char *linkage_name;
5918 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
5919 if (linkage_name == NULL)
5920 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
5922 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
5923 See https://github.com/rust-lang/rust/issues/32925. */
5924 if (cu->lang () == language_rust && linkage_name != NULL
5925 && strchr (linkage_name, '{') != NULL)
5926 linkage_name = NULL;
5928 return linkage_name;
5931 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5932 compute the physname for the object, which include a method's:
5933 - formal parameters (C++),
5934 - receiver type (Go),
5936 The term "physname" is a bit confusing.
5937 For C++, for example, it is the demangled name.
5938 For Go, for example, it's the mangled name.
5940 For Ada, return the DIE's linkage name rather than the fully qualified
5941 name. PHYSNAME is ignored..
5943 The result is allocated on the objfile->per_bfd's obstack and
5944 canonicalized. */
5946 static const char *
5947 dwarf2_compute_name (const char *name,
5948 struct die_info *die, struct dwarf2_cu *cu,
5949 int physname)
5951 struct objfile *objfile = cu->per_objfile->objfile;
5953 if (name == NULL)
5954 name = dwarf2_name (die, cu);
5956 enum language lang = cu->lang ();
5958 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
5959 but otherwise compute it by typename_concat inside GDB.
5960 FIXME: Actually this is not really true, or at least not always true.
5961 It's all very confusing. compute_and_set_names doesn't try to demangle
5962 Fortran names because there is no mangling standard. So new_symbol
5963 will set the demangled name to the result of dwarf2_full_name, and it is
5964 the demangled name that GDB uses if it exists. */
5965 if (lang == language_ada
5966 || (lang == language_fortran && physname))
5968 /* For Ada unit, we prefer the linkage name over the name, as
5969 the former contains the exported name, which the user expects
5970 to be able to reference. Ideally, we want the user to be able
5971 to reference this entity using either natural or linkage name,
5972 but we haven't started looking at this enhancement yet. */
5973 const char *linkage_name = dw2_linkage_name (die, cu);
5975 if (linkage_name != NULL)
5976 return linkage_name;
5979 /* These are the only languages we know how to qualify names in. */
5980 if (name != NULL
5981 && (lang == language_cplus
5982 || lang == language_fortran || lang == language_d
5983 || lang == language_rust))
5985 if (die_needs_namespace (die, cu))
5987 const char *prefix;
5989 string_file buf;
5991 prefix = determine_prefix (die, cu);
5992 if (*prefix != '\0')
5994 gdb::unique_xmalloc_ptr<char> prefixed_name
5995 = typename_concat (prefix, name, physname, cu);
5997 buf.puts (prefixed_name.get ());
5999 else
6000 buf.puts (name);
6002 /* Template parameters may be specified in the DIE's DW_AT_name, or
6003 as children with DW_TAG_template_type_param or
6004 DW_TAG_value_type_param. If the latter, add them to the name
6005 here. If the name already has template parameters, then
6006 skip this step; some versions of GCC emit both, and
6007 it is more efficient to use the pre-computed name.
6009 Something to keep in mind about this process: it is very
6010 unlikely, or in some cases downright impossible, to produce
6011 something that will match the mangled name of a function.
6012 If the definition of the function has the same debug info,
6013 we should be able to match up with it anyway. But fallbacks
6014 using the minimal symbol, for instance to find a method
6015 implemented in a stripped copy of libstdc++, will not work.
6016 If we do not have debug info for the definition, we will have to
6017 match them up some other way.
6019 When we do name matching there is a related problem with function
6020 templates; two instantiated function templates are allowed to
6021 differ only by their return types, which we do not add here. */
6023 if (lang == language_cplus && strchr (name, '<') == NULL)
6025 struct attribute *attr;
6026 struct die_info *child;
6027 int first = 1;
6029 die->building_fullname = 1;
6031 for (child = die->child; child != NULL; child = child->sibling)
6033 struct type *type;
6034 LONGEST value;
6035 const gdb_byte *bytes;
6036 struct dwarf2_locexpr_baton *baton;
6037 struct value *v;
6039 if (child->tag != DW_TAG_template_type_param
6040 && child->tag != DW_TAG_template_value_param)
6041 continue;
6043 if (first)
6045 buf.puts ("<");
6046 first = 0;
6048 else
6049 buf.puts (", ");
6051 attr = dwarf2_attr (child, DW_AT_type, cu);
6052 if (attr == NULL)
6054 complaint (_("template parameter missing DW_AT_type"));
6055 buf.puts ("UNKNOWN_TYPE");
6056 continue;
6058 type = die_type (child, cu);
6060 if (child->tag == DW_TAG_template_type_param)
6062 cu->language_defn->print_type (type, "", &buf, -1, 0,
6063 &type_print_raw_options);
6064 continue;
6067 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6068 if (attr == NULL)
6070 complaint (_("template parameter missing "
6071 "DW_AT_const_value"));
6072 buf.puts ("UNKNOWN_VALUE");
6073 continue;
6076 dwarf2_const_value_attr (attr, type, name,
6077 &cu->comp_unit_obstack, cu,
6078 &value, &bytes, &baton);
6080 if (type->has_no_signedness ())
6081 /* GDB prints characters as NUMBER 'CHAR'. If that's
6082 changed, this can use value_print instead. */
6083 cu->language_defn->printchar (value, type, &buf);
6084 else
6086 struct value_print_options opts;
6088 if (baton != NULL)
6089 v = dwarf2_evaluate_loc_desc (type, NULL,
6090 baton->data,
6091 baton->size,
6092 baton->per_cu,
6093 baton->per_objfile);
6094 else if (bytes != NULL)
6096 v = value::allocate (type);
6097 memcpy (v->contents_writeable ().data (), bytes,
6098 type->length ());
6100 else
6101 v = value_from_longest (type, value);
6103 /* Specify decimal so that we do not depend on
6104 the radix. */
6105 get_formatted_print_options (&opts, 'd');
6106 opts.raw = true;
6107 value_print (v, &buf, &opts);
6108 release_value (v);
6112 die->building_fullname = 0;
6114 if (!first)
6116 /* Close the argument list, with a space if necessary
6117 (nested templates). */
6118 if (!buf.empty () && buf.string ().back () == '>')
6119 buf.puts (" >");
6120 else
6121 buf.puts (">");
6125 /* For C++ methods, append formal parameter type
6126 information, if PHYSNAME. */
6128 if (physname && die->tag == DW_TAG_subprogram
6129 && lang == language_cplus)
6131 struct type *type = read_type_die (die, cu);
6133 c_type_print_args (type, &buf, 1, lang,
6134 &type_print_raw_options);
6136 if (lang == language_cplus)
6138 /* Assume that an artificial first parameter is
6139 "this", but do not crash if it is not. RealView
6140 marks unnamed (and thus unused) parameters as
6141 artificial; there is no way to differentiate
6142 the two cases. */
6143 if (type->num_fields () > 0
6144 && type->field (0).is_artificial ()
6145 && type->field (0).type ()->code () == TYPE_CODE_PTR
6146 && TYPE_CONST (type->field (0).type ()->target_type ()))
6147 buf.puts (" const");
6151 const std::string &intermediate_name = buf.string ();
6153 const char *canonical_name
6154 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
6155 objfile);
6157 /* If we only computed INTERMEDIATE_NAME, or if
6158 INTERMEDIATE_NAME is already canonical, then we need to
6159 intern it. */
6160 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
6161 name = objfile->intern (intermediate_name);
6162 else
6163 name = canonical_name;
6167 return name;
6170 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6171 If scope qualifiers are appropriate they will be added. The result
6172 will be allocated on the storage_obstack, or NULL if the DIE does
6173 not have a name. NAME may either be from a previous call to
6174 dwarf2_name or NULL.
6176 The output string will be canonicalized (if C++). */
6178 static const char *
6179 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
6181 return dwarf2_compute_name (name, die, cu, 0);
6184 /* Construct a physname for the given DIE in CU. NAME may either be
6185 from a previous call to dwarf2_name or NULL. The result will be
6186 allocated on the objfile_obstack or NULL if the DIE does not have a
6187 name.
6189 The output string will be canonicalized (if C++). */
6191 static const char *
6192 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
6194 struct objfile *objfile = cu->per_objfile->objfile;
6195 const char *retval, *mangled = NULL, *canon = NULL;
6196 int need_copy = 1;
6198 /* In this case dwarf2_compute_name is just a shortcut not building anything
6199 on its own. */
6200 if (!die_needs_namespace (die, cu))
6201 return dwarf2_compute_name (name, die, cu, 1);
6203 if (cu->lang () != language_rust)
6204 mangled = dw2_linkage_name (die, cu);
6206 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6207 has computed. */
6208 gdb::unique_xmalloc_ptr<char> demangled;
6209 if (mangled != NULL)
6211 if (cu->language_defn->store_sym_names_in_linkage_form_p ())
6213 /* Do nothing (do not demangle the symbol name). */
6215 else
6217 /* Use DMGL_RET_DROP for C++ template functions to suppress
6218 their return type. It is easier for GDB users to search
6219 for such functions as `name(params)' than `long name(params)'.
6220 In such case the minimal symbol names do not match the full
6221 symbol names but for template functions there is never a need
6222 to look up their definition from their declaration so
6223 the only disadvantage remains the minimal symbol variant
6224 `long name(params)' does not have the proper inferior type. */
6225 demangled = gdb_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
6226 | DMGL_RET_DROP));
6228 if (demangled)
6229 canon = demangled.get ();
6230 else
6232 canon = mangled;
6233 need_copy = 0;
6237 if (canon == NULL || check_physname)
6239 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6241 if (canon != NULL && strcmp (physname, canon) != 0)
6243 /* It may not mean a bug in GDB. The compiler could also
6244 compute DW_AT_linkage_name incorrectly. But in such case
6245 GDB would need to be bug-to-bug compatible. */
6247 complaint (_("Computed physname <%s> does not match demangled <%s> "
6248 "(from linkage <%s>) - DIE at %s [in module %s]"),
6249 physname, canon, mangled, sect_offset_str (die->sect_off),
6250 objfile_name (objfile));
6252 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6253 is available here - over computed PHYSNAME. It is safer
6254 against both buggy GDB and buggy compilers. */
6256 retval = canon;
6258 else
6260 retval = physname;
6261 need_copy = 0;
6264 else
6265 retval = canon;
6267 if (need_copy)
6268 retval = objfile->intern (retval);
6270 return retval;
6273 /* Inspect DIE in CU for a namespace alias or a variable with alias
6274 attribute. If one exists, record a new symbol for it.
6276 Returns true if an alias was recorded, false otherwise. */
6278 static bool
6279 read_alias (struct die_info *die, struct dwarf2_cu *cu)
6281 struct attribute *attr;
6283 /* If the die does not have a name, this is neither a namespace
6284 alias nor a variable alias. */
6285 attr = dwarf2_attr (die, DW_AT_name, cu);
6286 if (attr != NULL)
6288 int num;
6289 struct die_info *d = die;
6290 struct dwarf2_cu *imported_cu = cu;
6292 /* If the compiler has nested DW_AT_imported_declaration DIEs,
6293 keep inspecting DIEs until we hit the underlying import. */
6294 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
6295 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
6297 attr = dwarf2_attr (d, DW_AT_import, cu);
6298 if (attr == NULL)
6299 break;
6301 d = follow_die_ref (d, attr, &imported_cu);
6302 if (d->tag != DW_TAG_imported_declaration)
6303 break;
6306 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
6308 complaint (_("DIE at %s has too many recursively imported "
6309 "declarations"), sect_offset_str (d->sect_off));
6310 return false;
6313 if (attr != NULL)
6315 struct type *type;
6316 if (d->tag == DW_TAG_variable)
6318 /* This declaration is a C/C++ global variable alias.
6319 Add a symbol for it whose type is the same as the
6320 aliased variable's. */
6321 type = die_type (d, imported_cu);
6322 struct symbol *sym = new_symbol (die, type, cu);
6323 attr = dwarf2_attr (d, DW_AT_location, imported_cu);
6324 sym->set_aclass_index (LOC_UNRESOLVED);
6325 if (attr != nullptr)
6326 var_decode_location (attr, sym, cu);
6327 return true;
6329 else
6331 sect_offset sect_off = attr->get_ref_die_offset ();
6332 type = get_die_type_at_offset (sect_off, cu->per_cu,
6333 cu->per_objfile);
6334 if (type != nullptr && type->code () == TYPE_CODE_NAMESPACE)
6336 /* This declaration is a global namespace alias. Add
6337 a symbol for it whose type is the aliased
6338 namespace. */
6339 new_symbol (die, type, cu);
6340 return true;
6345 return false;
6348 /* Return the using directives repository (global or local?) to use in the
6349 current context for CU.
6351 For Ada, imported declarations can materialize renamings, which *may* be
6352 global. However it is impossible (for now?) in DWARF to distinguish
6353 "external" imported declarations and "static" ones. As all imported
6354 declarations seem to be static in all other languages, make them all CU-wide
6355 global only in Ada. */
6357 static struct using_direct **
6358 using_directives (struct dwarf2_cu *cu)
6360 if (cu->lang () == language_ada
6361 && cu->get_builder ()->outermost_context_p ())
6362 return cu->get_builder ()->get_global_using_directives ();
6363 else
6364 return cu->get_builder ()->get_local_using_directives ();
6367 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
6368 given CU. If the format is not recognized or the attribute is
6369 not present, set it to 0. */
6371 static unsigned int
6372 read_decl_line (struct die_info *die, struct dwarf2_cu *cu)
6374 struct attribute *decl_line = dwarf2_attr (die, DW_AT_decl_line, cu);
6375 if (decl_line == nullptr)
6376 return 0;
6377 if (decl_line->form_is_constant ())
6379 LONGEST val = decl_line->constant_value (0);
6380 if (0 <= val && val <= UINT_MAX)
6381 return (unsigned int) val;
6383 complaint (_("Declared line for using directive is too large"));
6384 return 0;
6387 complaint (_("Declared line for using directive is of incorrect format"));
6388 return 0;
6391 /* Read the import statement specified by the given die and record it. */
6393 static void
6394 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6396 struct objfile *objfile = cu->per_objfile->objfile;
6397 struct attribute *import_attr;
6398 struct die_info *imported_die, *child_die;
6399 struct dwarf2_cu *imported_cu;
6400 const char *imported_name;
6401 const char *imported_name_prefix;
6402 const char *canonical_name;
6403 const char *import_alias;
6404 const char *imported_declaration = NULL;
6405 const char *import_prefix;
6406 std::vector<const char *> excludes;
6408 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6409 if (import_attr == NULL)
6411 complaint (_("Tag '%s' has no DW_AT_import"),
6412 dwarf_tag_name (die->tag));
6413 return;
6416 imported_cu = cu;
6417 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6418 imported_name = dwarf2_name (imported_die, imported_cu);
6419 if (imported_name == NULL)
6421 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6423 The import in the following code:
6424 namespace A
6426 typedef int B;
6429 int main ()
6431 using A::B;
6432 B b;
6433 return b;
6437 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6438 <52> DW_AT_decl_file : 1
6439 <53> DW_AT_decl_line : 6
6440 <54> DW_AT_import : <0x75>
6441 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6442 <59> DW_AT_name : B
6443 <5b> DW_AT_decl_file : 1
6444 <5c> DW_AT_decl_line : 2
6445 <5d> DW_AT_type : <0x6e>
6447 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6448 <76> DW_AT_byte_size : 4
6449 <77> DW_AT_encoding : 5 (signed)
6451 imports the wrong die ( 0x75 instead of 0x58 ).
6452 This case will be ignored until the gcc bug is fixed. */
6453 return;
6456 /* Figure out the local name after import. */
6457 import_alias = dwarf2_name (die, cu);
6459 /* Figure out where the statement is being imported to. */
6460 import_prefix = determine_prefix (die, cu);
6462 /* Figure out what the scope of the imported die is and prepend it
6463 to the name of the imported die. */
6464 imported_name_prefix = determine_prefix (imported_die, imported_cu);
6466 if (imported_die->tag != DW_TAG_namespace
6467 && imported_die->tag != DW_TAG_module)
6469 imported_declaration = imported_name;
6470 canonical_name = imported_name_prefix;
6472 else if (strlen (imported_name_prefix) > 0)
6473 canonical_name = obconcat (&objfile->objfile_obstack,
6474 imported_name_prefix,
6475 (cu->lang () == language_d
6476 ? "."
6477 : "::"),
6478 imported_name, (char *) NULL);
6479 else
6480 canonical_name = imported_name;
6482 if (die->tag == DW_TAG_imported_module
6483 && cu->lang () == language_fortran)
6484 for (child_die = die->child; child_die && child_die->tag;
6485 child_die = child_die->sibling)
6487 /* DWARF-4: A Fortran use statement with a “rename list” may be
6488 represented by an imported module entry with an import attribute
6489 referring to the module and owned entries corresponding to those
6490 entities that are renamed as part of being imported. */
6492 if (child_die->tag != DW_TAG_imported_declaration)
6494 complaint (_("child DW_TAG_imported_declaration expected "
6495 "- DIE at %s [in module %s]"),
6496 sect_offset_str (child_die->sect_off),
6497 objfile_name (objfile));
6498 continue;
6501 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6502 if (import_attr == NULL)
6504 complaint (_("Tag '%s' has no DW_AT_import"),
6505 dwarf_tag_name (child_die->tag));
6506 continue;
6509 imported_cu = cu;
6510 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6511 &imported_cu);
6512 imported_name = dwarf2_name (imported_die, imported_cu);
6513 if (imported_name == NULL)
6515 complaint (_("child DW_TAG_imported_declaration has unknown "
6516 "imported name - DIE at %s [in module %s]"),
6517 sect_offset_str (child_die->sect_off),
6518 objfile_name (objfile));
6519 continue;
6522 excludes.push_back (imported_name);
6524 process_die (child_die, cu);
6527 add_using_directive (using_directives (cu),
6528 import_prefix,
6529 canonical_name,
6530 import_alias,
6531 imported_declaration,
6532 excludes,
6533 read_decl_line (die, cu),
6534 &objfile->objfile_obstack);
6537 /* ICC<14 does not output the required DW_AT_declaration on incomplete
6538 types, but gives them a size of zero. Starting with version 14,
6539 ICC is compatible with GCC. */
6541 static bool
6542 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
6544 if (!cu->checked_producer)
6545 check_producer (cu);
6547 return cu->producer_is_icc_lt_14;
6550 /* ICC generates a DW_AT_type for C void functions. This was observed on
6551 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
6552 which says that void functions should not have a DW_AT_type. */
6554 static bool
6555 producer_is_icc (struct dwarf2_cu *cu)
6557 if (!cu->checked_producer)
6558 check_producer (cu);
6560 return cu->producer_is_icc;
6563 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
6564 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
6565 this, it was first present in GCC release 4.3.0. */
6567 static bool
6568 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
6570 if (!cu->checked_producer)
6571 check_producer (cu);
6573 return cu->producer_is_gcc_lt_4_3;
6576 /* See dwarf2/read.h. */
6577 bool
6578 producer_is_clang (struct dwarf2_cu *cu)
6580 if (!cu->checked_producer)
6581 check_producer (cu);
6583 return cu->producer_is_clang;
6586 static file_and_directory &
6587 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
6589 if (cu->per_cu->fnd != nullptr)
6590 return *cu->per_cu->fnd;
6592 /* Find the filename. Do not use dwarf2_name here, since the filename
6593 is not a source language identifier. */
6594 file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
6595 dwarf2_string_attr (die, DW_AT_comp_dir, cu));
6597 if (res.get_comp_dir () == nullptr
6598 && producer_is_gcc_lt_4_3 (cu)
6599 && res.get_name () != nullptr
6600 && IS_ABSOLUTE_PATH (res.get_name ()))
6602 res.set_comp_dir (ldirname (res.get_name ()));
6603 res.set_name (make_unique_xstrdup (lbasename (res.get_name ())));
6606 cu->per_cu->fnd = std::make_unique<file_and_directory> (std::move (res));
6607 return *cu->per_cu->fnd;
6610 /* Handle DW_AT_stmt_list for a compilation unit.
6611 DIE is the DW_TAG_compile_unit die for CU.
6612 COMP_DIR is the compilation directory. LOWPC is passed to
6613 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
6615 static void
6616 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
6617 const file_and_directory &fnd, unrelocated_addr lowpc,
6618 bool have_code) /* ARI: editCase function */
6620 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6621 struct attribute *attr;
6622 hashval_t line_header_local_hash;
6623 void **slot;
6624 int decode_mapping;
6626 gdb_assert (! cu->per_cu->is_debug_types);
6628 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6629 if (attr == NULL || !attr->form_is_unsigned ())
6630 return;
6632 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
6634 /* The line header hash table is only created if needed (it exists to
6635 prevent redundant reading of the line table for partial_units).
6636 If we're given a partial_unit, we'll need it. If we're given a
6637 compile_unit, then use the line header hash table if it's already
6638 created, but don't create one just yet. */
6640 if (per_objfile->line_header_hash == NULL
6641 && die->tag == DW_TAG_partial_unit)
6643 per_objfile->line_header_hash
6644 .reset (htab_create_alloc (127, line_header_hash_voidp,
6645 line_header_eq_voidp,
6646 htab_delete_entry<line_header>,
6647 xcalloc, xfree));
6650 line_header line_header_local (line_offset, cu->per_cu->is_dwz);
6651 line_header_local_hash = line_header_hash (&line_header_local);
6652 if (per_objfile->line_header_hash != NULL)
6654 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
6655 &line_header_local,
6656 line_header_local_hash, NO_INSERT);
6658 /* For DW_TAG_compile_unit we need info like symtab::linetable which
6659 is not present in *SLOT (since if there is something in *SLOT then
6660 it will be for a partial_unit). */
6661 if (die->tag == DW_TAG_partial_unit && slot != NULL)
6663 gdb_assert (*slot != NULL);
6664 cu->line_header = (struct line_header *) *slot;
6665 return;
6669 /* dwarf_decode_line_header does not yet provide sufficient information.
6670 We always have to call also dwarf_decode_lines for it. */
6671 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
6672 fnd.get_comp_dir ());
6673 if (lh == NULL)
6674 return;
6676 cu->line_header = lh.release ();
6677 cu->line_header_die_owner = die;
6679 if (per_objfile->line_header_hash == NULL)
6680 slot = NULL;
6681 else
6683 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
6684 &line_header_local,
6685 line_header_local_hash, INSERT);
6686 gdb_assert (slot != NULL);
6688 if (slot != NULL && *slot == NULL)
6690 /* This newly decoded line number information unit will be owned
6691 by line_header_hash hash table. */
6692 *slot = cu->line_header;
6693 cu->line_header_die_owner = NULL;
6695 else
6697 /* We cannot free any current entry in (*slot) as that struct line_header
6698 may be already used by multiple CUs. Create only temporary decoded
6699 line_header for this CU - it may happen at most once for each line
6700 number information unit. And if we're not using line_header_hash
6701 then this is what we want as well. */
6702 gdb_assert (die->tag != DW_TAG_partial_unit);
6704 decode_mapping = (die->tag != DW_TAG_partial_unit);
6705 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
6706 then there won't be any interesting code in the CU, but a check later on
6707 (in lnp_state_machine::check_line_address) will fail to properly exclude
6708 an entry that was removed via --gc-sections. */
6709 dwarf_decode_lines (cu->line_header, cu, lowpc, decode_mapping && have_code);
6712 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
6714 static void
6715 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
6717 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6718 struct objfile *objfile = per_objfile->objfile;
6719 CORE_ADDR lowpc;
6720 struct attribute *attr;
6721 struct die_info *child_die;
6723 prepare_one_comp_unit (cu, die, cu->lang ());
6725 unrelocated_addr unrel_low, unrel_high;
6726 get_scope_pc_bounds (die, &unrel_low, &unrel_high, cu);
6728 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6729 from finish_block. */
6730 if (unrel_low == ((unrelocated_addr) -1))
6731 unrel_low = unrel_high;
6732 lowpc = per_objfile->relocate (unrel_low);
6734 file_and_directory &fnd = find_file_and_directory (die, cu);
6736 /* GAS supports generating dwarf-5 info starting version 2.35. Versions
6737 2.35-2.37 generate an incorrect CU name attribute: it's relative,
6738 implicitly prefixing it with the compilation dir. Work around this by
6739 prefixing it with the source dir instead. */
6740 if (cu->header.version == 5 && !IS_ABSOLUTE_PATH (fnd.get_name ())
6741 && producer_is_gas_lt_2_38 (cu))
6743 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6744 if (attr != nullptr && attr->form_is_unsigned ())
6746 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
6747 line_header_up lh = dwarf_decode_line_header (line_offset, cu,
6748 fnd.get_comp_dir ());
6749 if (lh->version == 5 && lh->is_valid_file_index (1))
6751 std::string dir = lh->include_dir_at (1);
6752 fnd.set_comp_dir (std::move (dir));
6757 cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir (objfile),
6758 lowpc);
6760 gdb_assert (per_objfile->sym_cu == nullptr);
6761 scoped_restore restore_sym_cu
6762 = make_scoped_restore (&per_objfile->sym_cu, cu);
6764 /* Decode line number information if present. We do this before
6765 processing child DIEs, so that the line header table is available
6766 for DW_AT_decl_file. */
6767 handle_DW_AT_stmt_list (die, cu, fnd, unrel_low, unrel_low != unrel_high);
6769 /* Process all dies in compilation unit. */
6770 if (die->child != NULL)
6772 child_die = die->child;
6773 while (child_die && child_die->tag)
6775 process_die (child_die, cu);
6776 child_die = child_die->sibling;
6779 per_objfile->sym_cu = nullptr;
6781 /* Decode macro information, if present. Dwarf 2 macro information
6782 refers to information in the line number info statement program
6783 header, so we can only read it if we've read the header
6784 successfully. */
6785 attr = dwarf2_attr (die, DW_AT_macros, cu);
6786 if (attr == NULL)
6787 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6788 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
6790 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6791 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
6793 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
6795 else
6797 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6798 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
6800 unsigned int macro_offset = attr->as_unsigned ();
6802 dwarf_decode_macros (cu, macro_offset, 0);
6807 void
6808 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
6810 struct type_unit_group *tu_group;
6811 int first_time;
6812 struct attribute *attr;
6813 unsigned int i;
6814 struct signatured_type *sig_type;
6816 gdb_assert (per_cu->is_debug_types);
6817 sig_type = (struct signatured_type *) per_cu;
6819 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
6821 /* If we're using .gdb_index (includes -readnow) then
6822 per_cu->type_unit_group may not have been set up yet. */
6823 if (sig_type->type_unit_group == NULL)
6824 sig_type->type_unit_group = get_type_unit_group (this, attr);
6825 tu_group = sig_type->type_unit_group;
6827 /* If we've already processed this stmt_list there's no real need to
6828 do it again, we could fake it and just recreate the part we need
6829 (file name,index -> symtab mapping). If data shows this optimization
6830 is useful we can do it then. */
6831 type_unit_group_unshareable *tug_unshare
6832 = per_objfile->get_type_unit_group_unshareable (tu_group);
6833 first_time = tug_unshare->compunit_symtab == NULL;
6835 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
6836 debug info. */
6837 line_header_up lh;
6838 if (attr != NULL && attr->form_is_unsigned ())
6840 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
6841 lh = dwarf_decode_line_header (line_offset, this, nullptr);
6843 if (lh == NULL)
6845 if (first_time)
6846 start_compunit_symtab ("", NULL, 0);
6847 else
6849 gdb_assert (tug_unshare->symtabs == NULL);
6850 gdb_assert (m_builder == nullptr);
6851 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
6852 m_builder = std::make_unique<buildsym_compunit>
6853 (cust->objfile (), "",
6854 cust->dirname (),
6855 cust->language (),
6856 0, cust);
6857 list_in_scope = get_builder ()->get_file_symbols ();
6859 return;
6862 line_header = lh.release ();
6863 line_header_die_owner = die;
6865 if (first_time)
6867 struct compunit_symtab *cust = start_compunit_symtab ("", NULL, 0);
6869 /* Note: We don't assign tu_group->compunit_symtab yet because we're
6870 still initializing it, and our caller (a few levels up)
6871 process_full_type_unit still needs to know if this is the first
6872 time. */
6874 tug_unshare->symtabs
6875 = XOBNEWVEC (&cust->objfile ()->objfile_obstack,
6876 struct symtab *, line_header->file_names_size ());
6878 auto &file_names = line_header->file_names ();
6879 for (i = 0; i < file_names.size (); ++i)
6881 file_entry &fe = file_names[i];
6882 dwarf2_start_subfile (this, fe, *line_header);
6883 buildsym_compunit *b = get_builder ();
6884 subfile *sf = b->get_current_subfile ();
6886 if (sf->symtab == nullptr)
6888 /* NOTE: start_subfile will recognize when it's been
6889 passed a file it has already seen. So we can't
6890 assume there's a simple mapping from
6891 cu->line_header->file_names to subfiles, plus
6892 cu->line_header->file_names may contain dups. */
6893 const char *name = sf->name.c_str ();
6894 const char *name_for_id = sf->name_for_id.c_str ();
6895 sf->symtab = allocate_symtab (cust, name, name_for_id);
6898 fe.symtab = b->get_current_subfile ()->symtab;
6899 tug_unshare->symtabs[i] = fe.symtab;
6902 else
6904 gdb_assert (m_builder == nullptr);
6905 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
6906 m_builder = std::make_unique<buildsym_compunit>
6907 (cust->objfile (), "",
6908 cust->dirname (),
6909 cust->language (),
6910 0, cust);
6911 list_in_scope = get_builder ()->get_file_symbols ();
6913 auto &file_names = line_header->file_names ();
6914 for (i = 0; i < file_names.size (); ++i)
6916 file_entry &fe = file_names[i];
6917 fe.symtab = tug_unshare->symtabs[i];
6921 /* The main symtab is allocated last. Type units don't have DW_AT_name
6922 so they don't have a "real" (so to speak) symtab anyway.
6923 There is later code that will assign the main symtab to all symbols
6924 that don't have one. We need to handle the case of a symbol with a
6925 missing symtab (DW_AT_decl_file) anyway. */
6928 /* Process DW_TAG_type_unit.
6929 For TUs we want to skip the first top level sibling if it's not the
6930 actual type being defined by this TU. In this case the first top
6931 level sibling is there to provide context only. */
6933 static void
6934 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6936 struct die_info *child_die;
6938 prepare_one_comp_unit (cu, die, language_minimal);
6940 /* Initialize (or reinitialize) the machinery for building symtabs.
6941 We do this before processing child DIEs, so that the line header table
6942 is available for DW_AT_decl_file. */
6943 cu->setup_type_unit_groups (die);
6945 if (die->child != NULL)
6947 child_die = die->child;
6948 while (child_die && child_die->tag)
6950 process_die (child_die, cu);
6951 child_die = child_die->sibling;
6956 /* DWO/DWP files.
6958 http://gcc.gnu.org/wiki/DebugFission
6959 http://gcc.gnu.org/wiki/DebugFissionDWP
6961 To simplify handling of both DWO files ("object" files with the DWARF info)
6962 and DWP files (a file with the DWOs packaged up into one file), we treat
6963 DWP files as having a collection of virtual DWO files. */
6965 /* A helper function to hash two file names. This is a separate
6966 function because the hash table uses a search with a different
6967 type. The second file may be NULL. */
6969 static hashval_t
6970 hash_two_files (const char *one, const char *two)
6972 hashval_t hash = htab_hash_string (one);
6973 if (two != nullptr)
6974 hash += htab_hash_string (two);
6975 return hash;
6978 static hashval_t
6979 hash_dwo_file (const void *item)
6981 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
6982 return hash_two_files (dwo_file->dwo_name.c_str (), dwo_file->comp_dir);
6985 /* This is used when looking up entries in the DWO hash table. */
6987 struct dwo_file_search
6989 /* Name of the DWO to look for. */
6990 const char *dwo_name;
6991 /* Compilation directory to look for. */
6992 const char *comp_dir;
6994 /* Return a hash value compatible with the table. */
6995 hashval_t hash () const
6997 return hash_two_files (dwo_name, comp_dir);
7001 static int
7002 eq_dwo_file (const void *item_lhs, const void *item_rhs)
7004 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
7005 const struct dwo_file_search *rhs
7006 = (const struct dwo_file_search *) item_rhs;
7008 if (lhs->dwo_name != rhs->dwo_name)
7009 return 0;
7010 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
7011 return lhs->comp_dir == rhs->comp_dir;
7012 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
7015 /* Allocate a hash table for DWO files. */
7017 static htab_up
7018 allocate_dwo_file_hash_table ()
7020 return htab_up (htab_create_alloc (41,
7021 hash_dwo_file,
7022 eq_dwo_file,
7023 htab_delete_entry<dwo_file>,
7024 xcalloc, xfree));
7027 /* Lookup DWO file DWO_NAME. */
7029 static void **
7030 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
7031 const char *dwo_name,
7032 const char *comp_dir)
7034 struct dwo_file_search find_entry;
7035 void **slot;
7037 if (per_objfile->per_bfd->dwo_files == NULL)
7038 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
7040 find_entry.dwo_name = dwo_name;
7041 find_entry.comp_dir = comp_dir;
7042 slot = htab_find_slot_with_hash (per_objfile->per_bfd->dwo_files.get (),
7043 &find_entry, find_entry.hash (),
7044 INSERT);
7046 return slot;
7049 static hashval_t
7050 hash_dwo_unit (const void *item)
7052 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
7054 /* This drops the top 32 bits of the id, but is ok for a hash. */
7055 return dwo_unit->signature;
7058 static int
7059 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7061 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
7062 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
7064 /* The signature is assumed to be unique within the DWO file.
7065 So while object file CU dwo_id's always have the value zero,
7066 that's OK, assuming each object file DWO file has only one CU,
7067 and that's the rule for now. */
7068 return lhs->signature == rhs->signature;
7071 /* Allocate a hash table for DWO CUs,TUs.
7072 There is one of these tables for each of CUs,TUs for each DWO file. */
7074 static htab_up
7075 allocate_dwo_unit_table ()
7077 /* Start out with a pretty small number.
7078 Generally DWO files contain only one CU and maybe some TUs. */
7079 return htab_up (htab_create_alloc (3,
7080 hash_dwo_unit,
7081 eq_dwo_unit,
7082 NULL, xcalloc, xfree));
7085 /* die_reader_func for create_dwo_cu. */
7087 static void
7088 create_dwo_cu_reader (const struct die_reader_specs *reader,
7089 const gdb_byte *info_ptr,
7090 struct die_info *comp_unit_die,
7091 struct dwo_file *dwo_file,
7092 struct dwo_unit *dwo_unit)
7094 struct dwarf2_cu *cu = reader->cu;
7095 sect_offset sect_off = cu->per_cu->sect_off;
7096 struct dwarf2_section_info *section = cu->per_cu->section;
7098 std::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7099 if (!signature.has_value ())
7101 complaint (_(DWARF_ERROR_PREFIX
7102 "debug entry at offset %s is missing its dwo_id"
7103 " [in module %s]"),
7104 sect_offset_str (sect_off), dwo_file->dwo_name.c_str ());
7105 return;
7108 dwo_unit->dwo_file = dwo_file;
7109 dwo_unit->signature = *signature;
7110 dwo_unit->section = section;
7111 dwo_unit->sect_off = sect_off;
7112 dwo_unit->length = cu->per_cu->length ();
7114 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7115 sect_offset_str (sect_off),
7116 hex_string (dwo_unit->signature));
7119 /* Create the dwo_units for the CUs in a DWO_FILE.
7120 Note: This function processes DWO files only, not DWP files. */
7122 static void
7123 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
7124 dwarf2_cu *cu, struct dwo_file &dwo_file,
7125 dwarf2_section_info &section, htab_up &cus_htab)
7127 struct objfile *objfile = per_objfile->objfile;
7128 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7129 const gdb_byte *info_ptr, *end_ptr;
7131 section.read (objfile);
7132 info_ptr = section.buffer;
7134 if (info_ptr == NULL)
7135 return;
7137 dwarf_read_debug_printf ("Reading %s for %s:",
7138 section.get_name (),
7139 section.get_file_name ());
7141 end_ptr = info_ptr + section.size;
7142 while (info_ptr < end_ptr)
7144 struct dwarf2_per_cu_data per_cu;
7145 struct dwo_unit read_unit {};
7146 struct dwo_unit *dwo_unit;
7147 void **slot;
7148 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
7150 per_cu.per_bfd = per_bfd;
7151 per_cu.is_debug_types = 0;
7152 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
7153 per_cu.section = &section;
7155 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
7156 if (!reader.dummy_p)
7157 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
7158 &dwo_file, &read_unit);
7159 info_ptr += per_cu.length ();
7161 // If the unit could not be parsed, skip it.
7162 if (read_unit.dwo_file == NULL)
7163 continue;
7165 if (cus_htab == NULL)
7166 cus_htab = allocate_dwo_unit_table ();
7168 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
7169 struct dwo_unit);
7170 *dwo_unit = read_unit;
7171 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
7172 gdb_assert (slot != NULL);
7173 if (*slot != NULL)
7175 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
7176 sect_offset dup_sect_off = dup_cu->sect_off;
7178 complaint (_("debug cu entry at offset %s is duplicate to"
7179 " the entry at offset %s, signature %s"),
7180 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
7181 hex_string (dwo_unit->signature));
7183 *slot = (void *)dwo_unit;
7187 /* DWP file .debug_{cu,tu}_index section format:
7188 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
7189 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
7191 DWP Versions 1 & 2 are older, pre-standard format versions. The first
7192 officially standard DWP format was published with DWARF v5 and is called
7193 Version 5. There are no versions 3 or 4.
7195 DWP Version 1:
7197 Both index sections have the same format, and serve to map a 64-bit
7198 signature to a set of section numbers. Each section begins with a header,
7199 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
7200 indexes, and a pool of 32-bit section numbers. The index sections will be
7201 aligned at 8-byte boundaries in the file.
7203 The index section header consists of:
7205 V, 32 bit version number
7206 -, 32 bits unused
7207 N, 32 bit number of compilation units or type units in the index
7208 M, 32 bit number of slots in the hash table
7210 Numbers are recorded using the byte order of the application binary.
7212 The hash table begins at offset 16 in the section, and consists of an array
7213 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
7214 order of the application binary). Unused slots in the hash table are 0.
7215 (We rely on the extreme unlikeliness of a signature being exactly 0.)
7217 The parallel table begins immediately after the hash table
7218 (at offset 16 + 8 * M from the beginning of the section), and consists of an
7219 array of 32-bit indexes (using the byte order of the application binary),
7220 corresponding 1-1 with slots in the hash table. Each entry in the parallel
7221 table contains a 32-bit index into the pool of section numbers. For unused
7222 hash table slots, the corresponding entry in the parallel table will be 0.
7224 The pool of section numbers begins immediately following the hash table
7225 (at offset 16 + 12 * M from the beginning of the section). The pool of
7226 section numbers consists of an array of 32-bit words (using the byte order
7227 of the application binary). Each item in the array is indexed starting
7228 from 0. The hash table entry provides the index of the first section
7229 number in the set. Additional section numbers in the set follow, and the
7230 set is terminated by a 0 entry (section number 0 is not used in ELF).
7232 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
7233 section must be the first entry in the set, and the .debug_abbrev.dwo must
7234 be the second entry. Other members of the set may follow in any order.
7238 DWP Versions 2 and 5:
7240 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
7241 and the entries in the index tables are now offsets into these sections.
7242 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
7243 section.
7245 Index Section Contents:
7246 Header
7247 Hash Table of Signatures dwp_hash_table.hash_table
7248 Parallel Table of Indices dwp_hash_table.unit_table
7249 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
7250 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
7252 The index section header consists of:
7254 V, 32 bit version number
7255 L, 32 bit number of columns in the table of section offsets
7256 N, 32 bit number of compilation units or type units in the index
7257 M, 32 bit number of slots in the hash table
7259 Numbers are recorded using the byte order of the application binary.
7261 The hash table has the same format as version 1.
7262 The parallel table of indices has the same format as version 1,
7263 except that the entries are origin-1 indices into the table of sections
7264 offsets and the table of section sizes.
7266 The table of offsets begins immediately following the parallel table
7267 (at offset 16 + 12 * M from the beginning of the section). The table is
7268 a two-dimensional array of 32-bit words (using the byte order of the
7269 application binary), with L columns and N+1 rows, in row-major order.
7270 Each row in the array is indexed starting from 0. The first row provides
7271 a key to the remaining rows: each column in this row provides an identifier
7272 for a debug section, and the offsets in the same column of subsequent rows
7273 refer to that section. The section identifiers for Version 2 are:
7275 DW_SECT_INFO 1 .debug_info.dwo
7276 DW_SECT_TYPES 2 .debug_types.dwo
7277 DW_SECT_ABBREV 3 .debug_abbrev.dwo
7278 DW_SECT_LINE 4 .debug_line.dwo
7279 DW_SECT_LOC 5 .debug_loc.dwo
7280 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
7281 DW_SECT_MACINFO 7 .debug_macinfo.dwo
7282 DW_SECT_MACRO 8 .debug_macro.dwo
7284 The section identifiers for Version 5 are:
7286 DW_SECT_INFO_V5 1 .debug_info.dwo
7287 DW_SECT_RESERVED_V5 2 --
7288 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
7289 DW_SECT_LINE_V5 4 .debug_line.dwo
7290 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
7291 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
7292 DW_SECT_MACRO_V5 7 .debug_macro.dwo
7293 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
7295 The offsets provided by the CU and TU index sections are the base offsets
7296 for the contributions made by each CU or TU to the corresponding section
7297 in the package file. Each CU and TU header contains an abbrev_offset
7298 field, used to find the abbreviations table for that CU or TU within the
7299 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
7300 be interpreted as relative to the base offset given in the index section.
7301 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
7302 should be interpreted as relative to the base offset for .debug_line.dwo,
7303 and offsets into other debug sections obtained from DWARF attributes should
7304 also be interpreted as relative to the corresponding base offset.
7306 The table of sizes begins immediately following the table of offsets.
7307 Like the table of offsets, it is a two-dimensional array of 32-bit words,
7308 with L columns and N rows, in row-major order. Each row in the array is
7309 indexed starting from 1 (row 0 is shared by the two tables).
7313 Hash table lookup is handled the same in version 1 and 2:
7315 We assume that N and M will not exceed 2^32 - 1.
7316 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
7318 Given a 64-bit compilation unit signature or a type signature S, an entry
7319 in the hash table is located as follows:
7321 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
7322 the low-order k bits all set to 1.
7324 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
7326 3) If the hash table entry at index H matches the signature, use that
7327 entry. If the hash table entry at index H is unused (all zeroes),
7328 terminate the search: the signature is not present in the table.
7330 4) Let H = (H + H') modulo M. Repeat at Step 3.
7332 Because M > N and H' and M are relatively prime, the search is guaranteed
7333 to stop at an unused slot or find the match. */
7335 /* Create a hash table to map DWO IDs to their CU/TU entry in
7336 .debug_{info,types}.dwo in DWP_FILE.
7337 Returns NULL if there isn't one.
7338 Note: This function processes DWP files only, not DWO files. */
7340 static struct dwp_hash_table *
7341 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
7342 struct dwp_file *dwp_file, int is_debug_types)
7344 struct objfile *objfile = per_objfile->objfile;
7345 bfd *dbfd = dwp_file->dbfd.get ();
7346 const gdb_byte *index_ptr, *index_end;
7347 struct dwarf2_section_info *index;
7348 uint32_t version, nr_columns, nr_units, nr_slots;
7349 struct dwp_hash_table *htab;
7351 if (is_debug_types)
7352 index = &dwp_file->sections.tu_index;
7353 else
7354 index = &dwp_file->sections.cu_index;
7356 if (index->empty ())
7357 return NULL;
7358 index->read (objfile);
7360 index_ptr = index->buffer;
7361 index_end = index_ptr + index->size;
7363 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
7364 For now it's safe to just read 4 bytes (particularly as it's difficult to
7365 tell if you're dealing with Version 5 before you've read the version). */
7366 version = read_4_bytes (dbfd, index_ptr);
7367 index_ptr += 4;
7368 if (version == 2 || version == 5)
7369 nr_columns = read_4_bytes (dbfd, index_ptr);
7370 else
7371 nr_columns = 0;
7372 index_ptr += 4;
7373 nr_units = read_4_bytes (dbfd, index_ptr);
7374 index_ptr += 4;
7375 nr_slots = read_4_bytes (dbfd, index_ptr);
7376 index_ptr += 4;
7378 if (version != 1 && version != 2 && version != 5)
7380 error (_(DWARF_ERROR_PREFIX
7381 "unsupported DWP file version (%s) [in module %s]"),
7382 pulongest (version), dwp_file->name);
7384 if (nr_slots != (nr_slots & -nr_slots))
7386 error (_(DWARF_ERROR_PREFIX
7387 "number of slots in DWP hash table (%s) is not power of 2"
7388 " [in module %s]"),
7389 pulongest (nr_slots), dwp_file->name);
7392 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
7393 htab->version = version;
7394 htab->nr_columns = nr_columns;
7395 htab->nr_units = nr_units;
7396 htab->nr_slots = nr_slots;
7397 htab->hash_table = index_ptr;
7398 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
7400 /* Exit early if the table is empty. */
7401 if (nr_slots == 0 || nr_units == 0
7402 || (version == 2 && nr_columns == 0)
7403 || (version == 5 && nr_columns == 0))
7405 /* All must be zero. */
7406 if (nr_slots != 0 || nr_units != 0
7407 || (version == 2 && nr_columns != 0)
7408 || (version == 5 && nr_columns != 0))
7410 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
7411 " all zero [in modules %s]"),
7412 dwp_file->name);
7414 return htab;
7417 if (version == 1)
7419 htab->section_pool.v1.indices =
7420 htab->unit_table + sizeof (uint32_t) * nr_slots;
7421 /* It's harder to decide whether the section is too small in v1.
7422 V1 is deprecated anyway so we punt. */
7424 else if (version == 2)
7426 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
7427 int *ids = htab->section_pool.v2.section_ids;
7428 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
7429 /* Reverse map for error checking. */
7430 int ids_seen[DW_SECT_MAX + 1];
7431 int i;
7433 if (nr_columns < 2)
7435 error (_(DWARF_ERROR_PREFIX
7436 "bad DWP hash table, too few columns in section table"
7437 " [in module %s]"),
7438 dwp_file->name);
7440 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
7442 error (_(DWARF_ERROR_PREFIX
7443 "bad DWP hash table, too many columns in section table"
7444 " [in module %s]"),
7445 dwp_file->name);
7447 memset (ids, 255, sizeof_ids);
7448 memset (ids_seen, 255, sizeof (ids_seen));
7449 for (i = 0; i < nr_columns; ++i)
7451 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
7453 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
7455 error (_(DWARF_ERROR_PREFIX
7456 "bad DWP hash table, bad section id %d in section table"
7457 " [in module %s]"),
7458 id, dwp_file->name);
7460 if (ids_seen[id] != -1)
7462 error (_(DWARF_ERROR_PREFIX
7463 "bad DWP hash table, duplicate section"
7464 " id %d in section table [in module %s]"),
7465 id, dwp_file->name);
7467 ids_seen[id] = i;
7468 ids[i] = id;
7470 /* Must have exactly one info or types section. */
7471 if (((ids_seen[DW_SECT_INFO] != -1)
7472 + (ids_seen[DW_SECT_TYPES] != -1))
7473 != 1)
7475 error (_(DWARF_ERROR_PREFIX
7476 "bad DWP hash table, missing/duplicate"
7477 " DWO info/types section [in module %s]"),
7478 dwp_file->name);
7480 /* Must have an abbrev section. */
7481 if (ids_seen[DW_SECT_ABBREV] == -1)
7483 error (_(DWARF_ERROR_PREFIX
7484 "bad DWP hash table, missing DWO abbrev section"
7485 " [in module %s]"),
7486 dwp_file->name);
7488 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
7489 htab->section_pool.v2.sizes =
7490 htab->section_pool.v2.offsets + (sizeof (uint32_t)
7491 * nr_units * nr_columns);
7492 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
7493 * nr_units * nr_columns))
7494 > index_end)
7496 error (_(DWARF_ERROR_PREFIX
7497 "DWP index section is corrupt (too small) [in module %s]"),
7498 dwp_file->name);
7501 else /* version == 5 */
7503 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
7504 int *ids = htab->section_pool.v5.section_ids;
7505 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
7506 /* Reverse map for error checking. */
7507 int ids_seen[DW_SECT_MAX_V5 + 1];
7509 if (nr_columns < 2)
7511 error (_(DWARF_ERROR_PREFIX
7512 "bad DWP hash table, too few columns in section table"
7513 " [in module %s]"),
7514 dwp_file->name);
7516 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
7518 error (_(DWARF_ERROR_PREFIX
7519 "bad DWP hash table, too many columns in section table"
7520 " [in module %s]"),
7521 dwp_file->name);
7523 memset (ids, 255, sizeof_ids);
7524 memset (ids_seen, 255, sizeof (ids_seen));
7525 for (int i = 0; i < nr_columns; ++i)
7527 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
7529 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
7531 error (_(DWARF_ERROR_PREFIX
7532 "bad DWP hash table, bad section id %d in section table"
7533 " [in module %s]"),
7534 id, dwp_file->name);
7536 if (ids_seen[id] != -1)
7538 error (_(DWARF_ERROR_PREFIX
7539 "bad DWP hash table, duplicate section"
7540 " id %d in section table [in module %s]"),
7541 id, dwp_file->name);
7543 ids_seen[id] = i;
7544 ids[i] = id;
7546 /* Must have seen an info section. */
7547 if (ids_seen[DW_SECT_INFO_V5] == -1)
7549 error (_(DWARF_ERROR_PREFIX
7550 "bad DWP hash table, missing/duplicate"
7551 " DWO info/types section [in module %s]"),
7552 dwp_file->name);
7554 /* Must have an abbrev section. */
7555 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
7557 error (_(DWARF_ERROR_PREFIX
7558 "bad DWP hash table, missing DWO abbrev section"
7559 " [in module %s]"),
7560 dwp_file->name);
7562 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
7563 htab->section_pool.v5.sizes
7564 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
7565 * nr_units * nr_columns);
7566 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
7567 * nr_units * nr_columns))
7568 > index_end)
7570 error (_(DWARF_ERROR_PREFIX
7571 "DWP index section is corrupt (too small) [in module %s]"),
7572 dwp_file->name);
7576 return htab;
7579 /* Update SECTIONS with the data from SECTP.
7581 This function is like the other "locate" section routines, but in
7582 this context the sections to read comes from the DWP V1 hash table,
7583 not the full ELF section table.
7585 The result is non-zero for success, or zero if an error was found. */
7587 static int
7588 locate_v1_virtual_dwo_sections (asection *sectp,
7589 struct virtual_v1_dwo_sections *sections)
7591 const struct dwop_section_names *names = &dwop_section_names;
7593 if (names->abbrev_dwo.matches (sectp->name))
7595 /* There can be only one. */
7596 if (sections->abbrev.s.section != NULL)
7597 return 0;
7598 sections->abbrev.s.section = sectp;
7599 sections->abbrev.size = bfd_section_size (sectp);
7601 else if (names->info_dwo.matches (sectp->name)
7602 || names->types_dwo.matches (sectp->name))
7604 /* There can be only one. */
7605 if (sections->info_or_types.s.section != NULL)
7606 return 0;
7607 sections->info_or_types.s.section = sectp;
7608 sections->info_or_types.size = bfd_section_size (sectp);
7610 else if (names->line_dwo.matches (sectp->name))
7612 /* There can be only one. */
7613 if (sections->line.s.section != NULL)
7614 return 0;
7615 sections->line.s.section = sectp;
7616 sections->line.size = bfd_section_size (sectp);
7618 else if (names->loc_dwo.matches (sectp->name))
7620 /* There can be only one. */
7621 if (sections->loc.s.section != NULL)
7622 return 0;
7623 sections->loc.s.section = sectp;
7624 sections->loc.size = bfd_section_size (sectp);
7626 else if (names->macinfo_dwo.matches (sectp->name))
7628 /* There can be only one. */
7629 if (sections->macinfo.s.section != NULL)
7630 return 0;
7631 sections->macinfo.s.section = sectp;
7632 sections->macinfo.size = bfd_section_size (sectp);
7634 else if (names->macro_dwo.matches (sectp->name))
7636 /* There can be only one. */
7637 if (sections->macro.s.section != NULL)
7638 return 0;
7639 sections->macro.s.section = sectp;
7640 sections->macro.size = bfd_section_size (sectp);
7642 else if (names->str_offsets_dwo.matches (sectp->name))
7644 /* There can be only one. */
7645 if (sections->str_offsets.s.section != NULL)
7646 return 0;
7647 sections->str_offsets.s.section = sectp;
7648 sections->str_offsets.size = bfd_section_size (sectp);
7650 else
7652 /* No other kind of section is valid. */
7653 return 0;
7656 return 1;
7659 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
7660 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
7661 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
7662 This is for DWP version 1 files. */
7664 static struct dwo_unit *
7665 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
7666 struct dwp_file *dwp_file,
7667 uint32_t unit_index,
7668 const char *comp_dir,
7669 ULONGEST signature, int is_debug_types)
7671 const struct dwp_hash_table *dwp_htab =
7672 is_debug_types ? dwp_file->tus : dwp_file->cus;
7673 bfd *dbfd = dwp_file->dbfd.get ();
7674 const char *kind = is_debug_types ? "TU" : "CU";
7675 struct dwo_file *dwo_file;
7676 struct dwo_unit *dwo_unit;
7677 struct virtual_v1_dwo_sections sections;
7678 void **dwo_file_slot;
7679 int i;
7681 gdb_assert (dwp_file->version == 1);
7683 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
7684 kind, pulongest (unit_index), hex_string (signature),
7685 dwp_file->name);
7687 /* Fetch the sections of this DWO unit.
7688 Put a limit on the number of sections we look for so that bad data
7689 doesn't cause us to loop forever. */
7691 #define MAX_NR_V1_DWO_SECTIONS \
7692 (1 /* .debug_info or .debug_types */ \
7693 + 1 /* .debug_abbrev */ \
7694 + 1 /* .debug_line */ \
7695 + 1 /* .debug_loc */ \
7696 + 1 /* .debug_str_offsets */ \
7697 + 1 /* .debug_macro or .debug_macinfo */ \
7698 + 1 /* trailing zero */)
7700 memset (&sections, 0, sizeof (sections));
7702 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
7704 asection *sectp;
7705 uint32_t section_nr =
7706 read_4_bytes (dbfd,
7707 dwp_htab->section_pool.v1.indices
7708 + (unit_index + i) * sizeof (uint32_t));
7710 if (section_nr == 0)
7711 break;
7712 if (section_nr >= dwp_file->num_sections)
7714 error (_(DWARF_ERROR_PREFIX
7715 "bad DWP hash table, section number too large"
7716 " [in module %s]"),
7717 dwp_file->name);
7720 sectp = dwp_file->elf_sections[section_nr];
7721 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
7723 error (_(DWARF_ERROR_PREFIX
7724 "bad DWP hash table, invalid section found [in module %s]"),
7725 dwp_file->name);
7729 if (i < 2
7730 || sections.info_or_types.empty ()
7731 || sections.abbrev.empty ())
7733 error (_(DWARF_ERROR_PREFIX
7734 "bad DWP hash table, missing DWO sections [in module %s]"),
7735 dwp_file->name);
7737 if (i == MAX_NR_V1_DWO_SECTIONS)
7739 error (_(DWARF_ERROR_PREFIX
7740 "bad DWP hash table, too many DWO sections [in module %s]"),
7741 dwp_file->name);
7744 /* It's easier for the rest of the code if we fake a struct dwo_file and
7745 have dwo_unit "live" in that. At least for now.
7747 The DWP file can be made up of a random collection of CUs and TUs.
7748 However, for each CU + set of TUs that came from the same original DWO
7749 file, we can combine them back into a virtual DWO file to save space
7750 (fewer struct dwo_file objects to allocate). Remember that for really
7751 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
7753 std::string virtual_dwo_name =
7754 string_printf ("virtual-dwo/%d-%d-%d-%d",
7755 sections.abbrev.get_id (),
7756 sections.line.get_id (),
7757 sections.loc.get_id (),
7758 sections.str_offsets.get_id ());
7759 /* Can we use an existing virtual DWO file? */
7760 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
7761 comp_dir);
7762 /* Create one if necessary. */
7763 if (*dwo_file_slot == NULL)
7765 dwarf_read_debug_printf ("Creating virtual DWO: %s",
7766 virtual_dwo_name.c_str ());
7768 dwo_file = new struct dwo_file;
7769 dwo_file->dwo_name = std::move (virtual_dwo_name);
7770 dwo_file->comp_dir = comp_dir;
7771 dwo_file->sections.abbrev = sections.abbrev;
7772 dwo_file->sections.line = sections.line;
7773 dwo_file->sections.loc = sections.loc;
7774 dwo_file->sections.macinfo = sections.macinfo;
7775 dwo_file->sections.macro = sections.macro;
7776 dwo_file->sections.str_offsets = sections.str_offsets;
7777 /* The "str" section is global to the entire DWP file. */
7778 dwo_file->sections.str = dwp_file->sections.str;
7779 /* The info or types section is assigned below to dwo_unit,
7780 there's no need to record it in dwo_file.
7781 Also, we can't simply record type sections in dwo_file because
7782 we record a pointer into the vector in dwo_unit. As we collect more
7783 types we'll grow the vector and eventually have to reallocate space
7784 for it, invalidating all copies of pointers into the previous
7785 contents. */
7786 *dwo_file_slot = dwo_file;
7788 else
7790 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
7791 virtual_dwo_name.c_str ());
7793 dwo_file = (struct dwo_file *) *dwo_file_slot;
7796 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
7797 dwo_unit->dwo_file = dwo_file;
7798 dwo_unit->signature = signature;
7799 dwo_unit->section =
7800 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
7801 *dwo_unit->section = sections.info_or_types;
7802 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
7804 return dwo_unit;
7807 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
7808 simplify them. Given a pointer to the containing section SECTION, and
7809 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
7810 virtual section of just that piece. */
7812 static struct dwarf2_section_info
7813 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
7814 struct dwarf2_section_info *section,
7815 bfd_size_type offset, bfd_size_type size)
7817 struct dwarf2_section_info result;
7818 asection *sectp;
7820 gdb_assert (section != NULL);
7821 gdb_assert (!section->is_virtual);
7823 memset (&result, 0, sizeof (result));
7824 result.s.containing_section = section;
7825 result.is_virtual = true;
7827 if (size == 0)
7828 return result;
7830 sectp = section->get_bfd_section ();
7832 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
7833 bounds of the real section. This is a pretty-rare event, so just
7834 flag an error (easier) instead of a warning and trying to cope. */
7835 if (sectp == NULL
7836 || offset + size > bfd_section_size (sectp))
7838 error (_(DWARF_ERROR_PREFIX
7839 "Bad DWP V2 or V5 section info, doesn't fit in section %s"
7840 " [in module %s]"),
7841 sectp ? bfd_section_name (sectp) : "<unknown>",
7842 objfile_name (per_objfile->objfile));
7845 result.virtual_offset = offset;
7846 result.size = size;
7847 gdb_assert (section->readin);
7848 result.readin = true;
7849 result.buffer = section->buffer + offset;
7850 return result;
7853 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
7854 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
7855 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
7856 This is for DWP version 2 files. */
7858 static struct dwo_unit *
7859 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
7860 struct dwp_file *dwp_file,
7861 uint32_t unit_index,
7862 const char *comp_dir,
7863 ULONGEST signature, int is_debug_types)
7865 const struct dwp_hash_table *dwp_htab =
7866 is_debug_types ? dwp_file->tus : dwp_file->cus;
7867 bfd *dbfd = dwp_file->dbfd.get ();
7868 const char *kind = is_debug_types ? "TU" : "CU";
7869 struct dwo_file *dwo_file;
7870 struct dwo_unit *dwo_unit;
7871 struct virtual_v2_or_v5_dwo_sections sections;
7872 void **dwo_file_slot;
7873 int i;
7875 gdb_assert (dwp_file->version == 2);
7877 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
7878 kind, pulongest (unit_index), hex_string (signature),
7879 dwp_file->name);
7881 /* Fetch the section offsets of this DWO unit. */
7883 memset (&sections, 0, sizeof (sections));
7885 for (i = 0; i < dwp_htab->nr_columns; ++i)
7887 uint32_t offset = read_4_bytes (dbfd,
7888 dwp_htab->section_pool.v2.offsets
7889 + (((unit_index - 1) * dwp_htab->nr_columns
7890 + i)
7891 * sizeof (uint32_t)));
7892 uint32_t size = read_4_bytes (dbfd,
7893 dwp_htab->section_pool.v2.sizes
7894 + (((unit_index - 1) * dwp_htab->nr_columns
7895 + i)
7896 * sizeof (uint32_t)));
7898 switch (dwp_htab->section_pool.v2.section_ids[i])
7900 case DW_SECT_INFO:
7901 case DW_SECT_TYPES:
7902 sections.info_or_types_offset = offset;
7903 sections.info_or_types_size = size;
7904 break;
7905 case DW_SECT_ABBREV:
7906 sections.abbrev_offset = offset;
7907 sections.abbrev_size = size;
7908 break;
7909 case DW_SECT_LINE:
7910 sections.line_offset = offset;
7911 sections.line_size = size;
7912 break;
7913 case DW_SECT_LOC:
7914 sections.loc_offset = offset;
7915 sections.loc_size = size;
7916 break;
7917 case DW_SECT_STR_OFFSETS:
7918 sections.str_offsets_offset = offset;
7919 sections.str_offsets_size = size;
7920 break;
7921 case DW_SECT_MACINFO:
7922 sections.macinfo_offset = offset;
7923 sections.macinfo_size = size;
7924 break;
7925 case DW_SECT_MACRO:
7926 sections.macro_offset = offset;
7927 sections.macro_size = size;
7928 break;
7932 /* It's easier for the rest of the code if we fake a struct dwo_file and
7933 have dwo_unit "live" in that. At least for now.
7935 The DWP file can be made up of a random collection of CUs and TUs.
7936 However, for each CU + set of TUs that came from the same original DWO
7937 file, we can combine them back into a virtual DWO file to save space
7938 (fewer struct dwo_file objects to allocate). Remember that for really
7939 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
7941 std::string virtual_dwo_name =
7942 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
7943 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
7944 (long) (sections.line_size ? sections.line_offset : 0),
7945 (long) (sections.loc_size ? sections.loc_offset : 0),
7946 (long) (sections.str_offsets_size
7947 ? sections.str_offsets_offset : 0));
7948 /* Can we use an existing virtual DWO file? */
7949 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
7950 comp_dir);
7951 /* Create one if necessary. */
7952 if (*dwo_file_slot == NULL)
7954 dwarf_read_debug_printf ("Creating virtual DWO: %s",
7955 virtual_dwo_name.c_str ());
7957 dwo_file = new struct dwo_file;
7958 dwo_file->dwo_name = std::move (virtual_dwo_name);
7959 dwo_file->comp_dir = comp_dir;
7960 dwo_file->sections.abbrev =
7961 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
7962 sections.abbrev_offset,
7963 sections.abbrev_size);
7964 dwo_file->sections.line =
7965 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
7966 sections.line_offset,
7967 sections.line_size);
7968 dwo_file->sections.loc =
7969 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
7970 sections.loc_offset, sections.loc_size);
7971 dwo_file->sections.macinfo =
7972 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
7973 sections.macinfo_offset,
7974 sections.macinfo_size);
7975 dwo_file->sections.macro =
7976 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
7977 sections.macro_offset,
7978 sections.macro_size);
7979 dwo_file->sections.str_offsets =
7980 create_dwp_v2_or_v5_section (per_objfile,
7981 &dwp_file->sections.str_offsets,
7982 sections.str_offsets_offset,
7983 sections.str_offsets_size);
7984 /* The "str" section is global to the entire DWP file. */
7985 dwo_file->sections.str = dwp_file->sections.str;
7986 /* The info or types section is assigned below to dwo_unit,
7987 there's no need to record it in dwo_file.
7988 Also, we can't simply record type sections in dwo_file because
7989 we record a pointer into the vector in dwo_unit. As we collect more
7990 types we'll grow the vector and eventually have to reallocate space
7991 for it, invalidating all copies of pointers into the previous
7992 contents. */
7993 *dwo_file_slot = dwo_file;
7995 else
7997 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
7998 virtual_dwo_name.c_str ());
8000 dwo_file = (struct dwo_file *) *dwo_file_slot;
8003 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8004 dwo_unit->dwo_file = dwo_file;
8005 dwo_unit->signature = signature;
8006 dwo_unit->section =
8007 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8008 *dwo_unit->section = create_dwp_v2_or_v5_section
8009 (per_objfile,
8010 is_debug_types
8011 ? &dwp_file->sections.types
8012 : &dwp_file->sections.info,
8013 sections.info_or_types_offset,
8014 sections.info_or_types_size);
8015 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8017 return dwo_unit;
8020 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8021 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8022 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8023 This is for DWP version 5 files. */
8025 static struct dwo_unit *
8026 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
8027 struct dwp_file *dwp_file,
8028 uint32_t unit_index,
8029 const char *comp_dir,
8030 ULONGEST signature, int is_debug_types)
8032 const struct dwp_hash_table *dwp_htab
8033 = is_debug_types ? dwp_file->tus : dwp_file->cus;
8034 bfd *dbfd = dwp_file->dbfd.get ();
8035 const char *kind = is_debug_types ? "TU" : "CU";
8036 struct dwo_file *dwo_file;
8037 struct dwo_unit *dwo_unit;
8038 struct virtual_v2_or_v5_dwo_sections sections {};
8039 void **dwo_file_slot;
8041 gdb_assert (dwp_file->version == 5);
8043 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8044 kind, pulongest (unit_index), hex_string (signature),
8045 dwp_file->name);
8047 /* Fetch the section offsets of this DWO unit. */
8049 /* memset (&sections, 0, sizeof (sections)); */
8051 for (int i = 0; i < dwp_htab->nr_columns; ++i)
8053 uint32_t offset = read_4_bytes (dbfd,
8054 dwp_htab->section_pool.v5.offsets
8055 + (((unit_index - 1)
8056 * dwp_htab->nr_columns
8057 + i)
8058 * sizeof (uint32_t)));
8059 uint32_t size = read_4_bytes (dbfd,
8060 dwp_htab->section_pool.v5.sizes
8061 + (((unit_index - 1) * dwp_htab->nr_columns
8062 + i)
8063 * sizeof (uint32_t)));
8065 switch (dwp_htab->section_pool.v5.section_ids[i])
8067 case DW_SECT_ABBREV_V5:
8068 sections.abbrev_offset = offset;
8069 sections.abbrev_size = size;
8070 break;
8071 case DW_SECT_INFO_V5:
8072 sections.info_or_types_offset = offset;
8073 sections.info_or_types_size = size;
8074 break;
8075 case DW_SECT_LINE_V5:
8076 sections.line_offset = offset;
8077 sections.line_size = size;
8078 break;
8079 case DW_SECT_LOCLISTS_V5:
8080 sections.loclists_offset = offset;
8081 sections.loclists_size = size;
8082 break;
8083 case DW_SECT_MACRO_V5:
8084 sections.macro_offset = offset;
8085 sections.macro_size = size;
8086 break;
8087 case DW_SECT_RNGLISTS_V5:
8088 sections.rnglists_offset = offset;
8089 sections.rnglists_size = size;
8090 break;
8091 case DW_SECT_STR_OFFSETS_V5:
8092 sections.str_offsets_offset = offset;
8093 sections.str_offsets_size = size;
8094 break;
8095 case DW_SECT_RESERVED_V5:
8096 default:
8097 break;
8101 /* It's easier for the rest of the code if we fake a struct dwo_file and
8102 have dwo_unit "live" in that. At least for now.
8104 The DWP file can be made up of a random collection of CUs and TUs.
8105 However, for each CU + set of TUs that came from the same original DWO
8106 file, we can combine them back into a virtual DWO file to save space
8107 (fewer struct dwo_file objects to allocate). Remember that for really
8108 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8110 std::string virtual_dwo_name =
8111 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8112 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
8113 (long) (sections.line_size ? sections.line_offset : 0),
8114 (long) (sections.loclists_size ? sections.loclists_offset : 0),
8115 (long) (sections.str_offsets_size
8116 ? sections.str_offsets_offset : 0),
8117 (long) (sections.macro_size ? sections.macro_offset : 0),
8118 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
8119 /* Can we use an existing virtual DWO file? */
8120 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
8121 virtual_dwo_name.c_str (),
8122 comp_dir);
8123 /* Create one if necessary. */
8124 if (*dwo_file_slot == NULL)
8126 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8127 virtual_dwo_name.c_str ());
8129 dwo_file = new struct dwo_file;
8130 dwo_file->dwo_name = std::move (virtual_dwo_name);
8131 dwo_file->comp_dir = comp_dir;
8132 dwo_file->sections.abbrev =
8133 create_dwp_v2_or_v5_section (per_objfile,
8134 &dwp_file->sections.abbrev,
8135 sections.abbrev_offset,
8136 sections.abbrev_size);
8137 dwo_file->sections.line =
8138 create_dwp_v2_or_v5_section (per_objfile,
8139 &dwp_file->sections.line,
8140 sections.line_offset, sections.line_size);
8141 dwo_file->sections.macro =
8142 create_dwp_v2_or_v5_section (per_objfile,
8143 &dwp_file->sections.macro,
8144 sections.macro_offset,
8145 sections.macro_size);
8146 dwo_file->sections.loclists =
8147 create_dwp_v2_or_v5_section (per_objfile,
8148 &dwp_file->sections.loclists,
8149 sections.loclists_offset,
8150 sections.loclists_size);
8151 dwo_file->sections.rnglists =
8152 create_dwp_v2_or_v5_section (per_objfile,
8153 &dwp_file->sections.rnglists,
8154 sections.rnglists_offset,
8155 sections.rnglists_size);
8156 dwo_file->sections.str_offsets =
8157 create_dwp_v2_or_v5_section (per_objfile,
8158 &dwp_file->sections.str_offsets,
8159 sections.str_offsets_offset,
8160 sections.str_offsets_size);
8161 /* The "str" section is global to the entire DWP file. */
8162 dwo_file->sections.str = dwp_file->sections.str;
8163 /* The info or types section is assigned below to dwo_unit,
8164 there's no need to record it in dwo_file.
8165 Also, we can't simply record type sections in dwo_file because
8166 we record a pointer into the vector in dwo_unit. As we collect more
8167 types we'll grow the vector and eventually have to reallocate space
8168 for it, invalidating all copies of pointers into the previous
8169 contents. */
8170 *dwo_file_slot = dwo_file;
8172 else
8174 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8175 virtual_dwo_name.c_str ());
8177 dwo_file = (struct dwo_file *) *dwo_file_slot;
8180 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
8181 dwo_unit->dwo_file = dwo_file;
8182 dwo_unit->signature = signature;
8183 dwo_unit->section
8184 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8185 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
8186 &dwp_file->sections.info,
8187 sections.info_or_types_offset,
8188 sections.info_or_types_size);
8189 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8191 return dwo_unit;
8194 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
8195 Returns NULL if the signature isn't found. */
8197 static struct dwo_unit *
8198 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
8199 struct dwp_file *dwp_file, const char *comp_dir,
8200 ULONGEST signature, int is_debug_types)
8202 const struct dwp_hash_table *dwp_htab =
8203 is_debug_types ? dwp_file->tus : dwp_file->cus;
8204 bfd *dbfd = dwp_file->dbfd.get ();
8205 uint32_t mask = dwp_htab->nr_slots - 1;
8206 uint32_t hash = signature & mask;
8207 uint32_t hash2 = ((signature >> 32) & mask) | 1;
8208 unsigned int i;
8209 void **slot;
8210 struct dwo_unit find_dwo_cu;
8212 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8213 find_dwo_cu.signature = signature;
8214 slot = htab_find_slot (is_debug_types
8215 ? dwp_file->loaded_tus.get ()
8216 : dwp_file->loaded_cus.get (),
8217 &find_dwo_cu, INSERT);
8219 if (*slot != NULL)
8220 return (struct dwo_unit *) *slot;
8222 /* Use a for loop so that we don't loop forever on bad debug info. */
8223 for (i = 0; i < dwp_htab->nr_slots; ++i)
8225 ULONGEST signature_in_table;
8227 signature_in_table =
8228 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
8229 if (signature_in_table == signature)
8231 uint32_t unit_index =
8232 read_4_bytes (dbfd,
8233 dwp_htab->unit_table + hash * sizeof (uint32_t));
8235 if (dwp_file->version == 1)
8237 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
8238 unit_index, comp_dir,
8239 signature, is_debug_types);
8241 else if (dwp_file->version == 2)
8243 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
8244 unit_index, comp_dir,
8245 signature, is_debug_types);
8247 else /* version == 5 */
8249 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
8250 unit_index, comp_dir,
8251 signature, is_debug_types);
8253 return (struct dwo_unit *) *slot;
8255 if (signature_in_table == 0)
8256 return NULL;
8257 hash = (hash + hash2) & mask;
8260 error (_(DWARF_ERROR_PREFIX
8261 "bad DWP hash table, lookup didn't terminate [in module %s]"),
8262 dwp_file->name);
8265 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8266 Open the file specified by FILE_NAME and hand it off to BFD for
8267 preliminary analysis. Return a newly initialized bfd *, which
8268 includes a canonicalized copy of FILE_NAME.
8269 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8270 SEARCH_CWD is true if the current directory is to be searched.
8271 It will be searched before debug-file-directory.
8272 If successful, the file is added to the bfd include table of the
8273 objfile's bfd (see gdb_bfd_record_inclusion).
8274 If unable to find/open the file, return NULL.
8275 NOTE: This function is derived from symfile_bfd_open. */
8277 static gdb_bfd_ref_ptr
8278 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
8279 const char *file_name, int is_dwp, int search_cwd)
8281 int desc;
8282 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
8283 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
8284 to debug_file_directory. */
8285 const char *search_path;
8286 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
8288 gdb::unique_xmalloc_ptr<char> search_path_holder;
8289 if (search_cwd)
8291 const std::string &debug_dir = per_objfile->per_bfd->captured_debug_dir;
8293 if (!debug_dir.empty ())
8295 search_path_holder.reset (concat (".", dirname_separator_string,
8296 debug_dir.c_str (),
8297 (char *) NULL));
8298 search_path = search_path_holder.get ();
8300 else
8301 search_path = ".";
8303 else
8304 search_path = per_objfile->per_bfd->captured_debug_dir.c_str ();
8306 /* Add the path for the executable binary to the list of search paths. */
8307 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
8308 search_path_holder.reset (concat (objfile_dir.c_str (),
8309 dirname_separator_string,
8310 search_path, nullptr));
8311 search_path = search_path_holder.get ();
8313 openp_flags flags = OPF_RETURN_REALPATH;
8314 if (is_dwp)
8315 flags |= OPF_SEARCH_IN_PATH;
8317 gdb::unique_xmalloc_ptr<char> absolute_name;
8318 desc = openp (search_path, flags, file_name,
8319 O_RDONLY | O_BINARY, &absolute_name,
8320 per_objfile->per_bfd->captured_cwd.c_str ());
8321 if (desc < 0)
8322 return NULL;
8324 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
8325 gnutarget, desc));
8326 if (sym_bfd == NULL)
8327 return NULL;
8329 if (!bfd_check_format (sym_bfd.get (), bfd_object))
8330 return NULL;
8332 /* Success. Record the bfd as having been included by the objfile's bfd.
8333 This is important because things like demangled_names_hash lives in the
8334 objfile's per_bfd space and may have references to things like symbol
8335 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
8336 gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
8338 return sym_bfd;
8341 /* Try to open DWO file FILE_NAME.
8342 COMP_DIR is the DW_AT_comp_dir attribute.
8343 The result is the bfd handle of the file.
8344 If there is a problem finding or opening the file, return NULL.
8345 Upon success, the canonicalized path of the file is stored in the bfd,
8346 same as symfile_bfd_open. */
8348 static gdb_bfd_ref_ptr
8349 open_dwo_file (dwarf2_per_objfile *per_objfile,
8350 const char *file_name, const char *comp_dir)
8352 if (IS_ABSOLUTE_PATH (file_name))
8353 return try_open_dwop_file (per_objfile, file_name,
8354 0 /*is_dwp*/, 0 /*search_cwd*/);
8356 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
8358 if (comp_dir != NULL)
8360 std::string path_to_try = path_join (comp_dir, file_name);
8362 /* NOTE: If comp_dir is a relative path, this will also try the
8363 search path, which seems useful. */
8364 gdb_bfd_ref_ptr abfd (try_open_dwop_file
8365 (per_objfile, path_to_try.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
8367 if (abfd != NULL)
8368 return abfd;
8371 /* That didn't work, try debug-file-directory, which, despite its name,
8372 is a list of paths. */
8374 if (per_objfile->per_bfd->captured_debug_dir.empty ())
8375 return NULL;
8377 return try_open_dwop_file (per_objfile, file_name,
8378 0 /*is_dwp*/, 1 /*search_cwd*/);
8381 /* This function is mapped across the sections and remembers the offset and
8382 size of each of the DWO debugging sections we are interested in. */
8384 static void
8385 dwarf2_locate_dwo_sections (struct objfile *objfile, bfd *abfd,
8386 asection *sectp, dwo_sections *dwo_sections)
8388 const struct dwop_section_names *names = &dwop_section_names;
8390 struct dwarf2_section_info *dw_sect = nullptr;
8392 if (names->abbrev_dwo.matches (sectp->name))
8393 dw_sect = &dwo_sections->abbrev;
8394 else if (names->info_dwo.matches (sectp->name))
8395 dw_sect = &dwo_sections->info;
8396 else if (names->line_dwo.matches (sectp->name))
8397 dw_sect = &dwo_sections->line;
8398 else if (names->loc_dwo.matches (sectp->name))
8399 dw_sect = &dwo_sections->loc;
8400 else if (names->loclists_dwo.matches (sectp->name))
8401 dw_sect = &dwo_sections->loclists;
8402 else if (names->macinfo_dwo.matches (sectp->name))
8403 dw_sect = &dwo_sections->macinfo;
8404 else if (names->macro_dwo.matches (sectp->name))
8405 dw_sect = &dwo_sections->macro;
8406 else if (names->rnglists_dwo.matches (sectp->name))
8407 dw_sect = &dwo_sections->rnglists;
8408 else if (names->str_dwo.matches (sectp->name))
8409 dw_sect = &dwo_sections->str;
8410 else if (names->str_offsets_dwo.matches (sectp->name))
8411 dw_sect = &dwo_sections->str_offsets;
8412 else if (names->types_dwo.matches (sectp->name))
8414 struct dwarf2_section_info type_section;
8416 memset (&type_section, 0, sizeof (type_section));
8417 dwo_sections->types.push_back (type_section);
8418 dw_sect = &dwo_sections->types.back ();
8421 if (dw_sect != nullptr)
8423 dw_sect->s.section = sectp;
8424 dw_sect->size = bfd_section_size (sectp);
8425 dw_sect->read (objfile);
8429 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
8430 by PER_CU. This is for the non-DWP case.
8431 The result is NULL if DWO_NAME can't be found. */
8433 static struct dwo_file *
8434 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
8435 const char *comp_dir)
8437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8439 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
8440 if (dbfd == NULL)
8442 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
8444 return NULL;
8447 dwo_file_up dwo_file (new struct dwo_file);
8448 dwo_file->dwo_name = dwo_name;
8449 dwo_file->comp_dir = comp_dir;
8450 dwo_file->dbfd = std::move (dbfd);
8452 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
8453 dwarf2_locate_dwo_sections (per_objfile->objfile, dwo_file->dbfd.get (),
8454 sec, &dwo_file->sections);
8456 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
8457 dwo_file->cus);
8459 if (cu->per_cu->version () < 5)
8461 create_debug_types_hash_table (per_objfile, dwo_file.get (),
8462 dwo_file->sections.types, dwo_file->tus);
8464 else
8466 create_debug_type_hash_table (per_objfile, dwo_file.get (),
8467 &dwo_file->sections.info, dwo_file->tus,
8468 rcuh_kind::COMPILE);
8471 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
8473 bfd_cache_close (dwo_file->dbfd.get ());
8475 return dwo_file.release ();
8478 /* This function is mapped across the sections and remembers the offset and
8479 size of each of the DWP debugging sections common to version 1 and 2 that
8480 we are interested in. */
8482 static void
8483 dwarf2_locate_common_dwp_sections (struct objfile *objfile, bfd *abfd,
8484 asection *sectp, dwp_file *dwp_file)
8486 const struct dwop_section_names *names = &dwop_section_names;
8487 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8489 /* Record the ELF section number for later lookup: this is what the
8490 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
8491 gdb_assert (elf_section_nr < dwp_file->num_sections);
8492 dwp_file->elf_sections[elf_section_nr] = sectp;
8494 /* Look for specific sections that we need. */
8495 struct dwarf2_section_info *dw_sect = nullptr;
8496 if (names->str_dwo.matches (sectp->name))
8497 dw_sect = &dwp_file->sections.str;
8498 else if (names->cu_index.matches (sectp->name))
8499 dw_sect = &dwp_file->sections.cu_index;
8500 else if (names->tu_index.matches (sectp->name))
8501 dw_sect = &dwp_file->sections.tu_index;
8503 if (dw_sect != nullptr)
8505 dw_sect->s.section = sectp;
8506 dw_sect->size = bfd_section_size (sectp);
8507 dw_sect->read (objfile);
8511 /* This function is mapped across the sections and remembers the offset and
8512 size of each of the DWP version 2 debugging sections that we are interested
8513 in. This is split into a separate function because we don't know if we
8514 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
8516 static void
8517 dwarf2_locate_v2_dwp_sections (struct objfile *objfile, bfd *abfd,
8518 asection *sectp, void *dwp_file_ptr)
8520 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
8521 const struct dwop_section_names *names = &dwop_section_names;
8522 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8524 /* Record the ELF section number for later lookup: this is what the
8525 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
8526 gdb_assert (elf_section_nr < dwp_file->num_sections);
8527 dwp_file->elf_sections[elf_section_nr] = sectp;
8529 /* Look for specific sections that we need. */
8530 struct dwarf2_section_info *dw_sect = nullptr;
8531 if (names->abbrev_dwo.matches (sectp->name))
8532 dw_sect = &dwp_file->sections.abbrev;
8533 else if (names->info_dwo.matches (sectp->name))
8534 dw_sect = &dwp_file->sections.info;
8535 else if (names->line_dwo.matches (sectp->name))
8536 dw_sect = &dwp_file->sections.line;
8537 else if (names->loc_dwo.matches (sectp->name))
8538 dw_sect = &dwp_file->sections.loc;
8539 else if (names->macinfo_dwo.matches (sectp->name))
8540 dw_sect = &dwp_file->sections.macinfo;
8541 else if (names->macro_dwo.matches (sectp->name))
8542 dw_sect = &dwp_file->sections.macro;
8543 else if (names->str_offsets_dwo.matches (sectp->name))
8544 dw_sect = &dwp_file->sections.str_offsets;
8545 else if (names->types_dwo.matches (sectp->name))
8546 dw_sect = &dwp_file->sections.types;
8548 if (dw_sect != nullptr)
8550 dw_sect->s.section = sectp;
8551 dw_sect->size = bfd_section_size (sectp);
8552 dw_sect->read (objfile);
8556 /* This function is mapped across the sections and remembers the offset and
8557 size of each of the DWP version 5 debugging sections that we are interested
8558 in. This is split into a separate function because we don't know if we
8559 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
8561 static void
8562 dwarf2_locate_v5_dwp_sections (struct objfile *objfile, bfd *abfd,
8563 asection *sectp, void *dwp_file_ptr)
8565 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
8566 const struct dwop_section_names *names = &dwop_section_names;
8567 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8569 /* Record the ELF section number for later lookup: this is what the
8570 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
8571 gdb_assert (elf_section_nr < dwp_file->num_sections);
8572 dwp_file->elf_sections[elf_section_nr] = sectp;
8574 /* Look for specific sections that we need. */
8575 struct dwarf2_section_info *dw_sect = nullptr;
8576 if (names->abbrev_dwo.matches (sectp->name))
8577 dw_sect = &dwp_file->sections.abbrev;
8578 else if (names->info_dwo.matches (sectp->name))
8579 dw_sect = &dwp_file->sections.info;
8580 else if (names->line_dwo.matches (sectp->name))
8581 dw_sect = &dwp_file->sections.line;
8582 else if (names->loclists_dwo.matches (sectp->name))
8583 dw_sect = &dwp_file->sections.loclists;
8584 else if (names->macro_dwo.matches (sectp->name))
8585 dw_sect = &dwp_file->sections.macro;
8586 else if (names->rnglists_dwo.matches (sectp->name))
8587 dw_sect = &dwp_file->sections.rnglists;
8588 else if (names->str_offsets_dwo.matches (sectp->name))
8589 dw_sect = &dwp_file->sections.str_offsets;
8591 if (dw_sect != nullptr)
8593 dw_sect->s.section = sectp;
8594 dw_sect->size = bfd_section_size (sectp);
8595 dw_sect->read (objfile);
8599 /* Hash function for dwp_file loaded CUs/TUs. */
8601 static hashval_t
8602 hash_dwp_loaded_cutus (const void *item)
8604 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
8606 /* This drops the top 32 bits of the signature, but is ok for a hash. */
8607 return dwo_unit->signature;
8610 /* Equality function for dwp_file loaded CUs/TUs. */
8612 static int
8613 eq_dwp_loaded_cutus (const void *a, const void *b)
8615 const struct dwo_unit *dua = (const struct dwo_unit *) a;
8616 const struct dwo_unit *dub = (const struct dwo_unit *) b;
8618 return dua->signature == dub->signature;
8621 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
8623 static htab_up
8624 allocate_dwp_loaded_cutus_table ()
8626 return htab_up (htab_create_alloc (3,
8627 hash_dwp_loaded_cutus,
8628 eq_dwp_loaded_cutus,
8629 NULL, xcalloc, xfree));
8632 /* Try to open DWP file FILE_NAME.
8633 The result is the bfd handle of the file.
8634 If there is a problem finding or opening the file, return NULL.
8635 Upon success, the canonicalized path of the file is stored in the bfd,
8636 same as symfile_bfd_open. */
8638 static gdb_bfd_ref_ptr
8639 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
8641 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
8642 1 /*is_dwp*/,
8643 1 /*search_cwd*/));
8644 if (abfd != NULL)
8645 return abfd;
8647 /* Work around upstream bug 15652.
8648 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
8649 [Whether that's a "bug" is debatable, but it is getting in our way.]
8650 We have no real idea where the dwp file is, because gdb's realpath-ing
8651 of the executable's path may have discarded the needed info.
8652 [IWBN if the dwp file name was recorded in the executable, akin to
8653 .gnu_debuglink, but that doesn't exist yet.]
8654 Strip the directory from FILE_NAME and search again. */
8655 if (!per_objfile->per_bfd->captured_debug_dir.empty ())
8657 /* Don't implicitly search the current directory here.
8658 If the user wants to search "." to handle this case,
8659 it must be added to debug-file-directory. */
8660 return try_open_dwop_file (per_objfile, lbasename (file_name),
8661 1 /*is_dwp*/,
8662 0 /*search_cwd*/);
8665 return NULL;
8668 /* Initialize the use of the DWP file for the current objfile.
8669 By convention the name of the DWP file is ${objfile}.dwp.
8670 The result is NULL if it can't be found. */
8672 static std::unique_ptr<struct dwp_file>
8673 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
8675 struct objfile *objfile = per_objfile->objfile;
8677 /* Try to find first .dwp for the binary file before any symbolic links
8678 resolving. */
8680 /* If the objfile is a debug file, find the name of the real binary
8681 file and get the name of dwp file from there. */
8682 std::string dwp_name;
8683 if (objfile->separate_debug_objfile_backlink != NULL)
8685 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
8686 const char *backlink_basename = lbasename (backlink->original_name);
8688 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
8690 else
8691 dwp_name = objfile->original_name;
8693 dwp_name += ".dwp";
8695 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
8696 if (dbfd == NULL
8697 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
8699 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
8700 dwp_name = objfile_name (objfile);
8701 dwp_name += ".dwp";
8702 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
8705 if (dbfd == NULL)
8707 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
8709 return std::unique_ptr<dwp_file> ();
8712 const char *name = bfd_get_filename (dbfd.get ());
8713 std::unique_ptr<struct dwp_file> dwp_file
8714 (new struct dwp_file (name, std::move (dbfd)));
8716 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
8717 dwp_file->elf_sections =
8718 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
8719 dwp_file->num_sections, asection *);
8721 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
8722 dwarf2_locate_common_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
8723 dwp_file.get ());
8725 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
8727 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
8729 /* The DWP file version is stored in the hash table. Oh well. */
8730 if (dwp_file->cus && dwp_file->tus
8731 && dwp_file->cus->version != dwp_file->tus->version)
8733 /* Technically speaking, we should try to limp along, but this is
8734 pretty bizarre. We use pulongest here because that's the established
8735 portability solution (e.g, we cannot use %u for uint32_t). */
8736 error (_(DWARF_ERROR_PREFIX
8737 "DWP file CU version %s doesn't match TU version %s"
8738 " [in DWP file %s]"),
8739 pulongest (dwp_file->cus->version),
8740 pulongest (dwp_file->tus->version), dwp_name.c_str ());
8743 if (dwp_file->cus)
8744 dwp_file->version = dwp_file->cus->version;
8745 else if (dwp_file->tus)
8746 dwp_file->version = dwp_file->tus->version;
8747 else
8748 dwp_file->version = 2;
8750 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
8752 if (dwp_file->version == 2)
8753 dwarf2_locate_v2_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
8754 dwp_file.get ());
8755 else
8756 dwarf2_locate_v5_dwp_sections (objfile, dwp_file->dbfd.get (), sec,
8757 dwp_file.get ());
8760 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
8761 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
8763 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
8764 dwarf_read_debug_printf (" %s CUs, %s TUs",
8765 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
8766 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
8768 bfd_cache_close (dwp_file->dbfd.get ());
8770 return dwp_file;
8773 /* Wrapper around open_and_init_dwp_file, only open it once. */
8775 static struct dwp_file *
8776 get_dwp_file (dwarf2_per_objfile *per_objfile)
8778 if (!per_objfile->per_bfd->dwp_checked)
8780 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
8781 per_objfile->per_bfd->dwp_checked = 1;
8783 return per_objfile->per_bfd->dwp_file.get ();
8786 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
8787 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
8788 or in the DWP file for the objfile, referenced by THIS_UNIT.
8789 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8790 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
8792 This is called, for example, when wanting to read a variable with a
8793 complex location. Therefore we don't want to do file i/o for every call.
8794 Therefore we don't want to look for a DWO file on every call.
8795 Therefore we first see if we've already seen SIGNATURE in a DWP file,
8796 then we check if we've already seen DWO_NAME, and only THEN do we check
8797 for a DWO file.
8799 The result is a pointer to the dwo_unit object or NULL if we didn't find it
8800 (dwo_id mismatch or couldn't find the DWO/DWP file). */
8802 static struct dwo_unit *
8803 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
8804 ULONGEST signature, int is_debug_types)
8806 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8807 struct objfile *objfile = per_objfile->objfile;
8808 const char *kind = is_debug_types ? "TU" : "CU";
8809 void **dwo_file_slot;
8810 struct dwo_file *dwo_file;
8811 struct dwp_file *dwp_file;
8813 /* First see if there's a DWP file.
8814 If we have a DWP file but didn't find the DWO inside it, don't
8815 look for the original DWO file. It makes gdb behave differently
8816 depending on whether one is debugging in the build tree. */
8818 dwp_file = get_dwp_file (per_objfile);
8819 if (dwp_file != NULL)
8821 const struct dwp_hash_table *dwp_htab =
8822 is_debug_types ? dwp_file->tus : dwp_file->cus;
8824 if (dwp_htab != NULL)
8826 struct dwo_unit *dwo_cutu =
8827 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
8828 is_debug_types);
8830 if (dwo_cutu != NULL)
8832 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
8833 kind, hex_string (signature),
8834 host_address_to_string (dwo_cutu));
8836 return dwo_cutu;
8840 else
8842 /* No DWP file, look for the DWO file. */
8844 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
8845 if (*dwo_file_slot == NULL)
8847 /* Read in the file and build a table of the CUs/TUs it contains. */
8848 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
8850 /* NOTE: This will be NULL if unable to open the file. */
8851 dwo_file = (struct dwo_file *) *dwo_file_slot;
8853 if (dwo_file != NULL)
8855 struct dwo_unit *dwo_cutu = NULL;
8857 if (is_debug_types && dwo_file->tus)
8859 struct dwo_unit find_dwo_cutu;
8861 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
8862 find_dwo_cutu.signature = signature;
8863 dwo_cutu
8864 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
8865 &find_dwo_cutu);
8867 else if (!is_debug_types && dwo_file->cus)
8869 struct dwo_unit find_dwo_cutu;
8871 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
8872 find_dwo_cutu.signature = signature;
8873 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
8874 &find_dwo_cutu);
8877 if (dwo_cutu != NULL)
8879 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
8880 kind, dwo_name, hex_string (signature),
8881 host_address_to_string (dwo_cutu));
8883 return dwo_cutu;
8888 /* We didn't find it. This could mean a dwo_id mismatch, or
8889 someone deleted the DWO/DWP file, or the search path isn't set up
8890 correctly to find the file. */
8892 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
8893 kind, dwo_name, hex_string (signature));
8895 /* This is a warning and not a complaint because it can be caused by
8896 pilot error (e.g., user accidentally deleting the DWO). */
8898 /* Print the name of the DWP file if we looked there, helps the user
8899 better diagnose the problem. */
8900 std::string dwp_text;
8902 if (dwp_file != NULL)
8903 dwp_text = string_printf (" [in DWP file %s]",
8904 lbasename (dwp_file->name));
8906 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
8907 " [in module %s]"),
8908 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
8909 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
8911 return NULL;
8914 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
8915 See lookup_dwo_cutu_unit for details. */
8917 static struct dwo_unit *
8918 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
8919 ULONGEST signature)
8921 gdb_assert (!cu->per_cu->is_debug_types);
8923 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
8926 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
8927 See lookup_dwo_cutu_unit for details. */
8929 static struct dwo_unit *
8930 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
8932 gdb_assert (cu->per_cu->is_debug_types);
8934 signatured_type *sig_type = (signatured_type *) cu->per_cu;
8936 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
8939 /* Traversal function for queue_and_load_all_dwo_tus. */
8941 static int
8942 queue_and_load_dwo_tu (void **slot, void *info)
8944 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8945 dwarf2_cu *cu = (dwarf2_cu *) info;
8946 ULONGEST signature = dwo_unit->signature;
8947 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
8949 if (sig_type != NULL)
8951 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
8952 a real dependency of PER_CU on SIG_TYPE. That is detected later
8953 while processing PER_CU. */
8954 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
8955 cu->lang ()))
8956 load_full_type_unit (sig_type, cu->per_objfile);
8957 cu->per_cu->imported_symtabs.push_back (sig_type);
8960 return 1;
8963 /* Queue all TUs contained in the DWO of CU to be read in.
8964 The DWO may have the only definition of the type, though it may not be
8965 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
8966 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
8968 static void
8969 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
8971 struct dwo_unit *dwo_unit;
8972 struct dwo_file *dwo_file;
8974 gdb_assert (cu != nullptr);
8975 gdb_assert (!cu->per_cu->is_debug_types);
8976 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
8978 dwo_unit = cu->dwo_unit;
8979 gdb_assert (dwo_unit != NULL);
8981 dwo_file = dwo_unit->dwo_file;
8982 if (dwo_file->tus != NULL)
8983 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
8986 /* Read in various DIEs. */
8988 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8989 Inherit only the children of the DW_AT_abstract_origin DIE not being
8990 already referenced by DW_AT_abstract_origin from the children of the
8991 current DIE. */
8993 static void
8994 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8996 attribute *attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8997 if (attr == nullptr)
8998 return;
9000 /* Note that following die references may follow to a die in a
9001 different CU. */
9002 dwarf2_cu *origin_cu = cu;
9004 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9005 die_info *origin_die = follow_die_ref (die, attr, &origin_cu);
9007 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9008 symbols in. */
9009 struct pending **origin_previous_list_in_scope = origin_cu->list_in_scope;
9010 origin_cu->list_in_scope = cu->list_in_scope;
9012 if (die->tag != origin_die->tag
9013 && !(die->tag == DW_TAG_inlined_subroutine
9014 && origin_die->tag == DW_TAG_subprogram))
9015 complaint (_("DIE %s and its abstract origin %s have different tags"),
9016 sect_offset_str (die->sect_off),
9017 sect_offset_str (origin_die->sect_off));
9019 /* Find if the concrete and abstract trees are structurally the
9020 same. This is a shallow traversal and it is not bullet-proof;
9021 the compiler can trick the debugger into believing that the trees
9022 are isomorphic, whereas they actually are not. However, the
9023 likelihood of this happening is pretty low, and a full-fledged
9024 check would be an overkill. */
9025 bool are_isomorphic = true;
9026 die_info *concrete_child = die->child;
9027 die_info *abstract_child = origin_die->child;
9028 while (concrete_child != nullptr || abstract_child != nullptr)
9030 if (concrete_child == nullptr
9031 || abstract_child == nullptr
9032 || concrete_child->tag != abstract_child->tag)
9034 are_isomorphic = false;
9035 break;
9038 concrete_child = concrete_child->sibling;
9039 abstract_child = abstract_child->sibling;
9042 /* Walk the origin's children in parallel to the concrete children.
9043 This helps match an origin child in case the debug info misses
9044 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9045 origin tree may not have the same tree structure as the concrete
9046 DIE, though. */
9047 die_info *corresponding_abstract_child
9048 = are_isomorphic ? origin_die->child : nullptr;
9050 std::vector<sect_offset> offsets;
9052 for (die_info *child_die = die->child;
9053 child_die && child_die->tag;
9054 child_die = child_die->sibling)
9056 /* We are trying to process concrete instance entries:
9057 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9058 it's not relevant to our analysis here. i.e. detecting DIEs that are
9059 present in the abstract instance but not referenced in the concrete
9060 one. */
9061 if (child_die->tag == DW_TAG_call_site
9062 || child_die->tag == DW_TAG_GNU_call_site)
9064 if (are_isomorphic)
9065 corresponding_abstract_child
9066 = corresponding_abstract_child->sibling;
9067 continue;
9070 /* For each CHILD_DIE, find the corresponding child of
9071 ORIGIN_DIE. If there is more than one layer of
9072 DW_AT_abstract_origin, follow them all; there shouldn't be,
9073 but GCC versions at least through 4.4 generate this (GCC PR
9074 40573). */
9075 die_info *child_origin_die = child_die;
9076 dwarf2_cu *child_origin_cu = cu;
9077 while (true)
9079 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9080 child_origin_cu);
9081 if (attr == nullptr)
9082 break;
9084 die_info *prev_child_origin_die = child_origin_die;
9085 child_origin_die = follow_die_ref (child_origin_die, attr,
9086 &child_origin_cu);
9088 if (prev_child_origin_die == child_origin_die)
9090 /* Handle DIE with self-reference. */
9091 break;
9095 /* If missing DW_AT_abstract_origin, try the corresponding child
9096 of the origin. Clang emits such lexical scopes. */
9097 if (child_origin_die == child_die
9098 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
9099 && are_isomorphic
9100 && child_die->tag == DW_TAG_lexical_block)
9101 child_origin_die = corresponding_abstract_child;
9103 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9104 counterpart may exist. */
9105 if (child_origin_die != child_die)
9107 if (child_die->tag != child_origin_die->tag
9108 && !(child_die->tag == DW_TAG_inlined_subroutine
9109 && child_origin_die->tag == DW_TAG_subprogram))
9110 complaint (_("Child DIE %s and its abstract origin %s have "
9111 "different tags"),
9112 sect_offset_str (child_die->sect_off),
9113 sect_offset_str (child_origin_die->sect_off));
9114 if (child_origin_die->parent != origin_die)
9115 complaint (_("Child DIE %s and its abstract origin %s have "
9116 "different parents"),
9117 sect_offset_str (child_die->sect_off),
9118 sect_offset_str (child_origin_die->sect_off));
9119 else
9120 offsets.push_back (child_origin_die->sect_off);
9123 if (are_isomorphic)
9124 corresponding_abstract_child = corresponding_abstract_child->sibling;
9127 if (!offsets.empty ())
9129 std::sort (offsets.begin (), offsets.end ());
9131 for (auto offsets_it = offsets.begin () + 1;
9132 offsets_it < offsets.end ();
9133 ++offsets_it)
9134 if (*(offsets_it - 1) == *offsets_it)
9135 complaint (_("Multiple children of DIE %s refer "
9136 "to DIE %s as their abstract origin"),
9137 sect_offset_str (die->sect_off),
9138 sect_offset_str (*offsets_it));
9141 auto offsets_it = offsets.begin ();
9142 die_info *origin_child_die = origin_die->child;
9143 while (origin_child_die != nullptr && origin_child_die->tag != 0)
9145 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9146 while (offsets_it < offsets.end ()
9147 && *offsets_it < origin_child_die->sect_off)
9148 ++offsets_it;
9150 if (offsets_it == offsets.end ()
9151 || *offsets_it > origin_child_die->sect_off)
9153 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9154 Check whether we're already processing ORIGIN_CHILD_DIE.
9155 This can happen with mutually referenced abstract_origins.
9156 PR 16581. */
9157 if (!origin_child_die->in_process)
9158 process_die (origin_child_die, origin_cu);
9161 origin_child_die = origin_child_die->sibling;
9164 origin_cu->list_in_scope = origin_previous_list_in_scope;
9166 if (cu != origin_cu)
9167 compute_delayed_physnames (origin_cu);
9170 /* Return TRUE if the given DIE is the program's "main". DWARF 4 has
9171 defined a dedicated DW_AT_main_subprogram attribute to indicate the
9172 starting function of the program, however with older versions the
9173 DW_CC_program value of the DW_AT_calling_convention attribute was
9174 used instead as the only means available. We handle both variants. */
9176 static bool
9177 dwarf2_func_is_main_p (struct die_info *die, struct dwarf2_cu *cu)
9179 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
9180 return true;
9181 struct attribute *attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
9182 return (attr != nullptr
9183 && attr->constant_value (DW_CC_normal) == DW_CC_program);
9186 /* A helper to handle Ada's "Pragma Import" feature when it is applied
9187 to a function. */
9189 static bool
9190 check_ada_pragma_import (struct die_info *die, struct dwarf2_cu *cu)
9192 /* A Pragma Import will have both a name and a linkage name. */
9193 const char *name = dwarf2_name (die, cu);
9194 if (name == nullptr)
9195 return false;
9197 const char *linkage_name = dw2_linkage_name (die, cu);
9198 /* Disallow the special Ada symbols. */
9199 if (!is_ada_import_or_export (cu, name, linkage_name))
9200 return false;
9202 /* A Pragma Import will be a declaration, while a Pragma Export will
9203 not be. */
9204 if (!die_is_declaration (die, cu))
9205 return false;
9207 new_symbol (die, read_type_die (die, cu), cu);
9208 return true;
9211 /* Apply fixups to LOW_PC and HIGH_PC due to an incorrect DIE in CU. */
9213 static void
9214 fixup_low_high_pc (struct dwarf2_cu *cu, struct die_info *die, CORE_ADDR *low_pc,
9215 CORE_ADDR *high_pc)
9217 if (die->tag != DW_TAG_subprogram)
9218 return;
9220 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9221 struct objfile *objfile = per_objfile->objfile;
9222 struct gdbarch *gdbarch = objfile->arch ();
9224 if (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_arm
9225 && producer_is_gas_ge_2_39 (cu))
9227 /* Gas version 2.39 produces DWARF for a Thumb subprogram with a low_pc
9228 attribute with the thumb bit set (PR gas/31115). Work around this. */
9229 *low_pc = gdbarch_addr_bits_remove (gdbarch, *low_pc);
9230 if (high_pc != nullptr)
9231 *high_pc = gdbarch_addr_bits_remove (gdbarch, *high_pc);
9235 static void
9236 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9238 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9239 struct objfile *objfile = per_objfile->objfile;
9240 struct gdbarch *gdbarch = objfile->arch ();
9241 struct context_stack *newobj;
9242 CORE_ADDR lowpc;
9243 CORE_ADDR highpc;
9244 struct die_info *child_die;
9245 struct attribute *attr, *call_line, *call_file;
9246 const char *name;
9247 struct block *block;
9248 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9249 std::vector<struct symbol *> template_args;
9250 struct template_symbol *templ_func = NULL;
9252 if (inlined_func)
9254 /* If we do not have call site information, we can't show the
9255 caller of this inlined function. That's too confusing, so
9256 only use the scope for local variables. */
9257 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9258 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9259 if (call_line == NULL || call_file == NULL)
9261 read_lexical_block_scope (die, cu);
9262 return;
9266 name = dwarf2_name (die, cu);
9267 if (name == nullptr)
9268 name = dw2_linkage_name (die, cu);
9270 /* Ignore functions with missing or empty names. These are actually
9271 illegal according to the DWARF standard. */
9272 if (name == NULL)
9274 complaint (_("missing name for subprogram DIE at %s"),
9275 sect_offset_str (die->sect_off));
9276 return;
9279 if (check_ada_pragma_import (die, cu))
9281 /* We already made the symbol for the Pragma Import, and because
9282 it is a declaration, we know it won't have any other
9283 important information, so we can simply return. */
9284 return;
9287 /* Ignore functions with missing or invalid low and high pc attributes. */
9288 unrelocated_addr unrel_low, unrel_high;
9289 if (dwarf2_get_pc_bounds (die, &unrel_low, &unrel_high, cu, nullptr, nullptr)
9290 <= PC_BOUNDS_INVALID)
9292 if (have_complaint ())
9294 attr = dwarf2_attr (die, DW_AT_external, cu);
9295 bool external_p = attr != nullptr && attr->as_boolean ();
9296 attr = dwarf2_attr (die, DW_AT_inline, cu);
9297 bool inlined_p
9298 = (attr != nullptr
9299 && attr->is_nonnegative ()
9300 && (attr->as_nonnegative () == DW_INL_inlined
9301 || attr->as_nonnegative () == DW_INL_declared_inlined));
9302 attr = dwarf2_attr (die, DW_AT_declaration, cu);
9303 bool decl_p = attr != nullptr && attr->as_boolean ();
9304 if (!external_p && !inlined_p && !decl_p)
9305 complaint (_("cannot get low and high bounds "
9306 "for subprogram DIE at %s"),
9307 sect_offset_str (die->sect_off));
9309 return;
9312 lowpc = per_objfile->relocate (unrel_low);
9313 highpc = per_objfile->relocate (unrel_high);
9314 fixup_low_high_pc (cu, die, &lowpc, &highpc);
9316 /* If we have any template arguments, then we must allocate a
9317 different sort of symbol. */
9318 for (child_die = die->child; child_die; child_die = child_die->sibling)
9320 if (child_die->tag == DW_TAG_template_type_param
9321 || child_die->tag == DW_TAG_template_value_param)
9323 templ_func = new (&objfile->objfile_obstack) template_symbol;
9324 templ_func->subclass = SYMBOL_TEMPLATE;
9325 break;
9329 gdb_assert (cu->get_builder () != nullptr);
9330 newobj = cu->get_builder ()->push_context (0, lowpc);
9331 newobj->name = new_symbol (die, read_type_die (die, cu), cu, templ_func);
9333 if (dwarf2_func_is_main_p (die, cu))
9334 set_objfile_main_name (objfile, newobj->name->linkage_name (),
9335 cu->lang ());
9337 /* If there is a location expression for DW_AT_frame_base, record
9338 it. */
9339 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9340 if (attr != nullptr)
9341 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
9343 /* If there is a location for the static link, record it. */
9344 newobj->static_link = NULL;
9345 attr = dwarf2_attr (die, DW_AT_static_link, cu);
9346 if (attr != nullptr)
9348 newobj->static_link
9349 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9350 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
9351 cu->addr_type ());
9354 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
9356 if (die->child != NULL)
9358 child_die = die->child;
9359 while (child_die && child_die->tag)
9361 if (child_die->tag == DW_TAG_template_type_param
9362 || child_die->tag == DW_TAG_template_value_param)
9364 struct symbol *arg = new_symbol (child_die, NULL, cu);
9366 if (arg != NULL)
9367 template_args.push_back (arg);
9369 else
9370 process_die (child_die, cu);
9371 child_die = child_die->sibling;
9375 inherit_abstract_dies (die, cu);
9377 /* If we have a DW_AT_specification, we might need to import using
9378 directives from the context of the specification DIE. See the
9379 comment in determine_prefix. */
9380 if (cu->lang () == language_cplus
9381 && dwarf2_attr (die, DW_AT_specification, cu))
9383 struct dwarf2_cu *spec_cu = cu;
9384 struct die_info *spec_die = die_specification (die, &spec_cu);
9386 while (spec_die)
9388 child_die = spec_die->child;
9389 while (child_die && child_die->tag)
9391 if (child_die->tag == DW_TAG_imported_module)
9392 process_die (child_die, spec_cu);
9393 child_die = child_die->sibling;
9396 /* In some cases, GCC generates specification DIEs that
9397 themselves contain DW_AT_specification attributes. */
9398 spec_die = die_specification (spec_die, &spec_cu);
9402 struct context_stack cstk = cu->get_builder ()->pop_context ();
9403 /* Make a block for the local symbols within. */
9404 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
9405 cstk.static_link, lowpc, highpc);
9407 /* For C++, set the block's scope. */
9408 if ((cu->lang () == language_cplus
9409 || cu->lang () == language_fortran
9410 || cu->lang () == language_d
9411 || cu->lang () == language_rust)
9412 && cu->processing_has_namespace_info)
9413 block->set_scope (determine_prefix (die, cu),
9414 &objfile->objfile_obstack);
9416 /* If we have address ranges, record them. */
9417 dwarf2_record_block_ranges (die, block, cu);
9419 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
9421 /* Attach template arguments to function. */
9422 if (!template_args.empty ())
9424 gdb_assert (templ_func != NULL);
9426 templ_func->n_template_arguments = template_args.size ();
9427 templ_func->template_arguments
9428 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
9429 templ_func->n_template_arguments);
9430 memcpy (templ_func->template_arguments,
9431 template_args.data (),
9432 (templ_func->n_template_arguments * sizeof (struct symbol *)));
9434 /* Make sure that the symtab is set on the new symbols. Even
9435 though they don't appear in this symtab directly, other parts
9436 of gdb assume that symbols do, and this is reasonably
9437 true. */
9438 for (symbol *sym : template_args)
9439 sym->set_symtab (templ_func->symtab ());
9442 /* In C++, we can have functions nested inside functions (e.g., when
9443 a function declares a class that has methods). This means that
9444 when we finish processing a function scope, we may need to go
9445 back to building a containing block's symbol lists. */
9446 *cu->get_builder ()->get_local_symbols () = cstk.locals;
9447 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
9449 /* If we've finished processing a top-level function, subsequent
9450 symbols go in the file symbol list. */
9451 if (cu->get_builder ()->outermost_context_p ())
9452 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
9455 /* Process all the DIES contained within a lexical block scope. Start
9456 a new scope, process the dies, and then close the scope. */
9458 static void
9459 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9461 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9462 CORE_ADDR lowpc, highpc;
9463 struct die_info *child_die;
9465 /* Ignore blocks with missing or invalid low and high pc attributes. */
9466 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9467 as multiple lexical blocks? Handling children in a sane way would
9468 be nasty. Might be easier to properly extend generic blocks to
9469 describe ranges. */
9470 unrelocated_addr unrel_low, unrel_high;
9471 switch (dwarf2_get_pc_bounds (die, &unrel_low, &unrel_high, cu,
9472 nullptr, nullptr))
9474 case PC_BOUNDS_NOT_PRESENT:
9475 /* DW_TAG_lexical_block has no attributes, process its children as if
9476 there was no wrapping by that DW_TAG_lexical_block.
9477 GCC does no longer produces such DWARF since GCC r224161. */
9478 for (child_die = die->child;
9479 child_die != NULL && child_die->tag;
9480 child_die = child_die->sibling)
9482 /* We might already be processing this DIE. This can happen
9483 in an unusual circumstance -- where a subroutine A
9484 appears lexically in another subroutine B, but A actually
9485 inlines B. The recursion is broken here, rather than in
9486 inherit_abstract_dies, because it seems better to simply
9487 drop concrete children here. */
9488 if (!child_die->in_process)
9489 process_die (child_die, cu);
9491 return;
9492 case PC_BOUNDS_INVALID:
9493 return;
9495 lowpc = per_objfile->relocate (unrel_low);
9496 highpc = per_objfile->relocate (unrel_high);
9498 cu->get_builder ()->push_context (0, lowpc);
9499 if (die->child != NULL)
9501 child_die = die->child;
9502 while (child_die && child_die->tag)
9504 process_die (child_die, cu);
9505 child_die = child_die->sibling;
9508 inherit_abstract_dies (die, cu);
9509 struct context_stack cstk = cu->get_builder ()->pop_context ();
9511 if (*cu->get_builder ()->get_local_symbols () != NULL
9512 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
9514 struct block *block
9515 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
9516 cstk.start_addr, highpc);
9518 /* Note that recording ranges after traversing children, as we
9519 do here, means that recording a parent's ranges entails
9520 walking across all its children's ranges as they appear in
9521 the address map, which is quadratic behavior.
9523 It would be nicer to record the parent's ranges before
9524 traversing its children, simply overriding whatever you find
9525 there. But since we don't even decide whether to create a
9526 block until after we've traversed its children, that's hard
9527 to do. */
9528 dwarf2_record_block_ranges (die, block, cu);
9530 *cu->get_builder ()->get_local_symbols () = cstk.locals;
9531 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
9534 static void dwarf2_ranges_read_low_addrs
9535 (unsigned offset,
9536 struct dwarf2_cu *cu,
9537 dwarf_tag tag,
9538 std::vector<unrelocated_addr> &result);
9540 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
9542 static void
9543 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9545 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9546 struct objfile *objfile = per_objfile->objfile;
9547 struct gdbarch *gdbarch = objfile->arch ();
9548 struct attribute *attr;
9549 int nparams;
9550 struct die_info *child_die;
9552 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
9553 if (attr == NULL)
9555 /* This was a pre-DWARF-5 GNU extension alias
9556 for DW_AT_call_return_pc. */
9557 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9559 if (!attr)
9561 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9562 "DIE %s [in module %s]"),
9563 sect_offset_str (die->sect_off), objfile_name (objfile));
9564 return;
9566 unrelocated_addr pc = attr->as_address ();
9568 /* Count parameters at the caller. */
9570 nparams = 0;
9571 for (child_die = die->child; child_die && child_die->tag;
9572 child_die = child_die->sibling)
9574 if (child_die->tag != DW_TAG_call_site_parameter
9575 && child_die->tag != DW_TAG_GNU_call_site_parameter)
9577 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9578 "DW_TAG_call_site child DIE %s [in module %s]"),
9579 child_die->tag, sect_offset_str (child_die->sect_off),
9580 objfile_name (objfile));
9581 continue;
9584 nparams++;
9587 struct call_site *call_site
9588 = new (XOBNEWVAR (&objfile->objfile_obstack,
9589 struct call_site,
9590 sizeof (*call_site) + sizeof (call_site->parameter[0]) * nparams))
9591 struct call_site (pc, cu->per_cu, per_objfile);
9593 if (!cu->call_site_htab.emplace (call_site).second)
9595 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9596 "DIE %s [in module %s]"),
9597 paddress (gdbarch, (CORE_ADDR) pc), sect_offset_str (die->sect_off),
9598 objfile_name (objfile));
9599 return;
9602 /* We never call the destructor of call_site, so we must ensure it is
9603 trivially destructible. */
9604 static_assert(std::is_trivially_destructible<struct call_site>::value);
9606 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
9607 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9609 struct die_info *func_die;
9611 /* Skip also over DW_TAG_inlined_subroutine. */
9612 for (func_die = die->parent;
9613 func_die && func_die->tag != DW_TAG_subprogram
9614 && func_die->tag != DW_TAG_subroutine_type;
9615 func_die = func_die->parent);
9617 /* DW_AT_call_all_calls is a superset
9618 of DW_AT_call_all_tail_calls. */
9619 if (func_die
9620 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
9621 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9622 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
9623 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9625 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9626 not complete. But keep CALL_SITE for look ups via call_site_htab,
9627 both the initial caller containing the real return address PC and
9628 the final callee containing the current PC of a chain of tail
9629 calls do not need to have the tail call list complete. But any
9630 function candidate for a virtual tail call frame searched via
9631 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9632 determined unambiguously. */
9634 else
9636 struct type *func_type = NULL;
9638 if (func_die)
9639 func_type = get_die_type (func_die, cu);
9640 if (func_type != NULL)
9642 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
9644 /* Enlist this call site to the function. */
9645 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9646 TYPE_TAIL_CALL_LIST (func_type) = call_site;
9648 else
9649 complaint (_("Cannot find function owning DW_TAG_call_site "
9650 "DIE %s [in module %s]"),
9651 sect_offset_str (die->sect_off), objfile_name (objfile));
9655 attr = dwarf2_attr (die, DW_AT_call_target, cu);
9656 if (attr == NULL)
9657 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9658 if (attr == NULL)
9659 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
9660 if (attr == NULL)
9662 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
9663 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9666 call_site->target.set_loc_dwarf_block (nullptr);
9667 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
9668 /* Keep NULL DWARF_BLOCK. */;
9669 else if (attr->form_is_block ())
9671 struct dwarf2_locexpr_baton *dlbaton;
9672 struct dwarf_block *block = attr->as_block ();
9674 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9675 dlbaton->data = block->data;
9676 dlbaton->size = block->size;
9677 dlbaton->per_objfile = per_objfile;
9678 dlbaton->per_cu = cu->per_cu;
9680 call_site->target.set_loc_dwarf_block (dlbaton);
9682 else if (attr->form_is_ref ())
9684 struct dwarf2_cu *target_cu = cu;
9685 struct die_info *target_die;
9687 target_die = follow_die_ref (die, attr, &target_cu);
9688 gdb_assert (target_cu->per_objfile->objfile == objfile);
9690 struct attribute *ranges_attr
9691 = dwarf2_attr (target_die, DW_AT_ranges, target_cu);
9693 if (die_is_declaration (target_die, target_cu))
9695 const char *target_physname;
9697 /* Prefer the mangled name; otherwise compute the demangled one. */
9698 target_physname = dw2_linkage_name (target_die, target_cu);
9699 if (target_physname == NULL)
9700 target_physname = dwarf2_physname (NULL, target_die, target_cu);
9701 if (target_physname == NULL)
9702 complaint (_("DW_AT_call_target target DIE has invalid "
9703 "physname, for referencing DIE %s [in module %s]"),
9704 sect_offset_str (die->sect_off), objfile_name (objfile));
9705 else
9706 call_site->target.set_loc_physname (target_physname);
9708 else if (ranges_attr != nullptr && ranges_attr->form_is_unsigned ())
9710 ULONGEST ranges_offset = (ranges_attr->as_unsigned ()
9711 + target_cu->gnu_ranges_base);
9712 std::vector<unrelocated_addr> addresses;
9713 dwarf2_ranges_read_low_addrs (ranges_offset, target_cu,
9714 target_die->tag, addresses);
9715 unrelocated_addr *saved = XOBNEWVEC (&objfile->objfile_obstack,
9716 unrelocated_addr,
9717 addresses.size ());
9718 std::copy (addresses.begin (), addresses.end (), saved);
9719 call_site->target.set_loc_array (addresses.size (), saved);
9721 else
9723 unrelocated_addr lowpc;
9725 /* DW_AT_entry_pc should be preferred. */
9726 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu,
9727 nullptr, nullptr)
9728 <= PC_BOUNDS_INVALID)
9729 complaint (_("DW_AT_call_target target DIE has invalid "
9730 "low pc, for referencing DIE %s [in module %s]"),
9731 sect_offset_str (die->sect_off), objfile_name (objfile));
9732 else
9733 call_site->target.set_loc_physaddr (lowpc);
9736 else
9737 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9738 "block nor reference, for DIE %s [in module %s]"),
9739 sect_offset_str (die->sect_off), objfile_name (objfile));
9741 for (child_die = die->child;
9742 child_die && child_die->tag;
9743 child_die = child_die->sibling)
9745 struct call_site_parameter *parameter;
9746 struct attribute *loc, *origin;
9748 if (child_die->tag != DW_TAG_call_site_parameter
9749 && child_die->tag != DW_TAG_GNU_call_site_parameter)
9751 /* Already printed the complaint above. */
9752 continue;
9755 gdb_assert (call_site->parameter_count < nparams);
9756 parameter = &call_site->parameter[call_site->parameter_count];
9758 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9759 specifies DW_TAG_formal_parameter. Value of the data assumed for the
9760 register is contained in DW_AT_call_value. */
9762 loc = dwarf2_attr (child_die, DW_AT_location, cu);
9763 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
9764 if (origin == NULL)
9766 /* This was a pre-DWARF-5 GNU extension alias
9767 for DW_AT_call_parameter. */
9768 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9770 if (loc == NULL && origin != NULL && origin->form_is_ref ())
9772 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9774 sect_offset sect_off = origin->get_ref_die_offset ();
9775 if (!cu->header.offset_in_cu_p (sect_off))
9777 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9778 binding can be done only inside one CU. Such referenced DIE
9779 therefore cannot be even moved to DW_TAG_partial_unit. */
9780 complaint (_("DW_AT_call_parameter offset is not in CU for "
9781 "DW_TAG_call_site child DIE %s [in module %s]"),
9782 sect_offset_str (child_die->sect_off),
9783 objfile_name (objfile));
9784 continue;
9786 parameter->u.param_cu_off
9787 = (cu_offset) (sect_off - cu->header.sect_off);
9789 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
9791 complaint (_("No DW_FORM_block* DW_AT_location for "
9792 "DW_TAG_call_site child DIE %s [in module %s]"),
9793 sect_offset_str (child_die->sect_off), objfile_name (objfile));
9794 continue;
9796 else
9798 struct dwarf_block *block = loc->as_block ();
9800 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9801 (block->data, &block->data[block->size]);
9802 if (parameter->u.dwarf_reg != -1)
9803 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9804 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
9805 &block->data[block->size],
9806 &parameter->u.fb_offset))
9807 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9808 else
9810 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
9811 "for DW_FORM_block* DW_AT_location is supported for "
9812 "DW_TAG_call_site child DIE %s "
9813 "[in module %s]"),
9814 sect_offset_str (child_die->sect_off),
9815 objfile_name (objfile));
9816 continue;
9820 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
9821 if (attr == NULL)
9822 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9823 if (attr == NULL || !attr->form_is_block ())
9825 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9826 "DW_TAG_call_site child DIE %s [in module %s]"),
9827 sect_offset_str (child_die->sect_off),
9828 objfile_name (objfile));
9829 continue;
9832 struct dwarf_block *block = attr->as_block ();
9833 parameter->value = block->data;
9834 parameter->value_size = block->size;
9836 /* Parameters are not pre-cleared by memset above. */
9837 parameter->data_value = NULL;
9838 parameter->data_value_size = 0;
9839 call_site->parameter_count++;
9841 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
9842 if (attr == NULL)
9843 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9844 if (attr != nullptr)
9846 if (!attr->form_is_block ())
9847 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9848 "DW_TAG_call_site child DIE %s [in module %s]"),
9849 sect_offset_str (child_die->sect_off),
9850 objfile_name (objfile));
9851 else
9853 block = attr->as_block ();
9854 parameter->data_value = block->data;
9855 parameter->data_value_size = block->size;
9861 /* Helper function for read_variable. If DIE represents a virtual
9862 table, then return the type of the concrete object that is
9863 associated with the virtual table. Otherwise, return NULL. */
9865 static struct type *
9866 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
9868 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
9869 if (attr == NULL)
9870 return NULL;
9872 /* Find the type DIE. */
9873 struct die_info *type_die = NULL;
9874 struct dwarf2_cu *type_cu = cu;
9876 if (attr->form_is_ref ())
9877 type_die = follow_die_ref (die, attr, &type_cu);
9878 if (type_die == NULL)
9879 return NULL;
9881 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
9882 return NULL;
9883 return die_containing_type (type_die, type_cu);
9886 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
9888 static void
9889 read_variable (struct die_info *die, struct dwarf2_cu *cu)
9891 struct rust_vtable_symbol *storage = NULL;
9893 if (cu->lang () == language_rust)
9895 struct type *containing_type = rust_containing_type (die, cu);
9897 if (containing_type != NULL)
9899 struct objfile *objfile = cu->per_objfile->objfile;
9901 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
9902 storage->concrete_type = containing_type;
9903 storage->subclass = SYMBOL_RUST_VTABLE;
9907 struct symbol *res = new_symbol (die, NULL, cu, storage);
9908 struct attribute *abstract_origin
9909 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9910 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
9911 if (res == NULL && loc && abstract_origin)
9913 /* We have a variable without a name, but with a location and an abstract
9914 origin. This may be a concrete instance of an abstract variable
9915 referenced from an DW_OP_GNU_variable_value, so save it to find it back
9916 later. */
9917 struct dwarf2_cu *origin_cu = cu;
9918 struct die_info *origin_die
9919 = follow_die_ref (die, abstract_origin, &origin_cu);
9920 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9921 per_objfile->per_bfd->abstract_to_concrete
9922 [origin_die->sect_off].push_back (die->sect_off);
9926 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
9927 reading .debug_rnglists.
9928 Callback's type should be:
9929 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
9930 Return true if the attributes are present and valid, otherwise,
9931 return false. */
9933 template <typename Callback>
9934 static bool
9935 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
9936 dwarf_tag tag, Callback &&callback)
9938 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9939 struct objfile *objfile = per_objfile->objfile;
9940 bfd *obfd = objfile->obfd.get ();
9941 /* Base address selection entry. */
9942 std::optional<unrelocated_addr> base;
9943 const gdb_byte *buffer;
9944 bool overflow = false;
9945 ULONGEST addr_index;
9946 struct dwarf2_section_info *rnglists_section;
9948 base = cu->base_address;
9949 rnglists_section = cu_debug_rnglists_section (cu, tag);
9950 rnglists_section->read (objfile);
9952 if (offset >= rnglists_section->size)
9954 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
9955 offset);
9956 return false;
9958 buffer = rnglists_section->buffer + offset;
9960 while (1)
9962 /* Initialize it due to a false compiler warning. */
9963 unrelocated_addr range_beginning = {}, range_end = {};
9964 const gdb_byte *buf_end = (rnglists_section->buffer
9965 + rnglists_section->size);
9966 unsigned int bytes_read;
9968 if (buffer == buf_end)
9970 overflow = true;
9971 break;
9973 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
9974 switch (rlet)
9976 case DW_RLE_end_of_list:
9977 break;
9978 case DW_RLE_base_address:
9979 if (buffer + cu->header.addr_size > buf_end)
9981 overflow = true;
9982 break;
9984 base = cu->header.read_address (obfd, buffer, &bytes_read);
9985 buffer += bytes_read;
9986 break;
9987 case DW_RLE_base_addressx:
9988 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
9989 buffer += bytes_read;
9990 base = read_addr_index (cu, addr_index);
9991 break;
9992 case DW_RLE_start_length:
9993 if (buffer + cu->header.addr_size > buf_end)
9995 overflow = true;
9996 break;
9998 range_beginning = cu->header.read_address (obfd, buffer,
9999 &bytes_read);
10000 buffer += bytes_read;
10001 range_end
10002 = (unrelocated_addr) ((CORE_ADDR) range_beginning
10003 + read_unsigned_leb128 (obfd, buffer,
10004 &bytes_read));
10005 buffer += bytes_read;
10006 if (buffer > buf_end)
10008 overflow = true;
10009 break;
10011 break;
10012 case DW_RLE_startx_length:
10013 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10014 buffer += bytes_read;
10015 range_beginning = read_addr_index (cu, addr_index);
10016 if (buffer > buf_end)
10018 overflow = true;
10019 break;
10021 range_end
10022 = (unrelocated_addr) ((CORE_ADDR) range_beginning
10023 + read_unsigned_leb128 (obfd, buffer,
10024 &bytes_read));
10025 buffer += bytes_read;
10026 break;
10027 case DW_RLE_offset_pair:
10028 range_beginning = (unrelocated_addr) read_unsigned_leb128 (obfd, buffer,
10029 &bytes_read);
10030 buffer += bytes_read;
10031 if (buffer > buf_end)
10033 overflow = true;
10034 break;
10036 range_end = (unrelocated_addr) read_unsigned_leb128 (obfd, buffer,
10037 &bytes_read);
10038 buffer += bytes_read;
10039 if (buffer > buf_end)
10041 overflow = true;
10042 break;
10044 break;
10045 case DW_RLE_start_end:
10046 if (buffer + 2 * cu->header.addr_size > buf_end)
10048 overflow = true;
10049 break;
10051 range_beginning = cu->header.read_address (obfd, buffer,
10052 &bytes_read);
10053 buffer += bytes_read;
10054 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
10055 buffer += bytes_read;
10056 break;
10057 case DW_RLE_startx_endx:
10058 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10059 buffer += bytes_read;
10060 range_beginning = read_addr_index (cu, addr_index);
10061 if (buffer > buf_end)
10063 overflow = true;
10064 break;
10066 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
10067 buffer += bytes_read;
10068 range_end = read_addr_index (cu, addr_index);
10069 break;
10070 default:
10071 complaint (_("Invalid .debug_rnglists data (no base address)"));
10072 return false;
10074 if (rlet == DW_RLE_end_of_list || overflow)
10075 break;
10076 if (rlet == DW_RLE_base_address)
10077 continue;
10079 if (range_beginning > range_end)
10081 /* Inverted range entries are invalid. */
10082 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10083 return false;
10086 /* Empty range entries have no effect. */
10087 if (range_beginning == range_end)
10088 continue;
10090 /* Only DW_RLE_offset_pair needs the base address added. */
10091 if (rlet == DW_RLE_offset_pair)
10093 if (!base.has_value ())
10095 /* We have no valid base address for the DW_RLE_offset_pair. */
10096 complaint (_("Invalid .debug_rnglists data (no base address for "
10097 "DW_RLE_offset_pair)"));
10098 return false;
10101 range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning
10102 + (CORE_ADDR) *base);
10103 range_end = (unrelocated_addr) ((CORE_ADDR) range_end
10104 + (CORE_ADDR) *base);
10107 /* A not-uncommon case of bad debug info.
10108 Don't pollute the addrmap with bad data. */
10109 if (range_beginning == (unrelocated_addr) 0
10110 && !per_objfile->per_bfd->has_section_at_zero)
10112 complaint (_(".debug_rnglists entry has start address of zero"
10113 " [in module %s]"), objfile_name (objfile));
10114 continue;
10117 callback (range_beginning, range_end);
10120 if (overflow)
10122 complaint (_("Offset %d is not terminated "
10123 "for DW_AT_ranges attribute"),
10124 offset);
10125 return false;
10128 return true;
10131 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10132 Callback's type should be:
10133 void (unrelocated_addr range_beginning, unrelocated_addr range_end)
10134 Return 1 if the attributes are present and valid, otherwise, return 0. */
10136 template <typename Callback>
10137 static int
10138 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
10139 Callback &&callback)
10141 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10142 struct objfile *objfile = per_objfile->objfile;
10143 struct comp_unit_head *cu_header = &cu->header;
10144 bfd *obfd = objfile->obfd.get ();
10145 unsigned int addr_size = cu_header->addr_size;
10146 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10147 /* Base address selection entry. */
10148 std::optional<unrelocated_addr> base;
10149 unsigned int dummy;
10150 const gdb_byte *buffer;
10152 if (cu_header->version >= 5)
10153 return dwarf2_rnglists_process (offset, cu, tag, callback);
10155 base = cu->base_address;
10157 per_objfile->per_bfd->ranges.read (objfile);
10158 if (offset >= per_objfile->per_bfd->ranges.size)
10160 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10161 offset);
10162 return 0;
10164 buffer = per_objfile->per_bfd->ranges.buffer + offset;
10166 while (1)
10168 unrelocated_addr range_beginning, range_end;
10170 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
10171 buffer += addr_size;
10172 range_end = cu->header.read_address (obfd, buffer, &dummy);
10173 buffer += addr_size;
10174 offset += 2 * addr_size;
10176 /* An end of list marker is a pair of zero addresses. */
10177 if (range_beginning == (unrelocated_addr) 0
10178 && range_end == (unrelocated_addr) 0)
10179 /* Found the end of list entry. */
10180 break;
10182 /* Each base address selection entry is a pair of 2 values.
10183 The first is the largest possible address, the second is
10184 the base address. Check for a base address here. */
10185 if (((CORE_ADDR) range_beginning & mask) == mask)
10187 /* If we found the largest possible address, then we already
10188 have the base address in range_end. */
10189 base = range_end;
10190 continue;
10193 if (!base.has_value ())
10195 /* We have no valid base address for the ranges
10196 data. */
10197 complaint (_("Invalid .debug_ranges data (no base address)"));
10198 return 0;
10201 if (range_beginning > range_end)
10203 /* Inverted range entries are invalid. */
10204 complaint (_("Invalid .debug_ranges data (inverted range)"));
10205 return 0;
10208 /* Empty range entries have no effect. */
10209 if (range_beginning == range_end)
10210 continue;
10212 range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning
10213 + (CORE_ADDR) *base);
10214 range_end = (unrelocated_addr) ((CORE_ADDR) range_end
10215 + (CORE_ADDR) *base);
10217 /* A not-uncommon case of bad debug info.
10218 Don't pollute the addrmap with bad data. */
10219 if (range_beginning == (unrelocated_addr) 0
10220 && !per_objfile->per_bfd->has_section_at_zero)
10222 complaint (_(".debug_ranges entry has start address of zero"
10223 " [in module %s]"), objfile_name (objfile));
10224 continue;
10227 callback (range_beginning, range_end);
10230 return 1;
10233 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10234 Return 1 if the attributes are present and valid, otherwise, return 0.
10235 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
10236 ranges in MAP are set, using DATUM as the value. */
10238 static int
10239 dwarf2_ranges_read (unsigned offset, unrelocated_addr *low_return,
10240 unrelocated_addr *high_return, struct dwarf2_cu *cu,
10241 addrmap_mutable *map, void *datum, dwarf_tag tag)
10243 int low_set = 0;
10244 unrelocated_addr low = {};
10245 unrelocated_addr high = {};
10246 int retval;
10248 retval = dwarf2_ranges_process (offset, cu, tag,
10249 [&] (unrelocated_addr range_beginning, unrelocated_addr range_end)
10251 if (map != nullptr)
10253 /* addrmap only accepts CORE_ADDR, so we must cast here. */
10254 map->set_empty ((CORE_ADDR) range_beginning,
10255 (CORE_ADDR) range_end - 1,
10256 datum);
10259 /* FIXME: This is recording everything as a low-high
10260 segment of consecutive addresses. We should have a
10261 data structure for discontiguous block ranges
10262 instead. */
10263 if (! low_set)
10265 low = range_beginning;
10266 high = range_end;
10267 low_set = 1;
10269 else
10271 if (range_beginning < low)
10272 low = range_beginning;
10273 if (range_end > high)
10274 high = range_end;
10277 if (!retval)
10278 return 0;
10280 if (! low_set)
10281 /* If the first entry is an end-of-list marker, the range
10282 describes an empty scope, i.e. no instructions. */
10283 return 0;
10285 if (low_return)
10286 *low_return = low;
10287 if (high_return)
10288 *high_return = high;
10289 return 1;
10292 /* Process ranges and fill in a vector of the low PC values only. */
10294 static void
10295 dwarf2_ranges_read_low_addrs (unsigned offset, struct dwarf2_cu *cu,
10296 dwarf_tag tag,
10297 std::vector<unrelocated_addr> &result)
10299 dwarf2_ranges_process (offset, cu, tag,
10300 [&] (unrelocated_addr start, unrelocated_addr end)
10302 result.push_back (start);
10306 /* Determine the low and high pc of a DW_TAG_entry_point. */
10308 static pc_bounds_kind
10309 dwarf2_get_pc_bounds_entry_point (die_info *die, unrelocated_addr *low,
10310 unrelocated_addr *high, dwarf2_cu *cu)
10312 gdb_assert (low != nullptr);
10313 gdb_assert (high != nullptr);
10315 if (die->parent->tag != DW_TAG_subprogram)
10317 complaint (_("DW_TAG_entry_point not embedded in DW_TAG_subprogram"));
10318 return PC_BOUNDS_INVALID;
10321 /* A DW_TAG_entry_point is embedded in an subprogram. Therefore, we can use
10322 the highpc from its enveloping subprogram and get the lowpc from
10323 DWARF. */
10324 const enum pc_bounds_kind bounds_kind = dwarf2_get_pc_bounds (die->parent,
10325 low, high,
10326 cu, nullptr,
10327 nullptr);
10328 if (bounds_kind == PC_BOUNDS_INVALID || bounds_kind == PC_BOUNDS_NOT_PRESENT)
10329 return bounds_kind;
10331 attribute *attr_low = dwarf2_attr (die, DW_AT_low_pc, cu);
10332 if (!attr_low)
10334 complaint (_("DW_TAG_entry_point is missing DW_AT_low_pc"));
10335 return PC_BOUNDS_INVALID;
10337 *low = attr_low->as_address ();
10338 return bounds_kind;
10341 /* Determine the low and high pc using the DW_AT_low_pc and DW_AT_high_pc or
10342 DW_AT_ranges attributes of a DIE. */
10344 static pc_bounds_kind
10345 dwarf_get_pc_bounds_ranges_or_highlow_pc (die_info *die, unrelocated_addr *low,
10346 unrelocated_addr *high, dwarf2_cu *cu,
10347 addrmap_mutable *map, void *datum)
10349 gdb_assert (low != nullptr);
10350 gdb_assert (high != nullptr);
10352 struct attribute *attr;
10353 struct attribute *attr_high;
10354 enum pc_bounds_kind ret;
10356 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10357 if (attr_high)
10359 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10360 if (attr != nullptr)
10362 *low = attr->as_address ();
10363 *high = attr_high->as_address ();
10364 if (cu->header.version >= 4 && attr_high->form_is_constant ())
10365 *high = (unrelocated_addr) ((ULONGEST) *high + (ULONGEST) *low);
10367 /* Found consecutive range of addresses. */
10368 ret = PC_BOUNDS_HIGH_LOW;
10370 else
10372 /* Found high w/o low attribute. */
10373 ret = PC_BOUNDS_INVALID;
10376 else
10378 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10379 if (attr != nullptr && attr->form_is_unsigned ())
10381 /* Offset in the .debug_ranges or .debug_rnglist section (depending
10382 on DWARF version). */
10383 ULONGEST ranges_offset = attr->as_unsigned ();
10385 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
10386 this value. */
10387 if (die->tag != DW_TAG_compile_unit)
10388 ranges_offset += cu->gnu_ranges_base;
10390 /* Value of the DW_AT_ranges attribute is the offset in the
10391 .debug_ranges section. */
10392 if (!dwarf2_ranges_read (ranges_offset, low, high, cu,
10393 map, datum, die->tag))
10394 return PC_BOUNDS_INVALID;
10395 /* Found discontinuous range of addresses. */
10396 ret = PC_BOUNDS_RANGES;
10398 else
10400 /* Could not find high_pc or ranges attributed and thus no bounds
10401 pair. */
10402 ret = PC_BOUNDS_NOT_PRESENT;
10406 return ret;
10409 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
10410 definition for the return value. *LOWPC and *HIGHPC are set iff
10411 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
10413 static enum pc_bounds_kind
10414 dwarf2_get_pc_bounds (struct die_info *die, unrelocated_addr *lowpc,
10415 unrelocated_addr *highpc, struct dwarf2_cu *cu,
10416 addrmap_mutable *map, void *datum)
10418 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10420 unrelocated_addr low = {};
10421 unrelocated_addr high = {};
10422 enum pc_bounds_kind ret;
10424 if (die->tag == DW_TAG_entry_point)
10425 ret = dwarf2_get_pc_bounds_entry_point (die, &low, &high, cu);
10426 else
10427 ret = dwarf_get_pc_bounds_ranges_or_highlow_pc (die, &low, &high, cu, map,
10428 datum);
10430 if (ret == PC_BOUNDS_NOT_PRESENT || ret == PC_BOUNDS_INVALID)
10431 return ret;
10433 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
10434 if (high <= low)
10435 return PC_BOUNDS_INVALID;
10437 /* When using the GNU linker, .gnu.linkonce. sections are used to
10438 eliminate duplicate copies of functions and vtables and such.
10439 The linker will arbitrarily choose one and discard the others.
10440 The AT_*_pc values for such functions refer to local labels in
10441 these sections. If the section from that file was discarded, the
10442 labels are not in the output, so the relocs get a value of 0.
10443 If this is a discarded function, mark the pc bounds as invalid,
10444 so that GDB will ignore it. */
10445 if (low == (unrelocated_addr) 0
10446 && !per_objfile->per_bfd->has_section_at_zero)
10447 return PC_BOUNDS_INVALID;
10449 gdb_assert (lowpc != nullptr);
10450 *lowpc = low;
10451 if (highpc != nullptr)
10452 *highpc = high;
10453 return ret;
10456 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10457 its low and high PC addresses. Do nothing if these addresses could not
10458 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10459 and HIGHPC to the high address if greater than HIGHPC. */
10461 static void
10462 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10463 unrelocated_addr *lowpc,
10464 unrelocated_addr *highpc,
10465 struct dwarf2_cu *cu)
10467 unrelocated_addr low, high;
10468 struct die_info *child = die->child;
10470 if (dwarf2_get_pc_bounds (die, &low, &high, cu, nullptr, nullptr)
10471 >= PC_BOUNDS_RANGES)
10473 *lowpc = std::min (*lowpc, low);
10474 *highpc = std::max (*highpc, high);
10477 /* If the language does not allow nested subprograms (either inside
10478 subprograms or lexical blocks), we're done. */
10479 if (cu->lang () != language_ada)
10480 return;
10482 /* Check all the children of the given DIE. If it contains nested
10483 subprograms, then check their pc bounds. Likewise, we need to
10484 check lexical blocks as well, as they may also contain subprogram
10485 definitions. */
10486 while (child && child->tag)
10488 if (child->tag == DW_TAG_subprogram
10489 || child->tag == DW_TAG_lexical_block)
10490 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10491 child = child->sibling;
10495 /* Get the low and high pc's represented by the scope DIE, and store
10496 them in *LOWPC and *HIGHPC. If the correct values can't be
10497 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10499 static void
10500 get_scope_pc_bounds (struct die_info *die,
10501 unrelocated_addr *lowpc, unrelocated_addr *highpc,
10502 struct dwarf2_cu *cu)
10504 unrelocated_addr best_low = (unrelocated_addr) -1;
10505 unrelocated_addr best_high = {};
10506 unrelocated_addr current_low, current_high;
10508 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu,
10509 nullptr, nullptr)
10510 >= PC_BOUNDS_RANGES)
10512 best_low = current_low;
10513 best_high = current_high;
10515 else
10517 struct die_info *child = die->child;
10519 while (child && child->tag)
10521 switch (child->tag) {
10522 case DW_TAG_subprogram:
10523 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10524 break;
10525 case DW_TAG_namespace:
10526 case DW_TAG_module:
10527 /* FIXME: carlton/2004-01-16: Should we do this for
10528 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10529 that current GCC's always emit the DIEs corresponding
10530 to definitions of methods of classes as children of a
10531 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10532 the DIEs giving the declarations, which could be
10533 anywhere). But I don't see any reason why the
10534 standards says that they have to be there. */
10535 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10537 if (current_low != ((unrelocated_addr) -1))
10539 best_low = std::min (best_low, current_low);
10540 best_high = std::max (best_high, current_high);
10542 break;
10543 default:
10544 /* Ignore. */
10545 break;
10548 child = child->sibling;
10552 *lowpc = best_low;
10553 *highpc = best_high;
10556 /* Return the base address for DIE (which is represented by BLOCK) within
10557 CU. The base address is the DW_AT_low_pc, or if that is not present,
10558 the first address in the first range defined by DW_AT_ranges.
10560 The DWARF standard actually says that if DIE has neither DW_AT_low_pc or
10561 DW_AT_ranges then we should search in the parent of DIE for those
10562 properties, and so on up the hierarchy, until we find a die with one of
10563 those attributes, and use that as the base address. We don't implement
10564 that yet simply because we've never encountered a need for it. */
10566 static std::optional<CORE_ADDR>
10567 dwarf2_die_base_address (struct die_info *die, struct block *block,
10568 struct dwarf2_cu *cu)
10570 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10572 struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10573 if (attr != nullptr)
10575 CORE_ADDR res = per_objfile->relocate (attr->as_address ());
10576 fixup_low_high_pc (cu, die, &res, nullptr);
10577 return res;
10579 else if (block->ranges ().size () > 0)
10580 return block->ranges ()[0].start ();
10582 return {};
10585 /* Return true if ADDR is within any of the ranges covered by BLOCK. If
10586 there are no sub-ranges then just check against the block's start and
10587 end addresses, otherwise, check each sub-range covered by the block. */
10589 static bool
10590 dwarf2_addr_in_block_ranges (CORE_ADDR addr, struct block *block)
10592 if (block->ranges ().size () == 0)
10593 return addr >= block->start () && addr < block->end ();
10595 /* Check if ADDR is within any of the block's sub-ranges. */
10596 for (const blockrange &br : block->ranges ())
10598 if (addr >= br.start () && addr < br.end ())
10599 return true;
10602 /* ADDR is not within any of the block's sub-ranges. */
10603 return false;
10607 /* Set the entry PC for BLOCK which represents DIE from CU. Relies on the
10608 range information (if present) already having been read from DIE and
10609 stored into BLOCK. */
10611 static void
10612 dwarf2_record_block_entry_pc (struct die_info *die, struct block *block,
10613 struct dwarf2_cu *cu)
10615 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10617 /* Filled with the entry-pc if we can find it. */
10618 std::optional<CORE_ADDR> entry;
10620 /* Set the block's entry PC where possible. */
10621 struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
10622 if (attr != nullptr)
10624 /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant
10625 offset from the containing DIE's base address. We don't limit the
10626 constant handling to DWARF-5 though. If a broken compiler emits
10627 this for DWARF-4 then we handle it just as we would for DWARF-5. */
10628 if (attr->form_is_constant ())
10630 if (attr->form_is_unsigned ())
10632 CORE_ADDR offset = attr->as_unsigned ();
10634 std::optional<CORE_ADDR> base
10635 = dwarf2_die_base_address (die, block, cu);
10637 if (base.has_value ())
10638 entry.emplace (base.value () + offset);
10640 else
10642 /* We could possibly handle signed constants, but this is out
10643 of spec, so for now, just complain and ignore it. */
10644 complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"),
10645 plongest (attr->as_nonnegative ()));
10648 else
10649 entry.emplace (per_objfile->relocate (attr->as_address ()));
10651 else
10652 entry = dwarf2_die_base_address (die, block, cu);
10654 if (entry.has_value ())
10656 CORE_ADDR entry_pc = entry.value ();
10658 /* Some compilers (e.g. GCC) will have the DW_AT_entry_pc point at an
10659 empty sub-range, which by a strict reading of the DWARF means that
10660 the entry-pc is outside the blocks code range. If we continue
10661 using this address then GDB will confuse itself, breakpoints will
10662 be placed at the entry-pc, but once stopped there, GDB will not
10663 recognise that it is inside this block.
10665 To avoid this, ignore entry-pc values that are outside the block's
10666 range, GDB will then select a suitable default entry-pc. */
10667 if (dwarf2_addr_in_block_ranges (entry_pc, block))
10668 block->set_entry_pc (entry_pc);
10669 else
10670 complaint (_("in %s, DIE %s, DW_AT_entry_pc (%s) outside "
10671 "block range (%s -> %s)"),
10672 objfile_name (per_objfile->objfile),
10673 sect_offset_str (die->sect_off),
10674 paddress (per_objfile->objfile->arch (), entry_pc),
10675 paddress (per_objfile->objfile->arch (), block->start ()),
10676 paddress (per_objfile->objfile->arch (), block->end ()));
10680 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10681 in DIE. Also set the entry PC for BLOCK. */
10683 static void
10684 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10685 struct dwarf2_cu *cu)
10687 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10688 struct objfile *objfile = per_objfile->objfile;
10689 struct attribute *attr;
10690 struct attribute *attr_high;
10692 /* Like dwarf_get_pc_bounds_ranges_or_highlow_pc, we read either the
10693 low/high pc attributes, OR the ranges attribute, but not both. If we
10694 parse both here then we open up the possibility that, due to invalid
10695 DWARF, a block's start() and end() might not contain all of the ranges.
10697 We have seen this in the wild with older (pre v9) versions of GCC. In
10698 this case a GCC bug meant that a DIE was linked via DW_AT_abstract_origin
10699 to the wrong DIE. Instead of pointing at the abstract DIE, GCC was
10700 linking one instance DIE to an earlier instance DIE. The first instance
10701 DIE had low/high pc attributes, while the second instance DIE had a
10702 ranges attribute. When processing the incorrectly linked instance GDB
10703 would see a DIE with both a low/high pc and some ranges data. However,
10704 the ranges data was all outside the low/high range, which would trigger
10705 asserts when setting the entry-pc. */
10707 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10708 if (attr_high)
10710 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10711 if (attr != nullptr)
10713 unrelocated_addr unrel_low = attr->as_address ();
10714 unrelocated_addr unrel_high = attr_high->as_address ();
10716 if (cu->header.version >= 4 && attr_high->form_is_constant ())
10717 unrel_high = (unrelocated_addr) ((ULONGEST) unrel_high
10718 + (ULONGEST) unrel_low);
10720 CORE_ADDR low = per_objfile->relocate (unrel_low);
10721 CORE_ADDR high = per_objfile->relocate (unrel_high);
10722 fixup_low_high_pc (cu, die, &low, &high);
10723 cu->get_builder ()->record_block_range (block, low, high - 1);
10726 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10727 if (attr != nullptr && attr->form_is_unsigned ())
10728 complaint (_("in %s, DIE %s, DW_AT_ranges ignored due to DW_AT_low_pc"),
10729 objfile_name (per_objfile->objfile),
10730 sect_offset_str (die->sect_off));
10732 else
10734 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10735 if (attr != nullptr && attr->form_is_unsigned ())
10737 /* Offset in the .debug_ranges or .debug_rnglist section (depending
10738 on DWARF version). */
10739 ULONGEST ranges_offset = attr->as_unsigned ();
10741 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
10742 this value. */
10743 if (die->tag != DW_TAG_compile_unit)
10744 ranges_offset += cu->gnu_ranges_base;
10746 std::vector<blockrange> blockvec;
10747 dwarf2_ranges_process (ranges_offset, cu, die->tag,
10748 [&] (unrelocated_addr start,
10749 unrelocated_addr end)
10751 CORE_ADDR abs_start = per_objfile->relocate (start);
10752 CORE_ADDR abs_end = per_objfile->relocate (end);
10753 cu->get_builder ()->record_block_range (block, abs_start,
10754 abs_end - 1);
10755 blockvec.emplace_back (abs_start, abs_end);
10758 block->set_ranges (make_blockranges (objfile, blockvec));
10762 dwarf2_record_block_entry_pc (die, block, cu);
10765 /* Check whether the producer field indicates either of GCC < 4.6, or the
10766 Intel C/C++ compiler, and cache the result in CU. */
10768 static void
10769 check_producer (struct dwarf2_cu *cu)
10771 int major, minor;
10773 if (cu->producer == NULL)
10775 /* For unknown compilers expect their behavior is DWARF version
10776 compliant.
10778 GCC started to support .debug_types sections by -gdwarf-4 since
10779 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10780 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10781 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10782 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10784 else if (producer_is_gcc (cu->producer, &major, &minor))
10786 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10787 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10788 cu->producer_is_gcc_11 = major == 11;
10790 else if (producer_is_icc (cu->producer, &major, &minor))
10792 cu->producer_is_icc = true;
10793 cu->producer_is_icc_lt_14 = major < 14;
10795 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
10796 cu->producer_is_codewarrior = true;
10797 else if (producer_is_clang (cu->producer, &major, &minor))
10798 cu->producer_is_clang = true;
10799 else if (producer_is_gas (cu->producer, &major, &minor))
10801 cu->producer_is_gas_lt_2_38 = major < 2 || (major == 2 && minor < 38);
10802 cu->producer_is_gas_2_39 = major == 2 && minor == 39;
10803 cu->producer_is_gas_ge_2_40 = major > 2 || (major == 2 && minor >= 40);
10805 else
10807 /* For other non-GCC compilers, expect their behavior is DWARF version
10808 compliant. */
10811 cu->checked_producer = true;
10814 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10815 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10816 during 4.6.0 experimental. */
10818 static bool
10819 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10821 if (!cu->checked_producer)
10822 check_producer (cu);
10824 return cu->producer_is_gxx_lt_4_6;
10828 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
10829 with incorrect is_stmt attributes. */
10831 static bool
10832 producer_is_codewarrior (struct dwarf2_cu *cu)
10834 if (!cu->checked_producer)
10835 check_producer (cu);
10837 return cu->producer_is_codewarrior;
10840 static bool
10841 producer_is_gas_lt_2_38 (struct dwarf2_cu *cu)
10843 if (!cu->checked_producer)
10844 check_producer (cu);
10846 return cu->producer_is_gas_lt_2_38;
10849 static bool
10850 producer_is_gas_2_39 (struct dwarf2_cu *cu)
10852 if (!cu->checked_producer)
10853 check_producer (cu);
10855 return cu->producer_is_gas_2_39;
10858 /* Return true if CU is produced by GAS 2.39 or later. */
10860 static bool
10861 producer_is_gas_ge_2_39 (struct dwarf2_cu *cu)
10863 if (!cu->checked_producer)
10864 check_producer (cu);
10866 return cu->producer_is_gas_2_39 || cu->producer_is_gas_ge_2_40;
10869 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
10870 If that attribute is not available, return the appropriate
10871 default. */
10873 static enum dwarf_access_attribute
10874 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10876 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10877 if (attr != nullptr)
10879 LONGEST value = attr->constant_value (-1);
10880 if (value == DW_ACCESS_public
10881 || value == DW_ACCESS_protected
10882 || value == DW_ACCESS_private)
10883 return (dwarf_access_attribute) value;
10884 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
10885 plongest (value));
10888 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10890 /* The default DWARF 2 accessibility for members is public, the default
10891 accessibility for inheritance is private. */
10893 if (die->tag != DW_TAG_inheritance)
10894 return DW_ACCESS_public;
10895 else
10896 return DW_ACCESS_private;
10898 else
10900 /* DWARF 3+ defines the default accessibility a different way. The same
10901 rules apply now for DW_TAG_inheritance as for the members and it only
10902 depends on the container kind. */
10904 if (die->parent->tag == DW_TAG_class_type)
10905 return DW_ACCESS_private;
10906 else
10907 return DW_ACCESS_public;
10911 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
10912 *OFFSET to the byte offset. If the attribute was not found return
10913 0, otherwise return 1. If it was found but could not properly be
10914 handled, set *OFFSET to 0. */
10916 static int
10917 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
10918 LONGEST *offset)
10920 struct attribute *attr;
10922 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10923 if (attr != NULL)
10925 *offset = 0;
10926 CORE_ADDR temp;
10928 /* Note that we do not check for a section offset first here.
10929 This is because DW_AT_data_member_location is new in DWARF 4,
10930 so if we see it, we can assume that a constant form is really
10931 a constant and not a section offset. */
10932 if (attr->form_is_constant ())
10933 *offset = attr->constant_value (0);
10934 else if (attr->form_is_section_offset ())
10935 dwarf2_complex_location_expr_complaint ();
10936 else if (attr->form_is_block ()
10937 && decode_locdesc (attr->as_block (), cu, &temp))
10939 *offset = temp;
10941 else
10942 dwarf2_complex_location_expr_complaint ();
10944 return 1;
10946 else
10948 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
10949 if (attr != nullptr)
10951 *offset = attr->constant_value (0);
10952 return 1;
10956 return 0;
10959 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
10960 store the results in FIELD. */
10962 static void
10963 handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
10964 struct field *field)
10966 struct attribute *attr;
10968 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10969 if (attr != NULL)
10971 if (attr->form_is_constant ())
10973 LONGEST offset = attr->constant_value (0);
10975 /* Work around this GCC 11 bug, where it would erroneously use -1
10976 data member locations, instead of 0:
10978 Negative DW_AT_data_member_location
10979 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
10981 if (offset == -1 && cu->producer_is_gcc_11)
10983 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
10984 offset = 0;
10987 field->set_loc_bitpos (offset * bits_per_byte);
10989 else if (attr->form_is_section_offset ())
10990 dwarf2_complex_location_expr_complaint ();
10991 else if (attr->form_is_block ())
10993 CORE_ADDR offset;
10994 if (decode_locdesc (attr->as_block (), cu, &offset))
10995 field->set_loc_bitpos (offset * bits_per_byte);
10996 else
10998 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10999 struct objfile *objfile = per_objfile->objfile;
11000 struct dwarf2_locexpr_baton *dlbaton
11001 = XOBNEW (&objfile->objfile_obstack,
11002 struct dwarf2_locexpr_baton);
11003 dlbaton->data = attr->as_block ()->data;
11004 dlbaton->size = attr->as_block ()->size;
11005 /* When using this baton, we want to compute the address
11006 of the field, not the value. This is why
11007 is_reference is set to false here. */
11008 dlbaton->is_reference = false;
11009 dlbaton->per_objfile = per_objfile;
11010 dlbaton->per_cu = cu->per_cu;
11012 field->set_loc_dwarf_block (dlbaton);
11015 else
11016 dwarf2_complex_location_expr_complaint ();
11018 else
11020 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
11021 if (attr != nullptr)
11022 field->set_loc_bitpos (attr->constant_value (0));
11026 /* Add an aggregate field to the field list. */
11028 static void
11029 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11030 struct dwarf2_cu *cu)
11032 struct objfile *objfile = cu->per_objfile->objfile;
11033 struct gdbarch *gdbarch = objfile->arch ();
11034 struct nextfield *new_field;
11035 struct attribute *attr;
11036 struct field *fp;
11037 const char *fieldname = "";
11039 if (die->tag == DW_TAG_inheritance)
11040 new_field = &fip->baseclasses.emplace_back ();
11041 else
11042 new_field = &fip->fields.emplace_back ();
11044 new_field->offset = die->sect_off;
11046 switch (dwarf2_access_attribute (die, cu))
11048 case DW_ACCESS_public:
11049 break;
11050 case DW_ACCESS_private:
11051 new_field->field.set_accessibility (accessibility::PRIVATE);
11052 break;
11053 case DW_ACCESS_protected:
11054 new_field->field.set_accessibility (accessibility::PROTECTED);
11055 break;
11056 default:
11057 gdb_assert_not_reached ("invalid accessibility");
11060 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11061 if (attr != nullptr && attr->as_virtuality ())
11062 new_field->field.set_virtual ();
11064 fp = &new_field->field;
11066 if ((die->tag == DW_TAG_member || die->tag == DW_TAG_namelist_item)
11067 && !die_is_declaration (die, cu))
11069 if (die->tag == DW_TAG_namelist_item)
11071 /* Typically, DW_TAG_namelist_item are references to namelist items.
11072 If so, follow that reference. */
11073 struct attribute *attr1 = dwarf2_attr (die, DW_AT_namelist_item, cu);
11074 struct die_info *item_die = nullptr;
11075 struct dwarf2_cu *item_cu = cu;
11076 if (attr1->form_is_ref ())
11077 item_die = follow_die_ref (die, attr1, &item_cu);
11078 if (item_die != nullptr)
11079 die = item_die;
11081 /* Data member other than a C++ static data member. */
11083 /* Get type of field. */
11084 fp->set_type (die_type (die, cu));
11086 fp->set_loc_bitpos (0);
11088 /* Get bit size of field (zero if none). */
11089 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11090 if (attr != nullptr)
11091 fp->set_bitsize (attr->constant_value (0));
11092 else
11093 fp->set_bitsize (0);
11095 /* Get bit offset of field. */
11096 handle_member_location (die, cu, fp);
11097 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11098 if (attr != nullptr && attr->form_is_constant ())
11100 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
11102 /* For big endian bits, the DW_AT_bit_offset gives the
11103 additional bit offset from the MSB of the containing
11104 anonymous object to the MSB of the field. We don't
11105 have to do anything special since we don't need to
11106 know the size of the anonymous object. */
11107 fp->set_loc_bitpos (fp->loc_bitpos () + attr->constant_value (0));
11109 else
11111 /* For little endian bits, compute the bit offset to the
11112 MSB of the anonymous object, subtract off the number of
11113 bits from the MSB of the field to the MSB of the
11114 object, and then subtract off the number of bits of
11115 the field itself. The result is the bit offset of
11116 the LSB of the field. */
11117 int anonymous_size;
11118 int bit_offset = attr->constant_value (0);
11120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11121 if (attr != nullptr && attr->form_is_constant ())
11123 /* The size of the anonymous object containing
11124 the bit field is explicit, so use the
11125 indicated size (in bytes). */
11126 anonymous_size = attr->constant_value (0);
11128 else
11130 /* The size of the anonymous object containing
11131 the bit field must be inferred from the type
11132 attribute of the data member containing the
11133 bit field. */
11134 anonymous_size = fp->type ()->length ();
11136 fp->set_loc_bitpos (fp->loc_bitpos ()
11137 + anonymous_size * bits_per_byte
11138 - bit_offset - fp->bitsize ());
11142 /* Get name of field. */
11143 fieldname = dwarf2_name (die, cu);
11144 if (fieldname == NULL)
11145 fieldname = "";
11147 /* The name is already allocated along with this objfile, so we don't
11148 need to duplicate it for the type. */
11149 fp->set_name (fieldname);
11151 /* Change accessibility for artificial fields (e.g. virtual table
11152 pointer or virtual base class pointer) to private. */
11153 if (dwarf2_attr (die, DW_AT_artificial, cu))
11155 fp->set_is_artificial (true);
11156 fp->set_accessibility (accessibility::PRIVATE);
11159 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11161 /* C++ static member. */
11163 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11164 is a declaration, but all versions of G++ as of this writing
11165 (so through at least 3.2.1) incorrectly generate
11166 DW_TAG_variable tags. */
11168 const char *physname;
11170 /* Get name of field. */
11171 fieldname = dwarf2_name (die, cu);
11172 if (fieldname == NULL)
11173 return;
11175 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11176 if (attr
11177 /* Only create a symbol if this is an external value.
11178 new_symbol checks this and puts the value in the global symbol
11179 table, which we want. If it is not external, new_symbol
11180 will try to put the value in cu->list_in_scope which is wrong. */
11181 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11183 /* A static const member, not much different than an enum as far as
11184 we're concerned, except that we can support more types. */
11185 new_symbol (die, NULL, cu);
11188 /* Get physical name. */
11189 physname = dwarf2_physname (fieldname, die, cu);
11191 /* The name is already allocated along with this objfile, so we don't
11192 need to duplicate it for the type. */
11193 fp->set_loc_physname (physname ? physname : "");
11194 fp->set_type (die_type (die, cu));
11195 fp->set_name (fieldname);
11197 else if (die->tag == DW_TAG_inheritance)
11199 /* C++ base class field. */
11200 handle_member_location (die, cu, fp);
11201 fp->set_bitsize (0);
11202 fp->set_type (die_type (die, cu));
11203 fp->set_name (fp->type ()->name ());
11205 else
11206 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11209 /* Can the type given by DIE define another type? */
11211 static bool
11212 type_can_define_types (const struct die_info *die)
11214 switch (die->tag)
11216 case DW_TAG_typedef:
11217 case DW_TAG_class_type:
11218 case DW_TAG_structure_type:
11219 case DW_TAG_union_type:
11220 case DW_TAG_enumeration_type:
11221 return true;
11223 default:
11224 return false;
11228 /* Add a type definition defined in the scope of the FIP's class. */
11230 static void
11231 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
11232 struct dwarf2_cu *cu)
11234 struct decl_field fp;
11235 memset (&fp, 0, sizeof (fp));
11237 gdb_assert (type_can_define_types (die));
11239 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11240 fp.name = dwarf2_name (die, cu);
11241 fp.type = read_type_die (die, cu);
11243 /* Save accessibility. */
11244 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11245 switch (accessibility)
11247 case DW_ACCESS_public:
11248 /* The assumed value if neither private nor protected. */
11249 break;
11250 case DW_ACCESS_private:
11251 fp.accessibility = accessibility::PRIVATE;
11252 break;
11253 case DW_ACCESS_protected:
11254 fp.accessibility = accessibility::PROTECTED;
11255 break;
11258 if (die->tag == DW_TAG_typedef)
11259 fip->typedef_field_list.push_back (fp);
11260 else
11261 fip->nested_types_list.push_back (fp);
11264 /* A convenience typedef that's used when finding the discriminant
11265 field for a variant part. */
11266 typedef std::unordered_map<sect_offset, int> offset_map_type;
11268 /* Compute the discriminant range for a given variant. OBSTACK is
11269 where the results will be stored. VARIANT is the variant to
11270 process. IS_UNSIGNED indicates whether the discriminant is signed
11271 or unsigned. */
11273 static const gdb::array_view<discriminant_range>
11274 convert_variant_range (struct obstack *obstack, const variant_field &variant,
11275 bool is_unsigned)
11277 std::vector<discriminant_range> ranges;
11279 if (variant.default_branch)
11280 return {};
11282 if (variant.discr_list_data == nullptr)
11284 discriminant_range r
11285 = {variant.discriminant_value, variant.discriminant_value};
11286 ranges.push_back (r);
11288 else
11290 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
11291 variant.discr_list_data->size);
11292 while (!data.empty ())
11294 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
11296 complaint (_("invalid discriminant marker: %d"), data[0]);
11297 break;
11299 bool is_range = data[0] == DW_DSC_range;
11300 data = data.slice (1);
11302 ULONGEST low, high;
11303 unsigned int bytes_read;
11305 if (data.empty ())
11307 complaint (_("DW_AT_discr_list missing low value"));
11308 break;
11310 if (is_unsigned)
11311 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
11312 else
11313 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
11314 &bytes_read);
11315 data = data.slice (bytes_read);
11317 if (is_range)
11319 if (data.empty ())
11321 complaint (_("DW_AT_discr_list missing high value"));
11322 break;
11324 if (is_unsigned)
11325 high = read_unsigned_leb128 (nullptr, data.data (),
11326 &bytes_read);
11327 else
11328 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
11329 &bytes_read);
11330 data = data.slice (bytes_read);
11332 else
11333 high = low;
11335 ranges.push_back ({ low, high });
11339 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
11340 ranges.size ());
11341 std::copy (ranges.begin (), ranges.end (), result);
11342 return gdb::array_view<discriminant_range> (result, ranges.size ());
11345 static const gdb::array_view<variant_part> create_variant_parts
11346 (struct obstack *obstack,
11347 const offset_map_type &offset_map,
11348 struct field_info *fi,
11349 const std::vector<variant_part_builder> &variant_parts);
11351 /* Fill in a "struct variant" for a given variant field. RESULT is
11352 the variant to fill in. OBSTACK is where any needed allocations
11353 will be done. OFFSET_MAP holds the mapping from section offsets to
11354 fields for the type. FI describes the fields of the type we're
11355 processing. FIELD is the variant field we're converting. */
11357 static void
11358 create_one_variant (variant &result, struct obstack *obstack,
11359 const offset_map_type &offset_map,
11360 struct field_info *fi, const variant_field &field)
11362 result.discriminants = convert_variant_range (obstack, field, false);
11363 result.first_field = field.first_field + fi->baseclasses.size ();
11364 result.last_field = field.last_field + fi->baseclasses.size ();
11365 result.parts = create_variant_parts (obstack, offset_map, fi,
11366 field.variant_parts);
11369 /* Fill in a "struct variant_part" for a given variant part. RESULT
11370 is the variant part to fill in. OBSTACK is where any needed
11371 allocations will be done. OFFSET_MAP holds the mapping from
11372 section offsets to fields for the type. FI describes the fields of
11373 the type we're processing. BUILDER is the variant part to be
11374 converted. */
11376 static void
11377 create_one_variant_part (variant_part &result,
11378 struct obstack *obstack,
11379 const offset_map_type &offset_map,
11380 struct field_info *fi,
11381 const variant_part_builder &builder)
11383 auto iter = offset_map.find (builder.discriminant_offset);
11384 if (iter == offset_map.end ())
11386 result.discriminant_index = -1;
11387 /* Doesn't matter. */
11388 result.is_unsigned = false;
11390 else
11392 result.discriminant_index = iter->second;
11393 result.is_unsigned
11394 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
11397 size_t n = builder.variants.size ();
11398 variant *output = new (obstack) variant[n];
11399 for (size_t i = 0; i < n; ++i)
11400 create_one_variant (output[i], obstack, offset_map, fi,
11401 builder.variants[i]);
11403 result.variants = gdb::array_view<variant> (output, n);
11406 /* Create a vector of variant parts that can be attached to a type.
11407 OBSTACK is where any needed allocations will be done. OFFSET_MAP
11408 holds the mapping from section offsets to fields for the type. FI
11409 describes the fields of the type we're processing. VARIANT_PARTS
11410 is the vector to convert. */
11412 static const gdb::array_view<variant_part>
11413 create_variant_parts (struct obstack *obstack,
11414 const offset_map_type &offset_map,
11415 struct field_info *fi,
11416 const std::vector<variant_part_builder> &variant_parts)
11418 if (variant_parts.empty ())
11419 return {};
11421 size_t n = variant_parts.size ();
11422 variant_part *result = new (obstack) variant_part[n];
11423 for (size_t i = 0; i < n; ++i)
11424 create_one_variant_part (result[i], obstack, offset_map, fi,
11425 variant_parts[i]);
11427 return gdb::array_view<variant_part> (result, n);
11430 /* Compute the variant part vector for FIP, attaching it to TYPE when
11431 done. */
11433 static void
11434 add_variant_property (struct field_info *fip, struct type *type,
11435 struct dwarf2_cu *cu)
11437 /* Map section offsets of fields to their field index. Note the
11438 field index here does not take the number of baseclasses into
11439 account. */
11440 offset_map_type offset_map;
11441 for (int i = 0; i < fip->fields.size (); ++i)
11442 offset_map[fip->fields[i].offset] = i;
11444 struct objfile *objfile = cu->per_objfile->objfile;
11445 gdb::array_view<const variant_part> parts
11446 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
11447 fip->variant_parts);
11449 struct dynamic_prop prop;
11450 prop.set_variant_parts ((gdb::array_view<variant_part> *)
11451 obstack_copy (&objfile->objfile_obstack, &parts,
11452 sizeof (parts)));
11454 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
11457 /* Create the vector of fields, and attach it to the type. */
11459 static void
11460 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11461 struct dwarf2_cu *cu)
11463 int nfields = fip->nfields ();
11465 /* Record the field count, allocate space for the array of fields,
11466 and create blank accessibility bitfields if necessary. */
11467 type->alloc_fields (nfields);
11469 if (!fip->baseclasses.empty () && cu->lang () != language_ada)
11471 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11472 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
11475 if (!fip->variant_parts.empty ())
11476 add_variant_property (fip, type, cu);
11478 /* Copy the saved-up fields into the field vector. */
11479 for (int i = 0; i < nfields; ++i)
11481 struct nextfield &field
11482 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
11483 : fip->fields[i - fip->baseclasses.size ()]);
11485 type->field (i) = field.field;
11489 /* Return true if this member function is a constructor, false
11490 otherwise. */
11492 static int
11493 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11495 const char *fieldname;
11496 const char *type_name;
11497 int len;
11499 if (die->parent == NULL)
11500 return 0;
11502 if (die->parent->tag != DW_TAG_structure_type
11503 && die->parent->tag != DW_TAG_union_type
11504 && die->parent->tag != DW_TAG_class_type)
11505 return 0;
11507 fieldname = dwarf2_name (die, cu);
11508 type_name = dwarf2_name (die->parent, cu);
11509 if (fieldname == NULL || type_name == NULL)
11510 return 0;
11512 len = strlen (fieldname);
11513 return (strncmp (fieldname, type_name, len) == 0
11514 && (type_name[len] == '\0' || type_name[len] == '<'));
11517 /* Add a member function to the proper fieldlist. */
11519 static void
11520 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11521 struct type *type, struct dwarf2_cu *cu)
11523 struct objfile *objfile = cu->per_objfile->objfile;
11524 struct attribute *attr;
11525 int i;
11526 struct fnfieldlist *flp = nullptr;
11527 struct fn_field *fnp;
11528 const char *fieldname;
11529 struct type *this_type;
11531 if (cu->lang () == language_ada)
11532 error (_("unexpected member function in Ada type"));
11534 /* Get name of member function. */
11535 fieldname = dwarf2_name (die, cu);
11536 if (fieldname == NULL)
11537 return;
11539 /* Look up member function name in fieldlist. */
11540 for (i = 0; i < fip->fnfieldlists.size (); i++)
11542 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11544 flp = &fip->fnfieldlists[i];
11545 break;
11549 /* Create a new fnfieldlist if necessary. */
11550 if (flp == nullptr)
11552 flp = &fip->fnfieldlists.emplace_back ();
11553 flp->name = fieldname;
11554 i = fip->fnfieldlists.size () - 1;
11557 /* Create a new member function field and add it to the vector of
11558 fnfieldlists. */
11559 fnp = &flp->fnfields.emplace_back ();
11561 /* Delay processing of the physname until later. */
11562 if (cu->lang () == language_cplus)
11563 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
11564 die, cu);
11565 else
11567 const char *physname = dwarf2_physname (fieldname, die, cu);
11568 fnp->physname = physname ? physname : "";
11571 fnp->type = type_allocator (objfile, cu->lang ()).new_type ();
11572 this_type = read_type_die (die, cu);
11573 if (this_type && this_type->code () == TYPE_CODE_FUNC)
11575 int nparams = this_type->num_fields ();
11577 /* TYPE is the domain of this method, and THIS_TYPE is the type
11578 of the method itself (TYPE_CODE_METHOD). */
11579 smash_to_method_type (fnp->type, type,
11580 this_type->target_type (),
11581 this_type->fields (),
11582 this_type->num_fields (),
11583 this_type->has_varargs ());
11585 /* Handle static member functions.
11586 Dwarf2 has no clean way to discern C++ static and non-static
11587 member functions. G++ helps GDB by marking the first
11588 parameter for non-static member functions (which is the this
11589 pointer) as artificial. We obtain this information from
11590 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11591 if (nparams == 0 || this_type->field (0).is_artificial () == 0)
11592 fnp->voffset = VOFFSET_STATIC;
11594 else
11595 complaint (_("member function type missing for '%s'"),
11596 dwarf2_full_name (fieldname, die, cu));
11598 /* Get fcontext from DW_AT_containing_type if present. */
11599 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11600 fnp->fcontext = die_containing_type (die, cu);
11602 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11603 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11605 /* Get accessibility. */
11606 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
11607 switch (accessibility)
11609 case DW_ACCESS_private:
11610 fnp->accessibility = accessibility::PRIVATE;
11611 break;
11612 case DW_ACCESS_protected:
11613 fnp->accessibility = accessibility::PROTECTED;
11614 break;
11617 /* Check for artificial methods. */
11618 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11619 if (attr && attr->as_boolean ())
11620 fnp->is_artificial = 1;
11622 /* Check for defaulted methods. */
11623 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
11624 if (attr != nullptr)
11625 fnp->defaulted = attr->defaulted ();
11627 /* Check for deleted methods. */
11628 attr = dwarf2_attr (die, DW_AT_deleted, cu);
11629 if (attr != nullptr && attr->as_boolean ())
11630 fnp->is_deleted = 1;
11632 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11634 /* Get index in virtual function table if it is a virtual member
11635 function. For older versions of GCC, this is an offset in the
11636 appropriate virtual table, as specified by DW_AT_containing_type.
11637 For everyone else, it is an expression to be evaluated relative
11638 to the object address. */
11640 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11641 if (attr != nullptr)
11643 if (attr->form_is_block () && attr->as_block ()->size > 0)
11645 struct dwarf_block *block = attr->as_block ();
11646 CORE_ADDR offset;
11648 if (block->data[0] == DW_OP_constu
11649 && decode_locdesc (block, cu, &offset))
11651 /* "Old"-style GCC. See
11652 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44126
11653 for discussion. This was known and a patch available
11654 in 2010, but as of 2023, both GCC and clang still
11655 emit this. */
11656 fnp->voffset = offset + 2;
11658 else if ((block->data[0] == DW_OP_deref
11659 || (block->size > 1
11660 && block->data[0] == DW_OP_deref_size
11661 && block->data[1] == cu->header.addr_size))
11662 && decode_locdesc (block, cu, &offset))
11664 fnp->voffset = offset;
11665 if ((fnp->voffset % cu->header.addr_size) != 0)
11666 dwarf2_complex_location_expr_complaint ();
11667 else
11668 fnp->voffset /= cu->header.addr_size;
11669 fnp->voffset += 2;
11671 else
11672 dwarf2_complex_location_expr_complaint ();
11674 if (!fnp->fcontext)
11676 /* If there is no `this' field and no DW_AT_containing_type,
11677 we cannot actually find a base class context for the
11678 vtable! */
11679 if (this_type->num_fields () == 0
11680 || !this_type->field (0).is_artificial ())
11682 complaint (_("cannot determine context for virtual member "
11683 "function \"%s\" (offset %s)"),
11684 fieldname, sect_offset_str (die->sect_off));
11686 else
11688 fnp->fcontext = this_type->field (0).type ()->target_type ();
11692 else if (attr->form_is_section_offset ())
11694 dwarf2_complex_location_expr_complaint ();
11696 else
11698 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11699 fieldname);
11702 else
11704 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11705 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
11707 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11708 complaint (_("Member function \"%s\" (offset %s) is virtual "
11709 "but the vtable offset is not specified"),
11710 fieldname, sect_offset_str (die->sect_off));
11711 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11712 TYPE_CPLUS_DYNAMIC (type) = 1;
11717 /* Create the vector of member function fields, and attach it to the type. */
11719 static void
11720 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11721 struct dwarf2_cu *cu)
11723 if (cu->lang () == language_ada)
11724 error (_("unexpected member functions in Ada type"));
11726 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11727 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11728 TYPE_ZALLOC (type,
11729 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
11731 for (int i = 0; i < fip->fnfieldlists.size (); i++)
11733 struct fnfieldlist &nf = fip->fnfieldlists[i];
11734 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11736 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
11737 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
11738 /* No need to zero-initialize, initialization is done by the copy in
11739 the loop below. */
11740 fn_flp->fn_fields = (struct fn_field *)
11741 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
11743 for (int k = 0; k < nf.fnfields.size (); ++k)
11744 fn_flp->fn_fields[k] = nf.fnfields[k];
11747 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
11750 /* Returns non-zero if NAME is the name of a vtable member in CU's
11751 language, zero otherwise. */
11752 static int
11753 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11755 static const char vptr[] = "_vptr";
11757 /* Look for the C++ form of the vtable. */
11758 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
11759 return 1;
11761 return 0;
11764 /* GCC outputs unnamed structures that are really pointers to member
11765 functions, with the ABI-specified layout. If TYPE describes
11766 such a structure, smash it into a member function type.
11768 GCC shouldn't do this; it should just output pointer to member DIEs.
11769 This is GCC PR debug/28767. */
11771 static void
11772 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11774 struct type *pfn_type, *self_type, *new_type;
11776 /* Check for a structure with no name and two children. */
11777 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
11778 return;
11780 /* Check for __pfn and __delta members. */
11781 if (type->field (0).name () == NULL
11782 || strcmp (type->field (0).name (), "__pfn") != 0
11783 || type->field (1).name () == NULL
11784 || strcmp (type->field (1).name (), "__delta") != 0)
11785 return;
11787 /* Find the type of the method. */
11788 pfn_type = type->field (0).type ();
11789 if (pfn_type == NULL
11790 || pfn_type->code () != TYPE_CODE_PTR
11791 || pfn_type->target_type ()->code () != TYPE_CODE_FUNC)
11792 return;
11794 /* Look for the "this" argument. */
11795 pfn_type = pfn_type->target_type ();
11796 if (pfn_type->num_fields () == 0
11797 /* || pfn_type->field (0).type () == NULL */
11798 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
11799 return;
11801 self_type = pfn_type->field (0).type ()->target_type ();
11802 new_type = type_allocator (type).new_type ();
11803 smash_to_method_type (new_type, self_type, pfn_type->target_type (),
11804 pfn_type->fields (), pfn_type->num_fields (),
11805 pfn_type->has_varargs ());
11806 smash_to_methodptr_type (type, new_type);
11809 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
11810 requires rewriting, then copy it and return the updated copy.
11811 Otherwise return nullptr. */
11813 static struct type *
11814 rewrite_array_type (struct type *type)
11816 if (type->code () != TYPE_CODE_ARRAY)
11817 return nullptr;
11819 struct type *index_type = type->index_type ();
11820 range_bounds *current_bounds = index_type->bounds ();
11822 /* Handle multi-dimensional arrays. */
11823 struct type *new_target = rewrite_array_type (type->target_type ());
11824 if (new_target == nullptr)
11826 /* Maybe we don't need to rewrite this array. */
11827 if (current_bounds->low.is_constant ()
11828 && current_bounds->high.is_constant ())
11829 return nullptr;
11832 /* Either the target type was rewritten, or the bounds have to be
11833 updated. Either way we want to copy the type and update
11834 everything. */
11835 struct type *copy = copy_type (type);
11836 copy->copy_fields (type);
11837 if (new_target != nullptr)
11838 copy->set_target_type (new_target);
11840 struct type *index_copy = copy_type (index_type);
11841 range_bounds *bounds
11842 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
11843 sizeof (range_bounds));
11844 *bounds = *current_bounds;
11845 bounds->low.set_const_val (1);
11846 bounds->high.set_const_val (0);
11847 index_copy->set_bounds (bounds);
11848 copy->set_index_type (index_copy);
11850 return copy;
11853 /* While some versions of GCC will generate complicated DWARF for an
11854 array (see quirk_ada_thick_pointer), more recent versions were
11855 modified to emit an explicit thick pointer structure. However, in
11856 this case, the array still has DWARF expressions for its ranges,
11857 and these must be ignored. */
11859 static void
11860 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
11861 struct type *type)
11863 gdb_assert (cu->lang () == language_ada);
11865 /* Check for a structure with two children. */
11866 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
11867 return;
11869 /* Check for P_ARRAY and P_BOUNDS members. */
11870 if (type->field (0).name () == NULL
11871 || strcmp (type->field (0).name (), "P_ARRAY") != 0
11872 || type->field (1).name () == NULL
11873 || strcmp (type->field (1).name (), "P_BOUNDS") != 0)
11874 return;
11876 /* Make sure we're looking at a pointer to an array. */
11877 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
11878 return;
11880 /* The Ada code already knows how to handle these types, so all that
11881 we need to do is turn the bounds into static bounds. However, we
11882 don't want to rewrite existing array or index types in-place,
11883 because those may be referenced in other contexts where this
11884 rewriting is undesirable. */
11885 struct type *new_ary_type
11886 = rewrite_array_type (type->field (0).type ()->target_type ());
11887 if (new_ary_type != nullptr)
11888 type->field (0).set_type (lookup_pointer_type (new_ary_type));
11891 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
11892 appropriate error checking and issuing complaints if there is a
11893 problem. */
11895 static ULONGEST
11896 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
11898 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
11900 if (attr == nullptr)
11901 return 0;
11903 if (!attr->form_is_constant ())
11905 complaint (_("DW_AT_alignment must have constant form"
11906 " - DIE at %s [in module %s]"),
11907 sect_offset_str (die->sect_off),
11908 objfile_name (cu->per_objfile->objfile));
11909 return 0;
11912 LONGEST val = attr->constant_value (0);
11913 if (val < 0)
11915 complaint (_("DW_AT_alignment value must not be negative"
11916 " - DIE at %s [in module %s]"),
11917 sect_offset_str (die->sect_off),
11918 objfile_name (cu->per_objfile->objfile));
11919 return 0;
11921 ULONGEST align = val;
11923 if (align == 0)
11925 complaint (_("DW_AT_alignment value must not be zero"
11926 " - DIE at %s [in module %s]"),
11927 sect_offset_str (die->sect_off),
11928 objfile_name (cu->per_objfile->objfile));
11929 return 0;
11931 if ((align & (align - 1)) != 0)
11933 complaint (_("DW_AT_alignment value must be a power of 2"
11934 " - DIE at %s [in module %s]"),
11935 sect_offset_str (die->sect_off),
11936 objfile_name (cu->per_objfile->objfile));
11937 return 0;
11940 return align;
11943 /* If the DIE has a DW_AT_alignment attribute, use its value to set
11944 the alignment for TYPE. */
11946 static void
11947 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
11948 struct type *type)
11950 if (!set_type_align (type, get_alignment (cu, die)))
11951 complaint (_("DW_AT_alignment value too large"
11952 " - DIE at %s [in module %s]"),
11953 sect_offset_str (die->sect_off),
11954 objfile_name (cu->per_objfile->objfile));
11957 /* Check if the given VALUE is a valid enum dwarf_calling_convention
11958 constant for a type, according to DWARF5 spec, Table 5.5. */
11960 static bool
11961 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
11963 switch (value)
11965 case DW_CC_normal:
11966 case DW_CC_pass_by_reference:
11967 case DW_CC_pass_by_value:
11968 return true;
11970 default:
11971 complaint (_("unrecognized DW_AT_calling_convention value "
11972 "(%s) for a type"), pulongest (value));
11973 return false;
11977 /* Check if the given VALUE is a valid enum dwarf_calling_convention
11978 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
11979 also according to GNU-specific values (see include/dwarf2.h). */
11981 static bool
11982 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
11984 switch (value)
11986 case DW_CC_normal:
11987 case DW_CC_program:
11988 case DW_CC_nocall:
11989 return true;
11991 case DW_CC_GNU_renesas_sh:
11992 case DW_CC_GNU_borland_fastcall_i386:
11993 case DW_CC_GDB_IBM_OpenCL:
11994 return true;
11996 default:
11997 complaint (_("unrecognized DW_AT_calling_convention value "
11998 "(%s) for a subroutine"), pulongest (value));
11999 return false;
12003 /* Called when we find the DIE that starts a structure or union scope
12004 (definition) to create a type for the structure or union. Fill in
12005 the type's name and general properties; the members will not be
12006 processed until process_structure_scope. A symbol table entry for
12007 the type will also not be done until process_structure_scope (assuming
12008 the type has a name).
12010 NOTE: we need to call these functions regardless of whether or not the
12011 DIE has a DW_AT_name attribute, since it might be an anonymous
12012 structure or union. This gets the type entered into our set of
12013 user defined types. */
12015 static struct type *
12016 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12018 struct objfile *objfile = cu->per_objfile->objfile;
12019 struct type *type;
12020 struct attribute *attr;
12021 const char *name;
12023 /* If the definition of this type lives in .debug_types, read that type.
12024 Don't follow DW_AT_specification though, that will take us back up
12025 the chain and we want to go down. */
12026 attr = die->attr (DW_AT_signature);
12027 if (attr != nullptr)
12029 type = get_DW_AT_signature_type (die, attr, cu);
12031 /* The type's CU may not be the same as CU.
12032 Ensure TYPE is recorded with CU in die_type_hash. */
12033 return set_die_type (die, type, cu);
12036 type = type_allocator (objfile, cu->lang ()).new_type ();
12037 INIT_CPLUS_SPECIFIC (type);
12039 name = dwarf2_name (die, cu);
12040 if (name != NULL)
12042 if (cu->lang () == language_cplus
12043 || cu->lang () == language_d
12044 || cu->lang () == language_rust)
12046 const char *full_name = dwarf2_full_name (name, die, cu);
12048 /* dwarf2_full_name might have already finished building the DIE's
12049 type. If so, there is no need to continue. */
12050 if (get_die_type (die, cu) != NULL)
12051 return get_die_type (die, cu);
12053 type->set_name (full_name);
12055 else
12057 /* The name is already allocated along with this objfile, so
12058 we don't need to duplicate it for the type. */
12059 type->set_name (name);
12063 if (die->tag == DW_TAG_structure_type)
12065 type->set_code (TYPE_CODE_STRUCT);
12067 else if (die->tag == DW_TAG_union_type)
12069 type->set_code (TYPE_CODE_UNION);
12071 else if (die->tag == DW_TAG_namelist)
12073 type->set_code (TYPE_CODE_NAMELIST);
12075 else
12077 type->set_code (TYPE_CODE_STRUCT);
12080 if (cu->lang () == language_cplus && die->tag == DW_TAG_class_type)
12081 type->set_is_declared_class (true);
12083 /* Store the calling convention in the type if it's available in
12084 the die. Otherwise the calling convention remains set to
12085 the default value DW_CC_normal. */
12086 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12087 if (attr != nullptr
12088 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
12090 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12091 TYPE_CPLUS_CALLING_CONVENTION (type)
12092 = (enum dwarf_calling_convention) (attr->constant_value (0));
12095 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12096 if (attr != nullptr)
12098 if (attr->form_is_constant ())
12099 type->set_length (attr->constant_value (0));
12100 else
12102 struct dynamic_prop prop;
12103 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
12104 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
12106 type->set_length (0);
12109 else
12110 type->set_length (0);
12112 maybe_set_alignment (cu, die, type);
12114 if (producer_is_icc_lt_14 (cu) && (type->length () == 0))
12116 /* ICC<14 does not output the required DW_AT_declaration on
12117 incomplete types, but gives them a size of zero. */
12118 type->set_is_stub (true);
12120 else
12121 type->set_stub_is_supported (true);
12123 if (die_is_declaration (die, cu))
12124 type->set_is_stub (true);
12125 else if (attr == NULL && die->child == NULL
12126 && producer_is_realview (cu->producer))
12127 /* RealView does not output the required DW_AT_declaration
12128 on incomplete types. */
12129 type->set_is_stub (true);
12131 /* We need to add the type field to the die immediately so we don't
12132 infinitely recurse when dealing with pointers to the structure
12133 type within the structure itself. */
12134 set_die_type (die, type, cu);
12136 /* set_die_type should be already done. */
12137 set_descriptive_type (type, die, cu);
12139 return type;
12142 static void handle_struct_member_die
12143 (struct die_info *child_die,
12144 struct type *type,
12145 struct field_info *fi,
12146 std::vector<struct symbol *> *template_args,
12147 struct dwarf2_cu *cu);
12149 /* A helper for handle_struct_member_die that handles
12150 DW_TAG_variant_part. */
12152 static void
12153 handle_variant_part (struct die_info *die, struct type *type,
12154 struct field_info *fi,
12155 std::vector<struct symbol *> *template_args,
12156 struct dwarf2_cu *cu)
12158 variant_part_builder *new_part;
12159 if (fi->current_variant_part == nullptr)
12160 new_part = &fi->variant_parts.emplace_back ();
12161 else if (!fi->current_variant_part->processing_variant)
12163 complaint (_("nested DW_TAG_variant_part seen "
12164 "- DIE at %s [in module %s]"),
12165 sect_offset_str (die->sect_off),
12166 objfile_name (cu->per_objfile->objfile));
12167 return;
12169 else
12171 variant_field &current = fi->current_variant_part->variants.back ();
12172 new_part = &current.variant_parts.emplace_back ();
12175 /* When we recurse, we want callees to add to this new variant
12176 part. */
12177 scoped_restore save_current_variant_part
12178 = make_scoped_restore (&fi->current_variant_part, new_part);
12180 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
12181 if (discr == NULL)
12183 /* It's a univariant form, an extension we support. */
12185 else if (discr->form_is_ref ())
12187 struct dwarf2_cu *target_cu = cu;
12188 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
12190 new_part->discriminant_offset = target_die->sect_off;
12192 else
12194 complaint (_("DW_AT_discr does not have DIE reference form"
12195 " - DIE at %s [in module %s]"),
12196 sect_offset_str (die->sect_off),
12197 objfile_name (cu->per_objfile->objfile));
12200 for (die_info *child_die = die->child;
12201 child_die != NULL;
12202 child_die = child_die->sibling)
12203 handle_struct_member_die (child_die, type, fi, template_args, cu);
12206 /* A helper for handle_struct_member_die that handles
12207 DW_TAG_variant. */
12209 static void
12210 handle_variant (struct die_info *die, struct type *type,
12211 struct field_info *fi,
12212 std::vector<struct symbol *> *template_args,
12213 struct dwarf2_cu *cu)
12215 if (fi->current_variant_part == nullptr)
12217 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12218 "- DIE at %s [in module %s]"),
12219 sect_offset_str (die->sect_off),
12220 objfile_name (cu->per_objfile->objfile));
12221 return;
12223 if (fi->current_variant_part->processing_variant)
12225 complaint (_("nested DW_TAG_variant seen "
12226 "- DIE at %s [in module %s]"),
12227 sect_offset_str (die->sect_off),
12228 objfile_name (cu->per_objfile->objfile));
12229 return;
12232 scoped_restore save_processing_variant
12233 = make_scoped_restore (&fi->current_variant_part->processing_variant,
12234 true);
12236 variant_field &variant = fi->current_variant_part->variants.emplace_back ();
12237 variant.first_field = fi->fields.size ();
12239 /* In a variant we want to get the discriminant and also add a
12240 field for our sole member child. */
12241 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
12242 if (discr == nullptr || !discr->form_is_constant ())
12244 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
12245 if (discr == nullptr || discr->as_block ()->size == 0)
12246 variant.default_branch = true;
12247 else
12248 variant.discr_list_data = discr->as_block ();
12250 else
12251 variant.discriminant_value = discr->constant_value (0);
12253 for (die_info *variant_child = die->child;
12254 variant_child != NULL;
12255 variant_child = variant_child->sibling)
12256 handle_struct_member_die (variant_child, type, fi, template_args, cu);
12258 variant.last_field = fi->fields.size ();
12261 /* A helper for process_structure_scope that handles a single member
12262 DIE. */
12264 static void
12265 handle_struct_member_die (struct die_info *child_die, struct type *type,
12266 struct field_info *fi,
12267 std::vector<struct symbol *> *template_args,
12268 struct dwarf2_cu *cu)
12270 if (child_die->tag == DW_TAG_member
12271 || child_die->tag == DW_TAG_variable
12272 || child_die->tag == DW_TAG_namelist_item)
12274 /* NOTE: carlton/2002-11-05: A C++ static data member
12275 should be a DW_TAG_member that is a declaration, but
12276 all versions of G++ as of this writing (so through at
12277 least 3.2.1) incorrectly generate DW_TAG_variable
12278 tags for them instead. */
12279 dwarf2_add_field (fi, child_die, cu);
12281 else if (child_die->tag == DW_TAG_subprogram)
12283 /* Rust doesn't have member functions in the C++ sense.
12284 However, it does emit ordinary functions as children
12285 of a struct DIE. */
12286 if (cu->lang () == language_rust)
12287 read_func_scope (child_die, cu);
12288 else
12290 /* C++ member function. */
12291 dwarf2_add_member_fn (fi, child_die, type, cu);
12294 else if (child_die->tag == DW_TAG_inheritance)
12296 /* C++ base class field. */
12297 dwarf2_add_field (fi, child_die, cu);
12299 else if (type_can_define_types (child_die))
12300 dwarf2_add_type_defn (fi, child_die, cu);
12301 else if (child_die->tag == DW_TAG_template_type_param
12302 || child_die->tag == DW_TAG_template_value_param)
12304 struct symbol *arg = new_symbol (child_die, NULL, cu);
12306 if (arg != NULL)
12307 template_args->push_back (arg);
12309 else if (child_die->tag == DW_TAG_variant_part)
12310 handle_variant_part (child_die, type, fi, template_args, cu);
12311 else if (child_die->tag == DW_TAG_variant)
12312 handle_variant (child_die, type, fi, template_args, cu);
12315 /* Finish creating a structure or union type, including filling in its
12316 members and creating a symbol for it. This function also handles Fortran
12317 namelist variables, their items or members and creating a symbol for
12318 them. */
12320 static void
12321 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12323 struct objfile *objfile = cu->per_objfile->objfile;
12324 struct die_info *child_die;
12325 struct type *type;
12327 type = get_die_type (die, cu);
12328 if (type == NULL)
12329 type = read_structure_type (die, cu);
12331 bool has_template_parameters = false;
12332 if (die->child != NULL && ! die_is_declaration (die, cu))
12334 struct field_info fi;
12335 std::vector<struct symbol *> template_args;
12337 child_die = die->child;
12339 while (child_die && child_die->tag)
12341 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
12342 child_die = child_die->sibling;
12345 /* Attach template arguments to type. */
12346 if (!template_args.empty ())
12348 has_template_parameters = true;
12349 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12350 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
12351 TYPE_TEMPLATE_ARGUMENTS (type)
12352 = XOBNEWVEC (&objfile->objfile_obstack,
12353 struct symbol *,
12354 TYPE_N_TEMPLATE_ARGUMENTS (type));
12355 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12356 template_args.data (),
12357 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12358 * sizeof (struct symbol *)));
12361 /* Attach fields and member functions to the type. */
12362 if (fi.nfields () > 0)
12363 dwarf2_attach_fields_to_type (&fi, type, cu);
12364 if (!fi.fnfieldlists.empty ())
12366 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12368 /* Get the type which refers to the base class (possibly this
12369 class itself) which contains the vtable pointer for the current
12370 class from the DW_AT_containing_type attribute. This use of
12371 DW_AT_containing_type is a GNU extension. */
12373 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12375 struct type *t = die_containing_type (die, cu);
12377 set_type_vptr_basetype (type, t);
12378 if (type == t)
12380 int i;
12382 /* Our own class provides vtbl ptr. */
12383 for (i = t->num_fields () - 1;
12384 i >= TYPE_N_BASECLASSES (t);
12385 --i)
12387 const char *fieldname = t->field (i).name ();
12389 if (is_vtable_name (fieldname, cu))
12391 set_type_vptr_fieldno (type, i);
12392 break;
12396 /* Complain if virtual function table field not found. */
12397 if (i < TYPE_N_BASECLASSES (t))
12398 complaint (_("virtual function table pointer "
12399 "not found when defining class '%s'"),
12400 type->name () ? type->name () : "");
12402 else
12404 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
12407 else if (cu->producer
12408 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
12410 /* The IBM XLC compiler does not provide direct indication
12411 of the containing type, but the vtable pointer is
12412 always named __vfp. */
12414 int i;
12416 for (i = type->num_fields () - 1;
12417 i >= TYPE_N_BASECLASSES (type);
12418 --i)
12420 if (strcmp (type->field (i).name (), "__vfp") == 0)
12422 set_type_vptr_fieldno (type, i);
12423 set_type_vptr_basetype (type, type);
12424 break;
12430 /* Copy fi.typedef_field_list linked list elements content into the
12431 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12432 if (!fi.typedef_field_list.empty ())
12434 int count = fi.typedef_field_list.size ();
12436 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12437 /* No zero-initialization is needed, the elements are initialized by
12438 the copy in the loop below. */
12439 TYPE_TYPEDEF_FIELD_ARRAY (type)
12440 = ((struct decl_field *)
12441 TYPE_ALLOC (type,
12442 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
12443 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
12445 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
12446 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
12449 /* Copy fi.nested_types_list linked list elements content into the
12450 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
12451 if (!fi.nested_types_list.empty ()
12452 && cu->lang () != language_ada)
12454 int count = fi.nested_types_list.size ();
12456 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12457 /* No zero-initialization is needed, the elements are initialized by
12458 the copy in the loop below. */
12459 TYPE_NESTED_TYPES_ARRAY (type)
12460 = ((struct decl_field *)
12461 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
12462 TYPE_NESTED_TYPES_COUNT (type) = count;
12464 for (int i = 0; i < fi.nested_types_list.size (); ++i)
12465 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
12469 quirk_gcc_member_function_pointer (type, objfile);
12470 if (cu->lang () == language_rust && die->tag == DW_TAG_union_type)
12471 cu->rust_unions.push_back (type);
12472 else if (cu->lang () == language_ada)
12473 quirk_ada_thick_pointer_struct (die, cu, type);
12475 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12476 snapshots) has been known to create a die giving a declaration
12477 for a class that has, as a child, a die giving a definition for a
12478 nested class. So we have to process our children even if the
12479 current die is a declaration. Normally, of course, a declaration
12480 won't have any children at all. */
12482 child_die = die->child;
12484 while (child_die != NULL && child_die->tag)
12486 if (child_die->tag == DW_TAG_member
12487 || child_die->tag == DW_TAG_variable
12488 || child_die->tag == DW_TAG_inheritance
12489 || child_die->tag == DW_TAG_template_value_param
12490 || child_die->tag == DW_TAG_template_type_param)
12492 /* Do nothing. */
12494 else
12495 process_die (child_die, cu);
12497 child_die = child_die->sibling;
12500 /* Do not consider external references. According to the DWARF standard,
12501 these DIEs are identified by the fact that they have no byte_size
12502 attribute, and a declaration attribute. */
12503 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12504 || !die_is_declaration (die, cu)
12505 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
12507 struct symbol *sym = new_symbol (die, type, cu);
12509 if (has_template_parameters)
12511 struct symtab *symtab;
12512 if (sym != nullptr)
12513 symtab = sym->symtab ();
12514 else if (cu->line_header != nullptr)
12516 /* Any related symtab will do. */
12517 symtab
12518 = cu->line_header->file_names ()[0].symtab;
12520 else
12522 symtab = nullptr;
12523 complaint (_("could not find suitable "
12524 "symtab for template parameter"
12525 " - DIE at %s [in module %s]"),
12526 sect_offset_str (die->sect_off),
12527 objfile_name (objfile));
12530 if (symtab != nullptr)
12532 /* Make sure that the symtab is set on the new symbols.
12533 Even though they don't appear in this symtab directly,
12534 other parts of gdb assume that symbols do, and this is
12535 reasonably true. */
12536 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
12537 TYPE_TEMPLATE_ARGUMENT (type, i)->set_symtab (symtab);
12543 /* Read DW_AT_endianity from DIE and compute the byte order that
12544 should be used. The CU's arch is used as the default. The result
12545 is true if the returned arch differs from the default, and false if
12546 they are the same. If provided, the out parameter BYTE_ORDER is
12547 also set. */
12549 static bool
12550 die_byte_order (die_info *die, dwarf2_cu *cu, enum bfd_endian *byte_order)
12552 gdbarch *arch = cu->per_objfile->objfile->arch ();
12553 enum bfd_endian arch_order = gdbarch_byte_order (arch);
12554 enum bfd_endian new_order = arch_order;
12556 attribute *attr = dwarf2_attr (die, DW_AT_endianity, cu);
12557 if (attr != nullptr && attr->form_is_constant ())
12559 int endianity = attr->constant_value (0);
12561 switch (endianity)
12563 case DW_END_big:
12564 new_order = BFD_ENDIAN_BIG;
12565 break;
12566 case DW_END_little:
12567 new_order = BFD_ENDIAN_LITTLE;
12568 break;
12569 default:
12570 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
12571 break;
12575 if (byte_order != nullptr)
12576 *byte_order = new_order;
12578 return new_order != arch_order;
12581 /* Assuming DIE is an enumeration type, and TYPE is its associated
12582 type, update TYPE using some information only available in DIE's
12583 children. In particular, the fields are computed. */
12585 static void
12586 update_enumeration_type_from_children (struct die_info *die,
12587 struct type *type,
12588 struct dwarf2_cu *cu)
12590 struct die_info *child_die;
12591 int unsigned_enum = 1;
12592 int flag_enum = 1;
12594 auto_obstack obstack;
12595 std::vector<struct field> fields;
12597 for (child_die = die->child;
12598 child_die != NULL && child_die->tag;
12599 child_die = child_die->sibling)
12601 struct attribute *attr;
12602 LONGEST value;
12603 const gdb_byte *bytes;
12604 struct dwarf2_locexpr_baton *baton;
12605 const char *name;
12607 if (child_die->tag != DW_TAG_enumerator)
12608 continue;
12610 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
12611 if (attr == NULL)
12612 continue;
12614 name = dwarf2_name (child_die, cu);
12615 if (name == NULL)
12616 name = "<anonymous enumerator>";
12618 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
12619 &value, &bytes, &baton);
12620 if (value < 0)
12622 unsigned_enum = 0;
12623 flag_enum = 0;
12625 else
12627 if (count_one_bits_ll (value) >= 2)
12628 flag_enum = 0;
12631 struct field &field = fields.emplace_back ();
12632 field.set_name (dwarf2_physname (name, child_die, cu));
12633 field.set_loc_enumval (value);
12636 if (!fields.empty ())
12637 type->copy_fields (fields);
12638 else
12639 flag_enum = 0;
12641 if (unsigned_enum)
12642 type->set_is_unsigned (true);
12644 if (flag_enum)
12645 type->set_is_flag_enum (true);
12648 /* Given a DW_AT_enumeration_type die, set its type. We do not
12649 complete the type's fields yet, or create any symbols. */
12651 static struct type *
12652 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12654 struct objfile *objfile = cu->per_objfile->objfile;
12655 struct type *type;
12656 struct attribute *attr;
12657 const char *name;
12659 /* If the definition of this type lives in .debug_types, read that type.
12660 Don't follow DW_AT_specification though, that will take us back up
12661 the chain and we want to go down. */
12662 attr = die->attr (DW_AT_signature);
12663 if (attr != nullptr)
12665 type = get_DW_AT_signature_type (die, attr, cu);
12667 /* The type's CU may not be the same as CU.
12668 Ensure TYPE is recorded with CU in die_type_hash. */
12669 return set_die_type (die, type, cu);
12672 type = type_allocator (objfile, cu->lang ()).new_type ();
12674 type->set_code (TYPE_CODE_ENUM);
12675 name = dwarf2_full_name (NULL, die, cu);
12676 if (name != NULL)
12677 type->set_name (name);
12679 attr = dwarf2_attr (die, DW_AT_type, cu);
12680 if (attr != NULL)
12682 struct type *underlying_type = die_type (die, cu);
12684 type->set_target_type (underlying_type);
12687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12688 if (attr != nullptr)
12689 type->set_length (attr->constant_value (0));
12690 else
12691 type->set_length (0);
12693 maybe_set_alignment (cu, die, type);
12695 /* The enumeration DIE can be incomplete. In Ada, any type can be
12696 declared as private in the package spec, and then defined only
12697 inside the package body. Such types are known as Taft Amendment
12698 Types. When another package uses such a type, an incomplete DIE
12699 may be generated by the compiler. */
12700 if (die_is_declaration (die, cu))
12701 type->set_is_stub (true);
12703 /* If this type has an underlying type that is not a stub, then we
12704 may use its attributes. We always use the "unsigned" attribute
12705 in this situation, because ordinarily we guess whether the type
12706 is unsigned -- but the guess can be wrong and the underlying type
12707 can tell us the reality. However, we defer to a local size
12708 attribute if one exists, because this lets the compiler override
12709 the underlying type if needed. */
12710 if (type->target_type () != NULL && !type->target_type ()->is_stub ())
12712 struct type *underlying_type = type->target_type ();
12713 underlying_type = check_typedef (underlying_type);
12715 type->set_is_unsigned (underlying_type->is_unsigned ());
12717 if (type->length () == 0)
12718 type->set_length (underlying_type->length ());
12720 if (TYPE_RAW_ALIGN (type) == 0
12721 && TYPE_RAW_ALIGN (underlying_type) != 0)
12722 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
12725 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
12727 type->set_endianity_is_not_default (die_byte_order (die, cu, nullptr));
12729 set_die_type (die, type, cu);
12731 /* Finish the creation of this type by using the enum's children.
12732 Note that, as usual, this must come after set_die_type to avoid
12733 infinite recursion when trying to compute the names of the
12734 enumerators. */
12735 update_enumeration_type_from_children (die, type, cu);
12737 return type;
12740 /* Given a pointer to a die which begins an enumeration, process all
12741 the dies that define the members of the enumeration, and create the
12742 symbol for the enumeration type.
12744 NOTE: We reverse the order of the element list. */
12746 static void
12747 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12749 struct type *this_type;
12751 this_type = get_die_type (die, cu);
12752 if (this_type == NULL)
12753 this_type = read_enumeration_type (die, cu);
12755 if (die->child != NULL)
12757 struct die_info *child_die;
12759 child_die = die->child;
12760 while (child_die && child_die->tag)
12762 if (child_die->tag != DW_TAG_enumerator)
12764 process_die (child_die, cu);
12766 else
12767 new_symbol (child_die, this_type, cu);
12769 child_die = child_die->sibling;
12773 /* If we are reading an enum from a .debug_types unit, and the enum
12774 is a declaration, and the enum is not the signatured type in the
12775 unit, then we do not want to add a symbol for it. Adding a
12776 symbol would in some cases obscure the true definition of the
12777 enum, giving users an incomplete type when the definition is
12778 actually available. Note that we do not want to do this for all
12779 enums which are just declarations, because C++0x allows forward
12780 enum declarations. */
12781 if (cu->per_cu->is_debug_types
12782 && die_is_declaration (die, cu))
12784 struct signatured_type *sig_type;
12786 sig_type = (struct signatured_type *) cu->per_cu;
12787 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
12788 if (sig_type->type_offset_in_section != die->sect_off)
12789 return;
12792 new_symbol (die, this_type, cu);
12795 /* Helper function for quirk_ada_thick_pointer that examines a bounds
12796 expression for an index type and finds the corresponding field
12797 offset in the hidden "P_BOUNDS" structure. Returns true on success
12798 and updates *FIELD, false if it fails to recognize an
12799 expression. */
12801 static bool
12802 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
12803 int *bounds_offset, struct field *field,
12804 struct dwarf2_cu *cu)
12806 struct attribute *attr = dwarf2_attr (die, name, cu);
12807 if (attr == nullptr || !attr->form_is_block ())
12808 return false;
12810 const struct dwarf_block *block = attr->as_block ();
12811 const gdb_byte *start = block->data;
12812 const gdb_byte *end = block->data + block->size;
12814 /* The expression to recognize generally looks like:
12816 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
12817 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
12819 However, the second "plus_uconst" may be missing:
12821 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
12822 DW_OP_deref_size: 4)
12824 This happens when the field is at the start of the structure.
12826 Also, the final deref may not be sized:
12828 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
12829 DW_OP_deref)
12831 This happens when the size of the index type happens to be the
12832 same as the architecture's word size. This can occur with or
12833 without the second plus_uconst. */
12835 if (end - start < 2)
12836 return false;
12837 if (*start++ != DW_OP_push_object_address)
12838 return false;
12839 if (*start++ != DW_OP_plus_uconst)
12840 return false;
12842 uint64_t this_bound_off;
12843 start = gdb_read_uleb128 (start, end, &this_bound_off);
12844 if (start == nullptr || (int) this_bound_off != this_bound_off)
12845 return false;
12846 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
12847 is consistent among all bounds. */
12848 if (*bounds_offset == -1)
12849 *bounds_offset = this_bound_off;
12850 else if (*bounds_offset != this_bound_off)
12851 return false;
12853 if (start == end || *start++ != DW_OP_deref)
12854 return false;
12856 int offset = 0;
12857 if (start ==end)
12858 return false;
12859 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
12861 /* This means an offset of 0. */
12863 else if (*start++ != DW_OP_plus_uconst)
12864 return false;
12865 else
12867 /* The size is the parameter to DW_OP_plus_uconst. */
12868 uint64_t val;
12869 start = gdb_read_uleb128 (start, end, &val);
12870 if (start == nullptr)
12871 return false;
12872 if ((int) val != val)
12873 return false;
12874 offset = val;
12877 if (start == end)
12878 return false;
12880 uint64_t size;
12881 if (*start == DW_OP_deref_size)
12883 start = gdb_read_uleb128 (start + 1, end, &size);
12884 if (start == nullptr)
12885 return false;
12887 else if (*start == DW_OP_deref)
12889 size = cu->header.addr_size;
12890 ++start;
12892 else
12893 return false;
12895 field->set_loc_bitpos (8 * offset);
12896 if (size != field->type ()->length ())
12897 field->set_bitsize (8 * size);
12899 return true;
12902 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
12903 some kinds of Ada arrays:
12905 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
12906 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
12907 <11e0> DW_AT_data_location: 2 byte block: 97 6
12908 (DW_OP_push_object_address; DW_OP_deref)
12909 <11e3> DW_AT_type : <0x1173>
12910 <11e7> DW_AT_sibling : <0x1201>
12911 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
12912 <11ec> DW_AT_type : <0x1206>
12913 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
12914 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
12915 DW_OP_deref_size: 4)
12916 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
12917 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
12918 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
12920 This actually represents a "thick pointer", which is a structure
12921 with two elements: one that is a pointer to the array data, and one
12922 that is a pointer to another structure; this second structure holds
12923 the array bounds.
12925 This returns a new type on success, or nullptr if this didn't
12926 recognize the type. */
12928 static struct type *
12929 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
12930 struct type *type)
12932 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
12933 /* So far we've only seen this with block form. */
12934 if (attr == nullptr || !attr->form_is_block ())
12935 return nullptr;
12937 /* Note that this will fail if the structure layout is changed by
12938 the compiler. However, we have no good way to recognize some
12939 other layout, because we don't know what expression the compiler
12940 might choose to emit should this happen. */
12941 struct dwarf_block *blk = attr->as_block ();
12942 if (blk->size != 2
12943 || blk->data[0] != DW_OP_push_object_address
12944 || blk->data[1] != DW_OP_deref)
12945 return nullptr;
12947 int bounds_offset = -1;
12948 int max_align = -1;
12949 std::vector<struct field> range_fields;
12950 for (struct die_info *child_die = die->child;
12951 child_die;
12952 child_die = child_die->sibling)
12954 if (child_die->tag == DW_TAG_subrange_type)
12956 struct type *underlying = read_subrange_index_type (child_die, cu);
12958 int this_align = type_align (underlying);
12959 if (this_align > max_align)
12960 max_align = this_align;
12962 range_fields.emplace_back ();
12963 range_fields.emplace_back ();
12965 struct field &lower = range_fields[range_fields.size () - 2];
12966 struct field &upper = range_fields[range_fields.size () - 1];
12968 lower.set_type (underlying);
12969 lower.set_is_artificial (true);
12971 upper.set_type (underlying);
12972 upper.set_is_artificial (true);
12974 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
12975 &bounds_offset, &lower, cu)
12976 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
12977 &bounds_offset, &upper, cu))
12978 return nullptr;
12982 /* This shouldn't really happen, but double-check that we found
12983 where the bounds are stored. */
12984 if (bounds_offset == -1)
12985 return nullptr;
12987 struct objfile *objfile = cu->per_objfile->objfile;
12988 for (int i = 0; i < range_fields.size (); i += 2)
12990 char name[20];
12992 /* Set the name of each field in the bounds. */
12993 xsnprintf (name, sizeof (name), "LB%d", i / 2);
12994 range_fields[i].set_name (objfile->intern (name));
12995 xsnprintf (name, sizeof (name), "UB%d", i / 2);
12996 range_fields[i + 1].set_name (objfile->intern (name));
12999 type_allocator alloc (objfile, cu->lang ());
13000 struct type *bounds = alloc.new_type ();
13001 bounds->set_code (TYPE_CODE_STRUCT);
13003 bounds->copy_fields (range_fields);
13005 int last_fieldno = range_fields.size () - 1;
13006 int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
13007 + bounds->field (last_fieldno).type ()->length ());
13008 bounds->set_length (align_up (bounds_size, max_align));
13010 /* Rewrite the existing array type in place. Specifically, we
13011 remove any dynamic properties we might have read, and we replace
13012 the index types. */
13013 struct type *iter = type;
13014 for (int i = 0; i < range_fields.size (); i += 2)
13016 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
13017 iter->main_type->dyn_prop_list = nullptr;
13018 iter->set_index_type
13019 (create_static_range_type (alloc, bounds->field (i).type (), 1, 0));
13020 iter = iter->target_type ();
13023 struct type *result = type_allocator (objfile, cu->lang ()).new_type ();
13024 result->set_code (TYPE_CODE_STRUCT);
13026 result->alloc_fields (2);
13028 /* The names are chosen to coincide with what the compiler does with
13029 -fgnat-encodings=all, which the Ada code in gdb already
13030 understands. */
13031 result->field (0).set_name ("P_ARRAY");
13032 result->field (0).set_type (lookup_pointer_type (type));
13034 result->field (1).set_name ("P_BOUNDS");
13035 result->field (1).set_type (lookup_pointer_type (bounds));
13036 result->field (1).set_loc_bitpos (8 * bounds_offset);
13038 result->set_name (type->name ());
13039 result->set_length (result->field (0).type ()->length ()
13040 + result->field (1).type ()->length ());
13042 return result;
13045 /* Extract all information from a DW_TAG_array_type DIE and put it in
13046 the DIE's type field. For now, this only handles one dimensional
13047 arrays. */
13049 static struct type *
13050 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13052 struct objfile *objfile = cu->per_objfile->objfile;
13053 struct die_info *child_die;
13054 struct type *type;
13055 struct type *element_type, *range_type, *index_type;
13056 struct attribute *attr;
13057 const char *name;
13058 struct dynamic_prop *byte_stride_prop = NULL;
13059 unsigned int bit_stride = 0;
13061 element_type = die_type (die, cu);
13063 /* The die_type call above may have already set the type for this DIE. */
13064 type = get_die_type (die, cu);
13065 if (type)
13066 return type;
13068 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
13069 if (attr != NULL)
13071 int stride_ok;
13072 struct type *prop_type = cu->addr_sized_int_type (false);
13074 byte_stride_prop
13075 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
13076 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
13077 prop_type);
13078 if (!stride_ok)
13080 complaint (_("unable to read array DW_AT_byte_stride "
13081 " - DIE at %s [in module %s]"),
13082 sect_offset_str (die->sect_off),
13083 objfile_name (cu->per_objfile->objfile));
13084 /* Ignore this attribute. We will likely not be able to print
13085 arrays of this type correctly, but there is little we can do
13086 to help if we cannot read the attribute's value. */
13087 byte_stride_prop = NULL;
13091 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
13092 if (attr != NULL)
13093 bit_stride = attr->constant_value (0);
13095 /* Irix 6.2 native cc creates array types without children for
13096 arrays with unspecified length. */
13097 if (die->child == NULL)
13099 index_type = builtin_type (objfile)->builtin_int;
13100 type_allocator alloc (objfile, cu->lang ());
13101 range_type = create_static_range_type (alloc, index_type, 0, -1);
13102 type = create_array_type_with_stride (alloc, element_type, range_type,
13103 byte_stride_prop, bit_stride);
13104 return set_die_type (die, type, cu);
13107 std::vector<struct type *> range_types;
13108 child_die = die->child;
13109 while (child_die && child_die->tag)
13111 if (child_die->tag == DW_TAG_subrange_type
13112 || child_die->tag == DW_TAG_generic_subrange)
13114 struct type *child_type = read_type_die (child_die, cu);
13116 if (child_type != NULL)
13118 /* The range type was successfully read. Save it for the
13119 array type creation. */
13120 range_types.push_back (child_type);
13123 child_die = child_die->sibling;
13126 if (range_types.empty ())
13128 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13129 sect_offset_str (die->sect_off),
13130 objfile_name (cu->per_objfile->objfile));
13131 return NULL;
13134 /* Dwarf2 dimensions are output from left to right, create the
13135 necessary array types in backwards order. */
13137 type = element_type;
13139 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
13140 if (read_array_order (die, cu) == DW_ORD_col_major)
13142 int i = 0;
13144 while (i < range_types.size ())
13146 type = create_array_type_with_stride (alloc, type, range_types[i++],
13147 byte_stride_prop, bit_stride);
13148 type->set_is_multi_dimensional (true);
13149 bit_stride = 0;
13150 byte_stride_prop = nullptr;
13153 else
13155 size_t ndim = range_types.size ();
13156 while (ndim-- > 0)
13158 type = create_array_type_with_stride (alloc, type, range_types[ndim],
13159 byte_stride_prop, bit_stride);
13160 type->set_is_multi_dimensional (true);
13161 bit_stride = 0;
13162 byte_stride_prop = nullptr;
13166 /* Clear the flag on the outermost array type. */
13167 type->set_is_multi_dimensional (false);
13168 gdb_assert (type != element_type);
13170 /* Understand Dwarf2 support for vector types (like they occur on
13171 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13172 array type. This is not part of the Dwarf2/3 standard yet, but a
13173 custom vendor extension. The main difference between a regular
13174 array and the vector variant is that vectors are passed by value
13175 to functions. */
13176 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13177 if (attr != nullptr)
13178 make_vector_type (type);
13180 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13181 implementation may choose to implement triple vectors using this
13182 attribute. */
13183 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13184 if (attr != nullptr && attr->form_is_unsigned ())
13186 if (attr->as_unsigned () >= type->length ())
13187 type->set_length (attr->as_unsigned ());
13188 else
13189 complaint (_("DW_AT_byte_size for array type smaller "
13190 "than the total size of elements"));
13193 name = dwarf2_name (die, cu);
13194 if (name)
13195 type->set_name (name);
13197 maybe_set_alignment (cu, die, type);
13199 struct type *replacement_type = nullptr;
13200 if (cu->lang () == language_ada)
13202 replacement_type = quirk_ada_thick_pointer (die, cu, type);
13203 if (replacement_type != nullptr)
13204 type = replacement_type;
13207 /* Install the type in the die. */
13208 set_die_type (die, type, cu, replacement_type != nullptr);
13210 /* set_die_type should be already done. */
13211 set_descriptive_type (type, die, cu);
13213 return type;
13216 static enum dwarf_array_dim_ordering
13217 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13219 struct attribute *attr;
13221 attr = dwarf2_attr (die, DW_AT_ordering, cu);
13223 if (attr != nullptr)
13225 LONGEST val = attr->constant_value (-1);
13226 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
13227 return (enum dwarf_array_dim_ordering) val;
13230 /* GNU F77 is a special case, as at 08/2004 array type info is the
13231 opposite order to the dwarf2 specification, but data is still
13232 laid out as per normal fortran.
13234 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13235 version checking. */
13237 if (cu->lang () == language_fortran
13238 && cu->producer && strstr (cu->producer, "GNU F77"))
13240 return DW_ORD_row_major;
13243 switch (cu->language_defn->array_ordering ())
13245 case array_column_major:
13246 return DW_ORD_col_major;
13247 case array_row_major:
13248 default:
13249 return DW_ORD_row_major;
13253 /* Extract all information from a DW_TAG_set_type DIE and put it in
13254 the DIE's type field. */
13256 static struct type *
13257 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13259 struct type *domain_type, *set_type;
13260 struct attribute *attr;
13262 domain_type = die_type (die, cu);
13264 /* The die_type call above may have already set the type for this DIE. */
13265 set_type = get_die_type (die, cu);
13266 if (set_type)
13267 return set_type;
13269 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
13270 set_type = create_set_type (alloc, domain_type);
13272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13273 if (attr != nullptr && attr->form_is_unsigned ())
13274 set_type->set_length (attr->as_unsigned ());
13276 maybe_set_alignment (cu, die, set_type);
13278 return set_die_type (die, set_type, cu);
13281 /* A helper for read_common_block that creates a locexpr baton.
13282 SYM is the symbol which we are marking as computed.
13283 COMMON_DIE is the DIE for the common block.
13284 COMMON_LOC is the location expression attribute for the common
13285 block itself.
13286 MEMBER_LOC is the location expression attribute for the particular
13287 member of the common block that we are processing.
13288 CU is the CU from which the above come. */
13290 static void
13291 mark_common_block_symbol_computed (struct symbol *sym,
13292 struct die_info *common_die,
13293 struct attribute *common_loc,
13294 struct attribute *member_loc,
13295 struct dwarf2_cu *cu)
13297 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13298 struct objfile *objfile = per_objfile->objfile;
13299 struct dwarf2_locexpr_baton *baton;
13300 gdb_byte *ptr;
13301 unsigned int cu_off;
13302 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
13303 LONGEST offset = 0;
13305 gdb_assert (common_loc && member_loc);
13306 gdb_assert (common_loc->form_is_block ());
13307 gdb_assert (member_loc->form_is_block ()
13308 || member_loc->form_is_constant ());
13310 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13311 baton->per_objfile = per_objfile;
13312 baton->per_cu = cu->per_cu;
13313 gdb_assert (baton->per_cu);
13315 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13317 if (member_loc->form_is_constant ())
13319 offset = member_loc->constant_value (0);
13320 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13322 else
13323 baton->size += member_loc->as_block ()->size;
13325 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
13326 baton->data = ptr;
13328 *ptr++ = DW_OP_call4;
13329 cu_off = common_die->sect_off - cu->per_cu->sect_off;
13330 store_unsigned_integer (ptr, 4, byte_order, cu_off);
13331 ptr += 4;
13333 if (member_loc->form_is_constant ())
13335 *ptr++ = DW_OP_addr;
13336 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13337 ptr += cu->header.addr_size;
13339 else
13341 /* We have to copy the data here, because DW_OP_call4 will only
13342 use a DW_AT_location attribute. */
13343 struct dwarf_block *block = member_loc->as_block ();
13344 memcpy (ptr, block->data, block->size);
13345 ptr += block->size;
13348 *ptr++ = DW_OP_plus;
13349 gdb_assert (ptr - baton->data == baton->size);
13351 SYMBOL_LOCATION_BATON (sym) = baton;
13352 sym->set_aclass_index (dwarf2_locexpr_index);
13355 /* Create appropriate locally-scoped variables for all the
13356 DW_TAG_common_block entries. Also create a struct common_block
13357 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13358 is used to separate the common blocks name namespace from regular
13359 variable names. */
13361 static void
13362 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13364 struct attribute *attr;
13366 attr = dwarf2_attr (die, DW_AT_location, cu);
13367 if (attr != nullptr)
13369 /* Support the .debug_loc offsets. */
13370 if (attr->form_is_block ())
13372 /* Ok. */
13374 else if (attr->form_is_section_offset ())
13376 dwarf2_complex_location_expr_complaint ();
13377 attr = NULL;
13379 else
13381 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13382 "common block member");
13383 attr = NULL;
13387 if (die->child != NULL)
13389 struct objfile *objfile = cu->per_objfile->objfile;
13390 struct die_info *child_die;
13391 size_t n_entries = 0, size;
13392 struct common_block *common_block;
13393 struct symbol *sym;
13395 for (child_die = die->child;
13396 child_die && child_die->tag;
13397 child_die = child_die->sibling)
13398 ++n_entries;
13400 size = (sizeof (struct common_block)
13401 + (n_entries - 1) * sizeof (struct symbol *));
13402 common_block
13403 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
13404 size);
13405 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13406 common_block->n_entries = 0;
13408 for (child_die = die->child;
13409 child_die && child_die->tag;
13410 child_die = child_die->sibling)
13412 /* Create the symbol in the DW_TAG_common_block block in the current
13413 symbol scope. */
13414 sym = new_symbol (child_die, NULL, cu);
13415 if (sym != NULL)
13417 struct attribute *member_loc;
13419 common_block->contents[common_block->n_entries++] = sym;
13421 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13422 cu);
13423 if (member_loc)
13425 /* GDB has handled this for a long time, but it is
13426 not specified by DWARF. It seems to have been
13427 emitted by gfortran at least as recently as:
13428 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13429 complaint (_("Variable in common block has "
13430 "DW_AT_data_member_location "
13431 "- DIE at %s [in module %s]"),
13432 sect_offset_str (child_die->sect_off),
13433 objfile_name (objfile));
13435 if (member_loc->form_is_section_offset ())
13436 dwarf2_complex_location_expr_complaint ();
13437 else if (member_loc->form_is_constant ()
13438 || member_loc->form_is_block ())
13440 if (attr != nullptr)
13441 mark_common_block_symbol_computed (sym, die, attr,
13442 member_loc, cu);
13444 else
13445 dwarf2_complex_location_expr_complaint ();
13450 sym = new_symbol (die, builtin_type (objfile)->builtin_void, cu);
13451 sym->set_value_common_block (common_block);
13455 /* Create a type for a C++ namespace. */
13457 static struct type *
13458 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
13460 struct objfile *objfile = cu->per_objfile->objfile;
13461 const char *previous_prefix, *name;
13462 int is_anonymous;
13463 struct type *type;
13465 /* For extensions, reuse the type of the original namespace. */
13466 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
13468 struct die_info *ext_die;
13469 struct dwarf2_cu *ext_cu = cu;
13471 ext_die = dwarf2_extension (die, &ext_cu);
13472 type = read_type_die (ext_die, ext_cu);
13474 /* EXT_CU may not be the same as CU.
13475 Ensure TYPE is recorded with CU in die_type_hash. */
13476 return set_die_type (die, type, cu);
13479 name = namespace_name (die, &is_anonymous, cu);
13481 /* Now build the name of the current namespace. */
13483 previous_prefix = determine_prefix (die, cu);
13484 gdb::unique_xmalloc_ptr<char> name_storage;
13485 if (previous_prefix[0] != '\0')
13487 name_storage = typename_concat (previous_prefix, name, 0, cu);
13488 name = name_storage.get ();
13491 /* Create the type. */
13492 type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_NAMESPACE,
13493 0, name);
13495 return set_die_type (die, type, cu);
13498 /* Read a namespace scope. */
13500 static void
13501 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
13503 struct objfile *objfile = cu->per_objfile->objfile;
13504 int is_anonymous;
13506 /* Add a symbol associated to this if we haven't seen the namespace
13507 before. Also, add a using directive if it's an anonymous
13508 namespace. */
13510 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
13512 struct type *type;
13514 type = read_type_die (die, cu);
13515 new_symbol (die, type, cu);
13517 namespace_name (die, &is_anonymous, cu);
13518 if (is_anonymous)
13520 const char *previous_prefix = determine_prefix (die, cu);
13522 std::vector<const char *> excludes;
13523 add_using_directive (using_directives (cu),
13524 previous_prefix, type->name (), NULL,
13525 NULL, excludes,
13526 read_decl_line (die, cu),
13527 &objfile->objfile_obstack);
13531 if (die->child != NULL)
13533 struct die_info *child_die = die->child;
13535 while (child_die && child_die->tag)
13537 process_die (child_die, cu);
13538 child_die = child_die->sibling;
13543 /* Read a Fortran module as type. This DIE can be only a declaration used for
13544 imported module. Still we need that type as local Fortran "use ... only"
13545 declaration imports depend on the created type in determine_prefix. */
13547 static struct type *
13548 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
13550 struct objfile *objfile = cu->per_objfile->objfile;
13551 const char *module_name;
13552 struct type *type;
13554 module_name = dwarf2_name (die, cu);
13555 type = type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_MODULE,
13556 0, module_name);
13558 return set_die_type (die, type, cu);
13561 /* Read a Fortran module. */
13563 static void
13564 read_module (struct die_info *die, struct dwarf2_cu *cu)
13566 struct die_info *child_die = die->child;
13567 struct type *type;
13569 type = read_type_die (die, cu);
13570 new_symbol (die, type, cu);
13572 while (child_die && child_die->tag)
13574 process_die (child_die, cu);
13575 child_die = child_die->sibling;
13579 /* Return the name of the namespace represented by DIE. Set
13580 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
13581 namespace. */
13583 static const char *
13584 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
13586 struct die_info *current_die;
13587 const char *name = NULL;
13589 /* Loop through the extensions until we find a name. */
13591 for (current_die = die;
13592 current_die != NULL;
13593 current_die = dwarf2_extension (die, &cu))
13595 /* We don't use dwarf2_name here so that we can detect the absence
13596 of a name -> anonymous namespace. */
13597 name = dwarf2_string_attr (die, DW_AT_name, cu);
13599 if (name != NULL)
13600 break;
13603 /* Is it an anonymous namespace? */
13605 *is_anonymous = (name == NULL);
13606 if (*is_anonymous)
13607 name = CP_ANONYMOUS_NAMESPACE_STR;
13609 return name;
13612 /* Extract all information from a DW_TAG_pointer_type DIE and add to
13613 the user defined type vector. */
13615 static struct type *
13616 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
13618 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
13619 struct comp_unit_head *cu_header = &cu->header;
13620 struct type *type;
13621 struct attribute *attr_byte_size;
13622 struct attribute *attr_address_class;
13623 int byte_size, addr_class;
13624 struct type *target_type;
13626 target_type = die_type (die, cu);
13628 /* The die_type call above may have already set the type for this DIE. */
13629 type = get_die_type (die, cu);
13630 if (type)
13631 return type;
13633 type = lookup_pointer_type (target_type);
13635 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
13636 if (attr_byte_size)
13637 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
13638 else
13639 byte_size = cu_header->addr_size;
13641 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
13642 if (attr_address_class)
13643 addr_class = attr_address_class->constant_value (DW_ADDR_none);
13644 else
13645 addr_class = DW_ADDR_none;
13647 ULONGEST alignment = get_alignment (cu, die);
13649 /* If the pointer size, alignment, or address class is different
13650 than the default, create a type variant marked as such and set
13651 the length accordingly. */
13652 if (type->length () != byte_size
13653 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
13654 && alignment != TYPE_RAW_ALIGN (type))
13655 || addr_class != DW_ADDR_none)
13657 if (gdbarch_address_class_type_flags_p (gdbarch))
13659 type_instance_flags type_flags
13660 = gdbarch_address_class_type_flags (gdbarch, byte_size,
13661 addr_class);
13662 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
13663 == 0);
13664 type = make_type_with_address_space (type, type_flags);
13666 else if (type->length () != byte_size)
13668 complaint (_("invalid pointer size %d"), byte_size);
13670 else if (TYPE_RAW_ALIGN (type) != alignment)
13672 complaint (_("Invalid DW_AT_alignment"
13673 " - DIE at %s [in module %s]"),
13674 sect_offset_str (die->sect_off),
13675 objfile_name (cu->per_objfile->objfile));
13677 else
13679 /* Should we also complain about unhandled address classes? */
13683 type->set_length (byte_size);
13684 set_type_align (type, alignment);
13685 return set_die_type (die, type, cu);
13688 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
13689 the user defined type vector. */
13691 static struct type *
13692 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
13694 struct type *type;
13695 struct type *to_type;
13696 struct type *domain;
13698 to_type = die_type (die, cu);
13699 domain = die_containing_type (die, cu);
13701 /* The calls above may have already set the type for this DIE. */
13702 type = get_die_type (die, cu);
13703 if (type)
13704 return type;
13706 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
13707 type = lookup_methodptr_type (to_type);
13708 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
13710 struct type *new_type
13711 = type_allocator (cu->per_objfile->objfile, cu->lang ()).new_type ();
13713 smash_to_method_type (new_type, domain, to_type->target_type (),
13714 to_type->fields (), to_type->num_fields (),
13715 to_type->has_varargs ());
13716 type = lookup_methodptr_type (new_type);
13718 else
13719 type = lookup_memberptr_type (to_type, domain);
13721 return set_die_type (die, type, cu);
13724 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
13725 the user defined type vector. */
13727 static struct type *
13728 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
13729 enum type_code refcode)
13731 struct comp_unit_head *cu_header = &cu->header;
13732 struct type *type, *target_type;
13733 struct attribute *attr;
13735 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
13737 target_type = die_type (die, cu);
13739 /* The die_type call above may have already set the type for this DIE. */
13740 type = get_die_type (die, cu);
13741 if (type)
13742 return type;
13744 type = lookup_reference_type (target_type, refcode);
13745 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13746 if (attr != nullptr)
13747 type->set_length (attr->constant_value (cu_header->addr_size));
13748 else
13749 type->set_length (cu_header->addr_size);
13751 maybe_set_alignment (cu, die, type);
13752 return set_die_type (die, type, cu);
13755 /* Add the given cv-qualifiers to the element type of the array. GCC
13756 outputs DWARF type qualifiers that apply to an array, not the
13757 element type. But GDB relies on the array element type to carry
13758 the cv-qualifiers. This mimics section 6.7.3 of the C99
13759 specification. */
13761 static struct type *
13762 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
13763 struct type *base_type, int cnst, int voltl)
13765 struct type *el_type, *inner_array;
13767 base_type = copy_type (base_type);
13768 inner_array = base_type;
13770 while (inner_array->target_type ()->code () == TYPE_CODE_ARRAY)
13772 inner_array->set_target_type (copy_type (inner_array->target_type ()));
13773 inner_array = inner_array->target_type ();
13776 el_type = inner_array->target_type ();
13777 cnst |= TYPE_CONST (el_type);
13778 voltl |= TYPE_VOLATILE (el_type);
13779 inner_array->set_target_type (make_cv_type (cnst, voltl, el_type, NULL));
13781 return set_die_type (die, base_type, cu);
13784 static struct type *
13785 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
13787 struct type *base_type, *cv_type;
13789 base_type = die_type (die, cu);
13791 /* The die_type call above may have already set the type for this DIE. */
13792 cv_type = get_die_type (die, cu);
13793 if (cv_type)
13794 return cv_type;
13796 /* In case the const qualifier is applied to an array type, the element type
13797 is so qualified, not the array type (section 6.7.3 of C99). */
13798 if (base_type->code () == TYPE_CODE_ARRAY)
13799 return add_array_cv_type (die, cu, base_type, 1, 0);
13801 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13802 return set_die_type (die, cv_type, cu);
13805 static struct type *
13806 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
13808 struct type *base_type, *cv_type;
13810 base_type = die_type (die, cu);
13812 /* The die_type call above may have already set the type for this DIE. */
13813 cv_type = get_die_type (die, cu);
13814 if (cv_type)
13815 return cv_type;
13817 /* In case the volatile qualifier is applied to an array type, the
13818 element type is so qualified, not the array type (section 6.7.3
13819 of C99). */
13820 if (base_type->code () == TYPE_CODE_ARRAY)
13821 return add_array_cv_type (die, cu, base_type, 0, 1);
13823 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13824 return set_die_type (die, cv_type, cu);
13827 /* Handle DW_TAG_restrict_type. */
13829 static struct type *
13830 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13832 struct type *base_type, *cv_type;
13834 base_type = die_type (die, cu);
13836 /* The die_type call above may have already set the type for this DIE. */
13837 cv_type = get_die_type (die, cu);
13838 if (cv_type)
13839 return cv_type;
13841 cv_type = make_restrict_type (base_type);
13842 return set_die_type (die, cv_type, cu);
13845 /* Handle DW_TAG_atomic_type. */
13847 static struct type *
13848 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
13850 struct type *base_type, *cv_type;
13852 base_type = die_type (die, cu);
13854 /* The die_type call above may have already set the type for this DIE. */
13855 cv_type = get_die_type (die, cu);
13856 if (cv_type)
13857 return cv_type;
13859 cv_type = make_atomic_type (base_type);
13860 return set_die_type (die, cv_type, cu);
13863 /* Extract all information from a DW_TAG_string_type DIE and add to
13864 the user defined type vector. It isn't really a user defined type,
13865 but it behaves like one, with other DIE's using an AT_user_def_type
13866 attribute to reference it. */
13868 static struct type *
13869 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
13871 struct objfile *objfile = cu->per_objfile->objfile;
13872 struct gdbarch *gdbarch = objfile->arch ();
13873 struct type *type, *range_type, *index_type, *char_type;
13874 struct attribute *attr;
13875 struct dynamic_prop prop;
13876 bool length_is_constant = true;
13877 LONGEST length;
13879 /* There are a couple of places where bit sizes might be made use of
13880 when parsing a DW_TAG_string_type, however, no producer that we know
13881 of make use of these. Handling bit sizes that are a multiple of the
13882 byte size is easy enough, but what about other bit sizes? Lets deal
13883 with that problem when we have to. Warn about these attributes being
13884 unsupported, then parse the type and ignore them like we always
13885 have. */
13886 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
13887 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
13889 static bool warning_printed = false;
13890 if (!warning_printed)
13892 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
13893 "currently supported on DW_TAG_string_type."));
13894 warning_printed = true;
13898 attr = dwarf2_attr (die, DW_AT_string_length, cu);
13899 if (attr != nullptr && !attr->form_is_constant ())
13901 /* The string length describes the location at which the length of
13902 the string can be found. The size of the length field can be
13903 specified with one of the attributes below. */
13904 struct type *prop_type;
13905 struct attribute *len
13906 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
13907 if (len == nullptr)
13908 len = dwarf2_attr (die, DW_AT_byte_size, cu);
13909 if (len != nullptr && len->form_is_constant ())
13911 /* Pass 0 as the default as we know this attribute is constant
13912 and the default value will not be returned. */
13913 LONGEST sz = len->constant_value (0);
13914 prop_type = objfile_int_type (objfile, sz, true);
13916 else
13918 /* If the size is not specified then we assume it is the size of
13919 an address on this target. */
13920 prop_type = cu->addr_sized_int_type (true);
13923 /* Convert the attribute into a dynamic property. */
13924 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
13925 length = 1;
13926 else
13927 length_is_constant = false;
13929 else if (attr != nullptr)
13931 /* This DW_AT_string_length just contains the length with no
13932 indirection. There's no need to create a dynamic property in this
13933 case. Pass 0 for the default value as we know it will not be
13934 returned in this case. */
13935 length = attr->constant_value (0);
13937 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
13939 /* We don't currently support non-constant byte sizes for strings. */
13940 length = attr->constant_value (1);
13942 else
13944 /* Use 1 as a fallback length if we have nothing else. */
13945 length = 1;
13948 index_type = builtin_type (objfile)->builtin_int;
13949 type_allocator alloc (objfile, cu->lang ());
13950 if (length_is_constant)
13951 range_type = create_static_range_type (alloc, index_type, 1, length);
13952 else
13954 struct dynamic_prop low_bound;
13956 low_bound.set_const_val (1);
13957 range_type = create_range_type (alloc, index_type, &low_bound, &prop, 0);
13959 char_type = language_string_char_type (cu->language_defn, gdbarch);
13960 type = create_string_type (alloc, char_type, range_type);
13962 return set_die_type (die, type, cu);
13965 /* Assuming that DIE corresponds to a function, returns nonzero
13966 if the function is prototyped. */
13968 static int
13969 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13971 struct attribute *attr;
13973 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13974 if (attr && attr->as_boolean ())
13975 return 1;
13977 /* The DWARF standard implies that the DW_AT_prototyped attribute
13978 is only meaningful for C, but the concept also extends to other
13979 languages that allow unprototyped functions (Eg: Objective C).
13980 For all other languages, assume that functions are always
13981 prototyped. */
13982 if (cu->lang () != language_c
13983 && cu->lang () != language_objc
13984 && cu->lang () != language_opencl)
13985 return 1;
13987 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13988 prototyped and unprototyped functions; default to prototyped,
13989 since that is more common in modern code (and RealView warns
13990 about unprototyped functions). */
13991 if (producer_is_realview (cu->producer))
13992 return 1;
13994 return 0;
13997 /* Handle DIES due to C code like:
13999 struct foo
14001 int (*funcp)(int a, long l);
14002 int b;
14005 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14007 static struct type *
14008 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14010 struct objfile *objfile = cu->per_objfile->objfile;
14011 struct type *type; /* Type that this function returns. */
14012 struct type *ftype; /* Function that returns above type. */
14013 struct attribute *attr;
14015 type = die_type (die, cu);
14017 /* PR gas/29517 occurs in 2.39, and is fixed in 2.40, but it's only fixed
14018 for dwarf version >= 3 which supports DW_TAG_unspecified_type. */
14019 if (type->code () == TYPE_CODE_VOID
14020 && !type->is_stub ()
14021 && die->child == nullptr
14022 && (cu->per_cu->version () == 2
14023 || producer_is_gas_2_39 (cu)))
14025 /* Work around PR gas/29517, pretend we have an DW_TAG_unspecified_type
14026 return type. */
14027 type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
14028 .new_type (TYPE_CODE_VOID, 0, nullptr));
14029 type->set_is_stub (true);
14032 /* The die_type call above may have already set the type for this DIE. */
14033 ftype = get_die_type (die, cu);
14034 if (ftype)
14035 return ftype;
14037 ftype = lookup_function_type (type);
14039 if (prototyped_function_p (die, cu))
14040 ftype->set_is_prototyped (true);
14042 /* Store the calling convention in the type if it's available in
14043 the subroutine die. Otherwise set the calling convention to
14044 the default value DW_CC_normal. */
14045 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14046 if (attr != nullptr
14047 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
14048 TYPE_CALLING_CONVENTION (ftype)
14049 = (enum dwarf_calling_convention) attr->constant_value (0);
14050 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14051 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14052 else
14053 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14055 /* Record whether the function returns normally to its caller or not
14056 if the DWARF producer set that information. */
14057 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14058 if (attr && attr->as_boolean ())
14059 TYPE_NO_RETURN (ftype) = 1;
14061 /* We need to add the subroutine type to the die immediately so
14062 we don't infinitely recurse when dealing with parameters
14063 declared as the same subroutine type. */
14064 set_die_type (die, ftype, cu);
14066 if (die->child != NULL)
14068 struct type *void_type = builtin_type (objfile)->builtin_void;
14069 struct die_info *child_die;
14070 int nparams, iparams;
14072 /* Count the number of parameters.
14073 FIXME: GDB currently ignores vararg functions, but knows about
14074 vararg member functions. */
14075 nparams = 0;
14076 child_die = die->child;
14077 while (child_die && child_die->tag)
14079 if (child_die->tag == DW_TAG_formal_parameter)
14080 nparams++;
14081 else if (child_die->tag == DW_TAG_unspecified_parameters)
14082 ftype->set_has_varargs (true);
14084 child_die = child_die->sibling;
14087 /* Allocate storage for parameters and fill them in. */
14088 ftype->alloc_fields (nparams);
14090 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14091 even if we error out during the parameters reading below. */
14092 for (iparams = 0; iparams < nparams; iparams++)
14093 ftype->field (iparams).set_type (void_type);
14095 iparams = 0;
14096 child_die = die->child;
14097 while (child_die && child_die->tag)
14099 if (child_die->tag == DW_TAG_formal_parameter)
14101 struct type *arg_type;
14103 /* DWARF version 2 has no clean way to discern C++
14104 static and non-static member functions. G++ helps
14105 GDB by marking the first parameter for non-static
14106 member functions (which is the this pointer) as
14107 artificial. We pass this information to
14108 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14110 DWARF version 3 added DW_AT_object_pointer, which GCC
14111 4.5 does not yet generate. */
14112 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14113 if (attr != nullptr)
14114 ftype->field (iparams).set_is_artificial (attr->as_boolean ());
14115 else
14116 ftype->field (iparams).set_is_artificial (false);
14117 arg_type = die_type (child_die, cu);
14119 /* RealView does not mark THIS as const, which the testsuite
14120 expects. GCC marks THIS as const in method definitions,
14121 but not in the class specifications (GCC PR 43053). */
14122 if (cu->lang () == language_cplus
14123 && !TYPE_CONST (arg_type)
14124 && ftype->field (iparams).is_artificial ())
14126 int is_this = 0;
14127 struct dwarf2_cu *arg_cu = cu;
14128 const char *name = dwarf2_name (child_die, cu);
14130 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14131 if (attr != nullptr)
14133 /* If the compiler emits this, use it. */
14134 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14135 is_this = 1;
14137 else if (name && strcmp (name, "this") == 0)
14138 /* Function definitions will have the argument names. */
14139 is_this = 1;
14140 else if (name == NULL && iparams == 0)
14141 /* Declarations may not have the names, so like
14142 elsewhere in GDB, assume an artificial first
14143 argument is "this". */
14144 is_this = 1;
14146 if (is_this)
14147 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14148 arg_type, 0);
14151 ftype->field (iparams).set_type (arg_type);
14152 iparams++;
14154 child_die = child_die->sibling;
14158 return ftype;
14161 static struct type *
14162 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14164 struct objfile *objfile = cu->per_objfile->objfile;
14165 const char *name = dwarf2_full_name (NULL, die, cu);
14166 struct type *this_type;
14167 struct gdbarch *gdbarch = objfile->arch ();
14168 struct type *target_type = die_type (die, cu);
14170 if (gdbarch_dwarf2_omit_typedef_p (gdbarch, target_type, cu->producer, name))
14172 /* The long double is defined as a base type in C. GCC creates a long
14173 double typedef with target-type _Float128 for the long double to
14174 identify it as the IEEE Float128 value. This is a GCC hack since the
14175 DWARF doesn't distinguish between the IBM long double and IEEE
14176 128-bit float. Replace the GCC workaround for the long double
14177 typedef with the actual type information copied from the target-type
14178 with the correct long double base type name. */
14179 this_type = copy_type (target_type);
14180 this_type->set_name (name);
14181 set_die_type (die, this_type, cu);
14182 return this_type;
14185 type_allocator alloc (objfile, cu->lang ());
14186 this_type = alloc.new_type (TYPE_CODE_TYPEDEF, 0, name);
14187 this_type->set_target_is_stub (true);
14188 set_die_type (die, this_type, cu);
14189 if (target_type != this_type)
14190 this_type->set_target_type (target_type);
14191 else
14193 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14194 spec and cause infinite loops in GDB. */
14195 complaint (_("Self-referential DW_TAG_typedef "
14196 "- DIE at %s [in module %s]"),
14197 sect_offset_str (die->sect_off), objfile_name (objfile));
14198 this_type->set_target_type (nullptr);
14200 if (name == NULL)
14202 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14203 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14204 Handle these by just returning the target type, rather than
14205 constructing an anonymous typedef type and trying to handle this
14206 elsewhere. */
14207 set_die_type (die, target_type, cu);
14208 return target_type;
14210 return this_type;
14213 /* Helper for get_dwarf2_rational_constant that computes the value of
14214 a given gmp_mpz given an attribute. */
14216 static void
14217 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
14219 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14220 location expression that pushes an implicit value. */
14221 if (attr->form == DW_FORM_exprloc)
14223 dwarf_block *blk = attr->as_block ();
14224 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
14226 uint64_t len;
14227 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
14228 blk->data + blk->size,
14229 &len);
14230 if (ptr - blk->data + len <= blk->size)
14232 value->read (gdb::make_array_view (ptr, len),
14233 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14234 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14235 true);
14236 return;
14240 /* On failure set it to 1. */
14241 *value = gdb_mpz (1);
14243 else if (attr->form_is_block ())
14245 dwarf_block *blk = attr->as_block ();
14246 value->read (gdb::make_array_view (blk->data, blk->size),
14247 bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
14248 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE,
14249 true);
14251 else if (attr->form_is_unsigned ())
14252 *value = gdb_mpz (attr->as_unsigned ());
14253 else
14254 *value = gdb_mpz (attr->constant_value (1));
14257 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14258 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14260 If the numerator and/or numerator attribute is missing,
14261 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14262 untouched. */
14264 static void
14265 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
14266 gdb_mpz *numerator, gdb_mpz *denominator)
14268 struct attribute *num_attr, *denom_attr;
14270 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
14271 if (num_attr == nullptr)
14272 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14273 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14275 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
14276 if (denom_attr == nullptr)
14277 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14278 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14280 if (num_attr == nullptr || denom_attr == nullptr)
14281 return;
14283 get_mpz (cu, numerator, num_attr);
14284 get_mpz (cu, denominator, denom_attr);
14287 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14288 rational constant, rather than a signed one.
14290 If the rational constant has a negative value, a complaint
14291 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14293 static void
14294 get_dwarf2_unsigned_rational_constant (struct die_info *die,
14295 struct dwarf2_cu *cu,
14296 gdb_mpz *numerator,
14297 gdb_mpz *denominator)
14299 gdb_mpz num (1);
14300 gdb_mpz denom (1);
14302 get_dwarf2_rational_constant (die, cu, &num, &denom);
14303 if (num < 0 && denom < 0)
14305 num.negate ();
14306 denom.negate ();
14308 else if (num < 0)
14310 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14311 " in DIE at %s"),
14312 sect_offset_str (die->sect_off));
14313 return;
14315 else if (denom < 0)
14317 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14318 " in DIE at %s"),
14319 sect_offset_str (die->sect_off));
14320 return;
14323 *numerator = std::move (num);
14324 *denominator = std::move (denom);
14327 /* Assuming that ENCODING is a string whose contents starting at the
14328 K'th character is "_nn" where "nn" is a decimal number, scan that
14329 number and set RESULT to the value. K is updated to point to the
14330 character immediately following the number.
14332 If the string does not conform to the format described above, false
14333 is returned, and K may or may not be changed. */
14335 static bool
14336 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
14338 /* The next character should be an underscore ('_') followed
14339 by a digit. */
14340 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
14341 return false;
14343 /* Skip the underscore. */
14344 k++;
14345 int start = k;
14347 /* Determine the number of digits for our number. */
14348 while (isdigit (encoding[k]))
14349 k++;
14350 if (k == start)
14351 return false;
14353 std::string copy (&encoding[start], k - start);
14354 return result->set (copy.c_str (), 10);
14357 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14358 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14359 DENOM, update OFFSET, and return true on success. Return false on
14360 failure. */
14362 static bool
14363 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
14364 gdb_mpz *num, gdb_mpz *denom)
14366 if (!ada_get_gnat_encoded_number (encoding, offset, num))
14367 return false;
14368 return ada_get_gnat_encoded_number (encoding, offset, denom);
14371 /* Assuming DIE corresponds to a fixed point type, finish the creation
14372 of the corresponding TYPE by setting its type-specific data. CU is
14373 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14374 encodings. It is nullptr if the GNAT encoding should be
14375 ignored. */
14377 static void
14378 finish_fixed_point_type (struct type *type, const char *suffix,
14379 struct die_info *die, struct dwarf2_cu *cu)
14381 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
14382 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
14384 /* If GNAT encodings are preferred, don't examine the
14385 attributes. */
14386 struct attribute *attr = nullptr;
14387 if (suffix == nullptr)
14389 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
14390 if (attr == nullptr)
14391 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
14392 if (attr == nullptr)
14393 attr = dwarf2_attr (die, DW_AT_small, cu);
14396 /* Numerator and denominator of our fixed-point type's scaling factor.
14397 The default is a scaling factor of 1, which we use as a fallback
14398 when we are not able to decode it (problem with the debugging info,
14399 unsupported forms, bug in GDB, etc...). Using that as the default
14400 allows us to at least print the unscaled value, which might still
14401 be useful to a user. */
14402 gdb_mpz scale_num (1);
14403 gdb_mpz scale_denom (1);
14405 if (attr == nullptr)
14407 int offset = 0;
14408 if (suffix != nullptr
14409 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14410 &scale_denom)
14411 /* The number might be encoded as _nn_dd_nn_dd, where the
14412 second ratio is the 'small value. In this situation, we
14413 want the second value. */
14414 && (suffix[offset] != '_'
14415 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
14416 &scale_denom)))
14418 /* Found it. */
14420 else
14422 /* Scaling factor not found. Assume a scaling factor of 1,
14423 and hope for the best. At least the user will be able to
14424 see the encoded value. */
14425 scale_num = 1;
14426 scale_denom = 1;
14427 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14428 sect_offset_str (die->sect_off));
14431 else if (attr->name == DW_AT_binary_scale)
14433 LONGEST scale_exp = attr->constant_value (0);
14434 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14436 num_or_denom <<= std::abs (scale_exp);
14438 else if (attr->name == DW_AT_decimal_scale)
14440 LONGEST scale_exp = attr->constant_value (0);
14441 gdb_mpz &num_or_denom = scale_exp > 0 ? scale_num : scale_denom;
14443 num_or_denom = gdb_mpz::pow (10, std::abs (scale_exp));
14445 else if (attr->name == DW_AT_small)
14447 struct die_info *scale_die;
14448 struct dwarf2_cu *scale_cu = cu;
14450 scale_die = follow_die_ref (die, attr, &scale_cu);
14451 if (scale_die->tag == DW_TAG_constant)
14452 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
14453 &scale_num, &scale_denom);
14454 else
14455 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14456 " (DIE at %s)"),
14457 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
14459 else
14461 complaint (_("unsupported scale attribute %s for fixed-point type"
14462 " (DIE at %s)"),
14463 dwarf_attr_name (attr->name),
14464 sect_offset_str (die->sect_off));
14467 type->fixed_point_info ().scaling_factor = gdb_mpq (scale_num, scale_denom);
14470 /* The gnat-encoding suffix for fixed point. */
14472 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14474 /* If NAME encodes an Ada fixed-point type, return a pointer to the
14475 "XF" suffix of the name. The text after this is what encodes the
14476 'small and 'delta information. Otherwise, return nullptr. */
14478 static const char *
14479 gnat_encoded_fixed_point_type_info (const char *name)
14481 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
14484 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
14485 (which may be different from NAME) to the architecture back-end to allow
14486 it to guess the correct format if necessary. */
14488 static struct type *
14489 dwarf2_init_float_type (struct dwarf2_cu *cu, int bits, const char *name,
14490 const char *name_hint, enum bfd_endian byte_order)
14492 struct objfile *objfile = cu->per_objfile->objfile;
14493 struct gdbarch *gdbarch = objfile->arch ();
14494 const struct floatformat **format;
14495 struct type *type;
14497 type_allocator alloc (objfile, cu->lang ());
14498 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
14499 if (format)
14500 type = init_float_type (alloc, bits, name, format, byte_order);
14501 else
14502 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
14504 return type;
14507 /* Allocate an integer type of size BITS and name NAME. */
14509 static struct type *
14510 dwarf2_init_integer_type (struct dwarf2_cu *cu, int bits, int unsigned_p,
14511 const char *name)
14513 struct type *type;
14514 struct objfile *objfile = cu->per_objfile->objfile;
14516 /* Versions of Intel's C Compiler generate an integer type called "void"
14517 instead of using DW_TAG_unspecified_type. This has been seen on
14518 at least versions 14, 17, and 18. */
14519 if (bits == 0 && producer_is_icc (cu) && name != nullptr
14520 && strcmp (name, "void") == 0)
14521 type = builtin_type (objfile)->builtin_void;
14522 else
14524 type_allocator alloc (objfile, cu->lang ());
14525 type = init_integer_type (alloc, bits, unsigned_p, name);
14528 return type;
14531 /* Return true if DIE has a DW_AT_small attribute whose value is
14532 a constant rational, where both the numerator and denominator
14533 are equal to zero.
14535 CU is the DIE's Compilation Unit. */
14537 static bool
14538 has_zero_over_zero_small_attribute (struct die_info *die,
14539 struct dwarf2_cu *cu)
14541 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
14542 if (attr == nullptr)
14543 return false;
14545 struct dwarf2_cu *scale_cu = cu;
14546 struct die_info *scale_die
14547 = follow_die_ref (die, attr, &scale_cu);
14549 if (scale_die->tag != DW_TAG_constant)
14550 return false;
14552 gdb_mpz num (1), denom (1);
14553 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
14554 return num == 0 && denom == 0;
14557 /* Initialise and return a floating point type of size BITS suitable for
14558 use as a component of a complex number. The NAME_HINT is passed through
14559 when initialising the floating point type and is the name of the complex
14560 type.
14562 As DWARF doesn't currently provide an explicit name for the components
14563 of a complex number, but it can be helpful to have these components
14564 named, we try to select a suitable name based on the size of the
14565 component. */
14566 static struct type *
14567 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
14568 int bits, const char *name_hint,
14569 enum bfd_endian byte_order)
14571 struct objfile *objfile = cu->per_objfile->objfile;
14572 gdbarch *gdbarch = objfile->arch ();
14573 struct type *tt = nullptr;
14575 /* Try to find a suitable floating point builtin type of size BITS.
14576 We're going to use the name of this type as the name for the complex
14577 target type that we are about to create. */
14578 switch (cu->lang ())
14580 case language_fortran:
14581 switch (bits)
14583 case 32:
14584 tt = builtin_f_type (gdbarch)->builtin_real;
14585 break;
14586 case 64:
14587 tt = builtin_f_type (gdbarch)->builtin_real_s8;
14588 break;
14589 case 96: /* The x86-32 ABI specifies 96-bit long double. */
14590 case 128:
14591 tt = builtin_f_type (gdbarch)->builtin_real_s16;
14592 break;
14594 break;
14595 default:
14596 switch (bits)
14598 case 32:
14599 tt = builtin_type (gdbarch)->builtin_float;
14600 break;
14601 case 64:
14602 if (builtin_type (gdbarch)->builtin_long_double->length () == 8
14603 && name_hint != nullptr
14604 && strstr (name_hint, "long") != nullptr)
14606 /* Use "long double" for "complex long double". */
14607 tt = builtin_type (gdbarch)->builtin_long_double;
14609 else
14610 tt = builtin_type (gdbarch)->builtin_double;
14611 break;
14612 case 96: /* The x86-32 ABI specifies 96-bit long double. */
14613 case 128:
14614 tt = builtin_type (gdbarch)->builtin_long_double;
14615 break;
14617 break;
14620 /* If the type we found doesn't match the size we were looking for, then
14621 pretend we didn't find a type at all, the complex target type we
14622 create will then be nameless. */
14623 if (tt != nullptr && tt->length () * TARGET_CHAR_BIT != bits)
14624 tt = nullptr;
14626 const char *name = (tt == nullptr) ? nullptr : tt->name ();
14627 return dwarf2_init_float_type (cu, bits, name, name_hint, byte_order);
14630 /* Find a representation of a given base type and install
14631 it in the TYPE field of the die. */
14633 static struct type *
14634 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14636 struct objfile *objfile = cu->per_objfile->objfile;
14637 struct type *type;
14638 struct attribute *attr;
14639 int encoding = 0, bits = 0;
14640 const char *name;
14642 attr = dwarf2_attr (die, DW_AT_encoding, cu);
14643 if (attr != nullptr && attr->form_is_constant ())
14644 encoding = attr->constant_value (0);
14645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14646 if (attr != nullptr)
14647 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
14648 name = dwarf2_name (die, cu);
14649 if (!name)
14650 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
14652 enum bfd_endian byte_order;
14653 bool not_default = die_byte_order (die, cu, &byte_order);
14655 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
14656 && cu->lang () == language_ada
14657 && has_zero_over_zero_small_attribute (die, cu))
14659 /* brobecker/2018-02-24: This is a fixed point type for which
14660 the scaling factor is represented as fraction whose value
14661 does not make sense (zero divided by zero), so we should
14662 normally never see these. However, there is a small category
14663 of fixed point types for which GNAT is unable to provide
14664 the scaling factor via the standard DWARF mechanisms, and
14665 for which the info is provided via the GNAT encodings instead.
14666 This is likely what this DIE is about. */
14667 encoding = (encoding == DW_ATE_signed_fixed
14668 ? DW_ATE_signed
14669 : DW_ATE_unsigned);
14672 /* With GNAT encodings, fixed-point information will be encoded in
14673 the type name. Note that this can also occur with the above
14674 zero-over-zero case, which is why this is a separate "if" rather
14675 than an "else if". */
14676 const char *gnat_encoding_suffix = nullptr;
14677 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
14678 && cu->lang () == language_ada
14679 && name != nullptr)
14681 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
14682 if (gnat_encoding_suffix != nullptr)
14684 gdb_assert (startswith (gnat_encoding_suffix,
14685 GNAT_FIXED_POINT_SUFFIX));
14686 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
14687 name, gnat_encoding_suffix - name);
14688 /* Use -1 here so that SUFFIX points at the "_" after the
14689 "XF". */
14690 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
14692 encoding = (encoding == DW_ATE_signed
14693 ? DW_ATE_signed_fixed
14694 : DW_ATE_unsigned_fixed);
14698 type_allocator alloc (objfile, cu->lang ());
14699 switch (encoding)
14701 case DW_ATE_address:
14702 /* Turn DW_ATE_address into a void * pointer. */
14703 type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
14704 type = init_pointer_type (alloc, bits, name, type);
14705 break;
14706 case DW_ATE_boolean:
14707 type = init_boolean_type (alloc, bits, 1, name);
14708 break;
14709 case DW_ATE_complex_float:
14710 type = dwarf2_init_complex_target_type (cu, bits / 2, name,
14711 byte_order);
14712 if (type->code () == TYPE_CODE_ERROR)
14714 if (name == nullptr)
14716 struct obstack *obstack
14717 = &cu->per_objfile->objfile->objfile_obstack;
14718 name = obconcat (obstack, "_Complex ", type->name (),
14719 nullptr);
14721 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
14723 else
14724 type = init_complex_type (name, type);
14725 break;
14726 case DW_ATE_decimal_float:
14727 type = init_decfloat_type (alloc, bits, name);
14728 break;
14729 case DW_ATE_float:
14730 type = dwarf2_init_float_type (cu, bits, name, name, byte_order);
14731 break;
14732 case DW_ATE_signed:
14733 type = dwarf2_init_integer_type (cu, bits, 0, name);
14734 break;
14735 case DW_ATE_unsigned:
14736 if (cu->lang () == language_fortran
14737 && name
14738 && startswith (name, "character("))
14739 type = init_character_type (alloc, bits, 1, name);
14740 else
14741 type = dwarf2_init_integer_type (cu, bits, 1, name);
14742 break;
14743 case DW_ATE_signed_char:
14744 if (cu->lang () == language_ada
14745 || cu->lang () == language_m2
14746 || cu->lang () == language_pascal
14747 || cu->lang () == language_fortran)
14748 type = init_character_type (alloc, bits, 0, name);
14749 else
14750 type = dwarf2_init_integer_type (cu, bits, 0, name);
14751 break;
14752 case DW_ATE_unsigned_char:
14753 if (cu->lang () == language_ada
14754 || cu->lang () == language_m2
14755 || cu->lang () == language_pascal
14756 || cu->lang () == language_fortran
14757 || cu->lang () == language_rust)
14758 type = init_character_type (alloc, bits, 1, name);
14759 else
14760 type = dwarf2_init_integer_type (cu, bits, 1, name);
14761 break;
14762 case DW_ATE_UTF:
14764 type = init_character_type (alloc, bits, 1, name);
14765 return set_die_type (die, type, cu);
14767 break;
14768 case DW_ATE_signed_fixed:
14769 type = init_fixed_point_type (alloc, bits, 0, name);
14770 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
14771 break;
14772 case DW_ATE_unsigned_fixed:
14773 type = init_fixed_point_type (alloc, bits, 1, name);
14774 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
14775 break;
14777 default:
14778 complaint (_("unsupported DW_AT_encoding: '%s'"),
14779 dwarf_type_encoding_name (encoding));
14780 type = alloc.new_type (TYPE_CODE_ERROR, bits, name);
14781 break;
14784 if (type->code () == TYPE_CODE_INT
14785 && name != nullptr
14786 && strcmp (name, "char") == 0)
14787 type->set_has_no_signedness (true);
14789 maybe_set_alignment (cu, die, type);
14791 type->set_endianity_is_not_default (not_default);
14793 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
14795 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14796 if (attr != nullptr && attr->form_is_constant ())
14798 unsigned real_bit_size = attr->constant_value (0);
14799 if (real_bit_size >= 0 && real_bit_size <= 8 * type->length ())
14801 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14802 /* Only use the attributes if they make sense together. */
14803 if (attr == nullptr
14804 || (attr->form_is_constant ()
14805 && attr->constant_value (0) >= 0
14806 && (attr->constant_value (0) + real_bit_size
14807 <= 8 * type->length ())))
14809 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
14810 = real_bit_size;
14811 if (attr != nullptr)
14812 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
14813 = attr->constant_value (0);
14819 return set_die_type (die, type, cu);
14822 /* A helper function that returns the name of DIE, if it refers to a
14823 variable declaration. */
14825 static const char *
14826 var_decl_name (struct die_info *die, struct dwarf2_cu *cu)
14828 if (die->tag != DW_TAG_variable)
14829 return nullptr;
14831 attribute *attr = dwarf2_attr (die, DW_AT_declaration, cu);
14832 if (attr == nullptr || !attr->as_boolean ())
14833 return nullptr;
14835 attr = dwarf2_attr (die, DW_AT_name, cu);
14836 if (attr == nullptr)
14837 return nullptr;
14838 return attr->as_string ();
14841 /* Parse dwarf attribute if it's a block, reference or constant and put the
14842 resulting value of the attribute into struct bound_prop.
14843 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
14845 static int
14846 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
14847 struct dwarf2_cu *cu, struct dynamic_prop *prop,
14848 struct type *default_type)
14850 struct dwarf2_property_baton *baton;
14851 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14852 struct objfile *objfile = per_objfile->objfile;
14853 struct obstack *obstack = &objfile->objfile_obstack;
14855 gdb_assert (default_type != NULL);
14857 if (attr == NULL || prop == NULL)
14858 return 0;
14860 if (attr->form_is_block ())
14862 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14863 baton->property_type = default_type;
14864 baton->locexpr.per_cu = cu->per_cu;
14865 baton->locexpr.per_objfile = per_objfile;
14867 struct dwarf_block block;
14868 if (attr->form == DW_FORM_data16)
14870 size_t data_size = 16;
14871 block.size = (data_size
14872 + 2 /* Extra bytes for DW_OP and arg. */);
14873 gdb_byte *data = XOBNEWVEC (obstack, gdb_byte, block.size);
14874 data[0] = DW_OP_implicit_value;
14875 data[1] = data_size;
14876 memcpy (&data[2], attr->as_block ()->data, data_size);
14877 block.data = data;
14879 else
14880 block = *attr->as_block ();
14882 baton->locexpr.size = block.size;
14883 baton->locexpr.data = block.data;
14884 switch (attr->name)
14886 case DW_AT_string_length:
14887 baton->locexpr.is_reference = true;
14888 break;
14889 default:
14890 baton->locexpr.is_reference = false;
14891 break;
14894 prop->set_locexpr (baton);
14895 gdb_assert (prop->baton () != NULL);
14897 else if (attr->form_is_ref ())
14899 struct dwarf2_cu *target_cu = cu;
14900 struct die_info *target_die;
14901 struct attribute *target_attr;
14903 target_die = follow_die_ref (die, attr, &target_cu);
14904 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
14905 if (target_attr == NULL)
14906 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
14907 target_cu);
14908 if (target_attr == nullptr)
14909 target_attr = dwarf2_attr (target_die, DW_AT_data_bit_offset,
14910 target_cu);
14911 if (target_attr == NULL)
14913 const char *name = var_decl_name (target_die, target_cu);
14914 if (name != nullptr)
14916 prop->set_variable_name (name);
14917 return 1;
14919 return 0;
14922 switch (target_attr->name)
14924 case DW_AT_location:
14925 if (target_attr->form_is_section_offset ())
14927 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14928 baton->property_type = die_type (target_die, target_cu);
14929 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
14930 prop->set_loclist (baton);
14931 gdb_assert (prop->baton () != NULL);
14933 else if (target_attr->form_is_block ())
14935 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14936 baton->property_type = die_type (target_die, target_cu);
14937 baton->locexpr.per_cu = cu->per_cu;
14938 baton->locexpr.per_objfile = per_objfile;
14939 struct dwarf_block *block = target_attr->as_block ();
14940 baton->locexpr.size = block->size;
14941 baton->locexpr.data = block->data;
14942 baton->locexpr.is_reference = true;
14943 prop->set_locexpr (baton);
14944 gdb_assert (prop->baton () != NULL);
14946 else
14948 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14949 "dynamic property");
14950 return 0;
14952 break;
14953 case DW_AT_data_member_location:
14954 case DW_AT_data_bit_offset:
14956 LONGEST offset;
14958 if (!handle_member_location (target_die, target_cu, &offset))
14959 return 0;
14961 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14962 baton->property_type = read_type_die (target_die->parent,
14963 target_cu);
14964 baton->offset_info.offset = offset;
14965 baton->offset_info.type = die_type (target_die, target_cu);
14966 prop->set_addr_offset (baton);
14967 break;
14971 else if (attr->form_is_constant ())
14972 prop->set_const_val (attr->constant_value (0));
14973 else if (attr->form_is_section_offset ())
14975 switch (attr->name)
14977 case DW_AT_string_length:
14978 baton = XOBNEW (obstack, struct dwarf2_property_baton);
14979 baton->property_type = default_type;
14980 fill_in_loclist_baton (cu, &baton->loclist, attr);
14981 prop->set_loclist (baton);
14982 gdb_assert (prop->baton () != NULL);
14983 break;
14984 default:
14985 goto invalid;
14988 else
14989 goto invalid;
14991 return 1;
14993 invalid:
14994 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
14995 dwarf2_name (die, cu));
14996 return 0;
14999 /* See read.h. */
15001 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15002 present (which is valid) then compute the default type based on the
15003 compilation units address size. */
15005 static struct type *
15006 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
15008 struct type *index_type = die_type (die, cu);
15010 /* Dwarf-2 specifications explicitly allows to create subrange types
15011 without specifying a base type.
15012 In that case, the base type must be set to the type of
15013 the lower bound, upper bound or count, in that order, if any of these
15014 three attributes references an object that has a type.
15015 If no base type is found, the Dwarf-2 specifications say that
15016 a signed integer type of size equal to the size of an address should
15017 be used.
15018 For the following C code: `extern char gdb_int [];'
15019 GCC produces an empty range DIE.
15020 FIXME: muller/2010-05-28: Possible references to object for low bound,
15021 high bound or count are not yet handled by this code. */
15022 if (index_type->code () == TYPE_CODE_VOID)
15023 index_type = cu->addr_sized_int_type (false);
15025 return index_type;
15028 /* Read the given DW_AT_subrange DIE. */
15030 static struct type *
15031 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15033 struct type *base_type, *orig_base_type;
15034 struct type *range_type;
15035 struct attribute *attr;
15036 struct dynamic_prop low, high;
15037 int low_default_is_valid;
15038 int high_bound_is_count = 0;
15039 const char *name;
15041 orig_base_type = read_subrange_index_type (die, cu);
15043 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15044 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15045 creating the range type, but we use the result of check_typedef
15046 when examining properties of the type. */
15047 base_type = check_typedef (orig_base_type);
15049 /* The die_type call above may have already set the type for this DIE. */
15050 range_type = get_die_type (die, cu);
15051 if (range_type)
15052 return range_type;
15054 high.set_const_val (0);
15056 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15057 omitting DW_AT_lower_bound. */
15058 switch (cu->lang ())
15060 case language_c:
15061 case language_cplus:
15062 low.set_const_val (0);
15063 low_default_is_valid = 1;
15064 break;
15065 case language_fortran:
15066 low.set_const_val (1);
15067 low_default_is_valid = 1;
15068 break;
15069 case language_d:
15070 case language_objc:
15071 case language_rust:
15072 low.set_const_val (0);
15073 low_default_is_valid = (cu->header.version >= 4);
15074 break;
15075 case language_ada:
15076 case language_m2:
15077 case language_pascal:
15078 low.set_const_val (1);
15079 low_default_is_valid = (cu->header.version >= 4);
15080 break;
15081 default:
15082 low.set_const_val (0);
15083 low_default_is_valid = 0;
15084 break;
15087 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15088 if (attr != nullptr)
15089 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
15090 else if (!low_default_is_valid)
15091 complaint (_("Missing DW_AT_lower_bound "
15092 "- DIE at %s [in module %s]"),
15093 sect_offset_str (die->sect_off),
15094 objfile_name (cu->per_objfile->objfile));
15096 struct attribute *attr_ub, *attr_count;
15097 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
15098 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15100 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
15101 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
15103 /* If bounds are constant do the final calculation here. */
15104 if (low.is_constant () && high.is_constant ())
15105 high.set_const_val (low.const_val () + high.const_val () - 1);
15106 else
15107 high_bound_is_count = 1;
15109 else
15111 if (attr_ub != NULL)
15112 complaint (_("Unresolved DW_AT_upper_bound "
15113 "- DIE at %s [in module %s]"),
15114 sect_offset_str (die->sect_off),
15115 objfile_name (cu->per_objfile->objfile));
15116 if (attr_count != NULL)
15117 complaint (_("Unresolved DW_AT_count "
15118 "- DIE at %s [in module %s]"),
15119 sect_offset_str (die->sect_off),
15120 objfile_name (cu->per_objfile->objfile));
15124 LONGEST bias = 0;
15125 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
15126 if (bias_attr != nullptr && bias_attr->form_is_constant ())
15127 bias = bias_attr->constant_value (0);
15129 /* Normally, the DWARF producers are expected to use a signed
15130 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15131 But this is unfortunately not always the case, as witnessed
15132 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15133 is used instead. To work around that ambiguity, we treat
15134 the bounds as signed, and thus sign-extend their values, when
15135 the base type is signed.
15137 Skip it if the base type's length is larger than ULONGEST, to avoid
15138 the undefined behavior of a too large left shift. We don't really handle
15139 constants larger than 8 bytes anyway, at the moment. */
15141 if (base_type->length () <= sizeof (ULONGEST))
15143 ULONGEST negative_mask
15144 = -((ULONGEST) 1 << (base_type->length () * TARGET_CHAR_BIT - 1));
15146 if (low.is_constant ()
15147 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
15148 low.set_const_val (low.const_val () | negative_mask);
15150 if (high.is_constant ()
15151 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
15152 high.set_const_val (high.const_val () | negative_mask);
15155 /* Check for bit and byte strides. */
15156 struct dynamic_prop byte_stride_prop;
15157 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
15158 if (attr_byte_stride != nullptr)
15160 struct type *prop_type = cu->addr_sized_int_type (false);
15161 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
15162 prop_type);
15165 struct dynamic_prop bit_stride_prop;
15166 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
15167 if (attr_bit_stride != nullptr)
15169 /* It only makes sense to have either a bit or byte stride. */
15170 if (attr_byte_stride != nullptr)
15172 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15173 "- DIE at %s [in module %s]"),
15174 sect_offset_str (die->sect_off),
15175 objfile_name (cu->per_objfile->objfile));
15176 attr_bit_stride = nullptr;
15178 else
15180 struct type *prop_type = cu->addr_sized_int_type (false);
15181 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
15182 prop_type);
15186 type_allocator alloc (cu->per_objfile->objfile, cu->lang ());
15187 if (attr_byte_stride != nullptr
15188 || attr_bit_stride != nullptr)
15190 bool byte_stride_p = (attr_byte_stride != nullptr);
15191 struct dynamic_prop *stride
15192 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
15194 range_type
15195 = create_range_type_with_stride (alloc, orig_base_type, &low,
15196 &high, bias, stride, byte_stride_p);
15198 else
15199 range_type = create_range_type (alloc, orig_base_type, &low, &high, bias);
15201 if (high_bound_is_count)
15202 range_type->bounds ()->flag_upper_bound_is_count = 1;
15204 /* Ada expects an empty array on no boundary attributes. */
15205 if (attr == NULL && cu->lang () != language_ada)
15206 range_type->bounds ()->high.set_undefined ();
15208 name = dwarf2_name (die, cu);
15209 if (name)
15210 range_type->set_name (name);
15212 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15213 if (attr != nullptr)
15214 range_type->set_length (attr->constant_value (0));
15216 maybe_set_alignment (cu, die, range_type);
15218 set_die_type (die, range_type, cu);
15220 /* set_die_type should be already done. */
15221 set_descriptive_type (range_type, die, cu);
15223 return range_type;
15226 static struct type *
15227 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15229 struct type *type;
15231 type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
15232 .new_type (TYPE_CODE_VOID, 0, nullptr));
15233 type->set_name (dwarf2_name (die, cu));
15235 /* In Ada, an unspecified type is typically used when the description
15236 of the type is deferred to a different unit. When encountering
15237 such a type, we treat it as a stub, and try to resolve it later on,
15238 when needed.
15239 Mark this as a stub type for all languages though. */
15240 type->set_is_stub (true);
15242 return set_die_type (die, type, cu);
15245 /* Read a single die and all its descendents. Set the die's sibling
15246 field to NULL; set other fields in the die correctly, and set all
15247 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15248 location of the info_ptr after reading all of those dies. PARENT
15249 is the parent of the die in question. */
15251 static struct die_info *
15252 read_die_and_children (const struct die_reader_specs *reader,
15253 const gdb_byte *info_ptr,
15254 const gdb_byte **new_info_ptr,
15255 struct die_info *parent)
15257 struct die_info *die;
15258 const gdb_byte *cur_ptr;
15260 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0, true);
15261 if (die == NULL)
15263 *new_info_ptr = cur_ptr;
15264 return NULL;
15267 bool inserted = reader->cu->die_hash.emplace (die).second;
15268 gdb_assert (inserted);
15270 if (die->has_children)
15271 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15272 else
15274 die->child = NULL;
15275 *new_info_ptr = cur_ptr;
15278 die->sibling = NULL;
15279 die->parent = parent;
15280 return die;
15283 /* Read a die, all of its descendents, and all of its siblings; set
15284 all of the fields of all of the dies correctly. Arguments are as
15285 in read_die_and_children. */
15287 static struct die_info *
15288 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15289 const gdb_byte *info_ptr,
15290 const gdb_byte **new_info_ptr,
15291 struct die_info *parent)
15293 struct die_info *first_die, *last_sibling;
15294 const gdb_byte *cur_ptr;
15296 cur_ptr = info_ptr;
15297 first_die = last_sibling = NULL;
15299 while (1)
15301 struct die_info *die
15302 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15304 if (die == NULL)
15306 *new_info_ptr = cur_ptr;
15307 return first_die;
15310 if (!first_die)
15311 first_die = die;
15312 else
15313 last_sibling->sibling = die;
15315 last_sibling = die;
15319 /* Read a die, all of its descendents, and all of its siblings; set
15320 all of the fields of all of the dies correctly. Arguments are as
15321 in read_die_and_children.
15322 This the main entry point for reading a DIE and all its children. */
15324 static struct die_info *
15325 read_die_and_siblings (const struct die_reader_specs *reader,
15326 const gdb_byte *info_ptr,
15327 const gdb_byte **new_info_ptr,
15328 struct die_info *parent)
15330 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15331 new_info_ptr, parent);
15333 if (dwarf_die_debug)
15335 gdb_printf (gdb_stdlog,
15336 "Read die from %s@0x%x of %s:\n",
15337 reader->die_section->get_name (),
15338 (unsigned) (info_ptr - reader->die_section->buffer),
15339 bfd_get_filename (reader->abfd));
15340 die->dump (dwarf_die_debug);
15343 return die;
15346 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15347 attributes.
15348 The caller is responsible for filling in the extra attributes
15349 and updating (*DIEP)->num_attrs.
15350 Set DIEP to point to a newly allocated die with its information,
15351 except for its child, sibling, and parent fields. */
15353 static const gdb_byte *
15354 read_full_die_1 (const struct die_reader_specs *reader,
15355 struct die_info **diep, const gdb_byte *info_ptr,
15356 int num_extra_attrs, bool allow_reprocess)
15358 unsigned int abbrev_number, bytes_read, i;
15359 const struct abbrev_info *abbrev;
15360 struct die_info *die;
15361 struct dwarf2_cu *cu = reader->cu;
15362 bfd *abfd = reader->abfd;
15364 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
15365 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15366 info_ptr += bytes_read;
15367 if (!abbrev_number)
15369 *diep = NULL;
15370 return info_ptr;
15373 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
15374 if (!abbrev)
15375 error (_(DWARF_ERROR_PREFIX
15376 "could not find abbrev number %d [in module %s]"),
15377 abbrev_number,
15378 bfd_get_filename (abfd));
15380 die = die_info::allocate (&cu->comp_unit_obstack,
15381 abbrev->num_attrs + num_extra_attrs);
15382 die->sect_off = sect_off;
15383 die->tag = abbrev->tag;
15384 die->abbrev = abbrev_number;
15385 die->has_children = abbrev->has_children;
15387 /* Make the result usable.
15388 The caller needs to update num_attrs after adding the extra
15389 attributes. */
15390 die->num_attrs = abbrev->num_attrs;
15392 for (i = 0; i < abbrev->num_attrs; ++i)
15393 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15394 info_ptr, allow_reprocess);
15396 *diep = die;
15397 return info_ptr;
15400 /* Read a die and all its attributes.
15401 Set DIEP to point to a newly allocated die with its information,
15402 except for its child, sibling, and parent fields. */
15404 static const gdb_byte *
15405 read_toplevel_die (const struct die_reader_specs *reader,
15406 struct die_info **diep, const gdb_byte *info_ptr,
15407 gdb::array_view<attribute *> extra_attrs)
15409 const gdb_byte *result;
15410 struct dwarf2_cu *cu = reader->cu;
15412 result = read_full_die_1 (reader, diep, info_ptr, extra_attrs.size (),
15413 false);
15415 /* Copy in the extra attributes, if any. */
15416 attribute *next = &(*diep)->attrs[(*diep)->num_attrs];
15417 for (attribute *extra : extra_attrs)
15418 *next++ = *extra;
15420 struct attribute *attr = (*diep)->attr (DW_AT_str_offsets_base);
15421 if (attr != nullptr && attr->form_is_unsigned ())
15422 cu->str_offsets_base = attr->as_unsigned ();
15424 attr = (*diep)->attr (DW_AT_loclists_base);
15425 if (attr != nullptr)
15426 cu->loclist_base = attr->as_unsigned ();
15428 auto maybe_addr_base = (*diep)->addr_base ();
15429 if (maybe_addr_base.has_value ())
15430 cu->addr_base = *maybe_addr_base;
15432 attr = (*diep)->attr (DW_AT_rnglists_base);
15433 if (attr != nullptr)
15434 cu->rnglists_base = attr->as_unsigned ();
15436 for (int i = 0; i < (*diep)->num_attrs; ++i)
15438 if ((*diep)->attrs[i].form_requires_reprocessing ())
15439 read_attribute_reprocess (reader, &(*diep)->attrs[i], (*diep)->tag);
15442 (*diep)->num_attrs += extra_attrs.size ();
15444 if (dwarf_die_debug)
15446 gdb_printf (gdb_stdlog,
15447 "Read die from %s@0x%x of %s:\n",
15448 reader->die_section->get_name (),
15449 (unsigned) (info_ptr - reader->die_section->buffer),
15450 bfd_get_filename (reader->abfd));
15451 (*diep)->dump (dwarf_die_debug);
15454 return result;
15458 void
15459 cooked_indexer::check_bounds (cutu_reader *reader)
15461 if (reader->cu->per_cu->addresses_seen)
15462 return;
15464 dwarf2_cu *cu = reader->cu;
15466 unrelocated_addr best_lowpc = {}, best_highpc = {};
15467 /* Possibly set the default values of LOWPC and HIGHPC from
15468 `DW_AT_ranges'. */
15469 dwarf2_find_base_address (reader->comp_unit_die, cu);
15470 enum pc_bounds_kind cu_bounds_kind
15471 = dwarf2_get_pc_bounds (reader->comp_unit_die, &best_lowpc, &best_highpc,
15472 cu, m_index_storage->get_addrmap (), cu->per_cu);
15473 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
15475 /* Store the contiguous range if it is not empty; it can be
15476 empty for CUs with no code. addrmap requires CORE_ADDR, so
15477 we cast here. */
15478 m_index_storage->get_addrmap ()->set_empty ((CORE_ADDR) best_lowpc,
15479 (CORE_ADDR) best_highpc - 1,
15480 cu->per_cu);
15482 cu->per_cu->addresses_seen = true;
15486 /* Helper function that returns true if TAG can have a linkage
15487 name. */
15489 static bool
15490 tag_can_have_linkage_name (enum dwarf_tag tag)
15492 switch (tag)
15494 case DW_TAG_variable:
15495 case DW_TAG_subprogram:
15496 return true;
15498 default:
15499 return false;
15503 cutu_reader *
15504 cooked_indexer::ensure_cu_exists (cutu_reader *reader,
15505 dwarf2_per_objfile *per_objfile,
15506 sect_offset sect_off, bool is_dwz,
15507 bool for_scanning)
15509 /* Lookups for type unit references are always in the CU, and
15510 cross-CU references will crash. */
15511 if (reader->cu->per_cu->is_dwz == is_dwz
15512 && reader->cu->header.offset_in_cu_p (sect_off))
15513 return reader;
15515 dwarf2_per_cu_data *per_cu
15516 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
15517 per_objfile->per_bfd);
15519 /* When scanning, we only want to visit a given CU a single time.
15520 Doing this check here avoids self-imports as well. */
15521 if (for_scanning)
15523 bool nope = false;
15524 if (!per_cu->scanned.compare_exchange_strong (nope, true))
15525 return nullptr;
15528 cutu_reader *result = m_index_storage->get_reader (per_cu);
15529 if (result == nullptr)
15531 cutu_reader new_reader (per_cu, per_objfile, nullptr, nullptr, false,
15532 &m_index_storage->get_abbrev_table_cache ());
15534 if (new_reader.dummy_p || new_reader.comp_unit_die == nullptr
15535 || !new_reader.comp_unit_die->has_children)
15536 return nullptr;
15538 prepare_one_comp_unit (new_reader.cu, new_reader.comp_unit_die,
15539 language_minimal);
15540 std::unique_ptr<cutu_reader> copy
15541 (new cutu_reader (std::move (new_reader)));
15542 result = m_index_storage->preserve (std::move (copy));
15545 if (result->dummy_p || result->comp_unit_die == nullptr
15546 || !result->comp_unit_die->has_children)
15547 return nullptr;
15549 if (for_scanning)
15550 check_bounds (result);
15552 return result;
15555 const gdb_byte *
15556 cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu,
15557 cutu_reader *reader,
15558 const gdb_byte *watermark_ptr,
15559 const gdb_byte *info_ptr,
15560 const abbrev_info *abbrev,
15561 const char **name,
15562 const char **linkage_name,
15563 cooked_index_flag *flags,
15564 sect_offset *sibling_offset,
15565 const cooked_index_entry **parent_entry,
15566 parent_map::addr_type *maybe_defer,
15567 bool *is_enum_class,
15568 bool for_specification)
15570 bool origin_is_dwz = false;
15571 bool is_declaration = false;
15572 sect_offset origin_offset {};
15574 std::optional<unrelocated_addr> low_pc;
15575 std::optional<unrelocated_addr> high_pc;
15576 bool high_pc_relative = false;
15578 for (int i = 0; i < abbrev->num_attrs; ++i)
15580 attribute attr;
15581 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15583 /* Store the data if it is of an attribute we want to keep in a
15584 partial symbol table. */
15585 switch (attr.name)
15587 case DW_AT_name:
15588 switch (abbrev->tag)
15590 case DW_TAG_compile_unit:
15591 case DW_TAG_partial_unit:
15592 case DW_TAG_type_unit:
15593 /* Compilation units have a DW_AT_name that is a filename, not
15594 a source language identifier. */
15595 break;
15597 default:
15598 if (*name == nullptr)
15599 *name = attr.as_string ();
15600 break;
15602 break;
15604 case DW_AT_linkage_name:
15605 case DW_AT_MIPS_linkage_name:
15606 /* Note that both forms of linkage name might appear. We
15607 assume they will be the same, and we only store the last
15608 one we see. */
15609 if (*linkage_name == nullptr)
15610 *linkage_name = attr.as_string ();
15611 break;
15613 /* DWARF 4 has defined a dedicated DW_AT_main_subprogram
15614 attribute to indicate the starting function of the program... */
15615 case DW_AT_main_subprogram:
15616 if (attr.as_boolean ())
15617 *flags |= IS_MAIN;
15618 break;
15620 /* ... however with older versions the DW_CC_program value of
15621 the DW_AT_calling_convention attribute was used instead as
15622 the only means available. We handle both variants then. */
15623 case DW_AT_calling_convention:
15624 if (attr.constant_value (DW_CC_normal) == DW_CC_program)
15625 *flags |= IS_MAIN;
15626 break;
15628 case DW_AT_declaration:
15629 is_declaration = attr.as_boolean ();
15630 break;
15632 case DW_AT_sibling:
15633 if (sibling_offset != nullptr)
15634 *sibling_offset = attr.get_ref_die_offset ();
15635 break;
15637 case DW_AT_specification:
15638 case DW_AT_abstract_origin:
15639 case DW_AT_extension:
15640 origin_offset = attr.get_ref_die_offset ();
15641 origin_is_dwz = attr.form == DW_FORM_GNU_ref_alt;
15642 break;
15644 case DW_AT_external:
15645 if (attr.as_boolean ())
15646 *flags &= ~IS_STATIC;
15647 break;
15649 case DW_AT_enum_class:
15650 if (attr.as_boolean ())
15651 *is_enum_class = true;
15652 break;
15654 case DW_AT_low_pc:
15655 low_pc = attr.as_address ();
15656 break;
15658 case DW_AT_high_pc:
15659 high_pc = attr.as_address ();
15660 if (reader->cu->header.version >= 4 && attr.form_is_constant ())
15661 high_pc_relative = true;
15662 break;
15664 case DW_AT_location:
15665 if (!scanning_per_cu->addresses_seen && attr.form_is_block ())
15667 struct dwarf_block *locdesc = attr.as_block ();
15668 CORE_ADDR addr;
15669 if (decode_locdesc (locdesc, reader->cu, &addr)
15670 && (addr != 0
15671 || reader->cu->per_objfile->per_bfd->has_section_at_zero))
15673 low_pc = (unrelocated_addr) addr;
15674 /* For variables, we don't want to try decoding the
15675 type just to find the size -- for gdb's purposes
15676 we only need the address of a variable. */
15677 high_pc = (unrelocated_addr) (addr + 1);
15678 high_pc_relative = false;
15681 break;
15683 case DW_AT_ranges:
15684 if (!scanning_per_cu->addresses_seen)
15686 /* Offset in the .debug_ranges or .debug_rnglist section
15687 (depending on DWARF version). */
15688 ULONGEST ranges_offset = attr.as_unsigned ();
15690 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
15691 want to add this value. */
15692 ranges_offset += reader->cu->gnu_ranges_base;
15694 unrelocated_addr lowpc, highpc;
15695 dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, reader->cu,
15696 m_index_storage->get_addrmap (),
15697 scanning_per_cu, abbrev->tag);
15699 break;
15703 /* We don't want to examine declarations, but if we found a
15704 declaration when handling DW_AT_specification or the like, then
15705 that is ok. Similarly, we allow an external variable without a
15706 location; those are resolved via minimal symbols. */
15707 if (is_declaration && !for_specification
15708 && !(abbrev->tag == DW_TAG_variable && (*flags & IS_STATIC) == 0))
15710 /* We always want to recurse into some types, but we may not
15711 want to treat them as definitions. */
15712 if ((abbrev->tag == DW_TAG_class_type
15713 || abbrev->tag == DW_TAG_structure_type
15714 || abbrev->tag == DW_TAG_union_type
15715 || abbrev->tag == DW_TAG_namespace)
15716 && abbrev->has_children)
15717 *flags |= IS_TYPE_DECLARATION;
15718 else
15720 *linkage_name = nullptr;
15721 *name = nullptr;
15724 else if ((*name == nullptr
15725 || (*linkage_name == nullptr
15726 && tag_can_have_linkage_name (abbrev->tag))
15727 || (*parent_entry == nullptr && m_language != language_c))
15728 && origin_offset != sect_offset (0))
15730 cutu_reader *new_reader
15731 = ensure_cu_exists (reader, reader->cu->per_objfile, origin_offset,
15732 origin_is_dwz, false);
15733 if (new_reader == nullptr)
15734 error (_(DWARF_ERROR_PREFIX
15735 "cannot follow reference to DIE at %s"
15736 " [in module %s]"),
15737 sect_offset_str (origin_offset),
15738 bfd_get_filename (reader->abfd));
15740 const gdb_byte *new_info_ptr = (new_reader->buffer
15741 + to_underlying (origin_offset));
15743 if (*parent_entry == nullptr)
15745 /* We only perform immediate lookups of parents for DIEs
15746 from earlier in this CU. This avoids any problem
15747 with a NULL result when when we see a reference to a
15748 DIE in another CU that we may or may not have
15749 imported locally. */
15750 parent_map::addr_type addr = parent_map::form_addr (new_info_ptr);
15751 if (new_reader->cu != reader->cu || new_info_ptr > watermark_ptr)
15752 *maybe_defer = addr;
15753 else
15754 *parent_entry = m_die_range_map->find (addr);
15757 unsigned int bytes_read;
15758 const abbrev_info *new_abbrev = peek_die_abbrev (*new_reader,
15759 new_info_ptr,
15760 &bytes_read);
15762 if (new_abbrev == nullptr)
15763 error (_(DWARF_ERROR_PREFIX
15764 "Unexpected null DIE at offset %s [in module %s]"),
15765 sect_offset_str (origin_offset),
15766 bfd_get_filename (new_reader->abfd));
15768 new_info_ptr += bytes_read;
15770 if (new_reader->cu == reader->cu && new_info_ptr == watermark_ptr)
15772 /* Self-reference, we're done. */
15774 else
15775 scan_attributes (scanning_per_cu, new_reader, new_info_ptr,
15776 new_info_ptr, new_abbrev, name, linkage_name,
15777 flags, nullptr, parent_entry, maybe_defer,
15778 is_enum_class, true);
15781 if (!for_specification)
15783 if (m_language == language_ada
15784 && *linkage_name == nullptr)
15785 *linkage_name = *name;
15787 if (!scanning_per_cu->addresses_seen
15788 && low_pc.has_value ()
15789 && (reader->cu->per_objfile->per_bfd->has_section_at_zero
15790 || *low_pc != (unrelocated_addr) 0)
15791 && high_pc.has_value ())
15793 if (high_pc_relative)
15794 high_pc = (unrelocated_addr) ((ULONGEST) *high_pc
15795 + (ULONGEST) *low_pc);
15797 if (*high_pc > *low_pc)
15799 /* Need CORE_ADDR casts for addrmap. */
15800 m_index_storage->get_addrmap ()->set_empty
15801 ((CORE_ADDR) *low_pc, (CORE_ADDR) *high_pc - 1,
15802 scanning_per_cu);
15806 if (abbrev->tag == DW_TAG_module || abbrev->tag == DW_TAG_namespace)
15807 *flags &= ~IS_STATIC;
15809 if (abbrev->tag == DW_TAG_namespace && *name == nullptr)
15810 *name = "(anonymous namespace)";
15812 if (m_language == language_cplus
15813 && (abbrev->tag == DW_TAG_class_type
15814 || abbrev->tag == DW_TAG_interface_type
15815 || abbrev->tag == DW_TAG_structure_type
15816 || abbrev->tag == DW_TAG_union_type
15817 || abbrev->tag == DW_TAG_enumeration_type
15818 || abbrev->tag == DW_TAG_enumerator))
15819 *flags &= ~IS_STATIC;
15821 /* Keep in sync with new_symbol. */
15822 if (abbrev->tag == DW_TAG_subprogram
15823 && (m_language == language_ada
15824 || m_language == language_fortran))
15825 *flags &= ~IS_STATIC;
15828 return info_ptr;
15831 const gdb_byte *
15832 cooked_indexer::index_imported_unit (cutu_reader *reader,
15833 const gdb_byte *info_ptr,
15834 const abbrev_info *abbrev)
15836 sect_offset sect_off {};
15837 bool is_dwz = false;
15839 for (int i = 0; i < abbrev->num_attrs; ++i)
15841 /* Note that we never need to reprocess attributes here. */
15842 attribute attr;
15843 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15845 if (attr.name == DW_AT_import)
15847 sect_off = attr.get_ref_die_offset ();
15848 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15849 || reader->cu->per_cu->is_dwz);
15853 /* Did not find DW_AT_import. */
15854 if (sect_off == sect_offset (0))
15855 return info_ptr;
15857 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
15858 cutu_reader *new_reader = ensure_cu_exists (reader, per_objfile, sect_off,
15859 is_dwz, true);
15860 if (new_reader != nullptr)
15862 index_dies (new_reader, new_reader->info_ptr, nullptr, false);
15864 reader->cu->add_dependence (new_reader->cu->per_cu);
15867 return info_ptr;
15870 const gdb_byte *
15871 cooked_indexer::recurse (cutu_reader *reader,
15872 const gdb_byte *info_ptr,
15873 std::variant<const cooked_index_entry *,
15874 parent_map::addr_type> parent,
15875 bool fully)
15877 info_ptr = index_dies (reader, info_ptr, parent, fully);
15879 if (!std::holds_alternative<const cooked_index_entry *> (parent))
15880 return info_ptr;
15881 const cooked_index_entry *parent_entry
15882 = std::get<const cooked_index_entry *> (parent);
15884 if (parent_entry != nullptr)
15886 /* Both start and end are inclusive, so use both "+ 1" and "- 1" to
15887 limit the range to the children of parent_entry. */
15888 parent_map::addr_type start
15889 = parent_map::form_addr (reader->buffer
15890 + to_underlying (parent_entry->die_offset)
15891 + 1);
15892 parent_map::addr_type end = parent_map::form_addr (info_ptr - 1);
15893 m_die_range_map->add_entry (start, end, parent_entry);
15896 return info_ptr;
15899 const gdb_byte *
15900 cooked_indexer::index_dies (cutu_reader *reader,
15901 const gdb_byte *info_ptr,
15902 std::variant<const cooked_index_entry *,
15903 parent_map::addr_type> parent,
15904 bool fully)
15906 const gdb_byte *end_ptr = (reader->buffer
15907 + to_underlying (reader->cu->header.sect_off)
15908 + reader->cu->header.get_length_with_initial ());
15910 while (info_ptr < end_ptr)
15912 sect_offset this_die = (sect_offset) (info_ptr - reader->buffer);
15913 unsigned int bytes_read;
15914 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
15915 &bytes_read);
15916 info_ptr += bytes_read;
15917 if (abbrev == nullptr)
15918 break;
15920 if (abbrev->tag == DW_TAG_imported_unit)
15922 info_ptr = index_imported_unit (reader, info_ptr, abbrev);
15923 continue;
15926 parent_map::addr_type defer {};
15927 if (std::holds_alternative<parent_map::addr_type> (parent))
15928 defer = std::get<parent_map::addr_type> (parent);
15929 const cooked_index_entry *parent_entry = nullptr;
15930 if (std::holds_alternative<const cooked_index_entry *> (parent))
15931 parent_entry = std::get<const cooked_index_entry *> (parent);
15933 /* If a DIE parent is a DW_TAG_subprogram, then the DIE is only
15934 interesting if it's a DW_TAG_subprogram or a DW_TAG_entry_point. */
15935 bool die_interesting
15936 = (abbrev->interesting
15937 && (parent_entry == nullptr
15938 || parent_entry->tag != DW_TAG_subprogram
15939 || abbrev->tag == DW_TAG_subprogram
15940 || abbrev->tag == DW_TAG_entry_point));
15942 if (!die_interesting)
15944 info_ptr = skip_one_die (reader, info_ptr, abbrev, !fully);
15945 if (fully && abbrev->has_children)
15946 info_ptr = index_dies (reader, info_ptr, parent, fully);
15947 continue;
15950 const char *name = nullptr;
15951 const char *linkage_name = nullptr;
15952 cooked_index_flag flags = IS_STATIC;
15953 sect_offset sibling {};
15954 const cooked_index_entry *this_parent_entry = parent_entry;
15955 bool is_enum_class = false;
15957 /* The scope of a DW_TAG_entry_point cooked_index_entry is the one of
15958 its surrounding subroutine. */
15959 if (abbrev->tag == DW_TAG_entry_point)
15960 this_parent_entry = parent_entry->get_parent ();
15961 info_ptr = scan_attributes (reader->cu->per_cu, reader, info_ptr,
15962 info_ptr, abbrev, &name, &linkage_name,
15963 &flags, &sibling, &this_parent_entry,
15964 &defer, &is_enum_class, false);
15965 /* A DW_TAG_entry_point inherits its static/extern property from
15966 the enclosing subroutine. */
15967 if (abbrev->tag == DW_TAG_entry_point)
15969 flags &= ~IS_STATIC;
15970 flags |= parent_entry->flags & IS_STATIC;
15973 if (abbrev->tag == DW_TAG_namespace
15974 && m_language == language_cplus
15975 && strcmp (name, "::") == 0)
15977 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
15978 generated bogus DW_TAG_namespace DIEs with a name of "::"
15979 for the global namespace. Work around this problem
15980 here. */
15981 name = nullptr;
15984 cooked_index_entry *this_entry = nullptr;
15985 if (name != nullptr)
15987 if (defer != 0)
15988 this_entry
15989 = m_index_storage->add (this_die, abbrev->tag,
15990 flags | IS_PARENT_DEFERRED, name,
15991 defer, m_per_cu);
15992 else
15993 this_entry
15994 = m_index_storage->add (this_die, abbrev->tag, flags, name,
15995 this_parent_entry, m_per_cu);
15998 if (linkage_name != nullptr)
16000 /* We only want this to be "main" if it has a linkage name
16001 but not an ordinary name. */
16002 if (name != nullptr)
16003 flags = flags & ~IS_MAIN;
16004 /* Set the IS_LINKAGE on for everything except when functions
16005 have linkage name present but name is absent. */
16006 if (name != nullptr
16007 || (abbrev->tag != DW_TAG_subprogram
16008 && abbrev->tag != DW_TAG_inlined_subroutine
16009 && abbrev->tag != DW_TAG_entry_point))
16010 flags = flags | IS_LINKAGE;
16011 m_index_storage->add (this_die, abbrev->tag, flags,
16012 linkage_name, nullptr, m_per_cu);
16015 if (abbrev->has_children)
16017 switch (abbrev->tag)
16019 case DW_TAG_class_type:
16020 case DW_TAG_interface_type:
16021 case DW_TAG_structure_type:
16022 case DW_TAG_union_type:
16023 if (m_language != language_c && this_entry != nullptr)
16025 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16026 continue;
16028 break;
16030 case DW_TAG_enumeration_type:
16031 /* Some versions of gdc could emit an "enum class"
16032 without a name, which is nonsensical. These are
16033 skipped. */
16034 if (is_enum_class && this_entry == nullptr)
16035 continue;
16037 /* We need to recurse even for an anonymous enumeration.
16038 Which scope we record as the parent scope depends on
16039 whether we're reading an "enum class". If so, we use
16040 the enum itself as the parent, yielding names like
16041 "enum_class::enumerator"; otherwise we inject the
16042 names into our own parent scope. */
16044 std::variant<const cooked_index_entry *,
16045 parent_map::addr_type> recurse_parent;
16046 if (is_enum_class)
16048 gdb_assert (this_entry != nullptr);
16049 recurse_parent = this_entry;
16051 else if (defer != 0)
16052 recurse_parent = defer;
16053 else
16054 recurse_parent = this_parent_entry;
16056 info_ptr = recurse (reader, info_ptr, recurse_parent, fully);
16058 continue;
16060 case DW_TAG_module:
16061 if (this_entry == nullptr)
16062 break;
16063 [[fallthrough]];
16064 case DW_TAG_namespace:
16065 /* We don't check THIS_ENTRY for a namespace, to handle
16066 the ancient G++ workaround pointed out above. */
16067 info_ptr = recurse (reader, info_ptr, this_entry, fully);
16068 continue;
16070 case DW_TAG_subprogram:
16071 if ((m_language == language_fortran
16072 || m_language == language_ada)
16073 && this_entry != nullptr)
16075 info_ptr = recurse (reader, info_ptr, this_entry, true);
16076 continue;
16078 break;
16081 if (sibling != sect_offset (0))
16083 const gdb_byte *sibling_ptr
16084 = reader->buffer + to_underlying (sibling);
16086 if (sibling_ptr < info_ptr)
16087 complaint (_("DW_AT_sibling points backwards"));
16088 else if (sibling_ptr > reader->buffer_end)
16089 reader->die_section->overflow_complaint ();
16090 else
16091 info_ptr = sibling_ptr;
16093 else
16094 info_ptr = skip_children (reader, info_ptr);
16098 return info_ptr;
16101 void
16102 cooked_indexer::make_index (cutu_reader *reader)
16104 check_bounds (reader);
16105 find_file_and_directory (reader->comp_unit_die, reader->cu);
16106 if (!reader->comp_unit_die->has_children)
16107 return;
16108 index_dies (reader, reader->info_ptr, nullptr, false);
16111 struct compunit_symtab *
16112 cooked_index_functions::find_compunit_symtab_by_address
16113 (struct objfile *objfile, CORE_ADDR address)
16115 if (objfile->sect_index_data == -1)
16116 return nullptr;
16118 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16119 cooked_index *table = wait (objfile, true);
16121 CORE_ADDR baseaddr = objfile->data_section_offset ();
16122 dwarf2_per_cu_data *per_cu
16123 = table->lookup ((unrelocated_addr) (address - baseaddr));
16124 if (per_cu == nullptr)
16125 return nullptr;
16127 return dw2_instantiate_symtab (per_cu, per_objfile, false);
16130 bool
16131 cooked_index_functions::expand_symtabs_matching
16132 (struct objfile *objfile,
16133 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
16134 const lookup_name_info *lookup_name,
16135 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
16136 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
16137 block_search_flags search_flags,
16138 domain_search_flags domain,
16139 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
16141 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16143 cooked_index *table = wait (objfile, true);
16145 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
16147 /* This invariant is documented in quick-functions.h. */
16148 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
16149 if (lookup_name == nullptr)
16151 for (dwarf2_per_cu_data *per_cu
16152 : all_units_range (per_objfile->per_bfd))
16154 QUIT;
16156 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
16157 file_matcher,
16158 expansion_notify,
16159 lang_matcher))
16160 return false;
16162 return true;
16165 lookup_name_info lookup_name_without_params
16166 = lookup_name->make_ignore_params ();
16167 bool completing = lookup_name->completion_mode ();
16169 /* Unique styles of language splitting. */
16170 static const enum language unique_styles[] =
16172 /* No splitting is also a style. */
16173 language_c,
16174 /* This includes Rust. */
16175 language_cplus,
16176 /* This includes Go. */
16177 language_d,
16178 language_ada
16181 symbol_name_match_type match_type
16182 = lookup_name_without_params.match_type ();
16184 std::bitset<nr_languages> unique_styles_used;
16185 if (lang_matcher != nullptr)
16186 for (unsigned iter = 0; iter < nr_languages; ++iter)
16188 enum language lang = (enum language) iter;
16189 if (!lang_matcher (lang))
16190 continue;
16192 switch (lang)
16194 case language_cplus:
16195 case language_rust:
16196 unique_styles_used[language_cplus] = true;
16197 break;
16198 case language_d:
16199 case language_go:
16200 unique_styles_used[language_d] = true;
16201 break;
16202 case language_ada:
16203 unique_styles_used[language_ada] = true;
16204 break;
16205 default:
16206 unique_styles_used[language_c] = true;
16209 if (unique_styles_used.count ()
16210 == sizeof (unique_styles) / sizeof (unique_styles[0]))
16211 break;
16214 for (enum language lang : unique_styles)
16216 if (lang_matcher != nullptr
16217 && !unique_styles_used.test (lang))
16218 continue;
16220 std::vector<std::string_view> name_vec
16221 = lookup_name_without_params.split_name (lang);
16222 std::vector<std::string> name_str_vec (name_vec.begin (), name_vec.end ());
16223 std::vector<lookup_name_info> segment_lookup_names;
16224 segment_lookup_names.reserve (name_vec.size ());
16225 for (auto &segment_name : name_str_vec)
16227 segment_lookup_names.emplace_back (segment_name,
16228 symbol_name_match_type::FULL, completing, true);
16231 for (const cooked_index_entry *entry : table->find (name_str_vec.back (),
16232 completing))
16234 QUIT;
16236 /* No need to consider symbols from expanded CUs. */
16237 if (per_objfile->symtab_set_p (entry->per_cu))
16238 continue;
16240 /* If file-matching was done, we don't need to consider
16241 symbols from unmarked CUs. */
16242 if (file_matcher != nullptr && !entry->per_cu->mark)
16243 continue;
16245 /* See if the symbol matches the type filter. */
16246 if (!entry->matches (search_flags)
16247 || !entry->matches (domain))
16248 continue;
16250 if (lang_matcher != nullptr)
16252 /* Try to skip CUs with non-matching language. */
16253 entry->per_cu->ensure_lang (per_objfile);
16254 if (!entry->per_cu->maybe_multi_language ()
16255 && !lang_matcher (entry->per_cu->lang ()))
16256 continue;
16259 /* We've found the base name of the symbol; now walk its
16260 parentage chain, ensuring that each component
16261 matches. */
16262 bool found = true;
16264 const cooked_index_entry *parent = entry->get_parent ();
16265 for (int i = name_vec.size () - 1; i > 0; --i)
16267 /* If we ran out of entries, or if this segment doesn't
16268 match, this did not match. */
16269 if (parent == nullptr)
16271 found = false;
16272 break;
16274 if (parent->lang != language_unknown)
16276 const language_defn *lang_def = language_def (parent->lang);
16277 symbol_name_matcher_ftype *name_matcher
16278 = lang_def->get_symbol_name_matcher
16279 (segment_lookup_names[i-1]);
16280 if (!name_matcher (parent->canonical,
16281 segment_lookup_names[i-1], nullptr))
16283 found = false;
16284 break;
16288 parent = parent->get_parent ();
16291 if (!found)
16292 continue;
16294 /* Might have been looking for "a::b" and found
16295 "x::a::b". */
16296 if (symbol_matcher == nullptr)
16298 if ((match_type == symbol_name_match_type::FULL
16299 || (lang != language_ada
16300 && match_type == symbol_name_match_type::EXPRESSION)))
16302 if (parent != nullptr)
16303 continue;
16305 if (entry->lang != language_unknown)
16307 const language_defn *lang_def = language_def (entry->lang);
16308 symbol_name_matcher_ftype *name_matcher
16309 = lang_def->get_symbol_name_matcher
16310 (segment_lookup_names.back ());
16311 if (!name_matcher (entry->canonical,
16312 segment_lookup_names.back (), nullptr))
16313 continue;
16317 else
16319 auto_obstack temp_storage;
16320 const char *full_name = entry->full_name (&temp_storage);
16321 if (!symbol_matcher (full_name))
16322 continue;
16325 if (!dw2_expand_symtabs_matching_one (entry->per_cu, per_objfile,
16326 file_matcher,
16327 expansion_notify, nullptr))
16328 return false;
16332 return true;
16335 /* Start reading .debug_info using the indexer. */
16337 static void
16338 start_debug_info_reader (dwarf2_per_objfile *per_objfile)
16340 /* Set the index table early so that sharing works even while
16341 scanning; and then start the scanning. */
16342 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
16343 std::unique_ptr<cooked_index_worker> worker
16344 (new cooked_index_debug_info (per_objfile));
16345 cooked_index *idx = new cooked_index (per_objfile, std::move (worker));
16346 per_bfd->index_table.reset (idx);
16347 /* Don't start reading until after 'index_table' is set. This
16348 avoids races. */
16349 idx->start_reading ();
16354 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16355 contents from the given SECTION in the HEADER.
16357 HEADER_OFFSET is the offset of the header in the section. */
16358 static void
16359 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
16360 struct dwarf2_section_info *section,
16361 sect_offset header_offset)
16363 unsigned int bytes_read;
16364 bfd *abfd = section->get_bfd_owner ();
16365 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
16367 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
16368 info_ptr += bytes_read;
16370 header->version = read_2_bytes (abfd, info_ptr);
16371 info_ptr += 2;
16373 header->addr_size = read_1_byte (abfd, info_ptr);
16374 info_ptr += 1;
16376 header->segment_collector_size = read_1_byte (abfd, info_ptr);
16377 info_ptr += 1;
16379 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
16382 /* Return the DW_AT_loclists_base value for the CU. */
16383 static ULONGEST
16384 lookup_loclist_base (struct dwarf2_cu *cu)
16386 /* For the .dwo unit, the loclist_base points to the first offset following
16387 the header. The header consists of the following entities-
16388 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16389 bit format)
16390 2. version (2 bytes)
16391 3. address size (1 byte)
16392 4. segment selector size (1 byte)
16393 5. offset entry count (4 bytes)
16394 These sizes are derived as per the DWARFv5 standard. */
16395 if (cu->dwo_unit != nullptr)
16397 if (cu->header.initial_length_size == 4)
16398 return LOCLIST_HEADER_SIZE32;
16399 return LOCLIST_HEADER_SIZE64;
16401 return cu->loclist_base;
16404 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16405 array of offsets in the .debug_loclists section. */
16407 static sect_offset
16408 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
16410 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16411 struct objfile *objfile = per_objfile->objfile;
16412 bfd *abfd = objfile->obfd.get ();
16413 ULONGEST loclist_header_size =
16414 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
16415 : LOCLIST_HEADER_SIZE64);
16416 ULONGEST loclist_base = lookup_loclist_base (cu);
16418 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16419 ULONGEST start_offset =
16420 loclist_base + loclist_index * cu->header.offset_size;
16422 /* Get loclists section. */
16423 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16425 /* Read the loclists section content. */
16426 section->read (objfile);
16427 if (section->buffer == NULL)
16428 error (_("DW_FORM_loclistx used without .debug_loclists "
16429 "section [in module %s]"), objfile_name (objfile));
16431 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16432 so if loclist_base is smaller than the header size, we have a problem. */
16433 if (loclist_base < loclist_header_size)
16434 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16435 objfile_name (objfile));
16437 /* Read the header of the loclists contribution. */
16438 struct loclists_rnglists_header header;
16439 read_loclists_rnglists_header (&header, section,
16440 (sect_offset) (loclist_base - loclist_header_size));
16442 /* Verify the loclist index is valid. */
16443 if (loclist_index >= header.offset_entry_count)
16444 error (_("DW_FORM_loclistx pointing outside of "
16445 ".debug_loclists offset array [in module %s]"),
16446 objfile_name (objfile));
16448 /* Validate that reading won't go beyond the end of the section. */
16449 if (start_offset + cu->header.offset_size > section->size)
16450 error (_("Reading DW_FORM_loclistx index beyond end of"
16451 ".debug_loclists section [in module %s]"),
16452 objfile_name (objfile));
16454 const gdb_byte *info_ptr = section->buffer + start_offset;
16456 if (cu->header.offset_size == 4)
16457 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
16458 else
16459 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
16462 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16463 array of offsets in the .debug_rnglists section. */
16465 static sect_offset
16466 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
16467 dwarf_tag tag)
16469 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
16470 struct objfile *objfile = dwarf2_per_objfile->objfile;
16471 bfd *abfd = objfile->obfd.get ();
16472 ULONGEST rnglist_header_size =
16473 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
16474 : RNGLIST_HEADER_SIZE64);
16476 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16477 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16478 doesn't apply. */
16479 ULONGEST rnglist_base =
16480 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
16482 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16483 ULONGEST start_offset =
16484 rnglist_base + rnglist_index * cu->header.offset_size;
16486 /* Get rnglists section. */
16487 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
16489 /* Read the rnglists section content. */
16490 section->read (objfile);
16491 if (section->buffer == nullptr)
16492 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16493 "[in module %s]"),
16494 objfile_name (objfile));
16496 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16497 so if rnglist_base is smaller than the header size, we have a problem. */
16498 if (rnglist_base < rnglist_header_size)
16499 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16500 objfile_name (objfile));
16502 /* Read the header of the rnglists contribution. */
16503 struct loclists_rnglists_header header;
16504 read_loclists_rnglists_header (&header, section,
16505 (sect_offset) (rnglist_base - rnglist_header_size));
16507 /* Verify the rnglist index is valid. */
16508 if (rnglist_index >= header.offset_entry_count)
16509 error (_("DW_FORM_rnglistx index pointing outside of "
16510 ".debug_rnglists offset array [in module %s]"),
16511 objfile_name (objfile));
16513 /* Validate that reading won't go beyond the end of the section. */
16514 if (start_offset + cu->header.offset_size > section->size)
16515 error (_("Reading DW_FORM_rnglistx index beyond end of"
16516 ".debug_rnglists section [in module %s]"),
16517 objfile_name (objfile));
16519 const gdb_byte *info_ptr = section->buffer + start_offset;
16521 if (cu->header.offset_size == 4)
16522 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
16523 else
16524 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
16527 /* Process the attributes that had to be skipped in the first round. These
16528 attributes are the ones that need str_offsets_base or addr_base attributes.
16529 They could not have been processed in the first round, because at the time
16530 the values of str_offsets_base or addr_base may not have been known. */
16531 static void
16532 read_attribute_reprocess (const struct die_reader_specs *reader,
16533 struct attribute *attr, dwarf_tag tag)
16535 struct dwarf2_cu *cu = reader->cu;
16536 switch (attr->form)
16538 case DW_FORM_addrx:
16539 case DW_FORM_GNU_addr_index:
16540 attr->set_address (read_addr_index (cu,
16541 attr->as_unsigned_reprocess ()));
16542 break;
16543 case DW_FORM_loclistx:
16545 sect_offset loclists_sect_off
16546 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
16548 attr->set_unsigned (to_underlying (loclists_sect_off));
16550 break;
16551 case DW_FORM_rnglistx:
16553 sect_offset rnglists_sect_off
16554 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
16556 attr->set_unsigned (to_underlying (rnglists_sect_off));
16558 break;
16559 case DW_FORM_strx:
16560 case DW_FORM_strx1:
16561 case DW_FORM_strx2:
16562 case DW_FORM_strx3:
16563 case DW_FORM_strx4:
16564 case DW_FORM_GNU_str_index:
16566 unsigned int str_index = attr->as_unsigned_reprocess ();
16567 gdb_assert (!attr->canonical_string_p ());
16568 if (reader->dwo_file != NULL)
16569 attr->set_string_noncanonical (read_dwo_str_index (reader,
16570 str_index));
16571 else
16572 attr->set_string_noncanonical (read_stub_str_index (cu,
16573 str_index));
16574 break;
16576 default:
16577 gdb_assert_not_reached ("Unexpected DWARF form.");
16581 /* Read an attribute value described by an attribute form. */
16583 static const gdb_byte *
16584 read_attribute_value (const struct die_reader_specs *reader,
16585 struct attribute *attr, unsigned form,
16586 LONGEST implicit_const, const gdb_byte *info_ptr,
16587 bool allow_reprocess)
16589 struct dwarf2_cu *cu = reader->cu;
16590 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16591 struct objfile *objfile = per_objfile->objfile;
16592 bfd *abfd = reader->abfd;
16593 struct comp_unit_head *cu_header = &cu->header;
16594 unsigned int bytes_read;
16595 struct dwarf_block *blk;
16597 attr->form = (enum dwarf_form) form;
16598 switch (form)
16600 case DW_FORM_ref_addr:
16601 if (cu_header->version == 2)
16602 attr->set_unsigned ((ULONGEST) cu_header->read_address (abfd, info_ptr,
16603 &bytes_read));
16604 else
16605 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
16606 &bytes_read));
16607 info_ptr += bytes_read;
16608 break;
16609 case DW_FORM_GNU_ref_alt:
16610 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
16611 &bytes_read));
16612 info_ptr += bytes_read;
16613 break;
16614 case DW_FORM_addr:
16616 unrelocated_addr addr = cu_header->read_address (abfd, info_ptr,
16617 &bytes_read);
16618 attr->set_address (addr);
16619 info_ptr += bytes_read;
16621 break;
16622 case DW_FORM_block2:
16623 blk = dwarf_alloc_block (cu);
16624 blk->size = read_2_bytes (abfd, info_ptr);
16625 info_ptr += 2;
16626 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16627 info_ptr += blk->size;
16628 attr->set_block (blk);
16629 break;
16630 case DW_FORM_block4:
16631 blk = dwarf_alloc_block (cu);
16632 blk->size = read_4_bytes (abfd, info_ptr);
16633 info_ptr += 4;
16634 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16635 info_ptr += blk->size;
16636 attr->set_block (blk);
16637 break;
16638 case DW_FORM_data2:
16639 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
16640 info_ptr += 2;
16641 break;
16642 case DW_FORM_data4:
16643 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
16644 info_ptr += 4;
16645 break;
16646 case DW_FORM_data8:
16647 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
16648 info_ptr += 8;
16649 break;
16650 case DW_FORM_data16:
16651 blk = dwarf_alloc_block (cu);
16652 blk->size = 16;
16653 blk->data = read_n_bytes (abfd, info_ptr, 16);
16654 info_ptr += 16;
16655 attr->set_block (blk);
16656 break;
16657 case DW_FORM_sec_offset:
16658 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
16659 &bytes_read));
16660 info_ptr += bytes_read;
16661 break;
16662 case DW_FORM_loclistx:
16664 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
16665 &bytes_read));
16666 info_ptr += bytes_read;
16667 if (allow_reprocess)
16668 read_attribute_reprocess (reader, attr);
16670 break;
16671 case DW_FORM_string:
16672 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
16673 &bytes_read));
16674 info_ptr += bytes_read;
16675 break;
16676 case DW_FORM_strp:
16677 if (!cu->per_cu->is_dwz)
16679 attr->set_string_noncanonical
16680 (read_indirect_string (per_objfile,
16681 abfd, info_ptr, cu_header,
16682 &bytes_read));
16683 info_ptr += bytes_read;
16684 break;
16686 [[fallthrough]];
16687 case DW_FORM_line_strp:
16688 if (!cu->per_cu->is_dwz)
16690 attr->set_string_noncanonical
16691 (per_objfile->read_line_string (info_ptr, cu_header,
16692 &bytes_read));
16693 info_ptr += bytes_read;
16694 break;
16696 [[fallthrough]];
16697 case DW_FORM_GNU_strp_alt:
16699 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
16700 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
16701 &bytes_read);
16703 attr->set_string_noncanonical
16704 (dwz->read_string (objfile, str_offset));
16705 info_ptr += bytes_read;
16707 break;
16708 case DW_FORM_exprloc:
16709 case DW_FORM_block:
16710 blk = dwarf_alloc_block (cu);
16711 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16712 info_ptr += bytes_read;
16713 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16714 info_ptr += blk->size;
16715 attr->set_block (blk);
16716 break;
16717 case DW_FORM_block1:
16718 blk = dwarf_alloc_block (cu);
16719 blk->size = read_1_byte (abfd, info_ptr);
16720 info_ptr += 1;
16721 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16722 info_ptr += blk->size;
16723 attr->set_block (blk);
16724 break;
16725 case DW_FORM_data1:
16726 case DW_FORM_flag:
16727 attr->set_unsigned (read_1_byte (abfd, info_ptr));
16728 info_ptr += 1;
16729 break;
16730 case DW_FORM_flag_present:
16731 attr->set_unsigned (1);
16732 break;
16733 case DW_FORM_sdata:
16734 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
16735 info_ptr += bytes_read;
16736 break;
16737 case DW_FORM_rnglistx:
16739 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
16740 &bytes_read));
16741 info_ptr += bytes_read;
16742 if (allow_reprocess)
16743 read_attribute_reprocess (reader, attr);
16745 break;
16746 case DW_FORM_udata:
16747 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16748 info_ptr += bytes_read;
16749 break;
16750 case DW_FORM_ref1:
16751 attr->set_unsigned ((to_underlying (cu_header->sect_off)
16752 + read_1_byte (abfd, info_ptr)));
16753 info_ptr += 1;
16754 break;
16755 case DW_FORM_ref2:
16756 attr->set_unsigned ((to_underlying (cu_header->sect_off)
16757 + read_2_bytes (abfd, info_ptr)));
16758 info_ptr += 2;
16759 break;
16760 case DW_FORM_ref4:
16761 attr->set_unsigned ((to_underlying (cu_header->sect_off)
16762 + read_4_bytes (abfd, info_ptr)));
16763 info_ptr += 4;
16764 break;
16765 case DW_FORM_ref8:
16766 attr->set_unsigned ((to_underlying (cu_header->sect_off)
16767 + read_8_bytes (abfd, info_ptr)));
16768 info_ptr += 8;
16769 break;
16770 case DW_FORM_ref_sig8:
16771 attr->set_signature (read_8_bytes (abfd, info_ptr));
16772 info_ptr += 8;
16773 break;
16774 case DW_FORM_ref_udata:
16775 attr->set_unsigned ((to_underlying (cu_header->sect_off)
16776 + read_unsigned_leb128 (abfd, info_ptr,
16777 &bytes_read)));
16778 info_ptr += bytes_read;
16779 break;
16780 case DW_FORM_indirect:
16781 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16782 info_ptr += bytes_read;
16783 if (form == DW_FORM_implicit_const)
16785 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16786 info_ptr += bytes_read;
16788 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16789 info_ptr, allow_reprocess);
16790 break;
16791 case DW_FORM_implicit_const:
16792 attr->set_signed (implicit_const);
16793 break;
16794 case DW_FORM_addrx:
16795 case DW_FORM_GNU_addr_index:
16796 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
16797 &bytes_read));
16798 info_ptr += bytes_read;
16799 if (allow_reprocess)
16800 read_attribute_reprocess (reader, attr);
16801 break;
16802 case DW_FORM_strx:
16803 case DW_FORM_strx1:
16804 case DW_FORM_strx2:
16805 case DW_FORM_strx3:
16806 case DW_FORM_strx4:
16807 case DW_FORM_GNU_str_index:
16809 ULONGEST str_index;
16810 if (form == DW_FORM_strx1)
16812 str_index = read_1_byte (abfd, info_ptr);
16813 info_ptr += 1;
16815 else if (form == DW_FORM_strx2)
16817 str_index = read_2_bytes (abfd, info_ptr);
16818 info_ptr += 2;
16820 else if (form == DW_FORM_strx3)
16822 str_index = read_3_bytes (abfd, info_ptr);
16823 info_ptr += 3;
16825 else if (form == DW_FORM_strx4)
16827 str_index = read_4_bytes (abfd, info_ptr);
16828 info_ptr += 4;
16830 else
16832 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16833 info_ptr += bytes_read;
16835 attr->set_unsigned_reprocess (str_index);
16836 if (allow_reprocess)
16837 read_attribute_reprocess (reader, attr);
16839 break;
16840 default:
16841 error (_(DWARF_ERROR_PREFIX
16842 "Cannot handle %s in DWARF reader [in module %s]"),
16843 dwarf_form_name (form),
16844 bfd_get_filename (abfd));
16847 /* Super hack. */
16848 if (cu->per_cu->is_dwz && attr->form_is_ref ())
16849 attr->form = DW_FORM_GNU_ref_alt;
16851 /* We have seen instances where the compiler tried to emit a byte
16852 size attribute of -1 which ended up being encoded as an unsigned
16853 0xffffffff. Although 0xffffffff is technically a valid size value,
16854 an object of this size seems pretty unlikely so we can relatively
16855 safely treat these cases as if the size attribute was invalid and
16856 treat them as zero by default. */
16857 if (attr->name == DW_AT_byte_size
16858 && form == DW_FORM_data4
16859 && attr->as_unsigned () >= 0xffffffff)
16861 complaint
16862 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16863 hex_string (attr->as_unsigned ()));
16864 attr->set_unsigned (0);
16867 return info_ptr;
16870 /* Read an attribute described by an abbreviated attribute. */
16872 static const gdb_byte *
16873 read_attribute (const struct die_reader_specs *reader,
16874 struct attribute *attr, const struct attr_abbrev *abbrev,
16875 const gdb_byte *info_ptr,
16876 bool allow_reprocess)
16878 attr->name = abbrev->name;
16879 attr->string_is_canonical = 0;
16880 return read_attribute_value (reader, attr, abbrev->form,
16881 abbrev->implicit_const, info_ptr,
16882 allow_reprocess);
16885 /* See read.h. */
16887 const char *
16888 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
16889 LONGEST str_offset)
16891 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
16892 str_offset, "DW_FORM_strp");
16895 /* Return pointer to string at .debug_str offset as read from BUF.
16896 BUF is assumed to be in a compilation unit described by CU_HEADER.
16897 Return *BYTES_READ_PTR count of bytes read from BUF. */
16899 static const char *
16900 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
16901 const gdb_byte *buf,
16902 const struct comp_unit_head *cu_header,
16903 unsigned int *bytes_read_ptr)
16905 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
16907 return read_indirect_string_at_offset (per_objfile, str_offset);
16910 /* See read.h. */
16912 const char *
16913 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
16914 unsigned int offset_size)
16916 bfd *abfd = objfile->obfd.get ();
16917 ULONGEST str_offset = read_offset (abfd, buf, offset_size);
16919 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
16922 /* See read.h. */
16924 const char *
16925 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
16926 const struct comp_unit_head *cu_header,
16927 unsigned int *bytes_read_ptr)
16929 bfd *abfd = objfile->obfd.get ();
16930 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
16932 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
16935 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
16936 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
16937 ADDR_SIZE is the size of addresses from the CU header. */
16939 static unrelocated_addr
16940 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
16941 std::optional<ULONGEST> addr_base, int addr_size)
16943 struct objfile *objfile = per_objfile->objfile;
16944 bfd *abfd = objfile->obfd.get ();
16945 const gdb_byte *info_ptr;
16946 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
16948 per_objfile->per_bfd->addr.read (objfile);
16949 if (per_objfile->per_bfd->addr.buffer == NULL)
16950 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
16951 objfile_name (objfile));
16952 if (addr_base_or_zero + addr_index * addr_size
16953 >= per_objfile->per_bfd->addr.size)
16954 error (_("DW_FORM_addr_index pointing outside of "
16955 ".debug_addr section [in module %s]"),
16956 objfile_name (objfile));
16957 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
16958 + addr_index * addr_size);
16959 if (addr_size == 4)
16960 return (unrelocated_addr) bfd_get_32 (abfd, info_ptr);
16961 else
16962 return (unrelocated_addr) bfd_get_64 (abfd, info_ptr);
16965 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
16967 static unrelocated_addr
16968 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16970 return read_addr_index_1 (cu->per_objfile, addr_index,
16971 cu->addr_base, cu->header.addr_size);
16974 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
16976 static unrelocated_addr
16977 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
16978 unsigned int *bytes_read)
16980 bfd *abfd = cu->per_objfile->objfile->obfd.get ();
16981 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16983 return read_addr_index (cu, addr_index);
16986 /* See read.h. */
16988 unrelocated_addr
16989 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
16990 dwarf2_per_objfile *per_objfile,
16991 unsigned int addr_index)
16993 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
16994 std::optional<ULONGEST> addr_base;
16995 int addr_size;
16997 /* We need addr_base and addr_size.
16998 If we don't have PER_CU->cu, we have to get it.
16999 Nasty, but the alternative is storing the needed info in PER_CU,
17000 which at this point doesn't seem justified: it's not clear how frequently
17001 it would get used and it would increase the size of every PER_CU.
17002 Entry points like dwarf2_per_cu_addr_size do a similar thing
17003 so we're not in uncharted territory here.
17004 Alas we need to be a bit more complicated as addr_base is contained
17005 in the DIE.
17007 We don't need to read the entire CU(/TU).
17008 We just need the header and top level die.
17010 IWBN to use the aging mechanism to let us lazily later discard the CU.
17011 For now we skip this optimization. */
17013 if (cu != NULL)
17015 addr_base = cu->addr_base;
17016 addr_size = cu->header.addr_size;
17018 else
17020 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
17021 addr_base = reader.cu->addr_base;
17022 addr_size = reader.cu->header.addr_size;
17025 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
17028 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17029 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17030 DWO file. */
17032 static const char *
17033 read_str_index (struct dwarf2_cu *cu,
17034 struct dwarf2_section_info *str_section,
17035 struct dwarf2_section_info *str_offsets_section,
17036 ULONGEST str_offsets_base, ULONGEST str_index,
17037 unsigned offset_size)
17039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17040 struct objfile *objfile = per_objfile->objfile;
17041 const char *objf_name = objfile_name (objfile);
17042 bfd *abfd = objfile->obfd.get ();
17043 const gdb_byte *info_ptr;
17044 ULONGEST str_offset;
17045 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17047 str_section->read (objfile);
17048 str_offsets_section->read (objfile);
17049 if (str_section->buffer == NULL)
17050 error (_("%s used without %s section"
17051 " in CU at offset %s [in module %s]"),
17052 form_name, str_section->get_name (),
17053 sect_offset_str (cu->header.sect_off), objf_name);
17054 if (str_offsets_section->buffer == NULL)
17055 error (_("%s used without %s section"
17056 " in CU at offset %s [in module %s]"),
17057 form_name, str_section->get_name (),
17058 sect_offset_str (cu->header.sect_off), objf_name);
17059 info_ptr = (str_offsets_section->buffer
17060 + str_offsets_base
17061 + str_index * offset_size);
17062 if (offset_size == 4)
17063 str_offset = bfd_get_32 (abfd, info_ptr);
17064 else
17065 str_offset = bfd_get_64 (abfd, info_ptr);
17066 if (str_offset >= str_section->size)
17067 error (_("Offset from %s pointing outside of"
17068 " %s section in CU at offset %s [in module %s]"),
17069 form_name, str_section->get_name (),
17070 sect_offset_str (cu->header.sect_off), objf_name);
17071 return (const char *) (str_section->buffer + str_offset);
17074 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17076 static const char *
17077 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17079 unsigned offset_size;
17080 ULONGEST str_offsets_base;
17081 if (reader->cu->header.version >= 5)
17083 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17084 so assume the .debug_str_offsets section is DWARF5 as well, and
17085 parse the header. FIXME: Parse the header only once. */
17086 unsigned int bytes_read = 0;
17087 bfd *abfd = reader->dwo_file->sections.str_offsets.get_bfd_owner ();
17088 const gdb_byte *p = reader->dwo_file->sections.str_offsets.buffer;
17090 /* Header: Initial length. */
17091 read_initial_length (abfd, p + bytes_read, &bytes_read);
17093 /* Determine offset_size based on the .debug_str_offsets header. */
17094 const bool dwarf5_is_dwarf64 = bytes_read != 4;
17095 offset_size = dwarf5_is_dwarf64 ? 8 : 4;
17097 /* Header: Version. */
17098 unsigned version = read_2_bytes (abfd, p + bytes_read);
17099 bytes_read += 2;
17101 if (version <= 4)
17103 /* We'd like one warning here about ignoring the section, but
17104 because we parse the header more than once (see FIXME above)
17105 we'd have many warnings, so use a complaint instead, which at
17106 least has a limit. */
17107 complaint (_("Section .debug_str_offsets in %s has unsupported"
17108 " version %d, use empty string."),
17109 reader->dwo_file->dwo_name.c_str (), version);
17110 return "";
17113 /* Header: Padding. */
17114 bytes_read += 2;
17116 str_offsets_base = bytes_read;
17118 else
17120 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17121 section, assume the .debug_str_offsets section is pre-DWARF5 as
17122 well, which doesn't have a header. */
17123 str_offsets_base = 0;
17125 /* Determine offset_size based on the .debug_info header. */
17126 offset_size = reader->cu->header.offset_size;
17129 return read_str_index (reader->cu,
17130 &reader->dwo_file->sections.str,
17131 &reader->dwo_file->sections.str_offsets,
17132 str_offsets_base, str_index, offset_size);
17135 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17137 static const char *
17138 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
17140 struct objfile *objfile = cu->per_objfile->objfile;
17141 const char *objf_name = objfile_name (objfile);
17142 static const char form_name[] = "DW_FORM_GNU_str_index";
17143 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
17145 if (!cu->str_offsets_base.has_value ())
17146 error (_("%s used in Fission stub without %s"
17147 " in CU at offset 0x%lx [in module %s]"),
17148 form_name, str_offsets_attr_name,
17149 (long) cu->header.offset_size, objf_name);
17151 return read_str_index (cu,
17152 &cu->per_objfile->per_bfd->str,
17153 &cu->per_objfile->per_bfd->str_offsets,
17154 *cu->str_offsets_base, str_index,
17155 cu->header.offset_size);
17158 /* Return the length of an LEB128 number in BUF. */
17160 static int
17161 leb128_size (const gdb_byte *buf)
17163 const gdb_byte *begin = buf;
17164 gdb_byte byte;
17166 while (1)
17168 byte = *buf++;
17169 if ((byte & 128) == 0)
17170 return buf - begin;
17174 /* Converts DWARF language names to GDB language names. */
17176 enum language
17177 dwarf_lang_to_enum_language (unsigned int lang)
17179 enum language language;
17181 switch (lang)
17183 case DW_LANG_C89:
17184 case DW_LANG_C99:
17185 case DW_LANG_C11:
17186 case DW_LANG_C17:
17187 case DW_LANG_C23:
17188 case DW_LANG_C:
17189 case DW_LANG_UPC:
17190 language = language_c;
17191 break;
17192 case DW_LANG_Java:
17193 case DW_LANG_C_plus_plus:
17194 case DW_LANG_C_plus_plus_11:
17195 case DW_LANG_C_plus_plus_14:
17196 case DW_LANG_C_plus_plus_17:
17197 case DW_LANG_C_plus_plus_20:
17198 case DW_LANG_C_plus_plus_23:
17199 language = language_cplus;
17200 break;
17201 case DW_LANG_D:
17202 language = language_d;
17203 break;
17204 case DW_LANG_Fortran77:
17205 case DW_LANG_Fortran90:
17206 case DW_LANG_Fortran95:
17207 case DW_LANG_Fortran03:
17208 case DW_LANG_Fortran08:
17209 case DW_LANG_Fortran18:
17210 case DW_LANG_Fortran23:
17211 language = language_fortran;
17212 break;
17213 case DW_LANG_Go:
17214 language = language_go;
17215 break;
17216 case DW_LANG_Assembly:
17217 case DW_LANG_Mips_Assembler:
17218 language = language_asm;
17219 break;
17220 case DW_LANG_Ada83:
17221 case DW_LANG_Ada95:
17222 case DW_LANG_Ada2005:
17223 case DW_LANG_Ada2012:
17224 language = language_ada;
17225 break;
17226 case DW_LANG_Modula2:
17227 language = language_m2;
17228 break;
17229 case DW_LANG_Pascal83:
17230 language = language_pascal;
17231 break;
17232 case DW_LANG_ObjC:
17233 language = language_objc;
17234 break;
17235 case DW_LANG_Rust:
17236 case DW_LANG_Rust_old:
17237 language = language_rust;
17238 break;
17239 case DW_LANG_OpenCL:
17240 language = language_opencl;
17241 break;
17242 case DW_LANG_Cobol74:
17243 case DW_LANG_Cobol85:
17244 default:
17245 language = language_minimal;
17246 break;
17249 return language;
17252 /* Return the NAME attribute of DIE in *CU, or return NULL if not there. Also
17253 return in *CU the cu in which the attribute was actually found. */
17255 static struct attribute *
17256 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu **cu)
17258 for (;;)
17260 unsigned int i;
17261 struct attribute *spec = NULL;
17263 for (i = 0; i < die->num_attrs; ++i)
17265 if (die->attrs[i].name == name)
17266 return &die->attrs[i];
17267 if (die->attrs[i].name == DW_AT_specification
17268 || die->attrs[i].name == DW_AT_abstract_origin)
17269 spec = &die->attrs[i];
17272 if (!spec)
17273 break;
17275 struct die_info *prev_die = die;
17276 die = follow_die_ref (die, spec, cu);
17277 if (die == prev_die)
17278 /* Self-reference, we're done. */
17279 break;
17282 return NULL;
17285 /* Return the NAME attribute of DIE in CU, or return NULL if not there. */
17287 static struct attribute *
17288 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17290 return dwarf2_attr (die, name, &cu);
17293 /* Return the string associated with a string-typed attribute, or NULL if it
17294 is either not found or is of an incorrect type. */
17296 static const char *
17297 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17299 struct attribute *attr;
17300 const char *str = NULL;
17302 attr = dwarf2_attr (die, name, cu);
17304 if (attr != NULL)
17306 str = attr->as_string ();
17307 if (str == nullptr)
17308 complaint (_("string type expected for attribute %s for "
17309 "DIE at %s [in module %s]"),
17310 dwarf_attr_name (name), sect_offset_str (die->sect_off),
17311 objfile_name (cu->per_objfile->objfile));
17314 return str;
17317 /* Return the dwo name or NULL if not present. If present, it is in either
17318 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17319 static const char *
17320 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
17322 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
17323 if (dwo_name == nullptr)
17324 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
17325 return dwo_name;
17328 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17329 and holds a non-zero value. This function should only be used for
17330 DW_FORM_flag or DW_FORM_flag_present attributes. */
17332 static int
17333 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17335 struct attribute *attr = dwarf2_attr (die, name, cu);
17337 return attr != nullptr && attr->as_boolean ();
17340 static int
17341 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17343 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17344 which value is non-zero. However, we have to be careful with
17345 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17346 (via dwarf2_flag_true_p) follows this attribute. So we may
17347 end up accidentally finding a declaration attribute that belongs
17348 to a different DIE referenced by the specification attribute,
17349 even though the given DIE does not have a declaration attribute. */
17350 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17351 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17354 /* Return the die giving the specification for DIE, if there is
17355 one. *SPEC_CU is the CU containing DIE on input, and the CU
17356 containing the return value on output. If there is no
17357 specification, but there is an abstract origin, that is
17358 returned. */
17360 static struct die_info *
17361 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17363 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17364 *spec_cu);
17366 if (spec_attr == NULL)
17367 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17369 if (spec_attr == NULL)
17370 return NULL;
17371 else
17372 return follow_die_ref (die, spec_attr, spec_cu);
17375 /* A convenience function to find the proper .debug_line section for a CU. */
17377 static struct dwarf2_section_info *
17378 get_debug_line_section (struct dwarf2_cu *cu)
17380 struct dwarf2_section_info *section;
17381 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17383 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17384 DWO file. */
17385 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17386 section = &cu->dwo_unit->dwo_file->sections.line;
17387 else if (cu->per_cu->is_dwz)
17389 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
17391 section = &dwz->line;
17393 else
17394 section = &per_objfile->per_bfd->line;
17396 return section;
17399 /* Read the statement program header starting at OFFSET in
17400 .debug_line, or .debug_line.dwo. Return a pointer
17401 to a struct line_header, allocated using xmalloc.
17402 Returns NULL if there is a problem reading the header, e.g., if it
17403 has a version we don't understand.
17405 NOTE: the strings in the include directory and file name tables of
17406 the returned object point into the dwarf line section buffer,
17407 and must not be freed. */
17409 static line_header_up
17410 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu,
17411 const char *comp_dir)
17413 struct dwarf2_section_info *section;
17414 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17416 section = get_debug_line_section (cu);
17417 section->read (per_objfile->objfile);
17418 if (section->buffer == NULL)
17420 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17421 complaint (_("missing .debug_line.dwo section"));
17422 else
17423 complaint (_("missing .debug_line section"));
17424 return 0;
17427 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
17428 per_objfile, section, &cu->header,
17429 comp_dir);
17432 /* Subroutine of dwarf_decode_lines to simplify it.
17433 Return the file name for the given file_entry.
17434 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17435 If space for the result is malloc'd, *NAME_HOLDER will be set.
17436 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17437 equivalent to CU_INFO. */
17439 static const char *
17440 compute_include_file_name (const struct line_header *lh, const file_entry &fe,
17441 const file_and_directory &cu_info,
17442 std::string &name_holder)
17444 const char *include_name = fe.name;
17445 const char *include_name_to_compare = include_name;
17447 const char *dir_name = fe.include_dir (lh);
17449 std::string hold_compare;
17450 if (!IS_ABSOLUTE_PATH (include_name)
17451 && (dir_name != nullptr || cu_info.get_comp_dir () != nullptr))
17453 /* Avoid creating a duplicate name for CU_INFO.
17454 We do this by comparing INCLUDE_NAME and CU_INFO.
17455 Before we do the comparison, however, we need to account
17456 for DIR_NAME and COMP_DIR.
17457 First prepend dir_name (if non-NULL). If we still don't
17458 have an absolute path prepend comp_dir (if non-NULL).
17459 However, the directory we record in the include-file's
17460 psymtab does not contain COMP_DIR (to match the
17461 corresponding symtab(s)).
17463 Example:
17465 bash$ cd /tmp
17466 bash$ gcc -g ./hello.c
17467 include_name = "hello.c"
17468 dir_name = "."
17469 DW_AT_comp_dir = comp_dir = "/tmp"
17470 DW_AT_name = "./hello.c"
17474 if (dir_name != NULL)
17476 name_holder = path_join (dir_name, include_name);
17477 include_name = name_holder.c_str ();
17478 include_name_to_compare = include_name;
17480 if (!IS_ABSOLUTE_PATH (include_name)
17481 && cu_info.get_comp_dir () != nullptr)
17483 hold_compare = path_join (cu_info.get_comp_dir (), include_name);
17484 include_name_to_compare = hold_compare.c_str ();
17488 std::string copied_name;
17489 const char *cu_filename = cu_info.get_name ();
17490 if (!IS_ABSOLUTE_PATH (cu_filename) && cu_info.get_comp_dir () != nullptr)
17492 copied_name = path_join (cu_info.get_comp_dir (), cu_filename);
17493 cu_filename = copied_name.c_str ();
17496 if (FILENAME_CMP (include_name_to_compare, cu_filename) == 0)
17497 return nullptr;
17498 return include_name;
17501 /* State machine to track the state of the line number program. */
17503 class lnp_state_machine
17505 public:
17506 /* Initialize a machine state for the start of a line number
17507 program. */
17508 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh);
17510 file_entry *current_file ()
17512 /* lh->file_names is 0-based, but the file name numbers in the
17513 statement program are 1-based. */
17514 return m_line_header->file_name_at (m_file);
17517 /* Record the line in the state machine. END_SEQUENCE is true if
17518 we're processing the end of a sequence. */
17519 void record_line (bool end_sequence);
17521 /* Check ADDRESS is -1, -2, or zero and less than UNRELOCATED_LOWPC, and if
17522 true nop-out rest of the lines in this sequence. */
17523 void check_line_address (struct dwarf2_cu *cu,
17524 const gdb_byte *line_ptr,
17525 unrelocated_addr unrelocated_lowpc,
17526 unrelocated_addr address);
17528 void handle_set_discriminator (unsigned int discriminator)
17530 m_discriminator = discriminator;
17531 m_line_has_non_zero_discriminator |= discriminator != 0;
17534 /* Handle DW_LNE_set_address. */
17535 void handle_set_address (unrelocated_addr address)
17537 m_op_index = 0;
17538 m_address
17539 = (unrelocated_addr) gdbarch_adjust_dwarf2_line (m_gdbarch,
17540 (CORE_ADDR) address,
17541 false);
17544 /* Handle DW_LNS_advance_pc. */
17545 void handle_advance_pc (CORE_ADDR adjust);
17547 /* Handle a special opcode. */
17548 void handle_special_opcode (unsigned char op_code);
17550 /* Handle DW_LNS_advance_line. */
17551 void handle_advance_line (int line_delta)
17553 advance_line (line_delta);
17556 /* Handle DW_LNS_set_file. */
17557 void handle_set_file (file_name_index file);
17559 /* Handle DW_LNS_negate_stmt. */
17560 void handle_negate_stmt ()
17562 m_flags ^= LEF_IS_STMT;
17565 /* Handle DW_LNS_const_add_pc. */
17566 void handle_const_add_pc ();
17568 /* Handle DW_LNS_fixed_advance_pc. */
17569 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
17571 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
17572 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
17573 m_op_index = 0;
17576 /* Handle DW_LNS_copy. */
17577 void handle_copy ()
17579 record_line (false);
17580 m_discriminator = 0;
17581 m_flags &= ~LEF_PROLOGUE_END;
17582 m_flags &= ~LEF_EPILOGUE_BEGIN;
17585 /* Handle DW_LNE_end_sequence. */
17586 void handle_end_sequence ()
17588 m_currently_recording_lines = true;
17591 /* Handle DW_LNS_set_prologue_end. */
17592 void handle_set_prologue_end ()
17594 m_flags |= LEF_PROLOGUE_END;
17597 void handle_set_epilogue_begin ()
17599 m_flags |= LEF_EPILOGUE_BEGIN;
17602 private:
17603 /* Advance the line by LINE_DELTA. */
17604 void advance_line (int line_delta)
17606 m_line += line_delta;
17608 if (line_delta != 0)
17609 m_line_has_non_zero_discriminator = m_discriminator != 0;
17612 struct dwarf2_cu *m_cu;
17614 gdbarch *m_gdbarch;
17616 /* The line number header. */
17617 line_header *m_line_header;
17619 /* These are part of the standard DWARF line number state machine,
17620 and initialized according to the DWARF spec. */
17622 unsigned char m_op_index = 0;
17623 /* The line table index of the current file. */
17624 file_name_index m_file = 1;
17625 unsigned int m_line = 1;
17627 /* These are initialized in the constructor. */
17629 unrelocated_addr m_address;
17630 linetable_entry_flags m_flags;
17631 unsigned int m_discriminator = 0;
17633 /* Additional bits of state we need to track. */
17635 /* The last file that we called dwarf2_start_subfile for.
17636 This is only used for TLLs. */
17637 unsigned int m_last_file = 0;
17638 /* The last file a line number was recorded for. */
17639 struct subfile *m_last_subfile = NULL;
17641 /* The address of the last line entry. */
17642 unrelocated_addr m_last_address;
17644 /* Set to true when a previous line at the same address (using
17645 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
17646 when a line entry at a new address (m_address different to
17647 m_last_address) is processed. */
17648 bool m_stmt_at_address = false;
17650 /* When true, record the lines we decode. */
17651 bool m_currently_recording_lines = true;
17653 /* The last line number that was recorded, used to coalesce
17654 consecutive entries for the same line. This can happen, for
17655 example, when discriminators are present. PR 17276. */
17656 unsigned int m_last_line = 0;
17657 bool m_line_has_non_zero_discriminator = false;
17660 void
17661 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
17663 CORE_ADDR addr_adj = (((m_op_index + adjust)
17664 / m_line_header->maximum_ops_per_instruction)
17665 * m_line_header->minimum_instruction_length);
17666 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
17667 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
17668 m_op_index = ((m_op_index + adjust)
17669 % m_line_header->maximum_ops_per_instruction);
17672 void
17673 lnp_state_machine::handle_special_opcode (unsigned char op_code)
17675 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
17676 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
17677 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
17678 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
17679 / m_line_header->maximum_ops_per_instruction)
17680 * m_line_header->minimum_instruction_length);
17681 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
17682 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
17683 m_op_index = ((m_op_index + adj_opcode_d)
17684 % m_line_header->maximum_ops_per_instruction);
17686 int line_delta = m_line_header->line_base + adj_opcode_r;
17687 advance_line (line_delta);
17688 record_line (false);
17689 m_discriminator = 0;
17690 m_flags &= ~LEF_PROLOGUE_END;
17691 m_flags &= ~LEF_EPILOGUE_BEGIN;
17694 void
17695 lnp_state_machine::handle_set_file (file_name_index file)
17697 m_file = file;
17699 const file_entry *fe = current_file ();
17700 if (fe == NULL)
17701 dwarf2_debug_line_missing_file_complaint ();
17702 else
17704 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
17705 m_line_has_non_zero_discriminator = m_discriminator != 0;
17706 dwarf2_start_subfile (m_cu, *fe, *m_line_header);
17710 void
17711 lnp_state_machine::handle_const_add_pc ()
17713 CORE_ADDR adjust
17714 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
17716 CORE_ADDR addr_adj
17717 = (((m_op_index + adjust)
17718 / m_line_header->maximum_ops_per_instruction)
17719 * m_line_header->minimum_instruction_length);
17721 addr_adj = gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
17722 m_address = (unrelocated_addr) ((CORE_ADDR) m_address + addr_adj);
17723 m_op_index = ((m_op_index + adjust)
17724 % m_line_header->maximum_ops_per_instruction);
17727 /* Return non-zero if we should add LINE to the line number table.
17728 LINE is the line to add, LAST_LINE is the last line that was added,
17729 LAST_SUBFILE is the subfile for LAST_LINE.
17730 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
17731 had a non-zero discriminator.
17733 We have to be careful in the presence of discriminators.
17734 E.g., for this line:
17736 for (i = 0; i < 100000; i++);
17738 clang can emit four line number entries for that one line,
17739 each with a different discriminator.
17740 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
17742 However, we want gdb to coalesce all four entries into one.
17743 Otherwise the user could stepi into the middle of the line and
17744 gdb would get confused about whether the pc really was in the
17745 middle of the line.
17747 Things are further complicated by the fact that two consecutive
17748 line number entries for the same line is a heuristic used by gcc
17749 to denote the end of the prologue. So we can't just discard duplicate
17750 entries, we have to be selective about it. The heuristic we use is
17751 that we only collapse consecutive entries for the same line if at least
17752 one of those entries has a non-zero discriminator. PR 17276.
17754 Note: Addresses in the line number state machine can never go backwards
17755 within one sequence, thus this coalescing is ok. */
17757 static int
17758 dwarf_record_line_p (struct dwarf2_cu *cu,
17759 unsigned int line, unsigned int last_line,
17760 int line_has_non_zero_discriminator,
17761 struct subfile *last_subfile)
17763 if (cu->get_builder ()->get_current_subfile () != last_subfile)
17764 return 1;
17765 if (line != last_line)
17766 return 1;
17767 /* Same line for the same file that we've seen already.
17768 As a last check, for pr 17276, only record the line if the line
17769 has never had a non-zero discriminator. */
17770 if (!line_has_non_zero_discriminator)
17771 return 1;
17772 return 0;
17775 /* Use the CU's builder to record line number LINE beginning at
17776 address ADDRESS in the line table of subfile SUBFILE. */
17778 static void
17779 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
17780 unsigned int line, unrelocated_addr address,
17781 linetable_entry_flags flags,
17782 struct dwarf2_cu *cu)
17784 unrelocated_addr addr
17785 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch,
17786 (CORE_ADDR) address));
17788 if (dwarf_line_debug)
17790 gdb_printf (gdb_stdlog,
17791 "Recording line %u, file %s, address %s\n",
17792 line, lbasename (subfile->name.c_str ()),
17793 paddress (gdbarch, (CORE_ADDR) address));
17796 if (cu != nullptr)
17797 cu->get_builder ()->record_line (subfile, line, addr, flags);
17800 /* Subroutine of dwarf_decode_lines_1 to simplify it.
17801 Mark the end of a set of line number records.
17802 The arguments are the same as for dwarf_record_line_1.
17803 If SUBFILE is NULL the request is ignored. */
17805 static void
17806 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
17807 unrelocated_addr address, struct dwarf2_cu *cu)
17809 if (subfile == NULL)
17810 return;
17812 if (dwarf_line_debug)
17814 gdb_printf (gdb_stdlog,
17815 "Finishing current line, file %s, address %s\n",
17816 lbasename (subfile->name.c_str ()),
17817 paddress (gdbarch, (CORE_ADDR) address));
17820 dwarf_record_line_1 (gdbarch, subfile, 0, address, LEF_IS_STMT, cu);
17823 void
17824 lnp_state_machine::record_line (bool end_sequence)
17826 if (dwarf_line_debug)
17828 gdb_printf (gdb_stdlog,
17829 "Processing actual line %u: file %u,"
17830 " address %s, is_stmt %u, prologue_end %u,"
17831 " epilogue_begin %u, discrim %u%s\n",
17832 m_line, m_file,
17833 paddress (m_gdbarch, (CORE_ADDR) m_address),
17834 (m_flags & LEF_IS_STMT) != 0,
17835 (m_flags & LEF_PROLOGUE_END) != 0,
17836 (m_flags & LEF_EPILOGUE_BEGIN) != 0,
17837 m_discriminator,
17838 (end_sequence ? "\t(end sequence)" : ""));
17841 file_entry *fe = current_file ();
17843 if (fe == NULL)
17844 dwarf2_debug_line_missing_file_complaint ();
17845 /* For now we ignore lines not starting on an instruction boundary.
17846 But not when processing end_sequence for compatibility with the
17847 previous version of the code. */
17848 else if (m_op_index == 0 || end_sequence)
17850 /* When we switch files we insert an end maker in the first file,
17851 switch to the second file and add a new line entry. The
17852 problem is that the end marker inserted in the first file will
17853 discard any previous line entries at the same address. If the
17854 line entries in the first file are marked as is-stmt, while
17855 the new line in the second file is non-stmt, then this means
17856 the end marker will discard is-stmt lines so we can have a
17857 non-stmt line. This means that there are less addresses at
17858 which the user can insert a breakpoint.
17860 To improve this we track the last address in m_last_address,
17861 and whether we have seen an is-stmt at this address. Then
17862 when switching files, if we have seen a stmt at the current
17863 address, and we are switching to create a non-stmt line, then
17864 discard the new line. */
17865 bool file_changed
17866 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
17867 bool ignore_this_line
17868 = ((file_changed && !end_sequence && m_last_address == m_address
17869 && ((m_flags & LEF_IS_STMT) == 0)
17870 && m_stmt_at_address)
17871 || (!end_sequence && m_line == 0));
17873 if ((file_changed && !ignore_this_line) || end_sequence)
17875 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
17876 m_currently_recording_lines ? m_cu : nullptr);
17879 if (!end_sequence && !ignore_this_line)
17881 linetable_entry_flags lte_flags = m_flags;
17882 if (producer_is_codewarrior (m_cu))
17883 lte_flags |= LEF_IS_STMT;
17885 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
17886 m_line_has_non_zero_discriminator,
17887 m_last_subfile))
17889 buildsym_compunit *builder = m_cu->get_builder ();
17890 dwarf_record_line_1 (m_gdbarch,
17891 builder->get_current_subfile (),
17892 m_line, m_address, lte_flags,
17893 m_currently_recording_lines ? m_cu : nullptr);
17895 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
17896 m_last_line = m_line;
17900 /* Track whether we have seen any IS_STMT true at m_address in case we
17901 have multiple line table entries all at m_address. */
17902 if (m_last_address != m_address)
17904 m_stmt_at_address = false;
17905 m_last_address = m_address;
17907 m_stmt_at_address |= (m_flags & LEF_IS_STMT) != 0;
17910 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
17911 line_header *lh)
17912 : m_cu (cu),
17913 m_gdbarch (arch),
17914 m_line_header (lh),
17915 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
17916 if there was a line entry for it so that the backend has a
17917 chance to adjust it and also record it in case it needs it.
17918 This is currently used by MIPS code,
17919 cf. `mips_adjust_dwarf2_line'. */
17920 m_address ((unrelocated_addr) gdbarch_adjust_dwarf2_line (arch, 0, 0)),
17921 m_flags (lh->default_is_stmt ? LEF_IS_STMT : (linetable_entry_flags) 0),
17922 m_last_address (m_address)
17926 void
17927 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
17928 const gdb_byte *line_ptr,
17929 unrelocated_addr unrelocated_lowpc,
17930 unrelocated_addr address)
17932 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0,
17933 -1 or -2 (-2 is used by certain lld versions, see
17934 https://github.com/llvm/llvm-project/commit/e618ccbf431f6730edb6d1467a127c3a52fd57f7).
17935 If ADDRESS is 0, ignoring the opcode will err if the text section is
17936 located at 0x0. In this case, additionally check that if
17937 ADDRESS < UNRELOCATED_LOWPC. */
17939 if ((address == (unrelocated_addr) 0 && address < unrelocated_lowpc)
17940 || address == (unrelocated_addr) -1
17941 || address == (unrelocated_addr) -2)
17943 /* This line table is for a function which has been
17944 GCd by the linker. Ignore it. PR gdb/12528 */
17946 struct objfile *objfile = cu->per_objfile->objfile;
17947 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
17949 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
17950 line_offset, objfile_name (objfile));
17951 m_currently_recording_lines = false;
17952 /* Note: m_currently_recording_lines is left as false until we see
17953 DW_LNE_end_sequence. */
17957 /* Subroutine of dwarf_decode_lines to simplify it.
17958 Process the line number information in LH. */
17960 static void
17961 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
17962 unrelocated_addr lowpc)
17964 const gdb_byte *line_ptr, *extended_end;
17965 const gdb_byte *line_end;
17966 unsigned int bytes_read, extended_len;
17967 unsigned char op_code, extended_op;
17968 struct objfile *objfile = cu->per_objfile->objfile;
17969 bfd *abfd = objfile->obfd.get ();
17970 struct gdbarch *gdbarch = objfile->arch ();
17972 line_ptr = lh->statement_program_start;
17973 line_end = lh->statement_program_end;
17975 /* Read the statement sequences until there's nothing left. */
17976 while (line_ptr < line_end)
17978 /* The DWARF line number program state machine. Reset the state
17979 machine at the start of each sequence. */
17980 lnp_state_machine state_machine (cu, gdbarch, lh);
17981 bool end_sequence = false;
17983 /* Start a subfile for the current file of the state
17984 machine. */
17985 const file_entry *fe = state_machine.current_file ();
17987 if (fe != NULL)
17988 dwarf2_start_subfile (cu, *fe, *lh);
17990 /* Decode the table. */
17991 while (line_ptr < line_end && !end_sequence)
17993 op_code = read_1_byte (abfd, line_ptr);
17994 line_ptr += 1;
17996 if (op_code >= lh->opcode_base)
17998 /* Special opcode. */
17999 state_machine.handle_special_opcode (op_code);
18001 else switch (op_code)
18003 case DW_LNS_extended_op:
18004 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18005 &bytes_read);
18006 line_ptr += bytes_read;
18007 extended_end = line_ptr + extended_len;
18008 extended_op = read_1_byte (abfd, line_ptr);
18009 line_ptr += 1;
18010 if (DW_LNE_lo_user <= extended_op
18011 && extended_op <= DW_LNE_hi_user)
18013 /* Vendor extension, ignore. */
18014 line_ptr = extended_end;
18015 break;
18017 switch (extended_op)
18019 case DW_LNE_end_sequence:
18020 state_machine.handle_end_sequence ();
18021 end_sequence = true;
18022 break;
18023 case DW_LNE_set_address:
18025 unrelocated_addr address
18026 = cu->header.read_address (abfd, line_ptr, &bytes_read);
18027 line_ptr += bytes_read;
18029 state_machine.check_line_address (cu, line_ptr, lowpc,
18030 address);
18031 state_machine.handle_set_address (address);
18033 break;
18034 case DW_LNE_define_file:
18036 const char *cur_file;
18037 unsigned int mod_time, length;
18038 dir_index dindex;
18040 cur_file = read_direct_string (abfd, line_ptr,
18041 &bytes_read);
18042 line_ptr += bytes_read;
18043 dindex = (dir_index)
18044 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18045 line_ptr += bytes_read;
18046 mod_time =
18047 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18048 line_ptr += bytes_read;
18049 length =
18050 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18051 line_ptr += bytes_read;
18052 lh->add_file_name (cur_file, dindex, mod_time, length);
18054 break;
18055 case DW_LNE_set_discriminator:
18057 /* The discriminator is not interesting to the
18058 debugger; just ignore it. We still need to
18059 check its value though:
18060 if there are consecutive entries for the same
18061 (non-prologue) line we want to coalesce them.
18062 PR 17276. */
18063 unsigned int discr
18064 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18065 line_ptr += bytes_read;
18067 state_machine.handle_set_discriminator (discr);
18069 break;
18070 default:
18071 complaint (_("mangled .debug_line section"));
18072 return;
18074 /* Make sure that we parsed the extended op correctly. If e.g.
18075 we expected a different address size than the producer used,
18076 we may have read the wrong number of bytes. */
18077 if (line_ptr != extended_end)
18079 complaint (_("mangled .debug_line section"));
18080 return;
18082 break;
18083 case DW_LNS_copy:
18084 state_machine.handle_copy ();
18085 break;
18086 case DW_LNS_advance_pc:
18088 CORE_ADDR adjust
18089 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18090 line_ptr += bytes_read;
18092 state_machine.handle_advance_pc (adjust);
18094 break;
18095 case DW_LNS_advance_line:
18097 int line_delta
18098 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18099 line_ptr += bytes_read;
18101 state_machine.handle_advance_line (line_delta);
18103 break;
18104 case DW_LNS_set_file:
18106 file_name_index file
18107 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18108 &bytes_read);
18109 line_ptr += bytes_read;
18111 state_machine.handle_set_file (file);
18113 break;
18114 case DW_LNS_set_column:
18115 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18116 line_ptr += bytes_read;
18117 break;
18118 case DW_LNS_negate_stmt:
18119 state_machine.handle_negate_stmt ();
18120 break;
18121 case DW_LNS_set_basic_block:
18122 break;
18123 /* Add to the address register of the state machine the
18124 address increment value corresponding to special opcode
18125 255. I.e., this value is scaled by the minimum
18126 instruction length since special opcode 255 would have
18127 scaled the increment. */
18128 case DW_LNS_const_add_pc:
18129 state_machine.handle_const_add_pc ();
18130 break;
18131 case DW_LNS_fixed_advance_pc:
18133 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
18134 line_ptr += 2;
18136 state_machine.handle_fixed_advance_pc (addr_adj);
18138 break;
18139 case DW_LNS_set_prologue_end:
18140 state_machine.handle_set_prologue_end ();
18141 break;
18142 case DW_LNS_set_epilogue_begin:
18143 state_machine.handle_set_epilogue_begin ();
18144 break;
18145 default:
18147 /* Unknown standard opcode, ignore it. */
18148 int i;
18150 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18152 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18153 line_ptr += bytes_read;
18159 if (!end_sequence)
18160 dwarf2_debug_line_missing_end_sequence_complaint ();
18162 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18163 in which case we still finish recording the last line). */
18164 state_machine.record_line (true);
18168 /* Decode the Line Number Program (LNP) for the given line_header
18169 structure and CU. The actual information extracted and the type
18170 of structures created from the LNP depends on the value of PST.
18172 FND holds the CU file name and directory, if known.
18173 It is used for relative paths in the line table.
18175 NOTE: It is important that psymtabs have the same file name (via
18176 strcmp) as the corresponding symtab. Since the directory is not
18177 used in the name of the symtab we don't use it in the name of the
18178 psymtabs we create. E.g. expand_line_sal requires this when
18179 finding psymtabs to expand. A good testcase for this is
18180 mb-inline.exp.
18182 LOWPC is the lowest address in CU (or 0 if not known).
18184 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18185 for its PC<->lines mapping information. Otherwise only the filename
18186 table is read in. */
18188 static void
18189 dwarf_decode_lines (struct line_header *lh, struct dwarf2_cu *cu,
18190 unrelocated_addr lowpc, int decode_mapping)
18192 if (decode_mapping)
18193 dwarf_decode_lines_1 (lh, cu, lowpc);
18195 /* Make sure a symtab is created for every file, even files
18196 which contain only variables (i.e. no code with associated
18197 line numbers). */
18198 buildsym_compunit *builder = cu->get_builder ();
18199 struct compunit_symtab *cust = builder->get_compunit_symtab ();
18201 for (auto &fe : lh->file_names ())
18203 dwarf2_start_subfile (cu, fe, *lh);
18204 subfile *sf = builder->get_current_subfile ();
18206 if (sf->symtab == nullptr)
18207 sf->symtab = allocate_symtab (cust, sf->name.c_str (),
18208 sf->name_for_id.c_str ());
18210 fe.symtab = sf->symtab;
18214 /* Start a subfile for DWARF. FILENAME is the name of the file and
18215 DIRNAME the name of the source directory which contains FILENAME
18216 or NULL if not known.
18217 This routine tries to keep line numbers from identical absolute and
18218 relative file names in a common subfile.
18220 Using the `list' example from the GDB testsuite, which resides in
18221 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18222 of /srcdir/list0.c yields the following debugging information for list0.c:
18224 DW_AT_name: /srcdir/list0.c
18225 DW_AT_comp_dir: /compdir
18226 files.files[0].name: list0.h
18227 files.files[0].dir: /srcdir
18228 files.files[1].name: list0.c
18229 files.files[1].dir: /srcdir
18231 The line number information for list0.c has to end up in a single
18232 subfile, so that `break /srcdir/list0.c:1' works as expected.
18233 start_subfile will ensure that this happens provided that we pass the
18234 concatenation of files.files[1].dir and files.files[1].name as the
18235 subfile's name. */
18237 static void
18238 dwarf2_start_subfile (dwarf2_cu *cu, const file_entry &fe,
18239 const line_header &lh)
18241 std::string filename_holder;
18242 const char *filename = fe.name;
18243 const char *dirname = lh.include_dir_at (fe.d_index);
18245 /* In order not to lose the line information directory,
18246 we concatenate it to the filename when it makes sense.
18247 Note that the Dwarf3 standard says (speaking of filenames in line
18248 information): ``The directory index is ignored for file names
18249 that represent full path names''. Thus ignoring dirname in the
18250 `else' branch below isn't an issue. */
18252 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18254 filename_holder = path_join (dirname, filename);
18255 filename = filename_holder.c_str ();
18258 std::string filename_for_id = lh.file_file_name (fe);
18259 cu->get_builder ()->start_subfile (filename, filename_for_id.c_str ());
18262 static void
18263 var_decode_location (struct attribute *attr, struct symbol *sym,
18264 struct dwarf2_cu *cu)
18266 struct objfile *objfile = cu->per_objfile->objfile;
18267 struct comp_unit_head *cu_header = &cu->header;
18269 /* NOTE drow/2003-01-30: There used to be a comment and some special
18270 code here to turn a symbol with DW_AT_external and a
18271 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18272 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18273 with some versions of binutils) where shared libraries could have
18274 relocations against symbols in their debug information - the
18275 minimal symbol would have the right address, but the debug info
18276 would not. It's no longer necessary, because we will explicitly
18277 apply relocations when we read in the debug information now. */
18279 /* A DW_AT_location attribute with no contents indicates that a
18280 variable has been optimized away. */
18281 if (attr->form_is_block () && attr->as_block ()->size == 0)
18283 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18284 return;
18287 /* Handle one degenerate form of location expression specially, to
18288 preserve GDB's previous behavior when section offsets are
18289 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18290 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18292 if (attr->form_is_block ())
18294 struct dwarf_block *block = attr->as_block ();
18296 if ((block->data[0] == DW_OP_addr
18297 && block->size == 1 + cu_header->addr_size)
18298 || ((block->data[0] == DW_OP_GNU_addr_index
18299 || block->data[0] == DW_OP_addrx)
18300 && (block->size
18301 == 1 + leb128_size (&block->data[1]))))
18303 unsigned int dummy;
18305 unrelocated_addr tem;
18306 if (block->data[0] == DW_OP_addr)
18307 tem = cu->header.read_address (objfile->obfd.get (),
18308 block->data + 1,
18309 &dummy);
18310 else
18311 tem = read_addr_index_from_leb128 (cu, block->data + 1, &dummy);
18312 sym->set_value_address ((CORE_ADDR) tem);
18313 sym->set_aclass_index (LOC_STATIC);
18314 fixup_symbol_section (sym, objfile);
18315 sym->set_value_address
18316 (sym->value_address ()
18317 + objfile->section_offsets[sym->section_index ()]);
18318 return;
18322 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18323 expression evaluator, and use LOC_COMPUTED only when necessary
18324 (i.e. when the value of a register or memory location is
18325 referenced, or a thread-local block, etc.). Then again, it might
18326 not be worthwhile. I'm assuming that it isn't unless performance
18327 or memory numbers show me otherwise. */
18329 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18331 if (sym->computed_ops ()->location_has_loclist)
18332 cu->has_loclist = true;
18335 /* A helper function to add an "export" symbol. The new symbol starts
18336 as a clone of ORIG, but is modified to defer to the symbol named
18337 ORIG_NAME. The original symbol uses the name given in the source
18338 code, and the symbol that is created here uses the linkage name as
18339 its name. See ada-imported.c. */
18341 static void
18342 add_ada_export_symbol (struct symbol *orig, const char *new_name,
18343 const char *orig_name, struct dwarf2_cu *cu,
18344 struct pending **list_to_add)
18346 struct symbol *copy
18347 = new (&cu->per_objfile->objfile->objfile_obstack) symbol (*orig);
18348 copy->set_linkage_name (new_name);
18349 SYMBOL_LOCATION_BATON (copy) = (void *) orig_name;
18350 copy->set_aclass_index (copy->aclass () == LOC_BLOCK
18351 ? ada_block_index
18352 : ada_imported_index);
18353 add_symbol_to_list (copy, list_to_add);
18356 /* A helper function that decides if a given symbol is an Ada Pragma
18357 Import or Pragma Export. */
18359 static bool
18360 is_ada_import_or_export (dwarf2_cu *cu, const char *name,
18361 const char *linkagename)
18363 return (cu->lang () == language_ada
18364 && linkagename != nullptr
18365 && !streq (name, linkagename)
18366 /* The following exclusions are necessary because symbols
18367 with names or linkage names that match here will meet the
18368 other criteria but are not in fact caused by Pragma
18369 Import or Pragma Export, and applying the import/export
18370 treatment to them will introduce problems. Some of these
18371 checks only apply to functions, but it is simpler and
18372 harmless to always do them all. */
18373 && !startswith (name, "__builtin")
18374 && !startswith (linkagename, "___ghost_")
18375 && !startswith (linkagename, "__gnat")
18376 && !startswith (linkagename, "_ada_")
18377 && !streq (linkagename, "adainit"));
18380 /* Given a pointer to a DWARF information entry, figure out if we need
18381 to make a symbol table entry for it, and if so, create a new entry
18382 and return a pointer to it.
18383 If TYPE is NULL, determine symbol type from the die, otherwise
18384 used the passed type.
18385 If SPACE is not NULL, use it to hold the new symbol. If it is
18386 NULL, allocate a new symbol on the objfile's obstack. */
18388 static struct symbol *
18389 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18390 struct symbol *space)
18392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18393 struct objfile *objfile = per_objfile->objfile;
18394 struct symbol *sym = NULL;
18395 const char *name;
18396 struct attribute *attr = NULL;
18397 struct attribute *attr2 = NULL;
18398 struct pending **list_to_add = NULL;
18400 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18402 name = dwarf2_name (die, cu);
18403 if (name == nullptr && (die->tag == DW_TAG_subprogram
18404 || die->tag == DW_TAG_inlined_subroutine
18405 || die->tag == DW_TAG_entry_point))
18406 name = dw2_linkage_name (die, cu);
18408 if (name)
18410 int suppress_add = 0;
18412 if (space)
18413 sym = space;
18414 else
18415 sym = new (&objfile->objfile_obstack) symbol;
18416 OBJSTAT (objfile, n_syms++);
18418 /* Cache this symbol's name and the name's demangled form (if any). */
18419 sym->set_language (cu->lang (), &objfile->objfile_obstack);
18420 /* Fortran does not have mangling standard and the mangling does differ
18421 between gfortran, iFort etc. */
18422 const char *physname
18423 = (cu->lang () == language_fortran
18424 ? dwarf2_full_name (name, die, cu)
18425 : dwarf2_physname (name, die, cu));
18426 const char *linkagename = dw2_linkage_name (die, cu);
18428 if (linkagename == nullptr || cu->lang () == language_ada)
18429 sym->set_linkage_name (physname);
18430 else
18432 if (physname == linkagename)
18433 sym->set_demangled_name (name, &objfile->objfile_obstack);
18434 else
18435 sym->set_demangled_name (physname, &objfile->objfile_obstack);
18437 sym->set_linkage_name (linkagename);
18440 /* Handle DW_AT_artificial. */
18441 attr = dwarf2_attr (die, DW_AT_artificial, cu);
18442 if (attr != nullptr)
18443 sym->set_is_artificial (attr->as_boolean ());
18445 /* Default assumptions.
18446 Use the passed type or decode it from the die. */
18447 sym->set_domain (UNDEF_DOMAIN);
18448 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18449 if (type != NULL)
18450 sym->set_type (type);
18451 else
18452 sym->set_type (die_type (die, cu));
18453 attr = dwarf2_attr (die,
18454 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18455 cu);
18456 if (attr != nullptr)
18457 sym->set_line (attr->constant_value (0));
18459 struct dwarf2_cu *file_cu = cu;
18460 attr = dwarf2_attr (die,
18461 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18462 &file_cu);
18463 if (attr != nullptr && attr->is_nonnegative ())
18465 file_name_index file_index
18466 = (file_name_index) attr->as_nonnegative ();
18467 struct file_entry *fe;
18469 if (file_cu->line_header == nullptr)
18471 file_and_directory fnd (nullptr, nullptr);
18472 handle_DW_AT_stmt_list (file_cu->dies, file_cu, fnd, {}, false);
18475 if (file_cu->line_header != nullptr)
18476 fe = file_cu->line_header->file_name_at (file_index);
18477 else
18478 fe = NULL;
18480 if (fe == NULL)
18481 complaint (_("file index out of range"));
18482 else
18483 sym->set_symtab (fe->symtab);
18486 switch (die->tag)
18488 case DW_TAG_label:
18489 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18490 if (attr != nullptr)
18492 CORE_ADDR addr = per_objfile->relocate (attr->as_address ());
18493 sym->set_section_index (SECT_OFF_TEXT (objfile));
18494 sym->set_value_address (addr);
18495 sym->set_aclass_index (LOC_LABEL);
18497 else
18498 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
18499 sym->set_type (builtin_type (objfile)->builtin_core_addr);
18500 sym->set_domain (LABEL_DOMAIN);
18501 list_to_add = cu->list_in_scope;
18502 break;
18503 case DW_TAG_entry_point:
18504 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18505 finish_block. */
18506 sym->set_domain (FUNCTION_DOMAIN);
18507 sym->set_aclass_index (LOC_BLOCK);
18508 /* DW_TAG_entry_point provides an additional entry_point to an
18509 existing sub_program. Therefore, we inherit the "external"
18510 attribute from the sub_program to which the entry_point
18511 belongs to. */
18512 attr2 = dwarf2_attr (die->parent, DW_AT_external, cu);
18513 if (attr2 != nullptr && attr2->as_boolean ())
18514 list_to_add = cu->get_builder ()->get_global_symbols ();
18515 else
18516 list_to_add = cu->list_in_scope;
18517 break;
18518 case DW_TAG_subprogram:
18519 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18520 finish_block. */
18521 sym->set_domain (FUNCTION_DOMAIN);
18522 sym->set_aclass_index (LOC_BLOCK);
18523 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18524 if ((attr2 != nullptr && attr2->as_boolean ())
18525 || cu->lang () == language_ada
18526 || cu->lang () == language_fortran)
18528 /* Subprograms marked external are stored as a global symbol.
18529 Ada and Fortran subprograms, whether marked external or
18530 not, are always stored as a global symbol, because we want
18531 to be able to access them globally. For instance, we want
18532 to be able to break on a nested subprogram without having
18533 to specify the context. */
18534 list_to_add = cu->get_builder ()->get_global_symbols ();
18536 else
18538 list_to_add = cu->list_in_scope;
18541 if (is_ada_import_or_export (cu, name, linkagename))
18543 /* This is either a Pragma Import or Export. They can
18544 be distinguished by the declaration flag. */
18545 sym->set_linkage_name (name);
18546 if (die_is_declaration (die, cu))
18548 /* For Import, create a symbol using the source
18549 name, and have it refer to the linkage name. */
18550 SYMBOL_LOCATION_BATON (sym) = (void *) linkagename;
18551 sym->set_aclass_index (ada_block_index);
18553 else
18555 /* For Export, create a symbol using the source
18556 name, then create a second symbol that refers
18557 back to it. */
18558 add_ada_export_symbol (sym, linkagename, name, cu,
18559 list_to_add);
18562 break;
18563 case DW_TAG_inlined_subroutine:
18564 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18565 finish_block. */
18566 sym->set_domain (FUNCTION_DOMAIN);
18567 sym->set_aclass_index (LOC_BLOCK);
18568 sym->set_is_inlined (1);
18569 list_to_add = cu->list_in_scope;
18570 break;
18571 case DW_TAG_template_value_param:
18572 suppress_add = 1;
18573 [[fallthrough]];
18574 case DW_TAG_constant:
18575 case DW_TAG_variable:
18576 case DW_TAG_member:
18577 sym->set_domain (VAR_DOMAIN);
18578 /* Compilation with minimal debug info may result in
18579 variables with missing type entries. Change the
18580 misleading `void' type to something sensible. */
18581 if (sym->type ()->code () == TYPE_CODE_VOID)
18582 sym->set_type (builtin_type (objfile)->builtin_int);
18584 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18585 /* In the case of DW_TAG_member, we should only be called for
18586 static const members. */
18587 if (die->tag == DW_TAG_member)
18589 /* dwarf2_add_field uses die_is_declaration,
18590 so we do the same. */
18591 gdb_assert (die_is_declaration (die, cu));
18592 gdb_assert (attr);
18594 if (attr != nullptr)
18596 dwarf2_const_value (attr, sym, cu);
18597 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18598 if (!suppress_add)
18600 if (attr2 != nullptr && attr2->as_boolean ())
18601 list_to_add = cu->get_builder ()->get_global_symbols ();
18602 else
18603 list_to_add = cu->list_in_scope;
18605 break;
18607 attr = dwarf2_attr (die, DW_AT_location, cu);
18608 if (attr != nullptr)
18610 var_decode_location (attr, sym, cu);
18611 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18613 /* Fortran explicitly imports any global symbols to the local
18614 scope by DW_TAG_common_block. */
18615 if (cu->lang () == language_fortran && die->parent
18616 && die->parent->tag == DW_TAG_common_block)
18617 attr2 = NULL;
18619 if (sym->aclass () == LOC_STATIC
18620 && sym->value_address () == 0
18621 && !per_objfile->per_bfd->has_section_at_zero)
18623 /* When a static variable is eliminated by the linker,
18624 the corresponding debug information is not stripped
18625 out, but the variable address is set to null;
18626 do not add such variables into symbol table. */
18628 else if (attr2 != nullptr && attr2->as_boolean ())
18630 if (sym->aclass () == LOC_STATIC
18631 && (objfile->flags & OBJF_MAINLINE) == 0
18632 && per_objfile->per_bfd->can_copy)
18634 /* A global static variable might be subject to
18635 copy relocation. We first check for a local
18636 minsym, though, because maybe the symbol was
18637 marked hidden, in which case this would not
18638 apply. */
18639 bound_minimal_symbol found
18640 = (lookup_minimal_symbol_linkage
18641 (sym->linkage_name (), objfile, false));
18642 if (found.minsym != nullptr)
18643 sym->maybe_copied = 1;
18646 /* A variable with DW_AT_external is never static,
18647 but it may be block-scoped. */
18648 list_to_add
18649 = ((cu->list_in_scope
18650 == cu->get_builder ()->get_file_symbols ())
18651 ? cu->get_builder ()->get_global_symbols ()
18652 : cu->list_in_scope);
18654 else
18655 list_to_add = cu->list_in_scope;
18657 if (list_to_add != nullptr
18658 && is_ada_import_or_export (cu, name, linkagename))
18660 /* This is a Pragma Export. A Pragma Import won't
18661 be seen here, because it will not have a location
18662 and so will be handled below. */
18663 add_ada_export_symbol (sym, name, linkagename, cu,
18664 list_to_add);
18667 else
18669 /* We do not know the address of this symbol.
18670 If it is an external symbol and we have type information
18671 for it, enter the symbol as a LOC_UNRESOLVED symbol.
18672 The address of the variable will then be determined from
18673 the minimal symbol table whenever the variable is
18674 referenced. */
18675 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18677 /* Fortran explicitly imports any global symbols to the local
18678 scope by DW_TAG_common_block. */
18679 if (cu->lang () == language_fortran && die->parent
18680 && die->parent->tag == DW_TAG_common_block)
18682 /* SYMBOL_CLASS doesn't matter here because
18683 read_common_block is going to reset it. */
18684 if (!suppress_add)
18685 list_to_add = cu->list_in_scope;
18687 else if (is_ada_import_or_export (cu, name, linkagename))
18689 /* This is a Pragma Import. A Pragma Export won't
18690 be seen here, because it will have a location and
18691 so will be handled above. */
18692 sym->set_linkage_name (name);
18693 list_to_add
18694 = ((cu->list_in_scope
18695 == cu->get_builder ()->get_file_symbols ())
18696 ? cu->get_builder ()->get_global_symbols ()
18697 : cu->list_in_scope);
18698 SYMBOL_LOCATION_BATON (sym) = (void *) linkagename;
18699 sym->set_aclass_index (ada_imported_index);
18701 else if (attr2 != nullptr && attr2->as_boolean ()
18702 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
18704 /* A variable with DW_AT_external is never static, but it
18705 may be block-scoped. */
18706 list_to_add
18707 = ((cu->list_in_scope
18708 == cu->get_builder ()->get_file_symbols ())
18709 ? cu->get_builder ()->get_global_symbols ()
18710 : cu->list_in_scope);
18712 sym->set_aclass_index (LOC_UNRESOLVED);
18714 else if (!die_is_declaration (die, cu))
18716 /* Use the default LOC_OPTIMIZED_OUT class. */
18717 gdb_assert (sym->aclass () == LOC_OPTIMIZED_OUT);
18718 if (!suppress_add)
18719 list_to_add = cu->list_in_scope;
18722 break;
18723 case DW_TAG_formal_parameter:
18725 /* If we are inside a function, mark this as an argument. If
18726 not, we might be looking at an argument to an inlined function
18727 when we do not have enough information to show inlined frames;
18728 pretend it's a local variable in that case so that the user can
18729 still see it. */
18730 sym->set_domain (VAR_DOMAIN);
18731 struct context_stack *curr
18732 = cu->get_builder ()->get_current_context_stack ();
18733 if (curr != nullptr && curr->name != nullptr)
18734 sym->set_is_argument (1);
18735 attr = dwarf2_attr (die, DW_AT_location, cu);
18736 if (attr != nullptr)
18738 var_decode_location (attr, sym, cu);
18740 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18741 if (attr != nullptr)
18743 dwarf2_const_value (attr, sym, cu);
18746 list_to_add = cu->list_in_scope;
18748 break;
18749 case DW_TAG_unspecified_parameters:
18750 /* From varargs functions; gdb doesn't seem to have any
18751 interest in this information, so just ignore it for now.
18752 (FIXME?) */
18753 break;
18754 case DW_TAG_template_type_param:
18755 suppress_add = 1;
18756 [[fallthrough]];
18757 case DW_TAG_class_type:
18758 case DW_TAG_interface_type:
18759 case DW_TAG_structure_type:
18760 case DW_TAG_union_type:
18761 case DW_TAG_set_type:
18762 case DW_TAG_enumeration_type:
18763 case DW_TAG_namelist:
18764 if (die->tag == DW_TAG_namelist)
18766 sym->set_aclass_index (LOC_STATIC);
18767 sym->set_domain (VAR_DOMAIN);
18769 else if (cu->lang () == language_c
18770 || cu->lang () == language_cplus
18771 || cu->lang () == language_objc
18772 || cu->lang () == language_opencl
18773 || cu->lang () == language_minimal)
18775 /* These languages have a tag namespace. Note that
18776 there's a special hack for C++ in the matching code,
18777 so we don't need to enter a separate typedef for the
18778 tag. */
18779 sym->set_aclass_index (LOC_TYPEDEF);
18780 sym->set_domain (STRUCT_DOMAIN);
18782 else
18784 /* Other languages don't have a tag namespace. */
18785 sym->set_aclass_index (LOC_TYPEDEF);
18786 sym->set_domain (TYPE_DOMAIN);
18789 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
18790 really ever be static objects: otherwise, if you try
18791 to, say, break of a class's method and you're in a file
18792 which doesn't mention that class, it won't work unless
18793 the check for all static symbols in lookup_symbol_aux
18794 saves you. See the OtherFileClass tests in
18795 gdb.c++/namespace.exp. */
18797 if (!suppress_add)
18799 buildsym_compunit *builder = cu->get_builder ();
18800 list_to_add
18801 = (cu->list_in_scope == builder->get_file_symbols ()
18802 && cu->lang () == language_cplus
18803 ? builder->get_global_symbols ()
18804 : cu->list_in_scope);
18806 /* The semantics of C++ state that "struct foo {
18807 ... }" also defines a typedef for "foo". */
18808 if (cu->lang () == language_cplus
18809 || cu->lang () == language_ada
18810 || cu->lang () == language_d
18811 || cu->lang () == language_rust)
18813 /* The symbol's name is already allocated along
18814 with this objfile, so we don't need to
18815 duplicate it for the type. */
18816 if (sym->type ()->name () == 0)
18817 sym->type ()->set_name (sym->search_name ());
18820 break;
18821 case DW_TAG_unspecified_type:
18822 if (cu->lang () == language_ada)
18823 break;
18824 [[fallthrough]];
18825 case DW_TAG_typedef:
18826 case DW_TAG_array_type:
18827 case DW_TAG_base_type:
18828 case DW_TAG_subrange_type:
18829 case DW_TAG_generic_subrange:
18830 sym->set_aclass_index (LOC_TYPEDEF);
18831 sym->set_domain (TYPE_DOMAIN);
18832 list_to_add = cu->list_in_scope;
18833 break;
18834 case DW_TAG_enumerator:
18835 sym->set_domain (VAR_DOMAIN);
18836 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18837 if (attr != nullptr)
18839 dwarf2_const_value (attr, sym, cu);
18842 /* NOTE: carlton/2003-11-10: See comment above in the
18843 DW_TAG_class_type, etc. block. */
18845 list_to_add
18846 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
18847 && cu->lang () == language_cplus
18848 ? cu->get_builder ()->get_global_symbols ()
18849 : cu->list_in_scope);
18850 break;
18851 case DW_TAG_imported_declaration:
18852 case DW_TAG_namespace:
18853 sym->set_domain (TYPE_DOMAIN);
18854 sym->set_aclass_index (LOC_TYPEDEF);
18855 list_to_add = cu->get_builder ()->get_global_symbols ();
18856 break;
18857 case DW_TAG_module:
18858 sym->set_aclass_index (LOC_TYPEDEF);
18859 sym->set_domain (MODULE_DOMAIN);
18860 list_to_add = cu->get_builder ()->get_global_symbols ();
18861 break;
18862 case DW_TAG_common_block:
18863 sym->set_aclass_index (LOC_COMMON_BLOCK);
18864 sym->set_domain (COMMON_BLOCK_DOMAIN);
18865 list_to_add = cu->list_in_scope;
18866 break;
18867 default:
18868 /* Not a tag we recognize. Hopefully we aren't processing
18869 trash data, but since we must specifically ignore things
18870 we don't recognize, there is nothing else we should do at
18871 this point. */
18872 complaint (_("unsupported tag: '%s'"),
18873 dwarf_tag_name (die->tag));
18874 break;
18877 if (suppress_add)
18879 sym->hash_next = objfile->template_symbols;
18880 objfile->template_symbols = sym;
18881 list_to_add = NULL;
18884 if (list_to_add != NULL)
18885 add_symbol_to_list (sym, list_to_add);
18887 /* For the benefit of old versions of GCC, check for anonymous
18888 namespaces based on the demangled name. */
18889 if (!cu->processing_has_namespace_info
18890 && cu->lang () == language_cplus)
18891 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
18893 return (sym);
18896 /* Given an attr with a DW_FORM_dataN value in host byte order,
18897 zero-extend it as appropriate for the symbol's type. The DWARF
18898 standard (v4) is not entirely clear about the meaning of using
18899 DW_FORM_dataN for a constant with a signed type, where the type is
18900 wider than the data. The conclusion of a discussion on the DWARF
18901 list was that this is unspecified. We choose to always zero-extend
18902 because that is the interpretation long in use by GCC. */
18904 static gdb_byte *
18905 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
18906 struct dwarf2_cu *cu, LONGEST *value, int bits)
18908 struct objfile *objfile = cu->per_objfile->objfile;
18909 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
18910 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
18911 LONGEST l = attr->constant_value (0);
18913 if (bits < sizeof (*value) * 8)
18915 l &= ((LONGEST) 1 << bits) - 1;
18916 *value = l;
18918 else if (bits == sizeof (*value) * 8)
18919 *value = l;
18920 else
18922 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
18923 store_unsigned_integer (bytes, bits / 8, byte_order, l);
18924 return bytes;
18927 return NULL;
18930 /* Read a constant value from an attribute. Either set *VALUE, or if
18931 the value does not fit in *VALUE, set *BYTES - either already
18932 allocated on the objfile obstack, or newly allocated on OBSTACK,
18933 or, set *BATON, if we translated the constant to a location
18934 expression. */
18936 static void
18937 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
18938 const char *name, struct obstack *obstack,
18939 struct dwarf2_cu *cu,
18940 LONGEST *value, const gdb_byte **bytes,
18941 struct dwarf2_locexpr_baton **baton)
18943 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18944 struct objfile *objfile = per_objfile->objfile;
18945 struct comp_unit_head *cu_header = &cu->header;
18946 struct dwarf_block *blk;
18947 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
18948 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18950 *value = 0;
18951 *bytes = NULL;
18952 *baton = NULL;
18954 switch (attr->form)
18956 case DW_FORM_addr:
18957 case DW_FORM_addrx:
18958 case DW_FORM_GNU_addr_index:
18960 gdb_byte *data;
18962 if (type->length () != cu_header->addr_size)
18963 dwarf2_const_value_length_mismatch_complaint (name,
18964 cu_header->addr_size,
18965 type->length ());
18966 /* Symbols of this form are reasonably rare, so we just
18967 piggyback on the existing location code rather than writing
18968 a new implementation of symbol_computed_ops. */
18969 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
18970 (*baton)->per_objfile = per_objfile;
18971 (*baton)->per_cu = cu->per_cu;
18972 gdb_assert ((*baton)->per_cu);
18974 (*baton)->size = 2 + cu_header->addr_size;
18975 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
18976 (*baton)->data = data;
18978 data[0] = DW_OP_addr;
18979 store_unsigned_integer (&data[1], cu_header->addr_size,
18980 byte_order, (ULONGEST) attr->as_address ());
18981 data[cu_header->addr_size + 1] = DW_OP_stack_value;
18983 break;
18984 case DW_FORM_string:
18985 case DW_FORM_strp:
18986 case DW_FORM_strx:
18987 case DW_FORM_GNU_str_index:
18988 case DW_FORM_GNU_strp_alt:
18989 /* The string is already allocated on the objfile obstack, point
18990 directly to it. */
18991 *bytes = (const gdb_byte *) attr->as_string ();
18992 break;
18993 case DW_FORM_block1:
18994 case DW_FORM_block2:
18995 case DW_FORM_block4:
18996 case DW_FORM_block:
18997 case DW_FORM_exprloc:
18998 case DW_FORM_data16:
18999 blk = attr->as_block ();
19000 if (type->length () != blk->size)
19001 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19002 type->length ());
19003 *bytes = blk->data;
19004 break;
19006 /* The DW_AT_const_value attributes are supposed to carry the
19007 symbol's value "represented as it would be on the target
19008 architecture." By the time we get here, it's already been
19009 converted to host endianness, so we just need to sign- or
19010 zero-extend it as appropriate. */
19011 case DW_FORM_data1:
19012 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19013 break;
19014 case DW_FORM_data2:
19015 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19016 break;
19017 case DW_FORM_data4:
19018 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19019 break;
19020 case DW_FORM_data8:
19021 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19022 break;
19024 case DW_FORM_sdata:
19025 case DW_FORM_implicit_const:
19026 *value = attr->as_signed ();
19027 break;
19029 case DW_FORM_udata:
19030 *value = attr->as_unsigned ();
19031 break;
19033 default:
19034 complaint (_("unsupported const value attribute form: '%s'"),
19035 dwarf_form_name (attr->form));
19036 *value = 0;
19037 break;
19042 /* Copy constant value from an attribute to a symbol. */
19044 static void
19045 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19046 struct dwarf2_cu *cu)
19048 struct objfile *objfile = cu->per_objfile->objfile;
19049 LONGEST value;
19050 const gdb_byte *bytes;
19051 struct dwarf2_locexpr_baton *baton;
19053 dwarf2_const_value_attr (attr, sym->type (),
19054 sym->print_name (),
19055 &objfile->objfile_obstack, cu,
19056 &value, &bytes, &baton);
19058 if (baton != NULL)
19060 SYMBOL_LOCATION_BATON (sym) = baton;
19061 sym->set_aclass_index (dwarf2_locexpr_index);
19063 else if (bytes != NULL)
19065 sym->set_value_bytes (bytes);
19066 sym->set_aclass_index (LOC_CONST_BYTES);
19068 else
19070 sym->set_value_longest (value);
19071 sym->set_aclass_index (LOC_CONST);
19075 /* Return the type of the die in question using its DW_AT_type attribute. */
19077 static struct type *
19078 die_type (struct die_info *die, struct dwarf2_cu *cu)
19080 struct attribute *type_attr;
19082 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19083 if (!type_attr)
19085 struct objfile *objfile = cu->per_objfile->objfile;
19086 /* A missing DW_AT_type represents a void type. */
19087 return builtin_type (objfile)->builtin_void;
19090 return lookup_die_type (die, type_attr, cu);
19093 /* True iff CU's producer generates GNAT Ada auxiliary information
19094 that allows to find parallel types through that information instead
19095 of having to do expensive parallel lookups by type name. */
19097 static int
19098 need_gnat_info (struct dwarf2_cu *cu)
19100 /* Assume that the Ada compiler was GNAT, which always produces
19101 the auxiliary information. */
19102 return (cu->lang () == language_ada);
19105 /* Return the auxiliary type of the die in question using its
19106 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19107 attribute is not present. */
19109 static struct type *
19110 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19112 struct attribute *type_attr;
19114 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19115 if (!type_attr)
19116 return NULL;
19118 return lookup_die_type (die, type_attr, cu);
19121 /* If DIE has a descriptive_type attribute, then set the TYPE's
19122 descriptive type accordingly. */
19124 static void
19125 set_descriptive_type (struct type *type, struct die_info *die,
19126 struct dwarf2_cu *cu)
19128 struct type *descriptive_type = die_descriptive_type (die, cu);
19130 if (descriptive_type)
19132 ALLOCATE_GNAT_AUX_TYPE (type);
19133 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19137 /* Return the containing type of the die in question using its
19138 DW_AT_containing_type attribute. */
19140 static struct type *
19141 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19143 struct attribute *type_attr;
19144 struct objfile *objfile = cu->per_objfile->objfile;
19146 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19147 if (!type_attr)
19148 error (_(DWARF_ERROR_PREFIX
19149 "Problem turning containing type into gdb type "
19150 "[in module %s]"),
19151 objfile_name (objfile));
19153 return lookup_die_type (die, type_attr, cu);
19156 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19158 static struct type *
19159 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19161 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19162 struct objfile *objfile = per_objfile->objfile;
19163 char *saved;
19165 std::string message
19166 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19167 objfile_name (objfile),
19168 sect_offset_str (cu->header.sect_off),
19169 sect_offset_str (die->sect_off));
19170 saved = obstack_strdup (&objfile->objfile_obstack, message);
19172 return type_allocator (objfile, cu->lang ()).new_type (TYPE_CODE_ERROR,
19173 0, saved);
19176 /* Look up the type of DIE in CU using its type attribute ATTR.
19177 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19178 DW_AT_containing_type.
19179 If there is no type substitute an error marker. */
19181 static struct type *
19182 lookup_die_type (struct die_info *die, const struct attribute *attr,
19183 struct dwarf2_cu *cu)
19185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19186 struct objfile *objfile = per_objfile->objfile;
19187 struct type *this_type;
19189 gdb_assert (attr->name == DW_AT_type
19190 || attr->name == DW_AT_GNAT_descriptive_type
19191 || attr->name == DW_AT_containing_type);
19193 /* First see if we have it cached. */
19195 if (attr->form == DW_FORM_GNU_ref_alt)
19197 struct dwarf2_per_cu_data *per_cu;
19198 sect_offset sect_off = attr->get_ref_die_offset ();
19200 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
19201 per_objfile->per_bfd);
19202 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
19204 else if (attr->form_is_ref ())
19206 sect_offset sect_off = attr->get_ref_die_offset ();
19208 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
19210 else if (attr->form == DW_FORM_ref_sig8)
19212 ULONGEST signature = attr->as_signature ();
19214 return get_signatured_type (die, signature, cu);
19216 else
19218 complaint (_(DWARF_ERROR_PREFIX
19219 "Bad type attribute %s in DIE at %s [in module %s]"),
19220 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
19221 objfile_name (objfile));
19222 return build_error_marker_type (cu, die);
19225 /* If not cached we need to read it in. */
19227 if (this_type == NULL)
19229 struct die_info *type_die = NULL;
19230 struct dwarf2_cu *type_cu = cu;
19232 if (attr->form_is_ref ())
19233 type_die = follow_die_ref (die, attr, &type_cu);
19234 if (type_die == NULL)
19235 return build_error_marker_type (cu, die);
19236 /* If we find the type now, it's probably because the type came
19237 from an inter-CU reference and the type's CU got expanded before
19238 ours. */
19239 this_type = read_type_die (type_die, type_cu);
19242 /* If we still don't have a type use an error marker. */
19244 if (this_type == NULL)
19245 return build_error_marker_type (cu, die);
19247 return this_type;
19250 /* Return the type in DIE, CU.
19251 Returns NULL for invalid types.
19253 This first does a lookup in die_type_hash,
19254 and only reads the die in if necessary.
19256 NOTE: This can be called when reading in partial or full symbols. */
19258 static struct type *
19259 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19261 struct type *this_type;
19263 this_type = get_die_type (die, cu);
19264 if (this_type)
19265 return this_type;
19267 return read_type_die_1 (die, cu);
19270 /* Read the type in DIE, CU.
19271 Returns NULL for invalid types. */
19273 static struct type *
19274 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19276 struct type *this_type = NULL;
19278 switch (die->tag)
19280 case DW_TAG_class_type:
19281 case DW_TAG_interface_type:
19282 case DW_TAG_structure_type:
19283 case DW_TAG_union_type:
19284 this_type = read_structure_type (die, cu);
19285 break;
19286 case DW_TAG_enumeration_type:
19287 this_type = read_enumeration_type (die, cu);
19288 break;
19289 case DW_TAG_entry_point:
19290 case DW_TAG_subprogram:
19291 case DW_TAG_subroutine_type:
19292 case DW_TAG_inlined_subroutine:
19293 this_type = read_subroutine_type (die, cu);
19294 break;
19295 case DW_TAG_array_type:
19296 this_type = read_array_type (die, cu);
19297 break;
19298 case DW_TAG_set_type:
19299 this_type = read_set_type (die, cu);
19300 break;
19301 case DW_TAG_pointer_type:
19302 this_type = read_tag_pointer_type (die, cu);
19303 break;
19304 case DW_TAG_ptr_to_member_type:
19305 this_type = read_tag_ptr_to_member_type (die, cu);
19306 break;
19307 case DW_TAG_reference_type:
19308 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19309 break;
19310 case DW_TAG_rvalue_reference_type:
19311 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19312 break;
19313 case DW_TAG_const_type:
19314 this_type = read_tag_const_type (die, cu);
19315 break;
19316 case DW_TAG_volatile_type:
19317 this_type = read_tag_volatile_type (die, cu);
19318 break;
19319 case DW_TAG_restrict_type:
19320 this_type = read_tag_restrict_type (die, cu);
19321 break;
19322 case DW_TAG_string_type:
19323 this_type = read_tag_string_type (die, cu);
19324 break;
19325 case DW_TAG_typedef:
19326 this_type = read_typedef (die, cu);
19327 break;
19328 case DW_TAG_generic_subrange:
19329 case DW_TAG_subrange_type:
19330 this_type = read_subrange_type (die, cu);
19331 break;
19332 case DW_TAG_base_type:
19333 this_type = read_base_type (die, cu);
19334 break;
19335 case DW_TAG_unspecified_type:
19336 this_type = read_unspecified_type (die, cu);
19337 break;
19338 case DW_TAG_namespace:
19339 this_type = read_namespace_type (die, cu);
19340 break;
19341 case DW_TAG_module:
19342 this_type = read_module_type (die, cu);
19343 break;
19344 case DW_TAG_atomic_type:
19345 this_type = read_tag_atomic_type (die, cu);
19346 break;
19347 default:
19348 complaint (_("unexpected tag in read_type_die: '%s'"),
19349 dwarf_tag_name (die->tag));
19350 break;
19353 return this_type;
19356 /* See if we can figure out if the class lives in a namespace. We do
19357 this by looking for a member function; its demangled name will
19358 contain namespace info, if there is any.
19359 Return the computed name or NULL.
19360 Space for the result is allocated on the objfile's obstack.
19361 This is the full-die version of guess_partial_die_structure_name.
19362 In this case we know DIE has no useful parent. */
19364 static const char *
19365 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19367 struct die_info *spec_die;
19368 struct dwarf2_cu *spec_cu;
19369 struct die_info *child;
19370 struct objfile *objfile = cu->per_objfile->objfile;
19372 spec_cu = cu;
19373 spec_die = die_specification (die, &spec_cu);
19374 if (spec_die != NULL)
19376 die = spec_die;
19377 cu = spec_cu;
19380 for (child = die->child;
19381 child != NULL;
19382 child = child->sibling)
19384 if (child->tag == DW_TAG_subprogram)
19386 const char *linkage_name = dw2_linkage_name (child, cu);
19388 if (linkage_name != NULL)
19390 gdb::unique_xmalloc_ptr<char> actual_name
19391 (cu->language_defn->class_name_from_physname (linkage_name));
19392 const char *name = NULL;
19394 if (actual_name != NULL)
19396 const char *die_name = dwarf2_name (die, cu);
19398 if (die_name != NULL
19399 && strcmp (die_name, actual_name.get ()) != 0)
19401 /* Strip off the class name from the full name.
19402 We want the prefix. */
19403 int die_name_len = strlen (die_name);
19404 int actual_name_len = strlen (actual_name.get ());
19405 const char *ptr = actual_name.get ();
19407 /* Test for '::' as a sanity check. */
19408 if (actual_name_len > die_name_len + 2
19409 && ptr[actual_name_len - die_name_len - 1] == ':')
19410 name = obstack_strndup (
19411 &objfile->per_bfd->storage_obstack,
19412 ptr, actual_name_len - die_name_len - 2);
19415 return name;
19420 return NULL;
19423 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19424 prefix part in such case. See
19425 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19427 static const char *
19428 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19430 struct attribute *attr;
19431 const char *base;
19433 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19434 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19435 return NULL;
19437 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19438 return NULL;
19440 attr = dw2_linkage_name_attr (die, cu);
19441 const char *attr_name = attr->as_string ();
19442 if (attr == NULL || attr_name == NULL)
19443 return NULL;
19445 /* dwarf2_name had to be already called. */
19446 gdb_assert (attr->canonical_string_p ());
19448 /* Strip the base name, keep any leading namespaces/classes. */
19449 base = strrchr (attr_name, ':');
19450 if (base == NULL || base == attr_name || base[-1] != ':')
19451 return "";
19453 struct objfile *objfile = cu->per_objfile->objfile;
19454 return obstack_strndup (&objfile->per_bfd->storage_obstack,
19455 attr_name,
19456 &base[-1] - attr_name);
19459 /* Return the name of the namespace/class that DIE is defined within,
19460 or "" if we can't tell. The caller should not xfree the result.
19462 For example, if we're within the method foo() in the following
19463 code:
19465 namespace N {
19466 class C {
19467 void foo () {
19472 then determine_prefix on foo's die will return "N::C". */
19474 static const char *
19475 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19477 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19478 struct die_info *parent, *spec_die;
19479 struct dwarf2_cu *spec_cu;
19480 struct type *parent_type;
19481 const char *retval;
19483 if (cu->lang () != language_cplus
19484 && cu->lang () != language_fortran
19485 && cu->lang () != language_d
19486 && cu->lang () != language_rust)
19487 return "";
19489 retval = anonymous_struct_prefix (die, cu);
19490 if (retval)
19491 return retval;
19493 /* We have to be careful in the presence of DW_AT_specification.
19494 For example, with GCC 3.4, given the code
19496 namespace N {
19497 void foo() {
19498 // Definition of N::foo.
19502 then we'll have a tree of DIEs like this:
19504 1: DW_TAG_compile_unit
19505 2: DW_TAG_namespace // N
19506 3: DW_TAG_subprogram // declaration of N::foo
19507 4: DW_TAG_subprogram // definition of N::foo
19508 DW_AT_specification // refers to die #3
19510 Thus, when processing die #4, we have to pretend that we're in
19511 the context of its DW_AT_specification, namely the context of die
19512 #3. */
19513 spec_cu = cu;
19514 spec_die = die_specification (die, &spec_cu);
19515 if (spec_die == NULL)
19516 parent = die->parent;
19517 else
19519 parent = spec_die->parent;
19520 cu = spec_cu;
19523 if (parent == NULL)
19524 return "";
19525 else if (parent->building_fullname)
19527 const char *name;
19528 const char *parent_name;
19530 /* It has been seen on RealView 2.2 built binaries,
19531 DW_TAG_template_type_param types actually _defined_ as
19532 children of the parent class:
19534 enum E {};
19535 template class <class Enum> Class{};
19536 Class<enum E> class_e;
19538 1: DW_TAG_class_type (Class)
19539 2: DW_TAG_enumeration_type (E)
19540 3: DW_TAG_enumerator (enum1:0)
19541 3: DW_TAG_enumerator (enum2:1)
19543 2: DW_TAG_template_type_param
19544 DW_AT_type DW_FORM_ref_udata (E)
19546 Besides being broken debug info, it can put GDB into an
19547 infinite loop. Consider:
19549 When we're building the full name for Class<E>, we'll start
19550 at Class, and go look over its template type parameters,
19551 finding E. We'll then try to build the full name of E, and
19552 reach here. We're now trying to build the full name of E,
19553 and look over the parent DIE for containing scope. In the
19554 broken case, if we followed the parent DIE of E, we'd again
19555 find Class, and once again go look at its template type
19556 arguments, etc., etc. Simply don't consider such parent die
19557 as source-level parent of this die (it can't be, the language
19558 doesn't allow it), and break the loop here. */
19559 name = dwarf2_name (die, cu);
19560 parent_name = dwarf2_name (parent, cu);
19561 complaint (_("template param type '%s' defined within parent '%s'"),
19562 name ? name : "<unknown>",
19563 parent_name ? parent_name : "<unknown>");
19564 return "";
19566 else
19567 switch (parent->tag)
19569 case DW_TAG_namespace:
19570 parent_type = read_type_die (parent, cu);
19571 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19572 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19573 Work around this problem here. */
19574 if (cu->lang () == language_cplus
19575 && strcmp (parent_type->name (), "::") == 0)
19576 return "";
19577 /* We give a name to even anonymous namespaces. */
19578 return parent_type->name ();
19579 case DW_TAG_class_type:
19580 case DW_TAG_interface_type:
19581 case DW_TAG_structure_type:
19582 case DW_TAG_union_type:
19583 case DW_TAG_module:
19584 parent_type = read_type_die (parent, cu);
19585 if (parent_type->name () != NULL)
19586 return parent_type->name ();
19587 else
19588 /* An anonymous structure is only allowed non-static data
19589 members; no typedefs, no member functions, et cetera.
19590 So it does not need a prefix. */
19591 return "";
19592 case DW_TAG_compile_unit:
19593 case DW_TAG_partial_unit:
19594 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19595 if (cu->lang () == language_cplus
19596 && !per_objfile->per_bfd->types.empty ()
19597 && die->child != NULL
19598 && (die->tag == DW_TAG_class_type
19599 || die->tag == DW_TAG_structure_type
19600 || die->tag == DW_TAG_union_type))
19602 const char *name = guess_full_die_structure_name (die, cu);
19603 if (name != NULL)
19604 return name;
19606 return "";
19607 case DW_TAG_subprogram:
19608 /* Nested subroutines in Fortran get a prefix with the name
19609 of the parent's subroutine. Entry points are prefixed by the
19610 parent's namespace. */
19611 if (cu->lang () == language_fortran)
19613 if ((die->tag == DW_TAG_subprogram)
19614 && (dwarf2_name (parent, cu) != NULL))
19615 return dwarf2_name (parent, cu);
19616 else if (die->tag == DW_TAG_entry_point)
19617 return determine_prefix (parent, cu);
19619 return "";
19620 case DW_TAG_enumeration_type:
19621 parent_type = read_type_die (parent, cu);
19622 if (parent_type->is_declared_class ())
19624 if (parent_type->name () != NULL)
19625 return parent_type->name ();
19626 return "";
19628 [[fallthrough]];
19629 default:
19630 return determine_prefix (parent, cu);
19634 /* Return a newly-allocated string formed by concatenating PREFIX and
19635 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or
19636 empty, then simply copy the SUFFIX or PREFIX, respectively. The CU
19637 argument is used to determine the language and hence, the
19638 appropriate separator. */
19640 static gdb::unique_xmalloc_ptr<char>
19641 typename_concat (const char *prefix, const char *suffix, int physname,
19642 struct dwarf2_cu *cu)
19644 const char *lead = "";
19645 const char *sep;
19647 if (suffix == NULL || suffix[0] == '\0'
19648 || prefix == NULL || prefix[0] == '\0')
19649 sep = "";
19650 else if (cu->lang () == language_d)
19652 /* For D, the 'main' function could be defined in any module, but it
19653 should never be prefixed. */
19654 if (strcmp (suffix, "D main") == 0)
19656 prefix = "";
19657 sep = "";
19659 else
19660 sep = ".";
19662 else if (cu->lang () == language_fortran && physname)
19664 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19665 DW_AT_MIPS_linkage_name is preferred and used instead. */
19667 lead = "__";
19668 sep = "_MOD_";
19670 else
19671 sep = "::";
19673 if (prefix == NULL)
19674 prefix = "";
19675 if (suffix == NULL)
19676 suffix = "";
19678 return gdb::unique_xmalloc_ptr<char> (concat (lead, prefix, sep, suffix,
19679 nullptr));
19682 /* Return a generic name for a DW_TAG_template_type_param or
19683 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
19684 per parent, so each function/class/struct template will have their own set
19685 of template parameters named <unnnamed0>, <unnamed1>, ... where the
19686 enumeration starts at 0 and represents the position of the template tag in
19687 the list of unnamed template tags for this parent, counting both, type and
19688 value tags. */
19690 static const char *
19691 unnamed_template_tag_name (die_info *die, dwarf2_cu *cu)
19693 if (die->parent == nullptr)
19694 return nullptr;
19696 /* Count the parent types unnamed template type and value children until, we
19697 arrive at our entry. */
19698 size_t nth_unnamed = 0;
19700 die_info *child = die->parent->child;
19701 while (child != die)
19703 gdb_assert (child != nullptr);
19704 if (child->tag == DW_TAG_template_type_param
19705 || child->tag == DW_TAG_template_value_param)
19707 if (dwarf2_attr (child, DW_AT_name, cu) == nullptr)
19708 ++nth_unnamed;
19710 child = child->sibling;
19713 const std::string name_str = "<unnamed" + std::to_string (nth_unnamed) + ">";
19714 return cu->per_objfile->objfile->intern (name_str.c_str ());
19717 /* Get name of a die, return NULL if not found. */
19719 static const char *
19720 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
19721 struct objfile *objfile)
19723 if (name == nullptr)
19724 return name;
19726 if (cu->lang () == language_cplus)
19728 gdb::unique_xmalloc_ptr<char> canon_name
19729 = cp_canonicalize_string (name);
19731 if (canon_name != nullptr)
19732 name = objfile->intern (canon_name.get ());
19734 else if (cu->lang () == language_c)
19736 gdb::unique_xmalloc_ptr<char> canon_name
19737 = c_canonicalize_name (name);
19739 if (canon_name != nullptr)
19740 name = objfile->intern (canon_name.get ());
19743 return name;
19746 /* Get name of a die, return NULL if not found.
19747 Anonymous namespaces are converted to their magic string. */
19749 static const char *
19750 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
19752 struct attribute *attr;
19753 struct objfile *objfile = cu->per_objfile->objfile;
19755 attr = dwarf2_attr (die, DW_AT_name, cu);
19756 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
19757 if (attr_name == nullptr
19758 && die->tag != DW_TAG_namespace
19759 && die->tag != DW_TAG_class_type
19760 && die->tag != DW_TAG_interface_type
19761 && die->tag != DW_TAG_structure_type
19762 && die->tag != DW_TAG_namelist
19763 && die->tag != DW_TAG_union_type
19764 && die->tag != DW_TAG_template_type_param
19765 && die->tag != DW_TAG_template_value_param)
19766 return NULL;
19768 switch (die->tag)
19770 /* A member's name should not be canonicalized. This is a bit
19771 of a hack, in that normally it should not be possible to run
19772 into this situation; however, the dw2-unusual-field-names.exp
19773 test creates custom DWARF that does. */
19774 case DW_TAG_member:
19775 case DW_TAG_compile_unit:
19776 case DW_TAG_partial_unit:
19777 /* Compilation units have a DW_AT_name that is a filename, not
19778 a source language identifier. */
19779 case DW_TAG_enumeration_type:
19780 case DW_TAG_enumerator:
19781 /* These tags always have simple identifiers already; no need
19782 to canonicalize them. */
19783 return attr_name;
19785 case DW_TAG_namespace:
19786 if (attr_name != nullptr)
19787 return attr_name;
19788 return CP_ANONYMOUS_NAMESPACE_STR;
19790 /* DWARF does not actually require template tags to have a name. */
19791 case DW_TAG_template_type_param:
19792 case DW_TAG_template_value_param:
19793 if (attr_name == nullptr)
19794 return unnamed_template_tag_name (die, cu);
19795 [[fallthrough]];
19796 case DW_TAG_class_type:
19797 case DW_TAG_interface_type:
19798 case DW_TAG_structure_type:
19799 case DW_TAG_union_type:
19800 case DW_TAG_namelist:
19801 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
19802 structures or unions. These were of the form "._%d" in GCC 4.1,
19803 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
19804 and GCC 4.4. We work around this problem by ignoring these. */
19805 if (attr_name != nullptr
19806 && (startswith (attr_name, "._")
19807 || startswith (attr_name, "<anonymous")))
19808 return NULL;
19810 /* GCC might emit a nameless typedef that has a linkage name. See
19811 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19812 if (!attr || attr_name == NULL)
19814 attr = dw2_linkage_name_attr (die, cu);
19815 attr_name = attr == nullptr ? nullptr : attr->as_string ();
19816 if (attr == NULL || attr_name == NULL)
19817 return NULL;
19819 /* Avoid demangling attr_name the second time on a second
19820 call for the same DIE. */
19821 if (!attr->canonical_string_p ())
19823 gdb::unique_xmalloc_ptr<char> demangled
19824 (gdb_demangle (attr_name, DMGL_TYPES));
19825 if (demangled == nullptr)
19826 return nullptr;
19828 attr->set_string_canonical (objfile->intern (demangled.get ()));
19829 attr_name = attr->as_string ();
19832 /* Strip any leading namespaces/classes, keep only the
19833 base name. DW_AT_name for named DIEs does not
19834 contain the prefixes. */
19835 const char *base = strrchr (attr_name, ':');
19836 if (base && base > attr_name && base[-1] == ':')
19837 return &base[1];
19838 else
19839 return attr_name;
19841 break;
19843 default:
19844 break;
19847 if (!attr->canonical_string_p ())
19848 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
19849 objfile));
19850 return attr->as_string ();
19853 /* Return the die that this die in an extension of, or NULL if there
19854 is none. *EXT_CU is the CU containing DIE on input, and the CU
19855 containing the return value on output. */
19857 static struct die_info *
19858 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
19860 struct attribute *attr;
19862 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
19863 if (attr == NULL)
19864 return NULL;
19866 return follow_die_ref (die, attr, ext_cu);
19869 /* Follow reference or signature attribute ATTR of SRC_DIE.
19870 On entry *REF_CU is the CU of SRC_DIE.
19871 On exit *REF_CU is the CU of the result. */
19873 static struct die_info *
19874 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
19875 struct dwarf2_cu **ref_cu)
19877 struct die_info *die;
19879 if (attr->form_is_ref ())
19880 die = follow_die_ref (src_die, attr, ref_cu);
19881 else if (attr->form == DW_FORM_ref_sig8)
19882 die = follow_die_sig (src_die, attr, ref_cu);
19883 else
19885 src_die->error_dump ();
19886 error (_(DWARF_ERROR_PREFIX
19887 "Expected reference attribute [in module %s]"),
19888 objfile_name ((*ref_cu)->per_objfile->objfile));
19891 return die;
19894 /* Follow reference OFFSET.
19895 On entry *REF_CU is the CU of the source die referencing OFFSET.
19896 On exit *REF_CU is the CU of the result.
19897 Returns NULL if OFFSET is invalid. */
19899 static struct die_info *
19900 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
19901 struct dwarf2_cu **ref_cu)
19903 struct dwarf2_cu *target_cu, *cu = *ref_cu;
19904 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19906 gdb_assert (cu->per_cu != NULL);
19908 target_cu = cu;
19910 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
19911 "source CU contains target offset: %d",
19912 sect_offset_str (cu->per_cu->sect_off),
19913 sect_offset_str (sect_off),
19914 cu->header.offset_in_cu_p (sect_off));
19916 if (cu->per_cu->is_debug_types)
19918 /* .debug_types CUs cannot reference anything outside their CU.
19919 If they need to, they have to reference a signatured type via
19920 DW_FORM_ref_sig8. */
19921 if (!cu->header.offset_in_cu_p (sect_off))
19922 return NULL;
19924 else if (offset_in_dwz != cu->per_cu->is_dwz
19925 || !cu->header.offset_in_cu_p (sect_off))
19927 struct dwarf2_per_cu_data *per_cu;
19929 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19930 per_objfile->per_bfd);
19932 dwarf_read_debug_printf_v ("target CU offset: %s, "
19933 "target CU DIEs loaded: %d",
19934 sect_offset_str (per_cu->sect_off),
19935 per_objfile->get_cu (per_cu) != nullptr);
19937 /* If necessary, add it to the queue and load its DIEs.
19939 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
19940 it doesn't mean they are currently loaded. Since we require them
19941 to be loaded, we must check for ourselves. */
19942 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->lang ())
19943 || per_objfile->get_cu (per_cu) == nullptr)
19944 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
19945 false, cu->lang ());
19947 target_cu = per_objfile->get_cu (per_cu);
19948 if (target_cu == nullptr)
19949 error (_(DWARF_ERROR_PREFIX
19950 "cannot follow reference to DIE at %s"
19951 " [in module %s]"),
19952 sect_offset_str (sect_off),
19953 objfile_name (per_objfile->objfile));
19955 else if (cu->dies == NULL)
19957 /* We're loading full DIEs during partial symbol reading. */
19958 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
19959 language_minimal);
19962 *ref_cu = target_cu;
19964 auto it = target_cu->die_hash.find (sect_off);
19965 return it != target_cu->die_hash.end () ? *it : nullptr;
19968 /* Follow reference attribute ATTR of SRC_DIE.
19969 On entry *REF_CU is the CU of SRC_DIE.
19970 On exit *REF_CU is the CU of the result. */
19972 static struct die_info *
19973 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
19974 struct dwarf2_cu **ref_cu)
19976 sect_offset sect_off = attr->get_ref_die_offset ();
19977 struct dwarf2_cu *cu = *ref_cu;
19978 struct die_info *die;
19980 if (attr->form != DW_FORM_GNU_ref_alt && src_die->sect_off == sect_off)
19982 /* Self-reference, we're done. */
19983 return src_die;
19986 die = follow_die_offset (sect_off,
19987 (attr->form == DW_FORM_GNU_ref_alt
19988 || cu->per_cu->is_dwz),
19989 ref_cu);
19990 if (!die)
19991 error (_(DWARF_ERROR_PREFIX
19992 "Cannot find DIE at %s referenced from DIE at %s [in module %s]"),
19993 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
19994 objfile_name (cu->per_objfile->objfile));
19996 return die;
19999 /* See read.h. */
20001 struct dwarf2_locexpr_baton
20002 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
20003 dwarf2_per_cu_data *per_cu,
20004 dwarf2_per_objfile *per_objfile,
20005 gdb::function_view<CORE_ADDR ()> get_frame_pc,
20006 bool resolve_abstract_p)
20008 struct die_info *die;
20009 struct attribute *attr;
20010 struct dwarf2_locexpr_baton retval;
20011 struct objfile *objfile = per_objfile->objfile;
20013 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20014 if (cu == nullptr)
20015 cu = load_cu (per_cu, per_objfile, false);
20017 if (cu == nullptr)
20019 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20020 Instead just throw an error, not much else we can do. */
20021 error (_(DWARF_ERROR_PREFIX
20022 "Dummy CU at %s referenced [in module %s]"),
20023 sect_offset_str (sect_off), objfile_name (objfile));
20026 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20027 if (!die)
20028 error (_(DWARF_ERROR_PREFIX
20029 "Cannot find DIE at %s referenced [in module %s]"),
20030 sect_offset_str (sect_off), objfile_name (objfile));
20032 attr = dwarf2_attr (die, DW_AT_location, cu);
20033 if (!attr && resolve_abstract_p
20034 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
20035 != per_objfile->per_bfd->abstract_to_concrete.end ()))
20037 CORE_ADDR pc = get_frame_pc ();
20039 for (const auto &cand_off
20040 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
20042 struct dwarf2_cu *cand_cu = cu;
20043 struct die_info *cand
20044 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
20045 if (!cand
20046 || !cand->parent
20047 || cand->parent->tag != DW_TAG_subprogram)
20048 continue;
20050 unrelocated_addr unrel_low, unrel_high;
20051 get_scope_pc_bounds (cand->parent, &unrel_low, &unrel_high, cu);
20052 if (unrel_low == ((unrelocated_addr) -1))
20053 continue;
20054 CORE_ADDR pc_low = per_objfile->relocate (unrel_low);
20055 CORE_ADDR pc_high = per_objfile->relocate (unrel_high);
20056 if (!(pc_low <= pc && pc < pc_high))
20057 continue;
20059 die = cand;
20060 attr = dwarf2_attr (die, DW_AT_location, cu);
20061 break;
20065 if (!attr)
20067 /* DWARF: "If there is no such attribute, then there is no effect.".
20068 DATA is ignored if SIZE is 0. */
20070 retval.data = NULL;
20071 retval.size = 0;
20073 else if (attr->form_is_section_offset ())
20075 struct dwarf2_loclist_baton loclist_baton;
20076 CORE_ADDR pc = get_frame_pc ();
20077 size_t size;
20079 fill_in_loclist_baton (cu, &loclist_baton, attr);
20081 retval.data = dwarf2_find_location_expression (&loclist_baton,
20082 &size, pc);
20083 retval.size = size;
20085 else
20087 if (!attr->form_is_block ())
20088 error (_(DWARF_ERROR_PREFIX
20089 "DIE at %s is neither DW_FORM_block* nor DW_FORM_exprloc"
20090 " [in module %s]"),
20091 sect_offset_str (sect_off), objfile_name (objfile));
20093 struct dwarf_block *block = attr->as_block ();
20094 retval.data = block->data;
20095 retval.size = block->size;
20097 retval.per_objfile = per_objfile;
20098 retval.per_cu = cu->per_cu;
20100 per_objfile->age_comp_units ();
20102 return retval;
20105 /* See read.h. */
20107 struct dwarf2_locexpr_baton
20108 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20109 dwarf2_per_cu_data *per_cu,
20110 dwarf2_per_objfile *per_objfile,
20111 gdb::function_view<CORE_ADDR ()> get_frame_pc)
20113 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
20115 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
20116 get_frame_pc);
20119 /* Write a constant of a given type as target-ordered bytes into
20120 OBSTACK. */
20122 static const gdb_byte *
20123 write_constant_as_bytes (struct obstack *obstack,
20124 enum bfd_endian byte_order,
20125 struct type *type,
20126 ULONGEST value,
20127 LONGEST *len)
20129 gdb_byte *result;
20131 *len = type->length ();
20132 result = (gdb_byte *) obstack_alloc (obstack, *len);
20133 store_unsigned_integer (result, *len, byte_order, value);
20135 return result;
20138 /* See read.h. */
20140 const gdb_byte *
20141 dwarf2_fetch_constant_bytes (sect_offset sect_off,
20142 dwarf2_per_cu_data *per_cu,
20143 dwarf2_per_objfile *per_objfile,
20144 obstack *obstack,
20145 LONGEST *len)
20147 struct die_info *die;
20148 struct attribute *attr;
20149 const gdb_byte *result = NULL;
20150 struct type *type;
20151 LONGEST value;
20152 enum bfd_endian byte_order;
20153 struct objfile *objfile = per_objfile->objfile;
20155 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20156 if (cu == nullptr)
20157 cu = load_cu (per_cu, per_objfile, false);
20159 if (cu == nullptr)
20161 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20162 Instead just throw an error, not much else we can do. */
20163 error (_(DWARF_ERROR_PREFIX
20164 "Dummy CU at %s referenced [in module %s]"),
20165 sect_offset_str (sect_off), objfile_name (objfile));
20168 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20169 if (!die)
20170 error (_(DWARF_ERROR_PREFIX
20171 "Cannot find DIE at %s referenced [in module %s]"),
20172 sect_offset_str (sect_off), objfile_name (objfile));
20174 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20175 if (attr == NULL)
20176 return NULL;
20178 byte_order = (bfd_big_endian (objfile->obfd.get ())
20179 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20181 switch (attr->form)
20183 case DW_FORM_addr:
20184 case DW_FORM_addrx:
20185 case DW_FORM_GNU_addr_index:
20187 gdb_byte *tem;
20189 *len = cu->header.addr_size;
20190 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20191 store_unsigned_integer (tem, *len, byte_order,
20192 (ULONGEST) attr->as_address ());
20193 result = tem;
20195 break;
20196 case DW_FORM_string:
20197 case DW_FORM_strp:
20198 case DW_FORM_strx:
20199 case DW_FORM_GNU_str_index:
20200 case DW_FORM_GNU_strp_alt:
20201 /* The string is already allocated on the objfile obstack, point
20202 directly to it. */
20204 const char *attr_name = attr->as_string ();
20205 result = (const gdb_byte *) attr_name;
20206 *len = strlen (attr_name);
20208 break;
20209 case DW_FORM_block1:
20210 case DW_FORM_block2:
20211 case DW_FORM_block4:
20212 case DW_FORM_block:
20213 case DW_FORM_exprloc:
20214 case DW_FORM_data16:
20216 struct dwarf_block *block = attr->as_block ();
20217 result = block->data;
20218 *len = block->size;
20220 break;
20222 /* The DW_AT_const_value attributes are supposed to carry the
20223 symbol's value "represented as it would be on the target
20224 architecture." By the time we get here, it's already been
20225 converted to host endianness, so we just need to sign- or
20226 zero-extend it as appropriate. */
20227 case DW_FORM_data1:
20228 type = die_type (die, cu);
20229 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20230 if (result == NULL)
20231 result = write_constant_as_bytes (obstack, byte_order,
20232 type, value, len);
20233 break;
20234 case DW_FORM_data2:
20235 type = die_type (die, cu);
20236 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20237 if (result == NULL)
20238 result = write_constant_as_bytes (obstack, byte_order,
20239 type, value, len);
20240 break;
20241 case DW_FORM_data4:
20242 type = die_type (die, cu);
20243 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20244 if (result == NULL)
20245 result = write_constant_as_bytes (obstack, byte_order,
20246 type, value, len);
20247 break;
20248 case DW_FORM_data8:
20249 type = die_type (die, cu);
20250 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20251 if (result == NULL)
20252 result = write_constant_as_bytes (obstack, byte_order,
20253 type, value, len);
20254 break;
20256 case DW_FORM_sdata:
20257 case DW_FORM_implicit_const:
20258 type = die_type (die, cu);
20259 result = write_constant_as_bytes (obstack, byte_order,
20260 type, attr->as_signed (), len);
20261 break;
20263 case DW_FORM_udata:
20264 type = die_type (die, cu);
20265 result = write_constant_as_bytes (obstack, byte_order,
20266 type, attr->as_unsigned (), len);
20267 break;
20269 default:
20270 complaint (_("unsupported const value attribute form: '%s'"),
20271 dwarf_form_name (attr->form));
20272 break;
20275 return result;
20278 /* See read.h. */
20280 struct type *
20281 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
20282 dwarf2_per_cu_data *per_cu,
20283 dwarf2_per_objfile *per_objfile,
20284 const char **var_name)
20286 struct die_info *die;
20288 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20289 if (cu == nullptr)
20290 cu = load_cu (per_cu, per_objfile, false);
20292 if (cu == nullptr)
20293 return nullptr;
20295 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
20296 if (!die)
20297 return NULL;
20299 if (var_name != nullptr)
20300 *var_name = var_decl_name (die, cu);
20301 return die_type (die, cu);
20304 /* See read.h. */
20306 struct type *
20307 dwarf2_get_die_type (cu_offset die_offset,
20308 dwarf2_per_cu_data *per_cu,
20309 dwarf2_per_objfile *per_objfile)
20311 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
20312 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
20315 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20316 On entry *REF_CU is the CU of SRC_DIE.
20317 On exit *REF_CU is the CU of the result.
20318 Returns NULL if the referenced DIE isn't found. */
20320 static struct die_info *
20321 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20322 struct dwarf2_cu **ref_cu)
20324 struct dwarf2_cu *sig_cu;
20325 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
20328 /* While it might be nice to assert sig_type->type == NULL here,
20329 we can get here for DW_AT_imported_declaration where we need
20330 the DIE not the type. */
20332 /* If necessary, add it to the queue and load its DIEs.
20334 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20335 it doesn't mean they are currently loaded. Since we require them
20336 to be loaded, we must check for ourselves. */
20337 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
20338 language_minimal)
20339 || per_objfile->get_cu (sig_type) == nullptr)
20340 read_signatured_type (sig_type, per_objfile);
20342 sig_cu = per_objfile->get_cu (sig_type);
20343 gdb_assert (sig_cu != NULL);
20344 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
20346 if (auto die_it = sig_cu->die_hash.find (sig_type->type_offset_in_section);
20347 die_it != sig_cu->die_hash.end ())
20349 /* For .gdb_index version 7 keep track of included TUs.
20350 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20351 if (per_objfile->per_bfd->index_table != NULL
20352 && !per_objfile->per_bfd->index_table->version_check ())
20353 (*ref_cu)->per_cu->imported_symtabs.push_back (sig_cu->per_cu);
20355 *ref_cu = sig_cu;
20356 return *die_it;
20359 return NULL;
20362 /* Follow signatured type referenced by ATTR in SRC_DIE.
20363 On entry *REF_CU is the CU of SRC_DIE.
20364 On exit *REF_CU is the CU of the result.
20365 The result is the DIE of the type.
20366 If the referenced type cannot be found an error is thrown. */
20368 static struct die_info *
20369 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20370 struct dwarf2_cu **ref_cu)
20372 ULONGEST signature = attr->as_signature ();
20373 struct signatured_type *sig_type;
20374 struct die_info *die;
20376 gdb_assert (attr->form == DW_FORM_ref_sig8);
20378 sig_type = lookup_signatured_type (*ref_cu, signature);
20379 /* sig_type will be NULL if the signatured type is missing from
20380 the debug info. */
20381 if (sig_type == NULL)
20383 error (_(DWARF_ERROR_PREFIX
20384 "Cannot find signatured DIE %s referenced from DIE at %s"
20385 " [in module %s]"),
20386 hex_string (signature), sect_offset_str (src_die->sect_off),
20387 objfile_name ((*ref_cu)->per_objfile->objfile));
20390 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20391 if (die == NULL)
20393 src_die->error_dump ();
20394 error (_(DWARF_ERROR_PREFIX
20395 "Problem reading signatured DIE %s referenced from DIE at %s"
20396 " [in module %s]"),
20397 hex_string (signature), sect_offset_str (src_die->sect_off),
20398 objfile_name ((*ref_cu)->per_objfile->objfile));
20401 return die;
20404 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20405 reading in and processing the type unit if necessary. */
20407 static struct type *
20408 get_signatured_type (struct die_info *die, ULONGEST signature,
20409 struct dwarf2_cu *cu)
20411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20412 struct signatured_type *sig_type;
20413 struct dwarf2_cu *type_cu;
20414 struct die_info *type_die;
20415 struct type *type;
20417 sig_type = lookup_signatured_type (cu, signature);
20418 /* sig_type will be NULL if the signatured type is missing from
20419 the debug info. */
20420 if (sig_type == NULL)
20422 complaint (_(DWARF_ERROR_PREFIX
20423 "Cannot find signatured DIE %s referenced from DIE at %s"
20424 " [in module %s]"),
20425 hex_string (signature), sect_offset_str (die->sect_off),
20426 objfile_name (per_objfile->objfile));
20427 return build_error_marker_type (cu, die);
20430 /* If we already know the type we're done. */
20431 type = per_objfile->get_type_for_signatured_type (sig_type);
20432 if (type != nullptr)
20433 return type;
20435 type_cu = cu;
20436 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20437 if (type_die != NULL)
20439 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20440 is created. This is important, for example, because for c++ classes
20441 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20442 type = read_type_die (type_die, type_cu);
20443 if (type == NULL)
20445 complaint (_(DWARF_ERROR_PREFIX
20446 "Cannot build signatured type %s"
20447 " referenced from DIE at %s [in module %s]"),
20448 hex_string (signature), sect_offset_str (die->sect_off),
20449 objfile_name (per_objfile->objfile));
20450 type = build_error_marker_type (cu, die);
20453 else
20455 complaint (_(DWARF_ERROR_PREFIX
20456 "Problem reading signatured DIE %s referenced"
20457 " from DIE at %s [in module %s]"),
20458 hex_string (signature), sect_offset_str (die->sect_off),
20459 objfile_name (per_objfile->objfile));
20460 type = build_error_marker_type (cu, die);
20463 per_objfile->set_type_for_signatured_type (sig_type, type);
20465 return type;
20468 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20469 reading in and processing the type unit if necessary. */
20471 static struct type *
20472 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20473 struct dwarf2_cu *cu) /* ARI: editCase function */
20475 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20476 if (attr->form_is_ref ())
20478 struct dwarf2_cu *type_cu = cu;
20479 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20481 return read_type_die (type_die, type_cu);
20483 else if (attr->form == DW_FORM_ref_sig8)
20485 return get_signatured_type (die, attr->as_signature (), cu);
20487 else
20489 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20491 complaint (_(DWARF_ERROR_PREFIX
20492 "DW_AT_signature has bad form %s in DIE at %s"
20493 " [in module %s]"),
20494 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
20495 objfile_name (per_objfile->objfile));
20496 return build_error_marker_type (cu, die);
20500 /* Load the DIEs associated with type unit PER_CU into memory. */
20502 static void
20503 load_full_type_unit (dwarf2_per_cu_data *per_cu,
20504 dwarf2_per_objfile *per_objfile)
20506 struct signatured_type *sig_type;
20508 /* We have the per_cu, but we need the signatured_type.
20509 Fortunately this is an easy translation. */
20510 gdb_assert (per_cu->is_debug_types);
20511 sig_type = (struct signatured_type *) per_cu;
20513 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
20515 read_signatured_type (sig_type, per_objfile);
20517 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
20520 /* Read in a signatured type and build its CU and DIEs.
20521 If the type is a stub for the real type in a DWO file,
20522 read in the real type from the DWO file as well. */
20524 static void
20525 read_signatured_type (signatured_type *sig_type,
20526 dwarf2_per_objfile *per_objfile)
20528 gdb_assert (sig_type->is_debug_types);
20529 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
20531 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
20533 if (!reader.dummy_p)
20535 struct dwarf2_cu *cu = reader.cu;
20536 const gdb_byte *info_ptr = reader.info_ptr;
20538 gdb_assert (cu->die_hash.empty ());
20539 cu->die_hash.reserve (cu->header.get_length_without_initial () / 12);
20541 if (reader.comp_unit_die->has_children)
20542 reader.comp_unit_die->child
20543 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
20544 reader.comp_unit_die);
20545 cu->dies = reader.comp_unit_die;
20546 /* comp_unit_die is not stored in die_hash, no need. */
20548 /* We try not to read any attributes in this function, because
20549 not all CUs needed for references have been loaded yet, and
20550 symbol table processing isn't initialized. But we have to
20551 set the CU language, or we won't be able to build types
20552 correctly. Similarly, if we do not read the producer, we can
20553 not apply producer-specific interpretation. */
20554 prepare_one_comp_unit (cu, cu->dies, language_minimal);
20556 reader.keep ();
20559 sig_type->tu_read = 1;
20562 /* Decode simple location descriptions.
20564 Given a pointer to a DWARF block that defines a location, compute
20565 the location. Returns true if the expression was computable by
20566 this function, false otherwise. On a true return, *RESULT is set.
20568 Note that this function does not implement a full DWARF expression
20569 evaluator. Instead, it is used for a few limited purposes:
20571 - Getting the address of a symbol that has a constant address. For
20572 example, if a symbol has a location like "DW_OP_addr", the address
20573 can be extracted.
20575 - Getting the offset of a virtual function in its vtable. There
20576 are two forms of this, one of which involves DW_OP_deref -- so this
20577 function handles derefs in a peculiar way to make this 'work'.
20578 (Probably this area should be rewritten.)
20580 - Getting the offset of a field, when it is constant.
20582 Opcodes that cannot be part of a constant expression, for example
20583 those involving registers, simply result in a return of
20584 'false'.
20586 This function may emit a complaint. */
20588 static bool
20589 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu,
20590 CORE_ADDR *result)
20592 struct objfile *objfile = cu->per_objfile->objfile;
20593 size_t i;
20594 size_t size = blk->size;
20595 const gdb_byte *data = blk->data;
20596 CORE_ADDR stack[64];
20597 int stacki;
20598 unsigned int bytes_read;
20599 gdb_byte op;
20601 *result = 0;
20602 i = 0;
20603 stacki = 0;
20604 stack[stacki] = 0;
20605 stack[++stacki] = 0;
20607 while (i < size)
20609 op = data[i++];
20610 switch (op)
20612 case DW_OP_lit0:
20613 case DW_OP_lit1:
20614 case DW_OP_lit2:
20615 case DW_OP_lit3:
20616 case DW_OP_lit4:
20617 case DW_OP_lit5:
20618 case DW_OP_lit6:
20619 case DW_OP_lit7:
20620 case DW_OP_lit8:
20621 case DW_OP_lit9:
20622 case DW_OP_lit10:
20623 case DW_OP_lit11:
20624 case DW_OP_lit12:
20625 case DW_OP_lit13:
20626 case DW_OP_lit14:
20627 case DW_OP_lit15:
20628 case DW_OP_lit16:
20629 case DW_OP_lit17:
20630 case DW_OP_lit18:
20631 case DW_OP_lit19:
20632 case DW_OP_lit20:
20633 case DW_OP_lit21:
20634 case DW_OP_lit22:
20635 case DW_OP_lit23:
20636 case DW_OP_lit24:
20637 case DW_OP_lit25:
20638 case DW_OP_lit26:
20639 case DW_OP_lit27:
20640 case DW_OP_lit28:
20641 case DW_OP_lit29:
20642 case DW_OP_lit30:
20643 case DW_OP_lit31:
20644 stack[++stacki] = op - DW_OP_lit0;
20645 break;
20647 case DW_OP_addr:
20648 stack[++stacki]
20649 = (CORE_ADDR) cu->header.read_address (objfile->obfd.get (),
20650 &data[i],
20651 &bytes_read);
20652 i += bytes_read;
20653 break;
20655 case DW_OP_const1u:
20656 stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
20657 i += 1;
20658 break;
20660 case DW_OP_const1s:
20661 stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
20662 i += 1;
20663 break;
20665 case DW_OP_const2u:
20666 stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
20667 i += 2;
20668 break;
20670 case DW_OP_const2s:
20671 stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
20672 i += 2;
20673 break;
20675 case DW_OP_const4u:
20676 stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
20677 i += 4;
20678 break;
20680 case DW_OP_const4s:
20681 stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
20682 i += 4;
20683 break;
20685 case DW_OP_const8u:
20686 stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
20687 i += 8;
20688 break;
20690 case DW_OP_constu:
20691 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
20692 &bytes_read);
20693 i += bytes_read;
20694 break;
20696 case DW_OP_consts:
20697 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
20698 i += bytes_read;
20699 break;
20701 case DW_OP_dup:
20702 stack[stacki + 1] = stack[stacki];
20703 stacki++;
20704 break;
20706 case DW_OP_plus:
20707 stack[stacki - 1] += stack[stacki];
20708 stacki--;
20709 break;
20711 case DW_OP_plus_uconst:
20712 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
20713 &bytes_read);
20714 i += bytes_read;
20715 break;
20717 case DW_OP_minus:
20718 stack[stacki - 1] -= stack[stacki];
20719 stacki--;
20720 break;
20722 case DW_OP_deref:
20723 /* If we're not the last op, then we definitely can't encode
20724 this using GDB's address_class enum. This is valid for partial
20725 global symbols, although the variable's address will be bogus
20726 in the psymtab. */
20727 if (i < size)
20728 return false;
20729 break;
20731 case DW_OP_addrx:
20732 case DW_OP_GNU_addr_index:
20733 case DW_OP_constx:
20734 case DW_OP_GNU_const_index:
20735 stack[++stacki]
20736 = (CORE_ADDR) read_addr_index_from_leb128 (cu, &data[i],
20737 &bytes_read);
20738 i += bytes_read;
20739 break;
20741 default:
20742 return false;
20745 /* Enforce maximum stack depth of SIZE-1 to avoid writing
20746 outside of the allocated space. Also enforce minimum>0. */
20747 if (stacki >= ARRAY_SIZE (stack) - 1)
20749 complaint (_("location description stack overflow"));
20750 return false;
20753 if (stacki <= 0)
20755 complaint (_("location description stack underflow"));
20756 return false;
20760 *result = stack[stacki];
20761 return true;
20764 /* memory allocation interface */
20766 static struct dwarf_block *
20767 dwarf_alloc_block (struct dwarf2_cu *cu)
20769 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
20774 /* Macro support. */
20776 /* An overload of dwarf_decode_macros that finds the correct section
20777 and ensures it is read in before calling the other overload. */
20779 static void
20780 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
20781 int section_is_gnu)
20783 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20784 struct objfile *objfile = per_objfile->objfile;
20785 const struct line_header *lh = cu->line_header;
20786 unsigned int offset_size = cu->header.offset_size;
20787 struct dwarf2_section_info *section;
20788 const char *section_name;
20790 if (cu->dwo_unit != nullptr)
20792 if (section_is_gnu)
20794 section = &cu->dwo_unit->dwo_file->sections.macro;
20795 section_name = ".debug_macro.dwo";
20797 else
20799 section = &cu->dwo_unit->dwo_file->sections.macinfo;
20800 section_name = ".debug_macinfo.dwo";
20803 else
20805 if (section_is_gnu)
20807 section = &per_objfile->per_bfd->macro;
20808 section_name = ".debug_macro";
20810 else
20812 section = &per_objfile->per_bfd->macinfo;
20813 section_name = ".debug_macinfo";
20817 section->read (objfile);
20818 if (section->buffer == nullptr)
20820 complaint (_("missing %s section"), section_name);
20821 return;
20824 buildsym_compunit *builder = cu->get_builder ();
20826 struct dwarf2_section_info *str_offsets_section;
20827 struct dwarf2_section_info *str_section;
20828 std::optional<ULONGEST> str_offsets_base;
20830 if (cu->dwo_unit != nullptr)
20832 str_offsets_section = &cu->dwo_unit->dwo_file
20833 ->sections.str_offsets;
20834 str_section = &cu->dwo_unit->dwo_file->sections.str;
20835 if (cu->header.version <= 4)
20836 str_offsets_base = 0;
20837 else
20839 bfd *abfd = str_offsets_section->get_bfd_owner ();
20840 unsigned int bytes_read = 0;
20841 read_initial_length (abfd, str_offsets_section->buffer, &bytes_read,
20842 false);
20843 const bool is_dwarf64 = bytes_read != 4;
20844 str_offsets_base = is_dwarf64 ? 16 : 8;
20847 else
20849 str_offsets_section = &per_objfile->per_bfd->str_offsets;
20850 str_section = &per_objfile->per_bfd->str;
20851 str_offsets_base = cu->str_offsets_base;
20854 dwarf_decode_macros (per_objfile, builder, section, lh,
20855 offset_size, offset, str_section, str_offsets_section,
20856 str_offsets_base, section_is_gnu, cu);
20859 /* Return the .debug_loc section to use for CU.
20860 For DWO files use .debug_loc.dwo. */
20862 static struct dwarf2_section_info *
20863 cu_debug_loc_section (struct dwarf2_cu *cu)
20865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20867 if (cu->dwo_unit)
20869 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
20871 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
20873 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
20874 : &per_objfile->per_bfd->loc);
20877 /* Return the .debug_rnglists section to use for CU. */
20878 static struct dwarf2_section_info *
20879 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
20881 if (cu->header.version < 5)
20882 error (_(".debug_rnglists section cannot be used in DWARF %d"),
20883 cu->header.version);
20884 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20886 /* Make sure we read the .debug_rnglists section from the file that
20887 contains the DW_AT_ranges attribute we are reading. Normally that
20888 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
20889 or DW_TAG_skeleton unit, we always want to read from objfile/linked
20890 program. */
20891 if (cu->dwo_unit != nullptr
20892 && tag != DW_TAG_compile_unit
20893 && tag != DW_TAG_skeleton_unit)
20895 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
20897 if (sections->rnglists.size > 0)
20898 return &sections->rnglists;
20899 else
20900 error (_(".debug_rnglists section is missing from .dwo file."));
20902 return &dwarf2_per_objfile->per_bfd->rnglists;
20905 /* A helper function that fills in a dwarf2_loclist_baton. */
20907 static void
20908 fill_in_loclist_baton (struct dwarf2_cu *cu,
20909 struct dwarf2_loclist_baton *baton,
20910 const struct attribute *attr)
20912 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20913 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20915 section->read (per_objfile->objfile);
20917 baton->per_objfile = per_objfile;
20918 baton->per_cu = cu->per_cu;
20919 gdb_assert (baton->per_cu);
20920 /* We don't know how long the location list is, but make sure we
20921 don't run off the edge of the section. */
20922 baton->size = section->size - attr->as_unsigned ();
20923 baton->data = section->buffer + attr->as_unsigned ();
20924 if (cu->base_address.has_value ())
20925 baton->base_address = *cu->base_address;
20926 else
20927 baton->base_address = {};
20928 baton->from_dwo = cu->dwo_unit != NULL;
20931 static void
20932 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
20933 struct dwarf2_cu *cu, int is_block)
20935 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20936 struct objfile *objfile = per_objfile->objfile;
20937 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20939 if (attr->form_is_section_offset ()
20940 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
20941 the section. If so, fall through to the complaint in the
20942 other branch. */
20943 && attr->as_unsigned () < section->get_size (objfile))
20945 struct dwarf2_loclist_baton *baton;
20947 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
20949 fill_in_loclist_baton (cu, baton, attr);
20951 if (!cu->base_address.has_value ())
20952 complaint (_("Location list used without "
20953 "specifying the CU base address."));
20955 sym->set_aclass_index ((is_block
20956 ? dwarf2_loclist_block_index
20957 : dwarf2_loclist_index));
20958 SYMBOL_LOCATION_BATON (sym) = baton;
20960 else
20962 struct dwarf2_locexpr_baton *baton;
20964 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
20965 baton->per_objfile = per_objfile;
20966 baton->per_cu = cu->per_cu;
20967 gdb_assert (baton->per_cu);
20969 if (attr->form_is_block ())
20971 /* Note that we're just copying the block's data pointer
20972 here, not the actual data. We're still pointing into the
20973 info_buffer for SYM's objfile; right now we never release
20974 that buffer, but when we do clean up properly this may
20975 need to change. */
20976 struct dwarf_block *block = attr->as_block ();
20977 baton->size = block->size;
20978 baton->data = block->data;
20980 else
20982 dwarf2_invalid_attrib_class_complaint ("location description",
20983 sym->natural_name ());
20984 baton->size = 0;
20987 sym->set_aclass_index ((is_block
20988 ? dwarf2_locexpr_block_index
20989 : dwarf2_locexpr_index));
20990 SYMBOL_LOCATION_BATON (sym) = baton;
20994 /* See read.h. */
20996 const comp_unit_head *
20997 dwarf2_per_cu_data::get_header () const
20999 if (!m_header_read_in)
21001 const gdb_byte *info_ptr
21002 = this->section->buffer + to_underlying (this->sect_off);
21004 read_comp_unit_head (&m_header, info_ptr, this->section,
21005 rcuh_kind::COMPILE);
21007 m_header_read_in = true;
21010 return &m_header;
21013 /* See read.h. */
21016 dwarf2_per_cu_data::addr_size () const
21018 return this->get_header ()->addr_size;
21021 /* See read.h. */
21024 dwarf2_per_cu_data::offset_size () const
21026 return this->get_header ()->offset_size;
21029 /* See read.h. */
21032 dwarf2_per_cu_data::ref_addr_size () const
21034 const comp_unit_head *header = this->get_header ();
21036 if (header->version == 2)
21037 return header->addr_size;
21038 else
21039 return header->offset_size;
21042 /* See read.h. */
21044 void
21045 dwarf2_per_cu_data::set_lang (enum language lang,
21046 dwarf_source_language dw_lang)
21048 if (unit_type () == DW_UT_partial)
21049 return;
21051 /* Set if not set already. */
21052 packed<language, LANGUAGE_BYTES> new_value = lang;
21053 packed<language, LANGUAGE_BYTES> old_value = m_lang.exchange (new_value);
21054 /* If already set, verify that it's the same value. */
21055 gdb_assert (old_value == language_unknown || old_value == lang);
21057 packed<dwarf_source_language, 2> new_dw = dw_lang;
21058 packed<dwarf_source_language, 2> old_dw = m_dw_lang.exchange (new_dw);
21059 gdb_assert (old_dw == 0 || old_dw == dw_lang);
21062 /* See read.h. */
21064 void
21065 dwarf2_per_cu_data::ensure_lang (dwarf2_per_objfile *per_objfile)
21067 if (lang (false) != language_unknown)
21068 return;
21070 cutu_reader reader (this, per_objfile);
21071 if (reader.dummy_p)
21073 set_lang (language_minimal, (dwarf_source_language)0);
21074 return;
21077 prepare_one_comp_unit (reader.cu, reader.comp_unit_die, language_minimal);
21080 /* A helper function for dwarf2_find_containing_comp_unit that returns
21081 the index of the result, and that searches a vector. It will
21082 return a result even if the offset in question does not actually
21083 occur in any CU. This is separate so that it can be unit
21084 tested. */
21086 static int
21087 dwarf2_find_containing_comp_unit
21088 (sect_offset sect_off,
21089 unsigned int offset_in_dwz,
21090 const std::vector<dwarf2_per_cu_data_up> &all_units)
21092 int low, high;
21094 low = 0;
21095 high = all_units.size () - 1;
21096 while (high > low)
21098 struct dwarf2_per_cu_data *mid_cu;
21099 int mid = low + (high - low) / 2;
21101 mid_cu = all_units[mid].get ();
21102 if (mid_cu->is_dwz > offset_in_dwz
21103 || (mid_cu->is_dwz == offset_in_dwz
21104 && mid_cu->sect_off + mid_cu->length () > sect_off))
21105 high = mid;
21106 else
21107 low = mid + 1;
21109 gdb_assert (low == high);
21110 return low;
21113 /* Locate the .debug_info compilation unit from CU's objfile which contains
21114 the DIE at OFFSET. Raises an error on failure. */
21116 static struct dwarf2_per_cu_data *
21117 dwarf2_find_containing_comp_unit (sect_offset sect_off,
21118 unsigned int offset_in_dwz,
21119 dwarf2_per_bfd *per_bfd)
21121 int low = dwarf2_find_containing_comp_unit
21122 (sect_off, offset_in_dwz, per_bfd->all_units);
21123 dwarf2_per_cu_data *this_cu = per_bfd->all_units[low].get ();
21125 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
21127 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
21128 error (_(DWARF_ERROR_PREFIX
21129 "could not find CU containing offset %s [in module %s]"),
21130 sect_offset_str (sect_off),
21131 bfd_get_filename (per_bfd->obfd));
21133 gdb_assert (per_bfd->all_units[low-1]->sect_off
21134 <= sect_off);
21135 return per_bfd->all_units[low - 1].get ();
21137 else
21139 if (low == per_bfd->all_units.size () - 1
21140 && sect_off >= this_cu->sect_off + this_cu->length ())
21141 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
21142 gdb_assert (sect_off < this_cu->sect_off + this_cu->length ());
21143 return this_cu;
21147 #if GDB_SELF_TEST
21149 namespace selftests {
21150 namespace find_containing_comp_unit {
21152 static void
21153 run_test ()
21155 dwarf2_per_cu_data_up one (new dwarf2_per_cu_data);
21156 dwarf2_per_cu_data *one_ptr = one.get ();
21157 dwarf2_per_cu_data_up two (new dwarf2_per_cu_data);
21158 dwarf2_per_cu_data *two_ptr = two.get ();
21159 dwarf2_per_cu_data_up three (new dwarf2_per_cu_data);
21160 dwarf2_per_cu_data *three_ptr = three.get ();
21161 dwarf2_per_cu_data_up four (new dwarf2_per_cu_data);
21162 dwarf2_per_cu_data *four_ptr = four.get ();
21164 one->set_length (5);
21165 two->sect_off = sect_offset (one->length ());
21166 two->set_length (7);
21168 three->set_length (5);
21169 three->is_dwz = 1;
21170 four->sect_off = sect_offset (three->length ());
21171 four->set_length (7);
21172 four->is_dwz = 1;
21174 std::vector<dwarf2_per_cu_data_up> units;
21175 units.push_back (std::move (one));
21176 units.push_back (std::move (two));
21177 units.push_back (std::move (three));
21178 units.push_back (std::move (four));
21180 int result;
21182 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
21183 SELF_CHECK (units[result].get () == one_ptr);
21184 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
21185 SELF_CHECK (units[result].get () == one_ptr);
21186 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
21187 SELF_CHECK (units[result].get () == two_ptr);
21189 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
21190 SELF_CHECK (units[result].get () == three_ptr);
21191 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
21192 SELF_CHECK (units[result].get () == three_ptr);
21193 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
21194 SELF_CHECK (units[result].get () == four_ptr);
21200 #endif /* GDB_SELF_TEST */
21202 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21204 static void
21205 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
21206 enum language pretend_language)
21208 struct attribute *attr;
21210 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
21212 /* Set the language we're debugging. */
21213 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
21214 enum language lang;
21215 dwarf_source_language dw_lang = (dwarf_source_language) 0;
21216 if (cu->producer != nullptr
21217 && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
21219 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21220 attribute is not standardised yet. As a workaround for the
21221 language detection we fall back to the DW_AT_producer
21222 string. */
21223 lang = language_opencl;
21224 dw_lang = DW_LANG_OpenCL;
21226 else if (cu->producer != nullptr
21227 && strstr (cu->producer, "GNU Go ") != NULL)
21229 /* Similar hack for Go. */
21230 lang = language_go;
21231 dw_lang = DW_LANG_Go;
21233 else if (attr != nullptr)
21235 lang = dwarf_lang_to_enum_language (attr->constant_value (0));
21236 dw_lang = (dwarf_source_language) attr->constant_value (0);
21238 else
21239 lang = pretend_language;
21241 cu->language_defn = language_def (lang);
21243 /* Initialize the lto_artificial field. */
21244 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
21245 if (attr != nullptr
21246 && cu->producer != nullptr
21247 && attr->as_string () != nullptr
21248 && strcmp (attr->as_string (), "<artificial>") == 0
21249 && producer_is_gcc (cu->producer, nullptr, nullptr))
21250 cu->per_cu->lto_artificial = true;
21252 switch (comp_unit_die->tag)
21254 case DW_TAG_compile_unit:
21255 cu->per_cu->set_unit_type (DW_UT_compile);
21256 break;
21257 case DW_TAG_partial_unit:
21258 cu->per_cu->set_unit_type (DW_UT_partial);
21259 break;
21260 case DW_TAG_type_unit:
21261 cu->per_cu->set_unit_type (DW_UT_type);
21262 break;
21263 default:
21264 error (_(DWARF_ERROR_PREFIX
21265 "unexpected tag '%s' at offset %s"),
21266 dwarf_tag_name (comp_unit_die->tag),
21267 sect_offset_str (cu->per_cu->sect_off));
21270 cu->per_cu->set_lang (lang, dw_lang);
21273 /* See read.h. */
21275 dwarf2_cu *
21276 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
21278 auto it = m_dwarf2_cus.find (per_cu);
21279 if (it == m_dwarf2_cus.end ())
21280 return nullptr;
21282 return it->second.get ();
21285 /* See read.h. */
21287 void
21288 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu,
21289 std::unique_ptr<dwarf2_cu> cu)
21291 gdb_assert (this->get_cu (per_cu) == nullptr);
21293 m_dwarf2_cus[per_cu] = std::move (cu);
21296 /* See read.h. */
21298 void
21299 dwarf2_per_objfile::age_comp_units ()
21301 dwarf_read_debug_printf_v ("running");
21303 /* This is not expected to be called in the middle of CU expansion. There is
21304 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21305 loaded in memory. Calling age_comp_units while the queue is in use could
21306 make us free the DIEs for a CU that is in the queue and therefore break
21307 that invariant. */
21308 gdb_assert (!queue.has_value ());
21310 /* Start by clearing all marks. */
21311 for (const auto &pair : m_dwarf2_cus)
21312 pair.second->clear_mark ();
21314 /* Traverse all CUs, mark them and their dependencies if used recently
21315 enough. */
21316 for (const auto &pair : m_dwarf2_cus)
21318 dwarf2_cu *cu = pair.second.get ();
21320 cu->last_used++;
21321 if (cu->last_used <= dwarf_max_cache_age)
21322 cu->mark ();
21325 /* Delete all CUs still not marked. */
21326 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
21328 dwarf2_cu *cu = it->second.get ();
21330 if (!cu->is_marked ())
21332 dwarf_read_debug_printf_v ("deleting old CU %s",
21333 sect_offset_str (cu->per_cu->sect_off));
21334 it = m_dwarf2_cus.erase (it);
21336 else
21337 it++;
21341 /* See read.h. */
21343 void
21344 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
21346 auto it = m_dwarf2_cus.find (per_cu);
21347 if (it == m_dwarf2_cus.end ())
21348 return;
21350 m_dwarf2_cus.erase (it);
21353 dwarf2_per_objfile::~dwarf2_per_objfile ()
21355 remove_all_cus ();
21358 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21359 table if necessary. For convenience, return TYPE.
21361 The DIEs reading must have careful ordering to:
21362 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21363 reading current DIE.
21364 * Not trying to dereference contents of still incompletely read in types
21365 while reading in other DIEs.
21366 * Enable referencing still incompletely read in types just by a pointer to
21367 the type without accessing its fields.
21369 Therefore caller should follow these rules:
21370 * Try to fetch any prerequisite types we may need to build this DIE type
21371 before building the type and calling set_die_type.
21372 * After building type call set_die_type for current DIE as soon as
21373 possible before fetching more types to complete the current type.
21374 * Make the type as complete as possible before fetching more types. */
21376 static struct type *
21377 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21378 bool skip_data_location)
21380 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21381 struct attribute *attr;
21382 struct dynamic_prop prop;
21384 /* For Ada types, make sure that the gnat-specific data is always
21385 initialized (if not already set). There are a few types where
21386 we should not be doing so, because the type-specific area is
21387 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21388 where the type-specific area is used to store the floatformat).
21389 But this is not a problem, because the gnat-specific information
21390 is actually not needed for these types. */
21391 if (need_gnat_info (cu)
21392 && type->code () != TYPE_CODE_FUNC
21393 && type->code () != TYPE_CODE_FLT
21394 && type->code () != TYPE_CODE_METHODPTR
21395 && type->code () != TYPE_CODE_MEMBERPTR
21396 && type->code () != TYPE_CODE_METHOD
21397 && type->code () != TYPE_CODE_FIXED_POINT
21398 && !HAVE_GNAT_AUX_INFO (type))
21399 INIT_GNAT_SPECIFIC (type);
21401 /* Read DW_AT_allocated and set in type. */
21402 attr = dwarf2_attr (die, DW_AT_allocated, cu);
21403 if (attr != NULL)
21405 struct type *prop_type = cu->addr_sized_int_type (false);
21406 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21407 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
21410 /* Read DW_AT_associated and set in type. */
21411 attr = dwarf2_attr (die, DW_AT_associated, cu);
21412 if (attr != NULL)
21414 struct type *prop_type = cu->addr_sized_int_type (false);
21415 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21416 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
21419 /* Read DW_AT_rank and set in type. */
21420 attr = dwarf2_attr (die, DW_AT_rank, cu);
21421 if (attr != NULL)
21423 struct type *prop_type = cu->addr_sized_int_type (false);
21424 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
21425 type->add_dyn_prop (DYN_PROP_RANK, prop);
21428 /* Read DW_AT_data_location and set in type. */
21429 if (!skip_data_location)
21431 attr = dwarf2_attr (die, DW_AT_data_location, cu);
21432 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
21433 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
21436 bool inserted
21437 = per_objfile->die_type_hash.emplace
21438 (per_cu_and_offset {cu->per_cu, die->sect_off}, type).second;
21439 if (!inserted)
21440 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21441 sect_offset_str (die->sect_off));
21443 return type;
21446 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21447 or return NULL if the die does not have a saved type. */
21449 static struct type *
21450 get_die_type_at_offset (sect_offset sect_off,
21451 dwarf2_per_cu_data *per_cu,
21452 dwarf2_per_objfile *per_objfile)
21454 auto it = per_objfile->die_type_hash.find ({per_cu, sect_off});
21456 return it != per_objfile->die_type_hash.end () ? it->second : nullptr;
21459 /* Look up the type for DIE in CU in die_type_hash,
21460 or return NULL if DIE does not have a saved type. */
21462 static struct type *
21463 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21465 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
21468 struct cmd_list_element *set_dwarf_cmdlist;
21469 struct cmd_list_element *show_dwarf_cmdlist;
21471 static void
21472 show_check_physname (struct ui_file *file, int from_tty,
21473 struct cmd_list_element *c, const char *value)
21475 gdb_printf (file,
21476 _("Whether to check \"physname\" is %s.\n"),
21477 value);
21480 void _initialize_dwarf2_read ();
21481 void
21482 _initialize_dwarf2_read ()
21484 add_setshow_prefix_cmd ("dwarf", class_maintenance,
21485 _("\
21486 Set DWARF specific variables.\n\
21487 Configure DWARF variables such as the cache size."),
21488 _("\
21489 Show DWARF specific variables.\n\
21490 Show DWARF variables such as the cache size."),
21491 &set_dwarf_cmdlist, &show_dwarf_cmdlist,
21492 &maintenance_set_cmdlist, &maintenance_show_cmdlist);
21494 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21495 &dwarf_max_cache_age, _("\
21496 Set the upper bound on the age of cached DWARF compilation units."), _("\
21497 Show the upper bound on the age of cached DWARF compilation units."), _("\
21498 A higher limit means that cached compilation units will be stored\n\
21499 in memory longer, and more total memory will be used. Zero disables\n\
21500 caching, which can slow down startup."),
21501 NULL,
21502 show_dwarf_max_cache_age,
21503 &set_dwarf_cmdlist,
21504 &show_dwarf_cmdlist);
21506 add_setshow_boolean_cmd ("synchronous", class_obscure,
21507 &dwarf_synchronous, _("\
21508 Set whether DWARF is read synchronously."), _("\
21509 Show whether DWARF is read synchronously."), _("\
21510 By default, DWARF information is read in worker threads,\n\
21511 and gdb will not generally wait for the reading to complete\n\
21512 before continuing with other work, for example presenting a\n\
21513 prompt to the user.\n\
21514 Enabling this setting will cause the DWARF reader to always wait\n\
21515 for debug info processing to be finished before gdb can proceed."),
21516 nullptr,
21517 show_dwarf_synchronous,
21518 &set_dwarf_cmdlist,
21519 &show_dwarf_cmdlist);
21521 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
21522 Set debugging of the DWARF reader."), _("\
21523 Show debugging of the DWARF reader."), _("\
21524 When enabled (non-zero), debugging messages are printed during DWARF\n\
21525 reading and symtab expansion. A value of 1 (one) provides basic\n\
21526 information. A value greater than 1 provides more verbose information."),
21527 NULL,
21528 NULL,
21529 &setdebuglist, &showdebuglist);
21531 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
21532 Set debugging of the DWARF DIE reader."), _("\
21533 Show debugging of the DWARF DIE reader."), _("\
21534 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21535 The value is the maximum depth to print."),
21536 NULL,
21537 NULL,
21538 &setdebuglist, &showdebuglist);
21540 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
21541 Set debugging of the dwarf line reader."), _("\
21542 Show debugging of the dwarf line reader."), _("\
21543 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21544 A value of 1 (one) provides basic information.\n\
21545 A value greater than 1 provides more verbose information."),
21546 NULL,
21547 NULL,
21548 &setdebuglist, &showdebuglist);
21550 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21551 Set cross-checking of \"physname\" code against demangler."), _("\
21552 Show cross-checking of \"physname\" code against demangler."), _("\
21553 When enabled, GDB's internal \"physname\" code is checked against\n\
21554 the demangler."),
21555 NULL, show_check_physname,
21556 &setdebuglist, &showdebuglist);
21558 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21559 &dwarf2_locexpr_funcs);
21560 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21561 &dwarf2_loclist_funcs);
21562 ada_imported_index = register_symbol_computed_impl (LOC_COMPUTED,
21563 &ada_imported_funcs);
21565 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21566 &dwarf2_block_frame_base_locexpr_funcs);
21567 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21568 &dwarf2_block_frame_base_loclist_funcs);
21569 ada_block_index = register_symbol_block_impl (LOC_BLOCK,
21570 &ada_function_alias_funcs);
21572 #if GDB_SELF_TEST
21573 selftests::register_test ("dwarf2_find_containing_comp_unit",
21574 selftests::find_containing_comp_unit::run_test);
21575 #endif