1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2024 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section. */
30 #include "dwarf2/read.h"
31 #include "dwarf2/abbrev.h"
32 #include "dwarf2/aranges.h"
33 #include "dwarf2/attribute.h"
34 #include "dwarf2/comp-unit-head.h"
35 #include "dwarf2/cu.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/read-debug-names.h"
44 #include "dwarf2/read-gdb-index.h"
45 #include "dwarf2/sect-names.h"
46 #include "dwarf2/stringify.h"
47 #include "dwarf2/public.h"
50 #include "event-top.h"
56 #include "gdb-demangle.h"
57 #include "filenames.h"
59 #include "complaints.h"
60 #include "dwarf2/expr.h"
61 #include "dwarf2/loc.h"
62 #include "cp-support.h"
65 #include "cli/cli-cmds.h"
68 #include "typeprint.h"
73 #include "gdb/gdb-index.h"
78 #include "namespace.h"
79 #include "gdbsupport/function-view.h"
81 #include "gdbsupport/underlying.h"
82 #include "gdbsupport/hash_enum.h"
83 #include "filename-seen-cache.h"
87 #include <unordered_map>
88 #include "gdbsupport/selftest.h"
89 #include "rust-lang.h"
90 #include "gdbsupport/pathstuff.h"
91 #include "count-one-bits.h"
92 #include <unordered_set>
93 #include "dwarf2/abbrev-cache.h"
94 #include "cooked-index.h"
95 #include "split-name.h"
96 #include "gdbsupport/thread-pool.h"
97 #include "run-on-main-thread.h"
98 #include "dwarf2/parent-map.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
);
152 static bool producer_is_gas_ge_2_39 (struct dwarf2_cu
*cu
);
154 /* Size of .debug_loclists section header for 32-bit DWARF format. */
155 #define LOCLIST_HEADER_SIZE32 12
157 /* Size of .debug_loclists section header for 64-bit DWARF format. */
158 #define LOCLIST_HEADER_SIZE64 20
160 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
161 #define RNGLIST_HEADER_SIZE32 12
163 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
164 #define RNGLIST_HEADER_SIZE64 20
166 /* See dwarf2/read.h. */
169 get_dwarf2_per_objfile (struct objfile
*objfile
)
171 return dwarf2_objfile_data_key
.get (objfile
);
174 /* Default names of the debugging sections. */
176 /* Note that if the debugging section has been compressed, it might
177 have a name like .zdebug_info. */
179 const struct dwarf2_debug_sections dwarf2_elf_names
=
181 { ".debug_info", ".zdebug_info" },
182 { ".debug_abbrev", ".zdebug_abbrev" },
183 { ".debug_line", ".zdebug_line" },
184 { ".debug_loc", ".zdebug_loc" },
185 { ".debug_loclists", ".zdebug_loclists" },
186 { ".debug_macinfo", ".zdebug_macinfo" },
187 { ".debug_macro", ".zdebug_macro" },
188 { ".debug_str", ".zdebug_str" },
189 { ".debug_str_offsets", ".zdebug_str_offsets" },
190 { ".debug_line_str", ".zdebug_line_str" },
191 { ".debug_ranges", ".zdebug_ranges" },
192 { ".debug_rnglists", ".zdebug_rnglists" },
193 { ".debug_types", ".zdebug_types" },
194 { ".debug_addr", ".zdebug_addr" },
195 { ".debug_frame", ".zdebug_frame" },
196 { ".eh_frame", NULL
},
197 { ".gdb_index", ".zgdb_index" },
198 { ".debug_names", ".zdebug_names" },
199 { ".debug_aranges", ".zdebug_aranges" },
203 /* List of DWO/DWP sections. */
205 static const struct dwop_section_names
207 struct dwarf2_section_names abbrev_dwo
;
208 struct dwarf2_section_names info_dwo
;
209 struct dwarf2_section_names line_dwo
;
210 struct dwarf2_section_names loc_dwo
;
211 struct dwarf2_section_names loclists_dwo
;
212 struct dwarf2_section_names macinfo_dwo
;
213 struct dwarf2_section_names macro_dwo
;
214 struct dwarf2_section_names rnglists_dwo
;
215 struct dwarf2_section_names str_dwo
;
216 struct dwarf2_section_names str_offsets_dwo
;
217 struct dwarf2_section_names types_dwo
;
218 struct dwarf2_section_names cu_index
;
219 struct dwarf2_section_names tu_index
;
223 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
224 { ".debug_info.dwo", ".zdebug_info.dwo" },
225 { ".debug_line.dwo", ".zdebug_line.dwo" },
226 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
227 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
228 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
229 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
230 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
231 { ".debug_str.dwo", ".zdebug_str.dwo" },
232 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
233 { ".debug_types.dwo", ".zdebug_types.dwo" },
234 { ".debug_cu_index", ".zdebug_cu_index" },
235 { ".debug_tu_index", ".zdebug_tu_index" },
238 /* local data types */
240 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
241 begin with a header, which contains the following information. */
242 struct loclists_rnglists_header
244 /* A 4-byte or 12-byte length containing the length of the
245 set of entries for this compilation unit, not including the
246 length field itself. */
249 /* A 2-byte version identifier. */
252 /* A 1-byte unsigned integer containing the size in bytes of an address on
253 the target system. */
254 unsigned char addr_size
;
256 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
257 on the target system. */
258 unsigned char segment_collector_size
;
260 /* A 4-byte count of the number of offsets that follow the header. */
261 unsigned int offset_entry_count
;
264 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
265 This includes type_unit_group and quick_file_names. */
267 struct stmt_list_hash
269 /* The DWO unit this table is from or NULL if there is none. */
270 struct dwo_unit
*dwo_unit
;
272 /* Offset in .debug_line or .debug_line.dwo. */
273 sect_offset line_sect_off
;
276 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
277 an object of this type. This contains elements of type unit groups
278 that can be shared across objfiles. The non-shareable parts are in
279 type_unit_group_unshareable. */
281 struct type_unit_group
283 /* The data used to construct the hash key. */
284 struct stmt_list_hash hash
{};
287 /* These sections are what may appear in a (real or virtual) DWO file. */
291 struct dwarf2_section_info abbrev
;
292 struct dwarf2_section_info line
;
293 struct dwarf2_section_info loc
;
294 struct dwarf2_section_info loclists
;
295 struct dwarf2_section_info macinfo
;
296 struct dwarf2_section_info macro
;
297 struct dwarf2_section_info rnglists
;
298 struct dwarf2_section_info str
;
299 struct dwarf2_section_info str_offsets
;
300 /* In the case of a virtual DWO file, these two are unused. */
301 struct dwarf2_section_info info
;
302 std::vector
<dwarf2_section_info
> types
;
305 /* CUs/TUs in DWP/DWO files. */
309 /* Backlink to the containing struct dwo_file. */
310 struct dwo_file
*dwo_file
;
312 /* The "id" that distinguishes this CU/TU.
313 .debug_info calls this "dwo_id", .debug_types calls this "signature".
314 Since signatures came first, we stick with it for consistency. */
317 /* The section this CU/TU lives in, in the DWO file. */
318 struct dwarf2_section_info
*section
;
320 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
321 sect_offset sect_off
;
324 /* For types, offset in the type's DIE of the type defined by this TU. */
325 cu_offset type_offset_in_tu
;
328 /* include/dwarf2.h defines the DWP section codes.
329 It defines a max value but it doesn't define a min value, which we
330 use for error checking, so provide one. */
332 enum dwp_v2_section_ids
337 /* Data for one DWO file.
339 This includes virtual DWO files (a virtual DWO file is a DWO file as it
340 appears in a DWP file). DWP files don't really have DWO files per se -
341 comdat folding of types "loses" the DWO file they came from, and from
342 a high level view DWP files appear to contain a mass of random types.
343 However, to maintain consistency with the non-DWP case we pretend DWP
344 files contain virtual DWO files, and we assign each TU with one virtual
345 DWO file (generally based on the line and abbrev section offsets -
346 a heuristic that seems to work in practice). */
350 dwo_file () = default;
351 DISABLE_COPY_AND_ASSIGN (dwo_file
);
353 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
354 For virtual DWO files the name is constructed from the section offsets
355 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
356 from related CU+TUs. */
357 std::string dwo_name
;
359 /* The DW_AT_comp_dir attribute. */
360 const char *comp_dir
= nullptr;
362 /* The bfd, when the file is open. Otherwise this is NULL.
363 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
364 gdb_bfd_ref_ptr dbfd
;
366 /* The sections that make up this DWO file.
367 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
368 sections (for lack of a better name). */
369 struct dwo_sections sections
{};
371 /* The CUs in the file.
372 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
373 an extension to handle LLVM's Link Time Optimization output (where
374 multiple source files may be compiled into a single object/dwo pair). */
377 /* Table of TUs in the file.
378 Each element is a struct dwo_unit. */
382 /* These sections are what may appear in a DWP file. */
386 /* These are used by all DWP versions (1, 2 and 5). */
387 struct dwarf2_section_info str
;
388 struct dwarf2_section_info cu_index
;
389 struct dwarf2_section_info tu_index
;
391 /* These are only used by DWP version 2 and version 5 files.
392 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
393 sections are referenced by section number, and are not recorded here.
394 In DWP version 2 or 5 there is at most one copy of all these sections,
395 each section being (effectively) comprised of the concatenation of all of
396 the individual sections that exist in the version 1 format.
397 To keep the code simple we treat each of these concatenated pieces as a
398 section itself (a virtual section?). */
399 struct dwarf2_section_info abbrev
;
400 struct dwarf2_section_info info
;
401 struct dwarf2_section_info line
;
402 struct dwarf2_section_info loc
;
403 struct dwarf2_section_info loclists
;
404 struct dwarf2_section_info macinfo
;
405 struct dwarf2_section_info macro
;
406 struct dwarf2_section_info rnglists
;
407 struct dwarf2_section_info str_offsets
;
408 struct dwarf2_section_info types
;
411 /* These sections are what may appear in a virtual DWO file in DWP version 1.
412 A virtual DWO file is a DWO file as it appears in a DWP file. */
414 struct virtual_v1_dwo_sections
416 struct dwarf2_section_info abbrev
;
417 struct dwarf2_section_info line
;
418 struct dwarf2_section_info loc
;
419 struct dwarf2_section_info macinfo
;
420 struct dwarf2_section_info macro
;
421 struct dwarf2_section_info str_offsets
;
422 /* Each DWP hash table entry records one CU or one TU.
423 That is recorded here, and copied to dwo_unit.section. */
424 struct dwarf2_section_info info_or_types
;
427 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
428 In version 2, the sections of the DWO files are concatenated together
429 and stored in one section of that name. Thus each ELF section contains
430 several "virtual" sections. */
432 struct virtual_v2_or_v5_dwo_sections
434 bfd_size_type abbrev_offset
;
435 bfd_size_type abbrev_size
;
437 bfd_size_type line_offset
;
438 bfd_size_type line_size
;
440 bfd_size_type loc_offset
;
441 bfd_size_type loc_size
;
443 bfd_size_type loclists_offset
;
444 bfd_size_type loclists_size
;
446 bfd_size_type macinfo_offset
;
447 bfd_size_type macinfo_size
;
449 bfd_size_type macro_offset
;
450 bfd_size_type macro_size
;
452 bfd_size_type rnglists_offset
;
453 bfd_size_type rnglists_size
;
455 bfd_size_type str_offsets_offset
;
456 bfd_size_type str_offsets_size
;
458 /* Each DWP hash table entry records one CU or one TU.
459 That is recorded here, and copied to dwo_unit.section. */
460 bfd_size_type info_or_types_offset
;
461 bfd_size_type info_or_types_size
;
464 /* Contents of DWP hash tables. */
466 struct dwp_hash_table
468 uint32_t version
, nr_columns
;
469 uint32_t nr_units
, nr_slots
;
470 const gdb_byte
*hash_table
, *unit_table
;
475 const gdb_byte
*indices
;
479 /* This is indexed by column number and gives the id of the section
481 #define MAX_NR_V2_DWO_SECTIONS \
482 (1 /* .debug_info or .debug_types */ \
483 + 1 /* .debug_abbrev */ \
484 + 1 /* .debug_line */ \
485 + 1 /* .debug_loc */ \
486 + 1 /* .debug_str_offsets */ \
487 + 1 /* .debug_macro or .debug_macinfo */)
488 int section_ids
[MAX_NR_V2_DWO_SECTIONS
];
489 const gdb_byte
*offsets
;
490 const gdb_byte
*sizes
;
494 /* This is indexed by column number and gives the id of the section
496 #define MAX_NR_V5_DWO_SECTIONS \
497 (1 /* .debug_info */ \
498 + 1 /* .debug_abbrev */ \
499 + 1 /* .debug_line */ \
500 + 1 /* .debug_loclists */ \
501 + 1 /* .debug_str_offsets */ \
502 + 1 /* .debug_macro */ \
503 + 1 /* .debug_rnglists */)
504 int section_ids
[MAX_NR_V5_DWO_SECTIONS
];
505 const gdb_byte
*offsets
;
506 const gdb_byte
*sizes
;
511 /* Data for one DWP file. */
515 dwp_file (const char *name_
, gdb_bfd_ref_ptr
&&abfd
)
517 dbfd (std::move (abfd
))
521 /* Name of the file. */
524 /* File format version. */
528 gdb_bfd_ref_ptr dbfd
;
530 /* Section info for this file. */
531 struct dwp_sections sections
{};
533 /* Table of CUs in the file. */
534 const struct dwp_hash_table
*cus
= nullptr;
536 /* Table of TUs in the file. */
537 const struct dwp_hash_table
*tus
= nullptr;
539 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
543 /* Table to map ELF section numbers to their sections.
544 This is only needed for the DWP V1 file format. */
545 unsigned int num_sections
= 0;
546 asection
**elf_sections
= nullptr;
549 /* Struct used to pass misc. parameters to read_die_and_children, et
550 al. which are used for both .debug_info and .debug_types dies.
551 All parameters here are unchanging for the life of the call. This
552 struct exists to abstract away the constant parameters of die reading. */
554 struct die_reader_specs
556 /* The bfd of die_section. */
559 /* The CU of the DIE we are parsing. */
560 struct dwarf2_cu
*cu
;
562 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
563 struct dwo_file
*dwo_file
;
565 /* The section the die comes from.
566 This is either .debug_info or .debug_types, or the .dwo variants. */
567 struct dwarf2_section_info
*die_section
;
569 /* die_section->buffer. */
570 const gdb_byte
*buffer
;
572 /* The end of the buffer. */
573 const gdb_byte
*buffer_end
;
575 /* The abbreviation table to use when reading the DIEs. */
576 struct abbrev_table
*abbrev_table
;
579 /* A subclass of die_reader_specs that holds storage and has complex
580 constructor and destructor behavior. */
582 class cutu_reader
: public die_reader_specs
586 cutu_reader (dwarf2_per_cu_data
*this_cu
,
587 dwarf2_per_objfile
*per_objfile
,
588 struct abbrev_table
*abbrev_table
,
589 dwarf2_cu
*existing_cu
,
591 abbrev_cache
*cache
= nullptr);
593 explicit cutu_reader (struct dwarf2_per_cu_data
*this_cu
,
594 dwarf2_per_objfile
*per_objfile
,
595 struct dwarf2_cu
*parent_cu
= nullptr,
596 struct dwo_file
*dwo_file
= nullptr);
598 DISABLE_COPY_AND_ASSIGN (cutu_reader
);
600 cutu_reader (cutu_reader
&&) = default;
602 const gdb_byte
*info_ptr
= nullptr;
603 struct die_info
*comp_unit_die
= nullptr;
604 bool dummy_p
= false;
606 /* Release the new CU, putting it on the chain. This cannot be done
610 /* Release the abbrev table, transferring ownership to the
612 abbrev_table_up
release_abbrev_table ()
614 return std::move (m_abbrev_table_holder
);
618 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
619 dwarf2_per_objfile
*per_objfile
,
620 dwarf2_cu
*existing_cu
);
622 struct dwarf2_per_cu_data
*m_this_cu
;
623 std::unique_ptr
<dwarf2_cu
> m_new_cu
;
625 /* The ordinary abbreviation table. */
626 abbrev_table_up m_abbrev_table_holder
;
628 /* The DWO abbreviation table. */
629 abbrev_table_up m_dwo_abbrev_table
;
632 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
633 but this would require a corresponding change in unpack_field_as_long
635 static int bits_per_byte
= 8;
637 struct variant_part_builder
;
639 /* When reading a variant, we track a bit more information about the
640 field, and store it in an object of this type. */
644 int first_field
= -1;
647 /* A variant can contain other variant parts. */
648 std::vector
<variant_part_builder
> variant_parts
;
650 /* If we see a DW_TAG_variant, then this will be set if this is the
652 bool default_branch
= false;
653 /* If we see a DW_AT_discr_value, then this will be the discriminant
655 ULONGEST discriminant_value
= 0;
656 /* If we see a DW_AT_discr_list, then this is a pointer to the list
658 struct dwarf_block
*discr_list_data
= nullptr;
661 /* This represents a DW_TAG_variant_part. */
663 struct variant_part_builder
665 /* The offset of the discriminant field. */
666 sect_offset discriminant_offset
{};
668 /* Variants that are direct children of this variant part. */
669 std::vector
<variant_field
> variants
;
671 /* True if we're currently reading a variant. */
672 bool processing_variant
= false;
677 /* Variant parts need to find the discriminant, which is a DIE
678 reference. We track the section offset of each field to make
681 struct field field
{};
686 const char *name
= nullptr;
687 std::vector
<struct fn_field
> fnfields
;
690 /* The routines that read and process dies for a C struct or C++ class
691 pass lists of data member fields and lists of member function fields
692 in an instance of a field_info structure, as defined below. */
695 /* List of data member and baseclasses fields. */
696 std::vector
<struct nextfield
> fields
;
697 std::vector
<struct nextfield
> baseclasses
;
699 /* Member function fieldlist array, contains name of possibly overloaded
700 member function, number of overloaded member functions and a pointer
701 to the head of the member function field chain. */
702 std::vector
<struct fnfieldlist
> fnfieldlists
;
704 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
705 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
706 std::vector
<struct decl_field
> typedef_field_list
;
708 /* Nested types defined by this class and the number of elements in this
710 std::vector
<struct decl_field
> nested_types_list
;
712 /* If non-null, this is the variant part we are currently
714 variant_part_builder
*current_variant_part
= nullptr;
715 /* This holds all the top-level variant parts attached to the type
717 std::vector
<variant_part_builder
> variant_parts
;
719 /* Return the total number of fields (including baseclasses). */
722 return fields
.size () + baseclasses
.size ();
726 /* Loaded secondary compilation units are kept in memory until they
727 have not been referenced for the processing of this many
728 compilation units. Set this to zero to disable caching. Cache
729 sizes of up to at least twenty will improve startup time for
730 typical inter-CU-reference binaries, at an obvious memory cost. */
731 static int dwarf_max_cache_age
= 5;
733 show_dwarf_max_cache_age (struct ui_file
*file
, int from_tty
,
734 struct cmd_list_element
*c
, const char *value
)
736 gdb_printf (file
, _("The upper bound on the age of cached "
737 "DWARF compilation units is %s.\n"),
741 /* When true, wait for DWARF reading to be complete. */
742 static bool dwarf_synchronous
= false;
744 /* "Show" callback for "maint set dwarf synchronous". */
746 show_dwarf_synchronous (struct ui_file
*file
, int from_tty
,
747 struct cmd_list_element
*c
, const char *value
)
749 gdb_printf (file
, _("Whether DWARF reading is synchronous is %s.\n"),
753 /* local function prototypes */
755 static void dwarf2_find_base_address (struct die_info
*die
,
756 struct dwarf2_cu
*cu
);
758 static void build_type_psymtabs_reader (cutu_reader
*reader
,
759 cooked_index_storage
*storage
);
761 static void var_decode_location (struct attribute
*attr
,
763 struct dwarf2_cu
*cu
);
765 static unsigned int peek_abbrev_code (bfd
*, const gdb_byte
*);
767 static const gdb_byte
*read_attribute (const struct die_reader_specs
*,
769 const struct attr_abbrev
*,
771 bool allow_reprocess
= true);
773 /* Note that the default for TAG is chosen because it only matters
774 when reading the top-level DIE, and that function is careful to
775 pass the correct tag. */
776 static void read_attribute_reprocess (const struct die_reader_specs
*reader
,
777 struct attribute
*attr
,
778 dwarf_tag tag
= DW_TAG_padding
);
780 static unrelocated_addr
read_addr_index (struct dwarf2_cu
*cu
,
781 unsigned int addr_index
);
783 static sect_offset
read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
784 dwarf2_section_info
*, sect_offset
);
786 static const char *read_indirect_string
787 (dwarf2_per_objfile
*per_objfile
, bfd
*, const gdb_byte
*,
788 const struct comp_unit_head
*, unsigned int *);
790 static unrelocated_addr
read_addr_index_from_leb128 (struct dwarf2_cu
*,
794 static const char *read_dwo_str_index (const struct die_reader_specs
*reader
,
797 static const char *read_stub_str_index (struct dwarf2_cu
*cu
,
800 static struct attribute
*dwarf2_attr (struct die_info
*, unsigned int,
803 static const char *dwarf2_string_attr (struct die_info
*die
, unsigned int name
,
804 struct dwarf2_cu
*cu
);
806 static const char *dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
);
808 static int dwarf2_flag_true_p (struct die_info
*die
, unsigned name
,
809 struct dwarf2_cu
*cu
);
811 static int die_is_declaration (struct die_info
*, struct dwarf2_cu
*cu
);
813 static struct die_info
*die_specification (struct die_info
*die
,
814 struct dwarf2_cu
**);
816 static line_header_up
dwarf_decode_line_header (sect_offset sect_off
,
817 struct dwarf2_cu
*cu
,
818 const char *comp_dir
);
820 static void dwarf_decode_lines (struct line_header
*,
822 unrelocated_addr
, int decode_mapping
);
824 static void dwarf2_start_subfile (dwarf2_cu
*cu
, const file_entry
&fe
,
825 const line_header
&lh
);
827 static struct symbol
*new_symbol (struct die_info
*, struct type
*,
828 struct dwarf2_cu
*, struct symbol
* = NULL
);
830 static void dwarf2_const_value (const struct attribute
*, struct symbol
*,
833 static void dwarf2_const_value_attr (const struct attribute
*attr
,
836 struct obstack
*obstack
,
837 struct dwarf2_cu
*cu
, LONGEST
*value
,
838 const gdb_byte
**bytes
,
839 struct dwarf2_locexpr_baton
**baton
);
841 static struct type
*read_subrange_index_type (struct die_info
*die
,
842 struct dwarf2_cu
*cu
);
844 static struct type
*die_type (struct die_info
*, struct dwarf2_cu
*);
846 static int need_gnat_info (struct dwarf2_cu
*);
848 static struct type
*die_descriptive_type (struct die_info
*,
851 static void set_descriptive_type (struct type
*, struct die_info
*,
854 static struct type
*die_containing_type (struct die_info
*,
857 static struct type
*lookup_die_type (struct die_info
*, const struct attribute
*,
860 static struct type
*read_type_die (struct die_info
*, struct dwarf2_cu
*);
862 static struct type
*read_type_die_1 (struct die_info
*, struct dwarf2_cu
*);
864 static const char *determine_prefix (struct die_info
*die
, struct dwarf2_cu
*);
866 static char *typename_concat (struct obstack
*obs
, const char *prefix
,
867 const char *suffix
, int physname
,
868 struct dwarf2_cu
*cu
);
870 static void read_file_scope (struct die_info
*, struct dwarf2_cu
*);
872 static void read_type_unit_scope (struct die_info
*, struct dwarf2_cu
*);
874 static void read_func_scope (struct die_info
*, struct dwarf2_cu
*);
876 static void read_lexical_block_scope (struct die_info
*, struct dwarf2_cu
*);
878 static void read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
);
880 static void read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
);
882 /* Return the .debug_loclists section to use for cu. */
883 static struct dwarf2_section_info
*cu_debug_loc_section (struct dwarf2_cu
*cu
);
885 /* Return the .debug_rnglists section to use for cu. */
886 static struct dwarf2_section_info
*cu_debug_rnglists_section
887 (struct dwarf2_cu
*cu
, dwarf_tag tag
);
889 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
890 values. Keep the items ordered with increasing constraints compliance. */
893 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
894 PC_BOUNDS_NOT_PRESENT
,
896 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
897 were present but they do not form a valid range of PC addresses. */
900 /* Discontiguous range was found - that is DW_AT_ranges was found. */
903 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
907 static enum pc_bounds_kind
dwarf2_get_pc_bounds (struct die_info
*,
914 static void get_scope_pc_bounds (struct die_info
*,
915 unrelocated_addr
*, unrelocated_addr
*,
918 static void dwarf2_record_block_ranges (struct die_info
*, struct block
*,
921 static void dwarf2_add_field (struct field_info
*, struct die_info
*,
924 static void dwarf2_attach_fields_to_type (struct field_info
*,
925 struct type
*, struct dwarf2_cu
*);
927 static void dwarf2_add_member_fn (struct field_info
*,
928 struct die_info
*, struct type
*,
931 static void dwarf2_attach_fn_fields_to_type (struct field_info
*,
935 static void process_structure_scope (struct die_info
*, struct dwarf2_cu
*);
937 static void read_common_block (struct die_info
*, struct dwarf2_cu
*);
939 static void read_namespace (struct die_info
*die
, struct dwarf2_cu
*);
941 static void read_module (struct die_info
*die
, struct dwarf2_cu
*cu
);
943 static struct using_direct
**using_directives (struct dwarf2_cu
*cu
);
945 static void read_import_statement (struct die_info
*die
, struct dwarf2_cu
*);
947 static bool read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
);
949 static struct type
*read_module_type (struct die_info
*die
,
950 struct dwarf2_cu
*cu
);
952 static const char *namespace_name (struct die_info
*die
,
953 int *is_anonymous
, struct dwarf2_cu
*);
955 static void process_enumeration_scope (struct die_info
*, struct dwarf2_cu
*);
957 static bool decode_locdesc (struct dwarf_block
*, struct dwarf2_cu
*,
960 static enum dwarf_array_dim_ordering
read_array_order (struct die_info
*,
963 static struct die_info
*read_die_and_siblings_1
964 (const struct die_reader_specs
*, const gdb_byte
*, const gdb_byte
**,
967 static struct die_info
*read_die_and_siblings (const struct die_reader_specs
*,
968 const gdb_byte
*info_ptr
,
969 const gdb_byte
**new_info_ptr
,
970 struct die_info
*parent
);
972 static const gdb_byte
*read_full_die_1 (const struct die_reader_specs
*,
973 struct die_info
**, const gdb_byte
*,
976 static const gdb_byte
*read_toplevel_die (const struct die_reader_specs
*,
979 gdb::array_view
<attribute
*> = {});
981 static void process_die (struct die_info
*, struct dwarf2_cu
*);
983 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu
*,
986 static const char *dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*);
988 static const char *dwarf2_full_name (const char *name
,
989 struct die_info
*die
,
990 struct dwarf2_cu
*cu
);
992 static const char *dwarf2_physname (const char *name
, struct die_info
*die
,
993 struct dwarf2_cu
*cu
);
995 static struct die_info
*dwarf2_extension (struct die_info
*die
,
996 struct dwarf2_cu
**);
998 static void store_in_ref_table (struct die_info
*,
1001 static struct die_info
*follow_die_ref_or_sig (struct die_info
*,
1002 const struct attribute
*,
1003 struct dwarf2_cu
**);
1005 static struct die_info
*follow_die_ref (struct die_info
*,
1006 const struct attribute
*,
1007 struct dwarf2_cu
**);
1009 static struct die_info
*follow_die_sig (struct die_info
*,
1010 const struct attribute
*,
1011 struct dwarf2_cu
**);
1013 static struct type
*get_signatured_type (struct die_info
*, ULONGEST
,
1014 struct dwarf2_cu
*);
1016 static struct type
*get_DW_AT_signature_type (struct die_info
*,
1017 const struct attribute
*,
1018 struct dwarf2_cu
*);
1020 static void load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
1021 dwarf2_per_objfile
*per_objfile
);
1023 static void read_signatured_type (signatured_type
*sig_type
,
1024 dwarf2_per_objfile
*per_objfile
);
1026 static int attr_to_dynamic_prop (const struct attribute
*attr
,
1027 struct die_info
*die
, struct dwarf2_cu
*cu
,
1028 struct dynamic_prop
*prop
, struct type
*type
);
1030 /* memory allocation interface */
1032 static struct dwarf_block
*dwarf_alloc_block (struct dwarf2_cu
*);
1034 static void dwarf_decode_macros (struct dwarf2_cu
*, unsigned int, int);
1036 static void fill_in_loclist_baton (struct dwarf2_cu
*cu
,
1037 struct dwarf2_loclist_baton
*baton
,
1038 const struct attribute
*attr
);
1040 static void dwarf2_symbol_mark_computed (const struct attribute
*attr
,
1042 struct dwarf2_cu
*cu
,
1045 static const gdb_byte
*skip_one_die (const struct die_reader_specs
*reader
,
1046 const gdb_byte
*info_ptr
,
1047 const struct abbrev_info
*abbrev
,
1048 bool do_skip_children
= true);
1050 static struct dwarf2_per_cu_data
*dwarf2_find_containing_comp_unit
1051 (sect_offset sect_off
, unsigned int offset_in_dwz
,
1052 dwarf2_per_bfd
*per_bfd
);
1054 static void prepare_one_comp_unit (struct dwarf2_cu
*cu
,
1055 struct die_info
*comp_unit_die
,
1056 enum language pretend_language
);
1058 static struct type
*set_die_type (struct die_info
*, struct type
*,
1059 struct dwarf2_cu
*, bool = false);
1061 static void load_full_comp_unit (dwarf2_per_cu_data
*per_cu
,
1062 dwarf2_per_objfile
*per_objfile
,
1063 dwarf2_cu
*existing_cu
,
1065 enum language pretend_language
);
1067 static void process_full_comp_unit (dwarf2_cu
*cu
,
1068 enum language pretend_language
);
1070 static void process_full_type_unit (dwarf2_cu
*cu
,
1071 enum language pretend_language
);
1073 static struct type
*get_die_type_at_offset (sect_offset
,
1074 dwarf2_per_cu_data
*per_cu
,
1075 dwarf2_per_objfile
*per_objfile
);
1077 static struct type
*get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
);
1079 static void queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
1080 dwarf2_per_objfile
*per_objfile
,
1081 enum language pretend_language
);
1083 static void process_queue (dwarf2_per_objfile
*per_objfile
);
1085 static bool is_ada_import_or_export (dwarf2_cu
*cu
, const char *name
,
1086 const char *linkagename
);
1088 /* Class, the destructor of which frees all allocated queue entries. This
1089 will only have work to do if an error was thrown while processing the
1090 dwarf. If no error was thrown then the queue entries should have all
1091 been processed, and freed, as we went along. */
1093 class dwarf2_queue_guard
1096 explicit dwarf2_queue_guard (dwarf2_per_objfile
*per_objfile
)
1097 : m_per_objfile (per_objfile
)
1099 gdb_assert (!m_per_objfile
->queue
.has_value ());
1101 m_per_objfile
->queue
.emplace ();
1104 /* Free any entries remaining on the queue. There should only be
1105 entries left if we hit an error while processing the dwarf. */
1106 ~dwarf2_queue_guard ()
1108 gdb_assert (m_per_objfile
->queue
.has_value ());
1110 m_per_objfile
->queue
.reset ();
1113 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard
);
1116 dwarf2_per_objfile
*m_per_objfile
;
1119 dwarf2_queue_item::~dwarf2_queue_item ()
1121 /* Anything still marked queued is likely to be in an
1122 inconsistent state, so discard it. */
1125 per_objfile
->remove_cu (per_cu
);
1130 /* See dwarf2/read.h. */
1133 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data
*data
)
1135 if (data
->is_debug_types
)
1136 delete static_cast<signatured_type
*> (data
);
1141 static file_and_directory
&find_file_and_directory
1142 (struct die_info
*die
, struct dwarf2_cu
*cu
);
1144 static const char *compute_include_file_name
1145 (const struct line_header
*lh
,
1146 const file_entry
&fe
,
1147 const file_and_directory
&cu_info
,
1148 std::string
&name_holder
);
1150 static htab_up
allocate_dwo_unit_table ();
1152 static struct dwo_unit
*lookup_dwo_unit_in_dwp
1153 (dwarf2_per_objfile
*per_objfile
, struct dwp_file
*dwp_file
,
1154 const char *comp_dir
, ULONGEST signature
, int is_debug_types
);
1156 static struct dwp_file
*get_dwp_file (dwarf2_per_objfile
*per_objfile
);
1158 static struct dwo_unit
*lookup_dwo_comp_unit
1159 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
1160 ULONGEST signature
);
1162 static struct dwo_unit
*lookup_dwo_type_unit
1163 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
);
1165 static void queue_and_load_all_dwo_tus (dwarf2_cu
*cu
);
1167 /* A unique pointer to a dwo_file. */
1169 typedef std::unique_ptr
<struct dwo_file
> dwo_file_up
;
1171 static void process_cu_includes (dwarf2_per_objfile
*per_objfile
);
1173 static void check_producer (struct dwarf2_cu
*cu
);
1175 /* Various complaints about symbol reading that don't abort the process. */
1178 dwarf2_debug_line_missing_file_complaint (void)
1180 complaint (_(".debug_line section has line data without a file"));
1184 dwarf2_debug_line_missing_end_sequence_complaint (void)
1186 complaint (_(".debug_line section has line "
1187 "program sequence without an end"));
1191 dwarf2_complex_location_expr_complaint (void)
1193 complaint (_("location expression too complex"));
1197 dwarf2_const_value_length_mismatch_complaint (const char *arg1
, int arg2
,
1200 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1205 dwarf2_invalid_attrib_class_complaint (const char *arg1
, const char *arg2
)
1207 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1214 dwarf2_per_objfile::relocate (unrelocated_addr addr
)
1216 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
1217 CORE_ADDR tem
= (CORE_ADDR
) addr
+ baseaddr
;
1218 return gdbarch_adjust_dwarf2_addr (objfile
->arch (), tem
);
1221 /* Hash function for line_header_hash. */
1224 line_header_hash (const struct line_header
*ofs
)
1226 return to_underlying (ofs
->sect_off
) ^ ofs
->offset_in_dwz
;
1229 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1232 line_header_hash_voidp (const void *item
)
1234 const struct line_header
*ofs
= (const struct line_header
*) item
;
1236 return line_header_hash (ofs
);
1239 /* Equality function for line_header_hash. */
1242 line_header_eq_voidp (const void *item_lhs
, const void *item_rhs
)
1244 const struct line_header
*ofs_lhs
= (const struct line_header
*) item_lhs
;
1245 const struct line_header
*ofs_rhs
= (const struct line_header
*) item_rhs
;
1247 return (ofs_lhs
->sect_off
== ofs_rhs
->sect_off
1248 && ofs_lhs
->offset_in_dwz
== ofs_rhs
->offset_in_dwz
);
1251 /* See declaration. */
1253 dwarf2_per_bfd::dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
,
1256 can_copy (can_copy_
)
1259 names
= &dwarf2_elf_names
;
1261 for (asection
*sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
1262 locate_sections (obfd
, sec
, *names
);
1265 dwarf2_per_bfd::~dwarf2_per_bfd ()
1267 /* Data from the per-BFD may be needed when finalizing the cooked
1268 index table, so wait here while this happens. */
1269 if (index_table
!= nullptr)
1270 index_table
->wait_completely ();
1272 for (auto &per_cu
: all_units
)
1273 per_cu
->free_cached_file_names ();
1275 /* Everything else should be on this->obstack. */
1281 dwarf2_per_objfile::remove_all_cus ()
1283 gdb_assert (!queue
.has_value ());
1285 m_dwarf2_cus
.clear ();
1288 /* A helper class that calls free_cached_comp_units on
1291 class free_cached_comp_units
1295 explicit free_cached_comp_units (dwarf2_per_objfile
*per_objfile
)
1296 : m_per_objfile (per_objfile
)
1300 ~free_cached_comp_units ()
1302 m_per_objfile
->remove_all_cus ();
1305 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units
);
1309 dwarf2_per_objfile
*m_per_objfile
;
1315 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const
1317 if (per_cu
->index
< this->m_symtabs
.size ())
1318 return this->m_symtabs
[per_cu
->index
] != nullptr;
1325 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data
*per_cu
) const
1327 if (per_cu
->index
< this->m_symtabs
.size ())
1328 return this->m_symtabs
[per_cu
->index
];
1335 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data
*per_cu
,
1336 compunit_symtab
*symtab
)
1338 if (per_cu
->index
>= this->m_symtabs
.size ())
1339 this->m_symtabs
.resize (per_cu
->index
+ 1);
1340 gdb_assert (this->m_symtabs
[per_cu
->index
] == nullptr);
1341 this->m_symtabs
[per_cu
->index
] = symtab
;
1344 /* Helper function for dwarf2_initialize_objfile that creates the
1348 dwarf2_has_info (struct objfile
*objfile
,
1349 const struct dwarf2_debug_sections
*names
,
1352 if (objfile
->flags
& OBJF_READNEVER
)
1355 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1356 bool just_created
= false;
1358 if (per_objfile
== NULL
)
1360 dwarf2_per_bfd
*per_bfd
;
1362 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1363 BFD doesn't require relocations.
1365 We don't share with objfiles for which -readnow was requested,
1366 because it would complicate things when loading the same BFD with
1367 -readnow and then without -readnow. */
1368 if (!gdb_bfd_requires_relocations (objfile
->obfd
.get ())
1369 && (objfile
->flags
& OBJF_READNOW
) == 0)
1371 /* See if one has been created for this BFD yet. */
1372 per_bfd
= dwarf2_per_bfd_bfd_data_key
.get (objfile
->obfd
.get ());
1374 if (per_bfd
== nullptr)
1376 /* No, create it now. */
1377 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
.get (), names
,
1379 dwarf2_per_bfd_bfd_data_key
.set (objfile
->obfd
.get (), per_bfd
);
1380 just_created
= true;
1385 /* No sharing possible, create one specifically for this objfile. */
1386 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
.get (), names
, can_copy
);
1387 dwarf2_per_bfd_objfile_data_key
.set (objfile
, per_bfd
);
1388 just_created
= true;
1391 per_objfile
= dwarf2_objfile_data_key
.emplace (objfile
, objfile
, per_bfd
);
1394 const bool has_info
= (!per_objfile
->per_bfd
->info
.is_virtual
1395 && per_objfile
->per_bfd
->info
.s
.section
!= nullptr
1396 && !per_objfile
->per_bfd
->abbrev
.is_virtual
1397 && per_objfile
->per_bfd
->abbrev
.s
.section
!= nullptr);
1399 if (just_created
&& has_info
)
1401 /* Try to fetch any potential dwz file early, while still on
1402 the main thread. Also, be sure to do it just once per
1403 BFD, to avoid races. */
1406 dwarf2_read_dwz_file (per_objfile
);
1408 catch (const gdb_exception_error
&err
)
1410 warning (_("%s"), err
.what ());
1417 /* See declaration. */
1420 dwarf2_per_bfd::locate_sections (bfd
*abfd
, asection
*sectp
,
1421 const dwarf2_debug_sections
&names
)
1423 flagword aflag
= bfd_section_flags (sectp
);
1425 if ((aflag
& SEC_HAS_CONTENTS
) == 0)
1428 else if (elf_section_data (sectp
)->this_hdr
.sh_size
1429 > bfd_get_file_size (abfd
))
1431 bfd_size_type size
= elf_section_data (sectp
)->this_hdr
.sh_size
;
1432 warning (_("Discarding section %s which has a section size (%s"
1433 ") larger than the file size [in module %s]"),
1434 bfd_section_name (sectp
), phex_nz (size
, sizeof (size
)),
1435 bfd_get_filename (abfd
));
1437 else if (names
.info
.matches (sectp
->name
))
1439 this->info
.s
.section
= sectp
;
1440 this->info
.size
= bfd_section_size (sectp
);
1442 else if (names
.abbrev
.matches (sectp
->name
))
1444 this->abbrev
.s
.section
= sectp
;
1445 this->abbrev
.size
= bfd_section_size (sectp
);
1447 else if (names
.line
.matches (sectp
->name
))
1449 this->line
.s
.section
= sectp
;
1450 this->line
.size
= bfd_section_size (sectp
);
1452 else if (names
.loc
.matches (sectp
->name
))
1454 this->loc
.s
.section
= sectp
;
1455 this->loc
.size
= bfd_section_size (sectp
);
1457 else if (names
.loclists
.matches (sectp
->name
))
1459 this->loclists
.s
.section
= sectp
;
1460 this->loclists
.size
= bfd_section_size (sectp
);
1462 else if (names
.macinfo
.matches (sectp
->name
))
1464 this->macinfo
.s
.section
= sectp
;
1465 this->macinfo
.size
= bfd_section_size (sectp
);
1467 else if (names
.macro
.matches (sectp
->name
))
1469 this->macro
.s
.section
= sectp
;
1470 this->macro
.size
= bfd_section_size (sectp
);
1472 else if (names
.str
.matches (sectp
->name
))
1474 this->str
.s
.section
= sectp
;
1475 this->str
.size
= bfd_section_size (sectp
);
1477 else if (names
.str_offsets
.matches (sectp
->name
))
1479 this->str_offsets
.s
.section
= sectp
;
1480 this->str_offsets
.size
= bfd_section_size (sectp
);
1482 else if (names
.line_str
.matches (sectp
->name
))
1484 this->line_str
.s
.section
= sectp
;
1485 this->line_str
.size
= bfd_section_size (sectp
);
1487 else if (names
.addr
.matches (sectp
->name
))
1489 this->addr
.s
.section
= sectp
;
1490 this->addr
.size
= bfd_section_size (sectp
);
1492 else if (names
.frame
.matches (sectp
->name
))
1494 this->frame
.s
.section
= sectp
;
1495 this->frame
.size
= bfd_section_size (sectp
);
1497 else if (names
.eh_frame
.matches (sectp
->name
))
1499 this->eh_frame
.s
.section
= sectp
;
1500 this->eh_frame
.size
= bfd_section_size (sectp
);
1502 else if (names
.ranges
.matches (sectp
->name
))
1504 this->ranges
.s
.section
= sectp
;
1505 this->ranges
.size
= bfd_section_size (sectp
);
1507 else if (names
.rnglists
.matches (sectp
->name
))
1509 this->rnglists
.s
.section
= sectp
;
1510 this->rnglists
.size
= bfd_section_size (sectp
);
1512 else if (names
.types
.matches (sectp
->name
))
1514 struct dwarf2_section_info type_section
;
1516 memset (&type_section
, 0, sizeof (type_section
));
1517 type_section
.s
.section
= sectp
;
1518 type_section
.size
= bfd_section_size (sectp
);
1520 this->types
.push_back (type_section
);
1522 else if (names
.gdb_index
.matches (sectp
->name
))
1524 this->gdb_index
.s
.section
= sectp
;
1525 this->gdb_index
.size
= bfd_section_size (sectp
);
1527 else if (names
.debug_names
.matches (sectp
->name
))
1529 this->debug_names
.s
.section
= sectp
;
1530 this->debug_names
.size
= bfd_section_size (sectp
);
1532 else if (names
.debug_aranges
.matches (sectp
->name
))
1534 this->debug_aranges
.s
.section
= sectp
;
1535 this->debug_aranges
.size
= bfd_section_size (sectp
);
1538 if ((bfd_section_flags (sectp
) & (SEC_LOAD
| SEC_ALLOC
))
1539 && bfd_section_vma (sectp
) == 0)
1540 this->has_section_at_zero
= true;
1543 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1547 dwarf2_get_section_info (struct objfile
*objfile
,
1548 enum dwarf2_section_enum sect
,
1549 asection
**sectp
, const gdb_byte
**bufp
,
1550 bfd_size_type
*sizep
)
1552 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1553 struct dwarf2_section_info
*info
;
1555 /* We may see an objfile without any DWARF, in which case we just
1557 if (per_objfile
== NULL
)
1566 case DWARF2_DEBUG_FRAME
:
1567 info
= &per_objfile
->per_bfd
->frame
;
1569 case DWARF2_EH_FRAME
:
1570 info
= &per_objfile
->per_bfd
->eh_frame
;
1573 gdb_assert_not_reached ("unexpected section");
1576 info
->read (objfile
);
1578 *sectp
= info
->get_bfd_section ();
1579 *bufp
= info
->buffer
;
1580 *sizep
= info
->size
;
1583 /* See dwarf2/read.h. */
1586 dwarf2_per_bfd::map_info_sections (struct objfile
*objfile
)
1588 info
.read (objfile
);
1589 abbrev
.read (objfile
);
1590 line
.read (objfile
);
1592 str_offsets
.read (objfile
);
1593 line_str
.read (objfile
);
1594 ranges
.read (objfile
);
1595 rnglists
.read (objfile
);
1596 addr
.read (objfile
);
1597 debug_aranges
.read (objfile
);
1599 for (auto §ion
: types
)
1600 section
.read (objfile
);
1604 /* DWARF quick_symbol_functions support. */
1606 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1607 unique line tables, so we maintain a separate table of all .debug_line
1608 derived entries to support the sharing.
1609 All the quick functions need is the list of file names. We discard the
1610 line_header when we're done and don't need to record it here. */
1611 struct quick_file_names
1613 /* The data used to construct the hash key. */
1614 struct stmt_list_hash hash
;
1616 /* The number of entries in file_names, real_names. */
1617 unsigned int num_file_names
;
1619 /* The CU directory, as given by DW_AT_comp_dir. May be
1621 const char *comp_dir
;
1623 /* The file names from the line table, after being run through
1625 const char **file_names
;
1627 /* The file names from the line table after being run through
1628 gdb_realpath. These are computed lazily. */
1629 const char **real_names
;
1632 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1633 It's handy in this case to have an empty implementation of the
1634 quick symbol functions, to avoid special cases in the rest of the
1637 struct readnow_functions
: public dwarf2_base_index_functions
1639 void dump (struct objfile
*objfile
) override
1643 bool expand_symtabs_matching
1644 (struct objfile
*objfile
,
1645 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1646 const lookup_name_info
*lookup_name
,
1647 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1648 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1649 block_search_flags search_flags
,
1650 domain_search_flags domain
) override
1656 /* Utility hash function for a stmt_list_hash. */
1659 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1663 if (stmt_list_hash
->dwo_unit
!= NULL
)
1664 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1665 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1669 /* Utility equality function for a stmt_list_hash. */
1672 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1673 const struct stmt_list_hash
*rhs
)
1675 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1677 if (lhs
->dwo_unit
!= NULL
1678 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1681 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1684 /* Hash function for a quick_file_names. */
1687 hash_file_name_entry (const void *e
)
1689 const struct quick_file_names
*file_data
1690 = (const struct quick_file_names
*) e
;
1692 return hash_stmt_list_entry (&file_data
->hash
);
1695 /* Equality function for a quick_file_names. */
1698 eq_file_name_entry (const void *a
, const void *b
)
1700 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
1701 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
1703 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
1709 create_quick_file_names_table (unsigned int nr_initial_entries
)
1711 return htab_up (htab_create_alloc (nr_initial_entries
,
1712 hash_file_name_entry
, eq_file_name_entry
,
1713 nullptr, xcalloc
, xfree
));
1716 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
1717 function is unrelated to symtabs, symtab would have to be created afterwards.
1718 You should call age_cached_comp_units after processing the CU. */
1721 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
1724 if (per_cu
->is_debug_types
)
1725 load_full_type_unit (per_cu
, per_objfile
);
1727 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
1728 skip_partial
, language_minimal
);
1730 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
1732 return nullptr; /* Dummy CU. */
1734 dwarf2_find_base_address (cu
->dies
, cu
);
1739 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
1742 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
1743 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
1746 /* The destructor of dwarf2_queue_guard frees any entries left on
1747 the queue. After this point we're guaranteed to leave this function
1748 with the dwarf queue empty. */
1749 dwarf2_queue_guard
q_guard (per_objfile
);
1751 if (!per_objfile
->symtab_set_p (per_cu
))
1753 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
1754 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
1756 /* If we just loaded a CU from a DWO, and we're working with an index
1757 that may badly handle TUs, load all the TUs in that DWO as well.
1758 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
1759 if (!per_cu
->is_debug_types
1761 && cu
->dwo_unit
!= NULL
1762 && per_objfile
->per_bfd
->index_table
!= NULL
1763 && !per_objfile
->per_bfd
->index_table
->version_check ()
1764 /* DWP files aren't supported yet. */
1765 && get_dwp_file (per_objfile
) == NULL
)
1766 queue_and_load_all_dwo_tus (cu
);
1769 process_queue (per_objfile
);
1772 /* Age the cache, releasing compilation units that have not
1773 been used recently. */
1774 per_objfile
->age_comp_units ();
1777 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
1778 the per-objfile for which this symtab is instantiated.
1780 Returns the resulting symbol table. */
1782 static struct compunit_symtab
*
1783 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
1784 dwarf2_per_objfile
*per_objfile
,
1787 if (!per_objfile
->symtab_set_p (per_cu
))
1789 free_cached_comp_units
freer (per_objfile
);
1790 scoped_restore decrementer
= increment_reading_symtab ();
1791 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
1792 process_cu_includes (per_objfile
);
1795 return per_objfile
->get_symtab (per_cu
);
1800 dwarf2_per_cu_data_up
1801 dwarf2_per_bfd::allocate_per_cu ()
1803 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
1804 result
->per_bfd
= this;
1805 result
->index
= all_units
.size ();
1812 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
1814 signatured_type_up
result (new signatured_type (signature
));
1815 result
->per_bfd
= this;
1816 result
->index
= all_units
.size ();
1817 result
->is_debug_types
= true;
1824 dwarf2_per_cu_data_up
1825 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
1826 struct dwarf2_section_info
*section
,
1828 sect_offset sect_off
, ULONGEST length
)
1830 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
1831 the_cu
->sect_off
= sect_off
;
1832 the_cu
->set_length (length
);
1833 the_cu
->section
= section
;
1834 the_cu
->is_dwz
= is_dwz
;
1838 /* die_reader_func for dw2_get_file_names. */
1841 dw2_get_file_names_reader (const struct die_reader_specs
*reader
,
1842 struct die_info
*comp_unit_die
)
1844 struct dwarf2_cu
*cu
= reader
->cu
;
1845 struct dwarf2_per_cu_data
*this_cu
= cu
->per_cu
;
1846 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
1847 struct dwarf2_per_cu_data
*lh_cu
;
1848 struct attribute
*attr
;
1850 struct quick_file_names
*qfn
;
1852 gdb_assert (! this_cu
->is_debug_types
);
1854 this_cu
->files_read
= true;
1855 /* Our callers never want to match partial units -- instead they
1856 will match the enclosing full CU. */
1857 if (comp_unit_die
->tag
== DW_TAG_partial_unit
)
1864 sect_offset line_offset
{};
1866 file_and_directory
&fnd
= find_file_and_directory (comp_unit_die
, cu
);
1868 attr
= dwarf2_attr (comp_unit_die
, DW_AT_stmt_list
, cu
);
1869 if (attr
!= nullptr && attr
->form_is_unsigned ())
1871 struct quick_file_names find_entry
;
1873 line_offset
= (sect_offset
) attr
->as_unsigned ();
1875 /* We may have already read in this line header (TU line header sharing).
1876 If we have we're done. */
1877 find_entry
.hash
.dwo_unit
= cu
->dwo_unit
;
1878 find_entry
.hash
.line_sect_off
= line_offset
;
1879 slot
= htab_find_slot (per_objfile
->per_bfd
->quick_file_names_table
.get (),
1880 &find_entry
, INSERT
);
1883 lh_cu
->file_names
= (struct quick_file_names
*) *slot
;
1887 lh
= dwarf_decode_line_header (line_offset
, cu
, fnd
.get_comp_dir ());
1891 if (!fnd
.is_unknown ())
1893 else if (lh
== nullptr)
1896 qfn
= XOBNEW (&per_objfile
->per_bfd
->obstack
, struct quick_file_names
);
1897 qfn
->hash
.dwo_unit
= cu
->dwo_unit
;
1898 qfn
->hash
.line_sect_off
= line_offset
;
1899 /* There may not be a DW_AT_stmt_list. */
1900 if (slot
!= nullptr)
1903 std::vector
<const char *> include_names
;
1906 for (const auto &entry
: lh
->file_names ())
1908 std::string name_holder
;
1909 const char *include_name
=
1910 compute_include_file_name (lh
.get (), entry
, fnd
, name_holder
);
1911 if (include_name
!= nullptr)
1913 include_name
= per_objfile
->objfile
->intern (include_name
);
1914 include_names
.push_back (include_name
);
1919 qfn
->num_file_names
= offset
+ include_names
.size ();
1920 qfn
->comp_dir
= fnd
.intern_comp_dir (per_objfile
->objfile
);
1922 XOBNEWVEC (&per_objfile
->per_bfd
->obstack
, const char *,
1923 qfn
->num_file_names
);
1925 qfn
->file_names
[0] = per_objfile
->objfile
->intern (fnd
.get_name ());
1927 if (!include_names
.empty ())
1928 memcpy (&qfn
->file_names
[offset
], include_names
.data (),
1929 include_names
.size () * sizeof (const char *));
1931 qfn
->real_names
= NULL
;
1933 lh_cu
->file_names
= qfn
;
1936 /* A helper for the "quick" functions which attempts to read the line
1937 table for THIS_CU. */
1939 static struct quick_file_names
*
1940 dw2_get_file_names (dwarf2_per_cu_data
*this_cu
,
1941 dwarf2_per_objfile
*per_objfile
)
1943 /* This should never be called for TUs. */
1944 gdb_assert (! this_cu
->is_debug_types
);
1946 if (this_cu
->files_read
)
1947 return this_cu
->file_names
;
1949 cutu_reader
reader (this_cu
, per_objfile
);
1950 if (!reader
.dummy_p
)
1951 dw2_get_file_names_reader (&reader
, reader
.comp_unit_die
);
1953 return this_cu
->file_names
;
1956 /* A helper for the "quick" functions which computes and caches the
1957 real path for a given file name from the line table. */
1960 dw2_get_real_path (dwarf2_per_objfile
*per_objfile
,
1961 struct quick_file_names
*qfn
, int index
)
1963 if (qfn
->real_names
== NULL
)
1964 qfn
->real_names
= OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
1965 qfn
->num_file_names
, const char *);
1967 if (qfn
->real_names
[index
] == NULL
)
1969 const char *dirname
= nullptr;
1971 if (!IS_ABSOLUTE_PATH (qfn
->file_names
[index
]))
1972 dirname
= qfn
->comp_dir
;
1974 gdb::unique_xmalloc_ptr
<char> fullname
;
1975 fullname
= find_source_or_rewrite (qfn
->file_names
[index
], dirname
);
1977 qfn
->real_names
[index
] = fullname
.release ();
1980 return qfn
->real_names
[index
];
1984 dwarf2_base_index_functions::find_last_source_symtab (struct objfile
*objfile
)
1986 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1987 dwarf2_per_cu_data
*dwarf_cu
1988 = per_objfile
->per_bfd
->all_units
.back ().get ();
1989 compunit_symtab
*cust
= dw2_instantiate_symtab (dwarf_cu
, per_objfile
, false);
1994 return cust
->primary_filetab ();
2000 dwarf2_per_cu_data::free_cached_file_names ()
2003 fnd
->forget_fullname ();
2005 if (per_bfd
== nullptr)
2008 struct quick_file_names
*file_data
= file_names
;
2009 if (file_data
!= nullptr && file_data
->real_names
!= nullptr)
2011 for (int i
= 0; i
< file_data
->num_file_names
; ++i
)
2013 xfree ((void *) file_data
->real_names
[i
]);
2014 file_data
->real_names
[i
] = nullptr;
2020 dwarf2_base_index_functions::forget_cached_source_info
2021 (struct objfile
*objfile
)
2023 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2025 for (auto &per_cu
: per_objfile
->per_bfd
->all_units
)
2026 per_cu
->free_cached_file_names ();
2030 dwarf2_base_index_functions::print_stats (struct objfile
*objfile
,
2036 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2037 int total
= per_objfile
->per_bfd
->all_units
.size ();
2040 for (int i
= 0; i
< total
; ++i
)
2042 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
2044 if (!per_objfile
->symtab_set_p (per_cu
))
2047 gdb_printf (_(" Number of read CUs: %d\n"), total
- count
);
2048 gdb_printf (_(" Number of unread CUs: %d\n"), count
);
2052 dwarf2_base_index_functions::expand_all_symtabs (struct objfile
*objfile
)
2054 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2055 int total_units
= per_objfile
->per_bfd
->all_units
.size ();
2057 for (int i
= 0; i
< total_units
; ++i
)
2059 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
2061 /* We don't want to directly expand a partial CU, because if we
2062 read it with the wrong language, then assertion failures can
2063 be triggered later on. See PR symtab/23010. So, tell
2064 dw2_instantiate_symtab to skip partial CUs -- any important
2065 partial CU will be read via DW_TAG_imported_unit anyway. */
2066 dw2_instantiate_symtab (per_cu
, per_objfile
, true);
2071 /* Starting from a search name, return the string that finds the upper
2072 bound of all strings that start with SEARCH_NAME in a sorted name
2073 list. Returns the empty string to indicate that the upper bound is
2074 the end of the list. */
2077 make_sort_after_prefix_name (const char *search_name
)
2079 /* When looking to complete "func", we find the upper bound of all
2080 symbols that start with "func" by looking for where we'd insert
2081 the closest string that would follow "func" in lexicographical
2082 order. Usually, that's "func"-with-last-character-incremented,
2083 i.e. "fund". Mind non-ASCII characters, though. Usually those
2084 will be UTF-8 multi-byte sequences, but we can't be certain.
2085 Especially mind the 0xff character, which is a valid character in
2086 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
2087 rule out compilers allowing it in identifiers. Note that
2088 conveniently, strcmp/strcasecmp are specified to compare
2089 characters interpreted as unsigned char. So what we do is treat
2090 the whole string as a base 256 number composed of a sequence of
2091 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
2092 to 0, and carries 1 to the following more-significant position.
2093 If the very first character in SEARCH_NAME ends up incremented
2094 and carries/overflows, then the upper bound is the end of the
2095 list. The string after the empty string is also the empty
2098 Some examples of this operation:
2100 SEARCH_NAME => "+1" RESULT
2104 "\xff" "a" "\xff" => "\xff" "b"
2109 Then, with these symbols for example:
2115 completing "func" looks for symbols between "func" and
2116 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
2117 which finds "func" and "func1", but not "fund".
2121 funcÿ (Latin1 'ÿ' [0xff])
2125 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
2126 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
2130 ÿÿ (Latin1 'ÿ' [0xff])
2133 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
2134 the end of the list.
2136 std::string after
= search_name
;
2137 while (!after
.empty () && (unsigned char) after
.back () == 0xff)
2139 if (!after
.empty ())
2140 after
.back () = (unsigned char) after
.back () + 1;
2144 /* See declaration. */
2146 std::pair
<std::vector
<name_component
>::const_iterator
,
2147 std::vector
<name_component
>::const_iterator
>
2148 mapped_index_base::find_name_components_bounds
2149 (const lookup_name_info
&lookup_name_without_params
, language lang
,
2150 dwarf2_per_objfile
*per_objfile
) const
2153 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
2155 const char *lang_name
2156 = lookup_name_without_params
.language_lookup_name (lang
);
2158 /* Comparison function object for lower_bound that matches against a
2159 given symbol name. */
2160 auto lookup_compare_lower
= [&] (const name_component
&elem
,
2163 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
2164 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
2165 return name_cmp (elem_name
, name
) < 0;
2168 /* Comparison function object for upper_bound that matches against a
2169 given symbol name. */
2170 auto lookup_compare_upper
= [&] (const char *name
,
2171 const name_component
&elem
)
2173 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
2174 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
2175 return name_cmp (name
, elem_name
) < 0;
2178 auto begin
= this->name_components
.begin ();
2179 auto end
= this->name_components
.end ();
2181 /* Find the lower bound. */
2184 if (lookup_name_without_params
.completion_mode () && lang_name
[0] == '\0')
2187 return std::lower_bound (begin
, end
, lang_name
, lookup_compare_lower
);
2190 /* Find the upper bound. */
2193 if (lookup_name_without_params
.completion_mode ())
2195 /* In completion mode, we want UPPER to point past all
2196 symbols names that have the same prefix. I.e., with
2197 these symbols, and completing "func":
2199 function << lower bound
2201 other_function << upper bound
2203 We find the upper bound by looking for the insertion
2204 point of "func"-with-last-character-incremented,
2206 std::string after
= make_sort_after_prefix_name (lang_name
);
2209 return std::lower_bound (lower
, end
, after
.c_str (),
2210 lookup_compare_lower
);
2213 return std::upper_bound (lower
, end
, lang_name
, lookup_compare_upper
);
2216 return {lower
, upper
};
2219 /* See declaration. */
2222 mapped_index_base::build_name_components (dwarf2_per_objfile
*per_objfile
)
2224 if (!this->name_components
.empty ())
2227 this->name_components_casing
= case_sensitivity
;
2229 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
2231 /* The code below only knows how to break apart components of C++
2232 symbol names (and other languages that use '::' as
2233 namespace/module separator) and Ada symbol names. */
2234 auto count
= this->symbol_name_count ();
2235 for (offset_type idx
= 0; idx
< count
; idx
++)
2237 if (this->symbol_name_slot_invalid (idx
))
2240 const char *name
= this->symbol_name_at (idx
, per_objfile
);
2242 /* Add each name component to the name component table. */
2243 unsigned int previous_len
= 0;
2245 if (strstr (name
, "::") != nullptr)
2247 for (unsigned int current_len
= cp_find_first_component (name
);
2248 name
[current_len
] != '\0';
2249 current_len
+= cp_find_first_component (name
+ current_len
))
2251 gdb_assert (name
[current_len
] == ':');
2252 this->name_components
.push_back ({previous_len
, idx
});
2253 /* Skip the '::'. */
2255 previous_len
= current_len
;
2260 /* Handle the Ada encoded (aka mangled) form here. */
2261 for (const char *iter
= strstr (name
, "__");
2263 iter
= strstr (iter
, "__"))
2265 this->name_components
.push_back ({previous_len
, idx
});
2267 previous_len
= iter
- name
;
2271 this->name_components
.push_back ({previous_len
, idx
});
2274 /* Sort name_components elements by name. */
2275 auto name_comp_compare
= [&] (const name_component
&left
,
2276 const name_component
&right
)
2278 const char *left_qualified
2279 = this->symbol_name_at (left
.idx
, per_objfile
);
2280 const char *right_qualified
2281 = this->symbol_name_at (right
.idx
, per_objfile
);
2283 const char *left_name
= left_qualified
+ left
.name_offset
;
2284 const char *right_name
= right_qualified
+ right
.name_offset
;
2286 return name_cmp (left_name
, right_name
) < 0;
2289 std::sort (this->name_components
.begin (),
2290 this->name_components
.end (),
2297 dw2_expand_symtabs_matching_symbol
2298 (mapped_index_base
&index
,
2299 const lookup_name_info
&lookup_name_in
,
2300 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
2301 gdb::function_view
<bool (offset_type
)> match_callback
,
2302 dwarf2_per_objfile
*per_objfile
)
2304 lookup_name_info lookup_name_without_params
2305 = lookup_name_in
.make_ignore_params ();
2307 /* Build the symbol name component sorted vector, if we haven't
2309 index
.build_name_components (per_objfile
);
2311 /* The same symbol may appear more than once in the range though.
2312 E.g., if we're looking for symbols that complete "w", and we have
2313 a symbol named "w1::w2", we'll find the two name components for
2314 that same symbol in the range. To be sure we only call the
2315 callback once per symbol, we first collect the symbol name
2316 indexes that matched in a temporary vector and ignore
2318 std::vector
<offset_type
> matches
;
2320 struct name_and_matcher
2322 symbol_name_matcher_ftype
*matcher
;
2325 bool operator== (const name_and_matcher
&other
) const
2327 return matcher
== other
.matcher
&& strcmp (name
, other
.name
) == 0;
2331 /* A vector holding all the different symbol name matchers, for all
2333 std::vector
<name_and_matcher
> matchers
;
2335 for (int i
= 0; i
< nr_languages
; i
++)
2337 enum language lang_e
= (enum language
) i
;
2339 const language_defn
*lang
= language_def (lang_e
);
2340 symbol_name_matcher_ftype
*name_matcher
2341 = lang
->get_symbol_name_matcher (lookup_name_without_params
);
2343 name_and_matcher key
{
2345 lookup_name_without_params
.language_lookup_name (lang_e
)
2348 /* Don't insert the same comparison routine more than once.
2349 Note that we do this linear walk. This is not a problem in
2350 practice because the number of supported languages is
2352 if (std::find (matchers
.begin (), matchers
.end (), key
)
2355 matchers
.push_back (std::move (key
));
2358 = index
.find_name_components_bounds (lookup_name_without_params
,
2359 lang_e
, per_objfile
);
2361 /* Now for each symbol name in range, check to see if we have a name
2362 match, and if so, call the MATCH_CALLBACK callback. */
2364 for (; bounds
.first
!= bounds
.second
; ++bounds
.first
)
2366 const char *qualified
2367 = index
.symbol_name_at (bounds
.first
->idx
, per_objfile
);
2369 if (!name_matcher (qualified
, lookup_name_without_params
, NULL
)
2370 || (symbol_matcher
!= NULL
&& !symbol_matcher (qualified
)))
2373 matches
.push_back (bounds
.first
->idx
);
2377 std::sort (matches
.begin (), matches
.end ());
2379 /* Finally call the callback, once per match. */
2382 for (offset_type idx
: matches
)
2386 if (!match_callback (idx
))
2395 /* Above we use a type wider than idx's for 'prev', since 0 and
2396 (offset_type)-1 are both possible values. */
2397 static_assert (sizeof (prev
) > sizeof (offset_type
), "");
2404 namespace selftests
{ namespace dw2_expand_symtabs_matching
{
2406 /* A mock .gdb_index/.debug_names-like name index table, enough to
2407 exercise dw2_expand_symtabs_matching_symbol, which works with the
2408 mapped_index_base interface. Builds an index from the symbol list
2409 passed as parameter to the constructor. */
2410 class mock_mapped_index
: public mapped_index_base
2413 mock_mapped_index (gdb::array_view
<const char *> symbols
)
2414 : m_symbol_table (symbols
)
2417 DISABLE_COPY_AND_ASSIGN (mock_mapped_index
);
2419 /* Return the number of names in the symbol table. */
2420 size_t symbol_name_count () const override
2422 return m_symbol_table
.size ();
2425 /* Get the name of the symbol at IDX in the symbol table. */
2426 const char *symbol_name_at
2427 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
2429 return m_symbol_table
[idx
];
2432 quick_symbol_functions_up
make_quick_functions () const override
2438 gdb::array_view
<const char *> m_symbol_table
;
2441 /* Convenience function that converts a NULL pointer to a "<null>"
2442 string, to pass to print routines. */
2445 string_or_null (const char *str
)
2447 return str
!= NULL
? str
: "<null>";
2450 /* Check if a lookup_name_info built from
2451 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
2452 index. EXPECTED_LIST is the list of expected matches, in expected
2453 matching order. If no match expected, then an empty list is
2454 specified. Returns true on success. On failure prints a warning
2455 indicating the file:line that failed, and returns false. */
2458 check_match (const char *file
, int line
,
2459 mock_mapped_index
&mock_index
,
2460 const char *name
, symbol_name_match_type match_type
,
2461 bool completion_mode
,
2462 std::initializer_list
<const char *> expected_list
,
2463 dwarf2_per_objfile
*per_objfile
)
2465 lookup_name_info
lookup_name (name
, match_type
, completion_mode
);
2467 bool matched
= true;
2469 auto mismatch
= [&] (const char *expected_str
,
2472 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
2473 "expected=\"%s\", got=\"%s\"\n"),
2475 (match_type
== symbol_name_match_type::FULL
2477 name
, string_or_null (expected_str
), string_or_null (got
));
2481 auto expected_it
= expected_list
.begin ();
2482 auto expected_end
= expected_list
.end ();
2484 dw2_expand_symtabs_matching_symbol (mock_index
, lookup_name
,
2486 [&] (offset_type idx
)
2488 const char *matched_name
= mock_index
.symbol_name_at (idx
, per_objfile
);
2489 const char *expected_str
2490 = expected_it
== expected_end
? NULL
: *expected_it
++;
2492 if (expected_str
== NULL
|| strcmp (expected_str
, matched_name
) != 0)
2493 mismatch (expected_str
, matched_name
);
2497 const char *expected_str
2498 = expected_it
== expected_end
? NULL
: *expected_it
++;
2499 if (expected_str
!= NULL
)
2500 mismatch (expected_str
, NULL
);
2505 /* The symbols added to the mock mapped_index for testing (in
2507 static const char *test_symbols
[] = {
2516 "ns2::tmpl<int>::foo2",
2517 "(anonymous namespace)::A::B::C",
2519 /* These are used to check that the increment-last-char in the
2520 matching algorithm for completion doesn't match "t1_fund" when
2521 completing "t1_func". */
2527 /* A UTF-8 name with multi-byte sequences to make sure that
2528 cp-name-parser understands this as a single identifier ("função"
2529 is "function" in PT). */
2530 (const char *)u8
"u8função",
2532 /* Test a symbol name that ends with a 0xff character, which is a
2533 valid character in non-UTF-8 source character sets (e.g. Latin1
2534 'ÿ'), and we can't rule out compilers allowing it in identifiers.
2535 We test this because the completion algorithm finds the upper
2536 bound of symbols by looking for the insertion point of
2537 "func"-with-last-character-incremented, i.e. "fund", and adding 1
2538 to 0xff should wraparound and carry to the previous character.
2539 See comments in make_sort_after_prefix_name. */
2542 /* Some more symbols with \377 (0xff). See above. */
2546 /* A name with all sorts of complications. Starts with "z" to make
2547 it easier for the completion tests below. */
2548 #define Z_SYM_NAME \
2549 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
2550 "::tuple<(anonymous namespace)::ui*, " \
2551 "std::default_delete<(anonymous namespace)::ui>, void>"
2556 /* Returns true if the mapped_index_base::find_name_component_bounds
2557 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
2558 in completion mode. */
2561 check_find_bounds_finds (mapped_index_base
&index
,
2562 const char *search_name
,
2563 gdb::array_view
<const char *> expected_syms
,
2564 dwarf2_per_objfile
*per_objfile
)
2566 lookup_name_info
lookup_name (search_name
,
2567 symbol_name_match_type::FULL
, true);
2569 auto bounds
= index
.find_name_components_bounds (lookup_name
,
2573 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
2574 if (distance
!= expected_syms
.size ())
2577 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
2579 auto nc_elem
= bounds
.first
+ exp_elem
;
2580 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
2581 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
2588 /* Test the lower-level mapped_index::find_name_component_bounds
2592 test_mapped_index_find_name_component_bounds ()
2594 mock_mapped_index
mock_index (test_symbols
);
2596 mock_index
.build_name_components (NULL
/* per_objfile */);
2598 /* Test the lower-level mapped_index::find_name_component_bounds
2599 method in completion mode. */
2601 static const char *expected_syms
[] = {
2606 SELF_CHECK (check_find_bounds_finds
2607 (mock_index
, "t1_func", expected_syms
,
2608 NULL
/* per_objfile */));
2611 /* Check that the increment-last-char in the name matching algorithm
2612 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
2613 make_sort_after_prefix_name. */
2615 static const char *expected_syms1
[] = {
2619 SELF_CHECK (check_find_bounds_finds
2620 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
2622 static const char *expected_syms2
[] = {
2625 SELF_CHECK (check_find_bounds_finds
2626 (mock_index
, "\377\377", expected_syms2
,
2627 NULL
/* per_objfile */));
2631 /* Test dw2_expand_symtabs_matching_symbol. */
2634 test_dw2_expand_symtabs_matching_symbol ()
2636 mock_mapped_index
mock_index (test_symbols
);
2638 /* We let all tests run until the end even if some fails, for debug
2640 bool any_mismatch
= false;
2642 /* Create the expected symbols list (an initializer_list). Needed
2643 because lists have commas, and we need to pass them to CHECK,
2644 which is a macro. */
2645 #define EXPECT(...) { __VA_ARGS__ }
2647 /* Wrapper for check_match that passes down the current
2648 __FILE__/__LINE__. */
2649 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
2650 any_mismatch |= !check_match (__FILE__, __LINE__, \
2652 NAME, MATCH_TYPE, COMPLETION_MODE, \
2653 EXPECTED_LIST, NULL)
2655 /* Identity checks. */
2656 for (const char *sym
: test_symbols
)
2658 /* Should be able to match all existing symbols. */
2659 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
2662 /* Should be able to match all existing symbols with
2664 std::string with_params
= std::string (sym
) + "(int)";
2665 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
2668 /* Should be able to match all existing symbols with
2669 parameters and qualifiers. */
2670 with_params
= std::string (sym
) + " ( int ) const";
2671 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
2674 /* This should really find sym, but cp-name-parser.y doesn't
2675 know about lvalue/rvalue qualifiers yet. */
2676 with_params
= std::string (sym
) + " ( int ) &&";
2677 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
2681 /* Check that the name matching algorithm for completion doesn't get
2682 confused with Latin1 'ÿ' / 0xff. See
2683 make_sort_after_prefix_name. */
2685 static const char str
[] = "\377";
2686 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
2687 EXPECT ("\377", "\377\377123"));
2690 /* Check that the increment-last-char in the matching algorithm for
2691 completion doesn't match "t1_fund" when completing "t1_func". */
2693 static const char str
[] = "t1_func";
2694 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
2695 EXPECT ("t1_func", "t1_func1"));
2698 /* Check that completion mode works at each prefix of the expected
2701 static const char str
[] = "function(int)";
2702 size_t len
= strlen (str
);
2705 for (size_t i
= 1; i
< len
; i
++)
2707 lookup
.assign (str
, i
);
2708 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
2709 EXPECT ("function"));
2713 /* While "w" is a prefix of both components, the match function
2714 should still only be called once. */
2716 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
2718 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
2722 /* Same, with a "complicated" symbol. */
2724 static const char str
[] = Z_SYM_NAME
;
2725 size_t len
= strlen (str
);
2728 for (size_t i
= 1; i
< len
; i
++)
2730 lookup
.assign (str
, i
);
2731 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
2732 EXPECT (Z_SYM_NAME
));
2736 /* In FULL mode, an incomplete symbol doesn't match. */
2738 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
2742 /* A complete symbol with parameters matches any overload, since the
2743 index has no overload info. */
2745 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
2746 EXPECT ("std::zfunction", "std::zfunction2"));
2747 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
2748 EXPECT ("std::zfunction", "std::zfunction2"));
2749 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
2750 EXPECT ("std::zfunction", "std::zfunction2"));
2753 /* Check that whitespace is ignored appropriately. A symbol with a
2754 template argument list. */
2756 static const char expected
[] = "ns::foo<int>";
2757 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
2759 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
2763 /* Check that whitespace is ignored appropriately. A symbol with a
2764 template argument list that includes a pointer. */
2766 static const char expected
[] = "ns::foo<char*>";
2767 /* Try both completion and non-completion modes. */
2768 static const bool completion_mode
[2] = {false, true};
2769 for (size_t i
= 0; i
< 2; i
++)
2771 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
2772 completion_mode
[i
], EXPECT (expected
));
2773 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
2774 completion_mode
[i
], EXPECT (expected
));
2776 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
2777 completion_mode
[i
], EXPECT (expected
));
2778 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
2779 completion_mode
[i
], EXPECT (expected
));
2784 /* Check method qualifiers are ignored. */
2785 static const char expected
[] = "ns::foo<char*>";
2786 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
2787 symbol_name_match_type::FULL
, true, EXPECT (expected
));
2788 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
2789 symbol_name_match_type::FULL
, true, EXPECT (expected
));
2790 CHECK_MATCH ("foo < char * > ( int ) const",
2791 symbol_name_match_type::WILD
, true, EXPECT (expected
));
2792 CHECK_MATCH ("foo < char * > ( int ) &&",
2793 symbol_name_match_type::WILD
, true, EXPECT (expected
));
2796 /* Test lookup names that don't match anything. */
2798 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
2801 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
2805 /* Some wild matching tests, exercising "(anonymous namespace)",
2806 which should not be confused with a parameter list. */
2808 static const char *syms
[] = {
2812 "A :: B :: C ( int )",
2817 for (const char *s
: syms
)
2819 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
2820 EXPECT ("(anonymous namespace)::A::B::C"));
2825 static const char expected
[] = "ns2::tmpl<int>::foo2";
2826 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
2828 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
2832 SELF_CHECK (!any_mismatch
);
2841 test_mapped_index_find_name_component_bounds ();
2842 test_dw2_expand_symtabs_matching_symbol ();
2845 }} // namespace selftests::dw2_expand_symtabs_matching
2847 #endif /* GDB_SELF_TEST */
2852 dw2_expand_symtabs_matching_one
2853 (dwarf2_per_cu_data
*per_cu
,
2854 dwarf2_per_objfile
*per_objfile
,
2855 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
2856 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
2858 if (file_matcher
== NULL
|| per_cu
->mark
)
2860 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
2862 compunit_symtab
*symtab
2863 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
2864 gdb_assert (symtab
!= nullptr);
2866 if (expansion_notify
!= NULL
&& symtab_was_null
)
2867 return expansion_notify (symtab
);
2875 dw_expand_symtabs_matching_file_matcher
2876 (dwarf2_per_objfile
*per_objfile
,
2877 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
2879 if (file_matcher
== NULL
)
2882 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
2884 NULL
, xcalloc
, xfree
));
2885 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
2887 NULL
, xcalloc
, xfree
));
2889 /* The rule is CUs specify all the files, including those used by
2890 any TU, so there's no need to scan TUs here. */
2892 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
2896 if (per_cu
->is_debug_types
)
2900 /* We only need to look at symtabs not already expanded. */
2901 if (per_objfile
->symtab_set_p (per_cu
.get ()))
2904 if (per_cu
->fnd
!= nullptr)
2906 file_and_directory
*fnd
= per_cu
->fnd
.get ();
2908 if (file_matcher (fnd
->get_name (), false))
2914 /* Before we invoke realpath, which can get expensive when many
2915 files are involved, do a quick comparison of the basenames. */
2916 if ((basenames_may_differ
2917 || file_matcher (lbasename (fnd
->get_name ()), true))
2918 && file_matcher (fnd
->get_fullname (), false))
2925 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
2927 if (file_data
== NULL
)
2930 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
2932 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
2938 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
2940 const char *this_real_name
;
2942 if (file_matcher (file_data
->file_names
[j
], false))
2948 /* Before we invoke realpath, which can get expensive when many
2949 files are involved, do a quick comparison of the basenames. */
2950 if (!basenames_may_differ
2951 && !file_matcher (lbasename (file_data
->file_names
[j
]),
2955 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
2956 if (file_matcher (this_real_name
, false))
2963 void **slot
= htab_find_slot (per_cu
->mark
2964 ? visited_found
.get ()
2965 : visited_not_found
.get (),
2972 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
2975 static struct compunit_symtab
*
2976 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
2981 if (cust
->blockvector () != nullptr
2982 && blockvector_contains_pc (cust
->blockvector (), pc
))
2985 if (cust
->includes
== NULL
)
2988 for (i
= 0; cust
->includes
[i
]; ++i
)
2990 struct compunit_symtab
*s
= cust
->includes
[i
];
2992 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
3000 dwarf2_per_cu_data
*
3001 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
3002 unrelocated_addr adjusted_pc
)
3004 if (per_bfd
->index_addrmap
== nullptr)
3007 void *obj
= per_bfd
->index_addrmap
->find ((CORE_ADDR
) adjusted_pc
);
3008 return static_cast<dwarf2_per_cu_data
*> (obj
);
3011 struct compunit_symtab
*
3012 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
3013 (struct objfile
*objfile
,
3014 struct bound_minimal_symbol msymbol
,
3016 struct obj_section
*section
,
3019 struct compunit_symtab
*result
;
3021 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3023 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
3024 struct dwarf2_per_cu_data
*data
3025 = find_per_cu (per_objfile
->per_bfd
, (unrelocated_addr
) (pc
- baseaddr
));
3026 if (data
== nullptr)
3029 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
3030 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3031 paddress (objfile
->arch (), pc
));
3033 result
= recursively_find_pc_sect_compunit_symtab
3034 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
3036 if (warn_if_readin
&& result
== nullptr)
3037 warning (_("(Error: pc %s in address map, but not in symtab.)"),
3038 paddress (objfile
->arch (), pc
));
3044 dwarf2_base_index_functions::map_symbol_filenames
3045 (struct objfile
*objfile
,
3046 gdb::function_view
<symbol_filename_ftype
> fun
,
3049 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3051 /* Use caches to ensure we only call FUN once for each filename. */
3052 filename_seen_cache filenames_cache
;
3053 std::unordered_set
<quick_file_names
*> qfn_cache
;
3055 /* The rule is CUs specify all the files, including those used by any TU,
3056 so there's no need to scan TUs here. We can ignore file names coming
3057 from already-expanded CUs. It is possible that an expanded CU might
3058 reuse the file names data from a currently unexpanded CU, in this
3059 case we don't want to report the files from the unexpanded CU. */
3061 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
3063 if (!per_cu
->is_debug_types
3064 && per_objfile
->symtab_set_p (per_cu
.get ()))
3066 if (per_cu
->file_names
!= nullptr)
3067 qfn_cache
.insert (per_cu
->file_names
);
3071 for (dwarf2_per_cu_data
*per_cu
3072 : all_units_range (per_objfile
->per_bfd
))
3074 /* We only need to look at symtabs not already expanded. */
3075 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
3078 if (per_cu
->fnd
!= nullptr)
3080 file_and_directory
*fnd
= per_cu
->fnd
.get ();
3082 const char *filename
= fnd
->get_name ();
3083 const char *key
= filename
;
3084 const char *fullname
= nullptr;
3088 fullname
= fnd
->get_fullname ();
3092 if (!filenames_cache
.seen (key
))
3093 fun (filename
, fullname
);
3096 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
3097 if (file_data
== nullptr
3098 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
3101 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
3103 const char *filename
= file_data
->file_names
[j
];
3104 const char *key
= filename
;
3105 const char *fullname
= nullptr;
3109 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
3113 if (!filenames_cache
.seen (key
))
3114 fun (filename
, fullname
);
3120 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
3125 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
3128 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
3130 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3132 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
3134 /* Is this already expanded? */
3135 if (per_objfile
->symtab_set_p (per_cu
.get ()))
3138 /* It has not yet been expanded. */
3145 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
3146 to either a dwarf2_per_bfd or dwz_file object. */
3148 template <typename T
>
3149 static gdb::array_view
<const gdb_byte
>
3150 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
3152 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
3154 if (section
->empty ())
3157 /* Older elfutils strip versions could keep the section in the main
3158 executable while splitting it for the separate debug info file. */
3159 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
3162 section
->read (obj
);
3164 /* dwarf2_section_info::size is a bfd_size_type, while
3165 gdb::array_view works with size_t. On 32-bit hosts, with
3166 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
3167 is 32-bit. So we need an explicit narrowing conversion here.
3168 This is fine, because it's impossible to allocate or mmap an
3169 array/buffer larger than what size_t can represent. */
3170 return gdb::make_array_view (section
->buffer
, section
->size
);
3173 /* Lookup the index cache for the contents of the index associated to
3176 static gdb::array_view
<const gdb_byte
>
3177 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
3179 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
.get ());
3180 if (build_id
== nullptr)
3183 return global_index_cache
.lookup_gdb_index (build_id
,
3184 &dwarf2_per_bfd
->index_cache_res
);
3187 /* Same as the above, but for DWZ. */
3189 static gdb::array_view
<const gdb_byte
>
3190 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
3192 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
3193 if (build_id
== nullptr)
3196 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
3199 static void start_debug_info_reader (dwarf2_per_objfile
*);
3201 /* See dwarf2/public.h. */
3204 dwarf2_initialize_objfile (struct objfile
*objfile
,
3205 const struct dwarf2_debug_sections
*names
,
3208 if (!dwarf2_has_info (objfile
, names
, can_copy
))
3211 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3212 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
3214 dwarf_read_debug_printf ("called");
3216 /* If we're about to read full symbols, don't bother with the
3217 indices. In this case we also don't care if some other debug
3218 format is making psymtabs, because they are all about to be
3220 if ((objfile
->flags
& OBJF_READNOW
))
3222 dwarf_read_debug_printf ("readnow requested");
3224 create_all_units (per_objfile
);
3225 per_bfd
->quick_file_names_table
3226 = create_quick_file_names_table (per_bfd
->all_units
.size ());
3228 objfile
->qf
.emplace_front (new readnow_functions
);
3230 /* Was a GDB index already read when we processed an objfile sharing
3232 else if (per_bfd
->index_table
!= nullptr)
3233 dwarf_read_debug_printf ("re-using symbols");
3234 else if (dwarf2_read_debug_names (per_objfile
))
3235 dwarf_read_debug_printf ("found debug names");
3236 else if (dwarf2_read_gdb_index (per_objfile
,
3237 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
3238 get_gdb_index_contents_from_section
<dwz_file
>))
3239 dwarf_read_debug_printf ("found gdb index from file");
3240 /* ... otherwise, try to find the index in the index cache. */
3241 else if (dwarf2_read_gdb_index (per_objfile
,
3242 get_gdb_index_contents_from_cache
,
3243 get_gdb_index_contents_from_cache_dwz
))
3245 dwarf_read_debug_printf ("found gdb index from cache");
3246 global_index_cache
.hit ();
3250 global_index_cache
.miss ();
3251 start_debug_info_reader (per_objfile
);
3254 if (per_bfd
->index_table
!= nullptr)
3256 if (dwarf_synchronous
)
3257 per_bfd
->index_table
->wait_completely ();
3258 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
3266 /* Find the base address of the compilation unit for range lists and
3267 location lists. It will normally be specified by DW_AT_low_pc.
3268 In DWARF-3 draft 4, the base address could be overridden by
3269 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3270 compilation units with discontinuous ranges. */
3273 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
3275 struct attribute
*attr
;
3277 cu
->base_address
.reset ();
3279 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
3280 if (attr
!= nullptr)
3281 cu
->base_address
= attr
->as_address ();
3284 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
3285 if (attr
!= nullptr)
3286 cu
->base_address
= attr
->as_address ();
3290 /* Helper function that returns the proper abbrev section for
3293 static struct dwarf2_section_info
*
3294 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
3296 struct dwarf2_section_info
*abbrev
;
3297 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
3299 if (this_cu
->is_dwz
)
3300 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
3302 abbrev
= &per_bfd
->abbrev
;
3307 /* Fetch the abbreviation table offset from a comp or type unit header. */
3310 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
3311 struct dwarf2_section_info
*section
,
3312 sect_offset sect_off
)
3314 bfd
*abfd
= section
->get_bfd_owner ();
3315 const gdb_byte
*info_ptr
;
3316 unsigned int initial_length_size
, offset_size
;
3319 section
->read (per_objfile
->objfile
);
3320 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
3321 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
3322 offset_size
= initial_length_size
== 4 ? 4 : 8;
3323 info_ptr
+= initial_length_size
;
3325 version
= read_2_bytes (abfd
, info_ptr
);
3329 /* Skip unit type and address size. */
3333 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
3337 hash_signatured_type (const void *item
)
3339 const struct signatured_type
*sig_type
3340 = (const struct signatured_type
*) item
;
3342 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3343 return sig_type
->signature
;
3347 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
3349 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
3350 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
3352 return lhs
->signature
== rhs
->signature
;
3358 allocate_signatured_type_table ()
3360 return htab_up (htab_create_alloc (41,
3361 hash_signatured_type
,
3363 NULL
, xcalloc
, xfree
));
3366 /* A helper for create_debug_types_hash_table. Read types from SECTION
3367 and fill them into TYPES_HTAB. It will process only type units,
3368 therefore DW_UT_type. */
3371 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
3372 struct dwo_file
*dwo_file
,
3373 dwarf2_section_info
*section
, htab_up
&types_htab
,
3374 rcuh_kind section_kind
)
3376 struct objfile
*objfile
= per_objfile
->objfile
;
3377 struct dwarf2_section_info
*abbrev_section
;
3379 const gdb_byte
*info_ptr
, *end_ptr
;
3381 abbrev_section
= &dwo_file
->sections
.abbrev
;
3383 dwarf_read_debug_printf ("Reading %s for %s",
3384 section
->get_name (),
3385 abbrev_section
->get_file_name ());
3387 section
->read (objfile
);
3388 info_ptr
= section
->buffer
;
3390 if (info_ptr
== NULL
)
3393 /* We can't set abfd until now because the section may be empty or
3394 not present, in which case the bfd is unknown. */
3395 abfd
= section
->get_bfd_owner ();
3397 /* We don't use cutu_reader here because we don't need to read
3398 any dies: the signature is in the header. */
3400 end_ptr
= info_ptr
+ section
->size
;
3401 while (info_ptr
< end_ptr
)
3403 signatured_type_up sig_type
;
3404 struct dwo_unit
*dwo_tu
;
3406 const gdb_byte
*ptr
= info_ptr
;
3407 struct comp_unit_head header
;
3408 unsigned int length
;
3410 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
3412 /* Initialize it due to a false compiler warning. */
3413 header
.signature
= -1;
3414 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
3416 /* We need to read the type's signature in order to build the hash
3417 table, but we don't need anything else just yet. */
3419 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
3420 abbrev_section
, ptr
, section_kind
);
3422 length
= header
.get_length_with_initial ();
3424 /* Skip dummy type units. */
3425 if (ptr
>= info_ptr
+ length
3426 || peek_abbrev_code (abfd
, ptr
) == 0
3427 || (header
.unit_type
!= DW_UT_type
3428 && header
.unit_type
!= DW_UT_split_type
))
3434 if (types_htab
== NULL
)
3435 types_htab
= allocate_dwo_unit_table ();
3437 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
3438 dwo_tu
->dwo_file
= dwo_file
;
3439 dwo_tu
->signature
= header
.signature
;
3440 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
3441 dwo_tu
->section
= section
;
3442 dwo_tu
->sect_off
= sect_off
;
3443 dwo_tu
->length
= length
;
3445 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
3446 gdb_assert (slot
!= NULL
);
3448 complaint (_("debug type entry at offset %s is duplicate to"
3449 " the entry at offset %s, signature %s"),
3450 sect_offset_str (sect_off
),
3451 sect_offset_str (dwo_tu
->sect_off
),
3452 hex_string (header
.signature
));
3455 dwarf_read_debug_printf_v (" offset %s, signature %s",
3456 sect_offset_str (sect_off
),
3457 hex_string (header
.signature
));
3463 /* Create the hash table of all entries in the .debug_types
3464 (or .debug_types.dwo) section(s).
3465 DWO_FILE is a pointer to the DWO file object.
3467 The result is a pointer to the hash table or NULL if there are no types.
3469 Note: This function processes DWO files only, not DWP files. */
3472 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
3473 struct dwo_file
*dwo_file
,
3474 gdb::array_view
<dwarf2_section_info
> type_sections
,
3475 htab_up
&types_htab
)
3477 for (dwarf2_section_info
§ion
: type_sections
)
3478 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
3482 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
3483 If SLOT is non-NULL, it is the entry to use in the hash table.
3484 Otherwise we find one. */
3486 static struct signatured_type
*
3487 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
3489 if (per_objfile
->per_bfd
->all_units
.size ()
3490 == per_objfile
->per_bfd
->all_units
.capacity ())
3491 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
3493 signatured_type_up sig_type_holder
3494 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
3495 signatured_type
*sig_type
= sig_type_holder
.get ();
3497 per_objfile
->per_bfd
->all_units
.emplace_back
3498 (sig_type_holder
.release ());
3502 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
3505 gdb_assert (*slot
== NULL
);
3507 /* The rest of sig_type must be filled in by the caller. */
3511 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
3512 Fill in SIG_ENTRY with DWO_ENTRY. */
3515 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
3516 struct signatured_type
*sig_entry
,
3517 struct dwo_unit
*dwo_entry
)
3519 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
3521 /* Make sure we're not clobbering something we don't expect to. */
3522 gdb_assert (! sig_entry
->queued
);
3523 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
3524 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
3525 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
3526 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
3527 || (to_underlying (sig_entry
->type_offset_in_section
)
3528 == to_underlying (dwo_entry
->type_offset_in_tu
)));
3529 gdb_assert (sig_entry
->type_unit_group
== NULL
);
3530 gdb_assert (sig_entry
->dwo_unit
== NULL
3531 || sig_entry
->dwo_unit
== dwo_entry
);
3533 sig_entry
->section
= dwo_entry
->section
;
3534 sig_entry
->sect_off
= dwo_entry
->sect_off
;
3535 sig_entry
->set_length (dwo_entry
->length
, false);
3536 sig_entry
->reading_dwo_directly
= 1;
3537 sig_entry
->per_bfd
= per_bfd
;
3538 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
3539 sig_entry
->dwo_unit
= dwo_entry
;
3542 /* Subroutine of lookup_signatured_type.
3543 If we haven't read the TU yet, create the signatured_type data structure
3544 for a TU to be read in directly from a DWO file, bypassing the stub.
3545 This is the "Stay in DWO Optimization": When there is no DWP file and we're
3546 using .gdb_index, then when reading a CU we want to stay in the DWO file
3547 containing that CU. Otherwise we could end up reading several other DWO
3548 files (due to comdat folding) to process the transitive closure of all the
3549 mentioned TUs, and that can be slow. The current DWO file will have every
3550 type signature that it needs.
3551 We only do this for .gdb_index because in the psymtab case we already have
3552 to read all the DWOs to build the type unit groups. */
3554 static struct signatured_type
*
3555 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
3557 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3558 struct dwo_file
*dwo_file
;
3559 struct dwo_unit find_dwo_entry
, *dwo_entry
;
3562 gdb_assert (cu
->dwo_unit
);
3564 /* If TU skeletons have been removed then we may not have read in any
3566 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
3567 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
3569 /* We only ever need to read in one copy of a signatured type.
3570 Use the global signatured_types array to do our own comdat-folding
3571 of types. If this is the first time we're reading this TU, and
3572 the TU has an entry in .gdb_index, replace the recorded data from
3573 .gdb_index with this TU. */
3575 signatured_type
find_sig_entry (sig
);
3576 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
3577 &find_sig_entry
, INSERT
);
3578 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
3580 /* We can get here with the TU already read, *or* in the process of being
3581 read. Don't reassign the global entry to point to this DWO if that's
3582 the case. Also note that if the TU is already being read, it may not
3583 have come from a DWO, the program may be a mix of Fission-compiled
3584 code and non-Fission-compiled code. */
3586 /* Have we already tried to read this TU?
3587 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3588 needn't exist in the global table yet). */
3589 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
3592 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
3593 dwo_unit of the TU itself. */
3594 dwo_file
= cu
->dwo_unit
->dwo_file
;
3596 /* Ok, this is the first time we're reading this TU. */
3597 if (dwo_file
->tus
== NULL
)
3599 find_dwo_entry
.signature
= sig
;
3600 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
3602 if (dwo_entry
== NULL
)
3605 /* If the global table doesn't have an entry for this TU, add one. */
3606 if (sig_entry
== NULL
)
3607 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
3609 if (sig_entry
->dwo_unit
== nullptr)
3610 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
3611 sig_entry
->tu_read
= 1;
3615 /* Subroutine of lookup_signatured_type.
3616 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
3617 then try the DWP file. If the TU stub (skeleton) has been removed then
3618 it won't be in .gdb_index. */
3620 static struct signatured_type
*
3621 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
3623 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3624 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
3625 struct dwo_unit
*dwo_entry
;
3628 gdb_assert (cu
->dwo_unit
);
3629 gdb_assert (dwp_file
!= NULL
);
3631 /* If TU skeletons have been removed then we may not have read in any
3633 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
3634 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
3636 signatured_type
find_sig_entry (sig
);
3637 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
3638 &find_sig_entry
, INSERT
);
3639 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
3641 /* Have we already tried to read this TU?
3642 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
3643 needn't exist in the global table yet). */
3644 if (sig_entry
!= NULL
)
3647 if (dwp_file
->tus
== NULL
)
3649 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
3650 1 /* is_debug_types */);
3651 if (dwo_entry
== NULL
)
3654 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
3655 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
3660 /* Lookup a signature based type for DW_FORM_ref_sig8.
3661 Returns NULL if signature SIG is not present in the table.
3662 It is up to the caller to complain about this. */
3664 static struct signatured_type
*
3665 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
3667 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3671 /* We're in a DWO/DWP file, and we're using .gdb_index.
3672 These cases require special processing. */
3673 if (get_dwp_file (per_objfile
) == NULL
)
3674 return lookup_dwo_signatured_type (cu
, sig
);
3676 return lookup_dwp_signatured_type (cu
, sig
);
3680 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
3682 signatured_type
find_entry (sig
);
3683 return ((struct signatured_type
*)
3684 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
3689 /* Low level DIE reading support. */
3691 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3694 init_cu_die_reader (struct die_reader_specs
*reader
,
3695 struct dwarf2_cu
*cu
,
3696 struct dwarf2_section_info
*section
,
3697 struct dwo_file
*dwo_file
,
3698 struct abbrev_table
*abbrev_table
)
3700 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
3701 reader
->abfd
= section
->get_bfd_owner ();
3703 reader
->dwo_file
= dwo_file
;
3704 reader
->die_section
= section
;
3705 reader
->buffer
= section
->buffer
;
3706 reader
->buffer_end
= section
->buffer
+ section
->size
;
3707 reader
->abbrev_table
= abbrev_table
;
3710 /* Subroutine of cutu_reader to simplify it.
3711 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
3712 There's just a lot of work to do, and cutu_reader is big enough
3715 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
3716 from it to the DIE in the DWO. If NULL we are skipping the stub.
3717 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
3718 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
3719 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
3720 STUB_COMP_DIR may be non-NULL.
3721 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
3722 are filled in with the info of the DIE from the DWO file.
3723 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
3724 from the dwo. Since *RESULT_READER references this abbrev table, it must be
3725 kept around for at least as long as *RESULT_READER.
3727 The result is non-zero if a valid (non-dummy) DIE was found. */
3730 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
3731 struct dwo_unit
*dwo_unit
,
3732 struct die_info
*stub_comp_unit_die
,
3733 const char *stub_comp_dir
,
3734 struct die_reader_specs
*result_reader
,
3735 const gdb_byte
**result_info_ptr
,
3736 struct die_info
**result_comp_unit_die
,
3737 abbrev_table_up
*result_dwo_abbrev_table
)
3739 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
3740 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
3741 struct objfile
*objfile
= per_objfile
->objfile
;
3743 const gdb_byte
*begin_info_ptr
, *info_ptr
;
3744 struct dwarf2_section_info
*dwo_abbrev_section
;
3746 /* At most one of these may be provided. */
3747 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
3749 /* These attributes aren't processed until later: DW_AT_stmt_list,
3750 DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_comp_dir.
3751 However, these attributes are found in the stub which we won't
3752 have later. In order to not impose this complication on the rest
3753 of the code, we read them here and copy them to the DWO CU/TU
3756 /* We store them all in an array. */
3757 struct attribute
*attributes
[5] {};
3758 /* Next available element of the attributes array. */
3759 int next_attr_idx
= 0;
3761 /* Push an element into ATTRIBUTES. */
3762 auto push_back
= [&] (struct attribute
*attr
)
3764 gdb_assert (next_attr_idx
< ARRAY_SIZE (attributes
));
3765 if (attr
!= nullptr)
3766 attributes
[next_attr_idx
++] = attr
;
3769 if (stub_comp_unit_die
!= NULL
)
3771 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3773 if (!per_cu
->is_debug_types
)
3774 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
));
3775 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
));
3776 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
));
3777 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
));
3778 push_back (dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
));
3780 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
3782 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
3783 We need the value before we can process DW_AT_ranges values from the
3785 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
3787 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
3788 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
3789 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
3790 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
3792 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
3794 else if (stub_comp_dir
!= NULL
)
3796 /* Reconstruct the comp_dir attribute to simplify the code below. */
3797 struct attribute
*comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
,
3799 comp_dir
->name
= DW_AT_comp_dir
;
3800 comp_dir
->form
= DW_FORM_string
;
3801 comp_dir
->set_string_noncanonical (stub_comp_dir
);
3802 push_back (comp_dir
);
3805 /* Set up for reading the DWO CU/TU. */
3806 cu
->dwo_unit
= dwo_unit
;
3807 dwarf2_section_info
*section
= dwo_unit
->section
;
3808 section
->read (objfile
);
3809 abfd
= section
->get_bfd_owner ();
3810 begin_info_ptr
= info_ptr
= (section
->buffer
3811 + to_underlying (dwo_unit
->sect_off
));
3812 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
3814 if (per_cu
->is_debug_types
)
3816 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
3818 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
3819 section
, dwo_abbrev_section
,
3820 info_ptr
, rcuh_kind::TYPE
);
3821 /* This is not an assert because it can be caused by bad debug info. */
3822 if (sig_type
->signature
!= cu
->header
.signature
)
3824 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
3825 " TU at offset %s [in module %s]"),
3826 hex_string (sig_type
->signature
),
3827 hex_string (cu
->header
.signature
),
3828 sect_offset_str (dwo_unit
->sect_off
),
3829 bfd_get_filename (abfd
));
3831 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
3832 /* For DWOs coming from DWP files, we don't know the CU length
3833 nor the type's offset in the TU until now. */
3834 dwo_unit
->length
= cu
->header
.get_length_with_initial ();
3835 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
3837 /* Establish the type offset that can be used to lookup the type.
3838 For DWO files, we don't know it until now. */
3839 sig_type
->type_offset_in_section
3840 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
3844 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
3845 section
, dwo_abbrev_section
,
3846 info_ptr
, rcuh_kind::COMPILE
);
3847 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
3848 /* For DWOs coming from DWP files, we don't know the CU length
3850 dwo_unit
->length
= cu
->header
.get_length_with_initial ();
3853 dwo_abbrev_section
->read (objfile
);
3854 *result_dwo_abbrev_table
3855 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
3856 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
3857 result_dwo_abbrev_table
->get ());
3859 /* Read in the die, filling in the attributes from the stub. This
3860 has the benefit of simplifying the rest of the code - all the
3861 work to maintain the illusion of a single
3862 DW_TAG_{compile,type}_unit DIE is done here. */
3863 info_ptr
= read_toplevel_die (result_reader
, result_comp_unit_die
, info_ptr
,
3864 gdb::make_array_view (attributes
,
3867 /* Skip dummy compilation units. */
3868 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
3869 || peek_abbrev_code (abfd
, info_ptr
) == 0)
3872 *result_info_ptr
= info_ptr
;
3876 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
3877 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
3878 signature is part of the header. */
3879 static std::optional
<ULONGEST
>
3880 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
3882 if (cu
->header
.version
>= 5)
3883 return cu
->header
.signature
;
3884 struct attribute
*attr
;
3885 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
3886 if (attr
== nullptr || !attr
->form_is_unsigned ())
3887 return std::optional
<ULONGEST
> ();
3888 return attr
->as_unsigned ();
3891 /* Subroutine of cutu_reader to simplify it.
3892 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
3893 Returns NULL if the specified DWO unit cannot be found. */
3895 static struct dwo_unit
*
3896 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
3899 /* We need a lock here to handle the DWO hash table. */
3900 static std::mutex dwo_lock
;
3902 std::lock_guard
<std::mutex
> guard (dwo_lock
);
3905 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
3906 struct dwo_unit
*dwo_unit
;
3907 const char *comp_dir
;
3909 gdb_assert (cu
!= NULL
);
3911 /* Yeah, we look dwo_name up again, but it simplifies the code. */
3912 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
3913 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
3915 if (per_cu
->is_debug_types
)
3916 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
3919 std::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
3921 if (!signature
.has_value ())
3922 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
3924 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
3926 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
3932 /* Subroutine of cutu_reader to simplify it.
3933 See it for a description of the parameters.
3934 Read a TU directly from a DWO file, bypassing the stub. */
3937 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
3938 dwarf2_per_objfile
*per_objfile
,
3939 dwarf2_cu
*existing_cu
)
3941 struct signatured_type
*sig_type
;
3943 /* Verify we can do the following downcast, and that we have the
3945 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
3946 sig_type
= (struct signatured_type
*) this_cu
;
3947 gdb_assert (sig_type
->dwo_unit
!= NULL
);
3951 if (existing_cu
!= nullptr)
3954 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
3955 /* There's no need to do the rereading_dwo_cu handling that
3956 cutu_reader does since we don't read the stub. */
3960 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
3961 in per_objfile yet. */
3962 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
3963 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
3964 cu
= m_new_cu
.get ();
3967 /* A future optimization, if needed, would be to use an existing
3968 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
3969 could share abbrev tables. */
3971 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
3972 NULL
/* stub_comp_unit_die */,
3973 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
3976 &m_dwo_abbrev_table
) == 0)
3983 /* Initialize a CU (or TU) and read its DIEs.
3984 If the CU defers to a DWO file, read the DWO file as well.
3986 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
3987 Otherwise the table specified in the comp unit header is read in and used.
3988 This is an optimization for when we already have the abbrev table.
3990 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
3993 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
3994 dwarf2_per_objfile
*per_objfile
,
3995 struct abbrev_table
*abbrev_table
,
3996 dwarf2_cu
*existing_cu
,
3998 abbrev_cache
*cache
)
3999 : die_reader_specs
{},
4002 struct objfile
*objfile
= per_objfile
->objfile
;
4003 struct dwarf2_section_info
*section
= this_cu
->section
;
4004 bfd
*abfd
= section
->get_bfd_owner ();
4005 const gdb_byte
*begin_info_ptr
;
4006 struct signatured_type
*sig_type
= NULL
;
4007 struct dwarf2_section_info
*abbrev_section
;
4008 /* Non-zero if CU currently points to a DWO file and we need to
4009 reread it. When this happens we need to reread the skeleton die
4010 before we can reread the DWO file (this only applies to CUs, not TUs). */
4011 int rereading_dwo_cu
= 0;
4013 if (dwarf_die_debug
)
4014 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
4015 this_cu
->is_debug_types
? "type" : "comp",
4016 sect_offset_str (this_cu
->sect_off
));
4018 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4019 file (instead of going through the stub), short-circuit all of this. */
4020 if (this_cu
->reading_dwo_directly
)
4022 /* Narrow down the scope of possibilities to have to understand. */
4023 gdb_assert (this_cu
->is_debug_types
);
4024 gdb_assert (abbrev_table
== NULL
);
4025 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
4029 /* This is cheap if the section is already read in. */
4030 section
->read (objfile
);
4032 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
4034 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
4038 if (existing_cu
!= nullptr)
4041 /* If this CU is from a DWO file we need to start over, we need to
4042 refetch the attributes from the skeleton CU.
4043 This could be optimized by retrieving those attributes from when we
4044 were here the first time: the previous comp_unit_die was stored in
4045 comp_unit_obstack. But there's no data yet that we need this
4047 if (cu
->dwo_unit
!= NULL
)
4048 rereading_dwo_cu
= 1;
4052 /* If an existing_cu is provided, a dwarf2_cu must not exist for
4053 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
4054 let us know that the CU is being scanned using the parallel
4055 indexer. This assert is avoided in this case because (1) it
4056 is irrelevant, and (2) the get_cu method is not
4058 gdb_assert (cache
!= nullptr
4059 || per_objfile
->get_cu (this_cu
) == nullptr);
4060 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
4061 cu
= m_new_cu
.get ();
4064 /* Get the header. */
4065 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
4067 /* We already have the header, there's no need to read it in again. */
4068 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
4072 if (this_cu
->is_debug_types
)
4074 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
4075 section
, abbrev_section
,
4076 info_ptr
, rcuh_kind::TYPE
);
4078 /* Since per_cu is the first member of struct signatured_type,
4079 we can go from a pointer to one to a pointer to the other. */
4080 sig_type
= (struct signatured_type
*) this_cu
;
4081 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
4082 gdb_assert (sig_type
->type_offset_in_tu
4083 == cu
->header
.type_cu_offset_in_tu
);
4084 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
4086 /* LENGTH has not been set yet for type units if we're
4087 using .gdb_index. */
4088 this_cu
->set_length (cu
->header
.get_length_with_initial ());
4090 /* Establish the type offset that can be used to lookup the type. */
4091 sig_type
->type_offset_in_section
=
4092 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
4094 this_cu
->set_version (cu
->header
.version
);
4098 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
4099 section
, abbrev_section
,
4101 rcuh_kind::COMPILE
);
4103 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
4104 this_cu
->set_length (cu
->header
.get_length_with_initial ());
4105 this_cu
->set_version (cu
->header
.version
);
4109 /* Skip dummy compilation units. */
4110 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
4111 || peek_abbrev_code (abfd
, info_ptr
) == 0)
4117 /* If we don't have them yet, read the abbrevs for this compilation unit.
4118 And if we need to read them now, make sure they're freed when we're
4120 if (abbrev_table
!= NULL
)
4121 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
4124 if (cache
!= nullptr)
4125 abbrev_table
= cache
->find (abbrev_section
,
4126 cu
->header
.abbrev_sect_off
);
4127 if (abbrev_table
== nullptr)
4129 abbrev_section
->read (objfile
);
4130 m_abbrev_table_holder
4131 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
4132 abbrev_table
= m_abbrev_table_holder
.get ();
4136 /* Read the top level CU/TU die. */
4137 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
4138 info_ptr
= read_toplevel_die (this, &comp_unit_die
, info_ptr
);
4140 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
4146 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
4147 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
4148 table from the DWO file and pass the ownership over to us. It will be
4149 referenced from READER, so we must make sure to free it after we're done
4152 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
4153 DWO CU, that this test will fail (the attribute will not be present). */
4154 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
4155 if (dwo_name
!= nullptr)
4157 struct dwo_unit
*dwo_unit
;
4158 struct die_info
*dwo_comp_unit_die
;
4160 if (comp_unit_die
->has_children
)
4162 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
4163 " has children (offset %s) [in module %s]"),
4164 sect_offset_str (this_cu
->sect_off
),
4165 bfd_get_filename (abfd
));
4167 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
4168 if (dwo_unit
!= NULL
)
4170 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
4171 comp_unit_die
, NULL
,
4174 &m_dwo_abbrev_table
) == 0)
4180 comp_unit_die
= dwo_comp_unit_die
;
4184 /* Yikes, we couldn't find the rest of the DIE, we only have
4185 the stub. A complaint has already been logged. There's
4186 not much more we can do except pass on the stub DIE to
4187 die_reader_func. We don't want to throw an error on bad
4194 cutu_reader::keep ()
4196 /* Done, clean up. */
4197 gdb_assert (!dummy_p
);
4198 if (m_new_cu
!= NULL
)
4200 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
4202 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
4203 per_objfile
->set_cu (m_this_cu
, std::move (m_new_cu
));
4207 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
4208 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
4209 assumed to have already done the lookup to find the DWO file).
4211 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4212 THIS_CU->is_debug_types, but nothing else.
4214 We fill in THIS_CU->length.
4216 THIS_CU->cu is always freed when done.
4217 This is done in order to not leave THIS_CU->cu in a state where we have
4218 to care whether it refers to the "main" CU or the DWO CU.
4220 When parent_cu is passed, it is used to provide a default value for
4221 str_offsets_base and addr_base from the parent. */
4223 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
4224 dwarf2_per_objfile
*per_objfile
,
4225 struct dwarf2_cu
*parent_cu
,
4226 struct dwo_file
*dwo_file
)
4227 : die_reader_specs
{},
4230 struct objfile
*objfile
= per_objfile
->objfile
;
4231 struct dwarf2_section_info
*section
= this_cu
->section
;
4232 bfd
*abfd
= section
->get_bfd_owner ();
4233 struct dwarf2_section_info
*abbrev_section
;
4234 const gdb_byte
*begin_info_ptr
, *info_ptr
;
4236 if (dwarf_die_debug
)
4237 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
4238 this_cu
->is_debug_types
? "type" : "comp",
4239 sect_offset_str (this_cu
->sect_off
));
4241 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
4243 abbrev_section
= (dwo_file
!= NULL
4244 ? &dwo_file
->sections
.abbrev
4245 : get_abbrev_section_for_cu (this_cu
));
4247 /* This is cheap if the section is already read in. */
4248 section
->read (objfile
);
4250 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
4252 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
4253 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
4254 section
, abbrev_section
, info_ptr
,
4255 (this_cu
->is_debug_types
4257 : rcuh_kind::COMPILE
));
4259 if (parent_cu
!= nullptr)
4261 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
4262 m_new_cu
->addr_base
= parent_cu
->addr_base
;
4264 this_cu
->set_length (m_new_cu
->header
.get_length_with_initial ());
4266 /* Skip dummy compilation units. */
4267 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
4268 || peek_abbrev_code (abfd
, info_ptr
) == 0)
4274 abbrev_section
->read (objfile
);
4275 m_abbrev_table_holder
4276 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
4278 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
4279 m_abbrev_table_holder
.get ());
4280 info_ptr
= read_toplevel_die (this, &comp_unit_die
, info_ptr
);
4284 /* Type Unit Groups.
4286 Type Unit Groups are a way to collapse the set of all TUs (type units) into
4287 a more manageable set. The grouping is done by DW_AT_stmt_list entry
4288 so that all types coming from the same compilation (.o file) are grouped
4289 together. A future step could be to put the types in the same symtab as
4290 the CU the types ultimately came from. */
4293 hash_type_unit_group (const void *item
)
4295 const struct type_unit_group
*tu_group
4296 = (const struct type_unit_group
*) item
;
4298 return hash_stmt_list_entry (&tu_group
->hash
);
4302 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
4304 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
4305 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
4307 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
4310 /* Allocate a hash table for type unit groups. */
4313 allocate_type_unit_groups_table ()
4315 return htab_up (htab_create_alloc (3,
4316 hash_type_unit_group
,
4318 htab_delete_entry
<type_unit_group
>,
4322 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4323 partial symtabs. We combine several TUs per psymtab to not let the size
4324 of any one psymtab grow too big. */
4325 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4326 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4328 /* Helper routine for get_type_unit_group.
4329 Create the type_unit_group object used to hold one or more TUs. */
4331 static std::unique_ptr
<type_unit_group
>
4332 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
4334 auto tu_group
= std::make_unique
<type_unit_group
> ();
4336 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
4337 tu_group
->hash
.line_sect_off
= line_offset_struct
;
4342 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4343 STMT_LIST is a DW_AT_stmt_list attribute. */
4345 static struct type_unit_group
*
4346 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
4348 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
4349 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
4350 struct type_unit_group
*tu_group
;
4352 unsigned int line_offset
;
4353 struct type_unit_group type_unit_group_for_lookup
;
4355 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
4356 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
4358 /* Do we need to create a new group, or can we use an existing one? */
4360 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
4362 line_offset
= stmt_list
->as_unsigned ();
4363 ++tu_stats
->nr_symtab_sharers
;
4367 /* Ugh, no stmt_list. Rare, but we have to handle it.
4368 We can do various things here like create one group per TU or
4369 spread them over multiple groups to split up the expansion work.
4370 To avoid worst case scenarios (too many groups or too large groups)
4371 we, umm, group them in bunches. */
4372 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
4373 | (tu_stats
->nr_stmt_less_type_units
4374 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
4375 ++tu_stats
->nr_stmt_less_type_units
;
4378 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
4379 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
4380 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
4381 &type_unit_group_for_lookup
, INSERT
);
4382 if (*slot
== nullptr)
4384 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
4385 std::unique_ptr
<type_unit_group
> grp
4386 = create_type_unit_group (cu
, line_offset_struct
);
4387 *slot
= grp
.release ();
4388 ++tu_stats
->nr_symtabs
;
4391 tu_group
= (struct type_unit_group
*) *slot
;
4392 gdb_assert (tu_group
!= nullptr);
4397 cooked_index_storage::cooked_index_storage ()
4398 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
4400 htab_delete_entry
<cutu_reader
>,
4402 m_index (new cooked_index_shard
)
4407 cooked_index_storage::get_reader (dwarf2_per_cu_data
*per_cu
)
4409 int index
= per_cu
->index
;
4410 return (cutu_reader
*) htab_find_with_hash (m_reader_hash
.get (),
4415 cooked_index_storage::preserve (std::unique_ptr
<cutu_reader
> reader
)
4417 m_abbrev_cache
.add (reader
->release_abbrev_table ());
4419 int index
= reader
->cu
->per_cu
->index
;
4420 void **slot
= htab_find_slot_with_hash (m_reader_hash
.get (), &index
,
4422 gdb_assert (*slot
== nullptr);
4423 cutu_reader
*result
= reader
.get ();
4424 *slot
= reader
.release ();
4428 /* Hash function for a cutu_reader. */
4430 cooked_index_storage::hash_cutu_reader (const void *a
)
4432 const cutu_reader
*reader
= (const cutu_reader
*) a
;
4433 return reader
->cu
->per_cu
->index
;
4436 /* Equality function for cutu_reader. */
4438 cooked_index_storage::eq_cutu_reader (const void *a
, const void *b
)
4440 const cutu_reader
*ra
= (const cutu_reader
*) a
;
4441 const int *rb
= (const int *) b
;
4442 return ra
->cu
->per_cu
->index
== *rb
;
4445 /* An instance of this is created to index a CU. */
4447 class cooked_indexer
4451 cooked_indexer (cooked_index_storage
*storage
,
4452 dwarf2_per_cu_data
*per_cu
,
4453 enum language language
)
4454 : m_index_storage (storage
),
4456 m_language (language
),
4457 m_die_range_map (storage
->get_parent_map ())
4461 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
4463 /* Index the given CU. */
4464 void make_index (cutu_reader
*reader
);
4468 /* A helper function to scan the PC bounds of READER and record them
4469 in the storage's addrmap. */
4470 void check_bounds (cutu_reader
*reader
);
4472 /* Ensure that the indicated CU exists. The cutu_reader for it is
4473 returned. FOR_SCANNING is true if the caller intends to scan all
4474 the DIEs in the CU; when false, this use is assumed to be to look
4475 up just a single DIE. */
4476 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
4477 dwarf2_per_objfile
*per_objfile
,
4478 sect_offset sect_off
,
4482 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
4483 the entry for the enclosing scope (nullptr at top level). FULLY
4484 is true when a full scan must be done -- in some languages,
4485 function scopes must be fully explored in order to find nested
4486 functions. This returns a pointer to just after the spot where
4488 const gdb_byte
*index_dies (cutu_reader
*reader
,
4489 const gdb_byte
*info_ptr
,
4490 const cooked_index_entry
*parent_entry
,
4493 /* Scan the attributes for a given DIE and update the out
4494 parameters. Returns a pointer to the byte after the DIE. */
4495 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
4496 cutu_reader
*reader
,
4497 const gdb_byte
*watermark_ptr
,
4498 const gdb_byte
*info_ptr
,
4499 const abbrev_info
*abbrev
,
4501 const char **linkage_name
,
4502 cooked_index_flag
*flags
,
4503 sect_offset
*sibling_offset
,
4504 const cooked_index_entry
**parent_entry
,
4505 parent_map::addr_type
*maybe_defer
,
4506 bool *is_enum_class
,
4507 bool for_specification
);
4509 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
4510 DW_AT_import, and then scanning the referenced CU. Returns a
4511 pointer to the byte after the DIE. */
4512 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
4513 const gdb_byte
*info_ptr
,
4514 const abbrev_info
*abbrev
);
4516 /* Recursively read DIEs, recording the section offsets in
4517 m_die_range_map and then calling index_dies. */
4518 const gdb_byte
*recurse (cutu_reader
*reader
,
4519 const gdb_byte
*info_ptr
,
4520 const cooked_index_entry
*parent_entry
,
4523 /* The storage object, where the results are kept. */
4524 cooked_index_storage
*m_index_storage
;
4525 /* The CU that we are reading on behalf of. This object might be
4526 asked to index one CU but to treat the results as if they come
4527 from some including CU; in this case the including CU would be
4529 dwarf2_per_cu_data
*m_per_cu
;
4530 /* The language that we're assuming when reading. */
4531 enum language m_language
;
4533 /* Map from DIE ranges to newly-created entries. */
4534 parent_map
*m_die_range_map
;
4537 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4538 Process compilation unit THIS_CU for a psymtab. */
4541 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
4542 dwarf2_per_objfile
*per_objfile
,
4543 cooked_index_storage
*storage
)
4545 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
4546 storage
->get_abbrev_cache ());
4548 if (reader
.comp_unit_die
== nullptr)
4555 else if (this_cu
->is_debug_types
)
4556 build_type_psymtabs_reader (&reader
, storage
);
4557 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
4560 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
4562 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
4564 gdb_assert (storage
!= nullptr);
4565 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->lang ());
4566 indexer
.make_index (&reader
);
4571 /* Reader function for build_type_psymtabs. */
4574 build_type_psymtabs_reader (cutu_reader
*reader
,
4575 cooked_index_storage
*storage
)
4577 struct dwarf2_cu
*cu
= reader
->cu
;
4578 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
4579 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
4581 gdb_assert (per_cu
->is_debug_types
);
4583 if (! type_unit_die
->has_children
)
4586 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
4588 gdb_assert (storage
!= nullptr);
4589 cooked_indexer
indexer (storage
, per_cu
, cu
->lang ());
4590 indexer
.make_index (reader
);
4593 /* Struct used to sort TUs by their abbreviation table offset. */
4595 struct tu_abbrev_offset
4597 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
4598 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
4601 /* This is used when sorting. */
4602 bool operator< (const tu_abbrev_offset
&other
) const
4604 return abbrev_offset
< other
.abbrev_offset
;
4607 signatured_type
*sig_type
;
4608 sect_offset abbrev_offset
;
4611 /* Efficiently read all the type units.
4613 The efficiency is because we sort TUs by the abbrev table they use and
4614 only read each abbrev table once. In one program there are 200K TUs
4615 sharing 8K abbrev tables.
4617 The main purpose of this function is to support building the
4618 dwarf2_per_objfile->per_bfd->type_unit_groups table.
4619 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
4620 can collapse the search space by grouping them by stmt_list.
4621 The savings can be significant, in the same program from above the 200K TUs
4622 share 8K stmt_list tables.
4624 FUNC is expected to call get_type_unit_group, which will create the
4625 struct type_unit_group if necessary and add it to
4626 dwarf2_per_objfile->per_bfd->type_unit_groups. */
4629 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
4630 cooked_index_storage
*storage
)
4632 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
4633 abbrev_table_up abbrev_table
;
4634 sect_offset abbrev_offset
;
4636 /* It's up to the caller to not call us multiple times. */
4637 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
4639 if (per_objfile
->per_bfd
->all_type_units
.size () == 0)
4642 /* TUs typically share abbrev tables, and there can be way more TUs than
4643 abbrev tables. Sort by abbrev table to reduce the number of times we
4644 read each abbrev table in.
4645 Alternatives are to punt or to maintain a cache of abbrev tables.
4646 This is simpler and efficient enough for now.
4648 Later we group TUs by their DW_AT_stmt_list value (as this defines the
4649 symtab to use). Typically TUs with the same abbrev offset have the same
4650 stmt_list value too so in practice this should work well.
4652 The basic algorithm here is:
4654 sort TUs by abbrev table
4655 for each TU with same abbrev table:
4656 read abbrev table if first user
4657 read TU top level DIE
4658 [IWBN if DWO skeletons had DW_AT_stmt_list]
4661 dwarf_read_debug_printf ("Building type unit groups ...");
4663 /* Sort in a separate table to maintain the order of all_units
4664 for .gdb_index: TU indices directly index all_type_units. */
4665 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
4666 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->all_type_units
.size ());
4668 for (const auto &cu
: per_objfile
->per_bfd
->all_units
)
4670 if (cu
->is_debug_types
)
4672 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
4673 sorted_by_abbrev
.emplace_back
4674 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
4675 sig_type
->sect_off
));
4679 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
4681 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
4683 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
4685 /* Switch to the next abbrev table if necessary. */
4686 if (abbrev_table
== NULL
4687 || tu
.abbrev_offset
!= abbrev_offset
)
4689 abbrev_offset
= tu
.abbrev_offset
;
4690 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
4692 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
4693 ++tu_stats
->nr_uniq_abbrev_tables
;
4696 cutu_reader
reader (tu
.sig_type
, per_objfile
,
4697 abbrev_table
.get (), nullptr, false);
4698 if (!reader
.dummy_p
)
4699 build_type_psymtabs_reader (&reader
, storage
);
4703 /* Print collected type unit statistics. */
4706 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
4708 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
4710 dwarf_read_debug_printf ("Type unit statistics:");
4711 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
4712 dwarf_read_debug_printf (" %d uniq abbrev tables",
4713 tu_stats
->nr_uniq_abbrev_tables
);
4714 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
4715 tu_stats
->nr_symtabs
);
4716 dwarf_read_debug_printf (" %d symtab sharers",
4717 tu_stats
->nr_symtab_sharers
);
4718 dwarf_read_debug_printf (" %d type units without a stmt_list",
4719 tu_stats
->nr_stmt_less_type_units
);
4720 dwarf_read_debug_printf (" %d all_type_units reallocs",
4721 tu_stats
->nr_all_type_units_reallocs
);
4724 struct skeleton_data
4726 dwarf2_per_objfile
*per_objfile
;
4727 cooked_index_storage
*storage
;
4730 /* Traversal function for process_skeletonless_type_unit.
4731 Read a TU in a DWO file and build partial symbols for it. */
4734 process_skeletonless_type_unit (void **slot
, void *info
)
4736 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
4737 skeleton_data
*data
= (skeleton_data
*) info
;
4739 /* If this TU doesn't exist in the global table, add it and read it in. */
4741 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
4742 data
->per_objfile
->per_bfd
->signatured_types
4743 = allocate_signatured_type_table ();
4745 signatured_type
find_entry (dwo_unit
->signature
);
4746 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
4747 &find_entry
, INSERT
);
4748 /* If we've already seen this type there's nothing to do. What's happening
4749 is we're doing our own version of comdat-folding here. */
4753 /* This does the job that create_all_units would have done for
4755 signatured_type
*entry
4756 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
4757 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
4760 /* This does the job that build_type_psymtabs would have done. */
4761 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
4762 if (!reader
.dummy_p
)
4763 build_type_psymtabs_reader (&reader
, data
->storage
);
4768 /* Traversal function for process_skeletonless_type_units. */
4771 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
4773 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
4775 if (dwo_file
->tus
!= NULL
)
4776 htab_traverse_noresize (dwo_file
->tus
.get (),
4777 process_skeletonless_type_unit
, info
);
4782 /* Scan all TUs of DWO files, verifying we've processed them.
4783 This is needed in case a TU was emitted without its skeleton.
4784 Note: This can't be done until we know what all the DWO files are. */
4787 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
4788 cooked_index_storage
*storage
)
4790 skeleton_data data
{ per_objfile
, storage
};
4792 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
4793 if (get_dwp_file (per_objfile
) == NULL
4794 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
4796 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
4797 process_dwo_file_for_skeletonless_type_units
,
4802 /* A subclass of cooked_index_worker that handles scanning
4805 class cooked_index_debug_info
: public cooked_index_worker
4808 cooked_index_debug_info (dwarf2_per_objfile
*per_objfile
)
4809 : cooked_index_worker (per_objfile
)
4811 gdb_assert (is_main_thread ());
4813 struct objfile
*objfile
= per_objfile
->objfile
;
4814 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4816 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
4817 objfile_name (objfile
));
4819 per_bfd
->map_info_sections (objfile
);
4824 void do_reading () override
;
4826 void print_stats () override
4828 if (dwarf_read_debug
> 0)
4829 print_tu_stats (m_per_objfile
);
4832 /* After the last DWARF-reading task has finished, this function
4833 does the remaining work to finish the scan. */
4834 void done_reading ();
4836 /* An iterator for the comp units. */
4837 typedef std::vector
<dwarf2_per_cu_data_up
>::iterator unit_iterator
;
4839 /* Process a batch of CUs. This may be called multiple times in
4840 separate threads. TASK_NUMBER indicates which task this is --
4841 the result is stored in that slot of M_RESULTS. */
4842 void process_cus (size_t task_number
, unit_iterator first
,
4845 /* A storage object for "leftovers" -- see the 'start' method, but
4846 essentially things not parsed during the normal CU parsing
4848 cooked_index_storage m_index_storage
;
4852 cooked_index_debug_info::process_cus (size_t task_number
, unit_iterator first
,
4855 SCOPE_EXIT
{ bfd_thread_cleanup (); };
4857 /* Ensure that complaints are handled correctly. */
4858 complaint_interceptor complaint_handler
;
4860 std::vector
<gdb_exception
> errors
;
4861 cooked_index_storage thread_storage
;
4862 for (auto inner
= first
; inner
!= end
; ++inner
)
4864 dwarf2_per_cu_data
*per_cu
= inner
->get ();
4867 process_psymtab_comp_unit (per_cu
, m_per_objfile
, &thread_storage
);
4869 catch (gdb_exception
&except
)
4871 errors
.push_back (std::move (except
));
4875 m_results
[task_number
] = result_type (thread_storage
.release (),
4876 complaint_handler
.release (),
4878 thread_storage
.release_parent_map ());
4882 cooked_index_debug_info::done_reading ()
4884 /* Only handle the scanning results here. Complaints and exceptions
4885 can only be dealt with on the main thread. */
4886 std::vector
<std::unique_ptr
<cooked_index_shard
>> indexes
;
4887 for (auto &one_result
: m_results
)
4889 indexes
.push_back (std::move (std::get
<0> (one_result
)));
4890 m_all_parents_map
.add_map (std::get
<3> (one_result
));
4893 /* This has to wait until we read the CUs, we need the list of DWOs. */
4894 process_skeletonless_type_units (m_per_objfile
, &m_index_storage
);
4896 indexes
.push_back (m_index_storage
.release ());
4897 indexes
.shrink_to_fit ();
4899 m_all_parents_map
.add_map (m_index_storage
.release_parent_map ());
4901 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4903 = (gdb::checked_static_cast
<cooked_index
*>
4904 (per_bfd
->index_table
.get ()));
4905 table
->set_contents (std::move (indexes
), &m_warnings
,
4906 &m_all_parents_map
);
4910 cooked_index_debug_info::do_reading ()
4912 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4914 create_all_units (m_per_objfile
);
4915 build_type_psymtabs (m_per_objfile
, &m_index_storage
);
4917 per_bfd
->quick_file_names_table
4918 = create_quick_file_names_table (per_bfd
->all_units
.size ());
4919 if (!per_bfd
->debug_aranges
.empty ())
4920 read_addrmap_from_aranges (m_per_objfile
, &per_bfd
->debug_aranges
,
4921 m_index_storage
.get_addrmap (),
4924 /* We want to balance the load between the worker threads. This is
4925 done by using the size of each CU as a rough estimate of how
4926 difficult it will be to operate on. This isn't ideal -- for
4927 example if dwz is used, the early CUs will all tend to be
4928 "included" and won't be parsed independently. However, this
4929 heuristic works well for typical compiler output. */
4931 size_t total_size
= 0;
4932 for (const auto &per_cu
: per_bfd
->all_units
)
4933 total_size
+= per_cu
->length ();
4935 /* How many worker threads we plan to use. We may not actually use
4936 this many. We use 1 as the minimum to avoid division by zero,
4937 and anyway in the N==0 case the work will be done
4939 const size_t n_worker_threads
4940 = std::max (gdb::thread_pool::g_thread_pool
->thread_count (), (size_t) 1);
4942 /* How much effort should be put into each worker. */
4943 const size_t size_per_thread
4944 = std::max (total_size
/ n_worker_threads
, (size_t) 1);
4946 /* Work is done in a task group. */
4947 gdb::task_group
workers ([this] ()
4949 this->done_reading ();
4952 auto end
= per_bfd
->all_units
.end ();
4953 size_t task_count
= 0;
4954 for (auto iter
= per_bfd
->all_units
.begin (); iter
!= end
; )
4957 /* Put all remaining CUs into the last task. */
4958 if (task_count
== n_worker_threads
- 1)
4962 size_t chunk_size
= 0;
4963 for (; last
!= end
&& chunk_size
< size_per_thread
; ++last
)
4964 chunk_size
+= (*last
)->length ();
4967 gdb_assert (iter
!= last
);
4968 workers
.add_task ([=] ()
4970 process_cus (task_count
, iter
, last
);
4977 m_results
.resize (task_count
);
4982 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
4983 struct dwarf2_section_info
*section
,
4984 struct dwarf2_section_info
*abbrev_section
,
4985 unsigned int is_dwz
,
4986 htab_up
&types_htab
,
4987 rcuh_kind section_kind
)
4989 const gdb_byte
*info_ptr
;
4990 struct objfile
*objfile
= per_objfile
->objfile
;
4992 dwarf_read_debug_printf ("Reading %s for %s",
4993 section
->get_name (),
4994 section
->get_file_name ());
4996 section
->read (objfile
);
4998 info_ptr
= section
->buffer
;
5000 while (info_ptr
< section
->buffer
+ section
->size
)
5002 dwarf2_per_cu_data_up this_cu
;
5004 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
5006 comp_unit_head cu_header
;
5007 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
5008 abbrev_section
, info_ptr
,
5011 /* Save the compilation unit for later lookup. */
5012 if (cu_header
.unit_type
!= DW_UT_type
)
5013 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
5016 if (types_htab
== nullptr)
5017 types_htab
= allocate_signatured_type_table ();
5019 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
5020 (cu_header
.signature
);
5021 signatured_type
*sig_ptr
= sig_type
.get ();
5022 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
5023 this_cu
.reset (sig_type
.release ());
5025 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
5026 gdb_assert (slot
!= nullptr);
5027 if (*slot
!= nullptr)
5028 complaint (_("debug type entry at offset %s is duplicate to"
5029 " the entry at offset %s, signature %s"),
5030 sect_offset_str (sect_off
),
5031 sect_offset_str (sig_ptr
->sect_off
),
5032 hex_string (sig_ptr
->signature
));
5035 this_cu
->sect_off
= sect_off
;
5036 this_cu
->set_length (cu_header
.get_length_with_initial ());
5037 this_cu
->is_dwz
= is_dwz
;
5038 this_cu
->section
= section
;
5039 /* Init this asap, to avoid a data race in the set_version in
5040 cutu_reader::cutu_reader (which may be run in parallel for the cooked
5042 this_cu
->set_version (cu_header
.version
);
5044 info_ptr
= info_ptr
+ this_cu
->length ();
5045 per_objfile
->per_bfd
->all_units
.push_back (std::move (this_cu
));
5049 /* Initialize the views on all_units. */
5052 finalize_all_units (dwarf2_per_bfd
*per_bfd
)
5054 size_t nr_tus
= per_bfd
->tu_stats
.nr_tus
;
5055 size_t nr_cus
= per_bfd
->all_units
.size () - nr_tus
;
5056 gdb::array_view
<dwarf2_per_cu_data_up
> tmp
= per_bfd
->all_units
;
5057 per_bfd
->all_comp_units
= tmp
.slice (0, nr_cus
);
5058 per_bfd
->all_type_units
= tmp
.slice (nr_cus
, nr_tus
);
5064 create_all_units (dwarf2_per_objfile
*per_objfile
)
5067 gdb_assert (per_objfile
->per_bfd
->all_units
.empty ());
5069 read_comp_units_from_section (per_objfile
, &per_objfile
->per_bfd
->info
,
5070 &per_objfile
->per_bfd
->abbrev
, 0,
5071 types_htab
, rcuh_kind::COMPILE
);
5072 for (dwarf2_section_info
§ion
: per_objfile
->per_bfd
->types
)
5073 read_comp_units_from_section (per_objfile
, §ion
,
5074 &per_objfile
->per_bfd
->abbrev
, 0,
5075 types_htab
, rcuh_kind::TYPE
);
5077 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
5080 read_comp_units_from_section (per_objfile
, &dwz
->info
, &dwz
->abbrev
, 1,
5081 types_htab
, rcuh_kind::COMPILE
);
5083 if (!dwz
->types
.empty ())
5085 per_objfile
->per_bfd
->all_units
.clear ();
5087 /* See enhancement PR symtab/30838. */
5088 error (_("Dwarf Error: .debug_types section not supported in dwz file"));
5092 per_objfile
->per_bfd
->signatured_types
= std::move (types_htab
);
5094 finalize_all_units (per_objfile
->per_bfd
);
5097 /* Return the initial uleb128 in the die at INFO_PTR. */
5100 peek_abbrev_code (bfd
*abfd
, const gdb_byte
*info_ptr
)
5102 unsigned int bytes_read
;
5104 return read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
5107 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
5108 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
5110 Return the corresponding abbrev, or NULL if the number is zero (indicating
5111 an empty DIE). In either case *BYTES_READ will be set to the length of
5112 the initial number. */
5114 static const struct abbrev_info
*
5115 peek_die_abbrev (const die_reader_specs
&reader
,
5116 const gdb_byte
*info_ptr
, unsigned int *bytes_read
)
5118 dwarf2_cu
*cu
= reader
.cu
;
5119 bfd
*abfd
= reader
.abfd
;
5120 unsigned int abbrev_number
5121 = read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
5123 if (abbrev_number
== 0)
5126 const abbrev_info
*abbrev
5127 = reader
.abbrev_table
->lookup_abbrev (abbrev_number
);
5130 error (_("Dwarf Error: Could not find abbrev number %d in %s"
5131 " at offset %s [in module %s]"),
5132 abbrev_number
, cu
->per_cu
->is_debug_types
? "TU" : "CU",
5133 sect_offset_str (cu
->header
.sect_off
), bfd_get_filename (abfd
));
5139 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5140 Returns a pointer to the end of a series of DIEs, terminated by an empty
5141 DIE. Any children of the skipped DIEs will also be skipped. */
5143 static const gdb_byte
*
5144 skip_children (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
)
5148 unsigned int bytes_read
;
5149 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
5153 return info_ptr
+ bytes_read
;
5155 info_ptr
= skip_one_die (reader
, info_ptr
+ bytes_read
, abbrev
);
5159 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5160 INFO_PTR should point just after the initial uleb128 of a DIE, and the
5161 abbrev corresponding to that skipped uleb128 should be passed in
5164 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
5165 returns a pointer to this DIE's sibling, skipping any children.
5166 Otherwise, returns a pointer to the DIE's first child. */
5168 static const gdb_byte
*
5169 skip_one_die (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
,
5170 const struct abbrev_info
*abbrev
, bool do_skip_children
)
5172 unsigned int bytes_read
;
5173 struct attribute attr
;
5174 bfd
*abfd
= reader
->abfd
;
5175 struct dwarf2_cu
*cu
= reader
->cu
;
5176 const gdb_byte
*buffer
= reader
->buffer
;
5177 const gdb_byte
*buffer_end
= reader
->buffer_end
;
5178 unsigned int form
, i
;
5180 if (do_skip_children
&& abbrev
->sibling_offset
!= (unsigned short) -1)
5182 /* We only handle DW_FORM_ref4 here. */
5183 const gdb_byte
*sibling_data
= info_ptr
+ abbrev
->sibling_offset
;
5184 unsigned int offset
= read_4_bytes (abfd
, sibling_data
);
5185 const gdb_byte
*sibling_ptr
5186 = buffer
+ to_underlying (cu
->header
.sect_off
) + offset
;
5187 if (sibling_ptr
>= info_ptr
&& sibling_ptr
< reader
->buffer_end
)
5189 /* Fall through to the slow way. */
5191 else if (abbrev
->size_if_constant
!= 0)
5193 info_ptr
+= abbrev
->size_if_constant
;
5194 if (do_skip_children
&& abbrev
->has_children
)
5195 return skip_children (reader
, info_ptr
);
5199 for (i
= 0; i
< abbrev
->num_attrs
; i
++)
5201 /* The only abbrev we care about is DW_AT_sibling. */
5202 if (do_skip_children
&& abbrev
->attrs
[i
].name
== DW_AT_sibling
)
5204 /* Note there is no need for the extra work of
5205 "reprocessing" here, so we pass false for that
5207 read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
, false);
5208 if (attr
.form
== DW_FORM_ref_addr
)
5209 complaint (_("ignoring absolute DW_AT_sibling"));
5212 sect_offset off
= attr
.get_ref_die_offset ();
5213 const gdb_byte
*sibling_ptr
= buffer
+ to_underlying (off
);
5215 if (sibling_ptr
< info_ptr
)
5216 complaint (_("DW_AT_sibling points backwards"));
5217 else if (sibling_ptr
> reader
->buffer_end
)
5218 reader
->die_section
->overflow_complaint ();
5224 /* If it isn't DW_AT_sibling, skip this attribute. */
5225 form
= abbrev
->attrs
[i
].form
;
5229 case DW_FORM_ref_addr
:
5230 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5231 and later it is offset sized. */
5232 if (cu
->header
.version
== 2)
5233 info_ptr
+= cu
->header
.addr_size
;
5235 info_ptr
+= cu
->header
.offset_size
;
5237 case DW_FORM_GNU_ref_alt
:
5238 info_ptr
+= cu
->header
.offset_size
;
5241 info_ptr
+= cu
->header
.addr_size
;
5249 case DW_FORM_flag_present
:
5250 case DW_FORM_implicit_const
:
5267 case DW_FORM_ref_sig8
:
5270 case DW_FORM_data16
:
5273 case DW_FORM_string
:
5274 read_direct_string (abfd
, info_ptr
, &bytes_read
);
5275 info_ptr
+= bytes_read
;
5277 case DW_FORM_sec_offset
:
5279 case DW_FORM_GNU_strp_alt
:
5280 info_ptr
+= cu
->header
.offset_size
;
5282 case DW_FORM_exprloc
:
5284 info_ptr
+= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
5285 info_ptr
+= bytes_read
;
5287 case DW_FORM_block1
:
5288 info_ptr
+= 1 + read_1_byte (abfd
, info_ptr
);
5290 case DW_FORM_block2
:
5291 info_ptr
+= 2 + read_2_bytes (abfd
, info_ptr
);
5293 case DW_FORM_block4
:
5294 info_ptr
+= 4 + read_4_bytes (abfd
, info_ptr
);
5300 case DW_FORM_ref_udata
:
5301 case DW_FORM_GNU_addr_index
:
5302 case DW_FORM_GNU_str_index
:
5303 case DW_FORM_rnglistx
:
5304 case DW_FORM_loclistx
:
5305 info_ptr
= safe_skip_leb128 (info_ptr
, buffer_end
);
5307 case DW_FORM_indirect
:
5308 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
5309 info_ptr
+= bytes_read
;
5310 /* We need to continue parsing from here, so just go back to
5312 goto skip_attribute
;
5315 error (_("Dwarf Error: Cannot handle %s "
5316 "in DWARF reader [in module %s]"),
5317 dwarf_form_name (form
),
5318 bfd_get_filename (abfd
));
5322 if (do_skip_children
&& abbrev
->has_children
)
5323 return skip_children (reader
, info_ptr
);
5328 /* Reading in full CUs. */
5330 /* Add PER_CU to the queue. */
5333 queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
5334 dwarf2_per_objfile
*per_objfile
,
5335 enum language pretend_language
)
5339 gdb_assert (per_objfile
->queue
.has_value ());
5340 per_objfile
->queue
->emplace (per_cu
, per_objfile
, pretend_language
);
5343 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
5345 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
5348 Return true if maybe_queue_comp_unit requires the caller to load the CU's
5349 DIEs, false otherwise.
5351 Explanation: there is an invariant that if a CU is queued for expansion
5352 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
5353 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
5354 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
5355 are not yet loaded, the the caller must load the CU's DIEs to ensure the
5356 invariant is respected.
5358 The caller is therefore not required to load the CU's DIEs (we return false)
5361 - the CU is already expanded, and therefore does not get enqueued
5362 - the CU gets enqueued for expansion, but its DIEs are already loaded
5364 Note that the caller should not use this function's return value as an
5365 indicator of whether the CU's DIEs are loaded right now, it should check
5366 that by calling `dwarf2_per_objfile::get_cu` instead. */
5369 maybe_queue_comp_unit (struct dwarf2_cu
*dependent_cu
,
5370 dwarf2_per_cu_data
*per_cu
,
5371 dwarf2_per_objfile
*per_objfile
,
5372 enum language pretend_language
)
5374 /* Mark the dependence relation so that we don't flush PER_CU
5376 if (dependent_cu
!= NULL
)
5377 dependent_cu
->add_dependence (per_cu
);
5379 /* If it's already on the queue, we have nothing to do. */
5382 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
5384 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
5386 /* If the CU is queued for expansion, it should not already be
5388 gdb_assert (!per_objfile
->symtab_set_p (per_cu
));
5390 /* The DIEs are already loaded, the caller doesn't need to do it. */
5394 bool queued
= false;
5395 if (!per_objfile
->symtab_set_p (per_cu
))
5397 /* Add it to the queue. */
5398 queue_comp_unit (per_cu
, per_objfile
, pretend_language
);
5402 /* If the compilation unit is already loaded, just mark it as
5404 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
5408 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
5409 and the DIEs are not already loaded. */
5410 return queued
&& cu
== nullptr;
5413 /* Process the queue. */
5416 process_queue (dwarf2_per_objfile
*per_objfile
)
5418 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
5419 objfile_name (per_objfile
->objfile
));
5421 /* The queue starts out with one item, but following a DIE reference
5422 may load a new CU, adding it to the end of the queue. */
5423 while (!per_objfile
->queue
->empty ())
5425 dwarf2_queue_item
&item
= per_objfile
->queue
->front ();
5426 dwarf2_per_cu_data
*per_cu
= item
.per_cu
;
5428 if (!per_objfile
->symtab_set_p (per_cu
))
5430 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
5432 /* Skip dummy CUs. */
5435 unsigned int debug_print_threshold
;
5438 if (per_cu
->is_debug_types
)
5440 struct signatured_type
*sig_type
=
5441 (struct signatured_type
*) per_cu
;
5443 sprintf (buf
, "TU %s at offset %s",
5444 hex_string (sig_type
->signature
),
5445 sect_offset_str (per_cu
->sect_off
));
5446 /* There can be 100s of TUs.
5447 Only print them in verbose mode. */
5448 debug_print_threshold
= 2;
5452 sprintf (buf
, "CU at offset %s",
5453 sect_offset_str (per_cu
->sect_off
));
5454 debug_print_threshold
= 1;
5457 if (dwarf_read_debug
>= debug_print_threshold
)
5458 dwarf_read_debug_printf ("Expanding symtab of %s", buf
);
5460 if (per_cu
->is_debug_types
)
5461 process_full_type_unit (cu
, item
.pretend_language
);
5463 process_full_comp_unit (cu
, item
.pretend_language
);
5465 if (dwarf_read_debug
>= debug_print_threshold
)
5466 dwarf_read_debug_printf ("Done expanding %s", buf
);
5471 per_objfile
->queue
->pop ();
5474 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
5475 objfile_name (per_objfile
->objfile
));
5478 /* Load the DIEs associated with PER_CU into memory.
5480 In some cases, the caller, while reading partial symbols, will need to load
5481 the full symbols for the CU for some reason. It will already have a
5482 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
5483 rather than creating a new one. */
5486 load_full_comp_unit (dwarf2_per_cu_data
*this_cu
,
5487 dwarf2_per_objfile
*per_objfile
,
5488 dwarf2_cu
*existing_cu
,
5490 enum language pretend_language
)
5492 gdb_assert (! this_cu
->is_debug_types
);
5494 cutu_reader
reader (this_cu
, per_objfile
, NULL
, existing_cu
, skip_partial
);
5498 struct dwarf2_cu
*cu
= reader
.cu
;
5499 const gdb_byte
*info_ptr
= reader
.info_ptr
;
5501 gdb_assert (cu
->die_hash
== NULL
);
5503 htab_create_alloc_ex (cu
->header
.get_length_without_initial () / 12,
5507 &cu
->comp_unit_obstack
,
5508 hashtab_obstack_allocate
,
5509 dummy_obstack_deallocate
);
5511 if (reader
.comp_unit_die
->has_children
)
5512 reader
.comp_unit_die
->child
5513 = read_die_and_siblings (&reader
, reader
.info_ptr
,
5514 &info_ptr
, reader
.comp_unit_die
);
5515 cu
->dies
= reader
.comp_unit_die
;
5516 /* comp_unit_die is not stored in die_hash, no need. */
5518 /* We try not to read any attributes in this function, because not
5519 all CUs needed for references have been loaded yet, and symbol
5520 table processing isn't initialized. But we have to set the CU language,
5521 or we won't be able to build types correctly.
5522 Similarly, if we do not read the producer, we can not apply
5523 producer-specific interpretation. */
5524 prepare_one_comp_unit (cu
, cu
->dies
, pretend_language
);
5529 /* Add a DIE to the delayed physname list. */
5532 add_to_method_list (struct type
*type
, int fnfield_index
, int index
,
5533 const char *name
, struct die_info
*die
,
5534 struct dwarf2_cu
*cu
)
5536 struct delayed_method_info mi
;
5538 mi
.fnfield_index
= fnfield_index
;
5542 cu
->method_list
.push_back (mi
);
5545 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
5546 "const" / "volatile". If so, decrements LEN by the length of the
5547 modifier and return true. Otherwise return false. */
5551 check_modifier (const char *physname
, size_t &len
, const char (&mod
)[N
])
5553 size_t mod_len
= sizeof (mod
) - 1;
5554 if (len
> mod_len
&& startswith (physname
+ (len
- mod_len
), mod
))
5562 /* Compute the physnames of any methods on the CU's method list.
5564 The computation of method physnames is delayed in order to avoid the
5565 (bad) condition that one of the method's formal parameters is of an as yet
5569 compute_delayed_physnames (struct dwarf2_cu
*cu
)
5571 /* Only C++ delays computing physnames. */
5572 if (cu
->method_list
.empty ())
5574 gdb_assert (cu
->lang () == language_cplus
);
5576 for (const delayed_method_info
&mi
: cu
->method_list
)
5578 const char *physname
;
5579 struct fn_fieldlist
*fn_flp
5580 = &TYPE_FN_FIELDLIST (mi
.type
, mi
.fnfield_index
);
5581 physname
= dwarf2_physname (mi
.name
, mi
.die
, cu
);
5582 TYPE_FN_FIELD_PHYSNAME (fn_flp
->fn_fields
, mi
.index
)
5583 = physname
? physname
: "";
5585 /* Since there's no tag to indicate whether a method is a
5586 const/volatile overload, extract that information out of the
5588 if (physname
!= NULL
)
5590 size_t len
= strlen (physname
);
5594 if (physname
[len
- 1] == ')') /* shortcut */
5596 else if (check_modifier (physname
, len
, " const"))
5597 TYPE_FN_FIELD_CONST (fn_flp
->fn_fields
, mi
.index
) = 1;
5598 else if (check_modifier (physname
, len
, " volatile"))
5599 TYPE_FN_FIELD_VOLATILE (fn_flp
->fn_fields
, mi
.index
) = 1;
5606 /* The list is no longer needed. */
5607 cu
->method_list
.clear ();
5610 /* Go objects should be embedded in a DW_TAG_module DIE,
5611 and it's not clear if/how imported objects will appear.
5612 To keep Go support simple until that's worked out,
5613 go back through what we've read and create something usable.
5614 We could do this while processing each DIE, and feels kinda cleaner,
5615 but that way is more invasive.
5616 This is to, for example, allow the user to type "p var" or "b main"
5617 without having to specify the package name, and allow lookups
5618 of module.object to work in contexts that use the expression
5622 fixup_go_packaging (struct dwarf2_cu
*cu
)
5624 gdb::unique_xmalloc_ptr
<char> package_name
;
5625 struct pending
*list
;
5628 for (list
= *cu
->get_builder ()->get_global_symbols ();
5632 for (i
= 0; i
< list
->nsyms
; ++i
)
5634 struct symbol
*sym
= list
->symbol
[i
];
5636 if (sym
->language () == language_go
5637 && sym
->aclass () == LOC_BLOCK
)
5639 gdb::unique_xmalloc_ptr
<char> this_package_name
5640 = go_symbol_package_name (sym
);
5642 if (this_package_name
== NULL
)
5644 if (package_name
== NULL
)
5645 package_name
= std::move (this_package_name
);
5648 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
5649 if (strcmp (package_name
.get (), this_package_name
.get ()) != 0)
5650 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
5651 (sym
->symtab () != NULL
5652 ? symtab_to_filename_for_display
5654 : objfile_name (objfile
)),
5655 this_package_name
.get (), package_name
.get ());
5661 if (package_name
!= NULL
)
5663 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
5664 const char *saved_package_name
= objfile
->intern (package_name
.get ());
5666 = type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_MODULE
, 0,
5667 saved_package_name
);
5670 sym
= new (&objfile
->objfile_obstack
) symbol
;
5671 sym
->set_language (language_go
, &objfile
->objfile_obstack
);
5672 sym
->compute_and_set_names (saved_package_name
, false, objfile
->per_bfd
);
5673 sym
->set_domain (TYPE_DOMAIN
);
5674 sym
->set_aclass_index (LOC_TYPEDEF
);
5675 sym
->set_type (type
);
5677 add_symbol_to_list (sym
, cu
->get_builder ()->get_global_symbols ());
5681 /* Allocate a fully-qualified name consisting of the two parts on the
5685 rust_fully_qualify (struct obstack
*obstack
, const char *p1
, const char *p2
)
5687 return obconcat (obstack
, p1
, "::", p2
, (char *) NULL
);
5690 /* A helper that allocates a variant part to attach to a Rust enum
5691 type. OBSTACK is where the results should be allocated. TYPE is
5692 the type we're processing. DISCRIMINANT_INDEX is the index of the
5693 discriminant. It must be the index of one of the fields of TYPE,
5694 or -1 to mean there is no discriminant (univariant enum).
5695 DEFAULT_INDEX is the index of the default field; or -1 if there is
5696 no default. RANGES is indexed by "effective" field number (the
5697 field index, but omitting the discriminant and default fields) and
5698 must hold the discriminant values used by the variants. Note that
5699 RANGES must have a lifetime at least as long as OBSTACK -- either
5700 already allocated on it, or static. */
5703 alloc_rust_variant (struct obstack
*obstack
, struct type
*type
,
5704 int discriminant_index
, int default_index
,
5705 gdb::array_view
<discriminant_range
> ranges
)
5707 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
5708 gdb_assert (discriminant_index
== -1
5709 || (discriminant_index
>= 0
5710 && discriminant_index
< type
->num_fields ()));
5711 gdb_assert (default_index
== -1
5712 || (default_index
>= 0 && default_index
< type
->num_fields ()));
5714 /* We have one variant for each non-discriminant field. */
5715 int n_variants
= type
->num_fields ();
5716 if (discriminant_index
!= -1)
5719 variant
*variants
= new (obstack
) variant
[n_variants
];
5722 for (int i
= 0; i
< type
->num_fields (); ++i
)
5724 if (i
== discriminant_index
)
5727 variants
[var_idx
].first_field
= i
;
5728 variants
[var_idx
].last_field
= i
+ 1;
5730 /* The default field does not need a range, but other fields do.
5731 We skipped the discriminant above. */
5732 if (i
!= default_index
)
5734 variants
[var_idx
].discriminants
= ranges
.slice (range_idx
, 1);
5741 gdb_assert (range_idx
== ranges
.size ());
5742 gdb_assert (var_idx
== n_variants
);
5744 variant_part
*part
= new (obstack
) variant_part
;
5745 part
->discriminant_index
= discriminant_index
;
5746 /* If there is no discriminant, then whether it is signed is of no
5749 = (discriminant_index
== -1
5751 : type
->field (discriminant_index
).type ()->is_unsigned ());
5752 part
->variants
= gdb::array_view
<variant
> (variants
, n_variants
);
5754 void *storage
= obstack_alloc (obstack
, sizeof (gdb::array_view
<variant_part
>));
5755 gdb::array_view
<variant_part
> *prop_value
5756 = new (storage
) gdb::array_view
<variant_part
> (part
, 1);
5758 struct dynamic_prop prop
;
5759 prop
.set_variant_parts (prop_value
);
5761 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
5764 /* Some versions of rustc emitted enums in an unusual way.
5766 Ordinary enums were emitted as unions. The first element of each
5767 structure in the union was named "RUST$ENUM$DISR". This element
5768 held the discriminant.
5770 These versions of Rust also implemented the "non-zero"
5771 optimization. When the enum had two values, and one is empty and
5772 the other holds a pointer that cannot be zero, the pointer is used
5773 as the discriminant, with a zero value meaning the empty variant.
5774 Here, the union's first member is of the form
5775 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
5776 where the fieldnos are the indices of the fields that should be
5777 traversed in order to find the field (which may be several fields deep)
5778 and the variantname is the name of the variant of the case when the
5781 This function recognizes whether TYPE is of one of these forms,
5782 and, if so, smashes it to be a variant type. */
5785 quirk_rust_enum (struct type
*type
, struct objfile
*objfile
)
5787 gdb_assert (type
->code () == TYPE_CODE_UNION
);
5789 /* We don't need to deal with empty enums. */
5790 if (type
->num_fields () == 0)
5793 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
5794 if (type
->num_fields () == 1
5795 && startswith (type
->field (0).name (), RUST_ENUM_PREFIX
))
5797 const char *name
= type
->field (0).name () + strlen (RUST_ENUM_PREFIX
);
5799 /* Decode the field name to find the offset of the
5801 ULONGEST bit_offset
= 0;
5802 struct type
*field_type
= type
->field (0).type ();
5803 while (name
[0] >= '0' && name
[0] <= '9')
5806 unsigned long index
= strtoul (name
, &tail
, 10);
5809 || index
>= field_type
->num_fields ()
5810 || (field_type
->field (index
).loc_kind ()
5811 != FIELD_LOC_KIND_BITPOS
))
5813 complaint (_("Could not parse Rust enum encoding string \"%s\""
5815 type
->field (0).name (),
5816 objfile_name (objfile
));
5821 bit_offset
+= field_type
->field (index
).loc_bitpos ();
5822 field_type
= field_type
->field (index
).type ();
5825 /* Smash this type to be a structure type. We have to do this
5826 because the type has already been recorded. */
5827 type
->set_code (TYPE_CODE_STRUCT
);
5828 /* Save the field we care about. */
5829 struct field saved_field
= type
->field (0);
5830 type
->alloc_fields (3);
5832 /* Put the discriminant at index 0. */
5833 type
->field (0).set_type (field_type
);
5834 type
->field (0).set_is_artificial (true);
5835 type
->field (0).set_name ("<<discriminant>>");
5836 type
->field (0).set_loc_bitpos (bit_offset
);
5838 /* The order of fields doesn't really matter, so put the real
5839 field at index 1 and the data-less field at index 2. */
5840 type
->field (1) = saved_field
;
5841 type
->field (1).set_name
5842 (rust_last_path_segment (type
->field (1).type ()->name ()));
5843 type
->field (1).type ()->set_name
5844 (rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
5845 type
->field (1).name ()));
5847 const char *dataless_name
5848 = rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
5850 struct type
*dataless_type
5851 = type_allocator (type
).new_type (TYPE_CODE_VOID
, 0,
5853 type
->field (2).set_type (dataless_type
);
5854 /* NAME points into the original discriminant name, which
5855 already has the correct lifetime. */
5856 type
->field (2).set_name (name
);
5857 type
->field (2).set_loc_bitpos (0);
5859 /* Indicate that this is a variant type. */
5860 static discriminant_range ranges
[1] = { { 0, 0 } };
5861 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, 1, ranges
);
5863 /* A union with a single anonymous field is probably an old-style
5865 else if (type
->num_fields () == 1 && streq (type
->field (0).name (), ""))
5867 /* Smash this type to be a structure type. We have to do this
5868 because the type has already been recorded. */
5869 type
->set_code (TYPE_CODE_STRUCT
);
5871 struct type
*field_type
= type
->field (0).type ();
5872 const char *variant_name
5873 = rust_last_path_segment (field_type
->name ());
5874 type
->field (0).set_name (variant_name
);
5875 field_type
->set_name
5876 (rust_fully_qualify (&objfile
->objfile_obstack
,
5877 type
->name (), variant_name
));
5879 alloc_rust_variant (&objfile
->objfile_obstack
, type
, -1, 0, {});
5883 struct type
*disr_type
= nullptr;
5884 for (int i
= 0; i
< type
->num_fields (); ++i
)
5886 disr_type
= type
->field (i
).type ();
5888 if (disr_type
->code () != TYPE_CODE_STRUCT
)
5890 /* All fields of a true enum will be structs. */
5893 else if (disr_type
->num_fields () == 0)
5895 /* Could be data-less variant, so keep going. */
5896 disr_type
= nullptr;
5898 else if (strcmp (disr_type
->field (0).name (),
5899 "RUST$ENUM$DISR") != 0)
5901 /* Not a Rust enum. */
5911 /* If we got here without a discriminant, then it's probably
5913 if (disr_type
== nullptr)
5916 /* Smash this type to be a structure type. We have to do this
5917 because the type has already been recorded. */
5918 type
->set_code (TYPE_CODE_STRUCT
);
5920 /* Make space for the discriminant field. */
5921 struct field
*disr_field
= &disr_type
->field (0);
5923 = (struct field
*) TYPE_ZALLOC (type
, ((type
->num_fields () + 1)
5924 * sizeof (struct field
)));
5925 memcpy (new_fields
+ 1, type
->fields (),
5926 type
->num_fields () * sizeof (struct field
));
5927 type
->set_fields (new_fields
);
5928 type
->set_num_fields (type
->num_fields () + 1);
5930 /* Install the discriminant at index 0 in the union. */
5931 type
->field (0) = *disr_field
;
5932 type
->field (0).set_is_artificial (true);
5933 type
->field (0).set_name ("<<discriminant>>");
5935 /* We need a way to find the correct discriminant given a
5936 variant name. For convenience we build a map here. */
5937 struct type
*enum_type
= disr_field
->type ();
5938 std::unordered_map
<std::string_view
, ULONGEST
> discriminant_map
;
5939 for (int i
= 0; i
< enum_type
->num_fields (); ++i
)
5941 if (enum_type
->field (i
).loc_kind () == FIELD_LOC_KIND_ENUMVAL
)
5944 = rust_last_path_segment (enum_type
->field (i
).name ());
5945 discriminant_map
[name
] = enum_type
->field (i
).loc_enumval ();
5949 int n_fields
= type
->num_fields ();
5950 /* We don't need a range entry for the discriminant, but we do
5951 need one for every other field, as there is no default
5953 discriminant_range
*ranges
= XOBNEWVEC (&objfile
->objfile_obstack
,
5956 /* Skip the discriminant here. */
5957 for (int i
= 1; i
< n_fields
; ++i
)
5959 /* Find the final word in the name of this variant's type.
5960 That name can be used to look up the correct
5962 const char *variant_name
5963 = rust_last_path_segment (type
->field (i
).type ()->name ());
5965 auto iter
= discriminant_map
.find (variant_name
);
5966 if (iter
!= discriminant_map
.end ())
5968 ranges
[i
- 1].low
= iter
->second
;
5969 ranges
[i
- 1].high
= iter
->second
;
5972 /* In Rust, each element should have the size of the
5974 type
->field (i
).type ()->set_length (type
->length ());
5976 /* Remove the discriminant field, if it exists. */
5977 struct type
*sub_type
= type
->field (i
).type ();
5978 if (sub_type
->num_fields () > 0)
5980 sub_type
->set_num_fields (sub_type
->num_fields () - 1);
5981 sub_type
->set_fields (sub_type
->fields () + 1);
5983 type
->field (i
).set_name (variant_name
);
5985 (rust_fully_qualify (&objfile
->objfile_obstack
,
5986 type
->name (), variant_name
));
5989 /* Indicate that this is a variant type. */
5990 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, -1,
5991 gdb::array_view
<discriminant_range
> (ranges
,
5996 /* Rewrite some Rust unions to be structures with variants parts. */
5999 rust_union_quirks (struct dwarf2_cu
*cu
)
6001 gdb_assert (cu
->lang () == language_rust
);
6002 for (type
*type_
: cu
->rust_unions
)
6003 quirk_rust_enum (type_
, cu
->per_objfile
->objfile
);
6004 /* We don't need this any more. */
6005 cu
->rust_unions
.clear ();
6010 type_unit_group_unshareable
*
6011 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group
*tu_group
)
6013 auto iter
= this->m_type_units
.find (tu_group
);
6014 if (iter
!= this->m_type_units
.end ())
6015 return iter
->second
.get ();
6017 type_unit_group_unshareable_up
uniq (new type_unit_group_unshareable
);
6018 type_unit_group_unshareable
*result
= uniq
.get ();
6019 this->m_type_units
[tu_group
] = std::move (uniq
);
6024 dwarf2_per_objfile::get_type_for_signatured_type
6025 (signatured_type
*sig_type
) const
6027 auto iter
= this->m_type_map
.find (sig_type
);
6028 if (iter
== this->m_type_map
.end ())
6031 return iter
->second
;
6034 void dwarf2_per_objfile::set_type_for_signatured_type
6035 (signatured_type
*sig_type
, struct type
*type
)
6037 gdb_assert (this->m_type_map
.find (sig_type
) == this->m_type_map
.end ());
6039 this->m_type_map
[sig_type
] = type
;
6042 /* A helper function for computing the list of all symbol tables
6043 included by PER_CU. */
6046 recursively_compute_inclusions (std::vector
<compunit_symtab
*> *result
,
6047 htab_t all_children
, htab_t all_type_symtabs
,
6048 dwarf2_per_cu_data
*per_cu
,
6049 dwarf2_per_objfile
*per_objfile
,
6050 struct compunit_symtab
*immediate_parent
)
6052 void **slot
= htab_find_slot (all_children
, per_cu
, INSERT
);
6055 /* This inclusion and its children have been processed. */
6061 /* Only add a CU if it has a symbol table. */
6062 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
6065 /* If this is a type unit only add its symbol table if we haven't
6066 seen it yet (type unit per_cu's can share symtabs). */
6067 if (per_cu
->is_debug_types
)
6069 slot
= htab_find_slot (all_type_symtabs
, cust
, INSERT
);
6073 result
->push_back (cust
);
6074 if (cust
->user
== NULL
)
6075 cust
->user
= immediate_parent
;
6080 result
->push_back (cust
);
6081 if (cust
->user
== NULL
)
6082 cust
->user
= immediate_parent
;
6086 for (dwarf2_per_cu_data
*ptr
: per_cu
->imported_symtabs
)
6087 recursively_compute_inclusions (result
, all_children
,
6088 all_type_symtabs
, ptr
, per_objfile
,
6092 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
6096 compute_compunit_symtab_includes (dwarf2_per_cu_data
*per_cu
,
6097 dwarf2_per_objfile
*per_objfile
)
6099 gdb_assert (! per_cu
->is_debug_types
);
6101 if (!per_cu
->imported_symtabs
.empty ())
6104 std::vector
<compunit_symtab
*> result_symtabs
;
6105 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
6107 /* If we don't have a symtab, we can just skip this case. */
6111 htab_up
all_children (htab_create_alloc (1, htab_hash_pointer
,
6113 NULL
, xcalloc
, xfree
));
6114 htab_up
all_type_symtabs (htab_create_alloc (1, htab_hash_pointer
,
6116 NULL
, xcalloc
, xfree
));
6118 for (dwarf2_per_cu_data
*ptr
: per_cu
->imported_symtabs
)
6119 recursively_compute_inclusions (&result_symtabs
, all_children
.get (),
6120 all_type_symtabs
.get (), ptr
,
6123 /* Now we have a transitive closure of all the included symtabs. */
6124 len
= result_symtabs
.size ();
6126 = XOBNEWVEC (&per_objfile
->objfile
->objfile_obstack
,
6127 struct compunit_symtab
*, len
+ 1);
6128 memcpy (cust
->includes
, result_symtabs
.data (),
6129 len
* sizeof (compunit_symtab
*));
6130 cust
->includes
[len
] = NULL
;
6134 /* Compute the 'includes' field for the symtabs of all the CUs we just
6138 process_cu_includes (dwarf2_per_objfile
*per_objfile
)
6140 for (dwarf2_per_cu_data
*iter
: per_objfile
->per_bfd
->just_read_cus
)
6142 if (! iter
->is_debug_types
)
6143 compute_compunit_symtab_includes (iter
, per_objfile
);
6146 per_objfile
->per_bfd
->just_read_cus
.clear ();
6149 /* Generate full symbol information for CU, whose DIEs have
6150 already been loaded into memory. */
6153 process_full_comp_unit (dwarf2_cu
*cu
, enum language pretend_language
)
6155 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6156 unrelocated_addr lowpc
, highpc
;
6157 struct compunit_symtab
*cust
;
6158 struct block
*static_block
;
6161 /* Clear the list here in case something was left over. */
6162 cu
->method_list
.clear ();
6164 dwarf2_find_base_address (cu
->dies
, cu
);
6166 /* Before we start reading the top-level DIE, ensure it has a valid tag
6168 switch (cu
->dies
->tag
)
6170 case DW_TAG_compile_unit
:
6171 case DW_TAG_partial_unit
:
6172 case DW_TAG_type_unit
:
6175 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6176 dwarf_tag_name (cu
->dies
->tag
),
6177 sect_offset_str (cu
->per_cu
->sect_off
),
6178 objfile_name (per_objfile
->objfile
));
6181 /* Do line number decoding in read_file_scope () */
6182 process_die (cu
->dies
, cu
);
6184 /* For now fudge the Go package. */
6185 if (cu
->lang () == language_go
)
6186 fixup_go_packaging (cu
);
6188 /* Now that we have processed all the DIEs in the CU, all the types
6189 should be complete, and it should now be safe to compute all of the
6191 compute_delayed_physnames (cu
);
6193 if (cu
->lang () == language_rust
)
6194 rust_union_quirks (cu
);
6196 /* Some compilers don't define a DW_AT_high_pc attribute for the
6197 compilation unit. If the DW_AT_high_pc is missing, synthesize
6198 it, by scanning the DIE's below the compilation unit. */
6199 get_scope_pc_bounds (cu
->dies
, &lowpc
, &highpc
, cu
);
6201 addr
= per_objfile
->relocate (highpc
);
6203 = cu
->get_builder ()->end_compunit_symtab_get_static_block (addr
, 0, 1);
6205 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6206 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6207 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
6208 addrmap to help ensure it has an accurate map of pc values belonging to
6210 dwarf2_record_block_ranges (cu
->dies
, static_block
, cu
);
6212 cust
= cu
->get_builder ()->end_compunit_symtab_from_static_block
6217 int gcc_4_minor
= producer_is_gcc_ge_4 (cu
->producer
);
6219 /* Set symtab language to language from DW_AT_language. If the
6220 compilation is from a C file generated by language preprocessors, do
6221 not set the language if it was already deduced by start_subfile. */
6222 if (!(cu
->lang () == language_c
6223 && cust
->primary_filetab ()->language () != language_unknown
))
6224 cust
->primary_filetab ()->set_language (cu
->lang ());
6226 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6227 produce DW_AT_location with location lists but it can be possibly
6228 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6229 there were bugs in prologue debug info, fixed later in GCC-4.5
6230 by "unwind info for epilogues" patch (which is not directly related).
6232 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6233 needed, it would be wrong due to missing DW_AT_producer there.
6235 Still one can confuse GDB by using non-standard GCC compilation
6236 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6238 if (cu
->has_loclist
&& gcc_4_minor
>= 5)
6239 cust
->set_locations_valid (true);
6242 if (cu
->producer
!= nullptr
6243 && producer_is_gcc (cu
->producer
, &major
, &minor
)
6244 && (major
< 4 || (major
== 4 && minor
< 5)))
6245 /* Don't trust gcc < 4.5.x. */
6246 cust
->set_epilogue_unwind_valid (false);
6248 cust
->set_epilogue_unwind_valid (true);
6250 cust
->set_call_site_htab (cu
->call_site_htab
);
6253 per_objfile
->set_symtab (cu
->per_cu
, cust
);
6255 /* Push it for inclusion processing later. */
6256 per_objfile
->per_bfd
->just_read_cus
.push_back (cu
->per_cu
);
6258 /* Not needed any more. */
6259 cu
->reset_builder ();
6262 /* Generate full symbol information for type unit CU, whose DIEs have
6263 already been loaded into memory. */
6266 process_full_type_unit (dwarf2_cu
*cu
,
6267 enum language pretend_language
)
6269 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6270 struct compunit_symtab
*cust
;
6271 struct signatured_type
*sig_type
;
6273 gdb_assert (cu
->per_cu
->is_debug_types
);
6274 sig_type
= (struct signatured_type
*) cu
->per_cu
;
6276 /* Clear the list here in case something was left over. */
6277 cu
->method_list
.clear ();
6279 /* The symbol tables are set up in read_type_unit_scope. */
6280 process_die (cu
->dies
, cu
);
6282 /* For now fudge the Go package. */
6283 if (cu
->lang () == language_go
)
6284 fixup_go_packaging (cu
);
6286 /* Now that we have processed all the DIEs in the CU, all the types
6287 should be complete, and it should now be safe to compute all of the
6289 compute_delayed_physnames (cu
);
6291 if (cu
->lang () == language_rust
)
6292 rust_union_quirks (cu
);
6294 /* TUs share symbol tables.
6295 If this is the first TU to use this symtab, complete the construction
6296 of it with end_expandable_symtab. Otherwise, complete the addition of
6297 this TU's symbols to the existing symtab. */
6298 type_unit_group_unshareable
*tug_unshare
=
6299 per_objfile
->get_type_unit_group_unshareable (sig_type
->type_unit_group
);
6300 if (tug_unshare
->compunit_symtab
== NULL
)
6302 buildsym_compunit
*builder
= cu
->get_builder ();
6303 cust
= builder
->end_expandable_symtab (0);
6304 tug_unshare
->compunit_symtab
= cust
;
6308 /* Set symtab language to language from DW_AT_language. If the
6309 compilation is from a C file generated by language preprocessors,
6310 do not set the language if it was already deduced by
6312 if (!(cu
->lang () == language_c
6313 && cust
->primary_filetab ()->language () != language_c
))
6314 cust
->primary_filetab ()->set_language (cu
->lang ());
6319 cu
->get_builder ()->augment_type_symtab ();
6320 cust
= tug_unshare
->compunit_symtab
;
6323 per_objfile
->set_symtab (cu
->per_cu
, cust
);
6325 /* Not needed any more. */
6326 cu
->reset_builder ();
6329 /* Process an imported unit DIE. */
6332 process_imported_unit_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
6334 struct attribute
*attr
;
6336 /* For now we don't handle imported units in type units. */
6337 if (cu
->per_cu
->is_debug_types
)
6339 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6340 " supported in type units [in module %s]"),
6341 objfile_name (cu
->per_objfile
->objfile
));
6344 attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
6347 sect_offset sect_off
= attr
->get_ref_die_offset ();
6348 bool is_dwz
= (attr
->form
== DW_FORM_GNU_ref_alt
|| cu
->per_cu
->is_dwz
);
6349 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6350 dwarf2_per_cu_data
*per_cu
6351 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
6352 per_objfile
->per_bfd
);
6354 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
6355 into another compilation unit, at root level. Regard this as a hint,
6356 and ignore it. This is a best effort, it only works if unit_type and
6357 lang are already set. */
6358 if (die
->parent
&& die
->parent
->parent
== NULL
6359 && per_cu
->unit_type (false) == DW_UT_compile
6360 && per_cu
->lang (false) == language_cplus
)
6363 /* If necessary, add it to the queue and load its DIEs. */
6364 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
6366 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
6367 false, cu
->lang ());
6369 cu
->per_cu
->imported_symtabs
.push_back (per_cu
);
6373 /* RAII object that represents a process_die scope: i.e.,
6374 starts/finishes processing a DIE. */
6375 class process_die_scope
6378 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
6379 : m_die (die
), m_cu (cu
)
6381 /* We should only be processing DIEs not already in process. */
6382 gdb_assert (!m_die
->in_process
);
6383 m_die
->in_process
= true;
6386 ~process_die_scope ()
6388 m_die
->in_process
= false;
6390 /* If we're done processing the DIE for the CU that owns the line
6391 header, we don't need the line header anymore. */
6392 if (m_cu
->line_header_die_owner
== m_die
)
6394 delete m_cu
->line_header
;
6395 m_cu
->line_header
= NULL
;
6396 m_cu
->line_header_die_owner
= NULL
;
6405 /* Process a die and its children. */
6408 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
6410 process_die_scope
scope (die
, cu
);
6414 case DW_TAG_padding
:
6416 case DW_TAG_compile_unit
:
6417 case DW_TAG_partial_unit
:
6418 read_file_scope (die
, cu
);
6420 case DW_TAG_type_unit
:
6421 read_type_unit_scope (die
, cu
);
6423 case DW_TAG_subprogram
:
6424 /* Nested subprograms in Fortran get a prefix. */
6425 if (cu
->lang () == language_fortran
6426 && die
->parent
!= NULL
6427 && die
->parent
->tag
== DW_TAG_subprogram
)
6428 cu
->processing_has_namespace_info
= true;
6431 case DW_TAG_entry_point
:
6432 case DW_TAG_inlined_subroutine
:
6433 read_func_scope (die
, cu
);
6435 case DW_TAG_lexical_block
:
6436 case DW_TAG_try_block
:
6437 case DW_TAG_catch_block
:
6438 read_lexical_block_scope (die
, cu
);
6440 case DW_TAG_call_site
:
6441 case DW_TAG_GNU_call_site
:
6442 read_call_site_scope (die
, cu
);
6444 case DW_TAG_class_type
:
6445 case DW_TAG_interface_type
:
6446 case DW_TAG_structure_type
:
6447 case DW_TAG_union_type
:
6448 case DW_TAG_namelist
:
6449 process_structure_scope (die
, cu
);
6451 case DW_TAG_enumeration_type
:
6452 process_enumeration_scope (die
, cu
);
6455 /* These dies have a type, but processing them does not create
6456 a symbol or recurse to process the children. Therefore we can
6457 read them on-demand through read_type_die. */
6458 case DW_TAG_subroutine_type
:
6459 case DW_TAG_set_type
:
6460 case DW_TAG_pointer_type
:
6461 case DW_TAG_ptr_to_member_type
:
6462 case DW_TAG_reference_type
:
6463 case DW_TAG_rvalue_reference_type
:
6464 case DW_TAG_string_type
:
6467 case DW_TAG_array_type
:
6468 /* We only need to handle this case for Ada -- in other
6469 languages, it's normal for the compiler to emit a typedef
6471 if (cu
->lang () != language_ada
)
6474 case DW_TAG_base_type
:
6475 case DW_TAG_subrange_type
:
6476 case DW_TAG_generic_subrange
:
6477 case DW_TAG_typedef
:
6478 case DW_TAG_unspecified_type
:
6479 /* Add a typedef symbol for the type definition, if it has a
6481 new_symbol (die
, read_type_die (die
, cu
), cu
);
6483 case DW_TAG_common_block
:
6484 read_common_block (die
, cu
);
6486 case DW_TAG_common_inclusion
:
6488 case DW_TAG_namespace
:
6489 cu
->processing_has_namespace_info
= true;
6490 read_namespace (die
, cu
);
6493 cu
->processing_has_namespace_info
= true;
6494 read_module (die
, cu
);
6496 case DW_TAG_imported_declaration
:
6497 cu
->processing_has_namespace_info
= true;
6498 if (read_alias (die
, cu
))
6500 /* The declaration is neither a global namespace nor a variable
6503 case DW_TAG_imported_module
:
6504 cu
->processing_has_namespace_info
= true;
6505 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
6506 || cu
->lang () != language_fortran
))
6507 complaint (_("Tag '%s' has unexpected children"),
6508 dwarf_tag_name (die
->tag
));
6509 read_import_statement (die
, cu
);
6512 case DW_TAG_imported_unit
:
6513 process_imported_unit_die (die
, cu
);
6516 case DW_TAG_variable
:
6517 read_variable (die
, cu
);
6521 new_symbol (die
, NULL
, cu
);
6526 /* DWARF name computation. */
6528 /* A helper function for dwarf2_compute_name which determines whether DIE
6529 needs to have the name of the scope prepended to the name listed in the
6533 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
6535 struct attribute
*attr
;
6539 case DW_TAG_namespace
:
6540 case DW_TAG_typedef
:
6541 case DW_TAG_class_type
:
6542 case DW_TAG_interface_type
:
6543 case DW_TAG_structure_type
:
6544 case DW_TAG_union_type
:
6545 case DW_TAG_enumeration_type
:
6546 case DW_TAG_enumerator
:
6547 case DW_TAG_subprogram
:
6548 case DW_TAG_inlined_subroutine
:
6549 case DW_TAG_entry_point
:
6551 case DW_TAG_imported_declaration
:
6554 case DW_TAG_variable
:
6555 case DW_TAG_constant
:
6556 /* We only need to prefix "globally" visible variables. These include
6557 any variable marked with DW_AT_external or any variable that
6558 lives in a namespace. [Variables in anonymous namespaces
6559 require prefixing, but they are not DW_AT_external.] */
6561 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
6563 struct dwarf2_cu
*spec_cu
= cu
;
6565 return die_needs_namespace (die_specification (die
, &spec_cu
),
6569 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
6570 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
6571 && die
->parent
->tag
!= DW_TAG_module
)
6573 /* A variable in a lexical block of some kind does not need a
6574 namespace, even though in C++ such variables may be external
6575 and have a mangled name. */
6576 if (die
->parent
->tag
== DW_TAG_lexical_block
6577 || die
->parent
->tag
== DW_TAG_try_block
6578 || die
->parent
->tag
== DW_TAG_catch_block
6579 || die
->parent
->tag
== DW_TAG_subprogram
)
6588 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
6589 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6590 defined for the given DIE. */
6592 static struct attribute
*
6593 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
6595 struct attribute
*attr
;
6597 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
6599 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
6604 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
6605 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
6606 defined for the given DIE. */
6609 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
6611 const char *linkage_name
;
6613 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
6614 if (linkage_name
== NULL
)
6615 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
6617 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
6618 See https://github.com/rust-lang/rust/issues/32925. */
6619 if (cu
->lang () == language_rust
&& linkage_name
!= NULL
6620 && strchr (linkage_name
, '{') != NULL
)
6621 linkage_name
= NULL
;
6623 return linkage_name
;
6626 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
6627 compute the physname for the object, which include a method's:
6628 - formal parameters (C++),
6629 - receiver type (Go),
6631 The term "physname" is a bit confusing.
6632 For C++, for example, it is the demangled name.
6633 For Go, for example, it's the mangled name.
6635 For Ada, return the DIE's linkage name rather than the fully qualified
6636 name. PHYSNAME is ignored..
6638 The result is allocated on the objfile->per_bfd's obstack and
6642 dwarf2_compute_name (const char *name
,
6643 struct die_info
*die
, struct dwarf2_cu
*cu
,
6646 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
6649 name
= dwarf2_name (die
, cu
);
6651 enum language lang
= cu
->lang ();
6653 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
6654 but otherwise compute it by typename_concat inside GDB.
6655 FIXME: Actually this is not really true, or at least not always true.
6656 It's all very confusing. compute_and_set_names doesn't try to demangle
6657 Fortran names because there is no mangling standard. So new_symbol
6658 will set the demangled name to the result of dwarf2_full_name, and it is
6659 the demangled name that GDB uses if it exists. */
6660 if (lang
== language_ada
6661 || (lang
== language_fortran
&& physname
))
6663 /* For Ada unit, we prefer the linkage name over the name, as
6664 the former contains the exported name, which the user expects
6665 to be able to reference. Ideally, we want the user to be able
6666 to reference this entity using either natural or linkage name,
6667 but we haven't started looking at this enhancement yet. */
6668 const char *linkage_name
= dw2_linkage_name (die
, cu
);
6670 if (linkage_name
!= NULL
)
6671 return linkage_name
;
6674 /* These are the only languages we know how to qualify names in. */
6676 && (lang
== language_cplus
6677 || lang
== language_fortran
|| lang
== language_d
6678 || lang
== language_rust
))
6680 if (die_needs_namespace (die
, cu
))
6686 prefix
= determine_prefix (die
, cu
);
6687 if (*prefix
!= '\0')
6689 gdb::unique_xmalloc_ptr
<char> prefixed_name
6690 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
6692 buf
.puts (prefixed_name
.get ());
6697 /* Template parameters may be specified in the DIE's DW_AT_name, or
6698 as children with DW_TAG_template_type_param or
6699 DW_TAG_value_type_param. If the latter, add them to the name
6700 here. If the name already has template parameters, then
6701 skip this step; some versions of GCC emit both, and
6702 it is more efficient to use the pre-computed name.
6704 Something to keep in mind about this process: it is very
6705 unlikely, or in some cases downright impossible, to produce
6706 something that will match the mangled name of a function.
6707 If the definition of the function has the same debug info,
6708 we should be able to match up with it anyway. But fallbacks
6709 using the minimal symbol, for instance to find a method
6710 implemented in a stripped copy of libstdc++, will not work.
6711 If we do not have debug info for the definition, we will have to
6712 match them up some other way.
6714 When we do name matching there is a related problem with function
6715 templates; two instantiated function templates are allowed to
6716 differ only by their return types, which we do not add here. */
6718 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
6720 struct attribute
*attr
;
6721 struct die_info
*child
;
6724 die
->building_fullname
= 1;
6726 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
6730 const gdb_byte
*bytes
;
6731 struct dwarf2_locexpr_baton
*baton
;
6734 if (child
->tag
!= DW_TAG_template_type_param
6735 && child
->tag
!= DW_TAG_template_value_param
)
6746 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
6749 complaint (_("template parameter missing DW_AT_type"));
6750 buf
.puts ("UNKNOWN_TYPE");
6753 type
= die_type (child
, cu
);
6755 if (child
->tag
== DW_TAG_template_type_param
)
6757 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
6758 &type_print_raw_options
);
6762 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
6765 complaint (_("template parameter missing "
6766 "DW_AT_const_value"));
6767 buf
.puts ("UNKNOWN_VALUE");
6771 dwarf2_const_value_attr (attr
, type
, name
,
6772 &cu
->comp_unit_obstack
, cu
,
6773 &value
, &bytes
, &baton
);
6775 if (type
->has_no_signedness ())
6776 /* GDB prints characters as NUMBER 'CHAR'. If that's
6777 changed, this can use value_print instead. */
6778 cu
->language_defn
->printchar (value
, type
, &buf
);
6781 struct value_print_options opts
;
6784 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
6788 baton
->per_objfile
);
6789 else if (bytes
!= NULL
)
6791 v
= value::allocate (type
);
6792 memcpy (v
->contents_writeable ().data (), bytes
,
6796 v
= value_from_longest (type
, value
);
6798 /* Specify decimal so that we do not depend on
6800 get_formatted_print_options (&opts
, 'd');
6802 value_print (v
, &buf
, &opts
);
6807 die
->building_fullname
= 0;
6811 /* Close the argument list, with a space if necessary
6812 (nested templates). */
6813 if (!buf
.empty () && buf
.string ().back () == '>')
6820 /* For C++ methods, append formal parameter type
6821 information, if PHYSNAME. */
6823 if (physname
&& die
->tag
== DW_TAG_subprogram
6824 && lang
== language_cplus
)
6826 struct type
*type
= read_type_die (die
, cu
);
6828 c_type_print_args (type
, &buf
, 1, lang
,
6829 &type_print_raw_options
);
6831 if (lang
== language_cplus
)
6833 /* Assume that an artificial first parameter is
6834 "this", but do not crash if it is not. RealView
6835 marks unnamed (and thus unused) parameters as
6836 artificial; there is no way to differentiate
6838 if (type
->num_fields () > 0
6839 && type
->field (0).is_artificial ()
6840 && type
->field (0).type ()->code () == TYPE_CODE_PTR
6841 && TYPE_CONST (type
->field (0).type ()->target_type ()))
6842 buf
.puts (" const");
6846 const std::string
&intermediate_name
= buf
.string ();
6848 const char *canonical_name
6849 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
6852 /* If we only computed INTERMEDIATE_NAME, or if
6853 INTERMEDIATE_NAME is already canonical, then we need to
6855 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
6856 name
= objfile
->intern (intermediate_name
);
6858 name
= canonical_name
;
6865 /* Return the fully qualified name of DIE, based on its DW_AT_name.
6866 If scope qualifiers are appropriate they will be added. The result
6867 will be allocated on the storage_obstack, or NULL if the DIE does
6868 not have a name. NAME may either be from a previous call to
6869 dwarf2_name or NULL.
6871 The output string will be canonicalized (if C++). */
6874 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
6876 return dwarf2_compute_name (name
, die
, cu
, 0);
6879 /* Construct a physname for the given DIE in CU. NAME may either be
6880 from a previous call to dwarf2_name or NULL. The result will be
6881 allocated on the objfile_obstack or NULL if the DIE does not have a
6884 The output string will be canonicalized (if C++). */
6887 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
6889 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
6890 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
6893 /* In this case dwarf2_compute_name is just a shortcut not building anything
6895 if (!die_needs_namespace (die
, cu
))
6896 return dwarf2_compute_name (name
, die
, cu
, 1);
6898 if (cu
->lang () != language_rust
)
6899 mangled
= dw2_linkage_name (die
, cu
);
6901 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6903 gdb::unique_xmalloc_ptr
<char> demangled
;
6904 if (mangled
!= NULL
)
6906 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
6908 /* Do nothing (do not demangle the symbol name). */
6912 /* Use DMGL_RET_DROP for C++ template functions to suppress
6913 their return type. It is easier for GDB users to search
6914 for such functions as `name(params)' than `long name(params)'.
6915 In such case the minimal symbol names do not match the full
6916 symbol names but for template functions there is never a need
6917 to look up their definition from their declaration so
6918 the only disadvantage remains the minimal symbol variant
6919 `long name(params)' does not have the proper inferior type. */
6920 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
6924 canon
= demangled
.get ();
6932 if (canon
== NULL
|| check_physname
)
6934 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
6936 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
6938 /* It may not mean a bug in GDB. The compiler could also
6939 compute DW_AT_linkage_name incorrectly. But in such case
6940 GDB would need to be bug-to-bug compatible. */
6942 complaint (_("Computed physname <%s> does not match demangled <%s> "
6943 "(from linkage <%s>) - DIE at %s [in module %s]"),
6944 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
6945 objfile_name (objfile
));
6947 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6948 is available here - over computed PHYSNAME. It is safer
6949 against both buggy GDB and buggy compilers. */
6963 retval
= objfile
->intern (retval
);
6968 /* Inspect DIE in CU for a namespace alias or a variable with alias
6969 attribute. If one exists, record a new symbol for it.
6971 Returns true if an alias was recorded, false otherwise. */
6974 read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
6976 struct attribute
*attr
;
6978 /* If the die does not have a name, this is neither a namespace
6979 alias nor a variable alias. */
6980 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
6984 struct die_info
*d
= die
;
6985 struct dwarf2_cu
*imported_cu
= cu
;
6987 /* If the compiler has nested DW_AT_imported_declaration DIEs,
6988 keep inspecting DIEs until we hit the underlying import. */
6989 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
6990 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
6992 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
6996 d
= follow_die_ref (d
, attr
, &imported_cu
);
6997 if (d
->tag
!= DW_TAG_imported_declaration
)
7001 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
7003 complaint (_("DIE at %s has too many recursively imported "
7004 "declarations"), sect_offset_str (d
->sect_off
));
7011 if (d
->tag
== DW_TAG_variable
)
7013 /* This declaration is a C/C++ global variable alias.
7014 Add a symbol for it whose type is the same as the
7015 aliased variable's. */
7016 type
= die_type (d
, imported_cu
);
7017 struct symbol
*sym
= new_symbol (die
, type
, cu
);
7018 attr
= dwarf2_attr (d
, DW_AT_location
, imported_cu
);
7019 sym
->set_aclass_index (LOC_UNRESOLVED
);
7020 if (attr
!= nullptr)
7021 var_decode_location (attr
, sym
, cu
);
7026 sect_offset sect_off
= attr
->get_ref_die_offset ();
7027 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
,
7029 if (type
!= nullptr && type
->code () == TYPE_CODE_NAMESPACE
)
7031 /* This declaration is a global namespace alias. Add
7032 a symbol for it whose type is the aliased
7034 new_symbol (die
, type
, cu
);
7043 /* Return the using directives repository (global or local?) to use in the
7044 current context for CU.
7046 For Ada, imported declarations can materialize renamings, which *may* be
7047 global. However it is impossible (for now?) in DWARF to distinguish
7048 "external" imported declarations and "static" ones. As all imported
7049 declarations seem to be static in all other languages, make them all CU-wide
7050 global only in Ada. */
7052 static struct using_direct
**
7053 using_directives (struct dwarf2_cu
*cu
)
7055 if (cu
->lang () == language_ada
7056 && cu
->get_builder ()->outermost_context_p ())
7057 return cu
->get_builder ()->get_global_using_directives ();
7059 return cu
->get_builder ()->get_local_using_directives ();
7062 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
7063 given CU. If the format is not recognized or the attribute is
7064 not present, set it to 0. */
7067 read_decl_line (struct die_info
*die
, struct dwarf2_cu
*cu
)
7069 struct attribute
*decl_line
= dwarf2_attr (die
, DW_AT_decl_line
, cu
);
7070 if (decl_line
== nullptr)
7072 if (decl_line
->form_is_constant ())
7074 LONGEST val
= decl_line
->constant_value (0);
7075 if (0 <= val
&& val
<= UINT_MAX
)
7076 return (unsigned int) val
;
7078 complaint (_("Declared line for using directive is too large"));
7082 complaint (_("Declared line for using directive is of incorrect format"));
7086 /* Read the import statement specified by the given die and record it. */
7089 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
7091 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7092 struct attribute
*import_attr
;
7093 struct die_info
*imported_die
, *child_die
;
7094 struct dwarf2_cu
*imported_cu
;
7095 const char *imported_name
;
7096 const char *imported_name_prefix
;
7097 const char *canonical_name
;
7098 const char *import_alias
;
7099 const char *imported_declaration
= NULL
;
7100 const char *import_prefix
;
7101 std::vector
<const char *> excludes
;
7103 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
7104 if (import_attr
== NULL
)
7106 complaint (_("Tag '%s' has no DW_AT_import"),
7107 dwarf_tag_name (die
->tag
));
7112 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
7113 imported_name
= dwarf2_name (imported_die
, imported_cu
);
7114 if (imported_name
== NULL
)
7116 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7118 The import in the following code:
7132 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7133 <52> DW_AT_decl_file : 1
7134 <53> DW_AT_decl_line : 6
7135 <54> DW_AT_import : <0x75>
7136 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7138 <5b> DW_AT_decl_file : 1
7139 <5c> DW_AT_decl_line : 2
7140 <5d> DW_AT_type : <0x6e>
7142 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7143 <76> DW_AT_byte_size : 4
7144 <77> DW_AT_encoding : 5 (signed)
7146 imports the wrong die ( 0x75 instead of 0x58 ).
7147 This case will be ignored until the gcc bug is fixed. */
7151 /* Figure out the local name after import. */
7152 import_alias
= dwarf2_name (die
, cu
);
7154 /* Figure out where the statement is being imported to. */
7155 import_prefix
= determine_prefix (die
, cu
);
7157 /* Figure out what the scope of the imported die is and prepend it
7158 to the name of the imported die. */
7159 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
7161 if (imported_die
->tag
!= DW_TAG_namespace
7162 && imported_die
->tag
!= DW_TAG_module
)
7164 imported_declaration
= imported_name
;
7165 canonical_name
= imported_name_prefix
;
7167 else if (strlen (imported_name_prefix
) > 0)
7168 canonical_name
= obconcat (&objfile
->objfile_obstack
,
7169 imported_name_prefix
,
7170 (cu
->lang () == language_d
7173 imported_name
, (char *) NULL
);
7175 canonical_name
= imported_name
;
7177 if (die
->tag
== DW_TAG_imported_module
7178 && cu
->lang () == language_fortran
)
7179 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
7180 child_die
= child_die
->sibling
)
7182 /* DWARF-4: A Fortran use statement with a “rename list” may be
7183 represented by an imported module entry with an import attribute
7184 referring to the module and owned entries corresponding to those
7185 entities that are renamed as part of being imported. */
7187 if (child_die
->tag
!= DW_TAG_imported_declaration
)
7189 complaint (_("child DW_TAG_imported_declaration expected "
7190 "- DIE at %s [in module %s]"),
7191 sect_offset_str (child_die
->sect_off
),
7192 objfile_name (objfile
));
7196 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
7197 if (import_attr
== NULL
)
7199 complaint (_("Tag '%s' has no DW_AT_import"),
7200 dwarf_tag_name (child_die
->tag
));
7205 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
7207 imported_name
= dwarf2_name (imported_die
, imported_cu
);
7208 if (imported_name
== NULL
)
7210 complaint (_("child DW_TAG_imported_declaration has unknown "
7211 "imported name - DIE at %s [in module %s]"),
7212 sect_offset_str (child_die
->sect_off
),
7213 objfile_name (objfile
));
7217 excludes
.push_back (imported_name
);
7219 process_die (child_die
, cu
);
7222 add_using_directive (using_directives (cu
),
7226 imported_declaration
,
7228 read_decl_line (die
, cu
),
7229 &objfile
->objfile_obstack
);
7232 /* ICC<14 does not output the required DW_AT_declaration on incomplete
7233 types, but gives them a size of zero. Starting with version 14,
7234 ICC is compatible with GCC. */
7237 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
7239 if (!cu
->checked_producer
)
7240 check_producer (cu
);
7242 return cu
->producer_is_icc_lt_14
;
7245 /* ICC generates a DW_AT_type for C void functions. This was observed on
7246 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
7247 which says that void functions should not have a DW_AT_type. */
7250 producer_is_icc (struct dwarf2_cu
*cu
)
7252 if (!cu
->checked_producer
)
7253 check_producer (cu
);
7255 return cu
->producer_is_icc
;
7258 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7259 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7260 this, it was first present in GCC release 4.3.0. */
7263 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
7265 if (!cu
->checked_producer
)
7266 check_producer (cu
);
7268 return cu
->producer_is_gcc_lt_4_3
;
7271 /* See dwarf2/read.h. */
7273 producer_is_clang (struct dwarf2_cu
*cu
)
7275 if (!cu
->checked_producer
)
7276 check_producer (cu
);
7278 return cu
->producer_is_clang
;
7281 static file_and_directory
&
7282 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
7284 if (cu
->per_cu
->fnd
!= nullptr)
7285 return *cu
->per_cu
->fnd
;
7287 /* Find the filename. Do not use dwarf2_name here, since the filename
7288 is not a source language identifier. */
7289 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
7290 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
7292 if (res
.get_comp_dir () == nullptr
7293 && producer_is_gcc_lt_4_3 (cu
)
7294 && res
.get_name () != nullptr
7295 && IS_ABSOLUTE_PATH (res
.get_name ()))
7297 res
.set_comp_dir (ldirname (res
.get_name ()));
7298 res
.set_name (make_unique_xstrdup (lbasename (res
.get_name ())));
7301 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
7302 return *cu
->per_cu
->fnd
;
7305 /* Handle DW_AT_stmt_list for a compilation unit.
7306 DIE is the DW_TAG_compile_unit die for CU.
7307 COMP_DIR is the compilation directory. LOWPC is passed to
7308 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
7311 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
7312 const file_and_directory
&fnd
, unrelocated_addr lowpc
,
7313 bool have_code
) /* ARI: editCase function */
7315 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
7316 struct attribute
*attr
;
7317 hashval_t line_header_local_hash
;
7321 gdb_assert (! cu
->per_cu
->is_debug_types
);
7323 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
7324 if (attr
== NULL
|| !attr
->form_is_unsigned ())
7327 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
7329 /* The line header hash table is only created if needed (it exists to
7330 prevent redundant reading of the line table for partial_units).
7331 If we're given a partial_unit, we'll need it. If we're given a
7332 compile_unit, then use the line header hash table if it's already
7333 created, but don't create one just yet. */
7335 if (per_objfile
->line_header_hash
== NULL
7336 && die
->tag
== DW_TAG_partial_unit
)
7338 per_objfile
->line_header_hash
7339 .reset (htab_create_alloc (127, line_header_hash_voidp
,
7340 line_header_eq_voidp
,
7341 htab_delete_entry
<line_header
>,
7345 line_header
line_header_local (line_offset
, cu
->per_cu
->is_dwz
);
7346 line_header_local_hash
= line_header_hash (&line_header_local
);
7347 if (per_objfile
->line_header_hash
!= NULL
)
7349 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
7351 line_header_local_hash
, NO_INSERT
);
7353 /* For DW_TAG_compile_unit we need info like symtab::linetable which
7354 is not present in *SLOT (since if there is something in *SLOT then
7355 it will be for a partial_unit). */
7356 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
7358 gdb_assert (*slot
!= NULL
);
7359 cu
->line_header
= (struct line_header
*) *slot
;
7364 /* dwarf_decode_line_header does not yet provide sufficient information.
7365 We always have to call also dwarf_decode_lines for it. */
7366 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
,
7367 fnd
.get_comp_dir ());
7371 cu
->line_header
= lh
.release ();
7372 cu
->line_header_die_owner
= die
;
7374 if (per_objfile
->line_header_hash
== NULL
)
7378 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
7380 line_header_local_hash
, INSERT
);
7381 gdb_assert (slot
!= NULL
);
7383 if (slot
!= NULL
&& *slot
== NULL
)
7385 /* This newly decoded line number information unit will be owned
7386 by line_header_hash hash table. */
7387 *slot
= cu
->line_header
;
7388 cu
->line_header_die_owner
= NULL
;
7392 /* We cannot free any current entry in (*slot) as that struct line_header
7393 may be already used by multiple CUs. Create only temporary decoded
7394 line_header for this CU - it may happen at most once for each line
7395 number information unit. And if we're not using line_header_hash
7396 then this is what we want as well. */
7397 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
7399 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
7400 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
7401 then there won't be any interesting code in the CU, but a check later on
7402 (in lnp_state_machine::check_line_address) will fail to properly exclude
7403 an entry that was removed via --gc-sections. */
7404 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
&& have_code
);
7407 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
7410 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
7412 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
7413 struct objfile
*objfile
= per_objfile
->objfile
;
7415 struct attribute
*attr
;
7416 struct die_info
*child_die
;
7418 prepare_one_comp_unit (cu
, die
, cu
->lang ());
7420 unrelocated_addr unrel_low
, unrel_high
;
7421 get_scope_pc_bounds (die
, &unrel_low
, &unrel_high
, cu
);
7423 /* If we didn't find a lowpc, set it to highpc to avoid complaints
7424 from finish_block. */
7425 if (unrel_low
== ((unrelocated_addr
) -1))
7426 unrel_low
= unrel_high
;
7427 lowpc
= per_objfile
->relocate (unrel_low
);
7429 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
7431 /* GAS supports generating dwarf-5 info starting version 2.35. Versions
7432 2.35-2.37 generate an incorrect CU name attribute: it's relative,
7433 implicitly prefixing it with the compilation dir. Work around this by
7434 prefixing it with the source dir instead. */
7435 if (cu
->header
.version
== 5 && !IS_ABSOLUTE_PATH (fnd
.get_name ())
7436 && producer_is_gas_lt_2_38 (cu
))
7438 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
7439 if (attr
!= nullptr && attr
->form_is_unsigned ())
7441 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
7442 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
,
7443 fnd
.get_comp_dir ());
7444 if (lh
->version
== 5 && lh
->is_valid_file_index (1))
7446 std::string dir
= lh
->include_dir_at (1);
7447 fnd
.set_comp_dir (std::move (dir
));
7452 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
7455 gdb_assert (per_objfile
->sym_cu
== nullptr);
7456 scoped_restore restore_sym_cu
7457 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
7459 /* Decode line number information if present. We do this before
7460 processing child DIEs, so that the line header table is available
7461 for DW_AT_decl_file. */
7462 handle_DW_AT_stmt_list (die
, cu
, fnd
, unrel_low
, unrel_low
!= unrel_high
);
7464 /* Process all dies in compilation unit. */
7465 if (die
->child
!= NULL
)
7467 child_die
= die
->child
;
7468 while (child_die
&& child_die
->tag
)
7470 process_die (child_die
, cu
);
7471 child_die
= child_die
->sibling
;
7474 per_objfile
->sym_cu
= nullptr;
7476 /* Decode macro information, if present. Dwarf 2 macro information
7477 refers to information in the line number info statement program
7478 header, so we can only read it if we've read the header
7480 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
7482 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
7483 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
7485 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
7486 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
7488 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
7492 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
7493 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
7495 unsigned int macro_offset
= attr
->as_unsigned ();
7497 dwarf_decode_macros (cu
, macro_offset
, 0);
7503 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
7505 struct type_unit_group
*tu_group
;
7507 struct attribute
*attr
;
7509 struct signatured_type
*sig_type
;
7511 gdb_assert (per_cu
->is_debug_types
);
7512 sig_type
= (struct signatured_type
*) per_cu
;
7514 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
7516 /* If we're using .gdb_index (includes -readnow) then
7517 per_cu->type_unit_group may not have been set up yet. */
7518 if (sig_type
->type_unit_group
== NULL
)
7519 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
7520 tu_group
= sig_type
->type_unit_group
;
7522 /* If we've already processed this stmt_list there's no real need to
7523 do it again, we could fake it and just recreate the part we need
7524 (file name,index -> symtab mapping). If data shows this optimization
7525 is useful we can do it then. */
7526 type_unit_group_unshareable
*tug_unshare
7527 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
7528 first_time
= tug_unshare
->compunit_symtab
== NULL
;
7530 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7533 if (attr
!= NULL
&& attr
->form_is_unsigned ())
7535 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
7536 lh
= dwarf_decode_line_header (line_offset
, this, nullptr);
7541 start_compunit_symtab ("", NULL
, 0);
7544 gdb_assert (tug_unshare
->symtabs
== NULL
);
7545 gdb_assert (m_builder
== nullptr);
7546 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
7547 m_builder
.reset (new struct buildsym_compunit
7548 (cust
->objfile (), "",
7552 list_in_scope
= get_builder ()->get_file_symbols ();
7557 line_header
= lh
.release ();
7558 line_header_die_owner
= die
;
7562 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
7564 /* Note: We don't assign tu_group->compunit_symtab yet because we're
7565 still initializing it, and our caller (a few levels up)
7566 process_full_type_unit still needs to know if this is the first
7569 tug_unshare
->symtabs
7570 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
7571 struct symtab
*, line_header
->file_names_size ());
7573 auto &file_names
= line_header
->file_names ();
7574 for (i
= 0; i
< file_names
.size (); ++i
)
7576 file_entry
&fe
= file_names
[i
];
7577 dwarf2_start_subfile (this, fe
, *line_header
);
7578 buildsym_compunit
*b
= get_builder ();
7579 subfile
*sf
= b
->get_current_subfile ();
7581 if (sf
->symtab
== nullptr)
7583 /* NOTE: start_subfile will recognize when it's been
7584 passed a file it has already seen. So we can't
7585 assume there's a simple mapping from
7586 cu->line_header->file_names to subfiles, plus
7587 cu->line_header->file_names may contain dups. */
7588 const char *name
= sf
->name
.c_str ();
7589 const char *name_for_id
= sf
->name_for_id
.c_str ();
7590 sf
->symtab
= allocate_symtab (cust
, name
, name_for_id
);
7593 fe
.symtab
= b
->get_current_subfile ()->symtab
;
7594 tug_unshare
->symtabs
[i
] = fe
.symtab
;
7599 gdb_assert (m_builder
== nullptr);
7600 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
7601 m_builder
.reset (new struct buildsym_compunit
7602 (cust
->objfile (), "",
7606 list_in_scope
= get_builder ()->get_file_symbols ();
7608 auto &file_names
= line_header
->file_names ();
7609 for (i
= 0; i
< file_names
.size (); ++i
)
7611 file_entry
&fe
= file_names
[i
];
7612 fe
.symtab
= tug_unshare
->symtabs
[i
];
7616 /* The main symtab is allocated last. Type units don't have DW_AT_name
7617 so they don't have a "real" (so to speak) symtab anyway.
7618 There is later code that will assign the main symtab to all symbols
7619 that don't have one. We need to handle the case of a symbol with a
7620 missing symtab (DW_AT_decl_file) anyway. */
7623 /* Process DW_TAG_type_unit.
7624 For TUs we want to skip the first top level sibling if it's not the
7625 actual type being defined by this TU. In this case the first top
7626 level sibling is there to provide context only. */
7629 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
7631 struct die_info
*child_die
;
7633 prepare_one_comp_unit (cu
, die
, language_minimal
);
7635 /* Initialize (or reinitialize) the machinery for building symtabs.
7636 We do this before processing child DIEs, so that the line header table
7637 is available for DW_AT_decl_file. */
7638 cu
->setup_type_unit_groups (die
);
7640 if (die
->child
!= NULL
)
7642 child_die
= die
->child
;
7643 while (child_die
&& child_die
->tag
)
7645 process_die (child_die
, cu
);
7646 child_die
= child_die
->sibling
;
7653 http://gcc.gnu.org/wiki/DebugFission
7654 http://gcc.gnu.org/wiki/DebugFissionDWP
7656 To simplify handling of both DWO files ("object" files with the DWARF info)
7657 and DWP files (a file with the DWOs packaged up into one file), we treat
7658 DWP files as having a collection of virtual DWO files. */
7660 /* A helper function to hash two file names. This is a separate
7661 function because the hash table uses a search with a different
7662 type. The second file may be NULL. */
7665 hash_two_files (const char *one
, const char *two
)
7667 hashval_t hash
= htab_hash_string (one
);
7669 hash
+= htab_hash_string (two
);
7674 hash_dwo_file (const void *item
)
7676 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
7677 return hash_two_files (dwo_file
->dwo_name
.c_str (), dwo_file
->comp_dir
);
7680 /* This is used when looking up entries in the DWO hash table. */
7682 struct dwo_file_search
7684 /* Name of the DWO to look for. */
7685 const char *dwo_name
;
7686 /* Compilation directory to look for. */
7687 const char *comp_dir
;
7689 /* Return a hash value compatible with the table. */
7690 hashval_t
hash () const
7692 return hash_two_files (dwo_name
, comp_dir
);
7697 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
7699 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
7700 const struct dwo_file_search
*rhs
7701 = (const struct dwo_file_search
*) item_rhs
;
7703 if (lhs
->dwo_name
!= rhs
->dwo_name
)
7705 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
7706 return lhs
->comp_dir
== rhs
->comp_dir
;
7707 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
7710 /* Allocate a hash table for DWO files. */
7713 allocate_dwo_file_hash_table ()
7715 return htab_up (htab_create_alloc (41,
7718 htab_delete_entry
<dwo_file
>,
7722 /* Lookup DWO file DWO_NAME. */
7725 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
7726 const char *dwo_name
,
7727 const char *comp_dir
)
7729 struct dwo_file_search find_entry
;
7732 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
7733 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
7735 find_entry
.dwo_name
= dwo_name
;
7736 find_entry
.comp_dir
= comp_dir
;
7737 slot
= htab_find_slot_with_hash (per_objfile
->per_bfd
->dwo_files
.get (),
7738 &find_entry
, find_entry
.hash (),
7745 hash_dwo_unit (const void *item
)
7747 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
7749 /* This drops the top 32 bits of the id, but is ok for a hash. */
7750 return dwo_unit
->signature
;
7754 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
7756 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
7757 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
7759 /* The signature is assumed to be unique within the DWO file.
7760 So while object file CU dwo_id's always have the value zero,
7761 that's OK, assuming each object file DWO file has only one CU,
7762 and that's the rule for now. */
7763 return lhs
->signature
== rhs
->signature
;
7766 /* Allocate a hash table for DWO CUs,TUs.
7767 There is one of these tables for each of CUs,TUs for each DWO file. */
7770 allocate_dwo_unit_table ()
7772 /* Start out with a pretty small number.
7773 Generally DWO files contain only one CU and maybe some TUs. */
7774 return htab_up (htab_create_alloc (3,
7777 NULL
, xcalloc
, xfree
));
7780 /* die_reader_func for create_dwo_cu. */
7783 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
7784 const gdb_byte
*info_ptr
,
7785 struct die_info
*comp_unit_die
,
7786 struct dwo_file
*dwo_file
,
7787 struct dwo_unit
*dwo_unit
)
7789 struct dwarf2_cu
*cu
= reader
->cu
;
7790 sect_offset sect_off
= cu
->per_cu
->sect_off
;
7791 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
7793 std::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
7794 if (!signature
.has_value ())
7796 complaint (_("Dwarf Error: debug entry at offset %s is missing"
7797 " its dwo_id [in module %s]"),
7798 sect_offset_str (sect_off
), dwo_file
->dwo_name
.c_str ());
7802 dwo_unit
->dwo_file
= dwo_file
;
7803 dwo_unit
->signature
= *signature
;
7804 dwo_unit
->section
= section
;
7805 dwo_unit
->sect_off
= sect_off
;
7806 dwo_unit
->length
= cu
->per_cu
->length ();
7808 dwarf_read_debug_printf (" offset %s, dwo_id %s",
7809 sect_offset_str (sect_off
),
7810 hex_string (dwo_unit
->signature
));
7813 /* Create the dwo_units for the CUs in a DWO_FILE.
7814 Note: This function processes DWO files only, not DWP files. */
7817 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
7818 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
7819 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
7821 struct objfile
*objfile
= per_objfile
->objfile
;
7822 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7823 const gdb_byte
*info_ptr
, *end_ptr
;
7825 section
.read (objfile
);
7826 info_ptr
= section
.buffer
;
7828 if (info_ptr
== NULL
)
7831 dwarf_read_debug_printf ("Reading %s for %s:",
7832 section
.get_name (),
7833 section
.get_file_name ());
7835 end_ptr
= info_ptr
+ section
.size
;
7836 while (info_ptr
< end_ptr
)
7838 struct dwarf2_per_cu_data per_cu
;
7839 struct dwo_unit read_unit
{};
7840 struct dwo_unit
*dwo_unit
;
7842 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
7844 per_cu
.per_bfd
= per_bfd
;
7845 per_cu
.is_debug_types
= 0;
7846 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
7847 per_cu
.section
= §ion
;
7849 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
7850 if (!reader
.dummy_p
)
7851 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
7852 &dwo_file
, &read_unit
);
7853 info_ptr
+= per_cu
.length ();
7855 // If the unit could not be parsed, skip it.
7856 if (read_unit
.dwo_file
== NULL
)
7859 if (cus_htab
== NULL
)
7860 cus_htab
= allocate_dwo_unit_table ();
7862 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
7864 *dwo_unit
= read_unit
;
7865 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
7866 gdb_assert (slot
!= NULL
);
7869 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
7870 sect_offset dup_sect_off
= dup_cu
->sect_off
;
7872 complaint (_("debug cu entry at offset %s is duplicate to"
7873 " the entry at offset %s, signature %s"),
7874 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
7875 hex_string (dwo_unit
->signature
));
7877 *slot
= (void *)dwo_unit
;
7881 /* DWP file .debug_{cu,tu}_index section format:
7882 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
7883 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
7885 DWP Versions 1 & 2 are older, pre-standard format versions. The first
7886 officially standard DWP format was published with DWARF v5 and is called
7887 Version 5. There are no versions 3 or 4.
7891 Both index sections have the same format, and serve to map a 64-bit
7892 signature to a set of section numbers. Each section begins with a header,
7893 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
7894 indexes, and a pool of 32-bit section numbers. The index sections will be
7895 aligned at 8-byte boundaries in the file.
7897 The index section header consists of:
7899 V, 32 bit version number
7901 N, 32 bit number of compilation units or type units in the index
7902 M, 32 bit number of slots in the hash table
7904 Numbers are recorded using the byte order of the application binary.
7906 The hash table begins at offset 16 in the section, and consists of an array
7907 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
7908 order of the application binary). Unused slots in the hash table are 0.
7909 (We rely on the extreme unlikeliness of a signature being exactly 0.)
7911 The parallel table begins immediately after the hash table
7912 (at offset 16 + 8 * M from the beginning of the section), and consists of an
7913 array of 32-bit indexes (using the byte order of the application binary),
7914 corresponding 1-1 with slots in the hash table. Each entry in the parallel
7915 table contains a 32-bit index into the pool of section numbers. For unused
7916 hash table slots, the corresponding entry in the parallel table will be 0.
7918 The pool of section numbers begins immediately following the hash table
7919 (at offset 16 + 12 * M from the beginning of the section). The pool of
7920 section numbers consists of an array of 32-bit words (using the byte order
7921 of the application binary). Each item in the array is indexed starting
7922 from 0. The hash table entry provides the index of the first section
7923 number in the set. Additional section numbers in the set follow, and the
7924 set is terminated by a 0 entry (section number 0 is not used in ELF).
7926 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
7927 section must be the first entry in the set, and the .debug_abbrev.dwo must
7928 be the second entry. Other members of the set may follow in any order.
7932 DWP Versions 2 and 5:
7934 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
7935 and the entries in the index tables are now offsets into these sections.
7936 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
7939 Index Section Contents:
7941 Hash Table of Signatures dwp_hash_table.hash_table
7942 Parallel Table of Indices dwp_hash_table.unit_table
7943 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
7944 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
7946 The index section header consists of:
7948 V, 32 bit version number
7949 L, 32 bit number of columns in the table of section offsets
7950 N, 32 bit number of compilation units or type units in the index
7951 M, 32 bit number of slots in the hash table
7953 Numbers are recorded using the byte order of the application binary.
7955 The hash table has the same format as version 1.
7956 The parallel table of indices has the same format as version 1,
7957 except that the entries are origin-1 indices into the table of sections
7958 offsets and the table of section sizes.
7960 The table of offsets begins immediately following the parallel table
7961 (at offset 16 + 12 * M from the beginning of the section). The table is
7962 a two-dimensional array of 32-bit words (using the byte order of the
7963 application binary), with L columns and N+1 rows, in row-major order.
7964 Each row in the array is indexed starting from 0. The first row provides
7965 a key to the remaining rows: each column in this row provides an identifier
7966 for a debug section, and the offsets in the same column of subsequent rows
7967 refer to that section. The section identifiers for Version 2 are:
7969 DW_SECT_INFO 1 .debug_info.dwo
7970 DW_SECT_TYPES 2 .debug_types.dwo
7971 DW_SECT_ABBREV 3 .debug_abbrev.dwo
7972 DW_SECT_LINE 4 .debug_line.dwo
7973 DW_SECT_LOC 5 .debug_loc.dwo
7974 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
7975 DW_SECT_MACINFO 7 .debug_macinfo.dwo
7976 DW_SECT_MACRO 8 .debug_macro.dwo
7978 The section identifiers for Version 5 are:
7980 DW_SECT_INFO_V5 1 .debug_info.dwo
7981 DW_SECT_RESERVED_V5 2 --
7982 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
7983 DW_SECT_LINE_V5 4 .debug_line.dwo
7984 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
7985 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
7986 DW_SECT_MACRO_V5 7 .debug_macro.dwo
7987 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
7989 The offsets provided by the CU and TU index sections are the base offsets
7990 for the contributions made by each CU or TU to the corresponding section
7991 in the package file. Each CU and TU header contains an abbrev_offset
7992 field, used to find the abbreviations table for that CU or TU within the
7993 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
7994 be interpreted as relative to the base offset given in the index section.
7995 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
7996 should be interpreted as relative to the base offset for .debug_line.dwo,
7997 and offsets into other debug sections obtained from DWARF attributes should
7998 also be interpreted as relative to the corresponding base offset.
8000 The table of sizes begins immediately following the table of offsets.
8001 Like the table of offsets, it is a two-dimensional array of 32-bit words,
8002 with L columns and N rows, in row-major order. Each row in the array is
8003 indexed starting from 1 (row 0 is shared by the two tables).
8007 Hash table lookup is handled the same in version 1 and 2:
8009 We assume that N and M will not exceed 2^32 - 1.
8010 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8012 Given a 64-bit compilation unit signature or a type signature S, an entry
8013 in the hash table is located as follows:
8015 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8016 the low-order k bits all set to 1.
8018 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8020 3) If the hash table entry at index H matches the signature, use that
8021 entry. If the hash table entry at index H is unused (all zeroes),
8022 terminate the search: the signature is not present in the table.
8024 4) Let H = (H + H') modulo M. Repeat at Step 3.
8026 Because M > N and H' and M are relatively prime, the search is guaranteed
8027 to stop at an unused slot or find the match. */
8029 /* Create a hash table to map DWO IDs to their CU/TU entry in
8030 .debug_{info,types}.dwo in DWP_FILE.
8031 Returns NULL if there isn't one.
8032 Note: This function processes DWP files only, not DWO files. */
8034 static struct dwp_hash_table
*
8035 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
8036 struct dwp_file
*dwp_file
, int is_debug_types
)
8038 struct objfile
*objfile
= per_objfile
->objfile
;
8039 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8040 const gdb_byte
*index_ptr
, *index_end
;
8041 struct dwarf2_section_info
*index
;
8042 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
8043 struct dwp_hash_table
*htab
;
8046 index
= &dwp_file
->sections
.tu_index
;
8048 index
= &dwp_file
->sections
.cu_index
;
8050 if (index
->empty ())
8052 index
->read (objfile
);
8054 index_ptr
= index
->buffer
;
8055 index_end
= index_ptr
+ index
->size
;
8057 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
8058 For now it's safe to just read 4 bytes (particularly as it's difficult to
8059 tell if you're dealing with Version 5 before you've read the version). */
8060 version
= read_4_bytes (dbfd
, index_ptr
);
8062 if (version
== 2 || version
== 5)
8063 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
8067 nr_units
= read_4_bytes (dbfd
, index_ptr
);
8069 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
8072 if (version
!= 1 && version
!= 2 && version
!= 5)
8074 error (_("Dwarf Error: unsupported DWP file version (%s)"
8076 pulongest (version
), dwp_file
->name
);
8078 if (nr_slots
!= (nr_slots
& -nr_slots
))
8080 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8081 " is not power of 2 [in module %s]"),
8082 pulongest (nr_slots
), dwp_file
->name
);
8085 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
8086 htab
->version
= version
;
8087 htab
->nr_columns
= nr_columns
;
8088 htab
->nr_units
= nr_units
;
8089 htab
->nr_slots
= nr_slots
;
8090 htab
->hash_table
= index_ptr
;
8091 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
8093 /* Exit early if the table is empty. */
8094 if (nr_slots
== 0 || nr_units
== 0
8095 || (version
== 2 && nr_columns
== 0)
8096 || (version
== 5 && nr_columns
== 0))
8098 /* All must be zero. */
8099 if (nr_slots
!= 0 || nr_units
!= 0
8100 || (version
== 2 && nr_columns
!= 0)
8101 || (version
== 5 && nr_columns
!= 0))
8103 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
8104 " all zero [in modules %s]"),
8112 htab
->section_pool
.v1
.indices
=
8113 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
8114 /* It's harder to decide whether the section is too small in v1.
8115 V1 is deprecated anyway so we punt. */
8117 else if (version
== 2)
8119 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
8120 int *ids
= htab
->section_pool
.v2
.section_ids
;
8121 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
8122 /* Reverse map for error checking. */
8123 int ids_seen
[DW_SECT_MAX
+ 1];
8128 error (_("Dwarf Error: bad DWP hash table, too few columns"
8129 " in section table [in module %s]"),
8132 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
8134 error (_("Dwarf Error: bad DWP hash table, too many columns"
8135 " in section table [in module %s]"),
8138 memset (ids
, 255, sizeof_ids
);
8139 memset (ids_seen
, 255, sizeof (ids_seen
));
8140 for (i
= 0; i
< nr_columns
; ++i
)
8142 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
8144 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
8146 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8147 " in section table [in module %s]"),
8148 id
, dwp_file
->name
);
8150 if (ids_seen
[id
] != -1)
8152 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8153 " id %d in section table [in module %s]"),
8154 id
, dwp_file
->name
);
8159 /* Must have exactly one info or types section. */
8160 if (((ids_seen
[DW_SECT_INFO
] != -1)
8161 + (ids_seen
[DW_SECT_TYPES
] != -1))
8164 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8165 " DWO info/types section [in module %s]"),
8168 /* Must have an abbrev section. */
8169 if (ids_seen
[DW_SECT_ABBREV
] == -1)
8171 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8172 " section [in module %s]"),
8175 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
8176 htab
->section_pool
.v2
.sizes
=
8177 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
8178 * nr_units
* nr_columns
);
8179 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
8180 * nr_units
* nr_columns
))
8183 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8188 else /* version == 5 */
8190 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
8191 int *ids
= htab
->section_pool
.v5
.section_ids
;
8192 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
8193 /* Reverse map for error checking. */
8194 int ids_seen
[DW_SECT_MAX_V5
+ 1];
8198 error (_("Dwarf Error: bad DWP hash table, too few columns"
8199 " in section table [in module %s]"),
8202 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
8204 error (_("Dwarf Error: bad DWP hash table, too many columns"
8205 " in section table [in module %s]"),
8208 memset (ids
, 255, sizeof_ids
);
8209 memset (ids_seen
, 255, sizeof (ids_seen
));
8210 for (int i
= 0; i
< nr_columns
; ++i
)
8212 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
8214 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
8216 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
8217 " in section table [in module %s]"),
8218 id
, dwp_file
->name
);
8220 if (ids_seen
[id
] != -1)
8222 error (_("Dwarf Error: bad DWP hash table, duplicate section"
8223 " id %d in section table [in module %s]"),
8224 id
, dwp_file
->name
);
8229 /* Must have seen an info section. */
8230 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
8232 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
8233 " DWO info/types section [in module %s]"),
8236 /* Must have an abbrev section. */
8237 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
8239 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
8240 " section [in module %s]"),
8243 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
8244 htab
->section_pool
.v5
.sizes
8245 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
8246 * nr_units
* nr_columns
);
8247 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
8248 * nr_units
* nr_columns
))
8251 error (_("Dwarf Error: DWP index section is corrupt (too small)"
8260 /* Update SECTIONS with the data from SECTP.
8262 This function is like the other "locate" section routines, but in
8263 this context the sections to read comes from the DWP V1 hash table,
8264 not the full ELF section table.
8266 The result is non-zero for success, or zero if an error was found. */
8269 locate_v1_virtual_dwo_sections (asection
*sectp
,
8270 struct virtual_v1_dwo_sections
*sections
)
8272 const struct dwop_section_names
*names
= &dwop_section_names
;
8274 if (names
->abbrev_dwo
.matches (sectp
->name
))
8276 /* There can be only one. */
8277 if (sections
->abbrev
.s
.section
!= NULL
)
8279 sections
->abbrev
.s
.section
= sectp
;
8280 sections
->abbrev
.size
= bfd_section_size (sectp
);
8282 else if (names
->info_dwo
.matches (sectp
->name
)
8283 || names
->types_dwo
.matches (sectp
->name
))
8285 /* There can be only one. */
8286 if (sections
->info_or_types
.s
.section
!= NULL
)
8288 sections
->info_or_types
.s
.section
= sectp
;
8289 sections
->info_or_types
.size
= bfd_section_size (sectp
);
8291 else if (names
->line_dwo
.matches (sectp
->name
))
8293 /* There can be only one. */
8294 if (sections
->line
.s
.section
!= NULL
)
8296 sections
->line
.s
.section
= sectp
;
8297 sections
->line
.size
= bfd_section_size (sectp
);
8299 else if (names
->loc_dwo
.matches (sectp
->name
))
8301 /* There can be only one. */
8302 if (sections
->loc
.s
.section
!= NULL
)
8304 sections
->loc
.s
.section
= sectp
;
8305 sections
->loc
.size
= bfd_section_size (sectp
);
8307 else if (names
->macinfo_dwo
.matches (sectp
->name
))
8309 /* There can be only one. */
8310 if (sections
->macinfo
.s
.section
!= NULL
)
8312 sections
->macinfo
.s
.section
= sectp
;
8313 sections
->macinfo
.size
= bfd_section_size (sectp
);
8315 else if (names
->macro_dwo
.matches (sectp
->name
))
8317 /* There can be only one. */
8318 if (sections
->macro
.s
.section
!= NULL
)
8320 sections
->macro
.s
.section
= sectp
;
8321 sections
->macro
.size
= bfd_section_size (sectp
);
8323 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
8325 /* There can be only one. */
8326 if (sections
->str_offsets
.s
.section
!= NULL
)
8328 sections
->str_offsets
.s
.section
= sectp
;
8329 sections
->str_offsets
.size
= bfd_section_size (sectp
);
8333 /* No other kind of section is valid. */
8340 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8341 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8342 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8343 This is for DWP version 1 files. */
8345 static struct dwo_unit
*
8346 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
8347 struct dwp_file
*dwp_file
,
8348 uint32_t unit_index
,
8349 const char *comp_dir
,
8350 ULONGEST signature
, int is_debug_types
)
8352 const struct dwp_hash_table
*dwp_htab
=
8353 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8354 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8355 const char *kind
= is_debug_types
? "TU" : "CU";
8356 struct dwo_file
*dwo_file
;
8357 struct dwo_unit
*dwo_unit
;
8358 struct virtual_v1_dwo_sections sections
;
8359 void **dwo_file_slot
;
8362 gdb_assert (dwp_file
->version
== 1);
8364 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
8365 kind
, pulongest (unit_index
), hex_string (signature
),
8368 /* Fetch the sections of this DWO unit.
8369 Put a limit on the number of sections we look for so that bad data
8370 doesn't cause us to loop forever. */
8372 #define MAX_NR_V1_DWO_SECTIONS \
8373 (1 /* .debug_info or .debug_types */ \
8374 + 1 /* .debug_abbrev */ \
8375 + 1 /* .debug_line */ \
8376 + 1 /* .debug_loc */ \
8377 + 1 /* .debug_str_offsets */ \
8378 + 1 /* .debug_macro or .debug_macinfo */ \
8379 + 1 /* trailing zero */)
8381 memset (§ions
, 0, sizeof (sections
));
8383 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
8386 uint32_t section_nr
=
8388 dwp_htab
->section_pool
.v1
.indices
8389 + (unit_index
+ i
) * sizeof (uint32_t));
8391 if (section_nr
== 0)
8393 if (section_nr
>= dwp_file
->num_sections
)
8395 error (_("Dwarf Error: bad DWP hash table, section number too large"
8400 sectp
= dwp_file
->elf_sections
[section_nr
];
8401 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
8403 error (_("Dwarf Error: bad DWP hash table, invalid section found"
8410 || sections
.info_or_types
.empty ()
8411 || sections
.abbrev
.empty ())
8413 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8417 if (i
== MAX_NR_V1_DWO_SECTIONS
)
8419 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8424 /* It's easier for the rest of the code if we fake a struct dwo_file and
8425 have dwo_unit "live" in that. At least for now.
8427 The DWP file can be made up of a random collection of CUs and TUs.
8428 However, for each CU + set of TUs that came from the same original DWO
8429 file, we can combine them back into a virtual DWO file to save space
8430 (fewer struct dwo_file objects to allocate). Remember that for really
8431 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8433 std::string virtual_dwo_name
=
8434 string_printf ("virtual-dwo/%d-%d-%d-%d",
8435 sections
.abbrev
.get_id (),
8436 sections
.line
.get_id (),
8437 sections
.loc
.get_id (),
8438 sections
.str_offsets
.get_id ());
8439 /* Can we use an existing virtual DWO file? */
8440 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
8442 /* Create one if necessary. */
8443 if (*dwo_file_slot
== NULL
)
8445 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8446 virtual_dwo_name
.c_str ());
8448 dwo_file
= new struct dwo_file
;
8449 dwo_file
->dwo_name
= std::move (virtual_dwo_name
);
8450 dwo_file
->comp_dir
= comp_dir
;
8451 dwo_file
->sections
.abbrev
= sections
.abbrev
;
8452 dwo_file
->sections
.line
= sections
.line
;
8453 dwo_file
->sections
.loc
= sections
.loc
;
8454 dwo_file
->sections
.macinfo
= sections
.macinfo
;
8455 dwo_file
->sections
.macro
= sections
.macro
;
8456 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
8457 /* The "str" section is global to the entire DWP file. */
8458 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
8459 /* The info or types section is assigned below to dwo_unit,
8460 there's no need to record it in dwo_file.
8461 Also, we can't simply record type sections in dwo_file because
8462 we record a pointer into the vector in dwo_unit. As we collect more
8463 types we'll grow the vector and eventually have to reallocate space
8464 for it, invalidating all copies of pointers into the previous
8466 *dwo_file_slot
= dwo_file
;
8470 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8471 virtual_dwo_name
.c_str ());
8473 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
8476 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
8477 dwo_unit
->dwo_file
= dwo_file
;
8478 dwo_unit
->signature
= signature
;
8480 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
8481 *dwo_unit
->section
= sections
.info_or_types
;
8482 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8487 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
8488 simplify them. Given a pointer to the containing section SECTION, and
8489 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
8490 virtual section of just that piece. */
8492 static struct dwarf2_section_info
8493 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
8494 struct dwarf2_section_info
*section
,
8495 bfd_size_type offset
, bfd_size_type size
)
8497 struct dwarf2_section_info result
;
8500 gdb_assert (section
!= NULL
);
8501 gdb_assert (!section
->is_virtual
);
8503 memset (&result
, 0, sizeof (result
));
8504 result
.s
.containing_section
= section
;
8505 result
.is_virtual
= true;
8510 sectp
= section
->get_bfd_section ();
8512 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
8513 bounds of the real section. This is a pretty-rare event, so just
8514 flag an error (easier) instead of a warning and trying to cope. */
8516 || offset
+ size
> bfd_section_size (sectp
))
8518 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
8519 " in section %s [in module %s]"),
8520 sectp
? bfd_section_name (sectp
) : "<unknown>",
8521 objfile_name (per_objfile
->objfile
));
8524 result
.virtual_offset
= offset
;
8529 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8530 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8531 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8532 This is for DWP version 2 files. */
8534 static struct dwo_unit
*
8535 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
8536 struct dwp_file
*dwp_file
,
8537 uint32_t unit_index
,
8538 const char *comp_dir
,
8539 ULONGEST signature
, int is_debug_types
)
8541 const struct dwp_hash_table
*dwp_htab
=
8542 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8543 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8544 const char *kind
= is_debug_types
? "TU" : "CU";
8545 struct dwo_file
*dwo_file
;
8546 struct dwo_unit
*dwo_unit
;
8547 struct virtual_v2_or_v5_dwo_sections sections
;
8548 void **dwo_file_slot
;
8551 gdb_assert (dwp_file
->version
== 2);
8553 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
8554 kind
, pulongest (unit_index
), hex_string (signature
),
8557 /* Fetch the section offsets of this DWO unit. */
8559 memset (§ions
, 0, sizeof (sections
));
8561 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
8563 uint32_t offset
= read_4_bytes (dbfd
,
8564 dwp_htab
->section_pool
.v2
.offsets
8565 + (((unit_index
- 1) * dwp_htab
->nr_columns
8567 * sizeof (uint32_t)));
8568 uint32_t size
= read_4_bytes (dbfd
,
8569 dwp_htab
->section_pool
.v2
.sizes
8570 + (((unit_index
- 1) * dwp_htab
->nr_columns
8572 * sizeof (uint32_t)));
8574 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
8578 sections
.info_or_types_offset
= offset
;
8579 sections
.info_or_types_size
= size
;
8581 case DW_SECT_ABBREV
:
8582 sections
.abbrev_offset
= offset
;
8583 sections
.abbrev_size
= size
;
8586 sections
.line_offset
= offset
;
8587 sections
.line_size
= size
;
8590 sections
.loc_offset
= offset
;
8591 sections
.loc_size
= size
;
8593 case DW_SECT_STR_OFFSETS
:
8594 sections
.str_offsets_offset
= offset
;
8595 sections
.str_offsets_size
= size
;
8597 case DW_SECT_MACINFO
:
8598 sections
.macinfo_offset
= offset
;
8599 sections
.macinfo_size
= size
;
8602 sections
.macro_offset
= offset
;
8603 sections
.macro_size
= size
;
8608 /* It's easier for the rest of the code if we fake a struct dwo_file and
8609 have dwo_unit "live" in that. At least for now.
8611 The DWP file can be made up of a random collection of CUs and TUs.
8612 However, for each CU + set of TUs that came from the same original DWO
8613 file, we can combine them back into a virtual DWO file to save space
8614 (fewer struct dwo_file objects to allocate). Remember that for really
8615 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8617 std::string virtual_dwo_name
=
8618 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
8619 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
8620 (long) (sections
.line_size
? sections
.line_offset
: 0),
8621 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
8622 (long) (sections
.str_offsets_size
8623 ? sections
.str_offsets_offset
: 0));
8624 /* Can we use an existing virtual DWO file? */
8625 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
8627 /* Create one if necessary. */
8628 if (*dwo_file_slot
== NULL
)
8630 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8631 virtual_dwo_name
.c_str ());
8633 dwo_file
= new struct dwo_file
;
8634 dwo_file
->dwo_name
= std::move (virtual_dwo_name
);
8635 dwo_file
->comp_dir
= comp_dir
;
8636 dwo_file
->sections
.abbrev
=
8637 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
8638 sections
.abbrev_offset
,
8639 sections
.abbrev_size
);
8640 dwo_file
->sections
.line
=
8641 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
8642 sections
.line_offset
,
8643 sections
.line_size
);
8644 dwo_file
->sections
.loc
=
8645 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
8646 sections
.loc_offset
, sections
.loc_size
);
8647 dwo_file
->sections
.macinfo
=
8648 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
8649 sections
.macinfo_offset
,
8650 sections
.macinfo_size
);
8651 dwo_file
->sections
.macro
=
8652 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
8653 sections
.macro_offset
,
8654 sections
.macro_size
);
8655 dwo_file
->sections
.str_offsets
=
8656 create_dwp_v2_or_v5_section (per_objfile
,
8657 &dwp_file
->sections
.str_offsets
,
8658 sections
.str_offsets_offset
,
8659 sections
.str_offsets_size
);
8660 /* The "str" section is global to the entire DWP file. */
8661 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
8662 /* The info or types section is assigned below to dwo_unit,
8663 there's no need to record it in dwo_file.
8664 Also, we can't simply record type sections in dwo_file because
8665 we record a pointer into the vector in dwo_unit. As we collect more
8666 types we'll grow the vector and eventually have to reallocate space
8667 for it, invalidating all copies of pointers into the previous
8669 *dwo_file_slot
= dwo_file
;
8673 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8674 virtual_dwo_name
.c_str ());
8676 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
8679 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
8680 dwo_unit
->dwo_file
= dwo_file
;
8681 dwo_unit
->signature
= signature
;
8683 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
8684 *dwo_unit
->section
= create_dwp_v2_or_v5_section
8687 ? &dwp_file
->sections
.types
8688 : &dwp_file
->sections
.info
,
8689 sections
.info_or_types_offset
,
8690 sections
.info_or_types_size
);
8691 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8696 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
8697 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
8698 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
8699 This is for DWP version 5 files. */
8701 static struct dwo_unit
*
8702 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
8703 struct dwp_file
*dwp_file
,
8704 uint32_t unit_index
,
8705 const char *comp_dir
,
8706 ULONGEST signature
, int is_debug_types
)
8708 const struct dwp_hash_table
*dwp_htab
8709 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8710 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8711 const char *kind
= is_debug_types
? "TU" : "CU";
8712 struct dwo_file
*dwo_file
;
8713 struct dwo_unit
*dwo_unit
;
8714 struct virtual_v2_or_v5_dwo_sections sections
{};
8715 void **dwo_file_slot
;
8717 gdb_assert (dwp_file
->version
== 5);
8719 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
8720 kind
, pulongest (unit_index
), hex_string (signature
),
8723 /* Fetch the section offsets of this DWO unit. */
8725 /* memset (§ions, 0, sizeof (sections)); */
8727 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
8729 uint32_t offset
= read_4_bytes (dbfd
,
8730 dwp_htab
->section_pool
.v5
.offsets
8731 + (((unit_index
- 1)
8732 * dwp_htab
->nr_columns
8734 * sizeof (uint32_t)));
8735 uint32_t size
= read_4_bytes (dbfd
,
8736 dwp_htab
->section_pool
.v5
.sizes
8737 + (((unit_index
- 1) * dwp_htab
->nr_columns
8739 * sizeof (uint32_t)));
8741 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
8743 case DW_SECT_ABBREV_V5
:
8744 sections
.abbrev_offset
= offset
;
8745 sections
.abbrev_size
= size
;
8747 case DW_SECT_INFO_V5
:
8748 sections
.info_or_types_offset
= offset
;
8749 sections
.info_or_types_size
= size
;
8751 case DW_SECT_LINE_V5
:
8752 sections
.line_offset
= offset
;
8753 sections
.line_size
= size
;
8755 case DW_SECT_LOCLISTS_V5
:
8756 sections
.loclists_offset
= offset
;
8757 sections
.loclists_size
= size
;
8759 case DW_SECT_MACRO_V5
:
8760 sections
.macro_offset
= offset
;
8761 sections
.macro_size
= size
;
8763 case DW_SECT_RNGLISTS_V5
:
8764 sections
.rnglists_offset
= offset
;
8765 sections
.rnglists_size
= size
;
8767 case DW_SECT_STR_OFFSETS_V5
:
8768 sections
.str_offsets_offset
= offset
;
8769 sections
.str_offsets_size
= size
;
8771 case DW_SECT_RESERVED_V5
:
8777 /* It's easier for the rest of the code if we fake a struct dwo_file and
8778 have dwo_unit "live" in that. At least for now.
8780 The DWP file can be made up of a random collection of CUs and TUs.
8781 However, for each CU + set of TUs that came from the same original DWO
8782 file, we can combine them back into a virtual DWO file to save space
8783 (fewer struct dwo_file objects to allocate). Remember that for really
8784 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
8786 std::string virtual_dwo_name
=
8787 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
8788 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
8789 (long) (sections
.line_size
? sections
.line_offset
: 0),
8790 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
8791 (long) (sections
.str_offsets_size
8792 ? sections
.str_offsets_offset
: 0),
8793 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
8794 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
8795 /* Can we use an existing virtual DWO file? */
8796 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
8797 virtual_dwo_name
.c_str (),
8799 /* Create one if necessary. */
8800 if (*dwo_file_slot
== NULL
)
8802 dwarf_read_debug_printf ("Creating virtual DWO: %s",
8803 virtual_dwo_name
.c_str ());
8805 dwo_file
= new struct dwo_file
;
8806 dwo_file
->dwo_name
= std::move (virtual_dwo_name
);
8807 dwo_file
->comp_dir
= comp_dir
;
8808 dwo_file
->sections
.abbrev
=
8809 create_dwp_v2_or_v5_section (per_objfile
,
8810 &dwp_file
->sections
.abbrev
,
8811 sections
.abbrev_offset
,
8812 sections
.abbrev_size
);
8813 dwo_file
->sections
.line
=
8814 create_dwp_v2_or_v5_section (per_objfile
,
8815 &dwp_file
->sections
.line
,
8816 sections
.line_offset
, sections
.line_size
);
8817 dwo_file
->sections
.macro
=
8818 create_dwp_v2_or_v5_section (per_objfile
,
8819 &dwp_file
->sections
.macro
,
8820 sections
.macro_offset
,
8821 sections
.macro_size
);
8822 dwo_file
->sections
.loclists
=
8823 create_dwp_v2_or_v5_section (per_objfile
,
8824 &dwp_file
->sections
.loclists
,
8825 sections
.loclists_offset
,
8826 sections
.loclists_size
);
8827 dwo_file
->sections
.rnglists
=
8828 create_dwp_v2_or_v5_section (per_objfile
,
8829 &dwp_file
->sections
.rnglists
,
8830 sections
.rnglists_offset
,
8831 sections
.rnglists_size
);
8832 dwo_file
->sections
.str_offsets
=
8833 create_dwp_v2_or_v5_section (per_objfile
,
8834 &dwp_file
->sections
.str_offsets
,
8835 sections
.str_offsets_offset
,
8836 sections
.str_offsets_size
);
8837 /* The "str" section is global to the entire DWP file. */
8838 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
8839 /* The info or types section is assigned below to dwo_unit,
8840 there's no need to record it in dwo_file.
8841 Also, we can't simply record type sections in dwo_file because
8842 we record a pointer into the vector in dwo_unit. As we collect more
8843 types we'll grow the vector and eventually have to reallocate space
8844 for it, invalidating all copies of pointers into the previous
8846 *dwo_file_slot
= dwo_file
;
8850 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
8851 virtual_dwo_name
.c_str ());
8853 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
8856 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
8857 dwo_unit
->dwo_file
= dwo_file
;
8858 dwo_unit
->signature
= signature
;
8860 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
8861 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
8862 &dwp_file
->sections
.info
,
8863 sections
.info_or_types_offset
,
8864 sections
.info_or_types_size
);
8865 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
8870 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
8871 Returns NULL if the signature isn't found. */
8873 static struct dwo_unit
*
8874 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
8875 struct dwp_file
*dwp_file
, const char *comp_dir
,
8876 ULONGEST signature
, int is_debug_types
)
8878 const struct dwp_hash_table
*dwp_htab
=
8879 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
8880 bfd
*dbfd
= dwp_file
->dbfd
.get ();
8881 uint32_t mask
= dwp_htab
->nr_slots
- 1;
8882 uint32_t hash
= signature
& mask
;
8883 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
8886 struct dwo_unit find_dwo_cu
;
8888 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
8889 find_dwo_cu
.signature
= signature
;
8890 slot
= htab_find_slot (is_debug_types
8891 ? dwp_file
->loaded_tus
.get ()
8892 : dwp_file
->loaded_cus
.get (),
8893 &find_dwo_cu
, INSERT
);
8896 return (struct dwo_unit
*) *slot
;
8898 /* Use a for loop so that we don't loop forever on bad debug info. */
8899 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
8901 ULONGEST signature_in_table
;
8903 signature_in_table
=
8904 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
8905 if (signature_in_table
== signature
)
8907 uint32_t unit_index
=
8909 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
8911 if (dwp_file
->version
== 1)
8913 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
8914 unit_index
, comp_dir
,
8915 signature
, is_debug_types
);
8917 else if (dwp_file
->version
== 2)
8919 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
8920 unit_index
, comp_dir
,
8921 signature
, is_debug_types
);
8923 else /* version == 5 */
8925 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
8926 unit_index
, comp_dir
,
8927 signature
, is_debug_types
);
8929 return (struct dwo_unit
*) *slot
;
8931 if (signature_in_table
== 0)
8933 hash
= (hash
+ hash2
) & mask
;
8936 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8941 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
8942 Open the file specified by FILE_NAME and hand it off to BFD for
8943 preliminary analysis. Return a newly initialized bfd *, which
8944 includes a canonicalized copy of FILE_NAME.
8945 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8946 SEARCH_CWD is true if the current directory is to be searched.
8947 It will be searched before debug-file-directory.
8948 If successful, the file is added to the bfd include table of the
8949 objfile's bfd (see gdb_bfd_record_inclusion).
8950 If unable to find/open the file, return NULL.
8951 NOTE: This function is derived from symfile_bfd_open. */
8953 static gdb_bfd_ref_ptr
8954 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
8955 const char *file_name
, int is_dwp
, int search_cwd
)
8958 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
8959 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
8960 to debug_file_directory. */
8961 const char *search_path
;
8962 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
8964 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
8967 if (!debug_file_directory
.empty ())
8969 search_path_holder
.reset (concat (".", dirname_separator_string
,
8970 debug_file_directory
.c_str (),
8972 search_path
= search_path_holder
.get ();
8978 search_path
= debug_file_directory
.c_str ();
8980 /* Add the path for the executable binary to the list of search paths. */
8981 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
8982 search_path_holder
.reset (concat (objfile_dir
.c_str (),
8983 dirname_separator_string
,
8984 search_path
, nullptr));
8985 search_path
= search_path_holder
.get ();
8987 openp_flags flags
= OPF_RETURN_REALPATH
;
8989 flags
|= OPF_SEARCH_IN_PATH
;
8991 gdb::unique_xmalloc_ptr
<char> absolute_name
;
8992 desc
= openp (search_path
, flags
, file_name
,
8993 O_RDONLY
| O_BINARY
, &absolute_name
);
8997 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
8999 if (sym_bfd
== NULL
)
9002 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
9005 /* Success. Record the bfd as having been included by the objfile's bfd.
9006 This is important because things like demangled_names_hash lives in the
9007 objfile's per_bfd space and may have references to things like symbol
9008 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
9009 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
.get (), sym_bfd
.get ());
9014 /* Try to open DWO file FILE_NAME.
9015 COMP_DIR is the DW_AT_comp_dir attribute.
9016 The result is the bfd handle of the file.
9017 If there is a problem finding or opening the file, return NULL.
9018 Upon success, the canonicalized path of the file is stored in the bfd,
9019 same as symfile_bfd_open. */
9021 static gdb_bfd_ref_ptr
9022 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
9023 const char *file_name
, const char *comp_dir
)
9025 if (IS_ABSOLUTE_PATH (file_name
))
9026 return try_open_dwop_file (per_objfile
, file_name
,
9027 0 /*is_dwp*/, 0 /*search_cwd*/);
9029 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9031 if (comp_dir
!= NULL
)
9033 std::string path_to_try
= path_join (comp_dir
, file_name
);
9035 /* NOTE: If comp_dir is a relative path, this will also try the
9036 search path, which seems useful. */
9037 gdb_bfd_ref_ptr
abfd (try_open_dwop_file
9038 (per_objfile
, path_to_try
.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
9044 /* That didn't work, try debug-file-directory, which, despite its name,
9045 is a list of paths. */
9047 if (debug_file_directory
.empty ())
9050 return try_open_dwop_file (per_objfile
, file_name
,
9051 0 /*is_dwp*/, 1 /*search_cwd*/);
9054 /* This function is mapped across the sections and remembers the offset and
9055 size of each of the DWO debugging sections we are interested in. */
9058 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
9059 asection
*sectp
, dwo_sections
*dwo_sections
)
9061 const struct dwop_section_names
*names
= &dwop_section_names
;
9063 struct dwarf2_section_info
*dw_sect
= nullptr;
9065 if (names
->abbrev_dwo
.matches (sectp
->name
))
9066 dw_sect
= &dwo_sections
->abbrev
;
9067 else if (names
->info_dwo
.matches (sectp
->name
))
9068 dw_sect
= &dwo_sections
->info
;
9069 else if (names
->line_dwo
.matches (sectp
->name
))
9070 dw_sect
= &dwo_sections
->line
;
9071 else if (names
->loc_dwo
.matches (sectp
->name
))
9072 dw_sect
= &dwo_sections
->loc
;
9073 else if (names
->loclists_dwo
.matches (sectp
->name
))
9074 dw_sect
= &dwo_sections
->loclists
;
9075 else if (names
->macinfo_dwo
.matches (sectp
->name
))
9076 dw_sect
= &dwo_sections
->macinfo
;
9077 else if (names
->macro_dwo
.matches (sectp
->name
))
9078 dw_sect
= &dwo_sections
->macro
;
9079 else if (names
->rnglists_dwo
.matches (sectp
->name
))
9080 dw_sect
= &dwo_sections
->rnglists
;
9081 else if (names
->str_dwo
.matches (sectp
->name
))
9082 dw_sect
= &dwo_sections
->str
;
9083 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
9084 dw_sect
= &dwo_sections
->str_offsets
;
9085 else if (names
->types_dwo
.matches (sectp
->name
))
9087 struct dwarf2_section_info type_section
;
9089 memset (&type_section
, 0, sizeof (type_section
));
9090 dwo_sections
->types
.push_back (type_section
);
9091 dw_sect
= &dwo_sections
->types
.back ();
9094 if (dw_sect
!= nullptr)
9096 dw_sect
->s
.section
= sectp
;
9097 dw_sect
->size
= bfd_section_size (sectp
);
9098 dw_sect
->read (objfile
);
9102 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9103 by PER_CU. This is for the non-DWP case.
9104 The result is NULL if DWO_NAME can't be found. */
9106 static struct dwo_file
*
9107 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
9108 const char *comp_dir
)
9110 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9112 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
9115 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
9120 dwo_file_up
dwo_file (new struct dwo_file
);
9121 dwo_file
->dwo_name
= dwo_name
;
9122 dwo_file
->comp_dir
= comp_dir
;
9123 dwo_file
->dbfd
= std::move (dbfd
);
9125 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
9126 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
9127 sec
, &dwo_file
->sections
);
9129 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
9132 if (cu
->per_cu
->version () < 5)
9134 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
9135 dwo_file
->sections
.types
, dwo_file
->tus
);
9139 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
9140 &dwo_file
->sections
.info
, dwo_file
->tus
,
9141 rcuh_kind::COMPILE
);
9144 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
9146 bfd_cache_close (dwo_file
->dbfd
.get ());
9148 return dwo_file
.release ();
9151 /* This function is mapped across the sections and remembers the offset and
9152 size of each of the DWP debugging sections common to version 1 and 2 that
9153 we are interested in. */
9156 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
9157 asection
*sectp
, dwp_file
*dwp_file
)
9159 const struct dwop_section_names
*names
= &dwop_section_names
;
9160 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
9162 /* Record the ELF section number for later lookup: this is what the
9163 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9164 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
9165 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
9167 /* Look for specific sections that we need. */
9168 struct dwarf2_section_info
*dw_sect
= nullptr;
9169 if (names
->str_dwo
.matches (sectp
->name
))
9170 dw_sect
= &dwp_file
->sections
.str
;
9171 else if (names
->cu_index
.matches (sectp
->name
))
9172 dw_sect
= &dwp_file
->sections
.cu_index
;
9173 else if (names
->tu_index
.matches (sectp
->name
))
9174 dw_sect
= &dwp_file
->sections
.tu_index
;
9176 if (dw_sect
!= nullptr)
9178 dw_sect
->s
.section
= sectp
;
9179 dw_sect
->size
= bfd_section_size (sectp
);
9180 dw_sect
->read (objfile
);
9184 /* This function is mapped across the sections and remembers the offset and
9185 size of each of the DWP version 2 debugging sections that we are interested
9186 in. This is split into a separate function because we don't know if we
9187 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9190 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
9191 asection
*sectp
, void *dwp_file_ptr
)
9193 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
9194 const struct dwop_section_names
*names
= &dwop_section_names
;
9195 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
9197 /* Record the ELF section number for later lookup: this is what the
9198 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9199 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
9200 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
9202 /* Look for specific sections that we need. */
9203 struct dwarf2_section_info
*dw_sect
= nullptr;
9204 if (names
->abbrev_dwo
.matches (sectp
->name
))
9205 dw_sect
= &dwp_file
->sections
.abbrev
;
9206 else if (names
->info_dwo
.matches (sectp
->name
))
9207 dw_sect
= &dwp_file
->sections
.info
;
9208 else if (names
->line_dwo
.matches (sectp
->name
))
9209 dw_sect
= &dwp_file
->sections
.line
;
9210 else if (names
->loc_dwo
.matches (sectp
->name
))
9211 dw_sect
= &dwp_file
->sections
.loc
;
9212 else if (names
->macinfo_dwo
.matches (sectp
->name
))
9213 dw_sect
= &dwp_file
->sections
.macinfo
;
9214 else if (names
->macro_dwo
.matches (sectp
->name
))
9215 dw_sect
= &dwp_file
->sections
.macro
;
9216 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
9217 dw_sect
= &dwp_file
->sections
.str_offsets
;
9218 else if (names
->types_dwo
.matches (sectp
->name
))
9219 dw_sect
= &dwp_file
->sections
.types
;
9221 if (dw_sect
!= nullptr)
9223 dw_sect
->s
.section
= sectp
;
9224 dw_sect
->size
= bfd_section_size (sectp
);
9225 dw_sect
->read (objfile
);
9229 /* This function is mapped across the sections and remembers the offset and
9230 size of each of the DWP version 5 debugging sections that we are interested
9231 in. This is split into a separate function because we don't know if we
9232 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
9235 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
9236 asection
*sectp
, void *dwp_file_ptr
)
9238 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
9239 const struct dwop_section_names
*names
= &dwop_section_names
;
9240 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
9242 /* Record the ELF section number for later lookup: this is what the
9243 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
9244 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
9245 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
9247 /* Look for specific sections that we need. */
9248 struct dwarf2_section_info
*dw_sect
= nullptr;
9249 if (names
->abbrev_dwo
.matches (sectp
->name
))
9250 dw_sect
= &dwp_file
->sections
.abbrev
;
9251 else if (names
->info_dwo
.matches (sectp
->name
))
9252 dw_sect
= &dwp_file
->sections
.info
;
9253 else if (names
->line_dwo
.matches (sectp
->name
))
9254 dw_sect
= &dwp_file
->sections
.line
;
9255 else if (names
->loclists_dwo
.matches (sectp
->name
))
9256 dw_sect
= &dwp_file
->sections
.loclists
;
9257 else if (names
->macro_dwo
.matches (sectp
->name
))
9258 dw_sect
= &dwp_file
->sections
.macro
;
9259 else if (names
->rnglists_dwo
.matches (sectp
->name
))
9260 dw_sect
= &dwp_file
->sections
.rnglists
;
9261 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
9262 dw_sect
= &dwp_file
->sections
.str_offsets
;
9264 if (dw_sect
!= nullptr)
9266 dw_sect
->s
.section
= sectp
;
9267 dw_sect
->size
= bfd_section_size (sectp
);
9268 dw_sect
->read (objfile
);
9272 /* Hash function for dwp_file loaded CUs/TUs. */
9275 hash_dwp_loaded_cutus (const void *item
)
9277 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9279 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9280 return dwo_unit
->signature
;
9283 /* Equality function for dwp_file loaded CUs/TUs. */
9286 eq_dwp_loaded_cutus (const void *a
, const void *b
)
9288 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
9289 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
9291 return dua
->signature
== dub
->signature
;
9294 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9297 allocate_dwp_loaded_cutus_table ()
9299 return htab_up (htab_create_alloc (3,
9300 hash_dwp_loaded_cutus
,
9301 eq_dwp_loaded_cutus
,
9302 NULL
, xcalloc
, xfree
));
9305 /* Try to open DWP file FILE_NAME.
9306 The result is the bfd handle of the file.
9307 If there is a problem finding or opening the file, return NULL.
9308 Upon success, the canonicalized path of the file is stored in the bfd,
9309 same as symfile_bfd_open. */
9311 static gdb_bfd_ref_ptr
9312 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
9314 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
9320 /* Work around upstream bug 15652.
9321 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9322 [Whether that's a "bug" is debatable, but it is getting in our way.]
9323 We have no real idea where the dwp file is, because gdb's realpath-ing
9324 of the executable's path may have discarded the needed info.
9325 [IWBN if the dwp file name was recorded in the executable, akin to
9326 .gnu_debuglink, but that doesn't exist yet.]
9327 Strip the directory from FILE_NAME and search again. */
9328 if (!debug_file_directory
.empty ())
9330 /* Don't implicitly search the current directory here.
9331 If the user wants to search "." to handle this case,
9332 it must be added to debug-file-directory. */
9333 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
9341 /* Initialize the use of the DWP file for the current objfile.
9342 By convention the name of the DWP file is ${objfile}.dwp.
9343 The result is NULL if it can't be found. */
9345 static std::unique_ptr
<struct dwp_file
>
9346 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
9348 struct objfile
*objfile
= per_objfile
->objfile
;
9350 /* Try to find first .dwp for the binary file before any symbolic links
9353 /* If the objfile is a debug file, find the name of the real binary
9354 file and get the name of dwp file from there. */
9355 std::string dwp_name
;
9356 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
9358 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
9359 const char *backlink_basename
= lbasename (backlink
->original_name
);
9361 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
9364 dwp_name
= objfile
->original_name
;
9368 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
9370 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
9372 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9373 dwp_name
= objfile_name (objfile
);
9375 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
9380 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
9382 return std::unique_ptr
<dwp_file
> ();
9385 const char *name
= bfd_get_filename (dbfd
.get ());
9386 std::unique_ptr
<struct dwp_file
> dwp_file
9387 (new struct dwp_file (name
, std::move (dbfd
)));
9389 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
9390 dwp_file
->elf_sections
=
9391 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
9392 dwp_file
->num_sections
, asection
*);
9394 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
9395 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
9398 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
9400 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
9402 /* The DWP file version is stored in the hash table. Oh well. */
9403 if (dwp_file
->cus
&& dwp_file
->tus
9404 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
9406 /* Technically speaking, we should try to limp along, but this is
9407 pretty bizarre. We use pulongest here because that's the established
9408 portability solution (e.g, we cannot use %u for uint32_t). */
9409 error (_("Dwarf Error: DWP file CU version %s doesn't match"
9410 " TU version %s [in DWP file %s]"),
9411 pulongest (dwp_file
->cus
->version
),
9412 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
9416 dwp_file
->version
= dwp_file
->cus
->version
;
9417 else if (dwp_file
->tus
)
9418 dwp_file
->version
= dwp_file
->tus
->version
;
9420 dwp_file
->version
= 2;
9422 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
9424 if (dwp_file
->version
== 2)
9425 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
9428 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
9432 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
9433 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
9435 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
9436 dwarf_read_debug_printf (" %s CUs, %s TUs",
9437 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
9438 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
9440 bfd_cache_close (dwp_file
->dbfd
.get ());
9445 /* Wrapper around open_and_init_dwp_file, only open it once. */
9447 static struct dwp_file
*
9448 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
9450 if (!per_objfile
->per_bfd
->dwp_checked
)
9452 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
9453 per_objfile
->per_bfd
->dwp_checked
= 1;
9455 return per_objfile
->per_bfd
->dwp_file
.get ();
9458 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9459 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9460 or in the DWP file for the objfile, referenced by THIS_UNIT.
9461 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9462 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9464 This is called, for example, when wanting to read a variable with a
9465 complex location. Therefore we don't want to do file i/o for every call.
9466 Therefore we don't want to look for a DWO file on every call.
9467 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9468 then we check if we've already seen DWO_NAME, and only THEN do we check
9471 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9472 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9474 static struct dwo_unit
*
9475 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
9476 ULONGEST signature
, int is_debug_types
)
9478 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9479 struct objfile
*objfile
= per_objfile
->objfile
;
9480 const char *kind
= is_debug_types
? "TU" : "CU";
9481 void **dwo_file_slot
;
9482 struct dwo_file
*dwo_file
;
9483 struct dwp_file
*dwp_file
;
9485 /* First see if there's a DWP file.
9486 If we have a DWP file but didn't find the DWO inside it, don't
9487 look for the original DWO file. It makes gdb behave differently
9488 depending on whether one is debugging in the build tree. */
9490 dwp_file
= get_dwp_file (per_objfile
);
9491 if (dwp_file
!= NULL
)
9493 const struct dwp_hash_table
*dwp_htab
=
9494 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
9496 if (dwp_htab
!= NULL
)
9498 struct dwo_unit
*dwo_cutu
=
9499 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
9502 if (dwo_cutu
!= NULL
)
9504 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
9505 kind
, hex_string (signature
),
9506 host_address_to_string (dwo_cutu
));
9514 /* No DWP file, look for the DWO file. */
9516 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
9517 if (*dwo_file_slot
== NULL
)
9519 /* Read in the file and build a table of the CUs/TUs it contains. */
9520 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
9522 /* NOTE: This will be NULL if unable to open the file. */
9523 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
9525 if (dwo_file
!= NULL
)
9527 struct dwo_unit
*dwo_cutu
= NULL
;
9529 if (is_debug_types
&& dwo_file
->tus
)
9531 struct dwo_unit find_dwo_cutu
;
9533 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
9534 find_dwo_cutu
.signature
= signature
;
9536 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
9539 else if (!is_debug_types
&& dwo_file
->cus
)
9541 struct dwo_unit find_dwo_cutu
;
9543 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
9544 find_dwo_cutu
.signature
= signature
;
9545 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
9549 if (dwo_cutu
!= NULL
)
9551 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
9552 kind
, dwo_name
, hex_string (signature
),
9553 host_address_to_string (dwo_cutu
));
9560 /* We didn't find it. This could mean a dwo_id mismatch, or
9561 someone deleted the DWO/DWP file, or the search path isn't set up
9562 correctly to find the file. */
9564 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
9565 kind
, dwo_name
, hex_string (signature
));
9567 /* This is a warning and not a complaint because it can be caused by
9568 pilot error (e.g., user accidentally deleting the DWO). */
9570 /* Print the name of the DWP file if we looked there, helps the user
9571 better diagnose the problem. */
9572 std::string dwp_text
;
9574 if (dwp_file
!= NULL
)
9575 dwp_text
= string_printf (" [in DWP file %s]",
9576 lbasename (dwp_file
->name
));
9578 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
9580 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
9581 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
9586 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9587 See lookup_dwo_cutu_unit for details. */
9589 static struct dwo_unit
*
9590 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
9593 gdb_assert (!cu
->per_cu
->is_debug_types
);
9595 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
9598 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9599 See lookup_dwo_cutu_unit for details. */
9601 static struct dwo_unit
*
9602 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
9604 gdb_assert (cu
->per_cu
->is_debug_types
);
9606 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
9608 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
9611 /* Traversal function for queue_and_load_all_dwo_tus. */
9614 queue_and_load_dwo_tu (void **slot
, void *info
)
9616 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
9617 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
9618 ULONGEST signature
= dwo_unit
->signature
;
9619 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
9621 if (sig_type
!= NULL
)
9623 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9624 a real dependency of PER_CU on SIG_TYPE. That is detected later
9625 while processing PER_CU. */
9626 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
9628 load_full_type_unit (sig_type
, cu
->per_objfile
);
9629 cu
->per_cu
->imported_symtabs
.push_back (sig_type
);
9635 /* Queue all TUs contained in the DWO of CU to be read in.
9636 The DWO may have the only definition of the type, though it may not be
9637 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9638 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9641 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
9643 struct dwo_unit
*dwo_unit
;
9644 struct dwo_file
*dwo_file
;
9646 gdb_assert (cu
!= nullptr);
9647 gdb_assert (!cu
->per_cu
->is_debug_types
);
9648 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
9650 dwo_unit
= cu
->dwo_unit
;
9651 gdb_assert (dwo_unit
!= NULL
);
9653 dwo_file
= dwo_unit
->dwo_file
;
9654 if (dwo_file
->tus
!= NULL
)
9655 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
9658 /* Read in various DIEs. */
9660 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9661 Inherit only the children of the DW_AT_abstract_origin DIE not being
9662 already referenced by DW_AT_abstract_origin from the children of the
9666 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
9668 attribute
*attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
9669 if (attr
== nullptr)
9672 /* Note that following die references may follow to a die in a
9674 dwarf2_cu
*origin_cu
= cu
;
9676 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9677 die_info
*origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
9679 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9681 struct pending
**origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
9682 origin_cu
->list_in_scope
= cu
->list_in_scope
;
9684 if (die
->tag
!= origin_die
->tag
9685 && !(die
->tag
== DW_TAG_inlined_subroutine
9686 && origin_die
->tag
== DW_TAG_subprogram
))
9687 complaint (_("DIE %s and its abstract origin %s have different tags"),
9688 sect_offset_str (die
->sect_off
),
9689 sect_offset_str (origin_die
->sect_off
));
9691 /* Find if the concrete and abstract trees are structurally the
9692 same. This is a shallow traversal and it is not bullet-proof;
9693 the compiler can trick the debugger into believing that the trees
9694 are isomorphic, whereas they actually are not. However, the
9695 likelihood of this happening is pretty low, and a full-fledged
9696 check would be an overkill. */
9697 bool are_isomorphic
= true;
9698 die_info
*concrete_child
= die
->child
;
9699 die_info
*abstract_child
= origin_die
->child
;
9700 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
9702 if (concrete_child
== nullptr
9703 || abstract_child
== nullptr
9704 || concrete_child
->tag
!= abstract_child
->tag
)
9706 are_isomorphic
= false;
9710 concrete_child
= concrete_child
->sibling
;
9711 abstract_child
= abstract_child
->sibling
;
9714 /* Walk the origin's children in parallel to the concrete children.
9715 This helps match an origin child in case the debug info misses
9716 DW_AT_abstract_origin attributes. Keep in mind that the abstract
9717 origin tree may not have the same tree structure as the concrete
9719 die_info
*corresponding_abstract_child
9720 = are_isomorphic
? origin_die
->child
: nullptr;
9722 std::vector
<sect_offset
> offsets
;
9724 for (die_info
*child_die
= die
->child
;
9725 child_die
&& child_die
->tag
;
9726 child_die
= child_die
->sibling
)
9728 /* We are trying to process concrete instance entries:
9729 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
9730 it's not relevant to our analysis here. i.e. detecting DIEs that are
9731 present in the abstract instance but not referenced in the concrete
9733 if (child_die
->tag
== DW_TAG_call_site
9734 || child_die
->tag
== DW_TAG_GNU_call_site
)
9737 corresponding_abstract_child
9738 = corresponding_abstract_child
->sibling
;
9742 /* For each CHILD_DIE, find the corresponding child of
9743 ORIGIN_DIE. If there is more than one layer of
9744 DW_AT_abstract_origin, follow them all; there shouldn't be,
9745 but GCC versions at least through 4.4 generate this (GCC PR
9747 die_info
*child_origin_die
= child_die
;
9748 dwarf2_cu
*child_origin_cu
= cu
;
9751 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
9753 if (attr
== nullptr)
9756 die_info
*prev_child_origin_die
= child_origin_die
;
9757 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
9760 if (prev_child_origin_die
== child_origin_die
)
9762 /* Handle DIE with self-reference. */
9767 /* If missing DW_AT_abstract_origin, try the corresponding child
9768 of the origin. Clang emits such lexical scopes. */
9769 if (child_origin_die
== child_die
9770 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
9772 && child_die
->tag
== DW_TAG_lexical_block
)
9773 child_origin_die
= corresponding_abstract_child
;
9775 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9776 counterpart may exist. */
9777 if (child_origin_die
!= child_die
)
9779 if (child_die
->tag
!= child_origin_die
->tag
9780 && !(child_die
->tag
== DW_TAG_inlined_subroutine
9781 && child_origin_die
->tag
== DW_TAG_subprogram
))
9782 complaint (_("Child DIE %s and its abstract origin %s have "
9784 sect_offset_str (child_die
->sect_off
),
9785 sect_offset_str (child_origin_die
->sect_off
));
9786 if (child_origin_die
->parent
!= origin_die
)
9787 complaint (_("Child DIE %s and its abstract origin %s have "
9788 "different parents"),
9789 sect_offset_str (child_die
->sect_off
),
9790 sect_offset_str (child_origin_die
->sect_off
));
9792 offsets
.push_back (child_origin_die
->sect_off
);
9796 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
9799 if (!offsets
.empty ())
9801 std::sort (offsets
.begin (), offsets
.end ());
9803 for (auto offsets_it
= offsets
.begin () + 1;
9804 offsets_it
< offsets
.end ();
9806 if (*(offsets_it
- 1) == *offsets_it
)
9807 complaint (_("Multiple children of DIE %s refer "
9808 "to DIE %s as their abstract origin"),
9809 sect_offset_str (die
->sect_off
),
9810 sect_offset_str (*offsets_it
));
9813 auto offsets_it
= offsets
.begin ();
9814 die_info
*origin_child_die
= origin_die
->child
;
9815 while (origin_child_die
!= nullptr && origin_child_die
->tag
!= 0)
9817 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9818 while (offsets_it
< offsets
.end ()
9819 && *offsets_it
< origin_child_die
->sect_off
)
9822 if (offsets_it
== offsets
.end ()
9823 || *offsets_it
> origin_child_die
->sect_off
)
9825 /* Found that ORIGIN_CHILD_DIE is really not referenced.
9826 Check whether we're already processing ORIGIN_CHILD_DIE.
9827 This can happen with mutually referenced abstract_origins.
9829 if (!origin_child_die
->in_process
)
9830 process_die (origin_child_die
, origin_cu
);
9833 origin_child_die
= origin_child_die
->sibling
;
9836 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
9838 if (cu
!= origin_cu
)
9839 compute_delayed_physnames (origin_cu
);
9842 /* Return TRUE if the given DIE is the program's "main". DWARF 4 has
9843 defined a dedicated DW_AT_main_subprogram attribute to indicate the
9844 starting function of the program, however with older versions the
9845 DW_CC_program value of the DW_AT_calling_convention attribute was
9846 used instead as the only means available. We handle both variants. */
9849 dwarf2_func_is_main_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
9851 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
9853 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
9854 return (attr
!= nullptr
9855 && attr
->constant_value (DW_CC_normal
) == DW_CC_program
);
9858 /* A helper to handle Ada's "Pragma Import" feature when it is applied
9862 check_ada_pragma_import (struct die_info
*die
, struct dwarf2_cu
*cu
)
9864 /* A Pragma Import will have both a name and a linkage name. */
9865 const char *name
= dwarf2_name (die
, cu
);
9866 if (name
== nullptr)
9869 const char *linkage_name
= dw2_linkage_name (die
, cu
);
9870 /* Disallow the special Ada symbols. */
9871 if (!is_ada_import_or_export (cu
, name
, linkage_name
))
9874 /* A Pragma Import will be a declaration, while a Pragma Export will
9876 if (!die_is_declaration (die
, cu
))
9879 new_symbol (die
, read_type_die (die
, cu
), cu
);
9884 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9886 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9887 struct objfile
*objfile
= per_objfile
->objfile
;
9888 struct gdbarch
*gdbarch
= objfile
->arch ();
9889 struct context_stack
*newobj
;
9892 struct die_info
*child_die
;
9893 struct attribute
*attr
, *call_line
, *call_file
;
9895 struct block
*block
;
9896 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
9897 std::vector
<struct symbol
*> template_args
;
9898 struct template_symbol
*templ_func
= NULL
;
9902 /* If we do not have call site information, we can't show the
9903 caller of this inlined function. That's too confusing, so
9904 only use the scope for local variables. */
9905 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
9906 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
9907 if (call_line
== NULL
|| call_file
== NULL
)
9909 read_lexical_block_scope (die
, cu
);
9914 name
= dwarf2_name (die
, cu
);
9915 if (name
== nullptr)
9916 name
= dw2_linkage_name (die
, cu
);
9918 /* Ignore functions with missing or empty names. These are actually
9919 illegal according to the DWARF standard. */
9922 complaint (_("missing name for subprogram DIE at %s"),
9923 sect_offset_str (die
->sect_off
));
9927 if (check_ada_pragma_import (die
, cu
))
9929 /* We already made the symbol for the Pragma Import, and because
9930 it is a declaration, we know it won't have any other
9931 important information, so we can simply return. */
9935 /* Ignore functions with missing or invalid low and high pc attributes. */
9936 unrelocated_addr unrel_low
, unrel_high
;
9937 if (dwarf2_get_pc_bounds (die
, &unrel_low
, &unrel_high
, cu
, nullptr, nullptr)
9938 <= PC_BOUNDS_INVALID
)
9940 if (have_complaint ())
9942 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
9943 bool external_p
= attr
!= nullptr && attr
->as_boolean ();
9944 attr
= dwarf2_attr (die
, DW_AT_inline
, cu
);
9947 && attr
->is_nonnegative ()
9948 && (attr
->as_nonnegative () == DW_INL_inlined
9949 || attr
->as_nonnegative () == DW_INL_declared_inlined
));
9950 attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
9951 bool decl_p
= attr
!= nullptr && attr
->as_boolean ();
9952 if (!external_p
&& !inlined_p
&& !decl_p
)
9953 complaint (_("cannot get low and high bounds "
9954 "for subprogram DIE at %s"),
9955 sect_offset_str (die
->sect_off
));
9960 lowpc
= per_objfile
->relocate (unrel_low
);
9961 highpc
= per_objfile
->relocate (unrel_high
);
9963 if (gdbarch_bfd_arch_info (gdbarch
)->arch
== bfd_arch_arm
9964 && producer_is_gas_ge_2_39 (cu
))
9966 /* Gas version 2.39 produces DWARF for a Thumb subprogram with a low_pc
9967 attribute with the thumb bit set (PR gas/31115). Work around this. */
9968 lowpc
= gdbarch_addr_bits_remove (gdbarch
, lowpc
);
9969 highpc
= gdbarch_addr_bits_remove (gdbarch
, highpc
);
9972 /* If we have any template arguments, then we must allocate a
9973 different sort of symbol. */
9974 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
9976 if (child_die
->tag
== DW_TAG_template_type_param
9977 || child_die
->tag
== DW_TAG_template_value_param
)
9979 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
9980 templ_func
->subclass
= SYMBOL_TEMPLATE
;
9985 gdb_assert (cu
->get_builder () != nullptr);
9986 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
9987 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
, templ_func
);
9989 if (dwarf2_func_is_main_p (die
, cu
))
9990 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
9993 /* If there is a location expression for DW_AT_frame_base, record
9995 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
9996 if (attr
!= nullptr)
9997 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
9999 /* If there is a location for the static link, record it. */
10000 newobj
->static_link
= NULL
;
10001 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
10002 if (attr
!= nullptr)
10004 newobj
->static_link
10005 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
10006 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
10010 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
10012 if (die
->child
!= NULL
)
10014 child_die
= die
->child
;
10015 while (child_die
&& child_die
->tag
)
10017 if (child_die
->tag
== DW_TAG_template_type_param
10018 || child_die
->tag
== DW_TAG_template_value_param
)
10020 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
10023 template_args
.push_back (arg
);
10026 process_die (child_die
, cu
);
10027 child_die
= child_die
->sibling
;
10031 inherit_abstract_dies (die
, cu
);
10033 /* If we have a DW_AT_specification, we might need to import using
10034 directives from the context of the specification DIE. See the
10035 comment in determine_prefix. */
10036 if (cu
->lang () == language_cplus
10037 && dwarf2_attr (die
, DW_AT_specification
, cu
))
10039 struct dwarf2_cu
*spec_cu
= cu
;
10040 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
10044 child_die
= spec_die
->child
;
10045 while (child_die
&& child_die
->tag
)
10047 if (child_die
->tag
== DW_TAG_imported_module
)
10048 process_die (child_die
, spec_cu
);
10049 child_die
= child_die
->sibling
;
10052 /* In some cases, GCC generates specification DIEs that
10053 themselves contain DW_AT_specification attributes. */
10054 spec_die
= die_specification (spec_die
, &spec_cu
);
10058 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
10059 /* Make a block for the local symbols within. */
10060 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
10061 cstk
.static_link
, lowpc
, highpc
);
10063 /* For C++, set the block's scope. */
10064 if ((cu
->lang () == language_cplus
10065 || cu
->lang () == language_fortran
10066 || cu
->lang () == language_d
10067 || cu
->lang () == language_rust
)
10068 && cu
->processing_has_namespace_info
)
10069 block
->set_scope (determine_prefix (die
, cu
),
10070 &objfile
->objfile_obstack
);
10072 /* If we have address ranges, record them. */
10073 dwarf2_record_block_ranges (die
, block
, cu
);
10075 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
10077 /* Attach template arguments to function. */
10078 if (!template_args
.empty ())
10080 gdb_assert (templ_func
!= NULL
);
10082 templ_func
->n_template_arguments
= template_args
.size ();
10083 templ_func
->template_arguments
10084 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
10085 templ_func
->n_template_arguments
);
10086 memcpy (templ_func
->template_arguments
,
10087 template_args
.data (),
10088 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
10090 /* Make sure that the symtab is set on the new symbols. Even
10091 though they don't appear in this symtab directly, other parts
10092 of gdb assume that symbols do, and this is reasonably
10094 for (symbol
*sym
: template_args
)
10095 sym
->set_symtab (templ_func
->symtab ());
10098 /* In C++, we can have functions nested inside functions (e.g., when
10099 a function declares a class that has methods). This means that
10100 when we finish processing a function scope, we may need to go
10101 back to building a containing block's symbol lists. */
10102 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
10103 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
10105 /* If we've finished processing a top-level function, subsequent
10106 symbols go in the file symbol list. */
10107 if (cu
->get_builder ()->outermost_context_p ())
10108 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
10111 /* Process all the DIES contained within a lexical block scope. Start
10112 a new scope, process the dies, and then close the scope. */
10115 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
10117 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10118 CORE_ADDR lowpc
, highpc
;
10119 struct die_info
*child_die
;
10121 /* Ignore blocks with missing or invalid low and high pc attributes. */
10122 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10123 as multiple lexical blocks? Handling children in a sane way would
10124 be nasty. Might be easier to properly extend generic blocks to
10125 describe ranges. */
10126 unrelocated_addr unrel_low
, unrel_high
;
10127 switch (dwarf2_get_pc_bounds (die
, &unrel_low
, &unrel_high
, cu
,
10130 case PC_BOUNDS_NOT_PRESENT
:
10131 /* DW_TAG_lexical_block has no attributes, process its children as if
10132 there was no wrapping by that DW_TAG_lexical_block.
10133 GCC does no longer produces such DWARF since GCC r224161. */
10134 for (child_die
= die
->child
;
10135 child_die
!= NULL
&& child_die
->tag
;
10136 child_die
= child_die
->sibling
)
10138 /* We might already be processing this DIE. This can happen
10139 in an unusual circumstance -- where a subroutine A
10140 appears lexically in another subroutine B, but A actually
10141 inlines B. The recursion is broken here, rather than in
10142 inherit_abstract_dies, because it seems better to simply
10143 drop concrete children here. */
10144 if (!child_die
->in_process
)
10145 process_die (child_die
, cu
);
10148 case PC_BOUNDS_INVALID
:
10151 lowpc
= per_objfile
->relocate (unrel_low
);
10152 highpc
= per_objfile
->relocate (unrel_high
);
10154 cu
->get_builder ()->push_context (0, lowpc
);
10155 if (die
->child
!= NULL
)
10157 child_die
= die
->child
;
10158 while (child_die
&& child_die
->tag
)
10160 process_die (child_die
, cu
);
10161 child_die
= child_die
->sibling
;
10164 inherit_abstract_dies (die
, cu
);
10165 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
10167 if (*cu
->get_builder ()->get_local_symbols () != NULL
10168 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
10170 struct block
*block
10171 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
10172 cstk
.start_addr
, highpc
);
10174 /* Note that recording ranges after traversing children, as we
10175 do here, means that recording a parent's ranges entails
10176 walking across all its children's ranges as they appear in
10177 the address map, which is quadratic behavior.
10179 It would be nicer to record the parent's ranges before
10180 traversing its children, simply overriding whatever you find
10181 there. But since we don't even decide whether to create a
10182 block until after we've traversed its children, that's hard
10184 dwarf2_record_block_ranges (die
, block
, cu
);
10186 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
10187 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
10190 static void dwarf2_ranges_read_low_addrs
10192 struct dwarf2_cu
*cu
,
10194 std::vector
<unrelocated_addr
> &result
);
10196 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
10199 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
10201 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10202 struct objfile
*objfile
= per_objfile
->objfile
;
10203 struct gdbarch
*gdbarch
= objfile
->arch ();
10204 struct attribute
*attr
;
10207 struct die_info
*child_die
;
10209 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
10212 /* This was a pre-DWARF-5 GNU extension alias
10213 for DW_AT_call_return_pc. */
10214 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
10218 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
10219 "DIE %s [in module %s]"),
10220 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10223 unrelocated_addr pc
= attr
->as_address ();
10225 if (cu
->call_site_htab
== NULL
)
10226 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
10227 call_site::eq
, NULL
,
10228 &objfile
->objfile_obstack
,
10229 hashtab_obstack_allocate
, NULL
);
10230 struct call_site
call_site_local (pc
, nullptr, nullptr);
10231 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
10234 complaint (_("Duplicate PC %s for DW_TAG_call_site "
10235 "DIE %s [in module %s]"),
10236 paddress (gdbarch
, (CORE_ADDR
) pc
), sect_offset_str (die
->sect_off
),
10237 objfile_name (objfile
));
10241 /* Count parameters at the caller. */
10244 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
10245 child_die
= child_die
->sibling
)
10247 if (child_die
->tag
!= DW_TAG_call_site_parameter
10248 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
10250 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
10251 "DW_TAG_call_site child DIE %s [in module %s]"),
10252 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
10253 objfile_name (objfile
));
10260 struct call_site
*call_site
10261 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
10263 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
10264 struct call_site (pc
, cu
->per_cu
, per_objfile
);
10267 /* We never call the destructor of call_site, so we must ensure it is
10268 trivially destructible. */
10269 static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
10271 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
10272 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
10274 struct die_info
*func_die
;
10276 /* Skip also over DW_TAG_inlined_subroutine. */
10277 for (func_die
= die
->parent
;
10278 func_die
&& func_die
->tag
!= DW_TAG_subprogram
10279 && func_die
->tag
!= DW_TAG_subroutine_type
;
10280 func_die
= func_die
->parent
);
10282 /* DW_AT_call_all_calls is a superset
10283 of DW_AT_call_all_tail_calls. */
10285 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
10286 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
10287 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
10288 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
10290 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10291 not complete. But keep CALL_SITE for look ups via call_site_htab,
10292 both the initial caller containing the real return address PC and
10293 the final callee containing the current PC of a chain of tail
10294 calls do not need to have the tail call list complete. But any
10295 function candidate for a virtual tail call frame searched via
10296 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10297 determined unambiguously. */
10301 struct type
*func_type
= NULL
;
10304 func_type
= get_die_type (func_die
, cu
);
10305 if (func_type
!= NULL
)
10307 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
10309 /* Enlist this call site to the function. */
10310 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
10311 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
10314 complaint (_("Cannot find function owning DW_TAG_call_site "
10315 "DIE %s [in module %s]"),
10316 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10320 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
10322 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
10324 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
10327 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
10328 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
10331 call_site
->target
.set_loc_dwarf_block (nullptr);
10332 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
10333 /* Keep NULL DWARF_BLOCK. */;
10334 else if (attr
->form_is_block ())
10336 struct dwarf2_locexpr_baton
*dlbaton
;
10337 struct dwarf_block
*block
= attr
->as_block ();
10339 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
10340 dlbaton
->data
= block
->data
;
10341 dlbaton
->size
= block
->size
;
10342 dlbaton
->per_objfile
= per_objfile
;
10343 dlbaton
->per_cu
= cu
->per_cu
;
10345 call_site
->target
.set_loc_dwarf_block (dlbaton
);
10347 else if (attr
->form_is_ref ())
10349 struct dwarf2_cu
*target_cu
= cu
;
10350 struct die_info
*target_die
;
10352 target_die
= follow_die_ref (die
, attr
, &target_cu
);
10353 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
10355 struct attribute
*ranges_attr
10356 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
10358 if (die_is_declaration (target_die
, target_cu
))
10360 const char *target_physname
;
10362 /* Prefer the mangled name; otherwise compute the demangled one. */
10363 target_physname
= dw2_linkage_name (target_die
, target_cu
);
10364 if (target_physname
== NULL
)
10365 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
10366 if (target_physname
== NULL
)
10367 complaint (_("DW_AT_call_target target DIE has invalid "
10368 "physname, for referencing DIE %s [in module %s]"),
10369 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10371 call_site
->target
.set_loc_physname (target_physname
);
10373 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
10375 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
10376 + target_cu
->gnu_ranges_base
);
10377 std::vector
<unrelocated_addr
> addresses
;
10378 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
10379 target_die
->tag
, addresses
);
10380 unrelocated_addr
*saved
= XOBNEWVEC (&objfile
->objfile_obstack
,
10382 addresses
.size ());
10383 std::copy (addresses
.begin (), addresses
.end (), saved
);
10384 call_site
->target
.set_loc_array (addresses
.size (), saved
);
10388 unrelocated_addr lowpc
;
10390 /* DW_AT_entry_pc should be preferred. */
10391 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
10393 <= PC_BOUNDS_INVALID
)
10394 complaint (_("DW_AT_call_target target DIE has invalid "
10395 "low pc, for referencing DIE %s [in module %s]"),
10396 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10398 call_site
->target
.set_loc_physaddr (lowpc
);
10402 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
10403 "block nor reference, for DIE %s [in module %s]"),
10404 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
10406 for (child_die
= die
->child
;
10407 child_die
&& child_die
->tag
;
10408 child_die
= child_die
->sibling
)
10410 struct call_site_parameter
*parameter
;
10411 struct attribute
*loc
, *origin
;
10413 if (child_die
->tag
!= DW_TAG_call_site_parameter
10414 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
10416 /* Already printed the complaint above. */
10420 gdb_assert (call_site
->parameter_count
< nparams
);
10421 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
10423 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10424 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10425 register is contained in DW_AT_call_value. */
10427 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
10428 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
10429 if (origin
== NULL
)
10431 /* This was a pre-DWARF-5 GNU extension alias
10432 for DW_AT_call_parameter. */
10433 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
10435 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
10437 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
10439 sect_offset sect_off
= origin
->get_ref_die_offset ();
10440 if (!cu
->header
.offset_in_cu_p (sect_off
))
10442 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10443 binding can be done only inside one CU. Such referenced DIE
10444 therefore cannot be even moved to DW_TAG_partial_unit. */
10445 complaint (_("DW_AT_call_parameter offset is not in CU for "
10446 "DW_TAG_call_site child DIE %s [in module %s]"),
10447 sect_offset_str (child_die
->sect_off
),
10448 objfile_name (objfile
));
10451 parameter
->u
.param_cu_off
10452 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
10454 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
10456 complaint (_("No DW_FORM_block* DW_AT_location for "
10457 "DW_TAG_call_site child DIE %s [in module %s]"),
10458 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
10463 struct dwarf_block
*block
= loc
->as_block ();
10465 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
10466 (block
->data
, &block
->data
[block
->size
]);
10467 if (parameter
->u
.dwarf_reg
!= -1)
10468 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
10469 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
10470 &block
->data
[block
->size
],
10471 ¶meter
->u
.fb_offset
))
10472 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
10475 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
10476 "for DW_FORM_block* DW_AT_location is supported for "
10477 "DW_TAG_call_site child DIE %s "
10479 sect_offset_str (child_die
->sect_off
),
10480 objfile_name (objfile
));
10485 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
10487 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
10488 if (attr
== NULL
|| !attr
->form_is_block ())
10490 complaint (_("No DW_FORM_block* DW_AT_call_value for "
10491 "DW_TAG_call_site child DIE %s [in module %s]"),
10492 sect_offset_str (child_die
->sect_off
),
10493 objfile_name (objfile
));
10497 struct dwarf_block
*block
= attr
->as_block ();
10498 parameter
->value
= block
->data
;
10499 parameter
->value_size
= block
->size
;
10501 /* Parameters are not pre-cleared by memset above. */
10502 parameter
->data_value
= NULL
;
10503 parameter
->data_value_size
= 0;
10504 call_site
->parameter_count
++;
10506 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
10508 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
10509 if (attr
!= nullptr)
10511 if (!attr
->form_is_block ())
10512 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
10513 "DW_TAG_call_site child DIE %s [in module %s]"),
10514 sect_offset_str (child_die
->sect_off
),
10515 objfile_name (objfile
));
10518 block
= attr
->as_block ();
10519 parameter
->data_value
= block
->data
;
10520 parameter
->data_value_size
= block
->size
;
10526 /* Helper function for read_variable. If DIE represents a virtual
10527 table, then return the type of the concrete object that is
10528 associated with the virtual table. Otherwise, return NULL. */
10530 static struct type
*
10531 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
10533 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
10537 /* Find the type DIE. */
10538 struct die_info
*type_die
= NULL
;
10539 struct dwarf2_cu
*type_cu
= cu
;
10541 if (attr
->form_is_ref ())
10542 type_die
= follow_die_ref (die
, attr
, &type_cu
);
10543 if (type_die
== NULL
)
10546 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
10548 return die_containing_type (type_die
, type_cu
);
10551 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
10554 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
10556 struct rust_vtable_symbol
*storage
= NULL
;
10558 if (cu
->lang () == language_rust
)
10560 struct type
*containing_type
= rust_containing_type (die
, cu
);
10562 if (containing_type
!= NULL
)
10564 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
10566 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
10567 storage
->concrete_type
= containing_type
;
10568 storage
->subclass
= SYMBOL_RUST_VTABLE
;
10572 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
10573 struct attribute
*abstract_origin
10574 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
10575 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
10576 if (res
== NULL
&& loc
&& abstract_origin
)
10578 /* We have a variable without a name, but with a location and an abstract
10579 origin. This may be a concrete instance of an abstract variable
10580 referenced from an DW_OP_GNU_variable_value, so save it to find it back
10582 struct dwarf2_cu
*origin_cu
= cu
;
10583 struct die_info
*origin_die
10584 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
10585 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10586 per_objfile
->per_bfd
->abstract_to_concrete
10587 [origin_die
->sect_off
].push_back (die
->sect_off
);
10591 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
10592 reading .debug_rnglists.
10593 Callback's type should be:
10594 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
10595 Return true if the attributes are present and valid, otherwise,
10598 template <typename Callback
>
10600 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
10601 dwarf_tag tag
, Callback
&&callback
)
10603 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10604 struct objfile
*objfile
= per_objfile
->objfile
;
10605 bfd
*obfd
= objfile
->obfd
.get ();
10606 /* Base address selection entry. */
10607 std::optional
<unrelocated_addr
> base
;
10608 const gdb_byte
*buffer
;
10609 bool overflow
= false;
10610 ULONGEST addr_index
;
10611 struct dwarf2_section_info
*rnglists_section
;
10613 base
= cu
->base_address
;
10614 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
10615 rnglists_section
->read (objfile
);
10617 if (offset
>= rnglists_section
->size
)
10619 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10623 buffer
= rnglists_section
->buffer
+ offset
;
10627 /* Initialize it due to a false compiler warning. */
10628 unrelocated_addr range_beginning
= {}, range_end
= {};
10629 const gdb_byte
*buf_end
= (rnglists_section
->buffer
10630 + rnglists_section
->size
);
10631 unsigned int bytes_read
;
10633 if (buffer
== buf_end
)
10638 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
10641 case DW_RLE_end_of_list
:
10643 case DW_RLE_base_address
:
10644 if (buffer
+ cu
->header
.addr_size
> buf_end
)
10649 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
10650 buffer
+= bytes_read
;
10652 case DW_RLE_base_addressx
:
10653 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10654 buffer
+= bytes_read
;
10655 base
= read_addr_index (cu
, addr_index
);
10657 case DW_RLE_start_length
:
10658 if (buffer
+ cu
->header
.addr_size
> buf_end
)
10663 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
10665 buffer
+= bytes_read
;
10667 = (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10668 + read_unsigned_leb128 (obfd
, buffer
,
10670 buffer
+= bytes_read
;
10671 if (buffer
> buf_end
)
10677 case DW_RLE_startx_length
:
10678 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10679 buffer
+= bytes_read
;
10680 range_beginning
= read_addr_index (cu
, addr_index
);
10681 if (buffer
> buf_end
)
10687 = (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10688 + read_unsigned_leb128 (obfd
, buffer
,
10690 buffer
+= bytes_read
;
10692 case DW_RLE_offset_pair
:
10693 range_beginning
= (unrelocated_addr
) read_unsigned_leb128 (obfd
, buffer
,
10695 buffer
+= bytes_read
;
10696 if (buffer
> buf_end
)
10701 range_end
= (unrelocated_addr
) read_unsigned_leb128 (obfd
, buffer
,
10703 buffer
+= bytes_read
;
10704 if (buffer
> buf_end
)
10710 case DW_RLE_start_end
:
10711 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
10716 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
10718 buffer
+= bytes_read
;
10719 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
10720 buffer
+= bytes_read
;
10722 case DW_RLE_startx_endx
:
10723 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10724 buffer
+= bytes_read
;
10725 range_beginning
= read_addr_index (cu
, addr_index
);
10726 if (buffer
> buf_end
)
10731 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
10732 buffer
+= bytes_read
;
10733 range_end
= read_addr_index (cu
, addr_index
);
10736 complaint (_("Invalid .debug_rnglists data (no base address)"));
10739 if (rlet
== DW_RLE_end_of_list
|| overflow
)
10741 if (rlet
== DW_RLE_base_address
)
10744 if (range_beginning
> range_end
)
10746 /* Inverted range entries are invalid. */
10747 complaint (_("Invalid .debug_rnglists data (inverted range)"));
10751 /* Empty range entries have no effect. */
10752 if (range_beginning
== range_end
)
10755 /* Only DW_RLE_offset_pair needs the base address added. */
10756 if (rlet
== DW_RLE_offset_pair
)
10758 if (!base
.has_value ())
10760 /* We have no valid base address for the DW_RLE_offset_pair. */
10761 complaint (_("Invalid .debug_rnglists data (no base address for "
10762 "DW_RLE_offset_pair)"));
10766 range_beginning
= (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10767 + (CORE_ADDR
) *base
);
10768 range_end
= (unrelocated_addr
) ((CORE_ADDR
) range_end
10769 + (CORE_ADDR
) *base
);
10772 /* A not-uncommon case of bad debug info.
10773 Don't pollute the addrmap with bad data. */
10774 if (range_beginning
== (unrelocated_addr
) 0
10775 && !per_objfile
->per_bfd
->has_section_at_zero
)
10777 complaint (_(".debug_rnglists entry has start address of zero"
10778 " [in module %s]"), objfile_name (objfile
));
10782 callback (range_beginning
, range_end
);
10787 complaint (_("Offset %d is not terminated "
10788 "for DW_AT_ranges attribute"),
10796 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
10797 Callback's type should be:
10798 void (unrelocated_addr range_beginning, unrelocated_addr range_end)
10799 Return 1 if the attributes are present and valid, otherwise, return 0. */
10801 template <typename Callback
>
10803 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
10804 Callback
&&callback
)
10806 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
10807 struct objfile
*objfile
= per_objfile
->objfile
;
10808 struct comp_unit_head
*cu_header
= &cu
->header
;
10809 bfd
*obfd
= objfile
->obfd
.get ();
10810 unsigned int addr_size
= cu_header
->addr_size
;
10811 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
10812 /* Base address selection entry. */
10813 std::optional
<unrelocated_addr
> base
;
10814 unsigned int dummy
;
10815 const gdb_byte
*buffer
;
10817 if (cu_header
->version
>= 5)
10818 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
10820 base
= cu
->base_address
;
10822 per_objfile
->per_bfd
->ranges
.read (objfile
);
10823 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
10825 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
10829 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
10833 unrelocated_addr range_beginning
, range_end
;
10835 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
10836 buffer
+= addr_size
;
10837 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
10838 buffer
+= addr_size
;
10839 offset
+= 2 * addr_size
;
10841 /* An end of list marker is a pair of zero addresses. */
10842 if (range_beginning
== (unrelocated_addr
) 0
10843 && range_end
== (unrelocated_addr
) 0)
10844 /* Found the end of list entry. */
10847 /* Each base address selection entry is a pair of 2 values.
10848 The first is the largest possible address, the second is
10849 the base address. Check for a base address here. */
10850 if (((CORE_ADDR
) range_beginning
& mask
) == mask
)
10852 /* If we found the largest possible address, then we already
10853 have the base address in range_end. */
10858 if (!base
.has_value ())
10860 /* We have no valid base address for the ranges
10862 complaint (_("Invalid .debug_ranges data (no base address)"));
10866 if (range_beginning
> range_end
)
10868 /* Inverted range entries are invalid. */
10869 complaint (_("Invalid .debug_ranges data (inverted range)"));
10873 /* Empty range entries have no effect. */
10874 if (range_beginning
== range_end
)
10877 range_beginning
= (unrelocated_addr
) ((CORE_ADDR
) range_beginning
10878 + (CORE_ADDR
) *base
);
10879 range_end
= (unrelocated_addr
) ((CORE_ADDR
) range_end
10880 + (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_ranges entry has start address of zero"
10888 " [in module %s]"), objfile_name (objfile
));
10892 callback (range_beginning
, range_end
);
10898 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10899 Return 1 if the attributes are present and valid, otherwise, return 0.
10900 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
10901 ranges in MAP are set, using DATUM as the value. */
10904 dwarf2_ranges_read (unsigned offset
, unrelocated_addr
*low_return
,
10905 unrelocated_addr
*high_return
, struct dwarf2_cu
*cu
,
10906 addrmap_mutable
*map
, void *datum
, dwarf_tag tag
)
10909 unrelocated_addr low
= {};
10910 unrelocated_addr high
= {};
10913 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
10914 [&] (unrelocated_addr range_beginning
, unrelocated_addr range_end
)
10916 if (map
!= nullptr)
10918 /* addrmap only accepts CORE_ADDR, so we must cast here. */
10919 map
->set_empty ((CORE_ADDR
) range_beginning
,
10920 (CORE_ADDR
) range_end
- 1,
10924 /* FIXME: This is recording everything as a low-high
10925 segment of consecutive addresses. We should have a
10926 data structure for discontiguous block ranges
10930 low
= range_beginning
;
10936 if (range_beginning
< low
)
10937 low
= range_beginning
;
10938 if (range_end
> high
)
10946 /* If the first entry is an end-of-list marker, the range
10947 describes an empty scope, i.e. no instructions. */
10953 *high_return
= high
;
10957 /* Process ranges and fill in a vector of the low PC values only. */
10960 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
10962 std::vector
<unrelocated_addr
> &result
)
10964 dwarf2_ranges_process (offset
, cu
, tag
,
10965 [&] (unrelocated_addr start
, unrelocated_addr end
)
10967 result
.push_back (start
);
10971 /* Determine the low and high pc of a DW_TAG_entry_point. */
10973 static pc_bounds_kind
10974 dwarf2_get_pc_bounds_entry_point (die_info
*die
, unrelocated_addr
*low
,
10975 unrelocated_addr
*high
, dwarf2_cu
*cu
)
10977 gdb_assert (low
!= nullptr);
10978 gdb_assert (high
!= nullptr);
10980 if (die
->parent
->tag
!= DW_TAG_subprogram
)
10982 complaint (_("DW_TAG_entry_point not embedded in DW_TAG_subprogram"));
10983 return PC_BOUNDS_INVALID
;
10986 /* A DW_TAG_entry_point is embedded in an subprogram. Therefore, we can use
10987 the highpc from its enveloping subprogram and get the lowpc from
10989 const enum pc_bounds_kind bounds_kind
= dwarf2_get_pc_bounds (die
->parent
,
10993 if (bounds_kind
== PC_BOUNDS_INVALID
|| bounds_kind
== PC_BOUNDS_NOT_PRESENT
)
10994 return bounds_kind
;
10996 attribute
*attr_low
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
10999 complaint (_("DW_TAG_entry_point is missing DW_AT_low_pc"));
11000 return PC_BOUNDS_INVALID
;
11002 *low
= attr_low
->as_address ();
11003 return bounds_kind
;
11006 /* Determine the low and high pc using the DW_AT_low_pc and DW_AT_high_pc or
11007 DW_AT_ranges attributes of a DIE. */
11009 static pc_bounds_kind
11010 dwarf_get_pc_bounds_ranges_or_highlow_pc (die_info
*die
, unrelocated_addr
*low
,
11011 unrelocated_addr
*high
, dwarf2_cu
*cu
,
11012 addrmap_mutable
*map
, void *datum
)
11014 gdb_assert (low
!= nullptr);
11015 gdb_assert (high
!= nullptr);
11017 struct attribute
*attr
;
11018 struct attribute
*attr_high
;
11019 enum pc_bounds_kind ret
;
11021 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
11024 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
11025 if (attr
!= nullptr)
11027 *low
= attr
->as_address ();
11028 *high
= attr_high
->as_address ();
11029 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
11030 *high
= (unrelocated_addr
) ((ULONGEST
) *high
+ (ULONGEST
) *low
);
11032 /* Found consecutive range of addresses. */
11033 ret
= PC_BOUNDS_HIGH_LOW
;
11037 /* Found high w/o low attribute. */
11038 ret
= PC_BOUNDS_INVALID
;
11043 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
11044 if (attr
!= nullptr && attr
->form_is_unsigned ())
11046 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11047 on DWARF version). */
11048 ULONGEST ranges_offset
= attr
->as_unsigned ();
11050 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11052 if (die
->tag
!= DW_TAG_compile_unit
)
11053 ranges_offset
+= cu
->gnu_ranges_base
;
11055 /* Value of the DW_AT_ranges attribute is the offset in the
11056 .debug_ranges section. */
11057 if (!dwarf2_ranges_read (ranges_offset
, low
, high
, cu
,
11058 map
, datum
, die
->tag
))
11059 return PC_BOUNDS_INVALID
;
11060 /* Found discontinuous range of addresses. */
11061 ret
= PC_BOUNDS_RANGES
;
11065 /* Could not find high_pc or ranges attributed and thus no bounds
11067 ret
= PC_BOUNDS_NOT_PRESENT
;
11074 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
11075 definition for the return value. *LOWPC and *HIGHPC are set iff
11076 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
11078 static enum pc_bounds_kind
11079 dwarf2_get_pc_bounds (struct die_info
*die
, unrelocated_addr
*lowpc
,
11080 unrelocated_addr
*highpc
, struct dwarf2_cu
*cu
,
11081 addrmap_mutable
*map
, void *datum
)
11083 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11085 unrelocated_addr low
= {};
11086 unrelocated_addr high
= {};
11087 enum pc_bounds_kind ret
;
11089 if (die
->tag
== DW_TAG_entry_point
)
11090 ret
= dwarf2_get_pc_bounds_entry_point (die
, &low
, &high
, cu
);
11092 ret
= dwarf_get_pc_bounds_ranges_or_highlow_pc (die
, &low
, &high
, cu
, map
,
11095 if (ret
== PC_BOUNDS_NOT_PRESENT
|| ret
== PC_BOUNDS_INVALID
)
11098 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
11100 return PC_BOUNDS_INVALID
;
11102 /* When using the GNU linker, .gnu.linkonce. sections are used to
11103 eliminate duplicate copies of functions and vtables and such.
11104 The linker will arbitrarily choose one and discard the others.
11105 The AT_*_pc values for such functions refer to local labels in
11106 these sections. If the section from that file was discarded, the
11107 labels are not in the output, so the relocs get a value of 0.
11108 If this is a discarded function, mark the pc bounds as invalid,
11109 so that GDB will ignore it. */
11110 if (low
== (unrelocated_addr
) 0
11111 && !per_objfile
->per_bfd
->has_section_at_zero
)
11112 return PC_BOUNDS_INVALID
;
11114 gdb_assert (lowpc
!= nullptr);
11116 if (highpc
!= nullptr)
11121 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11122 its low and high PC addresses. Do nothing if these addresses could not
11123 be determined. Otherwise, set LOWPC to the low address if it is smaller,
11124 and HIGHPC to the high address if greater than HIGHPC. */
11127 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
11128 unrelocated_addr
*lowpc
,
11129 unrelocated_addr
*highpc
,
11130 struct dwarf2_cu
*cu
)
11132 unrelocated_addr low
, high
;
11133 struct die_info
*child
= die
->child
;
11135 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
11136 >= PC_BOUNDS_RANGES
)
11138 *lowpc
= std::min (*lowpc
, low
);
11139 *highpc
= std::max (*highpc
, high
);
11142 /* If the language does not allow nested subprograms (either inside
11143 subprograms or lexical blocks), we're done. */
11144 if (cu
->lang () != language_ada
)
11147 /* Check all the children of the given DIE. If it contains nested
11148 subprograms, then check their pc bounds. Likewise, we need to
11149 check lexical blocks as well, as they may also contain subprogram
11151 while (child
&& child
->tag
)
11153 if (child
->tag
== DW_TAG_subprogram
11154 || child
->tag
== DW_TAG_lexical_block
)
11155 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
11156 child
= child
->sibling
;
11160 /* Get the low and high pc's represented by the scope DIE, and store
11161 them in *LOWPC and *HIGHPC. If the correct values can't be
11162 determined, set *LOWPC to -1 and *HIGHPC to 0. */
11165 get_scope_pc_bounds (struct die_info
*die
,
11166 unrelocated_addr
*lowpc
, unrelocated_addr
*highpc
,
11167 struct dwarf2_cu
*cu
)
11169 unrelocated_addr best_low
= (unrelocated_addr
) -1;
11170 unrelocated_addr best_high
= {};
11171 unrelocated_addr current_low
, current_high
;
11173 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
11175 >= PC_BOUNDS_RANGES
)
11177 best_low
= current_low
;
11178 best_high
= current_high
;
11182 struct die_info
*child
= die
->child
;
11184 while (child
&& child
->tag
)
11186 switch (child
->tag
) {
11187 case DW_TAG_subprogram
:
11188 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
11190 case DW_TAG_namespace
:
11191 case DW_TAG_module
:
11192 /* FIXME: carlton/2004-01-16: Should we do this for
11193 DW_TAG_class_type/DW_TAG_structure_type, too? I think
11194 that current GCC's always emit the DIEs corresponding
11195 to definitions of methods of classes as children of a
11196 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11197 the DIEs giving the declarations, which could be
11198 anywhere). But I don't see any reason why the
11199 standards says that they have to be there. */
11200 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
11202 if (current_low
!= ((unrelocated_addr
) -1))
11204 best_low
= std::min (best_low
, current_low
);
11205 best_high
= std::max (best_high
, current_high
);
11213 child
= child
->sibling
;
11218 *highpc
= best_high
;
11221 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11225 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
11226 struct dwarf2_cu
*cu
)
11228 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11229 struct objfile
*objfile
= per_objfile
->objfile
;
11230 struct attribute
*attr
;
11231 struct attribute
*attr_high
;
11233 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
11236 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
11237 if (attr
!= nullptr)
11239 unrelocated_addr unrel_low
= attr
->as_address ();
11240 unrelocated_addr unrel_high
= attr_high
->as_address ();
11242 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
11243 unrel_high
= (unrelocated_addr
) ((ULONGEST
) unrel_high
11244 + (ULONGEST
) unrel_low
);
11246 CORE_ADDR low
= per_objfile
->relocate (unrel_low
);
11247 CORE_ADDR high
= per_objfile
->relocate (unrel_high
);
11248 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
11252 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
11253 if (attr
!= nullptr && attr
->form_is_unsigned ())
11255 /* Offset in the .debug_ranges or .debug_rnglist section (depending
11256 on DWARF version). */
11257 ULONGEST ranges_offset
= attr
->as_unsigned ();
11259 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
11261 if (die
->tag
!= DW_TAG_compile_unit
)
11262 ranges_offset
+= cu
->gnu_ranges_base
;
11264 std::vector
<blockrange
> blockvec
;
11265 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
11266 [&] (unrelocated_addr start
, unrelocated_addr end
)
11268 CORE_ADDR abs_start
= per_objfile
->relocate (start
);
11269 CORE_ADDR abs_end
= per_objfile
->relocate (end
);
11270 cu
->get_builder ()->record_block_range (block
, abs_start
,
11272 blockvec
.emplace_back (abs_start
, abs_end
);
11275 block
->set_ranges (make_blockranges (objfile
, blockvec
));
11279 /* Check whether the producer field indicates either of GCC < 4.6, or the
11280 Intel C/C++ compiler, and cache the result in CU. */
11283 check_producer (struct dwarf2_cu
*cu
)
11287 if (cu
->producer
== NULL
)
11289 /* For unknown compilers expect their behavior is DWARF version
11292 GCC started to support .debug_types sections by -gdwarf-4 since
11293 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11294 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11295 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11296 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11298 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
11300 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
11301 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
11302 cu
->producer_is_gcc_11
= major
== 11;
11304 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
11306 cu
->producer_is_icc
= true;
11307 cu
->producer_is_icc_lt_14
= major
< 14;
11309 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
11310 cu
->producer_is_codewarrior
= true;
11311 else if (producer_is_clang (cu
->producer
, &major
, &minor
))
11312 cu
->producer_is_clang
= true;
11313 else if (producer_is_gas (cu
->producer
, &major
, &minor
))
11315 cu
->producer_is_gas_lt_2_38
= major
< 2 || (major
== 2 && minor
< 38);
11316 cu
->producer_is_gas_2_39
= major
== 2 && minor
== 39;
11317 cu
->producer_is_gas_ge_2_40
= major
> 2 || (major
== 2 && minor
>= 40);
11321 /* For other non-GCC compilers, expect their behavior is DWARF version
11325 cu
->checked_producer
= true;
11328 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11329 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11330 during 4.6.0 experimental. */
11333 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
11335 if (!cu
->checked_producer
)
11336 check_producer (cu
);
11338 return cu
->producer_is_gxx_lt_4_6
;
11342 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
11343 with incorrect is_stmt attributes. */
11346 producer_is_codewarrior (struct dwarf2_cu
*cu
)
11348 if (!cu
->checked_producer
)
11349 check_producer (cu
);
11351 return cu
->producer_is_codewarrior
;
11355 producer_is_gas_lt_2_38 (struct dwarf2_cu
*cu
)
11357 if (!cu
->checked_producer
)
11358 check_producer (cu
);
11360 return cu
->producer_is_gas_lt_2_38
;
11364 producer_is_gas_2_39 (struct dwarf2_cu
*cu
)
11366 if (!cu
->checked_producer
)
11367 check_producer (cu
);
11369 return cu
->producer_is_gas_2_39
;
11372 /* Return true if CU is produced by GAS 2.39 or later. */
11375 producer_is_gas_ge_2_39 (struct dwarf2_cu
*cu
)
11377 if (!cu
->checked_producer
)
11378 check_producer (cu
);
11380 return cu
->producer_is_gas_2_39
|| cu
->producer_is_gas_ge_2_40
;
11383 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
11384 If that attribute is not available, return the appropriate
11387 static enum dwarf_access_attribute
11388 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
11390 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
11391 if (attr
!= nullptr)
11393 LONGEST value
= attr
->constant_value (-1);
11394 if (value
== DW_ACCESS_public
11395 || value
== DW_ACCESS_protected
11396 || value
== DW_ACCESS_private
)
11397 return (dwarf_access_attribute
) value
;
11398 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
11402 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
11404 /* The default DWARF 2 accessibility for members is public, the default
11405 accessibility for inheritance is private. */
11407 if (die
->tag
!= DW_TAG_inheritance
)
11408 return DW_ACCESS_public
;
11410 return DW_ACCESS_private
;
11414 /* DWARF 3+ defines the default accessibility a different way. The same
11415 rules apply now for DW_TAG_inheritance as for the members and it only
11416 depends on the container kind. */
11418 if (die
->parent
->tag
== DW_TAG_class_type
)
11419 return DW_ACCESS_private
;
11421 return DW_ACCESS_public
;
11425 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
11426 *OFFSET to the byte offset. If the attribute was not found return
11427 0, otherwise return 1. If it was found but could not properly be
11428 handled, set *OFFSET to 0. */
11431 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
11434 struct attribute
*attr
;
11436 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
11442 /* Note that we do not check for a section offset first here.
11443 This is because DW_AT_data_member_location is new in DWARF 4,
11444 so if we see it, we can assume that a constant form is really
11445 a constant and not a section offset. */
11446 if (attr
->form_is_constant ())
11447 *offset
= attr
->constant_value (0);
11448 else if (attr
->form_is_section_offset ())
11449 dwarf2_complex_location_expr_complaint ();
11450 else if (attr
->form_is_block ()
11451 && decode_locdesc (attr
->as_block (), cu
, &temp
))
11456 dwarf2_complex_location_expr_complaint ();
11462 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
11463 if (attr
!= nullptr)
11465 *offset
= attr
->constant_value (0);
11473 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
11474 store the results in FIELD. */
11477 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
11478 struct field
*field
)
11480 struct attribute
*attr
;
11482 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
11485 if (attr
->form_is_constant ())
11487 LONGEST offset
= attr
->constant_value (0);
11489 /* Work around this GCC 11 bug, where it would erroneously use -1
11490 data member locations, instead of 0:
11492 Negative DW_AT_data_member_location
11493 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
11495 if (offset
== -1 && cu
->producer_is_gcc_11
)
11497 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
11501 field
->set_loc_bitpos (offset
* bits_per_byte
);
11503 else if (attr
->form_is_section_offset ())
11504 dwarf2_complex_location_expr_complaint ();
11505 else if (attr
->form_is_block ())
11508 if (decode_locdesc (attr
->as_block (), cu
, &offset
))
11509 field
->set_loc_bitpos (offset
* bits_per_byte
);
11512 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11513 struct objfile
*objfile
= per_objfile
->objfile
;
11514 struct dwarf2_locexpr_baton
*dlbaton
11515 = XOBNEW (&objfile
->objfile_obstack
,
11516 struct dwarf2_locexpr_baton
);
11517 dlbaton
->data
= attr
->as_block ()->data
;
11518 dlbaton
->size
= attr
->as_block ()->size
;
11519 /* When using this baton, we want to compute the address
11520 of the field, not the value. This is why
11521 is_reference is set to false here. */
11522 dlbaton
->is_reference
= false;
11523 dlbaton
->per_objfile
= per_objfile
;
11524 dlbaton
->per_cu
= cu
->per_cu
;
11526 field
->set_loc_dwarf_block (dlbaton
);
11530 dwarf2_complex_location_expr_complaint ();
11534 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
11535 if (attr
!= nullptr)
11536 field
->set_loc_bitpos (attr
->constant_value (0));
11540 /* Add an aggregate field to the field list. */
11543 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
11544 struct dwarf2_cu
*cu
)
11546 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11547 struct gdbarch
*gdbarch
= objfile
->arch ();
11548 struct nextfield
*new_field
;
11549 struct attribute
*attr
;
11551 const char *fieldname
= "";
11553 if (die
->tag
== DW_TAG_inheritance
)
11554 new_field
= &fip
->baseclasses
.emplace_back ();
11556 new_field
= &fip
->fields
.emplace_back ();
11558 new_field
->offset
= die
->sect_off
;
11560 switch (dwarf2_access_attribute (die
, cu
))
11562 case DW_ACCESS_public
:
11564 case DW_ACCESS_private
:
11565 new_field
->field
.set_accessibility (accessibility::PRIVATE
);
11567 case DW_ACCESS_protected
:
11568 new_field
->field
.set_accessibility (accessibility::PROTECTED
);
11571 gdb_assert_not_reached ("invalid accessibility");
11574 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
11575 if (attr
!= nullptr && attr
->as_virtuality ())
11576 new_field
->field
.set_virtual ();
11578 fp
= &new_field
->field
;
11580 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
11581 && !die_is_declaration (die
, cu
))
11583 if (die
->tag
== DW_TAG_namelist_item
)
11585 /* Typically, DW_TAG_namelist_item are references to namelist items.
11586 If so, follow that reference. */
11587 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
11588 struct die_info
*item_die
= nullptr;
11589 struct dwarf2_cu
*item_cu
= cu
;
11590 if (attr1
->form_is_ref ())
11591 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
11592 if (item_die
!= nullptr)
11595 /* Data member other than a C++ static data member. */
11597 /* Get type of field. */
11598 fp
->set_type (die_type (die
, cu
));
11600 fp
->set_loc_bitpos (0);
11602 /* Get bit size of field (zero if none). */
11603 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
11604 if (attr
!= nullptr)
11605 fp
->set_bitsize (attr
->constant_value (0));
11607 fp
->set_bitsize (0);
11609 /* Get bit offset of field. */
11610 handle_member_location (die
, cu
, fp
);
11611 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
11612 if (attr
!= nullptr && attr
->form_is_constant ())
11614 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
11616 /* For big endian bits, the DW_AT_bit_offset gives the
11617 additional bit offset from the MSB of the containing
11618 anonymous object to the MSB of the field. We don't
11619 have to do anything special since we don't need to
11620 know the size of the anonymous object. */
11621 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
11625 /* For little endian bits, compute the bit offset to the
11626 MSB of the anonymous object, subtract off the number of
11627 bits from the MSB of the field to the MSB of the
11628 object, and then subtract off the number of bits of
11629 the field itself. The result is the bit offset of
11630 the LSB of the field. */
11631 int anonymous_size
;
11632 int bit_offset
= attr
->constant_value (0);
11634 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
11635 if (attr
!= nullptr && attr
->form_is_constant ())
11637 /* The size of the anonymous object containing
11638 the bit field is explicit, so use the
11639 indicated size (in bytes). */
11640 anonymous_size
= attr
->constant_value (0);
11644 /* The size of the anonymous object containing
11645 the bit field must be inferred from the type
11646 attribute of the data member containing the
11648 anonymous_size
= fp
->type ()->length ();
11650 fp
->set_loc_bitpos (fp
->loc_bitpos ()
11651 + anonymous_size
* bits_per_byte
11652 - bit_offset
- fp
->bitsize ());
11656 /* Get name of field. */
11657 fieldname
= dwarf2_name (die
, cu
);
11658 if (fieldname
== NULL
)
11661 /* The name is already allocated along with this objfile, so we don't
11662 need to duplicate it for the type. */
11663 fp
->set_name (fieldname
);
11665 /* Change accessibility for artificial fields (e.g. virtual table
11666 pointer or virtual base class pointer) to private. */
11667 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
11669 fp
->set_is_artificial (true);
11670 fp
->set_accessibility (accessibility::PRIVATE
);
11673 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
11675 /* C++ static member. */
11677 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11678 is a declaration, but all versions of G++ as of this writing
11679 (so through at least 3.2.1) incorrectly generate
11680 DW_TAG_variable tags. */
11682 const char *physname
;
11684 /* Get name of field. */
11685 fieldname
= dwarf2_name (die
, cu
);
11686 if (fieldname
== NULL
)
11689 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
11691 /* Only create a symbol if this is an external value.
11692 new_symbol checks this and puts the value in the global symbol
11693 table, which we want. If it is not external, new_symbol
11694 will try to put the value in cu->list_in_scope which is wrong. */
11695 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
11697 /* A static const member, not much different than an enum as far as
11698 we're concerned, except that we can support more types. */
11699 new_symbol (die
, NULL
, cu
);
11702 /* Get physical name. */
11703 physname
= dwarf2_physname (fieldname
, die
, cu
);
11705 /* The name is already allocated along with this objfile, so we don't
11706 need to duplicate it for the type. */
11707 fp
->set_loc_physname (physname
? physname
: "");
11708 fp
->set_type (die_type (die
, cu
));
11709 fp
->set_name (fieldname
);
11711 else if (die
->tag
== DW_TAG_inheritance
)
11713 /* C++ base class field. */
11714 handle_member_location (die
, cu
, fp
);
11715 fp
->set_bitsize (0);
11716 fp
->set_type (die_type (die
, cu
));
11717 fp
->set_name (fp
->type ()->name ());
11720 gdb_assert_not_reached ("missing case in dwarf2_add_field");
11723 /* Can the type given by DIE define another type? */
11726 type_can_define_types (const struct die_info
*die
)
11730 case DW_TAG_typedef
:
11731 case DW_TAG_class_type
:
11732 case DW_TAG_structure_type
:
11733 case DW_TAG_union_type
:
11734 case DW_TAG_enumeration_type
:
11742 /* Add a type definition defined in the scope of the FIP's class. */
11745 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
11746 struct dwarf2_cu
*cu
)
11748 struct decl_field fp
;
11749 memset (&fp
, 0, sizeof (fp
));
11751 gdb_assert (type_can_define_types (die
));
11753 /* Get name of field. NULL is okay here, meaning an anonymous type. */
11754 fp
.name
= dwarf2_name (die
, cu
);
11755 fp
.type
= read_type_die (die
, cu
);
11757 /* Save accessibility. */
11758 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
11759 switch (accessibility
)
11761 case DW_ACCESS_public
:
11762 /* The assumed value if neither private nor protected. */
11764 case DW_ACCESS_private
:
11765 fp
.accessibility
= accessibility::PRIVATE
;
11767 case DW_ACCESS_protected
:
11768 fp
.accessibility
= accessibility::PROTECTED
;
11772 if (die
->tag
== DW_TAG_typedef
)
11773 fip
->typedef_field_list
.push_back (fp
);
11775 fip
->nested_types_list
.push_back (fp
);
11778 /* A convenience typedef that's used when finding the discriminant
11779 field for a variant part. */
11780 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
11783 /* Compute the discriminant range for a given variant. OBSTACK is
11784 where the results will be stored. VARIANT is the variant to
11785 process. IS_UNSIGNED indicates whether the discriminant is signed
11788 static const gdb::array_view
<discriminant_range
>
11789 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
11792 std::vector
<discriminant_range
> ranges
;
11794 if (variant
.default_branch
)
11797 if (variant
.discr_list_data
== nullptr)
11799 discriminant_range r
11800 = {variant
.discriminant_value
, variant
.discriminant_value
};
11801 ranges
.push_back (r
);
11805 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
11806 variant
.discr_list_data
->size
);
11807 while (!data
.empty ())
11809 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
11811 complaint (_("invalid discriminant marker: %d"), data
[0]);
11814 bool is_range
= data
[0] == DW_DSC_range
;
11815 data
= data
.slice (1);
11817 ULONGEST low
, high
;
11818 unsigned int bytes_read
;
11822 complaint (_("DW_AT_discr_list missing low value"));
11826 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
11828 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
11830 data
= data
.slice (bytes_read
);
11836 complaint (_("DW_AT_discr_list missing high value"));
11840 high
= read_unsigned_leb128 (nullptr, data
.data (),
11843 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
11845 data
= data
.slice (bytes_read
);
11850 ranges
.push_back ({ low
, high
});
11854 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
11856 std::copy (ranges
.begin (), ranges
.end (), result
);
11857 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
11860 static const gdb::array_view
<variant_part
> create_variant_parts
11861 (struct obstack
*obstack
,
11862 const offset_map_type
&offset_map
,
11863 struct field_info
*fi
,
11864 const std::vector
<variant_part_builder
> &variant_parts
);
11866 /* Fill in a "struct variant" for a given variant field. RESULT is
11867 the variant to fill in. OBSTACK is where any needed allocations
11868 will be done. OFFSET_MAP holds the mapping from section offsets to
11869 fields for the type. FI describes the fields of the type we're
11870 processing. FIELD is the variant field we're converting. */
11873 create_one_variant (variant
&result
, struct obstack
*obstack
,
11874 const offset_map_type
&offset_map
,
11875 struct field_info
*fi
, const variant_field
&field
)
11877 result
.discriminants
= convert_variant_range (obstack
, field
, false);
11878 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
11879 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
11880 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
11881 field
.variant_parts
);
11884 /* Fill in a "struct variant_part" for a given variant part. RESULT
11885 is the variant part to fill in. OBSTACK is where any needed
11886 allocations will be done. OFFSET_MAP holds the mapping from
11887 section offsets to fields for the type. FI describes the fields of
11888 the type we're processing. BUILDER is the variant part to be
11892 create_one_variant_part (variant_part
&result
,
11893 struct obstack
*obstack
,
11894 const offset_map_type
&offset_map
,
11895 struct field_info
*fi
,
11896 const variant_part_builder
&builder
)
11898 auto iter
= offset_map
.find (builder
.discriminant_offset
);
11899 if (iter
== offset_map
.end ())
11901 result
.discriminant_index
= -1;
11902 /* Doesn't matter. */
11903 result
.is_unsigned
= false;
11907 result
.discriminant_index
= iter
->second
;
11909 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
11912 size_t n
= builder
.variants
.size ();
11913 variant
*output
= new (obstack
) variant
[n
];
11914 for (size_t i
= 0; i
< n
; ++i
)
11915 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
11916 builder
.variants
[i
]);
11918 result
.variants
= gdb::array_view
<variant
> (output
, n
);
11921 /* Create a vector of variant parts that can be attached to a type.
11922 OBSTACK is where any needed allocations will be done. OFFSET_MAP
11923 holds the mapping from section offsets to fields for the type. FI
11924 describes the fields of the type we're processing. VARIANT_PARTS
11925 is the vector to convert. */
11927 static const gdb::array_view
<variant_part
>
11928 create_variant_parts (struct obstack
*obstack
,
11929 const offset_map_type
&offset_map
,
11930 struct field_info
*fi
,
11931 const std::vector
<variant_part_builder
> &variant_parts
)
11933 if (variant_parts
.empty ())
11936 size_t n
= variant_parts
.size ();
11937 variant_part
*result
= new (obstack
) variant_part
[n
];
11938 for (size_t i
= 0; i
< n
; ++i
)
11939 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
11942 return gdb::array_view
<variant_part
> (result
, n
);
11945 /* Compute the variant part vector for FIP, attaching it to TYPE when
11949 add_variant_property (struct field_info
*fip
, struct type
*type
,
11950 struct dwarf2_cu
*cu
)
11952 /* Map section offsets of fields to their field index. Note the
11953 field index here does not take the number of baseclasses into
11955 offset_map_type offset_map
;
11956 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
11957 offset_map
[fip
->fields
[i
].offset
] = i
;
11959 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11960 gdb::array_view
<const variant_part
> parts
11961 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
11962 fip
->variant_parts
);
11964 struct dynamic_prop prop
;
11965 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
11966 obstack_copy (&objfile
->objfile_obstack
, &parts
,
11969 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
11972 /* Create the vector of fields, and attach it to the type. */
11975 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
11976 struct dwarf2_cu
*cu
)
11978 int nfields
= fip
->nfields ();
11980 /* Record the field count, allocate space for the array of fields,
11981 and create blank accessibility bitfields if necessary. */
11982 type
->alloc_fields (nfields
);
11984 if (!fip
->baseclasses
.empty () && cu
->lang () != language_ada
)
11986 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
11987 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
11990 if (!fip
->variant_parts
.empty ())
11991 add_variant_property (fip
, type
, cu
);
11993 /* Copy the saved-up fields into the field vector. */
11994 for (int i
= 0; i
< nfields
; ++i
)
11996 struct nextfield
&field
11997 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
11998 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
12000 type
->field (i
) = field
.field
;
12004 /* Return true if this member function is a constructor, false
12008 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
12010 const char *fieldname
;
12011 const char *type_name
;
12014 if (die
->parent
== NULL
)
12017 if (die
->parent
->tag
!= DW_TAG_structure_type
12018 && die
->parent
->tag
!= DW_TAG_union_type
12019 && die
->parent
->tag
!= DW_TAG_class_type
)
12022 fieldname
= dwarf2_name (die
, cu
);
12023 type_name
= dwarf2_name (die
->parent
, cu
);
12024 if (fieldname
== NULL
|| type_name
== NULL
)
12027 len
= strlen (fieldname
);
12028 return (strncmp (fieldname
, type_name
, len
) == 0
12029 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
12032 /* Add a member function to the proper fieldlist. */
12035 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
12036 struct type
*type
, struct dwarf2_cu
*cu
)
12038 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12039 struct attribute
*attr
;
12041 struct fnfieldlist
*flp
= nullptr;
12042 struct fn_field
*fnp
;
12043 const char *fieldname
;
12044 struct type
*this_type
;
12046 if (cu
->lang () == language_ada
)
12047 error (_("unexpected member function in Ada type"));
12049 /* Get name of member function. */
12050 fieldname
= dwarf2_name (die
, cu
);
12051 if (fieldname
== NULL
)
12054 /* Look up member function name in fieldlist. */
12055 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
12057 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
12059 flp
= &fip
->fnfieldlists
[i
];
12064 /* Create a new fnfieldlist if necessary. */
12065 if (flp
== nullptr)
12067 flp
= &fip
->fnfieldlists
.emplace_back ();
12068 flp
->name
= fieldname
;
12069 i
= fip
->fnfieldlists
.size () - 1;
12072 /* Create a new member function field and add it to the vector of
12074 fnp
= &flp
->fnfields
.emplace_back ();
12076 /* Delay processing of the physname until later. */
12077 if (cu
->lang () == language_cplus
)
12078 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
12082 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
12083 fnp
->physname
= physname
? physname
: "";
12086 fnp
->type
= type_allocator (objfile
, cu
->lang ()).new_type ();
12087 this_type
= read_type_die (die
, cu
);
12088 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
12090 int nparams
= this_type
->num_fields ();
12092 /* TYPE is the domain of this method, and THIS_TYPE is the type
12093 of the method itself (TYPE_CODE_METHOD). */
12094 smash_to_method_type (fnp
->type
, type
,
12095 this_type
->target_type (),
12096 this_type
->fields (),
12097 this_type
->num_fields (),
12098 this_type
->has_varargs ());
12100 /* Handle static member functions.
12101 Dwarf2 has no clean way to discern C++ static and non-static
12102 member functions. G++ helps GDB by marking the first
12103 parameter for non-static member functions (which is the this
12104 pointer) as artificial. We obtain this information from
12105 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
12106 if (nparams
== 0 || this_type
->field (0).is_artificial () == 0)
12107 fnp
->voffset
= VOFFSET_STATIC
;
12110 complaint (_("member function type missing for '%s'"),
12111 dwarf2_full_name (fieldname
, die
, cu
));
12113 /* Get fcontext from DW_AT_containing_type if present. */
12114 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
12115 fnp
->fcontext
= die_containing_type (die
, cu
);
12117 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12118 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
12120 /* Get accessibility. */
12121 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
12122 switch (accessibility
)
12124 case DW_ACCESS_private
:
12125 fnp
->accessibility
= accessibility::PRIVATE
;
12127 case DW_ACCESS_protected
:
12128 fnp
->accessibility
= accessibility::PROTECTED
;
12132 /* Check for artificial methods. */
12133 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
12134 if (attr
&& attr
->as_boolean ())
12135 fnp
->is_artificial
= 1;
12137 /* Check for defaulted methods. */
12138 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
12139 if (attr
!= nullptr)
12140 fnp
->defaulted
= attr
->defaulted ();
12142 /* Check for deleted methods. */
12143 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
12144 if (attr
!= nullptr && attr
->as_boolean ())
12145 fnp
->is_deleted
= 1;
12147 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
12149 /* Get index in virtual function table if it is a virtual member
12150 function. For older versions of GCC, this is an offset in the
12151 appropriate virtual table, as specified by DW_AT_containing_type.
12152 For everyone else, it is an expression to be evaluated relative
12153 to the object address. */
12155 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
12156 if (attr
!= nullptr)
12158 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
12160 struct dwarf_block
*block
= attr
->as_block ();
12163 if (block
->data
[0] == DW_OP_constu
12164 && decode_locdesc (block
, cu
, &offset
))
12166 /* "Old"-style GCC. See
12167 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=44126
12168 for discussion. This was known and a patch available
12169 in 2010, but as of 2023, both GCC and clang still
12171 fnp
->voffset
= offset
+ 2;
12173 else if ((block
->data
[0] == DW_OP_deref
12174 || (block
->size
> 1
12175 && block
->data
[0] == DW_OP_deref_size
12176 && block
->data
[1] == cu
->header
.addr_size
))
12177 && decode_locdesc (block
, cu
, &offset
))
12179 fnp
->voffset
= offset
;
12180 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
12181 dwarf2_complex_location_expr_complaint ();
12183 fnp
->voffset
/= cu
->header
.addr_size
;
12187 dwarf2_complex_location_expr_complaint ();
12189 if (!fnp
->fcontext
)
12191 /* If there is no `this' field and no DW_AT_containing_type,
12192 we cannot actually find a base class context for the
12194 if (this_type
->num_fields () == 0
12195 || !this_type
->field (0).is_artificial ())
12197 complaint (_("cannot determine context for virtual member "
12198 "function \"%s\" (offset %s)"),
12199 fieldname
, sect_offset_str (die
->sect_off
));
12203 fnp
->fcontext
= this_type
->field (0).type ()->target_type ();
12207 else if (attr
->form_is_section_offset ())
12209 dwarf2_complex_location_expr_complaint ();
12213 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12219 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
12220 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
12222 /* GCC does this, as of 2008-08-25; PR debug/37237. */
12223 complaint (_("Member function \"%s\" (offset %s) is virtual "
12224 "but the vtable offset is not specified"),
12225 fieldname
, sect_offset_str (die
->sect_off
));
12226 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12227 TYPE_CPLUS_DYNAMIC (type
) = 1;
12232 /* Create the vector of member function fields, and attach it to the type. */
12235 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
12236 struct dwarf2_cu
*cu
)
12238 if (cu
->lang () == language_ada
)
12239 error (_("unexpected member functions in Ada type"));
12241 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12242 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
12244 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
12246 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
12248 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
12249 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
12251 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
12252 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
12253 /* No need to zero-initialize, initialization is done by the copy in
12255 fn_flp
->fn_fields
= (struct fn_field
*)
12256 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
12258 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
12259 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
12262 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
12265 /* Returns non-zero if NAME is the name of a vtable member in CU's
12266 language, zero otherwise. */
12268 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
12270 static const char vptr
[] = "_vptr";
12272 /* Look for the C++ form of the vtable. */
12273 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
12279 /* GCC outputs unnamed structures that are really pointers to member
12280 functions, with the ABI-specified layout. If TYPE describes
12281 such a structure, smash it into a member function type.
12283 GCC shouldn't do this; it should just output pointer to member DIEs.
12284 This is GCC PR debug/28767. */
12287 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
12289 struct type
*pfn_type
, *self_type
, *new_type
;
12291 /* Check for a structure with no name and two children. */
12292 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
12295 /* Check for __pfn and __delta members. */
12296 if (type
->field (0).name () == NULL
12297 || strcmp (type
->field (0).name (), "__pfn") != 0
12298 || type
->field (1).name () == NULL
12299 || strcmp (type
->field (1).name (), "__delta") != 0)
12302 /* Find the type of the method. */
12303 pfn_type
= type
->field (0).type ();
12304 if (pfn_type
== NULL
12305 || pfn_type
->code () != TYPE_CODE_PTR
12306 || pfn_type
->target_type ()->code () != TYPE_CODE_FUNC
)
12309 /* Look for the "this" argument. */
12310 pfn_type
= pfn_type
->target_type ();
12311 if (pfn_type
->num_fields () == 0
12312 /* || pfn_type->field (0).type () == NULL */
12313 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
12316 self_type
= pfn_type
->field (0).type ()->target_type ();
12317 new_type
= type_allocator (type
).new_type ();
12318 smash_to_method_type (new_type
, self_type
, pfn_type
->target_type (),
12319 pfn_type
->fields (), pfn_type
->num_fields (),
12320 pfn_type
->has_varargs ());
12321 smash_to_methodptr_type (type
, new_type
);
12324 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
12325 requires rewriting, then copy it and return the updated copy.
12326 Otherwise return nullptr. */
12328 static struct type
*
12329 rewrite_array_type (struct type
*type
)
12331 if (type
->code () != TYPE_CODE_ARRAY
)
12334 struct type
*index_type
= type
->index_type ();
12335 range_bounds
*current_bounds
= index_type
->bounds ();
12337 /* Handle multi-dimensional arrays. */
12338 struct type
*new_target
= rewrite_array_type (type
->target_type ());
12339 if (new_target
== nullptr)
12341 /* Maybe we don't need to rewrite this array. */
12342 if (current_bounds
->low
.is_constant ()
12343 && current_bounds
->high
.is_constant ())
12347 /* Either the target type was rewritten, or the bounds have to be
12348 updated. Either way we want to copy the type and update
12350 struct type
*copy
= copy_type (type
);
12351 copy
->copy_fields (type
);
12352 if (new_target
!= nullptr)
12353 copy
->set_target_type (new_target
);
12355 struct type
*index_copy
= copy_type (index_type
);
12356 range_bounds
*bounds
12357 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
12358 sizeof (range_bounds
));
12359 *bounds
= *current_bounds
;
12360 bounds
->low
.set_const_val (1);
12361 bounds
->high
.set_const_val (0);
12362 index_copy
->set_bounds (bounds
);
12363 copy
->set_index_type (index_copy
);
12368 /* While some versions of GCC will generate complicated DWARF for an
12369 array (see quirk_ada_thick_pointer), more recent versions were
12370 modified to emit an explicit thick pointer structure. However, in
12371 this case, the array still has DWARF expressions for its ranges,
12372 and these must be ignored. */
12375 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
12378 gdb_assert (cu
->lang () == language_ada
);
12380 /* Check for a structure with two children. */
12381 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
12384 /* Check for P_ARRAY and P_BOUNDS members. */
12385 if (type
->field (0).name () == NULL
12386 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
12387 || type
->field (1).name () == NULL
12388 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
12391 /* Make sure we're looking at a pointer to an array. */
12392 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
12395 /* The Ada code already knows how to handle these types, so all that
12396 we need to do is turn the bounds into static bounds. However, we
12397 don't want to rewrite existing array or index types in-place,
12398 because those may be referenced in other contexts where this
12399 rewriting is undesirable. */
12400 struct type
*new_ary_type
12401 = rewrite_array_type (type
->field (0).type ()->target_type ());
12402 if (new_ary_type
!= nullptr)
12403 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
12406 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
12407 appropriate error checking and issuing complaints if there is a
12411 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
12413 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
12415 if (attr
== nullptr)
12418 if (!attr
->form_is_constant ())
12420 complaint (_("DW_AT_alignment must have constant form"
12421 " - DIE at %s [in module %s]"),
12422 sect_offset_str (die
->sect_off
),
12423 objfile_name (cu
->per_objfile
->objfile
));
12427 LONGEST val
= attr
->constant_value (0);
12430 complaint (_("DW_AT_alignment value must not be negative"
12431 " - DIE at %s [in module %s]"),
12432 sect_offset_str (die
->sect_off
),
12433 objfile_name (cu
->per_objfile
->objfile
));
12436 ULONGEST align
= val
;
12440 complaint (_("DW_AT_alignment value must not be zero"
12441 " - DIE at %s [in module %s]"),
12442 sect_offset_str (die
->sect_off
),
12443 objfile_name (cu
->per_objfile
->objfile
));
12446 if ((align
& (align
- 1)) != 0)
12448 complaint (_("DW_AT_alignment value must be a power of 2"
12449 " - DIE at %s [in module %s]"),
12450 sect_offset_str (die
->sect_off
),
12451 objfile_name (cu
->per_objfile
->objfile
));
12458 /* If the DIE has a DW_AT_alignment attribute, use its value to set
12459 the alignment for TYPE. */
12462 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
12465 if (!set_type_align (type
, get_alignment (cu
, die
)))
12466 complaint (_("DW_AT_alignment value too large"
12467 " - DIE at %s [in module %s]"),
12468 sect_offset_str (die
->sect_off
),
12469 objfile_name (cu
->per_objfile
->objfile
));
12472 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12473 constant for a type, according to DWARF5 spec, Table 5.5. */
12476 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
12481 case DW_CC_pass_by_reference
:
12482 case DW_CC_pass_by_value
:
12486 complaint (_("unrecognized DW_AT_calling_convention value "
12487 "(%s) for a type"), pulongest (value
));
12492 /* Check if the given VALUE is a valid enum dwarf_calling_convention
12493 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
12494 also according to GNU-specific values (see include/dwarf2.h). */
12497 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
12502 case DW_CC_program
:
12506 case DW_CC_GNU_renesas_sh
:
12507 case DW_CC_GNU_borland_fastcall_i386
:
12508 case DW_CC_GDB_IBM_OpenCL
:
12512 complaint (_("unrecognized DW_AT_calling_convention value "
12513 "(%s) for a subroutine"), pulongest (value
));
12518 /* Called when we find the DIE that starts a structure or union scope
12519 (definition) to create a type for the structure or union. Fill in
12520 the type's name and general properties; the members will not be
12521 processed until process_structure_scope. A symbol table entry for
12522 the type will also not be done until process_structure_scope (assuming
12523 the type has a name).
12525 NOTE: we need to call these functions regardless of whether or not the
12526 DIE has a DW_AT_name attribute, since it might be an anonymous
12527 structure or union. This gets the type entered into our set of
12528 user defined types. */
12530 static struct type
*
12531 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12533 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12535 struct attribute
*attr
;
12538 /* If the definition of this type lives in .debug_types, read that type.
12539 Don't follow DW_AT_specification though, that will take us back up
12540 the chain and we want to go down. */
12541 attr
= die
->attr (DW_AT_signature
);
12542 if (attr
!= nullptr)
12544 type
= get_DW_AT_signature_type (die
, attr
, cu
);
12546 /* The type's CU may not be the same as CU.
12547 Ensure TYPE is recorded with CU in die_type_hash. */
12548 return set_die_type (die
, type
, cu
);
12551 type
= type_allocator (objfile
, cu
->lang ()).new_type ();
12552 INIT_CPLUS_SPECIFIC (type
);
12554 name
= dwarf2_name (die
, cu
);
12557 if (cu
->lang () == language_cplus
12558 || cu
->lang () == language_d
12559 || cu
->lang () == language_rust
)
12561 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
12563 /* dwarf2_full_name might have already finished building the DIE's
12564 type. If so, there is no need to continue. */
12565 if (get_die_type (die
, cu
) != NULL
)
12566 return get_die_type (die
, cu
);
12568 type
->set_name (full_name
);
12572 /* The name is already allocated along with this objfile, so
12573 we don't need to duplicate it for the type. */
12574 type
->set_name (name
);
12578 if (die
->tag
== DW_TAG_structure_type
)
12580 type
->set_code (TYPE_CODE_STRUCT
);
12582 else if (die
->tag
== DW_TAG_union_type
)
12584 type
->set_code (TYPE_CODE_UNION
);
12586 else if (die
->tag
== DW_TAG_namelist
)
12588 type
->set_code (TYPE_CODE_NAMELIST
);
12592 type
->set_code (TYPE_CODE_STRUCT
);
12595 if (cu
->lang () == language_cplus
&& die
->tag
== DW_TAG_class_type
)
12596 type
->set_is_declared_class (true);
12598 /* Store the calling convention in the type if it's available in
12599 the die. Otherwise the calling convention remains set to
12600 the default value DW_CC_normal. */
12601 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
12602 if (attr
!= nullptr
12603 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
12605 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12606 TYPE_CPLUS_CALLING_CONVENTION (type
)
12607 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
12610 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
12611 if (attr
!= nullptr)
12613 if (attr
->form_is_constant ())
12614 type
->set_length (attr
->constant_value (0));
12617 struct dynamic_prop prop
;
12618 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
12619 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
12621 type
->set_length (0);
12625 type
->set_length (0);
12627 maybe_set_alignment (cu
, die
, type
);
12629 if (producer_is_icc_lt_14 (cu
) && (type
->length () == 0))
12631 /* ICC<14 does not output the required DW_AT_declaration on
12632 incomplete types, but gives them a size of zero. */
12633 type
->set_is_stub (true);
12636 type
->set_stub_is_supported (true);
12638 if (die_is_declaration (die
, cu
))
12639 type
->set_is_stub (true);
12640 else if (attr
== NULL
&& die
->child
== NULL
12641 && producer_is_realview (cu
->producer
))
12642 /* RealView does not output the required DW_AT_declaration
12643 on incomplete types. */
12644 type
->set_is_stub (true);
12646 /* We need to add the type field to the die immediately so we don't
12647 infinitely recurse when dealing with pointers to the structure
12648 type within the structure itself. */
12649 set_die_type (die
, type
, cu
);
12651 /* set_die_type should be already done. */
12652 set_descriptive_type (type
, die
, cu
);
12657 static void handle_struct_member_die
12658 (struct die_info
*child_die
,
12660 struct field_info
*fi
,
12661 std::vector
<struct symbol
*> *template_args
,
12662 struct dwarf2_cu
*cu
);
12664 /* A helper for handle_struct_member_die that handles
12665 DW_TAG_variant_part. */
12668 handle_variant_part (struct die_info
*die
, struct type
*type
,
12669 struct field_info
*fi
,
12670 std::vector
<struct symbol
*> *template_args
,
12671 struct dwarf2_cu
*cu
)
12673 variant_part_builder
*new_part
;
12674 if (fi
->current_variant_part
== nullptr)
12675 new_part
= &fi
->variant_parts
.emplace_back ();
12676 else if (!fi
->current_variant_part
->processing_variant
)
12678 complaint (_("nested DW_TAG_variant_part seen "
12679 "- DIE at %s [in module %s]"),
12680 sect_offset_str (die
->sect_off
),
12681 objfile_name (cu
->per_objfile
->objfile
));
12686 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
12687 new_part
= ¤t
.variant_parts
.emplace_back ();
12690 /* When we recurse, we want callees to add to this new variant
12692 scoped_restore save_current_variant_part
12693 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
12695 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
12698 /* It's a univariant form, an extension we support. */
12700 else if (discr
->form_is_ref ())
12702 struct dwarf2_cu
*target_cu
= cu
;
12703 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
12705 new_part
->discriminant_offset
= target_die
->sect_off
;
12709 complaint (_("DW_AT_discr does not have DIE reference form"
12710 " - DIE at %s [in module %s]"),
12711 sect_offset_str (die
->sect_off
),
12712 objfile_name (cu
->per_objfile
->objfile
));
12715 for (die_info
*child_die
= die
->child
;
12717 child_die
= child_die
->sibling
)
12718 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
12721 /* A helper for handle_struct_member_die that handles
12725 handle_variant (struct die_info
*die
, struct type
*type
,
12726 struct field_info
*fi
,
12727 std::vector
<struct symbol
*> *template_args
,
12728 struct dwarf2_cu
*cu
)
12730 if (fi
->current_variant_part
== nullptr)
12732 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
12733 "- DIE at %s [in module %s]"),
12734 sect_offset_str (die
->sect_off
),
12735 objfile_name (cu
->per_objfile
->objfile
));
12738 if (fi
->current_variant_part
->processing_variant
)
12740 complaint (_("nested DW_TAG_variant seen "
12741 "- DIE at %s [in module %s]"),
12742 sect_offset_str (die
->sect_off
),
12743 objfile_name (cu
->per_objfile
->objfile
));
12747 scoped_restore save_processing_variant
12748 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
12751 variant_field
&variant
= fi
->current_variant_part
->variants
.emplace_back ();
12752 variant
.first_field
= fi
->fields
.size ();
12754 /* In a variant we want to get the discriminant and also add a
12755 field for our sole member child. */
12756 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
12757 if (discr
== nullptr || !discr
->form_is_constant ())
12759 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
12760 if (discr
== nullptr || discr
->as_block ()->size
== 0)
12761 variant
.default_branch
= true;
12763 variant
.discr_list_data
= discr
->as_block ();
12766 variant
.discriminant_value
= discr
->constant_value (0);
12768 for (die_info
*variant_child
= die
->child
;
12769 variant_child
!= NULL
;
12770 variant_child
= variant_child
->sibling
)
12771 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
12773 variant
.last_field
= fi
->fields
.size ();
12776 /* A helper for process_structure_scope that handles a single member
12780 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
12781 struct field_info
*fi
,
12782 std::vector
<struct symbol
*> *template_args
,
12783 struct dwarf2_cu
*cu
)
12785 if (child_die
->tag
== DW_TAG_member
12786 || child_die
->tag
== DW_TAG_variable
12787 || child_die
->tag
== DW_TAG_namelist_item
)
12789 /* NOTE: carlton/2002-11-05: A C++ static data member
12790 should be a DW_TAG_member that is a declaration, but
12791 all versions of G++ as of this writing (so through at
12792 least 3.2.1) incorrectly generate DW_TAG_variable
12793 tags for them instead. */
12794 dwarf2_add_field (fi
, child_die
, cu
);
12796 else if (child_die
->tag
== DW_TAG_subprogram
)
12798 /* Rust doesn't have member functions in the C++ sense.
12799 However, it does emit ordinary functions as children
12800 of a struct DIE. */
12801 if (cu
->lang () == language_rust
)
12802 read_func_scope (child_die
, cu
);
12805 /* C++ member function. */
12806 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
12809 else if (child_die
->tag
== DW_TAG_inheritance
)
12811 /* C++ base class field. */
12812 dwarf2_add_field (fi
, child_die
, cu
);
12814 else if (type_can_define_types (child_die
))
12815 dwarf2_add_type_defn (fi
, child_die
, cu
);
12816 else if (child_die
->tag
== DW_TAG_template_type_param
12817 || child_die
->tag
== DW_TAG_template_value_param
)
12819 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12822 template_args
->push_back (arg
);
12824 else if (child_die
->tag
== DW_TAG_variant_part
)
12825 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
12826 else if (child_die
->tag
== DW_TAG_variant
)
12827 handle_variant (child_die
, type
, fi
, template_args
, cu
);
12830 /* Finish creating a structure or union type, including filling in its
12831 members and creating a symbol for it. This function also handles Fortran
12832 namelist variables, their items or members and creating a symbol for
12836 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12838 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12839 struct die_info
*child_die
;
12842 type
= get_die_type (die
, cu
);
12844 type
= read_structure_type (die
, cu
);
12846 bool has_template_parameters
= false;
12847 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
12849 struct field_info fi
;
12850 std::vector
<struct symbol
*> template_args
;
12852 child_die
= die
->child
;
12854 while (child_die
&& child_die
->tag
)
12856 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
12857 child_die
= child_die
->sibling
;
12860 /* Attach template arguments to type. */
12861 if (!template_args
.empty ())
12863 has_template_parameters
= true;
12864 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12865 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
12866 TYPE_TEMPLATE_ARGUMENTS (type
)
12867 = XOBNEWVEC (&objfile
->objfile_obstack
,
12869 TYPE_N_TEMPLATE_ARGUMENTS (type
));
12870 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
12871 template_args
.data (),
12872 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
12873 * sizeof (struct symbol
*)));
12876 /* Attach fields and member functions to the type. */
12877 if (fi
.nfields () > 0)
12878 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
12879 if (!fi
.fnfieldlists
.empty ())
12881 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
12883 /* Get the type which refers to the base class (possibly this
12884 class itself) which contains the vtable pointer for the current
12885 class from the DW_AT_containing_type attribute. This use of
12886 DW_AT_containing_type is a GNU extension. */
12888 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
12890 struct type
*t
= die_containing_type (die
, cu
);
12892 set_type_vptr_basetype (type
, t
);
12897 /* Our own class provides vtbl ptr. */
12898 for (i
= t
->num_fields () - 1;
12899 i
>= TYPE_N_BASECLASSES (t
);
12902 const char *fieldname
= t
->field (i
).name ();
12904 if (is_vtable_name (fieldname
, cu
))
12906 set_type_vptr_fieldno (type
, i
);
12911 /* Complain if virtual function table field not found. */
12912 if (i
< TYPE_N_BASECLASSES (t
))
12913 complaint (_("virtual function table pointer "
12914 "not found when defining class '%s'"),
12915 type
->name () ? type
->name () : "");
12919 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
12922 else if (cu
->producer
12923 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
12925 /* The IBM XLC compiler does not provide direct indication
12926 of the containing type, but the vtable pointer is
12927 always named __vfp. */
12931 for (i
= type
->num_fields () - 1;
12932 i
>= TYPE_N_BASECLASSES (type
);
12935 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
12937 set_type_vptr_fieldno (type
, i
);
12938 set_type_vptr_basetype (type
, type
);
12945 /* Copy fi.typedef_field_list linked list elements content into the
12946 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12947 if (!fi
.typedef_field_list
.empty ())
12949 int count
= fi
.typedef_field_list
.size ();
12951 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12952 /* No zero-initialization is needed, the elements are initialized by
12953 the copy in the loop below. */
12954 TYPE_TYPEDEF_FIELD_ARRAY (type
)
12955 = ((struct decl_field
*)
12957 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
12958 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
12960 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
12961 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
12964 /* Copy fi.nested_types_list linked list elements content into the
12965 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
12966 if (!fi
.nested_types_list
.empty ()
12967 && cu
->lang () != language_ada
)
12969 int count
= fi
.nested_types_list
.size ();
12971 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
12972 /* No zero-initialization is needed, the elements are initialized by
12973 the copy in the loop below. */
12974 TYPE_NESTED_TYPES_ARRAY (type
)
12975 = ((struct decl_field
*)
12976 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
12977 TYPE_NESTED_TYPES_COUNT (type
) = count
;
12979 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
12980 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
12984 quirk_gcc_member_function_pointer (type
, objfile
);
12985 if (cu
->lang () == language_rust
&& die
->tag
== DW_TAG_union_type
)
12986 cu
->rust_unions
.push_back (type
);
12987 else if (cu
->lang () == language_ada
)
12988 quirk_ada_thick_pointer_struct (die
, cu
, type
);
12990 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12991 snapshots) has been known to create a die giving a declaration
12992 for a class that has, as a child, a die giving a definition for a
12993 nested class. So we have to process our children even if the
12994 current die is a declaration. Normally, of course, a declaration
12995 won't have any children at all. */
12997 child_die
= die
->child
;
12999 while (child_die
!= NULL
&& child_die
->tag
)
13001 if (child_die
->tag
== DW_TAG_member
13002 || child_die
->tag
== DW_TAG_variable
13003 || child_die
->tag
== DW_TAG_inheritance
13004 || child_die
->tag
== DW_TAG_template_value_param
13005 || child_die
->tag
== DW_TAG_template_type_param
)
13010 process_die (child_die
, cu
);
13012 child_die
= child_die
->sibling
;
13015 /* Do not consider external references. According to the DWARF standard,
13016 these DIEs are identified by the fact that they have no byte_size
13017 attribute, and a declaration attribute. */
13018 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
13019 || !die_is_declaration (die
, cu
)
13020 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
13022 struct symbol
*sym
= new_symbol (die
, type
, cu
);
13024 if (has_template_parameters
)
13026 struct symtab
*symtab
;
13027 if (sym
!= nullptr)
13028 symtab
= sym
->symtab ();
13029 else if (cu
->line_header
!= nullptr)
13031 /* Any related symtab will do. */
13033 = cu
->line_header
->file_names ()[0].symtab
;
13038 complaint (_("could not find suitable "
13039 "symtab for template parameter"
13040 " - DIE at %s [in module %s]"),
13041 sect_offset_str (die
->sect_off
),
13042 objfile_name (objfile
));
13045 if (symtab
!= nullptr)
13047 /* Make sure that the symtab is set on the new symbols.
13048 Even though they don't appear in this symtab directly,
13049 other parts of gdb assume that symbols do, and this is
13050 reasonably true. */
13051 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
13052 TYPE_TEMPLATE_ARGUMENT (type
, i
)->set_symtab (symtab
);
13058 /* Read DW_AT_endianity from DIE and compute the byte order that
13059 should be used. The CU's arch is used as the default. The result
13060 is true if the returned arch differs from the default, and false if
13061 they are the same. If provided, the out parameter BYTE_ORDER is
13065 die_byte_order (die_info
*die
, dwarf2_cu
*cu
, enum bfd_endian
*byte_order
)
13067 gdbarch
*arch
= cu
->per_objfile
->objfile
->arch ();
13068 enum bfd_endian arch_order
= gdbarch_byte_order (arch
);
13069 enum bfd_endian new_order
= arch_order
;
13071 attribute
*attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
13072 if (attr
!= nullptr && attr
->form_is_constant ())
13074 int endianity
= attr
->constant_value (0);
13079 new_order
= BFD_ENDIAN_BIG
;
13081 case DW_END_little
:
13082 new_order
= BFD_ENDIAN_LITTLE
;
13085 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
13090 if (byte_order
!= nullptr)
13091 *byte_order
= new_order
;
13093 return new_order
!= arch_order
;
13096 /* Assuming DIE is an enumeration type, and TYPE is its associated
13097 type, update TYPE using some information only available in DIE's
13098 children. In particular, the fields are computed. */
13101 update_enumeration_type_from_children (struct die_info
*die
,
13103 struct dwarf2_cu
*cu
)
13105 struct die_info
*child_die
;
13106 int unsigned_enum
= 1;
13109 auto_obstack obstack
;
13110 std::vector
<struct field
> fields
;
13112 for (child_die
= die
->child
;
13113 child_die
!= NULL
&& child_die
->tag
;
13114 child_die
= child_die
->sibling
)
13116 struct attribute
*attr
;
13118 const gdb_byte
*bytes
;
13119 struct dwarf2_locexpr_baton
*baton
;
13122 if (child_die
->tag
!= DW_TAG_enumerator
)
13125 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
13129 name
= dwarf2_name (child_die
, cu
);
13131 name
= "<anonymous enumerator>";
13133 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
13134 &value
, &bytes
, &baton
);
13142 if (count_one_bits_ll (value
) >= 2)
13146 struct field
&field
= fields
.emplace_back ();
13147 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
13148 field
.set_loc_enumval (value
);
13151 if (!fields
.empty ())
13152 type
->copy_fields (fields
);
13157 type
->set_is_unsigned (true);
13160 type
->set_is_flag_enum (true);
13163 /* Given a DW_AT_enumeration_type die, set its type. We do not
13164 complete the type's fields yet, or create any symbols. */
13166 static struct type
*
13167 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13169 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13171 struct attribute
*attr
;
13174 /* If the definition of this type lives in .debug_types, read that type.
13175 Don't follow DW_AT_specification though, that will take us back up
13176 the chain and we want to go down. */
13177 attr
= die
->attr (DW_AT_signature
);
13178 if (attr
!= nullptr)
13180 type
= get_DW_AT_signature_type (die
, attr
, cu
);
13182 /* The type's CU may not be the same as CU.
13183 Ensure TYPE is recorded with CU in die_type_hash. */
13184 return set_die_type (die
, type
, cu
);
13187 type
= type_allocator (objfile
, cu
->lang ()).new_type ();
13189 type
->set_code (TYPE_CODE_ENUM
);
13190 name
= dwarf2_full_name (NULL
, die
, cu
);
13192 type
->set_name (name
);
13194 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
13197 struct type
*underlying_type
= die_type (die
, cu
);
13199 type
->set_target_type (underlying_type
);
13202 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13203 if (attr
!= nullptr)
13204 type
->set_length (attr
->constant_value (0));
13206 type
->set_length (0);
13208 maybe_set_alignment (cu
, die
, type
);
13210 /* The enumeration DIE can be incomplete. In Ada, any type can be
13211 declared as private in the package spec, and then defined only
13212 inside the package body. Such types are known as Taft Amendment
13213 Types. When another package uses such a type, an incomplete DIE
13214 may be generated by the compiler. */
13215 if (die_is_declaration (die
, cu
))
13216 type
->set_is_stub (true);
13218 /* If this type has an underlying type that is not a stub, then we
13219 may use its attributes. We always use the "unsigned" attribute
13220 in this situation, because ordinarily we guess whether the type
13221 is unsigned -- but the guess can be wrong and the underlying type
13222 can tell us the reality. However, we defer to a local size
13223 attribute if one exists, because this lets the compiler override
13224 the underlying type if needed. */
13225 if (type
->target_type () != NULL
&& !type
->target_type ()->is_stub ())
13227 struct type
*underlying_type
= type
->target_type ();
13228 underlying_type
= check_typedef (underlying_type
);
13230 type
->set_is_unsigned (underlying_type
->is_unsigned ());
13232 if (type
->length () == 0)
13233 type
->set_length (underlying_type
->length ());
13235 if (TYPE_RAW_ALIGN (type
) == 0
13236 && TYPE_RAW_ALIGN (underlying_type
) != 0)
13237 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
13240 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
13242 type
->set_endianity_is_not_default (die_byte_order (die
, cu
, nullptr));
13244 set_die_type (die
, type
, cu
);
13246 /* Finish the creation of this type by using the enum's children.
13247 Note that, as usual, this must come after set_die_type to avoid
13248 infinite recursion when trying to compute the names of the
13250 update_enumeration_type_from_children (die
, type
, cu
);
13255 /* Given a pointer to a die which begins an enumeration, process all
13256 the dies that define the members of the enumeration, and create the
13257 symbol for the enumeration type.
13259 NOTE: We reverse the order of the element list. */
13262 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
13264 struct type
*this_type
;
13266 this_type
= get_die_type (die
, cu
);
13267 if (this_type
== NULL
)
13268 this_type
= read_enumeration_type (die
, cu
);
13270 if (die
->child
!= NULL
)
13272 struct die_info
*child_die
;
13275 child_die
= die
->child
;
13276 while (child_die
&& child_die
->tag
)
13278 if (child_die
->tag
!= DW_TAG_enumerator
)
13280 process_die (child_die
, cu
);
13284 name
= dwarf2_name (child_die
, cu
);
13286 new_symbol (child_die
, this_type
, cu
);
13289 child_die
= child_die
->sibling
;
13293 /* If we are reading an enum from a .debug_types unit, and the enum
13294 is a declaration, and the enum is not the signatured type in the
13295 unit, then we do not want to add a symbol for it. Adding a
13296 symbol would in some cases obscure the true definition of the
13297 enum, giving users an incomplete type when the definition is
13298 actually available. Note that we do not want to do this for all
13299 enums which are just declarations, because C++0x allows forward
13300 enum declarations. */
13301 if (cu
->per_cu
->is_debug_types
13302 && die_is_declaration (die
, cu
))
13304 struct signatured_type
*sig_type
;
13306 sig_type
= (struct signatured_type
*) cu
->per_cu
;
13307 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
13308 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
13312 new_symbol (die
, this_type
, cu
);
13315 /* Helper function for quirk_ada_thick_pointer that examines a bounds
13316 expression for an index type and finds the corresponding field
13317 offset in the hidden "P_BOUNDS" structure. Returns true on success
13318 and updates *FIELD, false if it fails to recognize an
13322 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
13323 int *bounds_offset
, struct field
*field
,
13324 struct dwarf2_cu
*cu
)
13326 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
13327 if (attr
== nullptr || !attr
->form_is_block ())
13330 const struct dwarf_block
*block
= attr
->as_block ();
13331 const gdb_byte
*start
= block
->data
;
13332 const gdb_byte
*end
= block
->data
+ block
->size
;
13334 /* The expression to recognize generally looks like:
13336 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13337 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13339 However, the second "plus_uconst" may be missing:
13341 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13342 DW_OP_deref_size: 4)
13344 This happens when the field is at the start of the structure.
13346 Also, the final deref may not be sized:
13348 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
13351 This happens when the size of the index type happens to be the
13352 same as the architecture's word size. This can occur with or
13353 without the second plus_uconst. */
13355 if (end
- start
< 2)
13357 if (*start
++ != DW_OP_push_object_address
)
13359 if (*start
++ != DW_OP_plus_uconst
)
13362 uint64_t this_bound_off
;
13363 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
13364 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
13366 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
13367 is consistent among all bounds. */
13368 if (*bounds_offset
== -1)
13369 *bounds_offset
= this_bound_off
;
13370 else if (*bounds_offset
!= this_bound_off
)
13373 if (start
== end
|| *start
++ != DW_OP_deref
)
13379 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
13381 /* This means an offset of 0. */
13383 else if (*start
++ != DW_OP_plus_uconst
)
13387 /* The size is the parameter to DW_OP_plus_uconst. */
13389 start
= gdb_read_uleb128 (start
, end
, &val
);
13390 if (start
== nullptr)
13392 if ((int) val
!= val
)
13401 if (*start
== DW_OP_deref_size
)
13403 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
13404 if (start
== nullptr)
13407 else if (*start
== DW_OP_deref
)
13409 size
= cu
->header
.addr_size
;
13415 field
->set_loc_bitpos (8 * offset
);
13416 if (size
!= field
->type ()->length ())
13417 field
->set_bitsize (8 * size
);
13422 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
13423 some kinds of Ada arrays:
13425 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
13426 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
13427 <11e0> DW_AT_data_location: 2 byte block: 97 6
13428 (DW_OP_push_object_address; DW_OP_deref)
13429 <11e3> DW_AT_type : <0x1173>
13430 <11e7> DW_AT_sibling : <0x1201>
13431 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
13432 <11ec> DW_AT_type : <0x1206>
13433 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
13434 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13435 DW_OP_deref_size: 4)
13436 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
13437 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
13438 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
13440 This actually represents a "thick pointer", which is a structure
13441 with two elements: one that is a pointer to the array data, and one
13442 that is a pointer to another structure; this second structure holds
13445 This returns a new type on success, or nullptr if this didn't
13446 recognize the type. */
13448 static struct type
*
13449 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
13452 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
13453 /* So far we've only seen this with block form. */
13454 if (attr
== nullptr || !attr
->form_is_block ())
13457 /* Note that this will fail if the structure layout is changed by
13458 the compiler. However, we have no good way to recognize some
13459 other layout, because we don't know what expression the compiler
13460 might choose to emit should this happen. */
13461 struct dwarf_block
*blk
= attr
->as_block ();
13463 || blk
->data
[0] != DW_OP_push_object_address
13464 || blk
->data
[1] != DW_OP_deref
)
13467 int bounds_offset
= -1;
13468 int max_align
= -1;
13469 std::vector
<struct field
> range_fields
;
13470 for (struct die_info
*child_die
= die
->child
;
13472 child_die
= child_die
->sibling
)
13474 if (child_die
->tag
== DW_TAG_subrange_type
)
13476 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
13478 int this_align
= type_align (underlying
);
13479 if (this_align
> max_align
)
13480 max_align
= this_align
;
13482 range_fields
.emplace_back ();
13483 range_fields
.emplace_back ();
13485 struct field
&lower
= range_fields
[range_fields
.size () - 2];
13486 struct field
&upper
= range_fields
[range_fields
.size () - 1];
13488 lower
.set_type (underlying
);
13489 lower
.set_is_artificial (true);
13491 upper
.set_type (underlying
);
13492 upper
.set_is_artificial (true);
13494 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
13495 &bounds_offset
, &lower
, cu
)
13496 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
13497 &bounds_offset
, &upper
, cu
))
13502 /* This shouldn't really happen, but double-check that we found
13503 where the bounds are stored. */
13504 if (bounds_offset
== -1)
13507 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13508 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
13512 /* Set the name of each field in the bounds. */
13513 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
13514 range_fields
[i
].set_name (objfile
->intern (name
));
13515 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
13516 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
13519 type_allocator
alloc (objfile
, cu
->lang ());
13520 struct type
*bounds
= alloc
.new_type ();
13521 bounds
->set_code (TYPE_CODE_STRUCT
);
13523 bounds
->copy_fields (range_fields
);
13525 int last_fieldno
= range_fields
.size () - 1;
13526 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
13527 + bounds
->field (last_fieldno
).type ()->length ());
13528 bounds
->set_length (align_up (bounds_size
, max_align
));
13530 /* Rewrite the existing array type in place. Specifically, we
13531 remove any dynamic properties we might have read, and we replace
13532 the index types. */
13533 struct type
*iter
= type
;
13534 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
13536 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
13537 iter
->main_type
->dyn_prop_list
= nullptr;
13538 iter
->set_index_type
13539 (create_static_range_type (alloc
, bounds
->field (i
).type (), 1, 0));
13540 iter
= iter
->target_type ();
13543 struct type
*result
= type_allocator (objfile
, cu
->lang ()).new_type ();
13544 result
->set_code (TYPE_CODE_STRUCT
);
13546 result
->alloc_fields (2);
13548 /* The names are chosen to coincide with what the compiler does with
13549 -fgnat-encodings=all, which the Ada code in gdb already
13551 result
->field (0).set_name ("P_ARRAY");
13552 result
->field (0).set_type (lookup_pointer_type (type
));
13554 result
->field (1).set_name ("P_BOUNDS");
13555 result
->field (1).set_type (lookup_pointer_type (bounds
));
13556 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
13558 result
->set_name (type
->name ());
13559 result
->set_length (result
->field (0).type ()->length ()
13560 + result
->field (1).type ()->length ());
13565 /* Extract all information from a DW_TAG_array_type DIE and put it in
13566 the DIE's type field. For now, this only handles one dimensional
13569 static struct type
*
13570 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13572 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13573 struct die_info
*child_die
;
13575 struct type
*element_type
, *range_type
, *index_type
;
13576 struct attribute
*attr
;
13578 struct dynamic_prop
*byte_stride_prop
= NULL
;
13579 unsigned int bit_stride
= 0;
13581 element_type
= die_type (die
, cu
);
13583 /* The die_type call above may have already set the type for this DIE. */
13584 type
= get_die_type (die
, cu
);
13588 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
13592 struct type
*prop_type
= cu
->addr_sized_int_type (false);
13595 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
13596 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
13600 complaint (_("unable to read array DW_AT_byte_stride "
13601 " - DIE at %s [in module %s]"),
13602 sect_offset_str (die
->sect_off
),
13603 objfile_name (cu
->per_objfile
->objfile
));
13604 /* Ignore this attribute. We will likely not be able to print
13605 arrays of this type correctly, but there is little we can do
13606 to help if we cannot read the attribute's value. */
13607 byte_stride_prop
= NULL
;
13611 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
13613 bit_stride
= attr
->constant_value (0);
13615 /* Irix 6.2 native cc creates array types without children for
13616 arrays with unspecified length. */
13617 if (die
->child
== NULL
)
13619 index_type
= builtin_type (objfile
)->builtin_int
;
13620 type_allocator
alloc (objfile
, cu
->lang ());
13621 range_type
= create_static_range_type (alloc
, index_type
, 0, -1);
13622 type
= create_array_type_with_stride (alloc
, element_type
, range_type
,
13623 byte_stride_prop
, bit_stride
);
13624 return set_die_type (die
, type
, cu
);
13627 std::vector
<struct type
*> range_types
;
13628 child_die
= die
->child
;
13629 while (child_die
&& child_die
->tag
)
13631 if (child_die
->tag
== DW_TAG_subrange_type
13632 || child_die
->tag
== DW_TAG_generic_subrange
)
13634 struct type
*child_type
= read_type_die (child_die
, cu
);
13636 if (child_type
!= NULL
)
13638 /* The range type was successfully read. Save it for the
13639 array type creation. */
13640 range_types
.push_back (child_type
);
13643 child_die
= child_die
->sibling
;
13646 if (range_types
.empty ())
13648 complaint (_("unable to find array range - DIE at %s [in module %s]"),
13649 sect_offset_str (die
->sect_off
),
13650 objfile_name (cu
->per_objfile
->objfile
));
13654 /* Dwarf2 dimensions are output from left to right, create the
13655 necessary array types in backwards order. */
13657 type
= element_type
;
13659 type_allocator
alloc (cu
->per_objfile
->objfile
, cu
->lang ());
13660 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
13664 while (i
< range_types
.size ())
13666 type
= create_array_type_with_stride (alloc
, type
, range_types
[i
++],
13667 byte_stride_prop
, bit_stride
);
13668 type
->set_is_multi_dimensional (true);
13670 byte_stride_prop
= nullptr;
13675 size_t ndim
= range_types
.size ();
13678 type
= create_array_type_with_stride (alloc
, type
, range_types
[ndim
],
13679 byte_stride_prop
, bit_stride
);
13680 type
->set_is_multi_dimensional (true);
13682 byte_stride_prop
= nullptr;
13686 /* Clear the flag on the outermost array type. */
13687 type
->set_is_multi_dimensional (false);
13688 gdb_assert (type
!= element_type
);
13690 /* Understand Dwarf2 support for vector types (like they occur on
13691 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
13692 array type. This is not part of the Dwarf2/3 standard yet, but a
13693 custom vendor extension. The main difference between a regular
13694 array and the vector variant is that vectors are passed by value
13696 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
13697 if (attr
!= nullptr)
13698 make_vector_type (type
);
13700 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
13701 implementation may choose to implement triple vectors using this
13703 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13704 if (attr
!= nullptr && attr
->form_is_unsigned ())
13706 if (attr
->as_unsigned () >= type
->length ())
13707 type
->set_length (attr
->as_unsigned ());
13709 complaint (_("DW_AT_byte_size for array type smaller "
13710 "than the total size of elements"));
13713 name
= dwarf2_name (die
, cu
);
13715 type
->set_name (name
);
13717 maybe_set_alignment (cu
, die
, type
);
13719 struct type
*replacement_type
= nullptr;
13720 if (cu
->lang () == language_ada
)
13722 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
13723 if (replacement_type
!= nullptr)
13724 type
= replacement_type
;
13727 /* Install the type in the die. */
13728 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
13730 /* set_die_type should be already done. */
13731 set_descriptive_type (type
, die
, cu
);
13736 static enum dwarf_array_dim_ordering
13737 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
13739 struct attribute
*attr
;
13741 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
13743 if (attr
!= nullptr)
13745 LONGEST val
= attr
->constant_value (-1);
13746 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
13747 return (enum dwarf_array_dim_ordering
) val
;
13750 /* GNU F77 is a special case, as at 08/2004 array type info is the
13751 opposite order to the dwarf2 specification, but data is still
13752 laid out as per normal fortran.
13754 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13755 version checking. */
13757 if (cu
->lang () == language_fortran
13758 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
13760 return DW_ORD_row_major
;
13763 switch (cu
->language_defn
->array_ordering ())
13765 case array_column_major
:
13766 return DW_ORD_col_major
;
13767 case array_row_major
:
13769 return DW_ORD_row_major
;
13773 /* Extract all information from a DW_TAG_set_type DIE and put it in
13774 the DIE's type field. */
13776 static struct type
*
13777 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13779 struct type
*domain_type
, *set_type
;
13780 struct attribute
*attr
;
13782 domain_type
= die_type (die
, cu
);
13784 /* The die_type call above may have already set the type for this DIE. */
13785 set_type
= get_die_type (die
, cu
);
13789 type_allocator
alloc (cu
->per_objfile
->objfile
, cu
->lang ());
13790 set_type
= create_set_type (alloc
, domain_type
);
13792 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13793 if (attr
!= nullptr && attr
->form_is_unsigned ())
13794 set_type
->set_length (attr
->as_unsigned ());
13796 maybe_set_alignment (cu
, die
, set_type
);
13798 return set_die_type (die
, set_type
, cu
);
13801 /* A helper for read_common_block that creates a locexpr baton.
13802 SYM is the symbol which we are marking as computed.
13803 COMMON_DIE is the DIE for the common block.
13804 COMMON_LOC is the location expression attribute for the common
13806 MEMBER_LOC is the location expression attribute for the particular
13807 member of the common block that we are processing.
13808 CU is the CU from which the above come. */
13811 mark_common_block_symbol_computed (struct symbol
*sym
,
13812 struct die_info
*common_die
,
13813 struct attribute
*common_loc
,
13814 struct attribute
*member_loc
,
13815 struct dwarf2_cu
*cu
)
13817 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13818 struct objfile
*objfile
= per_objfile
->objfile
;
13819 struct dwarf2_locexpr_baton
*baton
;
13821 unsigned int cu_off
;
13822 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
13823 LONGEST offset
= 0;
13825 gdb_assert (common_loc
&& member_loc
);
13826 gdb_assert (common_loc
->form_is_block ());
13827 gdb_assert (member_loc
->form_is_block ()
13828 || member_loc
->form_is_constant ());
13830 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
13831 baton
->per_objfile
= per_objfile
;
13832 baton
->per_cu
= cu
->per_cu
;
13833 gdb_assert (baton
->per_cu
);
13835 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13837 if (member_loc
->form_is_constant ())
13839 offset
= member_loc
->constant_value (0);
13840 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
13843 baton
->size
+= member_loc
->as_block ()->size
;
13845 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
13848 *ptr
++ = DW_OP_call4
;
13849 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
13850 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
13853 if (member_loc
->form_is_constant ())
13855 *ptr
++ = DW_OP_addr
;
13856 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
13857 ptr
+= cu
->header
.addr_size
;
13861 /* We have to copy the data here, because DW_OP_call4 will only
13862 use a DW_AT_location attribute. */
13863 struct dwarf_block
*block
= member_loc
->as_block ();
13864 memcpy (ptr
, block
->data
, block
->size
);
13865 ptr
+= block
->size
;
13868 *ptr
++ = DW_OP_plus
;
13869 gdb_assert (ptr
- baton
->data
== baton
->size
);
13871 SYMBOL_LOCATION_BATON (sym
) = baton
;
13872 sym
->set_aclass_index (dwarf2_locexpr_index
);
13875 /* Create appropriate locally-scoped variables for all the
13876 DW_TAG_common_block entries. Also create a struct common_block
13877 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
13878 is used to separate the common blocks name namespace from regular
13882 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
13884 struct attribute
*attr
;
13886 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
13887 if (attr
!= nullptr)
13889 /* Support the .debug_loc offsets. */
13890 if (attr
->form_is_block ())
13894 else if (attr
->form_is_section_offset ())
13896 dwarf2_complex_location_expr_complaint ();
13901 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13902 "common block member");
13907 if (die
->child
!= NULL
)
13909 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13910 struct die_info
*child_die
;
13911 size_t n_entries
= 0, size
;
13912 struct common_block
*common_block
;
13913 struct symbol
*sym
;
13915 for (child_die
= die
->child
;
13916 child_die
&& child_die
->tag
;
13917 child_die
= child_die
->sibling
)
13920 size
= (sizeof (struct common_block
)
13921 + (n_entries
- 1) * sizeof (struct symbol
*));
13923 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
13925 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
13926 common_block
->n_entries
= 0;
13928 for (child_die
= die
->child
;
13929 child_die
&& child_die
->tag
;
13930 child_die
= child_die
->sibling
)
13932 /* Create the symbol in the DW_TAG_common_block block in the current
13934 sym
= new_symbol (child_die
, NULL
, cu
);
13937 struct attribute
*member_loc
;
13939 common_block
->contents
[common_block
->n_entries
++] = sym
;
13941 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
13945 /* GDB has handled this for a long time, but it is
13946 not specified by DWARF. It seems to have been
13947 emitted by gfortran at least as recently as:
13948 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
13949 complaint (_("Variable in common block has "
13950 "DW_AT_data_member_location "
13951 "- DIE at %s [in module %s]"),
13952 sect_offset_str (child_die
->sect_off
),
13953 objfile_name (objfile
));
13955 if (member_loc
->form_is_section_offset ())
13956 dwarf2_complex_location_expr_complaint ();
13957 else if (member_loc
->form_is_constant ()
13958 || member_loc
->form_is_block ())
13960 if (attr
!= nullptr)
13961 mark_common_block_symbol_computed (sym
, die
, attr
,
13965 dwarf2_complex_location_expr_complaint ();
13970 sym
= new_symbol (die
, builtin_type (objfile
)->builtin_void
, cu
);
13971 sym
->set_value_common_block (common_block
);
13975 /* Create a type for a C++ namespace. */
13977 static struct type
*
13978 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
13980 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13981 const char *previous_prefix
, *name
;
13985 /* For extensions, reuse the type of the original namespace. */
13986 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
13988 struct die_info
*ext_die
;
13989 struct dwarf2_cu
*ext_cu
= cu
;
13991 ext_die
= dwarf2_extension (die
, &ext_cu
);
13992 type
= read_type_die (ext_die
, ext_cu
);
13994 /* EXT_CU may not be the same as CU.
13995 Ensure TYPE is recorded with CU in die_type_hash. */
13996 return set_die_type (die
, type
, cu
);
13999 name
= namespace_name (die
, &is_anonymous
, cu
);
14001 /* Now build the name of the current namespace. */
14003 previous_prefix
= determine_prefix (die
, cu
);
14004 if (previous_prefix
[0] != '\0')
14005 name
= typename_concat (&objfile
->objfile_obstack
,
14006 previous_prefix
, name
, 0, cu
);
14008 /* Create the type. */
14009 type
= type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_NAMESPACE
,
14012 return set_die_type (die
, type
, cu
);
14015 /* Read a namespace scope. */
14018 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
14020 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14023 /* Add a symbol associated to this if we haven't seen the namespace
14024 before. Also, add a using directive if it's an anonymous
14027 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
14031 type
= read_type_die (die
, cu
);
14032 new_symbol (die
, type
, cu
);
14034 namespace_name (die
, &is_anonymous
, cu
);
14037 const char *previous_prefix
= determine_prefix (die
, cu
);
14039 std::vector
<const char *> excludes
;
14040 add_using_directive (using_directives (cu
),
14041 previous_prefix
, type
->name (), NULL
,
14043 read_decl_line (die
, cu
),
14044 &objfile
->objfile_obstack
);
14048 if (die
->child
!= NULL
)
14050 struct die_info
*child_die
= die
->child
;
14052 while (child_die
&& child_die
->tag
)
14054 process_die (child_die
, cu
);
14055 child_die
= child_die
->sibling
;
14060 /* Read a Fortran module as type. This DIE can be only a declaration used for
14061 imported module. Still we need that type as local Fortran "use ... only"
14062 declaration imports depend on the created type in determine_prefix. */
14064 static struct type
*
14065 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14067 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14068 const char *module_name
;
14071 module_name
= dwarf2_name (die
, cu
);
14072 type
= type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_MODULE
,
14075 return set_die_type (die
, type
, cu
);
14078 /* Read a Fortran module. */
14081 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
14083 struct die_info
*child_die
= die
->child
;
14086 type
= read_type_die (die
, cu
);
14087 new_symbol (die
, type
, cu
);
14089 while (child_die
&& child_die
->tag
)
14091 process_die (child_die
, cu
);
14092 child_die
= child_die
->sibling
;
14096 /* Return the name of the namespace represented by DIE. Set
14097 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14100 static const char *
14101 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
14103 struct die_info
*current_die
;
14104 const char *name
= NULL
;
14106 /* Loop through the extensions until we find a name. */
14108 for (current_die
= die
;
14109 current_die
!= NULL
;
14110 current_die
= dwarf2_extension (die
, &cu
))
14112 /* We don't use dwarf2_name here so that we can detect the absence
14113 of a name -> anonymous namespace. */
14114 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
14120 /* Is it an anonymous namespace? */
14122 *is_anonymous
= (name
== NULL
);
14124 name
= CP_ANONYMOUS_NAMESPACE_STR
;
14129 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14130 the user defined type vector. */
14132 static struct type
*
14133 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14135 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
14136 struct comp_unit_head
*cu_header
= &cu
->header
;
14138 struct attribute
*attr_byte_size
;
14139 struct attribute
*attr_address_class
;
14140 int byte_size
, addr_class
;
14141 struct type
*target_type
;
14143 target_type
= die_type (die
, cu
);
14145 /* The die_type call above may have already set the type for this DIE. */
14146 type
= get_die_type (die
, cu
);
14150 type
= lookup_pointer_type (target_type
);
14152 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14153 if (attr_byte_size
)
14154 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
14156 byte_size
= cu_header
->addr_size
;
14158 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
14159 if (attr_address_class
)
14160 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
14162 addr_class
= DW_ADDR_none
;
14164 ULONGEST alignment
= get_alignment (cu
, die
);
14166 /* If the pointer size, alignment, or address class is different
14167 than the default, create a type variant marked as such and set
14168 the length accordingly. */
14169 if (type
->length () != byte_size
14170 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
14171 && alignment
!= TYPE_RAW_ALIGN (type
))
14172 || addr_class
!= DW_ADDR_none
)
14174 if (gdbarch_address_class_type_flags_p (gdbarch
))
14176 type_instance_flags type_flags
14177 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
14179 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
14181 type
= make_type_with_address_space (type
, type_flags
);
14183 else if (type
->length () != byte_size
)
14185 complaint (_("invalid pointer size %d"), byte_size
);
14187 else if (TYPE_RAW_ALIGN (type
) != alignment
)
14189 complaint (_("Invalid DW_AT_alignment"
14190 " - DIE at %s [in module %s]"),
14191 sect_offset_str (die
->sect_off
),
14192 objfile_name (cu
->per_objfile
->objfile
));
14196 /* Should we also complain about unhandled address classes? */
14200 type
->set_length (byte_size
);
14201 set_type_align (type
, alignment
);
14202 return set_die_type (die
, type
, cu
);
14205 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14206 the user defined type vector. */
14208 static struct type
*
14209 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14212 struct type
*to_type
;
14213 struct type
*domain
;
14215 to_type
= die_type (die
, cu
);
14216 domain
= die_containing_type (die
, cu
);
14218 /* The calls above may have already set the type for this DIE. */
14219 type
= get_die_type (die
, cu
);
14223 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
14224 type
= lookup_methodptr_type (to_type
);
14225 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
14227 struct type
*new_type
14228 = type_allocator (cu
->per_objfile
->objfile
, cu
->lang ()).new_type ();
14230 smash_to_method_type (new_type
, domain
, to_type
->target_type (),
14231 to_type
->fields (), to_type
->num_fields (),
14232 to_type
->has_varargs ());
14233 type
= lookup_methodptr_type (new_type
);
14236 type
= lookup_memberptr_type (to_type
, domain
);
14238 return set_die_type (die
, type
, cu
);
14241 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14242 the user defined type vector. */
14244 static struct type
*
14245 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
14246 enum type_code refcode
)
14248 struct comp_unit_head
*cu_header
= &cu
->header
;
14249 struct type
*type
, *target_type
;
14250 struct attribute
*attr
;
14252 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
14254 target_type
= die_type (die
, cu
);
14256 /* The die_type call above may have already set the type for this DIE. */
14257 type
= get_die_type (die
, cu
);
14261 type
= lookup_reference_type (target_type
, refcode
);
14262 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14263 if (attr
!= nullptr)
14264 type
->set_length (attr
->constant_value (cu_header
->addr_size
));
14266 type
->set_length (cu_header
->addr_size
);
14268 maybe_set_alignment (cu
, die
, type
);
14269 return set_die_type (die
, type
, cu
);
14272 /* Add the given cv-qualifiers to the element type of the array. GCC
14273 outputs DWARF type qualifiers that apply to an array, not the
14274 element type. But GDB relies on the array element type to carry
14275 the cv-qualifiers. This mimics section 6.7.3 of the C99
14278 static struct type
*
14279 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
14280 struct type
*base_type
, int cnst
, int voltl
)
14282 struct type
*el_type
, *inner_array
;
14284 base_type
= copy_type (base_type
);
14285 inner_array
= base_type
;
14287 while (inner_array
->target_type ()->code () == TYPE_CODE_ARRAY
)
14289 inner_array
->set_target_type (copy_type (inner_array
->target_type ()));
14290 inner_array
= inner_array
->target_type ();
14293 el_type
= inner_array
->target_type ();
14294 cnst
|= TYPE_CONST (el_type
);
14295 voltl
|= TYPE_VOLATILE (el_type
);
14296 inner_array
->set_target_type (make_cv_type (cnst
, voltl
, el_type
, NULL
));
14298 return set_die_type (die
, base_type
, cu
);
14301 static struct type
*
14302 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14304 struct type
*base_type
, *cv_type
;
14306 base_type
= die_type (die
, cu
);
14308 /* The die_type call above may have already set the type for this DIE. */
14309 cv_type
= get_die_type (die
, cu
);
14313 /* In case the const qualifier is applied to an array type, the element type
14314 is so qualified, not the array type (section 6.7.3 of C99). */
14315 if (base_type
->code () == TYPE_CODE_ARRAY
)
14316 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
14318 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
14319 return set_die_type (die
, cv_type
, cu
);
14322 static struct type
*
14323 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14325 struct type
*base_type
, *cv_type
;
14327 base_type
= die_type (die
, cu
);
14329 /* The die_type call above may have already set the type for this DIE. */
14330 cv_type
= get_die_type (die
, cu
);
14334 /* In case the volatile qualifier is applied to an array type, the
14335 element type is so qualified, not the array type (section 6.7.3
14337 if (base_type
->code () == TYPE_CODE_ARRAY
)
14338 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
14340 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
14341 return set_die_type (die
, cv_type
, cu
);
14344 /* Handle DW_TAG_restrict_type. */
14346 static struct type
*
14347 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14349 struct type
*base_type
, *cv_type
;
14351 base_type
= die_type (die
, cu
);
14353 /* The die_type call above may have already set the type for this DIE. */
14354 cv_type
= get_die_type (die
, cu
);
14358 cv_type
= make_restrict_type (base_type
);
14359 return set_die_type (die
, cv_type
, cu
);
14362 /* Handle DW_TAG_atomic_type. */
14364 static struct type
*
14365 read_tag_atomic_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14367 struct type
*base_type
, *cv_type
;
14369 base_type
= die_type (die
, cu
);
14371 /* The die_type call above may have already set the type for this DIE. */
14372 cv_type
= get_die_type (die
, cu
);
14376 cv_type
= make_atomic_type (base_type
);
14377 return set_die_type (die
, cv_type
, cu
);
14380 /* Extract all information from a DW_TAG_string_type DIE and add to
14381 the user defined type vector. It isn't really a user defined type,
14382 but it behaves like one, with other DIE's using an AT_user_def_type
14383 attribute to reference it. */
14385 static struct type
*
14386 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14388 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14389 struct gdbarch
*gdbarch
= objfile
->arch ();
14390 struct type
*type
, *range_type
, *index_type
, *char_type
;
14391 struct attribute
*attr
;
14392 struct dynamic_prop prop
;
14393 bool length_is_constant
= true;
14396 /* There are a couple of places where bit sizes might be made use of
14397 when parsing a DW_TAG_string_type, however, no producer that we know
14398 of make use of these. Handling bit sizes that are a multiple of the
14399 byte size is easy enough, but what about other bit sizes? Lets deal
14400 with that problem when we have to. Warn about these attributes being
14401 unsupported, then parse the type and ignore them like we always
14403 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
14404 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
14406 static bool warning_printed
= false;
14407 if (!warning_printed
)
14409 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
14410 "currently supported on DW_TAG_string_type."));
14411 warning_printed
= true;
14415 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
14416 if (attr
!= nullptr && !attr
->form_is_constant ())
14418 /* The string length describes the location at which the length of
14419 the string can be found. The size of the length field can be
14420 specified with one of the attributes below. */
14421 struct type
*prop_type
;
14422 struct attribute
*len
14423 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
14424 if (len
== nullptr)
14425 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14426 if (len
!= nullptr && len
->form_is_constant ())
14428 /* Pass 0 as the default as we know this attribute is constant
14429 and the default value will not be returned. */
14430 LONGEST sz
= len
->constant_value (0);
14431 prop_type
= objfile_int_type (objfile
, sz
, true);
14435 /* If the size is not specified then we assume it is the size of
14436 an address on this target. */
14437 prop_type
= cu
->addr_sized_int_type (true);
14440 /* Convert the attribute into a dynamic property. */
14441 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
14444 length_is_constant
= false;
14446 else if (attr
!= nullptr)
14448 /* This DW_AT_string_length just contains the length with no
14449 indirection. There's no need to create a dynamic property in this
14450 case. Pass 0 for the default value as we know it will not be
14451 returned in this case. */
14452 length
= attr
->constant_value (0);
14454 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
14456 /* We don't currently support non-constant byte sizes for strings. */
14457 length
= attr
->constant_value (1);
14461 /* Use 1 as a fallback length if we have nothing else. */
14465 index_type
= builtin_type (objfile
)->builtin_int
;
14466 type_allocator
alloc (objfile
, cu
->lang ());
14467 if (length_is_constant
)
14468 range_type
= create_static_range_type (alloc
, index_type
, 1, length
);
14471 struct dynamic_prop low_bound
;
14473 low_bound
.set_const_val (1);
14474 range_type
= create_range_type (alloc
, index_type
, &low_bound
, &prop
, 0);
14476 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
14477 type
= create_string_type (alloc
, char_type
, range_type
);
14479 return set_die_type (die
, type
, cu
);
14482 /* Assuming that DIE corresponds to a function, returns nonzero
14483 if the function is prototyped. */
14486 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
14488 struct attribute
*attr
;
14490 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
14491 if (attr
&& attr
->as_boolean ())
14494 /* The DWARF standard implies that the DW_AT_prototyped attribute
14495 is only meaningful for C, but the concept also extends to other
14496 languages that allow unprototyped functions (Eg: Objective C).
14497 For all other languages, assume that functions are always
14499 if (cu
->lang () != language_c
14500 && cu
->lang () != language_objc
14501 && cu
->lang () != language_opencl
)
14504 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14505 prototyped and unprototyped functions; default to prototyped,
14506 since that is more common in modern code (and RealView warns
14507 about unprototyped functions). */
14508 if (producer_is_realview (cu
->producer
))
14514 /* Handle DIES due to C code like:
14518 int (*funcp)(int a, long l);
14522 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14524 static struct type
*
14525 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14527 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14528 struct type
*type
; /* Type that this function returns. */
14529 struct type
*ftype
; /* Function that returns above type. */
14530 struct attribute
*attr
;
14532 type
= die_type (die
, cu
);
14534 /* PR gas/29517 occurs in 2.39, and is fixed in 2.40, but it's only fixed
14535 for dwarf version >= 3 which supports DW_TAG_unspecified_type. */
14536 if (type
->code () == TYPE_CODE_VOID
14537 && !type
->is_stub ()
14538 && die
->child
== nullptr
14539 && (cu
->per_cu
->version () == 2
14540 || producer_is_gas_2_39 (cu
)))
14542 /* Work around PR gas/29517, pretend we have an DW_TAG_unspecified_type
14544 type
= (type_allocator (cu
->per_objfile
->objfile
, cu
->lang ())
14545 .new_type (TYPE_CODE_VOID
, 0, nullptr));
14546 type
->set_is_stub (true);
14549 /* The die_type call above may have already set the type for this DIE. */
14550 ftype
= get_die_type (die
, cu
);
14554 ftype
= lookup_function_type (type
);
14556 if (prototyped_function_p (die
, cu
))
14557 ftype
->set_is_prototyped (true);
14559 /* Store the calling convention in the type if it's available in
14560 the subroutine die. Otherwise set the calling convention to
14561 the default value DW_CC_normal. */
14562 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14563 if (attr
!= nullptr
14564 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
14565 TYPE_CALLING_CONVENTION (ftype
)
14566 = (enum dwarf_calling_convention
) attr
->constant_value (0);
14567 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
14568 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
14570 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
14572 /* Record whether the function returns normally to its caller or not
14573 if the DWARF producer set that information. */
14574 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
14575 if (attr
&& attr
->as_boolean ())
14576 TYPE_NO_RETURN (ftype
) = 1;
14578 /* We need to add the subroutine type to the die immediately so
14579 we don't infinitely recurse when dealing with parameters
14580 declared as the same subroutine type. */
14581 set_die_type (die
, ftype
, cu
);
14583 if (die
->child
!= NULL
)
14585 struct type
*void_type
= builtin_type (objfile
)->builtin_void
;
14586 struct die_info
*child_die
;
14587 int nparams
, iparams
;
14589 /* Count the number of parameters.
14590 FIXME: GDB currently ignores vararg functions, but knows about
14591 vararg member functions. */
14593 child_die
= die
->child
;
14594 while (child_die
&& child_die
->tag
)
14596 if (child_die
->tag
== DW_TAG_formal_parameter
)
14598 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
14599 ftype
->set_has_varargs (true);
14601 child_die
= child_die
->sibling
;
14604 /* Allocate storage for parameters and fill them in. */
14605 ftype
->alloc_fields (nparams
);
14607 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14608 even if we error out during the parameters reading below. */
14609 for (iparams
= 0; iparams
< nparams
; iparams
++)
14610 ftype
->field (iparams
).set_type (void_type
);
14613 child_die
= die
->child
;
14614 while (child_die
&& child_die
->tag
)
14616 if (child_die
->tag
== DW_TAG_formal_parameter
)
14618 struct type
*arg_type
;
14620 /* DWARF version 2 has no clean way to discern C++
14621 static and non-static member functions. G++ helps
14622 GDB by marking the first parameter for non-static
14623 member functions (which is the this pointer) as
14624 artificial. We pass this information to
14625 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14627 DWARF version 3 added DW_AT_object_pointer, which GCC
14628 4.5 does not yet generate. */
14629 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
14630 if (attr
!= nullptr)
14631 ftype
->field (iparams
).set_is_artificial (attr
->as_boolean ());
14633 ftype
->field (iparams
).set_is_artificial (false);
14634 arg_type
= die_type (child_die
, cu
);
14636 /* RealView does not mark THIS as const, which the testsuite
14637 expects. GCC marks THIS as const in method definitions,
14638 but not in the class specifications (GCC PR 43053). */
14639 if (cu
->lang () == language_cplus
14640 && !TYPE_CONST (arg_type
)
14641 && ftype
->field (iparams
).is_artificial ())
14644 struct dwarf2_cu
*arg_cu
= cu
;
14645 const char *name
= dwarf2_name (child_die
, cu
);
14647 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
14648 if (attr
!= nullptr)
14650 /* If the compiler emits this, use it. */
14651 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
14654 else if (name
&& strcmp (name
, "this") == 0)
14655 /* Function definitions will have the argument names. */
14657 else if (name
== NULL
&& iparams
== 0)
14658 /* Declarations may not have the names, so like
14659 elsewhere in GDB, assume an artificial first
14660 argument is "this". */
14664 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
14668 ftype
->field (iparams
).set_type (arg_type
);
14671 child_die
= child_die
->sibling
;
14678 static struct type
*
14679 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
14681 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14682 const char *name
= dwarf2_full_name (NULL
, die
, cu
);
14683 struct type
*this_type
;
14684 struct gdbarch
*gdbarch
= objfile
->arch ();
14685 struct type
*target_type
= die_type (die
, cu
);
14687 if (gdbarch_dwarf2_omit_typedef_p (gdbarch
, target_type
, cu
->producer
, name
))
14689 /* The long double is defined as a base type in C. GCC creates a long
14690 double typedef with target-type _Float128 for the long double to
14691 identify it as the IEEE Float128 value. This is a GCC hack since the
14692 DWARF doesn't distinguish between the IBM long double and IEEE
14693 128-bit float. Replace the GCC workaround for the long double
14694 typedef with the actual type information copied from the target-type
14695 with the correct long double base type name. */
14696 this_type
= copy_type (target_type
);
14697 this_type
->set_name (name
);
14698 set_die_type (die
, this_type
, cu
);
14702 type_allocator
alloc (objfile
, cu
->lang ());
14703 this_type
= alloc
.new_type (TYPE_CODE_TYPEDEF
, 0, name
);
14704 this_type
->set_target_is_stub (true);
14705 set_die_type (die
, this_type
, cu
);
14706 if (target_type
!= this_type
)
14707 this_type
->set_target_type (target_type
);
14710 /* Self-referential typedefs are, it seems, not allowed by the DWARF
14711 spec and cause infinite loops in GDB. */
14712 complaint (_("Self-referential DW_TAG_typedef "
14713 "- DIE at %s [in module %s]"),
14714 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
14715 this_type
->set_target_type (nullptr);
14719 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
14720 anonymous typedefs, which is, strictly speaking, invalid DWARF.
14721 Handle these by just returning the target type, rather than
14722 constructing an anonymous typedef type and trying to handle this
14724 set_die_type (die
, target_type
, cu
);
14725 return target_type
;
14730 /* Helper for get_dwarf2_rational_constant that computes the value of
14731 a given gmp_mpz given an attribute. */
14734 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
14736 /* GCC will sometimes emit a 16-byte constant value as a DWARF
14737 location expression that pushes an implicit value. */
14738 if (attr
->form
== DW_FORM_exprloc
)
14740 dwarf_block
*blk
= attr
->as_block ();
14741 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
14744 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
14745 blk
->data
+ blk
->size
,
14747 if (ptr
- blk
->data
+ len
<= blk
->size
)
14749 value
->read (gdb::make_array_view (ptr
, len
),
14750 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
.get ())
14751 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
,
14757 /* On failure set it to 1. */
14758 *value
= gdb_mpz (1);
14760 else if (attr
->form_is_block ())
14762 dwarf_block
*blk
= attr
->as_block ();
14763 value
->read (gdb::make_array_view (blk
->data
, blk
->size
),
14764 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
.get ())
14765 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
,
14768 else if (attr
->form_is_unsigned ())
14769 *value
= gdb_mpz (attr
->as_unsigned ());
14771 *value
= gdb_mpz (attr
->constant_value (1));
14774 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
14775 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
14777 If the numerator and/or numerator attribute is missing,
14778 a complaint is filed, and NUMERATOR and DENOMINATOR are left
14782 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
14783 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
14785 struct attribute
*num_attr
, *denom_attr
;
14787 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
14788 if (num_attr
== nullptr)
14789 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
14790 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
14792 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
14793 if (denom_attr
== nullptr)
14794 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
14795 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
14797 if (num_attr
== nullptr || denom_attr
== nullptr)
14800 get_mpz (cu
, numerator
, num_attr
);
14801 get_mpz (cu
, denominator
, denom_attr
);
14804 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
14805 rational constant, rather than a signed one.
14807 If the rational constant has a negative value, a complaint
14808 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
14811 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
14812 struct dwarf2_cu
*cu
,
14813 gdb_mpz
*numerator
,
14814 gdb_mpz
*denominator
)
14819 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
14820 if (num
< 0 && denom
< 0)
14827 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
14829 sect_offset_str (die
->sect_off
));
14832 else if (denom
< 0)
14834 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
14836 sect_offset_str (die
->sect_off
));
14840 *numerator
= std::move (num
);
14841 *denominator
= std::move (denom
);
14844 /* Assuming that ENCODING is a string whose contents starting at the
14845 K'th character is "_nn" where "nn" is a decimal number, scan that
14846 number and set RESULT to the value. K is updated to point to the
14847 character immediately following the number.
14849 If the string does not conform to the format described above, false
14850 is returned, and K may or may not be changed. */
14853 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
14855 /* The next character should be an underscore ('_') followed
14857 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
14860 /* Skip the underscore. */
14864 /* Determine the number of digits for our number. */
14865 while (isdigit (encoding
[k
]))
14870 std::string
copy (&encoding
[start
], k
- start
);
14871 return result
->set (copy
.c_str (), 10);
14874 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
14875 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
14876 DENOM, update OFFSET, and return true on success. Return false on
14880 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
14881 gdb_mpz
*num
, gdb_mpz
*denom
)
14883 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
14885 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
14888 /* Assuming DIE corresponds to a fixed point type, finish the creation
14889 of the corresponding TYPE by setting its type-specific data. CU is
14890 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
14891 encodings. It is nullptr if the GNAT encoding should be
14895 finish_fixed_point_type (struct type
*type
, const char *suffix
,
14896 struct die_info
*die
, struct dwarf2_cu
*cu
)
14898 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
14899 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
14901 /* If GNAT encodings are preferred, don't examine the
14903 struct attribute
*attr
= nullptr;
14904 if (suffix
== nullptr)
14906 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
14907 if (attr
== nullptr)
14908 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
14909 if (attr
== nullptr)
14910 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
14913 /* Numerator and denominator of our fixed-point type's scaling factor.
14914 The default is a scaling factor of 1, which we use as a fallback
14915 when we are not able to decode it (problem with the debugging info,
14916 unsupported forms, bug in GDB, etc...). Using that as the default
14917 allows us to at least print the unscaled value, which might still
14918 be useful to a user. */
14919 gdb_mpz
scale_num (1);
14920 gdb_mpz
scale_denom (1);
14922 if (attr
== nullptr)
14925 if (suffix
!= nullptr
14926 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
14928 /* The number might be encoded as _nn_dd_nn_dd, where the
14929 second ratio is the 'small value. In this situation, we
14930 want the second value. */
14931 && (suffix
[offset
] != '_'
14932 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
14939 /* Scaling factor not found. Assume a scaling factor of 1,
14940 and hope for the best. At least the user will be able to
14941 see the encoded value. */
14944 complaint (_("no scale found for fixed-point type (DIE at %s)"),
14945 sect_offset_str (die
->sect_off
));
14948 else if (attr
->name
== DW_AT_binary_scale
)
14950 LONGEST scale_exp
= attr
->constant_value (0);
14951 gdb_mpz
&num_or_denom
= scale_exp
> 0 ? scale_num
: scale_denom
;
14953 num_or_denom
<<= std::abs (scale_exp
);
14955 else if (attr
->name
== DW_AT_decimal_scale
)
14957 LONGEST scale_exp
= attr
->constant_value (0);
14958 gdb_mpz
&num_or_denom
= scale_exp
> 0 ? scale_num
: scale_denom
;
14960 num_or_denom
= gdb_mpz::pow (10, std::abs (scale_exp
));
14962 else if (attr
->name
== DW_AT_small
)
14964 struct die_info
*scale_die
;
14965 struct dwarf2_cu
*scale_cu
= cu
;
14967 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
14968 if (scale_die
->tag
== DW_TAG_constant
)
14969 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
14970 &scale_num
, &scale_denom
);
14972 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
14974 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
14978 complaint (_("unsupported scale attribute %s for fixed-point type"
14980 dwarf_attr_name (attr
->name
),
14981 sect_offset_str (die
->sect_off
));
14984 type
->fixed_point_info ().scaling_factor
= gdb_mpq (scale_num
, scale_denom
);
14987 /* The gnat-encoding suffix for fixed point. */
14989 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
14991 /* If NAME encodes an Ada fixed-point type, return a pointer to the
14992 "XF" suffix of the name. The text after this is what encodes the
14993 'small and 'delta information. Otherwise, return nullptr. */
14995 static const char *
14996 gnat_encoded_fixed_point_type_info (const char *name
)
14998 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
15001 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15002 (which may be different from NAME) to the architecture back-end to allow
15003 it to guess the correct format if necessary. */
15005 static struct type
*
15006 dwarf2_init_float_type (struct dwarf2_cu
*cu
, int bits
, const char *name
,
15007 const char *name_hint
, enum bfd_endian byte_order
)
15009 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15010 struct gdbarch
*gdbarch
= objfile
->arch ();
15011 const struct floatformat
**format
;
15014 type_allocator
alloc (objfile
, cu
->lang ());
15015 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
15017 type
= init_float_type (alloc
, bits
, name
, format
, byte_order
);
15019 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
15024 /* Allocate an integer type of size BITS and name NAME. */
15026 static struct type
*
15027 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, int bits
, int unsigned_p
,
15031 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15033 /* Versions of Intel's C Compiler generate an integer type called "void"
15034 instead of using DW_TAG_unspecified_type. This has been seen on
15035 at least versions 14, 17, and 18. */
15036 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
15037 && strcmp (name
, "void") == 0)
15038 type
= builtin_type (objfile
)->builtin_void
;
15041 type_allocator
alloc (objfile
, cu
->lang ());
15042 type
= init_integer_type (alloc
, bits
, unsigned_p
, name
);
15048 /* Return true if DIE has a DW_AT_small attribute whose value is
15049 a constant rational, where both the numerator and denominator
15052 CU is the DIE's Compilation Unit. */
15055 has_zero_over_zero_small_attribute (struct die_info
*die
,
15056 struct dwarf2_cu
*cu
)
15058 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
15059 if (attr
== nullptr)
15062 struct dwarf2_cu
*scale_cu
= cu
;
15063 struct die_info
*scale_die
15064 = follow_die_ref (die
, attr
, &scale_cu
);
15066 if (scale_die
->tag
!= DW_TAG_constant
)
15069 gdb_mpz
num (1), denom (1);
15070 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
15071 return num
== 0 && denom
== 0;
15074 /* Initialise and return a floating point type of size BITS suitable for
15075 use as a component of a complex number. The NAME_HINT is passed through
15076 when initialising the floating point type and is the name of the complex
15079 As DWARF doesn't currently provide an explicit name for the components
15080 of a complex number, but it can be helpful to have these components
15081 named, we try to select a suitable name based on the size of the
15083 static struct type
*
15084 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
15085 int bits
, const char *name_hint
,
15086 enum bfd_endian byte_order
)
15088 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15089 gdbarch
*gdbarch
= objfile
->arch ();
15090 struct type
*tt
= nullptr;
15092 /* Try to find a suitable floating point builtin type of size BITS.
15093 We're going to use the name of this type as the name for the complex
15094 target type that we are about to create. */
15095 switch (cu
->lang ())
15097 case language_fortran
:
15101 tt
= builtin_f_type (gdbarch
)->builtin_real
;
15104 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
15106 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15108 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
15116 tt
= builtin_type (gdbarch
)->builtin_float
;
15119 tt
= builtin_type (gdbarch
)->builtin_double
;
15121 case 96: /* The x86-32 ABI specifies 96-bit long double. */
15123 tt
= builtin_type (gdbarch
)->builtin_long_double
;
15129 /* If the type we found doesn't match the size we were looking for, then
15130 pretend we didn't find a type at all, the complex target type we
15131 create will then be nameless. */
15132 if (tt
!= nullptr && tt
->length () * TARGET_CHAR_BIT
!= bits
)
15135 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
15136 return dwarf2_init_float_type (cu
, bits
, name
, name_hint
, byte_order
);
15139 /* Find a representation of a given base type and install
15140 it in the TYPE field of the die. */
15142 static struct type
*
15143 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15145 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15147 struct attribute
*attr
;
15148 int encoding
= 0, bits
= 0;
15151 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
15152 if (attr
!= nullptr && attr
->form_is_constant ())
15153 encoding
= attr
->constant_value (0);
15154 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15155 if (attr
!= nullptr)
15156 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
15157 name
= dwarf2_name (die
, cu
);
15159 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
15161 enum bfd_endian byte_order
;
15162 bool not_default
= die_byte_order (die
, cu
, &byte_order
);
15164 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
15165 && cu
->lang () == language_ada
15166 && has_zero_over_zero_small_attribute (die
, cu
))
15168 /* brobecker/2018-02-24: This is a fixed point type for which
15169 the scaling factor is represented as fraction whose value
15170 does not make sense (zero divided by zero), so we should
15171 normally never see these. However, there is a small category
15172 of fixed point types for which GNAT is unable to provide
15173 the scaling factor via the standard DWARF mechanisms, and
15174 for which the info is provided via the GNAT encodings instead.
15175 This is likely what this DIE is about. */
15176 encoding
= (encoding
== DW_ATE_signed_fixed
15178 : DW_ATE_unsigned
);
15181 /* With GNAT encodings, fixed-point information will be encoded in
15182 the type name. Note that this can also occur with the above
15183 zero-over-zero case, which is why this is a separate "if" rather
15184 than an "else if". */
15185 const char *gnat_encoding_suffix
= nullptr;
15186 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
15187 && cu
->lang () == language_ada
15188 && name
!= nullptr)
15190 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
15191 if (gnat_encoding_suffix
!= nullptr)
15193 gdb_assert (startswith (gnat_encoding_suffix
,
15194 GNAT_FIXED_POINT_SUFFIX
));
15195 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
15196 name
, gnat_encoding_suffix
- name
);
15197 /* Use -1 here so that SUFFIX points at the "_" after the
15199 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
15201 encoding
= (encoding
== DW_ATE_signed
15202 ? DW_ATE_signed_fixed
15203 : DW_ATE_unsigned_fixed
);
15207 type_allocator
alloc (objfile
, cu
->lang ());
15210 case DW_ATE_address
:
15211 /* Turn DW_ATE_address into a void * pointer. */
15212 type
= alloc
.new_type (TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
15213 type
= init_pointer_type (alloc
, bits
, name
, type
);
15215 case DW_ATE_boolean
:
15216 type
= init_boolean_type (alloc
, bits
, 1, name
);
15218 case DW_ATE_complex_float
:
15219 type
= dwarf2_init_complex_target_type (cu
, bits
/ 2, name
,
15221 if (type
->code () == TYPE_CODE_ERROR
)
15223 if (name
== nullptr)
15225 struct obstack
*obstack
15226 = &cu
->per_objfile
->objfile
->objfile_obstack
;
15227 name
= obconcat (obstack
, "_Complex ", type
->name (),
15230 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
15233 type
= init_complex_type (name
, type
);
15235 case DW_ATE_decimal_float
:
15236 type
= init_decfloat_type (alloc
, bits
, name
);
15239 type
= dwarf2_init_float_type (cu
, bits
, name
, name
, byte_order
);
15241 case DW_ATE_signed
:
15242 type
= dwarf2_init_integer_type (cu
, bits
, 0, name
);
15244 case DW_ATE_unsigned
:
15245 if (cu
->lang () == language_fortran
15247 && startswith (name
, "character("))
15248 type
= init_character_type (alloc
, bits
, 1, name
);
15250 type
= dwarf2_init_integer_type (cu
, bits
, 1, name
);
15252 case DW_ATE_signed_char
:
15253 if (cu
->lang () == language_ada
15254 || cu
->lang () == language_m2
15255 || cu
->lang () == language_pascal
15256 || cu
->lang () == language_fortran
)
15257 type
= init_character_type (alloc
, bits
, 0, name
);
15259 type
= dwarf2_init_integer_type (cu
, bits
, 0, name
);
15261 case DW_ATE_unsigned_char
:
15262 if (cu
->lang () == language_ada
15263 || cu
->lang () == language_m2
15264 || cu
->lang () == language_pascal
15265 || cu
->lang () == language_fortran
15266 || cu
->lang () == language_rust
)
15267 type
= init_character_type (alloc
, bits
, 1, name
);
15269 type
= dwarf2_init_integer_type (cu
, bits
, 1, name
);
15273 type
= init_character_type (alloc
, bits
, 1, name
);
15274 return set_die_type (die
, type
, cu
);
15277 case DW_ATE_signed_fixed
:
15278 type
= init_fixed_point_type (alloc
, bits
, 0, name
);
15279 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
15281 case DW_ATE_unsigned_fixed
:
15282 type
= init_fixed_point_type (alloc
, bits
, 1, name
);
15283 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
15287 complaint (_("unsupported DW_AT_encoding: '%s'"),
15288 dwarf_type_encoding_name (encoding
));
15289 type
= alloc
.new_type (TYPE_CODE_ERROR
, bits
, name
);
15293 if (type
->code () == TYPE_CODE_INT
15295 && strcmp (name
, "char") == 0)
15296 type
->set_has_no_signedness (true);
15298 maybe_set_alignment (cu
, die
, type
);
15300 type
->set_endianity_is_not_default (not_default
);
15302 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
15304 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
15305 if (attr
!= nullptr && attr
->form_is_constant ())
15307 unsigned real_bit_size
= attr
->constant_value (0);
15308 if (real_bit_size
>= 0 && real_bit_size
<= 8 * type
->length ())
15310 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
15311 /* Only use the attributes if they make sense together. */
15312 if (attr
== nullptr
15313 || (attr
->form_is_constant ()
15314 && attr
->constant_value (0) >= 0
15315 && (attr
->constant_value (0) + real_bit_size
15316 <= 8 * type
->length ())))
15318 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
15320 if (attr
!= nullptr)
15321 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
15322 = attr
->constant_value (0);
15328 return set_die_type (die
, type
, cu
);
15331 /* A helper function that returns the name of DIE, if it refers to a
15332 variable declaration. */
15334 static const char *
15335 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
15337 if (die
->tag
!= DW_TAG_variable
)
15340 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
15341 if (attr
== nullptr || !attr
->as_boolean ())
15344 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
15345 if (attr
== nullptr)
15347 return attr
->as_string ();
15350 /* Parse dwarf attribute if it's a block, reference or constant and put the
15351 resulting value of the attribute into struct bound_prop.
15352 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15355 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
15356 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
15357 struct type
*default_type
)
15359 struct dwarf2_property_baton
*baton
;
15360 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15361 struct objfile
*objfile
= per_objfile
->objfile
;
15362 struct obstack
*obstack
= &objfile
->objfile_obstack
;
15364 gdb_assert (default_type
!= NULL
);
15366 if (attr
== NULL
|| prop
== NULL
)
15369 if (attr
->form_is_block ())
15371 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15372 baton
->property_type
= default_type
;
15373 baton
->locexpr
.per_cu
= cu
->per_cu
;
15374 baton
->locexpr
.per_objfile
= per_objfile
;
15376 struct dwarf_block block
;
15377 if (attr
->form
== DW_FORM_data16
)
15379 size_t data_size
= 16;
15380 block
.size
= (data_size
15381 + 2 /* Extra bytes for DW_OP and arg. */);
15382 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
.size
);
15383 data
[0] = DW_OP_implicit_value
;
15384 data
[1] = data_size
;
15385 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
15389 block
= *attr
->as_block ();
15391 baton
->locexpr
.size
= block
.size
;
15392 baton
->locexpr
.data
= block
.data
;
15393 switch (attr
->name
)
15395 case DW_AT_string_length
:
15396 baton
->locexpr
.is_reference
= true;
15399 baton
->locexpr
.is_reference
= false;
15403 prop
->set_locexpr (baton
);
15404 gdb_assert (prop
->baton () != NULL
);
15406 else if (attr
->form_is_ref ())
15408 struct dwarf2_cu
*target_cu
= cu
;
15409 struct die_info
*target_die
;
15410 struct attribute
*target_attr
;
15412 target_die
= follow_die_ref (die
, attr
, &target_cu
);
15413 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
15414 if (target_attr
== NULL
)
15415 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
15417 if (target_attr
== nullptr)
15418 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
15420 if (target_attr
== NULL
)
15422 const char *name
= var_decl_name (target_die
, target_cu
);
15423 if (name
!= nullptr)
15425 prop
->set_variable_name (name
);
15431 switch (target_attr
->name
)
15433 case DW_AT_location
:
15434 if (target_attr
->form_is_section_offset ())
15436 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15437 baton
->property_type
= die_type (target_die
, target_cu
);
15438 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
15439 prop
->set_loclist (baton
);
15440 gdb_assert (prop
->baton () != NULL
);
15442 else if (target_attr
->form_is_block ())
15444 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15445 baton
->property_type
= die_type (target_die
, target_cu
);
15446 baton
->locexpr
.per_cu
= cu
->per_cu
;
15447 baton
->locexpr
.per_objfile
= per_objfile
;
15448 struct dwarf_block
*block
= target_attr
->as_block ();
15449 baton
->locexpr
.size
= block
->size
;
15450 baton
->locexpr
.data
= block
->data
;
15451 baton
->locexpr
.is_reference
= true;
15452 prop
->set_locexpr (baton
);
15453 gdb_assert (prop
->baton () != NULL
);
15457 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15458 "dynamic property");
15462 case DW_AT_data_member_location
:
15463 case DW_AT_data_bit_offset
:
15467 if (!handle_member_location (target_die
, target_cu
, &offset
))
15470 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15471 baton
->property_type
= read_type_die (target_die
->parent
,
15473 baton
->offset_info
.offset
= offset
;
15474 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
15475 prop
->set_addr_offset (baton
);
15480 else if (attr
->form_is_constant ())
15481 prop
->set_const_val (attr
->constant_value (0));
15482 else if (attr
->form_is_section_offset ())
15484 switch (attr
->name
)
15486 case DW_AT_string_length
:
15487 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
15488 baton
->property_type
= default_type
;
15489 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
15490 prop
->set_loclist (baton
);
15491 gdb_assert (prop
->baton () != NULL
);
15503 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
15504 dwarf2_name (die
, cu
));
15510 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
15511 present (which is valid) then compute the default type based on the
15512 compilation units address size. */
15514 static struct type
*
15515 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15517 struct type
*index_type
= die_type (die
, cu
);
15519 /* Dwarf-2 specifications explicitly allows to create subrange types
15520 without specifying a base type.
15521 In that case, the base type must be set to the type of
15522 the lower bound, upper bound or count, in that order, if any of these
15523 three attributes references an object that has a type.
15524 If no base type is found, the Dwarf-2 specifications say that
15525 a signed integer type of size equal to the size of an address should
15527 For the following C code: `extern char gdb_int [];'
15528 GCC produces an empty range DIE.
15529 FIXME: muller/2010-05-28: Possible references to object for low bound,
15530 high bound or count are not yet handled by this code. */
15531 if (index_type
->code () == TYPE_CODE_VOID
)
15532 index_type
= cu
->addr_sized_int_type (false);
15537 /* Read the given DW_AT_subrange DIE. */
15539 static struct type
*
15540 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15542 struct type
*base_type
, *orig_base_type
;
15543 struct type
*range_type
;
15544 struct attribute
*attr
;
15545 struct dynamic_prop low
, high
;
15546 int low_default_is_valid
;
15547 int high_bound_is_count
= 0;
15550 orig_base_type
= read_subrange_index_type (die
, cu
);
15552 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15553 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15554 creating the range type, but we use the result of check_typedef
15555 when examining properties of the type. */
15556 base_type
= check_typedef (orig_base_type
);
15558 /* The die_type call above may have already set the type for this DIE. */
15559 range_type
= get_die_type (die
, cu
);
15563 high
.set_const_val (0);
15565 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15566 omitting DW_AT_lower_bound. */
15567 switch (cu
->lang ())
15570 case language_cplus
:
15571 low
.set_const_val (0);
15572 low_default_is_valid
= 1;
15574 case language_fortran
:
15575 low
.set_const_val (1);
15576 low_default_is_valid
= 1;
15579 case language_objc
:
15580 case language_rust
:
15581 low
.set_const_val (0);
15582 low_default_is_valid
= (cu
->header
.version
>= 4);
15586 case language_pascal
:
15587 low
.set_const_val (1);
15588 low_default_is_valid
= (cu
->header
.version
>= 4);
15591 low
.set_const_val (0);
15592 low_default_is_valid
= 0;
15596 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
15597 if (attr
!= nullptr)
15598 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
15599 else if (!low_default_is_valid
)
15600 complaint (_("Missing DW_AT_lower_bound "
15601 "- DIE at %s [in module %s]"),
15602 sect_offset_str (die
->sect_off
),
15603 objfile_name (cu
->per_objfile
->objfile
));
15605 struct attribute
*attr_ub
, *attr_count
;
15606 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
15607 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
15609 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
15610 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
15612 /* If bounds are constant do the final calculation here. */
15613 if (low
.is_constant () && high
.is_constant ())
15614 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
15616 high_bound_is_count
= 1;
15620 if (attr_ub
!= NULL
)
15621 complaint (_("Unresolved DW_AT_upper_bound "
15622 "- DIE at %s [in module %s]"),
15623 sect_offset_str (die
->sect_off
),
15624 objfile_name (cu
->per_objfile
->objfile
));
15625 if (attr_count
!= NULL
)
15626 complaint (_("Unresolved DW_AT_count "
15627 "- DIE at %s [in module %s]"),
15628 sect_offset_str (die
->sect_off
),
15629 objfile_name (cu
->per_objfile
->objfile
));
15634 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
15635 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
15636 bias
= bias_attr
->constant_value (0);
15638 /* Normally, the DWARF producers are expected to use a signed
15639 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15640 But this is unfortunately not always the case, as witnessed
15641 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15642 is used instead. To work around that ambiguity, we treat
15643 the bounds as signed, and thus sign-extend their values, when
15644 the base type is signed.
15646 Skip it if the base type's length is larger than ULONGEST, to avoid
15647 the undefined behavior of a too large left shift. We don't really handle
15648 constants larger than 8 bytes anyway, at the moment. */
15650 if (base_type
->length () <= sizeof (ULONGEST
))
15652 ULONGEST negative_mask
15653 = -((ULONGEST
) 1 << (base_type
->length () * TARGET_CHAR_BIT
- 1));
15655 if (low
.is_constant ()
15656 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
15657 low
.set_const_val (low
.const_val () | negative_mask
);
15659 if (high
.is_constant ()
15660 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
15661 high
.set_const_val (high
.const_val () | negative_mask
);
15664 /* Check for bit and byte strides. */
15665 struct dynamic_prop byte_stride_prop
;
15666 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15667 if (attr_byte_stride
!= nullptr)
15669 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15670 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
15674 struct dynamic_prop bit_stride_prop
;
15675 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15676 if (attr_bit_stride
!= nullptr)
15678 /* It only makes sense to have either a bit or byte stride. */
15679 if (attr_byte_stride
!= nullptr)
15681 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
15682 "- DIE at %s [in module %s]"),
15683 sect_offset_str (die
->sect_off
),
15684 objfile_name (cu
->per_objfile
->objfile
));
15685 attr_bit_stride
= nullptr;
15689 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15690 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
15695 type_allocator
alloc (cu
->per_objfile
->objfile
, cu
->lang ());
15696 if (attr_byte_stride
!= nullptr
15697 || attr_bit_stride
!= nullptr)
15699 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
15700 struct dynamic_prop
*stride
15701 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
15704 = create_range_type_with_stride (alloc
, orig_base_type
, &low
,
15705 &high
, bias
, stride
, byte_stride_p
);
15708 range_type
= create_range_type (alloc
, orig_base_type
, &low
, &high
, bias
);
15710 if (high_bound_is_count
)
15711 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
15713 /* Ada expects an empty array on no boundary attributes. */
15714 if (attr
== NULL
&& cu
->lang () != language_ada
)
15715 range_type
->bounds ()->high
.set_undefined ();
15717 name
= dwarf2_name (die
, cu
);
15719 range_type
->set_name (name
);
15721 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15722 if (attr
!= nullptr)
15723 range_type
->set_length (attr
->constant_value (0));
15725 maybe_set_alignment (cu
, die
, range_type
);
15727 set_die_type (die
, range_type
, cu
);
15729 /* set_die_type should be already done. */
15730 set_descriptive_type (range_type
, die
, cu
);
15735 static struct type
*
15736 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15740 type
= (type_allocator (cu
->per_objfile
->objfile
, cu
->lang ())
15741 .new_type (TYPE_CODE_VOID
, 0, nullptr));
15742 type
->set_name (dwarf2_name (die
, cu
));
15744 /* In Ada, an unspecified type is typically used when the description
15745 of the type is deferred to a different unit. When encountering
15746 such a type, we treat it as a stub, and try to resolve it later on,
15748 Mark this as a stub type for all languages though. */
15749 type
->set_is_stub (true);
15751 return set_die_type (die
, type
, cu
);
15754 /* Read a single die and all its descendents. Set the die's sibling
15755 field to NULL; set other fields in the die correctly, and set all
15756 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15757 location of the info_ptr after reading all of those dies. PARENT
15758 is the parent of the die in question. */
15760 static struct die_info
*
15761 read_die_and_children (const struct die_reader_specs
*reader
,
15762 const gdb_byte
*info_ptr
,
15763 const gdb_byte
**new_info_ptr
,
15764 struct die_info
*parent
)
15766 struct die_info
*die
;
15767 const gdb_byte
*cur_ptr
;
15769 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0, true);
15772 *new_info_ptr
= cur_ptr
;
15775 store_in_ref_table (die
, reader
->cu
);
15777 if (die
->has_children
)
15778 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
15782 *new_info_ptr
= cur_ptr
;
15785 die
->sibling
= NULL
;
15786 die
->parent
= parent
;
15790 /* Read a die, all of its descendents, and all of its siblings; set
15791 all of the fields of all of the dies correctly. Arguments are as
15792 in read_die_and_children. */
15794 static struct die_info
*
15795 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
15796 const gdb_byte
*info_ptr
,
15797 const gdb_byte
**new_info_ptr
,
15798 struct die_info
*parent
)
15800 struct die_info
*first_die
, *last_sibling
;
15801 const gdb_byte
*cur_ptr
;
15803 cur_ptr
= info_ptr
;
15804 first_die
= last_sibling
= NULL
;
15808 struct die_info
*die
15809 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
15813 *new_info_ptr
= cur_ptr
;
15820 last_sibling
->sibling
= die
;
15822 last_sibling
= die
;
15826 /* Read a die, all of its descendents, and all of its siblings; set
15827 all of the fields of all of the dies correctly. Arguments are as
15828 in read_die_and_children.
15829 This the main entry point for reading a DIE and all its children. */
15831 static struct die_info
*
15832 read_die_and_siblings (const struct die_reader_specs
*reader
,
15833 const gdb_byte
*info_ptr
,
15834 const gdb_byte
**new_info_ptr
,
15835 struct die_info
*parent
)
15837 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
15838 new_info_ptr
, parent
);
15840 if (dwarf_die_debug
)
15842 gdb_printf (gdb_stdlog
,
15843 "Read die from %s@0x%x of %s:\n",
15844 reader
->die_section
->get_name (),
15845 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
15846 bfd_get_filename (reader
->abfd
));
15847 die
->dump (dwarf_die_debug
);
15853 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15855 The caller is responsible for filling in the extra attributes
15856 and updating (*DIEP)->num_attrs.
15857 Set DIEP to point to a newly allocated die with its information,
15858 except for its child, sibling, and parent fields. */
15860 static const gdb_byte
*
15861 read_full_die_1 (const struct die_reader_specs
*reader
,
15862 struct die_info
**diep
, const gdb_byte
*info_ptr
,
15863 int num_extra_attrs
, bool allow_reprocess
)
15865 unsigned int abbrev_number
, bytes_read
, i
;
15866 const struct abbrev_info
*abbrev
;
15867 struct die_info
*die
;
15868 struct dwarf2_cu
*cu
= reader
->cu
;
15869 bfd
*abfd
= reader
->abfd
;
15871 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
15872 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
15873 info_ptr
+= bytes_read
;
15874 if (!abbrev_number
)
15880 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
15882 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15884 bfd_get_filename (abfd
));
15886 die
= die_info::allocate (&cu
->comp_unit_obstack
,
15887 abbrev
->num_attrs
+ num_extra_attrs
);
15888 die
->sect_off
= sect_off
;
15889 die
->tag
= abbrev
->tag
;
15890 die
->abbrev
= abbrev_number
;
15891 die
->has_children
= abbrev
->has_children
;
15893 /* Make the result usable.
15894 The caller needs to update num_attrs after adding the extra
15896 die
->num_attrs
= abbrev
->num_attrs
;
15898 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
15899 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
15900 info_ptr
, allow_reprocess
);
15906 /* Read a die and all its attributes.
15907 Set DIEP to point to a newly allocated die with its information,
15908 except for its child, sibling, and parent fields. */
15910 static const gdb_byte
*
15911 read_toplevel_die (const struct die_reader_specs
*reader
,
15912 struct die_info
**diep
, const gdb_byte
*info_ptr
,
15913 gdb::array_view
<attribute
*> extra_attrs
)
15915 const gdb_byte
*result
;
15916 struct dwarf2_cu
*cu
= reader
->cu
;
15918 result
= read_full_die_1 (reader
, diep
, info_ptr
, extra_attrs
.size (),
15921 /* Copy in the extra attributes, if any. */
15922 attribute
*next
= &(*diep
)->attrs
[(*diep
)->num_attrs
];
15923 for (attribute
*extra
: extra_attrs
)
15926 struct attribute
*attr
= (*diep
)->attr (DW_AT_str_offsets_base
);
15927 if (attr
!= nullptr && attr
->form_is_unsigned ())
15928 cu
->str_offsets_base
= attr
->as_unsigned ();
15930 attr
= (*diep
)->attr (DW_AT_loclists_base
);
15931 if (attr
!= nullptr)
15932 cu
->loclist_base
= attr
->as_unsigned ();
15934 auto maybe_addr_base
= (*diep
)->addr_base ();
15935 if (maybe_addr_base
.has_value ())
15936 cu
->addr_base
= *maybe_addr_base
;
15938 attr
= (*diep
)->attr (DW_AT_rnglists_base
);
15939 if (attr
!= nullptr)
15940 cu
->rnglists_base
= attr
->as_unsigned ();
15942 for (int i
= 0; i
< (*diep
)->num_attrs
; ++i
)
15944 if ((*diep
)->attrs
[i
].form_requires_reprocessing ())
15945 read_attribute_reprocess (reader
, &(*diep
)->attrs
[i
], (*diep
)->tag
);
15948 (*diep
)->num_attrs
+= extra_attrs
.size ();
15950 if (dwarf_die_debug
)
15952 gdb_printf (gdb_stdlog
,
15953 "Read die from %s@0x%x of %s:\n",
15954 reader
->die_section
->get_name (),
15955 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
15956 bfd_get_filename (reader
->abfd
));
15957 (*diep
)->dump (dwarf_die_debug
);
15965 cooked_indexer::check_bounds (cutu_reader
*reader
)
15967 if (reader
->cu
->per_cu
->addresses_seen
)
15970 dwarf2_cu
*cu
= reader
->cu
;
15972 unrelocated_addr best_lowpc
= {}, best_highpc
= {};
15973 /* Possibly set the default values of LOWPC and HIGHPC from
15975 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
15976 enum pc_bounds_kind cu_bounds_kind
15977 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
15978 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
15979 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
15981 /* Store the contiguous range if it is not empty; it can be
15982 empty for CUs with no code. addrmap requires CORE_ADDR, so
15984 m_index_storage
->get_addrmap ()->set_empty ((CORE_ADDR
) best_lowpc
,
15985 (CORE_ADDR
) best_highpc
- 1,
15988 cu
->per_cu
->addresses_seen
= true;
15992 /* Helper function that returns true if TAG can have a linkage
15996 tag_can_have_linkage_name (enum dwarf_tag tag
)
16000 case DW_TAG_variable
:
16001 case DW_TAG_subprogram
:
16010 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
16011 dwarf2_per_objfile
*per_objfile
,
16012 sect_offset sect_off
, bool is_dwz
,
16015 /* Lookups for type unit references are always in the CU, and
16016 cross-CU references will crash. */
16017 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
16018 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
16021 dwarf2_per_cu_data
*per_cu
16022 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
16023 per_objfile
->per_bfd
);
16025 /* When scanning, we only want to visit a given CU a single time.
16026 Doing this check here avoids self-imports as well. */
16030 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
16033 if (per_cu
== m_per_cu
)
16036 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
16037 if (result
== nullptr)
16039 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
16040 m_index_storage
->get_abbrev_cache ());
16042 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
16044 std::unique_ptr
<cutu_reader
> copy
16045 (new cutu_reader (std::move (new_reader
)));
16046 result
= m_index_storage
->preserve (std::move (copy
));
16049 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
16053 check_bounds (result
);
16059 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
16060 cutu_reader
*reader
,
16061 const gdb_byte
*watermark_ptr
,
16062 const gdb_byte
*info_ptr
,
16063 const abbrev_info
*abbrev
,
16065 const char **linkage_name
,
16066 cooked_index_flag
*flags
,
16067 sect_offset
*sibling_offset
,
16068 const cooked_index_entry
**parent_entry
,
16069 parent_map::addr_type
*maybe_defer
,
16070 bool *is_enum_class
,
16071 bool for_specification
)
16073 bool origin_is_dwz
= false;
16074 bool is_declaration
= false;
16075 sect_offset origin_offset
{};
16077 std::optional
<unrelocated_addr
> low_pc
;
16078 std::optional
<unrelocated_addr
> high_pc
;
16079 bool high_pc_relative
= false;
16081 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
16084 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
16086 /* Store the data if it is of an attribute we want to keep in a
16087 partial symbol table. */
16091 switch (abbrev
->tag
)
16093 case DW_TAG_compile_unit
:
16094 case DW_TAG_partial_unit
:
16095 case DW_TAG_type_unit
:
16096 /* Compilation units have a DW_AT_name that is a filename, not
16097 a source language identifier. */
16101 if (*name
== nullptr)
16102 *name
= attr
.as_string ();
16107 case DW_AT_linkage_name
:
16108 case DW_AT_MIPS_linkage_name
:
16109 /* Note that both forms of linkage name might appear. We
16110 assume they will be the same, and we only store the last
16112 if (*linkage_name
== nullptr)
16113 *linkage_name
= attr
.as_string ();
16116 /* DWARF 4 has defined a dedicated DW_AT_main_subprogram
16117 attribute to indicate the starting function of the program... */
16118 case DW_AT_main_subprogram
:
16119 if (attr
.as_boolean ())
16123 /* ... however with older versions the DW_CC_program value of
16124 the DW_AT_calling_convention attribute was used instead as
16125 the only means available. We handle both variants then. */
16126 case DW_AT_calling_convention
:
16127 if (attr
.constant_value (DW_CC_normal
) == DW_CC_program
)
16131 case DW_AT_declaration
:
16132 is_declaration
= attr
.as_boolean ();
16135 case DW_AT_sibling
:
16136 if (sibling_offset
!= nullptr)
16137 *sibling_offset
= attr
.get_ref_die_offset ();
16140 case DW_AT_specification
:
16141 case DW_AT_abstract_origin
:
16142 case DW_AT_extension
:
16143 origin_offset
= attr
.get_ref_die_offset ();
16144 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
16147 case DW_AT_external
:
16148 if (attr
.as_boolean ())
16149 *flags
&= ~IS_STATIC
;
16152 case DW_AT_enum_class
:
16153 if (attr
.as_boolean ())
16154 *is_enum_class
= true;
16158 low_pc
= attr
.as_address ();
16161 case DW_AT_high_pc
:
16162 high_pc
= attr
.as_address ();
16163 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
16164 high_pc_relative
= true;
16167 case DW_AT_location
:
16168 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
16170 struct dwarf_block
*locdesc
= attr
.as_block ();
16172 if (decode_locdesc (locdesc
, reader
->cu
, &addr
)
16174 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
))
16176 low_pc
= (unrelocated_addr
) addr
;
16177 /* For variables, we don't want to try decoding the
16178 type just to find the size -- for gdb's purposes
16179 we only need the address of a variable. */
16180 high_pc
= (unrelocated_addr
) (addr
+ 1);
16181 high_pc_relative
= false;
16187 if (!scanning_per_cu
->addresses_seen
)
16189 /* Offset in the .debug_ranges or .debug_rnglist section
16190 (depending on DWARF version). */
16191 ULONGEST ranges_offset
= attr
.as_unsigned ();
16193 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
16194 want to add this value. */
16195 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
16197 unrelocated_addr lowpc
, highpc
;
16198 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
16199 m_index_storage
->get_addrmap (),
16200 scanning_per_cu
, abbrev
->tag
);
16206 /* We don't want to examine declarations, but if we found a
16207 declaration when handling DW_AT_specification or the like, then
16208 that is ok. Similarly, we allow an external variable without a
16209 location; those are resolved via minimal symbols. */
16210 if (is_declaration
&& !for_specification
16211 && !(abbrev
->tag
== DW_TAG_variable
&& (*flags
& IS_STATIC
) == 0))
16213 /* We always want to recurse into some types, but we may not
16214 want to treat them as definitions. */
16215 if ((abbrev
->tag
== DW_TAG_class_type
16216 || abbrev
->tag
== DW_TAG_structure_type
16217 || abbrev
->tag
== DW_TAG_union_type
)
16218 && abbrev
->has_children
)
16219 *flags
|= IS_TYPE_DECLARATION
;
16222 *linkage_name
= nullptr;
16226 else if ((*name
== nullptr
16227 || (*linkage_name
== nullptr
16228 && tag_can_have_linkage_name (abbrev
->tag
))
16229 || (*parent_entry
== nullptr && m_language
!= language_c
))
16230 && origin_offset
!= sect_offset (0))
16232 cutu_reader
*new_reader
16233 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
16234 origin_is_dwz
, false);
16235 if (new_reader
!= nullptr)
16237 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
16238 + to_underlying (origin_offset
));
16240 if (*parent_entry
== nullptr)
16242 /* We only perform immediate lookups of parents for DIEs
16243 from earlier in this CU. This avoids any problem
16244 with a NULL result when when we see a reference to a
16245 DIE in another CU that we may or may not have
16246 imported locally. */
16247 parent_map::addr_type addr
16248 = parent_map::form_addr (origin_offset
, origin_is_dwz
);
16249 if (new_reader
->cu
!= reader
->cu
|| new_info_ptr
> watermark_ptr
)
16250 *maybe_defer
= addr
;
16252 *parent_entry
= m_die_range_map
->find (addr
);
16255 unsigned int bytes_read
;
16256 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
16259 new_info_ptr
+= bytes_read
;
16261 if (new_reader
->cu
== reader
->cu
&& new_info_ptr
== watermark_ptr
)
16263 /* Self-reference, we're done. */
16266 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
,
16267 new_info_ptr
, new_abbrev
, name
, linkage_name
,
16268 flags
, nullptr, parent_entry
, maybe_defer
,
16269 is_enum_class
, true);
16273 if (!for_specification
)
16275 if (m_language
== language_ada
16276 && *linkage_name
== nullptr)
16277 *linkage_name
= *name
;
16279 if (!scanning_per_cu
->addresses_seen
16280 && low_pc
.has_value ()
16281 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
16282 || *low_pc
!= (unrelocated_addr
) 0)
16283 && high_pc
.has_value ())
16285 if (high_pc_relative
)
16286 high_pc
= (unrelocated_addr
) ((ULONGEST
) *high_pc
16287 + (ULONGEST
) *low_pc
);
16289 if (*high_pc
> *low_pc
)
16291 /* Need CORE_ADDR casts for addrmap. */
16292 m_index_storage
->get_addrmap ()->set_empty
16293 ((CORE_ADDR
) *low_pc
, (CORE_ADDR
) *high_pc
- 1,
16298 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
16299 *flags
&= ~IS_STATIC
;
16301 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
16302 *name
= "(anonymous namespace)";
16304 if (m_language
== language_cplus
16305 && (abbrev
->tag
== DW_TAG_class_type
16306 || abbrev
->tag
== DW_TAG_interface_type
16307 || abbrev
->tag
== DW_TAG_structure_type
16308 || abbrev
->tag
== DW_TAG_union_type
16309 || abbrev
->tag
== DW_TAG_enumeration_type
16310 || abbrev
->tag
== DW_TAG_enumerator
))
16311 *flags
&= ~IS_STATIC
;
16313 /* Keep in sync with new_symbol. */
16314 if (abbrev
->tag
== DW_TAG_subprogram
16315 && (m_language
== language_ada
16316 || m_language
== language_fortran
))
16317 *flags
&= ~IS_STATIC
;
16324 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
16325 const gdb_byte
*info_ptr
,
16326 const abbrev_info
*abbrev
)
16328 sect_offset sect_off
{};
16329 bool is_dwz
= false;
16331 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
16333 /* Note that we never need to reprocess attributes here. */
16335 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
16337 if (attr
.name
== DW_AT_import
)
16339 sect_off
= attr
.get_ref_die_offset ();
16340 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
16341 || reader
->cu
->per_cu
->is_dwz
);
16345 /* Did not find DW_AT_import. */
16346 if (sect_off
== sect_offset (0))
16349 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
16350 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
16352 if (new_reader
!= nullptr)
16354 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
16356 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
16363 cooked_indexer::recurse (cutu_reader
*reader
,
16364 const gdb_byte
*info_ptr
,
16365 const cooked_index_entry
*parent_entry
,
16368 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
16370 if (parent_entry
!= nullptr)
16372 /* Both start and end are inclusive, so use both "+ 1" and "- 1" to
16373 limit the range to the children of parent_entry. */
16374 parent_map::addr_type start
16375 = parent_map::form_addr (parent_entry
->die_offset
+ 1,
16376 reader
->cu
->per_cu
->is_dwz
);
16377 parent_map::addr_type end
16378 = parent_map::form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
16379 reader
->cu
->per_cu
->is_dwz
);
16380 m_die_range_map
->add_entry (start
, end
, parent_entry
);
16387 cooked_indexer::index_dies (cutu_reader
*reader
,
16388 const gdb_byte
*info_ptr
,
16389 const cooked_index_entry
*parent_entry
,
16392 const gdb_byte
*end_ptr
= (reader
->buffer
16393 + to_underlying (reader
->cu
->header
.sect_off
)
16394 + reader
->cu
->header
.get_length_with_initial ());
16396 while (info_ptr
< end_ptr
)
16398 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
16399 unsigned int bytes_read
;
16400 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
16402 info_ptr
+= bytes_read
;
16403 if (abbrev
== nullptr)
16406 if (abbrev
->tag
== DW_TAG_imported_unit
)
16408 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
16412 if (!abbrev
->interesting
)
16414 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
16415 if (fully
&& abbrev
->has_children
)
16416 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
16420 const char *name
= nullptr;
16421 const char *linkage_name
= nullptr;
16422 parent_map::addr_type defer
{};
16423 cooked_index_flag flags
= IS_STATIC
;
16424 sect_offset sibling
{};
16425 const cooked_index_entry
*this_parent_entry
= parent_entry
;
16426 bool is_enum_class
= false;
16428 /* The scope of a DW_TAG_entry_point cooked_index_entry is the one of
16429 its surrounding subroutine. */
16430 if (abbrev
->tag
== DW_TAG_entry_point
)
16431 this_parent_entry
= parent_entry
->get_parent ();
16432 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
16433 info_ptr
, abbrev
, &name
, &linkage_name
,
16434 &flags
, &sibling
, &this_parent_entry
,
16435 &defer
, &is_enum_class
, false);
16436 /* A DW_TAG_entry_point inherits its static/extern property from
16437 the enclosing subroutine. */
16438 if (abbrev
->tag
== DW_TAG_entry_point
)
16440 flags
&= ~IS_STATIC
;
16441 flags
|= parent_entry
->flags
& IS_STATIC
;
16443 /* If the parent is an enum, but not an enum class, then use the
16444 grandparent instead. */
16445 if (this_parent_entry
!= nullptr
16446 && this_parent_entry
->tag
== DW_TAG_enumeration_type
16448 this_parent_entry
= this_parent_entry
->get_parent ();
16450 if (abbrev
->tag
== DW_TAG_namespace
16451 && m_language
== language_cplus
16452 && strcmp (name
, "::") == 0)
16454 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
16455 generated bogus DW_TAG_namespace DIEs with a name of "::"
16456 for the global namespace. Work around this problem
16461 cooked_index_entry
*this_entry
= nullptr;
16462 if (name
!= nullptr)
16466 = m_index_storage
->add (this_die
, abbrev
->tag
,
16467 flags
| IS_PARENT_DEFERRED
, name
,
16471 = m_index_storage
->add (this_die
, abbrev
->tag
, flags
, name
,
16472 this_parent_entry
, m_per_cu
);
16475 if (linkage_name
!= nullptr)
16477 /* We only want this to be "main" if it has a linkage name
16478 but not an ordinary name. */
16479 if (name
!= nullptr)
16480 flags
= flags
& ~IS_MAIN
;
16481 /* Set the IS_LINKAGE on for everything except when functions
16482 have linkage name present but name is absent. */
16483 if (name
!= nullptr
16484 || (abbrev
->tag
!= DW_TAG_subprogram
16485 && abbrev
->tag
!= DW_TAG_inlined_subroutine
16486 && abbrev
->tag
!= DW_TAG_entry_point
))
16487 flags
= flags
| IS_LINKAGE
;
16488 m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
16489 linkage_name
, nullptr, m_per_cu
);
16492 if (abbrev
->has_children
)
16494 switch (abbrev
->tag
)
16496 case DW_TAG_class_type
:
16497 case DW_TAG_interface_type
:
16498 case DW_TAG_structure_type
:
16499 case DW_TAG_union_type
:
16500 if (m_language
!= language_c
&& this_entry
!= nullptr)
16502 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
16507 case DW_TAG_enumeration_type
:
16508 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
16511 case DW_TAG_module
:
16512 if (this_entry
== nullptr)
16515 case DW_TAG_namespace
:
16516 /* We don't check THIS_ENTRY for a namespace, to handle
16517 the ancient G++ workaround pointed out above. */
16518 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
16521 case DW_TAG_subprogram
:
16522 if ((m_language
== language_fortran
16523 || m_language
== language_ada
)
16524 && this_entry
!= nullptr)
16526 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
16532 if (sibling
!= sect_offset (0))
16534 const gdb_byte
*sibling_ptr
16535 = reader
->buffer
+ to_underlying (sibling
);
16537 if (sibling_ptr
< info_ptr
)
16538 complaint (_("DW_AT_sibling points backwards"));
16539 else if (sibling_ptr
> reader
->buffer_end
)
16540 reader
->die_section
->overflow_complaint ();
16542 info_ptr
= sibling_ptr
;
16545 info_ptr
= skip_children (reader
, info_ptr
);
16553 cooked_indexer::make_index (cutu_reader
*reader
)
16555 check_bounds (reader
);
16556 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
16557 if (!reader
->comp_unit_die
->has_children
)
16559 index_dies (reader
, reader
->info_ptr
, nullptr, false);
16562 dwarf2_per_cu_data
*
16563 cooked_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
16564 unrelocated_addr adjusted_pc
)
16566 cooked_index
*table
16567 = (gdb::checked_static_cast
<cooked_index
*>
16568 (per_bfd
->index_table
.get ()));
16569 return table
->lookup (adjusted_pc
);
16572 struct compunit_symtab
*
16573 cooked_index_functions::find_compunit_symtab_by_address
16574 (struct objfile
*objfile
, CORE_ADDR address
)
16576 if (objfile
->sect_index_data
== -1)
16579 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
16580 cooked_index
*table
= wait (objfile
, true);
16582 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
16583 dwarf2_per_cu_data
*per_cu
16584 = table
->lookup ((unrelocated_addr
) (address
- baseaddr
));
16585 if (per_cu
== nullptr)
16588 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
16592 cooked_index_functions::expand_symtabs_matching
16593 (struct objfile
*objfile
,
16594 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
16595 const lookup_name_info
*lookup_name
,
16596 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
16597 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
16598 block_search_flags search_flags
,
16599 domain_search_flags domain
)
16601 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
16603 cooked_index
*table
= wait (objfile
, true);
16605 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
16607 /* This invariant is documented in quick-functions.h. */
16608 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
16609 if (lookup_name
== nullptr)
16611 for (dwarf2_per_cu_data
*per_cu
16612 : all_units_range (per_objfile
->per_bfd
))
16616 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
16624 lookup_name_info lookup_name_without_params
16625 = lookup_name
->make_ignore_params ();
16626 bool completing
= lookup_name
->completion_mode ();
16628 /* Unique styles of language splitting. */
16629 static const enum language unique_styles
[] =
16631 /* No splitting is also a style. */
16633 /* This includes Rust. */
16635 /* This includes Go. */
16640 for (enum language lang
: unique_styles
)
16642 std::vector
<std::string_view
> name_vec
16643 = lookup_name_without_params
.split_name (lang
);
16644 std::string
last_name (name_vec
.back ());
16646 for (const cooked_index_entry
*entry
: table
->find (last_name
,
16651 /* No need to consider symbols from expanded CUs. */
16652 if (per_objfile
->symtab_set_p (entry
->per_cu
))
16655 /* If file-matching was done, we don't need to consider
16656 symbols from unmarked CUs. */
16657 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
16660 /* See if the symbol matches the type filter. */
16661 if (!entry
->matches (search_flags
)
16662 || !entry
->matches (domain
))
16665 /* We've found the base name of the symbol; now walk its
16666 parentage chain, ensuring that each component
16670 const cooked_index_entry
*parent
= entry
->get_parent ();
16671 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
16673 /* If we ran out of entries, or if this segment doesn't
16674 match, this did not match. */
16675 if (parent
== nullptr
16676 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
16677 name_vec
[i
- 1].length ()) != 0)
16683 parent
= parent
->get_parent ();
16689 /* Might have been looking for "a::b" and found
16691 if (symbol_matcher
== nullptr)
16693 symbol_name_match_type match_type
16694 = lookup_name_without_params
.match_type ();
16695 if ((match_type
== symbol_name_match_type::FULL
16696 || (lang
!= language_ada
16697 && match_type
== symbol_name_match_type::EXPRESSION
))
16698 && parent
!= nullptr)
16703 auto_obstack temp_storage
;
16704 const char *full_name
= entry
->full_name (&temp_storage
);
16705 if (!symbol_matcher (full_name
))
16709 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
16719 /* Start reading .debug_info using the indexer. */
16722 start_debug_info_reader (dwarf2_per_objfile
*per_objfile
)
16724 /* Set the index table early so that sharing works even while
16725 scanning; and then start the scanning. */
16726 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
16727 std::unique_ptr
<cooked_index_worker
> worker
16728 (new cooked_index_debug_info (per_objfile
));
16729 cooked_index
*idx
= new cooked_index (per_objfile
, std::move (worker
));
16730 per_bfd
->index_table
.reset (idx
);
16731 /* Don't start reading until after 'index_table' is set. This
16733 idx
->start_reading ();
16738 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
16739 contents from the given SECTION in the HEADER.
16741 HEADER_OFFSET is the offset of the header in the section. */
16743 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
16744 struct dwarf2_section_info
*section
,
16745 sect_offset header_offset
)
16747 unsigned int bytes_read
;
16748 bfd
*abfd
= section
->get_bfd_owner ();
16749 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
16751 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
16752 info_ptr
+= bytes_read
;
16754 header
->version
= read_2_bytes (abfd
, info_ptr
);
16757 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
16760 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
16763 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
16766 /* Return the DW_AT_loclists_base value for the CU. */
16768 lookup_loclist_base (struct dwarf2_cu
*cu
)
16770 /* For the .dwo unit, the loclist_base points to the first offset following
16771 the header. The header consists of the following entities-
16772 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
16774 2. version (2 bytes)
16775 3. address size (1 byte)
16776 4. segment selector size (1 byte)
16777 5. offset entry count (4 bytes)
16778 These sizes are derived as per the DWARFv5 standard. */
16779 if (cu
->dwo_unit
!= nullptr)
16781 if (cu
->header
.initial_length_size
== 4)
16782 return LOCLIST_HEADER_SIZE32
;
16783 return LOCLIST_HEADER_SIZE64
;
16785 return cu
->loclist_base
;
16788 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
16789 array of offsets in the .debug_loclists section. */
16792 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
16794 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
16795 struct objfile
*objfile
= per_objfile
->objfile
;
16796 bfd
*abfd
= objfile
->obfd
.get ();
16797 ULONGEST loclist_header_size
=
16798 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
16799 : LOCLIST_HEADER_SIZE64
);
16800 ULONGEST loclist_base
= lookup_loclist_base (cu
);
16802 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
16803 ULONGEST start_offset
=
16804 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
16806 /* Get loclists section. */
16807 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
16809 /* Read the loclists section content. */
16810 section
->read (objfile
);
16811 if (section
->buffer
== NULL
)
16812 error (_("DW_FORM_loclistx used without .debug_loclists "
16813 "section [in module %s]"), objfile_name (objfile
));
16815 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
16816 so if loclist_base is smaller than the header size, we have a problem. */
16817 if (loclist_base
< loclist_header_size
)
16818 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
16819 objfile_name (objfile
));
16821 /* Read the header of the loclists contribution. */
16822 struct loclists_rnglists_header header
;
16823 read_loclists_rnglists_header (&header
, section
,
16824 (sect_offset
) (loclist_base
- loclist_header_size
));
16826 /* Verify the loclist index is valid. */
16827 if (loclist_index
>= header
.offset_entry_count
)
16828 error (_("DW_FORM_loclistx pointing outside of "
16829 ".debug_loclists offset array [in module %s]"),
16830 objfile_name (objfile
));
16832 /* Validate that reading won't go beyond the end of the section. */
16833 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
16834 error (_("Reading DW_FORM_loclistx index beyond end of"
16835 ".debug_loclists section [in module %s]"),
16836 objfile_name (objfile
));
16838 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
16840 if (cu
->header
.offset_size
== 4)
16841 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
16843 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
16846 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
16847 array of offsets in the .debug_rnglists section. */
16850 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
16853 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
16854 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
16855 bfd
*abfd
= objfile
->obfd
.get ();
16856 ULONGEST rnglist_header_size
=
16857 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
16858 : RNGLIST_HEADER_SIZE64
);
16860 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
16861 .debug_rnglists.dwo section. The rnglists base given in the skeleton
16863 ULONGEST rnglist_base
=
16864 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
16866 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
16867 ULONGEST start_offset
=
16868 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
16870 /* Get rnglists section. */
16871 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
16873 /* Read the rnglists section content. */
16874 section
->read (objfile
);
16875 if (section
->buffer
== nullptr)
16876 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
16878 objfile_name (objfile
));
16880 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
16881 so if rnglist_base is smaller than the header size, we have a problem. */
16882 if (rnglist_base
< rnglist_header_size
)
16883 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
16884 objfile_name (objfile
));
16886 /* Read the header of the rnglists contribution. */
16887 struct loclists_rnglists_header header
;
16888 read_loclists_rnglists_header (&header
, section
,
16889 (sect_offset
) (rnglist_base
- rnglist_header_size
));
16891 /* Verify the rnglist index is valid. */
16892 if (rnglist_index
>= header
.offset_entry_count
)
16893 error (_("DW_FORM_rnglistx index pointing outside of "
16894 ".debug_rnglists offset array [in module %s]"),
16895 objfile_name (objfile
));
16897 /* Validate that reading won't go beyond the end of the section. */
16898 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
16899 error (_("Reading DW_FORM_rnglistx index beyond end of"
16900 ".debug_rnglists section [in module %s]"),
16901 objfile_name (objfile
));
16903 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
16905 if (cu
->header
.offset_size
== 4)
16906 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
16908 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
16911 /* Process the attributes that had to be skipped in the first round. These
16912 attributes are the ones that need str_offsets_base or addr_base attributes.
16913 They could not have been processed in the first round, because at the time
16914 the values of str_offsets_base or addr_base may not have been known. */
16916 read_attribute_reprocess (const struct die_reader_specs
*reader
,
16917 struct attribute
*attr
, dwarf_tag tag
)
16919 struct dwarf2_cu
*cu
= reader
->cu
;
16920 switch (attr
->form
)
16922 case DW_FORM_addrx
:
16923 case DW_FORM_GNU_addr_index
:
16924 attr
->set_address (read_addr_index (cu
,
16925 attr
->as_unsigned_reprocess ()));
16927 case DW_FORM_loclistx
:
16929 sect_offset loclists_sect_off
16930 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
16932 attr
->set_unsigned (to_underlying (loclists_sect_off
));
16935 case DW_FORM_rnglistx
:
16937 sect_offset rnglists_sect_off
16938 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
16940 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
16944 case DW_FORM_strx1
:
16945 case DW_FORM_strx2
:
16946 case DW_FORM_strx3
:
16947 case DW_FORM_strx4
:
16948 case DW_FORM_GNU_str_index
:
16950 unsigned int str_index
= attr
->as_unsigned_reprocess ();
16951 gdb_assert (!attr
->canonical_string_p ());
16952 if (reader
->dwo_file
!= NULL
)
16953 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
16956 attr
->set_string_noncanonical (read_stub_str_index (cu
,
16961 gdb_assert_not_reached ("Unexpected DWARF form.");
16965 /* Read an attribute value described by an attribute form. */
16967 static const gdb_byte
*
16968 read_attribute_value (const struct die_reader_specs
*reader
,
16969 struct attribute
*attr
, unsigned form
,
16970 LONGEST implicit_const
, const gdb_byte
*info_ptr
,
16971 bool allow_reprocess
)
16973 struct dwarf2_cu
*cu
= reader
->cu
;
16974 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
16975 struct objfile
*objfile
= per_objfile
->objfile
;
16976 bfd
*abfd
= reader
->abfd
;
16977 struct comp_unit_head
*cu_header
= &cu
->header
;
16978 unsigned int bytes_read
;
16979 struct dwarf_block
*blk
;
16981 attr
->form
= (enum dwarf_form
) form
;
16984 case DW_FORM_ref_addr
:
16985 if (cu_header
->version
== 2)
16986 attr
->set_unsigned ((ULONGEST
) cu_header
->read_address (abfd
, info_ptr
,
16989 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
16991 info_ptr
+= bytes_read
;
16993 case DW_FORM_GNU_ref_alt
:
16994 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
16996 info_ptr
+= bytes_read
;
17000 unrelocated_addr addr
= cu_header
->read_address (abfd
, info_ptr
,
17002 attr
->set_address (addr
);
17003 info_ptr
+= bytes_read
;
17006 case DW_FORM_block2
:
17007 blk
= dwarf_alloc_block (cu
);
17008 blk
->size
= read_2_bytes (abfd
, info_ptr
);
17010 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17011 info_ptr
+= blk
->size
;
17012 attr
->set_block (blk
);
17014 case DW_FORM_block4
:
17015 blk
= dwarf_alloc_block (cu
);
17016 blk
->size
= read_4_bytes (abfd
, info_ptr
);
17018 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17019 info_ptr
+= blk
->size
;
17020 attr
->set_block (blk
);
17022 case DW_FORM_data2
:
17023 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
17026 case DW_FORM_data4
:
17027 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
17030 case DW_FORM_data8
:
17031 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
17034 case DW_FORM_data16
:
17035 blk
= dwarf_alloc_block (cu
);
17037 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
17039 attr
->set_block (blk
);
17041 case DW_FORM_sec_offset
:
17042 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
17044 info_ptr
+= bytes_read
;
17046 case DW_FORM_loclistx
:
17048 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
17050 info_ptr
+= bytes_read
;
17051 if (allow_reprocess
)
17052 read_attribute_reprocess (reader
, attr
);
17055 case DW_FORM_string
:
17056 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
17058 info_ptr
+= bytes_read
;
17061 if (!cu
->per_cu
->is_dwz
)
17063 attr
->set_string_noncanonical
17064 (read_indirect_string (per_objfile
,
17065 abfd
, info_ptr
, cu_header
,
17067 info_ptr
+= bytes_read
;
17071 case DW_FORM_line_strp
:
17072 if (!cu
->per_cu
->is_dwz
)
17074 attr
->set_string_noncanonical
17075 (per_objfile
->read_line_string (info_ptr
, cu_header
,
17077 info_ptr
+= bytes_read
;
17081 case DW_FORM_GNU_strp_alt
:
17083 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
17084 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
17087 attr
->set_string_noncanonical
17088 (dwz
->read_string (objfile
, str_offset
));
17089 info_ptr
+= bytes_read
;
17092 case DW_FORM_exprloc
:
17093 case DW_FORM_block
:
17094 blk
= dwarf_alloc_block (cu
);
17095 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17096 info_ptr
+= bytes_read
;
17097 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17098 info_ptr
+= blk
->size
;
17099 attr
->set_block (blk
);
17101 case DW_FORM_block1
:
17102 blk
= dwarf_alloc_block (cu
);
17103 blk
->size
= read_1_byte (abfd
, info_ptr
);
17105 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
17106 info_ptr
+= blk
->size
;
17107 attr
->set_block (blk
);
17109 case DW_FORM_data1
:
17111 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
17114 case DW_FORM_flag_present
:
17115 attr
->set_unsigned (1);
17117 case DW_FORM_sdata
:
17118 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
17119 info_ptr
+= bytes_read
;
17121 case DW_FORM_rnglistx
:
17123 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
17125 info_ptr
+= bytes_read
;
17126 if (allow_reprocess
)
17127 read_attribute_reprocess (reader
, attr
);
17130 case DW_FORM_udata
:
17131 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
17132 info_ptr
+= bytes_read
;
17135 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17136 + read_1_byte (abfd
, info_ptr
)));
17140 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17141 + read_2_bytes (abfd
, info_ptr
)));
17145 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17146 + read_4_bytes (abfd
, info_ptr
)));
17150 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17151 + read_8_bytes (abfd
, info_ptr
)));
17154 case DW_FORM_ref_sig8
:
17155 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
17158 case DW_FORM_ref_udata
:
17159 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
17160 + read_unsigned_leb128 (abfd
, info_ptr
,
17162 info_ptr
+= bytes_read
;
17164 case DW_FORM_indirect
:
17165 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17166 info_ptr
+= bytes_read
;
17167 if (form
== DW_FORM_implicit_const
)
17169 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
17170 info_ptr
+= bytes_read
;
17172 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
17173 info_ptr
, allow_reprocess
);
17175 case DW_FORM_implicit_const
:
17176 attr
->set_signed (implicit_const
);
17178 case DW_FORM_addrx
:
17179 case DW_FORM_GNU_addr_index
:
17180 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
17182 info_ptr
+= bytes_read
;
17183 if (allow_reprocess
)
17184 read_attribute_reprocess (reader
, attr
);
17187 case DW_FORM_strx1
:
17188 case DW_FORM_strx2
:
17189 case DW_FORM_strx3
:
17190 case DW_FORM_strx4
:
17191 case DW_FORM_GNU_str_index
:
17193 ULONGEST str_index
;
17194 if (form
== DW_FORM_strx1
)
17196 str_index
= read_1_byte (abfd
, info_ptr
);
17199 else if (form
== DW_FORM_strx2
)
17201 str_index
= read_2_bytes (abfd
, info_ptr
);
17204 else if (form
== DW_FORM_strx3
)
17206 str_index
= read_3_bytes (abfd
, info_ptr
);
17209 else if (form
== DW_FORM_strx4
)
17211 str_index
= read_4_bytes (abfd
, info_ptr
);
17216 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17217 info_ptr
+= bytes_read
;
17219 attr
->set_unsigned_reprocess (str_index
);
17220 if (allow_reprocess
)
17221 read_attribute_reprocess (reader
, attr
);
17225 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
17226 dwarf_form_name (form
),
17227 bfd_get_filename (abfd
));
17231 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
17232 attr
->form
= DW_FORM_GNU_ref_alt
;
17234 /* We have seen instances where the compiler tried to emit a byte
17235 size attribute of -1 which ended up being encoded as an unsigned
17236 0xffffffff. Although 0xffffffff is technically a valid size value,
17237 an object of this size seems pretty unlikely so we can relatively
17238 safely treat these cases as if the size attribute was invalid and
17239 treat them as zero by default. */
17240 if (attr
->name
== DW_AT_byte_size
17241 && form
== DW_FORM_data4
17242 && attr
->as_unsigned () >= 0xffffffff)
17245 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
17246 hex_string (attr
->as_unsigned ()));
17247 attr
->set_unsigned (0);
17253 /* Read an attribute described by an abbreviated attribute. */
17255 static const gdb_byte
*
17256 read_attribute (const struct die_reader_specs
*reader
,
17257 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
17258 const gdb_byte
*info_ptr
,
17259 bool allow_reprocess
)
17261 attr
->name
= abbrev
->name
;
17262 attr
->string_is_canonical
= 0;
17263 return read_attribute_value (reader
, attr
, abbrev
->form
,
17264 abbrev
->implicit_const
, info_ptr
,
17271 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
17272 LONGEST str_offset
)
17274 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
17275 str_offset
, "DW_FORM_strp");
17278 /* Return pointer to string at .debug_str offset as read from BUF.
17279 BUF is assumed to be in a compilation unit described by CU_HEADER.
17280 Return *BYTES_READ_PTR count of bytes read from BUF. */
17282 static const char *
17283 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
17284 const gdb_byte
*buf
,
17285 const struct comp_unit_head
*cu_header
,
17286 unsigned int *bytes_read_ptr
)
17288 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
17290 return read_indirect_string_at_offset (per_objfile
, str_offset
);
17296 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
17297 unsigned int offset_size
)
17299 bfd
*abfd
= objfile
->obfd
.get ();
17300 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
17302 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
17308 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
17309 const struct comp_unit_head
*cu_header
,
17310 unsigned int *bytes_read_ptr
)
17312 bfd
*abfd
= objfile
->obfd
.get ();
17313 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
17315 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
17318 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17319 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
17320 ADDR_SIZE is the size of addresses from the CU header. */
17322 static unrelocated_addr
17323 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
17324 std::optional
<ULONGEST
> addr_base
, int addr_size
)
17326 struct objfile
*objfile
= per_objfile
->objfile
;
17327 bfd
*abfd
= objfile
->obfd
.get ();
17328 const gdb_byte
*info_ptr
;
17329 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
17331 per_objfile
->per_bfd
->addr
.read (objfile
);
17332 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
17333 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17334 objfile_name (objfile
));
17335 if (addr_base_or_zero
+ addr_index
* addr_size
17336 >= per_objfile
->per_bfd
->addr
.size
)
17337 error (_("DW_FORM_addr_index pointing outside of "
17338 ".debug_addr section [in module %s]"),
17339 objfile_name (objfile
));
17340 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
17341 + addr_index
* addr_size
);
17342 if (addr_size
== 4)
17343 return (unrelocated_addr
) bfd_get_32 (abfd
, info_ptr
);
17345 return (unrelocated_addr
) bfd_get_64 (abfd
, info_ptr
);
17348 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17350 static unrelocated_addr
17351 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
17353 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
17354 cu
->addr_base
, cu
->header
.addr_size
);
17357 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17359 static unrelocated_addr
17360 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
17361 unsigned int *bytes_read
)
17363 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
.get ();
17364 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
17366 return read_addr_index (cu
, addr_index
);
17372 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
17373 dwarf2_per_objfile
*per_objfile
,
17374 unsigned int addr_index
)
17376 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
17377 std::optional
<ULONGEST
> addr_base
;
17380 /* We need addr_base and addr_size.
17381 If we don't have PER_CU->cu, we have to get it.
17382 Nasty, but the alternative is storing the needed info in PER_CU,
17383 which at this point doesn't seem justified: it's not clear how frequently
17384 it would get used and it would increase the size of every PER_CU.
17385 Entry points like dwarf2_per_cu_addr_size do a similar thing
17386 so we're not in uncharted territory here.
17387 Alas we need to be a bit more complicated as addr_base is contained
17390 We don't need to read the entire CU(/TU).
17391 We just need the header and top level die.
17393 IWBN to use the aging mechanism to let us lazily later discard the CU.
17394 For now we skip this optimization. */
17398 addr_base
= cu
->addr_base
;
17399 addr_size
= cu
->header
.addr_size
;
17403 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
17404 addr_base
= reader
.cu
->addr_base
;
17405 addr_size
= reader
.cu
->header
.addr_size
;
17408 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
17411 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
17412 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
17415 static const char *
17416 read_str_index (struct dwarf2_cu
*cu
,
17417 struct dwarf2_section_info
*str_section
,
17418 struct dwarf2_section_info
*str_offsets_section
,
17419 ULONGEST str_offsets_base
, ULONGEST str_index
,
17420 unsigned offset_size
)
17422 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17423 struct objfile
*objfile
= per_objfile
->objfile
;
17424 const char *objf_name
= objfile_name (objfile
);
17425 bfd
*abfd
= objfile
->obfd
.get ();
17426 const gdb_byte
*info_ptr
;
17427 ULONGEST str_offset
;
17428 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
17430 str_section
->read (objfile
);
17431 str_offsets_section
->read (objfile
);
17432 if (str_section
->buffer
== NULL
)
17433 error (_("%s used without %s section"
17434 " in CU at offset %s [in module %s]"),
17435 form_name
, str_section
->get_name (),
17436 sect_offset_str (cu
->header
.sect_off
), objf_name
);
17437 if (str_offsets_section
->buffer
== NULL
)
17438 error (_("%s used without %s section"
17439 " in CU at offset %s [in module %s]"),
17440 form_name
, str_section
->get_name (),
17441 sect_offset_str (cu
->header
.sect_off
), objf_name
);
17442 info_ptr
= (str_offsets_section
->buffer
17444 + str_index
* offset_size
);
17445 if (offset_size
== 4)
17446 str_offset
= bfd_get_32 (abfd
, info_ptr
);
17448 str_offset
= bfd_get_64 (abfd
, info_ptr
);
17449 if (str_offset
>= str_section
->size
)
17450 error (_("Offset from %s pointing outside of"
17451 " %s section in CU at offset %s [in module %s]"),
17452 form_name
, str_section
->get_name (),
17453 sect_offset_str (cu
->header
.sect_off
), objf_name
);
17454 return (const char *) (str_section
->buffer
+ str_offset
);
17457 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
17459 static const char *
17460 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
17462 unsigned offset_size
;
17463 ULONGEST str_offsets_base
;
17464 if (reader
->cu
->header
.version
>= 5)
17466 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
17467 so assume the .debug_str_offsets section is DWARF5 as well, and
17468 parse the header. FIXME: Parse the header only once. */
17469 unsigned int bytes_read
= 0;
17470 bfd
*abfd
= reader
->dwo_file
->sections
.str_offsets
.get_bfd_owner ();
17471 const gdb_byte
*p
= reader
->dwo_file
->sections
.str_offsets
.buffer
;
17473 /* Header: Initial length. */
17474 read_initial_length (abfd
, p
+ bytes_read
, &bytes_read
);
17476 /* Determine offset_size based on the .debug_str_offsets header. */
17477 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
17478 offset_size
= dwarf5_is_dwarf64
? 8 : 4;
17480 /* Header: Version. */
17481 unsigned version
= read_2_bytes (abfd
, p
+ bytes_read
);
17486 /* We'd like one warning here about ignoring the section, but
17487 because we parse the header more than once (see FIXME above)
17488 we'd have many warnings, so use a complaint instead, which at
17489 least has a limit. */
17490 complaint (_("Section .debug_str_offsets in %s has unsupported"
17491 " version %d, use empty string."),
17492 reader
->dwo_file
->dwo_name
.c_str (), version
);
17496 /* Header: Padding. */
17499 str_offsets_base
= bytes_read
;
17503 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
17504 section, assume the .debug_str_offsets section is pre-DWARF5 as
17505 well, which doesn't have a header. */
17506 str_offsets_base
= 0;
17508 /* Determine offset_size based on the .debug_info header. */
17509 offset_size
= reader
->cu
->header
.offset_size
;
17512 return read_str_index (reader
->cu
,
17513 &reader
->dwo_file
->sections
.str
,
17514 &reader
->dwo_file
->sections
.str_offsets
,
17515 str_offsets_base
, str_index
, offset_size
);
17518 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
17520 static const char *
17521 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
17523 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17524 const char *objf_name
= objfile_name (objfile
);
17525 static const char form_name
[] = "DW_FORM_GNU_str_index";
17526 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
17528 if (!cu
->str_offsets_base
.has_value ())
17529 error (_("%s used in Fission stub without %s"
17530 " in CU at offset 0x%lx [in module %s]"),
17531 form_name
, str_offsets_attr_name
,
17532 (long) cu
->header
.offset_size
, objf_name
);
17534 return read_str_index (cu
,
17535 &cu
->per_objfile
->per_bfd
->str
,
17536 &cu
->per_objfile
->per_bfd
->str_offsets
,
17537 *cu
->str_offsets_base
, str_index
,
17538 cu
->header
.offset_size
);
17541 /* Return the length of an LEB128 number in BUF. */
17544 leb128_size (const gdb_byte
*buf
)
17546 const gdb_byte
*begin
= buf
;
17552 if ((byte
& 128) == 0)
17553 return buf
- begin
;
17557 /* Converts DWARF language names to GDB language names. */
17560 dwarf_lang_to_enum_language (unsigned int lang
)
17562 enum language language
;
17571 language
= language_c
;
17574 case DW_LANG_C_plus_plus
:
17575 case DW_LANG_C_plus_plus_11
:
17576 case DW_LANG_C_plus_plus_14
:
17577 language
= language_cplus
;
17580 language
= language_d
;
17582 case DW_LANG_Fortran77
:
17583 case DW_LANG_Fortran90
:
17584 case DW_LANG_Fortran95
:
17585 case DW_LANG_Fortran03
:
17586 case DW_LANG_Fortran08
:
17587 language
= language_fortran
;
17590 language
= language_go
;
17592 case DW_LANG_Mips_Assembler
:
17593 language
= language_asm
;
17595 case DW_LANG_Ada83
:
17596 case DW_LANG_Ada95
:
17597 language
= language_ada
;
17599 case DW_LANG_Modula2
:
17600 language
= language_m2
;
17602 case DW_LANG_Pascal83
:
17603 language
= language_pascal
;
17606 language
= language_objc
;
17609 case DW_LANG_Rust_old
:
17610 language
= language_rust
;
17612 case DW_LANG_OpenCL
:
17613 language
= language_opencl
;
17615 case DW_LANG_Cobol74
:
17616 case DW_LANG_Cobol85
:
17618 language
= language_minimal
;
17625 /* Return the named attribute or NULL if not there. */
17627 static struct attribute
*
17628 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
17633 struct attribute
*spec
= NULL
;
17635 for (i
= 0; i
< die
->num_attrs
; ++i
)
17637 if (die
->attrs
[i
].name
== name
)
17638 return &die
->attrs
[i
];
17639 if (die
->attrs
[i
].name
== DW_AT_specification
17640 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
17641 spec
= &die
->attrs
[i
];
17647 struct die_info
*prev_die
= die
;
17648 die
= follow_die_ref (die
, spec
, &cu
);
17649 if (die
== prev_die
)
17650 /* Self-reference, we're done. */
17657 /* Return the string associated with a string-typed attribute, or NULL if it
17658 is either not found or is of an incorrect type. */
17660 static const char *
17661 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
17663 struct attribute
*attr
;
17664 const char *str
= NULL
;
17666 attr
= dwarf2_attr (die
, name
, cu
);
17670 str
= attr
->as_string ();
17671 if (str
== nullptr)
17672 complaint (_("string type expected for attribute %s for "
17673 "DIE at %s in module %s"),
17674 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
17675 objfile_name (cu
->per_objfile
->objfile
));
17681 /* Return the dwo name or NULL if not present. If present, it is in either
17682 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
17683 static const char *
17684 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17686 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
17687 if (dwo_name
== nullptr)
17688 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
17692 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17693 and holds a non-zero value. This function should only be used for
17694 DW_FORM_flag or DW_FORM_flag_present attributes. */
17697 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
17699 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
17701 return attr
!= nullptr && attr
->as_boolean ();
17705 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
17707 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17708 which value is non-zero. However, we have to be careful with
17709 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17710 (via dwarf2_flag_true_p) follows this attribute. So we may
17711 end up accidently finding a declaration attribute that belongs
17712 to a different DIE referenced by the specification attribute,
17713 even though the given DIE does not have a declaration attribute. */
17714 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
17715 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
17718 /* Return the die giving the specification for DIE, if there is
17719 one. *SPEC_CU is the CU containing DIE on input, and the CU
17720 containing the return value on output. If there is no
17721 specification, but there is an abstract origin, that is
17724 static struct die_info
*
17725 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
17727 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
17730 if (spec_attr
== NULL
)
17731 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
17733 if (spec_attr
== NULL
)
17736 return follow_die_ref (die
, spec_attr
, spec_cu
);
17739 /* A convenience function to find the proper .debug_line section for a CU. */
17741 static struct dwarf2_section_info
*
17742 get_debug_line_section (struct dwarf2_cu
*cu
)
17744 struct dwarf2_section_info
*section
;
17745 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17747 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17749 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
17750 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
17751 else if (cu
->per_cu
->is_dwz
)
17753 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
17755 section
= &dwz
->line
;
17758 section
= &per_objfile
->per_bfd
->line
;
17763 /* Read the statement program header starting at OFFSET in
17764 .debug_line, or .debug_line.dwo. Return a pointer
17765 to a struct line_header, allocated using xmalloc.
17766 Returns NULL if there is a problem reading the header, e.g., if it
17767 has a version we don't understand.
17769 NOTE: the strings in the include directory and file name tables of
17770 the returned object point into the dwarf line section buffer,
17771 and must not be freed. */
17773 static line_header_up
17774 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
,
17775 const char *comp_dir
)
17777 struct dwarf2_section_info
*section
;
17778 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17780 section
= get_debug_line_section (cu
);
17781 section
->read (per_objfile
->objfile
);
17782 if (section
->buffer
== NULL
)
17784 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
17785 complaint (_("missing .debug_line.dwo section"));
17787 complaint (_("missing .debug_line section"));
17791 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
17792 per_objfile
, section
, &cu
->header
,
17796 /* Subroutine of dwarf_decode_lines to simplify it.
17797 Return the file name for the given file_entry.
17798 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
17799 If space for the result is malloc'd, *NAME_HOLDER will be set.
17800 Returns NULL if FILE_INDEX should be ignored, i.e., it is
17801 equivalent to CU_INFO. */
17803 static const char *
17804 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
17805 const file_and_directory
&cu_info
,
17806 std::string
&name_holder
)
17808 const char *include_name
= fe
.name
;
17809 const char *include_name_to_compare
= include_name
;
17811 const char *dir_name
= fe
.include_dir (lh
);
17813 std::string hold_compare
;
17814 if (!IS_ABSOLUTE_PATH (include_name
)
17815 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
17817 /* Avoid creating a duplicate name for CU_INFO.
17818 We do this by comparing INCLUDE_NAME and CU_INFO.
17819 Before we do the comparison, however, we need to account
17820 for DIR_NAME and COMP_DIR.
17821 First prepend dir_name (if non-NULL). If we still don't
17822 have an absolute path prepend comp_dir (if non-NULL).
17823 However, the directory we record in the include-file's
17824 psymtab does not contain COMP_DIR (to match the
17825 corresponding symtab(s)).
17830 bash$ gcc -g ./hello.c
17831 include_name = "hello.c"
17833 DW_AT_comp_dir = comp_dir = "/tmp"
17834 DW_AT_name = "./hello.c"
17838 if (dir_name
!= NULL
)
17840 name_holder
= path_join (dir_name
, include_name
);
17841 include_name
= name_holder
.c_str ();
17842 include_name_to_compare
= include_name
;
17844 if (!IS_ABSOLUTE_PATH (include_name
)
17845 && cu_info
.get_comp_dir () != nullptr)
17847 hold_compare
= path_join (cu_info
.get_comp_dir (), include_name
);
17848 include_name_to_compare
= hold_compare
.c_str ();
17852 std::string copied_name
;
17853 const char *cu_filename
= cu_info
.get_name ();
17854 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
17856 copied_name
= path_join (cu_info
.get_comp_dir (), cu_filename
);
17857 cu_filename
= copied_name
.c_str ();
17860 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
17862 return include_name
;
17865 /* State machine to track the state of the line number program. */
17867 class lnp_state_machine
17870 /* Initialize a machine state for the start of a line number
17872 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
17874 file_entry
*current_file ()
17876 /* lh->file_names is 0-based, but the file name numbers in the
17877 statement program are 1-based. */
17878 return m_line_header
->file_name_at (m_file
);
17881 /* Record the line in the state machine. END_SEQUENCE is true if
17882 we're processing the end of a sequence. */
17883 void record_line (bool end_sequence
);
17885 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
17886 nop-out rest of the lines in this sequence. */
17887 void check_line_address (struct dwarf2_cu
*cu
,
17888 const gdb_byte
*line_ptr
,
17889 unrelocated_addr unrelocated_lowpc
,
17890 unrelocated_addr address
);
17892 void handle_set_discriminator (unsigned int discriminator
)
17894 m_discriminator
= discriminator
;
17895 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
17898 /* Handle DW_LNE_set_address. */
17899 void handle_set_address (unrelocated_addr address
)
17903 = (unrelocated_addr
) gdbarch_adjust_dwarf2_line (m_gdbarch
,
17904 (CORE_ADDR
) address
,
17908 /* Handle DW_LNS_advance_pc. */
17909 void handle_advance_pc (CORE_ADDR adjust
);
17911 /* Handle a special opcode. */
17912 void handle_special_opcode (unsigned char op_code
);
17914 /* Handle DW_LNS_advance_line. */
17915 void handle_advance_line (int line_delta
)
17917 advance_line (line_delta
);
17920 /* Handle DW_LNS_set_file. */
17921 void handle_set_file (file_name_index file
);
17923 /* Handle DW_LNS_negate_stmt. */
17924 void handle_negate_stmt ()
17926 m_flags
^= LEF_IS_STMT
;
17929 /* Handle DW_LNS_const_add_pc. */
17930 void handle_const_add_pc ();
17932 /* Handle DW_LNS_fixed_advance_pc. */
17933 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
17935 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
17936 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
17940 /* Handle DW_LNS_copy. */
17941 void handle_copy ()
17943 record_line (false);
17944 m_discriminator
= 0;
17945 m_flags
&= ~LEF_PROLOGUE_END
;
17946 m_flags
&= ~LEF_EPILOGUE_BEGIN
;
17949 /* Handle DW_LNE_end_sequence. */
17950 void handle_end_sequence ()
17952 m_currently_recording_lines
= true;
17955 /* Handle DW_LNS_set_prologue_end. */
17956 void handle_set_prologue_end ()
17958 m_flags
|= LEF_PROLOGUE_END
;
17961 void handle_set_epilogue_begin ()
17963 m_flags
|= LEF_EPILOGUE_BEGIN
;
17967 /* Advance the line by LINE_DELTA. */
17968 void advance_line (int line_delta
)
17970 m_line
+= line_delta
;
17972 if (line_delta
!= 0)
17973 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
17976 struct dwarf2_cu
*m_cu
;
17978 gdbarch
*m_gdbarch
;
17980 /* The line number header. */
17981 line_header
*m_line_header
;
17983 /* These are part of the standard DWARF line number state machine,
17984 and initialized according to the DWARF spec. */
17986 unsigned char m_op_index
= 0;
17987 /* The line table index of the current file. */
17988 file_name_index m_file
= 1;
17989 unsigned int m_line
= 1;
17991 /* These are initialized in the constructor. */
17993 unrelocated_addr m_address
;
17994 linetable_entry_flags m_flags
;
17995 unsigned int m_discriminator
= 0;
17997 /* Additional bits of state we need to track. */
17999 /* The last file that we called dwarf2_start_subfile for.
18000 This is only used for TLLs. */
18001 unsigned int m_last_file
= 0;
18002 /* The last file a line number was recorded for. */
18003 struct subfile
*m_last_subfile
= NULL
;
18005 /* The address of the last line entry. */
18006 unrelocated_addr m_last_address
;
18008 /* Set to true when a previous line at the same address (using
18009 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
18010 when a line entry at a new address (m_address different to
18011 m_last_address) is processed. */
18012 bool m_stmt_at_address
= false;
18014 /* When true, record the lines we decode. */
18015 bool m_currently_recording_lines
= true;
18017 /* The last line number that was recorded, used to coalesce
18018 consecutive entries for the same line. This can happen, for
18019 example, when discriminators are present. PR 17276. */
18020 unsigned int m_last_line
= 0;
18021 bool m_line_has_non_zero_discriminator
= false;
18025 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
18027 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
18028 / m_line_header
->maximum_ops_per_instruction
)
18029 * m_line_header
->minimum_instruction_length
);
18030 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18031 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18032 m_op_index
= ((m_op_index
+ adjust
)
18033 % m_line_header
->maximum_ops_per_instruction
);
18037 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
18039 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
18040 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
18041 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
18042 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
18043 / m_line_header
->maximum_ops_per_instruction
)
18044 * m_line_header
->minimum_instruction_length
);
18045 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18046 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18047 m_op_index
= ((m_op_index
+ adj_opcode_d
)
18048 % m_line_header
->maximum_ops_per_instruction
);
18050 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
18051 advance_line (line_delta
);
18052 record_line (false);
18053 m_discriminator
= 0;
18054 m_flags
&= ~LEF_PROLOGUE_END
;
18055 m_flags
&= ~LEF_EPILOGUE_BEGIN
;
18059 lnp_state_machine::handle_set_file (file_name_index file
)
18063 const file_entry
*fe
= current_file ();
18065 dwarf2_debug_line_missing_file_complaint ();
18068 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
18069 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
18070 dwarf2_start_subfile (m_cu
, *fe
, *m_line_header
);
18075 lnp_state_machine::handle_const_add_pc ()
18078 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
18081 = (((m_op_index
+ adjust
)
18082 / m_line_header
->maximum_ops_per_instruction
)
18083 * m_line_header
->minimum_instruction_length
);
18085 addr_adj
= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
18086 m_address
= (unrelocated_addr
) ((CORE_ADDR
) m_address
+ addr_adj
);
18087 m_op_index
= ((m_op_index
+ adjust
)
18088 % m_line_header
->maximum_ops_per_instruction
);
18091 /* Return non-zero if we should add LINE to the line number table.
18092 LINE is the line to add, LAST_LINE is the last line that was added,
18093 LAST_SUBFILE is the subfile for LAST_LINE.
18094 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18095 had a non-zero discriminator.
18097 We have to be careful in the presence of discriminators.
18098 E.g., for this line:
18100 for (i = 0; i < 100000; i++);
18102 clang can emit four line number entries for that one line,
18103 each with a different discriminator.
18104 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18106 However, we want gdb to coalesce all four entries into one.
18107 Otherwise the user could stepi into the middle of the line and
18108 gdb would get confused about whether the pc really was in the
18109 middle of the line.
18111 Things are further complicated by the fact that two consecutive
18112 line number entries for the same line is a heuristic used by gcc
18113 to denote the end of the prologue. So we can't just discard duplicate
18114 entries, we have to be selective about it. The heuristic we use is
18115 that we only collapse consecutive entries for the same line if at least
18116 one of those entries has a non-zero discriminator. PR 17276.
18118 Note: Addresses in the line number state machine can never go backwards
18119 within one sequence, thus this coalescing is ok. */
18122 dwarf_record_line_p (struct dwarf2_cu
*cu
,
18123 unsigned int line
, unsigned int last_line
,
18124 int line_has_non_zero_discriminator
,
18125 struct subfile
*last_subfile
)
18127 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
18129 if (line
!= last_line
)
18131 /* Same line for the same file that we've seen already.
18132 As a last check, for pr 17276, only record the line if the line
18133 has never had a non-zero discriminator. */
18134 if (!line_has_non_zero_discriminator
)
18139 /* Use the CU's builder to record line number LINE beginning at
18140 address ADDRESS in the line table of subfile SUBFILE. */
18143 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
18144 unsigned int line
, unrelocated_addr address
,
18145 linetable_entry_flags flags
,
18146 struct dwarf2_cu
*cu
)
18148 unrelocated_addr addr
18149 = unrelocated_addr (gdbarch_addr_bits_remove (gdbarch
,
18150 (CORE_ADDR
) address
));
18152 if (dwarf_line_debug
)
18154 gdb_printf (gdb_stdlog
,
18155 "Recording line %u, file %s, address %s\n",
18156 line
, lbasename (subfile
->name
.c_str ()),
18157 paddress (gdbarch
, (CORE_ADDR
) address
));
18161 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
18164 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18165 Mark the end of a set of line number records.
18166 The arguments are the same as for dwarf_record_line_1.
18167 If SUBFILE is NULL the request is ignored. */
18170 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
18171 unrelocated_addr address
, struct dwarf2_cu
*cu
)
18173 if (subfile
== NULL
)
18176 if (dwarf_line_debug
)
18178 gdb_printf (gdb_stdlog
,
18179 "Finishing current line, file %s, address %s\n",
18180 lbasename (subfile
->name
.c_str ()),
18181 paddress (gdbarch
, (CORE_ADDR
) address
));
18184 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
18188 lnp_state_machine::record_line (bool end_sequence
)
18190 if (dwarf_line_debug
)
18192 gdb_printf (gdb_stdlog
,
18193 "Processing actual line %u: file %u,"
18194 " address %s, is_stmt %u, prologue_end %u,"
18195 " epilogue_begin %u, discrim %u%s\n",
18197 paddress (m_gdbarch
, (CORE_ADDR
) m_address
),
18198 (m_flags
& LEF_IS_STMT
) != 0,
18199 (m_flags
& LEF_PROLOGUE_END
) != 0,
18200 (m_flags
& LEF_EPILOGUE_BEGIN
) != 0,
18202 (end_sequence
? "\t(end sequence)" : ""));
18205 file_entry
*fe
= current_file ();
18208 dwarf2_debug_line_missing_file_complaint ();
18209 /* For now we ignore lines not starting on an instruction boundary.
18210 But not when processing end_sequence for compatibility with the
18211 previous version of the code. */
18212 else if (m_op_index
== 0 || end_sequence
)
18214 /* When we switch files we insert an end maker in the first file,
18215 switch to the second file and add a new line entry. The
18216 problem is that the end marker inserted in the first file will
18217 discard any previous line entries at the same address. If the
18218 line entries in the first file are marked as is-stmt, while
18219 the new line in the second file is non-stmt, then this means
18220 the end marker will discard is-stmt lines so we can have a
18221 non-stmt line. This means that there are less addresses at
18222 which the user can insert a breakpoint.
18224 To improve this we track the last address in m_last_address,
18225 and whether we have seen an is-stmt at this address. Then
18226 when switching files, if we have seen a stmt at the current
18227 address, and we are switching to create a non-stmt line, then
18228 discard the new line. */
18230 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
18231 bool ignore_this_line
18232 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
18233 && ((m_flags
& LEF_IS_STMT
) == 0)
18234 && m_stmt_at_address
)
18235 || (!end_sequence
&& m_line
== 0));
18237 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
18239 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
18240 m_currently_recording_lines
? m_cu
: nullptr);
18243 if (!end_sequence
&& !ignore_this_line
)
18245 linetable_entry_flags lte_flags
= m_flags
;
18246 if (producer_is_codewarrior (m_cu
))
18247 lte_flags
|= LEF_IS_STMT
;
18249 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
18250 m_line_has_non_zero_discriminator
,
18253 buildsym_compunit
*builder
= m_cu
->get_builder ();
18254 dwarf_record_line_1 (m_gdbarch
,
18255 builder
->get_current_subfile (),
18256 m_line
, m_address
, lte_flags
,
18257 m_currently_recording_lines
? m_cu
: nullptr);
18259 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
18260 m_last_line
= m_line
;
18264 /* Track whether we have seen any IS_STMT true at m_address in case we
18265 have multiple line table entries all at m_address. */
18266 if (m_last_address
!= m_address
)
18268 m_stmt_at_address
= false;
18269 m_last_address
= m_address
;
18271 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
18274 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
18278 m_line_header (lh
),
18279 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
18280 if there was a line entry for it so that the backend has a
18281 chance to adjust it and also record it in case it needs it.
18282 This is currently used by MIPS code,
18283 cf. `mips_adjust_dwarf2_line'. */
18284 m_address ((unrelocated_addr
) gdbarch_adjust_dwarf2_line (arch
, 0, 0)),
18285 m_flags (lh
->default_is_stmt
? LEF_IS_STMT
: (linetable_entry_flags
) 0),
18286 m_last_address (m_address
)
18291 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
18292 const gdb_byte
*line_ptr
,
18293 unrelocated_addr unrelocated_lowpc
,
18294 unrelocated_addr address
)
18296 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
18297 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
18298 located at 0x0. In this case, additionally check that if
18299 ADDRESS < UNRELOCATED_LOWPC. */
18301 if ((address
== (unrelocated_addr
) 0 && address
< unrelocated_lowpc
)
18302 || address
== (unrelocated_addr
) -1)
18304 /* This line table is for a function which has been
18305 GCd by the linker. Ignore it. PR gdb/12528 */
18307 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
18308 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
18310 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18311 line_offset
, objfile_name (objfile
));
18312 m_currently_recording_lines
= false;
18313 /* Note: m_currently_recording_lines is left as false until we see
18314 DW_LNE_end_sequence. */
18318 /* Subroutine of dwarf_decode_lines to simplify it.
18319 Process the line number information in LH. */
18322 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
18323 unrelocated_addr lowpc
)
18325 const gdb_byte
*line_ptr
, *extended_end
;
18326 const gdb_byte
*line_end
;
18327 unsigned int bytes_read
, extended_len
;
18328 unsigned char op_code
, extended_op
;
18329 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
18330 bfd
*abfd
= objfile
->obfd
.get ();
18331 struct gdbarch
*gdbarch
= objfile
->arch ();
18333 line_ptr
= lh
->statement_program_start
;
18334 line_end
= lh
->statement_program_end
;
18336 /* Read the statement sequences until there's nothing left. */
18337 while (line_ptr
< line_end
)
18339 /* The DWARF line number program state machine. Reset the state
18340 machine at the start of each sequence. */
18341 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
18342 bool end_sequence
= false;
18344 /* Start a subfile for the current file of the state
18346 const file_entry
*fe
= state_machine
.current_file ();
18349 dwarf2_start_subfile (cu
, *fe
, *lh
);
18351 /* Decode the table. */
18352 while (line_ptr
< line_end
&& !end_sequence
)
18354 op_code
= read_1_byte (abfd
, line_ptr
);
18357 if (op_code
>= lh
->opcode_base
)
18359 /* Special opcode. */
18360 state_machine
.handle_special_opcode (op_code
);
18362 else switch (op_code
)
18364 case DW_LNS_extended_op
:
18365 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
18367 line_ptr
+= bytes_read
;
18368 extended_end
= line_ptr
+ extended_len
;
18369 extended_op
= read_1_byte (abfd
, line_ptr
);
18371 if (DW_LNE_lo_user
<= extended_op
18372 && extended_op
<= DW_LNE_hi_user
)
18374 /* Vendor extension, ignore. */
18375 line_ptr
= extended_end
;
18378 switch (extended_op
)
18380 case DW_LNE_end_sequence
:
18381 state_machine
.handle_end_sequence ();
18382 end_sequence
= true;
18384 case DW_LNE_set_address
:
18386 unrelocated_addr address
18387 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
18388 line_ptr
+= bytes_read
;
18390 state_machine
.check_line_address (cu
, line_ptr
, lowpc
,
18392 state_machine
.handle_set_address (address
);
18395 case DW_LNE_define_file
:
18397 const char *cur_file
;
18398 unsigned int mod_time
, length
;
18401 cur_file
= read_direct_string (abfd
, line_ptr
,
18403 line_ptr
+= bytes_read
;
18404 dindex
= (dir_index
)
18405 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18406 line_ptr
+= bytes_read
;
18408 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18409 line_ptr
+= bytes_read
;
18411 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18412 line_ptr
+= bytes_read
;
18413 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
18416 case DW_LNE_set_discriminator
:
18418 /* The discriminator is not interesting to the
18419 debugger; just ignore it. We still need to
18420 check its value though:
18421 if there are consecutive entries for the same
18422 (non-prologue) line we want to coalesce them.
18425 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18426 line_ptr
+= bytes_read
;
18428 state_machine
.handle_set_discriminator (discr
);
18432 complaint (_("mangled .debug_line section"));
18435 /* Make sure that we parsed the extended op correctly. If e.g.
18436 we expected a different address size than the producer used,
18437 we may have read the wrong number of bytes. */
18438 if (line_ptr
!= extended_end
)
18440 complaint (_("mangled .debug_line section"));
18445 state_machine
.handle_copy ();
18447 case DW_LNS_advance_pc
:
18450 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18451 line_ptr
+= bytes_read
;
18453 state_machine
.handle_advance_pc (adjust
);
18456 case DW_LNS_advance_line
:
18459 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
18460 line_ptr
+= bytes_read
;
18462 state_machine
.handle_advance_line (line_delta
);
18465 case DW_LNS_set_file
:
18467 file_name_index file
18468 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
18470 line_ptr
+= bytes_read
;
18472 state_machine
.handle_set_file (file
);
18475 case DW_LNS_set_column
:
18476 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18477 line_ptr
+= bytes_read
;
18479 case DW_LNS_negate_stmt
:
18480 state_machine
.handle_negate_stmt ();
18482 case DW_LNS_set_basic_block
:
18484 /* Add to the address register of the state machine the
18485 address increment value corresponding to special opcode
18486 255. I.e., this value is scaled by the minimum
18487 instruction length since special opcode 255 would have
18488 scaled the increment. */
18489 case DW_LNS_const_add_pc
:
18490 state_machine
.handle_const_add_pc ();
18492 case DW_LNS_fixed_advance_pc
:
18494 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
18497 state_machine
.handle_fixed_advance_pc (addr_adj
);
18500 case DW_LNS_set_prologue_end
:
18501 state_machine
.handle_set_prologue_end ();
18503 case DW_LNS_set_epilogue_begin
:
18504 state_machine
.handle_set_epilogue_begin ();
18508 /* Unknown standard opcode, ignore it. */
18511 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
18513 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
18514 line_ptr
+= bytes_read
;
18521 dwarf2_debug_line_missing_end_sequence_complaint ();
18523 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18524 in which case we still finish recording the last line). */
18525 state_machine
.record_line (true);
18529 /* Decode the Line Number Program (LNP) for the given line_header
18530 structure and CU. The actual information extracted and the type
18531 of structures created from the LNP depends on the value of PST.
18533 FND holds the CU file name and directory, if known.
18534 It is used for relative paths in the line table.
18536 NOTE: It is important that psymtabs have the same file name (via
18537 strcmp) as the corresponding symtab. Since the directory is not
18538 used in the name of the symtab we don't use it in the name of the
18539 psymtabs we create. E.g. expand_line_sal requires this when
18540 finding psymtabs to expand. A good testcase for this is
18543 LOWPC is the lowest address in CU (or 0 if not known).
18545 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18546 for its PC<->lines mapping information. Otherwise only the filename
18547 table is read in. */
18550 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
18551 unrelocated_addr lowpc
, int decode_mapping
)
18553 if (decode_mapping
)
18554 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
18556 /* Make sure a symtab is created for every file, even files
18557 which contain only variables (i.e. no code with associated
18559 buildsym_compunit
*builder
= cu
->get_builder ();
18560 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
18562 for (auto &fe
: lh
->file_names ())
18564 dwarf2_start_subfile (cu
, fe
, *lh
);
18565 subfile
*sf
= builder
->get_current_subfile ();
18567 if (sf
->symtab
== nullptr)
18568 sf
->symtab
= allocate_symtab (cust
, sf
->name
.c_str (),
18569 sf
->name_for_id
.c_str ());
18571 fe
.symtab
= sf
->symtab
;
18575 /* Start a subfile for DWARF. FILENAME is the name of the file and
18576 DIRNAME the name of the source directory which contains FILENAME
18577 or NULL if not known.
18578 This routine tries to keep line numbers from identical absolute and
18579 relative file names in a common subfile.
18581 Using the `list' example from the GDB testsuite, which resides in
18582 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18583 of /srcdir/list0.c yields the following debugging information for list0.c:
18585 DW_AT_name: /srcdir/list0.c
18586 DW_AT_comp_dir: /compdir
18587 files.files[0].name: list0.h
18588 files.files[0].dir: /srcdir
18589 files.files[1].name: list0.c
18590 files.files[1].dir: /srcdir
18592 The line number information for list0.c has to end up in a single
18593 subfile, so that `break /srcdir/list0.c:1' works as expected.
18594 start_subfile will ensure that this happens provided that we pass the
18595 concatenation of files.files[1].dir and files.files[1].name as the
18599 dwarf2_start_subfile (dwarf2_cu
*cu
, const file_entry
&fe
,
18600 const line_header
&lh
)
18602 std::string filename_holder
;
18603 const char *filename
= fe
.name
;
18604 const char *dirname
= lh
.include_dir_at (fe
.d_index
);
18606 /* In order not to lose the line information directory,
18607 we concatenate it to the filename when it makes sense.
18608 Note that the Dwarf3 standard says (speaking of filenames in line
18609 information): ``The directory index is ignored for file names
18610 that represent full path names''. Thus ignoring dirname in the
18611 `else' branch below isn't an issue. */
18613 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
18615 filename_holder
= path_join (dirname
, filename
);
18616 filename
= filename_holder
.c_str ();
18619 std::string filename_for_id
= lh
.file_file_name (fe
);
18620 cu
->get_builder ()->start_subfile (filename
, filename_for_id
.c_str ());
18624 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
18625 struct dwarf2_cu
*cu
)
18627 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
18628 struct comp_unit_head
*cu_header
= &cu
->header
;
18630 /* NOTE drow/2003-01-30: There used to be a comment and some special
18631 code here to turn a symbol with DW_AT_external and a
18632 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18633 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18634 with some versions of binutils) where shared libraries could have
18635 relocations against symbols in their debug information - the
18636 minimal symbol would have the right address, but the debug info
18637 would not. It's no longer necessary, because we will explicitly
18638 apply relocations when we read in the debug information now. */
18640 /* A DW_AT_location attribute with no contents indicates that a
18641 variable has been optimized away. */
18642 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
18644 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
18648 /* Handle one degenerate form of location expression specially, to
18649 preserve GDB's previous behavior when section offsets are
18650 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
18651 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
18653 if (attr
->form_is_block ())
18655 struct dwarf_block
*block
= attr
->as_block ();
18657 if ((block
->data
[0] == DW_OP_addr
18658 && block
->size
== 1 + cu_header
->addr_size
)
18659 || ((block
->data
[0] == DW_OP_GNU_addr_index
18660 || block
->data
[0] == DW_OP_addrx
)
18662 == 1 + leb128_size (&block
->data
[1]))))
18664 unsigned int dummy
;
18666 unrelocated_addr tem
;
18667 if (block
->data
[0] == DW_OP_addr
)
18668 tem
= cu
->header
.read_address (objfile
->obfd
.get (),
18672 tem
= read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
);
18673 sym
->set_value_address ((CORE_ADDR
) tem
);
18674 sym
->set_aclass_index (LOC_STATIC
);
18675 fixup_symbol_section (sym
, objfile
);
18676 sym
->set_value_address
18677 (sym
->value_address ()
18678 + objfile
->section_offsets
[sym
->section_index ()]);
18683 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18684 expression evaluator, and use LOC_COMPUTED only when necessary
18685 (i.e. when the value of a register or memory location is
18686 referenced, or a thread-local block, etc.). Then again, it might
18687 not be worthwhile. I'm assuming that it isn't unless performance
18688 or memory numbers show me otherwise. */
18690 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
18692 if (sym
->computed_ops ()->location_has_loclist
)
18693 cu
->has_loclist
= true;
18696 /* A helper function to add an "export" symbol. The new symbol starts
18697 as a clone of ORIG, but is modified to defer to the symbol named
18698 ORIG_NAME. The original symbol uses the name given in the source
18699 code, and the symbol that is created here uses the linkage name as
18700 its name. See ada-imported.c. */
18703 add_ada_export_symbol (struct symbol
*orig
, const char *new_name
,
18704 const char *orig_name
, struct dwarf2_cu
*cu
,
18705 struct pending
**list_to_add
)
18707 struct symbol
*copy
18708 = new (&cu
->per_objfile
->objfile
->objfile_obstack
) symbol (*orig
);
18709 copy
->set_linkage_name (new_name
);
18710 SYMBOL_LOCATION_BATON (copy
) = (void *) orig_name
;
18711 copy
->set_aclass_index (copy
->aclass () == LOC_BLOCK
18713 : ada_imported_index
);
18714 add_symbol_to_list (copy
, list_to_add
);
18717 /* A helper function that decides if a given symbol is an Ada Pragma
18718 Import or Pragma Export. */
18721 is_ada_import_or_export (dwarf2_cu
*cu
, const char *name
,
18722 const char *linkagename
)
18724 return (cu
->lang () == language_ada
18725 && linkagename
!= nullptr
18726 && !streq (name
, linkagename
)
18727 /* The following exclusions are necessary because symbols
18728 with names or linkage names that match here will meet the
18729 other criteria but are not in fact caused by Pragma
18730 Import or Pragma Export, and applying the import/export
18731 treatment to them will introduce problems. Some of these
18732 checks only apply to functions, but it is simpler and
18733 harmless to always do them all. */
18734 && !startswith (name
, "__builtin")
18735 && !startswith (linkagename
, "___ghost_")
18736 && !startswith (linkagename
, "__gnat")
18737 && !startswith (linkagename
, "_ada_")
18738 && !streq (linkagename
, "adainit"));
18741 /* Given a pointer to a DWARF information entry, figure out if we need
18742 to make a symbol table entry for it, and if so, create a new entry
18743 and return a pointer to it.
18744 If TYPE is NULL, determine symbol type from the die, otherwise
18745 used the passed type.
18746 If SPACE is not NULL, use it to hold the new symbol. If it is
18747 NULL, allocate a new symbol on the objfile's obstack. */
18749 static struct symbol
*
18750 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
18751 struct symbol
*space
)
18753 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18754 struct objfile
*objfile
= per_objfile
->objfile
;
18755 struct symbol
*sym
= NULL
;
18757 struct attribute
*attr
= NULL
;
18758 struct attribute
*attr2
= NULL
;
18759 struct pending
**list_to_add
= NULL
;
18761 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
18763 name
= dwarf2_name (die
, cu
);
18764 if (name
== nullptr && (die
->tag
== DW_TAG_subprogram
18765 || die
->tag
== DW_TAG_inlined_subroutine
18766 || die
->tag
== DW_TAG_entry_point
))
18767 name
= dw2_linkage_name (die
, cu
);
18771 int suppress_add
= 0;
18776 sym
= new (&objfile
->objfile_obstack
) symbol
;
18777 OBJSTAT (objfile
, n_syms
++);
18779 /* Cache this symbol's name and the name's demangled form (if any). */
18780 sym
->set_language (cu
->lang (), &objfile
->objfile_obstack
);
18781 /* Fortran does not have mangling standard and the mangling does differ
18782 between gfortran, iFort etc. */
18783 const char *physname
18784 = (cu
->lang () == language_fortran
18785 ? dwarf2_full_name (name
, die
, cu
)
18786 : dwarf2_physname (name
, die
, cu
));
18787 const char *linkagename
= dw2_linkage_name (die
, cu
);
18789 if (linkagename
== nullptr || cu
->lang () == language_ada
)
18790 sym
->set_linkage_name (physname
);
18793 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
18794 sym
->set_linkage_name (linkagename
);
18797 /* Handle DW_AT_artificial. */
18798 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
18799 if (attr
!= nullptr)
18800 sym
->set_is_artificial (attr
->as_boolean ());
18802 /* Default assumptions.
18803 Use the passed type or decode it from the die. */
18804 sym
->set_domain (UNDEF_DOMAIN
);
18805 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
18807 sym
->set_type (type
);
18809 sym
->set_type (die_type (die
, cu
));
18810 attr
= dwarf2_attr (die
,
18811 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
18813 if (attr
!= nullptr)
18814 sym
->set_line (attr
->constant_value (0));
18816 attr
= dwarf2_attr (die
,
18817 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
18819 if (attr
!= nullptr && attr
->is_nonnegative ())
18821 file_name_index file_index
18822 = (file_name_index
) attr
->as_nonnegative ();
18823 struct file_entry
*fe
;
18825 if (cu
->line_header
!= NULL
)
18826 fe
= cu
->line_header
->file_name_at (file_index
);
18831 complaint (_("file index out of range"));
18833 sym
->set_symtab (fe
->symtab
);
18839 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
18840 if (attr
!= nullptr)
18842 CORE_ADDR addr
= per_objfile
->relocate (attr
->as_address ());
18843 sym
->set_section_index (SECT_OFF_TEXT (objfile
));
18844 sym
->set_value_address (addr
);
18845 sym
->set_aclass_index (LOC_LABEL
);
18848 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
18849 sym
->set_type (builtin_type (objfile
)->builtin_core_addr
);
18850 sym
->set_domain (LABEL_DOMAIN
);
18851 list_to_add
= cu
->list_in_scope
;
18853 case DW_TAG_entry_point
:
18854 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18856 sym
->set_domain (FUNCTION_DOMAIN
);
18857 sym
->set_aclass_index (LOC_BLOCK
);
18858 /* DW_TAG_entry_point provides an additional entry_point to an
18859 existing sub_program. Therefore, we inherit the "external"
18860 attribute from the sub_program to which the entry_point
18862 attr2
= dwarf2_attr (die
->parent
, DW_AT_external
, cu
);
18863 if (attr2
!= nullptr && attr2
->as_boolean ())
18864 list_to_add
= cu
->get_builder ()->get_global_symbols ();
18866 list_to_add
= cu
->list_in_scope
;
18868 case DW_TAG_subprogram
:
18869 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18871 sym
->set_domain (FUNCTION_DOMAIN
);
18872 sym
->set_aclass_index (LOC_BLOCK
);
18873 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
18874 if ((attr2
!= nullptr && attr2
->as_boolean ())
18875 || cu
->lang () == language_ada
18876 || cu
->lang () == language_fortran
)
18878 /* Subprograms marked external are stored as a global symbol.
18879 Ada and Fortran subprograms, whether marked external or
18880 not, are always stored as a global symbol, because we want
18881 to be able to access them globally. For instance, we want
18882 to be able to break on a nested subprogram without having
18883 to specify the context. */
18884 list_to_add
= cu
->get_builder ()->get_global_symbols ();
18888 list_to_add
= cu
->list_in_scope
;
18891 if (is_ada_import_or_export (cu
, name
, linkagename
))
18893 /* This is either a Pragma Import or Export. They can
18894 be distinguished by the declaration flag. */
18895 sym
->set_linkage_name (name
);
18896 if (die_is_declaration (die
, cu
))
18898 /* For Import, create a symbol using the source
18899 name, and have it refer to the linkage name. */
18900 SYMBOL_LOCATION_BATON (sym
) = (void *) linkagename
;
18901 sym
->set_aclass_index (ada_block_index
);
18905 /* For Export, create a symbol using the source
18906 name, then create a second symbol that refers
18908 add_ada_export_symbol (sym
, linkagename
, name
, cu
,
18913 case DW_TAG_inlined_subroutine
:
18914 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18916 sym
->set_domain (FUNCTION_DOMAIN
);
18917 sym
->set_aclass_index (LOC_BLOCK
);
18918 sym
->set_is_inlined (1);
18919 list_to_add
= cu
->list_in_scope
;
18921 case DW_TAG_template_value_param
:
18924 case DW_TAG_constant
:
18925 case DW_TAG_variable
:
18926 case DW_TAG_member
:
18927 sym
->set_domain (VAR_DOMAIN
);
18928 /* Compilation with minimal debug info may result in
18929 variables with missing type entries. Change the
18930 misleading `void' type to something sensible. */
18931 if (sym
->type ()->code () == TYPE_CODE_VOID
)
18932 sym
->set_type (builtin_type (objfile
)->builtin_int
);
18934 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
18935 /* In the case of DW_TAG_member, we should only be called for
18936 static const members. */
18937 if (die
->tag
== DW_TAG_member
)
18939 /* dwarf2_add_field uses die_is_declaration,
18940 so we do the same. */
18941 gdb_assert (die_is_declaration (die
, cu
));
18944 if (attr
!= nullptr)
18946 dwarf2_const_value (attr
, sym
, cu
);
18947 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
18950 if (attr2
!= nullptr && attr2
->as_boolean ())
18951 list_to_add
= cu
->get_builder ()->get_global_symbols ();
18953 list_to_add
= cu
->list_in_scope
;
18957 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
18958 if (attr
!= nullptr)
18960 var_decode_location (attr
, sym
, cu
);
18961 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
18963 /* Fortran explicitly imports any global symbols to the local
18964 scope by DW_TAG_common_block. */
18965 if (cu
->lang () == language_fortran
&& die
->parent
18966 && die
->parent
->tag
== DW_TAG_common_block
)
18969 if (sym
->aclass () == LOC_STATIC
18970 && sym
->value_address () == 0
18971 && !per_objfile
->per_bfd
->has_section_at_zero
)
18973 /* When a static variable is eliminated by the linker,
18974 the corresponding debug information is not stripped
18975 out, but the variable address is set to null;
18976 do not add such variables into symbol table. */
18978 else if (attr2
!= nullptr && attr2
->as_boolean ())
18980 if (sym
->aclass () == LOC_STATIC
18981 && (objfile
->flags
& OBJF_MAINLINE
) == 0
18982 && per_objfile
->per_bfd
->can_copy
)
18984 /* A global static variable might be subject to
18985 copy relocation. We first check for a local
18986 minsym, though, because maybe the symbol was
18987 marked hidden, in which case this would not
18989 bound_minimal_symbol found
18990 = (lookup_minimal_symbol_linkage
18991 (sym
->linkage_name (), objfile
));
18992 if (found
.minsym
!= nullptr)
18993 sym
->maybe_copied
= 1;
18996 /* A variable with DW_AT_external is never static,
18997 but it may be block-scoped. */
18999 = ((cu
->list_in_scope
19000 == cu
->get_builder ()->get_file_symbols ())
19001 ? cu
->get_builder ()->get_global_symbols ()
19002 : cu
->list_in_scope
);
19005 list_to_add
= cu
->list_in_scope
;
19007 if (list_to_add
!= nullptr
19008 && is_ada_import_or_export (cu
, name
, linkagename
))
19010 /* This is a Pragma Export. A Pragma Import won't
19011 be seen here, because it will not have a location
19012 and so will be handled below. */
19013 add_ada_export_symbol (sym
, name
, linkagename
, cu
,
19019 /* We do not know the address of this symbol.
19020 If it is an external symbol and we have type information
19021 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19022 The address of the variable will then be determined from
19023 the minimal symbol table whenever the variable is
19025 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
19027 /* Fortran explicitly imports any global symbols to the local
19028 scope by DW_TAG_common_block. */
19029 if (cu
->lang () == language_fortran
&& die
->parent
19030 && die
->parent
->tag
== DW_TAG_common_block
)
19032 /* SYMBOL_CLASS doesn't matter here because
19033 read_common_block is going to reset it. */
19035 list_to_add
= cu
->list_in_scope
;
19037 else if (is_ada_import_or_export (cu
, name
, linkagename
))
19039 /* This is a Pragma Import. A Pragma Export won't
19040 be seen here, because it will have a location and
19041 so will be handled above. */
19042 sym
->set_linkage_name (name
);
19043 list_to_add
= cu
->list_in_scope
;
19044 SYMBOL_LOCATION_BATON (sym
) = (void *) linkagename
;
19045 sym
->set_aclass_index (ada_imported_index
);
19047 else if (attr2
!= nullptr && attr2
->as_boolean ()
19048 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
19050 /* A variable with DW_AT_external is never static, but it
19051 may be block-scoped. */
19053 = ((cu
->list_in_scope
19054 == cu
->get_builder ()->get_file_symbols ())
19055 ? cu
->get_builder ()->get_global_symbols ()
19056 : cu
->list_in_scope
);
19058 sym
->set_aclass_index (LOC_UNRESOLVED
);
19060 else if (!die_is_declaration (die
, cu
))
19062 /* Use the default LOC_OPTIMIZED_OUT class. */
19063 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
19065 list_to_add
= cu
->list_in_scope
;
19069 case DW_TAG_formal_parameter
:
19071 /* If we are inside a function, mark this as an argument. If
19072 not, we might be looking at an argument to an inlined function
19073 when we do not have enough information to show inlined frames;
19074 pretend it's a local variable in that case so that the user can
19076 sym
->set_domain (VAR_DOMAIN
);
19077 struct context_stack
*curr
19078 = cu
->get_builder ()->get_current_context_stack ();
19079 if (curr
!= nullptr && curr
->name
!= nullptr)
19080 sym
->set_is_argument (1);
19081 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
19082 if (attr
!= nullptr)
19084 var_decode_location (attr
, sym
, cu
);
19086 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
19087 if (attr
!= nullptr)
19089 dwarf2_const_value (attr
, sym
, cu
);
19092 list_to_add
= cu
->list_in_scope
;
19095 case DW_TAG_unspecified_parameters
:
19096 /* From varargs functions; gdb doesn't seem to have any
19097 interest in this information, so just ignore it for now.
19100 case DW_TAG_template_type_param
:
19103 case DW_TAG_class_type
:
19104 case DW_TAG_interface_type
:
19105 case DW_TAG_structure_type
:
19106 case DW_TAG_union_type
:
19107 case DW_TAG_set_type
:
19108 case DW_TAG_enumeration_type
:
19109 case DW_TAG_namelist
:
19110 if (die
->tag
== DW_TAG_namelist
)
19112 sym
->set_aclass_index (LOC_STATIC
);
19113 sym
->set_domain (VAR_DOMAIN
);
19115 else if (cu
->lang () == language_c
19116 || cu
->lang () == language_cplus
19117 || cu
->lang () == language_objc
19118 || cu
->lang () == language_opencl
19119 || cu
->lang () == language_minimal
)
19121 /* These languages have a tag namespace. Note that
19122 there's a special hack for C++ in the matching code,
19123 so we don't need to enter a separate typedef for the
19125 sym
->set_aclass_index (LOC_TYPEDEF
);
19126 sym
->set_domain (STRUCT_DOMAIN
);
19130 /* Other languages don't have a tag namespace. */
19131 sym
->set_aclass_index (LOC_TYPEDEF
);
19132 sym
->set_domain (TYPE_DOMAIN
);
19135 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19136 really ever be static objects: otherwise, if you try
19137 to, say, break of a class's method and you're in a file
19138 which doesn't mention that class, it won't work unless
19139 the check for all static symbols in lookup_symbol_aux
19140 saves you. See the OtherFileClass tests in
19141 gdb.c++/namespace.exp. */
19145 buildsym_compunit
*builder
= cu
->get_builder ();
19147 = (cu
->list_in_scope
== builder
->get_file_symbols ()
19148 && cu
->lang () == language_cplus
19149 ? builder
->get_global_symbols ()
19150 : cu
->list_in_scope
);
19152 /* The semantics of C++ state that "struct foo {
19153 ... }" also defines a typedef for "foo". */
19154 if (cu
->lang () == language_cplus
19155 || cu
->lang () == language_ada
19156 || cu
->lang () == language_d
19157 || cu
->lang () == language_rust
)
19159 /* The symbol's name is already allocated along
19160 with this objfile, so we don't need to
19161 duplicate it for the type. */
19162 if (sym
->type ()->name () == 0)
19163 sym
->type ()->set_name (sym
->search_name ());
19167 case DW_TAG_unspecified_type
:
19168 if (cu
->lang () == language_ada
)
19171 case DW_TAG_typedef
:
19172 case DW_TAG_array_type
:
19173 case DW_TAG_base_type
:
19174 case DW_TAG_subrange_type
:
19175 case DW_TAG_generic_subrange
:
19176 sym
->set_aclass_index (LOC_TYPEDEF
);
19177 sym
->set_domain (TYPE_DOMAIN
);
19178 list_to_add
= cu
->list_in_scope
;
19180 case DW_TAG_enumerator
:
19181 sym
->set_domain (VAR_DOMAIN
);
19182 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
19183 if (attr
!= nullptr)
19185 dwarf2_const_value (attr
, sym
, cu
);
19188 /* NOTE: carlton/2003-11-10: See comment above in the
19189 DW_TAG_class_type, etc. block. */
19192 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
19193 && cu
->lang () == language_cplus
19194 ? cu
->get_builder ()->get_global_symbols ()
19195 : cu
->list_in_scope
);
19197 case DW_TAG_imported_declaration
:
19198 case DW_TAG_namespace
:
19199 sym
->set_domain (TYPE_DOMAIN
);
19200 sym
->set_aclass_index (LOC_TYPEDEF
);
19201 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19203 case DW_TAG_module
:
19204 sym
->set_aclass_index (LOC_TYPEDEF
);
19205 sym
->set_domain (MODULE_DOMAIN
);
19206 list_to_add
= cu
->get_builder ()->get_global_symbols ();
19208 case DW_TAG_common_block
:
19209 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
19210 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
19211 list_to_add
= cu
->list_in_scope
;
19214 /* Not a tag we recognize. Hopefully we aren't processing
19215 trash data, but since we must specifically ignore things
19216 we don't recognize, there is nothing else we should do at
19218 complaint (_("unsupported tag: '%s'"),
19219 dwarf_tag_name (die
->tag
));
19225 sym
->hash_next
= objfile
->template_symbols
;
19226 objfile
->template_symbols
= sym
;
19227 list_to_add
= NULL
;
19230 if (list_to_add
!= NULL
)
19231 add_symbol_to_list (sym
, list_to_add
);
19233 /* For the benefit of old versions of GCC, check for anonymous
19234 namespaces based on the demangled name. */
19235 if (!cu
->processing_has_namespace_info
19236 && cu
->lang () == language_cplus
)
19237 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
19242 /* Given an attr with a DW_FORM_dataN value in host byte order,
19243 zero-extend it as appropriate for the symbol's type. The DWARF
19244 standard (v4) is not entirely clear about the meaning of using
19245 DW_FORM_dataN for a constant with a signed type, where the type is
19246 wider than the data. The conclusion of a discussion on the DWARF
19247 list was that this is unspecified. We choose to always zero-extend
19248 because that is the interpretation long in use by GCC. */
19251 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
19252 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
19254 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19255 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
.get ()) ?
19256 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
19257 LONGEST l
= attr
->constant_value (0);
19259 if (bits
< sizeof (*value
) * 8)
19261 l
&= ((LONGEST
) 1 << bits
) - 1;
19264 else if (bits
== sizeof (*value
) * 8)
19268 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
19269 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
19276 /* Read a constant value from an attribute. Either set *VALUE, or if
19277 the value does not fit in *VALUE, set *BYTES - either already
19278 allocated on the objfile obstack, or newly allocated on OBSTACK,
19279 or, set *BATON, if we translated the constant to a location
19283 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
19284 const char *name
, struct obstack
*obstack
,
19285 struct dwarf2_cu
*cu
,
19286 LONGEST
*value
, const gdb_byte
**bytes
,
19287 struct dwarf2_locexpr_baton
**baton
)
19289 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19290 struct objfile
*objfile
= per_objfile
->objfile
;
19291 struct comp_unit_head
*cu_header
= &cu
->header
;
19292 struct dwarf_block
*blk
;
19293 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
.get ()) ?
19294 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
19300 switch (attr
->form
)
19303 case DW_FORM_addrx
:
19304 case DW_FORM_GNU_addr_index
:
19308 if (type
->length () != cu_header
->addr_size
)
19309 dwarf2_const_value_length_mismatch_complaint (name
,
19310 cu_header
->addr_size
,
19312 /* Symbols of this form are reasonably rare, so we just
19313 piggyback on the existing location code rather than writing
19314 a new implementation of symbol_computed_ops. */
19315 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
19316 (*baton
)->per_objfile
= per_objfile
;
19317 (*baton
)->per_cu
= cu
->per_cu
;
19318 gdb_assert ((*baton
)->per_cu
);
19320 (*baton
)->size
= 2 + cu_header
->addr_size
;
19321 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
19322 (*baton
)->data
= data
;
19324 data
[0] = DW_OP_addr
;
19325 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
19326 byte_order
, (ULONGEST
) attr
->as_address ());
19327 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
19330 case DW_FORM_string
:
19333 case DW_FORM_GNU_str_index
:
19334 case DW_FORM_GNU_strp_alt
:
19335 /* The string is already allocated on the objfile obstack, point
19337 *bytes
= (const gdb_byte
*) attr
->as_string ();
19339 case DW_FORM_block1
:
19340 case DW_FORM_block2
:
19341 case DW_FORM_block4
:
19342 case DW_FORM_block
:
19343 case DW_FORM_exprloc
:
19344 case DW_FORM_data16
:
19345 blk
= attr
->as_block ();
19346 if (type
->length () != blk
->size
)
19347 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
19349 *bytes
= blk
->data
;
19352 /* The DW_AT_const_value attributes are supposed to carry the
19353 symbol's value "represented as it would be on the target
19354 architecture." By the time we get here, it's already been
19355 converted to host endianness, so we just need to sign- or
19356 zero-extend it as appropriate. */
19357 case DW_FORM_data1
:
19358 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
19360 case DW_FORM_data2
:
19361 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
19363 case DW_FORM_data4
:
19364 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
19366 case DW_FORM_data8
:
19367 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
19370 case DW_FORM_sdata
:
19371 case DW_FORM_implicit_const
:
19372 *value
= attr
->as_signed ();
19375 case DW_FORM_udata
:
19376 *value
= attr
->as_unsigned ();
19380 complaint (_("unsupported const value attribute form: '%s'"),
19381 dwarf_form_name (attr
->form
));
19388 /* Copy constant value from an attribute to a symbol. */
19391 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
19392 struct dwarf2_cu
*cu
)
19394 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19396 const gdb_byte
*bytes
;
19397 struct dwarf2_locexpr_baton
*baton
;
19399 dwarf2_const_value_attr (attr
, sym
->type (),
19400 sym
->print_name (),
19401 &objfile
->objfile_obstack
, cu
,
19402 &value
, &bytes
, &baton
);
19406 SYMBOL_LOCATION_BATON (sym
) = baton
;
19407 sym
->set_aclass_index (dwarf2_locexpr_index
);
19409 else if (bytes
!= NULL
)
19411 sym
->set_value_bytes (bytes
);
19412 sym
->set_aclass_index (LOC_CONST_BYTES
);
19416 sym
->set_value_longest (value
);
19417 sym
->set_aclass_index (LOC_CONST
);
19421 /* Return the type of the die in question using its DW_AT_type attribute. */
19423 static struct type
*
19424 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
19426 struct attribute
*type_attr
;
19428 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
19431 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19432 /* A missing DW_AT_type represents a void type. */
19433 return builtin_type (objfile
)->builtin_void
;
19436 return lookup_die_type (die
, type_attr
, cu
);
19439 /* True iff CU's producer generates GNAT Ada auxiliary information
19440 that allows to find parallel types through that information instead
19441 of having to do expensive parallel lookups by type name. */
19444 need_gnat_info (struct dwarf2_cu
*cu
)
19446 /* Assume that the Ada compiler was GNAT, which always produces
19447 the auxiliary information. */
19448 return (cu
->lang () == language_ada
);
19451 /* Return the auxiliary type of the die in question using its
19452 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19453 attribute is not present. */
19455 static struct type
*
19456 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
19458 struct attribute
*type_attr
;
19460 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
19464 return lookup_die_type (die
, type_attr
, cu
);
19467 /* If DIE has a descriptive_type attribute, then set the TYPE's
19468 descriptive type accordingly. */
19471 set_descriptive_type (struct type
*type
, struct die_info
*die
,
19472 struct dwarf2_cu
*cu
)
19474 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
19476 if (descriptive_type
)
19478 ALLOCATE_GNAT_AUX_TYPE (type
);
19479 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
19483 /* Return the containing type of the die in question using its
19484 DW_AT_containing_type attribute. */
19486 static struct type
*
19487 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
19489 struct attribute
*type_attr
;
19490 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19492 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
19494 error (_("Dwarf Error: Problem turning containing type into gdb type "
19495 "[in module %s]"), objfile_name (objfile
));
19497 return lookup_die_type (die
, type_attr
, cu
);
19500 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19502 static struct type
*
19503 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
19505 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19506 struct objfile
*objfile
= per_objfile
->objfile
;
19509 std::string message
19510 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
19511 objfile_name (objfile
),
19512 sect_offset_str (cu
->header
.sect_off
),
19513 sect_offset_str (die
->sect_off
));
19514 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
19516 return type_allocator (objfile
, cu
->lang ()).new_type (TYPE_CODE_ERROR
,
19520 /* Look up the type of DIE in CU using its type attribute ATTR.
19521 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19522 DW_AT_containing_type.
19523 If there is no type substitute an error marker. */
19525 static struct type
*
19526 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
19527 struct dwarf2_cu
*cu
)
19529 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19530 struct objfile
*objfile
= per_objfile
->objfile
;
19531 struct type
*this_type
;
19533 gdb_assert (attr
->name
== DW_AT_type
19534 || attr
->name
== DW_AT_GNAT_descriptive_type
19535 || attr
->name
== DW_AT_containing_type
);
19537 /* First see if we have it cached. */
19539 if (attr
->form
== DW_FORM_GNU_ref_alt
)
19541 struct dwarf2_per_cu_data
*per_cu
;
19542 sect_offset sect_off
= attr
->get_ref_die_offset ();
19544 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
19545 per_objfile
->per_bfd
);
19546 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
19548 else if (attr
->form_is_ref ())
19550 sect_offset sect_off
= attr
->get_ref_die_offset ();
19552 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
19554 else if (attr
->form
== DW_FORM_ref_sig8
)
19556 ULONGEST signature
= attr
->as_signature ();
19558 return get_signatured_type (die
, signature
, cu
);
19562 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
19563 " at %s [in module %s]"),
19564 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
19565 objfile_name (objfile
));
19566 return build_error_marker_type (cu
, die
);
19569 /* If not cached we need to read it in. */
19571 if (this_type
== NULL
)
19573 struct die_info
*type_die
= NULL
;
19574 struct dwarf2_cu
*type_cu
= cu
;
19576 if (attr
->form_is_ref ())
19577 type_die
= follow_die_ref (die
, attr
, &type_cu
);
19578 if (type_die
== NULL
)
19579 return build_error_marker_type (cu
, die
);
19580 /* If we find the type now, it's probably because the type came
19581 from an inter-CU reference and the type's CU got expanded before
19583 this_type
= read_type_die (type_die
, type_cu
);
19586 /* If we still don't have a type use an error marker. */
19588 if (this_type
== NULL
)
19589 return build_error_marker_type (cu
, die
);
19594 /* Return the type in DIE, CU.
19595 Returns NULL for invalid types.
19597 This first does a lookup in die_type_hash,
19598 and only reads the die in if necessary.
19600 NOTE: This can be called when reading in partial or full symbols. */
19602 static struct type
*
19603 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
19605 struct type
*this_type
;
19607 this_type
= get_die_type (die
, cu
);
19611 return read_type_die_1 (die
, cu
);
19614 /* Read the type in DIE, CU.
19615 Returns NULL for invalid types. */
19617 static struct type
*
19618 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
19620 struct type
*this_type
= NULL
;
19624 case DW_TAG_class_type
:
19625 case DW_TAG_interface_type
:
19626 case DW_TAG_structure_type
:
19627 case DW_TAG_union_type
:
19628 this_type
= read_structure_type (die
, cu
);
19630 case DW_TAG_enumeration_type
:
19631 this_type
= read_enumeration_type (die
, cu
);
19633 case DW_TAG_entry_point
:
19634 case DW_TAG_subprogram
:
19635 case DW_TAG_subroutine_type
:
19636 case DW_TAG_inlined_subroutine
:
19637 this_type
= read_subroutine_type (die
, cu
);
19639 case DW_TAG_array_type
:
19640 this_type
= read_array_type (die
, cu
);
19642 case DW_TAG_set_type
:
19643 this_type
= read_set_type (die
, cu
);
19645 case DW_TAG_pointer_type
:
19646 this_type
= read_tag_pointer_type (die
, cu
);
19648 case DW_TAG_ptr_to_member_type
:
19649 this_type
= read_tag_ptr_to_member_type (die
, cu
);
19651 case DW_TAG_reference_type
:
19652 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
19654 case DW_TAG_rvalue_reference_type
:
19655 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
19657 case DW_TAG_const_type
:
19658 this_type
= read_tag_const_type (die
, cu
);
19660 case DW_TAG_volatile_type
:
19661 this_type
= read_tag_volatile_type (die
, cu
);
19663 case DW_TAG_restrict_type
:
19664 this_type
= read_tag_restrict_type (die
, cu
);
19666 case DW_TAG_string_type
:
19667 this_type
= read_tag_string_type (die
, cu
);
19669 case DW_TAG_typedef
:
19670 this_type
= read_typedef (die
, cu
);
19672 case DW_TAG_generic_subrange
:
19673 case DW_TAG_subrange_type
:
19674 this_type
= read_subrange_type (die
, cu
);
19676 case DW_TAG_base_type
:
19677 this_type
= read_base_type (die
, cu
);
19679 case DW_TAG_unspecified_type
:
19680 this_type
= read_unspecified_type (die
, cu
);
19682 case DW_TAG_namespace
:
19683 this_type
= read_namespace_type (die
, cu
);
19685 case DW_TAG_module
:
19686 this_type
= read_module_type (die
, cu
);
19688 case DW_TAG_atomic_type
:
19689 this_type
= read_tag_atomic_type (die
, cu
);
19692 complaint (_("unexpected tag in read_type_die: '%s'"),
19693 dwarf_tag_name (die
->tag
));
19700 /* See if we can figure out if the class lives in a namespace. We do
19701 this by looking for a member function; its demangled name will
19702 contain namespace info, if there is any.
19703 Return the computed name or NULL.
19704 Space for the result is allocated on the objfile's obstack.
19705 This is the full-die version of guess_partial_die_structure_name.
19706 In this case we know DIE has no useful parent. */
19708 static const char *
19709 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19711 struct die_info
*spec_die
;
19712 struct dwarf2_cu
*spec_cu
;
19713 struct die_info
*child
;
19714 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19717 spec_die
= die_specification (die
, &spec_cu
);
19718 if (spec_die
!= NULL
)
19724 for (child
= die
->child
;
19726 child
= child
->sibling
)
19728 if (child
->tag
== DW_TAG_subprogram
)
19730 const char *linkage_name
= dw2_linkage_name (child
, cu
);
19732 if (linkage_name
!= NULL
)
19734 gdb::unique_xmalloc_ptr
<char> actual_name
19735 (cu
->language_defn
->class_name_from_physname (linkage_name
));
19736 const char *name
= NULL
;
19738 if (actual_name
!= NULL
)
19740 const char *die_name
= dwarf2_name (die
, cu
);
19742 if (die_name
!= NULL
19743 && strcmp (die_name
, actual_name
.get ()) != 0)
19745 /* Strip off the class name from the full name.
19746 We want the prefix. */
19747 int die_name_len
= strlen (die_name
);
19748 int actual_name_len
= strlen (actual_name
.get ());
19749 const char *ptr
= actual_name
.get ();
19751 /* Test for '::' as a sanity check. */
19752 if (actual_name_len
> die_name_len
+ 2
19753 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
19754 name
= obstack_strndup (
19755 &objfile
->per_bfd
->storage_obstack
,
19756 ptr
, actual_name_len
- die_name_len
- 2);
19767 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19768 prefix part in such case. See
19769 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19771 static const char *
19772 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
19774 struct attribute
*attr
;
19777 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
19778 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
19781 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
19784 attr
= dw2_linkage_name_attr (die
, cu
);
19785 const char *attr_name
= attr
->as_string ();
19786 if (attr
== NULL
|| attr_name
== NULL
)
19789 /* dwarf2_name had to be already called. */
19790 gdb_assert (attr
->canonical_string_p ());
19792 /* Strip the base name, keep any leading namespaces/classes. */
19793 base
= strrchr (attr_name
, ':');
19794 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
19797 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19798 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
19800 &base
[-1] - attr_name
);
19803 /* Return the name of the namespace/class that DIE is defined within,
19804 or "" if we can't tell. The caller should not xfree the result.
19806 For example, if we're within the method foo() in the following
19816 then determine_prefix on foo's die will return "N::C". */
19818 static const char *
19819 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
19821 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19822 struct die_info
*parent
, *spec_die
;
19823 struct dwarf2_cu
*spec_cu
;
19824 struct type
*parent_type
;
19825 const char *retval
;
19827 if (cu
->lang () != language_cplus
19828 && cu
->lang () != language_fortran
19829 && cu
->lang () != language_d
19830 && cu
->lang () != language_rust
)
19833 retval
= anonymous_struct_prefix (die
, cu
);
19837 /* We have to be careful in the presence of DW_AT_specification.
19838 For example, with GCC 3.4, given the code
19842 // Definition of N::foo.
19846 then we'll have a tree of DIEs like this:
19848 1: DW_TAG_compile_unit
19849 2: DW_TAG_namespace // N
19850 3: DW_TAG_subprogram // declaration of N::foo
19851 4: DW_TAG_subprogram // definition of N::foo
19852 DW_AT_specification // refers to die #3
19854 Thus, when processing die #4, we have to pretend that we're in
19855 the context of its DW_AT_specification, namely the context of die
19858 spec_die
= die_specification (die
, &spec_cu
);
19859 if (spec_die
== NULL
)
19860 parent
= die
->parent
;
19863 parent
= spec_die
->parent
;
19867 if (parent
== NULL
)
19869 else if (parent
->building_fullname
)
19872 const char *parent_name
;
19874 /* It has been seen on RealView 2.2 built binaries,
19875 DW_TAG_template_type_param types actually _defined_ as
19876 children of the parent class:
19879 template class <class Enum> Class{};
19880 Class<enum E> class_e;
19882 1: DW_TAG_class_type (Class)
19883 2: DW_TAG_enumeration_type (E)
19884 3: DW_TAG_enumerator (enum1:0)
19885 3: DW_TAG_enumerator (enum2:1)
19887 2: DW_TAG_template_type_param
19888 DW_AT_type DW_FORM_ref_udata (E)
19890 Besides being broken debug info, it can put GDB into an
19891 infinite loop. Consider:
19893 When we're building the full name for Class<E>, we'll start
19894 at Class, and go look over its template type parameters,
19895 finding E. We'll then try to build the full name of E, and
19896 reach here. We're now trying to build the full name of E,
19897 and look over the parent DIE for containing scope. In the
19898 broken case, if we followed the parent DIE of E, we'd again
19899 find Class, and once again go look at its template type
19900 arguments, etc., etc. Simply don't consider such parent die
19901 as source-level parent of this die (it can't be, the language
19902 doesn't allow it), and break the loop here. */
19903 name
= dwarf2_name (die
, cu
);
19904 parent_name
= dwarf2_name (parent
, cu
);
19905 complaint (_("template param type '%s' defined within parent '%s'"),
19906 name
? name
: "<unknown>",
19907 parent_name
? parent_name
: "<unknown>");
19911 switch (parent
->tag
)
19913 case DW_TAG_namespace
:
19914 parent_type
= read_type_die (parent
, cu
);
19915 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19916 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19917 Work around this problem here. */
19918 if (cu
->lang () == language_cplus
19919 && strcmp (parent_type
->name (), "::") == 0)
19921 /* We give a name to even anonymous namespaces. */
19922 return parent_type
->name ();
19923 case DW_TAG_class_type
:
19924 case DW_TAG_interface_type
:
19925 case DW_TAG_structure_type
:
19926 case DW_TAG_union_type
:
19927 case DW_TAG_module
:
19928 parent_type
= read_type_die (parent
, cu
);
19929 if (parent_type
->name () != NULL
)
19930 return parent_type
->name ();
19932 /* An anonymous structure is only allowed non-static data
19933 members; no typedefs, no member functions, et cetera.
19934 So it does not need a prefix. */
19936 case DW_TAG_compile_unit
:
19937 case DW_TAG_partial_unit
:
19938 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19939 if (cu
->lang () == language_cplus
19940 && !per_objfile
->per_bfd
->types
.empty ()
19941 && die
->child
!= NULL
19942 && (die
->tag
== DW_TAG_class_type
19943 || die
->tag
== DW_TAG_structure_type
19944 || die
->tag
== DW_TAG_union_type
))
19946 const char *name
= guess_full_die_structure_name (die
, cu
);
19951 case DW_TAG_subprogram
:
19952 /* Nested subroutines in Fortran get a prefix with the name
19953 of the parent's subroutine. Entry points are prefixed by the
19954 parent's namespace. */
19955 if (cu
->lang () == language_fortran
)
19957 if ((die
->tag
== DW_TAG_subprogram
)
19958 && (dwarf2_name (parent
, cu
) != NULL
))
19959 return dwarf2_name (parent
, cu
);
19960 else if (die
->tag
== DW_TAG_entry_point
)
19961 return determine_prefix (parent
, cu
);
19964 case DW_TAG_enumeration_type
:
19965 parent_type
= read_type_die (parent
, cu
);
19966 if (parent_type
->is_declared_class ())
19968 if (parent_type
->name () != NULL
)
19969 return parent_type
->name ();
19974 return determine_prefix (parent
, cu
);
19978 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19979 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19980 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19981 an obconcat, otherwise allocate storage for the result. The CU argument is
19982 used to determine the language and hence, the appropriate separator. */
19984 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19987 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
19988 int physname
, struct dwarf2_cu
*cu
)
19990 const char *lead
= "";
19993 if (suffix
== NULL
|| suffix
[0] == '\0'
19994 || prefix
== NULL
|| prefix
[0] == '\0')
19996 else if (cu
->lang () == language_d
)
19998 /* For D, the 'main' function could be defined in any module, but it
19999 should never be prefixed. */
20000 if (strcmp (suffix
, "D main") == 0)
20008 else if (cu
->lang () == language_fortran
&& physname
)
20010 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
20011 DW_AT_MIPS_linkage_name is preferred and used instead. */
20019 if (prefix
== NULL
)
20021 if (suffix
== NULL
)
20028 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
20030 strcpy (retval
, lead
);
20031 strcat (retval
, prefix
);
20032 strcat (retval
, sep
);
20033 strcat (retval
, suffix
);
20038 /* We have an obstack. */
20039 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
20043 /* Return a generic name for a DW_TAG_template_type_param or
20044 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
20045 per parent, so each function/class/struct template will have their own set
20046 of template parameters named <unnnamed0>, <unnamed1>, ... where the
20047 enumeration starts at 0 and represents the position of the template tag in
20048 the list of unnamed template tags for this parent, counting both, type and
20051 static const char *
20052 unnamed_template_tag_name (die_info
*die
, dwarf2_cu
*cu
)
20054 if (die
->parent
== nullptr)
20057 /* Count the parent types unnamed template type and value children until, we
20058 arrive at our entry. */
20059 size_t nth_unnamed
= 0;
20061 die_info
*child
= die
->parent
->child
;
20062 while (child
!= die
)
20064 gdb_assert (child
!= nullptr);
20065 if (child
->tag
== DW_TAG_template_type_param
20066 || child
->tag
== DW_TAG_template_value_param
)
20068 if (dwarf2_attr (child
, DW_AT_name
, cu
) == nullptr)
20071 child
= child
->sibling
;
20074 const std::string name_str
= "<unnamed" + std::to_string (nth_unnamed
) + ">";
20075 return cu
->per_objfile
->objfile
->intern (name_str
.c_str ());
20078 /* Get name of a die, return NULL if not found. */
20080 static const char *
20081 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
20082 struct objfile
*objfile
)
20084 if (name
== nullptr)
20087 if (cu
->lang () == language_cplus
)
20089 gdb::unique_xmalloc_ptr
<char> canon_name
20090 = cp_canonicalize_string (name
);
20092 if (canon_name
!= nullptr)
20093 name
= objfile
->intern (canon_name
.get ());
20095 else if (cu
->lang () == language_c
)
20097 gdb::unique_xmalloc_ptr
<char> canon_name
20098 = c_canonicalize_name (name
);
20100 if (canon_name
!= nullptr)
20101 name
= objfile
->intern (canon_name
.get ());
20107 /* Get name of a die, return NULL if not found.
20108 Anonymous namespaces are converted to their magic string. */
20110 static const char *
20111 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
20113 struct attribute
*attr
;
20114 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20116 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
20117 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
20118 if (attr_name
== nullptr
20119 && die
->tag
!= DW_TAG_namespace
20120 && die
->tag
!= DW_TAG_class_type
20121 && die
->tag
!= DW_TAG_interface_type
20122 && die
->tag
!= DW_TAG_structure_type
20123 && die
->tag
!= DW_TAG_namelist
20124 && die
->tag
!= DW_TAG_union_type
20125 && die
->tag
!= DW_TAG_template_type_param
20126 && die
->tag
!= DW_TAG_template_value_param
)
20131 /* A member's name should not be canonicalized. This is a bit
20132 of a hack, in that normally it should not be possible to run
20133 into this situation; however, the dw2-unusual-field-names.exp
20134 test creates custom DWARF that does. */
20135 case DW_TAG_member
:
20136 case DW_TAG_compile_unit
:
20137 case DW_TAG_partial_unit
:
20138 /* Compilation units have a DW_AT_name that is a filename, not
20139 a source language identifier. */
20140 case DW_TAG_enumeration_type
:
20141 case DW_TAG_enumerator
:
20142 /* These tags always have simple identifiers already; no need
20143 to canonicalize them. */
20146 case DW_TAG_namespace
:
20147 if (attr_name
!= nullptr)
20149 return CP_ANONYMOUS_NAMESPACE_STR
;
20151 /* DWARF does not actually require template tags to have a name. */
20152 case DW_TAG_template_type_param
:
20153 case DW_TAG_template_value_param
:
20154 if (attr_name
== nullptr)
20155 return unnamed_template_tag_name (die
, cu
);
20157 case DW_TAG_class_type
:
20158 case DW_TAG_interface_type
:
20159 case DW_TAG_structure_type
:
20160 case DW_TAG_union_type
:
20161 case DW_TAG_namelist
:
20162 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20163 structures or unions. These were of the form "._%d" in GCC 4.1,
20164 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20165 and GCC 4.4. We work around this problem by ignoring these. */
20166 if (attr_name
!= nullptr
20167 && (startswith (attr_name
, "._")
20168 || startswith (attr_name
, "<anonymous")))
20171 /* GCC might emit a nameless typedef that has a linkage name. See
20172 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20173 if (!attr
|| attr_name
== NULL
)
20175 attr
= dw2_linkage_name_attr (die
, cu
);
20176 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
20177 if (attr
== NULL
|| attr_name
== NULL
)
20180 /* Avoid demangling attr_name the second time on a second
20181 call for the same DIE. */
20182 if (!attr
->canonical_string_p ())
20184 gdb::unique_xmalloc_ptr
<char> demangled
20185 (gdb_demangle (attr_name
, DMGL_TYPES
));
20186 if (demangled
== nullptr)
20189 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
20190 attr_name
= attr
->as_string ();
20193 /* Strip any leading namespaces/classes, keep only the
20194 base name. DW_AT_name for named DIEs does not
20195 contain the prefixes. */
20196 const char *base
= strrchr (attr_name
, ':');
20197 if (base
&& base
> attr_name
&& base
[-1] == ':')
20208 if (!attr
->canonical_string_p ())
20209 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
20211 return attr
->as_string ();
20214 /* Return the die that this die in an extension of, or NULL if there
20215 is none. *EXT_CU is the CU containing DIE on input, and the CU
20216 containing the return value on output. */
20218 static struct die_info
*
20219 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
20221 struct attribute
*attr
;
20223 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
20227 return follow_die_ref (die
, attr
, ext_cu
);
20231 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
20235 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
20236 to_underlying (die
->sect_off
),
20242 /* Follow reference or signature attribute ATTR of SRC_DIE.
20243 On entry *REF_CU is the CU of SRC_DIE.
20244 On exit *REF_CU is the CU of the result. */
20246 static struct die_info
*
20247 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
20248 struct dwarf2_cu
**ref_cu
)
20250 struct die_info
*die
;
20252 if (attr
->form_is_ref ())
20253 die
= follow_die_ref (src_die
, attr
, ref_cu
);
20254 else if (attr
->form
== DW_FORM_ref_sig8
)
20255 die
= follow_die_sig (src_die
, attr
, ref_cu
);
20258 src_die
->error_dump ();
20259 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20260 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
20266 /* Follow reference OFFSET.
20267 On entry *REF_CU is the CU of the source die referencing OFFSET.
20268 On exit *REF_CU is the CU of the result.
20269 Returns NULL if OFFSET is invalid. */
20271 static struct die_info
*
20272 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
20273 struct dwarf2_cu
**ref_cu
)
20275 struct die_info temp_die
;
20276 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
20277 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20279 gdb_assert (cu
->per_cu
!= NULL
);
20283 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
20284 "source CU contains target offset: %d",
20285 sect_offset_str (cu
->per_cu
->sect_off
),
20286 sect_offset_str (sect_off
),
20287 cu
->header
.offset_in_cu_p (sect_off
));
20289 if (cu
->per_cu
->is_debug_types
)
20291 /* .debug_types CUs cannot reference anything outside their CU.
20292 If they need to, they have to reference a signatured type via
20293 DW_FORM_ref_sig8. */
20294 if (!cu
->header
.offset_in_cu_p (sect_off
))
20297 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
20298 || !cu
->header
.offset_in_cu_p (sect_off
))
20300 struct dwarf2_per_cu_data
*per_cu
;
20302 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
20303 per_objfile
->per_bfd
);
20305 dwarf_read_debug_printf_v ("target CU offset: %s, "
20306 "target CU DIEs loaded: %d",
20307 sect_offset_str (per_cu
->sect_off
),
20308 per_objfile
->get_cu (per_cu
) != nullptr);
20310 /* If necessary, add it to the queue and load its DIEs.
20312 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20313 it doesn't mean they are currently loaded. Since we require them
20314 to be loaded, we must check for ourselves. */
20315 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->lang ())
20316 || per_objfile
->get_cu (per_cu
) == nullptr)
20317 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
20318 false, cu
->lang ());
20320 target_cu
= per_objfile
->get_cu (per_cu
);
20321 gdb_assert (target_cu
!= nullptr);
20323 else if (cu
->dies
== NULL
)
20325 /* We're loading full DIEs during partial symbol reading. */
20326 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
20330 *ref_cu
= target_cu
;
20331 temp_die
.sect_off
= sect_off
;
20333 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
20335 to_underlying (sect_off
));
20338 /* Follow reference attribute ATTR of SRC_DIE.
20339 On entry *REF_CU is the CU of SRC_DIE.
20340 On exit *REF_CU is the CU of the result. */
20342 static struct die_info
*
20343 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
20344 struct dwarf2_cu
**ref_cu
)
20346 sect_offset sect_off
= attr
->get_ref_die_offset ();
20347 struct dwarf2_cu
*cu
= *ref_cu
;
20348 struct die_info
*die
;
20350 if (attr
->form
!= DW_FORM_GNU_ref_alt
&& src_die
->sect_off
== sect_off
)
20352 /* Self-reference, we're done. */
20356 die
= follow_die_offset (sect_off
,
20357 (attr
->form
== DW_FORM_GNU_ref_alt
20358 || cu
->per_cu
->is_dwz
),
20361 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
20362 "at %s [in module %s]"),
20363 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
20364 objfile_name (cu
->per_objfile
->objfile
));
20371 struct dwarf2_locexpr_baton
20372 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
20373 dwarf2_per_cu_data
*per_cu
,
20374 dwarf2_per_objfile
*per_objfile
,
20375 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
20376 bool resolve_abstract_p
)
20378 struct die_info
*die
;
20379 struct attribute
*attr
;
20380 struct dwarf2_locexpr_baton retval
;
20381 struct objfile
*objfile
= per_objfile
->objfile
;
20383 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
20385 cu
= load_cu (per_cu
, per_objfile
, false);
20389 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20390 Instead just throw an error, not much else we can do. */
20391 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20392 sect_offset_str (sect_off
), objfile_name (objfile
));
20395 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
20397 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20398 sect_offset_str (sect_off
), objfile_name (objfile
));
20400 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20401 if (!attr
&& resolve_abstract_p
20402 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
20403 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
20405 CORE_ADDR pc
= get_frame_pc ();
20407 for (const auto &cand_off
20408 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
20410 struct dwarf2_cu
*cand_cu
= cu
;
20411 struct die_info
*cand
20412 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
20415 || cand
->parent
->tag
!= DW_TAG_subprogram
)
20418 unrelocated_addr unrel_low
, unrel_high
;
20419 get_scope_pc_bounds (cand
->parent
, &unrel_low
, &unrel_high
, cu
);
20420 if (unrel_low
== ((unrelocated_addr
) -1))
20422 CORE_ADDR pc_low
= per_objfile
->relocate (unrel_low
);
20423 CORE_ADDR pc_high
= per_objfile
->relocate (unrel_high
);
20424 if (!(pc_low
<= pc
&& pc
< pc_high
))
20428 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20435 /* DWARF: "If there is no such attribute, then there is no effect.".
20436 DATA is ignored if SIZE is 0. */
20438 retval
.data
= NULL
;
20441 else if (attr
->form_is_section_offset ())
20443 struct dwarf2_loclist_baton loclist_baton
;
20444 CORE_ADDR pc
= get_frame_pc ();
20447 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
20449 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
20451 retval
.size
= size
;
20455 if (!attr
->form_is_block ())
20456 error (_("Dwarf Error: DIE at %s referenced in module %s "
20457 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20458 sect_offset_str (sect_off
), objfile_name (objfile
));
20460 struct dwarf_block
*block
= attr
->as_block ();
20461 retval
.data
= block
->data
;
20462 retval
.size
= block
->size
;
20464 retval
.per_objfile
= per_objfile
;
20465 retval
.per_cu
= cu
->per_cu
;
20467 per_objfile
->age_comp_units ();
20474 struct dwarf2_locexpr_baton
20475 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
20476 dwarf2_per_cu_data
*per_cu
,
20477 dwarf2_per_objfile
*per_objfile
,
20478 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
20480 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
20482 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
20486 /* Write a constant of a given type as target-ordered bytes into
20489 static const gdb_byte
*
20490 write_constant_as_bytes (struct obstack
*obstack
,
20491 enum bfd_endian byte_order
,
20498 *len
= type
->length ();
20499 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
20500 store_unsigned_integer (result
, *len
, byte_order
, value
);
20508 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
20509 dwarf2_per_cu_data
*per_cu
,
20510 dwarf2_per_objfile
*per_objfile
,
20514 struct die_info
*die
;
20515 struct attribute
*attr
;
20516 const gdb_byte
*result
= NULL
;
20519 enum bfd_endian byte_order
;
20520 struct objfile
*objfile
= per_objfile
->objfile
;
20522 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
20524 cu
= load_cu (per_cu
, per_objfile
, false);
20528 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20529 Instead just throw an error, not much else we can do. */
20530 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
20531 sect_offset_str (sect_off
), objfile_name (objfile
));
20534 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
20536 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
20537 sect_offset_str (sect_off
), objfile_name (objfile
));
20539 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20543 byte_order
= (bfd_big_endian (objfile
->obfd
.get ())
20544 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
20546 switch (attr
->form
)
20549 case DW_FORM_addrx
:
20550 case DW_FORM_GNU_addr_index
:
20554 *len
= cu
->header
.addr_size
;
20555 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
20556 store_unsigned_integer (tem
, *len
, byte_order
,
20557 (ULONGEST
) attr
->as_address ());
20561 case DW_FORM_string
:
20564 case DW_FORM_GNU_str_index
:
20565 case DW_FORM_GNU_strp_alt
:
20566 /* The string is already allocated on the objfile obstack, point
20569 const char *attr_name
= attr
->as_string ();
20570 result
= (const gdb_byte
*) attr_name
;
20571 *len
= strlen (attr_name
);
20574 case DW_FORM_block1
:
20575 case DW_FORM_block2
:
20576 case DW_FORM_block4
:
20577 case DW_FORM_block
:
20578 case DW_FORM_exprloc
:
20579 case DW_FORM_data16
:
20581 struct dwarf_block
*block
= attr
->as_block ();
20582 result
= block
->data
;
20583 *len
= block
->size
;
20587 /* The DW_AT_const_value attributes are supposed to carry the
20588 symbol's value "represented as it would be on the target
20589 architecture." By the time we get here, it's already been
20590 converted to host endianness, so we just need to sign- or
20591 zero-extend it as appropriate. */
20592 case DW_FORM_data1
:
20593 type
= die_type (die
, cu
);
20594 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
20595 if (result
== NULL
)
20596 result
= write_constant_as_bytes (obstack
, byte_order
,
20599 case DW_FORM_data2
:
20600 type
= die_type (die
, cu
);
20601 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
20602 if (result
== NULL
)
20603 result
= write_constant_as_bytes (obstack
, byte_order
,
20606 case DW_FORM_data4
:
20607 type
= die_type (die
, cu
);
20608 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
20609 if (result
== NULL
)
20610 result
= write_constant_as_bytes (obstack
, byte_order
,
20613 case DW_FORM_data8
:
20614 type
= die_type (die
, cu
);
20615 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
20616 if (result
== NULL
)
20617 result
= write_constant_as_bytes (obstack
, byte_order
,
20621 case DW_FORM_sdata
:
20622 case DW_FORM_implicit_const
:
20623 type
= die_type (die
, cu
);
20624 result
= write_constant_as_bytes (obstack
, byte_order
,
20625 type
, attr
->as_signed (), len
);
20628 case DW_FORM_udata
:
20629 type
= die_type (die
, cu
);
20630 result
= write_constant_as_bytes (obstack
, byte_order
,
20631 type
, attr
->as_unsigned (), len
);
20635 complaint (_("unsupported const value attribute form: '%s'"),
20636 dwarf_form_name (attr
->form
));
20646 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
20647 dwarf2_per_cu_data
*per_cu
,
20648 dwarf2_per_objfile
*per_objfile
,
20649 const char **var_name
)
20651 struct die_info
*die
;
20653 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
20655 cu
= load_cu (per_cu
, per_objfile
, false);
20660 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
20664 if (var_name
!= nullptr)
20665 *var_name
= var_decl_name (die
, cu
);
20666 return die_type (die
, cu
);
20672 dwarf2_get_die_type (cu_offset die_offset
,
20673 dwarf2_per_cu_data
*per_cu
,
20674 dwarf2_per_objfile
*per_objfile
)
20676 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
20677 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
20680 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20681 On entry *REF_CU is the CU of SRC_DIE.
20682 On exit *REF_CU is the CU of the result.
20683 Returns NULL if the referenced DIE isn't found. */
20685 static struct die_info
*
20686 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
20687 struct dwarf2_cu
**ref_cu
)
20689 struct die_info temp_die
;
20690 struct dwarf2_cu
*sig_cu
;
20691 struct die_info
*die
;
20692 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
20695 /* While it might be nice to assert sig_type->type == NULL here,
20696 we can get here for DW_AT_imported_declaration where we need
20697 the DIE not the type. */
20699 /* If necessary, add it to the queue and load its DIEs.
20701 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
20702 it doesn't mean they are currently loaded. Since we require them
20703 to be loaded, we must check for ourselves. */
20704 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
20706 || per_objfile
->get_cu (sig_type
) == nullptr)
20707 read_signatured_type (sig_type
, per_objfile
);
20709 sig_cu
= per_objfile
->get_cu (sig_type
);
20710 gdb_assert (sig_cu
!= NULL
);
20711 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
20712 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
20713 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
20714 to_underlying (temp_die
.sect_off
));
20717 /* For .gdb_index version 7 keep track of included TUs.
20718 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20719 if (per_objfile
->per_bfd
->index_table
!= NULL
20720 && !per_objfile
->per_bfd
->index_table
->version_check ())
20721 (*ref_cu
)->per_cu
->imported_symtabs
.push_back (sig_cu
->per_cu
);
20730 /* Follow signatured type referenced by ATTR in SRC_DIE.
20731 On entry *REF_CU is the CU of SRC_DIE.
20732 On exit *REF_CU is the CU of the result.
20733 The result is the DIE of the type.
20734 If the referenced type cannot be found an error is thrown. */
20736 static struct die_info
*
20737 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
20738 struct dwarf2_cu
**ref_cu
)
20740 ULONGEST signature
= attr
->as_signature ();
20741 struct signatured_type
*sig_type
;
20742 struct die_info
*die
;
20744 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
20746 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
20747 /* sig_type will be NULL if the signatured type is missing from
20749 if (sig_type
== NULL
)
20751 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20752 " from DIE at %s [in module %s]"),
20753 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
20754 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
20757 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
20760 src_die
->error_dump ();
20761 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20762 " from DIE at %s [in module %s]"),
20763 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
20764 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
20770 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20771 reading in and processing the type unit if necessary. */
20773 static struct type
*
20774 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
20775 struct dwarf2_cu
*cu
)
20777 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20778 struct signatured_type
*sig_type
;
20779 struct dwarf2_cu
*type_cu
;
20780 struct die_info
*type_die
;
20783 sig_type
= lookup_signatured_type (cu
, signature
);
20784 /* sig_type will be NULL if the signatured type is missing from
20786 if (sig_type
== NULL
)
20788 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20789 " from DIE at %s [in module %s]"),
20790 hex_string (signature
), sect_offset_str (die
->sect_off
),
20791 objfile_name (per_objfile
->objfile
));
20792 return build_error_marker_type (cu
, die
);
20795 /* If we already know the type we're done. */
20796 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
20797 if (type
!= nullptr)
20801 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
20802 if (type_die
!= NULL
)
20804 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20805 is created. This is important, for example, because for c++ classes
20806 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20807 type
= read_type_die (type_die
, type_cu
);
20810 complaint (_("Dwarf Error: Cannot build signatured type %s"
20811 " referenced from DIE at %s [in module %s]"),
20812 hex_string (signature
), sect_offset_str (die
->sect_off
),
20813 objfile_name (per_objfile
->objfile
));
20814 type
= build_error_marker_type (cu
, die
);
20819 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20820 " from DIE at %s [in module %s]"),
20821 hex_string (signature
), sect_offset_str (die
->sect_off
),
20822 objfile_name (per_objfile
->objfile
));
20823 type
= build_error_marker_type (cu
, die
);
20826 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
20831 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20832 reading in and processing the type unit if necessary. */
20834 static struct type
*
20835 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
20836 struct dwarf2_cu
*cu
) /* ARI: editCase function */
20838 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20839 if (attr
->form_is_ref ())
20841 struct dwarf2_cu
*type_cu
= cu
;
20842 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
20844 return read_type_die (type_die
, type_cu
);
20846 else if (attr
->form
== DW_FORM_ref_sig8
)
20848 return get_signatured_type (die
, attr
->as_signature (), cu
);
20852 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20854 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20855 " at %s [in module %s]"),
20856 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
20857 objfile_name (per_objfile
->objfile
));
20858 return build_error_marker_type (cu
, die
);
20862 /* Load the DIEs associated with type unit PER_CU into memory. */
20865 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
20866 dwarf2_per_objfile
*per_objfile
)
20868 struct signatured_type
*sig_type
;
20870 /* We have the per_cu, but we need the signatured_type.
20871 Fortunately this is an easy translation. */
20872 gdb_assert (per_cu
->is_debug_types
);
20873 sig_type
= (struct signatured_type
*) per_cu
;
20875 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
20877 read_signatured_type (sig_type
, per_objfile
);
20879 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
20882 /* Read in a signatured type and build its CU and DIEs.
20883 If the type is a stub for the real type in a DWO file,
20884 read in the real type from the DWO file as well. */
20887 read_signatured_type (signatured_type
*sig_type
,
20888 dwarf2_per_objfile
*per_objfile
)
20890 gdb_assert (sig_type
->is_debug_types
);
20891 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
20893 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
20895 if (!reader
.dummy_p
)
20897 struct dwarf2_cu
*cu
= reader
.cu
;
20898 const gdb_byte
*info_ptr
= reader
.info_ptr
;
20900 gdb_assert (cu
->die_hash
== NULL
);
20902 htab_create_alloc_ex (cu
->header
.get_length_without_initial () / 12,
20906 &cu
->comp_unit_obstack
,
20907 hashtab_obstack_allocate
,
20908 dummy_obstack_deallocate
);
20910 if (reader
.comp_unit_die
->has_children
)
20911 reader
.comp_unit_die
->child
20912 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
20913 reader
.comp_unit_die
);
20914 cu
->dies
= reader
.comp_unit_die
;
20915 /* comp_unit_die is not stored in die_hash, no need. */
20917 /* We try not to read any attributes in this function, because
20918 not all CUs needed for references have been loaded yet, and
20919 symbol table processing isn't initialized. But we have to
20920 set the CU language, or we won't be able to build types
20921 correctly. Similarly, if we do not read the producer, we can
20922 not apply producer-specific interpretation. */
20923 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
20928 sig_type
->tu_read
= 1;
20931 /* Decode simple location descriptions.
20933 Given a pointer to a DWARF block that defines a location, compute
20934 the location. Returns true if the expression was computable by
20935 this function, false otherwise. On a true return, *RESULT is set.
20937 Note that this function does not implement a full DWARF expression
20938 evaluator. Instead, it is used for a few limited purposes:
20940 - Getting the address of a symbol that has a constant address. For
20941 example, if a symbol has a location like "DW_OP_addr", the address
20944 - Getting the offset of a virtual function in its vtable. There
20945 are two forms of this, one of which involves DW_OP_deref -- so this
20946 function handles derefs in a peculiar way to make this 'work'.
20947 (Probably this area should be rewritten.)
20949 - Getting the offset of a field, when it is constant.
20951 Opcodes that cannot be part of a constant expression, for example
20952 those involving registers, simply result in a return of
20955 This function may emit a complaint. */
20958 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
,
20961 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20963 size_t size
= blk
->size
;
20964 const gdb_byte
*data
= blk
->data
;
20965 CORE_ADDR stack
[64];
20967 unsigned int bytes_read
;
20974 stack
[++stacki
] = 0;
21013 stack
[++stacki
] = op
- DW_OP_lit0
;
21018 = (CORE_ADDR
) cu
->header
.read_address (objfile
->obfd
.get (),
21024 case DW_OP_const1u
:
21025 stack
[++stacki
] = read_1_byte (objfile
->obfd
.get (), &data
[i
]);
21029 case DW_OP_const1s
:
21030 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
.get (), &data
[i
]);
21034 case DW_OP_const2u
:
21035 stack
[++stacki
] = read_2_bytes (objfile
->obfd
.get (), &data
[i
]);
21039 case DW_OP_const2s
:
21040 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
.get (), &data
[i
]);
21044 case DW_OP_const4u
:
21045 stack
[++stacki
] = read_4_bytes (objfile
->obfd
.get (), &data
[i
]);
21049 case DW_OP_const4s
:
21050 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
.get (), &data
[i
]);
21054 case DW_OP_const8u
:
21055 stack
[++stacki
] = read_8_bytes (objfile
->obfd
.get (), &data
[i
]);
21060 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
21066 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
21071 stack
[stacki
+ 1] = stack
[stacki
];
21076 stack
[stacki
- 1] += stack
[stacki
];
21080 case DW_OP_plus_uconst
:
21081 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
21087 stack
[stacki
- 1] -= stack
[stacki
];
21092 /* If we're not the last op, then we definitely can't encode
21093 this using GDB's address_class enum. This is valid for partial
21094 global symbols, although the variable's address will be bogus
21101 case DW_OP_GNU_addr_index
:
21102 case DW_OP_GNU_const_index
:
21104 = (CORE_ADDR
) read_addr_index_from_leb128 (cu
, &data
[i
],
21113 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21114 outside of the allocated space. Also enforce minimum>0. */
21115 if (stacki
>= ARRAY_SIZE (stack
) - 1)
21117 complaint (_("location description stack overflow"));
21123 complaint (_("location description stack underflow"));
21128 *result
= stack
[stacki
];
21132 /* memory allocation interface */
21134 static struct dwarf_block
*
21135 dwarf_alloc_block (struct dwarf2_cu
*cu
)
21137 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
21142 /* Macro support. */
21144 /* An overload of dwarf_decode_macros that finds the correct section
21145 and ensures it is read in before calling the other overload. */
21148 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
21149 int section_is_gnu
)
21151 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21152 struct objfile
*objfile
= per_objfile
->objfile
;
21153 const struct line_header
*lh
= cu
->line_header
;
21154 unsigned int offset_size
= cu
->header
.offset_size
;
21155 struct dwarf2_section_info
*section
;
21156 const char *section_name
;
21158 if (cu
->dwo_unit
!= nullptr)
21160 if (section_is_gnu
)
21162 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
21163 section_name
= ".debug_macro.dwo";
21167 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
21168 section_name
= ".debug_macinfo.dwo";
21173 if (section_is_gnu
)
21175 section
= &per_objfile
->per_bfd
->macro
;
21176 section_name
= ".debug_macro";
21180 section
= &per_objfile
->per_bfd
->macinfo
;
21181 section_name
= ".debug_macinfo";
21185 section
->read (objfile
);
21186 if (section
->buffer
== nullptr)
21188 complaint (_("missing %s section"), section_name
);
21192 buildsym_compunit
*builder
= cu
->get_builder ();
21194 struct dwarf2_section_info
*str_offsets_section
;
21195 struct dwarf2_section_info
*str_section
;
21196 std::optional
<ULONGEST
> str_offsets_base
;
21198 if (cu
->dwo_unit
!= nullptr)
21200 str_offsets_section
= &cu
->dwo_unit
->dwo_file
21201 ->sections
.str_offsets
;
21202 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
21203 str_offsets_base
= cu
->header
.addr_size
;
21207 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
21208 str_section
= &per_objfile
->per_bfd
->str
;
21209 str_offsets_base
= cu
->str_offsets_base
;
21212 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
21213 offset_size
, offset
, str_section
, str_offsets_section
,
21214 str_offsets_base
, section_is_gnu
, cu
);
21217 /* Return the .debug_loc section to use for CU.
21218 For DWO files use .debug_loc.dwo. */
21220 static struct dwarf2_section_info
*
21221 cu_debug_loc_section (struct dwarf2_cu
*cu
)
21223 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21227 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
21229 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
21231 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
21232 : &per_objfile
->per_bfd
->loc
);
21235 /* Return the .debug_rnglists section to use for CU. */
21236 static struct dwarf2_section_info
*
21237 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
21239 if (cu
->header
.version
< 5)
21240 error (_(".debug_rnglists section cannot be used in DWARF %d"),
21241 cu
->header
.version
);
21242 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
21244 /* Make sure we read the .debug_rnglists section from the file that
21245 contains the DW_AT_ranges attribute we are reading. Normally that
21246 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
21247 or DW_TAG_skeleton unit, we always want to read from objfile/linked
21249 if (cu
->dwo_unit
!= nullptr
21250 && tag
!= DW_TAG_compile_unit
21251 && tag
!= DW_TAG_skeleton_unit
)
21253 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
21255 if (sections
->rnglists
.size
> 0)
21256 return §ions
->rnglists
;
21258 error (_(".debug_rnglists section is missing from .dwo file."));
21260 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
21263 /* A helper function that fills in a dwarf2_loclist_baton. */
21266 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
21267 struct dwarf2_loclist_baton
*baton
,
21268 const struct attribute
*attr
)
21270 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21271 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
21273 section
->read (per_objfile
->objfile
);
21275 baton
->per_objfile
= per_objfile
;
21276 baton
->per_cu
= cu
->per_cu
;
21277 gdb_assert (baton
->per_cu
);
21278 /* We don't know how long the location list is, but make sure we
21279 don't run off the edge of the section. */
21280 baton
->size
= section
->size
- attr
->as_unsigned ();
21281 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
21282 if (cu
->base_address
.has_value ())
21283 baton
->base_address
= *cu
->base_address
;
21285 baton
->base_address
= {};
21286 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
21290 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
21291 struct dwarf2_cu
*cu
, int is_block
)
21293 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21294 struct objfile
*objfile
= per_objfile
->objfile
;
21295 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
21297 if (attr
->form_is_section_offset ()
21298 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
21299 the section. If so, fall through to the complaint in the
21301 && attr
->as_unsigned () < section
->get_size (objfile
))
21303 struct dwarf2_loclist_baton
*baton
;
21305 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
21307 fill_in_loclist_baton (cu
, baton
, attr
);
21309 if (!cu
->base_address
.has_value ())
21310 complaint (_("Location list used without "
21311 "specifying the CU base address."));
21313 sym
->set_aclass_index ((is_block
21314 ? dwarf2_loclist_block_index
21315 : dwarf2_loclist_index
));
21316 SYMBOL_LOCATION_BATON (sym
) = baton
;
21320 struct dwarf2_locexpr_baton
*baton
;
21322 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
21323 baton
->per_objfile
= per_objfile
;
21324 baton
->per_cu
= cu
->per_cu
;
21325 gdb_assert (baton
->per_cu
);
21327 if (attr
->form_is_block ())
21329 /* Note that we're just copying the block's data pointer
21330 here, not the actual data. We're still pointing into the
21331 info_buffer for SYM's objfile; right now we never release
21332 that buffer, but when we do clean up properly this may
21334 struct dwarf_block
*block
= attr
->as_block ();
21335 baton
->size
= block
->size
;
21336 baton
->data
= block
->data
;
21340 dwarf2_invalid_attrib_class_complaint ("location description",
21341 sym
->natural_name ());
21345 sym
->set_aclass_index ((is_block
21346 ? dwarf2_locexpr_block_index
21347 : dwarf2_locexpr_index
));
21348 SYMBOL_LOCATION_BATON (sym
) = baton
;
21354 const comp_unit_head
*
21355 dwarf2_per_cu_data::get_header () const
21357 if (!m_header_read_in
)
21359 const gdb_byte
*info_ptr
21360 = this->section
->buffer
+ to_underlying (this->sect_off
);
21362 read_comp_unit_head (&m_header
, info_ptr
, this->section
,
21363 rcuh_kind::COMPILE
);
21365 m_header_read_in
= true;
21374 dwarf2_per_cu_data::addr_size () const
21376 return this->get_header ()->addr_size
;
21382 dwarf2_per_cu_data::offset_size () const
21384 return this->get_header ()->offset_size
;
21390 dwarf2_per_cu_data::ref_addr_size () const
21392 const comp_unit_head
*header
= this->get_header ();
21394 if (header
->version
== 2)
21395 return header
->addr_size
;
21397 return header
->offset_size
;
21403 dwarf2_per_cu_data::set_lang (enum language lang
,
21404 dwarf_source_language dw_lang
)
21406 if (unit_type () == DW_UT_partial
)
21409 /* Set if not set already. */
21410 packed
<language
, LANGUAGE_BYTES
> new_value
= lang
;
21411 packed
<language
, LANGUAGE_BYTES
> old_value
= m_lang
.exchange (new_value
);
21412 /* If already set, verify that it's the same value. */
21413 gdb_assert (old_value
== language_unknown
|| old_value
== lang
);
21415 packed
<dwarf_source_language
, 2> new_dw
= dw_lang
;
21416 packed
<dwarf_source_language
, 2> old_dw
= m_dw_lang
.exchange (new_dw
);
21417 gdb_assert (old_dw
== 0 || old_dw
== dw_lang
);
21420 /* A helper function for dwarf2_find_containing_comp_unit that returns
21421 the index of the result, and that searches a vector. It will
21422 return a result even if the offset in question does not actually
21423 occur in any CU. This is separate so that it can be unit
21427 dwarf2_find_containing_comp_unit
21428 (sect_offset sect_off
,
21429 unsigned int offset_in_dwz
,
21430 const std::vector
<dwarf2_per_cu_data_up
> &all_units
)
21435 high
= all_units
.size () - 1;
21438 struct dwarf2_per_cu_data
*mid_cu
;
21439 int mid
= low
+ (high
- low
) / 2;
21441 mid_cu
= all_units
[mid
].get ();
21442 if (mid_cu
->is_dwz
> offset_in_dwz
21443 || (mid_cu
->is_dwz
== offset_in_dwz
21444 && mid_cu
->sect_off
+ mid_cu
->length () > sect_off
))
21449 gdb_assert (low
== high
);
21453 /* Locate the .debug_info compilation unit from CU's objfile which contains
21454 the DIE at OFFSET. Raises an error on failure. */
21456 static struct dwarf2_per_cu_data
*
21457 dwarf2_find_containing_comp_unit (sect_offset sect_off
,
21458 unsigned int offset_in_dwz
,
21459 dwarf2_per_bfd
*per_bfd
)
21461 int low
= dwarf2_find_containing_comp_unit
21462 (sect_off
, offset_in_dwz
, per_bfd
->all_units
);
21463 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_units
[low
].get ();
21465 if (this_cu
->is_dwz
!= offset_in_dwz
|| this_cu
->sect_off
> sect_off
)
21467 if (low
== 0 || this_cu
->is_dwz
!= offset_in_dwz
)
21468 error (_("Dwarf Error: could not find CU containing "
21469 "offset %s [in module %s]"),
21470 sect_offset_str (sect_off
),
21471 bfd_get_filename (per_bfd
->obfd
));
21473 gdb_assert (per_bfd
->all_units
[low
-1]->sect_off
21475 return per_bfd
->all_units
[low
- 1].get ();
21479 if (low
== per_bfd
->all_units
.size () - 1
21480 && sect_off
>= this_cu
->sect_off
+ this_cu
->length ())
21481 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off
));
21482 gdb_assert (sect_off
< this_cu
->sect_off
+ this_cu
->length ());
21489 namespace selftests
{
21490 namespace find_containing_comp_unit
{
21495 dwarf2_per_cu_data_up
one (new dwarf2_per_cu_data
);
21496 dwarf2_per_cu_data
*one_ptr
= one
.get ();
21497 dwarf2_per_cu_data_up
two (new dwarf2_per_cu_data
);
21498 dwarf2_per_cu_data
*two_ptr
= two
.get ();
21499 dwarf2_per_cu_data_up
three (new dwarf2_per_cu_data
);
21500 dwarf2_per_cu_data
*three_ptr
= three
.get ();
21501 dwarf2_per_cu_data_up
four (new dwarf2_per_cu_data
);
21502 dwarf2_per_cu_data
*four_ptr
= four
.get ();
21504 one
->set_length (5);
21505 two
->sect_off
= sect_offset (one
->length ());
21506 two
->set_length (7);
21508 three
->set_length (5);
21510 four
->sect_off
= sect_offset (three
->length ());
21511 four
->set_length (7);
21514 std::vector
<dwarf2_per_cu_data_up
> units
;
21515 units
.push_back (std::move (one
));
21516 units
.push_back (std::move (two
));
21517 units
.push_back (std::move (three
));
21518 units
.push_back (std::move (four
));
21522 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 0, units
);
21523 SELF_CHECK (units
[result
].get () == one_ptr
);
21524 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 0, units
);
21525 SELF_CHECK (units
[result
].get () == one_ptr
);
21526 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 0, units
);
21527 SELF_CHECK (units
[result
].get () == two_ptr
);
21529 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 1, units
);
21530 SELF_CHECK (units
[result
].get () == three_ptr
);
21531 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 1, units
);
21532 SELF_CHECK (units
[result
].get () == three_ptr
);
21533 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 1, units
);
21534 SELF_CHECK (units
[result
].get () == four_ptr
);
21540 #endif /* GDB_SELF_TEST */
21542 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
21545 prepare_one_comp_unit (struct dwarf2_cu
*cu
, struct die_info
*comp_unit_die
,
21546 enum language pretend_language
)
21548 struct attribute
*attr
;
21550 cu
->producer
= dwarf2_string_attr (comp_unit_die
, DW_AT_producer
, cu
);
21552 /* Set the language we're debugging. */
21553 attr
= dwarf2_attr (comp_unit_die
, DW_AT_language
, cu
);
21554 enum language lang
;
21555 dwarf_source_language dw_lang
= (dwarf_source_language
) 0;
21556 if (cu
->producer
!= nullptr
21557 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
21559 /* The XLCL doesn't generate DW_LANG_OpenCL because this
21560 attribute is not standardised yet. As a workaround for the
21561 language detection we fall back to the DW_AT_producer
21563 lang
= language_opencl
;
21564 dw_lang
= DW_LANG_OpenCL
;
21566 else if (cu
->producer
!= nullptr
21567 && strstr (cu
->producer
, "GNU Go ") != NULL
)
21569 /* Similar hack for Go. */
21570 lang
= language_go
;
21571 dw_lang
= DW_LANG_Go
;
21573 else if (attr
!= nullptr)
21575 lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
21576 dw_lang
= (dwarf_source_language
) attr
->constant_value (0);
21579 lang
= pretend_language
;
21581 cu
->language_defn
= language_def (lang
);
21583 switch (comp_unit_die
->tag
)
21585 case DW_TAG_compile_unit
:
21586 cu
->per_cu
->set_unit_type (DW_UT_compile
);
21588 case DW_TAG_partial_unit
:
21589 cu
->per_cu
->set_unit_type (DW_UT_partial
);
21591 case DW_TAG_type_unit
:
21592 cu
->per_cu
->set_unit_type (DW_UT_type
);
21595 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
21596 dwarf_tag_name (comp_unit_die
->tag
),
21597 sect_offset_str (cu
->per_cu
->sect_off
));
21600 cu
->per_cu
->set_lang (lang
, dw_lang
);
21606 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
21608 auto it
= m_dwarf2_cus
.find (per_cu
);
21609 if (it
== m_dwarf2_cus
.end ())
21612 return it
->second
.get ();
21618 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
,
21619 std::unique_ptr
<dwarf2_cu
> cu
)
21621 gdb_assert (this->get_cu (per_cu
) == nullptr);
21623 m_dwarf2_cus
[per_cu
] = std::move (cu
);
21629 dwarf2_per_objfile::age_comp_units ()
21631 dwarf_read_debug_printf_v ("running");
21633 /* This is not expected to be called in the middle of CU expansion. There is
21634 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
21635 loaded in memory. Calling age_comp_units while the queue is in use could
21636 make us free the DIEs for a CU that is in the queue and therefore break
21638 gdb_assert (!queue
.has_value ());
21640 /* Start by clearing all marks. */
21641 for (const auto &pair
: m_dwarf2_cus
)
21642 pair
.second
->clear_mark ();
21644 /* Traverse all CUs, mark them and their dependencies if used recently
21646 for (const auto &pair
: m_dwarf2_cus
)
21648 dwarf2_cu
*cu
= pair
.second
.get ();
21651 if (cu
->last_used
<= dwarf_max_cache_age
)
21655 /* Delete all CUs still not marked. */
21656 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
21658 dwarf2_cu
*cu
= it
->second
.get ();
21660 if (!cu
->is_marked ())
21662 dwarf_read_debug_printf_v ("deleting old CU %s",
21663 sect_offset_str (cu
->per_cu
->sect_off
));
21664 it
= m_dwarf2_cus
.erase (it
);
21674 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
21676 auto it
= m_dwarf2_cus
.find (per_cu
);
21677 if (it
== m_dwarf2_cus
.end ())
21680 m_dwarf2_cus
.erase (it
);
21683 dwarf2_per_objfile::~dwarf2_per_objfile ()
21688 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21689 We store these in a hash table separate from the DIEs, and preserve them
21690 when the DIEs are flushed out of cache.
21692 The CU "per_cu" pointer is needed because offset alone is not enough to
21693 uniquely identify the type. A file may have multiple .debug_types sections,
21694 or the type may come from a DWO file. Furthermore, while it's more logical
21695 to use per_cu->section+offset, with Fission the section with the data is in
21696 the DWO file but we don't know that section at the point we need it.
21697 We have to use something in dwarf2_per_cu_data (or the pointer to it)
21698 because we can enter the lookup routine, get_die_type_at_offset, from
21699 outside this file, and thus won't necessarily have PER_CU->cu.
21700 Fortunately, PER_CU is stable for the life of the objfile. */
21702 struct dwarf2_per_cu_offset_and_type
21704 const struct dwarf2_per_cu_data
*per_cu
;
21705 sect_offset sect_off
;
21709 /* Hash function for a dwarf2_per_cu_offset_and_type. */
21712 per_cu_offset_and_type_hash (const void *item
)
21714 const struct dwarf2_per_cu_offset_and_type
*ofs
21715 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
21717 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
21720 /* Equality function for a dwarf2_per_cu_offset_and_type. */
21723 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
21725 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
21726 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
21727 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
21728 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
21730 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
21731 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
21734 /* Set the type associated with DIE to TYPE. Save it in CU's hash
21735 table if necessary. For convenience, return TYPE.
21737 The DIEs reading must have careful ordering to:
21738 * Not cause infinite loops trying to read in DIEs as a prerequisite for
21739 reading current DIE.
21740 * Not trying to dereference contents of still incompletely read in types
21741 while reading in other DIEs.
21742 * Enable referencing still incompletely read in types just by a pointer to
21743 the type without accessing its fields.
21745 Therefore caller should follow these rules:
21746 * Try to fetch any prerequisite types we may need to build this DIE type
21747 before building the type and calling set_die_type.
21748 * After building type call set_die_type for current DIE as soon as
21749 possible before fetching more types to complete the current type.
21750 * Make the type as complete as possible before fetching more types. */
21752 static struct type
*
21753 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
21754 bool skip_data_location
)
21756 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21757 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
21758 struct objfile
*objfile
= per_objfile
->objfile
;
21759 struct attribute
*attr
;
21760 struct dynamic_prop prop
;
21762 /* For Ada types, make sure that the gnat-specific data is always
21763 initialized (if not already set). There are a few types where
21764 we should not be doing so, because the type-specific area is
21765 already used to hold some other piece of info (eg: TYPE_CODE_FLT
21766 where the type-specific area is used to store the floatformat).
21767 But this is not a problem, because the gnat-specific information
21768 is actually not needed for these types. */
21769 if (need_gnat_info (cu
)
21770 && type
->code () != TYPE_CODE_FUNC
21771 && type
->code () != TYPE_CODE_FLT
21772 && type
->code () != TYPE_CODE_METHODPTR
21773 && type
->code () != TYPE_CODE_MEMBERPTR
21774 && type
->code () != TYPE_CODE_METHOD
21775 && type
->code () != TYPE_CODE_FIXED_POINT
21776 && !HAVE_GNAT_AUX_INFO (type
))
21777 INIT_GNAT_SPECIFIC (type
);
21779 /* Read DW_AT_allocated and set in type. */
21780 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
21783 struct type
*prop_type
= cu
->addr_sized_int_type (false);
21784 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
21785 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
21788 /* Read DW_AT_associated and set in type. */
21789 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
21792 struct type
*prop_type
= cu
->addr_sized_int_type (false);
21793 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
21794 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
21797 /* Read DW_AT_rank and set in type. */
21798 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
21801 struct type
*prop_type
= cu
->addr_sized_int_type (false);
21802 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
21803 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
21806 /* Read DW_AT_data_location and set in type. */
21807 if (!skip_data_location
)
21809 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
21810 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
21811 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
21814 if (per_objfile
->die_type_hash
== NULL
)
21815 per_objfile
->die_type_hash
21816 = htab_up (htab_create_alloc (127,
21817 per_cu_offset_and_type_hash
,
21818 per_cu_offset_and_type_eq
,
21819 NULL
, xcalloc
, xfree
));
21821 ofs
.per_cu
= cu
->per_cu
;
21822 ofs
.sect_off
= die
->sect_off
;
21824 slot
= (struct dwarf2_per_cu_offset_and_type
**)
21825 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
21827 complaint (_("A problem internal to GDB: DIE %s has type already set"),
21828 sect_offset_str (die
->sect_off
));
21829 *slot
= XOBNEW (&objfile
->objfile_obstack
,
21830 struct dwarf2_per_cu_offset_and_type
);
21835 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
21836 or return NULL if the die does not have a saved type. */
21838 static struct type
*
21839 get_die_type_at_offset (sect_offset sect_off
,
21840 dwarf2_per_cu_data
*per_cu
,
21841 dwarf2_per_objfile
*per_objfile
)
21843 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
21845 if (per_objfile
->die_type_hash
== NULL
)
21848 ofs
.per_cu
= per_cu
;
21849 ofs
.sect_off
= sect_off
;
21850 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
21851 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
21858 /* Look up the type for DIE in CU in die_type_hash,
21859 or return NULL if DIE does not have a saved type. */
21861 static struct type
*
21862 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21864 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
21867 struct cmd_list_element
*set_dwarf_cmdlist
;
21868 struct cmd_list_element
*show_dwarf_cmdlist
;
21871 show_check_physname (struct ui_file
*file
, int from_tty
,
21872 struct cmd_list_element
*c
, const char *value
)
21875 _("Whether to check \"physname\" is %s.\n"),
21879 void _initialize_dwarf2_read ();
21881 _initialize_dwarf2_read ()
21883 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
21885 Set DWARF specific variables.\n\
21886 Configure DWARF variables such as the cache size."),
21888 Show DWARF specific variables.\n\
21889 Show DWARF variables such as the cache size."),
21890 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
21891 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
21893 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
21894 &dwarf_max_cache_age
, _("\
21895 Set the upper bound on the age of cached DWARF compilation units."), _("\
21896 Show the upper bound on the age of cached DWARF compilation units."), _("\
21897 A higher limit means that cached compilation units will be stored\n\
21898 in memory longer, and more total memory will be used. Zero disables\n\
21899 caching, which can slow down startup."),
21901 show_dwarf_max_cache_age
,
21902 &set_dwarf_cmdlist
,
21903 &show_dwarf_cmdlist
);
21905 add_setshow_boolean_cmd ("synchronous", class_obscure
,
21906 &dwarf_synchronous
, _("\
21907 Set whether DWARF is read synchronously."), _("\
21908 Show whether DWARF is read synchronously."), _("\
21909 By default, DWARF information is read in worker threads,\n\
21910 and gdb will not generally wait for the reading to complete\n\
21911 before continuing with other work, for example presenting a\n\
21912 prompt to the user.\n\
21913 Enabling this setting will cause the DWARF reader to always wait\n\
21914 for debug info processing to be finished before gdb can proceed."),
21916 show_dwarf_synchronous
,
21917 &set_dwarf_cmdlist
,
21918 &show_dwarf_cmdlist
);
21920 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
21921 Set debugging of the DWARF reader."), _("\
21922 Show debugging of the DWARF reader."), _("\
21923 When enabled (non-zero), debugging messages are printed during DWARF\n\
21924 reading and symtab expansion. A value of 1 (one) provides basic\n\
21925 information. A value greater than 1 provides more verbose information."),
21928 &setdebuglist
, &showdebuglist
);
21930 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
21931 Set debugging of the DWARF DIE reader."), _("\
21932 Show debugging of the DWARF DIE reader."), _("\
21933 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21934 The value is the maximum depth to print."),
21937 &setdebuglist
, &showdebuglist
);
21939 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
21940 Set debugging of the dwarf line reader."), _("\
21941 Show debugging of the dwarf line reader."), _("\
21942 When enabled (non-zero), line number entries are dumped as they are read in.\n\
21943 A value of 1 (one) provides basic information.\n\
21944 A value greater than 1 provides more verbose information."),
21947 &setdebuglist
, &showdebuglist
);
21949 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
21950 Set cross-checking of \"physname\" code against demangler."), _("\
21951 Show cross-checking of \"physname\" code against demangler."), _("\
21952 When enabled, GDB's internal \"physname\" code is checked against\n\
21954 NULL
, show_check_physname
,
21955 &setdebuglist
, &showdebuglist
);
21957 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
21958 &dwarf2_locexpr_funcs
);
21959 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
21960 &dwarf2_loclist_funcs
);
21961 ada_imported_index
= register_symbol_computed_impl (LOC_COMPUTED
,
21962 &ada_imported_funcs
);
21964 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
21965 &dwarf2_block_frame_base_locexpr_funcs
);
21966 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
21967 &dwarf2_block_frame_base_loclist_funcs
);
21968 ada_block_index
= register_symbol_block_impl (LOC_BLOCK
,
21969 &ada_function_alias_funcs
);
21972 selftests::register_test ("dw2_expand_symtabs_matching",
21973 selftests::dw2_expand_symtabs_matching::run_test
);
21974 selftests::register_test ("dwarf2_find_containing_comp_unit",
21975 selftests::find_containing_comp_unit::run_test
);