1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2023 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
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.
29 E.g., load_partial_dies, read_partial_die. */
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/aranges.h"
35 #include "dwarf2/attribute.h"
36 #include "dwarf2/comp-unit-head.h"
37 #include "dwarf2/cu.h"
38 #include "dwarf2/index-cache.h"
39 #include "dwarf2/index-common.h"
40 #include "dwarf2/leb.h"
41 #include "dwarf2/line-header.h"
42 #include "dwarf2/dwz.h"
43 #include "dwarf2/macro.h"
44 #include "dwarf2/die.h"
45 #include "dwarf2/read-debug-names.h"
46 #include "dwarf2/read-gdb-index.h"
47 #include "dwarf2/sect-names.h"
48 #include "dwarf2/stringify.h"
49 #include "dwarf2/public.h"
57 #include "gdb-demangle.h"
58 #include "filenames.h"
60 #include "complaints.h"
61 #include "dwarf2/expr.h"
62 #include "dwarf2/loc.h"
63 #include "cp-support.h"
69 #include "typeprint.h"
74 #include "gdb/gdb-index.h"
79 #include "namespace.h"
80 #include "gdbsupport/function-view.h"
82 #include "gdbsupport/underlying.h"
83 #include "gdbsupport/hash_enum.h"
84 #include "filename-seen-cache.h"
88 #include <unordered_map>
89 #include "gdbsupport/selftest.h"
90 #include "rust-lang.h"
91 #include "gdbsupport/pathstuff.h"
92 #include "count-one-bits.h"
93 #include <unordered_set>
94 #include "dwarf2/abbrev-cache.h"
95 #include "cooked-index.h"
96 #include "split-name.h"
97 #include "gdbsupport/thread-pool.h"
98 #include "run-on-main-thread.h"
100 /* When == 1, print basic high level tracing messages.
101 When > 1, be more verbose.
102 This is in contrast to the low level DIE reading of dwarf_die_debug. */
103 static unsigned int dwarf_read_debug
= 0;
105 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
107 #define dwarf_read_debug_printf(fmt, ...) \
108 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
111 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
113 #define dwarf_read_debug_printf_v(fmt, ...) \
114 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
117 /* When non-zero, dump DIEs after they are read in. */
118 static unsigned int dwarf_die_debug
= 0;
120 /* When non-zero, dump line number entries as they are read in. */
121 unsigned int dwarf_line_debug
= 0;
123 /* When true, cross-check physname against demangler. */
124 static bool check_physname
= false;
126 /* This is used to store the data that is always per objfile. */
127 static const registry
<objfile
>::key
<dwarf2_per_objfile
>
128 dwarf2_objfile_data_key
;
130 /* These are used to store the dwarf2_per_bfd objects.
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
138 static const registry
<bfd
>::key
<dwarf2_per_bfd
> dwarf2_per_bfd_bfd_data_key
;
139 static const registry
<objfile
>::key
<dwarf2_per_bfd
>
140 dwarf2_per_bfd_objfile_data_key
;
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
144 static int dwarf2_locexpr_index
;
145 static int dwarf2_loclist_index
;
146 static int ada_imported_index
;
147 static int dwarf2_locexpr_block_index
;
148 static int dwarf2_loclist_block_index
;
149 static int ada_block_index
;
151 static bool producer_is_gas_lt_2_38 (struct dwarf2_cu
*cu
);
153 /* Size of .debug_loclists section header for 32-bit DWARF format. */
154 #define LOCLIST_HEADER_SIZE32 12
156 /* Size of .debug_loclists section header for 64-bit DWARF format. */
157 #define LOCLIST_HEADER_SIZE64 20
159 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
160 #define RNGLIST_HEADER_SIZE32 12
162 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
163 #define RNGLIST_HEADER_SIZE64 20
165 /* See dwarf2/read.h. */
168 get_dwarf2_per_objfile (struct objfile
*objfile
)
170 return dwarf2_objfile_data_key
.get (objfile
);
173 /* Default names of the debugging sections. */
175 /* Note that if the debugging section has been compressed, it might
176 have a name like .zdebug_info. */
178 const struct dwarf2_debug_sections dwarf2_elf_names
=
180 { ".debug_info", ".zdebug_info" },
181 { ".debug_abbrev", ".zdebug_abbrev" },
182 { ".debug_line", ".zdebug_line" },
183 { ".debug_loc", ".zdebug_loc" },
184 { ".debug_loclists", ".zdebug_loclists" },
185 { ".debug_macinfo", ".zdebug_macinfo" },
186 { ".debug_macro", ".zdebug_macro" },
187 { ".debug_str", ".zdebug_str" },
188 { ".debug_str_offsets", ".zdebug_str_offsets" },
189 { ".debug_line_str", ".zdebug_line_str" },
190 { ".debug_ranges", ".zdebug_ranges" },
191 { ".debug_rnglists", ".zdebug_rnglists" },
192 { ".debug_types", ".zdebug_types" },
193 { ".debug_addr", ".zdebug_addr" },
194 { ".debug_frame", ".zdebug_frame" },
195 { ".eh_frame", NULL
},
196 { ".gdb_index", ".zgdb_index" },
197 { ".debug_names", ".zdebug_names" },
198 { ".debug_aranges", ".zdebug_aranges" },
202 /* List of DWO/DWP sections. */
204 static const struct dwop_section_names
206 struct dwarf2_section_names abbrev_dwo
;
207 struct dwarf2_section_names info_dwo
;
208 struct dwarf2_section_names line_dwo
;
209 struct dwarf2_section_names loc_dwo
;
210 struct dwarf2_section_names loclists_dwo
;
211 struct dwarf2_section_names macinfo_dwo
;
212 struct dwarf2_section_names macro_dwo
;
213 struct dwarf2_section_names rnglists_dwo
;
214 struct dwarf2_section_names str_dwo
;
215 struct dwarf2_section_names str_offsets_dwo
;
216 struct dwarf2_section_names types_dwo
;
217 struct dwarf2_section_names cu_index
;
218 struct dwarf2_section_names tu_index
;
222 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
223 { ".debug_info.dwo", ".zdebug_info.dwo" },
224 { ".debug_line.dwo", ".zdebug_line.dwo" },
225 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
226 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
227 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
228 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
229 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
230 { ".debug_str.dwo", ".zdebug_str.dwo" },
231 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
232 { ".debug_types.dwo", ".zdebug_types.dwo" },
233 { ".debug_cu_index", ".zdebug_cu_index" },
234 { ".debug_tu_index", ".zdebug_tu_index" },
237 /* local data types */
239 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
240 begin with a header, which contains the following information. */
241 struct loclists_rnglists_header
243 /* A 4-byte or 12-byte length containing the length of the
244 set of entries for this compilation unit, not including the
245 length field itself. */
248 /* A 2-byte version identifier. */
251 /* A 1-byte unsigned integer containing the size in bytes of an address on
252 the target system. */
253 unsigned char addr_size
;
255 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
256 on the target system. */
257 unsigned char segment_collector_size
;
259 /* A 4-byte count of the number of offsets that follow the header. */
260 unsigned int offset_entry_count
;
263 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
264 This includes type_unit_group and quick_file_names. */
266 struct stmt_list_hash
268 /* The DWO unit this table is from or NULL if there is none. */
269 struct dwo_unit
*dwo_unit
;
271 /* Offset in .debug_line or .debug_line.dwo. */
272 sect_offset line_sect_off
;
275 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
276 an object of this type. This contains elements of type unit groups
277 that can be shared across objfiles. The non-shareable parts are in
278 type_unit_group_unshareable. */
280 struct type_unit_group
282 /* The data used to construct the hash key. */
283 struct stmt_list_hash hash
{};
286 /* These sections are what may appear in a (real or virtual) DWO file. */
290 struct dwarf2_section_info abbrev
;
291 struct dwarf2_section_info line
;
292 struct dwarf2_section_info loc
;
293 struct dwarf2_section_info loclists
;
294 struct dwarf2_section_info macinfo
;
295 struct dwarf2_section_info macro
;
296 struct dwarf2_section_info rnglists
;
297 struct dwarf2_section_info str
;
298 struct dwarf2_section_info str_offsets
;
299 /* In the case of a virtual DWO file, these two are unused. */
300 struct dwarf2_section_info info
;
301 std::vector
<dwarf2_section_info
> types
;
304 /* CUs/TUs in DWP/DWO files. */
308 /* Backlink to the containing struct dwo_file. */
309 struct dwo_file
*dwo_file
;
311 /* The "id" that distinguishes this CU/TU.
312 .debug_info calls this "dwo_id", .debug_types calls this "signature".
313 Since signatures came first, we stick with it for consistency. */
316 /* The section this CU/TU lives in, in the DWO file. */
317 struct dwarf2_section_info
*section
;
319 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
320 sect_offset sect_off
;
323 /* For types, offset in the type's DIE of the type defined by this TU. */
324 cu_offset type_offset_in_tu
;
327 /* include/dwarf2.h defines the DWP section codes.
328 It defines a max value but it doesn't define a min value, which we
329 use for error checking, so provide one. */
331 enum dwp_v2_section_ids
336 /* Data for one DWO file.
338 This includes virtual DWO files (a virtual DWO file is a DWO file as it
339 appears in a DWP file). DWP files don't really have DWO files per se -
340 comdat folding of types "loses" the DWO file they came from, and from
341 a high level view DWP files appear to contain a mass of random types.
342 However, to maintain consistency with the non-DWP case we pretend DWP
343 files contain virtual DWO files, and we assign each TU with one virtual
344 DWO file (generally based on the line and abbrev section offsets -
345 a heuristic that seems to work in practice). */
349 dwo_file () = default;
350 DISABLE_COPY_AND_ASSIGN (dwo_file
);
352 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
353 For virtual DWO files the name is constructed from the section offsets
354 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
355 from related CU+TUs. */
356 std::string dwo_name
;
358 /* The DW_AT_comp_dir attribute. */
359 const char *comp_dir
= nullptr;
361 /* The bfd, when the file is open. Otherwise this is NULL.
362 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
363 gdb_bfd_ref_ptr dbfd
;
365 /* The sections that make up this DWO file.
366 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
367 sections (for lack of a better name). */
368 struct dwo_sections sections
{};
370 /* The CUs in the file.
371 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
372 an extension to handle LLVM's Link Time Optimization output (where
373 multiple source files may be compiled into a single object/dwo pair). */
376 /* Table of TUs in the file.
377 Each element is a struct dwo_unit. */
381 /* These sections are what may appear in a DWP file. */
385 /* These are used by all DWP versions (1, 2 and 5). */
386 struct dwarf2_section_info str
;
387 struct dwarf2_section_info cu_index
;
388 struct dwarf2_section_info tu_index
;
390 /* These are only used by DWP version 2 and version 5 files.
391 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
392 sections are referenced by section number, and are not recorded here.
393 In DWP version 2 or 5 there is at most one copy of all these sections,
394 each section being (effectively) comprised of the concatenation of all of
395 the individual sections that exist in the version 1 format.
396 To keep the code simple we treat each of these concatenated pieces as a
397 section itself (a virtual section?). */
398 struct dwarf2_section_info abbrev
;
399 struct dwarf2_section_info info
;
400 struct dwarf2_section_info line
;
401 struct dwarf2_section_info loc
;
402 struct dwarf2_section_info loclists
;
403 struct dwarf2_section_info macinfo
;
404 struct dwarf2_section_info macro
;
405 struct dwarf2_section_info rnglists
;
406 struct dwarf2_section_info str_offsets
;
407 struct dwarf2_section_info types
;
410 /* These sections are what may appear in a virtual DWO file in DWP version 1.
411 A virtual DWO file is a DWO file as it appears in a DWP file. */
413 struct virtual_v1_dwo_sections
415 struct dwarf2_section_info abbrev
;
416 struct dwarf2_section_info line
;
417 struct dwarf2_section_info loc
;
418 struct dwarf2_section_info macinfo
;
419 struct dwarf2_section_info macro
;
420 struct dwarf2_section_info str_offsets
;
421 /* Each DWP hash table entry records one CU or one TU.
422 That is recorded here, and copied to dwo_unit.section. */
423 struct dwarf2_section_info info_or_types
;
426 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
427 In version 2, the sections of the DWO files are concatenated together
428 and stored in one section of that name. Thus each ELF section contains
429 several "virtual" sections. */
431 struct virtual_v2_or_v5_dwo_sections
433 bfd_size_type abbrev_offset
;
434 bfd_size_type abbrev_size
;
436 bfd_size_type line_offset
;
437 bfd_size_type line_size
;
439 bfd_size_type loc_offset
;
440 bfd_size_type loc_size
;
442 bfd_size_type loclists_offset
;
443 bfd_size_type loclists_size
;
445 bfd_size_type macinfo_offset
;
446 bfd_size_type macinfo_size
;
448 bfd_size_type macro_offset
;
449 bfd_size_type macro_size
;
451 bfd_size_type rnglists_offset
;
452 bfd_size_type rnglists_size
;
454 bfd_size_type str_offsets_offset
;
455 bfd_size_type str_offsets_size
;
457 /* Each DWP hash table entry records one CU or one TU.
458 That is recorded here, and copied to dwo_unit.section. */
459 bfd_size_type info_or_types_offset
;
460 bfd_size_type info_or_types_size
;
463 /* Contents of DWP hash tables. */
465 struct dwp_hash_table
467 uint32_t version
, nr_columns
;
468 uint32_t nr_units
, nr_slots
;
469 const gdb_byte
*hash_table
, *unit_table
;
474 const gdb_byte
*indices
;
478 /* This is indexed by column number and gives the id of the section
480 #define MAX_NR_V2_DWO_SECTIONS \
481 (1 /* .debug_info or .debug_types */ \
482 + 1 /* .debug_abbrev */ \
483 + 1 /* .debug_line */ \
484 + 1 /* .debug_loc */ \
485 + 1 /* .debug_str_offsets */ \
486 + 1 /* .debug_macro or .debug_macinfo */)
487 int section_ids
[MAX_NR_V2_DWO_SECTIONS
];
488 const gdb_byte
*offsets
;
489 const gdb_byte
*sizes
;
493 /* This is indexed by column number and gives the id of the section
495 #define MAX_NR_V5_DWO_SECTIONS \
496 (1 /* .debug_info */ \
497 + 1 /* .debug_abbrev */ \
498 + 1 /* .debug_line */ \
499 + 1 /* .debug_loclists */ \
500 + 1 /* .debug_str_offsets */ \
501 + 1 /* .debug_macro */ \
502 + 1 /* .debug_rnglists */)
503 int section_ids
[MAX_NR_V5_DWO_SECTIONS
];
504 const gdb_byte
*offsets
;
505 const gdb_byte
*sizes
;
510 /* Data for one DWP file. */
514 dwp_file (const char *name_
, gdb_bfd_ref_ptr
&&abfd
)
516 dbfd (std::move (abfd
))
520 /* Name of the file. */
523 /* File format version. */
527 gdb_bfd_ref_ptr dbfd
;
529 /* Section info for this file. */
530 struct dwp_sections sections
{};
532 /* Table of CUs in the file. */
533 const struct dwp_hash_table
*cus
= nullptr;
535 /* Table of TUs in the file. */
536 const struct dwp_hash_table
*tus
= nullptr;
538 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
542 /* Table to map ELF section numbers to their sections.
543 This is only needed for the DWP V1 file format. */
544 unsigned int num_sections
= 0;
545 asection
**elf_sections
= nullptr;
548 /* Struct used to pass misc. parameters to read_die_and_children, et
549 al. which are used for both .debug_info and .debug_types dies.
550 All parameters here are unchanging for the life of the call. This
551 struct exists to abstract away the constant parameters of die reading. */
553 struct die_reader_specs
555 /* The bfd of die_section. */
558 /* The CU of the DIE we are parsing. */
559 struct dwarf2_cu
*cu
;
561 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
562 struct dwo_file
*dwo_file
;
564 /* The section the die comes from.
565 This is either .debug_info or .debug_types, or the .dwo variants. */
566 struct dwarf2_section_info
*die_section
;
568 /* die_section->buffer. */
569 const gdb_byte
*buffer
;
571 /* The end of the buffer. */
572 const gdb_byte
*buffer_end
;
574 /* The abbreviation table to use when reading the DIEs. */
575 struct abbrev_table
*abbrev_table
;
578 /* A subclass of die_reader_specs that holds storage and has complex
579 constructor and destructor behavior. */
581 class cutu_reader
: public die_reader_specs
585 cutu_reader (dwarf2_per_cu_data
*this_cu
,
586 dwarf2_per_objfile
*per_objfile
,
587 struct abbrev_table
*abbrev_table
,
588 dwarf2_cu
*existing_cu
,
590 abbrev_cache
*cache
= nullptr);
592 explicit cutu_reader (struct dwarf2_per_cu_data
*this_cu
,
593 dwarf2_per_objfile
*per_objfile
,
594 struct dwarf2_cu
*parent_cu
= nullptr,
595 struct dwo_file
*dwo_file
= nullptr);
597 DISABLE_COPY_AND_ASSIGN (cutu_reader
);
599 cutu_reader (cutu_reader
&&) = default;
601 const gdb_byte
*info_ptr
= nullptr;
602 struct die_info
*comp_unit_die
= nullptr;
603 bool dummy_p
= false;
605 /* Release the new CU, putting it on the chain. This cannot be done
609 /* Release the abbrev table, transferring ownership to the
611 abbrev_table_up
release_abbrev_table ()
613 return std::move (m_abbrev_table_holder
);
617 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
618 dwarf2_per_objfile
*per_objfile
,
619 dwarf2_cu
*existing_cu
);
621 struct dwarf2_per_cu_data
*m_this_cu
;
622 std::unique_ptr
<dwarf2_cu
> m_new_cu
;
624 /* The ordinary abbreviation table. */
625 abbrev_table_up m_abbrev_table_holder
;
627 /* The DWO abbreviation table. */
628 abbrev_table_up m_dwo_abbrev_table
;
631 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
632 but this would require a corresponding change in unpack_field_as_long
634 static int bits_per_byte
= 8;
636 struct variant_part_builder
;
638 /* When reading a variant, we track a bit more information about the
639 field, and store it in an object of this type. */
643 int first_field
= -1;
646 /* A variant can contain other variant parts. */
647 std::vector
<variant_part_builder
> variant_parts
;
649 /* If we see a DW_TAG_variant, then this will be set if this is the
651 bool default_branch
= false;
652 /* If we see a DW_AT_discr_value, then this will be the discriminant
654 ULONGEST discriminant_value
= 0;
655 /* If we see a DW_AT_discr_list, then this is a pointer to the list
657 struct dwarf_block
*discr_list_data
= nullptr;
660 /* This represents a DW_TAG_variant_part. */
662 struct variant_part_builder
664 /* The offset of the discriminant field. */
665 sect_offset discriminant_offset
{};
667 /* Variants that are direct children of this variant part. */
668 std::vector
<variant_field
> variants
;
670 /* True if we're currently reading a variant. */
671 bool processing_variant
= false;
676 /* Variant parts need to find the discriminant, which is a DIE
677 reference. We track the section offset of each field to make
680 struct field field
{};
685 const char *name
= nullptr;
686 std::vector
<struct fn_field
> fnfields
;
689 /* The routines that read and process dies for a C struct or C++ class
690 pass lists of data member fields and lists of member function fields
691 in an instance of a field_info structure, as defined below. */
694 /* List of data member and baseclasses fields. */
695 std::vector
<struct nextfield
> fields
;
696 std::vector
<struct nextfield
> baseclasses
;
698 /* Member function fieldlist array, contains name of possibly overloaded
699 member function, number of overloaded member functions and a pointer
700 to the head of the member function field chain. */
701 std::vector
<struct fnfieldlist
> fnfieldlists
;
703 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
704 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
705 std::vector
<struct decl_field
> typedef_field_list
;
707 /* Nested types defined by this class and the number of elements in this
709 std::vector
<struct decl_field
> nested_types_list
;
711 /* If non-null, this is the variant part we are currently
713 variant_part_builder
*current_variant_part
= nullptr;
714 /* This holds all the top-level variant parts attached to the type
716 std::vector
<variant_part_builder
> variant_parts
;
718 /* Return the total number of fields (including baseclasses). */
721 return fields
.size () + baseclasses
.size ();
725 /* Loaded secondary compilation units are kept in memory until they
726 have not been referenced for the processing of this many
727 compilation units. Set this to zero to disable caching. Cache
728 sizes of up to at least twenty will improve startup time for
729 typical inter-CU-reference binaries, at an obvious memory cost. */
730 static int dwarf_max_cache_age
= 5;
732 show_dwarf_max_cache_age (struct ui_file
*file
, int from_tty
,
733 struct cmd_list_element
*c
, const char *value
)
735 gdb_printf (file
, _("The upper bound on the age of cached "
736 "DWARF compilation units is %s.\n"),
740 /* When true, wait for DWARF reading to be complete. */
741 static bool dwarf_synchronous
= false;
743 /* "Show" callback for "maint set dwarf synchronous". */
745 show_dwarf_synchronous (struct ui_file
*file
, int from_tty
,
746 struct cmd_list_element
*c
, const char *value
)
748 gdb_printf (file
, _("Whether DWARF reading is synchronous is %s.\n"),
752 /* local function prototypes */
754 static void dwarf2_find_base_address (struct die_info
*die
,
755 struct dwarf2_cu
*cu
);
757 static void build_type_psymtabs_reader (cutu_reader
*reader
,
758 cooked_index_storage
*storage
);
760 static void var_decode_location (struct attribute
*attr
,
762 struct dwarf2_cu
*cu
);
764 static unsigned int peek_abbrev_code (bfd
*, const gdb_byte
*);
766 static const gdb_byte
*read_attribute (const struct die_reader_specs
*,
768 const struct attr_abbrev
*,
770 bool allow_reprocess
= true);
772 /* Note that the default for TAG is chosen because it only matters
773 when reading the top-level DIE, and that function is careful to
774 pass the correct tag. */
775 static void read_attribute_reprocess (const struct die_reader_specs
*reader
,
776 struct attribute
*attr
,
777 dwarf_tag tag
= DW_TAG_padding
);
779 static unrelocated_addr
read_addr_index (struct dwarf2_cu
*cu
,
780 unsigned int addr_index
);
782 static sect_offset
read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
783 dwarf2_section_info
*, sect_offset
);
785 static const char *read_indirect_string
786 (dwarf2_per_objfile
*per_objfile
, bfd
*, const gdb_byte
*,
787 const struct comp_unit_head
*, unsigned int *);
789 static unrelocated_addr
read_addr_index_from_leb128 (struct dwarf2_cu
*,
793 static const char *read_dwo_str_index (const struct die_reader_specs
*reader
,
796 static const char *read_stub_str_index (struct dwarf2_cu
*cu
,
799 static struct attribute
*dwarf2_attr (struct die_info
*, unsigned int,
802 static const char *dwarf2_string_attr (struct die_info
*die
, unsigned int name
,
803 struct dwarf2_cu
*cu
);
805 static const char *dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
);
807 static int dwarf2_flag_true_p (struct die_info
*die
, unsigned name
,
808 struct dwarf2_cu
*cu
);
810 static int die_is_declaration (struct die_info
*, struct dwarf2_cu
*cu
);
812 static struct die_info
*die_specification (struct die_info
*die
,
813 struct dwarf2_cu
**);
815 static line_header_up
dwarf_decode_line_header (sect_offset sect_off
,
816 struct dwarf2_cu
*cu
,
817 const char *comp_dir
);
819 static void dwarf_decode_lines (struct line_header
*,
821 unrelocated_addr
, int decode_mapping
);
823 static void dwarf2_start_subfile (dwarf2_cu
*cu
, const file_entry
&fe
,
824 const line_header
&lh
);
826 static struct symbol
*new_symbol (struct die_info
*, struct type
*,
827 struct dwarf2_cu
*, struct symbol
* = NULL
);
829 static void dwarf2_const_value (const struct attribute
*, struct symbol
*,
832 static void dwarf2_const_value_attr (const struct attribute
*attr
,
835 struct obstack
*obstack
,
836 struct dwarf2_cu
*cu
, LONGEST
*value
,
837 const gdb_byte
**bytes
,
838 struct dwarf2_locexpr_baton
**baton
);
840 static struct type
*read_subrange_index_type (struct die_info
*die
,
841 struct dwarf2_cu
*cu
);
843 static struct type
*die_type (struct die_info
*, struct dwarf2_cu
*);
845 static int need_gnat_info (struct dwarf2_cu
*);
847 static struct type
*die_descriptive_type (struct die_info
*,
850 static void set_descriptive_type (struct type
*, struct die_info
*,
853 static struct type
*die_containing_type (struct die_info
*,
856 static struct type
*lookup_die_type (struct die_info
*, const struct attribute
*,
859 static struct type
*read_type_die (struct die_info
*, struct dwarf2_cu
*);
861 static struct type
*read_type_die_1 (struct die_info
*, struct dwarf2_cu
*);
863 static const char *determine_prefix (struct die_info
*die
, struct dwarf2_cu
*);
865 static char *typename_concat (struct obstack
*obs
, const char *prefix
,
866 const char *suffix
, int physname
,
867 struct dwarf2_cu
*cu
);
869 static void read_file_scope (struct die_info
*, struct dwarf2_cu
*);
871 static void read_type_unit_scope (struct die_info
*, struct dwarf2_cu
*);
873 static void read_func_scope (struct die_info
*, struct dwarf2_cu
*);
875 static void read_lexical_block_scope (struct die_info
*, struct dwarf2_cu
*);
877 static void read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
);
879 static void read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
);
881 /* Return the .debug_loclists section to use for cu. */
882 static struct dwarf2_section_info
*cu_debug_loc_section (struct dwarf2_cu
*cu
);
884 /* Return the .debug_rnglists section to use for cu. */
885 static struct dwarf2_section_info
*cu_debug_rnglists_section
886 (struct dwarf2_cu
*cu
, dwarf_tag tag
);
888 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
889 values. Keep the items ordered with increasing constraints compliance. */
892 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
893 PC_BOUNDS_NOT_PRESENT
,
895 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
896 were present but they do not form a valid range of PC addresses. */
899 /* Discontiguous range was found - that is DW_AT_ranges was found. */
902 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
906 static enum pc_bounds_kind
dwarf2_get_pc_bounds (struct die_info
*,
913 static void get_scope_pc_bounds (struct die_info
*,
914 unrelocated_addr
*, unrelocated_addr
*,
917 static void dwarf2_record_block_ranges (struct die_info
*, struct block
*,
920 static void dwarf2_add_field (struct field_info
*, struct die_info
*,
923 static void dwarf2_attach_fields_to_type (struct field_info
*,
924 struct type
*, struct dwarf2_cu
*);
926 static void dwarf2_add_member_fn (struct field_info
*,
927 struct die_info
*, struct type
*,
930 static void dwarf2_attach_fn_fields_to_type (struct field_info
*,
934 static void process_structure_scope (struct die_info
*, struct dwarf2_cu
*);
936 static void read_common_block (struct die_info
*, struct dwarf2_cu
*);
938 static void read_namespace (struct die_info
*die
, struct dwarf2_cu
*);
940 static void read_module (struct die_info
*die
, struct dwarf2_cu
*cu
);
942 static struct using_direct
**using_directives (struct dwarf2_cu
*cu
);
944 static void read_import_statement (struct die_info
*die
, struct dwarf2_cu
*);
946 static bool read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
);
948 static struct type
*read_module_type (struct die_info
*die
,
949 struct dwarf2_cu
*cu
);
951 static const char *namespace_name (struct die_info
*die
,
952 int *is_anonymous
, struct dwarf2_cu
*);
954 static void process_enumeration_scope (struct die_info
*, struct dwarf2_cu
*);
956 static bool decode_locdesc (struct dwarf_block
*, struct dwarf2_cu
*,
959 static enum dwarf_array_dim_ordering
read_array_order (struct die_info
*,
962 static struct die_info
*read_die_and_siblings_1
963 (const struct die_reader_specs
*, const gdb_byte
*, const gdb_byte
**,
966 static struct die_info
*read_die_and_siblings (const struct die_reader_specs
*,
967 const gdb_byte
*info_ptr
,
968 const gdb_byte
**new_info_ptr
,
969 struct die_info
*parent
);
971 static const gdb_byte
*read_full_die_1 (const struct die_reader_specs
*,
972 struct die_info
**, const gdb_byte
*,
975 static const gdb_byte
*read_toplevel_die (const struct die_reader_specs
*,
978 gdb::array_view
<attribute
*> = {});
980 static void process_die (struct die_info
*, struct dwarf2_cu
*);
982 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu
*,
985 static const char *dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*);
987 static const char *dwarf2_full_name (const char *name
,
988 struct die_info
*die
,
989 struct dwarf2_cu
*cu
);
991 static const char *dwarf2_physname (const char *name
, struct die_info
*die
,
992 struct dwarf2_cu
*cu
);
994 static struct die_info
*dwarf2_extension (struct die_info
*die
,
995 struct dwarf2_cu
**);
997 static void store_in_ref_table (struct die_info
*,
1000 static struct die_info
*follow_die_ref_or_sig (struct die_info
*,
1001 const struct attribute
*,
1002 struct dwarf2_cu
**);
1004 static struct die_info
*follow_die_ref (struct die_info
*,
1005 const struct attribute
*,
1006 struct dwarf2_cu
**);
1008 static struct die_info
*follow_die_sig (struct die_info
*,
1009 const struct attribute
*,
1010 struct dwarf2_cu
**);
1012 static struct type
*get_signatured_type (struct die_info
*, ULONGEST
,
1013 struct dwarf2_cu
*);
1015 static struct type
*get_DW_AT_signature_type (struct die_info
*,
1016 const struct attribute
*,
1017 struct dwarf2_cu
*);
1019 static void load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
1020 dwarf2_per_objfile
*per_objfile
);
1022 static void read_signatured_type (signatured_type
*sig_type
,
1023 dwarf2_per_objfile
*per_objfile
);
1025 static int attr_to_dynamic_prop (const struct attribute
*attr
,
1026 struct die_info
*die
, struct dwarf2_cu
*cu
,
1027 struct dynamic_prop
*prop
, struct type
*type
);
1029 /* memory allocation interface */
1031 static struct dwarf_block
*dwarf_alloc_block (struct dwarf2_cu
*);
1033 static void dwarf_decode_macros (struct dwarf2_cu
*, unsigned int, int);
1035 static void fill_in_loclist_baton (struct dwarf2_cu
*cu
,
1036 struct dwarf2_loclist_baton
*baton
,
1037 const struct attribute
*attr
);
1039 static void dwarf2_symbol_mark_computed (const struct attribute
*attr
,
1041 struct dwarf2_cu
*cu
,
1044 static const gdb_byte
*skip_one_die (const struct die_reader_specs
*reader
,
1045 const gdb_byte
*info_ptr
,
1046 const struct abbrev_info
*abbrev
,
1047 bool do_skip_children
= true);
1049 static struct dwarf2_per_cu_data
*dwarf2_find_containing_comp_unit
1050 (sect_offset sect_off
, unsigned int offset_in_dwz
,
1051 dwarf2_per_bfd
*per_bfd
);
1053 static void prepare_one_comp_unit (struct dwarf2_cu
*cu
,
1054 struct die_info
*comp_unit_die
,
1055 enum language pretend_language
);
1057 static struct type
*set_die_type (struct die_info
*, struct type
*,
1058 struct dwarf2_cu
*, bool = false);
1060 static void load_full_comp_unit (dwarf2_per_cu_data
*per_cu
,
1061 dwarf2_per_objfile
*per_objfile
,
1062 dwarf2_cu
*existing_cu
,
1064 enum language pretend_language
);
1066 static void process_full_comp_unit (dwarf2_cu
*cu
,
1067 enum language pretend_language
);
1069 static void process_full_type_unit (dwarf2_cu
*cu
,
1070 enum language pretend_language
);
1072 static struct type
*get_die_type_at_offset (sect_offset
,
1073 dwarf2_per_cu_data
*per_cu
,
1074 dwarf2_per_objfile
*per_objfile
);
1076 static struct type
*get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
);
1078 static void queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
1079 dwarf2_per_objfile
*per_objfile
,
1080 enum language pretend_language
);
1082 static void process_queue (dwarf2_per_objfile
*per_objfile
);
1084 static bool is_ada_import_or_export (dwarf2_cu
*cu
, const char *name
,
1085 const char *linkagename
);
1087 /* Class, the destructor of which frees all allocated queue entries. This
1088 will only have work to do if an error was thrown while processing the
1089 dwarf. If no error was thrown then the queue entries should have all
1090 been processed, and freed, as we went along. */
1092 class dwarf2_queue_guard
1095 explicit dwarf2_queue_guard (dwarf2_per_objfile
*per_objfile
)
1096 : m_per_objfile (per_objfile
)
1098 gdb_assert (!m_per_objfile
->queue
.has_value ());
1100 m_per_objfile
->queue
.emplace ();
1103 /* Free any entries remaining on the queue. There should only be
1104 entries left if we hit an error while processing the dwarf. */
1105 ~dwarf2_queue_guard ()
1107 gdb_assert (m_per_objfile
->queue
.has_value ());
1109 m_per_objfile
->queue
.reset ();
1112 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard
);
1115 dwarf2_per_objfile
*m_per_objfile
;
1118 dwarf2_queue_item::~dwarf2_queue_item ()
1120 /* Anything still marked queued is likely to be in an
1121 inconsistent state, so discard it. */
1124 per_objfile
->remove_cu (per_cu
);
1129 /* See dwarf2/read.h. */
1132 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data
*data
)
1134 if (data
->is_debug_types
)
1135 delete static_cast<signatured_type
*> (data
);
1140 static file_and_directory
&find_file_and_directory
1141 (struct die_info
*die
, struct dwarf2_cu
*cu
);
1143 static const char *compute_include_file_name
1144 (const struct line_header
*lh
,
1145 const file_entry
&fe
,
1146 const file_and_directory
&cu_info
,
1147 std::string
&name_holder
);
1149 static htab_up
allocate_dwo_unit_table ();
1151 static struct dwo_unit
*lookup_dwo_unit_in_dwp
1152 (dwarf2_per_objfile
*per_objfile
, struct dwp_file
*dwp_file
,
1153 const char *comp_dir
, ULONGEST signature
, int is_debug_types
);
1155 static struct dwp_file
*get_dwp_file (dwarf2_per_objfile
*per_objfile
);
1157 static struct dwo_unit
*lookup_dwo_comp_unit
1158 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
1159 ULONGEST signature
);
1161 static struct dwo_unit
*lookup_dwo_type_unit
1162 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
);
1164 static void queue_and_load_all_dwo_tus (dwarf2_cu
*cu
);
1166 /* A unique pointer to a dwo_file. */
1168 typedef std::unique_ptr
<struct dwo_file
> dwo_file_up
;
1170 static void process_cu_includes (dwarf2_per_objfile
*per_objfile
);
1172 static void check_producer (struct dwarf2_cu
*cu
);
1174 /* Various complaints about symbol reading that don't abort the process. */
1177 dwarf2_debug_line_missing_file_complaint (void)
1179 complaint (_(".debug_line section has line data without a file"));
1183 dwarf2_debug_line_missing_end_sequence_complaint (void)
1185 complaint (_(".debug_line section has line "
1186 "program sequence without an end"));
1190 dwarf2_complex_location_expr_complaint (void)
1192 complaint (_("location expression too complex"));
1196 dwarf2_const_value_length_mismatch_complaint (const char *arg1
, int arg2
,
1199 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1204 dwarf2_invalid_attrib_class_complaint (const char *arg1
, const char *arg2
)
1206 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1213 dwarf2_per_objfile::adjust (unrelocated_addr addr
)
1215 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
1216 CORE_ADDR tem
= (CORE_ADDR
) addr
+ baseaddr
;
1217 tem
= gdbarch_adjust_dwarf2_addr (objfile
->arch (), tem
);
1218 return (unrelocated_addr
) (tem
- baseaddr
);
1224 dwarf2_per_objfile::relocate (unrelocated_addr addr
)
1226 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
1227 CORE_ADDR tem
= (CORE_ADDR
) addr
+ baseaddr
;
1228 return gdbarch_adjust_dwarf2_addr (objfile
->arch (), tem
);
1231 /* Hash function for line_header_hash. */
1234 line_header_hash (const struct line_header
*ofs
)
1236 return to_underlying (ofs
->sect_off
) ^ ofs
->offset_in_dwz
;
1239 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1242 line_header_hash_voidp (const void *item
)
1244 const struct line_header
*ofs
= (const struct line_header
*) item
;
1246 return line_header_hash (ofs
);
1249 /* Equality function for line_header_hash. */
1252 line_header_eq_voidp (const void *item_lhs
, const void *item_rhs
)
1254 const struct line_header
*ofs_lhs
= (const struct line_header
*) item_lhs
;
1255 const struct line_header
*ofs_rhs
= (const struct line_header
*) item_rhs
;
1257 return (ofs_lhs
->sect_off
== ofs_rhs
->sect_off
1258 && ofs_lhs
->offset_in_dwz
== ofs_rhs
->offset_in_dwz
);
1261 /* See declaration. */
1263 dwarf2_per_bfd::dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
,
1266 can_copy (can_copy_
)
1269 names
= &dwarf2_elf_names
;
1271 for (asection
*sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
1272 locate_sections (obfd
, sec
, *names
);
1275 dwarf2_per_bfd::~dwarf2_per_bfd ()
1277 /* Data from the per-BFD may be needed when finalizing the cooked
1278 index table, so wait here while this happens. */
1279 if (index_table
!= nullptr)
1280 index_table
->wait_completely ();
1282 for (auto &per_cu
: all_units
)
1284 per_cu
->imported_symtabs_free ();
1285 per_cu
->free_cached_file_names ();
1288 /* Everything else should be on this->obstack. */
1294 dwarf2_per_objfile::remove_all_cus ()
1296 gdb_assert (!queue
.has_value ());
1298 m_dwarf2_cus
.clear ();
1301 /* A helper class that calls free_cached_comp_units on
1304 class free_cached_comp_units
1308 explicit free_cached_comp_units (dwarf2_per_objfile
*per_objfile
)
1309 : m_per_objfile (per_objfile
)
1313 ~free_cached_comp_units ()
1315 m_per_objfile
->remove_all_cus ();
1318 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units
);
1322 dwarf2_per_objfile
*m_per_objfile
;
1328 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const
1330 if (per_cu
->index
< this->m_symtabs
.size ())
1331 return this->m_symtabs
[per_cu
->index
] != nullptr;
1338 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data
*per_cu
) const
1340 if (per_cu
->index
< this->m_symtabs
.size ())
1341 return this->m_symtabs
[per_cu
->index
];
1348 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data
*per_cu
,
1349 compunit_symtab
*symtab
)
1351 if (per_cu
->index
>= this->m_symtabs
.size ())
1352 this->m_symtabs
.resize (per_cu
->index
+ 1);
1353 gdb_assert (this->m_symtabs
[per_cu
->index
] == nullptr);
1354 this->m_symtabs
[per_cu
->index
] = symtab
;
1357 /* Helper function for dwarf2_initialize_objfile that creates the
1361 dwarf2_has_info (struct objfile
*objfile
,
1362 const struct dwarf2_debug_sections
*names
,
1365 if (objfile
->flags
& OBJF_READNEVER
)
1368 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1370 if (per_objfile
== NULL
)
1372 dwarf2_per_bfd
*per_bfd
;
1374 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1375 BFD doesn't require relocations.
1377 We don't share with objfiles for which -readnow was requested,
1378 because it would complicate things when loading the same BFD with
1379 -readnow and then without -readnow. */
1380 if (!gdb_bfd_requires_relocations (objfile
->obfd
.get ())
1381 && (objfile
->flags
& OBJF_READNOW
) == 0)
1383 /* See if one has been created for this BFD yet. */
1384 per_bfd
= dwarf2_per_bfd_bfd_data_key
.get (objfile
->obfd
.get ());
1386 if (per_bfd
== nullptr)
1388 /* No, create it now. */
1389 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
.get (), names
,
1391 dwarf2_per_bfd_bfd_data_key
.set (objfile
->obfd
.get (), per_bfd
);
1396 /* No sharing possible, create one specifically for this objfile. */
1397 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
.get (), names
, can_copy
);
1398 dwarf2_per_bfd_objfile_data_key
.set (objfile
, per_bfd
);
1401 per_objfile
= dwarf2_objfile_data_key
.emplace (objfile
, objfile
, per_bfd
);
1404 return (!per_objfile
->per_bfd
->info
.is_virtual
1405 && per_objfile
->per_bfd
->info
.s
.section
!= NULL
1406 && !per_objfile
->per_bfd
->abbrev
.is_virtual
1407 && per_objfile
->per_bfd
->abbrev
.s
.section
!= NULL
);
1410 /* See declaration. */
1413 dwarf2_per_bfd::locate_sections (bfd
*abfd
, asection
*sectp
,
1414 const dwarf2_debug_sections
&names
)
1416 flagword aflag
= bfd_section_flags (sectp
);
1418 if ((aflag
& SEC_HAS_CONTENTS
) == 0)
1421 else if (elf_section_data (sectp
)->this_hdr
.sh_size
1422 > bfd_get_file_size (abfd
))
1424 bfd_size_type size
= elf_section_data (sectp
)->this_hdr
.sh_size
;
1425 warning (_("Discarding section %s which has a section size (%s"
1426 ") larger than the file size [in module %s]"),
1427 bfd_section_name (sectp
), phex_nz (size
, sizeof (size
)),
1428 bfd_get_filename (abfd
));
1430 else if (names
.info
.matches (sectp
->name
))
1432 this->info
.s
.section
= sectp
;
1433 this->info
.size
= bfd_section_size (sectp
);
1435 else if (names
.abbrev
.matches (sectp
->name
))
1437 this->abbrev
.s
.section
= sectp
;
1438 this->abbrev
.size
= bfd_section_size (sectp
);
1440 else if (names
.line
.matches (sectp
->name
))
1442 this->line
.s
.section
= sectp
;
1443 this->line
.size
= bfd_section_size (sectp
);
1445 else if (names
.loc
.matches (sectp
->name
))
1447 this->loc
.s
.section
= sectp
;
1448 this->loc
.size
= bfd_section_size (sectp
);
1450 else if (names
.loclists
.matches (sectp
->name
))
1452 this->loclists
.s
.section
= sectp
;
1453 this->loclists
.size
= bfd_section_size (sectp
);
1455 else if (names
.macinfo
.matches (sectp
->name
))
1457 this->macinfo
.s
.section
= sectp
;
1458 this->macinfo
.size
= bfd_section_size (sectp
);
1460 else if (names
.macro
.matches (sectp
->name
))
1462 this->macro
.s
.section
= sectp
;
1463 this->macro
.size
= bfd_section_size (sectp
);
1465 else if (names
.str
.matches (sectp
->name
))
1467 this->str
.s
.section
= sectp
;
1468 this->str
.size
= bfd_section_size (sectp
);
1470 else if (names
.str_offsets
.matches (sectp
->name
))
1472 this->str_offsets
.s
.section
= sectp
;
1473 this->str_offsets
.size
= bfd_section_size (sectp
);
1475 else if (names
.line_str
.matches (sectp
->name
))
1477 this->line_str
.s
.section
= sectp
;
1478 this->line_str
.size
= bfd_section_size (sectp
);
1480 else if (names
.addr
.matches (sectp
->name
))
1482 this->addr
.s
.section
= sectp
;
1483 this->addr
.size
= bfd_section_size (sectp
);
1485 else if (names
.frame
.matches (sectp
->name
))
1487 this->frame
.s
.section
= sectp
;
1488 this->frame
.size
= bfd_section_size (sectp
);
1490 else if (names
.eh_frame
.matches (sectp
->name
))
1492 this->eh_frame
.s
.section
= sectp
;
1493 this->eh_frame
.size
= bfd_section_size (sectp
);
1495 else if (names
.ranges
.matches (sectp
->name
))
1497 this->ranges
.s
.section
= sectp
;
1498 this->ranges
.size
= bfd_section_size (sectp
);
1500 else if (names
.rnglists
.matches (sectp
->name
))
1502 this->rnglists
.s
.section
= sectp
;
1503 this->rnglists
.size
= bfd_section_size (sectp
);
1505 else if (names
.types
.matches (sectp
->name
))
1507 struct dwarf2_section_info type_section
;
1509 memset (&type_section
, 0, sizeof (type_section
));
1510 type_section
.s
.section
= sectp
;
1511 type_section
.size
= bfd_section_size (sectp
);
1513 this->types
.push_back (type_section
);
1515 else if (names
.gdb_index
.matches (sectp
->name
))
1517 this->gdb_index
.s
.section
= sectp
;
1518 this->gdb_index
.size
= bfd_section_size (sectp
);
1520 else if (names
.debug_names
.matches (sectp
->name
))
1522 this->debug_names
.s
.section
= sectp
;
1523 this->debug_names
.size
= bfd_section_size (sectp
);
1525 else if (names
.debug_aranges
.matches (sectp
->name
))
1527 this->debug_aranges
.s
.section
= sectp
;
1528 this->debug_aranges
.size
= bfd_section_size (sectp
);
1531 if ((bfd_section_flags (sectp
) & (SEC_LOAD
| SEC_ALLOC
))
1532 && bfd_section_vma (sectp
) == 0)
1533 this->has_section_at_zero
= true;
1536 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1540 dwarf2_get_section_info (struct objfile
*objfile
,
1541 enum dwarf2_section_enum sect
,
1542 asection
**sectp
, const gdb_byte
**bufp
,
1543 bfd_size_type
*sizep
)
1545 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1546 struct dwarf2_section_info
*info
;
1548 /* We may see an objfile without any DWARF, in which case we just
1550 if (per_objfile
== NULL
)
1559 case DWARF2_DEBUG_FRAME
:
1560 info
= &per_objfile
->per_bfd
->frame
;
1562 case DWARF2_EH_FRAME
:
1563 info
= &per_objfile
->per_bfd
->eh_frame
;
1566 gdb_assert_not_reached ("unexpected section");
1569 info
->read (objfile
);
1571 *sectp
= info
->get_bfd_section ();
1572 *bufp
= info
->buffer
;
1573 *sizep
= info
->size
;
1576 /* See dwarf2/read.h. */
1579 dwarf2_per_bfd::map_info_sections (struct objfile
*objfile
)
1581 info
.read (objfile
);
1582 abbrev
.read (objfile
);
1583 line
.read (objfile
);
1585 str_offsets
.read (objfile
);
1586 line_str
.read (objfile
);
1587 ranges
.read (objfile
);
1588 rnglists
.read (objfile
);
1589 addr
.read (objfile
);
1590 debug_aranges
.read (objfile
);
1592 for (auto §ion
: types
)
1593 section
.read (objfile
);
1597 /* DWARF quick_symbol_functions support. */
1599 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1600 unique line tables, so we maintain a separate table of all .debug_line
1601 derived entries to support the sharing.
1602 All the quick functions need is the list of file names. We discard the
1603 line_header when we're done and don't need to record it here. */
1604 struct quick_file_names
1606 /* The data used to construct the hash key. */
1607 struct stmt_list_hash hash
;
1609 /* The number of entries in file_names, real_names. */
1610 unsigned int num_file_names
;
1612 /* The CU directory, as given by DW_AT_comp_dir. May be
1614 const char *comp_dir
;
1616 /* The file names from the line table, after being run through
1618 const char **file_names
;
1620 /* The file names from the line table after being run through
1621 gdb_realpath. These are computed lazily. */
1622 const char **real_names
;
1625 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1626 It's handy in this case to have an empty implementation of the
1627 quick symbol functions, to avoid special cases in the rest of the
1630 struct readnow_functions
: public dwarf2_base_index_functions
1632 void dump (struct objfile
*objfile
) override
1636 bool expand_symtabs_matching
1637 (struct objfile
*objfile
,
1638 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1639 const lookup_name_info
*lookup_name
,
1640 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1641 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1642 block_search_flags search_flags
,
1644 enum search_domain kind
) override
1650 /* Utility hash function for a stmt_list_hash. */
1653 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1657 if (stmt_list_hash
->dwo_unit
!= NULL
)
1658 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1659 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1663 /* Utility equality function for a stmt_list_hash. */
1666 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1667 const struct stmt_list_hash
*rhs
)
1669 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1671 if (lhs
->dwo_unit
!= NULL
1672 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1675 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1678 /* Hash function for a quick_file_names. */
1681 hash_file_name_entry (const void *e
)
1683 const struct quick_file_names
*file_data
1684 = (const struct quick_file_names
*) e
;
1686 return hash_stmt_list_entry (&file_data
->hash
);
1689 /* Equality function for a quick_file_names. */
1692 eq_file_name_entry (const void *a
, const void *b
)
1694 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
1695 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
1697 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
1703 create_quick_file_names_table (unsigned int nr_initial_entries
)
1705 return htab_up (htab_create_alloc (nr_initial_entries
,
1706 hash_file_name_entry
, eq_file_name_entry
,
1707 nullptr, xcalloc
, xfree
));
1710 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1711 function is unrelated to symtabs, symtab would have to be created afterwards.
1712 You should call age_cached_comp_units after processing the CU. */
1715 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
1718 if (per_cu
->is_debug_types
)
1719 load_full_type_unit (per_cu
, per_objfile
);
1721 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
1722 skip_partial
, language_minimal
);
1724 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
1726 return nullptr; /* Dummy CU. */
1728 dwarf2_find_base_address (cu
->dies
, cu
);
1733 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1736 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
1737 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
1740 /* The destructor of dwarf2_queue_guard frees any entries left on
1741 the queue. After this point we're guaranteed to leave this function
1742 with the dwarf queue empty. */
1743 dwarf2_queue_guard
q_guard (per_objfile
);
1745 if (!per_objfile
->symtab_set_p (per_cu
))
1747 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
1748 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
1750 /* If we just loaded a CU from a DWO, and we're working with an index
1751 that may badly handle TUs, load all the TUs in that DWO as well.
1752 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1753 if (!per_cu
->is_debug_types
1755 && cu
->dwo_unit
!= NULL
1756 && per_objfile
->per_bfd
->index_table
!= NULL
1757 && !per_objfile
->per_bfd
->index_table
->version_check ()
1758 /* DWP files aren't supported yet. */
1759 && get_dwp_file (per_objfile
) == NULL
)
1760 queue_and_load_all_dwo_tus (cu
);
1763 process_queue (per_objfile
);
1766 /* Age the cache, releasing compilation units that have not
1767 been used recently. */
1768 per_objfile
->age_comp_units ();
1771 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1772 the per-objfile for which this symtab is instantiated.
1774 Returns the resulting symbol table. */
1776 static struct compunit_symtab
*
1777 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
1778 dwarf2_per_objfile
*per_objfile
,
1781 if (!per_objfile
->symtab_set_p (per_cu
))
1783 free_cached_comp_units
freer (per_objfile
);
1784 scoped_restore decrementer
= increment_reading_symtab ();
1785 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
1786 process_cu_includes (per_objfile
);
1789 return per_objfile
->get_symtab (per_cu
);
1794 dwarf2_per_cu_data_up
1795 dwarf2_per_bfd::allocate_per_cu ()
1797 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
1798 result
->per_bfd
= this;
1799 result
->index
= all_units
.size ();
1806 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
1808 signatured_type_up
result (new signatured_type (signature
));
1809 result
->per_bfd
= this;
1810 result
->index
= all_units
.size ();
1811 result
->is_debug_types
= true;
1818 dwarf2_per_cu_data_up
1819 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
1820 struct dwarf2_section_info
*section
,
1822 sect_offset sect_off
, ULONGEST length
)
1824 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
1825 the_cu
->sect_off
= sect_off
;
1826 the_cu
->set_length (length
);
1827 the_cu
->section
= section
;
1828 the_cu
->is_dwz
= is_dwz
;
1832 /* die_reader_func for dw2_get_file_names. */
1835 dw2_get_file_names_reader (const struct die_reader_specs
*reader
,
1836 struct die_info
*comp_unit_die
)
1838 struct dwarf2_cu
*cu
= reader
->cu
;
1839 struct dwarf2_per_cu_data
*this_cu
= cu
->per_cu
;
1840 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
1841 struct dwarf2_per_cu_data
*lh_cu
;
1842 struct attribute
*attr
;
1844 struct quick_file_names
*qfn
;
1846 gdb_assert (! this_cu
->is_debug_types
);
1848 this_cu
->files_read
= true;
1849 /* Our callers never want to match partial units -- instead they
1850 will match the enclosing full CU. */
1851 if (comp_unit_die
->tag
== DW_TAG_partial_unit
)
1858 sect_offset line_offset
{};
1860 file_and_directory
&fnd
= find_file_and_directory (comp_unit_die
, cu
);
1862 attr
= dwarf2_attr (comp_unit_die
, DW_AT_stmt_list
, cu
);
1863 if (attr
!= nullptr && attr
->form_is_unsigned ())
1865 struct quick_file_names find_entry
;
1867 line_offset
= (sect_offset
) attr
->as_unsigned ();
1869 /* We may have already read in this line header (TU line header sharing).
1870 If we have we're done. */
1871 find_entry
.hash
.dwo_unit
= cu
->dwo_unit
;
1872 find_entry
.hash
.line_sect_off
= line_offset
;
1873 slot
= htab_find_slot (per_objfile
->per_bfd
->quick_file_names_table
.get (),
1874 &find_entry
, INSERT
);
1877 lh_cu
->file_names
= (struct quick_file_names
*) *slot
;
1881 lh
= dwarf_decode_line_header (line_offset
, cu
, fnd
.get_comp_dir ());
1885 if (!fnd
.is_unknown ())
1887 else if (lh
== nullptr)
1890 qfn
= XOBNEW (&per_objfile
->per_bfd
->obstack
, struct quick_file_names
);
1891 qfn
->hash
.dwo_unit
= cu
->dwo_unit
;
1892 qfn
->hash
.line_sect_off
= line_offset
;
1893 /* There may not be a DW_AT_stmt_list. */
1894 if (slot
!= nullptr)
1897 std::vector
<const char *> include_names
;
1900 for (const auto &entry
: lh
->file_names ())
1902 std::string name_holder
;
1903 const char *include_name
=
1904 compute_include_file_name (lh
.get (), entry
, fnd
, name_holder
);
1905 if (include_name
!= nullptr)
1907 include_name
= per_objfile
->objfile
->intern (include_name
);
1908 include_names
.push_back (include_name
);
1913 qfn
->num_file_names
= offset
+ include_names
.size ();
1914 qfn
->comp_dir
= fnd
.intern_comp_dir (per_objfile
->objfile
);
1916 XOBNEWVEC (&per_objfile
->per_bfd
->obstack
, const char *,
1917 qfn
->num_file_names
);
1919 qfn
->file_names
[0] = per_objfile
->objfile
->intern (fnd
.get_name ());
1921 if (!include_names
.empty ())
1922 memcpy (&qfn
->file_names
[offset
], include_names
.data (),
1923 include_names
.size () * sizeof (const char *));
1925 qfn
->real_names
= NULL
;
1927 lh_cu
->file_names
= qfn
;
1930 /* A helper for the "quick" functions which attempts to read the line
1931 table for THIS_CU. */
1933 static struct quick_file_names
*
1934 dw2_get_file_names (dwarf2_per_cu_data
*this_cu
,
1935 dwarf2_per_objfile
*per_objfile
)
1937 /* This should never be called for TUs. */
1938 gdb_assert (! this_cu
->is_debug_types
);
1940 if (this_cu
->files_read
)
1941 return this_cu
->file_names
;
1943 cutu_reader
reader (this_cu
, per_objfile
);
1944 if (!reader
.dummy_p
)
1945 dw2_get_file_names_reader (&reader
, reader
.comp_unit_die
);
1947 return this_cu
->file_names
;
1950 /* A helper for the "quick" functions which computes and caches the
1951 real path for a given file name from the line table. */
1954 dw2_get_real_path (dwarf2_per_objfile
*per_objfile
,
1955 struct quick_file_names
*qfn
, int index
)
1957 if (qfn
->real_names
== NULL
)
1958 qfn
->real_names
= OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
1959 qfn
->num_file_names
, const char *);
1961 if (qfn
->real_names
[index
] == NULL
)
1963 const char *dirname
= nullptr;
1965 if (!IS_ABSOLUTE_PATH (qfn
->file_names
[index
]))
1966 dirname
= qfn
->comp_dir
;
1968 gdb::unique_xmalloc_ptr
<char> fullname
;
1969 fullname
= find_source_or_rewrite (qfn
->file_names
[index
], dirname
);
1971 qfn
->real_names
[index
] = fullname
.release ();
1974 return qfn
->real_names
[index
];
1978 dwarf2_base_index_functions::find_last_source_symtab (struct objfile
*objfile
)
1980 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1981 dwarf2_per_cu_data
*dwarf_cu
1982 = per_objfile
->per_bfd
->all_units
.back ().get ();
1983 compunit_symtab
*cust
= dw2_instantiate_symtab (dwarf_cu
, per_objfile
, false);
1988 return cust
->primary_filetab ();
1994 dwarf2_per_cu_data::free_cached_file_names ()
1997 fnd
->forget_fullname ();
1999 if (per_bfd
== nullptr)
2002 struct quick_file_names
*file_data
= file_names
;
2003 if (file_data
!= nullptr && file_data
->real_names
!= nullptr)
2005 for (int i
= 0; i
< file_data
->num_file_names
; ++i
)
2007 xfree ((void *) file_data
->real_names
[i
]);
2008 file_data
->real_names
[i
] = nullptr;
2014 dwarf2_base_index_functions::forget_cached_source_info
2015 (struct objfile
*objfile
)
2017 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2019 for (auto &per_cu
: per_objfile
->per_bfd
->all_units
)
2020 per_cu
->free_cached_file_names ();
2024 dwarf2_base_index_functions::print_stats (struct objfile
*objfile
,
2030 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2031 int total
= per_objfile
->per_bfd
->all_units
.size ();
2034 for (int i
= 0; i
< total
; ++i
)
2036 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
2038 if (!per_objfile
->symtab_set_p (per_cu
))
2041 gdb_printf (_(" Number of read CUs: %d\n"), total
- count
);
2042 gdb_printf (_(" Number of unread CUs: %d\n"), count
);
2046 dwarf2_base_index_functions::expand_all_symtabs (struct objfile
*objfile
)
2048 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2049 int total_units
= per_objfile
->per_bfd
->all_units
.size ();
2051 for (int i
= 0; i
< total_units
; ++i
)
2053 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
2055 /* We don't want to directly expand a partial CU, because if we
2056 read it with the wrong language, then assertion failures can
2057 be triggered later on. See PR symtab/23010. So, tell
2058 dw2_instantiate_symtab to skip partial CUs -- any important
2059 partial CU will be read via DW_TAG_imported_unit anyway. */
2060 dw2_instantiate_symtab (per_cu
, per_objfile
, true);
2065 /* Starting from a search name, return the string that finds the upper
2066 bound of all strings that start with SEARCH_NAME in a sorted name
2067 list. Returns the empty string to indicate that the upper bound is
2068 the end of the list. */
2071 make_sort_after_prefix_name (const char *search_name
)
2073 /* When looking to complete "func", we find the upper bound of all
2074 symbols that start with "func" by looking for where we'd insert
2075 the closest string that would follow "func" in lexicographical
2076 order. Usually, that's "func"-with-last-character-incremented,
2077 i.e. "fund". Mind non-ASCII characters, though. Usually those
2078 will be UTF-8 multi-byte sequences, but we can't be certain.
2079 Especially mind the 0xff character, which is a valid character in
2080 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
2081 rule out compilers allowing it in identifiers. Note that
2082 conveniently, strcmp/strcasecmp are specified to compare
2083 characters interpreted as unsigned char. So what we do is treat
2084 the whole string as a base 256 number composed of a sequence of
2085 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
2086 to 0, and carries 1 to the following more-significant position.
2087 If the very first character in SEARCH_NAME ends up incremented
2088 and carries/overflows, then the upper bound is the end of the
2089 list. The string after the empty string is also the empty
2092 Some examples of this operation:
2094 SEARCH_NAME => "+1" RESULT
2098 "\xff" "a" "\xff" => "\xff" "b"
2103 Then, with these symbols for example:
2109 completing "func" looks for symbols between "func" and
2110 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
2111 which finds "func" and "func1", but not "fund".
2115 funcÿ (Latin1 'ÿ' [0xff])
2119 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
2120 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
2124 ÿÿ (Latin1 'ÿ' [0xff])
2127 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
2128 the end of the list.
2130 std::string after
= search_name
;
2131 while (!after
.empty () && (unsigned char) after
.back () == 0xff)
2133 if (!after
.empty ())
2134 after
.back () = (unsigned char) after
.back () + 1;
2138 /* See declaration. */
2140 std::pair
<std::vector
<name_component
>::const_iterator
,
2141 std::vector
<name_component
>::const_iterator
>
2142 mapped_index_base::find_name_components_bounds
2143 (const lookup_name_info
&lookup_name_without_params
, language lang
,
2144 dwarf2_per_objfile
*per_objfile
) const
2147 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
2149 const char *lang_name
2150 = lookup_name_without_params
.language_lookup_name (lang
);
2152 /* Comparison function object for lower_bound that matches against a
2153 given symbol name. */
2154 auto lookup_compare_lower
= [&] (const name_component
&elem
,
2157 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
2158 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
2159 return name_cmp (elem_name
, name
) < 0;
2162 /* Comparison function object for upper_bound that matches against a
2163 given symbol name. */
2164 auto lookup_compare_upper
= [&] (const char *name
,
2165 const name_component
&elem
)
2167 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
2168 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
2169 return name_cmp (name
, elem_name
) < 0;
2172 auto begin
= this->name_components
.begin ();
2173 auto end
= this->name_components
.end ();
2175 /* Find the lower bound. */
2178 if (lookup_name_without_params
.completion_mode () && lang_name
[0] == '\0')
2181 return std::lower_bound (begin
, end
, lang_name
, lookup_compare_lower
);
2184 /* Find the upper bound. */
2187 if (lookup_name_without_params
.completion_mode ())
2189 /* In completion mode, we want UPPER to point past all
2190 symbols names that have the same prefix. I.e., with
2191 these symbols, and completing "func":
2193 function << lower bound
2195 other_function << upper bound
2197 We find the upper bound by looking for the insertion
2198 point of "func"-with-last-character-incremented,
2200 std::string after
= make_sort_after_prefix_name (lang_name
);
2203 return std::lower_bound (lower
, end
, after
.c_str (),
2204 lookup_compare_lower
);
2207 return std::upper_bound (lower
, end
, lang_name
, lookup_compare_upper
);
2210 return {lower
, upper
};
2213 /* See declaration. */
2216 mapped_index_base::build_name_components (dwarf2_per_objfile
*per_objfile
)
2218 if (!this->name_components
.empty ())
2221 this->name_components_casing
= case_sensitivity
;
2223 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
2225 /* The code below only knows how to break apart components of C++
2226 symbol names (and other languages that use '::' as
2227 namespace/module separator) and Ada symbol names. */
2228 auto count
= this->symbol_name_count ();
2229 for (offset_type idx
= 0; idx
< count
; idx
++)
2231 if (this->symbol_name_slot_invalid (idx
))
2234 const char *name
= this->symbol_name_at (idx
, per_objfile
);
2236 /* Add each name component to the name component table. */
2237 unsigned int previous_len
= 0;
2239 if (strstr (name
, "::") != nullptr)
2241 for (unsigned int current_len
= cp_find_first_component (name
);
2242 name
[current_len
] != '\0';
2243 current_len
+= cp_find_first_component (name
+ current_len
))
2245 gdb_assert (name
[current_len
] == ':');
2246 this->name_components
.push_back ({previous_len
, idx
});
2247 /* Skip the '::'. */
2249 previous_len
= current_len
;
2254 /* Handle the Ada encoded (aka mangled) form here. */
2255 for (const char *iter
= strstr (name
, "__");
2257 iter
= strstr (iter
, "__"))
2259 this->name_components
.push_back ({previous_len
, idx
});
2261 previous_len
= iter
- name
;
2265 this->name_components
.push_back ({previous_len
, idx
});
2268 /* Sort name_components elements by name. */
2269 auto name_comp_compare
= [&] (const name_component
&left
,
2270 const name_component
&right
)
2272 const char *left_qualified
2273 = this->symbol_name_at (left
.idx
, per_objfile
);
2274 const char *right_qualified
2275 = this->symbol_name_at (right
.idx
, per_objfile
);
2277 const char *left_name
= left_qualified
+ left
.name_offset
;
2278 const char *right_name
= right_qualified
+ right
.name_offset
;
2280 return name_cmp (left_name
, right_name
) < 0;
2283 std::sort (this->name_components
.begin (),
2284 this->name_components
.end (),
2291 dw2_expand_symtabs_matching_symbol
2292 (mapped_index_base
&index
,
2293 const lookup_name_info
&lookup_name_in
,
2294 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
2295 gdb::function_view
<bool (offset_type
)> match_callback
,
2296 dwarf2_per_objfile
*per_objfile
)
2298 lookup_name_info lookup_name_without_params
2299 = lookup_name_in
.make_ignore_params ();
2301 /* Build the symbol name component sorted vector, if we haven't
2303 index
.build_name_components (per_objfile
);
2305 /* The same symbol may appear more than once in the range though.
2306 E.g., if we're looking for symbols that complete "w", and we have
2307 a symbol named "w1::w2", we'll find the two name components for
2308 that same symbol in the range. To be sure we only call the
2309 callback once per symbol, we first collect the symbol name
2310 indexes that matched in a temporary vector and ignore
2312 std::vector
<offset_type
> matches
;
2314 struct name_and_matcher
2316 symbol_name_matcher_ftype
*matcher
;
2319 bool operator== (const name_and_matcher
&other
) const
2321 return matcher
== other
.matcher
&& strcmp (name
, other
.name
) == 0;
2325 /* A vector holding all the different symbol name matchers, for all
2327 std::vector
<name_and_matcher
> matchers
;
2329 for (int i
= 0; i
< nr_languages
; i
++)
2331 enum language lang_e
= (enum language
) i
;
2333 const language_defn
*lang
= language_def (lang_e
);
2334 symbol_name_matcher_ftype
*name_matcher
2335 = lang
->get_symbol_name_matcher (lookup_name_without_params
);
2337 name_and_matcher key
{
2339 lookup_name_without_params
.language_lookup_name (lang_e
)
2342 /* Don't insert the same comparison routine more than once.
2343 Note that we do this linear walk. This is not a problem in
2344 practice because the number of supported languages is
2346 if (std::find (matchers
.begin (), matchers
.end (), key
)
2349 matchers
.push_back (std::move (key
));
2352 = index
.find_name_components_bounds (lookup_name_without_params
,
2353 lang_e
, per_objfile
);
2355 /* Now for each symbol name in range, check to see if we have a name
2356 match, and if so, call the MATCH_CALLBACK callback. */
2358 for (; bounds
.first
!= bounds
.second
; ++bounds
.first
)
2360 const char *qualified
2361 = index
.symbol_name_at (bounds
.first
->idx
, per_objfile
);
2363 if (!name_matcher (qualified
, lookup_name_without_params
, NULL
)
2364 || (symbol_matcher
!= NULL
&& !symbol_matcher (qualified
)))
2367 matches
.push_back (bounds
.first
->idx
);
2371 std::sort (matches
.begin (), matches
.end ());
2373 /* Finally call the callback, once per match. */
2376 for (offset_type idx
: matches
)
2380 if (!match_callback (idx
))
2389 /* Above we use a type wider than idx's for 'prev', since 0 and
2390 (offset_type)-1 are both possible values. */
2391 static_assert (sizeof (prev
) > sizeof (offset_type
), "");
2398 namespace selftests
{ namespace dw2_expand_symtabs_matching
{
2400 /* A mock .gdb_index/.debug_names-like name index table, enough to
2401 exercise dw2_expand_symtabs_matching_symbol, which works with the
2402 mapped_index_base interface. Builds an index from the symbol list
2403 passed as parameter to the constructor. */
2404 class mock_mapped_index
: public mapped_index_base
2407 mock_mapped_index (gdb::array_view
<const char *> symbols
)
2408 : m_symbol_table (symbols
)
2411 DISABLE_COPY_AND_ASSIGN (mock_mapped_index
);
2413 /* Return the number of names in the symbol table. */
2414 size_t symbol_name_count () const override
2416 return m_symbol_table
.size ();
2419 /* Get the name of the symbol at IDX in the symbol table. */
2420 const char *symbol_name_at
2421 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
2423 return m_symbol_table
[idx
];
2426 quick_symbol_functions_up
make_quick_functions () const override
2432 gdb::array_view
<const char *> m_symbol_table
;
2435 /* Convenience function that converts a NULL pointer to a "<null>"
2436 string, to pass to print routines. */
2439 string_or_null (const char *str
)
2441 return str
!= NULL
? str
: "<null>";
2444 /* Check if a lookup_name_info built from
2445 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
2446 index. EXPECTED_LIST is the list of expected matches, in expected
2447 matching order. If no match expected, then an empty list is
2448 specified. Returns true on success. On failure prints a warning
2449 indicating the file:line that failed, and returns false. */
2452 check_match (const char *file
, int line
,
2453 mock_mapped_index
&mock_index
,
2454 const char *name
, symbol_name_match_type match_type
,
2455 bool completion_mode
,
2456 std::initializer_list
<const char *> expected_list
,
2457 dwarf2_per_objfile
*per_objfile
)
2459 lookup_name_info
lookup_name (name
, match_type
, completion_mode
);
2461 bool matched
= true;
2463 auto mismatch
= [&] (const char *expected_str
,
2466 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
2467 "expected=\"%s\", got=\"%s\"\n"),
2469 (match_type
== symbol_name_match_type::FULL
2471 name
, string_or_null (expected_str
), string_or_null (got
));
2475 auto expected_it
= expected_list
.begin ();
2476 auto expected_end
= expected_list
.end ();
2478 dw2_expand_symtabs_matching_symbol (mock_index
, lookup_name
,
2480 [&] (offset_type idx
)
2482 const char *matched_name
= mock_index
.symbol_name_at (idx
, per_objfile
);
2483 const char *expected_str
2484 = expected_it
== expected_end
? NULL
: *expected_it
++;
2486 if (expected_str
== NULL
|| strcmp (expected_str
, matched_name
) != 0)
2487 mismatch (expected_str
, matched_name
);
2491 const char *expected_str
2492 = expected_it
== expected_end
? NULL
: *expected_it
++;
2493 if (expected_str
!= NULL
)
2494 mismatch (expected_str
, NULL
);
2499 /* The symbols added to the mock mapped_index for testing (in
2501 static const char *test_symbols
[] = {
2510 "ns2::tmpl<int>::foo2",
2511 "(anonymous namespace)::A::B::C",
2513 /* These are used to check that the increment-last-char in the
2514 matching algorithm for completion doesn't match "t1_fund" when
2515 completing "t1_func". */
2521 /* A UTF-8 name with multi-byte sequences to make sure that
2522 cp-name-parser understands this as a single identifier ("função"
2523 is "function" in PT). */
2524 (const char *)u8
"u8função",
2526 /* Test a symbol name that ends with a 0xff character, which is a
2527 valid character in non-UTF-8 source character sets (e.g. Latin1
2528 'ÿ'), and we can't rule out compilers allowing it in identifiers.
2529 We test this because the completion algorithm finds the upper
2530 bound of symbols by looking for the insertion point of
2531 "func"-with-last-character-incremented, i.e. "fund", and adding 1
2532 to 0xff should wraparound and carry to the previous character.
2533 See comments in make_sort_after_prefix_name. */
2536 /* Some more symbols with \377 (0xff). See above. */
2540 /* A name with all sorts of complications. Starts with "z" to make
2541 it easier for the completion tests below. */
2542 #define Z_SYM_NAME \
2543 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
2544 "::tuple<(anonymous namespace)::ui*, " \
2545 "std::default_delete<(anonymous namespace)::ui>, void>"
2550 /* Returns true if the mapped_index_base::find_name_component_bounds
2551 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
2552 in completion mode. */
2555 check_find_bounds_finds (mapped_index_base
&index
,
2556 const char *search_name
,
2557 gdb::array_view
<const char *> expected_syms
,
2558 dwarf2_per_objfile
*per_objfile
)
2560 lookup_name_info
lookup_name (search_name
,
2561 symbol_name_match_type::FULL
, true);
2563 auto bounds
= index
.find_name_components_bounds (lookup_name
,
2567 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
2568 if (distance
!= expected_syms
.size ())
2571 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
2573 auto nc_elem
= bounds
.first
+ exp_elem
;
2574 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
2575 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
2582 /* Test the lower-level mapped_index::find_name_component_bounds
2586 test_mapped_index_find_name_component_bounds ()
2588 mock_mapped_index
mock_index (test_symbols
);
2590 mock_index
.build_name_components (NULL
/* per_objfile */);
2592 /* Test the lower-level mapped_index::find_name_component_bounds
2593 method in completion mode. */
2595 static const char *expected_syms
[] = {
2600 SELF_CHECK (check_find_bounds_finds
2601 (mock_index
, "t1_func", expected_syms
,
2602 NULL
/* per_objfile */));
2605 /* Check that the increment-last-char in the name matching algorithm
2606 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
2607 make_sort_after_prefix_name. */
2609 static const char *expected_syms1
[] = {
2613 SELF_CHECK (check_find_bounds_finds
2614 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
2616 static const char *expected_syms2
[] = {
2619 SELF_CHECK (check_find_bounds_finds
2620 (mock_index
, "\377\377", expected_syms2
,
2621 NULL
/* per_objfile */));
2625 /* Test dw2_expand_symtabs_matching_symbol. */
2628 test_dw2_expand_symtabs_matching_symbol ()
2630 mock_mapped_index
mock_index (test_symbols
);
2632 /* We let all tests run until the end even if some fails, for debug
2634 bool any_mismatch
= false;
2636 /* Create the expected symbols list (an initializer_list). Needed
2637 because lists have commas, and we need to pass them to CHECK,
2638 which is a macro. */
2639 #define EXPECT(...) { __VA_ARGS__ }
2641 /* Wrapper for check_match that passes down the current
2642 __FILE__/__LINE__. */
2643 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
2644 any_mismatch |= !check_match (__FILE__, __LINE__, \
2646 NAME, MATCH_TYPE, COMPLETION_MODE, \
2647 EXPECTED_LIST, NULL)
2649 /* Identity checks. */
2650 for (const char *sym
: test_symbols
)
2652 /* Should be able to match all existing symbols. */
2653 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
2656 /* Should be able to match all existing symbols with
2658 std::string with_params
= std::string (sym
) + "(int)";
2659 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
2662 /* Should be able to match all existing symbols with
2663 parameters and qualifiers. */
2664 with_params
= std::string (sym
) + " ( int ) const";
2665 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
2668 /* This should really find sym, but cp-name-parser.y doesn't
2669 know about lvalue/rvalue qualifiers yet. */
2670 with_params
= std::string (sym
) + " ( int ) &&";
2671 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
2675 /* Check that the name matching algorithm for completion doesn't get
2676 confused with Latin1 'ÿ' / 0xff. See
2677 make_sort_after_prefix_name. */
2679 static const char str
[] = "\377";
2680 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
2681 EXPECT ("\377", "\377\377123"));
2684 /* Check that the increment-last-char in the matching algorithm for
2685 completion doesn't match "t1_fund" when completing "t1_func". */
2687 static const char str
[] = "t1_func";
2688 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
2689 EXPECT ("t1_func", "t1_func1"));
2692 /* Check that completion mode works at each prefix of the expected
2695 static const char str
[] = "function(int)";
2696 size_t len
= strlen (str
);
2699 for (size_t i
= 1; i
< len
; i
++)
2701 lookup
.assign (str
, i
);
2702 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
2703 EXPECT ("function"));
2707 /* While "w" is a prefix of both components, the match function
2708 should still only be called once. */
2710 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
2712 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
2716 /* Same, with a "complicated" symbol. */
2718 static const char str
[] = Z_SYM_NAME
;
2719 size_t len
= strlen (str
);
2722 for (size_t i
= 1; i
< len
; i
++)
2724 lookup
.assign (str
, i
);
2725 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
2726 EXPECT (Z_SYM_NAME
));
2730 /* In FULL mode, an incomplete symbol doesn't match. */
2732 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
2736 /* A complete symbol with parameters matches any overload, since the
2737 index has no overload info. */
2739 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
2740 EXPECT ("std::zfunction", "std::zfunction2"));
2741 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
2742 EXPECT ("std::zfunction", "std::zfunction2"));
2743 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
2744 EXPECT ("std::zfunction", "std::zfunction2"));
2747 /* Check that whitespace is ignored appropriately. A symbol with a
2748 template argument list. */
2750 static const char expected
[] = "ns::foo<int>";
2751 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
2753 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
2757 /* Check that whitespace is ignored appropriately. A symbol with a
2758 template argument list that includes a pointer. */
2760 static const char expected
[] = "ns::foo<char*>";
2761 /* Try both completion and non-completion modes. */
2762 static const bool completion_mode
[2] = {false, true};
2763 for (size_t i
= 0; i
< 2; i
++)
2765 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
2766 completion_mode
[i
], EXPECT (expected
));
2767 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
2768 completion_mode
[i
], EXPECT (expected
));
2770 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
2771 completion_mode
[i
], EXPECT (expected
));
2772 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
2773 completion_mode
[i
], EXPECT (expected
));
2778 /* Check method qualifiers are ignored. */
2779 static const char expected
[] = "ns::foo<char*>";
2780 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
2781 symbol_name_match_type::FULL
, true, EXPECT (expected
));
2782 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
2783 symbol_name_match_type::FULL
, true, EXPECT (expected
));
2784 CHECK_MATCH ("foo < char * > ( int ) const",
2785 symbol_name_match_type::WILD
, true, EXPECT (expected
));
2786 CHECK_MATCH ("foo < char * > ( int ) &&",
2787 symbol_name_match_type::WILD
, true, EXPECT (expected
));
2790 /* Test lookup names that don't match anything. */
2792 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
2795 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
2799 /* Some wild matching tests, exercising "(anonymous namespace)",
2800 which should not be confused with a parameter list. */
2802 static const char *syms
[] = {
2806 "A :: B :: C ( int )",
2811 for (const char *s
: syms
)
2813 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
2814 EXPECT ("(anonymous namespace)::A::B::C"));
2819 static const char expected
[] = "ns2::tmpl<int>::foo2";
2820 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
2822 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
2826 SELF_CHECK (!any_mismatch
);
2835 test_mapped_index_find_name_component_bounds ();
2836 test_dw2_expand_symtabs_matching_symbol ();
2839 }} // namespace selftests::dw2_expand_symtabs_matching
2841 #endif /* GDB_SELF_TEST */
2846 dw2_expand_symtabs_matching_one
2847 (dwarf2_per_cu_data
*per_cu
,
2848 dwarf2_per_objfile
*per_objfile
,
2849 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
2850 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
2852 if (file_matcher
== NULL
|| per_cu
->mark
)
2854 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
2856 compunit_symtab
*symtab
2857 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
2858 gdb_assert (symtab
!= nullptr);
2860 if (expansion_notify
!= NULL
&& symtab_was_null
)
2861 return expansion_notify (symtab
);
2869 dw_expand_symtabs_matching_file_matcher
2870 (dwarf2_per_objfile
*per_objfile
,
2871 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
2873 if (file_matcher
== NULL
)
2876 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
2878 NULL
, xcalloc
, xfree
));
2879 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
2881 NULL
, xcalloc
, xfree
));
2883 /* The rule is CUs specify all the files, including those used by
2884 any TU, so there's no need to scan TUs here. */
2886 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
2890 if (per_cu
->is_debug_types
)
2894 /* We only need to look at symtabs not already expanded. */
2895 if (per_objfile
->symtab_set_p (per_cu
.get ()))
2898 if (per_cu
->fnd
!= nullptr)
2900 file_and_directory
*fnd
= per_cu
->fnd
.get ();
2902 if (file_matcher (fnd
->get_name (), false))
2908 /* Before we invoke realpath, which can get expensive when many
2909 files are involved, do a quick comparison of the basenames. */
2910 if ((basenames_may_differ
2911 || file_matcher (lbasename (fnd
->get_name ()), true))
2912 && file_matcher (fnd
->get_fullname (), false))
2919 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
2921 if (file_data
== NULL
)
2924 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
2926 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
2932 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
2934 const char *this_real_name
;
2936 if (file_matcher (file_data
->file_names
[j
], false))
2942 /* Before we invoke realpath, which can get expensive when many
2943 files are involved, do a quick comparison of the basenames. */
2944 if (!basenames_may_differ
2945 && !file_matcher (lbasename (file_data
->file_names
[j
]),
2949 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
2950 if (file_matcher (this_real_name
, false))
2957 void **slot
= htab_find_slot (per_cu
->mark
2958 ? visited_found
.get ()
2959 : visited_not_found
.get (),
2966 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
2969 static struct compunit_symtab
*
2970 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
2975 if (cust
->blockvector () != nullptr
2976 && blockvector_contains_pc (cust
->blockvector (), pc
))
2979 if (cust
->includes
== NULL
)
2982 for (i
= 0; cust
->includes
[i
]; ++i
)
2984 struct compunit_symtab
*s
= cust
->includes
[i
];
2986 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
2994 dwarf2_per_cu_data
*
2995 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
2996 unrelocated_addr adjusted_pc
)
2998 if (per_bfd
->index_addrmap
== nullptr)
3001 void *obj
= per_bfd
->index_addrmap
->find ((CORE_ADDR
) adjusted_pc
);
3002 return static_cast<dwarf2_per_cu_data
*> (obj
);
3005 struct compunit_symtab
*
3006 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
3007 (struct objfile
*objfile
,
3008 struct bound_minimal_symbol msymbol
,
3010 struct obj_section
*section
,
3013 struct compunit_symtab
*result
;
3015 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3017 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
3018 struct dwarf2_per_cu_data
*data
3019 = find_per_cu (per_objfile
->per_bfd
, (unrelocated_addr
) (pc
- baseaddr
));
3020 if (data
== nullptr)
3023 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
3024 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3025 paddress (objfile
->arch (), pc
));
3027 result
= recursively_find_pc_sect_compunit_symtab
3028 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
3030 if (warn_if_readin
&& result
== nullptr)
3031 warning (_("(Error: pc %s in address map, but not in symtab.)"),
3032 paddress (objfile
->arch (), pc
));
3038 dwarf2_base_index_functions::map_symbol_filenames
3039 (struct objfile
*objfile
,
3040 gdb::function_view
<symbol_filename_ftype
> fun
,
3043 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3045 /* Use caches to ensure we only call FUN once for each filename. */
3046 filename_seen_cache filenames_cache
;
3047 std::unordered_set
<quick_file_names
*> qfn_cache
;
3049 /* The rule is CUs specify all the files, including those used by any TU,
3050 so there's no need to scan TUs here. We can ignore file names coming
3051 from already-expanded CUs. It is possible that an expanded CU might
3052 reuse the file names data from a currently unexpanded CU, in this
3053 case we don't want to report the files from the unexpanded CU. */
3055 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
3057 if (!per_cu
->is_debug_types
3058 && per_objfile
->symtab_set_p (per_cu
.get ()))
3060 if (per_cu
->file_names
!= nullptr)
3061 qfn_cache
.insert (per_cu
->file_names
);
3065 for (dwarf2_per_cu_data
*per_cu
3066 : all_units_range (per_objfile
->per_bfd
))
3068 /* We only need to look at symtabs not already expanded. */
3069 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
3072 if (per_cu
->fnd
!= nullptr)
3074 file_and_directory
*fnd
= per_cu
->fnd
.get ();
3076 const char *filename
= fnd
->get_name ();
3077 const char *key
= filename
;
3078 const char *fullname
= nullptr;
3082 fullname
= fnd
->get_fullname ();
3086 if (!filenames_cache
.seen (key
))
3087 fun (filename
, fullname
);
3090 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
3091 if (file_data
== nullptr
3092 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
3095 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
3097 const char *filename
= file_data
->file_names
[j
];
3098 const char *key
= filename
;
3099 const char *fullname
= nullptr;
3103 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
3107 if (!filenames_cache
.seen (key
))
3108 fun (filename
, fullname
);
3114 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
3119 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
3122 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
3124 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3126 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
3128 /* Is this already expanded? */
3129 if (per_objfile
->symtab_set_p (per_cu
.get ()))
3132 /* It has not yet been expanded. */
3139 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
3140 to either a dwarf2_per_bfd or dwz_file object. */
3142 template <typename T
>
3143 static gdb::array_view
<const gdb_byte
>
3144 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
3146 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
3148 if (section
->empty ())
3151 /* Older elfutils strip versions could keep the section in the main
3152 executable while splitting it for the separate debug info file. */
3153 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
3156 section
->read (obj
);
3158 /* dwarf2_section_info::size is a bfd_size_type, while
3159 gdb::array_view works with size_t. On 32-bit hosts, with
3160 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
3161 is 32-bit. So we need an explicit narrowing conversion here.
3162 This is fine, because it's impossible to allocate or mmap an
3163 array/buffer larger than what size_t can represent. */
3164 return gdb::make_array_view (section
->buffer
, section
->size
);
3167 /* Lookup the index cache for the contents of the index associated to
3170 static gdb::array_view
<const gdb_byte
>
3171 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
3173 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
.get ());
3174 if (build_id
== nullptr)
3177 return global_index_cache
.lookup_gdb_index (build_id
,
3178 &dwarf2_per_bfd
->index_cache_res
);
3181 /* Same as the above, but for DWZ. */
3183 static gdb::array_view
<const gdb_byte
>
3184 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
3186 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
3187 if (build_id
== nullptr)
3190 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
3193 static quick_symbol_functions_up make_cooked_index_funcs
3194 (dwarf2_per_objfile
*);
3196 /* See dwarf2/public.h. */
3199 dwarf2_initialize_objfile (struct objfile
*objfile
,
3200 const struct dwarf2_debug_sections
*names
,
3203 if (!dwarf2_has_info (objfile
, names
, can_copy
))
3206 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3207 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
3209 dwarf_read_debug_printf ("called");
3211 /* Try to fetch any potential dwz file early, while still on the
3215 dwarf2_read_dwz_file (per_objfile
);
3217 catch (const gdb_exception_error
&err
)
3219 warning (_("%s"), err
.what ());
3222 /* If we're about to read full symbols, don't bother with the
3223 indices. In this case we also don't care if some other debug
3224 format is making psymtabs, because they are all about to be
3226 if ((objfile
->flags
& OBJF_READNOW
))
3228 dwarf_read_debug_printf ("readnow requested");
3230 create_all_units (per_objfile
);
3231 per_bfd
->quick_file_names_table
3232 = create_quick_file_names_table (per_bfd
->all_units
.size ());
3234 objfile
->qf
.emplace_front (new readnow_functions
);
3236 /* Was a GDB index already read when we processed an objfile sharing
3238 else if (per_bfd
->index_table
!= nullptr)
3240 dwarf_read_debug_printf ("re-using symbols");
3241 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
3243 else if (dwarf2_read_debug_names (per_objfile
))
3245 dwarf_read_debug_printf ("found debug names");
3246 objfile
->qf
.push_front
3247 (per_bfd
->index_table
->make_quick_functions ());
3249 else if (dwarf2_read_gdb_index (per_objfile
,
3250 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
3251 get_gdb_index_contents_from_section
<dwz_file
>))
3253 dwarf_read_debug_printf ("found gdb index from file");
3254 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
3256 /* ... otherwise, try to find the index in the index cache. */
3257 else if (dwarf2_read_gdb_index (per_objfile
,
3258 get_gdb_index_contents_from_cache
,
3259 get_gdb_index_contents_from_cache_dwz
))
3261 dwarf_read_debug_printf ("found gdb index from cache");
3262 global_index_cache
.hit ();
3263 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
3267 global_index_cache
.miss ();
3268 objfile
->qf
.push_front (make_cooked_index_funcs (per_objfile
));
3275 /* Find the base address of the compilation unit for range lists and
3276 location lists. It will normally be specified by DW_AT_low_pc.
3277 In DWARF-3 draft 4, the base address could be overridden by
3278 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3279 compilation units with discontinuous ranges. */
3282 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
3284 struct attribute
*attr
;
3286 cu
->base_address
.reset ();
3288 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
3289 if (attr
!= nullptr)
3290 cu
->base_address
= attr
->as_address ();
3293 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
3294 if (attr
!= nullptr)
3295 cu
->base_address
= attr
->as_address ();
3299 /* Helper function that returns the proper abbrev section for
3302 static struct dwarf2_section_info
*
3303 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
3305 struct dwarf2_section_info
*abbrev
;
3306 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
3308 if (this_cu
->is_dwz
)
3309 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
3311 abbrev
= &per_bfd
->abbrev
;
3316 /* Fetch the abbreviation table offset from a comp or type unit header. */
3319 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
3320 struct dwarf2_section_info
*section
,
3321 sect_offset sect_off
)
3323 bfd
*abfd
= section
->get_bfd_owner ();
3324 const gdb_byte
*info_ptr
;
3325 unsigned int initial_length_size
, offset_size
;
3328 section
->read (per_objfile
->objfile
);
3329 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
3330 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
3331 offset_size
= initial_length_size
== 4 ? 4 : 8;
3332 info_ptr
+= initial_length_size
;
3334 version
= read_2_bytes (abfd
, info_ptr
);
3338 /* Skip unit type and address size. */
3342 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
3346 hash_signatured_type (const void *item
)
3348 const struct signatured_type
*sig_type
3349 = (const struct signatured_type
*) item
;
3351 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3352 return sig_type
->signature
;
3356 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
3358 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
3359 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
3361 return lhs
->signature
== rhs
->signature
;
3367 allocate_signatured_type_table ()
3369 return htab_up (htab_create_alloc (41,
3370 hash_signatured_type
,
3372 NULL
, xcalloc
, xfree
));
3375 /* A helper for create_debug_types_hash_table. Read types from SECTION
3376 and fill them into TYPES_HTAB. It will process only type units,
3377 therefore DW_UT_type. */
3380 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
3381 struct dwo_file
*dwo_file
,
3382 dwarf2_section_info
*section
, htab_up
&types_htab
,
3383 rcuh_kind section_kind
)
3385 struct objfile
*objfile
= per_objfile
->objfile
;
3386 struct dwarf2_section_info
*abbrev_section
;
3388 const gdb_byte
*info_ptr
, *end_ptr
;
3390 abbrev_section
= &dwo_file
->sections
.abbrev
;
3392 dwarf_read_debug_printf ("Reading %s for %s",
3393 section
->get_name (),
3394 abbrev_section
->get_file_name ());
3396 section
->read (objfile
);
3397 info_ptr
= section
->buffer
;
3399 if (info_ptr
== NULL
)
3402 /* We can't set abfd until now because the section may be empty or
3403 not present, in which case the bfd is unknown. */
3404 abfd
= section
->get_bfd_owner ();
3406 /* We don't use cutu_reader here because we don't need to read
3407 any dies: the signature is in the header. */
3409 end_ptr
= info_ptr
+ section
->size
;
3410 while (info_ptr
< end_ptr
)
3412 signatured_type_up sig_type
;
3413 struct dwo_unit
*dwo_tu
;
3415 const gdb_byte
*ptr
= info_ptr
;
3416 struct comp_unit_head header
;
3417 unsigned int length
;
3419 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
3421 /* Initialize it due to a false compiler warning. */
3422 header
.signature
= -1;
3423 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
3425 /* We need to read the type's signature in order to build the hash
3426 table, but we don't need anything else just yet. */
3428 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
3429 abbrev_section
, ptr
, section_kind
);
3431 length
= header
.get_length_with_initial ();
3433 /* Skip dummy type units. */
3434 if (ptr
>= info_ptr
+ length
3435 || peek_abbrev_code (abfd
, ptr
) == 0
3436 || (header
.unit_type
!= DW_UT_type
3437 && header
.unit_type
!= DW_UT_split_type
))
3443 if (types_htab
== NULL
)
3444 types_htab
= allocate_dwo_unit_table ();
3446 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
3447 dwo_tu
->dwo_file
= dwo_file
;
3448 dwo_tu
->signature
= header
.signature
;
3449 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
3450 dwo_tu
->section
= section
;
3451 dwo_tu
->sect_off
= sect_off
;
3452 dwo_tu
->length
= length
;
3454 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
3455 gdb_assert (slot
!= NULL
);
3457 complaint (_("debug type entry at offset %s is duplicate to"
3458 " the entry at offset %s, signature %s"),
3459 sect_offset_str (sect_off
),
3460 sect_offset_str (dwo_tu
->sect_off
),
3461 hex_string (header
.signature
));
3464 dwarf_read_debug_printf_v (" offset %s, signature %s",
3465 sect_offset_str (sect_off
),
3466 hex_string (header
.signature
));
3472 /* Create the hash table of all entries in the .debug_types
3473 (or .debug_types.dwo) section(s).
3474 DWO_FILE is a pointer to the DWO file object.
3476 The result is a pointer to the hash table or NULL if there are no types.
3478 Note: This function processes DWO files only, not DWP files. */
3481 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
3482 struct dwo_file
*dwo_file
,
3483 gdb::array_view
<dwarf2_section_info
> type_sections
,
3484 htab_up
&types_htab
)
3486 for (dwarf2_section_info
§ion
: type_sections
)
3487 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
3491 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
3492 If SLOT is non-NULL, it is the entry to use in the hash table.
3493 Otherwise we find one. */
3495 static struct signatured_type
*
3496 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
3498 if (per_objfile
->per_bfd
->all_units
.size ()
3499 == per_objfile
->per_bfd
->all_units
.capacity ())
3500 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
3502 signatured_type_up sig_type_holder
3503 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
3504 signatured_type
*sig_type
= sig_type_holder
.get ();
3506 per_objfile
->per_bfd
->all_units
.emplace_back
3507 (sig_type_holder
.release ());
3511 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
3514 gdb_assert (*slot
== NULL
);
3516 /* The rest of sig_type must be filled in by the caller. */
3520 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
3521 Fill in SIG_ENTRY with DWO_ENTRY. */
3524 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
3525 struct signatured_type
*sig_entry
,
3526 struct dwo_unit
*dwo_entry
)
3528 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
3530 /* Make sure we're not clobbering something we don't expect to. */
3531 gdb_assert (! sig_entry
->queued
);
3532 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
3533 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
3534 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
3535 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
3536 || (to_underlying (sig_entry
->type_offset_in_section
)
3537 == to_underlying (dwo_entry
->type_offset_in_tu
)));
3538 gdb_assert (sig_entry
->type_unit_group
== NULL
);
3539 gdb_assert (sig_entry
->dwo_unit
== NULL
3540 || sig_entry
->dwo_unit
== dwo_entry
);
3542 sig_entry
->section
= dwo_entry
->section
;
3543 sig_entry
->sect_off
= dwo_entry
->sect_off
;
3544 sig_entry
->set_length (dwo_entry
->length
, false);
3545 sig_entry
->reading_dwo_directly
= 1;
3546 sig_entry
->per_bfd
= per_bfd
;
3547 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
3548 sig_entry
->dwo_unit
= dwo_entry
;
3551 /* Subroutine of lookup_signatured_type.
3552 If we haven't read the TU yet, create the signatured_type data structure
3553 for a TU to be read in directly from a DWO file, bypassing the stub.
3554 This is the "Stay in DWO Optimization": When there is no DWP file and we're
3555 using .gdb_index, then when reading a CU we want to stay in the DWO file
3556 containing that CU. Otherwise we could end up reading several other DWO
3557 files (due to comdat folding) to process the transitive closure of all the
3558 mentioned TUs, and that can be slow. The current DWO file will have every
3559 type signature that it needs.
3560 We only do this for .gdb_index because in the psymtab case we already have
3561 to read all the DWOs to build the type unit groups. */
3563 static struct signatured_type
*
3564 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
3566 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3567 struct dwo_file
*dwo_file
;
3568 struct dwo_unit find_dwo_entry
, *dwo_entry
;
3571 gdb_assert (cu
->dwo_unit
);
3573 /* If TU skeletons have been removed then we may not have read in any
3575 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
3576 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
3578 /* We only ever need to read in one copy of a signatured type.
3579 Use the global signatured_types array to do our own comdat-folding
3580 of types. If this is the first time we're reading this TU, and
3581 the TU has an entry in .gdb_index, replace the recorded data from
3582 .gdb_index with this TU. */
3584 signatured_type
find_sig_entry (sig
);
3585 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
3586 &find_sig_entry
, INSERT
);
3587 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
3589 /* We can get here with the TU already read, *or* in the process of being
3590 read. Don't reassign the global entry to point to this DWO if that's
3591 the case. Also note that if the TU is already being read, it may not
3592 have come from a DWO, the program may be a mix of Fission-compiled
3593 code and non-Fission-compiled code. */
3595 /* Have we already tried to read this TU?
3596 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3597 needn't exist in the global table yet). */
3598 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
3601 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
3602 dwo_unit of the TU itself. */
3603 dwo_file
= cu
->dwo_unit
->dwo_file
;
3605 /* Ok, this is the first time we're reading this TU. */
3606 if (dwo_file
->tus
== NULL
)
3608 find_dwo_entry
.signature
= sig
;
3609 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
3611 if (dwo_entry
== NULL
)
3614 /* If the global table doesn't have an entry for this TU, add one. */
3615 if (sig_entry
== NULL
)
3616 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
3618 if (sig_entry
->dwo_unit
== nullptr)
3619 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
3620 sig_entry
->tu_read
= 1;
3624 /* Subroutine of lookup_signatured_type.
3625 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
3626 then try the DWP file. If the TU stub (skeleton) has been removed then
3627 it won't be in .gdb_index. */
3629 static struct signatured_type
*
3630 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
3632 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3633 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
3634 struct dwo_unit
*dwo_entry
;
3637 gdb_assert (cu
->dwo_unit
);
3638 gdb_assert (dwp_file
!= NULL
);
3640 /* If TU skeletons have been removed then we may not have read in any
3642 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
3643 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
3645 signatured_type
find_sig_entry (sig
);
3646 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
3647 &find_sig_entry
, INSERT
);
3648 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
3650 /* Have we already tried to read this TU?
3651 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3652 needn't exist in the global table yet). */
3653 if (sig_entry
!= NULL
)
3656 if (dwp_file
->tus
== NULL
)
3658 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
3659 1 /* is_debug_types */);
3660 if (dwo_entry
== NULL
)
3663 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
3664 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
3669 /* Lookup a signature based type for DW_FORM_ref_sig8.
3670 Returns NULL if signature SIG is not present in the table.
3671 It is up to the caller to complain about this. */
3673 static struct signatured_type
*
3674 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
3676 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3680 /* We're in a DWO/DWP file, and we're using .gdb_index.
3681 These cases require special processing. */
3682 if (get_dwp_file (per_objfile
) == NULL
)
3683 return lookup_dwo_signatured_type (cu
, sig
);
3685 return lookup_dwp_signatured_type (cu
, sig
);
3689 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
3691 signatured_type
find_entry (sig
);
3692 return ((struct signatured_type
*)
3693 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
3698 /* Low level DIE reading support. */
3700 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3703 init_cu_die_reader (struct die_reader_specs
*reader
,
3704 struct dwarf2_cu
*cu
,
3705 struct dwarf2_section_info
*section
,
3706 struct dwo_file
*dwo_file
,
3707 struct abbrev_table
*abbrev_table
)
3709 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
3710 reader
->abfd
= section
->get_bfd_owner ();
3712 reader
->dwo_file
= dwo_file
;
3713 reader
->die_section
= section
;
3714 reader
->buffer
= section
->buffer
;
3715 reader
->buffer_end
= section
->buffer
+ section
->size
;
3716 reader
->abbrev_table
= abbrev_table
;
3719 /* Subroutine of cutu_reader to simplify it.
3720 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
3721 There's just a lot of work to do, and cutu_reader is big enough
3724 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
3725 from it to the DIE in the DWO. If NULL we are skipping the stub.
3726 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
3727 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
3728 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
3729 STUB_COMP_DIR may be non-NULL.
3730 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
3731 are filled in with the info of the DIE from the DWO file.
3732 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
3733 from the dwo. Since *RESULT_READER references this abbrev table, it must be
3734 kept around for at least as long as *RESULT_READER.
3736 The result is non-zero if a valid (non-dummy) DIE was found. */
3739 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
3740 struct dwo_unit
*dwo_unit
,
3741 struct die_info
*stub_comp_unit_die
,
3742 const char *stub_comp_dir
,
3743 struct die_reader_specs
*result_reader
,
3744 const gdb_byte
**result_info_ptr
,
3745 struct die_info
**result_comp_unit_die
,
3746 abbrev_table_up
*result_dwo_abbrev_table
)
3748 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3749 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
3750 struct objfile
*objfile
= per_objfile
->objfile
;
3752 const gdb_byte
*begin_info_ptr
, *info_ptr
;
3753 struct dwarf2_section_info
*dwo_abbrev_section
;
3755 /* At most one of these may be provided. */
3756 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
3758 /* These attributes aren't processed until later: DW_AT_stmt_list,
3759 DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_comp_dir.
3760 However, these attributes are found in the stub which we won't
3761 have later. In order to not impose this complication on the rest
3762 of the code, we read them here and copy them to the DWO CU/TU
3765 /* We store them all in an array. */
3766 struct attribute
*attributes
[5] {};
3767 /* Next available element of the attributes array. */
3768 int next_attr_idx
= 0;
3770 /* Push an element into ATTRIBUTES. */
3771 auto push_back
= [&] (struct attribute
*attr
)
3773 gdb_assert (next_attr_idx
< ARRAY_SIZE (attributes
));
3774 if (attr
!= nullptr)
3775 attributes
[next_attr_idx
++] = attr
;
3778 if (stub_comp_unit_die
!= NULL
)
3780 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3782 if (!per_cu
->is_debug_types
)
3783 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
));
3784 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
));
3785 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
));
3786 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
));
3787 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
));
3789 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
3791 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3792 We need the value before we can process DW_AT_ranges values from the
3794 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
3796 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3797 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3798 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3799 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3801 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
3803 else if (stub_comp_dir
!= NULL
)
3805 /* Reconstruct the comp_dir attribute to simplify the code below. */
3806 struct attribute
*comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
,
3808 comp_dir
->name
= DW_AT_comp_dir
;
3809 comp_dir
->form
= DW_FORM_string
;
3810 comp_dir
->set_string_noncanonical (stub_comp_dir
);
3811 push_back (comp_dir
);
3814 /* Set up for reading the DWO CU/TU. */
3815 cu
->dwo_unit
= dwo_unit
;
3816 dwarf2_section_info
*section
= dwo_unit
->section
;
3817 section
->read (objfile
);
3818 abfd
= section
->get_bfd_owner ();
3819 begin_info_ptr
= info_ptr
= (section
->buffer
3820 + to_underlying (dwo_unit
->sect_off
));
3821 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
3823 if (per_cu
->is_debug_types
)
3825 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
3827 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
3828 section
, dwo_abbrev_section
,
3829 info_ptr
, rcuh_kind::TYPE
);
3830 /* This is not an assert because it can be caused by bad debug info. */
3831 if (sig_type
->signature
!= cu
->header
.signature
)
3833 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
3834 " TU at offset %s [in module %s]"),
3835 hex_string (sig_type
->signature
),
3836 hex_string (cu
->header
.signature
),
3837 sect_offset_str (dwo_unit
->sect_off
),
3838 bfd_get_filename (abfd
));
3840 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
3841 /* For DWOs coming from DWP files, we don't know the CU length
3842 nor the type's offset in the TU until now. */
3843 dwo_unit
->length
= cu
->header
.get_length_with_initial ();
3844 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
3846 /* Establish the type offset that can be used to lookup the type.
3847 For DWO files, we don't know it until now. */
3848 sig_type
->type_offset_in_section
3849 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
3853 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
3854 section
, dwo_abbrev_section
,
3855 info_ptr
, rcuh_kind::COMPILE
);
3856 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
3857 /* For DWOs coming from DWP files, we don't know the CU length
3859 dwo_unit
->length
= cu
->header
.get_length_with_initial ();
3862 dwo_abbrev_section
->read (objfile
);
3863 *result_dwo_abbrev_table
3864 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
3865 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
3866 result_dwo_abbrev_table
->get ());
3868 /* Read in the die, filling in the attributes from the stub. This
3869 has the benefit of simplifying the rest of the code - all the
3870 work to maintain the illusion of a single
3871 DW_TAG_{compile,type}_unit DIE is done here. */
3872 info_ptr
= read_toplevel_die (result_reader
, result_comp_unit_die
, info_ptr
,
3873 gdb::make_array_view (attributes
,
3876 /* Skip dummy compilation units. */
3877 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
3878 || peek_abbrev_code (abfd
, info_ptr
) == 0)
3881 *result_info_ptr
= info_ptr
;
3885 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
3886 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
3887 signature is part of the header. */
3888 static std::optional
<ULONGEST
>
3889 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
3891 if (cu
->header
.version
>= 5)
3892 return cu
->header
.signature
;
3893 struct attribute
*attr
;
3894 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
3895 if (attr
== nullptr || !attr
->form_is_unsigned ())
3896 return std::optional
<ULONGEST
> ();
3897 return attr
->as_unsigned ();
3900 /* Subroutine of cutu_reader to simplify it.
3901 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
3902 Returns NULL if the specified DWO unit cannot be found. */
3904 static struct dwo_unit
*
3905 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
3908 /* We need a lock here to handle the DWO hash table. */
3909 static std::mutex dwo_lock
;
3911 std::lock_guard
<std::mutex
> guard (dwo_lock
);
3914 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
3915 struct dwo_unit
*dwo_unit
;
3916 const char *comp_dir
;
3918 gdb_assert (cu
!= NULL
);
3920 /* Yeah, we look dwo_name up again, but it simplifies the code. */
3921 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
3922 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
3924 if (per_cu
->is_debug_types
)
3925 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
3928 std::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
3930 if (!signature
.has_value ())
3931 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
3933 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
3935 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
3941 /* Subroutine of cutu_reader to simplify it.
3942 See it for a description of the parameters.
3943 Read a TU directly from a DWO file, bypassing the stub. */
3946 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
3947 dwarf2_per_objfile
*per_objfile
,
3948 dwarf2_cu
*existing_cu
)
3950 struct signatured_type
*sig_type
;
3952 /* Verify we can do the following downcast, and that we have the
3954 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
3955 sig_type
= (struct signatured_type
*) this_cu
;
3956 gdb_assert (sig_type
->dwo_unit
!= NULL
);
3960 if (existing_cu
!= nullptr)
3963 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
3964 /* There's no need to do the rereading_dwo_cu handling that
3965 cutu_reader does since we don't read the stub. */
3969 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
3970 in per_objfile yet. */
3971 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
3972 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
3973 cu
= m_new_cu
.get ();
3976 /* A future optimization, if needed, would be to use an existing
3977 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
3978 could share abbrev tables. */
3980 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
3981 NULL
/* stub_comp_unit_die */,
3982 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
3985 &m_dwo_abbrev_table
) == 0)
3992 /* Initialize a CU (or TU) and read its DIEs.
3993 If the CU defers to a DWO file, read the DWO file as well.
3995 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
3996 Otherwise the table specified in the comp unit header is read in and used.
3997 This is an optimization for when we already have the abbrev table.
3999 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
4002 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
4003 dwarf2_per_objfile
*per_objfile
,
4004 struct abbrev_table
*abbrev_table
,
4005 dwarf2_cu
*existing_cu
,
4007 abbrev_cache
*cache
)
4008 : die_reader_specs
{},
4011 struct objfile
*objfile
= per_objfile
->objfile
;
4012 struct dwarf2_section_info
*section
= this_cu
->section
;
4013 bfd
*abfd
= section
->get_bfd_owner ();
4014 const gdb_byte
*begin_info_ptr
;
4015 struct signatured_type
*sig_type
= NULL
;
4016 struct dwarf2_section_info
*abbrev_section
;
4017 /* Non-zero if CU currently points to a DWO file and we need to
4018 reread it. When this happens we need to reread the skeleton die
4019 before we can reread the DWO file (this only applies to CUs, not TUs). */
4020 int rereading_dwo_cu
= 0;
4022 if (dwarf_die_debug
)
4023 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
4024 this_cu
->is_debug_types
? "type" : "comp",
4025 sect_offset_str (this_cu
->sect_off
));
4027 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4028 file (instead of going through the stub), short-circuit all of this. */
4029 if (this_cu
->reading_dwo_directly
)
4031 /* Narrow down the scope of possibilities to have to understand. */
4032 gdb_assert (this_cu
->is_debug_types
);
4033 gdb_assert (abbrev_table
== NULL
);
4034 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
4038 /* This is cheap if the section is already read in. */
4039 section
->read (objfile
);
4041 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
4043 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
4047 if (existing_cu
!= nullptr)
4050 /* If this CU is from a DWO file we need to start over, we need to
4051 refetch the attributes from the skeleton CU.
4052 This could be optimized by retrieving those attributes from when we
4053 were here the first time: the previous comp_unit_die was stored in
4054 comp_unit_obstack. But there's no data yet that we need this
4056 if (cu
->dwo_unit
!= NULL
)
4057 rereading_dwo_cu
= 1;
4061 /* If an existing_cu is provided, a dwarf2_cu must not exist for
4062 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
4063 let us know that the CU is being scanned using the parallel
4064 indexer. This assert is avoided in this case because (1) it
4065 is irrelevant, and (2) the get_cu method is not
4067 gdb_assert (cache
!= nullptr
4068 || per_objfile
->get_cu (this_cu
) == nullptr);
4069 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
4070 cu
= m_new_cu
.get ();
4073 /* Get the header. */
4074 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
4076 /* We already have the header, there's no need to read it in again. */
4077 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
4081 if (this_cu
->is_debug_types
)
4083 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
4084 section
, abbrev_section
,
4085 info_ptr
, rcuh_kind::TYPE
);
4087 /* Since per_cu is the first member of struct signatured_type,
4088 we can go from a pointer to one to a pointer to the other. */
4089 sig_type
= (struct signatured_type
*) this_cu
;
4090 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
4091 gdb_assert (sig_type
->type_offset_in_tu
4092 == cu
->header
.type_cu_offset_in_tu
);
4093 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
4095 /* LENGTH has not been set yet for type units if we're
4096 using .gdb_index. */
4097 this_cu
->set_length (cu
->header
.get_length_with_initial ());
4099 /* Establish the type offset that can be used to lookup the type. */
4100 sig_type
->type_offset_in_section
=
4101 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
4103 this_cu
->set_version (cu
->header
.version
);
4107 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
4108 section
, abbrev_section
,
4110 rcuh_kind::COMPILE
);
4112 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
4113 this_cu
->set_length (cu
->header
.get_length_with_initial ());
4114 this_cu
->set_version (cu
->header
.version
);
4118 /* Skip dummy compilation units. */
4119 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
4120 || peek_abbrev_code (abfd
, info_ptr
) == 0)
4126 /* If we don't have them yet, read the abbrevs for this compilation unit.
4127 And if we need to read them now, make sure they're freed when we're
4129 if (abbrev_table
!= NULL
)
4130 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
4133 if (cache
!= nullptr)
4134 abbrev_table
= cache
->find (abbrev_section
,
4135 cu
->header
.abbrev_sect_off
);
4136 if (abbrev_table
== nullptr)
4138 abbrev_section
->read (objfile
);
4139 m_abbrev_table_holder
4140 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
4141 abbrev_table
= m_abbrev_table_holder
.get ();
4145 /* Read the top level CU/TU die. */
4146 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
4147 info_ptr
= read_toplevel_die (this, &comp_unit_die
, info_ptr
);
4149 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
4155 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4156 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
4157 table from the DWO file and pass the ownership over to us. It will be
4158 referenced from READER, so we must make sure to free it after we're done
4161 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4162 DWO CU, that this test will fail (the attribute will not be present). */
4163 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
4164 if (dwo_name
!= nullptr)
4166 struct dwo_unit
*dwo_unit
;
4167 struct die_info
*dwo_comp_unit_die
;
4169 if (comp_unit_die
->has_children
)
4171 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
4172 " has children (offset %s) [in module %s]"),
4173 sect_offset_str (this_cu
->sect_off
),
4174 bfd_get_filename (abfd
));
4176 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
4177 if (dwo_unit
!= NULL
)
4179 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
4180 comp_unit_die
, NULL
,
4183 &m_dwo_abbrev_table
) == 0)
4189 comp_unit_die
= dwo_comp_unit_die
;
4193 /* Yikes, we couldn't find the rest of the DIE, we only have
4194 the stub. A complaint has already been logged. There's
4195 not much more we can do except pass on the stub DIE to
4196 die_reader_func. We don't want to throw an error on bad
4203 cutu_reader::keep ()
4205 /* Done, clean up. */
4206 gdb_assert (!dummy_p
);
4207 if (m_new_cu
!= NULL
)
4209 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
4211 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
4212 per_objfile
->set_cu (m_this_cu
, std::move (m_new_cu
));
4216 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
4217 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
4218 assumed to have already done the lookup to find the DWO file).
4220 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4221 THIS_CU->is_debug_types, but nothing else.
4223 We fill in THIS_CU->length.
4225 THIS_CU->cu is always freed when done.
4226 This is done in order to not leave THIS_CU->cu in a state where we have
4227 to care whether it refers to the "main" CU or the DWO CU.
4229 When parent_cu is passed, it is used to provide a default value for
4230 str_offsets_base and addr_base from the parent. */
4232 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
4233 dwarf2_per_objfile
*per_objfile
,
4234 struct dwarf2_cu
*parent_cu
,
4235 struct dwo_file
*dwo_file
)
4236 : die_reader_specs
{},
4239 struct objfile
*objfile
= per_objfile
->objfile
;
4240 struct dwarf2_section_info
*section
= this_cu
->section
;
4241 bfd
*abfd
= section
->get_bfd_owner ();
4242 struct dwarf2_section_info
*abbrev_section
;
4243 const gdb_byte
*begin_info_ptr
, *info_ptr
;
4245 if (dwarf_die_debug
)
4246 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
4247 this_cu
->is_debug_types
? "type" : "comp",
4248 sect_offset_str (this_cu
->sect_off
));
4250 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
4252 abbrev_section
= (dwo_file
!= NULL
4253 ? &dwo_file
->sections
.abbrev
4254 : get_abbrev_section_for_cu (this_cu
));
4256 /* This is cheap if the section is already read in. */
4257 section
->read (objfile
);
4259 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
4261 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
4262 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
4263 section
, abbrev_section
, info_ptr
,
4264 (this_cu
->is_debug_types
4266 : rcuh_kind::COMPILE
));
4268 if (parent_cu
!= nullptr)
4270 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
4271 m_new_cu
->addr_base
= parent_cu
->addr_base
;
4273 this_cu
->set_length (m_new_cu
->header
.get_length_with_initial ());
4275 /* Skip dummy compilation units. */
4276 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
4277 || peek_abbrev_code (abfd
, info_ptr
) == 0)
4283 abbrev_section
->read (objfile
);
4284 m_abbrev_table_holder
4285 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
4287 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
4288 m_abbrev_table_holder
.get ());
4289 info_ptr
= read_toplevel_die (this, &comp_unit_die
, info_ptr
);
4293 /* Type Unit Groups.
4295 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4296 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4297 so that all types coming from the same compilation (.o file) are grouped
4298 together. A future step could be to put the types in the same symtab as
4299 the CU the types ultimately came from. */
4302 hash_type_unit_group (const void *item
)
4304 const struct type_unit_group
*tu_group
4305 = (const struct type_unit_group
*) item
;
4307 return hash_stmt_list_entry (&tu_group
->hash
);
4311 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
4313 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
4314 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
4316 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
4319 /* Allocate a hash table for type unit groups. */
4322 allocate_type_unit_groups_table ()
4324 return htab_up (htab_create_alloc (3,
4325 hash_type_unit_group
,
4327 htab_delete_entry
<type_unit_group
>,
4331 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4332 partial symtabs. We combine several TUs per psymtab to not let the size
4333 of any one psymtab grow too big. */
4334 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4335 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4337 /* Helper routine for get_type_unit_group.
4338 Create the type_unit_group object used to hold one or more TUs. */
4340 static std::unique_ptr
<type_unit_group
>
4341 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
4343 auto tu_group
= std::make_unique
<type_unit_group
> ();
4345 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
4346 tu_group
->hash
.line_sect_off
= line_offset_struct
;
4351 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4352 STMT_LIST is a DW_AT_stmt_list attribute. */
4354 static struct type_unit_group
*
4355 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
4357 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
4358 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
4359 struct type_unit_group
*tu_group
;
4361 unsigned int line_offset
;
4362 struct type_unit_group type_unit_group_for_lookup
;
4364 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
4365 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
4367 /* Do we need to create a new group, or can we use an existing one? */
4369 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
4371 line_offset
= stmt_list
->as_unsigned ();
4372 ++tu_stats
->nr_symtab_sharers
;
4376 /* Ugh, no stmt_list. Rare, but we have to handle it.
4377 We can do various things here like create one group per TU or
4378 spread them over multiple groups to split up the expansion work.
4379 To avoid worst case scenarios (too many groups or too large groups)
4380 we, umm, group them in bunches. */
4381 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4382 | (tu_stats
->nr_stmt_less_type_units
4383 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
4384 ++tu_stats
->nr_stmt_less_type_units
;
4387 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
4388 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
4389 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
4390 &type_unit_group_for_lookup
, INSERT
);
4391 if (*slot
== nullptr)
4393 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
4394 std::unique_ptr
<type_unit_group
> grp
4395 = create_type_unit_group (cu
, line_offset_struct
);
4396 *slot
= grp
.release ();
4397 ++tu_stats
->nr_symtabs
;
4400 tu_group
= (struct type_unit_group
*) *slot
;
4401 gdb_assert (tu_group
!= nullptr);
4406 cooked_index_storage::cooked_index_storage ()
4407 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
4409 htab_delete_entry
<cutu_reader
>,
4411 m_index (new cooked_index_shard
)
4416 cooked_index_storage::get_reader (dwarf2_per_cu_data
*per_cu
)
4418 int index
= per_cu
->index
;
4419 return (cutu_reader
*) htab_find_with_hash (m_reader_hash
.get (),
4424 cooked_index_storage::preserve (std::unique_ptr
<cutu_reader
> reader
)
4426 m_abbrev_cache
.add (reader
->release_abbrev_table ());
4428 int index
= reader
->cu
->per_cu
->index
;
4429 void **slot
= htab_find_slot_with_hash (m_reader_hash
.get (), &index
,
4431 gdb_assert (*slot
== nullptr);
4432 cutu_reader
*result
= reader
.get ();
4433 *slot
= reader
.release ();
4437 /* Hash function for a cutu_reader. */
4439 cooked_index_storage::hash_cutu_reader (const void *a
)
4441 const cutu_reader
*reader
= (const cutu_reader
*) a
;
4442 return reader
->cu
->per_cu
->index
;
4445 /* Equality function for cutu_reader. */
4447 cooked_index_storage::eq_cutu_reader (const void *a
, const void *b
)
4449 const cutu_reader
*ra
= (const cutu_reader
*) a
;
4450 const int *rb
= (const int *) b
;
4451 return ra
->cu
->per_cu
->index
== *rb
;
4454 /* An instance of this is created to index a CU. */
4456 class cooked_indexer
4460 cooked_indexer (cooked_index_storage
*storage
,
4461 dwarf2_per_cu_data
*per_cu
,
4462 enum language language
)
4463 : m_index_storage (storage
),
4465 m_language (language
)
4469 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
4471 /* Index the given CU. */
4472 void make_index (cutu_reader
*reader
);
4476 /* A helper function to turn a section offset into an address that
4477 can be used in an addrmap. */
4478 CORE_ADDR
form_addr (sect_offset offset
, bool is_dwz
)
4480 CORE_ADDR value
= to_underlying (offset
);
4482 value
|= ((CORE_ADDR
) 1) << (8 * sizeof (CORE_ADDR
) - 1);
4486 /* A helper function to scan the PC bounds of READER and record them
4487 in the storage's addrmap. */
4488 void check_bounds (cutu_reader
*reader
);
4490 /* Ensure that the indicated CU exists. The cutu_reader for it is
4491 returned. FOR_SCANNING is true if the caller intends to scan all
4492 the DIEs in the CU; when false, this use is assumed to be to look
4493 up just a single DIE. */
4494 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
4495 dwarf2_per_objfile
*per_objfile
,
4496 sect_offset sect_off
,
4500 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
4501 the entry for the enclosing scope (nullptr at top level). FULLY
4502 is true when a full scan must be done -- in some languages,
4503 function scopes must be fully explored in order to find nested
4504 functions. This returns a pointer to just after the spot where
4506 const gdb_byte
*index_dies (cutu_reader
*reader
,
4507 const gdb_byte
*info_ptr
,
4508 const cooked_index_entry
*parent_entry
,
4511 /* Scan the attributes for a given DIE and update the out
4512 parameters. Returns a pointer to the byte after the DIE. */
4513 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
4514 cutu_reader
*reader
,
4515 const gdb_byte
*watermark_ptr
,
4516 const gdb_byte
*info_ptr
,
4517 const abbrev_info
*abbrev
,
4519 const char **linkage_name
,
4520 cooked_index_flag
*flags
,
4521 sect_offset
*sibling_offset
,
4522 const cooked_index_entry
**parent_entry
,
4523 CORE_ADDR
*maybe_defer
,
4524 bool for_specification
);
4526 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
4527 DW_AT_import, and then scanning the referenced CU. Returns a
4528 pointer to the byte after the DIE. */
4529 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
4530 const gdb_byte
*info_ptr
,
4531 const abbrev_info
*abbrev
);
4533 /* Recursively read DIEs, recording the section offsets in
4534 m_die_range_map and then calling index_dies. */
4535 const gdb_byte
*recurse (cutu_reader
*reader
,
4536 const gdb_byte
*info_ptr
,
4537 const cooked_index_entry
*parent_entry
,
4540 /* The storage object, where the results are kept. */
4541 cooked_index_storage
*m_index_storage
;
4542 /* The CU that we are reading on behalf of. This object might be
4543 asked to index one CU but to treat the results as if they come
4544 from some including CU; in this case the including CU would be
4546 dwarf2_per_cu_data
*m_per_cu
;
4547 /* The language that we're assuming when reading. */
4548 enum language m_language
;
4550 /* An addrmap that maps from section offsets (see the form_addr
4551 method) to newly-created entries. See m_deferred_entries to
4553 addrmap_mutable m_die_range_map
;
4555 /* The generated DWARF can sometimes have the declaration for a
4556 method in a class (or perhaps namespace) scope, with the
4557 definition appearing outside this scope... just one of the many
4558 bad things about DWARF. In order to handle this situation, we
4559 defer certain entries until the end of scanning, at which point
4560 we'll know the containing context of all the DIEs that we might
4561 have scanned. This vector stores these deferred entries. */
4562 std::vector
<cooked_index_entry
*> m_deferred_entries
;
4565 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4566 Process compilation unit THIS_CU for a psymtab. */
4569 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
4570 dwarf2_per_objfile
*per_objfile
,
4571 cooked_index_storage
*storage
)
4573 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
4574 storage
->get_abbrev_cache ());
4576 if (reader
.comp_unit_die
== nullptr)
4583 else if (this_cu
->is_debug_types
)
4584 build_type_psymtabs_reader (&reader
, storage
);
4585 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
4588 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
4590 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
4592 gdb_assert (storage
!= nullptr);
4593 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->lang ());
4594 indexer
.make_index (&reader
);
4599 /* Reader function for build_type_psymtabs. */
4602 build_type_psymtabs_reader (cutu_reader
*reader
,
4603 cooked_index_storage
*storage
)
4605 struct dwarf2_cu
*cu
= reader
->cu
;
4606 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
4607 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
4609 gdb_assert (per_cu
->is_debug_types
);
4611 if (! type_unit_die
->has_children
)
4614 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
4616 gdb_assert (storage
!= nullptr);
4617 cooked_indexer
indexer (storage
, per_cu
, cu
->lang ());
4618 indexer
.make_index (reader
);
4621 /* Struct used to sort TUs by their abbreviation table offset. */
4623 struct tu_abbrev_offset
4625 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
4626 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
4629 /* This is used when sorting. */
4630 bool operator< (const tu_abbrev_offset
&other
) const
4632 return abbrev_offset
< other
.abbrev_offset
;
4635 signatured_type
*sig_type
;
4636 sect_offset abbrev_offset
;
4639 /* Efficiently read all the type units.
4641 The efficiency is because we sort TUs by the abbrev table they use and
4642 only read each abbrev table once. In one program there are 200K TUs
4643 sharing 8K abbrev tables.
4645 The main purpose of this function is to support building the
4646 dwarf2_per_objfile->per_bfd->type_unit_groups table.
4647 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4648 can collapse the search space by grouping them by stmt_list.
4649 The savings can be significant, in the same program from above the 200K TUs
4650 share 8K stmt_list tables.
4652 FUNC is expected to call get_type_unit_group, which will create the
4653 struct type_unit_group if necessary and add it to
4654 dwarf2_per_objfile->per_bfd->type_unit_groups. */
4657 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
4658 cooked_index_storage
*storage
)
4660 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
4661 abbrev_table_up abbrev_table
;
4662 sect_offset abbrev_offset
;
4664 /* It's up to the caller to not call us multiple times. */
4665 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
4667 if (per_objfile
->per_bfd
->all_type_units
.size () == 0)
4670 /* TUs typically share abbrev tables, and there can be way more TUs than
4671 abbrev tables. Sort by abbrev table to reduce the number of times we
4672 read each abbrev table in.
4673 Alternatives are to punt or to maintain a cache of abbrev tables.
4674 This is simpler and efficient enough for now.
4676 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4677 symtab to use). Typically TUs with the same abbrev offset have the same
4678 stmt_list value too so in practice this should work well.
4680 The basic algorithm here is:
4682 sort TUs by abbrev table
4683 for each TU with same abbrev table:
4684 read abbrev table if first user
4685 read TU top level DIE
4686 [IWBN if DWO skeletons had DW_AT_stmt_list]
4689 dwarf_read_debug_printf ("Building type unit groups ...");
4691 /* Sort in a separate table to maintain the order of all_units
4692 for .gdb_index: TU indices directly index all_type_units. */
4693 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
4694 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->all_type_units
.size ());
4696 for (const auto &cu
: per_objfile
->per_bfd
->all_units
)
4698 if (cu
->is_debug_types
)
4700 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
4701 sorted_by_abbrev
.emplace_back
4702 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
4703 sig_type
->sect_off
));
4707 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
4709 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
4711 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
4713 /* Switch to the next abbrev table if necessary. */
4714 if (abbrev_table
== NULL
4715 || tu
.abbrev_offset
!= abbrev_offset
)
4717 abbrev_offset
= tu
.abbrev_offset
;
4718 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
4720 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
4721 ++tu_stats
->nr_uniq_abbrev_tables
;
4724 cutu_reader
reader (tu
.sig_type
, per_objfile
,
4725 abbrev_table
.get (), nullptr, false);
4726 if (!reader
.dummy_p
)
4727 build_type_psymtabs_reader (&reader
, storage
);
4731 /* Print collected type unit statistics. */
4734 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
4736 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
4738 dwarf_read_debug_printf ("Type unit statistics:");
4739 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
4740 dwarf_read_debug_printf (" %d uniq abbrev tables",
4741 tu_stats
->nr_uniq_abbrev_tables
);
4742 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4743 tu_stats
->nr_symtabs
);
4744 dwarf_read_debug_printf (" %d symtab sharers",
4745 tu_stats
->nr_symtab_sharers
);
4746 dwarf_read_debug_printf (" %d type units without a stmt_list",
4747 tu_stats
->nr_stmt_less_type_units
);
4748 dwarf_read_debug_printf (" %d all_type_units reallocs",
4749 tu_stats
->nr_all_type_units_reallocs
);
4752 struct skeleton_data
4754 dwarf2_per_objfile
*per_objfile
;
4755 cooked_index_storage
*storage
;
4758 /* Traversal function for process_skeletonless_type_unit.
4759 Read a TU in a DWO file and build partial symbols for it. */
4762 process_skeletonless_type_unit (void **slot
, void *info
)
4764 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
4765 skeleton_data
*data
= (skeleton_data
*) info
;
4767 /* If this TU doesn't exist in the global table, add it and read it in. */
4769 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
4770 data
->per_objfile
->per_bfd
->signatured_types
4771 = allocate_signatured_type_table ();
4773 signatured_type
find_entry (dwo_unit
->signature
);
4774 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
4775 &find_entry
, INSERT
);
4776 /* If we've already seen this type there's nothing to do. What's happening
4777 is we're doing our own version of comdat-folding here. */
4781 /* This does the job that create_all_units would have done for
4783 signatured_type
*entry
4784 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
4785 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
4788 /* This does the job that build_type_psymtabs would have done. */
4789 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
4790 if (!reader
.dummy_p
)
4791 build_type_psymtabs_reader (&reader
, data
->storage
);
4796 /* Traversal function for process_skeletonless_type_units. */
4799 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
4801 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
4803 if (dwo_file
->tus
!= NULL
)
4804 htab_traverse_noresize (dwo_file
->tus
.get (),
4805 process_skeletonless_type_unit
, info
);
4810 /* Scan all TUs of DWO files, verifying we've processed them.
4811 This is needed in case a TU was emitted without its skeleton.
4812 Note: This can't be done until we know what all the DWO files are. */
4815 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
4816 cooked_index_storage
*storage
)
4818 skeleton_data data
{ per_objfile
, storage
};
4820 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
4821 if (get_dwp_file (per_objfile
) == NULL
4822 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
4824 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
4825 process_dwo_file_for_skeletonless_type_units
,
4830 cooked_index_worker::cooked_index_worker (dwarf2_per_objfile
*per_objfile
)
4831 : m_per_objfile (per_objfile
)
4833 gdb_assert (is_main_thread ());
4835 struct objfile
*objfile
= per_objfile
->objfile
;
4836 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4838 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
4839 objfile_name (objfile
));
4841 per_bfd
->map_info_sections (objfile
);
4845 cooked_index_worker::start ()
4847 gdb::thread_pool::g_thread_pool
->post_task ([=] ()
4849 this->start_reading ();
4854 cooked_index_worker::process_cus (size_t task_number
, unit_iterator first
,
4857 SCOPE_EXIT
{ bfd_thread_cleanup (); };
4859 /* Ensure that complaints are handled correctly. */
4860 complaint_interceptor complaint_handler
;
4862 std::vector
<gdb_exception
> errors
;
4863 cooked_index_storage thread_storage
;
4864 for (auto inner
= first
; inner
!= end
; ++inner
)
4866 dwarf2_per_cu_data
*per_cu
= inner
->get ();
4869 process_psymtab_comp_unit (per_cu
, m_per_objfile
, &thread_storage
);
4871 catch (gdb_exception
&except
)
4873 errors
.push_back (std::move (except
));
4877 m_results
[task_number
] = result_type (thread_storage
.release (),
4878 complaint_handler
.release (),
4879 std::move (errors
));
4883 cooked_index_worker::done_reading ()
4885 /* Only handle the scanning results here. Complaints and exceptions
4886 can only be dealt with on the main thread. */
4887 std::vector
<std::unique_ptr
<cooked_index_shard
>> indexes
;
4888 for (auto &one_result
: m_results
)
4889 indexes
.push_back (std::move (std::get
<0> (one_result
)));
4891 /* This has to wait until we read the CUs, we need the list of DWOs. */
4892 process_skeletonless_type_units (m_per_objfile
, &m_index_storage
);
4894 indexes
.push_back (m_index_storage
.release ());
4895 indexes
.shrink_to_fit ();
4897 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4899 = (gdb::checked_static_cast
<cooked_index
*>
4900 (per_bfd
->index_table
.get ()));
4901 table
->set_contents (std::move (indexes
));
4905 cooked_index_worker::start_reading ()
4907 SCOPE_EXIT
{ bfd_thread_cleanup (); };
4913 catch (const gdb_exception
&exc
)
4916 set (cooked_state::CACHE_DONE
);
4921 cooked_index_worker::do_reading ()
4923 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4925 create_all_units (m_per_objfile
);
4926 build_type_psymtabs (m_per_objfile
, &m_index_storage
);
4928 per_bfd
->quick_file_names_table
4929 = create_quick_file_names_table (per_bfd
->all_units
.size ());
4930 if (!per_bfd
->debug_aranges
.empty ())
4931 read_addrmap_from_aranges (m_per_objfile
, &per_bfd
->debug_aranges
,
4932 m_index_storage
.get_addrmap (),
4935 /* We want to balance the load between the worker threads. This is
4936 done by using the size of each CU as a rough estimate of how
4937 difficult it will be to operate on. This isn't ideal -- for
4938 example if dwz is used, the early CUs will all tend to be
4939 "included" and won't be parsed independently. However, this
4940 heuristic works well for typical compiler output. */
4942 size_t total_size
= 0;
4943 for (const auto &per_cu
: per_bfd
->all_units
)
4944 total_size
+= per_cu
->length ();
4946 /* How many worker threads we plan to use. We may not actually use
4947 this many. We use 1 as the minimum to avoid division by zero,
4948 and anyway in the N==0 case the work will be done
4950 const size_t n_worker_threads
4951 = std::max (gdb::thread_pool::g_thread_pool
->thread_count (), (size_t) 1);
4953 /* How much effort should be put into each worker. */
4954 const size_t size_per_thread
4955 = std::max (total_size
/ n_worker_threads
, (size_t) 1);
4957 /* Work is done in a task group. */
4958 gdb::task_group
workers ([this] ()
4960 this->done_reading ();
4963 auto end
= per_bfd
->all_units
.end ();
4964 size_t task_count
= 0;
4965 for (auto iter
= per_bfd
->all_units
.begin (); iter
!= end
; )
4968 /* Put all remaining CUs into the last task. */
4969 if (task_count
== n_worker_threads
- 1)
4973 size_t chunk_size
= 0;
4974 for (; last
!= end
&& chunk_size
< size_per_thread
; ++last
)
4975 chunk_size
+= (*last
)->length ();
4978 gdb_assert (iter
!= last
);
4979 workers
.add_task ([=] ()
4981 process_cus (task_count
, iter
, last
);
4988 m_results
.resize (task_count
);
4993 cooked_index_worker::wait (cooked_state desired_state
, bool allow_quit
)
4998 std::unique_lock
<std::mutex
> lock (m_mutex
);
5000 /* This may be called from a non-main thread -- this functionality
5001 is needed for the index cache -- but in this case we require
5002 that the desired state already have been attained. */
5003 gdb_assert (is_main_thread () || desired_state
<= m_state
);
5005 while (desired_state
> m_state
)
5009 std::chrono::milliseconds duration
{ 15 };
5010 if (m_cond
.wait_for (lock
, duration
) == std::cv_status::timeout
)
5016 done
= m_state
== cooked_state::CACHE_DONE
;
5019 /* Without threads, all the work is done immediately on the main
5020 thread, and there is never anything to wait for. */
5022 #endif /* CXX_STD_THREAD */
5024 /* Only the main thread is allowed to report complaints and the
5026 if (!is_main_thread ())
5033 /* Emit warnings first, maybe they were emitted before an exception
5034 (if any) was thrown. */
5037 if (m_failed
.has_value ())
5039 /* start_reading failed -- report it. */
5040 exception_print (gdb_stderr
, *m_failed
);
5045 /* Only show a given exception a single time. */
5046 std::unordered_set
<gdb_exception
> seen_exceptions
;
5047 for (auto &one_result
: m_results
)
5049 re_emit_complaints (std::get
<1> (one_result
));
5050 for (auto &one_exc
: std::get
<2> (one_result
))
5051 if (seen_exceptions
.insert (one_exc
).second
)
5052 exception_print (gdb_stderr
, one_exc
);
5055 if (dwarf_read_debug
> 0)
5056 print_tu_stats (m_per_objfile
);
5058 struct objfile
*objfile
= m_per_objfile
->objfile
;
5059 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
5061 = (gdb::checked_static_cast
<cooked_index
*>
5062 (per_bfd
->index_table
.get ()));
5064 auto_obstack temp_storage
;
5065 enum language lang
= language_unknown
;
5066 const char *main_name
= table
->get_main_name (&temp_storage
, &lang
);
5067 if (main_name
!= nullptr)
5068 set_objfile_main_name (objfile
, main_name
, lang
);
5070 dwarf_read_debug_printf ("Done building psymtabs of %s",
5071 objfile_name (objfile
));
5077 cooked_index_worker::set (cooked_state desired_state
)
5079 gdb_assert (desired_state
!= cooked_state::INITIAL
);
5082 std::lock_guard
<std::mutex
> guard (m_mutex
);
5083 gdb_assert (desired_state
> m_state
);
5084 m_state
= desired_state
;
5085 m_cond
.notify_one ();
5087 /* Without threads, all the work is done immediately on the main
5088 thread, and there is never anything to do. */
5089 #endif /* CXX_STD_THREAD */
5093 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
5094 struct dwarf2_section_info
*section
,
5095 struct dwarf2_section_info
*abbrev_section
,
5096 unsigned int is_dwz
,
5097 htab_up
&types_htab
,
5098 rcuh_kind section_kind
)
5100 const gdb_byte
*info_ptr
;
5101 struct objfile
*objfile
= per_objfile
->objfile
;
5103 dwarf_read_debug_printf ("Reading %s for %s",
5104 section
->get_name (),
5105 section
->get_file_name ());
5107 section
->read (objfile
);
5109 info_ptr
= section
->buffer
;
5111 while (info_ptr
< section
->buffer
+ section
->size
)
5113 dwarf2_per_cu_data_up this_cu
;
5115 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
5117 comp_unit_head cu_header
;
5118 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
5119 abbrev_section
, info_ptr
,
5122 /* Save the compilation unit for later lookup. */
5123 if (cu_header
.unit_type
!= DW_UT_type
)
5124 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
5127 if (types_htab
== nullptr)
5128 types_htab
= allocate_signatured_type_table ();
5130 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
5131 (cu_header
.signature
);
5132 signatured_type
*sig_ptr
= sig_type
.get ();
5133 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
5134 this_cu
.reset (sig_type
.release ());
5136 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
5137 gdb_assert (slot
!= nullptr);
5138 if (*slot
!= nullptr)
5139 complaint (_("debug type entry at offset %s is duplicate to"
5140 " the entry at offset %s, signature %s"),
5141 sect_offset_str (sect_off
),
5142 sect_offset_str (sig_ptr
->sect_off
),
5143 hex_string (sig_ptr
->signature
));
5146 this_cu
->sect_off
= sect_off
;
5147 this_cu
->set_length (cu_header
.get_length_with_initial ());
5148 this_cu
->is_dwz
= is_dwz
;
5149 this_cu
->section
= section
;
5150 /* Init this asap, to avoid a data race in the set_version in
5151 cutu_reader::cutu_reader (which may be run in parallel for the cooked
5153 this_cu
->set_version (cu_header
.version
);
5155 info_ptr
= info_ptr
+ this_cu
->length ();
5156 per_objfile
->per_bfd
->all_units
.push_back (std::move (this_cu
));
5160 /* Initialize the views on all_units. */
5163 finalize_all_units (dwarf2_per_bfd
*per_bfd
)
5165 size_t nr_tus
= per_bfd
->tu_stats
.nr_tus
;
5166 size_t nr_cus
= per_bfd
->all_units
.size () - nr_tus
;
5167 gdb::array_view
<dwarf2_per_cu_data_up
> tmp
= per_bfd
->all_units
;
5168 per_bfd
->all_comp_units
= tmp
.slice (0, nr_cus
);
5169 per_bfd
->all_type_units
= tmp
.slice (nr_cus
, nr_tus
);
5175 create_all_units (dwarf2_per_objfile
*per_objfile
)
5178 gdb_assert (per_objfile
->per_bfd
->all_units
.empty ());
5180 read_comp_units_from_section (per_objfile
, &per_objfile
->per_bfd
->info
,
5181 &per_objfile
->per_bfd
->abbrev
, 0,
5182 types_htab
, rcuh_kind::COMPILE
);
5183 for (dwarf2_section_info
§ion
: per_objfile
->per_bfd
->types
)
5184 read_comp_units_from_section (per_objfile
, §ion
,
5185 &per_objfile
->per_bfd
->abbrev
, 0,
5186 types_htab
, rcuh_kind::TYPE
);
5188 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
5191 read_comp_units_from_section (per_objfile
, &dwz
->info
, &dwz
->abbrev
, 1,
5192 types_htab
, rcuh_kind::COMPILE
);
5194 if (!dwz
->types
.empty ())
5196 /* See enhancement PR symtab/30838. */
5197 error (_("Dwarf Error: .debug_types section not supported in dwz file"));
5201 per_objfile
->per_bfd
->signatured_types
= std::move (types_htab
);
5203 finalize_all_units (per_objfile
->per_bfd
);
5206 /* Return the initial uleb128 in the die at INFO_PTR. */
5209 peek_abbrev_code (bfd
*abfd
, const gdb_byte
*info_ptr
)
5211 unsigned int bytes_read
;
5213 return read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
5216 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
5217 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
5219 Return the corresponding abbrev, or NULL if the number is zero (indicating
5220 an empty DIE). In either case *BYTES_READ will be set to the length of
5221 the initial number. */
5223 static const struct abbrev_info
*
5224 peek_die_abbrev (const die_reader_specs
&reader
,
5225 const gdb_byte
*info_ptr
, unsigned int *bytes_read
)
5227 dwarf2_cu
*cu
= reader
.cu
;
5228 bfd
*abfd
= reader
.abfd
;
5229 unsigned int abbrev_number
5230 = read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
5232 if (abbrev_number
== 0)
5235 const abbrev_info
*abbrev
5236 = reader
.abbrev_table
->lookup_abbrev (abbrev_number
);
5239 error (_("Dwarf Error: Could not find abbrev number %d in %s"
5240 " at offset %s [in module %s]"),
5241 abbrev_number
, cu
->per_cu
->is_debug_types
? "TU" : "CU",
5242 sect_offset_str (cu
->header
.sect_off
), bfd_get_filename (abfd
));
5248 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5249 Returns a pointer to the end of a series of DIEs, terminated by an empty
5250 DIE. Any children of the skipped DIEs will also be skipped. */
5252 static const gdb_byte
*
5253 skip_children (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
)
5257 unsigned int bytes_read
;
5258 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
5262 return info_ptr
+ bytes_read
;
5264 info_ptr
= skip_one_die (reader
, info_ptr
+ bytes_read
, abbrev
);
5268 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5269 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5270 abbrev corresponding to that skipped uleb128 should be passed in
5273 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
5274 returns a pointer to this DIE's sibling, skipping any children.
5275 Otherwise, returns a pointer to the DIE's first child. */
5277 static const gdb_byte
*
5278 skip_one_die (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
,
5279 const struct abbrev_info
*abbrev
, bool do_skip_children
)
5281 unsigned int bytes_read
;
5282 struct attribute attr
;
5283 bfd
*abfd
= reader
->abfd
;
5284 struct dwarf2_cu
*cu
= reader
->cu
;
5285 const gdb_byte
*buffer
= reader
->buffer
;
5286 const gdb_byte
*buffer_end
= reader
->buffer_end
;
5287 unsigned int form
, i
;
5289 if (do_skip_children
&& abbrev
->sibling_offset
!= (unsigned short) -1)
5291 /* We only handle DW_FORM_ref4 here. */
5292 const gdb_byte
*sibling_data
= info_ptr
+ abbrev
->sibling_offset
;
5293 unsigned int offset
= read_4_bytes (abfd
, sibling_data
);
5294 const gdb_byte
*sibling_ptr
5295 = buffer
+ to_underlying (cu
->header
.sect_off
) + offset
;
5296 if (sibling_ptr
>= info_ptr
&& sibling_ptr
< reader
->buffer_end
)
5298 /* Fall through to the slow way. */
5300 else if (abbrev
->size_if_constant
!= 0)
5302 info_ptr
+= abbrev
->size_if_constant
;
5303 if (do_skip_children
&& abbrev
->has_children
)
5304 return skip_children (reader
, info_ptr
);
5308 for (i
= 0; i
< abbrev
->num_attrs
; i
++)
5310 /* The only abbrev we care about is DW_AT_sibling. */
5311 if (do_skip_children
&& abbrev
->attrs
[i
].name
== DW_AT_sibling
)
5313 /* Note there is no need for the extra work of
5314 "reprocessing" here, so we pass false for that
5316 read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
, false);
5317 if (attr
.form
== DW_FORM_ref_addr
)
5318 complaint (_("ignoring absolute DW_AT_sibling"));
5321 sect_offset off
= attr
.get_ref_die_offset ();
5322 const gdb_byte
*sibling_ptr
= buffer
+ to_underlying (off
);
5324 if (sibling_ptr
< info_ptr
)
5325 complaint (_("DW_AT_sibling points backwards"));
5326 else if (sibling_ptr
> reader
->buffer_end
)
5327 reader
->die_section
->overflow_complaint ();
5333 /* If it isn't DW_AT_sibling, skip this attribute. */
5334 form
= abbrev
->attrs
[i
].form
;
5338 case DW_FORM_ref_addr
:
5339 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5340 and later it is offset sized. */
5341 if (cu
->header
.version
== 2)
5342 info_ptr
+= cu
->header
.addr_size
;
5344 info_ptr
+= cu
->header
.offset_size
;
5346 case DW_FORM_GNU_ref_alt
:
5347 info_ptr
+= cu
->header
.offset_size
;
5350 info_ptr
+= cu
->header
.addr_size
;
5358 case DW_FORM_flag_present
:
5359 case DW_FORM_implicit_const
:
5376 case DW_FORM_ref_sig8
:
5379 case DW_FORM_data16
:
5382 case DW_FORM_string
:
5383 read_direct_string (abfd
, info_ptr
, &bytes_read
);
5384 info_ptr
+= bytes_read
;
5386 case DW_FORM_sec_offset
:
5388 case DW_FORM_GNU_strp_alt
:
5389 info_ptr
+= cu
->header
.offset_size
;
5391 case DW_FORM_exprloc
:
5393 info_ptr
+= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
5394 info_ptr
+= bytes_read
;
5396 case DW_FORM_block1
:
5397 info_ptr
+= 1 + read_1_byte (abfd
, info_ptr
);
5399 case DW_FORM_block2
:
5400 info_ptr
+= 2 + read_2_bytes (abfd
, info_ptr
);
5402 case DW_FORM_block4
:
5403 info_ptr
+= 4 + read_4_bytes (abfd
, info_ptr
);
5409 case DW_FORM_ref_udata
:
5410 case DW_FORM_GNU_addr_index
:
5411 case DW_FORM_GNU_str_index
:
5412 case DW_FORM_rnglistx
:
5413 case DW_FORM_loclistx
:
5414 info_ptr
= safe_skip_leb128 (info_ptr
, buffer_end
);
5416 case DW_FORM_indirect
:
5417 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
5418 info_ptr
+= bytes_read
;
5419 /* We need to continue parsing from here, so just go back to
5421 goto skip_attribute
;
5424 error (_("Dwarf Error: Cannot handle %s "
5425 "in DWARF reader [in module %s]"),
5426 dwarf_form_name (form
),
5427 bfd_get_filename (abfd
));
5431 if (do_skip_children
&& abbrev
->has_children
)
5432 return skip_children (reader
, info_ptr
);
5437 /* Reading in full CUs. */
5439 /* Add PER_CU to the queue. */
5442 queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
5443 dwarf2_per_objfile
*per_objfile
,
5444 enum language pretend_language
)
5448 gdb_assert (per_objfile
->queue
.has_value ());
5449 per_objfile
->queue
->emplace (per_cu
, per_objfile
, pretend_language
);
5452 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
5454 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
5457 Return true if maybe_queue_comp_unit requires the caller to load the CU's
5458 DIEs, false otherwise.
5460 Explanation: there is an invariant that if a CU is queued for expansion
5461 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
5462 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
5463 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
5464 are not yet loaded, the the caller must load the CU's DIEs to ensure the
5465 invariant is respected.
5467 The caller is therefore not required to load the CU's DIEs (we return false)
5470 - the CU is already expanded, and therefore does not get enqueued
5471 - the CU gets enqueued for expansion, but its DIEs are already loaded
5473 Note that the caller should not use this function's return value as an
5474 indicator of whether the CU's DIEs are loaded right now, it should check
5475 that by calling `dwarf2_per_objfile::get_cu` instead. */
5478 maybe_queue_comp_unit (struct dwarf2_cu
*dependent_cu
,
5479 dwarf2_per_cu_data
*per_cu
,
5480 dwarf2_per_objfile
*per_objfile
,
5481 enum language pretend_language
)
5483 /* Mark the dependence relation so that we don't flush PER_CU
5485 if (dependent_cu
!= NULL
)
5486 dependent_cu
->add_dependence (per_cu
);
5488 /* If it's already on the queue, we have nothing to do. */
5491 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
5493 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
5495 /* If the CU is queued for expansion, it should not already be
5497 gdb_assert (!per_objfile
->symtab_set_p (per_cu
));
5499 /* The DIEs are already loaded, the caller doesn't need to do it. */
5503 bool queued
= false;
5504 if (!per_objfile
->symtab_set_p (per_cu
))
5506 /* Add it to the queue. */
5507 queue_comp_unit (per_cu
, per_objfile
, pretend_language
);
5511 /* If the compilation unit is already loaded, just mark it as
5513 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
5517 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
5518 and the DIEs are not already loaded. */
5519 return queued
&& cu
== nullptr;
5522 /* Process the queue. */
5525 process_queue (dwarf2_per_objfile
*per_objfile
)
5527 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
5528 objfile_name (per_objfile
->objfile
));
5530 /* The queue starts out with one item, but following a DIE reference
5531 may load a new CU, adding it to the end of the queue. */
5532 while (!per_objfile
->queue
->empty ())
5534 dwarf2_queue_item
&item
= per_objfile
->queue
->front ();
5535 dwarf2_per_cu_data
*per_cu
= item
.per_cu
;
5537 if (!per_objfile
->symtab_set_p (per_cu
))
5539 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
5541 /* Skip dummy CUs. */
5544 unsigned int debug_print_threshold
;
5547 if (per_cu
->is_debug_types
)
5549 struct signatured_type
*sig_type
=
5550 (struct signatured_type
*) per_cu
;
5552 sprintf (buf
, "TU %s at offset %s",
5553 hex_string (sig_type
->signature
),
5554 sect_offset_str (per_cu
->sect_off
));
5555 /* There can be 100s of TUs.
5556 Only print them in verbose mode. */
5557 debug_print_threshold
= 2;
5561 sprintf (buf
, "CU at offset %s",
5562 sect_offset_str (per_cu
->sect_off
));
5563 debug_print_threshold
= 1;
5566 if (dwarf_read_debug
>= debug_print_threshold
)
5567 dwarf_read_debug_printf ("Expanding symtab of %s", buf
);
5569 if (per_cu
->is_debug_types
)
5570 process_full_type_unit (cu
, item
.pretend_language
);
5572 process_full_comp_unit (cu
, item
.pretend_language
);
5574 if (dwarf_read_debug
>= debug_print_threshold
)
5575 dwarf_read_debug_printf ("Done expanding %s", buf
);
5580 per_objfile
->queue
->pop ();
5583 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
5584 objfile_name (per_objfile
->objfile
));
5587 /* Load the DIEs associated with PER_CU into memory.
5589 In some cases, the caller, while reading partial symbols, will need to load
5590 the full symbols for the CU for some reason. It will already have a
5591 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
5592 rather than creating a new one. */
5595 load_full_comp_unit (dwarf2_per_cu_data
*this_cu
,
5596 dwarf2_per_objfile
*per_objfile
,
5597 dwarf2_cu
*existing_cu
,
5599 enum language pretend_language
)
5601 gdb_assert (! this_cu
->is_debug_types
);
5603 cutu_reader
reader (this_cu
, per_objfile
, NULL
, existing_cu
, skip_partial
);
5607 struct dwarf2_cu
*cu
= reader
.cu
;
5608 const gdb_byte
*info_ptr
= reader
.info_ptr
;
5610 gdb_assert (cu
->die_hash
== NULL
);
5612 htab_create_alloc_ex (cu
->header
.get_length_without_initial () / 12,
5616 &cu
->comp_unit_obstack
,
5617 hashtab_obstack_allocate
,
5618 dummy_obstack_deallocate
);
5620 if (reader
.comp_unit_die
->has_children
)
5621 reader
.comp_unit_die
->child
5622 = read_die_and_siblings (&reader
, reader
.info_ptr
,
5623 &info_ptr
, reader
.comp_unit_die
);
5624 cu
->dies
= reader
.comp_unit_die
;
5625 /* comp_unit_die is not stored in die_hash, no need. */
5627 /* We try not to read any attributes in this function, because not
5628 all CUs needed for references have been loaded yet, and symbol
5629 table processing isn't initialized. But we have to set the CU language,
5630 or we won't be able to build types correctly.
5631 Similarly, if we do not read the producer, we can not apply
5632 producer-specific interpretation. */
5633 prepare_one_comp_unit (cu
, cu
->dies
, pretend_language
);
5638 /* Add a DIE to the delayed physname list. */
5641 add_to_method_list (struct type
*type
, int fnfield_index
, int index
,
5642 const char *name
, struct die_info
*die
,
5643 struct dwarf2_cu
*cu
)
5645 struct delayed_method_info mi
;
5647 mi
.fnfield_index
= fnfield_index
;
5651 cu
->method_list
.push_back (mi
);
5654 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
5655 "const" / "volatile". If so, decrements LEN by the length of the
5656 modifier and return true. Otherwise return false. */
5660 check_modifier (const char *physname
, size_t &len
, const char (&mod
)[N
])
5662 size_t mod_len
= sizeof (mod
) - 1;
5663 if (len
> mod_len
&& startswith (physname
+ (len
- mod_len
), mod
))
5671 /* Compute the physnames of any methods on the CU's method list.
5673 The computation of method physnames is delayed in order to avoid the
5674 (bad) condition that one of the method's formal parameters is of an as yet
5678 compute_delayed_physnames (struct dwarf2_cu
*cu
)
5680 /* Only C++ delays computing physnames. */
5681 if (cu
->method_list
.empty ())
5683 gdb_assert (cu
->lang () == language_cplus
);
5685 for (const delayed_method_info
&mi
: cu
->method_list
)
5687 const char *physname
;
5688 struct fn_fieldlist
*fn_flp
5689 = &TYPE_FN_FIELDLIST (mi
.type
, mi
.fnfield_index
);
5690 physname
= dwarf2_physname (mi
.name
, mi
.die
, cu
);
5691 TYPE_FN_FIELD_PHYSNAME (fn_flp
->fn_fields
, mi
.index
)
5692 = physname
? physname
: "";
5694 /* Since there's no tag to indicate whether a method is a
5695 const/volatile overload, extract that information out of the
5697 if (physname
!= NULL
)
5699 size_t len
= strlen (physname
);
5703 if (physname
[len
- 1] == ')') /* shortcut */
5705 else if (check_modifier (physname
, len
, " const"))
5706 TYPE_FN_FIELD_CONST (fn_flp
->fn_fields
, mi
.index
) = 1;
5707 else if (check_modifier (physname
, len
, " volatile"))
5708 TYPE_FN_FIELD_VOLATILE (fn_flp
->fn_fields
, mi
.index
) = 1;
5715 /* The list is no longer needed. */
5716 cu
->method_list
.clear ();
5719 /* Go objects should be embedded in a DW_TAG_module DIE,
5720 and it's not clear if/how imported objects will appear.
5721 To keep Go support simple until that's worked out,
5722 go back through what we've read and create something usable.
5723 We could do this while processing each DIE, and feels kinda cleaner,
5724 but that way is more invasive.
5725 This is to, for example, allow the user to type "p var" or "b main"
5726 without having to specify the package name, and allow lookups
5727 of module.object to work in contexts that use the expression
5731 fixup_go_packaging (struct dwarf2_cu
*cu
)
5733 gdb::unique_xmalloc_ptr
<char> package_name
;
5734 struct pending
*list
;
5737 for (list
= *cu
->get_builder ()->get_global_symbols ();
5741 for (i
= 0; i
< list
->nsyms
; ++i
)
5743 struct symbol
*sym
= list
->symbol
[i
];
5745 if (sym
->language () == language_go
5746 && sym
->aclass () == LOC_BLOCK
)
5748 gdb::unique_xmalloc_ptr
<char> this_package_name
5749 = go_symbol_package_name (sym
);
5751 if (this_package_name
== NULL
)
5753 if (package_name
== NULL
)
5754 package_name
= std::move (this_package_name
);
5757 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
5758 if (strcmp (package_name
.get (), this_package_name
.get ()) != 0)
5759 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
5760 (sym
->symtab () != NULL
5761 ? symtab_to_filename_for_display
5763 : objfile_name (objfile
)),
5764 this_package_name
.get (), package_name
.get ());
5770 if (package_name
!= NULL
)
5772 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
5773 const char *saved_package_name
= objfile
->intern (package_name
.get ());
5775 = type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_MODULE
, 0,
5776 saved_package_name
);
5779 sym
= new (&objfile
->objfile_obstack
) symbol
;
5780 sym
->set_language (language_go
, &objfile
->objfile_obstack
);
5781 sym
->compute_and_set_names (saved_package_name
, false, objfile
->per_bfd
);
5782 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5783 e.g., "main" finds the "main" module and not C's main(). */
5784 sym
->set_domain (STRUCT_DOMAIN
);
5785 sym
->set_aclass_index (LOC_TYPEDEF
);
5786 sym
->set_type (type
);
5788 add_symbol_to_list (sym
, cu
->get_builder ()->get_global_symbols ());
5792 /* Allocate a fully-qualified name consisting of the two parts on the
5796 rust_fully_qualify (struct obstack
*obstack
, const char *p1
, const char *p2
)
5798 return obconcat (obstack
, p1
, "::", p2
, (char *) NULL
);
5801 /* A helper that allocates a variant part to attach to a Rust enum
5802 type. OBSTACK is where the results should be allocated. TYPE is
5803 the type we're processing. DISCRIMINANT_INDEX is the index of the
5804 discriminant. It must be the index of one of the fields of TYPE,
5805 or -1 to mean there is no discriminant (univariant enum).
5806 DEFAULT_INDEX is the index of the default field; or -1 if there is
5807 no default. RANGES is indexed by "effective" field number (the
5808 field index, but omitting the discriminant and default fields) and
5809 must hold the discriminant values used by the variants. Note that
5810 RANGES must have a lifetime at least as long as OBSTACK -- either
5811 already allocated on it, or static. */
5814 alloc_rust_variant (struct obstack
*obstack
, struct type
*type
,
5815 int discriminant_index
, int default_index
,
5816 gdb::array_view
<discriminant_range
> ranges
)
5818 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5819 gdb_assert (discriminant_index
== -1
5820 || (discriminant_index
>= 0
5821 && discriminant_index
< type
->num_fields ()));
5822 gdb_assert (default_index
== -1
5823 || (default_index
>= 0 && default_index
< type
->num_fields ()));
5825 /* We have one variant for each non-discriminant field. */
5826 int n_variants
= type
->num_fields ();
5827 if (discriminant_index
!= -1)
5830 variant
*variants
= new (obstack
) variant
[n_variants
];
5833 for (int i
= 0; i
< type
->num_fields (); ++i
)
5835 if (i
== discriminant_index
)
5838 variants
[var_idx
].first_field
= i
;
5839 variants
[var_idx
].last_field
= i
+ 1;
5841 /* The default field does not need a range, but other fields do.
5842 We skipped the discriminant above. */
5843 if (i
!= default_index
)
5845 variants
[var_idx
].discriminants
= ranges
.slice (range_idx
, 1);
5852 gdb_assert (range_idx
== ranges
.size ());
5853 gdb_assert (var_idx
== n_variants
);
5855 variant_part
*part
= new (obstack
) variant_part
;
5856 part
->discriminant_index
= discriminant_index
;
5857 /* If there is no discriminant, then whether it is signed is of no
5860 = (discriminant_index
== -1
5862 : type
->field (discriminant_index
).type ()->is_unsigned ());
5863 part
->variants
= gdb::array_view
<variant
> (variants
, n_variants
);
5865 void *storage
= obstack_alloc (obstack
, sizeof (gdb::array_view
<variant_part
>));
5866 gdb::array_view
<variant_part
> *prop_value
5867 = new (storage
) gdb::array_view
<variant_part
> (part
, 1);
5869 struct dynamic_prop prop
;
5870 prop
.set_variant_parts (prop_value
);
5872 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
5875 /* Some versions of rustc emitted enums in an unusual way.
5877 Ordinary enums were emitted as unions. The first element of each
5878 structure in the union was named "RUST$ENUM$DISR". This element
5879 held the discriminant.
5881 These versions of Rust also implemented the "non-zero"
5882 optimization. When the enum had two values, and one is empty and
5883 the other holds a pointer that cannot be zero, the pointer is used
5884 as the discriminant, with a zero value meaning the empty variant.
5885 Here, the union's first member is of the form
5886 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5887 where the fieldnos are the indices of the fields that should be
5888 traversed in order to find the field (which may be several fields deep)
5889 and the variantname is the name of the variant of the case when the
5892 This function recognizes whether TYPE is of one of these forms,
5893 and, if so, smashes it to be a variant type. */
5896 quirk_rust_enum (struct type
*type
, struct objfile
*objfile
)
5898 gdb_assert (type
->code () == TYPE_CODE_UNION
);
5900 /* We don't need to deal with empty enums. */
5901 if (type
->num_fields () == 0)
5904 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5905 if (type
->num_fields () == 1
5906 && startswith (type
->field (0).name (), RUST_ENUM_PREFIX
))
5908 const char *name
= type
->field (0).name () + strlen (RUST_ENUM_PREFIX
);
5910 /* Decode the field name to find the offset of the
5912 ULONGEST bit_offset
= 0;
5913 struct type
*field_type
= type
->field (0).type ();
5914 while (name
[0] >= '0' && name
[0] <= '9')
5917 unsigned long index
= strtoul (name
, &tail
, 10);
5920 || index
>= field_type
->num_fields ()
5921 || (field_type
->field (index
).loc_kind ()
5922 != FIELD_LOC_KIND_BITPOS
))
5924 complaint (_("Could not parse Rust enum encoding string \"%s\""
5926 type
->field (0).name (),
5927 objfile_name (objfile
));
5932 bit_offset
+= field_type
->field (index
).loc_bitpos ();
5933 field_type
= field_type
->field (index
).type ();
5936 /* Smash this type to be a structure type. We have to do this
5937 because the type has already been recorded. */
5938 type
->set_code (TYPE_CODE_STRUCT
);
5939 /* Save the field we care about. */
5940 struct field saved_field
= type
->field (0);
5941 type
->alloc_fields (3);
5943 /* Put the discriminant at index 0. */
5944 type
->field (0).set_type (field_type
);
5945 type
->field (0).set_is_artificial (true);
5946 type
->field (0).set_name ("<<discriminant>>");
5947 type
->field (0).set_loc_bitpos (bit_offset
);
5949 /* The order of fields doesn't really matter, so put the real
5950 field at index 1 and the data-less field at index 2. */
5951 type
->field (1) = saved_field
;
5952 type
->field (1).set_name
5953 (rust_last_path_segment (type
->field (1).type ()->name ()));
5954 type
->field (1).type ()->set_name
5955 (rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
5956 type
->field (1).name ()));
5958 const char *dataless_name
5959 = rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
5961 struct type
*dataless_type
5962 = type_allocator (type
).new_type (TYPE_CODE_VOID
, 0,
5964 type
->field (2).set_type (dataless_type
);
5965 /* NAME points into the original discriminant name, which
5966 already has the correct lifetime. */
5967 type
->field (2).set_name (name
);
5968 type
->field (2).set_loc_bitpos (0);
5970 /* Indicate that this is a variant type. */
5971 static discriminant_range ranges
[1] = { { 0, 0 } };
5972 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, 1, ranges
);
5974 /* A union with a single anonymous field is probably an old-style
5976 else if (type
->num_fields () == 1 && streq (type
->field (0).name (), ""))
5978 /* Smash this type to be a structure type. We have to do this
5979 because the type has already been recorded. */
5980 type
->set_code (TYPE_CODE_STRUCT
);
5982 struct type
*field_type
= type
->field (0).type ();
5983 const char *variant_name
5984 = rust_last_path_segment (field_type
->name ());
5985 type
->field (0).set_name (variant_name
);
5986 field_type
->set_name
5987 (rust_fully_qualify (&objfile
->objfile_obstack
,
5988 type
->name (), variant_name
));
5990 alloc_rust_variant (&objfile
->objfile_obstack
, type
, -1, 0, {});
5994 struct type
*disr_type
= nullptr;
5995 for (int i
= 0; i
< type
->num_fields (); ++i
)
5997 disr_type
= type
->field (i
).type ();
5999 if (disr_type
->code () != TYPE_CODE_STRUCT
)
6001 /* All fields of a true enum will be structs. */
6004 else if (disr_type
->num_fields () == 0)
6006 /* Could be data-less variant, so keep going. */
6007 disr_type
= nullptr;
6009 else if (strcmp (disr_type
->field (0).name (),
6010 "RUST$ENUM$DISR") != 0)
6012 /* Not a Rust enum. */
6022 /* If we got here without a discriminant, then it's probably
6024 if (disr_type
== nullptr)
6027 /* Smash this type to be a structure type. We have to do this
6028 because the type has already been recorded. */
6029 type
->set_code (TYPE_CODE_STRUCT
);
6031 /* Make space for the discriminant field. */
6032 struct field
*disr_field
= &disr_type
->field (0);
6034 = (struct field
*) TYPE_ZALLOC (type
, ((type
->num_fields () + 1)
6035 * sizeof (struct field
)));
6036 memcpy (new_fields
+ 1, type
->fields (),
6037 type
->num_fields () * sizeof (struct field
));
6038 type
->set_fields (new_fields
);
6039 type
->set_num_fields (type
->num_fields () + 1);
6041 /* Install the discriminant at index 0 in the union. */
6042 type
->field (0) = *disr_field
;
6043 type
->field (0).set_is_artificial (true);
6044 type
->field (0).set_name ("<<discriminant>>");
6046 /* We need a way to find the correct discriminant given a
6047 variant name. For convenience we build a map here. */
6048 struct type
*enum_type
= disr_field
->type ();
6049 std::unordered_map
<std::string
, ULONGEST
> discriminant_map
;
6050 for (int i
= 0; i
< enum_type
->num_fields (); ++i
)
6052 if (enum_type
->field (i
).loc_kind () == FIELD_LOC_KIND_ENUMVAL
)
6055 = rust_last_path_segment (enum_type
->field (i
).name ());
6056 discriminant_map
[name
] = enum_type
->field (i
).loc_enumval ();
6060 int n_fields
= type
->num_fields ();
6061 /* We don't need a range entry for the discriminant, but we do
6062 need one for every other field, as there is no default
6064 discriminant_range
*ranges
= XOBNEWVEC (&objfile
->objfile_obstack
,
6067 /* Skip the discriminant here. */
6068 for (int i
= 1; i
< n_fields
; ++i
)
6070 /* Find the final word in the name of this variant's type.
6071 That name can be used to look up the correct
6073 const char *variant_name
6074 = rust_last_path_segment (type
->field (i
).type ()->name ());
6076 auto iter
= discriminant_map
.find (variant_name
);
6077 if (iter
!= discriminant_map
.end ())
6079 ranges
[i
- 1].low
= iter
->second
;
6080 ranges
[i
- 1].high
= iter
->second
;
6083 /* In Rust, each element should have the size of the
6085 type
->field (i
).type ()->set_length (type
->length ());
6087 /* Remove the discriminant field, if it exists. */
6088 struct type
*sub_type
= type
->field (i
).type ();
6089 if (sub_type
->num_fields () > 0)
6091 sub_type
->set_num_fields (sub_type
->num_fields () - 1);
6092 sub_type
->set_fields (sub_type
->fields () + 1);
6094 type
->field (i
).set_name (variant_name
);
6096 (rust_fully_qualify (&objfile
->objfile_obstack
,
6097 type
->name (), variant_name
));
6100 /* Indicate that this is a variant type. */
6101 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, -1,
6102 gdb::array_view
<discriminant_range
> (ranges
,
6107 /* Rewrite some Rust unions to be structures with variants parts. */
6110 rust_union_quirks (struct dwarf2_cu
*cu
)
6112 gdb_assert (cu
->lang () == language_rust
);
6113 for (type
*type_
: cu
->rust_unions
)
6114 quirk_rust_enum (type_
, cu
->per_objfile
->objfile
);
6115 /* We don't need this any more. */
6116 cu
->rust_unions
.clear ();
6121 type_unit_group_unshareable
*
6122 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group
*tu_group
)
6124 auto iter
= this->m_type_units
.find (tu_group
);
6125 if (iter
!= this->m_type_units
.end ())
6126 return iter
->second
.get ();
6128 type_unit_group_unshareable_up
uniq (new type_unit_group_unshareable
);
6129 type_unit_group_unshareable
*result
= uniq
.get ();
6130 this->m_type_units
[tu_group
] = std::move (uniq
);
6135 dwarf2_per_objfile::get_type_for_signatured_type
6136 (signatured_type
*sig_type
) const
6138 auto iter
= this->m_type_map
.find (sig_type
);
6139 if (iter
== this->m_type_map
.end ())
6142 return iter
->second
;
6145 void dwarf2_per_objfile::set_type_for_signatured_type
6146 (signatured_type
*sig_type
, struct type
*type
)
6148 gdb_assert (this->m_type_map
.find (sig_type
) == this->m_type_map
.end ());
6150 this->m_type_map
[sig_type
] = type
;
6153 /* A helper function for computing the list of all symbol tables
6154 included by PER_CU. */
6157 recursively_compute_inclusions (std::vector
<compunit_symtab
*> *result
,
6158 htab_t all_children
, htab_t all_type_symtabs
,
6159 dwarf2_per_cu_data
*per_cu
,
6160 dwarf2_per_objfile
*per_objfile
,
6161 struct compunit_symtab
*immediate_parent
)
6163 void **slot
= htab_find_slot (all_children
, per_cu
, INSERT
);
6166 /* This inclusion and its children have been processed. */
6172 /* Only add a CU if it has a symbol table. */
6173 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
6176 /* If this is a type unit only add its symbol table if we haven't
6177 seen it yet (type unit per_cu's can share symtabs). */
6178 if (per_cu
->is_debug_types
)
6180 slot
= htab_find_slot (all_type_symtabs
, cust
, INSERT
);
6184 result
->push_back (cust
);
6185 if (cust
->user
== NULL
)
6186 cust
->user
= immediate_parent
;
6191 result
->push_back (cust
);
6192 if (cust
->user
== NULL
)
6193 cust
->user
= immediate_parent
;
6197 if (!per_cu
->imported_symtabs_empty ())
6198 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
6200 recursively_compute_inclusions (result
, all_children
,
6201 all_type_symtabs
, ptr
, per_objfile
,
6206 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6210 compute_compunit_symtab_includes (dwarf2_per_cu_data
*per_cu
,
6211 dwarf2_per_objfile
*per_objfile
)
6213 gdb_assert (! per_cu
->is_debug_types
);
6215 if (!per_cu
->imported_symtabs_empty ())
6218 std::vector
<compunit_symtab
*> result_symtabs
;
6219 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
6221 /* If we don't have a symtab, we can just skip this case. */
6225 htab_up
all_children (htab_create_alloc (1, htab_hash_pointer
,
6227 NULL
, xcalloc
, xfree
));
6228 htab_up
all_type_symtabs (htab_create_alloc (1, htab_hash_pointer
,
6230 NULL
, xcalloc
, xfree
));
6232 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
6234 recursively_compute_inclusions (&result_symtabs
, all_children
.get (),
6235 all_type_symtabs
.get (), ptr
,
6239 /* Now we have a transitive closure of all the included symtabs. */
6240 len
= result_symtabs
.size ();
6242 = XOBNEWVEC (&per_objfile
->objfile
->objfile_obstack
,
6243 struct compunit_symtab
*, len
+ 1);
6244 memcpy (cust
->includes
, result_symtabs
.data (),
6245 len
* sizeof (compunit_symtab
*));
6246 cust
->includes
[len
] = NULL
;
6250 /* Compute the 'includes' field for the symtabs of all the CUs we just
6254 process_cu_includes (dwarf2_per_objfile
*per_objfile
)
6256 for (dwarf2_per_cu_data
*iter
: per_objfile
->per_bfd
->just_read_cus
)
6258 if (! iter
->is_debug_types
)
6259 compute_compunit_symtab_includes (iter
, per_objfile
);
6262 per_objfile
->per_bfd
->just_read_cus
.clear ();
6265 /* Generate full symbol information for CU, whose DIEs have
6266 already been loaded into memory. */
6269 process_full_comp_unit (dwarf2_cu
*cu
, enum language pretend_language
)
6271 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6272 unrelocated_addr lowpc
, highpc
;
6273 struct compunit_symtab
*cust
;
6274 struct block
*static_block
;
6277 /* Clear the list here in case something was left over. */
6278 cu
->method_list
.clear ();
6280 dwarf2_find_base_address (cu
->dies
, cu
);
6282 /* Before we start reading the top-level DIE, ensure it has a valid tag
6284 switch (cu
->dies
->tag
)
6286 case DW_TAG_compile_unit
:
6287 case DW_TAG_partial_unit
:
6288 case DW_TAG_type_unit
:
6291 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6292 dwarf_tag_name (cu
->dies
->tag
),
6293 sect_offset_str (cu
->per_cu
->sect_off
),
6294 objfile_name (per_objfile
->objfile
));
6297 /* Do line number decoding in read_file_scope () */
6298 process_die (cu
->dies
, cu
);
6300 /* For now fudge the Go package. */
6301 if (cu
->lang () == language_go
)
6302 fixup_go_packaging (cu
);
6304 /* Now that we have processed all the DIEs in the CU, all the types
6305 should be complete, and it should now be safe to compute all of the
6307 compute_delayed_physnames (cu
);
6309 if (cu
->lang () == language_rust
)
6310 rust_union_quirks (cu
);
6312 /* Some compilers don't define a DW_AT_high_pc attribute for the
6313 compilation unit. If the DW_AT_high_pc is missing, synthesize
6314 it, by scanning the DIE's below the compilation unit. */
6315 get_scope_pc_bounds (cu
->dies
, &lowpc
, &highpc
, cu
);
6317 addr
= per_objfile
->relocate (highpc
);
6319 = cu
->get_builder ()->end_compunit_symtab_get_static_block (addr
, 0, 1);
6321 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6322 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6323 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6324 addrmap to help ensure it has an accurate map of pc values belonging to
6326 dwarf2_record_block_ranges (cu
->dies
, static_block
, cu
);
6328 cust
= cu
->get_builder ()->end_compunit_symtab_from_static_block
6333 int gcc_4_minor
= producer_is_gcc_ge_4 (cu
->producer
);
6335 /* Set symtab language to language from DW_AT_language. If the
6336 compilation is from a C file generated by language preprocessors, do
6337 not set the language if it was already deduced by start_subfile. */
6338 if (!(cu
->lang () == language_c
6339 && cust
->primary_filetab ()->language () != language_unknown
))
6340 cust
->primary_filetab ()->set_language (cu
->lang ());
6342 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6343 produce DW_AT_location with location lists but it can be possibly
6344 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6345 there were bugs in prologue debug info, fixed later in GCC-4.5
6346 by "unwind info for epilogues" patch (which is not directly related).
6348 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6349 needed, it would be wrong due to missing DW_AT_producer there.
6351 Still one can confuse GDB by using non-standard GCC compilation
6352 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6354 if (cu
->has_loclist
&& gcc_4_minor
>= 5)
6355 cust
->set_locations_valid (true);
6358 if (cu
->producer
!= nullptr
6359 && producer_is_gcc (cu
->producer
, &major
, &minor
)
6360 && (major
< 4 || (major
== 4 && minor
< 5)))
6361 /* Don't trust gcc < 4.5.x. */
6362 cust
->set_epilogue_unwind_valid (false);
6364 cust
->set_epilogue_unwind_valid (true);
6366 cust
->set_call_site_htab (cu
->call_site_htab
);
6369 per_objfile
->set_symtab (cu
->per_cu
, cust
);
6371 /* Push it for inclusion processing later. */
6372 per_objfile
->per_bfd
->just_read_cus
.push_back (cu
->per_cu
);
6374 /* Not needed any more. */
6375 cu
->reset_builder ();
6378 /* Generate full symbol information for type unit CU, whose DIEs have
6379 already been loaded into memory. */
6382 process_full_type_unit (dwarf2_cu
*cu
,
6383 enum language pretend_language
)
6385 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6386 struct compunit_symtab
*cust
;
6387 struct signatured_type
*sig_type
;
6389 gdb_assert (cu
->per_cu
->is_debug_types
);
6390 sig_type
= (struct signatured_type
*) cu
->per_cu
;
6392 /* Clear the list here in case something was left over. */
6393 cu
->method_list
.clear ();
6395 /* The symbol tables are set up in read_type_unit_scope. */
6396 process_die (cu
->dies
, cu
);
6398 /* For now fudge the Go package. */
6399 if (cu
->lang () == language_go
)
6400 fixup_go_packaging (cu
);
6402 /* Now that we have processed all the DIEs in the CU, all the types
6403 should be complete, and it should now be safe to compute all of the
6405 compute_delayed_physnames (cu
);
6407 if (cu
->lang () == language_rust
)
6408 rust_union_quirks (cu
);
6410 /* TUs share symbol tables.
6411 If this is the first TU to use this symtab, complete the construction
6412 of it with end_expandable_symtab. Otherwise, complete the addition of
6413 this TU's symbols to the existing symtab. */
6414 type_unit_group_unshareable
*tug_unshare
=
6415 per_objfile
->get_type_unit_group_unshareable (sig_type
->type_unit_group
);
6416 if (tug_unshare
->compunit_symtab
== NULL
)
6418 buildsym_compunit
*builder
= cu
->get_builder ();
6419 cust
= builder
->end_expandable_symtab (0);
6420 tug_unshare
->compunit_symtab
= cust
;
6424 /* Set symtab language to language from DW_AT_language. If the
6425 compilation is from a C file generated by language preprocessors,
6426 do not set the language if it was already deduced by
6428 if (!(cu
->lang () == language_c
6429 && cust
->primary_filetab ()->language () != language_c
))
6430 cust
->primary_filetab ()->set_language (cu
->lang ());
6435 cu
->get_builder ()->augment_type_symtab ();
6436 cust
= tug_unshare
->compunit_symtab
;
6439 per_objfile
->set_symtab (cu
->per_cu
, cust
);
6441 /* Not needed any more. */
6442 cu
->reset_builder ();
6445 /* Process an imported unit DIE. */
6448 process_imported_unit_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
6450 struct attribute
*attr
;
6452 /* For now we don't handle imported units in type units. */
6453 if (cu
->per_cu
->is_debug_types
)
6455 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6456 " supported in type units [in module %s]"),
6457 objfile_name (cu
->per_objfile
->objfile
));
6460 attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
6463 sect_offset sect_off
= attr
->get_ref_die_offset ();
6464 bool is_dwz
= (attr
->form
== DW_FORM_GNU_ref_alt
|| cu
->per_cu
->is_dwz
);
6465 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6466 dwarf2_per_cu_data
*per_cu
6467 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
6468 per_objfile
->per_bfd
);
6470 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6471 into another compilation unit, at root level. Regard this as a hint,
6472 and ignore it. This is a best effort, it only works if unit_type and
6473 lang are already set. */
6474 if (die
->parent
&& die
->parent
->parent
== NULL
6475 && per_cu
->unit_type (false) == DW_UT_compile
6476 && per_cu
->lang (false) == language_cplus
)
6479 /* If necessary, add it to the queue and load its DIEs. */
6480 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
6482 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
6483 false, cu
->lang ());
6485 cu
->per_cu
->imported_symtabs_push (per_cu
);
6489 /* RAII object that represents a process_die scope: i.e.,
6490 starts/finishes processing a DIE. */
6491 class process_die_scope
6494 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
6495 : m_die (die
), m_cu (cu
)
6497 /* We should only be processing DIEs not already in process. */
6498 gdb_assert (!m_die
->in_process
);
6499 m_die
->in_process
= true;
6502 ~process_die_scope ()
6504 m_die
->in_process
= false;
6506 /* If we're done processing the DIE for the CU that owns the line
6507 header, we don't need the line header anymore. */
6508 if (m_cu
->line_header_die_owner
== m_die
)
6510 delete m_cu
->line_header
;
6511 m_cu
->line_header
= NULL
;
6512 m_cu
->line_header_die_owner
= NULL
;
6521 /* Process a die and its children. */
6524 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
6526 process_die_scope
scope (die
, cu
);
6530 case DW_TAG_padding
:
6532 case DW_TAG_compile_unit
:
6533 case DW_TAG_partial_unit
:
6534 read_file_scope (die
, cu
);
6536 case DW_TAG_type_unit
:
6537 read_type_unit_scope (die
, cu
);
6539 case DW_TAG_subprogram
:
6540 /* Nested subprograms in Fortran get a prefix. */
6541 if (cu
->lang () == language_fortran
6542 && die
->parent
!= NULL
6543 && die
->parent
->tag
== DW_TAG_subprogram
)
6544 cu
->processing_has_namespace_info
= true;
6547 case DW_TAG_entry_point
:
6548 case DW_TAG_inlined_subroutine
:
6549 read_func_scope (die
, cu
);
6551 case DW_TAG_lexical_block
:
6552 case DW_TAG_try_block
:
6553 case DW_TAG_catch_block
:
6554 read_lexical_block_scope (die
, cu
);
6556 case DW_TAG_call_site
:
6557 case DW_TAG_GNU_call_site
:
6558 read_call_site_scope (die
, cu
);
6560 case DW_TAG_class_type
:
6561 case DW_TAG_interface_type
:
6562 case DW_TAG_structure_type
:
6563 case DW_TAG_union_type
:
6564 case DW_TAG_namelist
:
6565 process_structure_scope (die
, cu
);
6567 case DW_TAG_enumeration_type
:
6568 process_enumeration_scope (die
, cu
);
6571 /* These dies have a type, but processing them does not create
6572 a symbol or recurse to process the children. Therefore we can
6573 read them on-demand through read_type_die. */
6574 case DW_TAG_subroutine_type
:
6575 case DW_TAG_set_type
:
6576 case DW_TAG_pointer_type
:
6577 case DW_TAG_ptr_to_member_type
:
6578 case DW_TAG_reference_type
:
6579 case DW_TAG_rvalue_reference_type
:
6580 case DW_TAG_string_type
:
6583 case DW_TAG_array_type
:
6584 /* We only need to handle this case for Ada -- in other
6585 languages, it's normal for the compiler to emit a typedef
6587 if (cu
->lang () != language_ada
)
6590 case DW_TAG_base_type
:
6591 case DW_TAG_subrange_type
:
6592 case DW_TAG_generic_subrange
:
6593 case DW_TAG_typedef
:
6594 /* Add a typedef symbol for the type definition, if it has a
6596 new_symbol (die
, read_type_die (die
, cu
), cu
);
6598 case DW_TAG_common_block
:
6599 read_common_block (die
, cu
);
6601 case DW_TAG_common_inclusion
:
6603 case DW_TAG_namespace
:
6604 cu
->processing_has_namespace_info
= true;
6605 read_namespace (die
, cu
);
6608 cu
->processing_has_namespace_info
= true;
6609 read_module (die
, cu
);
6611 case DW_TAG_imported_declaration
:
6612 cu
->processing_has_namespace_info
= true;
6613 if (read_alias (die
, cu
))
6615 /* The declaration is neither a global namespace nor a variable
6618 case DW_TAG_imported_module
:
6619 cu
->processing_has_namespace_info
= true;
6620 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
6621 || cu
->lang () != language_fortran
))
6622 complaint (_("Tag '%s' has unexpected children"),
6623 dwarf_tag_name (die
->tag
));
6624 read_import_statement (die
, cu
);
6627 case DW_TAG_imported_unit
:
6628 process_imported_unit_die (die
, cu
);
6631 case DW_TAG_variable
:
6632 read_variable (die
, cu
);
6636 new_symbol (die
, NULL
, cu
);
6641 /* DWARF name computation. */
6643 /* A helper function for dwarf2_compute_name which determines whether DIE
6644 needs to have the name of the scope prepended to the name listed in the
6648 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
6650 struct attribute
*attr
;
6654 case DW_TAG_namespace
:
6655 case DW_TAG_typedef
:
6656 case DW_TAG_class_type
:
6657 case DW_TAG_interface_type
:
6658 case DW_TAG_structure_type
:
6659 case DW_TAG_union_type
:
6660 case DW_TAG_enumeration_type
:
6661 case DW_TAG_enumerator
:
6662 case DW_TAG_subprogram
:
6663 case DW_TAG_inlined_subroutine
:
6664 case DW_TAG_entry_point
:
6666 case DW_TAG_imported_declaration
:
6669 case DW_TAG_variable
:
6670 case DW_TAG_constant
:
6671 /* We only need to prefix "globally" visible variables. These include
6672 any variable marked with DW_AT_external or any variable that
6673 lives in a namespace. [Variables in anonymous namespaces
6674 require prefixing, but they are not DW_AT_external.] */
6676 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
6678 struct dwarf2_cu
*spec_cu
= cu
;
6680 return die_needs_namespace (die_specification (die
, &spec_cu
),
6684 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
6685 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
6686 && die
->parent
->tag
!= DW_TAG_module
)
6688 /* A variable in a lexical block of some kind does not need a
6689 namespace, even though in C++ such variables may be external
6690 and have a mangled name. */
6691 if (die
->parent
->tag
== DW_TAG_lexical_block
6692 || die
->parent
->tag
== DW_TAG_try_block
6693 || die
->parent
->tag
== DW_TAG_catch_block
6694 || die
->parent
->tag
== DW_TAG_subprogram
)
6703 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6704 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6705 defined for the given DIE. */
6707 static struct attribute
*
6708 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
6710 struct attribute
*attr
;
6712 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
6714 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
6719 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6720 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6721 defined for the given DIE. */
6724 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
6726 const char *linkage_name
;
6728 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
6729 if (linkage_name
== NULL
)
6730 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
6732 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6733 See https://github.com/rust-lang/rust/issues/32925. */
6734 if (cu
->lang () == language_rust
&& linkage_name
!= NULL
6735 && strchr (linkage_name
, '{') != NULL
)
6736 linkage_name
= NULL
;
6738 return linkage_name
;
6741 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6742 compute the physname for the object, which include a method's:
6743 - formal parameters (C++),
6744 - receiver type (Go),
6746 The term "physname" is a bit confusing.
6747 For C++, for example, it is the demangled name.
6748 For Go, for example, it's the mangled name.
6750 For Ada, return the DIE's linkage name rather than the fully qualified
6751 name. PHYSNAME is ignored..
6753 The result is allocated on the objfile->per_bfd's obstack and
6757 dwarf2_compute_name (const char *name
,
6758 struct die_info
*die
, struct dwarf2_cu
*cu
,
6761 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
6764 name
= dwarf2_name (die
, cu
);
6766 enum language lang
= cu
->lang ();
6768 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6769 but otherwise compute it by typename_concat inside GDB.
6770 FIXME: Actually this is not really true, or at least not always true.
6771 It's all very confusing. compute_and_set_names doesn't try to demangle
6772 Fortran names because there is no mangling standard. So new_symbol
6773 will set the demangled name to the result of dwarf2_full_name, and it is
6774 the demangled name that GDB uses if it exists. */
6775 if (lang
== language_ada
6776 || (lang
== language_fortran
&& physname
))
6778 /* For Ada unit, we prefer the linkage name over the name, as
6779 the former contains the exported name, which the user expects
6780 to be able to reference. Ideally, we want the user to be able
6781 to reference this entity using either natural or linkage name,
6782 but we haven't started looking at this enhancement yet. */
6783 const char *linkage_name
= dw2_linkage_name (die
, cu
);
6785 if (linkage_name
!= NULL
)
6786 return linkage_name
;
6789 /* These are the only languages we know how to qualify names in. */
6791 && (lang
== language_cplus
6792 || lang
== language_fortran
|| lang
== language_d
6793 || lang
== language_rust
))
6795 if (die_needs_namespace (die
, cu
))
6801 prefix
= determine_prefix (die
, cu
);
6802 if (*prefix
!= '\0')
6804 gdb::unique_xmalloc_ptr
<char> prefixed_name
6805 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
6807 buf
.puts (prefixed_name
.get ());
6812 /* Template parameters may be specified in the DIE's DW_AT_name, or
6813 as children with DW_TAG_template_type_param or
6814 DW_TAG_value_type_param. If the latter, add them to the name
6815 here. If the name already has template parameters, then
6816 skip this step; some versions of GCC emit both, and
6817 it is more efficient to use the pre-computed name.
6819 Something to keep in mind about this process: it is very
6820 unlikely, or in some cases downright impossible, to produce
6821 something that will match the mangled name of a function.
6822 If the definition of the function has the same debug info,
6823 we should be able to match up with it anyway. But fallbacks
6824 using the minimal symbol, for instance to find a method
6825 implemented in a stripped copy of libstdc++, will not work.
6826 If we do not have debug info for the definition, we will have to
6827 match them up some other way.
6829 When we do name matching there is a related problem with function
6830 templates; two instantiated function templates are allowed to
6831 differ only by their return types, which we do not add here. */
6833 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
6835 struct attribute
*attr
;
6836 struct die_info
*child
;
6839 die
->building_fullname
= 1;
6841 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
6845 const gdb_byte
*bytes
;
6846 struct dwarf2_locexpr_baton
*baton
;
6849 if (child
->tag
!= DW_TAG_template_type_param
6850 && child
->tag
!= DW_TAG_template_value_param
)
6861 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
6864 complaint (_("template parameter missing DW_AT_type"));
6865 buf
.puts ("UNKNOWN_TYPE");
6868 type
= die_type (child
, cu
);
6870 if (child
->tag
== DW_TAG_template_type_param
)
6872 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
6873 &type_print_raw_options
);
6877 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
6880 complaint (_("template parameter missing "
6881 "DW_AT_const_value"));
6882 buf
.puts ("UNKNOWN_VALUE");
6886 dwarf2_const_value_attr (attr
, type
, name
,
6887 &cu
->comp_unit_obstack
, cu
,
6888 &value
, &bytes
, &baton
);
6890 if (type
->has_no_signedness ())
6891 /* GDB prints characters as NUMBER 'CHAR'. If that's
6892 changed, this can use value_print instead. */
6893 cu
->language_defn
->printchar (value
, type
, &buf
);
6896 struct value_print_options opts
;
6899 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
6903 baton
->per_objfile
);
6904 else if (bytes
!= NULL
)
6906 v
= value::allocate (type
);
6907 memcpy (v
->contents_writeable ().data (), bytes
,
6911 v
= value_from_longest (type
, value
);
6913 /* Specify decimal so that we do not depend on
6915 get_formatted_print_options (&opts
, 'd');
6917 value_print (v
, &buf
, &opts
);
6922 die
->building_fullname
= 0;
6926 /* Close the argument list, with a space if necessary
6927 (nested templates). */
6928 if (!buf
.empty () && buf
.string ().back () == '>')
6935 /* For C++ methods, append formal parameter type
6936 information, if PHYSNAME. */
6938 if (physname
&& die
->tag
== DW_TAG_subprogram
6939 && lang
== language_cplus
)
6941 struct type
*type
= read_type_die (die
, cu
);
6943 c_type_print_args (type
, &buf
, 1, lang
,
6944 &type_print_raw_options
);
6946 if (lang
== language_cplus
)
6948 /* Assume that an artificial first parameter is
6949 "this", but do not crash if it is not. RealView
6950 marks unnamed (and thus unused) parameters as
6951 artificial; there is no way to differentiate
6953 if (type
->num_fields () > 0
6954 && type
->field (0).is_artificial ()
6955 && type
->field (0).type ()->code () == TYPE_CODE_PTR
6956 && TYPE_CONST (type
->field (0).type ()->target_type ()))
6957 buf
.puts (" const");
6961 const std::string
&intermediate_name
= buf
.string ();
6963 const char *canonical_name
6964 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
6967 /* If we only computed INTERMEDIATE_NAME, or if
6968 INTERMEDIATE_NAME is already canonical, then we need to
6970 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
6971 name
= objfile
->intern (intermediate_name
);
6973 name
= canonical_name
;
6980 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6981 If scope qualifiers are appropriate they will be added. The result
6982 will be allocated on the storage_obstack, or NULL if the DIE does
6983 not have a name. NAME may either be from a previous call to
6984 dwarf2_name or NULL.
6986 The output string will be canonicalized (if C++). */
6989 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
6991 return dwarf2_compute_name (name
, die
, cu
, 0);
6994 /* Construct a physname for the given DIE in CU. NAME may either be
6995 from a previous call to dwarf2_name or NULL. The result will be
6996 allocated on the objfile_objstack or NULL if the DIE does not have a
6999 The output string will be canonicalized (if C++). */
7002 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
7004 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7005 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
7008 /* In this case dwarf2_compute_name is just a shortcut not building anything
7010 if (!die_needs_namespace (die
, cu
))
7011 return dwarf2_compute_name (name
, die
, cu
, 1);
7013 if (cu
->lang () != language_rust
)
7014 mangled
= dw2_linkage_name (die
, cu
);
7016 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7018 gdb::unique_xmalloc_ptr
<char> demangled
;
7019 if (mangled
!= NULL
)
7021 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
7023 /* Do nothing (do not demangle the symbol name). */
7027 /* Use DMGL_RET_DROP for C++ template functions to suppress
7028 their return type. It is easier for GDB users to search
7029 for such functions as `name(params)' than `long name(params)'.
7030 In such case the minimal symbol names do not match the full
7031 symbol names but for template functions there is never a need
7032 to look up their definition from their declaration so
7033 the only disadvantage remains the minimal symbol variant
7034 `long name(params)' does not have the proper inferior type. */
7035 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
7039 canon
= demangled
.get ();
7047 if (canon
== NULL
|| check_physname
)
7049 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
7051 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
7053 /* It may not mean a bug in GDB. The compiler could also
7054 compute DW_AT_linkage_name incorrectly. But in such case
7055 GDB would need to be bug-to-bug compatible. */
7057 complaint (_("Computed physname <%s> does not match demangled <%s> "
7058 "(from linkage <%s>) - DIE at %s [in module %s]"),
7059 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
7060 objfile_name (objfile
));
7062 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7063 is available here - over computed PHYSNAME. It is safer
7064 against both buggy GDB and buggy compilers. */
7078 retval
= objfile
->intern (retval
);
7083 /* Inspect DIE in CU for a namespace alias or a variable with alias
7084 attribute. If one exists, record a new symbol for it.
7086 Returns true if an alias was recorded, false otherwise. */
7089 read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
7091 struct attribute
*attr
;
7093 /* If the die does not have a name, this is neither a namespace
7094 alias nor a variable alias. */
7095 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
7099 struct die_info
*d
= die
;
7100 struct dwarf2_cu
*imported_cu
= cu
;
7102 /* If the compiler has nested DW_AT_imported_declaration DIEs,
7103 keep inspecting DIEs until we hit the underlying import. */
7104 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
7105 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
7107 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
7111 d
= follow_die_ref (d
, attr
, &imported_cu
);
7112 if (d
->tag
!= DW_TAG_imported_declaration
)
7116 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
7118 complaint (_("DIE at %s has too many recursively imported "
7119 "declarations"), sect_offset_str (d
->sect_off
));
7126 if (d
->tag
== DW_TAG_variable
)
7128 /* This declaration is a C/C++ global variable alias.
7129 Add a symbol for it whose type is the same as the
7130 aliased variable's. */
7131 type
= die_type (d
, imported_cu
);
7132 struct symbol
*sym
= new_symbol (die
, type
, cu
);
7133 attr
= dwarf2_attr (d
, DW_AT_location
, imported_cu
);
7134 sym
->set_aclass_index (LOC_UNRESOLVED
);
7135 if (attr
!= nullptr)
7136 var_decode_location (attr
, sym
, cu
);
7141 sect_offset sect_off
= attr
->get_ref_die_offset ();
7142 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
,
7144 if (type
!= nullptr && type
->code () == TYPE_CODE_NAMESPACE
)
7146 /* This declaration is a global namespace alias. Add
7147 a symbol for it whose type is the aliased
7149 new_symbol (die
, type
, cu
);
7158 /* Return the using directives repository (global or local?) to use in the
7159 current context for CU.
7161 For Ada, imported declarations can materialize renamings, which *may* be
7162 global. However it is impossible (for now?) in DWARF to distinguish
7163 "external" imported declarations and "static" ones. As all imported
7164 declarations seem to be static in all other languages, make them all CU-wide
7165 global only in Ada. */
7167 static struct using_direct
**
7168 using_directives (struct dwarf2_cu
*cu
)
7170 if (cu
->lang () == language_ada
7171 && cu
->get_builder ()->outermost_context_p ())
7172 return cu
->get_builder ()->get_global_using_directives ();
7174 return cu
->get_builder ()->get_local_using_directives ();
7177 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7178 given CU. If the format is not recognized or the attribute is
7179 not present, set it to 0. */
7182 read_decl_line (struct die_info
*die
, struct dwarf2_cu
*cu
)
7184 struct attribute
*decl_line
= dwarf2_attr (die
, DW_AT_decl_line
, cu
);
7185 if (decl_line
== nullptr)
7187 if (decl_line
->form_is_constant ())
7189 LONGEST val
= decl_line
->constant_value (0);
7190 if (0 <= val
&& val
<= UINT_MAX
)
7191 return (unsigned int) val
;
7193 complaint (_("Declared line for using directive is too large"));
7197 complaint (_("Declared line for using directive is of incorrect format"));
7201 /* Read the import statement specified by the given die and record it. */
7204 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
7206 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7207 struct attribute
*import_attr
;
7208 struct die_info
*imported_die
, *child_die
;
7209 struct dwarf2_cu
*imported_cu
;
7210 const char *imported_name
;
7211 const char *imported_name_prefix
;
7212 const char *canonical_name
;
7213 const char *import_alias
;
7214 const char *imported_declaration
= NULL
;
7215 const char *import_prefix
;
7216 std::vector
<const char *> excludes
;
7218 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
7219 if (import_attr
== NULL
)
7221 complaint (_("Tag '%s' has no DW_AT_import"),
7222 dwarf_tag_name (die
->tag
));
7227 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
7228 imported_name
= dwarf2_name (imported_die
, imported_cu
);
7229 if (imported_name
== NULL
)
7231 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7233 The import in the following code:
7247 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7248 <52> DW_AT_decl_file : 1
7249 <53> DW_AT_decl_line : 6
7250 <54> DW_AT_import : <0x75>
7251 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7253 <5b> DW_AT_decl_file : 1
7254 <5c> DW_AT_decl_line : 2
7255 <5d> DW_AT_type : <0x6e>
7257 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7258 <76> DW_AT_byte_size : 4
7259 <77> DW_AT_encoding : 5 (signed)
7261 imports the wrong die ( 0x75 instead of 0x58 ).
7262 This case will be ignored until the gcc bug is fixed. */
7266 /* Figure out the local name after import. */
7267 import_alias
= dwarf2_name (die
, cu
);
7269 /* Figure out where the statement is being imported to. */
7270 import_prefix
= determine_prefix (die
, cu
);
7272 /* Figure out what the scope of the imported die is and prepend it
7273 to the name of the imported die. */
7274 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
7276 if (imported_die
->tag
!= DW_TAG_namespace
7277 && imported_die
->tag
!= DW_TAG_module
)
7279 imported_declaration
= imported_name
;
7280 canonical_name
= imported_name_prefix
;
7282 else if (strlen (imported_name_prefix
) > 0)
7283 canonical_name
= obconcat (&objfile
->objfile_obstack
,
7284 imported_name_prefix
,
7285 (cu
->lang () == language_d
7288 imported_name
, (char *) NULL
);
7290 canonical_name
= imported_name
;
7292 if (die
->tag
== DW_TAG_imported_module
7293 && cu
->lang () == language_fortran
)
7294 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
7295 child_die
= child_die
->sibling
)
7297 /* DWARF-4: A Fortran use statement with a “rename list” may be
7298 represented by an imported module entry with an import attribute
7299 referring to the module and owned entries corresponding to those
7300 entities that are renamed as part of being imported. */
7302 if (child_die
->tag
!= DW_TAG_imported_declaration
)
7304 complaint (_("child DW_TAG_imported_declaration expected "
7305 "- DIE at %s [in module %s]"),
7306 sect_offset_str (child_die
->sect_off
),
7307 objfile_name (objfile
));
7311 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
7312 if (import_attr
== NULL
)
7314 complaint (_("Tag '%s' has no DW_AT_import"),
7315 dwarf_tag_name (child_die
->tag
));
7320 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
7322 imported_name
= dwarf2_name (imported_die
, imported_cu
);
7323 if (imported_name
== NULL
)
7325 complaint (_("child DW_TAG_imported_declaration has unknown "
7326 "imported name - DIE at %s [in module %s]"),
7327 sect_offset_str (child_die
->sect_off
),
7328 objfile_name (objfile
));
7332 excludes
.push_back (imported_name
);
7334 process_die (child_die
, cu
);
7337 add_using_directive (using_directives (cu
),
7341 imported_declaration
,
7343 read_decl_line (die
, cu
),
7345 &objfile
->objfile_obstack
);
7348 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7349 types, but gives them a size of zero. Starting with version 14,
7350 ICC is compatible with GCC. */
7353 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
7355 if (!cu
->checked_producer
)
7356 check_producer (cu
);
7358 return cu
->producer_is_icc_lt_14
;
7361 /* ICC generates a DW_AT_type for C void functions. This was observed on
7362 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7363 which says that void functions should not have a DW_AT_type. */
7366 producer_is_icc (struct dwarf2_cu
*cu
)
7368 if (!cu
->checked_producer
)
7369 check_producer (cu
);
7371 return cu
->producer_is_icc
;
7374 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7375 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7376 this, it was first present in GCC release 4.3.0. */
7379 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
7381 if (!cu
->checked_producer
)
7382 check_producer (cu
);
7384 return cu
->producer_is_gcc_lt_4_3
;
7387 /* See dwarf2/read.h. */
7389 producer_is_clang (struct dwarf2_cu
*cu
)
7391 if (!cu
->checked_producer
)
7392 check_producer (cu
);
7394 return cu
->producer_is_clang
;
7397 static file_and_directory
&
7398 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
7400 if (cu
->per_cu
->fnd
!= nullptr)
7401 return *cu
->per_cu
->fnd
;
7403 /* Find the filename. Do not use dwarf2_name here, since the filename
7404 is not a source language identifier. */
7405 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
7406 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
7408 if (res
.get_comp_dir () == nullptr
7409 && producer_is_gcc_lt_4_3 (cu
)
7410 && res
.get_name () != nullptr
7411 && IS_ABSOLUTE_PATH (res
.get_name ()))
7413 res
.set_comp_dir (ldirname (res
.get_name ()));
7414 res
.set_name (make_unique_xstrdup (lbasename (res
.get_name ())));
7417 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
7418 return *cu
->per_cu
->fnd
;
7421 /* Handle DW_AT_stmt_list for a compilation unit.
7422 DIE is the DW_TAG_compile_unit die for CU.
7423 COMP_DIR is the compilation directory. LOWPC is passed to
7424 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7427 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
7428 const file_and_directory
&fnd
, unrelocated_addr lowpc
,
7429 bool have_code
) /* ARI: editCase function */
7431 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
7432 struct attribute
*attr
;
7433 hashval_t line_header_local_hash
;
7437 gdb_assert (! cu
->per_cu
->is_debug_types
);
7439 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
7440 if (attr
== NULL
|| !attr
->form_is_unsigned ())
7443 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
7445 /* The line header hash table is only created if needed (it exists to
7446 prevent redundant reading of the line table for partial_units).
7447 If we're given a partial_unit, we'll need it. If we're given a
7448 compile_unit, then use the line header hash table if it's already
7449 created, but don't create one just yet. */
7451 if (per_objfile
->line_header_hash
== NULL
7452 && die
->tag
== DW_TAG_partial_unit
)
7454 per_objfile
->line_header_hash
7455 .reset (htab_create_alloc (127, line_header_hash_voidp
,
7456 line_header_eq_voidp
,
7457 htab_delete_entry
<line_header
>,
7461 line_header
line_header_local (line_offset
, cu
->per_cu
->is_dwz
);
7462 line_header_local_hash
= line_header_hash (&line_header_local
);
7463 if (per_objfile
->line_header_hash
!= NULL
)
7465 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
7467 line_header_local_hash
, NO_INSERT
);
7469 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7470 is not present in *SLOT (since if there is something in *SLOT then
7471 it will be for a partial_unit). */
7472 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
7474 gdb_assert (*slot
!= NULL
);
7475 cu
->line_header
= (struct line_header
*) *slot
;
7480 /* dwarf_decode_line_header does not yet provide sufficient information.
7481 We always have to call also dwarf_decode_lines for it. */
7482 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
,
7483 fnd
.get_comp_dir ());
7487 cu
->line_header
= lh
.release ();
7488 cu
->line_header_die_owner
= die
;
7490 if (per_objfile
->line_header_hash
== NULL
)
7494 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
7496 line_header_local_hash
, INSERT
);
7497 gdb_assert (slot
!= NULL
);
7499 if (slot
!= NULL
&& *slot
== NULL
)
7501 /* This newly decoded line number information unit will be owned
7502 by line_header_hash hash table. */
7503 *slot
= cu
->line_header
;
7504 cu
->line_header_die_owner
= NULL
;
7508 /* We cannot free any current entry in (*slot) as that struct line_header
7509 may be already used by multiple CUs. Create only temporary decoded
7510 line_header for this CU - it may happen at most once for each line
7511 number information unit. And if we're not using line_header_hash
7512 then this is what we want as well. */
7513 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
7515 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
7516 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7517 then there won't be any interesting code in the CU, but a check later on
7518 (in lnp_state_machine::check_line_address) will fail to properly exclude
7519 an entry that was removed via --gc-sections. */
7520 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
&& have_code
);
7523 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7526 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
7528 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
7529 struct objfile
*objfile
= per_objfile
->objfile
;
7531 struct attribute
*attr
;
7532 struct die_info
*child_die
;
7534 prepare_one_comp_unit (cu
, die
, cu
->lang ());
7536 unrelocated_addr unrel_low
, unrel_high
;
7537 get_scope_pc_bounds (die
, &unrel_low
, &unrel_high
, cu
);
7539 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7540 from finish_block. */
7541 if (unrel_low
== ((unrelocated_addr
) -1))
7542 unrel_low
= unrel_high
;
7543 lowpc
= per_objfile
->relocate (unrel_low
);
7545 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
7547 /* GAS supports generating dwarf-5 info starting version 2.35. Versions
7548 2.35-2.37 generate an incorrect CU name attribute: it's relative,
7549 implicitly prefixing it with the compilation dir. Work around this by
7550 prefixing it with the source dir instead. */
7551 if (cu
->header
.version
== 5 && !IS_ABSOLUTE_PATH (fnd
.get_name ())
7552 && producer_is_gas_lt_2_38 (cu
))
7554 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
7555 if (attr
!= nullptr && attr
->form_is_unsigned ())
7557 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
7558 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
,
7559 fnd
.get_comp_dir ());
7560 if (lh
->version
== 5 && lh
->is_valid_file_index (1))
7562 std::string dir
= lh
->include_dir_at (1);
7563 fnd
.set_comp_dir (std::move (dir
));
7568 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
7571 gdb_assert (per_objfile
->sym_cu
== nullptr);
7572 scoped_restore restore_sym_cu
7573 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
7575 /* Decode line number information if present. We do this before
7576 processing child DIEs, so that the line header table is available
7577 for DW_AT_decl_file. */
7578 handle_DW_AT_stmt_list (die
, cu
, fnd
, unrel_low
, unrel_low
!= unrel_high
);
7580 /* Process all dies in compilation unit. */
7581 if (die
->child
!= NULL
)
7583 child_die
= die
->child
;
7584 while (child_die
&& child_die
->tag
)
7586 process_die (child_die
, cu
);
7587 child_die
= child_die
->sibling
;
7590 per_objfile
->sym_cu
= nullptr;
7592 /* Decode macro information, if present. Dwarf 2 macro information
7593 refers to information in the line number info statement program
7594 header, so we can only read it if we've read the header
7596 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
7598 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
7599 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
7601 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
7602 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7604 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
7608 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
7609 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
7611 unsigned int macro_offset
= attr
->as_unsigned ();
7613 dwarf_decode_macros (cu
, macro_offset
, 0);
7619 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
7621 struct type_unit_group
*tu_group
;
7623 struct attribute
*attr
;
7625 struct signatured_type
*sig_type
;
7627 gdb_assert (per_cu
->is_debug_types
);
7628 sig_type
= (struct signatured_type
*) per_cu
;
7630 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
7632 /* If we're using .gdb_index (includes -readnow) then
7633 per_cu->type_unit_group may not have been set up yet. */
7634 if (sig_type
->type_unit_group
== NULL
)
7635 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
7636 tu_group
= sig_type
->type_unit_group
;
7638 /* If we've already processed this stmt_list there's no real need to
7639 do it again, we could fake it and just recreate the part we need
7640 (file name,index -> symtab mapping). If data shows this optimization
7641 is useful we can do it then. */
7642 type_unit_group_unshareable
*tug_unshare
7643 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
7644 first_time
= tug_unshare
->compunit_symtab
== NULL
;
7646 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7649 if (attr
!= NULL
&& attr
->form_is_unsigned ())
7651 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
7652 lh
= dwarf_decode_line_header (line_offset
, this, nullptr);
7657 start_compunit_symtab ("", NULL
, 0);
7660 gdb_assert (tug_unshare
->symtabs
== NULL
);
7661 gdb_assert (m_builder
== nullptr);
7662 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
7663 m_builder
.reset (new struct buildsym_compunit
7664 (cust
->objfile (), "",
7668 list_in_scope
= get_builder ()->get_file_symbols ();
7673 line_header
= lh
.release ();
7674 line_header_die_owner
= die
;
7678 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
7680 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7681 still initializing it, and our caller (a few levels up)
7682 process_full_type_unit still needs to know if this is the first
7685 tug_unshare
->symtabs
7686 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
7687 struct symtab
*, line_header
->file_names_size ());
7689 auto &file_names
= line_header
->file_names ();
7690 for (i
= 0; i
< file_names
.size (); ++i
)
7692 file_entry
&fe
= file_names
[i
];
7693 dwarf2_start_subfile (this, fe
, *line_header
);
7694 buildsym_compunit
*b
= get_builder ();
7695 subfile
*sf
= b
->get_current_subfile ();
7697 if (sf
->symtab
== nullptr)
7699 /* NOTE: start_subfile will recognize when it's been
7700 passed a file it has already seen. So we can't
7701 assume there's a simple mapping from
7702 cu->line_header->file_names to subfiles, plus
7703 cu->line_header->file_names may contain dups. */
7704 const char *name
= sf
->name
.c_str ();
7705 const char *name_for_id
= sf
->name_for_id
.c_str ();
7706 sf
->symtab
= allocate_symtab (cust
, name
, name_for_id
);
7709 fe
.symtab
= b
->get_current_subfile ()->symtab
;
7710 tug_unshare
->symtabs
[i
] = fe
.symtab
;
7715 gdb_assert (m_builder
== nullptr);
7716 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
7717 m_builder
.reset (new struct buildsym_compunit
7718 (cust
->objfile (), "",
7722 list_in_scope
= get_builder ()->get_file_symbols ();
7724 auto &file_names
= line_header
->file_names ();
7725 for (i
= 0; i
< file_names
.size (); ++i
)
7727 file_entry
&fe
= file_names
[i
];
7728 fe
.symtab
= tug_unshare
->symtabs
[i
];
7732 /* The main symtab is allocated last. Type units don't have DW_AT_name
7733 so they don't have a "real" (so to speak) symtab anyway.
7734 There is later code that will assign the main symtab to all symbols
7735 that don't have one. We need to handle the case of a symbol with a
7736 missing symtab (DW_AT_decl_file) anyway. */
7739 /* Process DW_TAG_type_unit.
7740 For TUs we want to skip the first top level sibling if it's not the
7741 actual type being defined by this TU. In this case the first top
7742 level sibling is there to provide context only. */
7745 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
7747 struct die_info
*child_die
;
7749 prepare_one_comp_unit (cu
, die
, language_minimal
);
7751 /* Initialize (or reinitialize) the machinery for building symtabs.
7752 We do this before processing child DIEs, so that the line header table
7753 is available for DW_AT_decl_file. */
7754 cu
->setup_type_unit_groups (die
);
7756 if (die
->child
!= NULL
)
7758 child_die
= die
->child
;
7759 while (child_die
&& child_die
->tag
)
7761 process_die (child_die
, cu
);
7762 child_die
= child_die
->sibling
;
7769 http://gcc.gnu.org/wiki/DebugFission
7770 http://gcc.gnu.org/wiki/DebugFissionDWP
7772 To simplify handling of both DWO files ("object" files with the DWARF info)
7773 and DWP files (a file with the DWOs packaged up into one file), we treat
7774 DWP files as having a collection of virtual DWO files. */
7776 /* A helper function to hash two file names. This is a separate
7777 function because the hash table uses a search with a different
7778 type. The second file may be NULL. */
7781 hash_two_files (const char *one
, const char *two
)
7783 hashval_t hash
= htab_hash_string (one
);
7785 hash
+= htab_hash_string (two
);
7790 hash_dwo_file (const void *item
)
7792 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
7793 return hash_two_files (dwo_file
->dwo_name
.c_str (), dwo_file
->comp_dir
);
7796 /* This is used when looking up entries in the DWO hash table. */
7798 struct dwo_file_search
7800 /* Name of the DWO to look for. */
7801 const char *dwo_name
;
7802 /* Compilation directory to look for. */
7803 const char *comp_dir
;
7805 /* Return a hash value compatible with the table. */
7806 hashval_t
hash () const
7808 return hash_two_files (dwo_name
, comp_dir
);
7813 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
7815 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
7816 const struct dwo_file_search
*rhs
7817 = (const struct dwo_file_search
*) item_rhs
;
7819 if (lhs
->dwo_name
!= rhs
->dwo_name
)
7821 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
7822 return lhs
->comp_dir
== rhs
->comp_dir
;
7823 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
7826 /* Allocate a hash table for DWO files. */
7829 allocate_dwo_file_hash_table ()
7831 return htab_up (htab_create_alloc (41,
7834 htab_delete_entry
<dwo_file
>,
7838 /* Lookup DWO file DWO_NAME. */
7841 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
7842 const char *dwo_name
,
7843 const char *comp_dir
)
7845 struct dwo_file_search find_entry
;
7848 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
7849 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
7851 find_entry
.dwo_name
= dwo_name
;
7852 find_entry
.comp_dir
= comp_dir
;
7853 slot
= htab_find_slot_with_hash (per_objfile
->per_bfd
->dwo_files
.get (),
7854 &find_entry
, find_entry
.hash (),
7861 hash_dwo_unit (const void *item
)
7863 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
7865 /* This drops the top 32 bits of the id, but is ok for a hash. */
7866 return dwo_unit
->signature
;
7870 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
7872 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
7873 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
7875 /* The signature is assumed to be unique within the DWO file.
7876 So while object file CU dwo_id's always have the value zero,
7877 that's OK, assuming each object file DWO file has only one CU,
7878 and that's the rule for now. */
7879 return lhs
->signature
== rhs
->signature
;
7882 /* Allocate a hash table for DWO CUs,TUs.
7883 There is one of these tables for each of CUs,TUs for each DWO file. */
7886 allocate_dwo_unit_table ()
7888 /* Start out with a pretty small number.
7889 Generally DWO files contain only one CU and maybe some TUs. */
7890 return htab_up (htab_create_alloc (3,
7893 NULL
, xcalloc
, xfree
));
7896 /* die_reader_func for create_dwo_cu. */
7899 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
7900 const gdb_byte
*info_ptr
,
7901 struct die_info
*comp_unit_die
,
7902 struct dwo_file
*dwo_file
,
7903 struct dwo_unit
*dwo_unit
)
7905 struct dwarf2_cu
*cu
= reader
->cu
;
7906 sect_offset sect_off
= cu
->per_cu
->sect_off
;
7907 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
7909 std::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
7910 if (!signature
.has_value ())
7912 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7913 " its dwo_id [in module %s]"),
7914 sect_offset_str (sect_off
), dwo_file
->dwo_name
.c_str ());
7918 dwo_unit
->dwo_file
= dwo_file
;
7919 dwo_unit
->signature
= *signature
;
7920 dwo_unit
->section
= section
;
7921 dwo_unit
->sect_off
= sect_off
;
7922 dwo_unit
->length
= cu
->per_cu
->length ();
7924 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7925 sect_offset_str (sect_off
),
7926 hex_string (dwo_unit
->signature
));
7929 /* Create the dwo_units for the CUs in a DWO_FILE.
7930 Note: This function processes DWO files only, not DWP files. */
7933 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
7934 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
7935 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
7937 struct objfile
*objfile
= per_objfile
->objfile
;
7938 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7939 const gdb_byte
*info_ptr
, *end_ptr
;
7941 section
.read (objfile
);
7942 info_ptr
= section
.buffer
;
7944 if (info_ptr
== NULL
)
7947 dwarf_read_debug_printf ("Reading %s for %s:",
7948 section
.get_name (),
7949 section
.get_file_name ());
7951 end_ptr
= info_ptr
+ section
.size
;
7952 while (info_ptr
< end_ptr
)
7954 struct dwarf2_per_cu_data per_cu
;
7955 struct dwo_unit read_unit
{};
7956 struct dwo_unit
*dwo_unit
;
7958 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
7960 per_cu
.per_bfd
= per_bfd
;
7961 per_cu
.is_debug_types
= 0;
7962 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
7963 per_cu
.section
= §ion
;
7965 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
7966 if (!reader
.dummy_p
)
7967 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
7968 &dwo_file
, &read_unit
);
7969 info_ptr
+= per_cu
.length ();
7971 // If the unit could not be parsed, skip it.
7972 if (read_unit
.dwo_file
== NULL
)
7975 if (cus_htab
== NULL
)
7976 cus_htab
= allocate_dwo_unit_table ();
7978 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
7980 *dwo_unit
= read_unit
;
7981 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
7982 gdb_assert (slot
!= NULL
);
7985 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
7986 sect_offset dup_sect_off
= dup_cu
->sect_off
;
7988 complaint (_("debug cu entry at offset %s is duplicate to"
7989 " the entry at offset %s, signature %s"),
7990 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
7991 hex_string (dwo_unit
->signature
));
7993 *slot
= (void *)dwo_unit
;
7997 /* DWP file .debug_{cu,tu}_index section format:
7998 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
7999 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
8001 DWP Versions 1 & 2 are older, pre-standard format versions. The first
8002 officially standard DWP format was published with DWARF v5 and is called
8003 Version 5. There are no versions 3 or 4.
8007 Both index sections have the same format, and serve to map a 64-bit
8008 signature to a set of section numbers. Each section begins with a header,
8009 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8010 indexes, and a pool of 32-bit section numbers. The index sections will be
8011 aligned at 8-byte boundaries in the file.
8013 The index section header consists of:
8015 V, 32 bit version number
8017 N, 32 bit number of compilation units or type units in the index
8018 M, 32 bit number of slots in the hash table
8020 Numbers are recorded using the byte order of the application binary.
8022 The hash table begins at offset 16 in the section, and consists of an array
8023 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8024 order of the application binary). Unused slots in the hash table are 0.
8025 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8027 The parallel table begins immediately after the hash table
8028 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8029 array of 32-bit indexes (using the byte order of the application binary),
8030 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8031 table contains a 32-bit index into the pool of section numbers. For unused
8032 hash table slots, the corresponding entry in the parallel table will be 0.
8034 The pool of section numbers begins immediately following the hash table
8035 (at offset 16 + 12 * M from the beginning of the section). The pool of
8036 section numbers consists of an array of 32-bit words (using the byte order
8037 of the application binary). Each item in the array is indexed starting
8038 from 0. The hash table entry provides the index of the first section
8039 number in the set. Additional section numbers in the set follow, and the
8040 set is terminated by a 0 entry (section number 0 is not used in ELF).
8042 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8043 section must be the first entry in the set, and the .debug_abbrev.dwo must
8044 be the second entry. Other members of the set may follow in any order.
8048 DWP Versions 2 and 5:
8050 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
8051 and the entries in the index tables are now offsets into these sections.
8052 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
8055 Index Section Contents:
8057 Hash Table of Signatures dwp_hash_table.hash_table
8058 Parallel Table of Indices dwp_hash_table.unit_table
8059 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
8060 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
8062 The index section header consists of:
8064 V, 32 bit version number
8065 L, 32 bit number of columns in the table of section offsets
8066 N, 32 bit number of compilation units or type units in the index
8067 M, 32 bit number of slots in the hash table
8069 Numbers are recorded using the byte order of the application binary.
8071 The hash table has the same format as version 1.
8072 The parallel table of indices has the same format as version 1,
8073 except that the entries are origin-1 indices into the table of sections
8074 offsets and the table of section sizes.
8076 The table of offsets begins immediately following the parallel table
8077 (at offset 16 + 12 * M from the beginning of the section). The table is
8078 a two-dimensional array of 32-bit words (using the byte order of the
8079 application binary), with L columns and N+1 rows, in row-major order.
8080 Each row in the array is indexed starting from 0. The first row provides
8081 a key to the remaining rows: each column in this row provides an identifier
8082 for a debug section, and the offsets in the same column of subsequent rows
8083 refer to that section. The section identifiers for Version 2 are:
8085 DW_SECT_INFO 1 .debug_info.dwo
8086 DW_SECT_TYPES 2 .debug_types.dwo
8087 DW_SECT_ABBREV 3 .debug_abbrev.dwo
8088 DW_SECT_LINE 4 .debug_line.dwo
8089 DW_SECT_LOC 5 .debug_loc.dwo
8090 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
8091 DW_SECT_MACINFO 7 .debug_macinfo.dwo
8092 DW_SECT_MACRO 8 .debug_macro.dwo
8094 The section identifiers for Version 5 are:
8096 DW_SECT_INFO_V5 1 .debug_info.dwo
8097 DW_SECT_RESERVED_V5 2 --
8098 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
8099 DW_SECT_LINE_V5 4 .debug_line.dwo
8100 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
8101 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
8102 DW_SECT_MACRO_V5 7 .debug_macro.dwo
8103 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
8105 The offsets provided by the CU and TU index sections are the base offsets
8106 for the contributions made by each CU or TU to the corresponding section
8107 in the package file. Each CU and TU header contains an abbrev_offset
8108 field, used to find the abbreviations table for that CU or TU within the
8109 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
8110 be interpreted as relative to the base offset given in the index section.
8111 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
8112 should be interpreted as relative to the base offset for .debug_line.dwo,
8113 and offsets into other debug sections obtained from DWARF attributes should
8114 also be interpreted as relative to the corresponding base offset.
8116 The table of sizes begins immediately following the table of offsets.
8117 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8118 with L columns and N rows, in row-major order. Each row in the array is
8119 indexed starting from 1 (row 0 is shared by the two tables).
8123 Hash table lookup is handled the same in version 1 and 2:
8125 We assume that N and M will not exceed 2^32 - 1.
8126 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8128 Given a 64-bit compilation unit signature or a type signature S, an entry
8129 in the hash table is located as follows:
8131 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8132 the low-order k bits all set to 1.
8134 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8136 3) If the hash table entry at index H matches the signature, use that
8137 entry. If the hash table entry at index H is unused (all zeroes),
8138 terminate the search: the signature is not present in the table.
8140 4) Let H = (H + H') modulo M. Repeat at Step 3.
8142 Because M > N and H' and M are relatively prime, the search is guaranteed
8143 to stop at an unused slot or find the match. */
8145 /* Create a hash table to map DWO IDs to their CU/TU entry in
8146 .debug_{info,types}.dwo in DWP_FILE.
8147 Returns NULL if there isn't one.
8148 Note: This function processes DWP files only, not DWO files. */
8150 static struct dwp_hash_table
*
8151 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
8152 struct dwp_file
*dwp_file
, int is_debug_types
)
8154 struct objfile
*objfile
= per_objfile
->objfile
;
8155 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8156 const gdb_byte
*index_ptr
, *index_end
;
8157 struct dwarf2_section_info
*index
;
8158 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
8159 struct dwp_hash_table
*htab
;
8162 index
= &dwp_file
->sections
.tu_index
;
8164 index
= &dwp_file
->sections
.cu_index
;
8166 if (index
->empty ())
8168 index
->read (objfile
);
8170 index_ptr
= index
->buffer
;
8171 index_end
= index_ptr
+ index
->size
;
8173 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8174 For now it's safe to just read 4 bytes (particularly as it's difficult to
8175 tell if you're dealing with Version 5 before you've read the version). */
8176 version
= read_4_bytes (dbfd
, index_ptr
);
8178 if (version
== 2 || version
== 5)
8179 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
8183 nr_units
= read_4_bytes (dbfd
, index_ptr
);
8185 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
8188 if (version
!= 1 && version
!= 2 && version
!= 5)
8190 error (_("Dwarf Error: unsupported DWP file version (%s)"
8192 pulongest (version
), dwp_file
->name
);
8194 if (nr_slots
!= (nr_slots
& -nr_slots
))
8196 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8197 " is not power of 2 [in module %s]"),
8198 pulongest (nr_slots
), dwp_file
->name
);
8201 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
8202 htab
->version
= version
;
8203 htab
->nr_columns
= nr_columns
;
8204 htab
->nr_units
= nr_units
;
8205 htab
->nr_slots
= nr_slots
;
8206 htab
->hash_table
= index_ptr
;
8207 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
8209 /* Exit early if the table is empty. */
8210 if (nr_slots
== 0 || nr_units
== 0
8211 || (version
== 2 && nr_columns
== 0)
8212 || (version
== 5 && nr_columns
== 0))
8214 /* All must be zero. */
8215 if (nr_slots
!= 0 || nr_units
!= 0
8216 || (version
== 2 && nr_columns
!= 0)
8217 || (version
== 5 && nr_columns
!= 0))
8219 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8220 " all zero [in modules %s]"),
8228 htab
->section_pool
.v1
.indices
=
8229 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
8230 /* It's harder to decide whether the section is too small in v1.
8231 V1 is deprecated anyway so we punt. */
8233 else if (version
== 2)
8235 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
8236 int *ids
= htab
->section_pool
.v2
.section_ids
;
8237 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
8238 /* Reverse map for error checking. */
8239 int ids_seen
[DW_SECT_MAX
+ 1];
8244 error (_("Dwarf Error: bad DWP hash table, too few columns"
8245 " in section table [in module %s]"),
8248 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
8250 error (_("Dwarf Error: bad DWP hash table, too many columns"
8251 " in section table [in module %s]"),
8254 memset (ids
, 255, sizeof_ids
);
8255 memset (ids_seen
, 255, sizeof (ids_seen
));
8256 for (i
= 0; i
< nr_columns
; ++i
)
8258 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
8260 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
8262 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8263 " in section table [in module %s]"),
8264 id
, dwp_file
->name
);
8266 if (ids_seen
[id
] != -1)
8268 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8269 " id %d in section table [in module %s]"),
8270 id
, dwp_file
->name
);
8275 /* Must have exactly one info or types section. */
8276 if (((ids_seen
[DW_SECT_INFO
] != -1)
8277 + (ids_seen
[DW_SECT_TYPES
] != -1))
8280 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8281 " DWO info/types section [in module %s]"),
8284 /* Must have an abbrev section. */
8285 if (ids_seen
[DW_SECT_ABBREV
] == -1)
8287 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8288 " section [in module %s]"),
8291 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
8292 htab
->section_pool
.v2
.sizes
=
8293 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
8294 * nr_units
* nr_columns
);
8295 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
8296 * nr_units
* nr_columns
))
8299 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8304 else /* version == 5 */
8306 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
8307 int *ids
= htab
->section_pool
.v5
.section_ids
;
8308 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
8309 /* Reverse map for error checking. */
8310 int ids_seen
[DW_SECT_MAX_V5
+ 1];
8314 error (_("Dwarf Error: bad DWP hash table, too few columns"
8315 " in section table [in module %s]"),
8318 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
8320 error (_("Dwarf Error: bad DWP hash table, too many columns"
8321 " in section table [in module %s]"),
8324 memset (ids
, 255, sizeof_ids
);
8325 memset (ids_seen
, 255, sizeof (ids_seen
));
8326 for (int i
= 0; i
< nr_columns
; ++i
)
8328 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
8330 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
8332 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8333 " in section table [in module %s]"),
8334 id
, dwp_file
->name
);
8336 if (ids_seen
[id
] != -1)
8338 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8339 " id %d in section table [in module %s]"),
8340 id
, dwp_file
->name
);
8345 /* Must have seen an info section. */
8346 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
8348 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8349 " DWO info/types section [in module %s]"),
8352 /* Must have an abbrev section. */
8353 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
8355 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8356 " section [in module %s]"),
8359 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
8360 htab
->section_pool
.v5
.sizes
8361 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
8362 * nr_units
* nr_columns
);
8363 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
8364 * nr_units
* nr_columns
))
8367 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8376 /* Update SECTIONS with the data from SECTP.
8378 This function is like the other "locate" section routines, but in
8379 this context the sections to read comes from the DWP V1 hash table,
8380 not the full ELF section table.
8382 The result is non-zero for success, or zero if an error was found. */
8385 locate_v1_virtual_dwo_sections (asection
*sectp
,
8386 struct virtual_v1_dwo_sections
*sections
)
8388 const struct dwop_section_names
*names
= &dwop_section_names
;
8390 if (names
->abbrev_dwo
.matches (sectp
->name
))
8392 /* There can be only one. */
8393 if (sections
->abbrev
.s
.section
!= NULL
)
8395 sections
->abbrev
.s
.section
= sectp
;
8396 sections
->abbrev
.size
= bfd_section_size (sectp
);
8398 else if (names
->info_dwo
.matches (sectp
->name
)
8399 || names
->types_dwo
.matches (sectp
->name
))
8401 /* There can be only one. */
8402 if (sections
->info_or_types
.s
.section
!= NULL
)
8404 sections
->info_or_types
.s
.section
= sectp
;
8405 sections
->info_or_types
.size
= bfd_section_size (sectp
);
8407 else if (names
->line_dwo
.matches (sectp
->name
))
8409 /* There can be only one. */
8410 if (sections
->line
.s
.section
!= NULL
)
8412 sections
->line
.s
.section
= sectp
;
8413 sections
->line
.size
= bfd_section_size (sectp
);
8415 else if (names
->loc_dwo
.matches (sectp
->name
))
8417 /* There can be only one. */
8418 if (sections
->loc
.s
.section
!= NULL
)
8420 sections
->loc
.s
.section
= sectp
;
8421 sections
->loc
.size
= bfd_section_size (sectp
);
8423 else if (names
->macinfo_dwo
.matches (sectp
->name
))
8425 /* There can be only one. */
8426 if (sections
->macinfo
.s
.section
!= NULL
)
8428 sections
->macinfo
.s
.section
= sectp
;
8429 sections
->macinfo
.size
= bfd_section_size (sectp
);
8431 else if (names
->macro_dwo
.matches (sectp
->name
))
8433 /* There can be only one. */
8434 if (sections
->macro
.s
.section
!= NULL
)
8436 sections
->macro
.s
.section
= sectp
;
8437 sections
->macro
.size
= bfd_section_size (sectp
);
8439 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
8441 /* There can be only one. */
8442 if (sections
->str_offsets
.s
.section
!= NULL
)
8444 sections
->str_offsets
.s
.section
= sectp
;
8445 sections
->str_offsets
.size
= bfd_section_size (sectp
);
8449 /* No other kind of section is valid. */
8456 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8457 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8458 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8459 This is for DWP version 1 files. */
8461 static struct dwo_unit
*
8462 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
8463 struct dwp_file
*dwp_file
,
8464 uint32_t unit_index
,
8465 const char *comp_dir
,
8466 ULONGEST signature
, int is_debug_types
)
8468 const struct dwp_hash_table
*dwp_htab
=
8469 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8470 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8471 const char *kind
= is_debug_types
? "TU" : "CU";
8472 struct dwo_file
*dwo_file
;
8473 struct dwo_unit
*dwo_unit
;
8474 struct virtual_v1_dwo_sections sections
;
8475 void **dwo_file_slot
;
8478 gdb_assert (dwp_file
->version
== 1);
8480 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8481 kind
, pulongest (unit_index
), hex_string (signature
),
8484 /* Fetch the sections of this DWO unit.
8485 Put a limit on the number of sections we look for so that bad data
8486 doesn't cause us to loop forever. */
8488 #define MAX_NR_V1_DWO_SECTIONS \
8489 (1 /* .debug_info or .debug_types */ \
8490 + 1 /* .debug_abbrev */ \
8491 + 1 /* .debug_line */ \
8492 + 1 /* .debug_loc */ \
8493 + 1 /* .debug_str_offsets */ \
8494 + 1 /* .debug_macro or .debug_macinfo */ \
8495 + 1 /* trailing zero */)
8497 memset (§ions
, 0, sizeof (sections
));
8499 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
8502 uint32_t section_nr
=
8504 dwp_htab
->section_pool
.v1
.indices
8505 + (unit_index
+ i
) * sizeof (uint32_t));
8507 if (section_nr
== 0)
8509 if (section_nr
>= dwp_file
->num_sections
)
8511 error (_("Dwarf Error: bad DWP hash table, section number too large"
8516 sectp
= dwp_file
->elf_sections
[section_nr
];
8517 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
8519 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8526 || sections
.info_or_types
.empty ()
8527 || sections
.abbrev
.empty ())
8529 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8533 if (i
== MAX_NR_V1_DWO_SECTIONS
)
8535 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8540 /* It's easier for the rest of the code if we fake a struct dwo_file and
8541 have dwo_unit "live" in that. At least for now.
8543 The DWP file can be made up of a random collection of CUs and TUs.
8544 However, for each CU + set of TUs that came from the same original DWO
8545 file, we can combine them back into a virtual DWO file to save space
8546 (fewer struct dwo_file objects to allocate). Remember that for really
8547 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8549 std::string virtual_dwo_name
=
8550 string_printf ("virtual-dwo/%d-%d-%d-%d",
8551 sections
.abbrev
.get_id (),
8552 sections
.line
.get_id (),
8553 sections
.loc
.get_id (),
8554 sections
.str_offsets
.get_id ());
8555 /* Can we use an existing virtual DWO file? */
8556 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
8558 /* Create one if necessary. */
8559 if (*dwo_file_slot
== NULL
)
8561 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8562 virtual_dwo_name
.c_str ());
8564 dwo_file
= new struct dwo_file
;
8565 dwo_file
->dwo_name
= std::move (virtual_dwo_name
);
8566 dwo_file
->comp_dir
= comp_dir
;
8567 dwo_file
->sections
.abbrev
= sections
.abbrev
;
8568 dwo_file
->sections
.line
= sections
.line
;
8569 dwo_file
->sections
.loc
= sections
.loc
;
8570 dwo_file
->sections
.macinfo
= sections
.macinfo
;
8571 dwo_file
->sections
.macro
= sections
.macro
;
8572 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
8573 /* The "str" section is global to the entire DWP file. */
8574 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
8575 /* The info or types section is assigned below to dwo_unit,
8576 there's no need to record it in dwo_file.
8577 Also, we can't simply record type sections in dwo_file because
8578 we record a pointer into the vector in dwo_unit. As we collect more
8579 types we'll grow the vector and eventually have to reallocate space
8580 for it, invalidating all copies of pointers into the previous
8582 *dwo_file_slot
= dwo_file
;
8586 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8587 virtual_dwo_name
.c_str ());
8589 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
8592 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
8593 dwo_unit
->dwo_file
= dwo_file
;
8594 dwo_unit
->signature
= signature
;
8596 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
8597 *dwo_unit
->section
= sections
.info_or_types
;
8598 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8603 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8604 simplify them. Given a pointer to the containing section SECTION, and
8605 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8606 virtual section of just that piece. */
8608 static struct dwarf2_section_info
8609 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
8610 struct dwarf2_section_info
*section
,
8611 bfd_size_type offset
, bfd_size_type size
)
8613 struct dwarf2_section_info result
;
8616 gdb_assert (section
!= NULL
);
8617 gdb_assert (!section
->is_virtual
);
8619 memset (&result
, 0, sizeof (result
));
8620 result
.s
.containing_section
= section
;
8621 result
.is_virtual
= true;
8626 sectp
= section
->get_bfd_section ();
8628 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8629 bounds of the real section. This is a pretty-rare event, so just
8630 flag an error (easier) instead of a warning and trying to cope. */
8632 || offset
+ size
> bfd_section_size (sectp
))
8634 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8635 " in section %s [in module %s]"),
8636 sectp
? bfd_section_name (sectp
) : "<unknown>",
8637 objfile_name (per_objfile
->objfile
));
8640 result
.virtual_offset
= offset
;
8645 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8646 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8647 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8648 This is for DWP version 2 files. */
8650 static struct dwo_unit
*
8651 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
8652 struct dwp_file
*dwp_file
,
8653 uint32_t unit_index
,
8654 const char *comp_dir
,
8655 ULONGEST signature
, int is_debug_types
)
8657 const struct dwp_hash_table
*dwp_htab
=
8658 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8659 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8660 const char *kind
= is_debug_types
? "TU" : "CU";
8661 struct dwo_file
*dwo_file
;
8662 struct dwo_unit
*dwo_unit
;
8663 struct virtual_v2_or_v5_dwo_sections sections
;
8664 void **dwo_file_slot
;
8667 gdb_assert (dwp_file
->version
== 2);
8669 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8670 kind
, pulongest (unit_index
), hex_string (signature
),
8673 /* Fetch the section offsets of this DWO unit. */
8675 memset (§ions
, 0, sizeof (sections
));
8677 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
8679 uint32_t offset
= read_4_bytes (dbfd
,
8680 dwp_htab
->section_pool
.v2
.offsets
8681 + (((unit_index
- 1) * dwp_htab
->nr_columns
8683 * sizeof (uint32_t)));
8684 uint32_t size
= read_4_bytes (dbfd
,
8685 dwp_htab
->section_pool
.v2
.sizes
8686 + (((unit_index
- 1) * dwp_htab
->nr_columns
8688 * sizeof (uint32_t)));
8690 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
8694 sections
.info_or_types_offset
= offset
;
8695 sections
.info_or_types_size
= size
;
8697 case DW_SECT_ABBREV
:
8698 sections
.abbrev_offset
= offset
;
8699 sections
.abbrev_size
= size
;
8702 sections
.line_offset
= offset
;
8703 sections
.line_size
= size
;
8706 sections
.loc_offset
= offset
;
8707 sections
.loc_size
= size
;
8709 case DW_SECT_STR_OFFSETS
:
8710 sections
.str_offsets_offset
= offset
;
8711 sections
.str_offsets_size
= size
;
8713 case DW_SECT_MACINFO
:
8714 sections
.macinfo_offset
= offset
;
8715 sections
.macinfo_size
= size
;
8718 sections
.macro_offset
= offset
;
8719 sections
.macro_size
= size
;
8724 /* It's easier for the rest of the code if we fake a struct dwo_file and
8725 have dwo_unit "live" in that. At least for now.
8727 The DWP file can be made up of a random collection of CUs and TUs.
8728 However, for each CU + set of TUs that came from the same original DWO
8729 file, we can combine them back into a virtual DWO file to save space
8730 (fewer struct dwo_file objects to allocate). Remember that for really
8731 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8733 std::string virtual_dwo_name
=
8734 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8735 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
8736 (long) (sections
.line_size
? sections
.line_offset
: 0),
8737 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
8738 (long) (sections
.str_offsets_size
8739 ? sections
.str_offsets_offset
: 0));
8740 /* Can we use an existing virtual DWO file? */
8741 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
8743 /* Create one if necessary. */
8744 if (*dwo_file_slot
== NULL
)
8746 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8747 virtual_dwo_name
.c_str ());
8749 dwo_file
= new struct dwo_file
;
8750 dwo_file
->dwo_name
= std::move (virtual_dwo_name
);
8751 dwo_file
->comp_dir
= comp_dir
;
8752 dwo_file
->sections
.abbrev
=
8753 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
8754 sections
.abbrev_offset
,
8755 sections
.abbrev_size
);
8756 dwo_file
->sections
.line
=
8757 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
8758 sections
.line_offset
,
8759 sections
.line_size
);
8760 dwo_file
->sections
.loc
=
8761 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
8762 sections
.loc_offset
, sections
.loc_size
);
8763 dwo_file
->sections
.macinfo
=
8764 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
8765 sections
.macinfo_offset
,
8766 sections
.macinfo_size
);
8767 dwo_file
->sections
.macro
=
8768 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
8769 sections
.macro_offset
,
8770 sections
.macro_size
);
8771 dwo_file
->sections
.str_offsets
=
8772 create_dwp_v2_or_v5_section (per_objfile
,
8773 &dwp_file
->sections
.str_offsets
,
8774 sections
.str_offsets_offset
,
8775 sections
.str_offsets_size
);
8776 /* The "str" section is global to the entire DWP file. */
8777 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
8778 /* The info or types section is assigned below to dwo_unit,
8779 there's no need to record it in dwo_file.
8780 Also, we can't simply record type sections in dwo_file because
8781 we record a pointer into the vector in dwo_unit. As we collect more
8782 types we'll grow the vector and eventually have to reallocate space
8783 for it, invalidating all copies of pointers into the previous
8785 *dwo_file_slot
= dwo_file
;
8789 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8790 virtual_dwo_name
.c_str ());
8792 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
8795 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
8796 dwo_unit
->dwo_file
= dwo_file
;
8797 dwo_unit
->signature
= signature
;
8799 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
8800 *dwo_unit
->section
= create_dwp_v2_or_v5_section
8803 ? &dwp_file
->sections
.types
8804 : &dwp_file
->sections
.info
,
8805 sections
.info_or_types_offset
,
8806 sections
.info_or_types_size
);
8807 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8812 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8813 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8814 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8815 This is for DWP version 5 files. */
8817 static struct dwo_unit
*
8818 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
8819 struct dwp_file
*dwp_file
,
8820 uint32_t unit_index
,
8821 const char *comp_dir
,
8822 ULONGEST signature
, int is_debug_types
)
8824 const struct dwp_hash_table
*dwp_htab
8825 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8826 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8827 const char *kind
= is_debug_types
? "TU" : "CU";
8828 struct dwo_file
*dwo_file
;
8829 struct dwo_unit
*dwo_unit
;
8830 struct virtual_v2_or_v5_dwo_sections sections
{};
8831 void **dwo_file_slot
;
8833 gdb_assert (dwp_file
->version
== 5);
8835 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8836 kind
, pulongest (unit_index
), hex_string (signature
),
8839 /* Fetch the section offsets of this DWO unit. */
8841 /* memset (§ions, 0, sizeof (sections)); */
8843 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
8845 uint32_t offset
= read_4_bytes (dbfd
,
8846 dwp_htab
->section_pool
.v5
.offsets
8847 + (((unit_index
- 1)
8848 * dwp_htab
->nr_columns
8850 * sizeof (uint32_t)));
8851 uint32_t size
= read_4_bytes (dbfd
,
8852 dwp_htab
->section_pool
.v5
.sizes
8853 + (((unit_index
- 1) * dwp_htab
->nr_columns
8855 * sizeof (uint32_t)));
8857 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
8859 case DW_SECT_ABBREV_V5
:
8860 sections
.abbrev_offset
= offset
;
8861 sections
.abbrev_size
= size
;
8863 case DW_SECT_INFO_V5
:
8864 sections
.info_or_types_offset
= offset
;
8865 sections
.info_or_types_size
= size
;
8867 case DW_SECT_LINE_V5
:
8868 sections
.line_offset
= offset
;
8869 sections
.line_size
= size
;
8871 case DW_SECT_LOCLISTS_V5
:
8872 sections
.loclists_offset
= offset
;
8873 sections
.loclists_size
= size
;
8875 case DW_SECT_MACRO_V5
:
8876 sections
.macro_offset
= offset
;
8877 sections
.macro_size
= size
;
8879 case DW_SECT_RNGLISTS_V5
:
8880 sections
.rnglists_offset
= offset
;
8881 sections
.rnglists_size
= size
;
8883 case DW_SECT_STR_OFFSETS_V5
:
8884 sections
.str_offsets_offset
= offset
;
8885 sections
.str_offsets_size
= size
;
8887 case DW_SECT_RESERVED_V5
:
8893 /* It's easier for the rest of the code if we fake a struct dwo_file and
8894 have dwo_unit "live" in that. At least for now.
8896 The DWP file can be made up of a random collection of CUs and TUs.
8897 However, for each CU + set of TUs that came from the same original DWO
8898 file, we can combine them back into a virtual DWO file to save space
8899 (fewer struct dwo_file objects to allocate). Remember that for really
8900 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8902 std::string virtual_dwo_name
=
8903 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8904 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
8905 (long) (sections
.line_size
? sections
.line_offset
: 0),
8906 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
8907 (long) (sections
.str_offsets_size
8908 ? sections
.str_offsets_offset
: 0),
8909 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
8910 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
8911 /* Can we use an existing virtual DWO file? */
8912 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
8913 virtual_dwo_name
.c_str (),
8915 /* Create one if necessary. */
8916 if (*dwo_file_slot
== NULL
)
8918 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8919 virtual_dwo_name
.c_str ());
8921 dwo_file
= new struct dwo_file
;
8922 dwo_file
->dwo_name
= std::move (virtual_dwo_name
);
8923 dwo_file
->comp_dir
= comp_dir
;
8924 dwo_file
->sections
.abbrev
=
8925 create_dwp_v2_or_v5_section (per_objfile
,
8926 &dwp_file
->sections
.abbrev
,
8927 sections
.abbrev_offset
,
8928 sections
.abbrev_size
);
8929 dwo_file
->sections
.line
=
8930 create_dwp_v2_or_v5_section (per_objfile
,
8931 &dwp_file
->sections
.line
,
8932 sections
.line_offset
, sections
.line_size
);
8933 dwo_file
->sections
.macro
=
8934 create_dwp_v2_or_v5_section (per_objfile
,
8935 &dwp_file
->sections
.macro
,
8936 sections
.macro_offset
,
8937 sections
.macro_size
);
8938 dwo_file
->sections
.loclists
=
8939 create_dwp_v2_or_v5_section (per_objfile
,
8940 &dwp_file
->sections
.loclists
,
8941 sections
.loclists_offset
,
8942 sections
.loclists_size
);
8943 dwo_file
->sections
.rnglists
=
8944 create_dwp_v2_or_v5_section (per_objfile
,
8945 &dwp_file
->sections
.rnglists
,
8946 sections
.rnglists_offset
,
8947 sections
.rnglists_size
);
8948 dwo_file
->sections
.str_offsets
=
8949 create_dwp_v2_or_v5_section (per_objfile
,
8950 &dwp_file
->sections
.str_offsets
,
8951 sections
.str_offsets_offset
,
8952 sections
.str_offsets_size
);
8953 /* The "str" section is global to the entire DWP file. */
8954 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
8955 /* The info or types section is assigned below to dwo_unit,
8956 there's no need to record it in dwo_file.
8957 Also, we can't simply record type sections in dwo_file because
8958 we record a pointer into the vector in dwo_unit. As we collect more
8959 types we'll grow the vector and eventually have to reallocate space
8960 for it, invalidating all copies of pointers into the previous
8962 *dwo_file_slot
= dwo_file
;
8966 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8967 virtual_dwo_name
.c_str ());
8969 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
8972 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
8973 dwo_unit
->dwo_file
= dwo_file
;
8974 dwo_unit
->signature
= signature
;
8976 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
8977 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
8978 &dwp_file
->sections
.info
,
8979 sections
.info_or_types_offset
,
8980 sections
.info_or_types_size
);
8981 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8986 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
8987 Returns NULL if the signature isn't found. */
8989 static struct dwo_unit
*
8990 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
8991 struct dwp_file
*dwp_file
, const char *comp_dir
,
8992 ULONGEST signature
, int is_debug_types
)
8994 const struct dwp_hash_table
*dwp_htab
=
8995 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8996 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8997 uint32_t mask
= dwp_htab
->nr_slots
- 1;
8998 uint32_t hash
= signature
& mask
;
8999 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
9002 struct dwo_unit find_dwo_cu
;
9004 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
9005 find_dwo_cu
.signature
= signature
;
9006 slot
= htab_find_slot (is_debug_types
9007 ? dwp_file
->loaded_tus
.get ()
9008 : dwp_file
->loaded_cus
.get (),
9009 &find_dwo_cu
, INSERT
);
9012 return (struct dwo_unit
*) *slot
;
9014 /* Use a for loop so that we don't loop forever on bad debug info. */
9015 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
9017 ULONGEST signature_in_table
;
9019 signature_in_table
=
9020 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
9021 if (signature_in_table
== signature
)
9023 uint32_t unit_index
=
9025 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
9027 if (dwp_file
->version
== 1)
9029 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
9030 unit_index
, comp_dir
,
9031 signature
, is_debug_types
);
9033 else if (dwp_file
->version
== 2)
9035 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
9036 unit_index
, comp_dir
,
9037 signature
, is_debug_types
);
9039 else /* version == 5 */
9041 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
9042 unit_index
, comp_dir
,
9043 signature
, is_debug_types
);
9045 return (struct dwo_unit
*) *slot
;
9047 if (signature_in_table
== 0)
9049 hash
= (hash
+ hash2
) & mask
;
9052 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9057 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9058 Open the file specified by FILE_NAME and hand it off to BFD for
9059 preliminary analysis. Return a newly initialized bfd *, which
9060 includes a canonicalized copy of FILE_NAME.
9061 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9062 SEARCH_CWD is true if the current directory is to be searched.
9063 It will be searched before debug-file-directory.
9064 If successful, the file is added to the bfd include table of the
9065 objfile's bfd (see gdb_bfd_record_inclusion).
9066 If unable to find/open the file, return NULL.
9067 NOTE: This function is derived from symfile_bfd_open. */
9069 static gdb_bfd_ref_ptr
9070 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
9071 const char *file_name
, int is_dwp
, int search_cwd
)
9074 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9075 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9076 to debug_file_directory. */
9077 const char *search_path
;
9078 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
9080 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
9083 if (!debug_file_directory
.empty ())
9085 search_path_holder
.reset (concat (".", dirname_separator_string
,
9086 debug_file_directory
.c_str (),
9088 search_path
= search_path_holder
.get ();
9094 search_path
= debug_file_directory
.c_str ();
9096 /* Add the path for the executable binary to the list of search paths. */
9097 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
9098 search_path_holder
.reset (concat (objfile_dir
.c_str (),
9099 dirname_separator_string
,
9100 search_path
, nullptr));
9101 search_path
= search_path_holder
.get ();
9103 openp_flags flags
= OPF_RETURN_REALPATH
;
9105 flags
|= OPF_SEARCH_IN_PATH
;
9107 gdb::unique_xmalloc_ptr
<char> absolute_name
;
9108 desc
= openp (search_path
, flags
, file_name
,
9109 O_RDONLY
| O_BINARY
, &absolute_name
);
9113 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
9115 if (sym_bfd
== NULL
)
9118 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
9121 /* Success. Record the bfd as having been included by the objfile's bfd.
9122 This is important because things like demangled_names_hash lives in the
9123 objfile's per_bfd space and may have references to things like symbol
9124 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9125 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
.get (), sym_bfd
.get ());
9130 /* Try to open DWO file FILE_NAME.
9131 COMP_DIR is the DW_AT_comp_dir attribute.
9132 The result is the bfd handle of the file.
9133 If there is a problem finding or opening the file, return NULL.
9134 Upon success, the canonicalized path of the file is stored in the bfd,
9135 same as symfile_bfd_open. */
9137 static gdb_bfd_ref_ptr
9138 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
9139 const char *file_name
, const char *comp_dir
)
9141 if (IS_ABSOLUTE_PATH (file_name
))
9142 return try_open_dwop_file (per_objfile
, file_name
,
9143 0 /*is_dwp*/, 0 /*search_cwd*/);
9145 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9147 if (comp_dir
!= NULL
)
9149 std::string path_to_try
= path_join (comp_dir
, file_name
);
9151 /* NOTE: If comp_dir is a relative path, this will also try the
9152 search path, which seems useful. */
9153 gdb_bfd_ref_ptr
abfd (try_open_dwop_file
9154 (per_objfile
, path_to_try
.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9160 /* That didn't work, try debug-file-directory, which, despite its name,
9161 is a list of paths. */
9163 if (debug_file_directory
.empty ())
9166 return try_open_dwop_file (per_objfile
, file_name
,
9167 0 /*is_dwp*/, 1 /*search_cwd*/);
9170 /* This function is mapped across the sections and remembers the offset and
9171 size of each of the DWO debugging sections we are interested in. */
9174 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
9175 asection
*sectp
, dwo_sections
*dwo_sections
)
9177 const struct dwop_section_names
*names
= &dwop_section_names
;
9179 struct dwarf2_section_info
*dw_sect
= nullptr;
9181 if (names
->abbrev_dwo
.matches (sectp
->name
))
9182 dw_sect
= &dwo_sections
->abbrev
;
9183 else if (names
->info_dwo
.matches (sectp
->name
))
9184 dw_sect
= &dwo_sections
->info
;
9185 else if (names
->line_dwo
.matches (sectp
->name
))
9186 dw_sect
= &dwo_sections
->line
;
9187 else if (names
->loc_dwo
.matches (sectp
->name
))
9188 dw_sect
= &dwo_sections
->loc
;
9189 else if (names
->loclists_dwo
.matches (sectp
->name
))
9190 dw_sect
= &dwo_sections
->loclists
;
9191 else if (names
->macinfo_dwo
.matches (sectp
->name
))
9192 dw_sect
= &dwo_sections
->macinfo
;
9193 else if (names
->macro_dwo
.matches (sectp
->name
))
9194 dw_sect
= &dwo_sections
->macro
;
9195 else if (names
->rnglists_dwo
.matches (sectp
->name
))
9196 dw_sect
= &dwo_sections
->rnglists
;
9197 else if (names
->str_dwo
.matches (sectp
->name
))
9198 dw_sect
= &dwo_sections
->str
;
9199 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
9200 dw_sect
= &dwo_sections
->str_offsets
;
9201 else if (names
->types_dwo
.matches (sectp
->name
))
9203 struct dwarf2_section_info type_section
;
9205 memset (&type_section
, 0, sizeof (type_section
));
9206 dwo_sections
->types
.push_back (type_section
);
9207 dw_sect
= &dwo_sections
->types
.back ();
9210 if (dw_sect
!= nullptr)
9212 dw_sect
->s
.section
= sectp
;
9213 dw_sect
->size
= bfd_section_size (sectp
);
9214 dw_sect
->read (objfile
);
9218 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9219 by PER_CU. This is for the non-DWP case.
9220 The result is NULL if DWO_NAME can't be found. */
9222 static struct dwo_file
*
9223 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
9224 const char *comp_dir
)
9226 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9228 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
9231 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
9236 dwo_file_up
dwo_file (new struct dwo_file
);
9237 dwo_file
->dwo_name
= dwo_name
;
9238 dwo_file
->comp_dir
= comp_dir
;
9239 dwo_file
->dbfd
= std::move (dbfd
);
9241 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
9242 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
9243 sec
, &dwo_file
->sections
);
9245 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
9248 if (cu
->per_cu
->version () < 5)
9250 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
9251 dwo_file
->sections
.types
, dwo_file
->tus
);
9255 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
9256 &dwo_file
->sections
.info
, dwo_file
->tus
,
9257 rcuh_kind::COMPILE
);
9260 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
9262 bfd_cache_close (dwo_file
->dbfd
.get ());
9264 return dwo_file
.release ();
9267 /* This function is mapped across the sections and remembers the offset and
9268 size of each of the DWP debugging sections common to version 1 and 2 that
9269 we are interested in. */
9272 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
9273 asection
*sectp
, dwp_file
*dwp_file
)
9275 const struct dwop_section_names
*names
= &dwop_section_names
;
9276 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
9278 /* Record the ELF section number for later lookup: this is what the
9279 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9280 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
9281 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
9283 /* Look for specific sections that we need. */
9284 struct dwarf2_section_info
*dw_sect
= nullptr;
9285 if (names
->str_dwo
.matches (sectp
->name
))
9286 dw_sect
= &dwp_file
->sections
.str
;
9287 else if (names
->cu_index
.matches (sectp
->name
))
9288 dw_sect
= &dwp_file
->sections
.cu_index
;
9289 else if (names
->tu_index
.matches (sectp
->name
))
9290 dw_sect
= &dwp_file
->sections
.tu_index
;
9292 if (dw_sect
!= nullptr)
9294 dw_sect
->s
.section
= sectp
;
9295 dw_sect
->size
= bfd_section_size (sectp
);
9296 dw_sect
->read (objfile
);
9300 /* This function is mapped across the sections and remembers the offset and
9301 size of each of the DWP version 2 debugging sections that we are interested
9302 in. This is split into a separate function because we don't know if we
9303 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9306 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
9307 asection
*sectp
, void *dwp_file_ptr
)
9309 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
9310 const struct dwop_section_names
*names
= &dwop_section_names
;
9311 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
9313 /* Record the ELF section number for later lookup: this is what the
9314 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9315 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
9316 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
9318 /* Look for specific sections that we need. */
9319 struct dwarf2_section_info
*dw_sect
= nullptr;
9320 if (names
->abbrev_dwo
.matches (sectp
->name
))
9321 dw_sect
= &dwp_file
->sections
.abbrev
;
9322 else if (names
->info_dwo
.matches (sectp
->name
))
9323 dw_sect
= &dwp_file
->sections
.info
;
9324 else if (names
->line_dwo
.matches (sectp
->name
))
9325 dw_sect
= &dwp_file
->sections
.line
;
9326 else if (names
->loc_dwo
.matches (sectp
->name
))
9327 dw_sect
= &dwp_file
->sections
.loc
;
9328 else if (names
->macinfo_dwo
.matches (sectp
->name
))
9329 dw_sect
= &dwp_file
->sections
.macinfo
;
9330 else if (names
->macro_dwo
.matches (sectp
->name
))
9331 dw_sect
= &dwp_file
->sections
.macro
;
9332 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
9333 dw_sect
= &dwp_file
->sections
.str_offsets
;
9334 else if (names
->types_dwo
.matches (sectp
->name
))
9335 dw_sect
= &dwp_file
->sections
.types
;
9337 if (dw_sect
!= nullptr)
9339 dw_sect
->s
.section
= sectp
;
9340 dw_sect
->size
= bfd_section_size (sectp
);
9341 dw_sect
->read (objfile
);
9345 /* This function is mapped across the sections and remembers the offset and
9346 size of each of the DWP version 5 debugging sections that we are interested
9347 in. This is split into a separate function because we don't know if we
9348 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9351 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
9352 asection
*sectp
, void *dwp_file_ptr
)
9354 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
9355 const struct dwop_section_names
*names
= &dwop_section_names
;
9356 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
9358 /* Record the ELF section number for later lookup: this is what the
9359 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9360 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
9361 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
9363 /* Look for specific sections that we need. */
9364 struct dwarf2_section_info
*dw_sect
= nullptr;
9365 if (names
->abbrev_dwo
.matches (sectp
->name
))
9366 dw_sect
= &dwp_file
->sections
.abbrev
;
9367 else if (names
->info_dwo
.matches (sectp
->name
))
9368 dw_sect
= &dwp_file
->sections
.info
;
9369 else if (names
->line_dwo
.matches (sectp
->name
))
9370 dw_sect
= &dwp_file
->sections
.line
;
9371 else if (names
->loclists_dwo
.matches (sectp
->name
))
9372 dw_sect
= &dwp_file
->sections
.loclists
;
9373 else if (names
->macro_dwo
.matches (sectp
->name
))
9374 dw_sect
= &dwp_file
->sections
.macro
;
9375 else if (names
->rnglists_dwo
.matches (sectp
->name
))
9376 dw_sect
= &dwp_file
->sections
.rnglists
;
9377 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
9378 dw_sect
= &dwp_file
->sections
.str_offsets
;
9380 if (dw_sect
!= nullptr)
9382 dw_sect
->s
.section
= sectp
;
9383 dw_sect
->size
= bfd_section_size (sectp
);
9384 dw_sect
->read (objfile
);
9388 /* Hash function for dwp_file loaded CUs/TUs. */
9391 hash_dwp_loaded_cutus (const void *item
)
9393 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9395 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9396 return dwo_unit
->signature
;
9399 /* Equality function for dwp_file loaded CUs/TUs. */
9402 eq_dwp_loaded_cutus (const void *a
, const void *b
)
9404 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
9405 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
9407 return dua
->signature
== dub
->signature
;
9410 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9413 allocate_dwp_loaded_cutus_table ()
9415 return htab_up (htab_create_alloc (3,
9416 hash_dwp_loaded_cutus
,
9417 eq_dwp_loaded_cutus
,
9418 NULL
, xcalloc
, xfree
));
9421 /* Try to open DWP file FILE_NAME.
9422 The result is the bfd handle of the file.
9423 If there is a problem finding or opening the file, return NULL.
9424 Upon success, the canonicalized path of the file is stored in the bfd,
9425 same as symfile_bfd_open. */
9427 static gdb_bfd_ref_ptr
9428 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
9430 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
9436 /* Work around upstream bug 15652.
9437 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9438 [Whether that's a "bug" is debatable, but it is getting in our way.]
9439 We have no real idea where the dwp file is, because gdb's realpath-ing
9440 of the executable's path may have discarded the needed info.
9441 [IWBN if the dwp file name was recorded in the executable, akin to
9442 .gnu_debuglink, but that doesn't exist yet.]
9443 Strip the directory from FILE_NAME and search again. */
9444 if (!debug_file_directory
.empty ())
9446 /* Don't implicitly search the current directory here.
9447 If the user wants to search "." to handle this case,
9448 it must be added to debug-file-directory. */
9449 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
9457 /* Initialize the use of the DWP file for the current objfile.
9458 By convention the name of the DWP file is ${objfile}.dwp.
9459 The result is NULL if it can't be found. */
9461 static std::unique_ptr
<struct dwp_file
>
9462 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
9464 struct objfile
*objfile
= per_objfile
->objfile
;
9466 /* Try to find first .dwp for the binary file before any symbolic links
9469 /* If the objfile is a debug file, find the name of the real binary
9470 file and get the name of dwp file from there. */
9471 std::string dwp_name
;
9472 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
9474 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
9475 const char *backlink_basename
= lbasename (backlink
->original_name
);
9477 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
9480 dwp_name
= objfile
->original_name
;
9484 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
9486 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
9488 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9489 dwp_name
= objfile_name (objfile
);
9491 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
9496 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
9498 return std::unique_ptr
<dwp_file
> ();
9501 const char *name
= bfd_get_filename (dbfd
.get ());
9502 std::unique_ptr
<struct dwp_file
> dwp_file
9503 (new struct dwp_file (name
, std::move (dbfd
)));
9505 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
9506 dwp_file
->elf_sections
=
9507 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
9508 dwp_file
->num_sections
, asection
*);
9510 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
9511 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
9514 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
9516 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
9518 /* The DWP file version is stored in the hash table. Oh well. */
9519 if (dwp_file
->cus
&& dwp_file
->tus
9520 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
9522 /* Technically speaking, we should try to limp along, but this is
9523 pretty bizarre. We use pulongest here because that's the established
9524 portability solution (e.g, we cannot use %u for uint32_t). */
9525 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9526 " TU version %s [in DWP file %s]"),
9527 pulongest (dwp_file
->cus
->version
),
9528 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
9532 dwp_file
->version
= dwp_file
->cus
->version
;
9533 else if (dwp_file
->tus
)
9534 dwp_file
->version
= dwp_file
->tus
->version
;
9536 dwp_file
->version
= 2;
9538 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
9540 if (dwp_file
->version
== 2)
9541 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
9544 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
9548 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
9549 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
9551 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
9552 dwarf_read_debug_printf (" %s CUs, %s TUs",
9553 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
9554 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
9556 bfd_cache_close (dwp_file
->dbfd
.get ());
9561 /* Wrapper around open_and_init_dwp_file, only open it once. */
9563 static struct dwp_file
*
9564 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
9566 if (!per_objfile
->per_bfd
->dwp_checked
)
9568 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
9569 per_objfile
->per_bfd
->dwp_checked
= 1;
9571 return per_objfile
->per_bfd
->dwp_file
.get ();
9574 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9575 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9576 or in the DWP file for the objfile, referenced by THIS_UNIT.
9577 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9578 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9580 This is called, for example, when wanting to read a variable with a
9581 complex location. Therefore we don't want to do file i/o for every call.
9582 Therefore we don't want to look for a DWO file on every call.
9583 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9584 then we check if we've already seen DWO_NAME, and only THEN do we check
9587 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9588 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9590 static struct dwo_unit
*
9591 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
9592 ULONGEST signature
, int is_debug_types
)
9594 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9595 struct objfile
*objfile
= per_objfile
->objfile
;
9596 const char *kind
= is_debug_types
? "TU" : "CU";
9597 void **dwo_file_slot
;
9598 struct dwo_file
*dwo_file
;
9599 struct dwp_file
*dwp_file
;
9601 /* First see if there's a DWP file.
9602 If we have a DWP file but didn't find the DWO inside it, don't
9603 look for the original DWO file. It makes gdb behave differently
9604 depending on whether one is debugging in the build tree. */
9606 dwp_file
= get_dwp_file (per_objfile
);
9607 if (dwp_file
!= NULL
)
9609 const struct dwp_hash_table
*dwp_htab
=
9610 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
9612 if (dwp_htab
!= NULL
)
9614 struct dwo_unit
*dwo_cutu
=
9615 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
9618 if (dwo_cutu
!= NULL
)
9620 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9621 kind
, hex_string (signature
),
9622 host_address_to_string (dwo_cutu
));
9630 /* No DWP file, look for the DWO file. */
9632 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
9633 if (*dwo_file_slot
== NULL
)
9635 /* Read in the file and build a table of the CUs/TUs it contains. */
9636 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
9638 /* NOTE: This will be NULL if unable to open the file. */
9639 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
9641 if (dwo_file
!= NULL
)
9643 struct dwo_unit
*dwo_cutu
= NULL
;
9645 if (is_debug_types
&& dwo_file
->tus
)
9647 struct dwo_unit find_dwo_cutu
;
9649 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
9650 find_dwo_cutu
.signature
= signature
;
9652 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
9655 else if (!is_debug_types
&& dwo_file
->cus
)
9657 struct dwo_unit find_dwo_cutu
;
9659 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
9660 find_dwo_cutu
.signature
= signature
;
9661 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
9665 if (dwo_cutu
!= NULL
)
9667 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9668 kind
, dwo_name
, hex_string (signature
),
9669 host_address_to_string (dwo_cutu
));
9676 /* We didn't find it. This could mean a dwo_id mismatch, or
9677 someone deleted the DWO/DWP file, or the search path isn't set up
9678 correctly to find the file. */
9680 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9681 kind
, dwo_name
, hex_string (signature
));
9683 /* This is a warning and not a complaint because it can be caused by
9684 pilot error (e.g., user accidentally deleting the DWO). */
9686 /* Print the name of the DWP file if we looked there, helps the user
9687 better diagnose the problem. */
9688 std::string dwp_text
;
9690 if (dwp_file
!= NULL
)
9691 dwp_text
= string_printf (" [in DWP file %s]",
9692 lbasename (dwp_file
->name
));
9694 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9696 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
9697 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
9702 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9703 See lookup_dwo_cutu_unit for details. */
9705 static struct dwo_unit
*
9706 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
9709 gdb_assert (!cu
->per_cu
->is_debug_types
);
9711 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
9714 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9715 See lookup_dwo_cutu_unit for details. */
9717 static struct dwo_unit
*
9718 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
9720 gdb_assert (cu
->per_cu
->is_debug_types
);
9722 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
9724 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
9727 /* Traversal function for queue_and_load_all_dwo_tus. */
9730 queue_and_load_dwo_tu (void **slot
, void *info
)
9732 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
9733 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
9734 ULONGEST signature
= dwo_unit
->signature
;
9735 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
9737 if (sig_type
!= NULL
)
9739 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9740 a real dependency of PER_CU on SIG_TYPE. That is detected later
9741 while processing PER_CU. */
9742 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
9744 load_full_type_unit (sig_type
, cu
->per_objfile
);
9745 cu
->per_cu
->imported_symtabs_push (sig_type
);
9751 /* Queue all TUs contained in the DWO of CU to be read in.
9752 The DWO may have the only definition of the type, though it may not be
9753 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9754 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9757 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
9759 struct dwo_unit
*dwo_unit
;
9760 struct dwo_file
*dwo_file
;
9762 gdb_assert (cu
!= nullptr);
9763 gdb_assert (!cu
->per_cu
->is_debug_types
);
9764 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
9766 dwo_unit
= cu
->dwo_unit
;
9767 gdb_assert (dwo_unit
!= NULL
);
9769 dwo_file
= dwo_unit
->dwo_file
;
9770 if (dwo_file
->tus
!= NULL
)
9771 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
9774 /* Read in various DIEs. */
9776 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9777 Inherit only the children of the DW_AT_abstract_origin DIE not being
9778 already referenced by DW_AT_abstract_origin from the children of the
9782 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
9784 attribute
*attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
9785 if (attr
== nullptr)
9788 /* Note that following die references may follow to a die in a
9790 dwarf2_cu
*origin_cu
= cu
;
9792 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9793 die_info
*origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
9795 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9797 struct pending
**origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
9798 origin_cu
->list_in_scope
= cu
->list_in_scope
;
9800 if (die
->tag
!= origin_die
->tag
9801 && !(die
->tag
== DW_TAG_inlined_subroutine
9802 && origin_die
->tag
== DW_TAG_subprogram
))
9803 complaint (_("DIE %s and its abstract origin %s have different tags"),
9804 sect_offset_str (die
->sect_off
),
9805 sect_offset_str (origin_die
->sect_off
));
9807 /* Find if the concrete and abstract trees are structurally the
9808 same. This is a shallow traversal and it is not bullet-proof;
9809 the compiler can trick the debugger into believing that the trees
9810 are isomorphic, whereas they actually are not. However, the
9811 likelihood of this happening is pretty low, and a full-fledged
9812 check would be an overkill. */
9813 bool are_isomorphic
= true;
9814 die_info
*concrete_child
= die
->child
;
9815 die_info
*abstract_child
= origin_die
->child
;
9816 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
9818 if (concrete_child
== nullptr
9819 || abstract_child
== nullptr
9820 || concrete_child
->tag
!= abstract_child
->tag
)
9822 are_isomorphic
= false;
9826 concrete_child
= concrete_child
->sibling
;
9827 abstract_child
= abstract_child
->sibling
;
9830 /* Walk the origin's children in parallel to the concrete children.
9831 This helps match an origin child in case the debug info misses
9832 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9833 origin tree may not have the same tree structure as the concrete
9835 die_info
*corresponding_abstract_child
9836 = are_isomorphic
? origin_die
->child
: nullptr;
9838 std::vector
<sect_offset
> offsets
;
9840 for (die_info
*child_die
= die
->child
;
9841 child_die
&& child_die
->tag
;
9842 child_die
= child_die
->sibling
)
9844 /* We are trying to process concrete instance entries:
9845 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9846 it's not relevant to our analysis here. i.e. detecting DIEs that are
9847 present in the abstract instance but not referenced in the concrete
9849 if (child_die
->tag
== DW_TAG_call_site
9850 || child_die
->tag
== DW_TAG_GNU_call_site
)
9853 corresponding_abstract_child
9854 = corresponding_abstract_child
->sibling
;
9858 /* For each CHILD_DIE, find the corresponding child of
9859 ORIGIN_DIE. If there is more than one layer of
9860 DW_AT_abstract_origin, follow them all; there shouldn't be,
9861 but GCC versions at least through 4.4 generate this (GCC PR
9863 die_info
*child_origin_die
= child_die
;
9864 dwarf2_cu
*child_origin_cu
= cu
;
9867 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
9869 if (attr
== nullptr)
9872 die_info
*prev_child_origin_die
= child_origin_die
;
9873 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
9876 if (prev_child_origin_die
== child_origin_die
)
9878 /* Handle DIE with self-reference. */
9883 /* If missing DW_AT_abstract_origin, try the corresponding child
9884 of the origin. Clang emits such lexical scopes. */
9885 if (child_origin_die
== child_die
9886 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
9888 && child_die
->tag
== DW_TAG_lexical_block
)
9889 child_origin_die
= corresponding_abstract_child
;
9891 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9892 counterpart may exist. */
9893 if (child_origin_die
!= child_die
)
9895 if (child_die
->tag
!= child_origin_die
->tag
9896 && !(child_die
->tag
== DW_TAG_inlined_subroutine
9897 && child_origin_die
->tag
== DW_TAG_subprogram
))
9898 complaint (_("Child DIE %s and its abstract origin %s have "
9900 sect_offset_str (child_die
->sect_off
),
9901 sect_offset_str (child_origin_die
->sect_off
));
9902 if (child_origin_die
->parent
!= origin_die
)
9903 complaint (_("Child DIE %s and its abstract origin %s have "
9904 "different parents"),
9905 sect_offset_str (child_die
->sect_off
),
9906 sect_offset_str (child_origin_die
->sect_off
));
9908 offsets
.push_back (child_origin_die
->sect_off
);
9912 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
9915 if (!offsets
.empty ())
9917 std::sort (offsets
.begin (), offsets
.end ());
9919 for (auto offsets_it
= offsets
.begin () + 1;
9920 offsets_it
< offsets
.end ();
9922 if (*(offsets_it
- 1) == *offsets_it
)
9923 complaint (_("Multiple children of DIE %s refer "
9924 "to DIE %s as their abstract origin"),
9925 sect_offset_str (die
->sect_off
),
9926 sect_offset_str (*offsets_it
));
9929 auto offsets_it
= offsets
.begin ();
9930 die_info
*origin_child_die
= origin_die
->child
;
9931 while (origin_child_die
!= nullptr && origin_child_die
->tag
!= 0)
9933 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9934 while (offsets_it
< offsets
.end ()
9935 && *offsets_it
< origin_child_die
->sect_off
)
9938 if (offsets_it
== offsets
.end ()
9939 || *offsets_it
> origin_child_die
->sect_off
)
9941 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9942 Check whether we're already processing ORIGIN_CHILD_DIE.
9943 This can happen with mutually referenced abstract_origins.
9945 if (!origin_child_die
->in_process
)
9946 process_die (origin_child_die
, origin_cu
);
9949 origin_child_die
= origin_child_die
->sibling
;
9952 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
9954 if (cu
!= origin_cu
)
9955 compute_delayed_physnames (origin_cu
);
9958 /* Return TRUE if the given DIE is the program's "main". DWARF 4 has
9959 defined a dedicated DW_AT_main_subprogram attribute to indicate the
9960 starting function of the program, however with older versions the
9961 DW_CC_program value of the DW_AT_calling_convention attribute was
9962 used instead as the only means available. We handle both variants. */
9965 dwarf2_func_is_main_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
9967 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
9969 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
9970 return (attr
!= nullptr
9971 && attr
->constant_value (DW_CC_normal
) == DW_CC_program
);
9974 /* A helper to handle Ada's "Pragma Import" feature when it is applied
9978 check_ada_pragma_import (struct die_info
*die
, struct dwarf2_cu
*cu
)
9980 /* A Pragma Import will have both a name and a linkage name. */
9981 const char *name
= dwarf2_name (die
, cu
);
9982 if (name
== nullptr)
9985 const char *linkage_name
= dw2_linkage_name (die
, cu
);
9986 /* Disallow the special Ada symbols. */
9987 if (!is_ada_import_or_export (cu
, name
, linkage_name
))
9990 /* A Pragma Import will be a declaration, while a Pragma Export will
9992 if (!die_is_declaration (die
, cu
))
9995 new_symbol (die
, read_type_die (die
, cu
), cu
);
10000 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
10002 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10003 struct objfile
*objfile
= per_objfile
->objfile
;
10004 struct gdbarch
*gdbarch
= objfile
->arch ();
10005 struct context_stack
*newobj
;
10008 struct die_info
*child_die
;
10009 struct attribute
*attr
, *call_line
, *call_file
;
10011 struct block
*block
;
10012 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
10013 std::vector
<struct symbol
*> template_args
;
10014 struct template_symbol
*templ_func
= NULL
;
10018 /* If we do not have call site information, we can't show the
10019 caller of this inlined function. That's too confusing, so
10020 only use the scope for local variables. */
10021 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
10022 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
10023 if (call_line
== NULL
|| call_file
== NULL
)
10025 read_lexical_block_scope (die
, cu
);
10030 name
= dwarf2_name (die
, cu
);
10031 if (name
== nullptr)
10032 name
= dw2_linkage_name (die
, cu
);
10034 /* Ignore functions with missing or empty names. These are actually
10035 illegal according to the DWARF standard. */
10038 complaint (_("missing name for subprogram DIE at %s"),
10039 sect_offset_str (die
->sect_off
));
10043 if (check_ada_pragma_import (die
, cu
))
10045 /* We already made the symbol for the Pragma Import, and because
10046 it is a declaration, we know it won't have any other
10047 important information, so we can simply return. */
10051 /* Ignore functions with missing or invalid low and high pc attributes. */
10052 unrelocated_addr unrel_low
, unrel_high
;
10053 if (dwarf2_get_pc_bounds (die
, &unrel_low
, &unrel_high
, cu
, nullptr, nullptr)
10054 <= PC_BOUNDS_INVALID
)
10056 if (have_complaint ())
10058 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
10059 bool external_p
= attr
!= nullptr && attr
->as_boolean ();
10060 attr
= dwarf2_attr (die
, DW_AT_inline
, cu
);
10063 && attr
->is_nonnegative ()
10064 && (attr
->as_nonnegative () == DW_INL_inlined
10065 || attr
->as_nonnegative () == DW_INL_declared_inlined
));
10066 attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
10067 bool decl_p
= attr
!= nullptr && attr
->as_boolean ();
10068 if (!external_p
&& !inlined_p
&& !decl_p
)
10069 complaint (_("cannot get low and high bounds "
10070 "for subprogram DIE at %s"),
10071 sect_offset_str (die
->sect_off
));
10076 lowpc
= per_objfile
->relocate (unrel_low
);
10077 highpc
= per_objfile
->relocate (unrel_high
);
10079 /* If we have any template arguments, then we must allocate a
10080 different sort of symbol. */
10081 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
10083 if (child_die
->tag
== DW_TAG_template_type_param
10084 || child_die
->tag
== DW_TAG_template_value_param
)
10086 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
10087 templ_func
->subclass
= SYMBOL_TEMPLATE
;
10092 gdb_assert (cu
->get_builder () != nullptr);
10093 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
10094 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
, templ_func
);
10096 if (dwarf2_func_is_main_p (die
, cu
))
10097 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
10100 /* If there is a location expression for DW_AT_frame_base, record
10102 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
10103 if (attr
!= nullptr)
10104 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
10106 /* If there is a location for the static link, record it. */
10107 newobj
->static_link
= NULL
;
10108 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
10109 if (attr
!= nullptr)
10111 newobj
->static_link
10112 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
10113 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
10117 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
10119 if (die
->child
!= NULL
)
10121 child_die
= die
->child
;
10122 while (child_die
&& child_die
->tag
)
10124 if (child_die
->tag
== DW_TAG_template_type_param
10125 || child_die
->tag
== DW_TAG_template_value_param
)
10127 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
10130 template_args
.push_back (arg
);
10133 process_die (child_die
, cu
);
10134 child_die
= child_die
->sibling
;
10138 inherit_abstract_dies (die
, cu
);
10140 /* If we have a DW_AT_specification, we might need to import using
10141 directives from the context of the specification DIE. See the
10142 comment in determine_prefix. */
10143 if (cu
->lang () == language_cplus
10144 && dwarf2_attr (die
, DW_AT_specification
, cu
))
10146 struct dwarf2_cu
*spec_cu
= cu
;
10147 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
10151 child_die
= spec_die
->child
;
10152 while (child_die
&& child_die
->tag
)
10154 if (child_die
->tag
== DW_TAG_imported_module
)
10155 process_die (child_die
, spec_cu
);
10156 child_die
= child_die
->sibling
;
10159 /* In some cases, GCC generates specification DIEs that
10160 themselves contain DW_AT_specification attributes. */
10161 spec_die
= die_specification (spec_die
, &spec_cu
);
10165 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
10166 /* Make a block for the local symbols within. */
10167 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
10168 cstk
.static_link
, lowpc
, highpc
);
10170 /* For C++, set the block's scope. */
10171 if ((cu
->lang () == language_cplus
10172 || cu
->lang () == language_fortran
10173 || cu
->lang () == language_d
10174 || cu
->lang () == language_rust
)
10175 && cu
->processing_has_namespace_info
)
10176 block
->set_scope (determine_prefix (die
, cu
),
10177 &objfile
->objfile_obstack
);
10179 /* If we have address ranges, record them. */
10180 dwarf2_record_block_ranges (die
, block
, cu
);
10182 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
10184 /* Attach template arguments to function. */
10185 if (!template_args
.empty ())
10187 gdb_assert (templ_func
!= NULL
);
10189 templ_func
->n_template_arguments
= template_args
.size ();
10190 templ_func
->template_arguments
10191 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
10192 templ_func
->n_template_arguments
);
10193 memcpy (templ_func
->template_arguments
,
10194 template_args
.data (),
10195 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
10197 /* Make sure that the symtab is set on the new symbols. Even
10198 though they don't appear in this symtab directly, other parts
10199 of gdb assume that symbols do, and this is reasonably
10201 for (symbol
*sym
: template_args
)
10202 sym
->set_symtab (templ_func
->symtab ());
10205 /* In C++, we can have functions nested inside functions (e.g., when
10206 a function declares a class that has methods). This means that
10207 when we finish processing a function scope, we may need to go
10208 back to building a containing block's symbol lists. */
10209 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
10210 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
10212 /* If we've finished processing a top-level function, subsequent
10213 symbols go in the file symbol list. */
10214 if (cu
->get_builder ()->outermost_context_p ())
10215 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
10218 /* Process all the DIES contained within a lexical block scope. Start
10219 a new scope, process the dies, and then close the scope. */
10222 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
10224 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10225 CORE_ADDR lowpc
, highpc
;
10226 struct die_info
*child_die
;
10228 /* Ignore blocks with missing or invalid low and high pc attributes. */
10229 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10230 as multiple lexical blocks? Handling children in a sane way would
10231 be nasty. Might be easier to properly extend generic blocks to
10232 describe ranges. */
10233 unrelocated_addr unrel_low
, unrel_high
;
10234 switch (dwarf2_get_pc_bounds (die
, &unrel_low
, &unrel_high
, cu
,
10237 case PC_BOUNDS_NOT_PRESENT
:
10238 /* DW_TAG_lexical_block has no attributes, process its children as if
10239 there was no wrapping by that DW_TAG_lexical_block.
10240 GCC does no longer produces such DWARF since GCC r224161. */
10241 for (child_die
= die
->child
;
10242 child_die
!= NULL
&& child_die
->tag
;
10243 child_die
= child_die
->sibling
)
10245 /* We might already be processing this DIE. This can happen
10246 in an unusual circumstance -- where a subroutine A
10247 appears lexically in another subroutine B, but A actually
10248 inlines B. The recursion is broken here, rather than in
10249 inherit_abstract_dies, because it seems better to simply
10250 drop concrete children here. */
10251 if (!child_die
->in_process
)
10252 process_die (child_die
, cu
);
10255 case PC_BOUNDS_INVALID
:
10258 lowpc
= per_objfile
->relocate (unrel_low
);
10259 highpc
= per_objfile
->relocate (unrel_high
);
10261 cu
->get_builder ()->push_context (0, lowpc
);
10262 if (die
->child
!= NULL
)
10264 child_die
= die
->child
;
10265 while (child_die
&& child_die
->tag
)
10267 process_die (child_die
, cu
);
10268 child_die
= child_die
->sibling
;
10271 inherit_abstract_dies (die
, cu
);
10272 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
10274 if (*cu
->get_builder ()->get_local_symbols () != NULL
10275 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
10277 struct block
*block
10278 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
10279 cstk
.start_addr
, highpc
);
10281 /* Note that recording ranges after traversing children, as we
10282 do here, means that recording a parent's ranges entails
10283 walking across all its children's ranges as they appear in
10284 the address map, which is quadratic behavior.
10286 It would be nicer to record the parent's ranges before
10287 traversing its children, simply overriding whatever you find
10288 there. But since we don't even decide whether to create a
10289 block until after we've traversed its children, that's hard
10291 dwarf2_record_block_ranges (die
, block
, cu
);
10293 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
10294 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
10297 static void dwarf2_ranges_read_low_addrs
10299 struct dwarf2_cu
*cu
,
10301 std::vector
<unrelocated_addr
> &result
);
10303 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10306 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
10308 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10309 struct objfile
*objfile
= per_objfile
->objfile
;
10310 struct gdbarch
*gdbarch
= objfile
->arch ();
10311 struct attribute
*attr
;
10314 struct die_info
*child_die
;
10316 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
10319 /* This was a pre-DWARF-5 GNU extension alias
10320 for DW_AT_call_return_pc. */
10321 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
10325 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10326 "DIE %s [in module %s]"),
10327 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10330 unrelocated_addr pc
= per_objfile
->adjust (attr
->as_address ());
10332 if (cu
->call_site_htab
== NULL
)
10333 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
10334 call_site::eq
, NULL
,
10335 &objfile
->objfile_obstack
,
10336 hashtab_obstack_allocate
, NULL
);
10337 struct call_site
call_site_local (pc
, nullptr, nullptr);
10338 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
10341 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10342 "DIE %s [in module %s]"),
10343 paddress (gdbarch
, (CORE_ADDR
) pc
), sect_offset_str (die
->sect_off
),
10344 objfile_name (objfile
));
10348 /* Count parameters at the caller. */
10351 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
10352 child_die
= child_die
->sibling
)
10354 if (child_die
->tag
!= DW_TAG_call_site_parameter
10355 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
10357 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10358 "DW_TAG_call_site child DIE %s [in module %s]"),
10359 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
10360 objfile_name (objfile
));
10367 struct call_site
*call_site
10368 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
10370 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
10371 struct call_site (pc
, cu
->per_cu
, per_objfile
);
10374 /* We never call the destructor of call_site, so we must ensure it is
10375 trivially destructible. */
10376 static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
10378 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
10379 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
10381 struct die_info
*func_die
;
10383 /* Skip also over DW_TAG_inlined_subroutine. */
10384 for (func_die
= die
->parent
;
10385 func_die
&& func_die
->tag
!= DW_TAG_subprogram
10386 && func_die
->tag
!= DW_TAG_subroutine_type
;
10387 func_die
= func_die
->parent
);
10389 /* DW_AT_call_all_calls is a superset
10390 of DW_AT_call_all_tail_calls. */
10392 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
10393 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
10394 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
10395 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
10397 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10398 not complete. But keep CALL_SITE for look ups via call_site_htab,
10399 both the initial caller containing the real return address PC and
10400 the final callee containing the current PC of a chain of tail
10401 calls do not need to have the tail call list complete. But any
10402 function candidate for a virtual tail call frame searched via
10403 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10404 determined unambiguously. */
10408 struct type
*func_type
= NULL
;
10411 func_type
= get_die_type (func_die
, cu
);
10412 if (func_type
!= NULL
)
10414 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
10416 /* Enlist this call site to the function. */
10417 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
10418 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
10421 complaint (_("Cannot find function owning DW_TAG_call_site "
10422 "DIE %s [in module %s]"),
10423 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10427 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
10429 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
10431 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
10434 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10435 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
10438 call_site
->target
.set_loc_dwarf_block (nullptr);
10439 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
10440 /* Keep NULL DWARF_BLOCK. */;
10441 else if (attr
->form_is_block ())
10443 struct dwarf2_locexpr_baton
*dlbaton
;
10444 struct dwarf_block
*block
= attr
->as_block ();
10446 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
10447 dlbaton
->data
= block
->data
;
10448 dlbaton
->size
= block
->size
;
10449 dlbaton
->per_objfile
= per_objfile
;
10450 dlbaton
->per_cu
= cu
->per_cu
;
10452 call_site
->target
.set_loc_dwarf_block (dlbaton
);
10454 else if (attr
->form_is_ref ())
10456 struct dwarf2_cu
*target_cu
= cu
;
10457 struct die_info
*target_die
;
10459 target_die
= follow_die_ref (die
, attr
, &target_cu
);
10460 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
10462 struct attribute
*ranges_attr
10463 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
10465 if (die_is_declaration (target_die
, target_cu
))
10467 const char *target_physname
;
10469 /* Prefer the mangled name; otherwise compute the demangled one. */
10470 target_physname
= dw2_linkage_name (target_die
, target_cu
);
10471 if (target_physname
== NULL
)
10472 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
10473 if (target_physname
== NULL
)
10474 complaint (_("DW_AT_call_target target DIE has invalid "
10475 "physname, for referencing DIE %s [in module %s]"),
10476 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10478 call_site
->target
.set_loc_physname (target_physname
);
10480 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
10482 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
10483 + target_cu
->gnu_ranges_base
);
10484 std::vector
<unrelocated_addr
> addresses
;
10485 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
10486 target_die
->tag
, addresses
);
10487 unrelocated_addr
*saved
= XOBNEWVEC (&objfile
->objfile_obstack
,
10489 addresses
.size ());
10490 std::copy (addresses
.begin (), addresses
.end (), saved
);
10491 call_site
->target
.set_loc_array (addresses
.size (), saved
);
10495 unrelocated_addr lowpc
;
10497 /* DW_AT_entry_pc should be preferred. */
10498 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
10500 <= PC_BOUNDS_INVALID
)
10501 complaint (_("DW_AT_call_target target DIE has invalid "
10502 "low pc, for referencing DIE %s [in module %s]"),
10503 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10506 lowpc
= per_objfile
->adjust (lowpc
);
10507 call_site
->target
.set_loc_physaddr (lowpc
);
10512 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10513 "block nor reference, for DIE %s [in module %s]"),
10514 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10516 for (child_die
= die
->child
;
10517 child_die
&& child_die
->tag
;
10518 child_die
= child_die
->sibling
)
10520 struct call_site_parameter
*parameter
;
10521 struct attribute
*loc
, *origin
;
10523 if (child_die
->tag
!= DW_TAG_call_site_parameter
10524 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
10526 /* Already printed the complaint above. */
10530 gdb_assert (call_site
->parameter_count
< nparams
);
10531 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
10533 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10534 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10535 register is contained in DW_AT_call_value. */
10537 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
10538 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
10539 if (origin
== NULL
)
10541 /* This was a pre-DWARF-5 GNU extension alias
10542 for DW_AT_call_parameter. */
10543 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
10545 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
10547 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
10549 sect_offset sect_off
= origin
->get_ref_die_offset ();
10550 if (!cu
->header
.offset_in_cu_p (sect_off
))
10552 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10553 binding can be done only inside one CU. Such referenced DIE
10554 therefore cannot be even moved to DW_TAG_partial_unit. */
10555 complaint (_("DW_AT_call_parameter offset is not in CU for "
10556 "DW_TAG_call_site child DIE %s [in module %s]"),
10557 sect_offset_str (child_die
->sect_off
),
10558 objfile_name (objfile
));
10561 parameter
->u
.param_cu_off
10562 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
10564 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
10566 complaint (_("No DW_FORM_block* DW_AT_location for "
10567 "DW_TAG_call_site child DIE %s [in module %s]"),
10568 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
10573 struct dwarf_block
*block
= loc
->as_block ();
10575 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
10576 (block
->data
, &block
->data
[block
->size
]);
10577 if (parameter
->u
.dwarf_reg
!= -1)
10578 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
10579 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
10580 &block
->data
[block
->size
],
10581 ¶meter
->u
.fb_offset
))
10582 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
10585 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10586 "for DW_FORM_block* DW_AT_location is supported for "
10587 "DW_TAG_call_site child DIE %s "
10589 sect_offset_str (child_die
->sect_off
),
10590 objfile_name (objfile
));
10595 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
10597 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
10598 if (attr
== NULL
|| !attr
->form_is_block ())
10600 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10601 "DW_TAG_call_site child DIE %s [in module %s]"),
10602 sect_offset_str (child_die
->sect_off
),
10603 objfile_name (objfile
));
10607 struct dwarf_block
*block
= attr
->as_block ();
10608 parameter
->value
= block
->data
;
10609 parameter
->value_size
= block
->size
;
10611 /* Parameters are not pre-cleared by memset above. */
10612 parameter
->data_value
= NULL
;
10613 parameter
->data_value_size
= 0;
10614 call_site
->parameter_count
++;
10616 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
10618 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
10619 if (attr
!= nullptr)
10621 if (!attr
->form_is_block ())
10622 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10623 "DW_TAG_call_site child DIE %s [in module %s]"),
10624 sect_offset_str (child_die
->sect_off
),
10625 objfile_name (objfile
));
10628 block
= attr
->as_block ();
10629 parameter
->data_value
= block
->data
;
10630 parameter
->data_value_size
= block
->size
;
10636 /* Helper function for read_variable. If DIE represents a virtual
10637 table, then return the type of the concrete object that is
10638 associated with the virtual table. Otherwise, return NULL. */
10640 static struct type
*
10641 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
10643 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
10647 /* Find the type DIE. */
10648 struct die_info
*type_die
= NULL
;
10649 struct dwarf2_cu
*type_cu
= cu
;
10651 if (attr
->form_is_ref ())
10652 type_die
= follow_die_ref (die
, attr
, &type_cu
);
10653 if (type_die
== NULL
)
10656 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
10658 return die_containing_type (type_die
, type_cu
);
10661 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10664 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
10666 struct rust_vtable_symbol
*storage
= NULL
;
10668 if (cu
->lang () == language_rust
)
10670 struct type
*containing_type
= rust_containing_type (die
, cu
);
10672 if (containing_type
!= NULL
)
10674 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
10676 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
10677 storage
->concrete_type
= containing_type
;
10678 storage
->subclass
= SYMBOL_RUST_VTABLE
;
10682 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
10683 struct attribute
*abstract_origin
10684 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
10685 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
10686 if (res
== NULL
&& loc
&& abstract_origin
)
10688 /* We have a variable without a name, but with a location and an abstract
10689 origin. This may be a concrete instance of an abstract variable
10690 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10692 struct dwarf2_cu
*origin_cu
= cu
;
10693 struct die_info
*origin_die
10694 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
10695 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10696 per_objfile
->per_bfd
->abstract_to_concrete
10697 [origin_die
->sect_off
].push_back (die
->sect_off
);
10701 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10702 reading .debug_rnglists.
10703 Callback's type should be:
10704 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10705 Return true if the attributes are present and valid, otherwise,
10708 template <typename Callback
>
10710 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
10711 dwarf_tag tag
, Callback
&&callback
)
10713 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10714 struct objfile
*objfile
= per_objfile
->objfile
;
10715 bfd
*obfd
= objfile
->obfd
.get ();
10716 /* Base address selection entry. */
10717 std::optional
<unrelocated_addr
> base
;
10718 const gdb_byte
*buffer
;
10719 bool overflow
= false;
10720 ULONGEST addr_index
;
10721 struct dwarf2_section_info
*rnglists_section
;
10723 base
= cu
->base_address
;
10724 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
10725 rnglists_section
->read (objfile
);
10727 if (offset
>= rnglists_section
->size
)
10729 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10733 buffer
= rnglists_section
->buffer
+ offset
;
10737 /* Initialize it due to a false compiler warning. */
10738 unrelocated_addr range_beginning
= {}, range_end
= {};
10739 const gdb_byte
*buf_end
= (rnglists_section
->buffer
10740 + rnglists_section
->size
);
10741 unsigned int bytes_read
;
10743 if (buffer
== buf_end
)
10748 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
10751 case DW_RLE_end_of_list
:
10753 case DW_RLE_base_address
:
10754 if (buffer
+ cu
->header
.addr_size
> buf_end
)
10759 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
10760 buffer
+= bytes_read
;
10762 case DW_RLE_base_addressx
:
10763 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10764 buffer
+= bytes_read
;
10765 base
= read_addr_index (cu
, addr_index
);
10767 case DW_RLE_start_length
:
10768 if (buffer
+ cu
->header
.addr_size
> buf_end
)
10773 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
10775 buffer
+= bytes_read
;
10777 = (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10778 + read_unsigned_leb128 (obfd
, buffer
,
10780 buffer
+= bytes_read
;
10781 if (buffer
> buf_end
)
10787 case DW_RLE_startx_length
:
10788 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10789 buffer
+= bytes_read
;
10790 range_beginning
= read_addr_index (cu
, addr_index
);
10791 if (buffer
> buf_end
)
10797 = (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10798 + read_unsigned_leb128 (obfd
, buffer
,
10800 buffer
+= bytes_read
;
10802 case DW_RLE_offset_pair
:
10803 range_beginning
= (unrelocated_addr
) read_unsigned_leb128 (obfd
, buffer
,
10805 buffer
+= bytes_read
;
10806 if (buffer
> buf_end
)
10811 range_end
= (unrelocated_addr
) read_unsigned_leb128 (obfd
, buffer
,
10813 buffer
+= bytes_read
;
10814 if (buffer
> buf_end
)
10820 case DW_RLE_start_end
:
10821 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
10826 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
10828 buffer
+= bytes_read
;
10829 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
10830 buffer
+= bytes_read
;
10832 case DW_RLE_startx_endx
:
10833 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10834 buffer
+= bytes_read
;
10835 range_beginning
= read_addr_index (cu
, addr_index
);
10836 if (buffer
> buf_end
)
10841 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10842 buffer
+= bytes_read
;
10843 range_end
= read_addr_index (cu
, addr_index
);
10846 complaint (_("Invalid .debug_rnglists data (no base address)"));
10849 if (rlet
== DW_RLE_end_of_list
|| overflow
)
10851 if (rlet
== DW_RLE_base_address
)
10854 if (range_beginning
> range_end
)
10856 /* Inverted range entries are invalid. */
10857 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10861 /* Empty range entries have no effect. */
10862 if (range_beginning
== range_end
)
10865 /* Only DW_RLE_offset_pair needs the base address added. */
10866 if (rlet
== DW_RLE_offset_pair
)
10868 if (!base
.has_value ())
10870 /* We have no valid base address for the DW_RLE_offset_pair. */
10871 complaint (_("Invalid .debug_rnglists data (no base address for "
10872 "DW_RLE_offset_pair)"));
10876 range_beginning
= (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10877 + (CORE_ADDR
) *base
);
10878 range_end
= (unrelocated_addr
) ((CORE_ADDR
) range_end
10879 + (CORE_ADDR
) *base
);
10882 /* A not-uncommon case of bad debug info.
10883 Don't pollute the addrmap with bad data. */
10884 if (range_beginning
== (unrelocated_addr
) 0
10885 && !per_objfile
->per_bfd
->has_section_at_zero
)
10887 complaint (_(".debug_rnglists entry has start address of zero"
10888 " [in module %s]"), objfile_name (objfile
));
10892 callback (range_beginning
, range_end
);
10897 complaint (_("Offset %d is not terminated "
10898 "for DW_AT_ranges attribute"),
10906 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10907 Callback's type should be:
10908 void (unrelocated_addr range_beginning, unrelocated_addr range_end)
10909 Return 1 if the attributes are present and valid, otherwise, return 0. */
10911 template <typename Callback
>
10913 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
10914 Callback
&&callback
)
10916 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10917 struct objfile
*objfile
= per_objfile
->objfile
;
10918 struct comp_unit_head
*cu_header
= &cu
->header
;
10919 bfd
*obfd
= objfile
->obfd
.get ();
10920 unsigned int addr_size
= cu_header
->addr_size
;
10921 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
10922 /* Base address selection entry. */
10923 std::optional
<unrelocated_addr
> base
;
10924 unsigned int dummy
;
10925 const gdb_byte
*buffer
;
10927 if (cu_header
->version
>= 5)
10928 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
10930 base
= cu
->base_address
;
10932 per_objfile
->per_bfd
->ranges
.read (objfile
);
10933 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
10935 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10939 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
10943 unrelocated_addr range_beginning
, range_end
;
10945 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
10946 buffer
+= addr_size
;
10947 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
10948 buffer
+= addr_size
;
10949 offset
+= 2 * addr_size
;
10951 /* An end of list marker is a pair of zero addresses. */
10952 if (range_beginning
== (unrelocated_addr
) 0
10953 && range_end
== (unrelocated_addr
) 0)
10954 /* Found the end of list entry. */
10957 /* Each base address selection entry is a pair of 2 values.
10958 The first is the largest possible address, the second is
10959 the base address. Check for a base address here. */
10960 if (((CORE_ADDR
) range_beginning
& mask
) == mask
)
10962 /* If we found the largest possible address, then we already
10963 have the base address in range_end. */
10968 if (!base
.has_value ())
10970 /* We have no valid base address for the ranges
10972 complaint (_("Invalid .debug_ranges data (no base address)"));
10976 if (range_beginning
> range_end
)
10978 /* Inverted range entries are invalid. */
10979 complaint (_("Invalid .debug_ranges data (inverted range)"));
10983 /* Empty range entries have no effect. */
10984 if (range_beginning
== range_end
)
10987 range_beginning
= (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10988 + (CORE_ADDR
) *base
);
10989 range_end
= (unrelocated_addr
) ((CORE_ADDR
) range_end
10990 + (CORE_ADDR
) *base
);
10992 /* A not-uncommon case of bad debug info.
10993 Don't pollute the addrmap with bad data. */
10994 if (range_beginning
== (unrelocated_addr
) 0
10995 && !per_objfile
->per_bfd
->has_section_at_zero
)
10997 complaint (_(".debug_ranges entry has start address of zero"
10998 " [in module %s]"), objfile_name (objfile
));
11002 callback (range_beginning
, range_end
);
11008 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11009 Return 1 if the attributes are present and valid, otherwise, return 0.
11010 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
11011 ranges in MAP are set, using DATUM as the value. */
11014 dwarf2_ranges_read (unsigned offset
, unrelocated_addr
*low_return
,
11015 unrelocated_addr
*high_return
, struct dwarf2_cu
*cu
,
11016 addrmap
*map
, void *datum
, dwarf_tag tag
)
11018 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11020 unrelocated_addr low
= {};
11021 unrelocated_addr high
= {};
11024 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
11025 [&] (unrelocated_addr range_beginning
, unrelocated_addr range_end
)
11027 if (map
!= nullptr)
11029 unrelocated_addr lowpc
;
11030 unrelocated_addr highpc
;
11032 lowpc
= per_objfile
->adjust (range_beginning
);
11033 highpc
= per_objfile
->adjust (range_end
);
11034 /* addrmap only accepts CORE_ADDR, so we must cast here. */
11035 map
->set_empty ((CORE_ADDR
) lowpc
, (CORE_ADDR
) highpc
- 1, datum
);
11038 /* FIXME: This is recording everything as a low-high
11039 segment of consecutive addresses. We should have a
11040 data structure for discontiguous block ranges
11044 low
= range_beginning
;
11050 if (range_beginning
< low
)
11051 low
= range_beginning
;
11052 if (range_end
> high
)
11060 /* If the first entry is an end-of-list marker, the range
11061 describes an empty scope, i.e. no instructions. */
11067 *high_return
= high
;
11071 /* Process ranges and fill in a vector of the low PC values only. */
11074 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
11076 std::vector
<unrelocated_addr
> &result
)
11078 dwarf2_ranges_process (offset
, cu
, tag
,
11079 [&] (unrelocated_addr start
, unrelocated_addr end
)
11081 result
.push_back (start
);
11085 /* Determine the low and high pc of a DW_TAG_entry_point. */
11087 static pc_bounds_kind
11088 dwarf2_get_pc_bounds_entry_point (die_info
*die
, unrelocated_addr
*low
,
11089 unrelocated_addr
*high
, dwarf2_cu
*cu
)
11091 gdb_assert (low
!= nullptr);
11092 gdb_assert (high
!= nullptr);
11094 if (die
->parent
->tag
!= DW_TAG_subprogram
)
11096 complaint (_("DW_TAG_entry_point not embedded in DW_TAG_subprogram"));
11097 return PC_BOUNDS_INVALID
;
11100 /* A DW_TAG_entry_point is embedded in an subprogram. Therefore, we can use
11101 the highpc from its enveloping subprogram and get the lowpc from
11103 const enum pc_bounds_kind bounds_kind
= dwarf2_get_pc_bounds (die
->parent
,
11107 if (bounds_kind
== PC_BOUNDS_INVALID
|| bounds_kind
== PC_BOUNDS_NOT_PRESENT
)
11108 return bounds_kind
;
11110 attribute
*attr_low
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
11113 complaint (_("DW_TAG_entry_point is missing DW_AT_low_pc"));
11114 return PC_BOUNDS_INVALID
;
11116 *low
= attr_low
->as_address ();
11117 return bounds_kind
;
11120 /* Determine the low and high pc using the DW_AT_low_pc and DW_AT_high_pc or
11121 DW_AT_ranges attributes of a DIE. */
11123 static pc_bounds_kind
11124 dwarf_get_pc_bounds_ranges_or_highlow_pc (die_info
*die
, unrelocated_addr
*low
,
11125 unrelocated_addr
*high
, dwarf2_cu
*cu
,
11126 addrmap
*map
, void *datum
)
11128 gdb_assert (low
!= nullptr);
11129 gdb_assert (high
!= nullptr);
11131 struct attribute
*attr
;
11132 struct attribute
*attr_high
;
11133 enum pc_bounds_kind ret
;
11135 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
11138 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
11139 if (attr
!= nullptr)
11141 *low
= attr
->as_address ();
11142 *high
= attr_high
->as_address ();
11143 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
11144 *high
= (unrelocated_addr
) ((ULONGEST
) *high
+ (ULONGEST
) *low
);
11146 /* Found consecutive range of addresses. */
11147 ret
= PC_BOUNDS_HIGH_LOW
;
11151 /* Found high w/o low attribute. */
11152 ret
= PC_BOUNDS_INVALID
;
11157 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
11158 if (attr
!= nullptr && attr
->form_is_unsigned ())
11160 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11161 on DWARF version). */
11162 ULONGEST ranges_offset
= attr
->as_unsigned ();
11164 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11166 if (die
->tag
!= DW_TAG_compile_unit
)
11167 ranges_offset
+= cu
->gnu_ranges_base
;
11169 /* Value of the DW_AT_ranges attribute is the offset in the
11170 .debug_ranges section. */
11171 if (!dwarf2_ranges_read (ranges_offset
, low
, high
, cu
,
11172 map
, datum
, die
->tag
))
11173 return PC_BOUNDS_INVALID
;
11174 /* Found discontinuous range of addresses. */
11175 ret
= PC_BOUNDS_RANGES
;
11179 /* Could not find high_pc or ranges attributed and thus no bounds
11181 ret
= PC_BOUNDS_NOT_PRESENT
;
11188 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11189 definition for the return value. *LOWPC and *HIGHPC are set iff
11190 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11192 static enum pc_bounds_kind
11193 dwarf2_get_pc_bounds (struct die_info
*die
, unrelocated_addr
*lowpc
,
11194 unrelocated_addr
*highpc
, struct dwarf2_cu
*cu
,
11195 addrmap
*map
, void *datum
)
11197 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11199 unrelocated_addr low
= {};
11200 unrelocated_addr high
= {};
11201 enum pc_bounds_kind ret
;
11203 if (die
->tag
== DW_TAG_entry_point
)
11204 ret
= dwarf2_get_pc_bounds_entry_point (die
, &low
, &high
, cu
);
11206 ret
= dwarf_get_pc_bounds_ranges_or_highlow_pc (die
, &low
, &high
, cu
, map
,
11209 if (ret
== PC_BOUNDS_NOT_PRESENT
|| ret
== PC_BOUNDS_INVALID
)
11212 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11214 return PC_BOUNDS_INVALID
;
11216 /* When using the GNU linker, .gnu.linkonce. sections are used to
11217 eliminate duplicate copies of functions and vtables and such.
11218 The linker will arbitrarily choose one and discard the others.
11219 The AT_*_pc values for such functions refer to local labels in
11220 these sections. If the section from that file was discarded, the
11221 labels are not in the output, so the relocs get a value of 0.
11222 If this is a discarded function, mark the pc bounds as invalid,
11223 so that GDB will ignore it. */
11224 if (low
== (unrelocated_addr
) 0
11225 && !per_objfile
->per_bfd
->has_section_at_zero
)
11226 return PC_BOUNDS_INVALID
;
11228 gdb_assert (lowpc
!= nullptr);
11230 if (highpc
!= nullptr)
11235 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11236 its low and high PC addresses. Do nothing if these addresses could not
11237 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11238 and HIGHPC to the high address if greater than HIGHPC. */
11241 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
11242 unrelocated_addr
*lowpc
,
11243 unrelocated_addr
*highpc
,
11244 struct dwarf2_cu
*cu
)
11246 unrelocated_addr low
, high
;
11247 struct die_info
*child
= die
->child
;
11249 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
11250 >= PC_BOUNDS_RANGES
)
11252 *lowpc
= std::min (*lowpc
, low
);
11253 *highpc
= std::max (*highpc
, high
);
11256 /* If the language does not allow nested subprograms (either inside
11257 subprograms or lexical blocks), we're done. */
11258 if (cu
->lang () != language_ada
)
11261 /* Check all the children of the given DIE. If it contains nested
11262 subprograms, then check their pc bounds. Likewise, we need to
11263 check lexical blocks as well, as they may also contain subprogram
11265 while (child
&& child
->tag
)
11267 if (child
->tag
== DW_TAG_subprogram
11268 || child
->tag
== DW_TAG_lexical_block
)
11269 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
11270 child
= child
->sibling
;
11274 /* Get the low and high pc's represented by the scope DIE, and store
11275 them in *LOWPC and *HIGHPC. If the correct values can't be
11276 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11279 get_scope_pc_bounds (struct die_info
*die
,
11280 unrelocated_addr
*lowpc
, unrelocated_addr
*highpc
,
11281 struct dwarf2_cu
*cu
)
11283 unrelocated_addr best_low
= (unrelocated_addr
) -1;
11284 unrelocated_addr best_high
= {};
11285 unrelocated_addr current_low
, current_high
;
11287 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
11289 >= PC_BOUNDS_RANGES
)
11291 best_low
= current_low
;
11292 best_high
= current_high
;
11296 struct die_info
*child
= die
->child
;
11298 while (child
&& child
->tag
)
11300 switch (child
->tag
) {
11301 case DW_TAG_subprogram
:
11302 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
11304 case DW_TAG_namespace
:
11305 case DW_TAG_module
:
11306 /* FIXME: carlton/2004-01-16: Should we do this for
11307 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11308 that current GCC's always emit the DIEs corresponding
11309 to definitions of methods of classes as children of a
11310 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11311 the DIEs giving the declarations, which could be
11312 anywhere). But I don't see any reason why the
11313 standards says that they have to be there. */
11314 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
11316 if (current_low
!= ((unrelocated_addr
) -1))
11318 best_low
= std::min (best_low
, current_low
);
11319 best_high
= std::max (best_high
, current_high
);
11327 child
= child
->sibling
;
11332 *highpc
= best_high
;
11335 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11339 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
11340 struct dwarf2_cu
*cu
)
11342 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11343 struct objfile
*objfile
= per_objfile
->objfile
;
11344 struct attribute
*attr
;
11345 struct attribute
*attr_high
;
11347 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
11350 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
11351 if (attr
!= nullptr)
11353 unrelocated_addr unrel_low
= attr
->as_address ();
11354 unrelocated_addr unrel_high
= attr_high
->as_address ();
11356 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
11357 unrel_high
= (unrelocated_addr
) ((ULONGEST
) unrel_high
11358 + (ULONGEST
) unrel_low
);
11360 CORE_ADDR low
= per_objfile
->relocate (unrel_low
);
11361 CORE_ADDR high
= per_objfile
->relocate (unrel_high
);
11362 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
11366 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
11367 if (attr
!= nullptr && attr
->form_is_unsigned ())
11369 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11370 on DWARF version). */
11371 ULONGEST ranges_offset
= attr
->as_unsigned ();
11373 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11375 if (die
->tag
!= DW_TAG_compile_unit
)
11376 ranges_offset
+= cu
->gnu_ranges_base
;
11378 std::vector
<blockrange
> blockvec
;
11379 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
11380 [&] (unrelocated_addr start
, unrelocated_addr end
)
11382 CORE_ADDR abs_start
= per_objfile
->relocate (start
);
11383 CORE_ADDR abs_end
= per_objfile
->relocate (end
);
11384 cu
->get_builder ()->record_block_range (block
, abs_start
,
11386 blockvec
.emplace_back (abs_start
, abs_end
);
11389 block
->set_ranges (make_blockranges (objfile
, blockvec
));
11393 /* Check whether the producer field indicates either of GCC < 4.6, or the
11394 Intel C/C++ compiler, and cache the result in CU. */
11397 check_producer (struct dwarf2_cu
*cu
)
11401 if (cu
->producer
== NULL
)
11403 /* For unknown compilers expect their behavior is DWARF version
11406 GCC started to support .debug_types sections by -gdwarf-4 since
11407 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11408 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11409 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11410 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11412 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
11414 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
11415 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
11416 cu
->producer_is_gcc_11
= major
== 11;
11418 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
11420 cu
->producer_is_icc
= true;
11421 cu
->producer_is_icc_lt_14
= major
< 14;
11423 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
11424 cu
->producer_is_codewarrior
= true;
11425 else if (producer_is_clang (cu
->producer
, &major
, &minor
))
11426 cu
->producer_is_clang
= true;
11427 else if (producer_is_gas (cu
->producer
, &major
, &minor
))
11429 cu
->producer_is_gas_lt_2_38
= major
< 2 || (major
== 2 && minor
< 38);
11430 cu
->producer_is_gas_2_39
= major
== 2 && minor
== 39;
11434 /* For other non-GCC compilers, expect their behavior is DWARF version
11438 cu
->checked_producer
= true;
11441 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11442 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11443 during 4.6.0 experimental. */
11446 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
11448 if (!cu
->checked_producer
)
11449 check_producer (cu
);
11451 return cu
->producer_is_gxx_lt_4_6
;
11455 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11456 with incorrect is_stmt attributes. */
11459 producer_is_codewarrior (struct dwarf2_cu
*cu
)
11461 if (!cu
->checked_producer
)
11462 check_producer (cu
);
11464 return cu
->producer_is_codewarrior
;
11468 producer_is_gas_lt_2_38 (struct dwarf2_cu
*cu
)
11470 if (!cu
->checked_producer
)
11471 check_producer (cu
);
11473 return cu
->producer_is_gas_lt_2_38
;
11477 producer_is_gas_2_39 (struct dwarf2_cu
*cu
)
11479 if (!cu
->checked_producer
)
11480 check_producer (cu
);
11482 return cu
->producer_is_gas_2_39
;
11485 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11486 If that attribute is not available, return the appropriate
11489 static enum dwarf_access_attribute
11490 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
11492 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
11493 if (attr
!= nullptr)
11495 LONGEST value
= attr
->constant_value (-1);
11496 if (value
== DW_ACCESS_public
11497 || value
== DW_ACCESS_protected
11498 || value
== DW_ACCESS_private
)
11499 return (dwarf_access_attribute
) value
;
11500 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11504 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
11506 /* The default DWARF 2 accessibility for members is public, the default
11507 accessibility for inheritance is private. */
11509 if (die
->tag
!= DW_TAG_inheritance
)
11510 return DW_ACCESS_public
;
11512 return DW_ACCESS_private
;
11516 /* DWARF 3+ defines the default accessibility a different way. The same
11517 rules apply now for DW_TAG_inheritance as for the members and it only
11518 depends on the container kind. */
11520 if (die
->parent
->tag
== DW_TAG_class_type
)
11521 return DW_ACCESS_private
;
11523 return DW_ACCESS_public
;
11527 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11528 *OFFSET to the byte offset. If the attribute was not found return
11529 0, otherwise return 1. If it was found but could not properly be
11530 handled, set *OFFSET to 0. */
11533 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
11536 struct attribute
*attr
;
11538 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
11544 /* Note that we do not check for a section offset first here.
11545 This is because DW_AT_data_member_location is new in DWARF 4,
11546 so if we see it, we can assume that a constant form is really
11547 a constant and not a section offset. */
11548 if (attr
->form_is_constant ())
11549 *offset
= attr
->constant_value (0);
11550 else if (attr
->form_is_section_offset ())
11551 dwarf2_complex_location_expr_complaint ();
11552 else if (attr
->form_is_block ()
11553 && decode_locdesc (attr
->as_block (), cu
, &temp
))
11558 dwarf2_complex_location_expr_complaint ();
11564 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
11565 if (attr
!= nullptr)
11567 *offset
= attr
->constant_value (0);
11575 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11576 store the results in FIELD. */
11579 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
11580 struct field
*field
)
11582 struct attribute
*attr
;
11584 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
11587 if (attr
->form_is_constant ())
11589 LONGEST offset
= attr
->constant_value (0);
11591 /* Work around this GCC 11 bug, where it would erroneously use -1
11592 data member locations, instead of 0:
11594 Negative DW_AT_data_member_location
11595 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11597 if (offset
== -1 && cu
->producer_is_gcc_11
)
11599 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11603 field
->set_loc_bitpos (offset
* bits_per_byte
);
11605 else if (attr
->form_is_section_offset ())
11606 dwarf2_complex_location_expr_complaint ();
11607 else if (attr
->form_is_block ())
11610 if (decode_locdesc (attr
->as_block (), cu
, &offset
))
11611 field
->set_loc_bitpos (offset
* bits_per_byte
);
11614 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11615 struct objfile
*objfile
= per_objfile
->objfile
;
11616 struct dwarf2_locexpr_baton
*dlbaton
11617 = XOBNEW (&objfile
->objfile_obstack
,
11618 struct dwarf2_locexpr_baton
);
11619 dlbaton
->data
= attr
->as_block ()->data
;
11620 dlbaton
->size
= attr
->as_block ()->size
;
11621 /* When using this baton, we want to compute the address
11622 of the field, not the value. This is why
11623 is_reference is set to false here. */
11624 dlbaton
->is_reference
= false;
11625 dlbaton
->per_objfile
= per_objfile
;
11626 dlbaton
->per_cu
= cu
->per_cu
;
11628 field
->set_loc_dwarf_block (dlbaton
);
11632 dwarf2_complex_location_expr_complaint ();
11636 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
11637 if (attr
!= nullptr)
11638 field
->set_loc_bitpos (attr
->constant_value (0));
11642 /* Add an aggregate field to the field list. */
11645 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
11646 struct dwarf2_cu
*cu
)
11648 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11649 struct gdbarch
*gdbarch
= objfile
->arch ();
11650 struct nextfield
*new_field
;
11651 struct attribute
*attr
;
11653 const char *fieldname
= "";
11655 if (die
->tag
== DW_TAG_inheritance
)
11657 fip
->baseclasses
.emplace_back ();
11658 new_field
= &fip
->baseclasses
.back ();
11662 fip
->fields
.emplace_back ();
11663 new_field
= &fip
->fields
.back ();
11666 new_field
->offset
= die
->sect_off
;
11668 switch (dwarf2_access_attribute (die
, cu
))
11670 case DW_ACCESS_public
:
11672 case DW_ACCESS_private
:
11673 new_field
->field
.set_accessibility (accessibility::PRIVATE
);
11675 case DW_ACCESS_protected
:
11676 new_field
->field
.set_accessibility (accessibility::PROTECTED
);
11679 gdb_assert_not_reached ("invalid accessibility");
11682 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
11683 if (attr
!= nullptr && attr
->as_virtuality ())
11684 new_field
->field
.set_virtual ();
11686 fp
= &new_field
->field
;
11688 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
11689 && !die_is_declaration (die
, cu
))
11691 if (die
->tag
== DW_TAG_namelist_item
)
11693 /* Typically, DW_TAG_namelist_item are references to namelist items.
11694 If so, follow that reference. */
11695 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
11696 struct die_info
*item_die
= nullptr;
11697 struct dwarf2_cu
*item_cu
= cu
;
11698 if (attr1
->form_is_ref ())
11699 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
11700 if (item_die
!= nullptr)
11703 /* Data member other than a C++ static data member. */
11705 /* Get type of field. */
11706 fp
->set_type (die_type (die
, cu
));
11708 fp
->set_loc_bitpos (0);
11710 /* Get bit size of field (zero if none). */
11711 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
11712 if (attr
!= nullptr)
11713 fp
->set_bitsize (attr
->constant_value (0));
11715 fp
->set_bitsize (0);
11717 /* Get bit offset of field. */
11718 handle_member_location (die
, cu
, fp
);
11719 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
11720 if (attr
!= nullptr && attr
->form_is_constant ())
11722 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
11724 /* For big endian bits, the DW_AT_bit_offset gives the
11725 additional bit offset from the MSB of the containing
11726 anonymous object to the MSB of the field. We don't
11727 have to do anything special since we don't need to
11728 know the size of the anonymous object. */
11729 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
11733 /* For little endian bits, compute the bit offset to the
11734 MSB of the anonymous object, subtract off the number of
11735 bits from the MSB of the field to the MSB of the
11736 object, and then subtract off the number of bits of
11737 the field itself. The result is the bit offset of
11738 the LSB of the field. */
11739 int anonymous_size
;
11740 int bit_offset
= attr
->constant_value (0);
11742 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
11743 if (attr
!= nullptr && attr
->form_is_constant ())
11745 /* The size of the anonymous object containing
11746 the bit field is explicit, so use the
11747 indicated size (in bytes). */
11748 anonymous_size
= attr
->constant_value (0);
11752 /* The size of the anonymous object containing
11753 the bit field must be inferred from the type
11754 attribute of the data member containing the
11756 anonymous_size
= fp
->type ()->length ();
11758 fp
->set_loc_bitpos (fp
->loc_bitpos ()
11759 + anonymous_size
* bits_per_byte
11760 - bit_offset
- fp
->bitsize ());
11764 /* Get name of field. */
11765 fieldname
= dwarf2_name (die
, cu
);
11766 if (fieldname
== NULL
)
11769 /* The name is already allocated along with this objfile, so we don't
11770 need to duplicate it for the type. */
11771 fp
->set_name (fieldname
);
11773 /* Change accessibility for artificial fields (e.g. virtual table
11774 pointer or virtual base class pointer) to private. */
11775 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
11777 fp
->set_is_artificial (true);
11778 fp
->set_accessibility (accessibility::PRIVATE
);
11781 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
11783 /* C++ static member. */
11785 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11786 is a declaration, but all versions of G++ as of this writing
11787 (so through at least 3.2.1) incorrectly generate
11788 DW_TAG_variable tags. */
11790 const char *physname
;
11792 /* Get name of field. */
11793 fieldname
= dwarf2_name (die
, cu
);
11794 if (fieldname
== NULL
)
11797 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
11799 /* Only create a symbol if this is an external value.
11800 new_symbol checks this and puts the value in the global symbol
11801 table, which we want. If it is not external, new_symbol
11802 will try to put the value in cu->list_in_scope which is wrong. */
11803 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
11805 /* A static const member, not much different than an enum as far as
11806 we're concerned, except that we can support more types. */
11807 new_symbol (die
, NULL
, cu
);
11810 /* Get physical name. */
11811 physname
= dwarf2_physname (fieldname
, die
, cu
);
11813 /* The name is already allocated along with this objfile, so we don't
11814 need to duplicate it for the type. */
11815 fp
->set_loc_physname (physname
? physname
: "");
11816 fp
->set_type (die_type (die
, cu
));
11817 fp
->set_name (fieldname
);
11819 else if (die
->tag
== DW_TAG_inheritance
)
11821 /* C++ base class field. */
11822 handle_member_location (die
, cu
, fp
);
11823 fp
->set_bitsize (0);
11824 fp
->set_type (die_type (die
, cu
));
11825 fp
->set_name (fp
->type ()->name ());
11828 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11831 /* Can the type given by DIE define another type? */
11834 type_can_define_types (const struct die_info
*die
)
11838 case DW_TAG_typedef
:
11839 case DW_TAG_class_type
:
11840 case DW_TAG_structure_type
:
11841 case DW_TAG_union_type
:
11842 case DW_TAG_enumeration_type
:
11850 /* Add a type definition defined in the scope of the FIP's class. */
11853 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
11854 struct dwarf2_cu
*cu
)
11856 struct decl_field fp
;
11857 memset (&fp
, 0, sizeof (fp
));
11859 gdb_assert (type_can_define_types (die
));
11861 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11862 fp
.name
= dwarf2_name (die
, cu
);
11863 fp
.type
= read_type_die (die
, cu
);
11865 /* Save accessibility. */
11866 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
11867 switch (accessibility
)
11869 case DW_ACCESS_public
:
11870 /* The assumed value if neither private nor protected. */
11872 case DW_ACCESS_private
:
11873 fp
.accessibility
= accessibility::PRIVATE
;
11875 case DW_ACCESS_protected
:
11876 fp
.accessibility
= accessibility::PROTECTED
;
11880 if (die
->tag
== DW_TAG_typedef
)
11881 fip
->typedef_field_list
.push_back (fp
);
11883 fip
->nested_types_list
.push_back (fp
);
11886 /* A convenience typedef that's used when finding the discriminant
11887 field for a variant part. */
11888 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
11891 /* Compute the discriminant range for a given variant. OBSTACK is
11892 where the results will be stored. VARIANT is the variant to
11893 process. IS_UNSIGNED indicates whether the discriminant is signed
11896 static const gdb::array_view
<discriminant_range
>
11897 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
11900 std::vector
<discriminant_range
> ranges
;
11902 if (variant
.default_branch
)
11905 if (variant
.discr_list_data
== nullptr)
11907 discriminant_range r
11908 = {variant
.discriminant_value
, variant
.discriminant_value
};
11909 ranges
.push_back (r
);
11913 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
11914 variant
.discr_list_data
->size
);
11915 while (!data
.empty ())
11917 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
11919 complaint (_("invalid discriminant marker: %d"), data
[0]);
11922 bool is_range
= data
[0] == DW_DSC_range
;
11923 data
= data
.slice (1);
11925 ULONGEST low
, high
;
11926 unsigned int bytes_read
;
11930 complaint (_("DW_AT_discr_list missing low value"));
11934 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
11936 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
11938 data
= data
.slice (bytes_read
);
11944 complaint (_("DW_AT_discr_list missing high value"));
11948 high
= read_unsigned_leb128 (nullptr, data
.data (),
11951 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
11953 data
= data
.slice (bytes_read
);
11958 ranges
.push_back ({ low
, high
});
11962 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
11964 std::copy (ranges
.begin (), ranges
.end (), result
);
11965 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
11968 static const gdb::array_view
<variant_part
> create_variant_parts
11969 (struct obstack
*obstack
,
11970 const offset_map_type
&offset_map
,
11971 struct field_info
*fi
,
11972 const std::vector
<variant_part_builder
> &variant_parts
);
11974 /* Fill in a "struct variant" for a given variant field. RESULT is
11975 the variant to fill in. OBSTACK is where any needed allocations
11976 will be done. OFFSET_MAP holds the mapping from section offsets to
11977 fields for the type. FI describes the fields of the type we're
11978 processing. FIELD is the variant field we're converting. */
11981 create_one_variant (variant
&result
, struct obstack
*obstack
,
11982 const offset_map_type
&offset_map
,
11983 struct field_info
*fi
, const variant_field
&field
)
11985 result
.discriminants
= convert_variant_range (obstack
, field
, false);
11986 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
11987 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
11988 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
11989 field
.variant_parts
);
11992 /* Fill in a "struct variant_part" for a given variant part. RESULT
11993 is the variant part to fill in. OBSTACK is where any needed
11994 allocations will be done. OFFSET_MAP holds the mapping from
11995 section offsets to fields for the type. FI describes the fields of
11996 the type we're processing. BUILDER is the variant part to be
12000 create_one_variant_part (variant_part
&result
,
12001 struct obstack
*obstack
,
12002 const offset_map_type
&offset_map
,
12003 struct field_info
*fi
,
12004 const variant_part_builder
&builder
)
12006 auto iter
= offset_map
.find (builder
.discriminant_offset
);
12007 if (iter
== offset_map
.end ())
12009 result
.discriminant_index
= -1;
12010 /* Doesn't matter. */
12011 result
.is_unsigned
= false;
12015 result
.discriminant_index
= iter
->second
;
12017 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
12020 size_t n
= builder
.variants
.size ();
12021 variant
*output
= new (obstack
) variant
[n
];
12022 for (size_t i
= 0; i
< n
; ++i
)
12023 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
12024 builder
.variants
[i
]);
12026 result
.variants
= gdb::array_view
<variant
> (output
, n
);
12029 /* Create a vector of variant parts that can be attached to a type.
12030 OBSTACK is where any needed allocations will be done. OFFSET_MAP
12031 holds the mapping from section offsets to fields for the type. FI
12032 describes the fields of the type we're processing. VARIANT_PARTS
12033 is the vector to convert. */
12035 static const gdb::array_view
<variant_part
>
12036 create_variant_parts (struct obstack
*obstack
,
12037 const offset_map_type
&offset_map
,
12038 struct field_info
*fi
,
12039 const std::vector
<variant_part_builder
> &variant_parts
)
12041 if (variant_parts
.empty ())
12044 size_t n
= variant_parts
.size ();
12045 variant_part
*result
= new (obstack
) variant_part
[n
];
12046 for (size_t i
= 0; i
< n
; ++i
)
12047 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
12050 return gdb::array_view
<variant_part
> (result
, n
);
12053 /* Compute the variant part vector for FIP, attaching it to TYPE when
12057 add_variant_property (struct field_info
*fip
, struct type
*type
,
12058 struct dwarf2_cu
*cu
)
12060 /* Map section offsets of fields to their field index. Note the
12061 field index here does not take the number of baseclasses into
12063 offset_map_type offset_map
;
12064 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
12065 offset_map
[fip
->fields
[i
].offset
] = i
;
12067 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12068 gdb::array_view
<const variant_part
> parts
12069 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
12070 fip
->variant_parts
);
12072 struct dynamic_prop prop
;
12073 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
12074 obstack_copy (&objfile
->objfile_obstack
, &parts
,
12077 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
12080 /* Create the vector of fields, and attach it to the type. */
12083 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
12084 struct dwarf2_cu
*cu
)
12086 int nfields
= fip
->nfields ();
12088 /* Record the field count, allocate space for the array of fields,
12089 and create blank accessibility bitfields if necessary. */
12090 type
->alloc_fields (nfields
);
12092 if (!fip
->baseclasses
.empty () && cu
->lang () != language_ada
)
12094 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12095 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
12098 if (!fip
->variant_parts
.empty ())
12099 add_variant_property (fip
, type
, cu
);
12101 /* Copy the saved-up fields into the field vector. */
12102 for (int i
= 0; i
< nfields
; ++i
)
12104 struct nextfield
&field
12105 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
12106 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
12108 type
->field (i
) = field
.field
;
12112 /* Return true if this member function is a constructor, false
12116 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
12118 const char *fieldname
;
12119 const char *type_name
;
12122 if (die
->parent
== NULL
)
12125 if (die
->parent
->tag
!= DW_TAG_structure_type
12126 && die
->parent
->tag
!= DW_TAG_union_type
12127 && die
->parent
->tag
!= DW_TAG_class_type
)
12130 fieldname
= dwarf2_name (die
, cu
);
12131 type_name
= dwarf2_name (die
->parent
, cu
);
12132 if (fieldname
== NULL
|| type_name
== NULL
)
12135 len
= strlen (fieldname
);
12136 return (strncmp (fieldname
, type_name
, len
) == 0
12137 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
12140 /* Add a member function to the proper fieldlist. */
12143 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
12144 struct type
*type
, struct dwarf2_cu
*cu
)
12146 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12147 struct attribute
*attr
;
12149 struct fnfieldlist
*flp
= nullptr;
12150 struct fn_field
*fnp
;
12151 const char *fieldname
;
12152 struct type
*this_type
;
12154 if (cu
->lang () == language_ada
)
12155 error (_("unexpected member function in Ada type"));
12157 /* Get name of member function. */
12158 fieldname
= dwarf2_name (die
, cu
);
12159 if (fieldname
== NULL
)
12162 /* Look up member function name in fieldlist. */
12163 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
12165 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
12167 flp
= &fip
->fnfieldlists
[i
];
12172 /* Create a new fnfieldlist if necessary. */
12173 if (flp
== nullptr)
12175 fip
->fnfieldlists
.emplace_back ();
12176 flp
= &fip
->fnfieldlists
.back ();
12177 flp
->name
= fieldname
;
12178 i
= fip
->fnfieldlists
.size () - 1;
12181 /* Create a new member function field and add it to the vector of
12183 flp
->fnfields
.emplace_back ();
12184 fnp
= &flp
->fnfields
.back ();
12186 /* Delay processing of the physname until later. */
12187 if (cu
->lang () == language_cplus
)
12188 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
12192 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
12193 fnp
->physname
= physname
? physname
: "";
12196 fnp
->type
= type_allocator (objfile
, cu
->lang ()).new_type ();
12197 this_type
= read_type_die (die
, cu
);
12198 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
12200 int nparams
= this_type
->num_fields ();
12202 /* TYPE is the domain of this method, and THIS_TYPE is the type
12203 of the method itself (TYPE_CODE_METHOD). */
12204 smash_to_method_type (fnp
->type
, type
,
12205 this_type
->target_type (),
12206 this_type
->fields (),
12207 this_type
->num_fields (),
12208 this_type
->has_varargs ());
12210 /* Handle static member functions.
12211 Dwarf2 has no clean way to discern C++ static and non-static
12212 member functions. G++ helps GDB by marking the first
12213 parameter for non-static member functions (which is the this
12214 pointer) as artificial. We obtain this information from
12215 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12216 if (nparams
== 0 || this_type
->field (0).is_artificial () == 0)
12217 fnp
->voffset
= VOFFSET_STATIC
;
12220 complaint (_("member function type missing for '%s'"),
12221 dwarf2_full_name (fieldname
, die
, cu
));
12223 /* Get fcontext from DW_AT_containing_type if present. */
12224 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
12225 fnp
->fcontext
= die_containing_type (die
, cu
);
12227 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12228 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12230 /* Get accessibility. */
12231 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
12232 switch (accessibility
)
12234 case DW_ACCESS_private
:
12235 fnp
->accessibility
= accessibility::PRIVATE
;
12237 case DW_ACCESS_protected
:
12238 fnp
->accessibility
= accessibility::PROTECTED
;
12242 /* Check for artificial methods. */
12243 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
12244 if (attr
&& attr
->as_boolean ())
12245 fnp
->is_artificial
= 1;
12247 /* Check for defaulted methods. */
12248 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
12249 if (attr
!= nullptr)
12250 fnp
->defaulted
= attr
->defaulted ();
12252 /* Check for deleted methods. */
12253 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
12254 if (attr
!= nullptr && attr
->as_boolean ())
12255 fnp
->is_deleted
= 1;
12257 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
12259 /* Get index in virtual function table if it is a virtual member
12260 function. For older versions of GCC, this is an offset in the
12261 appropriate virtual table, as specified by DW_AT_containing_type.
12262 For everyone else, it is an expression to be evaluated relative
12263 to the object address. */
12265 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
12266 if (attr
!= nullptr)
12268 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
12270 struct dwarf_block
*block
= attr
->as_block ();
12273 if (block
->data
[0] == DW_OP_constu
12274 && decode_locdesc (block
, cu
, &offset
))
12276 /* "Old"-style GCC. See
12277 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44126
12278 for discussion. This was known and a patch available
12279 in 2010, but as of 2023, both GCC and clang still
12281 fnp
->voffset
= offset
+ 2;
12283 else if ((block
->data
[0] == DW_OP_deref
12284 || (block
->size
> 1
12285 && block
->data
[0] == DW_OP_deref_size
12286 && block
->data
[1] == cu
->header
.addr_size
))
12287 && decode_locdesc (block
, cu
, &offset
))
12289 fnp
->voffset
= offset
;
12290 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
12291 dwarf2_complex_location_expr_complaint ();
12293 fnp
->voffset
/= cu
->header
.addr_size
;
12297 dwarf2_complex_location_expr_complaint ();
12299 if (!fnp
->fcontext
)
12301 /* If there is no `this' field and no DW_AT_containing_type,
12302 we cannot actually find a base class context for the
12304 if (this_type
->num_fields () == 0
12305 || !this_type
->field (0).is_artificial ())
12307 complaint (_("cannot determine context for virtual member "
12308 "function \"%s\" (offset %s)"),
12309 fieldname
, sect_offset_str (die
->sect_off
));
12313 fnp
->fcontext
= this_type
->field (0).type ()->target_type ();
12317 else if (attr
->form_is_section_offset ())
12319 dwarf2_complex_location_expr_complaint ();
12323 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12329 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
12330 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
12332 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12333 complaint (_("Member function \"%s\" (offset %s) is virtual "
12334 "but the vtable offset is not specified"),
12335 fieldname
, sect_offset_str (die
->sect_off
));
12336 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12337 TYPE_CPLUS_DYNAMIC (type
) = 1;
12342 /* Create the vector of member function fields, and attach it to the type. */
12345 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
12346 struct dwarf2_cu
*cu
)
12348 if (cu
->lang () == language_ada
)
12349 error (_("unexpected member functions in Ada type"));
12351 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12352 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
12354 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
12356 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
12358 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
12359 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
12361 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
12362 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
12363 /* No need to zero-initialize, initialization is done by the copy in
12365 fn_flp
->fn_fields
= (struct fn_field
*)
12366 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
12368 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
12369 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
12372 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
12375 /* Returns non-zero if NAME is the name of a vtable member in CU's
12376 language, zero otherwise. */
12378 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
12380 static const char vptr
[] = "_vptr";
12382 /* Look for the C++ form of the vtable. */
12383 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
12389 /* GCC outputs unnamed structures that are really pointers to member
12390 functions, with the ABI-specified layout. If TYPE describes
12391 such a structure, smash it into a member function type.
12393 GCC shouldn't do this; it should just output pointer to member DIEs.
12394 This is GCC PR debug/28767. */
12397 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
12399 struct type
*pfn_type
, *self_type
, *new_type
;
12401 /* Check for a structure with no name and two children. */
12402 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
12405 /* Check for __pfn and __delta members. */
12406 if (type
->field (0).name () == NULL
12407 || strcmp (type
->field (0).name (), "__pfn") != 0
12408 || type
->field (1).name () == NULL
12409 || strcmp (type
->field (1).name (), "__delta") != 0)
12412 /* Find the type of the method. */
12413 pfn_type
= type
->field (0).type ();
12414 if (pfn_type
== NULL
12415 || pfn_type
->code () != TYPE_CODE_PTR
12416 || pfn_type
->target_type ()->code () != TYPE_CODE_FUNC
)
12419 /* Look for the "this" argument. */
12420 pfn_type
= pfn_type
->target_type ();
12421 if (pfn_type
->num_fields () == 0
12422 /* || pfn_type->field (0).type () == NULL */
12423 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
12426 self_type
= pfn_type
->field (0).type ()->target_type ();
12427 new_type
= type_allocator (type
).new_type ();
12428 smash_to_method_type (new_type
, self_type
, pfn_type
->target_type (),
12429 pfn_type
->fields (), pfn_type
->num_fields (),
12430 pfn_type
->has_varargs ());
12431 smash_to_methodptr_type (type
, new_type
);
12434 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12435 requires rewriting, then copy it and return the updated copy.
12436 Otherwise return nullptr. */
12438 static struct type
*
12439 rewrite_array_type (struct type
*type
)
12441 if (type
->code () != TYPE_CODE_ARRAY
)
12444 struct type
*index_type
= type
->index_type ();
12445 range_bounds
*current_bounds
= index_type
->bounds ();
12447 /* Handle multi-dimensional arrays. */
12448 struct type
*new_target
= rewrite_array_type (type
->target_type ());
12449 if (new_target
== nullptr)
12451 /* Maybe we don't need to rewrite this array. */
12452 if (current_bounds
->low
.is_constant ()
12453 && current_bounds
->high
.is_constant ())
12457 /* Either the target type was rewritten, or the bounds have to be
12458 updated. Either way we want to copy the type and update
12460 struct type
*copy
= copy_type (type
);
12461 copy
->copy_fields (type
);
12462 if (new_target
!= nullptr)
12463 copy
->set_target_type (new_target
);
12465 struct type
*index_copy
= copy_type (index_type
);
12466 range_bounds
*bounds
12467 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
12468 sizeof (range_bounds
));
12469 *bounds
= *current_bounds
;
12470 bounds
->low
.set_const_val (1);
12471 bounds
->high
.set_const_val (0);
12472 index_copy
->set_bounds (bounds
);
12473 copy
->set_index_type (index_copy
);
12478 /* While some versions of GCC will generate complicated DWARF for an
12479 array (see quirk_ada_thick_pointer), more recent versions were
12480 modified to emit an explicit thick pointer structure. However, in
12481 this case, the array still has DWARF expressions for its ranges,
12482 and these must be ignored. */
12485 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
12488 gdb_assert (cu
->lang () == language_ada
);
12490 /* Check for a structure with two children. */
12491 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
12494 /* Check for P_ARRAY and P_BOUNDS members. */
12495 if (type
->field (0).name () == NULL
12496 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
12497 || type
->field (1).name () == NULL
12498 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
12501 /* Make sure we're looking at a pointer to an array. */
12502 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
12505 /* The Ada code already knows how to handle these types, so all that
12506 we need to do is turn the bounds into static bounds. However, we
12507 don't want to rewrite existing array or index types in-place,
12508 because those may be referenced in other contexts where this
12509 rewriting is undesirable. */
12510 struct type
*new_ary_type
12511 = rewrite_array_type (type
->field (0).type ()->target_type ());
12512 if (new_ary_type
!= nullptr)
12513 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
12516 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12517 appropriate error checking and issuing complaints if there is a
12521 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
12523 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
12525 if (attr
== nullptr)
12528 if (!attr
->form_is_constant ())
12530 complaint (_("DW_AT_alignment must have constant form"
12531 " - DIE at %s [in module %s]"),
12532 sect_offset_str (die
->sect_off
),
12533 objfile_name (cu
->per_objfile
->objfile
));
12537 LONGEST val
= attr
->constant_value (0);
12540 complaint (_("DW_AT_alignment value must not be negative"
12541 " - DIE at %s [in module %s]"),
12542 sect_offset_str (die
->sect_off
),
12543 objfile_name (cu
->per_objfile
->objfile
));
12546 ULONGEST align
= val
;
12550 complaint (_("DW_AT_alignment value must not be zero"
12551 " - DIE at %s [in module %s]"),
12552 sect_offset_str (die
->sect_off
),
12553 objfile_name (cu
->per_objfile
->objfile
));
12556 if ((align
& (align
- 1)) != 0)
12558 complaint (_("DW_AT_alignment value must be a power of 2"
12559 " - DIE at %s [in module %s]"),
12560 sect_offset_str (die
->sect_off
),
12561 objfile_name (cu
->per_objfile
->objfile
));
12568 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12569 the alignment for TYPE. */
12572 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
12575 if (!set_type_align (type
, get_alignment (cu
, die
)))
12576 complaint (_("DW_AT_alignment value too large"
12577 " - DIE at %s [in module %s]"),
12578 sect_offset_str (die
->sect_off
),
12579 objfile_name (cu
->per_objfile
->objfile
));
12582 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12583 constant for a type, according to DWARF5 spec, Table 5.5. */
12586 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
12591 case DW_CC_pass_by_reference
:
12592 case DW_CC_pass_by_value
:
12596 complaint (_("unrecognized DW_AT_calling_convention value "
12597 "(%s) for a type"), pulongest (value
));
12602 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12603 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12604 also according to GNU-specific values (see include/dwarf2.h). */
12607 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
12612 case DW_CC_program
:
12616 case DW_CC_GNU_renesas_sh
:
12617 case DW_CC_GNU_borland_fastcall_i386
:
12618 case DW_CC_GDB_IBM_OpenCL
:
12622 complaint (_("unrecognized DW_AT_calling_convention value "
12623 "(%s) for a subroutine"), pulongest (value
));
12628 /* Called when we find the DIE that starts a structure or union scope
12629 (definition) to create a type for the structure or union. Fill in
12630 the type's name and general properties; the members will not be
12631 processed until process_structure_scope. A symbol table entry for
12632 the type will also not be done until process_structure_scope (assuming
12633 the type has a name).
12635 NOTE: we need to call these functions regardless of whether or not the
12636 DIE has a DW_AT_name attribute, since it might be an anonymous
12637 structure or union. This gets the type entered into our set of
12638 user defined types. */
12640 static struct type
*
12641 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12643 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12645 struct attribute
*attr
;
12648 /* If the definition of this type lives in .debug_types, read that type.
12649 Don't follow DW_AT_specification though, that will take us back up
12650 the chain and we want to go down. */
12651 attr
= die
->attr (DW_AT_signature
);
12652 if (attr
!= nullptr)
12654 type
= get_DW_AT_signature_type (die
, attr
, cu
);
12656 /* The type's CU may not be the same as CU.
12657 Ensure TYPE is recorded with CU in die_type_hash. */
12658 return set_die_type (die
, type
, cu
);
12661 type
= type_allocator (objfile
, cu
->lang ()).new_type ();
12662 INIT_CPLUS_SPECIFIC (type
);
12664 name
= dwarf2_name (die
, cu
);
12667 if (cu
->lang () == language_cplus
12668 || cu
->lang () == language_d
12669 || cu
->lang () == language_rust
)
12671 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
12673 /* dwarf2_full_name might have already finished building the DIE's
12674 type. If so, there is no need to continue. */
12675 if (get_die_type (die
, cu
) != NULL
)
12676 return get_die_type (die
, cu
);
12678 type
->set_name (full_name
);
12682 /* The name is already allocated along with this objfile, so
12683 we don't need to duplicate it for the type. */
12684 type
->set_name (name
);
12688 if (die
->tag
== DW_TAG_structure_type
)
12690 type
->set_code (TYPE_CODE_STRUCT
);
12692 else if (die
->tag
== DW_TAG_union_type
)
12694 type
->set_code (TYPE_CODE_UNION
);
12696 else if (die
->tag
== DW_TAG_namelist
)
12698 type
->set_code (TYPE_CODE_NAMELIST
);
12702 type
->set_code (TYPE_CODE_STRUCT
);
12705 if (cu
->lang () == language_cplus
&& die
->tag
== DW_TAG_class_type
)
12706 type
->set_is_declared_class (true);
12708 /* Store the calling convention in the type if it's available in
12709 the die. Otherwise the calling convention remains set to
12710 the default value DW_CC_normal. */
12711 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
12712 if (attr
!= nullptr
12713 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
12715 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12716 TYPE_CPLUS_CALLING_CONVENTION (type
)
12717 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
12720 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
12721 if (attr
!= nullptr)
12723 if (attr
->form_is_constant ())
12724 type
->set_length (attr
->constant_value (0));
12727 struct dynamic_prop prop
;
12728 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
12729 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
12731 type
->set_length (0);
12735 type
->set_length (0);
12737 maybe_set_alignment (cu
, die
, type
);
12739 if (producer_is_icc_lt_14 (cu
) && (type
->length () == 0))
12741 /* ICC<14 does not output the required DW_AT_declaration on
12742 incomplete types, but gives them a size of zero. */
12743 type
->set_is_stub (true);
12746 type
->set_stub_is_supported (true);
12748 if (die_is_declaration (die
, cu
))
12749 type
->set_is_stub (true);
12750 else if (attr
== NULL
&& die
->child
== NULL
12751 && producer_is_realview (cu
->producer
))
12752 /* RealView does not output the required DW_AT_declaration
12753 on incomplete types. */
12754 type
->set_is_stub (true);
12756 /* We need to add the type field to the die immediately so we don't
12757 infinitely recurse when dealing with pointers to the structure
12758 type within the structure itself. */
12759 set_die_type (die
, type
, cu
);
12761 /* set_die_type should be already done. */
12762 set_descriptive_type (type
, die
, cu
);
12767 static void handle_struct_member_die
12768 (struct die_info
*child_die
,
12770 struct field_info
*fi
,
12771 std::vector
<struct symbol
*> *template_args
,
12772 struct dwarf2_cu
*cu
);
12774 /* A helper for handle_struct_member_die that handles
12775 DW_TAG_variant_part. */
12778 handle_variant_part (struct die_info
*die
, struct type
*type
,
12779 struct field_info
*fi
,
12780 std::vector
<struct symbol
*> *template_args
,
12781 struct dwarf2_cu
*cu
)
12783 variant_part_builder
*new_part
;
12784 if (fi
->current_variant_part
== nullptr)
12786 fi
->variant_parts
.emplace_back ();
12787 new_part
= &fi
->variant_parts
.back ();
12789 else if (!fi
->current_variant_part
->processing_variant
)
12791 complaint (_("nested DW_TAG_variant_part seen "
12792 "- DIE at %s [in module %s]"),
12793 sect_offset_str (die
->sect_off
),
12794 objfile_name (cu
->per_objfile
->objfile
));
12799 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
12800 current
.variant_parts
.emplace_back ();
12801 new_part
= ¤t
.variant_parts
.back ();
12804 /* When we recurse, we want callees to add to this new variant
12806 scoped_restore save_current_variant_part
12807 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
12809 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
12812 /* It's a univariant form, an extension we support. */
12814 else if (discr
->form_is_ref ())
12816 struct dwarf2_cu
*target_cu
= cu
;
12817 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
12819 new_part
->discriminant_offset
= target_die
->sect_off
;
12823 complaint (_("DW_AT_discr does not have DIE reference form"
12824 " - DIE at %s [in module %s]"),
12825 sect_offset_str (die
->sect_off
),
12826 objfile_name (cu
->per_objfile
->objfile
));
12829 for (die_info
*child_die
= die
->child
;
12831 child_die
= child_die
->sibling
)
12832 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
12835 /* A helper for handle_struct_member_die that handles
12839 handle_variant (struct die_info
*die
, struct type
*type
,
12840 struct field_info
*fi
,
12841 std::vector
<struct symbol
*> *template_args
,
12842 struct dwarf2_cu
*cu
)
12844 if (fi
->current_variant_part
== nullptr)
12846 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12847 "- DIE at %s [in module %s]"),
12848 sect_offset_str (die
->sect_off
),
12849 objfile_name (cu
->per_objfile
->objfile
));
12852 if (fi
->current_variant_part
->processing_variant
)
12854 complaint (_("nested DW_TAG_variant seen "
12855 "- DIE at %s [in module %s]"),
12856 sect_offset_str (die
->sect_off
),
12857 objfile_name (cu
->per_objfile
->objfile
));
12861 scoped_restore save_processing_variant
12862 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
12865 fi
->current_variant_part
->variants
.emplace_back ();
12866 variant_field
&variant
= fi
->current_variant_part
->variants
.back ();
12867 variant
.first_field
= fi
->fields
.size ();
12869 /* In a variant we want to get the discriminant and also add a
12870 field for our sole member child. */
12871 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
12872 if (discr
== nullptr || !discr
->form_is_constant ())
12874 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
12875 if (discr
== nullptr || discr
->as_block ()->size
== 0)
12876 variant
.default_branch
= true;
12878 variant
.discr_list_data
= discr
->as_block ();
12881 variant
.discriminant_value
= discr
->constant_value (0);
12883 for (die_info
*variant_child
= die
->child
;
12884 variant_child
!= NULL
;
12885 variant_child
= variant_child
->sibling
)
12886 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
12888 variant
.last_field
= fi
->fields
.size ();
12891 /* A helper for process_structure_scope that handles a single member
12895 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
12896 struct field_info
*fi
,
12897 std::vector
<struct symbol
*> *template_args
,
12898 struct dwarf2_cu
*cu
)
12900 if (child_die
->tag
== DW_TAG_member
12901 || child_die
->tag
== DW_TAG_variable
12902 || child_die
->tag
== DW_TAG_namelist_item
)
12904 /* NOTE: carlton/2002-11-05: A C++ static data member
12905 should be a DW_TAG_member that is a declaration, but
12906 all versions of G++ as of this writing (so through at
12907 least 3.2.1) incorrectly generate DW_TAG_variable
12908 tags for them instead. */
12909 dwarf2_add_field (fi
, child_die
, cu
);
12911 else if (child_die
->tag
== DW_TAG_subprogram
)
12913 /* Rust doesn't have member functions in the C++ sense.
12914 However, it does emit ordinary functions as children
12915 of a struct DIE. */
12916 if (cu
->lang () == language_rust
)
12917 read_func_scope (child_die
, cu
);
12920 /* C++ member function. */
12921 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
12924 else if (child_die
->tag
== DW_TAG_inheritance
)
12926 /* C++ base class field. */
12927 dwarf2_add_field (fi
, child_die
, cu
);
12929 else if (type_can_define_types (child_die
))
12930 dwarf2_add_type_defn (fi
, child_die
, cu
);
12931 else if (child_die
->tag
== DW_TAG_template_type_param
12932 || child_die
->tag
== DW_TAG_template_value_param
)
12934 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12937 template_args
->push_back (arg
);
12939 else if (child_die
->tag
== DW_TAG_variant_part
)
12940 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
12941 else if (child_die
->tag
== DW_TAG_variant
)
12942 handle_variant (child_die
, type
, fi
, template_args
, cu
);
12945 /* Finish creating a structure or union type, including filling in its
12946 members and creating a symbol for it. This function also handles Fortran
12947 namelist variables, their items or members and creating a symbol for
12951 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12953 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12954 struct die_info
*child_die
;
12957 type
= get_die_type (die
, cu
);
12959 type
= read_structure_type (die
, cu
);
12961 bool has_template_parameters
= false;
12962 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
12964 struct field_info fi
;
12965 std::vector
<struct symbol
*> template_args
;
12967 child_die
= die
->child
;
12969 while (child_die
&& child_die
->tag
)
12971 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
12972 child_die
= child_die
->sibling
;
12975 /* Attach template arguments to type. */
12976 if (!template_args
.empty ())
12978 has_template_parameters
= true;
12979 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12980 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
12981 TYPE_TEMPLATE_ARGUMENTS (type
)
12982 = XOBNEWVEC (&objfile
->objfile_obstack
,
12984 TYPE_N_TEMPLATE_ARGUMENTS (type
));
12985 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
12986 template_args
.data (),
12987 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
12988 * sizeof (struct symbol
*)));
12991 /* Attach fields and member functions to the type. */
12992 if (fi
.nfields () > 0)
12993 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
12994 if (!fi
.fnfieldlists
.empty ())
12996 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
12998 /* Get the type which refers to the base class (possibly this
12999 class itself) which contains the vtable pointer for the current
13000 class from the DW_AT_containing_type attribute. This use of
13001 DW_AT_containing_type is a GNU extension. */
13003 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
13005 struct type
*t
= die_containing_type (die
, cu
);
13007 set_type_vptr_basetype (type
, t
);
13012 /* Our own class provides vtbl ptr. */
13013 for (i
= t
->num_fields () - 1;
13014 i
>= TYPE_N_BASECLASSES (t
);
13017 const char *fieldname
= t
->field (i
).name ();
13019 if (is_vtable_name (fieldname
, cu
))
13021 set_type_vptr_fieldno (type
, i
);
13026 /* Complain if virtual function table field not found. */
13027 if (i
< TYPE_N_BASECLASSES (t
))
13028 complaint (_("virtual function table pointer "
13029 "not found when defining class '%s'"),
13030 type
->name () ? type
->name () : "");
13034 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
13037 else if (cu
->producer
13038 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
13040 /* The IBM XLC compiler does not provide direct indication
13041 of the containing type, but the vtable pointer is
13042 always named __vfp. */
13046 for (i
= type
->num_fields () - 1;
13047 i
>= TYPE_N_BASECLASSES (type
);
13050 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
13052 set_type_vptr_fieldno (type
, i
);
13053 set_type_vptr_basetype (type
, type
);
13060 /* Copy fi.typedef_field_list linked list elements content into the
13061 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13062 if (!fi
.typedef_field_list
.empty ())
13064 int count
= fi
.typedef_field_list
.size ();
13066 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13067 /* No zero-initialization is needed, the elements are initialized by
13068 the copy in the loop below. */
13069 TYPE_TYPEDEF_FIELD_ARRAY (type
)
13070 = ((struct decl_field
*)
13072 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
13073 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
13075 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
13076 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
13079 /* Copy fi.nested_types_list linked list elements content into the
13080 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
13081 if (!fi
.nested_types_list
.empty ()
13082 && cu
->lang () != language_ada
)
13084 int count
= fi
.nested_types_list
.size ();
13086 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13087 /* No zero-initialization is needed, the elements are initialized by
13088 the copy in the loop below. */
13089 TYPE_NESTED_TYPES_ARRAY (type
)
13090 = ((struct decl_field
*)
13091 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
13092 TYPE_NESTED_TYPES_COUNT (type
) = count
;
13094 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
13095 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
13099 quirk_gcc_member_function_pointer (type
, objfile
);
13100 if (cu
->lang () == language_rust
&& die
->tag
== DW_TAG_union_type
)
13101 cu
->rust_unions
.push_back (type
);
13102 else if (cu
->lang () == language_ada
)
13103 quirk_ada_thick_pointer_struct (die
, cu
, type
);
13105 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13106 snapshots) has been known to create a die giving a declaration
13107 for a class that has, as a child, a die giving a definition for a
13108 nested class. So we have to process our children even if the
13109 current die is a declaration. Normally, of course, a declaration
13110 won't have any children at all. */
13112 child_die
= die
->child
;
13114 while (child_die
!= NULL
&& child_die
->tag
)
13116 if (child_die
->tag
== DW_TAG_member
13117 || child_die
->tag
== DW_TAG_variable
13118 || child_die
->tag
== DW_TAG_inheritance
13119 || child_die
->tag
== DW_TAG_template_value_param
13120 || child_die
->tag
== DW_TAG_template_type_param
)
13125 process_die (child_die
, cu
);
13127 child_die
= child_die
->sibling
;
13130 /* Do not consider external references. According to the DWARF standard,
13131 these DIEs are identified by the fact that they have no byte_size
13132 attribute, and a declaration attribute. */
13133 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
13134 || !die_is_declaration (die
, cu
)
13135 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
13137 struct symbol
*sym
= new_symbol (die
, type
, cu
);
13139 if (has_template_parameters
)
13141 struct symtab
*symtab
;
13142 if (sym
!= nullptr)
13143 symtab
= sym
->symtab ();
13144 else if (cu
->line_header
!= nullptr)
13146 /* Any related symtab will do. */
13148 = cu
->line_header
->file_names ()[0].symtab
;
13153 complaint (_("could not find suitable "
13154 "symtab for template parameter"
13155 " - DIE at %s [in module %s]"),
13156 sect_offset_str (die
->sect_off
),
13157 objfile_name (objfile
));
13160 if (symtab
!= nullptr)
13162 /* Make sure that the symtab is set on the new symbols.
13163 Even though they don't appear in this symtab directly,
13164 other parts of gdb assume that symbols do, and this is
13165 reasonably true. */
13166 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
13167 TYPE_TEMPLATE_ARGUMENT (type
, i
)->set_symtab (symtab
);
13173 /* Assuming DIE is an enumeration type, and TYPE is its associated
13174 type, update TYPE using some information only available in DIE's
13175 children. In particular, the fields are computed. */
13178 update_enumeration_type_from_children (struct die_info
*die
,
13180 struct dwarf2_cu
*cu
)
13182 struct die_info
*child_die
;
13183 int unsigned_enum
= 1;
13186 auto_obstack obstack
;
13187 std::vector
<struct field
> fields
;
13189 for (child_die
= die
->child
;
13190 child_die
!= NULL
&& child_die
->tag
;
13191 child_die
= child_die
->sibling
)
13193 struct attribute
*attr
;
13195 const gdb_byte
*bytes
;
13196 struct dwarf2_locexpr_baton
*baton
;
13199 if (child_die
->tag
!= DW_TAG_enumerator
)
13202 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
13206 name
= dwarf2_name (child_die
, cu
);
13208 name
= "<anonymous enumerator>";
13210 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
13211 &value
, &bytes
, &baton
);
13219 if (count_one_bits_ll (value
) >= 2)
13223 fields
.emplace_back ();
13224 struct field
&field
= fields
.back ();
13225 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
13226 field
.set_loc_enumval (value
);
13229 if (!fields
.empty ())
13230 type
->copy_fields (fields
);
13235 type
->set_is_unsigned (true);
13238 type
->set_is_flag_enum (true);
13241 /* Given a DW_AT_enumeration_type die, set its type. We do not
13242 complete the type's fields yet, or create any symbols. */
13244 static struct type
*
13245 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13247 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13249 struct attribute
*attr
;
13252 /* If the definition of this type lives in .debug_types, read that type.
13253 Don't follow DW_AT_specification though, that will take us back up
13254 the chain and we want to go down. */
13255 attr
= die
->attr (DW_AT_signature
);
13256 if (attr
!= nullptr)
13258 type
= get_DW_AT_signature_type (die
, attr
, cu
);
13260 /* The type's CU may not be the same as CU.
13261 Ensure TYPE is recorded with CU in die_type_hash. */
13262 return set_die_type (die
, type
, cu
);
13265 type
= type_allocator (objfile
, cu
->lang ()).new_type ();
13267 type
->set_code (TYPE_CODE_ENUM
);
13268 name
= dwarf2_full_name (NULL
, die
, cu
);
13270 type
->set_name (name
);
13272 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
13275 struct type
*underlying_type
= die_type (die
, cu
);
13277 type
->set_target_type (underlying_type
);
13280 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13281 if (attr
!= nullptr)
13282 type
->set_length (attr
->constant_value (0));
13284 type
->set_length (0);
13286 maybe_set_alignment (cu
, die
, type
);
13288 /* The enumeration DIE can be incomplete. In Ada, any type can be
13289 declared as private in the package spec, and then defined only
13290 inside the package body. Such types are known as Taft Amendment
13291 Types. When another package uses such a type, an incomplete DIE
13292 may be generated by the compiler. */
13293 if (die_is_declaration (die
, cu
))
13294 type
->set_is_stub (true);
13296 /* If this type has an underlying type that is not a stub, then we
13297 may use its attributes. We always use the "unsigned" attribute
13298 in this situation, because ordinarily we guess whether the type
13299 is unsigned -- but the guess can be wrong and the underlying type
13300 can tell us the reality. However, we defer to a local size
13301 attribute if one exists, because this lets the compiler override
13302 the underlying type if needed. */
13303 if (type
->target_type () != NULL
&& !type
->target_type ()->is_stub ())
13305 struct type
*underlying_type
= type
->target_type ();
13306 underlying_type
= check_typedef (underlying_type
);
13308 type
->set_is_unsigned (underlying_type
->is_unsigned ());
13310 if (type
->length () == 0)
13311 type
->set_length (underlying_type
->length ());
13313 if (TYPE_RAW_ALIGN (type
) == 0
13314 && TYPE_RAW_ALIGN (underlying_type
) != 0)
13315 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
13318 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
13320 set_die_type (die
, type
, cu
);
13322 /* Finish the creation of this type by using the enum's children.
13323 Note that, as usual, this must come after set_die_type to avoid
13324 infinite recursion when trying to compute the names of the
13326 update_enumeration_type_from_children (die
, type
, cu
);
13331 /* Given a pointer to a die which begins an enumeration, process all
13332 the dies that define the members of the enumeration, and create the
13333 symbol for the enumeration type.
13335 NOTE: We reverse the order of the element list. */
13338 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
13340 struct type
*this_type
;
13342 this_type
= get_die_type (die
, cu
);
13343 if (this_type
== NULL
)
13344 this_type
= read_enumeration_type (die
, cu
);
13346 if (die
->child
!= NULL
)
13348 struct die_info
*child_die
;
13351 child_die
= die
->child
;
13352 while (child_die
&& child_die
->tag
)
13354 if (child_die
->tag
!= DW_TAG_enumerator
)
13356 process_die (child_die
, cu
);
13360 name
= dwarf2_name (child_die
, cu
);
13362 new_symbol (child_die
, this_type
, cu
);
13365 child_die
= child_die
->sibling
;
13369 /* If we are reading an enum from a .debug_types unit, and the enum
13370 is a declaration, and the enum is not the signatured type in the
13371 unit, then we do not want to add a symbol for it. Adding a
13372 symbol would in some cases obscure the true definition of the
13373 enum, giving users an incomplete type when the definition is
13374 actually available. Note that we do not want to do this for all
13375 enums which are just declarations, because C++0x allows forward
13376 enum declarations. */
13377 if (cu
->per_cu
->is_debug_types
13378 && die_is_declaration (die
, cu
))
13380 struct signatured_type
*sig_type
;
13382 sig_type
= (struct signatured_type
*) cu
->per_cu
;
13383 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
13384 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
13388 new_symbol (die
, this_type
, cu
);
13391 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13392 expression for an index type and finds the corresponding field
13393 offset in the hidden "P_BOUNDS" structure. Returns true on success
13394 and updates *FIELD, false if it fails to recognize an
13398 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
13399 int *bounds_offset
, struct field
*field
,
13400 struct dwarf2_cu
*cu
)
13402 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
13403 if (attr
== nullptr || !attr
->form_is_block ())
13406 const struct dwarf_block
*block
= attr
->as_block ();
13407 const gdb_byte
*start
= block
->data
;
13408 const gdb_byte
*end
= block
->data
+ block
->size
;
13410 /* The expression to recognize generally looks like:
13412 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13413 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13415 However, the second "plus_uconst" may be missing:
13417 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13418 DW_OP_deref_size: 4)
13420 This happens when the field is at the start of the structure.
13422 Also, the final deref may not be sized:
13424 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13427 This happens when the size of the index type happens to be the
13428 same as the architecture's word size. This can occur with or
13429 without the second plus_uconst. */
13431 if (end
- start
< 2)
13433 if (*start
++ != DW_OP_push_object_address
)
13435 if (*start
++ != DW_OP_plus_uconst
)
13438 uint64_t this_bound_off
;
13439 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
13440 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
13442 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13443 is consistent among all bounds. */
13444 if (*bounds_offset
== -1)
13445 *bounds_offset
= this_bound_off
;
13446 else if (*bounds_offset
!= this_bound_off
)
13449 if (start
== end
|| *start
++ != DW_OP_deref
)
13455 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
13457 /* This means an offset of 0. */
13459 else if (*start
++ != DW_OP_plus_uconst
)
13463 /* The size is the parameter to DW_OP_plus_uconst. */
13465 start
= gdb_read_uleb128 (start
, end
, &val
);
13466 if (start
== nullptr)
13468 if ((int) val
!= val
)
13477 if (*start
== DW_OP_deref_size
)
13479 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
13480 if (start
== nullptr)
13483 else if (*start
== DW_OP_deref
)
13485 size
= cu
->header
.addr_size
;
13491 field
->set_loc_bitpos (8 * offset
);
13492 if (size
!= field
->type ()->length ())
13493 field
->set_bitsize (8 * size
);
13498 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13499 some kinds of Ada arrays:
13501 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13502 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13503 <11e0> DW_AT_data_location: 2 byte block: 97 6
13504 (DW_OP_push_object_address; DW_OP_deref)
13505 <11e3> DW_AT_type : <0x1173>
13506 <11e7> DW_AT_sibling : <0x1201>
13507 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13508 <11ec> DW_AT_type : <0x1206>
13509 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13510 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13511 DW_OP_deref_size: 4)
13512 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13513 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13514 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13516 This actually represents a "thick pointer", which is a structure
13517 with two elements: one that is a pointer to the array data, and one
13518 that is a pointer to another structure; this second structure holds
13521 This returns a new type on success, or nullptr if this didn't
13522 recognize the type. */
13524 static struct type
*
13525 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
13528 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
13529 /* So far we've only seen this with block form. */
13530 if (attr
== nullptr || !attr
->form_is_block ())
13533 /* Note that this will fail if the structure layout is changed by
13534 the compiler. However, we have no good way to recognize some
13535 other layout, because we don't know what expression the compiler
13536 might choose to emit should this happen. */
13537 struct dwarf_block
*blk
= attr
->as_block ();
13539 || blk
->data
[0] != DW_OP_push_object_address
13540 || blk
->data
[1] != DW_OP_deref
)
13543 int bounds_offset
= -1;
13544 int max_align
= -1;
13545 std::vector
<struct field
> range_fields
;
13546 for (struct die_info
*child_die
= die
->child
;
13548 child_die
= child_die
->sibling
)
13550 if (child_die
->tag
== DW_TAG_subrange_type
)
13552 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
13554 int this_align
= type_align (underlying
);
13555 if (this_align
> max_align
)
13556 max_align
= this_align
;
13558 range_fields
.emplace_back ();
13559 range_fields
.emplace_back ();
13561 struct field
&lower
= range_fields
[range_fields
.size () - 2];
13562 struct field
&upper
= range_fields
[range_fields
.size () - 1];
13564 lower
.set_type (underlying
);
13565 lower
.set_is_artificial (true);
13567 upper
.set_type (underlying
);
13568 upper
.set_is_artificial (true);
13570 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
13571 &bounds_offset
, &lower
, cu
)
13572 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
13573 &bounds_offset
, &upper
, cu
))
13578 /* This shouldn't really happen, but double-check that we found
13579 where the bounds are stored. */
13580 if (bounds_offset
== -1)
13583 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13584 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
13588 /* Set the name of each field in the bounds. */
13589 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
13590 range_fields
[i
].set_name (objfile
->intern (name
));
13591 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
13592 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
13595 type_allocator
alloc (objfile
, cu
->lang ());
13596 struct type
*bounds
= alloc
.new_type ();
13597 bounds
->set_code (TYPE_CODE_STRUCT
);
13599 bounds
->copy_fields (range_fields
);
13601 int last_fieldno
= range_fields
.size () - 1;
13602 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
13603 + bounds
->field (last_fieldno
).type ()->length ());
13604 bounds
->set_length (align_up (bounds_size
, max_align
));
13606 /* Rewrite the existing array type in place. Specifically, we
13607 remove any dynamic properties we might have read, and we replace
13608 the index types. */
13609 struct type
*iter
= type
;
13610 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
13612 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
13613 iter
->main_type
->dyn_prop_list
= nullptr;
13614 iter
->set_index_type
13615 (create_static_range_type (alloc
, bounds
->field (i
).type (), 1, 0));
13616 iter
= iter
->target_type ();
13619 struct type
*result
= type_allocator (objfile
, cu
->lang ()).new_type ();
13620 result
->set_code (TYPE_CODE_STRUCT
);
13622 result
->alloc_fields (2);
13624 /* The names are chosen to coincide with what the compiler does with
13625 -fgnat-encodings=all, which the Ada code in gdb already
13627 result
->field (0).set_name ("P_ARRAY");
13628 result
->field (0).set_type (lookup_pointer_type (type
));
13630 result
->field (1).set_name ("P_BOUNDS");
13631 result
->field (1).set_type (lookup_pointer_type (bounds
));
13632 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
13634 result
->set_name (type
->name ());
13635 result
->set_length (result
->field (0).type ()->length ()
13636 + result
->field (1).type ()->length ());
13641 /* Extract all information from a DW_TAG_array_type DIE and put it in
13642 the DIE's type field. For now, this only handles one dimensional
13645 static struct type
*
13646 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13648 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13649 struct die_info
*child_die
;
13651 struct type
*element_type
, *range_type
, *index_type
;
13652 struct attribute
*attr
;
13654 struct dynamic_prop
*byte_stride_prop
= NULL
;
13655 unsigned int bit_stride
= 0;
13657 element_type
= die_type (die
, cu
);
13659 /* The die_type call above may have already set the type for this DIE. */
13660 type
= get_die_type (die
, cu
);
13664 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
13668 struct type
*prop_type
= cu
->addr_sized_int_type (false);
13671 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
13672 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
13676 complaint (_("unable to read array DW_AT_byte_stride "
13677 " - DIE at %s [in module %s]"),
13678 sect_offset_str (die
->sect_off
),
13679 objfile_name (cu
->per_objfile
->objfile
));
13680 /* Ignore this attribute. We will likely not be able to print
13681 arrays of this type correctly, but there is little we can do
13682 to help if we cannot read the attribute's value. */
13683 byte_stride_prop
= NULL
;
13687 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
13689 bit_stride
= attr
->constant_value (0);
13691 /* Irix 6.2 native cc creates array types without children for
13692 arrays with unspecified length. */
13693 if (die
->child
== NULL
)
13695 index_type
= builtin_type (objfile
)->builtin_int
;
13696 type_allocator
alloc (objfile
, cu
->lang ());
13697 range_type
= create_static_range_type (alloc
, index_type
, 0, -1);
13698 type
= create_array_type_with_stride (alloc
, element_type
, range_type
,
13699 byte_stride_prop
, bit_stride
);
13700 return set_die_type (die
, type
, cu
);
13703 std::vector
<struct type
*> range_types
;
13704 child_die
= die
->child
;
13705 while (child_die
&& child_die
->tag
)
13707 if (child_die
->tag
== DW_TAG_subrange_type
13708 || child_die
->tag
== DW_TAG_generic_subrange
)
13710 struct type
*child_type
= read_type_die (child_die
, cu
);
13712 if (child_type
!= NULL
)
13714 /* The range type was successfully read. Save it for the
13715 array type creation. */
13716 range_types
.push_back (child_type
);
13719 child_die
= child_die
->sibling
;
13722 if (range_types
.empty ())
13724 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13725 sect_offset_str (die
->sect_off
),
13726 objfile_name (cu
->per_objfile
->objfile
));
13730 /* Dwarf2 dimensions are output from left to right, create the
13731 necessary array types in backwards order. */
13733 type
= element_type
;
13735 type_allocator
alloc (cu
->per_objfile
->objfile
, cu
->lang ());
13736 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
13740 while (i
< range_types
.size ())
13742 type
= create_array_type_with_stride (alloc
, type
, range_types
[i
++],
13743 byte_stride_prop
, bit_stride
);
13744 type
->set_is_multi_dimensional (true);
13746 byte_stride_prop
= nullptr;
13751 size_t ndim
= range_types
.size ();
13754 type
= create_array_type_with_stride (alloc
, type
, range_types
[ndim
],
13755 byte_stride_prop
, bit_stride
);
13756 type
->set_is_multi_dimensional (true);
13758 byte_stride_prop
= nullptr;
13762 /* Clear the flag on the outermost array type. */
13763 type
->set_is_multi_dimensional (false);
13764 gdb_assert (type
!= element_type
);
13766 /* Understand Dwarf2 support for vector types (like they occur on
13767 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13768 array type. This is not part of the Dwarf2/3 standard yet, but a
13769 custom vendor extension. The main difference between a regular
13770 array and the vector variant is that vectors are passed by value
13772 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
13773 if (attr
!= nullptr)
13774 make_vector_type (type
);
13776 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13777 implementation may choose to implement triple vectors using this
13779 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13780 if (attr
!= nullptr && attr
->form_is_unsigned ())
13782 if (attr
->as_unsigned () >= type
->length ())
13783 type
->set_length (attr
->as_unsigned ());
13785 complaint (_("DW_AT_byte_size for array type smaller "
13786 "than the total size of elements"));
13789 name
= dwarf2_name (die
, cu
);
13791 type
->set_name (name
);
13793 maybe_set_alignment (cu
, die
, type
);
13795 struct type
*replacement_type
= nullptr;
13796 if (cu
->lang () == language_ada
)
13798 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
13799 if (replacement_type
!= nullptr)
13800 type
= replacement_type
;
13803 /* Install the type in the die. */
13804 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
13806 /* set_die_type should be already done. */
13807 set_descriptive_type (type
, die
, cu
);
13812 static enum dwarf_array_dim_ordering
13813 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
13815 struct attribute
*attr
;
13817 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
13819 if (attr
!= nullptr)
13821 LONGEST val
= attr
->constant_value (-1);
13822 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
13823 return (enum dwarf_array_dim_ordering
) val
;
13826 /* GNU F77 is a special case, as at 08/2004 array type info is the
13827 opposite order to the dwarf2 specification, but data is still
13828 laid out as per normal fortran.
13830 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13831 version checking. */
13833 if (cu
->lang () == language_fortran
13834 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
13836 return DW_ORD_row_major
;
13839 switch (cu
->language_defn
->array_ordering ())
13841 case array_column_major
:
13842 return DW_ORD_col_major
;
13843 case array_row_major
:
13845 return DW_ORD_row_major
;
13849 /* Extract all information from a DW_TAG_set_type DIE and put it in
13850 the DIE's type field. */
13852 static struct type
*
13853 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13855 struct type
*domain_type
, *set_type
;
13856 struct attribute
*attr
;
13858 domain_type
= die_type (die
, cu
);
13860 /* The die_type call above may have already set the type for this DIE. */
13861 set_type
= get_die_type (die
, cu
);
13865 type_allocator
alloc (cu
->per_objfile
->objfile
, cu
->lang ());
13866 set_type
= create_set_type (alloc
, domain_type
);
13868 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13869 if (attr
!= nullptr && attr
->form_is_unsigned ())
13870 set_type
->set_length (attr
->as_unsigned ());
13872 maybe_set_alignment (cu
, die
, set_type
);
13874 return set_die_type (die
, set_type
, cu
);
13877 /* A helper for read_common_block that creates a locexpr baton.
13878 SYM is the symbol which we are marking as computed.
13879 COMMON_DIE is the DIE for the common block.
13880 COMMON_LOC is the location expression attribute for the common
13882 MEMBER_LOC is the location expression attribute for the particular
13883 member of the common block that we are processing.
13884 CU is the CU from which the above come. */
13887 mark_common_block_symbol_computed (struct symbol
*sym
,
13888 struct die_info
*common_die
,
13889 struct attribute
*common_loc
,
13890 struct attribute
*member_loc
,
13891 struct dwarf2_cu
*cu
)
13893 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13894 struct objfile
*objfile
= per_objfile
->objfile
;
13895 struct dwarf2_locexpr_baton
*baton
;
13897 unsigned int cu_off
;
13898 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
13899 LONGEST offset
= 0;
13901 gdb_assert (common_loc
&& member_loc
);
13902 gdb_assert (common_loc
->form_is_block ());
13903 gdb_assert (member_loc
->form_is_block ()
13904 || member_loc
->form_is_constant ());
13906 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
13907 baton
->per_objfile
= per_objfile
;
13908 baton
->per_cu
= cu
->per_cu
;
13909 gdb_assert (baton
->per_cu
);
13911 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13913 if (member_loc
->form_is_constant ())
13915 offset
= member_loc
->constant_value (0);
13916 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
13919 baton
->size
+= member_loc
->as_block ()->size
;
13921 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
13924 *ptr
++ = DW_OP_call4
;
13925 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
13926 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
13929 if (member_loc
->form_is_constant ())
13931 *ptr
++ = DW_OP_addr
;
13932 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
13933 ptr
+= cu
->header
.addr_size
;
13937 /* We have to copy the data here, because DW_OP_call4 will only
13938 use a DW_AT_location attribute. */
13939 struct dwarf_block
*block
= member_loc
->as_block ();
13940 memcpy (ptr
, block
->data
, block
->size
);
13941 ptr
+= block
->size
;
13944 *ptr
++ = DW_OP_plus
;
13945 gdb_assert (ptr
- baton
->data
== baton
->size
);
13947 SYMBOL_LOCATION_BATON (sym
) = baton
;
13948 sym
->set_aclass_index (dwarf2_locexpr_index
);
13951 /* Create appropriate locally-scoped variables for all the
13952 DW_TAG_common_block entries. Also create a struct common_block
13953 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13954 is used to separate the common blocks name namespace from regular
13958 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
13960 struct attribute
*attr
;
13962 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
13963 if (attr
!= nullptr)
13965 /* Support the .debug_loc offsets. */
13966 if (attr
->form_is_block ())
13970 else if (attr
->form_is_section_offset ())
13972 dwarf2_complex_location_expr_complaint ();
13977 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13978 "common block member");
13983 if (die
->child
!= NULL
)
13985 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13986 struct die_info
*child_die
;
13987 size_t n_entries
= 0, size
;
13988 struct common_block
*common_block
;
13989 struct symbol
*sym
;
13991 for (child_die
= die
->child
;
13992 child_die
&& child_die
->tag
;
13993 child_die
= child_die
->sibling
)
13996 size
= (sizeof (struct common_block
)
13997 + (n_entries
- 1) * sizeof (struct symbol
*));
13999 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
14001 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
14002 common_block
->n_entries
= 0;
14004 for (child_die
= die
->child
;
14005 child_die
&& child_die
->tag
;
14006 child_die
= child_die
->sibling
)
14008 /* Create the symbol in the DW_TAG_common_block block in the current
14010 sym
= new_symbol (child_die
, NULL
, cu
);
14013 struct attribute
*member_loc
;
14015 common_block
->contents
[common_block
->n_entries
++] = sym
;
14017 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
14021 /* GDB has handled this for a long time, but it is
14022 not specified by DWARF. It seems to have been
14023 emitted by gfortran at least as recently as:
14024 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14025 complaint (_("Variable in common block has "
14026 "DW_AT_data_member_location "
14027 "- DIE at %s [in module %s]"),
14028 sect_offset_str (child_die
->sect_off
),
14029 objfile_name (objfile
));
14031 if (member_loc
->form_is_section_offset ())
14032 dwarf2_complex_location_expr_complaint ();
14033 else if (member_loc
->form_is_constant ()
14034 || member_loc
->form_is_block ())
14036 if (attr
!= nullptr)
14037 mark_common_block_symbol_computed (sym
, die
, attr
,
14041 dwarf2_complex_location_expr_complaint ();
14046 sym
= new_symbol (die
, builtin_type (objfile
)->builtin_void
, cu
);
14047 sym
->set_value_common_block (common_block
);
14051 /* Create a type for a C++ namespace. */
14053 static struct type
*
14054 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14056 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14057 const char *previous_prefix
, *name
;
14061 /* For extensions, reuse the type of the original namespace. */
14062 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
14064 struct die_info
*ext_die
;
14065 struct dwarf2_cu
*ext_cu
= cu
;
14067 ext_die
= dwarf2_extension (die
, &ext_cu
);
14068 type
= read_type_die (ext_die
, ext_cu
);
14070 /* EXT_CU may not be the same as CU.
14071 Ensure TYPE is recorded with CU in die_type_hash. */
14072 return set_die_type (die
, type
, cu
);
14075 name
= namespace_name (die
, &is_anonymous
, cu
);
14077 /* Now build the name of the current namespace. */
14079 previous_prefix
= determine_prefix (die
, cu
);
14080 if (previous_prefix
[0] != '\0')
14081 name
= typename_concat (&objfile
->objfile_obstack
,
14082 previous_prefix
, name
, 0, cu
);
14084 /* Create the type. */
14085 type
= type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_NAMESPACE
,
14088 return set_die_type (die
, type
, cu
);
14091 /* Read a namespace scope. */
14094 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
14096 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14099 /* Add a symbol associated to this if we haven't seen the namespace
14100 before. Also, add a using directive if it's an anonymous
14103 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
14107 type
= read_type_die (die
, cu
);
14108 new_symbol (die
, type
, cu
);
14110 namespace_name (die
, &is_anonymous
, cu
);
14113 const char *previous_prefix
= determine_prefix (die
, cu
);
14115 std::vector
<const char *> excludes
;
14116 add_using_directive (using_directives (cu
),
14117 previous_prefix
, type
->name (), NULL
,
14119 read_decl_line (die
, cu
),
14120 0, &objfile
->objfile_obstack
);
14124 if (die
->child
!= NULL
)
14126 struct die_info
*child_die
= die
->child
;
14128 while (child_die
&& child_die
->tag
)
14130 process_die (child_die
, cu
);
14131 child_die
= child_die
->sibling
;
14136 /* Read a Fortran module as type. This DIE can be only a declaration used for
14137 imported module. Still we need that type as local Fortran "use ... only"
14138 declaration imports depend on the created type in determine_prefix. */
14140 static struct type
*
14141 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14143 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14144 const char *module_name
;
14147 module_name
= dwarf2_name (die
, cu
);
14148 type
= type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_MODULE
,
14151 return set_die_type (die
, type
, cu
);
14154 /* Read a Fortran module. */
14157 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
14159 struct die_info
*child_die
= die
->child
;
14162 type
= read_type_die (die
, cu
);
14163 new_symbol (die
, type
, cu
);
14165 while (child_die
&& child_die
->tag
)
14167 process_die (child_die
, cu
);
14168 child_die
= child_die
->sibling
;
14172 /* Return the name of the namespace represented by DIE. Set
14173 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14176 static const char *
14177 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
14179 struct die_info
*current_die
;
14180 const char *name
= NULL
;
14182 /* Loop through the extensions until we find a name. */
14184 for (current_die
= die
;
14185 current_die
!= NULL
;
14186 current_die
= dwarf2_extension (die
, &cu
))
14188 /* We don't use dwarf2_name here so that we can detect the absence
14189 of a name -> anonymous namespace. */
14190 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
14196 /* Is it an anonymous namespace? */
14198 *is_anonymous
= (name
== NULL
);
14200 name
= CP_ANONYMOUS_NAMESPACE_STR
;
14205 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14206 the user defined type vector. */
14208 static struct type
*
14209 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14211 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
14212 struct comp_unit_head
*cu_header
= &cu
->header
;
14214 struct attribute
*attr_byte_size
;
14215 struct attribute
*attr_address_class
;
14216 int byte_size
, addr_class
;
14217 struct type
*target_type
;
14219 target_type
= die_type (die
, cu
);
14221 /* The die_type call above may have already set the type for this DIE. */
14222 type
= get_die_type (die
, cu
);
14226 type
= lookup_pointer_type (target_type
);
14228 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14229 if (attr_byte_size
)
14230 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
14232 byte_size
= cu_header
->addr_size
;
14234 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
14235 if (attr_address_class
)
14236 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
14238 addr_class
= DW_ADDR_none
;
14240 ULONGEST alignment
= get_alignment (cu
, die
);
14242 /* If the pointer size, alignment, or address class is different
14243 than the default, create a type variant marked as such and set
14244 the length accordingly. */
14245 if (type
->length () != byte_size
14246 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
14247 && alignment
!= TYPE_RAW_ALIGN (type
))
14248 || addr_class
!= DW_ADDR_none
)
14250 if (gdbarch_address_class_type_flags_p (gdbarch
))
14252 type_instance_flags type_flags
14253 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
14255 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
14257 type
= make_type_with_address_space (type
, type_flags
);
14259 else if (type
->length () != byte_size
)
14261 complaint (_("invalid pointer size %d"), byte_size
);
14263 else if (TYPE_RAW_ALIGN (type
) != alignment
)
14265 complaint (_("Invalid DW_AT_alignment"
14266 " - DIE at %s [in module %s]"),
14267 sect_offset_str (die
->sect_off
),
14268 objfile_name (cu
->per_objfile
->objfile
));
14272 /* Should we also complain about unhandled address classes? */
14276 type
->set_length (byte_size
);
14277 set_type_align (type
, alignment
);
14278 return set_die_type (die
, type
, cu
);
14281 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14282 the user defined type vector. */
14284 static struct type
*
14285 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14288 struct type
*to_type
;
14289 struct type
*domain
;
14291 to_type
= die_type (die
, cu
);
14292 domain
= die_containing_type (die
, cu
);
14294 /* The calls above may have already set the type for this DIE. */
14295 type
= get_die_type (die
, cu
);
14299 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
14300 type
= lookup_methodptr_type (to_type
);
14301 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
14303 struct type
*new_type
14304 = type_allocator (cu
->per_objfile
->objfile
, cu
->lang ()).new_type ();
14306 smash_to_method_type (new_type
, domain
, to_type
->target_type (),
14307 to_type
->fields (), to_type
->num_fields (),
14308 to_type
->has_varargs ());
14309 type
= lookup_methodptr_type (new_type
);
14312 type
= lookup_memberptr_type (to_type
, domain
);
14314 return set_die_type (die
, type
, cu
);
14317 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14318 the user defined type vector. */
14320 static struct type
*
14321 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
14322 enum type_code refcode
)
14324 struct comp_unit_head
*cu_header
= &cu
->header
;
14325 struct type
*type
, *target_type
;
14326 struct attribute
*attr
;
14328 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
14330 target_type
= die_type (die
, cu
);
14332 /* The die_type call above may have already set the type for this DIE. */
14333 type
= get_die_type (die
, cu
);
14337 type
= lookup_reference_type (target_type
, refcode
);
14338 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14339 if (attr
!= nullptr)
14340 type
->set_length (attr
->constant_value (cu_header
->addr_size
));
14342 type
->set_length (cu_header
->addr_size
);
14344 maybe_set_alignment (cu
, die
, type
);
14345 return set_die_type (die
, type
, cu
);
14348 /* Add the given cv-qualifiers to the element type of the array. GCC
14349 outputs DWARF type qualifiers that apply to an array, not the
14350 element type. But GDB relies on the array element type to carry
14351 the cv-qualifiers. This mimics section 6.7.3 of the C99
14354 static struct type
*
14355 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
14356 struct type
*base_type
, int cnst
, int voltl
)
14358 struct type
*el_type
, *inner_array
;
14360 base_type
= copy_type (base_type
);
14361 inner_array
= base_type
;
14363 while (inner_array
->target_type ()->code () == TYPE_CODE_ARRAY
)
14365 inner_array
->set_target_type (copy_type (inner_array
->target_type ()));
14366 inner_array
= inner_array
->target_type ();
14369 el_type
= inner_array
->target_type ();
14370 cnst
|= TYPE_CONST (el_type
);
14371 voltl
|= TYPE_VOLATILE (el_type
);
14372 inner_array
->set_target_type (make_cv_type (cnst
, voltl
, el_type
, NULL
));
14374 return set_die_type (die
, base_type
, cu
);
14377 static struct type
*
14378 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14380 struct type
*base_type
, *cv_type
;
14382 base_type
= die_type (die
, cu
);
14384 /* The die_type call above may have already set the type for this DIE. */
14385 cv_type
= get_die_type (die
, cu
);
14389 /* In case the const qualifier is applied to an array type, the element type
14390 is so qualified, not the array type (section 6.7.3 of C99). */
14391 if (base_type
->code () == TYPE_CODE_ARRAY
)
14392 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
14394 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
14395 return set_die_type (die
, cv_type
, cu
);
14398 static struct type
*
14399 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14401 struct type
*base_type
, *cv_type
;
14403 base_type
= die_type (die
, cu
);
14405 /* The die_type call above may have already set the type for this DIE. */
14406 cv_type
= get_die_type (die
, cu
);
14410 /* In case the volatile qualifier is applied to an array type, the
14411 element type is so qualified, not the array type (section 6.7.3
14413 if (base_type
->code () == TYPE_CODE_ARRAY
)
14414 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
14416 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
14417 return set_die_type (die
, cv_type
, cu
);
14420 /* Handle DW_TAG_restrict_type. */
14422 static struct type
*
14423 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14425 struct type
*base_type
, *cv_type
;
14427 base_type
= die_type (die
, cu
);
14429 /* The die_type call above may have already set the type for this DIE. */
14430 cv_type
= get_die_type (die
, cu
);
14434 cv_type
= make_restrict_type (base_type
);
14435 return set_die_type (die
, cv_type
, cu
);
14438 /* Handle DW_TAG_atomic_type. */
14440 static struct type
*
14441 read_tag_atomic_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14443 struct type
*base_type
, *cv_type
;
14445 base_type
= die_type (die
, cu
);
14447 /* The die_type call above may have already set the type for this DIE. */
14448 cv_type
= get_die_type (die
, cu
);
14452 cv_type
= make_atomic_type (base_type
);
14453 return set_die_type (die
, cv_type
, cu
);
14456 /* Extract all information from a DW_TAG_string_type DIE and add to
14457 the user defined type vector. It isn't really a user defined type,
14458 but it behaves like one, with other DIE's using an AT_user_def_type
14459 attribute to reference it. */
14461 static struct type
*
14462 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14464 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14465 struct gdbarch
*gdbarch
= objfile
->arch ();
14466 struct type
*type
, *range_type
, *index_type
, *char_type
;
14467 struct attribute
*attr
;
14468 struct dynamic_prop prop
;
14469 bool length_is_constant
= true;
14472 /* There are a couple of places where bit sizes might be made use of
14473 when parsing a DW_TAG_string_type, however, no producer that we know
14474 of make use of these. Handling bit sizes that are a multiple of the
14475 byte size is easy enough, but what about other bit sizes? Lets deal
14476 with that problem when we have to. Warn about these attributes being
14477 unsupported, then parse the type and ignore them like we always
14479 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
14480 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
14482 static bool warning_printed
= false;
14483 if (!warning_printed
)
14485 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14486 "currently supported on DW_TAG_string_type."));
14487 warning_printed
= true;
14491 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
14492 if (attr
!= nullptr && !attr
->form_is_constant ())
14494 /* The string length describes the location at which the length of
14495 the string can be found. The size of the length field can be
14496 specified with one of the attributes below. */
14497 struct type
*prop_type
;
14498 struct attribute
*len
14499 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
14500 if (len
== nullptr)
14501 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14502 if (len
!= nullptr && len
->form_is_constant ())
14504 /* Pass 0 as the default as we know this attribute is constant
14505 and the default value will not be returned. */
14506 LONGEST sz
= len
->constant_value (0);
14507 prop_type
= objfile_int_type (objfile
, sz
, true);
14511 /* If the size is not specified then we assume it is the size of
14512 an address on this target. */
14513 prop_type
= cu
->addr_sized_int_type (true);
14516 /* Convert the attribute into a dynamic property. */
14517 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
14520 length_is_constant
= false;
14522 else if (attr
!= nullptr)
14524 /* This DW_AT_string_length just contains the length with no
14525 indirection. There's no need to create a dynamic property in this
14526 case. Pass 0 for the default value as we know it will not be
14527 returned in this case. */
14528 length
= attr
->constant_value (0);
14530 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
14532 /* We don't currently support non-constant byte sizes for strings. */
14533 length
= attr
->constant_value (1);
14537 /* Use 1 as a fallback length if we have nothing else. */
14541 index_type
= builtin_type (objfile
)->builtin_int
;
14542 type_allocator
alloc (objfile
, cu
->lang ());
14543 if (length_is_constant
)
14544 range_type
= create_static_range_type (alloc
, index_type
, 1, length
);
14547 struct dynamic_prop low_bound
;
14549 low_bound
.set_const_val (1);
14550 range_type
= create_range_type (alloc
, index_type
, &low_bound
, &prop
, 0);
14552 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
14553 type
= create_string_type (alloc
, char_type
, range_type
);
14555 return set_die_type (die
, type
, cu
);
14558 /* Assuming that DIE corresponds to a function, returns nonzero
14559 if the function is prototyped. */
14562 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
14564 struct attribute
*attr
;
14566 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
14567 if (attr
&& attr
->as_boolean ())
14570 /* The DWARF standard implies that the DW_AT_prototyped attribute
14571 is only meaningful for C, but the concept also extends to other
14572 languages that allow unprototyped functions (Eg: Objective C).
14573 For all other languages, assume that functions are always
14575 if (cu
->lang () != language_c
14576 && cu
->lang () != language_objc
14577 && cu
->lang () != language_opencl
)
14580 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14581 prototyped and unprototyped functions; default to prototyped,
14582 since that is more common in modern code (and RealView warns
14583 about unprototyped functions). */
14584 if (producer_is_realview (cu
->producer
))
14590 /* Handle DIES due to C code like:
14594 int (*funcp)(int a, long l);
14598 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14600 static struct type
*
14601 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14603 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14604 struct type
*type
; /* Type that this function returns. */
14605 struct type
*ftype
; /* Function that returns above type. */
14606 struct attribute
*attr
;
14608 type
= die_type (die
, cu
);
14610 if (type
->code () == TYPE_CODE_VOID
14611 && !type
->is_stub ()
14612 && die
->child
== nullptr
14613 && producer_is_gas_2_39 (cu
))
14615 /* Work around PR gas/29517, pretend we have an DW_TAG_unspecified_type
14617 type
= (type_allocator (cu
->per_objfile
->objfile
, cu
->lang ())
14618 .new_type (TYPE_CODE_VOID
, 0, nullptr));
14619 type
->set_is_stub (true);
14622 /* The die_type call above may have already set the type for this DIE. */
14623 ftype
= get_die_type (die
, cu
);
14627 ftype
= lookup_function_type (type
);
14629 if (prototyped_function_p (die
, cu
))
14630 ftype
->set_is_prototyped (true);
14632 /* Store the calling convention in the type if it's available in
14633 the subroutine die. Otherwise set the calling convention to
14634 the default value DW_CC_normal. */
14635 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14636 if (attr
!= nullptr
14637 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
14638 TYPE_CALLING_CONVENTION (ftype
)
14639 = (enum dwarf_calling_convention
) attr
->constant_value (0);
14640 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
14641 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
14643 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
14645 /* Record whether the function returns normally to its caller or not
14646 if the DWARF producer set that information. */
14647 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
14648 if (attr
&& attr
->as_boolean ())
14649 TYPE_NO_RETURN (ftype
) = 1;
14651 /* We need to add the subroutine type to the die immediately so
14652 we don't infinitely recurse when dealing with parameters
14653 declared as the same subroutine type. */
14654 set_die_type (die
, ftype
, cu
);
14656 if (die
->child
!= NULL
)
14658 struct type
*void_type
= builtin_type (objfile
)->builtin_void
;
14659 struct die_info
*child_die
;
14660 int nparams
, iparams
;
14662 /* Count the number of parameters.
14663 FIXME: GDB currently ignores vararg functions, but knows about
14664 vararg member functions. */
14666 child_die
= die
->child
;
14667 while (child_die
&& child_die
->tag
)
14669 if (child_die
->tag
== DW_TAG_formal_parameter
)
14671 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
14672 ftype
->set_has_varargs (true);
14674 child_die
= child_die
->sibling
;
14677 /* Allocate storage for parameters and fill them in. */
14678 ftype
->alloc_fields (nparams
);
14680 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14681 even if we error out during the parameters reading below. */
14682 for (iparams
= 0; iparams
< nparams
; iparams
++)
14683 ftype
->field (iparams
).set_type (void_type
);
14686 child_die
= die
->child
;
14687 while (child_die
&& child_die
->tag
)
14689 if (child_die
->tag
== DW_TAG_formal_parameter
)
14691 struct type
*arg_type
;
14693 /* DWARF version 2 has no clean way to discern C++
14694 static and non-static member functions. G++ helps
14695 GDB by marking the first parameter for non-static
14696 member functions (which is the this pointer) as
14697 artificial. We pass this information to
14698 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14700 DWARF version 3 added DW_AT_object_pointer, which GCC
14701 4.5 does not yet generate. */
14702 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
14703 if (attr
!= nullptr)
14704 ftype
->field (iparams
).set_is_artificial (attr
->as_boolean ());
14706 ftype
->field (iparams
).set_is_artificial (false);
14707 arg_type
= die_type (child_die
, cu
);
14709 /* RealView does not mark THIS as const, which the testsuite
14710 expects. GCC marks THIS as const in method definitions,
14711 but not in the class specifications (GCC PR 43053). */
14712 if (cu
->lang () == language_cplus
14713 && !TYPE_CONST (arg_type
)
14714 && ftype
->field (iparams
).is_artificial ())
14717 struct dwarf2_cu
*arg_cu
= cu
;
14718 const char *name
= dwarf2_name (child_die
, cu
);
14720 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
14721 if (attr
!= nullptr)
14723 /* If the compiler emits this, use it. */
14724 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
14727 else if (name
&& strcmp (name
, "this") == 0)
14728 /* Function definitions will have the argument names. */
14730 else if (name
== NULL
&& iparams
== 0)
14731 /* Declarations may not have the names, so like
14732 elsewhere in GDB, assume an artificial first
14733 argument is "this". */
14737 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
14741 ftype
->field (iparams
).set_type (arg_type
);
14744 child_die
= child_die
->sibling
;
14751 static struct type
*
14752 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
14754 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14755 const char *name
= dwarf2_full_name (NULL
, die
, cu
);
14756 struct type
*this_type
;
14757 struct gdbarch
*gdbarch
= objfile
->arch ();
14758 struct type
*target_type
= die_type (die
, cu
);
14760 if (gdbarch_dwarf2_omit_typedef_p (gdbarch
, target_type
, cu
->producer
, name
))
14762 /* The long double is defined as a base type in C. GCC creates a long
14763 double typedef with target-type _Float128 for the long double to
14764 identify it as the IEEE Float128 value. This is a GCC hack since the
14765 DWARF doesn't distinguish between the IBM long double and IEEE
14766 128-bit float. Replace the GCC workaround for the long double
14767 typedef with the actual type information copied from the target-type
14768 with the correct long double base type name. */
14769 this_type
= copy_type (target_type
);
14770 this_type
->set_name (name
);
14771 set_die_type (die
, this_type
, cu
);
14775 type_allocator
alloc (objfile
, cu
->lang ());
14776 this_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
, 0, name
);
14777 this_type
->set_target_is_stub (true);
14778 set_die_type (die
, this_type
, cu
);
14779 if (target_type
!= this_type
)
14780 this_type
->set_target_type (target_type
);
14783 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14784 spec and cause infinite loops in GDB. */
14785 complaint (_("Self-referential DW_TAG_typedef "
14786 "- DIE at %s [in module %s]"),
14787 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
14788 this_type
->set_target_type (nullptr);
14792 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14793 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14794 Handle these by just returning the target type, rather than
14795 constructing an anonymous typedef type and trying to handle this
14797 set_die_type (die
, target_type
, cu
);
14798 return target_type
;
14803 /* Helper for get_dwarf2_rational_constant that computes the value of
14804 a given gmp_mpz given an attribute. */
14807 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
14809 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14810 location expression that pushes an implicit value. */
14811 if (attr
->form
== DW_FORM_exprloc
)
14813 dwarf_block
*blk
= attr
->as_block ();
14814 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
14817 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
14818 blk
->data
+ blk
->size
,
14820 if (ptr
- blk
->data
+ len
<= blk
->size
)
14822 value
->read (gdb::make_array_view (ptr
, len
),
14823 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
.get ())
14824 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
,
14830 /* On failure set it to 1. */
14831 *value
= gdb_mpz (1);
14833 else if (attr
->form_is_block ())
14835 dwarf_block
*blk
= attr
->as_block ();
14836 value
->read (gdb::make_array_view (blk
->data
, blk
->size
),
14837 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
.get ())
14838 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
,
14841 else if (attr
->form_is_unsigned ())
14842 *value
= gdb_mpz (attr
->as_unsigned ());
14844 *value
= gdb_mpz (attr
->constant_value (1));
14847 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14848 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14850 If the numerator and/or numerator attribute is missing,
14851 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14855 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
14856 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
14858 struct attribute
*num_attr
, *denom_attr
;
14860 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
14861 if (num_attr
== nullptr)
14862 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14863 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
14865 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
14866 if (denom_attr
== nullptr)
14867 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14868 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
14870 if (num_attr
== nullptr || denom_attr
== nullptr)
14873 get_mpz (cu
, numerator
, num_attr
);
14874 get_mpz (cu
, denominator
, denom_attr
);
14877 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14878 rational constant, rather than a signed one.
14880 If the rational constant has a negative value, a complaint
14881 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14884 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
14885 struct dwarf2_cu
*cu
,
14886 gdb_mpz
*numerator
,
14887 gdb_mpz
*denominator
)
14892 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
14893 if (num
< 0 && denom
< 0)
14900 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14902 sect_offset_str (die
->sect_off
));
14905 else if (denom
< 0)
14907 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14909 sect_offset_str (die
->sect_off
));
14913 *numerator
= std::move (num
);
14914 *denominator
= std::move (denom
);
14917 /* Assuming that ENCODING is a string whose contents starting at the
14918 K'th character is "_nn" where "nn" is a decimal number, scan that
14919 number and set RESULT to the value. K is updated to point to the
14920 character immediately following the number.
14922 If the string does not conform to the format described above, false
14923 is returned, and K may or may not be changed. */
14926 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
14928 /* The next character should be an underscore ('_') followed
14930 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
14933 /* Skip the underscore. */
14937 /* Determine the number of digits for our number. */
14938 while (isdigit (encoding
[k
]))
14943 std::string
copy (&encoding
[start
], k
- start
);
14944 return result
->set (copy
.c_str (), 10);
14947 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14948 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14949 DENOM, update OFFSET, and return true on success. Return false on
14953 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
14954 gdb_mpz
*num
, gdb_mpz
*denom
)
14956 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
14958 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
14961 /* Assuming DIE corresponds to a fixed point type, finish the creation
14962 of the corresponding TYPE by setting its type-specific data. CU is
14963 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14964 encodings. It is nullptr if the GNAT encoding should be
14968 finish_fixed_point_type (struct type
*type
, const char *suffix
,
14969 struct die_info
*die
, struct dwarf2_cu
*cu
)
14971 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
14972 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
14974 /* If GNAT encodings are preferred, don't examine the
14976 struct attribute
*attr
= nullptr;
14977 if (suffix
== nullptr)
14979 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
14980 if (attr
== nullptr)
14981 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
14982 if (attr
== nullptr)
14983 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
14986 /* Numerator and denominator of our fixed-point type's scaling factor.
14987 The default is a scaling factor of 1, which we use as a fallback
14988 when we are not able to decode it (problem with the debugging info,
14989 unsupported forms, bug in GDB, etc...). Using that as the default
14990 allows us to at least print the unscaled value, which might still
14991 be useful to a user. */
14992 gdb_mpz
scale_num (1);
14993 gdb_mpz
scale_denom (1);
14995 if (attr
== nullptr)
14998 if (suffix
!= nullptr
14999 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
15001 /* The number might be encoded as _nn_dd_nn_dd, where the
15002 second ratio is the 'small value. In this situation, we
15003 want the second value. */
15004 && (suffix
[offset
] != '_'
15005 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
15012 /* Scaling factor not found. Assume a scaling factor of 1,
15013 and hope for the best. At least the user will be able to
15014 see the encoded value. */
15017 complaint (_("no scale found for fixed-point type (DIE at %s)"),
15018 sect_offset_str (die
->sect_off
));
15021 else if (attr
->name
== DW_AT_binary_scale
)
15023 LONGEST scale_exp
= attr
->constant_value (0);
15024 gdb_mpz
&num_or_denom
= scale_exp
> 0 ? scale_num
: scale_denom
;
15026 num_or_denom
<<= std::abs (scale_exp
);
15028 else if (attr
->name
== DW_AT_decimal_scale
)
15030 LONGEST scale_exp
= attr
->constant_value (0);
15031 gdb_mpz
&num_or_denom
= scale_exp
> 0 ? scale_num
: scale_denom
;
15033 num_or_denom
= gdb_mpz::pow (10, std::abs (scale_exp
));
15035 else if (attr
->name
== DW_AT_small
)
15037 struct die_info
*scale_die
;
15038 struct dwarf2_cu
*scale_cu
= cu
;
15040 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
15041 if (scale_die
->tag
== DW_TAG_constant
)
15042 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
15043 &scale_num
, &scale_denom
);
15045 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
15047 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
15051 complaint (_("unsupported scale attribute %s for fixed-point type"
15053 dwarf_attr_name (attr
->name
),
15054 sect_offset_str (die
->sect_off
));
15057 type
->fixed_point_info ().scaling_factor
= gdb_mpq (scale_num
, scale_denom
);
15060 /* The gnat-encoding suffix for fixed point. */
15062 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
15064 /* If NAME encodes an Ada fixed-point type, return a pointer to the
15065 "XF" suffix of the name. The text after this is what encodes the
15066 'small and 'delta information. Otherwise, return nullptr. */
15068 static const char *
15069 gnat_encoded_fixed_point_type_info (const char *name
)
15071 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
15074 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15075 (which may be different from NAME) to the architecture back-end to allow
15076 it to guess the correct format if necessary. */
15078 static struct type
*
15079 dwarf2_init_float_type (struct dwarf2_cu
*cu
, int bits
, const char *name
,
15080 const char *name_hint
, enum bfd_endian byte_order
)
15082 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15083 struct gdbarch
*gdbarch
= objfile
->arch ();
15084 const struct floatformat
**format
;
15087 type_allocator
alloc (objfile
, cu
->lang ());
15088 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
15090 type
= init_float_type (alloc
, bits
, name
, format
, byte_order
);
15092 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
15097 /* Allocate an integer type of size BITS and name NAME. */
15099 static struct type
*
15100 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, int bits
, int unsigned_p
,
15104 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15106 /* Versions of Intel's C Compiler generate an integer type called "void"
15107 instead of using DW_TAG_unspecified_type. This has been seen on
15108 at least versions 14, 17, and 18. */
15109 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
15110 && strcmp (name
, "void") == 0)
15111 type
= builtin_type (objfile
)->builtin_void
;
15114 type_allocator
alloc (objfile
, cu
->lang ());
15115 type
= init_integer_type (alloc
, bits
, unsigned_p
, name
);
15121 /* Return true if DIE has a DW_AT_small attribute whose value is
15122 a constant rational, where both the numerator and denominator
15125 CU is the DIE's Compilation Unit. */
15128 has_zero_over_zero_small_attribute (struct die_info
*die
,
15129 struct dwarf2_cu
*cu
)
15131 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
15132 if (attr
== nullptr)
15135 struct dwarf2_cu
*scale_cu
= cu
;
15136 struct die_info
*scale_die
15137 = follow_die_ref (die
, attr
, &scale_cu
);
15139 if (scale_die
->tag
!= DW_TAG_constant
)
15142 gdb_mpz
num (1), denom (1);
15143 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
15144 return num
== 0 && denom
== 0;
15147 /* Initialise and return a floating point type of size BITS suitable for
15148 use as a component of a complex number. The NAME_HINT is passed through
15149 when initialising the floating point type and is the name of the complex
15152 As DWARF doesn't currently provide an explicit name for the components
15153 of a complex number, but it can be helpful to have these components
15154 named, we try to select a suitable name based on the size of the
15156 static struct type
*
15157 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
15158 int bits
, const char *name_hint
,
15159 enum bfd_endian byte_order
)
15161 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15162 gdbarch
*gdbarch
= objfile
->arch ();
15163 struct type
*tt
= nullptr;
15165 /* Try to find a suitable floating point builtin type of size BITS.
15166 We're going to use the name of this type as the name for the complex
15167 target type that we are about to create. */
15168 switch (cu
->lang ())
15170 case language_fortran
:
15174 tt
= builtin_f_type (gdbarch
)->builtin_real
;
15177 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
15179 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15181 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
15189 tt
= builtin_type (gdbarch
)->builtin_float
;
15192 tt
= builtin_type (gdbarch
)->builtin_double
;
15194 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15196 tt
= builtin_type (gdbarch
)->builtin_long_double
;
15202 /* If the type we found doesn't match the size we were looking for, then
15203 pretend we didn't find a type at all, the complex target type we
15204 create will then be nameless. */
15205 if (tt
!= nullptr && tt
->length () * TARGET_CHAR_BIT
!= bits
)
15208 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
15209 return dwarf2_init_float_type (cu
, bits
, name
, name_hint
, byte_order
);
15212 /* Find a representation of a given base type and install
15213 it in the TYPE field of the die. */
15215 static struct type
*
15216 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15218 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15220 struct attribute
*attr
;
15221 int encoding
= 0, bits
= 0;
15225 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
15226 if (attr
!= nullptr && attr
->form_is_constant ())
15227 encoding
= attr
->constant_value (0);
15228 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15229 if (attr
!= nullptr)
15230 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
15231 name
= dwarf2_name (die
, cu
);
15233 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15235 arch
= objfile
->arch ();
15236 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
15238 attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
15239 if (attr
!= nullptr && attr
->form_is_constant ())
15241 int endianity
= attr
->constant_value (0);
15246 byte_order
= BFD_ENDIAN_BIG
;
15248 case DW_END_little
:
15249 byte_order
= BFD_ENDIAN_LITTLE
;
15252 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
15257 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
15258 && cu
->lang () == language_ada
15259 && has_zero_over_zero_small_attribute (die
, cu
))
15261 /* brobecker/2018-02-24: This is a fixed point type for which
15262 the scaling factor is represented as fraction whose value
15263 does not make sense (zero divided by zero), so we should
15264 normally never see these. However, there is a small category
15265 of fixed point types for which GNAT is unable to provide
15266 the scaling factor via the standard DWARF mechanisms, and
15267 for which the info is provided via the GNAT encodings instead.
15268 This is likely what this DIE is about. */
15269 encoding
= (encoding
== DW_ATE_signed_fixed
15271 : DW_ATE_unsigned
);
15274 /* With GNAT encodings, fixed-point information will be encoded in
15275 the type name. Note that this can also occur with the above
15276 zero-over-zero case, which is why this is a separate "if" rather
15277 than an "else if". */
15278 const char *gnat_encoding_suffix
= nullptr;
15279 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
15280 && cu
->lang () == language_ada
15281 && name
!= nullptr)
15283 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
15284 if (gnat_encoding_suffix
!= nullptr)
15286 gdb_assert (startswith (gnat_encoding_suffix
,
15287 GNAT_FIXED_POINT_SUFFIX
));
15288 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
15289 name
, gnat_encoding_suffix
- name
);
15290 /* Use -1 here so that SUFFIX points at the "_" after the
15292 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
15294 encoding
= (encoding
== DW_ATE_signed
15295 ? DW_ATE_signed_fixed
15296 : DW_ATE_unsigned_fixed
);
15300 type_allocator
alloc (objfile
, cu
->lang ());
15303 case DW_ATE_address
:
15304 /* Turn DW_ATE_address into a void * pointer. */
15305 type
= alloc
.new_type (TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
15306 type
= init_pointer_type (alloc
, bits
, name
, type
);
15308 case DW_ATE_boolean
:
15309 type
= init_boolean_type (alloc
, bits
, 1, name
);
15311 case DW_ATE_complex_float
:
15312 type
= dwarf2_init_complex_target_type (cu
, bits
/ 2, name
,
15314 if (type
->code () == TYPE_CODE_ERROR
)
15316 if (name
== nullptr)
15318 struct obstack
*obstack
15319 = &cu
->per_objfile
->objfile
->objfile_obstack
;
15320 name
= obconcat (obstack
, "_Complex ", type
->name (),
15323 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
15326 type
= init_complex_type (name
, type
);
15328 case DW_ATE_decimal_float
:
15329 type
= init_decfloat_type (alloc
, bits
, name
);
15332 type
= dwarf2_init_float_type (cu
, bits
, name
, name
, byte_order
);
15334 case DW_ATE_signed
:
15335 type
= dwarf2_init_integer_type (cu
, bits
, 0, name
);
15337 case DW_ATE_unsigned
:
15338 if (cu
->lang () == language_fortran
15340 && startswith (name
, "character("))
15341 type
= init_character_type (alloc
, bits
, 1, name
);
15343 type
= dwarf2_init_integer_type (cu
, bits
, 1, name
);
15345 case DW_ATE_signed_char
:
15346 if (cu
->lang () == language_ada
15347 || cu
->lang () == language_m2
15348 || cu
->lang () == language_pascal
15349 || cu
->lang () == language_fortran
)
15350 type
= init_character_type (alloc
, bits
, 0, name
);
15352 type
= dwarf2_init_integer_type (cu
, bits
, 0, name
);
15354 case DW_ATE_unsigned_char
:
15355 if (cu
->lang () == language_ada
15356 || cu
->lang () == language_m2
15357 || cu
->lang () == language_pascal
15358 || cu
->lang () == language_fortran
15359 || cu
->lang () == language_rust
)
15360 type
= init_character_type (alloc
, bits
, 1, name
);
15362 type
= dwarf2_init_integer_type (cu
, bits
, 1, name
);
15366 type
= init_character_type (alloc
, bits
, 1, name
);
15367 return set_die_type (die
, type
, cu
);
15370 case DW_ATE_signed_fixed
:
15371 type
= init_fixed_point_type (alloc
, bits
, 0, name
);
15372 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
15374 case DW_ATE_unsigned_fixed
:
15375 type
= init_fixed_point_type (alloc
, bits
, 1, name
);
15376 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
15380 complaint (_("unsupported DW_AT_encoding: '%s'"),
15381 dwarf_type_encoding_name (encoding
));
15382 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
15386 if (type
->code () == TYPE_CODE_INT
15388 && strcmp (name
, "char") == 0)
15389 type
->set_has_no_signedness (true);
15391 maybe_set_alignment (cu
, die
, type
);
15393 type
->set_endianity_is_not_default (gdbarch_byte_order (arch
) != byte_order
);
15395 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
15397 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
15398 if (attr
!= nullptr && attr
->form_is_constant ())
15400 unsigned real_bit_size
= attr
->constant_value (0);
15401 if (real_bit_size
>= 0 && real_bit_size
<= 8 * type
->length ())
15403 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
15404 /* Only use the attributes if they make sense together. */
15405 if (attr
== nullptr
15406 || (attr
->form_is_constant ()
15407 && attr
->constant_value (0) >= 0
15408 && (attr
->constant_value (0) + real_bit_size
15409 <= 8 * type
->length ())))
15411 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
15413 if (attr
!= nullptr)
15414 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
15415 = attr
->constant_value (0);
15421 return set_die_type (die
, type
, cu
);
15424 /* A helper function that returns the name of DIE, if it refers to a
15425 variable declaration. */
15427 static const char *
15428 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
15430 if (die
->tag
!= DW_TAG_variable
)
15433 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
15434 if (attr
== nullptr || !attr
->as_boolean ())
15437 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
15438 if (attr
== nullptr)
15440 return attr
->as_string ();
15443 /* Parse dwarf attribute if it's a block, reference or constant and put the
15444 resulting value of the attribute into struct bound_prop.
15445 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15448 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
15449 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
15450 struct type
*default_type
)
15452 struct dwarf2_property_baton
*baton
;
15453 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15454 struct objfile
*objfile
= per_objfile
->objfile
;
15455 struct obstack
*obstack
= &objfile
->objfile_obstack
;
15457 gdb_assert (default_type
!= NULL
);
15459 if (attr
== NULL
|| prop
== NULL
)
15462 if (attr
->form_is_block ())
15464 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15465 baton
->property_type
= default_type
;
15466 baton
->locexpr
.per_cu
= cu
->per_cu
;
15467 baton
->locexpr
.per_objfile
= per_objfile
;
15469 struct dwarf_block
*block
;
15470 if (attr
->form
== DW_FORM_data16
)
15472 size_t data_size
= 16;
15473 block
= XOBNEW (obstack
, struct dwarf_block
);
15474 block
->size
= (data_size
15475 + 2 /* Extra bytes for DW_OP and arg. */);
15476 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
->size
);
15477 data
[0] = DW_OP_implicit_value
;
15478 data
[1] = data_size
;
15479 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
15480 block
->data
= data
;
15483 block
= attr
->as_block ();
15485 baton
->locexpr
.size
= block
->size
;
15486 baton
->locexpr
.data
= block
->data
;
15487 switch (attr
->name
)
15489 case DW_AT_string_length
:
15490 baton
->locexpr
.is_reference
= true;
15493 baton
->locexpr
.is_reference
= false;
15497 prop
->set_locexpr (baton
);
15498 gdb_assert (prop
->baton () != NULL
);
15500 else if (attr
->form_is_ref ())
15502 struct dwarf2_cu
*target_cu
= cu
;
15503 struct die_info
*target_die
;
15504 struct attribute
*target_attr
;
15506 target_die
= follow_die_ref (die
, attr
, &target_cu
);
15507 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
15508 if (target_attr
== NULL
)
15509 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
15511 if (target_attr
== nullptr)
15512 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
15514 if (target_attr
== NULL
)
15516 const char *name
= var_decl_name (target_die
, target_cu
);
15517 if (name
!= nullptr)
15519 prop
->set_variable_name (name
);
15525 switch (target_attr
->name
)
15527 case DW_AT_location
:
15528 if (target_attr
->form_is_section_offset ())
15530 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15531 baton
->property_type
= die_type (target_die
, target_cu
);
15532 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
15533 prop
->set_loclist (baton
);
15534 gdb_assert (prop
->baton () != NULL
);
15536 else if (target_attr
->form_is_block ())
15538 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15539 baton
->property_type
= die_type (target_die
, target_cu
);
15540 baton
->locexpr
.per_cu
= cu
->per_cu
;
15541 baton
->locexpr
.per_objfile
= per_objfile
;
15542 struct dwarf_block
*block
= target_attr
->as_block ();
15543 baton
->locexpr
.size
= block
->size
;
15544 baton
->locexpr
.data
= block
->data
;
15545 baton
->locexpr
.is_reference
= true;
15546 prop
->set_locexpr (baton
);
15547 gdb_assert (prop
->baton () != NULL
);
15551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15552 "dynamic property");
15556 case DW_AT_data_member_location
:
15557 case DW_AT_data_bit_offset
:
15561 if (!handle_member_location (target_die
, target_cu
, &offset
))
15564 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15565 baton
->property_type
= read_type_die (target_die
->parent
,
15567 baton
->offset_info
.offset
= offset
;
15568 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
15569 prop
->set_addr_offset (baton
);
15574 else if (attr
->form_is_constant ())
15575 prop
->set_const_val (attr
->constant_value (0));
15576 else if (attr
->form_is_section_offset ())
15578 switch (attr
->name
)
15580 case DW_AT_string_length
:
15581 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15582 baton
->property_type
= default_type
;
15583 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
15584 prop
->set_loclist (baton
);
15585 gdb_assert (prop
->baton () != NULL
);
15597 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
15598 dwarf2_name (die
, cu
));
15604 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15605 present (which is valid) then compute the default type based on the
15606 compilation units address size. */
15608 static struct type
*
15609 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15611 struct type
*index_type
= die_type (die
, cu
);
15613 /* Dwarf-2 specifications explicitly allows to create subrange types
15614 without specifying a base type.
15615 In that case, the base type must be set to the type of
15616 the lower bound, upper bound or count, in that order, if any of these
15617 three attributes references an object that has a type.
15618 If no base type is found, the Dwarf-2 specifications say that
15619 a signed integer type of size equal to the size of an address should
15621 For the following C code: `extern char gdb_int [];'
15622 GCC produces an empty range DIE.
15623 FIXME: muller/2010-05-28: Possible references to object for low bound,
15624 high bound or count are not yet handled by this code. */
15625 if (index_type
->code () == TYPE_CODE_VOID
)
15626 index_type
= cu
->addr_sized_int_type (false);
15631 /* Read the given DW_AT_subrange DIE. */
15633 static struct type
*
15634 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15636 struct type
*base_type
, *orig_base_type
;
15637 struct type
*range_type
;
15638 struct attribute
*attr
;
15639 struct dynamic_prop low
, high
;
15640 int low_default_is_valid
;
15641 int high_bound_is_count
= 0;
15644 orig_base_type
= read_subrange_index_type (die
, cu
);
15646 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15647 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15648 creating the range type, but we use the result of check_typedef
15649 when examining properties of the type. */
15650 base_type
= check_typedef (orig_base_type
);
15652 /* The die_type call above may have already set the type for this DIE. */
15653 range_type
= get_die_type (die
, cu
);
15657 high
.set_const_val (0);
15659 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15660 omitting DW_AT_lower_bound. */
15661 switch (cu
->lang ())
15664 case language_cplus
:
15665 low
.set_const_val (0);
15666 low_default_is_valid
= 1;
15668 case language_fortran
:
15669 low
.set_const_val (1);
15670 low_default_is_valid
= 1;
15673 case language_objc
:
15674 case language_rust
:
15675 low
.set_const_val (0);
15676 low_default_is_valid
= (cu
->header
.version
>= 4);
15680 case language_pascal
:
15681 low
.set_const_val (1);
15682 low_default_is_valid
= (cu
->header
.version
>= 4);
15685 low
.set_const_val (0);
15686 low_default_is_valid
= 0;
15690 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
15691 if (attr
!= nullptr)
15692 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
15693 else if (!low_default_is_valid
)
15694 complaint (_("Missing DW_AT_lower_bound "
15695 "- DIE at %s [in module %s]"),
15696 sect_offset_str (die
->sect_off
),
15697 objfile_name (cu
->per_objfile
->objfile
));
15699 struct attribute
*attr_ub
, *attr_count
;
15700 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
15701 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
15703 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
15704 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
15706 /* If bounds are constant do the final calculation here. */
15707 if (low
.is_constant () && high
.is_constant ())
15708 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
15710 high_bound_is_count
= 1;
15714 if (attr_ub
!= NULL
)
15715 complaint (_("Unresolved DW_AT_upper_bound "
15716 "- DIE at %s [in module %s]"),
15717 sect_offset_str (die
->sect_off
),
15718 objfile_name (cu
->per_objfile
->objfile
));
15719 if (attr_count
!= NULL
)
15720 complaint (_("Unresolved DW_AT_count "
15721 "- DIE at %s [in module %s]"),
15722 sect_offset_str (die
->sect_off
),
15723 objfile_name (cu
->per_objfile
->objfile
));
15728 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
15729 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
15730 bias
= bias_attr
->constant_value (0);
15732 /* Normally, the DWARF producers are expected to use a signed
15733 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15734 But this is unfortunately not always the case, as witnessed
15735 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15736 is used instead. To work around that ambiguity, we treat
15737 the bounds as signed, and thus sign-extend their values, when
15738 the base type is signed.
15740 Skip it if the base type's length is larger than ULONGEST, to avoid
15741 the undefined behavior of a too large left shift. We don't really handle
15742 constants larger than 8 bytes anyway, at the moment. */
15744 if (base_type
->length () <= sizeof (ULONGEST
))
15746 ULONGEST negative_mask
15747 = -((ULONGEST
) 1 << (base_type
->length () * TARGET_CHAR_BIT
- 1));
15749 if (low
.is_constant ()
15750 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
15751 low
.set_const_val (low
.const_val () | negative_mask
);
15753 if (high
.is_constant ()
15754 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
15755 high
.set_const_val (high
.const_val () | negative_mask
);
15758 /* Check for bit and byte strides. */
15759 struct dynamic_prop byte_stride_prop
;
15760 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15761 if (attr_byte_stride
!= nullptr)
15763 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15764 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
15768 struct dynamic_prop bit_stride_prop
;
15769 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15770 if (attr_bit_stride
!= nullptr)
15772 /* It only makes sense to have either a bit or byte stride. */
15773 if (attr_byte_stride
!= nullptr)
15775 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15776 "- DIE at %s [in module %s]"),
15777 sect_offset_str (die
->sect_off
),
15778 objfile_name (cu
->per_objfile
->objfile
));
15779 attr_bit_stride
= nullptr;
15783 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15784 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
15789 type_allocator
alloc (cu
->per_objfile
->objfile
, cu
->lang ());
15790 if (attr_byte_stride
!= nullptr
15791 || attr_bit_stride
!= nullptr)
15793 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
15794 struct dynamic_prop
*stride
15795 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
15798 = create_range_type_with_stride (alloc
, orig_base_type
, &low
,
15799 &high
, bias
, stride
, byte_stride_p
);
15802 range_type
= create_range_type (alloc
, orig_base_type
, &low
, &high
, bias
);
15804 if (high_bound_is_count
)
15805 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
15807 /* Ada expects an empty array on no boundary attributes. */
15808 if (attr
== NULL
&& cu
->lang () != language_ada
)
15809 range_type
->bounds ()->high
.set_undefined ();
15811 name
= dwarf2_name (die
, cu
);
15813 range_type
->set_name (name
);
15815 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15816 if (attr
!= nullptr)
15817 range_type
->set_length (attr
->constant_value (0));
15819 maybe_set_alignment (cu
, die
, range_type
);
15821 set_die_type (die
, range_type
, cu
);
15823 /* set_die_type should be already done. */
15824 set_descriptive_type (range_type
, die
, cu
);
15829 static struct type
*
15830 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15834 type
= (type_allocator (cu
->per_objfile
->objfile
, cu
->lang ())
15835 .new_type (TYPE_CODE_VOID
, 0, nullptr));
15836 type
->set_name (dwarf2_name (die
, cu
));
15838 /* In Ada, an unspecified type is typically used when the description
15839 of the type is deferred to a different unit. When encountering
15840 such a type, we treat it as a stub, and try to resolve it later on,
15842 Mark this as a stub type for all languages though. */
15843 type
->set_is_stub (true);
15845 return set_die_type (die
, type
, cu
);
15848 /* Read a single die and all its descendents. Set the die's sibling
15849 field to NULL; set other fields in the die correctly, and set all
15850 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15851 location of the info_ptr after reading all of those dies. PARENT
15852 is the parent of the die in question. */
15854 static struct die_info
*
15855 read_die_and_children (const struct die_reader_specs
*reader
,
15856 const gdb_byte
*info_ptr
,
15857 const gdb_byte
**new_info_ptr
,
15858 struct die_info
*parent
)
15860 struct die_info
*die
;
15861 const gdb_byte
*cur_ptr
;
15863 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0, true);
15866 *new_info_ptr
= cur_ptr
;
15869 store_in_ref_table (die
, reader
->cu
);
15871 if (die
->has_children
)
15872 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
15876 *new_info_ptr
= cur_ptr
;
15879 die
->sibling
= NULL
;
15880 die
->parent
= parent
;
15884 /* Read a die, all of its descendents, and all of its siblings; set
15885 all of the fields of all of the dies correctly. Arguments are as
15886 in read_die_and_children. */
15888 static struct die_info
*
15889 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
15890 const gdb_byte
*info_ptr
,
15891 const gdb_byte
**new_info_ptr
,
15892 struct die_info
*parent
)
15894 struct die_info
*first_die
, *last_sibling
;
15895 const gdb_byte
*cur_ptr
;
15897 cur_ptr
= info_ptr
;
15898 first_die
= last_sibling
= NULL
;
15902 struct die_info
*die
15903 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
15907 *new_info_ptr
= cur_ptr
;
15914 last_sibling
->sibling
= die
;
15916 last_sibling
= die
;
15920 /* Read a die, all of its descendents, and all of its siblings; set
15921 all of the fields of all of the dies correctly. Arguments are as
15922 in read_die_and_children.
15923 This the main entry point for reading a DIE and all its children. */
15925 static struct die_info
*
15926 read_die_and_siblings (const struct die_reader_specs
*reader
,
15927 const gdb_byte
*info_ptr
,
15928 const gdb_byte
**new_info_ptr
,
15929 struct die_info
*parent
)
15931 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
15932 new_info_ptr
, parent
);
15934 if (dwarf_die_debug
)
15936 gdb_printf (gdb_stdlog
,
15937 "Read die from %s@0x%x of %s:\n",
15938 reader
->die_section
->get_name (),
15939 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
15940 bfd_get_filename (reader
->abfd
));
15941 die
->dump (dwarf_die_debug
);
15947 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15949 The caller is responsible for filling in the extra attributes
15950 and updating (*DIEP)->num_attrs.
15951 Set DIEP to point to a newly allocated die with its information,
15952 except for its child, sibling, and parent fields. */
15954 static const gdb_byte
*
15955 read_full_die_1 (const struct die_reader_specs
*reader
,
15956 struct die_info
**diep
, const gdb_byte
*info_ptr
,
15957 int num_extra_attrs
, bool allow_reprocess
)
15959 unsigned int abbrev_number
, bytes_read
, i
;
15960 const struct abbrev_info
*abbrev
;
15961 struct die_info
*die
;
15962 struct dwarf2_cu
*cu
= reader
->cu
;
15963 bfd
*abfd
= reader
->abfd
;
15965 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
15966 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
15967 info_ptr
+= bytes_read
;
15968 if (!abbrev_number
)
15974 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
15976 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15978 bfd_get_filename (abfd
));
15980 die
= die_info::allocate (&cu
->comp_unit_obstack
,
15981 abbrev
->num_attrs
+ num_extra_attrs
);
15982 die
->sect_off
= sect_off
;
15983 die
->tag
= abbrev
->tag
;
15984 die
->abbrev
= abbrev_number
;
15985 die
->has_children
= abbrev
->has_children
;
15987 /* Make the result usable.
15988 The caller needs to update num_attrs after adding the extra
15990 die
->num_attrs
= abbrev
->num_attrs
;
15992 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
15993 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
15994 info_ptr
, allow_reprocess
);
16000 /* Read a die and all its attributes.
16001 Set DIEP to point to a newly allocated die with its information,
16002 except for its child, sibling, and parent fields. */
16004 static const gdb_byte
*
16005 read_toplevel_die (const struct die_reader_specs
*reader
,
16006 struct die_info
**diep
, const gdb_byte
*info_ptr
,
16007 gdb::array_view
<attribute
*> extra_attrs
)
16009 const gdb_byte
*result
;
16010 struct dwarf2_cu
*cu
= reader
->cu
;
16012 result
= read_full_die_1 (reader
, diep
, info_ptr
, extra_attrs
.size (),
16015 /* Copy in the extra attributes, if any. */
16016 attribute
*next
= &(*diep
)->attrs
[(*diep
)->num_attrs
];
16017 for (attribute
*extra
: extra_attrs
)
16020 struct attribute
*attr
= (*diep
)->attr (DW_AT_str_offsets_base
);
16021 if (attr
!= nullptr && attr
->form_is_unsigned ())
16022 cu
->str_offsets_base
= attr
->as_unsigned ();
16024 attr
= (*diep
)->attr (DW_AT_loclists_base
);
16025 if (attr
!= nullptr)
16026 cu
->loclist_base
= attr
->as_unsigned ();
16028 auto maybe_addr_base
= (*diep
)->addr_base ();
16029 if (maybe_addr_base
.has_value ())
16030 cu
->addr_base
= *maybe_addr_base
;
16032 attr
= (*diep
)->attr (DW_AT_rnglists_base
);
16033 if (attr
!= nullptr)
16034 cu
->rnglists_base
= attr
->as_unsigned ();
16036 for (int i
= 0; i
< (*diep
)->num_attrs
; ++i
)
16038 if ((*diep
)->attrs
[i
].form_requires_reprocessing ())
16039 read_attribute_reprocess (reader
, &(*diep
)->attrs
[i
], (*diep
)->tag
);
16042 (*diep
)->num_attrs
+= extra_attrs
.size ();
16044 if (dwarf_die_debug
)
16046 gdb_printf (gdb_stdlog
,
16047 "Read die from %s@0x%x of %s:\n",
16048 reader
->die_section
->get_name (),
16049 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
16050 bfd_get_filename (reader
->abfd
));
16051 (*diep
)->dump (dwarf_die_debug
);
16059 cooked_indexer::check_bounds (cutu_reader
*reader
)
16061 if (reader
->cu
->per_cu
->addresses_seen
)
16064 dwarf2_cu
*cu
= reader
->cu
;
16066 unrelocated_addr best_lowpc
= {}, best_highpc
= {};
16067 /* Possibly set the default values of LOWPC and HIGHPC from
16069 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
16070 enum pc_bounds_kind cu_bounds_kind
16071 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
16072 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
16073 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
16075 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
16076 unrelocated_addr low
= per_objfile
->adjust (best_lowpc
);
16077 unrelocated_addr high
= per_objfile
->adjust (best_highpc
);
16078 /* Store the contiguous range if it is not empty; it can be
16079 empty for CUs with no code. addrmap requires CORE_ADDR, so
16081 m_index_storage
->get_addrmap ()->set_empty ((CORE_ADDR
) low
,
16082 (CORE_ADDR
) high
- 1,
16085 cu
->per_cu
->addresses_seen
= true;
16089 /* Helper function that returns true if TAG can have a linkage
16093 tag_can_have_linkage_name (enum dwarf_tag tag
)
16097 case DW_TAG_variable
:
16098 case DW_TAG_subprogram
:
16107 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
16108 dwarf2_per_objfile
*per_objfile
,
16109 sect_offset sect_off
, bool is_dwz
,
16112 /* Lookups for type unit references are always in the CU, and
16113 cross-CU references will crash. */
16114 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
16115 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
16118 dwarf2_per_cu_data
*per_cu
16119 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
16120 per_objfile
->per_bfd
);
16122 /* When scanning, we only want to visit a given CU a single time.
16123 Doing this check here avoids self-imports as well. */
16127 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
16130 if (per_cu
== m_per_cu
)
16133 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
16134 if (result
== nullptr)
16136 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
16137 m_index_storage
->get_abbrev_cache ());
16139 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
16141 std::unique_ptr
<cutu_reader
> copy
16142 (new cutu_reader (std::move (new_reader
)));
16143 result
= m_index_storage
->preserve (std::move (copy
));
16146 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
16150 check_bounds (result
);
16156 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
16157 cutu_reader
*reader
,
16158 const gdb_byte
*watermark_ptr
,
16159 const gdb_byte
*info_ptr
,
16160 const abbrev_info
*abbrev
,
16162 const char **linkage_name
,
16163 cooked_index_flag
*flags
,
16164 sect_offset
*sibling_offset
,
16165 const cooked_index_entry
**parent_entry
,
16166 CORE_ADDR
*maybe_defer
,
16167 bool for_specification
)
16169 bool origin_is_dwz
= false;
16170 bool is_declaration
= false;
16171 sect_offset origin_offset
{};
16173 std::optional
<unrelocated_addr
> low_pc
;
16174 std::optional
<unrelocated_addr
> high_pc
;
16175 bool high_pc_relative
= false;
16177 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
16180 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
16182 /* Store the data if it is of an attribute we want to keep in a
16183 partial symbol table. */
16187 switch (abbrev
->tag
)
16189 case DW_TAG_compile_unit
:
16190 case DW_TAG_partial_unit
:
16191 case DW_TAG_type_unit
:
16192 /* Compilation units have a DW_AT_name that is a filename, not
16193 a source language identifier. */
16197 if (*name
== nullptr)
16198 *name
= attr
.as_string ();
16203 case DW_AT_linkage_name
:
16204 case DW_AT_MIPS_linkage_name
:
16205 /* Note that both forms of linkage name might appear. We
16206 assume they will be the same, and we only store the last
16208 if (*linkage_name
== nullptr)
16209 *linkage_name
= attr
.as_string ();
16212 /* DWARF 4 has defined a dedicated DW_AT_main_subprogram
16213 attribute to indicate the starting function of the program... */
16214 case DW_AT_main_subprogram
:
16215 if (attr
.as_boolean ())
16219 /* ... however with older versions the DW_CC_program value of
16220 the DW_AT_calling_convention attribute was used instead as
16221 the only means available. We handle both variants then. */
16222 case DW_AT_calling_convention
:
16223 if (attr
.constant_value (DW_CC_normal
) == DW_CC_program
)
16227 case DW_AT_declaration
:
16228 is_declaration
= attr
.as_boolean ();
16231 case DW_AT_sibling
:
16232 if (sibling_offset
!= nullptr)
16233 *sibling_offset
= attr
.get_ref_die_offset ();
16236 case DW_AT_specification
:
16237 case DW_AT_abstract_origin
:
16238 case DW_AT_extension
:
16239 origin_offset
= attr
.get_ref_die_offset ();
16240 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
16243 case DW_AT_external
:
16244 if (attr
.as_boolean ())
16245 *flags
&= ~IS_STATIC
;
16248 case DW_AT_enum_class
:
16249 if (attr
.as_boolean ())
16250 *flags
|= IS_ENUM_CLASS
;
16254 low_pc
= attr
.as_address ();
16257 case DW_AT_high_pc
:
16258 high_pc
= attr
.as_address ();
16259 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
16260 high_pc_relative
= true;
16263 case DW_AT_location
:
16264 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
16266 struct dwarf_block
*locdesc
= attr
.as_block ();
16268 if (decode_locdesc (locdesc
, reader
->cu
, &addr
)
16270 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
))
16272 low_pc
= (unrelocated_addr
) addr
;
16273 /* For variables, we don't want to try decoding the
16274 type just to find the size -- for gdb's purposes
16275 we only need the address of a variable. */
16276 high_pc
= (unrelocated_addr
) (addr
+ 1);
16277 high_pc_relative
= false;
16283 if (!scanning_per_cu
->addresses_seen
)
16285 /* Offset in the .debug_ranges or .debug_rnglist section
16286 (depending on DWARF version). */
16287 ULONGEST ranges_offset
= attr
.as_unsigned ();
16289 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16290 want to add this value. */
16291 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
16293 unrelocated_addr lowpc
, highpc
;
16294 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
16295 m_index_storage
->get_addrmap (),
16296 scanning_per_cu
, abbrev
->tag
);
16302 /* We don't want to examine declarations, but if we found a
16303 declaration when handling DW_AT_specification or the like, then
16304 that is ok. Similarly, we allow an external variable without a
16305 location; those are resolved via minimal symbols. */
16306 if (is_declaration
&& !for_specification
16307 && !(abbrev
->tag
== DW_TAG_variable
&& (*flags
& IS_STATIC
) == 0))
16309 /* We always want to recurse into some types, but we may not
16310 want to treat them as definitions. */
16311 if ((abbrev
->tag
== DW_TAG_class_type
16312 || abbrev
->tag
== DW_TAG_structure_type
16313 || abbrev
->tag
== DW_TAG_union_type
)
16314 && abbrev
->has_children
)
16315 *flags
|= IS_TYPE_DECLARATION
;
16318 *linkage_name
= nullptr;
16322 else if ((*name
== nullptr
16323 || (*linkage_name
== nullptr
16324 && tag_can_have_linkage_name (abbrev
->tag
))
16325 || (*parent_entry
== nullptr && m_language
!= language_c
))
16326 && origin_offset
!= sect_offset (0))
16328 cutu_reader
*new_reader
16329 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
16330 origin_is_dwz
, false);
16331 if (new_reader
!= nullptr)
16333 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
16334 + to_underlying (origin_offset
));
16336 if (new_reader
->cu
== reader
->cu
16337 && new_info_ptr
> watermark_ptr
16338 && *parent_entry
== nullptr)
16339 *maybe_defer
= form_addr (origin_offset
, origin_is_dwz
);
16340 else if (*parent_entry
== nullptr)
16342 CORE_ADDR lookup
= form_addr (origin_offset
, origin_is_dwz
);
16343 void *obj
= m_die_range_map
.find (lookup
);
16344 *parent_entry
= static_cast <cooked_index_entry
*> (obj
);
16347 unsigned int bytes_read
;
16348 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
16351 new_info_ptr
+= bytes_read
;
16353 if (new_reader
->cu
== reader
->cu
&& new_info_ptr
== watermark_ptr
)
16355 /* Self-reference, we're done. */
16358 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
,
16359 new_info_ptr
, new_abbrev
, name
, linkage_name
,
16360 flags
, nullptr, parent_entry
, maybe_defer
, true);
16364 if (!for_specification
)
16366 if (m_language
== language_ada
16367 && *linkage_name
== nullptr)
16368 *linkage_name
= *name
;
16370 if (!scanning_per_cu
->addresses_seen
16371 && low_pc
.has_value ()
16372 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
16373 || *low_pc
!= (unrelocated_addr
) 0)
16374 && high_pc
.has_value ())
16376 if (high_pc_relative
)
16377 high_pc
= (unrelocated_addr
) ((ULONGEST
) *high_pc
16378 + (ULONGEST
) *low_pc
);
16380 if (*high_pc
> *low_pc
)
16382 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
16383 unrelocated_addr lo
= per_objfile
->adjust (*low_pc
);
16384 unrelocated_addr hi
= per_objfile
->adjust (*high_pc
);
16385 /* Need CORE_ADDR casts for addrmap. */
16386 m_index_storage
->get_addrmap ()->set_empty ((CORE_ADDR
) lo
,
16387 (CORE_ADDR
) hi
- 1,
16392 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
16393 *flags
&= ~IS_STATIC
;
16395 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
16396 *name
= "(anonymous namespace)";
16398 if (m_language
== language_cplus
16399 && (abbrev
->tag
== DW_TAG_class_type
16400 || abbrev
->tag
== DW_TAG_interface_type
16401 || abbrev
->tag
== DW_TAG_structure_type
16402 || abbrev
->tag
== DW_TAG_union_type
16403 || abbrev
->tag
== DW_TAG_enumeration_type
16404 || abbrev
->tag
== DW_TAG_enumerator
))
16405 *flags
&= ~IS_STATIC
;
16412 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
16413 const gdb_byte
*info_ptr
,
16414 const abbrev_info
*abbrev
)
16416 sect_offset sect_off
{};
16417 bool is_dwz
= false;
16419 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
16421 /* Note that we never need to reprocess attributes here. */
16423 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
16425 if (attr
.name
== DW_AT_import
)
16427 sect_off
= attr
.get_ref_die_offset ();
16428 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
16429 || reader
->cu
->per_cu
->is_dwz
);
16433 /* Did not find DW_AT_import. */
16434 if (sect_off
== sect_offset (0))
16437 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
16438 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
16440 if (new_reader
!= nullptr)
16442 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
16444 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
16451 cooked_indexer::recurse (cutu_reader
*reader
,
16452 const gdb_byte
*info_ptr
,
16453 const cooked_index_entry
*parent_entry
,
16456 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
16458 if (parent_entry
!= nullptr)
16460 /* Both start and end are inclusive, so use both "+ 1" and "- 1" to
16461 limit the range to the children of parent_entry. */
16462 CORE_ADDR start
= form_addr (parent_entry
->die_offset
+ 1,
16463 reader
->cu
->per_cu
->is_dwz
);
16464 CORE_ADDR end
= form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
16465 reader
->cu
->per_cu
->is_dwz
);
16466 m_die_range_map
.set_empty (start
, end
, (void *) parent_entry
);
16473 cooked_indexer::index_dies (cutu_reader
*reader
,
16474 const gdb_byte
*info_ptr
,
16475 const cooked_index_entry
*parent_entry
,
16478 const gdb_byte
*end_ptr
= (reader
->buffer
16479 + to_underlying (reader
->cu
->header
.sect_off
)
16480 + reader
->cu
->header
.get_length_with_initial ());
16482 while (info_ptr
< end_ptr
)
16484 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
16485 unsigned int bytes_read
;
16486 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
16488 info_ptr
+= bytes_read
;
16489 if (abbrev
== nullptr)
16492 if (abbrev
->tag
== DW_TAG_imported_unit
)
16494 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
16498 if (!abbrev
->interesting
)
16500 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
16501 if (fully
&& abbrev
->has_children
)
16502 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
16506 const char *name
= nullptr;
16507 const char *linkage_name
= nullptr;
16508 CORE_ADDR defer
= 0;
16509 cooked_index_flag flags
= IS_STATIC
;
16510 sect_offset sibling
{};
16511 const cooked_index_entry
*this_parent_entry
= parent_entry
;
16513 /* The scope of a DW_TAG_entry_point cooked_index_entry is the one of
16514 its surrounding subroutine. */
16515 if (abbrev
->tag
== DW_TAG_entry_point
)
16516 this_parent_entry
= parent_entry
->get_parent ();
16517 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
16518 info_ptr
, abbrev
, &name
, &linkage_name
,
16519 &flags
, &sibling
, &this_parent_entry
,
16522 if (abbrev
->tag
== DW_TAG_namespace
16523 && m_language
== language_cplus
16524 && strcmp (name
, "::") == 0)
16526 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16527 generated bogus DW_TAG_namespace DIEs with a name of "::"
16528 for the global namespace. Work around this problem
16533 cooked_index_entry
*this_entry
= nullptr;
16534 if (name
!= nullptr)
16539 = m_index_storage
->add (this_die
, abbrev
->tag
,
16540 flags
| IS_PARENT_DEFERRED
, name
,
16542 m_deferred_entries
.push_back (this_entry
);
16546 = m_index_storage
->add (this_die
, abbrev
->tag
, flags
, name
,
16547 this_parent_entry
, m_per_cu
);
16550 if (linkage_name
!= nullptr)
16552 /* We only want this to be "main" if it has a linkage name
16553 but not an ordinary name. */
16554 if (name
!= nullptr)
16555 flags
= flags
& ~IS_MAIN
;
16556 /* Set the IS_LINKAGE on for everything except when functions
16557 have linkage name present but name is absent. */
16558 if (name
!= nullptr
16559 || (abbrev
->tag
!= DW_TAG_subprogram
16560 && abbrev
->tag
!= DW_TAG_inlined_subroutine
16561 && abbrev
->tag
!= DW_TAG_entry_point
))
16562 flags
= flags
| IS_LINKAGE
;
16563 m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
16564 linkage_name
, nullptr, m_per_cu
);
16567 if (abbrev
->has_children
)
16569 switch (abbrev
->tag
)
16571 case DW_TAG_class_type
:
16572 case DW_TAG_interface_type
:
16573 case DW_TAG_structure_type
:
16574 case DW_TAG_union_type
:
16575 if (m_language
!= language_c
&& this_entry
!= nullptr)
16577 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
16582 case DW_TAG_enumeration_type
:
16583 /* We need to recurse even for an anonymous enumeration.
16584 Which scope we record as the parent scope depends on
16585 whether we're reading an "enum class". If so, we use
16586 the enum itself as the parent, yielding names like
16587 "enum_class::enumerator"; otherwise we inject the
16588 names into our own parent scope. */
16589 info_ptr
= recurse (reader
, info_ptr
,
16590 ((flags
& IS_ENUM_CLASS
) == 0)
16596 case DW_TAG_module
:
16597 if (this_entry
== nullptr)
16600 case DW_TAG_namespace
:
16601 /* We don't check THIS_ENTRY for a namespace, to handle
16602 the ancient G++ workaround pointed out above. */
16603 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
16606 case DW_TAG_subprogram
:
16607 if ((m_language
== language_fortran
16608 || m_language
== language_ada
)
16609 && this_entry
!= nullptr)
16611 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
16617 if (sibling
!= sect_offset (0))
16619 const gdb_byte
*sibling_ptr
16620 = reader
->buffer
+ to_underlying (sibling
);
16622 if (sibling_ptr
< info_ptr
)
16623 complaint (_("DW_AT_sibling points backwards"));
16624 else if (sibling_ptr
> reader
->buffer_end
)
16625 reader
->die_section
->overflow_complaint ();
16627 info_ptr
= sibling_ptr
;
16630 info_ptr
= skip_children (reader
, info_ptr
);
16638 cooked_indexer::make_index (cutu_reader
*reader
)
16640 check_bounds (reader
);
16641 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
16642 if (!reader
->comp_unit_die
->has_children
)
16644 index_dies (reader
, reader
->info_ptr
, nullptr, false);
16646 for (const auto &entry
: m_deferred_entries
)
16648 void *obj
= m_die_range_map
.find (entry
->get_deferred_parent ());
16649 cooked_index_entry
*parent
= static_cast<cooked_index_entry
*> (obj
);
16650 entry
->resolve_parent (parent
);
16654 /* An implementation of quick_symbol_functions for the cooked DWARF
16657 struct cooked_index_functions
: public dwarf2_base_index_functions
16659 cooked_index
*wait (struct objfile
*objfile
, bool allow_quit
)
16661 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
16662 cooked_index
*table
16663 = (gdb::checked_static_cast
<cooked_index
*>
16664 (per_objfile
->per_bfd
->index_table
.get ()));
16665 table
->wait (cooked_state::MAIN_AVAILABLE
, allow_quit
);
16669 dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
16670 unrelocated_addr adjusted_pc
) override
;
16672 struct compunit_symtab
*find_compunit_symtab_by_address
16673 (struct objfile
*objfile
, CORE_ADDR address
) override
;
16675 bool has_unexpanded_symtabs (struct objfile
*objfile
) override
16677 wait (objfile
, true);
16678 return dwarf2_base_index_functions::has_unexpanded_symtabs (objfile
);
16681 struct symtab
*find_last_source_symtab (struct objfile
*objfile
) override
16683 wait (objfile
, true);
16684 return dwarf2_base_index_functions::find_last_source_symtab (objfile
);
16687 void forget_cached_source_info (struct objfile
*objfile
) override
16689 wait (objfile
, true);
16690 dwarf2_base_index_functions::forget_cached_source_info (objfile
);
16693 void print_stats (struct objfile
*objfile
, bool print_bcache
) override
16695 wait (objfile
, true);
16696 dwarf2_base_index_functions::print_stats (objfile
, print_bcache
);
16699 void dump (struct objfile
*objfile
) override
16701 cooked_index
*index
= wait (objfile
, true);
16702 gdb_printf ("Cooked index in use:\n");
16704 index
->dump (objfile
->arch ());
16707 void expand_all_symtabs (struct objfile
*objfile
) override
16709 wait (objfile
, true);
16710 dwarf2_base_index_functions::expand_all_symtabs (objfile
);
16713 bool expand_symtabs_matching
16714 (struct objfile
*objfile
,
16715 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
16716 const lookup_name_info
*lookup_name
,
16717 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
16718 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
16719 block_search_flags search_flags
,
16720 domain_enum domain
,
16721 enum search_domain kind
) override
;
16723 struct compunit_symtab
*find_pc_sect_compunit_symtab
16724 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
16725 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
) override
16727 wait (objfile
, true);
16728 return (dwarf2_base_index_functions::find_pc_sect_compunit_symtab
16729 (objfile
, msymbol
, pc
, section
, warn_if_readin
));
16732 void map_symbol_filenames
16733 (struct objfile
*objfile
,
16734 gdb::function_view
<symbol_filename_ftype
> fun
,
16735 bool need_fullname
) override
16737 wait (objfile
, true);
16738 return (dwarf2_base_index_functions::map_symbol_filenames
16739 (objfile
, fun
, need_fullname
));
16742 void compute_main_name (struct objfile
*objfile
) override
16744 wait (objfile
, false);
16748 dwarf2_per_cu_data
*
16749 cooked_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
16750 unrelocated_addr adjusted_pc
)
16752 cooked_index
*table
16753 = (gdb::checked_static_cast
<cooked_index
*>
16754 (per_bfd
->index_table
.get ()));
16755 return table
->lookup (adjusted_pc
);
16758 struct compunit_symtab
*
16759 cooked_index_functions::find_compunit_symtab_by_address
16760 (struct objfile
*objfile
, CORE_ADDR address
)
16762 if (objfile
->sect_index_data
== -1)
16765 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
16766 cooked_index
*table
= wait (objfile
, true);
16768 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
16769 dwarf2_per_cu_data
*per_cu
16770 = table
->lookup ((unrelocated_addr
) (address
- baseaddr
));
16771 if (per_cu
== nullptr)
16774 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
16778 cooked_index_functions::expand_symtabs_matching
16779 (struct objfile
*objfile
,
16780 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
16781 const lookup_name_info
*lookup_name
,
16782 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
16783 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
16784 block_search_flags search_flags
,
16785 domain_enum domain
,
16786 enum search_domain kind
)
16788 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
16790 cooked_index
*table
= wait (objfile
, true);
16792 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
16794 /* This invariant is documented in quick-functions.h. */
16795 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
16796 if (lookup_name
== nullptr)
16798 for (dwarf2_per_cu_data
*per_cu
16799 : all_units_range (per_objfile
->per_bfd
))
16803 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
16811 lookup_name_info lookup_name_without_params
16812 = lookup_name
->make_ignore_params ();
16813 bool completing
= lookup_name
->completion_mode ();
16815 /* Unique styles of language splitting. */
16816 static const enum language unique_styles
[] =
16818 /* No splitting is also a style. */
16820 /* This includes Rust. */
16822 /* This includes Go. */
16827 for (enum language lang
: unique_styles
)
16829 std::vector
<std::string_view
> name_vec
16830 = lookup_name_without_params
.split_name (lang
);
16831 std::string
last_name (name_vec
.back ());
16833 for (const cooked_index_entry
*entry
: table
->find (last_name
,
16838 /* No need to consider symbols from expanded CUs. */
16839 if (per_objfile
->symtab_set_p (entry
->per_cu
))
16842 /* If file-matching was done, we don't need to consider
16843 symbols from unmarked CUs. */
16844 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
16847 /* See if the symbol matches the type filter. */
16848 if (!entry
->matches (search_flags
)
16849 || !entry
->matches (domain
)
16850 || !entry
->matches (kind
))
16853 /* We've found the base name of the symbol; now walk its
16854 parentage chain, ensuring that each component
16858 const cooked_index_entry
*parent
= entry
->get_parent ();
16859 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
16861 /* If we ran out of entries, or if this segment doesn't
16862 match, this did not match. */
16863 if (parent
== nullptr
16864 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
16865 name_vec
[i
- 1].length ()) != 0)
16871 parent
= parent
->get_parent ();
16877 /* Might have been looking for "a::b" and found
16879 if (symbol_matcher
== nullptr)
16881 symbol_name_match_type match_type
16882 = lookup_name_without_params
.match_type ();
16883 if ((match_type
== symbol_name_match_type::FULL
16884 || (lang
!= language_ada
16885 && match_type
== symbol_name_match_type::EXPRESSION
))
16886 && parent
!= nullptr)
16891 auto_obstack temp_storage
;
16892 const char *full_name
= entry
->full_name (&temp_storage
);
16893 if (!symbol_matcher (full_name
))
16897 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
16907 /* Return a new cooked_index_functions object. */
16909 static quick_symbol_functions_up
16910 make_cooked_index_funcs (dwarf2_per_objfile
*per_objfile
)
16912 /* Set the index table early so that sharing works even while
16913 scanning; and then start the scanning. */
16914 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
16915 cooked_index
*idx
= new cooked_index (per_objfile
);
16916 per_bfd
->index_table
.reset (idx
);
16917 /* Don't start reading until after 'index_table' is set. This
16919 idx
->start_reading ();
16921 if (dwarf_synchronous
)
16922 idx
->wait_completely ();
16924 return quick_symbol_functions_up (new cooked_index_functions
);
16927 quick_symbol_functions_up
16928 cooked_index::make_quick_functions () const
16930 return quick_symbol_functions_up (new cooked_index_functions
);
16935 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16936 contents from the given SECTION in the HEADER.
16938 HEADER_OFFSET is the offset of the header in the section. */
16940 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
16941 struct dwarf2_section_info
*section
,
16942 sect_offset header_offset
)
16944 unsigned int bytes_read
;
16945 bfd
*abfd
= section
->get_bfd_owner ();
16946 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
16948 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
16949 info_ptr
+= bytes_read
;
16951 header
->version
= read_2_bytes (abfd
, info_ptr
);
16954 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
16957 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
16960 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
16963 /* Return the DW_AT_loclists_base value for the CU. */
16965 lookup_loclist_base (struct dwarf2_cu
*cu
)
16967 /* For the .dwo unit, the loclist_base points to the first offset following
16968 the header. The header consists of the following entities-
16969 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16971 2. version (2 bytes)
16972 3. address size (1 byte)
16973 4. segment selector size (1 byte)
16974 5. offset entry count (4 bytes)
16975 These sizes are derived as per the DWARFv5 standard. */
16976 if (cu
->dwo_unit
!= nullptr)
16978 if (cu
->header
.initial_length_size
== 4)
16979 return LOCLIST_HEADER_SIZE32
;
16980 return LOCLIST_HEADER_SIZE64
;
16982 return cu
->loclist_base
;
16985 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16986 array of offsets in the .debug_loclists section. */
16989 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
16991 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
16992 struct objfile
*objfile
= per_objfile
->objfile
;
16993 bfd
*abfd
= objfile
->obfd
.get ();
16994 ULONGEST loclist_header_size
=
16995 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
16996 : LOCLIST_HEADER_SIZE64
);
16997 ULONGEST loclist_base
= lookup_loclist_base (cu
);
16999 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
17000 ULONGEST start_offset
=
17001 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
17003 /* Get loclists section. */
17004 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
17006 /* Read the loclists section content. */
17007 section
->read (objfile
);
17008 if (section
->buffer
== NULL
)
17009 error (_("DW_FORM_loclistx used without .debug_loclists "
17010 "section [in module %s]"), objfile_name (objfile
));
17012 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
17013 so if loclist_base is smaller than the header size, we have a problem. */
17014 if (loclist_base
< loclist_header_size
)
17015 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
17016 objfile_name (objfile
));
17018 /* Read the header of the loclists contribution. */
17019 struct loclists_rnglists_header header
;
17020 read_loclists_rnglists_header (&header
, section
,
17021 (sect_offset
) (loclist_base
- loclist_header_size
));
17023 /* Verify the loclist index is valid. */
17024 if (loclist_index
>= header
.offset_entry_count
)
17025 error (_("DW_FORM_loclistx pointing outside of "
17026 ".debug_loclists offset array [in module %s]"),
17027 objfile_name (objfile
));
17029 /* Validate that reading won't go beyond the end of the section. */
17030 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
17031 error (_("Reading DW_FORM_loclistx index beyond end of"
17032 ".debug_loclists section [in module %s]"),
17033 objfile_name (objfile
));
17035 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
17037 if (cu
->header
.offset_size
== 4)
17038 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
17040 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
17043 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
17044 array of offsets in the .debug_rnglists section. */
17047 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
17050 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
17051 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
17052 bfd
*abfd
= objfile
->obfd
.get ();
17053 ULONGEST rnglist_header_size
=
17054 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
17055 : RNGLIST_HEADER_SIZE64
);
17057 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
17058 .debug_rnglists.dwo section. The rnglists base given in the skeleton
17060 ULONGEST rnglist_base
=
17061 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
17063 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
17064 ULONGEST start_offset
=
17065 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
17067 /* Get rnglists section. */
17068 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
17070 /* Read the rnglists section content. */
17071 section
->read (objfile
);
17072 if (section
->buffer
== nullptr)
17073 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
17075 objfile_name (objfile
));
17077 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
17078 so if rnglist_base is smaller than the header size, we have a problem. */
17079 if (rnglist_base
< rnglist_header_size
)
17080 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
17081 objfile_name (objfile
));
17083 /* Read the header of the rnglists contribution. */
17084 struct loclists_rnglists_header header
;
17085 read_loclists_rnglists_header (&header
, section
,
17086 (sect_offset
) (rnglist_base
- rnglist_header_size
));
17088 /* Verify the rnglist index is valid. */
17089 if (rnglist_index
>= header
.offset_entry_count
)
17090 error (_("DW_FORM_rnglistx index pointing outside of "
17091 ".debug_rnglists offset array [in module %s]"),
17092 objfile_name (objfile
));
17094 /* Validate that reading won't go beyond the end of the section. */
17095 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
17096 error (_("Reading DW_FORM_rnglistx index beyond end of"
17097 ".debug_rnglists section [in module %s]"),
17098 objfile_name (objfile
));
17100 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
17102 if (cu
->header
.offset_size
== 4)
17103 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
17105 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
17108 /* Process the attributes that had to be skipped in the first round. These
17109 attributes are the ones that need str_offsets_base or addr_base attributes.
17110 They could not have been processed in the first round, because at the time
17111 the values of str_offsets_base or addr_base may not have been known. */
17113 read_attribute_reprocess (const struct die_reader_specs
*reader
,
17114 struct attribute
*attr
, dwarf_tag tag
)
17116 struct dwarf2_cu
*cu
= reader
->cu
;
17117 switch (attr
->form
)
17119 case DW_FORM_addrx
:
17120 case DW_FORM_GNU_addr_index
:
17121 attr
->set_address (read_addr_index (cu
,
17122 attr
->as_unsigned_reprocess ()));
17124 case DW_FORM_loclistx
:
17126 sect_offset loclists_sect_off
17127 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
17129 attr
->set_unsigned (to_underlying (loclists_sect_off
));
17132 case DW_FORM_rnglistx
:
17134 sect_offset rnglists_sect_off
17135 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
17137 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
17141 case DW_FORM_strx1
:
17142 case DW_FORM_strx2
:
17143 case DW_FORM_strx3
:
17144 case DW_FORM_strx4
:
17145 case DW_FORM_GNU_str_index
:
17147 unsigned int str_index
= attr
->as_unsigned_reprocess ();
17148 gdb_assert (!attr
->canonical_string_p ());
17149 if (reader
->dwo_file
!= NULL
)
17150 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
17153 attr
->set_string_noncanonical (read_stub_str_index (cu
,
17158 gdb_assert_not_reached ("Unexpected DWARF form.");
17162 /* Read an attribute value described by an attribute form. */
17164 static const gdb_byte
*
17165 read_attribute_value (const struct die_reader_specs
*reader
,
17166 struct attribute
*attr
, unsigned form
,
17167 LONGEST implicit_const
, const gdb_byte
*info_ptr
,
17168 bool allow_reprocess
)
17170 struct dwarf2_cu
*cu
= reader
->cu
;
17171 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17172 struct objfile
*objfile
= per_objfile
->objfile
;
17173 bfd
*abfd
= reader
->abfd
;
17174 struct comp_unit_head
*cu_header
= &cu
->header
;
17175 unsigned int bytes_read
;
17176 struct dwarf_block
*blk
;
17178 attr
->form
= (enum dwarf_form
) form
;
17181 case DW_FORM_ref_addr
:
17182 if (cu_header
->version
== 2)
17183 attr
->set_unsigned ((ULONGEST
) cu_header
->read_address (abfd
, info_ptr
,
17186 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
17188 info_ptr
+= bytes_read
;
17190 case DW_FORM_GNU_ref_alt
:
17191 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
17193 info_ptr
+= bytes_read
;
17197 unrelocated_addr addr
= cu_header
->read_address (abfd
, info_ptr
,
17199 addr
= per_objfile
->adjust (addr
);
17200 attr
->set_address (addr
);
17201 info_ptr
+= bytes_read
;
17204 case DW_FORM_block2
:
17205 blk
= dwarf_alloc_block (cu
);
17206 blk
->size
= read_2_bytes (abfd
, info_ptr
);
17208 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17209 info_ptr
+= blk
->size
;
17210 attr
->set_block (blk
);
17212 case DW_FORM_block4
:
17213 blk
= dwarf_alloc_block (cu
);
17214 blk
->size
= read_4_bytes (abfd
, info_ptr
);
17216 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17217 info_ptr
+= blk
->size
;
17218 attr
->set_block (blk
);
17220 case DW_FORM_data2
:
17221 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
17224 case DW_FORM_data4
:
17225 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
17228 case DW_FORM_data8
:
17229 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
17232 case DW_FORM_data16
:
17233 blk
= dwarf_alloc_block (cu
);
17235 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
17237 attr
->set_block (blk
);
17239 case DW_FORM_sec_offset
:
17240 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
17242 info_ptr
+= bytes_read
;
17244 case DW_FORM_loclistx
:
17246 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
17248 info_ptr
+= bytes_read
;
17249 if (allow_reprocess
)
17250 read_attribute_reprocess (reader
, attr
);
17253 case DW_FORM_string
:
17254 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
17256 info_ptr
+= bytes_read
;
17259 if (!cu
->per_cu
->is_dwz
)
17261 attr
->set_string_noncanonical
17262 (read_indirect_string (per_objfile
,
17263 abfd
, info_ptr
, cu_header
,
17265 info_ptr
+= bytes_read
;
17269 case DW_FORM_line_strp
:
17270 if (!cu
->per_cu
->is_dwz
)
17272 attr
->set_string_noncanonical
17273 (per_objfile
->read_line_string (info_ptr
, cu_header
,
17275 info_ptr
+= bytes_read
;
17279 case DW_FORM_GNU_strp_alt
:
17281 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
17282 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
17285 attr
->set_string_noncanonical
17286 (dwz
->read_string (objfile
, str_offset
));
17287 info_ptr
+= bytes_read
;
17290 case DW_FORM_exprloc
:
17291 case DW_FORM_block
:
17292 blk
= dwarf_alloc_block (cu
);
17293 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17294 info_ptr
+= bytes_read
;
17295 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17296 info_ptr
+= blk
->size
;
17297 attr
->set_block (blk
);
17299 case DW_FORM_block1
:
17300 blk
= dwarf_alloc_block (cu
);
17301 blk
->size
= read_1_byte (abfd
, info_ptr
);
17303 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17304 info_ptr
+= blk
->size
;
17305 attr
->set_block (blk
);
17307 case DW_FORM_data1
:
17309 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
17312 case DW_FORM_flag_present
:
17313 attr
->set_unsigned (1);
17315 case DW_FORM_sdata
:
17316 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
17317 info_ptr
+= bytes_read
;
17319 case DW_FORM_rnglistx
:
17321 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
17323 info_ptr
+= bytes_read
;
17324 if (allow_reprocess
)
17325 read_attribute_reprocess (reader
, attr
);
17328 case DW_FORM_udata
:
17329 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
17330 info_ptr
+= bytes_read
;
17333 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17334 + read_1_byte (abfd
, info_ptr
)));
17338 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17339 + read_2_bytes (abfd
, info_ptr
)));
17343 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17344 + read_4_bytes (abfd
, info_ptr
)));
17348 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17349 + read_8_bytes (abfd
, info_ptr
)));
17352 case DW_FORM_ref_sig8
:
17353 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
17356 case DW_FORM_ref_udata
:
17357 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17358 + read_unsigned_leb128 (abfd
, info_ptr
,
17360 info_ptr
+= bytes_read
;
17362 case DW_FORM_indirect
:
17363 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17364 info_ptr
+= bytes_read
;
17365 if (form
== DW_FORM_implicit_const
)
17367 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
17368 info_ptr
+= bytes_read
;
17370 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
17371 info_ptr
, allow_reprocess
);
17373 case DW_FORM_implicit_const
:
17374 attr
->set_signed (implicit_const
);
17376 case DW_FORM_addrx
:
17377 case DW_FORM_GNU_addr_index
:
17378 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
17380 info_ptr
+= bytes_read
;
17381 if (allow_reprocess
)
17382 read_attribute_reprocess (reader
, attr
);
17385 case DW_FORM_strx1
:
17386 case DW_FORM_strx2
:
17387 case DW_FORM_strx3
:
17388 case DW_FORM_strx4
:
17389 case DW_FORM_GNU_str_index
:
17391 ULONGEST str_index
;
17392 if (form
== DW_FORM_strx1
)
17394 str_index
= read_1_byte (abfd
, info_ptr
);
17397 else if (form
== DW_FORM_strx2
)
17399 str_index
= read_2_bytes (abfd
, info_ptr
);
17402 else if (form
== DW_FORM_strx3
)
17404 str_index
= read_3_bytes (abfd
, info_ptr
);
17407 else if (form
== DW_FORM_strx4
)
17409 str_index
= read_4_bytes (abfd
, info_ptr
);
17414 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17415 info_ptr
+= bytes_read
;
17417 attr
->set_unsigned_reprocess (str_index
);
17418 if (allow_reprocess
)
17419 read_attribute_reprocess (reader
, attr
);
17423 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17424 dwarf_form_name (form
),
17425 bfd_get_filename (abfd
));
17429 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
17430 attr
->form
= DW_FORM_GNU_ref_alt
;
17432 /* We have seen instances where the compiler tried to emit a byte
17433 size attribute of -1 which ended up being encoded as an unsigned
17434 0xffffffff. Although 0xffffffff is technically a valid size value,
17435 an object of this size seems pretty unlikely so we can relatively
17436 safely treat these cases as if the size attribute was invalid and
17437 treat them as zero by default. */
17438 if (attr
->name
== DW_AT_byte_size
17439 && form
== DW_FORM_data4
17440 && attr
->as_unsigned () >= 0xffffffff)
17443 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17444 hex_string (attr
->as_unsigned ()));
17445 attr
->set_unsigned (0);
17451 /* Read an attribute described by an abbreviated attribute. */
17453 static const gdb_byte
*
17454 read_attribute (const struct die_reader_specs
*reader
,
17455 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
17456 const gdb_byte
*info_ptr
,
17457 bool allow_reprocess
)
17459 attr
->name
= abbrev
->name
;
17460 attr
->string_is_canonical
= 0;
17461 return read_attribute_value (reader
, attr
, abbrev
->form
,
17462 abbrev
->implicit_const
, info_ptr
,
17469 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
17470 LONGEST str_offset
)
17472 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
17473 str_offset
, "DW_FORM_strp");
17476 /* Return pointer to string at .debug_str offset as read from BUF.
17477 BUF is assumed to be in a compilation unit described by CU_HEADER.
17478 Return *BYTES_READ_PTR count of bytes read from BUF. */
17480 static const char *
17481 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
17482 const gdb_byte
*buf
,
17483 const struct comp_unit_head
*cu_header
,
17484 unsigned int *bytes_read_ptr
)
17486 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
17488 return read_indirect_string_at_offset (per_objfile
, str_offset
);
17494 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
17495 unsigned int offset_size
)
17497 bfd
*abfd
= objfile
->obfd
.get ();
17498 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
17500 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
17506 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
17507 const struct comp_unit_head
*cu_header
,
17508 unsigned int *bytes_read_ptr
)
17510 bfd
*abfd
= objfile
->obfd
.get ();
17511 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
17513 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
17516 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17517 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17518 ADDR_SIZE is the size of addresses from the CU header. */
17520 static unrelocated_addr
17521 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
17522 std::optional
<ULONGEST
> addr_base
, int addr_size
)
17524 struct objfile
*objfile
= per_objfile
->objfile
;
17525 bfd
*abfd
= objfile
->obfd
.get ();
17526 const gdb_byte
*info_ptr
;
17527 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
17529 per_objfile
->per_bfd
->addr
.read (objfile
);
17530 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
17531 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17532 objfile_name (objfile
));
17533 if (addr_base_or_zero
+ addr_index
* addr_size
17534 >= per_objfile
->per_bfd
->addr
.size
)
17535 error (_("DW_FORM_addr_index pointing outside of "
17536 ".debug_addr section [in module %s]"),
17537 objfile_name (objfile
));
17538 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
17539 + addr_index
* addr_size
);
17540 if (addr_size
== 4)
17541 return (unrelocated_addr
) bfd_get_32 (abfd
, info_ptr
);
17543 return (unrelocated_addr
) bfd_get_64 (abfd
, info_ptr
);
17546 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17548 static unrelocated_addr
17549 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
17551 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
17552 cu
->addr_base
, cu
->header
.addr_size
);
17555 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17557 static unrelocated_addr
17558 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
17559 unsigned int *bytes_read
)
17561 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
.get ();
17562 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
17564 return read_addr_index (cu
, addr_index
);
17570 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
17571 dwarf2_per_objfile
*per_objfile
,
17572 unsigned int addr_index
)
17574 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
17575 std::optional
<ULONGEST
> addr_base
;
17578 /* We need addr_base and addr_size.
17579 If we don't have PER_CU->cu, we have to get it.
17580 Nasty, but the alternative is storing the needed info in PER_CU,
17581 which at this point doesn't seem justified: it's not clear how frequently
17582 it would get used and it would increase the size of every PER_CU.
17583 Entry points like dwarf2_per_cu_addr_size do a similar thing
17584 so we're not in uncharted territory here.
17585 Alas we need to be a bit more complicated as addr_base is contained
17588 We don't need to read the entire CU(/TU).
17589 We just need the header and top level die.
17591 IWBN to use the aging mechanism to let us lazily later discard the CU.
17592 For now we skip this optimization. */
17596 addr_base
= cu
->addr_base
;
17597 addr_size
= cu
->header
.addr_size
;
17601 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
17602 addr_base
= reader
.cu
->addr_base
;
17603 addr_size
= reader
.cu
->header
.addr_size
;
17606 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
17609 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17610 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17613 static const char *
17614 read_str_index (struct dwarf2_cu
*cu
,
17615 struct dwarf2_section_info
*str_section
,
17616 struct dwarf2_section_info
*str_offsets_section
,
17617 ULONGEST str_offsets_base
, ULONGEST str_index
,
17618 unsigned offset_size
)
17620 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17621 struct objfile
*objfile
= per_objfile
->objfile
;
17622 const char *objf_name
= objfile_name (objfile
);
17623 bfd
*abfd
= objfile
->obfd
.get ();
17624 const gdb_byte
*info_ptr
;
17625 ULONGEST str_offset
;
17626 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17628 str_section
->read (objfile
);
17629 str_offsets_section
->read (objfile
);
17630 if (str_section
->buffer
== NULL
)
17631 error (_("%s used without %s section"
17632 " in CU at offset %s [in module %s]"),
17633 form_name
, str_section
->get_name (),
17634 sect_offset_str (cu
->header
.sect_off
), objf_name
);
17635 if (str_offsets_section
->buffer
== NULL
)
17636 error (_("%s used without %s section"
17637 " in CU at offset %s [in module %s]"),
17638 form_name
, str_section
->get_name (),
17639 sect_offset_str (cu
->header
.sect_off
), objf_name
);
17640 info_ptr
= (str_offsets_section
->buffer
17642 + str_index
* offset_size
);
17643 if (offset_size
== 4)
17644 str_offset
= bfd_get_32 (abfd
, info_ptr
);
17646 str_offset
= bfd_get_64 (abfd
, info_ptr
);
17647 if (str_offset
>= str_section
->size
)
17648 error (_("Offset from %s pointing outside of"
17649 " .debug_str.dwo section in CU at offset %s [in module %s]"),
17650 form_name
, sect_offset_str (cu
->header
.sect_off
), objf_name
);
17651 return (const char *) (str_section
->buffer
+ str_offset
);
17654 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17656 static const char *
17657 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
17659 unsigned offset_size
;
17660 ULONGEST str_offsets_base
;
17661 if (reader
->cu
->header
.version
>= 5)
17663 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17664 so assume the .debug_str_offsets section is DWARF5 as well, and
17665 parse the header. FIXME: Parse the header only once. */
17666 unsigned int bytes_read
= 0;
17667 bfd
*abfd
= reader
->dwo_file
->sections
.str_offsets
.get_bfd_owner ();
17668 const gdb_byte
*p
= reader
->dwo_file
->sections
.str_offsets
.buffer
;
17670 /* Header: Initial length. */
17671 read_initial_length (abfd
, p
+ bytes_read
, &bytes_read
);
17673 /* Determine offset_size based on the .debug_str_offsets header. */
17674 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
17675 offset_size
= dwarf5_is_dwarf64
? 8 : 4;
17677 /* Header: Version. */
17678 unsigned version
= read_2_bytes (abfd
, p
+ bytes_read
);
17683 /* We'd like one warning here about ignoring the section, but
17684 because we parse the header more than once (see FIXME above)
17685 we'd have many warnings, so use a complaint instead, which at
17686 least has a limit. */
17687 complaint (_("Section .debug_str_offsets in %s has unsupported"
17688 " version %d, use empty string."),
17689 reader
->dwo_file
->dwo_name
.c_str (), version
);
17693 /* Header: Padding. */
17696 str_offsets_base
= bytes_read
;
17700 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17701 section, assume the .debug_str_offsets section is pre-DWARF5 as
17702 well, which doesn't have a header. */
17703 str_offsets_base
= 0;
17705 /* Determine offset_size based on the .debug_info header. */
17706 offset_size
= reader
->cu
->header
.offset_size
;
17709 return read_str_index (reader
->cu
,
17710 &reader
->dwo_file
->sections
.str
,
17711 &reader
->dwo_file
->sections
.str_offsets
,
17712 str_offsets_base
, str_index
, offset_size
);
17715 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17717 static const char *
17718 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
17720 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17721 const char *objf_name
= objfile_name (objfile
);
17722 static const char form_name
[] = "DW_FORM_GNU_str_index";
17723 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
17725 if (!cu
->str_offsets_base
.has_value ())
17726 error (_("%s used in Fission stub without %s"
17727 " in CU at offset 0x%lx [in module %s]"),
17728 form_name
, str_offsets_attr_name
,
17729 (long) cu
->header
.offset_size
, objf_name
);
17731 return read_str_index (cu
,
17732 &cu
->per_objfile
->per_bfd
->str
,
17733 &cu
->per_objfile
->per_bfd
->str_offsets
,
17734 *cu
->str_offsets_base
, str_index
,
17735 cu
->header
.offset_size
);
17738 /* Return the length of an LEB128 number in BUF. */
17741 leb128_size (const gdb_byte
*buf
)
17743 const gdb_byte
*begin
= buf
;
17749 if ((byte
& 128) == 0)
17750 return buf
- begin
;
17754 /* Converts DWARF language names to GDB language names. */
17757 dwarf_lang_to_enum_language (unsigned int lang
)
17759 enum language language
;
17768 language
= language_c
;
17771 case DW_LANG_C_plus_plus
:
17772 case DW_LANG_C_plus_plus_11
:
17773 case DW_LANG_C_plus_plus_14
:
17774 language
= language_cplus
;
17777 language
= language_d
;
17779 case DW_LANG_Fortran77
:
17780 case DW_LANG_Fortran90
:
17781 case DW_LANG_Fortran95
:
17782 case DW_LANG_Fortran03
:
17783 case DW_LANG_Fortran08
:
17784 language
= language_fortran
;
17787 language
= language_go
;
17789 case DW_LANG_Mips_Assembler
:
17790 language
= language_asm
;
17792 case DW_LANG_Ada83
:
17793 case DW_LANG_Ada95
:
17794 language
= language_ada
;
17796 case DW_LANG_Modula2
:
17797 language
= language_m2
;
17799 case DW_LANG_Pascal83
:
17800 language
= language_pascal
;
17803 language
= language_objc
;
17806 case DW_LANG_Rust_old
:
17807 language
= language_rust
;
17809 case DW_LANG_OpenCL
:
17810 language
= language_opencl
;
17812 case DW_LANG_Cobol74
:
17813 case DW_LANG_Cobol85
:
17815 language
= language_minimal
;
17822 /* Return the named attribute or NULL if not there. */
17824 static struct attribute
*
17825 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
17830 struct attribute
*spec
= NULL
;
17832 for (i
= 0; i
< die
->num_attrs
; ++i
)
17834 if (die
->attrs
[i
].name
== name
)
17835 return &die
->attrs
[i
];
17836 if (die
->attrs
[i
].name
== DW_AT_specification
17837 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
17838 spec
= &die
->attrs
[i
];
17844 struct die_info
*prev_die
= die
;
17845 die
= follow_die_ref (die
, spec
, &cu
);
17846 if (die
== prev_die
)
17847 /* Self-reference, we're done. */
17854 /* Return the string associated with a string-typed attribute, or NULL if it
17855 is either not found or is of an incorrect type. */
17857 static const char *
17858 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
17860 struct attribute
*attr
;
17861 const char *str
= NULL
;
17863 attr
= dwarf2_attr (die
, name
, cu
);
17867 str
= attr
->as_string ();
17868 if (str
== nullptr)
17869 complaint (_("string type expected for attribute %s for "
17870 "DIE at %s in module %s"),
17871 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
17872 objfile_name (cu
->per_objfile
->objfile
));
17878 /* Return the dwo name or NULL if not present. If present, it is in either
17879 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17880 static const char *
17881 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17883 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
17884 if (dwo_name
== nullptr)
17885 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
17889 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17890 and holds a non-zero value. This function should only be used for
17891 DW_FORM_flag or DW_FORM_flag_present attributes. */
17894 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
17896 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
17898 return attr
!= nullptr && attr
->as_boolean ();
17902 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
17904 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17905 which value is non-zero. However, we have to be careful with
17906 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17907 (via dwarf2_flag_true_p) follows this attribute. So we may
17908 end up accidently finding a declaration attribute that belongs
17909 to a different DIE referenced by the specification attribute,
17910 even though the given DIE does not have a declaration attribute. */
17911 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
17912 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
17915 /* Return the die giving the specification for DIE, if there is
17916 one. *SPEC_CU is the CU containing DIE on input, and the CU
17917 containing the return value on output. If there is no
17918 specification, but there is an abstract origin, that is
17921 static struct die_info
*
17922 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
17924 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
17927 if (spec_attr
== NULL
)
17928 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
17930 if (spec_attr
== NULL
)
17933 return follow_die_ref (die
, spec_attr
, spec_cu
);
17936 /* A convenience function to find the proper .debug_line section for a CU. */
17938 static struct dwarf2_section_info
*
17939 get_debug_line_section (struct dwarf2_cu
*cu
)
17941 struct dwarf2_section_info
*section
;
17942 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17944 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17946 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
17947 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
17948 else if (cu
->per_cu
->is_dwz
)
17950 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
17952 section
= &dwz
->line
;
17955 section
= &per_objfile
->per_bfd
->line
;
17960 /* Read the statement program header starting at OFFSET in
17961 .debug_line, or .debug_line.dwo. Return a pointer
17962 to a struct line_header, allocated using xmalloc.
17963 Returns NULL if there is a problem reading the header, e.g., if it
17964 has a version we don't understand.
17966 NOTE: the strings in the include directory and file name tables of
17967 the returned object point into the dwarf line section buffer,
17968 and must not be freed. */
17970 static line_header_up
17971 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
,
17972 const char *comp_dir
)
17974 struct dwarf2_section_info
*section
;
17975 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17977 section
= get_debug_line_section (cu
);
17978 section
->read (per_objfile
->objfile
);
17979 if (section
->buffer
== NULL
)
17981 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
17982 complaint (_("missing .debug_line.dwo section"));
17984 complaint (_("missing .debug_line section"));
17988 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
17989 per_objfile
, section
, &cu
->header
,
17993 /* Subroutine of dwarf_decode_lines to simplify it.
17994 Return the file name for the given file_entry.
17995 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17996 If space for the result is malloc'd, *NAME_HOLDER will be set.
17997 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17998 equivalent to CU_INFO. */
18000 static const char *
18001 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
18002 const file_and_directory
&cu_info
,
18003 std::string
&name_holder
)
18005 const char *include_name
= fe
.name
;
18006 const char *include_name_to_compare
= include_name
;
18008 const char *dir_name
= fe
.include_dir (lh
);
18010 std::string hold_compare
;
18011 if (!IS_ABSOLUTE_PATH (include_name
)
18012 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
18014 /* Avoid creating a duplicate name for CU_INFO.
18015 We do this by comparing INCLUDE_NAME and CU_INFO.
18016 Before we do the comparison, however, we need to account
18017 for DIR_NAME and COMP_DIR.
18018 First prepend dir_name (if non-NULL). If we still don't
18019 have an absolute path prepend comp_dir (if non-NULL).
18020 However, the directory we record in the include-file's
18021 psymtab does not contain COMP_DIR (to match the
18022 corresponding symtab(s)).
18027 bash$ gcc -g ./hello.c
18028 include_name = "hello.c"
18030 DW_AT_comp_dir = comp_dir = "/tmp"
18031 DW_AT_name = "./hello.c"
18035 if (dir_name
!= NULL
)
18037 name_holder
= path_join (dir_name
, include_name
);
18038 include_name
= name_holder
.c_str ();
18039 include_name_to_compare
= include_name
;
18041 if (!IS_ABSOLUTE_PATH (include_name
)
18042 && cu_info
.get_comp_dir () != nullptr)
18044 hold_compare
= path_join (cu_info
.get_comp_dir (), include_name
);
18045 include_name_to_compare
= hold_compare
.c_str ();
18049 std::string copied_name
;
18050 const char *cu_filename
= cu_info
.get_name ();
18051 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
18053 copied_name
= path_join (cu_info
.get_comp_dir (), cu_filename
);
18054 cu_filename
= copied_name
.c_str ();
18057 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
18059 return include_name
;
18062 /* State machine to track the state of the line number program. */
18064 class lnp_state_machine
18067 /* Initialize a machine state for the start of a line number
18069 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
18071 file_entry
*current_file ()
18073 /* lh->file_names is 0-based, but the file name numbers in the
18074 statement program are 1-based. */
18075 return m_line_header
->file_name_at (m_file
);
18078 /* Record the line in the state machine. END_SEQUENCE is true if
18079 we're processing the end of a sequence. */
18080 void record_line (bool end_sequence
);
18082 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
18083 nop-out rest of the lines in this sequence. */
18084 void check_line_address (struct dwarf2_cu
*cu
,
18085 const gdb_byte
*line_ptr
,
18086 unrelocated_addr unrelocated_lowpc
,
18087 unrelocated_addr address
);
18089 void handle_set_discriminator (unsigned int discriminator
)
18091 m_discriminator
= discriminator
;
18092 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
18095 /* Handle DW_LNE_set_address. */
18096 void handle_set_address (unrelocated_addr address
)
18100 = (unrelocated_addr
) gdbarch_adjust_dwarf2_line (m_gdbarch
,
18101 (CORE_ADDR
) address
,
18105 /* Handle DW_LNS_advance_pc. */
18106 void handle_advance_pc (CORE_ADDR adjust
);
18108 /* Handle a special opcode. */
18109 void handle_special_opcode (unsigned char op_code
);
18111 /* Handle DW_LNS_advance_line. */
18112 void handle_advance_line (int line_delta
)
18114 advance_line (line_delta
);
18117 /* Handle DW_LNS_set_file. */
18118 void handle_set_file (file_name_index file
);
18120 /* Handle DW_LNS_negate_stmt. */
18121 void handle_negate_stmt ()
18123 m_flags
^= LEF_IS_STMT
;
18126 /* Handle DW_LNS_const_add_pc. */
18127 void handle_const_add_pc ();
18129 /* Handle DW_LNS_fixed_advance_pc. */
18130 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
18132 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18133 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18137 /* Handle DW_LNS_copy. */
18138 void handle_copy ()
18140 record_line (false);
18141 m_discriminator
= 0;
18142 m_flags
&= ~LEF_PROLOGUE_END
;
18143 m_flags
&= ~LEF_EPILOGUE_BEGIN
;
18146 /* Handle DW_LNE_end_sequence. */
18147 void handle_end_sequence ()
18149 m_currently_recording_lines
= true;
18152 /* Handle DW_LNS_set_prologue_end. */
18153 void handle_set_prologue_end ()
18155 m_flags
|= LEF_PROLOGUE_END
;
18158 void handle_set_epilogue_begin ()
18160 m_flags
|= LEF_EPILOGUE_BEGIN
;
18164 /* Advance the line by LINE_DELTA. */
18165 void advance_line (int line_delta
)
18167 m_line
+= line_delta
;
18169 if (line_delta
!= 0)
18170 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
18173 struct dwarf2_cu
*m_cu
;
18175 gdbarch
*m_gdbarch
;
18177 /* The line number header. */
18178 line_header
*m_line_header
;
18180 /* These are part of the standard DWARF line number state machine,
18181 and initialized according to the DWARF spec. */
18183 unsigned char m_op_index
= 0;
18184 /* The line table index of the current file. */
18185 file_name_index m_file
= 1;
18186 unsigned int m_line
= 1;
18188 /* These are initialized in the constructor. */
18190 unrelocated_addr m_address
;
18191 linetable_entry_flags m_flags
;
18192 unsigned int m_discriminator
= 0;
18194 /* Additional bits of state we need to track. */
18196 /* The last file that we called dwarf2_start_subfile for.
18197 This is only used for TLLs. */
18198 unsigned int m_last_file
= 0;
18199 /* The last file a line number was recorded for. */
18200 struct subfile
*m_last_subfile
= NULL
;
18202 /* The address of the last line entry. */
18203 unrelocated_addr m_last_address
;
18205 /* Set to true when a previous line at the same address (using
18206 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18207 when a line entry at a new address (m_address different to
18208 m_last_address) is processed. */
18209 bool m_stmt_at_address
= false;
18211 /* When true, record the lines we decode. */
18212 bool m_currently_recording_lines
= true;
18214 /* The last line number that was recorded, used to coalesce
18215 consecutive entries for the same line. This can happen, for
18216 example, when discriminators are present. PR 17276. */
18217 unsigned int m_last_line
= 0;
18218 bool m_line_has_non_zero_discriminator
= false;
18222 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
18224 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
18225 / m_line_header
->maximum_ops_per_instruction
)
18226 * m_line_header
->minimum_instruction_length
);
18227 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18228 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18229 m_op_index
= ((m_op_index
+ adjust
)
18230 % m_line_header
->maximum_ops_per_instruction
);
18234 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
18236 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
18237 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
18238 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
18239 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
18240 / m_line_header
->maximum_ops_per_instruction
)
18241 * m_line_header
->minimum_instruction_length
);
18242 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18243 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18244 m_op_index
= ((m_op_index
+ adj_opcode_d
)
18245 % m_line_header
->maximum_ops_per_instruction
);
18247 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
18248 advance_line (line_delta
);
18249 record_line (false);
18250 m_discriminator
= 0;
18251 m_flags
&= ~LEF_PROLOGUE_END
;
18252 m_flags
&= ~LEF_EPILOGUE_BEGIN
;
18256 lnp_state_machine::handle_set_file (file_name_index file
)
18260 const file_entry
*fe
= current_file ();
18262 dwarf2_debug_line_missing_file_complaint ();
18265 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
18266 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
18267 dwarf2_start_subfile (m_cu
, *fe
, *m_line_header
);
18272 lnp_state_machine::handle_const_add_pc ()
18275 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
18278 = (((m_op_index
+ adjust
)
18279 / m_line_header
->maximum_ops_per_instruction
)
18280 * m_line_header
->minimum_instruction_length
);
18282 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18283 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18284 m_op_index
= ((m_op_index
+ adjust
)
18285 % m_line_header
->maximum_ops_per_instruction
);
18288 /* Return non-zero if we should add LINE to the line number table.
18289 LINE is the line to add, LAST_LINE is the last line that was added,
18290 LAST_SUBFILE is the subfile for LAST_LINE.
18291 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18292 had a non-zero discriminator.
18294 We have to be careful in the presence of discriminators.
18295 E.g., for this line:
18297 for (i = 0; i < 100000; i++);
18299 clang can emit four line number entries for that one line,
18300 each with a different discriminator.
18301 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18303 However, we want gdb to coalesce all four entries into one.
18304 Otherwise the user could stepi into the middle of the line and
18305 gdb would get confused about whether the pc really was in the
18306 middle of the line.
18308 Things are further complicated by the fact that two consecutive
18309 line number entries for the same line is a heuristic used by gcc
18310 to denote the end of the prologue. So we can't just discard duplicate
18311 entries, we have to be selective about it. The heuristic we use is
18312 that we only collapse consecutive entries for the same line if at least
18313 one of those entries has a non-zero discriminator. PR 17276.
18315 Note: Addresses in the line number state machine can never go backwards
18316 within one sequence, thus this coalescing is ok. */
18319 dwarf_record_line_p (struct dwarf2_cu
*cu
,
18320 unsigned int line
, unsigned int last_line
,
18321 int line_has_non_zero_discriminator
,
18322 struct subfile
*last_subfile
)
18324 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
18326 if (line
!= last_line
)
18328 /* Same line for the same file that we've seen already.
18329 As a last check, for pr 17276, only record the line if the line
18330 has never had a non-zero discriminator. */
18331 if (!line_has_non_zero_discriminator
)
18336 /* Use the CU's builder to record line number LINE beginning at
18337 address ADDRESS in the line table of subfile SUBFILE. */
18340 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
18341 unsigned int line
, unrelocated_addr address
,
18342 linetable_entry_flags flags
,
18343 struct dwarf2_cu
*cu
)
18345 unrelocated_addr addr
18346 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch
,
18347 (CORE_ADDR
) address
));
18349 if (dwarf_line_debug
)
18351 gdb_printf (gdb_stdlog
,
18352 "Recording line %u, file %s, address %s\n",
18353 line
, lbasename (subfile
->name
.c_str ()),
18354 paddress (gdbarch
, (CORE_ADDR
) address
));
18358 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
18361 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18362 Mark the end of a set of line number records.
18363 The arguments are the same as for dwarf_record_line_1.
18364 If SUBFILE is NULL the request is ignored. */
18367 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
18368 unrelocated_addr address
, struct dwarf2_cu
*cu
)
18370 if (subfile
== NULL
)
18373 if (dwarf_line_debug
)
18375 gdb_printf (gdb_stdlog
,
18376 "Finishing current line, file %s, address %s\n",
18377 lbasename (subfile
->name
.c_str ()),
18378 paddress (gdbarch
, (CORE_ADDR
) address
));
18381 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
18385 lnp_state_machine::record_line (bool end_sequence
)
18387 if (dwarf_line_debug
)
18389 gdb_printf (gdb_stdlog
,
18390 "Processing actual line %u: file %u,"
18391 " address %s, is_stmt %u, prologue_end %u,"
18392 " epilogue_begin %u, discrim %u%s\n",
18394 paddress (m_gdbarch
, (CORE_ADDR
) m_address
),
18395 (m_flags
& LEF_IS_STMT
) != 0,
18396 (m_flags
& LEF_PROLOGUE_END
) != 0,
18397 (m_flags
& LEF_EPILOGUE_BEGIN
) != 0,
18399 (end_sequence
? "\t(end sequence)" : ""));
18402 file_entry
*fe
= current_file ();
18405 dwarf2_debug_line_missing_file_complaint ();
18406 /* For now we ignore lines not starting on an instruction boundary.
18407 But not when processing end_sequence for compatibility with the
18408 previous version of the code. */
18409 else if (m_op_index
== 0 || end_sequence
)
18411 /* When we switch files we insert an end maker in the first file,
18412 switch to the second file and add a new line entry. The
18413 problem is that the end marker inserted in the first file will
18414 discard any previous line entries at the same address. If the
18415 line entries in the first file are marked as is-stmt, while
18416 the new line in the second file is non-stmt, then this means
18417 the end marker will discard is-stmt lines so we can have a
18418 non-stmt line. This means that there are less addresses at
18419 which the user can insert a breakpoint.
18421 To improve this we track the last address in m_last_address,
18422 and whether we have seen an is-stmt at this address. Then
18423 when switching files, if we have seen a stmt at the current
18424 address, and we are switching to create a non-stmt line, then
18425 discard the new line. */
18427 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
18428 bool ignore_this_line
18429 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
18430 && ((m_flags
& LEF_IS_STMT
) == 0)
18431 && m_stmt_at_address
)
18432 || (!end_sequence
&& m_line
== 0));
18434 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
18436 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
18437 m_currently_recording_lines
? m_cu
: nullptr);
18440 if (!end_sequence
&& !ignore_this_line
)
18442 linetable_entry_flags lte_flags
= m_flags
;
18443 if (producer_is_codewarrior (m_cu
))
18444 lte_flags
|= LEF_IS_STMT
;
18446 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
18447 m_line_has_non_zero_discriminator
,
18450 buildsym_compunit
*builder
= m_cu
->get_builder ();
18451 dwarf_record_line_1 (m_gdbarch
,
18452 builder
->get_current_subfile (),
18453 m_line
, m_address
, lte_flags
,
18454 m_currently_recording_lines
? m_cu
: nullptr);
18456 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
18457 m_last_line
= m_line
;
18461 /* Track whether we have seen any IS_STMT true at m_address in case we
18462 have multiple line table entries all at m_address. */
18463 if (m_last_address
!= m_address
)
18465 m_stmt_at_address
= false;
18466 m_last_address
= m_address
;
18468 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
18471 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
18475 m_line_header (lh
),
18476 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18477 if there was a line entry for it so that the backend has a
18478 chance to adjust it and also record it in case it needs it.
18479 This is currently used by MIPS code,
18480 cf. `mips_adjust_dwarf2_line'. */
18481 m_address ((unrelocated_addr
) gdbarch_adjust_dwarf2_line (arch
, 0, 0)),
18482 m_flags (lh
->default_is_stmt
? LEF_IS_STMT
: (linetable_entry_flags
) 0),
18483 m_last_address (m_address
)
18488 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
18489 const gdb_byte
*line_ptr
,
18490 unrelocated_addr unrelocated_lowpc
,
18491 unrelocated_addr address
)
18493 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18494 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18495 located at 0x0. In this case, additionally check that if
18496 ADDRESS < UNRELOCATED_LOWPC. */
18498 if ((address
== (unrelocated_addr
) 0 && address
< unrelocated_lowpc
)
18499 || address
== (unrelocated_addr
) -1)
18501 /* This line table is for a function which has been
18502 GCd by the linker. Ignore it. PR gdb/12528 */
18504 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
18505 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
18507 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18508 line_offset
, objfile_name (objfile
));
18509 m_currently_recording_lines
= false;
18510 /* Note: m_currently_recording_lines is left as false until we see
18511 DW_LNE_end_sequence. */
18515 /* Subroutine of dwarf_decode_lines to simplify it.
18516 Process the line number information in LH. */
18519 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
18520 unrelocated_addr lowpc
)
18522 const gdb_byte
*line_ptr
, *extended_end
;
18523 const gdb_byte
*line_end
;
18524 unsigned int bytes_read
, extended_len
;
18525 unsigned char op_code
, extended_op
;
18526 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
18527 bfd
*abfd
= objfile
->obfd
.get ();
18528 struct gdbarch
*gdbarch
= objfile
->arch ();
18530 line_ptr
= lh
->statement_program_start
;
18531 line_end
= lh
->statement_program_end
;
18533 /* Read the statement sequences until there's nothing left. */
18534 while (line_ptr
< line_end
)
18536 /* The DWARF line number program state machine. Reset the state
18537 machine at the start of each sequence. */
18538 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
18539 bool end_sequence
= false;
18541 /* Start a subfile for the current file of the state
18543 const file_entry
*fe
= state_machine
.current_file ();
18546 dwarf2_start_subfile (cu
, *fe
, *lh
);
18548 /* Decode the table. */
18549 while (line_ptr
< line_end
&& !end_sequence
)
18551 op_code
= read_1_byte (abfd
, line_ptr
);
18554 if (op_code
>= lh
->opcode_base
)
18556 /* Special opcode. */
18557 state_machine
.handle_special_opcode (op_code
);
18559 else switch (op_code
)
18561 case DW_LNS_extended_op
:
18562 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
18564 line_ptr
+= bytes_read
;
18565 extended_end
= line_ptr
+ extended_len
;
18566 extended_op
= read_1_byte (abfd
, line_ptr
);
18568 if (DW_LNE_lo_user
<= extended_op
18569 && extended_op
<= DW_LNE_hi_user
)
18571 /* Vendor extension, ignore. */
18572 line_ptr
= extended_end
;
18575 switch (extended_op
)
18577 case DW_LNE_end_sequence
:
18578 state_machine
.handle_end_sequence ();
18579 end_sequence
= true;
18581 case DW_LNE_set_address
:
18583 unrelocated_addr address
18584 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
18585 line_ptr
+= bytes_read
;
18587 state_machine
.check_line_address (cu
, line_ptr
, lowpc
,
18589 state_machine
.handle_set_address (address
);
18592 case DW_LNE_define_file
:
18594 const char *cur_file
;
18595 unsigned int mod_time
, length
;
18598 cur_file
= read_direct_string (abfd
, line_ptr
,
18600 line_ptr
+= bytes_read
;
18601 dindex
= (dir_index
)
18602 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18603 line_ptr
+= bytes_read
;
18605 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18606 line_ptr
+= bytes_read
;
18608 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18609 line_ptr
+= bytes_read
;
18610 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
18613 case DW_LNE_set_discriminator
:
18615 /* The discriminator is not interesting to the
18616 debugger; just ignore it. We still need to
18617 check its value though:
18618 if there are consecutive entries for the same
18619 (non-prologue) line we want to coalesce them.
18622 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18623 line_ptr
+= bytes_read
;
18625 state_machine
.handle_set_discriminator (discr
);
18629 complaint (_("mangled .debug_line section"));
18632 /* Make sure that we parsed the extended op correctly. If e.g.
18633 we expected a different address size than the producer used,
18634 we may have read the wrong number of bytes. */
18635 if (line_ptr
!= extended_end
)
18637 complaint (_("mangled .debug_line section"));
18642 state_machine
.handle_copy ();
18644 case DW_LNS_advance_pc
:
18647 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18648 line_ptr
+= bytes_read
;
18650 state_machine
.handle_advance_pc (adjust
);
18653 case DW_LNS_advance_line
:
18656 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
18657 line_ptr
+= bytes_read
;
18659 state_machine
.handle_advance_line (line_delta
);
18662 case DW_LNS_set_file
:
18664 file_name_index file
18665 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
18667 line_ptr
+= bytes_read
;
18669 state_machine
.handle_set_file (file
);
18672 case DW_LNS_set_column
:
18673 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18674 line_ptr
+= bytes_read
;
18676 case DW_LNS_negate_stmt
:
18677 state_machine
.handle_negate_stmt ();
18679 case DW_LNS_set_basic_block
:
18681 /* Add to the address register of the state machine the
18682 address increment value corresponding to special opcode
18683 255. I.e., this value is scaled by the minimum
18684 instruction length since special opcode 255 would have
18685 scaled the increment. */
18686 case DW_LNS_const_add_pc
:
18687 state_machine
.handle_const_add_pc ();
18689 case DW_LNS_fixed_advance_pc
:
18691 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
18694 state_machine
.handle_fixed_advance_pc (addr_adj
);
18697 case DW_LNS_set_prologue_end
:
18698 state_machine
.handle_set_prologue_end ();
18700 case DW_LNS_set_epilogue_begin
:
18701 state_machine
.handle_set_epilogue_begin ();
18705 /* Unknown standard opcode, ignore it. */
18708 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
18710 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18711 line_ptr
+= bytes_read
;
18718 dwarf2_debug_line_missing_end_sequence_complaint ();
18720 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18721 in which case we still finish recording the last line). */
18722 state_machine
.record_line (true);
18726 /* Decode the Line Number Program (LNP) for the given line_header
18727 structure and CU. The actual information extracted and the type
18728 of structures created from the LNP depends on the value of PST.
18730 FND holds the CU file name and directory, if known.
18731 It is used for relative paths in the line table.
18733 NOTE: It is important that psymtabs have the same file name (via
18734 strcmp) as the corresponding symtab. Since the directory is not
18735 used in the name of the symtab we don't use it in the name of the
18736 psymtabs we create. E.g. expand_line_sal requires this when
18737 finding psymtabs to expand. A good testcase for this is
18740 LOWPC is the lowest address in CU (or 0 if not known).
18742 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18743 for its PC<->lines mapping information. Otherwise only the filename
18744 table is read in. */
18747 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
18748 unrelocated_addr lowpc
, int decode_mapping
)
18750 if (decode_mapping
)
18751 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
18753 /* Make sure a symtab is created for every file, even files
18754 which contain only variables (i.e. no code with associated
18756 buildsym_compunit
*builder
= cu
->get_builder ();
18757 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
18759 for (auto &fe
: lh
->file_names ())
18761 dwarf2_start_subfile (cu
, fe
, *lh
);
18762 subfile
*sf
= builder
->get_current_subfile ();
18764 if (sf
->symtab
== nullptr)
18765 sf
->symtab
= allocate_symtab (cust
, sf
->name
.c_str (),
18766 sf
->name_for_id
.c_str ());
18768 fe
.symtab
= sf
->symtab
;
18772 /* Start a subfile for DWARF. FILENAME is the name of the file and
18773 DIRNAME the name of the source directory which contains FILENAME
18774 or NULL if not known.
18775 This routine tries to keep line numbers from identical absolute and
18776 relative file names in a common subfile.
18778 Using the `list' example from the GDB testsuite, which resides in
18779 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18780 of /srcdir/list0.c yields the following debugging information for list0.c:
18782 DW_AT_name: /srcdir/list0.c
18783 DW_AT_comp_dir: /compdir
18784 files.files[0].name: list0.h
18785 files.files[0].dir: /srcdir
18786 files.files[1].name: list0.c
18787 files.files[1].dir: /srcdir
18789 The line number information for list0.c has to end up in a single
18790 subfile, so that `break /srcdir/list0.c:1' works as expected.
18791 start_subfile will ensure that this happens provided that we pass the
18792 concatenation of files.files[1].dir and files.files[1].name as the
18796 dwarf2_start_subfile (dwarf2_cu
*cu
, const file_entry
&fe
,
18797 const line_header
&lh
)
18799 std::string filename_holder
;
18800 const char *filename
= fe
.name
;
18801 const char *dirname
= lh
.include_dir_at (fe
.d_index
);
18803 /* In order not to lose the line information directory,
18804 we concatenate it to the filename when it makes sense.
18805 Note that the Dwarf3 standard says (speaking of filenames in line
18806 information): ``The directory index is ignored for file names
18807 that represent full path names''. Thus ignoring dirname in the
18808 `else' branch below isn't an issue. */
18810 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
18812 filename_holder
= path_join (dirname
, filename
);
18813 filename
= filename_holder
.c_str ();
18816 std::string filename_for_id
= lh
.file_file_name (fe
);
18817 cu
->get_builder ()->start_subfile (filename
, filename_for_id
.c_str ());
18821 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
18822 struct dwarf2_cu
*cu
)
18824 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
18825 struct comp_unit_head
*cu_header
= &cu
->header
;
18827 /* NOTE drow/2003-01-30: There used to be a comment and some special
18828 code here to turn a symbol with DW_AT_external and a
18829 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18830 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18831 with some versions of binutils) where shared libraries could have
18832 relocations against symbols in their debug information - the
18833 minimal symbol would have the right address, but the debug info
18834 would not. It's no longer necessary, because we will explicitly
18835 apply relocations when we read in the debug information now. */
18837 /* A DW_AT_location attribute with no contents indicates that a
18838 variable has been optimized away. */
18839 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
18841 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
18845 /* Handle one degenerate form of location expression specially, to
18846 preserve GDB's previous behavior when section offsets are
18847 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18848 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18850 if (attr
->form_is_block ())
18852 struct dwarf_block
*block
= attr
->as_block ();
18854 if ((block
->data
[0] == DW_OP_addr
18855 && block
->size
== 1 + cu_header
->addr_size
)
18856 || ((block
->data
[0] == DW_OP_GNU_addr_index
18857 || block
->data
[0] == DW_OP_addrx
)
18859 == 1 + leb128_size (&block
->data
[1]))))
18861 unsigned int dummy
;
18863 unrelocated_addr tem
;
18864 if (block
->data
[0] == DW_OP_addr
)
18865 tem
= cu
->header
.read_address (objfile
->obfd
.get (),
18869 tem
= read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
);
18870 sym
->set_value_address ((CORE_ADDR
) tem
);
18871 sym
->set_aclass_index (LOC_STATIC
);
18872 fixup_symbol_section (sym
, objfile
);
18873 sym
->set_value_address
18874 (sym
->value_address ()
18875 + objfile
->section_offsets
[sym
->section_index ()]);
18880 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18881 expression evaluator, and use LOC_COMPUTED only when necessary
18882 (i.e. when the value of a register or memory location is
18883 referenced, or a thread-local block, etc.). Then again, it might
18884 not be worthwhile. I'm assuming that it isn't unless performance
18885 or memory numbers show me otherwise. */
18887 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
18889 if (SYMBOL_COMPUTED_OPS (sym
)->location_has_loclist
)
18890 cu
->has_loclist
= true;
18893 /* A helper function to add an "export" symbol. The new symbol starts
18894 as a clone of ORIG, but is modified to defer to the symbol named
18895 ORIG_NAME. The original symbol uses the name given in the source
18896 code, and the symbol that is created here uses the linkage name as
18897 its name. See ada-imported.c. */
18900 add_ada_export_symbol (struct symbol
*orig
, const char *new_name
,
18901 const char *orig_name
, struct dwarf2_cu
*cu
,
18902 struct pending
**list_to_add
)
18904 struct symbol
*copy
18905 = new (&cu
->per_objfile
->objfile
->objfile_obstack
) symbol (*orig
);
18906 copy
->set_linkage_name (new_name
);
18907 SYMBOL_LOCATION_BATON (copy
) = (void *) orig_name
;
18908 copy
->set_aclass_index (copy
->aclass () == LOC_BLOCK
18910 : ada_imported_index
);
18911 add_symbol_to_list (copy
, list_to_add
);
18914 /* A helper function that decides if a given symbol is an Ada Pragma
18915 Import or Pragma Export. */
18918 is_ada_import_or_export (dwarf2_cu
*cu
, const char *name
,
18919 const char *linkagename
)
18921 return (cu
->lang () == language_ada
18922 && linkagename
!= nullptr
18923 && !streq (name
, linkagename
)
18924 /* The following exclusions are necessary because symbols
18925 with names or linkage names that match here will meet the
18926 other criteria but are not in fact caused by Pragma
18927 Import or Pragma Export, and applying the import/export
18928 treatment to them will introduce problems. Some of these
18929 checks only apply to functions, but it is simpler and
18930 harmless to always do them all. */
18931 && !startswith (name
, "__builtin")
18932 && !startswith (linkagename
, "___ghost_")
18933 && !startswith (linkagename
, "__gnat")
18934 && !startswith (linkagename
, "_ada_")
18935 && !streq (linkagename
, "adainit"));
18938 /* Given a pointer to a DWARF information entry, figure out if we need
18939 to make a symbol table entry for it, and if so, create a new entry
18940 and return a pointer to it.
18941 If TYPE is NULL, determine symbol type from the die, otherwise
18942 used the passed type.
18943 If SPACE is not NULL, use it to hold the new symbol. If it is
18944 NULL, allocate a new symbol on the objfile's obstack. */
18946 static struct symbol
*
18947 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
18948 struct symbol
*space
)
18950 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18951 struct objfile
*objfile
= per_objfile
->objfile
;
18952 struct symbol
*sym
= NULL
;
18954 struct attribute
*attr
= NULL
;
18955 struct attribute
*attr2
= NULL
;
18956 struct pending
**list_to_add
= NULL
;
18958 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
18960 name
= dwarf2_name (die
, cu
);
18961 if (name
== nullptr && (die
->tag
== DW_TAG_subprogram
18962 || die
->tag
== DW_TAG_inlined_subroutine
18963 || die
->tag
== DW_TAG_entry_point
))
18964 name
= dw2_linkage_name (die
, cu
);
18968 int suppress_add
= 0;
18973 sym
= new (&objfile
->objfile_obstack
) symbol
;
18974 OBJSTAT (objfile
, n_syms
++);
18976 /* Cache this symbol's name and the name's demangled form (if any). */
18977 sym
->set_language (cu
->lang (), &objfile
->objfile_obstack
);
18978 /* Fortran does not have mangling standard and the mangling does differ
18979 between gfortran, iFort etc. */
18980 const char *physname
18981 = (cu
->lang () == language_fortran
18982 ? dwarf2_full_name (name
, die
, cu
)
18983 : dwarf2_physname (name
, die
, cu
));
18984 const char *linkagename
= dw2_linkage_name (die
, cu
);
18986 if (linkagename
== nullptr || cu
->lang () == language_ada
)
18987 sym
->set_linkage_name (physname
);
18990 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
18991 sym
->set_linkage_name (linkagename
);
18994 /* Handle DW_AT_artificial. */
18995 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
18996 if (attr
!= nullptr)
18997 sym
->set_is_artificial (attr
->as_boolean ());
18999 /* Default assumptions.
19000 Use the passed type or decode it from the die. */
19001 sym
->set_domain (VAR_DOMAIN
);
19002 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
19004 sym
->set_type (type
);
19006 sym
->set_type (die_type (die
, cu
));
19007 attr
= dwarf2_attr (die
,
19008 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
19010 if (attr
!= nullptr)
19011 sym
->set_line (attr
->constant_value (0));
19013 attr
= dwarf2_attr (die
,
19014 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
19016 if (attr
!= nullptr && attr
->is_nonnegative ())
19018 file_name_index file_index
19019 = (file_name_index
) attr
->as_nonnegative ();
19020 struct file_entry
*fe
;
19022 if (cu
->line_header
!= NULL
)
19023 fe
= cu
->line_header
->file_name_at (file_index
);
19028 complaint (_("file index out of range"));
19030 sym
->set_symtab (fe
->symtab
);
19036 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
19037 if (attr
!= nullptr)
19039 CORE_ADDR addr
= per_objfile
->relocate (attr
->as_address ());
19040 sym
->set_section_index (SECT_OFF_TEXT (objfile
));
19041 sym
->set_value_address (addr
);
19042 sym
->set_aclass_index (LOC_LABEL
);
19045 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
19046 sym
->set_type (builtin_type (objfile
)->builtin_core_addr
);
19047 sym
->set_domain (LABEL_DOMAIN
);
19048 add_symbol_to_list (sym
, cu
->list_in_scope
);
19050 case DW_TAG_entry_point
:
19051 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19053 sym
->set_aclass_index (LOC_BLOCK
);
19054 /* DW_TAG_entry_point provides an additional entry_point to an
19055 existing sub_program. Therefore, we inherit the "external"
19056 attribute from the sub_program to which the entry_point
19058 attr2
= dwarf2_attr (die
->parent
, DW_AT_external
, cu
);
19059 if (attr2
!= nullptr && attr2
->as_boolean ())
19060 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19062 list_to_add
= cu
->list_in_scope
;
19064 case DW_TAG_subprogram
:
19065 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19067 sym
->set_aclass_index (LOC_BLOCK
);
19068 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
19069 if ((attr2
!= nullptr && attr2
->as_boolean ())
19070 || cu
->lang () == language_ada
19071 || cu
->lang () == language_fortran
)
19073 /* Subprograms marked external are stored as a global symbol.
19074 Ada and Fortran subprograms, whether marked external or
19075 not, are always stored as a global symbol, because we want
19076 to be able to access them globally. For instance, we want
19077 to be able to break on a nested subprogram without having
19078 to specify the context. */
19079 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19083 list_to_add
= cu
->list_in_scope
;
19086 if (is_ada_import_or_export (cu
, name
, linkagename
))
19088 /* This is either a Pragma Import or Export. They can
19089 be distinguished by the declaration flag. */
19090 sym
->set_linkage_name (name
);
19091 if (die_is_declaration (die
, cu
))
19093 /* For Import, create a symbol using the source
19094 name, and have it refer to the linkage name. */
19095 SYMBOL_LOCATION_BATON (sym
) = (void *) linkagename
;
19096 sym
->set_aclass_index (ada_block_index
);
19100 /* For Export, create a symbol using the source
19101 name, then create a second symbol that refers
19103 add_ada_export_symbol (sym
, linkagename
, name
, cu
,
19108 case DW_TAG_inlined_subroutine
:
19109 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
19111 sym
->set_aclass_index (LOC_BLOCK
);
19112 sym
->set_is_inlined (1);
19113 list_to_add
= cu
->list_in_scope
;
19115 case DW_TAG_template_value_param
:
19118 case DW_TAG_constant
:
19119 case DW_TAG_variable
:
19120 case DW_TAG_member
:
19121 /* Compilation with minimal debug info may result in
19122 variables with missing type entries. Change the
19123 misleading `void' type to something sensible. */
19124 if (sym
->type ()->code () == TYPE_CODE_VOID
)
19125 sym
->set_type (builtin_type (objfile
)->builtin_int
);
19127 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
19128 /* In the case of DW_TAG_member, we should only be called for
19129 static const members. */
19130 if (die
->tag
== DW_TAG_member
)
19132 /* dwarf2_add_field uses die_is_declaration,
19133 so we do the same. */
19134 gdb_assert (die_is_declaration (die
, cu
));
19137 if (attr
!= nullptr)
19139 dwarf2_const_value (attr
, sym
, cu
);
19140 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
19143 if (attr2
!= nullptr && attr2
->as_boolean ())
19144 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19146 list_to_add
= cu
->list_in_scope
;
19150 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
19151 if (attr
!= nullptr)
19153 var_decode_location (attr
, sym
, cu
);
19154 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
19156 /* Fortran explicitly imports any global symbols to the local
19157 scope by DW_TAG_common_block. */
19158 if (cu
->lang () == language_fortran
&& die
->parent
19159 && die
->parent
->tag
== DW_TAG_common_block
)
19162 if (sym
->aclass () == LOC_STATIC
19163 && sym
->value_address () == 0
19164 && !per_objfile
->per_bfd
->has_section_at_zero
)
19166 /* When a static variable is eliminated by the linker,
19167 the corresponding debug information is not stripped
19168 out, but the variable address is set to null;
19169 do not add such variables into symbol table. */
19171 else if (attr2
!= nullptr && attr2
->as_boolean ())
19173 if (sym
->aclass () == LOC_STATIC
19174 && (objfile
->flags
& OBJF_MAINLINE
) == 0
19175 && per_objfile
->per_bfd
->can_copy
)
19177 /* A global static variable might be subject to
19178 copy relocation. We first check for a local
19179 minsym, though, because maybe the symbol was
19180 marked hidden, in which case this would not
19182 bound_minimal_symbol found
19183 = (lookup_minimal_symbol_linkage
19184 (sym
->linkage_name (), objfile
));
19185 if (found
.minsym
!= nullptr)
19186 sym
->maybe_copied
= 1;
19189 /* A variable with DW_AT_external is never static,
19190 but it may be block-scoped. */
19192 = ((cu
->list_in_scope
19193 == cu
->get_builder ()->get_file_symbols ())
19194 ? cu
->get_builder ()->get_global_symbols ()
19195 : cu
->list_in_scope
);
19198 list_to_add
= cu
->list_in_scope
;
19200 if (list_to_add
!= nullptr
19201 && is_ada_import_or_export (cu
, name
, linkagename
))
19203 /* This is a Pragma Export. A Pragma Import won't
19204 be seen here, because it will not have a location
19205 and so will be handled below. */
19206 add_ada_export_symbol (sym
, name
, linkagename
, cu
,
19212 /* We do not know the address of this symbol.
19213 If it is an external symbol and we have type information
19214 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19215 The address of the variable will then be determined from
19216 the minimal symbol table whenever the variable is
19218 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
19220 /* Fortran explicitly imports any global symbols to the local
19221 scope by DW_TAG_common_block. */
19222 if (cu
->lang () == language_fortran
&& die
->parent
19223 && die
->parent
->tag
== DW_TAG_common_block
)
19225 /* SYMBOL_CLASS doesn't matter here because
19226 read_common_block is going to reset it. */
19228 list_to_add
= cu
->list_in_scope
;
19230 else if (is_ada_import_or_export (cu
, name
, linkagename
))
19232 /* This is a Pragma Import. A Pragma Export won't
19233 be seen here, because it will have a location and
19234 so will be handled above. */
19235 sym
->set_linkage_name (name
);
19236 list_to_add
= cu
->list_in_scope
;
19237 SYMBOL_LOCATION_BATON (sym
) = (void *) linkagename
;
19238 sym
->set_aclass_index (ada_imported_index
);
19240 else if (attr2
!= nullptr && attr2
->as_boolean ()
19241 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
19243 /* A variable with DW_AT_external is never static, but it
19244 may be block-scoped. */
19246 = ((cu
->list_in_scope
19247 == cu
->get_builder ()->get_file_symbols ())
19248 ? cu
->get_builder ()->get_global_symbols ()
19249 : cu
->list_in_scope
);
19251 sym
->set_aclass_index (LOC_UNRESOLVED
);
19253 else if (!die_is_declaration (die
, cu
))
19255 /* Use the default LOC_OPTIMIZED_OUT class. */
19256 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
19258 list_to_add
= cu
->list_in_scope
;
19262 case DW_TAG_formal_parameter
:
19264 /* If we are inside a function, mark this as an argument. If
19265 not, we might be looking at an argument to an inlined function
19266 when we do not have enough information to show inlined frames;
19267 pretend it's a local variable in that case so that the user can
19269 struct context_stack
*curr
19270 = cu
->get_builder ()->get_current_context_stack ();
19271 if (curr
!= nullptr && curr
->name
!= nullptr)
19272 sym
->set_is_argument (1);
19273 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
19274 if (attr
!= nullptr)
19276 var_decode_location (attr
, sym
, cu
);
19278 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
19279 if (attr
!= nullptr)
19281 dwarf2_const_value (attr
, sym
, cu
);
19284 list_to_add
= cu
->list_in_scope
;
19287 case DW_TAG_unspecified_parameters
:
19288 /* From varargs functions; gdb doesn't seem to have any
19289 interest in this information, so just ignore it for now.
19292 case DW_TAG_template_type_param
:
19295 case DW_TAG_class_type
:
19296 case DW_TAG_interface_type
:
19297 case DW_TAG_structure_type
:
19298 case DW_TAG_union_type
:
19299 case DW_TAG_set_type
:
19300 case DW_TAG_enumeration_type
:
19301 case DW_TAG_namelist
:
19302 if (die
->tag
== DW_TAG_namelist
)
19304 sym
->set_aclass_index (LOC_STATIC
);
19305 sym
->set_domain (VAR_DOMAIN
);
19309 sym
->set_aclass_index (LOC_TYPEDEF
);
19310 sym
->set_domain (STRUCT_DOMAIN
);
19313 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19314 really ever be static objects: otherwise, if you try
19315 to, say, break of a class's method and you're in a file
19316 which doesn't mention that class, it won't work unless
19317 the check for all static symbols in lookup_symbol_aux
19318 saves you. See the OtherFileClass tests in
19319 gdb.c++/namespace.exp. */
19323 buildsym_compunit
*builder
= cu
->get_builder ();
19325 = (cu
->list_in_scope
== builder
->get_file_symbols ()
19326 && cu
->lang () == language_cplus
19327 ? builder
->get_global_symbols ()
19328 : cu
->list_in_scope
);
19330 /* The semantics of C++ state that "struct foo {
19331 ... }" also defines a typedef for "foo". */
19332 if (cu
->lang () == language_cplus
19333 || cu
->lang () == language_ada
19334 || cu
->lang () == language_d
19335 || cu
->lang () == language_rust
)
19337 /* The symbol's name is already allocated along
19338 with this objfile, so we don't need to
19339 duplicate it for the type. */
19340 if (sym
->type ()->name () == 0)
19341 sym
->type ()->set_name (sym
->search_name ());
19345 case DW_TAG_unspecified_type
:
19346 if (cu
->lang () == language_ada
)
19349 case DW_TAG_typedef
:
19350 case DW_TAG_array_type
:
19351 case DW_TAG_base_type
:
19352 case DW_TAG_subrange_type
:
19353 case DW_TAG_generic_subrange
:
19354 sym
->set_aclass_index (LOC_TYPEDEF
);
19355 sym
->set_domain (VAR_DOMAIN
);
19356 list_to_add
= cu
->list_in_scope
;
19358 case DW_TAG_enumerator
:
19359 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
19360 if (attr
!= nullptr)
19362 dwarf2_const_value (attr
, sym
, cu
);
19365 /* NOTE: carlton/2003-11-10: See comment above in the
19366 DW_TAG_class_type, etc. block. */
19369 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
19370 && cu
->lang () == language_cplus
19371 ? cu
->get_builder ()->get_global_symbols ()
19372 : cu
->list_in_scope
);
19374 case DW_TAG_imported_declaration
:
19375 case DW_TAG_namespace
:
19376 sym
->set_aclass_index (LOC_TYPEDEF
);
19377 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19379 case DW_TAG_module
:
19380 sym
->set_aclass_index (LOC_TYPEDEF
);
19381 sym
->set_domain (MODULE_DOMAIN
);
19382 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19384 case DW_TAG_common_block
:
19385 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
19386 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
19387 add_symbol_to_list (sym
, cu
->list_in_scope
);
19390 /* Not a tag we recognize. Hopefully we aren't processing
19391 trash data, but since we must specifically ignore things
19392 we don't recognize, there is nothing else we should do at
19394 complaint (_("unsupported tag: '%s'"),
19395 dwarf_tag_name (die
->tag
));
19401 sym
->hash_next
= objfile
->template_symbols
;
19402 objfile
->template_symbols
= sym
;
19403 list_to_add
= NULL
;
19406 if (list_to_add
!= NULL
)
19407 add_symbol_to_list (sym
, list_to_add
);
19409 /* For the benefit of old versions of GCC, check for anonymous
19410 namespaces based on the demangled name. */
19411 if (!cu
->processing_has_namespace_info
19412 && cu
->lang () == language_cplus
)
19413 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
19418 /* Given an attr with a DW_FORM_dataN value in host byte order,
19419 zero-extend it as appropriate for the symbol's type. The DWARF
19420 standard (v4) is not entirely clear about the meaning of using
19421 DW_FORM_dataN for a constant with a signed type, where the type is
19422 wider than the data. The conclusion of a discussion on the DWARF
19423 list was that this is unspecified. We choose to always zero-extend
19424 because that is the interpretation long in use by GCC. */
19427 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
19428 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
19430 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19431 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
.get ()) ?
19432 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
19433 LONGEST l
= attr
->constant_value (0);
19435 if (bits
< sizeof (*value
) * 8)
19437 l
&= ((LONGEST
) 1 << bits
) - 1;
19440 else if (bits
== sizeof (*value
) * 8)
19444 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
19445 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
19452 /* Read a constant value from an attribute. Either set *VALUE, or if
19453 the value does not fit in *VALUE, set *BYTES - either already
19454 allocated on the objfile obstack, or newly allocated on OBSTACK,
19455 or, set *BATON, if we translated the constant to a location
19459 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
19460 const char *name
, struct obstack
*obstack
,
19461 struct dwarf2_cu
*cu
,
19462 LONGEST
*value
, const gdb_byte
**bytes
,
19463 struct dwarf2_locexpr_baton
**baton
)
19465 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19466 struct objfile
*objfile
= per_objfile
->objfile
;
19467 struct comp_unit_head
*cu_header
= &cu
->header
;
19468 struct dwarf_block
*blk
;
19469 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
.get ()) ?
19470 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
19476 switch (attr
->form
)
19479 case DW_FORM_addrx
:
19480 case DW_FORM_GNU_addr_index
:
19484 if (type
->length () != cu_header
->addr_size
)
19485 dwarf2_const_value_length_mismatch_complaint (name
,
19486 cu_header
->addr_size
,
19488 /* Symbols of this form are reasonably rare, so we just
19489 piggyback on the existing location code rather than writing
19490 a new implementation of symbol_computed_ops. */
19491 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
19492 (*baton
)->per_objfile
= per_objfile
;
19493 (*baton
)->per_cu
= cu
->per_cu
;
19494 gdb_assert ((*baton
)->per_cu
);
19496 (*baton
)->size
= 2 + cu_header
->addr_size
;
19497 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
19498 (*baton
)->data
= data
;
19500 data
[0] = DW_OP_addr
;
19501 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
19502 byte_order
, (ULONGEST
) attr
->as_address ());
19503 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
19506 case DW_FORM_string
:
19509 case DW_FORM_GNU_str_index
:
19510 case DW_FORM_GNU_strp_alt
:
19511 /* The string is already allocated on the objfile obstack, point
19513 *bytes
= (const gdb_byte
*) attr
->as_string ();
19515 case DW_FORM_block1
:
19516 case DW_FORM_block2
:
19517 case DW_FORM_block4
:
19518 case DW_FORM_block
:
19519 case DW_FORM_exprloc
:
19520 case DW_FORM_data16
:
19521 blk
= attr
->as_block ();
19522 if (type
->length () != blk
->size
)
19523 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
19525 *bytes
= blk
->data
;
19528 /* The DW_AT_const_value attributes are supposed to carry the
19529 symbol's value "represented as it would be on the target
19530 architecture." By the time we get here, it's already been
19531 converted to host endianness, so we just need to sign- or
19532 zero-extend it as appropriate. */
19533 case DW_FORM_data1
:
19534 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
19536 case DW_FORM_data2
:
19537 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
19539 case DW_FORM_data4
:
19540 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
19542 case DW_FORM_data8
:
19543 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
19546 case DW_FORM_sdata
:
19547 case DW_FORM_implicit_const
:
19548 *value
= attr
->as_signed ();
19551 case DW_FORM_udata
:
19552 *value
= attr
->as_unsigned ();
19556 complaint (_("unsupported const value attribute form: '%s'"),
19557 dwarf_form_name (attr
->form
));
19564 /* Copy constant value from an attribute to a symbol. */
19567 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
19568 struct dwarf2_cu
*cu
)
19570 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19572 const gdb_byte
*bytes
;
19573 struct dwarf2_locexpr_baton
*baton
;
19575 dwarf2_const_value_attr (attr
, sym
->type (),
19576 sym
->print_name (),
19577 &objfile
->objfile_obstack
, cu
,
19578 &value
, &bytes
, &baton
);
19582 SYMBOL_LOCATION_BATON (sym
) = baton
;
19583 sym
->set_aclass_index (dwarf2_locexpr_index
);
19585 else if (bytes
!= NULL
)
19587 sym
->set_value_bytes (bytes
);
19588 sym
->set_aclass_index (LOC_CONST_BYTES
);
19592 sym
->set_value_longest (value
);
19593 sym
->set_aclass_index (LOC_CONST
);
19597 /* Return the type of the die in question using its DW_AT_type attribute. */
19599 static struct type
*
19600 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
19602 struct attribute
*type_attr
;
19604 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
19607 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19608 /* A missing DW_AT_type represents a void type. */
19609 return builtin_type (objfile
)->builtin_void
;
19612 return lookup_die_type (die
, type_attr
, cu
);
19615 /* True iff CU's producer generates GNAT Ada auxiliary information
19616 that allows to find parallel types through that information instead
19617 of having to do expensive parallel lookups by type name. */
19620 need_gnat_info (struct dwarf2_cu
*cu
)
19622 /* Assume that the Ada compiler was GNAT, which always produces
19623 the auxiliary information. */
19624 return (cu
->lang () == language_ada
);
19627 /* Return the auxiliary type of the die in question using its
19628 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19629 attribute is not present. */
19631 static struct type
*
19632 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
19634 struct attribute
*type_attr
;
19636 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
19640 return lookup_die_type (die
, type_attr
, cu
);
19643 /* If DIE has a descriptive_type attribute, then set the TYPE's
19644 descriptive type accordingly. */
19647 set_descriptive_type (struct type
*type
, struct die_info
*die
,
19648 struct dwarf2_cu
*cu
)
19650 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
19652 if (descriptive_type
)
19654 ALLOCATE_GNAT_AUX_TYPE (type
);
19655 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
19659 /* Return the containing type of the die in question using its
19660 DW_AT_containing_type attribute. */
19662 static struct type
*
19663 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
19665 struct attribute
*type_attr
;
19666 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19668 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
19670 error (_("Dwarf Error: Problem turning containing type into gdb type "
19671 "[in module %s]"), objfile_name (objfile
));
19673 return lookup_die_type (die
, type_attr
, cu
);
19676 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19678 static struct type
*
19679 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
19681 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19682 struct objfile
*objfile
= per_objfile
->objfile
;
19685 std::string message
19686 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19687 objfile_name (objfile
),
19688 sect_offset_str (cu
->header
.sect_off
),
19689 sect_offset_str (die
->sect_off
));
19690 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
19692 return type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_ERROR
,
19696 /* Look up the type of DIE in CU using its type attribute ATTR.
19697 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19698 DW_AT_containing_type.
19699 If there is no type substitute an error marker. */
19701 static struct type
*
19702 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
19703 struct dwarf2_cu
*cu
)
19705 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19706 struct objfile
*objfile
= per_objfile
->objfile
;
19707 struct type
*this_type
;
19709 gdb_assert (attr
->name
== DW_AT_type
19710 || attr
->name
== DW_AT_GNAT_descriptive_type
19711 || attr
->name
== DW_AT_containing_type
);
19713 /* First see if we have it cached. */
19715 if (attr
->form
== DW_FORM_GNU_ref_alt
)
19717 struct dwarf2_per_cu_data
*per_cu
;
19718 sect_offset sect_off
= attr
->get_ref_die_offset ();
19720 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
19721 per_objfile
->per_bfd
);
19722 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
19724 else if (attr
->form_is_ref ())
19726 sect_offset sect_off
= attr
->get_ref_die_offset ();
19728 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
19730 else if (attr
->form
== DW_FORM_ref_sig8
)
19732 ULONGEST signature
= attr
->as_signature ();
19734 return get_signatured_type (die
, signature
, cu
);
19738 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19739 " at %s [in module %s]"),
19740 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
19741 objfile_name (objfile
));
19742 return build_error_marker_type (cu
, die
);
19745 /* If not cached we need to read it in. */
19747 if (this_type
== NULL
)
19749 struct die_info
*type_die
= NULL
;
19750 struct dwarf2_cu
*type_cu
= cu
;
19752 if (attr
->form_is_ref ())
19753 type_die
= follow_die_ref (die
, attr
, &type_cu
);
19754 if (type_die
== NULL
)
19755 return build_error_marker_type (cu
, die
);
19756 /* If we find the type now, it's probably because the type came
19757 from an inter-CU reference and the type's CU got expanded before
19759 this_type
= read_type_die (type_die
, type_cu
);
19762 /* If we still don't have a type use an error marker. */
19764 if (this_type
== NULL
)
19765 return build_error_marker_type (cu
, die
);
19770 /* Return the type in DIE, CU.
19771 Returns NULL for invalid types.
19773 This first does a lookup in die_type_hash,
19774 and only reads the die in if necessary.
19776 NOTE: This can be called when reading in partial or full symbols. */
19778 static struct type
*
19779 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
19781 struct type
*this_type
;
19783 this_type
= get_die_type (die
, cu
);
19787 return read_type_die_1 (die
, cu
);
19790 /* Read the type in DIE, CU.
19791 Returns NULL for invalid types. */
19793 static struct type
*
19794 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
19796 struct type
*this_type
= NULL
;
19800 case DW_TAG_class_type
:
19801 case DW_TAG_interface_type
:
19802 case DW_TAG_structure_type
:
19803 case DW_TAG_union_type
:
19804 this_type
= read_structure_type (die
, cu
);
19806 case DW_TAG_enumeration_type
:
19807 this_type
= read_enumeration_type (die
, cu
);
19809 case DW_TAG_entry_point
:
19810 case DW_TAG_subprogram
:
19811 case DW_TAG_subroutine_type
:
19812 case DW_TAG_inlined_subroutine
:
19813 this_type
= read_subroutine_type (die
, cu
);
19815 case DW_TAG_array_type
:
19816 this_type
= read_array_type (die
, cu
);
19818 case DW_TAG_set_type
:
19819 this_type
= read_set_type (die
, cu
);
19821 case DW_TAG_pointer_type
:
19822 this_type
= read_tag_pointer_type (die
, cu
);
19824 case DW_TAG_ptr_to_member_type
:
19825 this_type
= read_tag_ptr_to_member_type (die
, cu
);
19827 case DW_TAG_reference_type
:
19828 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
19830 case DW_TAG_rvalue_reference_type
:
19831 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
19833 case DW_TAG_const_type
:
19834 this_type
= read_tag_const_type (die
, cu
);
19836 case DW_TAG_volatile_type
:
19837 this_type
= read_tag_volatile_type (die
, cu
);
19839 case DW_TAG_restrict_type
:
19840 this_type
= read_tag_restrict_type (die
, cu
);
19842 case DW_TAG_string_type
:
19843 this_type
= read_tag_string_type (die
, cu
);
19845 case DW_TAG_typedef
:
19846 this_type
= read_typedef (die
, cu
);
19848 case DW_TAG_generic_subrange
:
19849 case DW_TAG_subrange_type
:
19850 this_type
= read_subrange_type (die
, cu
);
19852 case DW_TAG_base_type
:
19853 this_type
= read_base_type (die
, cu
);
19855 case DW_TAG_unspecified_type
:
19856 this_type
= read_unspecified_type (die
, cu
);
19858 case DW_TAG_namespace
:
19859 this_type
= read_namespace_type (die
, cu
);
19861 case DW_TAG_module
:
19862 this_type
= read_module_type (die
, cu
);
19864 case DW_TAG_atomic_type
:
19865 this_type
= read_tag_atomic_type (die
, cu
);
19868 complaint (_("unexpected tag in read_type_die: '%s'"),
19869 dwarf_tag_name (die
->tag
));
19876 /* See if we can figure out if the class lives in a namespace. We do
19877 this by looking for a member function; its demangled name will
19878 contain namespace info, if there is any.
19879 Return the computed name or NULL.
19880 Space for the result is allocated on the objfile's obstack.
19881 This is the full-die version of guess_partial_die_structure_name.
19882 In this case we know DIE has no useful parent. */
19884 static const char *
19885 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19887 struct die_info
*spec_die
;
19888 struct dwarf2_cu
*spec_cu
;
19889 struct die_info
*child
;
19890 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19893 spec_die
= die_specification (die
, &spec_cu
);
19894 if (spec_die
!= NULL
)
19900 for (child
= die
->child
;
19902 child
= child
->sibling
)
19904 if (child
->tag
== DW_TAG_subprogram
)
19906 const char *linkage_name
= dw2_linkage_name (child
, cu
);
19908 if (linkage_name
!= NULL
)
19910 gdb::unique_xmalloc_ptr
<char> actual_name
19911 (cu
->language_defn
->class_name_from_physname (linkage_name
));
19912 const char *name
= NULL
;
19914 if (actual_name
!= NULL
)
19916 const char *die_name
= dwarf2_name (die
, cu
);
19918 if (die_name
!= NULL
19919 && strcmp (die_name
, actual_name
.get ()) != 0)
19921 /* Strip off the class name from the full name.
19922 We want the prefix. */
19923 int die_name_len
= strlen (die_name
);
19924 int actual_name_len
= strlen (actual_name
.get ());
19925 const char *ptr
= actual_name
.get ();
19927 /* Test for '::' as a sanity check. */
19928 if (actual_name_len
> die_name_len
+ 2
19929 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
19930 name
= obstack_strndup (
19931 &objfile
->per_bfd
->storage_obstack
,
19932 ptr
, actual_name_len
- die_name_len
- 2);
19943 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19944 prefix part in such case. See
19945 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19947 static const char *
19948 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
19950 struct attribute
*attr
;
19953 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
19954 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
19957 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
19960 attr
= dw2_linkage_name_attr (die
, cu
);
19961 const char *attr_name
= attr
->as_string ();
19962 if (attr
== NULL
|| attr_name
== NULL
)
19965 /* dwarf2_name had to be already called. */
19966 gdb_assert (attr
->canonical_string_p ());
19968 /* Strip the base name, keep any leading namespaces/classes. */
19969 base
= strrchr (attr_name
, ':');
19970 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
19973 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19974 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
19976 &base
[-1] - attr_name
);
19979 /* Return the name of the namespace/class that DIE is defined within,
19980 or "" if we can't tell. The caller should not xfree the result.
19982 For example, if we're within the method foo() in the following
19992 then determine_prefix on foo's die will return "N::C". */
19994 static const char *
19995 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
19997 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19998 struct die_info
*parent
, *spec_die
;
19999 struct dwarf2_cu
*spec_cu
;
20000 struct type
*parent_type
;
20001 const char *retval
;
20003 if (cu
->lang () != language_cplus
20004 && cu
->lang () != language_fortran
20005 && cu
->lang () != language_d
20006 && cu
->lang () != language_rust
)
20009 retval
= anonymous_struct_prefix (die
, cu
);
20013 /* We have to be careful in the presence of DW_AT_specification.
20014 For example, with GCC 3.4, given the code
20018 // Definition of N::foo.
20022 then we'll have a tree of DIEs like this:
20024 1: DW_TAG_compile_unit
20025 2: DW_TAG_namespace // N
20026 3: DW_TAG_subprogram // declaration of N::foo
20027 4: DW_TAG_subprogram // definition of N::foo
20028 DW_AT_specification // refers to die #3
20030 Thus, when processing die #4, we have to pretend that we're in
20031 the context of its DW_AT_specification, namely the context of die
20034 spec_die
= die_specification (die
, &spec_cu
);
20035 if (spec_die
== NULL
)
20036 parent
= die
->parent
;
20039 parent
= spec_die
->parent
;
20043 if (parent
== NULL
)
20045 else if (parent
->building_fullname
)
20048 const char *parent_name
;
20050 /* It has been seen on RealView 2.2 built binaries,
20051 DW_TAG_template_type_param types actually _defined_ as
20052 children of the parent class:
20055 template class <class Enum> Class{};
20056 Class<enum E> class_e;
20058 1: DW_TAG_class_type (Class)
20059 2: DW_TAG_enumeration_type (E)
20060 3: DW_TAG_enumerator (enum1:0)
20061 3: DW_TAG_enumerator (enum2:1)
20063 2: DW_TAG_template_type_param
20064 DW_AT_type DW_FORM_ref_udata (E)
20066 Besides being broken debug info, it can put GDB into an
20067 infinite loop. Consider:
20069 When we're building the full name for Class<E>, we'll start
20070 at Class, and go look over its template type parameters,
20071 finding E. We'll then try to build the full name of E, and
20072 reach here. We're now trying to build the full name of E,
20073 and look over the parent DIE for containing scope. In the
20074 broken case, if we followed the parent DIE of E, we'd again
20075 find Class, and once again go look at its template type
20076 arguments, etc., etc. Simply don't consider such parent die
20077 as source-level parent of this die (it can't be, the language
20078 doesn't allow it), and break the loop here. */
20079 name
= dwarf2_name (die
, cu
);
20080 parent_name
= dwarf2_name (parent
, cu
);
20081 complaint (_("template param type '%s' defined within parent '%s'"),
20082 name
? name
: "<unknown>",
20083 parent_name
? parent_name
: "<unknown>");
20087 switch (parent
->tag
)
20089 case DW_TAG_namespace
:
20090 parent_type
= read_type_die (parent
, cu
);
20091 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
20092 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
20093 Work around this problem here. */
20094 if (cu
->lang () == language_cplus
20095 && strcmp (parent_type
->name (), "::") == 0)
20097 /* We give a name to even anonymous namespaces. */
20098 return parent_type
->name ();
20099 case DW_TAG_class_type
:
20100 case DW_TAG_interface_type
:
20101 case DW_TAG_structure_type
:
20102 case DW_TAG_union_type
:
20103 case DW_TAG_module
:
20104 parent_type
= read_type_die (parent
, cu
);
20105 if (parent_type
->name () != NULL
)
20106 return parent_type
->name ();
20108 /* An anonymous structure is only allowed non-static data
20109 members; no typedefs, no member functions, et cetera.
20110 So it does not need a prefix. */
20112 case DW_TAG_compile_unit
:
20113 case DW_TAG_partial_unit
:
20114 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
20115 if (cu
->lang () == language_cplus
20116 && !per_objfile
->per_bfd
->types
.empty ()
20117 && die
->child
!= NULL
20118 && (die
->tag
== DW_TAG_class_type
20119 || die
->tag
== DW_TAG_structure_type
20120 || die
->tag
== DW_TAG_union_type
))
20122 const char *name
= guess_full_die_structure_name (die
, cu
);
20127 case DW_TAG_subprogram
:
20128 /* Nested subroutines in Fortran get a prefix with the name
20129 of the parent's subroutine. Entry points are prefixed by the
20130 parent's namespace. */
20131 if (cu
->lang () == language_fortran
)
20133 if ((die
->tag
== DW_TAG_subprogram
)
20134 && (dwarf2_name (parent
, cu
) != NULL
))
20135 return dwarf2_name (parent
, cu
);
20136 else if (die
->tag
== DW_TAG_entry_point
)
20137 return determine_prefix (parent
, cu
);
20140 case DW_TAG_enumeration_type
:
20141 parent_type
= read_type_die (parent
, cu
);
20142 if (parent_type
->is_declared_class ())
20144 if (parent_type
->name () != NULL
)
20145 return parent_type
->name ();
20150 return determine_prefix (parent
, cu
);
20154 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
20155 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
20156 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
20157 an obconcat, otherwise allocate storage for the result. The CU argument is
20158 used to determine the language and hence, the appropriate separator. */
20160 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
20163 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
20164 int physname
, struct dwarf2_cu
*cu
)
20166 const char *lead
= "";
20169 if (suffix
== NULL
|| suffix
[0] == '\0'
20170 || prefix
== NULL
|| prefix
[0] == '\0')
20172 else if (cu
->lang () == language_d
)
20174 /* For D, the 'main' function could be defined in any module, but it
20175 should never be prefixed. */
20176 if (strcmp (suffix
, "D main") == 0)
20184 else if (cu
->lang () == language_fortran
&& physname
)
20186 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20187 DW_AT_MIPS_linkage_name is preferred and used instead. */
20195 if (prefix
== NULL
)
20197 if (suffix
== NULL
)
20204 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
20206 strcpy (retval
, lead
);
20207 strcat (retval
, prefix
);
20208 strcat (retval
, sep
);
20209 strcat (retval
, suffix
);
20214 /* We have an obstack. */
20215 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
20219 /* Return a generic name for a DW_TAG_template_type_param or
20220 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
20221 per parent, so each function/class/struct template will have their own set
20222 of template parameters named <unnnamed0>, <unnamed1>, ... where the
20223 enumeration starts at 0 and represents the position of the template tag in
20224 the list of unnamed template tags for this parent, counting both, type and
20227 static const char *
20228 unnamed_template_tag_name (die_info
*die
, dwarf2_cu
*cu
)
20230 if (die
->parent
== nullptr)
20233 /* Count the parent types unnamed template type and value children until, we
20234 arrive at our entry. */
20235 size_t nth_unnamed
= 0;
20237 die_info
*child
= die
->parent
->child
;
20238 while (child
!= die
)
20240 gdb_assert (child
!= nullptr);
20241 if (child
->tag
== DW_TAG_template_type_param
20242 || child
->tag
== DW_TAG_template_value_param
)
20244 if (dwarf2_attr (child
, DW_AT_name
, cu
) == nullptr)
20247 child
= child
->sibling
;
20250 const std::string name_str
= "<unnamed" + std::to_string (nth_unnamed
) + ">";
20251 return cu
->per_objfile
->objfile
->intern (name_str
.c_str ());
20254 /* Get name of a die, return NULL if not found. */
20256 static const char *
20257 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
20258 struct objfile
*objfile
)
20260 if (name
== nullptr)
20263 if (cu
->lang () == language_cplus
)
20265 gdb::unique_xmalloc_ptr
<char> canon_name
20266 = cp_canonicalize_string (name
);
20268 if (canon_name
!= nullptr)
20269 name
= objfile
->intern (canon_name
.get ());
20271 else if (cu
->lang () == language_c
)
20273 gdb::unique_xmalloc_ptr
<char> canon_name
20274 = c_canonicalize_name (name
);
20276 if (canon_name
!= nullptr)
20277 name
= objfile
->intern (canon_name
.get ());
20283 /* Get name of a die, return NULL if not found.
20284 Anonymous namespaces are converted to their magic string. */
20286 static const char *
20287 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
20289 struct attribute
*attr
;
20290 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20292 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
20293 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
20294 if (attr_name
== nullptr
20295 && die
->tag
!= DW_TAG_namespace
20296 && die
->tag
!= DW_TAG_class_type
20297 && die
->tag
!= DW_TAG_interface_type
20298 && die
->tag
!= DW_TAG_structure_type
20299 && die
->tag
!= DW_TAG_namelist
20300 && die
->tag
!= DW_TAG_union_type
20301 && die
->tag
!= DW_TAG_template_type_param
20302 && die
->tag
!= DW_TAG_template_value_param
)
20307 /* A member's name should not be canonicalized. This is a bit
20308 of a hack, in that normally it should not be possible to run
20309 into this situation; however, the dw2-unusual-field-names.exp
20310 test creates custom DWARF that does. */
20311 case DW_TAG_member
:
20312 case DW_TAG_compile_unit
:
20313 case DW_TAG_partial_unit
:
20314 /* Compilation units have a DW_AT_name that is a filename, not
20315 a source language identifier. */
20316 case DW_TAG_enumeration_type
:
20317 case DW_TAG_enumerator
:
20318 /* These tags always have simple identifiers already; no need
20319 to canonicalize them. */
20322 case DW_TAG_namespace
:
20323 if (attr_name
!= nullptr)
20325 return CP_ANONYMOUS_NAMESPACE_STR
;
20327 /* DWARF does not actually require template tags to have a name. */
20328 case DW_TAG_template_type_param
:
20329 case DW_TAG_template_value_param
:
20330 if (attr_name
== nullptr)
20331 return unnamed_template_tag_name (die
, cu
);
20333 case DW_TAG_class_type
:
20334 case DW_TAG_interface_type
:
20335 case DW_TAG_structure_type
:
20336 case DW_TAG_union_type
:
20337 case DW_TAG_namelist
:
20338 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20339 structures or unions. These were of the form "._%d" in GCC 4.1,
20340 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20341 and GCC 4.4. We work around this problem by ignoring these. */
20342 if (attr_name
!= nullptr
20343 && (startswith (attr_name
, "._")
20344 || startswith (attr_name
, "<anonymous")))
20347 /* GCC might emit a nameless typedef that has a linkage name. See
20348 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20349 if (!attr
|| attr_name
== NULL
)
20351 attr
= dw2_linkage_name_attr (die
, cu
);
20352 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
20353 if (attr
== NULL
|| attr_name
== NULL
)
20356 /* Avoid demangling attr_name the second time on a second
20357 call for the same DIE. */
20358 if (!attr
->canonical_string_p ())
20360 gdb::unique_xmalloc_ptr
<char> demangled
20361 (gdb_demangle (attr_name
, DMGL_TYPES
));
20362 if (demangled
== nullptr)
20365 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
20366 attr_name
= attr
->as_string ();
20369 /* Strip any leading namespaces/classes, keep only the
20370 base name. DW_AT_name for named DIEs does not
20371 contain the prefixes. */
20372 const char *base
= strrchr (attr_name
, ':');
20373 if (base
&& base
> attr_name
&& base
[-1] == ':')
20384 if (!attr
->canonical_string_p ())
20385 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
20387 return attr
->as_string ();
20390 /* Return the die that this die in an extension of, or NULL if there
20391 is none. *EXT_CU is the CU containing DIE on input, and the CU
20392 containing the return value on output. */
20394 static struct die_info
*
20395 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
20397 struct attribute
*attr
;
20399 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
20403 return follow_die_ref (die
, attr
, ext_cu
);
20407 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
20411 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
20412 to_underlying (die
->sect_off
),
20418 /* Follow reference or signature attribute ATTR of SRC_DIE.
20419 On entry *REF_CU is the CU of SRC_DIE.
20420 On exit *REF_CU is the CU of the result. */
20422 static struct die_info
*
20423 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
20424 struct dwarf2_cu
**ref_cu
)
20426 struct die_info
*die
;
20428 if (attr
->form_is_ref ())
20429 die
= follow_die_ref (src_die
, attr
, ref_cu
);
20430 else if (attr
->form
== DW_FORM_ref_sig8
)
20431 die
= follow_die_sig (src_die
, attr
, ref_cu
);
20434 src_die
->error_dump ();
20435 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20436 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
20442 /* Follow reference OFFSET.
20443 On entry *REF_CU is the CU of the source die referencing OFFSET.
20444 On exit *REF_CU is the CU of the result.
20445 Returns NULL if OFFSET is invalid. */
20447 static struct die_info
*
20448 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
20449 struct dwarf2_cu
**ref_cu
)
20451 struct die_info temp_die
;
20452 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
20453 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20455 gdb_assert (cu
->per_cu
!= NULL
);
20459 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20460 "source CU contains target offset: %d",
20461 sect_offset_str (cu
->per_cu
->sect_off
),
20462 sect_offset_str (sect_off
),
20463 cu
->header
.offset_in_cu_p (sect_off
));
20465 if (cu
->per_cu
->is_debug_types
)
20467 /* .debug_types CUs cannot reference anything outside their CU.
20468 If they need to, they have to reference a signatured type via
20469 DW_FORM_ref_sig8. */
20470 if (!cu
->header
.offset_in_cu_p (sect_off
))
20473 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
20474 || !cu
->header
.offset_in_cu_p (sect_off
))
20476 struct dwarf2_per_cu_data
*per_cu
;
20478 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
20479 per_objfile
->per_bfd
);
20481 dwarf_read_debug_printf_v ("target CU offset: %s, "
20482 "target CU DIEs loaded: %d",
20483 sect_offset_str (per_cu
->sect_off
),
20484 per_objfile
->get_cu (per_cu
) != nullptr);
20486 /* If necessary, add it to the queue and load its DIEs.
20488 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20489 it doesn't mean they are currently loaded. Since we require them
20490 to be loaded, we must check for ourselves. */
20491 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->lang ())
20492 || per_objfile
->get_cu (per_cu
) == nullptr)
20493 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
20494 false, cu
->lang ());
20496 target_cu
= per_objfile
->get_cu (per_cu
);
20497 gdb_assert (target_cu
!= nullptr);
20499 else if (cu
->dies
== NULL
)
20501 /* We're loading full DIEs during partial symbol reading. */
20502 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
20506 *ref_cu
= target_cu
;
20507 temp_die
.sect_off
= sect_off
;
20509 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
20511 to_underlying (sect_off
));
20514 /* Follow reference attribute ATTR of SRC_DIE.
20515 On entry *REF_CU is the CU of SRC_DIE.
20516 On exit *REF_CU is the CU of the result. */
20518 static struct die_info
*
20519 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
20520 struct dwarf2_cu
**ref_cu
)
20522 sect_offset sect_off
= attr
->get_ref_die_offset ();
20523 struct dwarf2_cu
*cu
= *ref_cu
;
20524 struct die_info
*die
;
20526 if (attr
->form
!= DW_FORM_GNU_ref_alt
&& src_die
->sect_off
== sect_off
)
20528 /* Self-reference, we're done. */
20532 die
= follow_die_offset (sect_off
,
20533 (attr
->form
== DW_FORM_GNU_ref_alt
20534 || cu
->per_cu
->is_dwz
),
20537 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20538 "at %s [in module %s]"),
20539 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
20540 objfile_name (cu
->per_objfile
->objfile
));
20547 struct dwarf2_locexpr_baton
20548 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
20549 dwarf2_per_cu_data
*per_cu
,
20550 dwarf2_per_objfile
*per_objfile
,
20551 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
20552 bool resolve_abstract_p
)
20554 struct die_info
*die
;
20555 struct attribute
*attr
;
20556 struct dwarf2_locexpr_baton retval
;
20557 struct objfile
*objfile
= per_objfile
->objfile
;
20559 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
20561 cu
= load_cu (per_cu
, per_objfile
, false);
20565 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20566 Instead just throw an error, not much else we can do. */
20567 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20568 sect_offset_str (sect_off
), objfile_name (objfile
));
20571 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
20573 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20574 sect_offset_str (sect_off
), objfile_name (objfile
));
20576 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20577 if (!attr
&& resolve_abstract_p
20578 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
20579 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
20581 CORE_ADDR pc
= get_frame_pc ();
20583 for (const auto &cand_off
20584 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
20586 struct dwarf2_cu
*cand_cu
= cu
;
20587 struct die_info
*cand
20588 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
20591 || cand
->parent
->tag
!= DW_TAG_subprogram
)
20594 unrelocated_addr unrel_low
, unrel_high
;
20595 get_scope_pc_bounds (cand
->parent
, &unrel_low
, &unrel_high
, cu
);
20596 if (unrel_low
== ((unrelocated_addr
) -1))
20598 CORE_ADDR pc_low
= per_objfile
->relocate (unrel_low
);
20599 CORE_ADDR pc_high
= per_objfile
->relocate (unrel_high
);
20600 if (!(pc_low
<= pc
&& pc
< pc_high
))
20604 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20611 /* DWARF: "If there is no such attribute, then there is no effect.".
20612 DATA is ignored if SIZE is 0. */
20614 retval
.data
= NULL
;
20617 else if (attr
->form_is_section_offset ())
20619 struct dwarf2_loclist_baton loclist_baton
;
20620 CORE_ADDR pc
= get_frame_pc ();
20623 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
20625 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
20627 retval
.size
= size
;
20631 if (!attr
->form_is_block ())
20632 error (_("Dwarf Error: DIE at %s referenced in module %s "
20633 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20634 sect_offset_str (sect_off
), objfile_name (objfile
));
20636 struct dwarf_block
*block
= attr
->as_block ();
20637 retval
.data
= block
->data
;
20638 retval
.size
= block
->size
;
20640 retval
.per_objfile
= per_objfile
;
20641 retval
.per_cu
= cu
->per_cu
;
20643 per_objfile
->age_comp_units ();
20650 struct dwarf2_locexpr_baton
20651 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
20652 dwarf2_per_cu_data
*per_cu
,
20653 dwarf2_per_objfile
*per_objfile
,
20654 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
20656 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
20658 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
20662 /* Write a constant of a given type as target-ordered bytes into
20665 static const gdb_byte
*
20666 write_constant_as_bytes (struct obstack
*obstack
,
20667 enum bfd_endian byte_order
,
20674 *len
= type
->length ();
20675 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
20676 store_unsigned_integer (result
, *len
, byte_order
, value
);
20684 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
20685 dwarf2_per_cu_data
*per_cu
,
20686 dwarf2_per_objfile
*per_objfile
,
20690 struct die_info
*die
;
20691 struct attribute
*attr
;
20692 const gdb_byte
*result
= NULL
;
20695 enum bfd_endian byte_order
;
20696 struct objfile
*objfile
= per_objfile
->objfile
;
20698 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
20700 cu
= load_cu (per_cu
, per_objfile
, false);
20704 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20705 Instead just throw an error, not much else we can do. */
20706 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20707 sect_offset_str (sect_off
), objfile_name (objfile
));
20710 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
20712 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20713 sect_offset_str (sect_off
), objfile_name (objfile
));
20715 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20719 byte_order
= (bfd_big_endian (objfile
->obfd
.get ())
20720 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
20722 switch (attr
->form
)
20725 case DW_FORM_addrx
:
20726 case DW_FORM_GNU_addr_index
:
20730 *len
= cu
->header
.addr_size
;
20731 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
20732 store_unsigned_integer (tem
, *len
, byte_order
,
20733 (ULONGEST
) attr
->as_address ());
20737 case DW_FORM_string
:
20740 case DW_FORM_GNU_str_index
:
20741 case DW_FORM_GNU_strp_alt
:
20742 /* The string is already allocated on the objfile obstack, point
20745 const char *attr_name
= attr
->as_string ();
20746 result
= (const gdb_byte
*) attr_name
;
20747 *len
= strlen (attr_name
);
20750 case DW_FORM_block1
:
20751 case DW_FORM_block2
:
20752 case DW_FORM_block4
:
20753 case DW_FORM_block
:
20754 case DW_FORM_exprloc
:
20755 case DW_FORM_data16
:
20757 struct dwarf_block
*block
= attr
->as_block ();
20758 result
= block
->data
;
20759 *len
= block
->size
;
20763 /* The DW_AT_const_value attributes are supposed to carry the
20764 symbol's value "represented as it would be on the target
20765 architecture." By the time we get here, it's already been
20766 converted to host endianness, so we just need to sign- or
20767 zero-extend it as appropriate. */
20768 case DW_FORM_data1
:
20769 type
= die_type (die
, cu
);
20770 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
20771 if (result
== NULL
)
20772 result
= write_constant_as_bytes (obstack
, byte_order
,
20775 case DW_FORM_data2
:
20776 type
= die_type (die
, cu
);
20777 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
20778 if (result
== NULL
)
20779 result
= write_constant_as_bytes (obstack
, byte_order
,
20782 case DW_FORM_data4
:
20783 type
= die_type (die
, cu
);
20784 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
20785 if (result
== NULL
)
20786 result
= write_constant_as_bytes (obstack
, byte_order
,
20789 case DW_FORM_data8
:
20790 type
= die_type (die
, cu
);
20791 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
20792 if (result
== NULL
)
20793 result
= write_constant_as_bytes (obstack
, byte_order
,
20797 case DW_FORM_sdata
:
20798 case DW_FORM_implicit_const
:
20799 type
= die_type (die
, cu
);
20800 result
= write_constant_as_bytes (obstack
, byte_order
,
20801 type
, attr
->as_signed (), len
);
20804 case DW_FORM_udata
:
20805 type
= die_type (die
, cu
);
20806 result
= write_constant_as_bytes (obstack
, byte_order
,
20807 type
, attr
->as_unsigned (), len
);
20811 complaint (_("unsupported const value attribute form: '%s'"),
20812 dwarf_form_name (attr
->form
));
20822 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
20823 dwarf2_per_cu_data
*per_cu
,
20824 dwarf2_per_objfile
*per_objfile
,
20825 const char **var_name
)
20827 struct die_info
*die
;
20829 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
20831 cu
= load_cu (per_cu
, per_objfile
, false);
20836 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
20840 if (var_name
!= nullptr)
20841 *var_name
= var_decl_name (die
, cu
);
20842 return die_type (die
, cu
);
20848 dwarf2_get_die_type (cu_offset die_offset
,
20849 dwarf2_per_cu_data
*per_cu
,
20850 dwarf2_per_objfile
*per_objfile
)
20852 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
20853 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
20856 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20857 On entry *REF_CU is the CU of SRC_DIE.
20858 On exit *REF_CU is the CU of the result.
20859 Returns NULL if the referenced DIE isn't found. */
20861 static struct die_info
*
20862 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
20863 struct dwarf2_cu
**ref_cu
)
20865 struct die_info temp_die
;
20866 struct dwarf2_cu
*sig_cu
;
20867 struct die_info
*die
;
20868 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
20871 /* While it might be nice to assert sig_type->type == NULL here,
20872 we can get here for DW_AT_imported_declaration where we need
20873 the DIE not the type. */
20875 /* If necessary, add it to the queue and load its DIEs.
20877 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20878 it doesn't mean they are currently loaded. Since we require them
20879 to be loaded, we must check for ourselves. */
20880 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
20882 || per_objfile
->get_cu (sig_type
) == nullptr)
20883 read_signatured_type (sig_type
, per_objfile
);
20885 sig_cu
= per_objfile
->get_cu (sig_type
);
20886 gdb_assert (sig_cu
!= NULL
);
20887 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
20888 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
20889 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
20890 to_underlying (temp_die
.sect_off
));
20893 /* For .gdb_index version 7 keep track of included TUs.
20894 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20895 if (per_objfile
->per_bfd
->index_table
!= NULL
20896 && !per_objfile
->per_bfd
->index_table
->version_check ())
20898 (*ref_cu
)->per_cu
->imported_symtabs_push (sig_cu
->per_cu
);
20908 /* Follow signatured type referenced by ATTR in SRC_DIE.
20909 On entry *REF_CU is the CU of SRC_DIE.
20910 On exit *REF_CU is the CU of the result.
20911 The result is the DIE of the type.
20912 If the referenced type cannot be found an error is thrown. */
20914 static struct die_info
*
20915 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
20916 struct dwarf2_cu
**ref_cu
)
20918 ULONGEST signature
= attr
->as_signature ();
20919 struct signatured_type
*sig_type
;
20920 struct die_info
*die
;
20922 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
20924 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
20925 /* sig_type will be NULL if the signatured type is missing from
20927 if (sig_type
== NULL
)
20929 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20930 " from DIE at %s [in module %s]"),
20931 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
20932 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
20935 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
20938 src_die
->error_dump ();
20939 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20940 " from DIE at %s [in module %s]"),
20941 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
20942 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
20948 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20949 reading in and processing the type unit if necessary. */
20951 static struct type
*
20952 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
20953 struct dwarf2_cu
*cu
)
20955 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20956 struct signatured_type
*sig_type
;
20957 struct dwarf2_cu
*type_cu
;
20958 struct die_info
*type_die
;
20961 sig_type
= lookup_signatured_type (cu
, signature
);
20962 /* sig_type will be NULL if the signatured type is missing from
20964 if (sig_type
== NULL
)
20966 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20967 " from DIE at %s [in module %s]"),
20968 hex_string (signature
), sect_offset_str (die
->sect_off
),
20969 objfile_name (per_objfile
->objfile
));
20970 return build_error_marker_type (cu
, die
);
20973 /* If we already know the type we're done. */
20974 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
20975 if (type
!= nullptr)
20979 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
20980 if (type_die
!= NULL
)
20982 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20983 is created. This is important, for example, because for c++ classes
20984 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20985 type
= read_type_die (type_die
, type_cu
);
20988 complaint (_("Dwarf Error: Cannot build signatured type %s"
20989 " referenced from DIE at %s [in module %s]"),
20990 hex_string (signature
), sect_offset_str (die
->sect_off
),
20991 objfile_name (per_objfile
->objfile
));
20992 type
= build_error_marker_type (cu
, die
);
20997 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20998 " from DIE at %s [in module %s]"),
20999 hex_string (signature
), sect_offset_str (die
->sect_off
),
21000 objfile_name (per_objfile
->objfile
));
21001 type
= build_error_marker_type (cu
, die
);
21004 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
21009 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
21010 reading in and processing the type unit if necessary. */
21012 static struct type
*
21013 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
21014 struct dwarf2_cu
*cu
) /* ARI: editCase function */
21016 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
21017 if (attr
->form_is_ref ())
21019 struct dwarf2_cu
*type_cu
= cu
;
21020 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
21022 return read_type_die (type_die
, type_cu
);
21024 else if (attr
->form
== DW_FORM_ref_sig8
)
21026 return get_signatured_type (die
, attr
->as_signature (), cu
);
21030 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21032 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
21033 " at %s [in module %s]"),
21034 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
21035 objfile_name (per_objfile
->objfile
));
21036 return build_error_marker_type (cu
, die
);
21040 /* Load the DIEs associated with type unit PER_CU into memory. */
21043 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
21044 dwarf2_per_objfile
*per_objfile
)
21046 struct signatured_type
*sig_type
;
21048 /* We have the per_cu, but we need the signatured_type.
21049 Fortunately this is an easy translation. */
21050 gdb_assert (per_cu
->is_debug_types
);
21051 sig_type
= (struct signatured_type
*) per_cu
;
21053 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
21055 read_signatured_type (sig_type
, per_objfile
);
21057 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
21060 /* Read in a signatured type and build its CU and DIEs.
21061 If the type is a stub for the real type in a DWO file,
21062 read in the real type from the DWO file as well. */
21065 read_signatured_type (signatured_type
*sig_type
,
21066 dwarf2_per_objfile
*per_objfile
)
21068 gdb_assert (sig_type
->is_debug_types
);
21069 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
21071 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
21073 if (!reader
.dummy_p
)
21075 struct dwarf2_cu
*cu
= reader
.cu
;
21076 const gdb_byte
*info_ptr
= reader
.info_ptr
;
21078 gdb_assert (cu
->die_hash
== NULL
);
21080 htab_create_alloc_ex (cu
->header
.get_length_without_initial () / 12,
21084 &cu
->comp_unit_obstack
,
21085 hashtab_obstack_allocate
,
21086 dummy_obstack_deallocate
);
21088 if (reader
.comp_unit_die
->has_children
)
21089 reader
.comp_unit_die
->child
21090 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
21091 reader
.comp_unit_die
);
21092 cu
->dies
= reader
.comp_unit_die
;
21093 /* comp_unit_die is not stored in die_hash, no need. */
21095 /* We try not to read any attributes in this function, because
21096 not all CUs needed for references have been loaded yet, and
21097 symbol table processing isn't initialized. But we have to
21098 set the CU language, or we won't be able to build types
21099 correctly. Similarly, if we do not read the producer, we can
21100 not apply producer-specific interpretation. */
21101 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
21106 sig_type
->tu_read
= 1;
21109 /* Decode simple location descriptions.
21111 Given a pointer to a DWARF block that defines a location, compute
21112 the location. Returns true if the expression was computable by
21113 this function, false otherwise. On a true return, *RESULT is set.
21115 Note that this function does not implement a full DWARF expression
21116 evaluator. Instead, it is used for a few limited purposes:
21118 - Getting the address of a symbol that has a constant address. For
21119 example, if a symbol has a location like "DW_OP_addr", the address
21122 - Getting the offset of a virtual function in its vtable. There
21123 are two forms of this, one of which involves DW_OP_deref -- so this
21124 function handles derefs in a peculiar way to make this 'work'.
21125 (Probably this area should be rewritten.)
21127 - Getting the offset of a field, when it is constant.
21129 Opcodes that cannot be part of a constant expression, for example
21130 those involving registers, simply result in a return of
21133 This function may emit a complaint. */
21136 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
,
21139 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21141 size_t size
= blk
->size
;
21142 const gdb_byte
*data
= blk
->data
;
21143 CORE_ADDR stack
[64];
21145 unsigned int bytes_read
;
21152 stack
[++stacki
] = 0;
21191 stack
[++stacki
] = op
- DW_OP_lit0
;
21196 = (CORE_ADDR
) cu
->header
.read_address (objfile
->obfd
.get (),
21202 case DW_OP_const1u
:
21203 stack
[++stacki
] = read_1_byte (objfile
->obfd
.get (), &data
[i
]);
21207 case DW_OP_const1s
:
21208 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
.get (), &data
[i
]);
21212 case DW_OP_const2u
:
21213 stack
[++stacki
] = read_2_bytes (objfile
->obfd
.get (), &data
[i
]);
21217 case DW_OP_const2s
:
21218 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
.get (), &data
[i
]);
21222 case DW_OP_const4u
:
21223 stack
[++stacki
] = read_4_bytes (objfile
->obfd
.get (), &data
[i
]);
21227 case DW_OP_const4s
:
21228 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
.get (), &data
[i
]);
21232 case DW_OP_const8u
:
21233 stack
[++stacki
] = read_8_bytes (objfile
->obfd
.get (), &data
[i
]);
21238 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
21244 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
21249 stack
[stacki
+ 1] = stack
[stacki
];
21254 stack
[stacki
- 1] += stack
[stacki
];
21258 case DW_OP_plus_uconst
:
21259 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
21265 stack
[stacki
- 1] -= stack
[stacki
];
21270 /* If we're not the last op, then we definitely can't encode
21271 this using GDB's address_class enum. This is valid for partial
21272 global symbols, although the variable's address will be bogus
21279 case DW_OP_GNU_addr_index
:
21280 case DW_OP_GNU_const_index
:
21282 = (CORE_ADDR
) read_addr_index_from_leb128 (cu
, &data
[i
],
21291 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21292 outside of the allocated space. Also enforce minimum>0. */
21293 if (stacki
>= ARRAY_SIZE (stack
) - 1)
21295 complaint (_("location description stack overflow"));
21301 complaint (_("location description stack underflow"));
21306 *result
= stack
[stacki
];
21310 /* memory allocation interface */
21312 static struct dwarf_block
*
21313 dwarf_alloc_block (struct dwarf2_cu
*cu
)
21315 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
21320 /* Macro support. */
21322 /* An overload of dwarf_decode_macros that finds the correct section
21323 and ensures it is read in before calling the other overload. */
21326 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
21327 int section_is_gnu
)
21329 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21330 struct objfile
*objfile
= per_objfile
->objfile
;
21331 const struct line_header
*lh
= cu
->line_header
;
21332 unsigned int offset_size
= cu
->header
.offset_size
;
21333 struct dwarf2_section_info
*section
;
21334 const char *section_name
;
21336 if (cu
->dwo_unit
!= nullptr)
21338 if (section_is_gnu
)
21340 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
21341 section_name
= ".debug_macro.dwo";
21345 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
21346 section_name
= ".debug_macinfo.dwo";
21351 if (section_is_gnu
)
21353 section
= &per_objfile
->per_bfd
->macro
;
21354 section_name
= ".debug_macro";
21358 section
= &per_objfile
->per_bfd
->macinfo
;
21359 section_name
= ".debug_macinfo";
21363 section
->read (objfile
);
21364 if (section
->buffer
== nullptr)
21366 complaint (_("missing %s section"), section_name
);
21370 buildsym_compunit
*builder
= cu
->get_builder ();
21372 struct dwarf2_section_info
*str_offsets_section
;
21373 struct dwarf2_section_info
*str_section
;
21374 std::optional
<ULONGEST
> str_offsets_base
;
21376 if (cu
->dwo_unit
!= nullptr)
21378 str_offsets_section
= &cu
->dwo_unit
->dwo_file
21379 ->sections
.str_offsets
;
21380 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
21381 str_offsets_base
= cu
->header
.addr_size
;
21385 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
21386 str_section
= &per_objfile
->per_bfd
->str
;
21387 str_offsets_base
= cu
->str_offsets_base
;
21390 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
21391 offset_size
, offset
, str_section
, str_offsets_section
,
21392 str_offsets_base
, section_is_gnu
, cu
);
21395 /* Return the .debug_loc section to use for CU.
21396 For DWO files use .debug_loc.dwo. */
21398 static struct dwarf2_section_info
*
21399 cu_debug_loc_section (struct dwarf2_cu
*cu
)
21401 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21405 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
21407 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
21409 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
21410 : &per_objfile
->per_bfd
->loc
);
21413 /* Return the .debug_rnglists section to use for CU. */
21414 static struct dwarf2_section_info
*
21415 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
21417 if (cu
->header
.version
< 5)
21418 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21419 cu
->header
.version
);
21420 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
21422 /* Make sure we read the .debug_rnglists section from the file that
21423 contains the DW_AT_ranges attribute we are reading. Normally that
21424 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21425 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21427 if (cu
->dwo_unit
!= nullptr
21428 && tag
!= DW_TAG_compile_unit
21429 && tag
!= DW_TAG_skeleton_unit
)
21431 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
21433 if (sections
->rnglists
.size
> 0)
21434 return §ions
->rnglists
;
21436 error (_(".debug_rnglists section is missing from .dwo file."));
21438 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
21441 /* A helper function that fills in a dwarf2_loclist_baton. */
21444 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
21445 struct dwarf2_loclist_baton
*baton
,
21446 const struct attribute
*attr
)
21448 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21449 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
21451 section
->read (per_objfile
->objfile
);
21453 baton
->per_objfile
= per_objfile
;
21454 baton
->per_cu
= cu
->per_cu
;
21455 gdb_assert (baton
->per_cu
);
21456 /* We don't know how long the location list is, but make sure we
21457 don't run off the edge of the section. */
21458 baton
->size
= section
->size
- attr
->as_unsigned ();
21459 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
21460 if (cu
->base_address
.has_value ())
21461 baton
->base_address
= *cu
->base_address
;
21463 baton
->base_address
= {};
21464 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
21468 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
21469 struct dwarf2_cu
*cu
, int is_block
)
21471 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21472 struct objfile
*objfile
= per_objfile
->objfile
;
21473 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
21475 if (attr
->form_is_section_offset ()
21476 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21477 the section. If so, fall through to the complaint in the
21479 && attr
->as_unsigned () < section
->get_size (objfile
))
21481 struct dwarf2_loclist_baton
*baton
;
21483 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
21485 fill_in_loclist_baton (cu
, baton
, attr
);
21487 if (!cu
->base_address
.has_value ())
21488 complaint (_("Location list used without "
21489 "specifying the CU base address."));
21491 sym
->set_aclass_index ((is_block
21492 ? dwarf2_loclist_block_index
21493 : dwarf2_loclist_index
));
21494 SYMBOL_LOCATION_BATON (sym
) = baton
;
21498 struct dwarf2_locexpr_baton
*baton
;
21500 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
21501 baton
->per_objfile
= per_objfile
;
21502 baton
->per_cu
= cu
->per_cu
;
21503 gdb_assert (baton
->per_cu
);
21505 if (attr
->form_is_block ())
21507 /* Note that we're just copying the block's data pointer
21508 here, not the actual data. We're still pointing into the
21509 info_buffer for SYM's objfile; right now we never release
21510 that buffer, but when we do clean up properly this may
21512 struct dwarf_block
*block
= attr
->as_block ();
21513 baton
->size
= block
->size
;
21514 baton
->data
= block
->data
;
21518 dwarf2_invalid_attrib_class_complaint ("location description",
21519 sym
->natural_name ());
21523 sym
->set_aclass_index ((is_block
21524 ? dwarf2_locexpr_block_index
21525 : dwarf2_locexpr_index
));
21526 SYMBOL_LOCATION_BATON (sym
) = baton
;
21532 const comp_unit_head
*
21533 dwarf2_per_cu_data::get_header () const
21535 if (!m_header_read_in
)
21537 const gdb_byte
*info_ptr
21538 = this->section
->buffer
+ to_underlying (this->sect_off
);
21540 read_comp_unit_head (&m_header
, info_ptr
, this->section
,
21541 rcuh_kind::COMPILE
);
21543 m_header_read_in
= true;
21552 dwarf2_per_cu_data::addr_size () const
21554 return this->get_header ()->addr_size
;
21560 dwarf2_per_cu_data::offset_size () const
21562 return this->get_header ()->offset_size
;
21568 dwarf2_per_cu_data::ref_addr_size () const
21570 const comp_unit_head
*header
= this->get_header ();
21572 if (header
->version
== 2)
21573 return header
->addr_size
;
21575 return header
->offset_size
;
21581 dwarf2_per_cu_data::set_lang (enum language lang
,
21582 dwarf_source_language dw_lang
)
21584 if (unit_type () == DW_UT_partial
)
21587 /* Set if not set already. */
21588 packed
<language
, LANGUAGE_BYTES
> new_value
= lang
;
21589 packed
<language
, LANGUAGE_BYTES
> old_value
= m_lang
.exchange (new_value
);
21590 /* If already set, verify that it's the same value. */
21591 gdb_assert (old_value
== language_unknown
|| old_value
== lang
);
21593 packed
<dwarf_source_language
, 2> new_dw
= dw_lang
;
21594 packed
<dwarf_source_language
, 2> old_dw
= m_dw_lang
.exchange (new_dw
);
21595 gdb_assert (old_dw
== 0 || old_dw
== dw_lang
);
21598 /* A helper function for dwarf2_find_containing_comp_unit that returns
21599 the index of the result, and that searches a vector. It will
21600 return a result even if the offset in question does not actually
21601 occur in any CU. This is separate so that it can be unit
21605 dwarf2_find_containing_comp_unit
21606 (sect_offset sect_off
,
21607 unsigned int offset_in_dwz
,
21608 const std::vector
<dwarf2_per_cu_data_up
> &all_units
)
21613 high
= all_units
.size () - 1;
21616 struct dwarf2_per_cu_data
*mid_cu
;
21617 int mid
= low
+ (high
- low
) / 2;
21619 mid_cu
= all_units
[mid
].get ();
21620 if (mid_cu
->is_dwz
> offset_in_dwz
21621 || (mid_cu
->is_dwz
== offset_in_dwz
21622 && mid_cu
->sect_off
+ mid_cu
->length () > sect_off
))
21627 gdb_assert (low
== high
);
21631 /* Locate the .debug_info compilation unit from CU's objfile which contains
21632 the DIE at OFFSET. Raises an error on failure. */
21634 static struct dwarf2_per_cu_data
*
21635 dwarf2_find_containing_comp_unit (sect_offset sect_off
,
21636 unsigned int offset_in_dwz
,
21637 dwarf2_per_bfd
*per_bfd
)
21639 int low
= dwarf2_find_containing_comp_unit
21640 (sect_off
, offset_in_dwz
, per_bfd
->all_units
);
21641 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_units
[low
].get ();
21643 if (this_cu
->is_dwz
!= offset_in_dwz
|| this_cu
->sect_off
> sect_off
)
21645 if (low
== 0 || this_cu
->is_dwz
!= offset_in_dwz
)
21646 error (_("Dwarf Error: could not find partial DIE containing "
21647 "offset %s [in module %s]"),
21648 sect_offset_str (sect_off
),
21649 bfd_get_filename (per_bfd
->obfd
));
21651 gdb_assert (per_bfd
->all_units
[low
-1]->sect_off
21653 return per_bfd
->all_units
[low
- 1].get ();
21657 if (low
== per_bfd
->all_units
.size () - 1
21658 && sect_off
>= this_cu
->sect_off
+ this_cu
->length ())
21659 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off
));
21660 gdb_assert (sect_off
< this_cu
->sect_off
+ this_cu
->length ());
21667 namespace selftests
{
21668 namespace find_containing_comp_unit
{
21673 dwarf2_per_cu_data_up
one (new dwarf2_per_cu_data
);
21674 dwarf2_per_cu_data
*one_ptr
= one
.get ();
21675 dwarf2_per_cu_data_up
two (new dwarf2_per_cu_data
);
21676 dwarf2_per_cu_data
*two_ptr
= two
.get ();
21677 dwarf2_per_cu_data_up
three (new dwarf2_per_cu_data
);
21678 dwarf2_per_cu_data
*three_ptr
= three
.get ();
21679 dwarf2_per_cu_data_up
four (new dwarf2_per_cu_data
);
21680 dwarf2_per_cu_data
*four_ptr
= four
.get ();
21682 one
->set_length (5);
21683 two
->sect_off
= sect_offset (one
->length ());
21684 two
->set_length (7);
21686 three
->set_length (5);
21688 four
->sect_off
= sect_offset (three
->length ());
21689 four
->set_length (7);
21692 std::vector
<dwarf2_per_cu_data_up
> units
;
21693 units
.push_back (std::move (one
));
21694 units
.push_back (std::move (two
));
21695 units
.push_back (std::move (three
));
21696 units
.push_back (std::move (four
));
21700 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 0, units
);
21701 SELF_CHECK (units
[result
].get () == one_ptr
);
21702 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 0, units
);
21703 SELF_CHECK (units
[result
].get () == one_ptr
);
21704 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 0, units
);
21705 SELF_CHECK (units
[result
].get () == two_ptr
);
21707 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 1, units
);
21708 SELF_CHECK (units
[result
].get () == three_ptr
);
21709 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 1, units
);
21710 SELF_CHECK (units
[result
].get () == three_ptr
);
21711 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 1, units
);
21712 SELF_CHECK (units
[result
].get () == four_ptr
);
21718 #endif /* GDB_SELF_TEST */
21720 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21723 prepare_one_comp_unit (struct dwarf2_cu
*cu
, struct die_info
*comp_unit_die
,
21724 enum language pretend_language
)
21726 struct attribute
*attr
;
21728 cu
->producer
= dwarf2_string_attr (comp_unit_die
, DW_AT_producer
, cu
);
21730 /* Set the language we're debugging. */
21731 attr
= dwarf2_attr (comp_unit_die
, DW_AT_language
, cu
);
21732 enum language lang
;
21733 dwarf_source_language dw_lang
= (dwarf_source_language
) 0;
21734 if (cu
->producer
!= nullptr
21735 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
21737 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21738 attribute is not standardised yet. As a workaround for the
21739 language detection we fall back to the DW_AT_producer
21741 lang
= language_opencl
;
21742 dw_lang
= DW_LANG_OpenCL
;
21744 else if (cu
->producer
!= nullptr
21745 && strstr (cu
->producer
, "GNU Go ") != NULL
)
21747 /* Similar hack for Go. */
21748 lang
= language_go
;
21749 dw_lang
= DW_LANG_Go
;
21751 else if (attr
!= nullptr)
21753 lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
21754 dw_lang
= (dwarf_source_language
) attr
->constant_value (0);
21757 lang
= pretend_language
;
21759 cu
->language_defn
= language_def (lang
);
21761 switch (comp_unit_die
->tag
)
21763 case DW_TAG_compile_unit
:
21764 cu
->per_cu
->set_unit_type (DW_UT_compile
);
21766 case DW_TAG_partial_unit
:
21767 cu
->per_cu
->set_unit_type (DW_UT_partial
);
21769 case DW_TAG_type_unit
:
21770 cu
->per_cu
->set_unit_type (DW_UT_type
);
21773 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21774 dwarf_tag_name (comp_unit_die
->tag
),
21775 sect_offset_str (cu
->per_cu
->sect_off
));
21778 cu
->per_cu
->set_lang (lang
, dw_lang
);
21784 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
21786 auto it
= m_dwarf2_cus
.find (per_cu
);
21787 if (it
== m_dwarf2_cus
.end ())
21790 return it
->second
.get ();
21796 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
,
21797 std::unique_ptr
<dwarf2_cu
> cu
)
21799 gdb_assert (this->get_cu (per_cu
) == nullptr);
21801 m_dwarf2_cus
[per_cu
] = std::move (cu
);
21807 dwarf2_per_objfile::age_comp_units ()
21809 dwarf_read_debug_printf_v ("running");
21811 /* This is not expected to be called in the middle of CU expansion. There is
21812 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21813 loaded in memory. Calling age_comp_units while the queue is in use could
21814 make us free the DIEs for a CU that is in the queue and therefore break
21816 gdb_assert (!queue
.has_value ());
21818 /* Start by clearing all marks. */
21819 for (const auto &pair
: m_dwarf2_cus
)
21820 pair
.second
->clear_mark ();
21822 /* Traverse all CUs, mark them and their dependencies if used recently
21824 for (const auto &pair
: m_dwarf2_cus
)
21826 dwarf2_cu
*cu
= pair
.second
.get ();
21829 if (cu
->last_used
<= dwarf_max_cache_age
)
21833 /* Delete all CUs still not marked. */
21834 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
21836 dwarf2_cu
*cu
= it
->second
.get ();
21838 if (!cu
->is_marked ())
21840 dwarf_read_debug_printf_v ("deleting old CU %s",
21841 sect_offset_str (cu
->per_cu
->sect_off
));
21842 it
= m_dwarf2_cus
.erase (it
);
21852 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
21854 auto it
= m_dwarf2_cus
.find (per_cu
);
21855 if (it
== m_dwarf2_cus
.end ())
21858 m_dwarf2_cus
.erase (it
);
21861 dwarf2_per_objfile::~dwarf2_per_objfile ()
21866 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21867 We store these in a hash table separate from the DIEs, and preserve them
21868 when the DIEs are flushed out of cache.
21870 The CU "per_cu" pointer is needed because offset alone is not enough to
21871 uniquely identify the type. A file may have multiple .debug_types sections,
21872 or the type may come from a DWO file. Furthermore, while it's more logical
21873 to use per_cu->section+offset, with Fission the section with the data is in
21874 the DWO file but we don't know that section at the point we need it.
21875 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21876 because we can enter the lookup routine, get_die_type_at_offset, from
21877 outside this file, and thus won't necessarily have PER_CU->cu.
21878 Fortunately, PER_CU is stable for the life of the objfile. */
21880 struct dwarf2_per_cu_offset_and_type
21882 const struct dwarf2_per_cu_data
*per_cu
;
21883 sect_offset sect_off
;
21887 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21890 per_cu_offset_and_type_hash (const void *item
)
21892 const struct dwarf2_per_cu_offset_and_type
*ofs
21893 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
21895 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
21898 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21901 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
21903 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
21904 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
21905 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
21906 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
21908 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
21909 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
21912 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21913 table if necessary. For convenience, return TYPE.
21915 The DIEs reading must have careful ordering to:
21916 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21917 reading current DIE.
21918 * Not trying to dereference contents of still incompletely read in types
21919 while reading in other DIEs.
21920 * Enable referencing still incompletely read in types just by a pointer to
21921 the type without accessing its fields.
21923 Therefore caller should follow these rules:
21924 * Try to fetch any prerequisite types we may need to build this DIE type
21925 before building the type and calling set_die_type.
21926 * After building type call set_die_type for current DIE as soon as
21927 possible before fetching more types to complete the current type.
21928 * Make the type as complete as possible before fetching more types. */
21930 static struct type
*
21931 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
21932 bool skip_data_location
)
21934 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21935 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
21936 struct objfile
*objfile
= per_objfile
->objfile
;
21937 struct attribute
*attr
;
21938 struct dynamic_prop prop
;
21940 /* For Ada types, make sure that the gnat-specific data is always
21941 initialized (if not already set). There are a few types where
21942 we should not be doing so, because the type-specific area is
21943 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21944 where the type-specific area is used to store the floatformat).
21945 But this is not a problem, because the gnat-specific information
21946 is actually not needed for these types. */
21947 if (need_gnat_info (cu
)
21948 && type
->code () != TYPE_CODE_FUNC
21949 && type
->code () != TYPE_CODE_FLT
21950 && type
->code () != TYPE_CODE_METHODPTR
21951 && type
->code () != TYPE_CODE_MEMBERPTR
21952 && type
->code () != TYPE_CODE_METHOD
21953 && type
->code () != TYPE_CODE_FIXED_POINT
21954 && !HAVE_GNAT_AUX_INFO (type
))
21955 INIT_GNAT_SPECIFIC (type
);
21957 /* Read DW_AT_allocated and set in type. */
21958 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
21961 struct type
*prop_type
= cu
->addr_sized_int_type (false);
21962 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
21963 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
21966 /* Read DW_AT_associated and set in type. */
21967 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
21970 struct type
*prop_type
= cu
->addr_sized_int_type (false);
21971 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
21972 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
21975 /* Read DW_AT_rank and set in type. */
21976 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
21979 struct type
*prop_type
= cu
->addr_sized_int_type (false);
21980 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
21981 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
21984 /* Read DW_AT_data_location and set in type. */
21985 if (!skip_data_location
)
21987 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
21988 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
21989 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
21992 if (per_objfile
->die_type_hash
== NULL
)
21993 per_objfile
->die_type_hash
21994 = htab_up (htab_create_alloc (127,
21995 per_cu_offset_and_type_hash
,
21996 per_cu_offset_and_type_eq
,
21997 NULL
, xcalloc
, xfree
));
21999 ofs
.per_cu
= cu
->per_cu
;
22000 ofs
.sect_off
= die
->sect_off
;
22002 slot
= (struct dwarf2_per_cu_offset_and_type
**)
22003 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
22005 complaint (_("A problem internal to GDB: DIE %s has type already set"),
22006 sect_offset_str (die
->sect_off
));
22007 *slot
= XOBNEW (&objfile
->objfile_obstack
,
22008 struct dwarf2_per_cu_offset_and_type
);
22013 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
22014 or return NULL if the die does not have a saved type. */
22016 static struct type
*
22017 get_die_type_at_offset (sect_offset sect_off
,
22018 dwarf2_per_cu_data
*per_cu
,
22019 dwarf2_per_objfile
*per_objfile
)
22021 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
22023 if (per_objfile
->die_type_hash
== NULL
)
22026 ofs
.per_cu
= per_cu
;
22027 ofs
.sect_off
= sect_off
;
22028 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
22029 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
22036 /* Look up the type for DIE in CU in die_type_hash,
22037 or return NULL if DIE does not have a saved type. */
22039 static struct type
*
22040 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
22042 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
22045 struct cmd_list_element
*set_dwarf_cmdlist
;
22046 struct cmd_list_element
*show_dwarf_cmdlist
;
22049 show_check_physname (struct ui_file
*file
, int from_tty
,
22050 struct cmd_list_element
*c
, const char *value
)
22053 _("Whether to check \"physname\" is %s.\n"),
22057 void _initialize_dwarf2_read ();
22059 _initialize_dwarf2_read ()
22061 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
22063 Set DWARF specific variables.\n\
22064 Configure DWARF variables such as the cache size."),
22066 Show DWARF specific variables.\n\
22067 Show DWARF variables such as the cache size."),
22068 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
22069 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
22071 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
22072 &dwarf_max_cache_age
, _("\
22073 Set the upper bound on the age of cached DWARF compilation units."), _("\
22074 Show the upper bound on the age of cached DWARF compilation units."), _("\
22075 A higher limit means that cached compilation units will be stored\n\
22076 in memory longer, and more total memory will be used. Zero disables\n\
22077 caching, which can slow down startup."),
22079 show_dwarf_max_cache_age
,
22080 &set_dwarf_cmdlist
,
22081 &show_dwarf_cmdlist
);
22083 add_setshow_boolean_cmd ("synchronous", class_obscure
,
22084 &dwarf_synchronous
, _("\
22085 Set whether DWARF is read synchronously."), _("\
22086 Show whether DWARF is read synchronously."), _("\
22087 By default, DWARF information is read in worker threads,\n\
22088 and gdb will not generally wait for the reading to complete\n\
22089 before continuing with other work, for example presenting a\n\
22090 prompt to the user.\n\
22091 Enabling this setting will cause the DWARF reader to always wait\n\
22092 for debug info processing to be finished before gdb can proceed."),
22094 show_dwarf_synchronous
,
22095 &set_dwarf_cmdlist
,
22096 &show_dwarf_cmdlist
);
22098 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
22099 Set debugging of the DWARF reader."), _("\
22100 Show debugging of the DWARF reader."), _("\
22101 When enabled (non-zero), debugging messages are printed during DWARF\n\
22102 reading and symtab expansion. A value of 1 (one) provides basic\n\
22103 information. A value greater than 1 provides more verbose information."),
22106 &setdebuglist
, &showdebuglist
);
22108 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
22109 Set debugging of the DWARF DIE reader."), _("\
22110 Show debugging of the DWARF DIE reader."), _("\
22111 When enabled (non-zero), DIEs are dumped after they are read in.\n\
22112 The value is the maximum depth to print."),
22115 &setdebuglist
, &showdebuglist
);
22117 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
22118 Set debugging of the dwarf line reader."), _("\
22119 Show debugging of the dwarf line reader."), _("\
22120 When enabled (non-zero), line number entries are dumped as they are read in.\n\
22121 A value of 1 (one) provides basic information.\n\
22122 A value greater than 1 provides more verbose information."),
22125 &setdebuglist
, &showdebuglist
);
22127 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
22128 Set cross-checking of \"physname\" code against demangler."), _("\
22129 Show cross-checking of \"physname\" code against demangler."), _("\
22130 When enabled, GDB's internal \"physname\" code is checked against\n\
22132 NULL
, show_check_physname
,
22133 &setdebuglist
, &showdebuglist
);
22135 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
22136 &dwarf2_locexpr_funcs
);
22137 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
22138 &dwarf2_loclist_funcs
);
22139 ada_imported_index
= register_symbol_computed_impl (LOC_COMPUTED
,
22140 &ada_imported_funcs
);
22142 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
22143 &dwarf2_block_frame_base_locexpr_funcs
);
22144 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
22145 &dwarf2_block_frame_base_loclist_funcs
);
22146 ada_block_index
= register_symbol_block_impl (LOC_BLOCK
,
22147 &ada_function_alias_funcs
);
22150 selftests::register_test ("dw2_expand_symtabs_matching",
22151 selftests::dw2_expand_symtabs_matching::run_test
);
22152 selftests::register_test ("dwarf2_find_containing_comp_unit",
22153 selftests::find_containing_comp_unit::run_test
);