1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
66 #include "typeprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug
= 0;
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug
= 0;
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug
= 0;
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname
= false;
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections
= false;
126 /* This is used to store the data that is always per objfile. */
127 static const objfile_key
<dwarf2_per_objfile
> dwarf2_objfile_data_key
;
129 /* These are used to store the dwarf2_per_bfd objects.
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
137 static const struct bfd_key
<dwarf2_per_bfd
> dwarf2_per_bfd_bfd_data_key
;
138 static const struct objfile_key
<dwarf2_per_bfd
> dwarf2_per_bfd_objfile_data_key
;
140 /* The "aclass" indices for various kinds of computed DWARF symbols. */
142 static int dwarf2_locexpr_index
;
143 static int dwarf2_loclist_index
;
144 static int dwarf2_locexpr_block_index
;
145 static int dwarf2_loclist_block_index
;
147 /* Size of .debug_loclists section header for 32-bit DWARF format. */
148 #define LOCLIST_HEADER_SIZE32 12
150 /* Size of .debug_loclists section header for 64-bit DWARF format. */
151 #define LOCLIST_HEADER_SIZE64 20
153 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
154 #define RNGLIST_HEADER_SIZE32 12
156 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
157 #define RNGLIST_HEADER_SIZE64 20
159 /* An index into a (C++) symbol name component in a symbol name as
160 recorded in the mapped_index's symbol table. For each C++ symbol
161 in the symbol table, we record one entry for the start of each
162 component in the symbol in a table of name components, and then
163 sort the table, in order to be able to binary search symbol names,
164 ignoring leading namespaces, both completion and regular look up.
165 For example, for symbol "A::B::C", we'll have an entry that points
166 to "A::B::C", another that points to "B::C", and another for "C".
167 Note that function symbols in GDB index have no parameter
168 information, just the function/method names. You can convert a
169 name_component to a "const char *" using the
170 'mapped_index::symbol_name_at(offset_type)' method. */
172 struct name_component
174 /* Offset in the symbol name where the component starts. Stored as
175 a (32-bit) offset instead of a pointer to save memory and improve
176 locality on 64-bit architectures. */
177 offset_type name_offset
;
179 /* The symbol's index in the symbol and constant pool tables of a
184 /* Base class containing bits shared by both .gdb_index and
185 .debug_name indexes. */
187 struct mapped_index_base
189 mapped_index_base () = default;
190 DISABLE_COPY_AND_ASSIGN (mapped_index_base
);
192 /* The name_component table (a sorted vector). See name_component's
193 description above. */
194 std::vector
<name_component
> name_components
;
196 /* How NAME_COMPONENTS is sorted. */
197 enum case_sensitivity name_components_casing
;
199 /* Return the number of names in the symbol table. */
200 virtual size_t symbol_name_count () const = 0;
202 /* Get the name of the symbol at IDX in the symbol table. */
203 virtual const char *symbol_name_at
204 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const = 0;
206 /* Return whether the name at IDX in the symbol table should be
208 virtual bool symbol_name_slot_invalid (offset_type idx
) const
213 /* Build the symbol name component sorted vector, if we haven't
215 void build_name_components (dwarf2_per_objfile
*per_objfile
);
217 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
218 possible matches for LN_NO_PARAMS in the name component
220 std::pair
<std::vector
<name_component
>::const_iterator
,
221 std::vector
<name_component
>::const_iterator
>
222 find_name_components_bounds (const lookup_name_info
&ln_no_params
,
224 dwarf2_per_objfile
*per_objfile
) const;
226 /* Prevent deleting/destroying via a base class pointer. */
228 ~mapped_index_base() = default;
231 /* This is a view into the index that converts from bytes to an
232 offset_type, and allows indexing. Unaligned bytes are specifically
233 allowed here, and handled via unpacking. */
238 offset_view () = default;
240 explicit offset_view (gdb::array_view
<const gdb_byte
> bytes
)
245 /* Extract the INDEXth offset_type from the array. */
246 offset_type
operator[] (size_t index
) const
248 const gdb_byte
*bytes
= &m_bytes
[index
* sizeof (offset_type
)];
249 return (offset_type
) extract_unsigned_integer (bytes
,
250 sizeof (offset_type
),
254 /* Return the number of offset_types in this array. */
257 return m_bytes
.size () / sizeof (offset_type
);
260 /* Return true if this view is empty. */
263 return m_bytes
.empty ();
267 /* The underlying bytes. */
268 gdb::array_view
<const gdb_byte
> m_bytes
;
271 /* A description of the mapped index. The file format is described in
272 a comment by the code that writes the index. */
273 struct mapped_index final
: public mapped_index_base
275 /* Index data format version. */
278 /* The address table data. */
279 gdb::array_view
<const gdb_byte
> address_table
;
281 /* The symbol table, implemented as a hash table. */
282 offset_view symbol_table
;
284 /* A pointer to the constant pool. */
285 gdb::array_view
<const gdb_byte
> constant_pool
;
287 /* Return the index into the constant pool of the name of the IDXth
288 symbol in the symbol table. */
289 offset_type
symbol_name_index (offset_type idx
) const
291 return symbol_table
[2 * idx
];
294 /* Return the index into the constant pool of the CU vector of the
295 IDXth symbol in the symbol table. */
296 offset_type
symbol_vec_index (offset_type idx
) const
298 return symbol_table
[2 * idx
+ 1];
301 bool symbol_name_slot_invalid (offset_type idx
) const override
303 return (symbol_name_index (idx
) == 0
304 && symbol_vec_index (idx
) == 0);
307 /* Convenience method to get at the name of the symbol at IDX in the
309 const char *symbol_name_at
310 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
312 return (const char *) (this->constant_pool
.data ()
313 + symbol_name_index (idx
));
316 size_t symbol_name_count () const override
317 { return this->symbol_table
.size () / 2; }
320 /* A description of the mapped .debug_names.
321 Uninitialized map has CU_COUNT 0. */
322 struct mapped_debug_names final
: public mapped_index_base
324 bfd_endian dwarf5_byte_order
;
325 bool dwarf5_is_dwarf64
;
326 bool augmentation_is_gdb
;
328 uint32_t cu_count
= 0;
329 uint32_t tu_count
, bucket_count
, name_count
;
330 const gdb_byte
*cu_table_reordered
, *tu_table_reordered
;
331 const uint32_t *bucket_table_reordered
, *hash_table_reordered
;
332 const gdb_byte
*name_table_string_offs_reordered
;
333 const gdb_byte
*name_table_entry_offs_reordered
;
334 const gdb_byte
*entry_pool
;
341 /* Attribute name DW_IDX_*. */
344 /* Attribute form DW_FORM_*. */
347 /* Value if FORM is DW_FORM_implicit_const. */
348 LONGEST implicit_const
;
350 std::vector
<attr
> attr_vec
;
353 std::unordered_map
<ULONGEST
, index_val
> abbrev_map
;
355 const char *namei_to_name
356 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const;
358 /* Implementation of the mapped_index_base virtual interface, for
359 the name_components cache. */
361 const char *symbol_name_at
362 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
363 { return namei_to_name (idx
, per_objfile
); }
365 size_t symbol_name_count () const override
366 { return this->name_count
; }
369 /* See dwarf2/read.h. */
372 get_dwarf2_per_objfile (struct objfile
*objfile
)
374 return dwarf2_objfile_data_key
.get (objfile
);
377 /* Default names of the debugging sections. */
379 /* Note that if the debugging section has been compressed, it might
380 have a name like .zdebug_info. */
382 const struct dwarf2_debug_sections dwarf2_elf_names
=
384 { ".debug_info", ".zdebug_info" },
385 { ".debug_abbrev", ".zdebug_abbrev" },
386 { ".debug_line", ".zdebug_line" },
387 { ".debug_loc", ".zdebug_loc" },
388 { ".debug_loclists", ".zdebug_loclists" },
389 { ".debug_macinfo", ".zdebug_macinfo" },
390 { ".debug_macro", ".zdebug_macro" },
391 { ".debug_str", ".zdebug_str" },
392 { ".debug_str_offsets", ".zdebug_str_offsets" },
393 { ".debug_line_str", ".zdebug_line_str" },
394 { ".debug_ranges", ".zdebug_ranges" },
395 { ".debug_rnglists", ".zdebug_rnglists" },
396 { ".debug_types", ".zdebug_types" },
397 { ".debug_addr", ".zdebug_addr" },
398 { ".debug_frame", ".zdebug_frame" },
399 { ".eh_frame", NULL
},
400 { ".gdb_index", ".zgdb_index" },
401 { ".debug_names", ".zdebug_names" },
402 { ".debug_aranges", ".zdebug_aranges" },
406 /* List of DWO/DWP sections. */
408 static const struct dwop_section_names
410 struct dwarf2_section_names abbrev_dwo
;
411 struct dwarf2_section_names info_dwo
;
412 struct dwarf2_section_names line_dwo
;
413 struct dwarf2_section_names loc_dwo
;
414 struct dwarf2_section_names loclists_dwo
;
415 struct dwarf2_section_names macinfo_dwo
;
416 struct dwarf2_section_names macro_dwo
;
417 struct dwarf2_section_names rnglists_dwo
;
418 struct dwarf2_section_names str_dwo
;
419 struct dwarf2_section_names str_offsets_dwo
;
420 struct dwarf2_section_names types_dwo
;
421 struct dwarf2_section_names cu_index
;
422 struct dwarf2_section_names tu_index
;
426 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
427 { ".debug_info.dwo", ".zdebug_info.dwo" },
428 { ".debug_line.dwo", ".zdebug_line.dwo" },
429 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
430 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
431 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
432 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
433 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
434 { ".debug_str.dwo", ".zdebug_str.dwo" },
435 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
436 { ".debug_types.dwo", ".zdebug_types.dwo" },
437 { ".debug_cu_index", ".zdebug_cu_index" },
438 { ".debug_tu_index", ".zdebug_tu_index" },
441 /* local data types */
443 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
444 begin with a header, which contains the following information. */
445 struct loclists_rnglists_header
447 /* A 4-byte or 12-byte length containing the length of the
448 set of entries for this compilation unit, not including the
449 length field itself. */
452 /* A 2-byte version identifier. */
455 /* A 1-byte unsigned integer containing the size in bytes of an address on
456 the target system. */
457 unsigned char addr_size
;
459 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
460 on the target system. */
461 unsigned char segment_collector_size
;
463 /* A 4-byte count of the number of offsets that follow the header. */
464 unsigned int offset_entry_count
;
467 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
468 This includes type_unit_group and quick_file_names. */
470 struct stmt_list_hash
472 /* The DWO unit this table is from or NULL if there is none. */
473 struct dwo_unit
*dwo_unit
;
475 /* Offset in .debug_line or .debug_line.dwo. */
476 sect_offset line_sect_off
;
479 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
480 an object of this type. This contains elements of type unit groups
481 that can be shared across objfiles. The non-shareable parts are in
482 type_unit_group_unshareable. */
484 struct type_unit_group
486 /* The data used to construct the hash key. */
487 struct stmt_list_hash hash
{};
490 /* These sections are what may appear in a (real or virtual) DWO file. */
494 struct dwarf2_section_info abbrev
;
495 struct dwarf2_section_info line
;
496 struct dwarf2_section_info loc
;
497 struct dwarf2_section_info loclists
;
498 struct dwarf2_section_info macinfo
;
499 struct dwarf2_section_info macro
;
500 struct dwarf2_section_info rnglists
;
501 struct dwarf2_section_info str
;
502 struct dwarf2_section_info str_offsets
;
503 /* In the case of a virtual DWO file, these two are unused. */
504 struct dwarf2_section_info info
;
505 std::vector
<dwarf2_section_info
> types
;
508 /* CUs/TUs in DWP/DWO files. */
512 /* Backlink to the containing struct dwo_file. */
513 struct dwo_file
*dwo_file
;
515 /* The "id" that distinguishes this CU/TU.
516 .debug_info calls this "dwo_id", .debug_types calls this "signature".
517 Since signatures came first, we stick with it for consistency. */
520 /* The section this CU/TU lives in, in the DWO file. */
521 struct dwarf2_section_info
*section
;
523 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
524 sect_offset sect_off
;
527 /* For types, offset in the type's DIE of the type defined by this TU. */
528 cu_offset type_offset_in_tu
;
531 /* include/dwarf2.h defines the DWP section codes.
532 It defines a max value but it doesn't define a min value, which we
533 use for error checking, so provide one. */
535 enum dwp_v2_section_ids
540 /* Data for one DWO file.
542 This includes virtual DWO files (a virtual DWO file is a DWO file as it
543 appears in a DWP file). DWP files don't really have DWO files per se -
544 comdat folding of types "loses" the DWO file they came from, and from
545 a high level view DWP files appear to contain a mass of random types.
546 However, to maintain consistency with the non-DWP case we pretend DWP
547 files contain virtual DWO files, and we assign each TU with one virtual
548 DWO file (generally based on the line and abbrev section offsets -
549 a heuristic that seems to work in practice). */
553 dwo_file () = default;
554 DISABLE_COPY_AND_ASSIGN (dwo_file
);
556 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
557 For virtual DWO files the name is constructed from the section offsets
558 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
559 from related CU+TUs. */
560 const char *dwo_name
= nullptr;
562 /* The DW_AT_comp_dir attribute. */
563 const char *comp_dir
= nullptr;
565 /* The bfd, when the file is open. Otherwise this is NULL.
566 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
567 gdb_bfd_ref_ptr dbfd
;
569 /* The sections that make up this DWO file.
570 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
571 sections (for lack of a better name). */
572 struct dwo_sections sections
{};
574 /* The CUs in the file.
575 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
576 an extension to handle LLVM's Link Time Optimization output (where
577 multiple source files may be compiled into a single object/dwo pair). */
580 /* Table of TUs in the file.
581 Each element is a struct dwo_unit. */
585 /* These sections are what may appear in a DWP file. */
589 /* These are used by all DWP versions (1, 2 and 5). */
590 struct dwarf2_section_info str
;
591 struct dwarf2_section_info cu_index
;
592 struct dwarf2_section_info tu_index
;
594 /* These are only used by DWP version 2 and version 5 files.
595 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
596 sections are referenced by section number, and are not recorded here.
597 In DWP version 2 or 5 there is at most one copy of all these sections,
598 each section being (effectively) comprised of the concatenation of all of
599 the individual sections that exist in the version 1 format.
600 To keep the code simple we treat each of these concatenated pieces as a
601 section itself (a virtual section?). */
602 struct dwarf2_section_info abbrev
;
603 struct dwarf2_section_info info
;
604 struct dwarf2_section_info line
;
605 struct dwarf2_section_info loc
;
606 struct dwarf2_section_info loclists
;
607 struct dwarf2_section_info macinfo
;
608 struct dwarf2_section_info macro
;
609 struct dwarf2_section_info rnglists
;
610 struct dwarf2_section_info str_offsets
;
611 struct dwarf2_section_info types
;
614 /* These sections are what may appear in a virtual DWO file in DWP version 1.
615 A virtual DWO file is a DWO file as it appears in a DWP file. */
617 struct virtual_v1_dwo_sections
619 struct dwarf2_section_info abbrev
;
620 struct dwarf2_section_info line
;
621 struct dwarf2_section_info loc
;
622 struct dwarf2_section_info macinfo
;
623 struct dwarf2_section_info macro
;
624 struct dwarf2_section_info str_offsets
;
625 /* Each DWP hash table entry records one CU or one TU.
626 That is recorded here, and copied to dwo_unit.section. */
627 struct dwarf2_section_info info_or_types
;
630 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
631 In version 2, the sections of the DWO files are concatenated together
632 and stored in one section of that name. Thus each ELF section contains
633 several "virtual" sections. */
635 struct virtual_v2_or_v5_dwo_sections
637 bfd_size_type abbrev_offset
;
638 bfd_size_type abbrev_size
;
640 bfd_size_type line_offset
;
641 bfd_size_type line_size
;
643 bfd_size_type loc_offset
;
644 bfd_size_type loc_size
;
646 bfd_size_type loclists_offset
;
647 bfd_size_type loclists_size
;
649 bfd_size_type macinfo_offset
;
650 bfd_size_type macinfo_size
;
652 bfd_size_type macro_offset
;
653 bfd_size_type macro_size
;
655 bfd_size_type rnglists_offset
;
656 bfd_size_type rnglists_size
;
658 bfd_size_type str_offsets_offset
;
659 bfd_size_type str_offsets_size
;
661 /* Each DWP hash table entry records one CU or one TU.
662 That is recorded here, and copied to dwo_unit.section. */
663 bfd_size_type info_or_types_offset
;
664 bfd_size_type info_or_types_size
;
667 /* Contents of DWP hash tables. */
669 struct dwp_hash_table
671 uint32_t version
, nr_columns
;
672 uint32_t nr_units
, nr_slots
;
673 const gdb_byte
*hash_table
, *unit_table
;
678 const gdb_byte
*indices
;
682 /* This is indexed by column number and gives the id of the section
684 #define MAX_NR_V2_DWO_SECTIONS \
685 (1 /* .debug_info or .debug_types */ \
686 + 1 /* .debug_abbrev */ \
687 + 1 /* .debug_line */ \
688 + 1 /* .debug_loc */ \
689 + 1 /* .debug_str_offsets */ \
690 + 1 /* .debug_macro or .debug_macinfo */)
691 int section_ids
[MAX_NR_V2_DWO_SECTIONS
];
692 const gdb_byte
*offsets
;
693 const gdb_byte
*sizes
;
697 /* This is indexed by column number and gives the id of the section
699 #define MAX_NR_V5_DWO_SECTIONS \
700 (1 /* .debug_info */ \
701 + 1 /* .debug_abbrev */ \
702 + 1 /* .debug_line */ \
703 + 1 /* .debug_loclists */ \
704 + 1 /* .debug_str_offsets */ \
705 + 1 /* .debug_macro */ \
706 + 1 /* .debug_rnglists */)
707 int section_ids
[MAX_NR_V5_DWO_SECTIONS
];
708 const gdb_byte
*offsets
;
709 const gdb_byte
*sizes
;
714 /* Data for one DWP file. */
718 dwp_file (const char *name_
, gdb_bfd_ref_ptr
&&abfd
)
720 dbfd (std::move (abfd
))
724 /* Name of the file. */
727 /* File format version. */
731 gdb_bfd_ref_ptr dbfd
;
733 /* Section info for this file. */
734 struct dwp_sections sections
{};
736 /* Table of CUs in the file. */
737 const struct dwp_hash_table
*cus
= nullptr;
739 /* Table of TUs in the file. */
740 const struct dwp_hash_table
*tus
= nullptr;
742 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
746 /* Table to map ELF section numbers to their sections.
747 This is only needed for the DWP V1 file format. */
748 unsigned int num_sections
= 0;
749 asection
**elf_sections
= nullptr;
752 /* Struct used to pass misc. parameters to read_die_and_children, et
753 al. which are used for both .debug_info and .debug_types dies.
754 All parameters here are unchanging for the life of the call. This
755 struct exists to abstract away the constant parameters of die reading. */
757 struct die_reader_specs
759 /* The bfd of die_section. */
762 /* The CU of the DIE we are parsing. */
763 struct dwarf2_cu
*cu
;
765 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
766 struct dwo_file
*dwo_file
;
768 /* The section the die comes from.
769 This is either .debug_info or .debug_types, or the .dwo variants. */
770 struct dwarf2_section_info
*die_section
;
772 /* die_section->buffer. */
773 const gdb_byte
*buffer
;
775 /* The end of the buffer. */
776 const gdb_byte
*buffer_end
;
778 /* The abbreviation table to use when reading the DIEs. */
779 struct abbrev_table
*abbrev_table
;
782 /* A subclass of die_reader_specs that holds storage and has complex
783 constructor and destructor behavior. */
785 class cutu_reader
: public die_reader_specs
789 cutu_reader (dwarf2_per_cu_data
*this_cu
,
790 dwarf2_per_objfile
*per_objfile
,
791 struct abbrev_table
*abbrev_table
,
792 dwarf2_cu
*existing_cu
,
794 abbrev_cache
*cache
= nullptr);
796 explicit cutu_reader (struct dwarf2_per_cu_data
*this_cu
,
797 dwarf2_per_objfile
*per_objfile
,
798 struct dwarf2_cu
*parent_cu
= nullptr,
799 struct dwo_file
*dwo_file
= nullptr);
801 DISABLE_COPY_AND_ASSIGN (cutu_reader
);
803 cutu_reader (cutu_reader
&&) = default;
805 const gdb_byte
*info_ptr
= nullptr;
806 struct die_info
*comp_unit_die
= nullptr;
807 bool dummy_p
= false;
809 /* Release the new CU, putting it on the chain. This cannot be done
813 /* Release the abbrev table, transferring ownership to the
815 abbrev_table_up
release_abbrev_table ()
817 return std::move (m_abbrev_table_holder
);
821 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
822 dwarf2_per_objfile
*per_objfile
,
823 dwarf2_cu
*existing_cu
);
825 struct dwarf2_per_cu_data
*m_this_cu
;
826 std::unique_ptr
<dwarf2_cu
> m_new_cu
;
828 /* The ordinary abbreviation table. */
829 abbrev_table_up m_abbrev_table_holder
;
831 /* The DWO abbreviation table. */
832 abbrev_table_up m_dwo_abbrev_table
;
835 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
836 but this would require a corresponding change in unpack_field_as_long
838 static int bits_per_byte
= 8;
840 struct variant_part_builder
;
842 /* When reading a variant, we track a bit more information about the
843 field, and store it in an object of this type. */
847 int first_field
= -1;
850 /* A variant can contain other variant parts. */
851 std::vector
<variant_part_builder
> variant_parts
;
853 /* If we see a DW_TAG_variant, then this will be set if this is the
855 bool default_branch
= false;
856 /* If we see a DW_AT_discr_value, then this will be the discriminant
858 ULONGEST discriminant_value
= 0;
859 /* If we see a DW_AT_discr_list, then this is a pointer to the list
861 struct dwarf_block
*discr_list_data
= nullptr;
864 /* This represents a DW_TAG_variant_part. */
866 struct variant_part_builder
868 /* The offset of the discriminant field. */
869 sect_offset discriminant_offset
{};
871 /* Variants that are direct children of this variant part. */
872 std::vector
<variant_field
> variants
;
874 /* True if we're currently reading a variant. */
875 bool processing_variant
= false;
880 int accessibility
= 0;
882 /* Variant parts need to find the discriminant, which is a DIE
883 reference. We track the section offset of each field to make
886 struct field field
{};
891 const char *name
= nullptr;
892 std::vector
<struct fn_field
> fnfields
;
895 /* The routines that read and process dies for a C struct or C++ class
896 pass lists of data member fields and lists of member function fields
897 in an instance of a field_info structure, as defined below. */
900 /* List of data member and baseclasses fields. */
901 std::vector
<struct nextfield
> fields
;
902 std::vector
<struct nextfield
> baseclasses
;
904 /* Set if the accessibility of one of the fields is not public. */
905 bool non_public_fields
= false;
907 /* Member function fieldlist array, contains name of possibly overloaded
908 member function, number of overloaded member functions and a pointer
909 to the head of the member function field chain. */
910 std::vector
<struct fnfieldlist
> fnfieldlists
;
912 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
913 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
914 std::vector
<struct decl_field
> typedef_field_list
;
916 /* Nested types defined by this class and the number of elements in this
918 std::vector
<struct decl_field
> nested_types_list
;
920 /* If non-null, this is the variant part we are currently
922 variant_part_builder
*current_variant_part
= nullptr;
923 /* This holds all the top-level variant parts attached to the type
925 std::vector
<variant_part_builder
> variant_parts
;
927 /* Return the total number of fields (including baseclasses). */
930 return fields
.size () + baseclasses
.size ();
934 /* Loaded secondary compilation units are kept in memory until they
935 have not been referenced for the processing of this many
936 compilation units. Set this to zero to disable caching. Cache
937 sizes of up to at least twenty will improve startup time for
938 typical inter-CU-reference binaries, at an obvious memory cost. */
939 static int dwarf_max_cache_age
= 5;
941 show_dwarf_max_cache_age (struct ui_file
*file
, int from_tty
,
942 struct cmd_list_element
*c
, const char *value
)
944 gdb_printf (file
, _("The upper bound on the age of cached "
945 "DWARF compilation units is %s.\n"),
949 /* local function prototypes */
951 static void dwarf2_find_base_address (struct die_info
*die
,
952 struct dwarf2_cu
*cu
);
954 class cooked_index_storage
;
955 static void build_type_psymtabs_reader (cutu_reader
*reader
,
956 cooked_index_storage
*storage
);
958 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
);
960 static unsigned int peek_abbrev_code (bfd
*, const gdb_byte
*);
962 static const gdb_byte
*read_attribute (const struct die_reader_specs
*,
964 const struct attr_abbrev
*,
967 static void read_attribute_reprocess (const struct die_reader_specs
*reader
,
968 struct attribute
*attr
, dwarf_tag tag
);
970 static CORE_ADDR
read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
);
972 static sect_offset
read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
973 dwarf2_section_info
*, sect_offset
);
975 static const char *read_indirect_string
976 (dwarf2_per_objfile
*per_objfile
, bfd
*, const gdb_byte
*,
977 const struct comp_unit_head
*, unsigned int *);
979 static const char *read_indirect_string_at_offset
980 (dwarf2_per_objfile
*per_objfile
, LONGEST str_offset
);
982 static CORE_ADDR
read_addr_index_from_leb128 (struct dwarf2_cu
*,
986 static const char *read_dwo_str_index (const struct die_reader_specs
*reader
,
989 static const char *read_stub_str_index (struct dwarf2_cu
*cu
,
992 static struct attribute
*dwarf2_attr (struct die_info
*, unsigned int,
995 static const char *dwarf2_string_attr (struct die_info
*die
, unsigned int name
,
996 struct dwarf2_cu
*cu
);
998 static const char *dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
);
1000 static int dwarf2_flag_true_p (struct die_info
*die
, unsigned name
,
1001 struct dwarf2_cu
*cu
);
1003 static int die_is_declaration (struct die_info
*, struct dwarf2_cu
*cu
);
1005 static struct die_info
*die_specification (struct die_info
*die
,
1006 struct dwarf2_cu
**);
1008 static line_header_up
dwarf_decode_line_header (sect_offset sect_off
,
1009 struct dwarf2_cu
*cu
);
1011 static void dwarf_decode_lines (struct line_header
*,
1013 CORE_ADDR
, int decode_mapping
);
1015 static void dwarf2_start_subfile (struct dwarf2_cu
*, const char *,
1018 static struct symbol
*new_symbol (struct die_info
*, struct type
*,
1019 struct dwarf2_cu
*, struct symbol
* = NULL
);
1021 static void dwarf2_const_value (const struct attribute
*, struct symbol
*,
1022 struct dwarf2_cu
*);
1024 static void dwarf2_const_value_attr (const struct attribute
*attr
,
1027 struct obstack
*obstack
,
1028 struct dwarf2_cu
*cu
, LONGEST
*value
,
1029 const gdb_byte
**bytes
,
1030 struct dwarf2_locexpr_baton
**baton
);
1032 static struct type
*read_subrange_index_type (struct die_info
*die
,
1033 struct dwarf2_cu
*cu
);
1035 static struct type
*die_type (struct die_info
*, struct dwarf2_cu
*);
1037 static int need_gnat_info (struct dwarf2_cu
*);
1039 static struct type
*die_descriptive_type (struct die_info
*,
1040 struct dwarf2_cu
*);
1042 static void set_descriptive_type (struct type
*, struct die_info
*,
1043 struct dwarf2_cu
*);
1045 static struct type
*die_containing_type (struct die_info
*,
1046 struct dwarf2_cu
*);
1048 static struct type
*lookup_die_type (struct die_info
*, const struct attribute
*,
1049 struct dwarf2_cu
*);
1051 static struct type
*read_type_die (struct die_info
*, struct dwarf2_cu
*);
1053 static struct type
*read_type_die_1 (struct die_info
*, struct dwarf2_cu
*);
1055 static const char *determine_prefix (struct die_info
*die
, struct dwarf2_cu
*);
1057 static char *typename_concat (struct obstack
*obs
, const char *prefix
,
1058 const char *suffix
, int physname
,
1059 struct dwarf2_cu
*cu
);
1061 static void read_file_scope (struct die_info
*, struct dwarf2_cu
*);
1063 static void read_type_unit_scope (struct die_info
*, struct dwarf2_cu
*);
1065 static void read_func_scope (struct die_info
*, struct dwarf2_cu
*);
1067 static void read_lexical_block_scope (struct die_info
*, struct dwarf2_cu
*);
1069 static void read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
);
1071 static void read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
);
1073 /* Return the .debug_loclists section to use for cu. */
1074 static struct dwarf2_section_info
*cu_debug_loc_section (struct dwarf2_cu
*cu
);
1076 /* Return the .debug_rnglists section to use for cu. */
1077 static struct dwarf2_section_info
*cu_debug_rnglists_section
1078 (struct dwarf2_cu
*cu
, dwarf_tag tag
);
1080 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1081 values. Keep the items ordered with increasing constraints compliance. */
1084 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1085 PC_BOUNDS_NOT_PRESENT
,
1087 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1088 were present but they do not form a valid range of PC addresses. */
1091 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1094 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1098 static enum pc_bounds_kind
dwarf2_get_pc_bounds (struct die_info
*,
1099 CORE_ADDR
*, CORE_ADDR
*,
1104 static void get_scope_pc_bounds (struct die_info
*,
1105 CORE_ADDR
*, CORE_ADDR
*,
1106 struct dwarf2_cu
*);
1108 static void dwarf2_record_block_ranges (struct die_info
*, struct block
*,
1109 CORE_ADDR
, struct dwarf2_cu
*);
1111 static void dwarf2_add_field (struct field_info
*, struct die_info
*,
1112 struct dwarf2_cu
*);
1114 static void dwarf2_attach_fields_to_type (struct field_info
*,
1115 struct type
*, struct dwarf2_cu
*);
1117 static void dwarf2_add_member_fn (struct field_info
*,
1118 struct die_info
*, struct type
*,
1119 struct dwarf2_cu
*);
1121 static void dwarf2_attach_fn_fields_to_type (struct field_info
*,
1123 struct dwarf2_cu
*);
1125 static void process_structure_scope (struct die_info
*, struct dwarf2_cu
*);
1127 static void read_common_block (struct die_info
*, struct dwarf2_cu
*);
1129 static void read_namespace (struct die_info
*die
, struct dwarf2_cu
*);
1131 static void read_module (struct die_info
*die
, struct dwarf2_cu
*cu
);
1133 static struct using_direct
**using_directives (struct dwarf2_cu
*cu
);
1135 static void read_import_statement (struct die_info
*die
, struct dwarf2_cu
*);
1137 static int read_namespace_alias (struct die_info
*die
, struct dwarf2_cu
*cu
);
1139 static struct type
*read_module_type (struct die_info
*die
,
1140 struct dwarf2_cu
*cu
);
1142 static const char *namespace_name (struct die_info
*die
,
1143 int *is_anonymous
, struct dwarf2_cu
*);
1145 static void process_enumeration_scope (struct die_info
*, struct dwarf2_cu
*);
1147 static CORE_ADDR
decode_locdesc (struct dwarf_block
*, struct dwarf2_cu
*,
1150 static enum dwarf_array_dim_ordering
read_array_order (struct die_info
*,
1151 struct dwarf2_cu
*);
1153 static struct die_info
*read_die_and_siblings_1
1154 (const struct die_reader_specs
*, const gdb_byte
*, const gdb_byte
**,
1157 static struct die_info
*read_die_and_siblings (const struct die_reader_specs
*,
1158 const gdb_byte
*info_ptr
,
1159 const gdb_byte
**new_info_ptr
,
1160 struct die_info
*parent
);
1162 static const gdb_byte
*read_full_die_1 (const struct die_reader_specs
*,
1163 struct die_info
**, const gdb_byte
*,
1166 static const gdb_byte
*read_full_die (const struct die_reader_specs
*,
1167 struct die_info
**, const gdb_byte
*);
1169 static void process_die (struct die_info
*, struct dwarf2_cu
*);
1171 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu
*,
1174 static const char *dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*);
1176 static const char *dwarf2_full_name (const char *name
,
1177 struct die_info
*die
,
1178 struct dwarf2_cu
*cu
);
1180 static const char *dwarf2_physname (const char *name
, struct die_info
*die
,
1181 struct dwarf2_cu
*cu
);
1183 static struct die_info
*dwarf2_extension (struct die_info
*die
,
1184 struct dwarf2_cu
**);
1186 static void dump_die_shallow (struct ui_file
*, int indent
, struct die_info
*);
1188 static void dump_die_for_error (struct die_info
*);
1190 static void dump_die_1 (struct ui_file
*, int level
, int max_level
,
1193 /*static*/ void dump_die (struct die_info
*, int max_level
);
1195 static void store_in_ref_table (struct die_info
*,
1196 struct dwarf2_cu
*);
1198 static struct die_info
*follow_die_ref_or_sig (struct die_info
*,
1199 const struct attribute
*,
1200 struct dwarf2_cu
**);
1202 static struct die_info
*follow_die_ref (struct die_info
*,
1203 const struct attribute
*,
1204 struct dwarf2_cu
**);
1206 static struct die_info
*follow_die_sig (struct die_info
*,
1207 const struct attribute
*,
1208 struct dwarf2_cu
**);
1210 static struct type
*get_signatured_type (struct die_info
*, ULONGEST
,
1211 struct dwarf2_cu
*);
1213 static struct type
*get_DW_AT_signature_type (struct die_info
*,
1214 const struct attribute
*,
1215 struct dwarf2_cu
*);
1217 static void load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
1218 dwarf2_per_objfile
*per_objfile
);
1220 static void read_signatured_type (signatured_type
*sig_type
,
1221 dwarf2_per_objfile
*per_objfile
);
1223 static int attr_to_dynamic_prop (const struct attribute
*attr
,
1224 struct die_info
*die
, struct dwarf2_cu
*cu
,
1225 struct dynamic_prop
*prop
, struct type
*type
);
1227 /* memory allocation interface */
1229 static struct dwarf_block
*dwarf_alloc_block (struct dwarf2_cu
*);
1231 static struct die_info
*dwarf_alloc_die (struct dwarf2_cu
*, int);
1233 static void dwarf_decode_macros (struct dwarf2_cu
*, unsigned int, int);
1235 static void fill_in_loclist_baton (struct dwarf2_cu
*cu
,
1236 struct dwarf2_loclist_baton
*baton
,
1237 const struct attribute
*attr
);
1239 static void dwarf2_symbol_mark_computed (const struct attribute
*attr
,
1241 struct dwarf2_cu
*cu
,
1244 static const gdb_byte
*skip_one_die (const struct die_reader_specs
*reader
,
1245 const gdb_byte
*info_ptr
,
1246 const struct abbrev_info
*abbrev
,
1247 bool do_skip_children
= true);
1249 static struct dwarf2_per_cu_data
*dwarf2_find_containing_comp_unit
1250 (sect_offset sect_off
, unsigned int offset_in_dwz
,
1251 dwarf2_per_bfd
*per_bfd
);
1253 static void prepare_one_comp_unit (struct dwarf2_cu
*cu
,
1254 struct die_info
*comp_unit_die
,
1255 enum language pretend_language
);
1257 static struct type
*set_die_type (struct die_info
*, struct type
*,
1258 struct dwarf2_cu
*, bool = false);
1260 static void create_all_comp_units (dwarf2_per_objfile
*per_objfile
);
1262 static void load_full_comp_unit (dwarf2_per_cu_data
*per_cu
,
1263 dwarf2_per_objfile
*per_objfile
,
1264 dwarf2_cu
*existing_cu
,
1266 enum language pretend_language
);
1268 static void process_full_comp_unit (dwarf2_cu
*cu
,
1269 enum language pretend_language
);
1271 static void process_full_type_unit (dwarf2_cu
*cu
,
1272 enum language pretend_language
);
1274 static struct type
*get_die_type_at_offset (sect_offset
,
1275 dwarf2_per_cu_data
*per_cu
,
1276 dwarf2_per_objfile
*per_objfile
);
1278 static struct type
*get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
);
1280 static void queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
1281 dwarf2_per_objfile
*per_objfile
,
1282 enum language pretend_language
);
1284 static void process_queue (dwarf2_per_objfile
*per_objfile
);
1286 /* Class, the destructor of which frees all allocated queue entries. This
1287 will only have work to do if an error was thrown while processing the
1288 dwarf. If no error was thrown then the queue entries should have all
1289 been processed, and freed, as we went along. */
1291 class dwarf2_queue_guard
1294 explicit dwarf2_queue_guard (dwarf2_per_objfile
*per_objfile
)
1295 : m_per_objfile (per_objfile
)
1297 gdb_assert (!m_per_objfile
->per_bfd
->queue
.has_value ());
1299 m_per_objfile
->per_bfd
->queue
.emplace ();
1302 /* Free any entries remaining on the queue. There should only be
1303 entries left if we hit an error while processing the dwarf. */
1304 ~dwarf2_queue_guard ()
1306 gdb_assert (m_per_objfile
->per_bfd
->queue
.has_value ());
1308 m_per_objfile
->per_bfd
->queue
.reset ();
1311 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard
);
1314 dwarf2_per_objfile
*m_per_objfile
;
1317 dwarf2_queue_item::~dwarf2_queue_item ()
1319 /* Anything still marked queued is likely to be in an
1320 inconsistent state, so discard it. */
1323 per_objfile
->remove_cu (per_cu
);
1328 /* See dwarf2/read.h. */
1331 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data
*data
)
1333 if (data
->is_debug_types
)
1334 delete static_cast<signatured_type
*> (data
);
1339 static file_and_directory
&find_file_and_directory
1340 (struct die_info
*die
, struct dwarf2_cu
*cu
);
1342 static const char *compute_include_file_name
1343 (const struct line_header
*lh
,
1344 const file_entry
&fe
,
1345 const file_and_directory
&cu_info
,
1346 gdb::unique_xmalloc_ptr
<char> *name_holder
);
1348 static htab_up
allocate_signatured_type_table ();
1350 static htab_up
allocate_dwo_unit_table ();
1352 static struct dwo_unit
*lookup_dwo_unit_in_dwp
1353 (dwarf2_per_objfile
*per_objfile
, struct dwp_file
*dwp_file
,
1354 const char *comp_dir
, ULONGEST signature
, int is_debug_types
);
1356 static struct dwp_file
*get_dwp_file (dwarf2_per_objfile
*per_objfile
);
1358 static struct dwo_unit
*lookup_dwo_comp_unit
1359 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
1360 ULONGEST signature
);
1362 static struct dwo_unit
*lookup_dwo_type_unit
1363 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
);
1365 static void queue_and_load_all_dwo_tus (dwarf2_cu
*cu
);
1367 /* A unique pointer to a dwo_file. */
1369 typedef std::unique_ptr
<struct dwo_file
> dwo_file_up
;
1371 static void process_cu_includes (dwarf2_per_objfile
*per_objfile
);
1373 static void check_producer (struct dwarf2_cu
*cu
);
1375 /* Various complaints about symbol reading that don't abort the process. */
1378 dwarf2_debug_line_missing_file_complaint (void)
1380 complaint (_(".debug_line section has line data without a file"));
1384 dwarf2_debug_line_missing_end_sequence_complaint (void)
1386 complaint (_(".debug_line section has line "
1387 "program sequence without an end"));
1391 dwarf2_complex_location_expr_complaint (void)
1393 complaint (_("location expression too complex"));
1397 dwarf2_const_value_length_mismatch_complaint (const char *arg1
, int arg2
,
1400 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1405 dwarf2_invalid_attrib_class_complaint (const char *arg1
, const char *arg2
)
1407 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1411 /* Hash function for line_header_hash. */
1414 line_header_hash (const struct line_header
*ofs
)
1416 return to_underlying (ofs
->sect_off
) ^ ofs
->offset_in_dwz
;
1419 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1422 line_header_hash_voidp (const void *item
)
1424 const struct line_header
*ofs
= (const struct line_header
*) item
;
1426 return line_header_hash (ofs
);
1429 /* Equality function for line_header_hash. */
1432 line_header_eq_voidp (const void *item_lhs
, const void *item_rhs
)
1434 const struct line_header
*ofs_lhs
= (const struct line_header
*) item_lhs
;
1435 const struct line_header
*ofs_rhs
= (const struct line_header
*) item_rhs
;
1437 return (ofs_lhs
->sect_off
== ofs_rhs
->sect_off
1438 && ofs_lhs
->offset_in_dwz
== ofs_rhs
->offset_in_dwz
);
1443 /* An iterator for all_comp_units that is based on index. This
1444 approach makes it possible to iterate over all_comp_units safely,
1445 when some caller in the loop may add new units. */
1447 class all_comp_units_iterator
1451 all_comp_units_iterator (dwarf2_per_bfd
*per_bfd
, bool start
)
1452 : m_per_bfd (per_bfd
),
1453 m_index (start
? 0 : per_bfd
->all_comp_units
.size ())
1457 all_comp_units_iterator
&operator++ ()
1463 dwarf2_per_cu_data
*operator* () const
1465 return m_per_bfd
->get_cu (m_index
);
1468 bool operator== (const all_comp_units_iterator
&other
) const
1470 return m_index
== other
.m_index
;
1474 bool operator!= (const all_comp_units_iterator
&other
) const
1476 return m_index
!= other
.m_index
;
1481 dwarf2_per_bfd
*m_per_bfd
;
1485 /* A range adapter for the all_comp_units_iterator. */
1486 class all_comp_units_range
1490 all_comp_units_range (dwarf2_per_bfd
*per_bfd
)
1491 : m_per_bfd (per_bfd
)
1495 all_comp_units_iterator
begin ()
1497 return all_comp_units_iterator (m_per_bfd
, true);
1500 all_comp_units_iterator
end ()
1502 return all_comp_units_iterator (m_per_bfd
, false);
1507 dwarf2_per_bfd
*m_per_bfd
;
1510 /* See declaration. */
1512 dwarf2_per_bfd::dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
,
1515 can_copy (can_copy_
)
1518 names
= &dwarf2_elf_names
;
1520 for (asection
*sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
1521 locate_sections (obfd
, sec
, *names
);
1524 dwarf2_per_bfd::~dwarf2_per_bfd ()
1526 for (auto &per_cu
: all_comp_units
)
1528 per_cu
->imported_symtabs_free ();
1529 per_cu
->free_cached_file_names ();
1532 /* Everything else should be on this->obstack. */
1538 dwarf2_per_objfile::remove_all_cus ()
1540 gdb_assert (!this->per_bfd
->queue
.has_value ());
1542 for (auto pair
: m_dwarf2_cus
)
1545 m_dwarf2_cus
.clear ();
1548 /* A helper class that calls free_cached_comp_units on
1551 class free_cached_comp_units
1555 explicit free_cached_comp_units (dwarf2_per_objfile
*per_objfile
)
1556 : m_per_objfile (per_objfile
)
1560 ~free_cached_comp_units ()
1562 m_per_objfile
->remove_all_cus ();
1565 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units
);
1569 dwarf2_per_objfile
*m_per_objfile
;
1575 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const
1577 if (per_cu
->index
< this->m_symtabs
.size ())
1578 return this->m_symtabs
[per_cu
->index
] != nullptr;
1585 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data
*per_cu
) const
1587 if (per_cu
->index
< this->m_symtabs
.size ())
1588 return this->m_symtabs
[per_cu
->index
];
1595 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data
*per_cu
,
1596 compunit_symtab
*symtab
)
1598 if (per_cu
->index
>= this->m_symtabs
.size ())
1599 this->m_symtabs
.resize (per_cu
->index
+ 1);
1600 gdb_assert (this->m_symtabs
[per_cu
->index
] == nullptr);
1601 this->m_symtabs
[per_cu
->index
] = symtab
;
1604 /* Try to locate the sections we need for DWARF 2 debugging
1605 information and return true if we have enough to do something.
1606 NAMES points to the dwarf2 section names, or is NULL if the standard
1607 ELF names are used. CAN_COPY is true for formats where symbol
1608 interposition is possible and so symbol values must follow copy
1609 relocation rules. */
1612 dwarf2_has_info (struct objfile
*objfile
,
1613 const struct dwarf2_debug_sections
*names
,
1616 if (objfile
->flags
& OBJF_READNEVER
)
1619 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1621 if (per_objfile
== NULL
)
1623 dwarf2_per_bfd
*per_bfd
;
1625 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1626 BFD doesn't require relocations.
1628 We don't share with objfiles for which -readnow was requested,
1629 because it would complicate things when loading the same BFD with
1630 -readnow and then without -readnow. */
1631 if (!gdb_bfd_requires_relocations (objfile
->obfd
)
1632 && (objfile
->flags
& OBJF_READNOW
) == 0)
1634 /* See if one has been created for this BFD yet. */
1635 per_bfd
= dwarf2_per_bfd_bfd_data_key
.get (objfile
->obfd
);
1637 if (per_bfd
== nullptr)
1639 /* No, create it now. */
1640 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
, names
, can_copy
);
1641 dwarf2_per_bfd_bfd_data_key
.set (objfile
->obfd
, per_bfd
);
1646 /* No sharing possible, create one specifically for this objfile. */
1647 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
, names
, can_copy
);
1648 dwarf2_per_bfd_objfile_data_key
.set (objfile
, per_bfd
);
1651 per_objfile
= dwarf2_objfile_data_key
.emplace (objfile
, objfile
, per_bfd
);
1654 return (!per_objfile
->per_bfd
->info
.is_virtual
1655 && per_objfile
->per_bfd
->info
.s
.section
!= NULL
1656 && !per_objfile
->per_bfd
->abbrev
.is_virtual
1657 && per_objfile
->per_bfd
->abbrev
.s
.section
!= NULL
);
1660 /* See declaration. */
1663 dwarf2_per_bfd::locate_sections (bfd
*abfd
, asection
*sectp
,
1664 const dwarf2_debug_sections
&names
)
1666 flagword aflag
= bfd_section_flags (sectp
);
1668 if ((aflag
& SEC_HAS_CONTENTS
) == 0)
1671 else if (elf_section_data (sectp
)->this_hdr
.sh_size
1672 > bfd_get_file_size (abfd
))
1674 bfd_size_type size
= elf_section_data (sectp
)->this_hdr
.sh_size
;
1675 warning (_("Discarding section %s which has a section size (%s"
1676 ") larger than the file size [in module %s]"),
1677 bfd_section_name (sectp
), phex_nz (size
, sizeof (size
)),
1678 bfd_get_filename (abfd
));
1680 else if (names
.info
.matches (sectp
->name
))
1682 this->info
.s
.section
= sectp
;
1683 this->info
.size
= bfd_section_size (sectp
);
1685 else if (names
.abbrev
.matches (sectp
->name
))
1687 this->abbrev
.s
.section
= sectp
;
1688 this->abbrev
.size
= bfd_section_size (sectp
);
1690 else if (names
.line
.matches (sectp
->name
))
1692 this->line
.s
.section
= sectp
;
1693 this->line
.size
= bfd_section_size (sectp
);
1695 else if (names
.loc
.matches (sectp
->name
))
1697 this->loc
.s
.section
= sectp
;
1698 this->loc
.size
= bfd_section_size (sectp
);
1700 else if (names
.loclists
.matches (sectp
->name
))
1702 this->loclists
.s
.section
= sectp
;
1703 this->loclists
.size
= bfd_section_size (sectp
);
1705 else if (names
.macinfo
.matches (sectp
->name
))
1707 this->macinfo
.s
.section
= sectp
;
1708 this->macinfo
.size
= bfd_section_size (sectp
);
1710 else if (names
.macro
.matches (sectp
->name
))
1712 this->macro
.s
.section
= sectp
;
1713 this->macro
.size
= bfd_section_size (sectp
);
1715 else if (names
.str
.matches (sectp
->name
))
1717 this->str
.s
.section
= sectp
;
1718 this->str
.size
= bfd_section_size (sectp
);
1720 else if (names
.str_offsets
.matches (sectp
->name
))
1722 this->str_offsets
.s
.section
= sectp
;
1723 this->str_offsets
.size
= bfd_section_size (sectp
);
1725 else if (names
.line_str
.matches (sectp
->name
))
1727 this->line_str
.s
.section
= sectp
;
1728 this->line_str
.size
= bfd_section_size (sectp
);
1730 else if (names
.addr
.matches (sectp
->name
))
1732 this->addr
.s
.section
= sectp
;
1733 this->addr
.size
= bfd_section_size (sectp
);
1735 else if (names
.frame
.matches (sectp
->name
))
1737 this->frame
.s
.section
= sectp
;
1738 this->frame
.size
= bfd_section_size (sectp
);
1740 else if (names
.eh_frame
.matches (sectp
->name
))
1742 this->eh_frame
.s
.section
= sectp
;
1743 this->eh_frame
.size
= bfd_section_size (sectp
);
1745 else if (names
.ranges
.matches (sectp
->name
))
1747 this->ranges
.s
.section
= sectp
;
1748 this->ranges
.size
= bfd_section_size (sectp
);
1750 else if (names
.rnglists
.matches (sectp
->name
))
1752 this->rnglists
.s
.section
= sectp
;
1753 this->rnglists
.size
= bfd_section_size (sectp
);
1755 else if (names
.types
.matches (sectp
->name
))
1757 struct dwarf2_section_info type_section
;
1759 memset (&type_section
, 0, sizeof (type_section
));
1760 type_section
.s
.section
= sectp
;
1761 type_section
.size
= bfd_section_size (sectp
);
1763 this->types
.push_back (type_section
);
1765 else if (names
.gdb_index
.matches (sectp
->name
))
1767 this->gdb_index
.s
.section
= sectp
;
1768 this->gdb_index
.size
= bfd_section_size (sectp
);
1770 else if (names
.debug_names
.matches (sectp
->name
))
1772 this->debug_names
.s
.section
= sectp
;
1773 this->debug_names
.size
= bfd_section_size (sectp
);
1775 else if (names
.debug_aranges
.matches (sectp
->name
))
1777 this->debug_aranges
.s
.section
= sectp
;
1778 this->debug_aranges
.size
= bfd_section_size (sectp
);
1781 if ((bfd_section_flags (sectp
) & (SEC_LOAD
| SEC_ALLOC
))
1782 && bfd_section_vma (sectp
) == 0)
1783 this->has_section_at_zero
= true;
1786 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1790 dwarf2_get_section_info (struct objfile
*objfile
,
1791 enum dwarf2_section_enum sect
,
1792 asection
**sectp
, const gdb_byte
**bufp
,
1793 bfd_size_type
*sizep
)
1795 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1796 struct dwarf2_section_info
*info
;
1798 /* We may see an objfile without any DWARF, in which case we just
1800 if (per_objfile
== NULL
)
1809 case DWARF2_DEBUG_FRAME
:
1810 info
= &per_objfile
->per_bfd
->frame
;
1812 case DWARF2_EH_FRAME
:
1813 info
= &per_objfile
->per_bfd
->eh_frame
;
1816 gdb_assert_not_reached ("unexpected section");
1819 info
->read (objfile
);
1821 *sectp
= info
->get_bfd_section ();
1822 *bufp
= info
->buffer
;
1823 *sizep
= info
->size
;
1826 /* See dwarf2/read.h. */
1829 dwarf2_per_bfd::map_info_sections (struct objfile
*objfile
)
1831 info
.read (objfile
);
1832 abbrev
.read (objfile
);
1833 line
.read (objfile
);
1835 str_offsets
.read (objfile
);
1836 line_str
.read (objfile
);
1837 ranges
.read (objfile
);
1838 rnglists
.read (objfile
);
1839 addr
.read (objfile
);
1841 for (auto §ion
: types
)
1842 section
.read (objfile
);
1846 /* DWARF quick_symbol_functions support. */
1848 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1849 unique line tables, so we maintain a separate table of all .debug_line
1850 derived entries to support the sharing.
1851 All the quick functions need is the list of file names. We discard the
1852 line_header when we're done and don't need to record it here. */
1853 struct quick_file_names
1855 /* The data used to construct the hash key. */
1856 struct stmt_list_hash hash
;
1858 /* The number of entries in file_names, real_names. */
1859 unsigned int num_file_names
;
1861 /* The CU directory, as given by DW_AT_comp_dir. May be
1863 const char *comp_dir
;
1865 /* The file names from the line table, after being run through
1867 const char **file_names
;
1869 /* The file names from the line table after being run through
1870 gdb_realpath. These are computed lazily. */
1871 const char **real_names
;
1874 struct dwarf2_base_index_functions
: public quick_symbol_functions
1876 bool has_symbols (struct objfile
*objfile
) override
;
1878 bool has_unexpanded_symtabs (struct objfile
*objfile
) override
;
1880 struct symtab
*find_last_source_symtab (struct objfile
*objfile
) override
;
1882 void forget_cached_source_info (struct objfile
*objfile
) override
;
1884 enum language
lookup_global_symbol_language (struct objfile
*objfile
,
1887 bool *symbol_found_p
) override
1889 *symbol_found_p
= false;
1890 return language_unknown
;
1893 void print_stats (struct objfile
*objfile
, bool print_bcache
) override
;
1895 void expand_all_symtabs (struct objfile
*objfile
) override
;
1897 struct compunit_symtab
*find_pc_sect_compunit_symtab
1898 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
1899 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
) override
;
1901 struct compunit_symtab
*find_compunit_symtab_by_address
1902 (struct objfile
*objfile
, CORE_ADDR address
) override
1907 void map_symbol_filenames (struct objfile
*objfile
,
1908 gdb::function_view
<symbol_filename_ftype
> fun
,
1909 bool need_fullname
) override
;
1912 struct dwarf2_gdb_index
: public dwarf2_base_index_functions
1914 void dump (struct objfile
*objfile
) override
;
1916 void expand_matching_symbols
1918 const lookup_name_info
&lookup_name
,
1921 symbol_compare_ftype
*ordered_compare
) override
;
1923 bool expand_symtabs_matching
1924 (struct objfile
*objfile
,
1925 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1926 const lookup_name_info
*lookup_name
,
1927 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1928 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1929 block_search_flags search_flags
,
1931 enum search_domain kind
) override
;
1934 struct dwarf2_debug_names_index
: public dwarf2_base_index_functions
1936 void dump (struct objfile
*objfile
) override
;
1938 void expand_matching_symbols
1940 const lookup_name_info
&lookup_name
,
1943 symbol_compare_ftype
*ordered_compare
) override
;
1945 bool expand_symtabs_matching
1946 (struct objfile
*objfile
,
1947 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1948 const lookup_name_info
*lookup_name
,
1949 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1950 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1951 block_search_flags search_flags
,
1953 enum search_domain kind
) override
;
1956 static quick_symbol_functions_up
1957 make_dwarf_gdb_index ()
1959 return quick_symbol_functions_up (new dwarf2_gdb_index
);
1962 static quick_symbol_functions_up
1963 make_dwarf_debug_names ()
1965 return quick_symbol_functions_up (new dwarf2_debug_names_index
);
1968 /* Utility hash function for a stmt_list_hash. */
1971 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1975 if (stmt_list_hash
->dwo_unit
!= NULL
)
1976 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1977 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1981 /* Utility equality function for a stmt_list_hash. */
1984 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1985 const struct stmt_list_hash
*rhs
)
1987 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1989 if (lhs
->dwo_unit
!= NULL
1990 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1993 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1996 /* Hash function for a quick_file_names. */
1999 hash_file_name_entry (const void *e
)
2001 const struct quick_file_names
*file_data
2002 = (const struct quick_file_names
*) e
;
2004 return hash_stmt_list_entry (&file_data
->hash
);
2007 /* Equality function for a quick_file_names. */
2010 eq_file_name_entry (const void *a
, const void *b
)
2012 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
2013 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
2015 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
2018 /* Create a quick_file_names hash table. */
2021 create_quick_file_names_table (unsigned int nr_initial_entries
)
2023 return htab_up (htab_create_alloc (nr_initial_entries
,
2024 hash_file_name_entry
, eq_file_name_entry
,
2025 nullptr, xcalloc
, xfree
));
2028 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2029 function is unrelated to symtabs, symtab would have to be created afterwards.
2030 You should call age_cached_comp_units after processing the CU. */
2033 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
2036 if (per_cu
->is_debug_types
)
2037 load_full_type_unit (per_cu
, per_objfile
);
2039 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
2040 skip_partial
, language_minimal
);
2042 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
2044 return nullptr; /* Dummy CU. */
2046 dwarf2_find_base_address (cu
->dies
, cu
);
2051 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2054 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2055 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
2058 /* The destructor of dwarf2_queue_guard frees any entries left on
2059 the queue. After this point we're guaranteed to leave this function
2060 with the dwarf queue empty. */
2061 dwarf2_queue_guard
q_guard (per_objfile
);
2063 if (!per_objfile
->symtab_set_p (per_cu
))
2065 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
2066 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
2068 /* If we just loaded a CU from a DWO, and we're working with an index
2069 that may badly handle TUs, load all the TUs in that DWO as well.
2070 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2071 if (!per_cu
->is_debug_types
2073 && cu
->dwo_unit
!= NULL
2074 && per_objfile
->per_bfd
->index_table
!= NULL
2075 && per_objfile
->per_bfd
->index_table
->version
<= 7
2076 /* DWP files aren't supported yet. */
2077 && get_dwp_file (per_objfile
) == NULL
)
2078 queue_and_load_all_dwo_tus (cu
);
2081 process_queue (per_objfile
);
2084 /* Age the cache, releasing compilation units that have not
2085 been used recently. */
2086 per_objfile
->age_comp_units ();
2089 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2090 the per-objfile for which this symtab is instantiated.
2092 Returns the resulting symbol table. */
2094 static struct compunit_symtab
*
2095 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2096 dwarf2_per_objfile
*per_objfile
,
2099 if (!per_objfile
->symtab_set_p (per_cu
))
2101 free_cached_comp_units
freer (per_objfile
);
2102 scoped_restore decrementer
= increment_reading_symtab ();
2103 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
2104 process_cu_includes (per_objfile
);
2107 return per_objfile
->get_symtab (per_cu
);
2112 dwarf2_per_cu_data_up
2113 dwarf2_per_bfd::allocate_per_cu ()
2115 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
2116 result
->per_bfd
= this;
2117 result
->index
= all_comp_units
.size ();
2124 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
2126 signatured_type_up
result (new signatured_type (signature
));
2127 result
->per_bfd
= this;
2128 result
->index
= all_comp_units
.size ();
2129 result
->is_debug_types
= true;
2134 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2135 obstack, and constructed with the specified field values. */
2137 static dwarf2_per_cu_data_up
2138 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
2139 struct dwarf2_section_info
*section
,
2141 sect_offset sect_off
, ULONGEST length
)
2143 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
2144 the_cu
->sect_off
= sect_off
;
2145 the_cu
->length
= length
;
2146 the_cu
->section
= section
;
2147 the_cu
->is_dwz
= is_dwz
;
2151 /* A helper for create_cus_from_index that handles a given list of
2155 create_cus_from_index_list (dwarf2_per_bfd
*per_bfd
,
2156 const gdb_byte
*cu_list
, offset_type n_elements
,
2157 struct dwarf2_section_info
*section
,
2160 for (offset_type i
= 0; i
< n_elements
; i
+= 2)
2162 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2164 sect_offset sect_off
2165 = (sect_offset
) extract_unsigned_integer (cu_list
, 8, BFD_ENDIAN_LITTLE
);
2166 ULONGEST length
= extract_unsigned_integer (cu_list
+ 8, 8, BFD_ENDIAN_LITTLE
);
2169 dwarf2_per_cu_data_up per_cu
2170 = create_cu_from_index_list (per_bfd
, section
, is_dwz
, sect_off
,
2172 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
2176 /* Read the CU list from the mapped index, and use it to create all
2177 the CU objects for PER_BFD. */
2180 create_cus_from_index (dwarf2_per_bfd
*per_bfd
,
2181 const gdb_byte
*cu_list
, offset_type cu_list_elements
,
2182 const gdb_byte
*dwz_list
, offset_type dwz_elements
)
2184 gdb_assert (per_bfd
->all_comp_units
.empty ());
2185 per_bfd
->all_comp_units
.reserve ((cu_list_elements
+ dwz_elements
) / 2);
2187 create_cus_from_index_list (per_bfd
, cu_list
, cu_list_elements
,
2190 if (dwz_elements
== 0)
2193 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
2194 create_cus_from_index_list (per_bfd
, dwz_list
, dwz_elements
,
2198 /* Create the signatured type hash table from the index. */
2201 create_signatured_type_table_from_index
2202 (dwarf2_per_bfd
*per_bfd
, struct dwarf2_section_info
*section
,
2203 const gdb_byte
*bytes
, offset_type elements
)
2205 htab_up sig_types_hash
= allocate_signatured_type_table ();
2207 for (offset_type i
= 0; i
< elements
; i
+= 3)
2209 signatured_type_up sig_type
;
2212 cu_offset type_offset_in_tu
;
2214 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2215 sect_offset sect_off
2216 = (sect_offset
) extract_unsigned_integer (bytes
, 8, BFD_ENDIAN_LITTLE
);
2218 = (cu_offset
) extract_unsigned_integer (bytes
+ 8, 8,
2220 signature
= extract_unsigned_integer (bytes
+ 16, 8, BFD_ENDIAN_LITTLE
);
2223 sig_type
= per_bfd
->allocate_signatured_type (signature
);
2224 sig_type
->type_offset_in_tu
= type_offset_in_tu
;
2225 sig_type
->section
= section
;
2226 sig_type
->sect_off
= sect_off
;
2228 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2229 *slot
= sig_type
.get ();
2231 per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2234 per_bfd
->signatured_types
= std::move (sig_types_hash
);
2237 /* Create the signatured type hash table from .debug_names. */
2240 create_signatured_type_table_from_debug_names
2241 (dwarf2_per_objfile
*per_objfile
,
2242 const mapped_debug_names
&map
,
2243 struct dwarf2_section_info
*section
,
2244 struct dwarf2_section_info
*abbrev_section
)
2246 struct objfile
*objfile
= per_objfile
->objfile
;
2248 section
->read (objfile
);
2249 abbrev_section
->read (objfile
);
2251 htab_up sig_types_hash
= allocate_signatured_type_table ();
2253 for (uint32_t i
= 0; i
< map
.tu_count
; ++i
)
2255 signatured_type_up sig_type
;
2258 sect_offset sect_off
2259 = (sect_offset
) (extract_unsigned_integer
2260 (map
.tu_table_reordered
+ i
* map
.offset_size
,
2262 map
.dwarf5_byte_order
));
2264 comp_unit_head cu_header
;
2265 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
2267 section
->buffer
+ to_underlying (sect_off
),
2270 sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
2271 (cu_header
.signature
);
2272 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
2273 sig_type
->section
= section
;
2274 sig_type
->sect_off
= sect_off
;
2276 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2277 *slot
= sig_type
.get ();
2279 per_objfile
->per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2282 per_objfile
->per_bfd
->signatured_types
= std::move (sig_types_hash
);
2285 /* Read the address map data from the mapped index, and use it to
2286 populate the psymtabs_addrmap. */
2289 create_addrmap_from_index (dwarf2_per_objfile
*per_objfile
,
2290 struct mapped_index
*index
)
2292 struct objfile
*objfile
= per_objfile
->objfile
;
2293 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2294 struct gdbarch
*gdbarch
= objfile
->arch ();
2295 const gdb_byte
*iter
, *end
;
2296 struct addrmap
*mutable_map
;
2299 auto_obstack temp_obstack
;
2301 mutable_map
= addrmap_create_mutable (&temp_obstack
);
2303 iter
= index
->address_table
.data ();
2304 end
= iter
+ index
->address_table
.size ();
2306 baseaddr
= objfile
->text_section_offset ();
2310 ULONGEST hi
, lo
, cu_index
;
2311 lo
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2313 hi
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2315 cu_index
= extract_unsigned_integer (iter
, 4, BFD_ENDIAN_LITTLE
);
2320 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2321 hex_string (lo
), hex_string (hi
));
2325 if (cu_index
>= per_bfd
->all_comp_units
.size ())
2327 complaint (_(".gdb_index address table has invalid CU number %u"),
2328 (unsigned) cu_index
);
2332 lo
= gdbarch_adjust_dwarf2_addr (gdbarch
, lo
+ baseaddr
) - baseaddr
;
2333 hi
= gdbarch_adjust_dwarf2_addr (gdbarch
, hi
+ baseaddr
) - baseaddr
;
2334 addrmap_set_empty (mutable_map
, lo
, hi
- 1,
2335 per_bfd
->get_cu (cu_index
));
2338 per_bfd
->index_addrmap
= addrmap_create_fixed (mutable_map
,
2342 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2343 to populate given addrmap. Returns true on success, false on
2347 read_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2348 struct dwarf2_section_info
*section
,
2349 addrmap
*mutable_map
)
2351 struct objfile
*objfile
= per_objfile
->objfile
;
2352 bfd
*abfd
= objfile
->obfd
;
2353 struct gdbarch
*gdbarch
= objfile
->arch ();
2354 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
2355 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2357 std::unordered_map
<sect_offset
,
2358 dwarf2_per_cu_data
*,
2359 gdb::hash_enum
<sect_offset
>>
2360 debug_info_offset_to_per_cu
;
2361 for (const auto &per_cu
: per_bfd
->all_comp_units
)
2363 /* A TU will not need aranges, and skipping them here is an easy
2364 way of ignoring .debug_types -- and possibly seeing a
2365 duplicate section offset -- entirely. The same applies to
2366 units coming from a dwz file. */
2367 if (per_cu
->is_debug_types
|| per_cu
->is_dwz
)
2370 const auto insertpair
2371 = debug_info_offset_to_per_cu
.emplace (per_cu
->sect_off
,
2373 if (!insertpair
.second
)
2375 warning (_("Section .debug_aranges in %s has duplicate "
2376 "debug_info_offset %s, ignoring .debug_aranges."),
2377 objfile_name (objfile
), sect_offset_str (per_cu
->sect_off
));
2382 section
->read (objfile
);
2384 const bfd_endian dwarf5_byte_order
= gdbarch_byte_order (gdbarch
);
2386 const gdb_byte
*addr
= section
->buffer
;
2388 while (addr
< section
->buffer
+ section
->size
)
2390 const gdb_byte
*const entry_addr
= addr
;
2391 unsigned int bytes_read
;
2393 const LONGEST entry_length
= read_initial_length (abfd
, addr
,
2397 const gdb_byte
*const entry_end
= addr
+ entry_length
;
2398 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
2399 const uint8_t offset_size
= dwarf5_is_dwarf64
? 8 : 4;
2400 if (addr
+ entry_length
> section
->buffer
+ section
->size
)
2402 warning (_("Section .debug_aranges in %s entry at offset %s "
2403 "length %s exceeds section length %s, "
2404 "ignoring .debug_aranges."),
2405 objfile_name (objfile
),
2406 plongest (entry_addr
- section
->buffer
),
2407 plongest (bytes_read
+ entry_length
),
2408 pulongest (section
->size
));
2412 /* The version number. */
2413 const uint16_t version
= read_2_bytes (abfd
, addr
);
2417 warning (_("Section .debug_aranges in %s entry at offset %s "
2418 "has unsupported version %d, ignoring .debug_aranges."),
2419 objfile_name (objfile
),
2420 plongest (entry_addr
- section
->buffer
), version
);
2424 const uint64_t debug_info_offset
2425 = extract_unsigned_integer (addr
, offset_size
, dwarf5_byte_order
);
2426 addr
+= offset_size
;
2427 const auto per_cu_it
2428 = debug_info_offset_to_per_cu
.find (sect_offset (debug_info_offset
));
2429 if (per_cu_it
== debug_info_offset_to_per_cu
.cend ())
2431 warning (_("Section .debug_aranges in %s entry at offset %s "
2432 "debug_info_offset %s does not exists, "
2433 "ignoring .debug_aranges."),
2434 objfile_name (objfile
),
2435 plongest (entry_addr
- section
->buffer
),
2436 pulongest (debug_info_offset
));
2439 dwarf2_per_cu_data
*const per_cu
= per_cu_it
->second
;
2441 const uint8_t address_size
= *addr
++;
2442 if (address_size
< 1 || address_size
> 8)
2444 warning (_("Section .debug_aranges in %s entry at offset %s "
2445 "address_size %u is invalid, ignoring .debug_aranges."),
2446 objfile_name (objfile
),
2447 plongest (entry_addr
- section
->buffer
), address_size
);
2451 const uint8_t segment_selector_size
= *addr
++;
2452 if (segment_selector_size
!= 0)
2454 warning (_("Section .debug_aranges in %s entry at offset %s "
2455 "segment_selector_size %u is not supported, "
2456 "ignoring .debug_aranges."),
2457 objfile_name (objfile
),
2458 plongest (entry_addr
- section
->buffer
),
2459 segment_selector_size
);
2463 /* Must pad to an alignment boundary that is twice the address
2464 size. It is undocumented by the DWARF standard but GCC does
2465 use it. However, not every compiler does this. We can see
2466 whether it has happened by looking at the total length of the
2467 contents of the aranges for this CU -- it if isn't a multiple
2468 of twice the address size, then we skip any leftover
2470 addr
+= (entry_end
- addr
) % (2 * address_size
);
2474 if (addr
+ 2 * address_size
> entry_end
)
2476 warning (_("Section .debug_aranges in %s entry at offset %s "
2477 "address list is not properly terminated, "
2478 "ignoring .debug_aranges."),
2479 objfile_name (objfile
),
2480 plongest (entry_addr
- section
->buffer
));
2483 ULONGEST start
= extract_unsigned_integer (addr
, address_size
,
2485 addr
+= address_size
;
2486 ULONGEST length
= extract_unsigned_integer (addr
, address_size
,
2488 addr
+= address_size
;
2489 if (start
== 0 && length
== 0)
2491 if (start
== 0 && !per_bfd
->has_section_at_zero
)
2493 /* Symbol was eliminated due to a COMDAT group. */
2496 ULONGEST end
= start
+ length
;
2497 start
= (gdbarch_adjust_dwarf2_addr (gdbarch
, start
+ baseaddr
)
2499 end
= (gdbarch_adjust_dwarf2_addr (gdbarch
, end
+ baseaddr
)
2501 addrmap_set_empty (mutable_map
, start
, end
- 1, per_cu
);
2504 per_cu
->addresses_seen
= true;
2510 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2511 populate the psymtabs_addrmap. */
2514 create_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2515 struct dwarf2_section_info
*section
)
2517 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2519 auto_obstack temp_obstack
;
2520 addrmap
*mutable_map
= addrmap_create_mutable (&temp_obstack
);
2522 if (read_addrmap_from_aranges (per_objfile
, section
, mutable_map
))
2523 per_bfd
->index_addrmap
= addrmap_create_fixed (mutable_map
,
2527 /* A helper function that reads the .gdb_index from BUFFER and fills
2528 in MAP. FILENAME is the name of the file containing the data;
2529 it is used for error reporting. DEPRECATED_OK is true if it is
2530 ok to use deprecated sections.
2532 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2533 out parameters that are filled in with information about the CU and
2534 TU lists in the section.
2536 Returns true if all went well, false otherwise. */
2539 read_gdb_index_from_buffer (const char *filename
,
2541 gdb::array_view
<const gdb_byte
> buffer
,
2542 struct mapped_index
*map
,
2543 const gdb_byte
**cu_list
,
2544 offset_type
*cu_list_elements
,
2545 const gdb_byte
**types_list
,
2546 offset_type
*types_list_elements
)
2548 const gdb_byte
*addr
= &buffer
[0];
2549 offset_view
metadata (buffer
);
2551 /* Version check. */
2552 offset_type version
= metadata
[0];
2553 /* Versions earlier than 3 emitted every copy of a psymbol. This
2554 causes the index to behave very poorly for certain requests. Version 3
2555 contained incomplete addrmap. So, it seems better to just ignore such
2559 static int warning_printed
= 0;
2560 if (!warning_printed
)
2562 warning (_("Skipping obsolete .gdb_index section in %s."),
2564 warning_printed
= 1;
2568 /* Index version 4 uses a different hash function than index version
2571 Versions earlier than 6 did not emit psymbols for inlined
2572 functions. Using these files will cause GDB not to be able to
2573 set breakpoints on inlined functions by name, so we ignore these
2574 indices unless the user has done
2575 "set use-deprecated-index-sections on". */
2576 if (version
< 6 && !deprecated_ok
)
2578 static int warning_printed
= 0;
2579 if (!warning_printed
)
2582 Skipping deprecated .gdb_index section in %s.\n\
2583 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2584 to use the section anyway."),
2586 warning_printed
= 1;
2590 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2591 of the TU (for symbols coming from TUs),
2592 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2593 Plus gold-generated indices can have duplicate entries for global symbols,
2594 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2595 These are just performance bugs, and we can't distinguish gdb-generated
2596 indices from gold-generated ones, so issue no warning here. */
2598 /* Indexes with higher version than the one supported by GDB may be no
2599 longer backward compatible. */
2603 map
->version
= version
;
2606 *cu_list
= addr
+ metadata
[i
];
2607 *cu_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2610 *types_list
= addr
+ metadata
[i
];
2611 *types_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2614 const gdb_byte
*address_table
= addr
+ metadata
[i
];
2615 const gdb_byte
*address_table_end
= addr
+ metadata
[i
+ 1];
2617 = gdb::array_view
<const gdb_byte
> (address_table
, address_table_end
);
2620 const gdb_byte
*symbol_table
= addr
+ metadata
[i
];
2621 const gdb_byte
*symbol_table_end
= addr
+ metadata
[i
+ 1];
2623 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2627 map
->constant_pool
= buffer
.slice (metadata
[i
]);
2629 if (map
->constant_pool
.empty () && !map
->symbol_table
.empty ())
2631 /* An empty constant pool implies that all symbol table entries are
2632 empty. Make map->symbol_table.empty () == true. */
2634 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2641 /* Callback types for dwarf2_read_gdb_index. */
2643 typedef gdb::function_view
2644 <gdb::array_view
<const gdb_byte
>(objfile
*, dwarf2_per_bfd
*)>
2645 get_gdb_index_contents_ftype
;
2646 typedef gdb::function_view
2647 <gdb::array_view
<const gdb_byte
>(objfile
*, dwz_file
*)>
2648 get_gdb_index_contents_dwz_ftype
;
2650 /* Read .gdb_index. If everything went ok, initialize the "quick"
2651 elements of all the CUs and return 1. Otherwise, return 0. */
2654 dwarf2_read_gdb_index
2655 (dwarf2_per_objfile
*per_objfile
,
2656 get_gdb_index_contents_ftype get_gdb_index_contents
,
2657 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz
)
2659 const gdb_byte
*cu_list
, *types_list
, *dwz_list
= NULL
;
2660 offset_type cu_list_elements
, types_list_elements
, dwz_list_elements
= 0;
2661 struct dwz_file
*dwz
;
2662 struct objfile
*objfile
= per_objfile
->objfile
;
2663 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2665 gdb::array_view
<const gdb_byte
> main_index_contents
2666 = get_gdb_index_contents (objfile
, per_bfd
);
2668 if (main_index_contents
.empty ())
2671 std::unique_ptr
<struct mapped_index
> map (new struct mapped_index
);
2672 if (!read_gdb_index_from_buffer (objfile_name (objfile
),
2673 use_deprecated_index_sections
,
2674 main_index_contents
, map
.get (), &cu_list
,
2675 &cu_list_elements
, &types_list
,
2676 &types_list_elements
))
2679 /* Don't use the index if it's empty. */
2680 if (map
->symbol_table
.empty ())
2683 /* If there is a .dwz file, read it so we can get its CU list as
2685 dwz
= dwarf2_get_dwz_file (per_bfd
);
2688 struct mapped_index dwz_map
;
2689 const gdb_byte
*dwz_types_ignore
;
2690 offset_type dwz_types_elements_ignore
;
2692 gdb::array_view
<const gdb_byte
> dwz_index_content
2693 = get_gdb_index_contents_dwz (objfile
, dwz
);
2695 if (dwz_index_content
.empty ())
2698 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz
->dwz_bfd
.get ()),
2699 1, dwz_index_content
, &dwz_map
,
2700 &dwz_list
, &dwz_list_elements
,
2702 &dwz_types_elements_ignore
))
2704 warning (_("could not read '.gdb_index' section from %s; skipping"),
2705 bfd_get_filename (dwz
->dwz_bfd
.get ()));
2710 create_cus_from_index (per_bfd
, cu_list
, cu_list_elements
, dwz_list
,
2713 if (types_list_elements
)
2715 /* We can only handle a single .debug_types when we have an
2717 if (per_bfd
->types
.size () != 1)
2720 dwarf2_section_info
*section
= &per_bfd
->types
[0];
2722 create_signatured_type_table_from_index (per_bfd
, section
, types_list
,
2723 types_list_elements
);
2726 create_addrmap_from_index (per_objfile
, map
.get ());
2728 per_bfd
->index_table
= std::move (map
);
2729 per_bfd
->quick_file_names_table
=
2730 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
2735 /* die_reader_func for dw2_get_file_names. */
2738 dw2_get_file_names_reader (const struct die_reader_specs
*reader
,
2739 struct die_info
*comp_unit_die
)
2741 struct dwarf2_cu
*cu
= reader
->cu
;
2742 struct dwarf2_per_cu_data
*this_cu
= cu
->per_cu
;
2743 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
2744 struct dwarf2_per_cu_data
*lh_cu
;
2745 struct attribute
*attr
;
2747 struct quick_file_names
*qfn
;
2749 gdb_assert (! this_cu
->is_debug_types
);
2751 this_cu
->files_read
= true;
2752 /* Our callers never want to match partial units -- instead they
2753 will match the enclosing full CU. */
2754 if (comp_unit_die
->tag
== DW_TAG_partial_unit
)
2761 sect_offset line_offset
{};
2763 attr
= dwarf2_attr (comp_unit_die
, DW_AT_stmt_list
, cu
);
2764 if (attr
!= nullptr && attr
->form_is_unsigned ())
2766 struct quick_file_names find_entry
;
2768 line_offset
= (sect_offset
) attr
->as_unsigned ();
2770 /* We may have already read in this line header (TU line header sharing).
2771 If we have we're done. */
2772 find_entry
.hash
.dwo_unit
= cu
->dwo_unit
;
2773 find_entry
.hash
.line_sect_off
= line_offset
;
2774 slot
= htab_find_slot (per_objfile
->per_bfd
->quick_file_names_table
.get (),
2775 &find_entry
, INSERT
);
2778 lh_cu
->file_names
= (struct quick_file_names
*) *slot
;
2782 lh
= dwarf_decode_line_header (line_offset
, cu
);
2785 file_and_directory
&fnd
= find_file_and_directory (comp_unit_die
, cu
);
2788 if (!fnd
.is_unknown ())
2790 else if (lh
== nullptr)
2793 qfn
= XOBNEW (&per_objfile
->per_bfd
->obstack
, struct quick_file_names
);
2794 qfn
->hash
.dwo_unit
= cu
->dwo_unit
;
2795 qfn
->hash
.line_sect_off
= line_offset
;
2796 /* There may not be a DW_AT_stmt_list. */
2797 if (slot
!= nullptr)
2800 std::vector
<const char *> include_names
;
2803 for (const auto &entry
: lh
->file_names ())
2805 gdb::unique_xmalloc_ptr
<char> name_holder
;
2806 const char *include_name
=
2807 compute_include_file_name (lh
.get (), entry
, fnd
, &name_holder
);
2808 if (include_name
!= nullptr)
2810 include_name
= per_objfile
->objfile
->intern (include_name
);
2811 include_names
.push_back (include_name
);
2816 qfn
->num_file_names
= offset
+ include_names
.size ();
2817 qfn
->comp_dir
= fnd
.intern_comp_dir (per_objfile
->objfile
);
2819 XOBNEWVEC (&per_objfile
->per_bfd
->obstack
, const char *,
2820 qfn
->num_file_names
);
2822 qfn
->file_names
[0] = xstrdup (fnd
.get_name ());
2824 if (!include_names
.empty ())
2825 memcpy (&qfn
->file_names
[offset
], include_names
.data (),
2826 include_names
.size () * sizeof (const char *));
2828 qfn
->real_names
= NULL
;
2830 lh_cu
->file_names
= qfn
;
2833 /* A helper for the "quick" functions which attempts to read the line
2834 table for THIS_CU. */
2836 static struct quick_file_names
*
2837 dw2_get_file_names (dwarf2_per_cu_data
*this_cu
,
2838 dwarf2_per_objfile
*per_objfile
)
2840 /* This should never be called for TUs. */
2841 gdb_assert (! this_cu
->is_debug_types
);
2843 if (this_cu
->files_read
)
2844 return this_cu
->file_names
;
2846 cutu_reader
reader (this_cu
, per_objfile
);
2847 if (!reader
.dummy_p
)
2848 dw2_get_file_names_reader (&reader
, reader
.comp_unit_die
);
2850 return this_cu
->file_names
;
2853 /* A helper for the "quick" functions which computes and caches the
2854 real path for a given file name from the line table. */
2857 dw2_get_real_path (dwarf2_per_objfile
*per_objfile
,
2858 struct quick_file_names
*qfn
, int index
)
2860 if (qfn
->real_names
== NULL
)
2861 qfn
->real_names
= OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
2862 qfn
->num_file_names
, const char *);
2864 if (qfn
->real_names
[index
] == NULL
)
2866 const char *dirname
= nullptr;
2868 if (!IS_ABSOLUTE_PATH (qfn
->file_names
[index
]))
2869 dirname
= qfn
->comp_dir
;
2871 gdb::unique_xmalloc_ptr
<char> fullname
;
2872 fullname
= find_source_or_rewrite (qfn
->file_names
[index
], dirname
);
2874 qfn
->real_names
[index
] = fullname
.release ();
2877 return qfn
->real_names
[index
];
2881 dwarf2_base_index_functions::find_last_source_symtab (struct objfile
*objfile
)
2883 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2884 dwarf2_per_cu_data
*dwarf_cu
2885 = per_objfile
->per_bfd
->all_comp_units
.back ().get ();
2886 compunit_symtab
*cust
= dw2_instantiate_symtab (dwarf_cu
, per_objfile
, false);
2891 return cust
->primary_filetab ();
2897 dwarf2_per_cu_data::free_cached_file_names ()
2900 fnd
->forget_fullname ();
2902 if (per_bfd
== nullptr)
2905 struct quick_file_names
*file_data
= file_names
;
2906 if (file_data
!= nullptr && file_data
->real_names
!= nullptr)
2908 for (int i
= 0; i
< file_data
->num_file_names
; ++i
)
2910 xfree ((void *) file_data
->real_names
[i
]);
2911 file_data
->real_names
[i
] = nullptr;
2917 dwarf2_base_index_functions::forget_cached_source_info
2918 (struct objfile
*objfile
)
2920 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2922 for (auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
2923 per_cu
->free_cached_file_names ();
2926 /* Struct used to manage iterating over all CUs looking for a symbol. */
2928 struct dw2_symtab_iterator
2930 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2931 dwarf2_per_objfile
*per_objfile
;
2932 /* If set, only look for symbols that match that block. Valid values are
2933 GLOBAL_BLOCK and STATIC_BLOCK. */
2934 gdb::optional
<block_enum
> block_index
;
2935 /* The kind of symbol we're looking for. */
2937 /* The list of CUs from the index entry of the symbol,
2938 or NULL if not found. */
2940 /* The next element in VEC to look at. */
2942 /* The number of elements in VEC, or zero if there is no match. */
2944 /* Have we seen a global version of the symbol?
2945 If so we can ignore all further global instances.
2946 This is to work around gold/15646, inefficient gold-generated
2951 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2954 dw2_symtab_iter_init (struct dw2_symtab_iterator
*iter
,
2955 dwarf2_per_objfile
*per_objfile
,
2956 gdb::optional
<block_enum
> block_index
,
2957 domain_enum domain
, offset_type namei
)
2959 iter
->per_objfile
= per_objfile
;
2960 iter
->block_index
= block_index
;
2961 iter
->domain
= domain
;
2963 iter
->global_seen
= 0;
2967 mapped_index
*index
= per_objfile
->per_bfd
->index_table
.get ();
2968 /* index is NULL if OBJF_READNOW. */
2972 gdb_assert (!index
->symbol_name_slot_invalid (namei
));
2973 offset_type vec_idx
= index
->symbol_vec_index (namei
);
2975 iter
->vec
= offset_view (index
->constant_pool
.slice (vec_idx
));
2976 iter
->length
= iter
->vec
[0];
2979 /* Return the next matching CU or NULL if there are no more. */
2981 static struct dwarf2_per_cu_data
*
2982 dw2_symtab_iter_next (struct dw2_symtab_iterator
*iter
)
2984 dwarf2_per_objfile
*per_objfile
= iter
->per_objfile
;
2986 for ( ; iter
->next
< iter
->length
; ++iter
->next
)
2988 offset_type cu_index_and_attrs
= iter
->vec
[iter
->next
+ 1];
2989 offset_type cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
2990 gdb_index_symbol_kind symbol_kind
=
2991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
2992 /* Only check the symbol attributes if they're present.
2993 Indices prior to version 7 don't record them,
2994 and indices >= 7 may elide them for certain symbols
2995 (gold does this). */
2997 (per_objfile
->per_bfd
->index_table
->version
>= 7
2998 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
3000 /* Don't crash on bad data. */
3001 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
3003 complaint (_(".gdb_index entry has bad CU index"
3004 " [in module %s]"), objfile_name (per_objfile
->objfile
));
3008 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
3010 /* Skip if already read in. */
3011 if (per_objfile
->symtab_set_p (per_cu
))
3014 /* Check static vs global. */
3017 bool is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
3019 if (iter
->block_index
.has_value ())
3021 bool want_static
= *iter
->block_index
== STATIC_BLOCK
;
3023 if (is_static
!= want_static
)
3027 /* Work around gold/15646. */
3029 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
3031 if (iter
->global_seen
)
3034 iter
->global_seen
= 1;
3038 /* Only check the symbol's kind if it has one. */
3041 switch (iter
->domain
)
3044 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
3045 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
3046 /* Some types are also in VAR_DOMAIN. */
3047 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3051 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3055 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3059 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3075 dwarf2_base_index_functions::print_stats (struct objfile
*objfile
,
3081 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3082 int total
= per_objfile
->per_bfd
->all_comp_units
.size ();
3085 for (int i
= 0; i
< total
; ++i
)
3087 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3089 if (!per_objfile
->symtab_set_p (per_cu
))
3092 gdb_printf (_(" Number of read CUs: %d\n"), total
- count
);
3093 gdb_printf (_(" Number of unread CUs: %d\n"), count
);
3096 /* This dumps minimal information about the index.
3097 It is called via "mt print objfiles".
3098 One use is to verify .gdb_index has been loaded by the
3099 gdb.dwarf2/gdb-index.exp testcase. */
3102 dwarf2_gdb_index::dump (struct objfile
*objfile
)
3104 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3106 gdb_printf (".gdb_index:");
3107 if (per_objfile
->per_bfd
->index_table
!= NULL
)
3109 gdb_printf (" version %d\n",
3110 per_objfile
->per_bfd
->index_table
->version
);
3113 gdb_printf (" faked for \"readnow\"\n");
3118 dwarf2_base_index_functions::expand_all_symtabs (struct objfile
*objfile
)
3120 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3121 int total_units
= per_objfile
->per_bfd
->all_comp_units
.size ();
3123 for (int i
= 0; i
< total_units
; ++i
)
3125 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3127 /* We don't want to directly expand a partial CU, because if we
3128 read it with the wrong language, then assertion failures can
3129 be triggered later on. See PR symtab/23010. So, tell
3130 dw2_instantiate_symtab to skip partial CUs -- any important
3131 partial CU will be read via DW_TAG_imported_unit anyway. */
3132 dw2_instantiate_symtab (per_cu
, per_objfile
, true);
3137 dw2_expand_symtabs_matching_symbol
3138 (mapped_index_base
&index
,
3139 const lookup_name_info
&lookup_name_in
,
3140 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3141 gdb::function_view
<bool (offset_type
)> match_callback
,
3142 dwarf2_per_objfile
*per_objfile
);
3145 dw2_expand_symtabs_matching_one
3146 (dwarf2_per_cu_data
*per_cu
,
3147 dwarf2_per_objfile
*per_objfile
,
3148 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3149 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
);
3152 dwarf2_gdb_index::expand_matching_symbols
3153 (struct objfile
*objfile
,
3154 const lookup_name_info
&name
, domain_enum domain
,
3156 symbol_compare_ftype
*ordered_compare
)
3159 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3161 const block_enum block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
3163 if (per_objfile
->per_bfd
->index_table
!= nullptr)
3165 mapped_index
&index
= *per_objfile
->per_bfd
->index_table
;
3167 const char *match_name
= name
.ada ().lookup_name ().c_str ();
3168 auto matcher
= [&] (const char *symname
)
3170 if (ordered_compare
== nullptr)
3172 return ordered_compare (symname
, match_name
) == 0;
3175 dw2_expand_symtabs_matching_symbol (index
, name
, matcher
,
3176 [&] (offset_type namei
)
3178 struct dw2_symtab_iterator iter
;
3179 struct dwarf2_per_cu_data
*per_cu
;
3181 dw2_symtab_iter_init (&iter
, per_objfile
, block_kind
, domain
,
3183 while ((per_cu
= dw2_symtab_iter_next (&iter
)) != NULL
)
3184 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
3191 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3192 proceed assuming all symtabs have been read in. */
3196 /* Starting from a search name, return the string that finds the upper
3197 bound of all strings that start with SEARCH_NAME in a sorted name
3198 list. Returns the empty string to indicate that the upper bound is
3199 the end of the list. */
3202 make_sort_after_prefix_name (const char *search_name
)
3204 /* When looking to complete "func", we find the upper bound of all
3205 symbols that start with "func" by looking for where we'd insert
3206 the closest string that would follow "func" in lexicographical
3207 order. Usually, that's "func"-with-last-character-incremented,
3208 i.e. "fund". Mind non-ASCII characters, though. Usually those
3209 will be UTF-8 multi-byte sequences, but we can't be certain.
3210 Especially mind the 0xff character, which is a valid character in
3211 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3212 rule out compilers allowing it in identifiers. Note that
3213 conveniently, strcmp/strcasecmp are specified to compare
3214 characters interpreted as unsigned char. So what we do is treat
3215 the whole string as a base 256 number composed of a sequence of
3216 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3217 to 0, and carries 1 to the following more-significant position.
3218 If the very first character in SEARCH_NAME ends up incremented
3219 and carries/overflows, then the upper bound is the end of the
3220 list. The string after the empty string is also the empty
3223 Some examples of this operation:
3225 SEARCH_NAME => "+1" RESULT
3229 "\xff" "a" "\xff" => "\xff" "b"
3234 Then, with these symbols for example:
3240 completing "func" looks for symbols between "func" and
3241 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3242 which finds "func" and "func1", but not "fund".
3246 funcÿ (Latin1 'ÿ' [0xff])
3250 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3251 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3255 ÿÿ (Latin1 'ÿ' [0xff])
3258 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3259 the end of the list.
3261 std::string after
= search_name
;
3262 while (!after
.empty () && (unsigned char) after
.back () == 0xff)
3264 if (!after
.empty ())
3265 after
.back () = (unsigned char) after
.back () + 1;
3269 /* See declaration. */
3271 std::pair
<std::vector
<name_component
>::const_iterator
,
3272 std::vector
<name_component
>::const_iterator
>
3273 mapped_index_base::find_name_components_bounds
3274 (const lookup_name_info
&lookup_name_without_params
, language lang
,
3275 dwarf2_per_objfile
*per_objfile
) const
3278 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3280 const char *lang_name
3281 = lookup_name_without_params
.language_lookup_name (lang
);
3283 /* Comparison function object for lower_bound that matches against a
3284 given symbol name. */
3285 auto lookup_compare_lower
= [&] (const name_component
&elem
,
3288 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3289 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3290 return name_cmp (elem_name
, name
) < 0;
3293 /* Comparison function object for upper_bound that matches against a
3294 given symbol name. */
3295 auto lookup_compare_upper
= [&] (const char *name
,
3296 const name_component
&elem
)
3298 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3299 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3300 return name_cmp (name
, elem_name
) < 0;
3303 auto begin
= this->name_components
.begin ();
3304 auto end
= this->name_components
.end ();
3306 /* Find the lower bound. */
3309 if (lookup_name_without_params
.completion_mode () && lang_name
[0] == '\0')
3312 return std::lower_bound (begin
, end
, lang_name
, lookup_compare_lower
);
3315 /* Find the upper bound. */
3318 if (lookup_name_without_params
.completion_mode ())
3320 /* In completion mode, we want UPPER to point past all
3321 symbols names that have the same prefix. I.e., with
3322 these symbols, and completing "func":
3324 function << lower bound
3326 other_function << upper bound
3328 We find the upper bound by looking for the insertion
3329 point of "func"-with-last-character-incremented,
3331 std::string after
= make_sort_after_prefix_name (lang_name
);
3334 return std::lower_bound (lower
, end
, after
.c_str (),
3335 lookup_compare_lower
);
3338 return std::upper_bound (lower
, end
, lang_name
, lookup_compare_upper
);
3341 return {lower
, upper
};
3344 /* See declaration. */
3347 mapped_index_base::build_name_components (dwarf2_per_objfile
*per_objfile
)
3349 if (!this->name_components
.empty ())
3352 this->name_components_casing
= case_sensitivity
;
3354 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3356 /* The code below only knows how to break apart components of C++
3357 symbol names (and other languages that use '::' as
3358 namespace/module separator) and Ada symbol names. */
3359 auto count
= this->symbol_name_count ();
3360 for (offset_type idx
= 0; idx
< count
; idx
++)
3362 if (this->symbol_name_slot_invalid (idx
))
3365 const char *name
= this->symbol_name_at (idx
, per_objfile
);
3367 /* Add each name component to the name component table. */
3368 unsigned int previous_len
= 0;
3370 if (strstr (name
, "::") != nullptr)
3372 for (unsigned int current_len
= cp_find_first_component (name
);
3373 name
[current_len
] != '\0';
3374 current_len
+= cp_find_first_component (name
+ current_len
))
3376 gdb_assert (name
[current_len
] == ':');
3377 this->name_components
.push_back ({previous_len
, idx
});
3378 /* Skip the '::'. */
3380 previous_len
= current_len
;
3385 /* Handle the Ada encoded (aka mangled) form here. */
3386 for (const char *iter
= strstr (name
, "__");
3388 iter
= strstr (iter
, "__"))
3390 this->name_components
.push_back ({previous_len
, idx
});
3392 previous_len
= iter
- name
;
3396 this->name_components
.push_back ({previous_len
, idx
});
3399 /* Sort name_components elements by name. */
3400 auto name_comp_compare
= [&] (const name_component
&left
,
3401 const name_component
&right
)
3403 const char *left_qualified
3404 = this->symbol_name_at (left
.idx
, per_objfile
);
3405 const char *right_qualified
3406 = this->symbol_name_at (right
.idx
, per_objfile
);
3408 const char *left_name
= left_qualified
+ left
.name_offset
;
3409 const char *right_name
= right_qualified
+ right
.name_offset
;
3411 return name_cmp (left_name
, right_name
) < 0;
3414 std::sort (this->name_components
.begin (),
3415 this->name_components
.end (),
3419 /* Helper for dw2_expand_symtabs_matching that works with a
3420 mapped_index_base instead of the containing objfile. This is split
3421 to a separate function in order to be able to unit test the
3422 name_components matching using a mock mapped_index_base. For each
3423 symbol name that matches, calls MATCH_CALLBACK, passing it the
3424 symbol's index in the mapped_index_base symbol table. */
3427 dw2_expand_symtabs_matching_symbol
3428 (mapped_index_base
&index
,
3429 const lookup_name_info
&lookup_name_in
,
3430 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3431 gdb::function_view
<bool (offset_type
)> match_callback
,
3432 dwarf2_per_objfile
*per_objfile
)
3434 lookup_name_info lookup_name_without_params
3435 = lookup_name_in
.make_ignore_params ();
3437 /* Build the symbol name component sorted vector, if we haven't
3439 index
.build_name_components (per_objfile
);
3441 /* The same symbol may appear more than once in the range though.
3442 E.g., if we're looking for symbols that complete "w", and we have
3443 a symbol named "w1::w2", we'll find the two name components for
3444 that same symbol in the range. To be sure we only call the
3445 callback once per symbol, we first collect the symbol name
3446 indexes that matched in a temporary vector and ignore
3448 std::vector
<offset_type
> matches
;
3450 struct name_and_matcher
3452 symbol_name_matcher_ftype
*matcher
;
3455 bool operator== (const name_and_matcher
&other
) const
3457 return matcher
== other
.matcher
&& strcmp (name
, other
.name
) == 0;
3461 /* A vector holding all the different symbol name matchers, for all
3463 std::vector
<name_and_matcher
> matchers
;
3465 for (int i
= 0; i
< nr_languages
; i
++)
3467 enum language lang_e
= (enum language
) i
;
3469 const language_defn
*lang
= language_def (lang_e
);
3470 symbol_name_matcher_ftype
*name_matcher
3471 = lang
->get_symbol_name_matcher (lookup_name_without_params
);
3473 name_and_matcher key
{
3475 lookup_name_without_params
.language_lookup_name (lang_e
)
3478 /* Don't insert the same comparison routine more than once.
3479 Note that we do this linear walk. This is not a problem in
3480 practice because the number of supported languages is
3482 if (std::find (matchers
.begin (), matchers
.end (), key
)
3485 matchers
.push_back (std::move (key
));
3488 = index
.find_name_components_bounds (lookup_name_without_params
,
3489 lang_e
, per_objfile
);
3491 /* Now for each symbol name in range, check to see if we have a name
3492 match, and if so, call the MATCH_CALLBACK callback. */
3494 for (; bounds
.first
!= bounds
.second
; ++bounds
.first
)
3496 const char *qualified
3497 = index
.symbol_name_at (bounds
.first
->idx
, per_objfile
);
3499 if (!name_matcher (qualified
, lookup_name_without_params
, NULL
)
3500 || (symbol_matcher
!= NULL
&& !symbol_matcher (qualified
)))
3503 matches
.push_back (bounds
.first
->idx
);
3507 std::sort (matches
.begin (), matches
.end ());
3509 /* Finally call the callback, once per match. */
3512 for (offset_type idx
: matches
)
3516 if (!match_callback (idx
))
3525 /* Above we use a type wider than idx's for 'prev', since 0 and
3526 (offset_type)-1 are both possible values. */
3527 static_assert (sizeof (prev
) > sizeof (offset_type
), "");
3534 namespace selftests
{ namespace dw2_expand_symtabs_matching
{
3536 /* A mock .gdb_index/.debug_names-like name index table, enough to
3537 exercise dw2_expand_symtabs_matching_symbol, which works with the
3538 mapped_index_base interface. Builds an index from the symbol list
3539 passed as parameter to the constructor. */
3540 class mock_mapped_index
: public mapped_index_base
3543 mock_mapped_index (gdb::array_view
<const char *> symbols
)
3544 : m_symbol_table (symbols
)
3547 DISABLE_COPY_AND_ASSIGN (mock_mapped_index
);
3549 /* Return the number of names in the symbol table. */
3550 size_t symbol_name_count () const override
3552 return m_symbol_table
.size ();
3555 /* Get the name of the symbol at IDX in the symbol table. */
3556 const char *symbol_name_at
3557 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
3559 return m_symbol_table
[idx
];
3563 gdb::array_view
<const char *> m_symbol_table
;
3566 /* Convenience function that converts a NULL pointer to a "<null>"
3567 string, to pass to print routines. */
3570 string_or_null (const char *str
)
3572 return str
!= NULL
? str
: "<null>";
3575 /* Check if a lookup_name_info built from
3576 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3577 index. EXPECTED_LIST is the list of expected matches, in expected
3578 matching order. If no match expected, then an empty list is
3579 specified. Returns true on success. On failure prints a warning
3580 indicating the file:line that failed, and returns false. */
3583 check_match (const char *file
, int line
,
3584 mock_mapped_index
&mock_index
,
3585 const char *name
, symbol_name_match_type match_type
,
3586 bool completion_mode
,
3587 std::initializer_list
<const char *> expected_list
,
3588 dwarf2_per_objfile
*per_objfile
)
3590 lookup_name_info
lookup_name (name
, match_type
, completion_mode
);
3592 bool matched
= true;
3594 auto mismatch
= [&] (const char *expected_str
,
3597 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3598 "expected=\"%s\", got=\"%s\"\n"),
3600 (match_type
== symbol_name_match_type::FULL
3602 name
, string_or_null (expected_str
), string_or_null (got
));
3606 auto expected_it
= expected_list
.begin ();
3607 auto expected_end
= expected_list
.end ();
3609 dw2_expand_symtabs_matching_symbol (mock_index
, lookup_name
,
3611 [&] (offset_type idx
)
3613 const char *matched_name
= mock_index
.symbol_name_at (idx
, per_objfile
);
3614 const char *expected_str
3615 = expected_it
== expected_end
? NULL
: *expected_it
++;
3617 if (expected_str
== NULL
|| strcmp (expected_str
, matched_name
) != 0)
3618 mismatch (expected_str
, matched_name
);
3622 const char *expected_str
3623 = expected_it
== expected_end
? NULL
: *expected_it
++;
3624 if (expected_str
!= NULL
)
3625 mismatch (expected_str
, NULL
);
3630 /* The symbols added to the mock mapped_index for testing (in
3632 static const char *test_symbols
[] = {
3641 "ns2::tmpl<int>::foo2",
3642 "(anonymous namespace)::A::B::C",
3644 /* These are used to check that the increment-last-char in the
3645 matching algorithm for completion doesn't match "t1_fund" when
3646 completing "t1_func". */
3652 /* A UTF-8 name with multi-byte sequences to make sure that
3653 cp-name-parser understands this as a single identifier ("função"
3654 is "function" in PT). */
3657 /* \377 (0xff) is Latin1 'ÿ'. */
3660 /* \377 (0xff) is Latin1 'ÿ'. */
3664 /* A name with all sorts of complications. Starts with "z" to make
3665 it easier for the completion tests below. */
3666 #define Z_SYM_NAME \
3667 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3668 "::tuple<(anonymous namespace)::ui*, " \
3669 "std::default_delete<(anonymous namespace)::ui>, void>"
3674 /* Returns true if the mapped_index_base::find_name_component_bounds
3675 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3676 in completion mode. */
3679 check_find_bounds_finds (mapped_index_base
&index
,
3680 const char *search_name
,
3681 gdb::array_view
<const char *> expected_syms
,
3682 dwarf2_per_objfile
*per_objfile
)
3684 lookup_name_info
lookup_name (search_name
,
3685 symbol_name_match_type::FULL
, true);
3687 auto bounds
= index
.find_name_components_bounds (lookup_name
,
3691 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
3692 if (distance
!= expected_syms
.size ())
3695 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
3697 auto nc_elem
= bounds
.first
+ exp_elem
;
3698 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
3699 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
3706 /* Test the lower-level mapped_index::find_name_component_bounds
3710 test_mapped_index_find_name_component_bounds ()
3712 mock_mapped_index
mock_index (test_symbols
);
3714 mock_index
.build_name_components (NULL
/* per_objfile */);
3716 /* Test the lower-level mapped_index::find_name_component_bounds
3717 method in completion mode. */
3719 static const char *expected_syms
[] = {
3724 SELF_CHECK (check_find_bounds_finds
3725 (mock_index
, "t1_func", expected_syms
,
3726 NULL
/* per_objfile */));
3729 /* Check that the increment-last-char in the name matching algorithm
3730 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3732 static const char *expected_syms1
[] = {
3736 SELF_CHECK (check_find_bounds_finds
3737 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
3739 static const char *expected_syms2
[] = {
3742 SELF_CHECK (check_find_bounds_finds
3743 (mock_index
, "\377\377", expected_syms2
,
3744 NULL
/* per_objfile */));
3748 /* Test dw2_expand_symtabs_matching_symbol. */
3751 test_dw2_expand_symtabs_matching_symbol ()
3753 mock_mapped_index
mock_index (test_symbols
);
3755 /* We let all tests run until the end even if some fails, for debug
3757 bool any_mismatch
= false;
3759 /* Create the expected symbols list (an initializer_list). Needed
3760 because lists have commas, and we need to pass them to CHECK,
3761 which is a macro. */
3762 #define EXPECT(...) { __VA_ARGS__ }
3764 /* Wrapper for check_match that passes down the current
3765 __FILE__/__LINE__. */
3766 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3767 any_mismatch |= !check_match (__FILE__, __LINE__, \
3769 NAME, MATCH_TYPE, COMPLETION_MODE, \
3770 EXPECTED_LIST, NULL)
3772 /* Identity checks. */
3773 for (const char *sym
: test_symbols
)
3775 /* Should be able to match all existing symbols. */
3776 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
3779 /* Should be able to match all existing symbols with
3781 std::string with_params
= std::string (sym
) + "(int)";
3782 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3785 /* Should be able to match all existing symbols with
3786 parameters and qualifiers. */
3787 with_params
= std::string (sym
) + " ( int ) const";
3788 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3791 /* This should really find sym, but cp-name-parser.y doesn't
3792 know about lvalue/rvalue qualifiers yet. */
3793 with_params
= std::string (sym
) + " ( int ) &&";
3794 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3798 /* Check that the name matching algorithm for completion doesn't get
3799 confused with Latin1 'ÿ' / 0xff. */
3801 static const char str
[] = "\377";
3802 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3803 EXPECT ("\377", "\377\377123"));
3806 /* Check that the increment-last-char in the matching algorithm for
3807 completion doesn't match "t1_fund" when completing "t1_func". */
3809 static const char str
[] = "t1_func";
3810 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3811 EXPECT ("t1_func", "t1_func1"));
3814 /* Check that completion mode works at each prefix of the expected
3817 static const char str
[] = "function(int)";
3818 size_t len
= strlen (str
);
3821 for (size_t i
= 1; i
< len
; i
++)
3823 lookup
.assign (str
, i
);
3824 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3825 EXPECT ("function"));
3829 /* While "w" is a prefix of both components, the match function
3830 should still only be called once. */
3832 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
3834 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
3838 /* Same, with a "complicated" symbol. */
3840 static const char str
[] = Z_SYM_NAME
;
3841 size_t len
= strlen (str
);
3844 for (size_t i
= 1; i
< len
; i
++)
3846 lookup
.assign (str
, i
);
3847 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3848 EXPECT (Z_SYM_NAME
));
3852 /* In FULL mode, an incomplete symbol doesn't match. */
3854 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
3858 /* A complete symbol with parameters matches any overload, since the
3859 index has no overload info. */
3861 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
3862 EXPECT ("std::zfunction", "std::zfunction2"));
3863 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
3864 EXPECT ("std::zfunction", "std::zfunction2"));
3865 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
3866 EXPECT ("std::zfunction", "std::zfunction2"));
3869 /* Check that whitespace is ignored appropriately. A symbol with a
3870 template argument list. */
3872 static const char expected
[] = "ns::foo<int>";
3873 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
3875 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
3879 /* Check that whitespace is ignored appropriately. A symbol with a
3880 template argument list that includes a pointer. */
3882 static const char expected
[] = "ns::foo<char*>";
3883 /* Try both completion and non-completion modes. */
3884 static const bool completion_mode
[2] = {false, true};
3885 for (size_t i
= 0; i
< 2; i
++)
3887 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
3888 completion_mode
[i
], EXPECT (expected
));
3889 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
3890 completion_mode
[i
], EXPECT (expected
));
3892 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
3893 completion_mode
[i
], EXPECT (expected
));
3894 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
3895 completion_mode
[i
], EXPECT (expected
));
3900 /* Check method qualifiers are ignored. */
3901 static const char expected
[] = "ns::foo<char*>";
3902 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3903 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3904 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3905 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3906 CHECK_MATCH ("foo < char * > ( int ) const",
3907 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3908 CHECK_MATCH ("foo < char * > ( int ) &&",
3909 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3912 /* Test lookup names that don't match anything. */
3914 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
3917 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
3921 /* Some wild matching tests, exercising "(anonymous namespace)",
3922 which should not be confused with a parameter list. */
3924 static const char *syms
[] = {
3928 "A :: B :: C ( int )",
3933 for (const char *s
: syms
)
3935 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
3936 EXPECT ("(anonymous namespace)::A::B::C"));
3941 static const char expected
[] = "ns2::tmpl<int>::foo2";
3942 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
3944 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
3948 SELF_CHECK (!any_mismatch
);
3957 test_mapped_index_find_name_component_bounds ();
3958 test_dw2_expand_symtabs_matching_symbol ();
3961 }} // namespace selftests::dw2_expand_symtabs_matching
3963 #endif /* GDB_SELF_TEST */
3965 /* If FILE_MATCHER is NULL or if PER_CU has
3966 dwarf2_per_cu_quick_data::MARK set (see
3967 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3968 EXPANSION_NOTIFY on it. */
3971 dw2_expand_symtabs_matching_one
3972 (dwarf2_per_cu_data
*per_cu
,
3973 dwarf2_per_objfile
*per_objfile
,
3974 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3975 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
3977 if (file_matcher
== NULL
|| per_cu
->mark
)
3979 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
3981 compunit_symtab
*symtab
3982 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
3983 gdb_assert (symtab
!= nullptr);
3985 if (expansion_notify
!= NULL
&& symtab_was_null
)
3986 return expansion_notify (symtab
);
3991 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3992 matched, to expand corresponding CUs that were marked. IDX is the
3993 index of the symbol name that matched. */
3996 dw2_expand_marked_cus
3997 (dwarf2_per_objfile
*per_objfile
, offset_type idx
,
3998 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3999 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
4000 block_search_flags search_flags
,
4003 offset_type vec_len
, vec_idx
;
4004 bool global_seen
= false;
4005 mapped_index
&index
= *per_objfile
->per_bfd
->index_table
;
4007 offset_view
vec (index
.constant_pool
.slice (index
.symbol_vec_index (idx
)));
4009 for (vec_idx
= 0; vec_idx
< vec_len
; ++vec_idx
)
4011 offset_type cu_index_and_attrs
= vec
[vec_idx
+ 1];
4012 /* This value is only valid for index versions >= 7. */
4013 int is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
4014 gdb_index_symbol_kind symbol_kind
=
4015 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
4016 int cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
4017 /* Only check the symbol attributes if they're present.
4018 Indices prior to version 7 don't record them,
4019 and indices >= 7 may elide them for certain symbols
4020 (gold does this). */
4023 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
4025 /* Work around gold/15646. */
4028 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
4036 /* Only check the symbol's kind if it has one. */
4041 if ((search_flags
& SEARCH_STATIC_BLOCK
) == 0)
4046 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
4052 case VARIABLES_DOMAIN
:
4053 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
)
4056 case FUNCTIONS_DOMAIN
:
4057 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
)
4061 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
4064 case MODULES_DOMAIN
:
4065 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
4073 /* Don't crash on bad data. */
4074 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
4076 complaint (_(".gdb_index entry has bad CU index"
4077 " [in module %s]"), objfile_name (per_objfile
->objfile
));
4081 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
4082 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, file_matcher
,
4090 /* If FILE_MATCHER is non-NULL, set all the
4091 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4092 that match FILE_MATCHER. */
4095 dw_expand_symtabs_matching_file_matcher
4096 (dwarf2_per_objfile
*per_objfile
,
4097 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
4099 if (file_matcher
== NULL
)
4102 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
4104 NULL
, xcalloc
, xfree
));
4105 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
4107 NULL
, xcalloc
, xfree
));
4109 /* The rule is CUs specify all the files, including those used by
4110 any TU, so there's no need to scan TUs here. */
4112 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4116 if (per_cu
->is_debug_types
)
4120 /* We only need to look at symtabs not already expanded. */
4121 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4124 if (per_cu
->fnd
!= nullptr)
4126 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4128 if (file_matcher (fnd
->get_name (), false))
4134 /* Before we invoke realpath, which can get expensive when many
4135 files are involved, do a quick comparison of the basenames. */
4136 if ((basenames_may_differ
4137 || file_matcher (lbasename (fnd
->get_name ()), true))
4138 && file_matcher (fnd
->get_fullname (), false))
4145 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
4147 if (file_data
== NULL
)
4150 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
4152 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
4158 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4160 const char *this_real_name
;
4162 if (file_matcher (file_data
->file_names
[j
], false))
4168 /* Before we invoke realpath, which can get expensive when many
4169 files are involved, do a quick comparison of the basenames. */
4170 if (!basenames_may_differ
4171 && !file_matcher (lbasename (file_data
->file_names
[j
]),
4175 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
4176 if (file_matcher (this_real_name
, false))
4183 void **slot
= htab_find_slot (per_cu
->mark
4184 ? visited_found
.get ()
4185 : visited_not_found
.get (),
4192 dwarf2_gdb_index::expand_symtabs_matching
4193 (struct objfile
*objfile
,
4194 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
4195 const lookup_name_info
*lookup_name
,
4196 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
4197 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
4198 block_search_flags search_flags
,
4200 enum search_domain kind
)
4202 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4204 /* index_table is NULL if OBJF_READNOW. */
4205 if (!per_objfile
->per_bfd
->index_table
)
4208 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
4210 /* This invariant is documented in quick-functions.h. */
4211 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
4212 if (lookup_name
== nullptr)
4214 for (dwarf2_per_cu_data
*per_cu
4215 : all_comp_units_range (per_objfile
->per_bfd
))
4219 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
4227 mapped_index
&index
= *per_objfile
->per_bfd
->index_table
;
4230 = dw2_expand_symtabs_matching_symbol (index
, *lookup_name
,
4232 [&] (offset_type idx
)
4234 if (!dw2_expand_marked_cus (per_objfile
, idx
, file_matcher
,
4235 expansion_notify
, search_flags
, kind
))
4243 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4246 static struct compunit_symtab
*
4247 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
4252 if (cust
->blockvector () != nullptr
4253 && blockvector_contains_pc (cust
->blockvector (), pc
))
4256 if (cust
->includes
== NULL
)
4259 for (i
= 0; cust
->includes
[i
]; ++i
)
4261 struct compunit_symtab
*s
= cust
->includes
[i
];
4263 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
4271 struct compunit_symtab
*
4272 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4273 (struct objfile
*objfile
,
4274 struct bound_minimal_symbol msymbol
,
4276 struct obj_section
*section
,
4279 struct dwarf2_per_cu_data
*data
;
4280 struct compunit_symtab
*result
;
4282 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4283 if (per_objfile
->per_bfd
->index_addrmap
== nullptr)
4286 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
4287 data
= ((struct dwarf2_per_cu_data
*)
4288 addrmap_find (per_objfile
->per_bfd
->index_addrmap
,
4293 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
4294 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4295 paddress (objfile
->arch (), pc
));
4297 result
= recursively_find_pc_sect_compunit_symtab
4298 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
4300 gdb_assert (result
!= NULL
);
4305 dwarf2_base_index_functions::map_symbol_filenames
4306 (struct objfile
*objfile
,
4307 gdb::function_view
<symbol_filename_ftype
> fun
,
4310 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4312 /* Use caches to ensure we only call FUN once for each filename. */
4313 filename_seen_cache filenames_cache
;
4314 std::unordered_set
<quick_file_names
*> qfn_cache
;
4316 /* The rule is CUs specify all the files, including those used by any TU,
4317 so there's no need to scan TUs here. We can ignore file names coming
4318 from already-expanded CUs. It is possible that an expanded CU might
4319 reuse the file names data from a currently unexpanded CU, in this
4320 case we don't want to report the files from the unexpanded CU. */
4322 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4324 if (!per_cu
->is_debug_types
4325 && per_objfile
->symtab_set_p (per_cu
.get ()))
4327 if (per_cu
->file_names
!= nullptr)
4328 qfn_cache
.insert (per_cu
->file_names
);
4332 for (dwarf2_per_cu_data
*per_cu
4333 : all_comp_units_range (per_objfile
->per_bfd
))
4335 /* We only need to look at symtabs not already expanded. */
4336 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
4339 if (per_cu
->fnd
!= nullptr)
4341 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4343 const char *filename
= fnd
->get_name ();
4344 const char *key
= filename
;
4345 const char *fullname
= nullptr;
4349 fullname
= fnd
->get_fullname ();
4353 if (!filenames_cache
.seen (key
))
4354 fun (filename
, fullname
);
4357 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
4358 if (file_data
== nullptr
4359 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
4362 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4364 const char *filename
= file_data
->file_names
[j
];
4365 const char *key
= filename
;
4366 const char *fullname
= nullptr;
4370 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
4374 if (!filenames_cache
.seen (key
))
4375 fun (filename
, fullname
);
4381 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
4386 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4389 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
4391 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4393 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4395 /* Is this already expanded? */
4396 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4399 /* It has not yet been expanded. */
4406 /* DWARF-5 debug_names reader. */
4408 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4409 static const gdb_byte dwarf5_augmentation
[] = { 'G', 'D', 'B', 0 };
4411 /* A helper function that reads the .debug_names section in SECTION
4412 and fills in MAP. FILENAME is the name of the file containing the
4413 section; it is used for error reporting.
4415 Returns true if all went well, false otherwise. */
4418 read_debug_names_from_section (struct objfile
*objfile
,
4419 const char *filename
,
4420 struct dwarf2_section_info
*section
,
4421 mapped_debug_names
&map
)
4423 if (section
->empty ())
4426 /* Older elfutils strip versions could keep the section in the main
4427 executable while splitting it for the separate debug info file. */
4428 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
4431 section
->read (objfile
);
4433 map
.dwarf5_byte_order
= gdbarch_byte_order (objfile
->arch ());
4435 const gdb_byte
*addr
= section
->buffer
;
4437 bfd
*const abfd
= section
->get_bfd_owner ();
4439 unsigned int bytes_read
;
4440 LONGEST length
= read_initial_length (abfd
, addr
, &bytes_read
);
4443 map
.dwarf5_is_dwarf64
= bytes_read
!= 4;
4444 map
.offset_size
= map
.dwarf5_is_dwarf64
? 8 : 4;
4445 if (bytes_read
+ length
!= section
->size
)
4447 /* There may be multiple per-CU indices. */
4448 warning (_("Section .debug_names in %s length %s does not match "
4449 "section length %s, ignoring .debug_names."),
4450 filename
, plongest (bytes_read
+ length
),
4451 pulongest (section
->size
));
4455 /* The version number. */
4456 uint16_t version
= read_2_bytes (abfd
, addr
);
4460 warning (_("Section .debug_names in %s has unsupported version %d, "
4461 "ignoring .debug_names."),
4467 uint16_t padding
= read_2_bytes (abfd
, addr
);
4471 warning (_("Section .debug_names in %s has unsupported padding %d, "
4472 "ignoring .debug_names."),
4477 /* comp_unit_count - The number of CUs in the CU list. */
4478 map
.cu_count
= read_4_bytes (abfd
, addr
);
4481 /* local_type_unit_count - The number of TUs in the local TU
4483 map
.tu_count
= read_4_bytes (abfd
, addr
);
4486 /* foreign_type_unit_count - The number of TUs in the foreign TU
4488 uint32_t foreign_tu_count
= read_4_bytes (abfd
, addr
);
4490 if (foreign_tu_count
!= 0)
4492 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4493 "ignoring .debug_names."),
4494 filename
, static_cast<unsigned long> (foreign_tu_count
));
4498 /* bucket_count - The number of hash buckets in the hash lookup
4500 map
.bucket_count
= read_4_bytes (abfd
, addr
);
4503 /* name_count - The number of unique names in the index. */
4504 map
.name_count
= read_4_bytes (abfd
, addr
);
4507 /* abbrev_table_size - The size in bytes of the abbreviations
4509 uint32_t abbrev_table_size
= read_4_bytes (abfd
, addr
);
4512 /* augmentation_string_size - The size in bytes of the augmentation
4513 string. This value is rounded up to a multiple of 4. */
4514 uint32_t augmentation_string_size
= read_4_bytes (abfd
, addr
);
4516 map
.augmentation_is_gdb
= ((augmentation_string_size
4517 == sizeof (dwarf5_augmentation
))
4518 && memcmp (addr
, dwarf5_augmentation
,
4519 sizeof (dwarf5_augmentation
)) == 0);
4520 augmentation_string_size
+= (-augmentation_string_size
) & 3;
4521 addr
+= augmentation_string_size
;
4524 map
.cu_table_reordered
= addr
;
4525 addr
+= map
.cu_count
* map
.offset_size
;
4527 /* List of Local TUs */
4528 map
.tu_table_reordered
= addr
;
4529 addr
+= map
.tu_count
* map
.offset_size
;
4531 /* Hash Lookup Table */
4532 map
.bucket_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4533 addr
+= map
.bucket_count
* 4;
4534 map
.hash_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4535 addr
+= map
.name_count
* 4;
4538 map
.name_table_string_offs_reordered
= addr
;
4539 addr
+= map
.name_count
* map
.offset_size
;
4540 map
.name_table_entry_offs_reordered
= addr
;
4541 addr
+= map
.name_count
* map
.offset_size
;
4543 const gdb_byte
*abbrev_table_start
= addr
;
4546 const ULONGEST index_num
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4551 const auto insertpair
4552 = map
.abbrev_map
.emplace (index_num
, mapped_debug_names::index_val ());
4553 if (!insertpair
.second
)
4555 warning (_("Section .debug_names in %s has duplicate index %s, "
4556 "ignoring .debug_names."),
4557 filename
, pulongest (index_num
));
4560 mapped_debug_names::index_val
&indexval
= insertpair
.first
->second
;
4561 indexval
.dwarf_tag
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4566 mapped_debug_names::index_val::attr attr
;
4567 attr
.dw_idx
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4569 attr
.form
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4571 if (attr
.form
== DW_FORM_implicit_const
)
4573 attr
.implicit_const
= read_signed_leb128 (abfd
, addr
,
4577 if (attr
.dw_idx
== 0 && attr
.form
== 0)
4579 indexval
.attr_vec
.push_back (std::move (attr
));
4582 if (addr
!= abbrev_table_start
+ abbrev_table_size
)
4584 warning (_("Section .debug_names in %s has abbreviation_table "
4585 "of size %s vs. written as %u, ignoring .debug_names."),
4586 filename
, plongest (addr
- abbrev_table_start
),
4590 map
.entry_pool
= addr
;
4595 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4599 create_cus_from_debug_names_list (dwarf2_per_bfd
*per_bfd
,
4600 const mapped_debug_names
&map
,
4601 dwarf2_section_info
§ion
,
4604 if (!map
.augmentation_is_gdb
)
4606 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
4608 sect_offset sect_off
4609 = (sect_offset
) (extract_unsigned_integer
4610 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4612 map
.dwarf5_byte_order
));
4613 /* We don't know the length of the CU, because the CU list in a
4614 .debug_names index can be incomplete, so we can't use the start
4615 of the next CU as end of this CU. We create the CUs here with
4616 length 0, and in cutu_reader::cutu_reader we'll fill in the
4618 dwarf2_per_cu_data_up per_cu
4619 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4621 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4626 sect_offset sect_off_prev
;
4627 for (uint32_t i
= 0; i
<= map
.cu_count
; ++i
)
4629 sect_offset sect_off_next
;
4630 if (i
< map
.cu_count
)
4633 = (sect_offset
) (extract_unsigned_integer
4634 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4636 map
.dwarf5_byte_order
));
4639 sect_off_next
= (sect_offset
) section
.size
;
4642 const ULONGEST length
= sect_off_next
- sect_off_prev
;
4643 dwarf2_per_cu_data_up per_cu
4644 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4645 sect_off_prev
, length
);
4646 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4648 sect_off_prev
= sect_off_next
;
4652 /* Read the CU list from the mapped index, and use it to create all
4653 the CU objects for this dwarf2_per_objfile. */
4656 create_cus_from_debug_names (dwarf2_per_bfd
*per_bfd
,
4657 const mapped_debug_names
&map
,
4658 const mapped_debug_names
&dwz_map
)
4660 gdb_assert (per_bfd
->all_comp_units
.empty ());
4661 per_bfd
->all_comp_units
.reserve (map
.cu_count
+ dwz_map
.cu_count
);
4663 create_cus_from_debug_names_list (per_bfd
, map
, per_bfd
->info
,
4664 false /* is_dwz */);
4666 if (dwz_map
.cu_count
== 0)
4669 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4670 create_cus_from_debug_names_list (per_bfd
, dwz_map
, dwz
->info
,
4674 /* Read .debug_names. If everything went ok, initialize the "quick"
4675 elements of all the CUs and return true. Otherwise, return false. */
4678 dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
4680 std::unique_ptr
<mapped_debug_names
> map (new mapped_debug_names
);
4681 mapped_debug_names dwz_map
;
4682 struct objfile
*objfile
= per_objfile
->objfile
;
4683 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4685 if (!read_debug_names_from_section (objfile
, objfile_name (objfile
),
4686 &per_bfd
->debug_names
, *map
))
4689 /* Don't use the index if it's empty. */
4690 if (map
->name_count
== 0)
4693 /* If there is a .dwz file, read it so we can get its CU list as
4695 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4698 if (!read_debug_names_from_section (objfile
,
4699 bfd_get_filename (dwz
->dwz_bfd
.get ()),
4700 &dwz
->debug_names
, dwz_map
))
4702 warning (_("could not read '.debug_names' section from %s; skipping"),
4703 bfd_get_filename (dwz
->dwz_bfd
.get ()));
4708 create_cus_from_debug_names (per_bfd
, *map
, dwz_map
);
4710 if (map
->tu_count
!= 0)
4712 /* We can only handle a single .debug_types when we have an
4714 if (per_bfd
->types
.size () != 1)
4717 dwarf2_section_info
*section
= &per_bfd
->types
[0];
4719 create_signatured_type_table_from_debug_names
4720 (per_objfile
, *map
, section
, &per_bfd
->abbrev
);
4723 create_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
);
4725 per_bfd
->debug_names_table
= std::move (map
);
4726 per_bfd
->quick_file_names_table
=
4727 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
4732 /* Type used to manage iterating over all CUs looking for a symbol for
4735 class dw2_debug_names_iterator
4738 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4739 block_search_flags block_index
,
4741 const char *name
, dwarf2_per_objfile
*per_objfile
)
4742 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4743 m_addr (find_vec_in_debug_names (map
, name
, per_objfile
)),
4744 m_per_objfile (per_objfile
)
4747 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4748 search_domain search
, uint32_t namei
,
4749 dwarf2_per_objfile
*per_objfile
,
4750 domain_enum domain
= UNDEF_DOMAIN
)
4754 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4755 m_per_objfile (per_objfile
)
4758 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4759 block_search_flags block_index
, domain_enum domain
,
4760 uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4761 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4762 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4763 m_per_objfile (per_objfile
)
4766 /* Return the next matching CU or NULL if there are no more. */
4767 dwarf2_per_cu_data
*next ();
4770 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4772 dwarf2_per_objfile
*per_objfile
);
4773 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4775 dwarf2_per_objfile
*per_objfile
);
4777 /* The internalized form of .debug_names. */
4778 const mapped_debug_names
&m_map
;
4780 /* Restrict the search to these blocks. */
4781 block_search_flags m_block_index
= (SEARCH_GLOBAL_BLOCK
4782 | SEARCH_STATIC_BLOCK
);
4784 /* The kind of symbol we're looking for. */
4785 const domain_enum m_domain
= UNDEF_DOMAIN
;
4786 const search_domain m_search
= ALL_DOMAIN
;
4788 /* The list of CUs from the index entry of the symbol, or NULL if
4790 const gdb_byte
*m_addr
;
4792 dwarf2_per_objfile
*m_per_objfile
;
4796 mapped_debug_names::namei_to_name
4797 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const
4799 const ULONGEST namei_string_offs
4800 = extract_unsigned_integer ((name_table_string_offs_reordered
4801 + namei
* offset_size
),
4804 return read_indirect_string_at_offset (per_objfile
, namei_string_offs
);
4807 /* Find a slot in .debug_names for the object named NAME. If NAME is
4808 found, return pointer to its pool data. If NAME cannot be found,
4812 dw2_debug_names_iterator::find_vec_in_debug_names
4813 (const mapped_debug_names
&map
, const char *name
,
4814 dwarf2_per_objfile
*per_objfile
)
4816 int (*cmp
) (const char *, const char *);
4818 gdb::unique_xmalloc_ptr
<char> without_params
;
4819 if (current_language
->la_language
== language_cplus
4820 || current_language
->la_language
== language_fortran
4821 || current_language
->la_language
== language_d
)
4823 /* NAME is already canonical. Drop any qualifiers as
4824 .debug_names does not contain any. */
4826 if (strchr (name
, '(') != NULL
)
4828 without_params
= cp_remove_params (name
);
4829 if (without_params
!= NULL
)
4830 name
= without_params
.get ();
4834 cmp
= (case_sensitivity
== case_sensitive_on
? strcmp
: strcasecmp
);
4836 const uint32_t full_hash
= dwarf5_djb_hash (name
);
4838 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4839 (map
.bucket_table_reordered
4840 + (full_hash
% map
.bucket_count
)), 4,
4841 map
.dwarf5_byte_order
);
4845 if (namei
>= map
.name_count
)
4847 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4849 namei
, map
.name_count
,
4850 objfile_name (per_objfile
->objfile
));
4856 const uint32_t namei_full_hash
4857 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4858 (map
.hash_table_reordered
+ namei
), 4,
4859 map
.dwarf5_byte_order
);
4860 if (full_hash
% map
.bucket_count
!= namei_full_hash
% map
.bucket_count
)
4863 if (full_hash
== namei_full_hash
)
4865 const char *const namei_string
= map
.namei_to_name (namei
, per_objfile
);
4867 #if 0 /* An expensive sanity check. */
4868 if (namei_full_hash
!= dwarf5_djb_hash (namei_string
))
4870 complaint (_("Wrong .debug_names hash for string at index %u "
4872 namei
, objfile_name (dwarf2_per_objfile
->objfile
));
4877 if (cmp (namei_string
, name
) == 0)
4879 const ULONGEST namei_entry_offs
4880 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4881 + namei
* map
.offset_size
),
4882 map
.offset_size
, map
.dwarf5_byte_order
);
4883 return map
.entry_pool
+ namei_entry_offs
;
4888 if (namei
>= map
.name_count
)
4894 dw2_debug_names_iterator::find_vec_in_debug_names
4895 (const mapped_debug_names
&map
, uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4897 if (namei
>= map
.name_count
)
4899 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4901 namei
, map
.name_count
,
4902 objfile_name (per_objfile
->objfile
));
4906 const ULONGEST namei_entry_offs
4907 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4908 + namei
* map
.offset_size
),
4909 map
.offset_size
, map
.dwarf5_byte_order
);
4910 return map
.entry_pool
+ namei_entry_offs
;
4913 /* See dw2_debug_names_iterator. */
4915 dwarf2_per_cu_data
*
4916 dw2_debug_names_iterator::next ()
4921 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4922 struct objfile
*objfile
= m_per_objfile
->objfile
;
4923 bfd
*const abfd
= objfile
->obfd
;
4927 unsigned int bytes_read
;
4928 const ULONGEST abbrev
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4929 m_addr
+= bytes_read
;
4933 const auto indexval_it
= m_map
.abbrev_map
.find (abbrev
);
4934 if (indexval_it
== m_map
.abbrev_map
.cend ())
4936 complaint (_("Wrong .debug_names undefined abbrev code %s "
4938 pulongest (abbrev
), objfile_name (objfile
));
4941 const mapped_debug_names::index_val
&indexval
= indexval_it
->second
;
4942 enum class symbol_linkage
{
4946 } symbol_linkage_
= symbol_linkage::unknown
;
4947 dwarf2_per_cu_data
*per_cu
= NULL
;
4948 for (const mapped_debug_names::index_val::attr
&attr
: indexval
.attr_vec
)
4953 case DW_FORM_implicit_const
:
4954 ull
= attr
.implicit_const
;
4956 case DW_FORM_flag_present
:
4960 ull
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4961 m_addr
+= bytes_read
;
4964 ull
= read_4_bytes (abfd
, m_addr
);
4968 ull
= read_8_bytes (abfd
, m_addr
);
4971 case DW_FORM_ref_sig8
:
4972 ull
= read_8_bytes (abfd
, m_addr
);
4976 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4977 dwarf_form_name (attr
.form
),
4978 objfile_name (objfile
));
4981 switch (attr
.dw_idx
)
4983 case DW_IDX_compile_unit
:
4984 /* Don't crash on bad data. */
4985 if (ull
>= per_bfd
->all_comp_units
.size ())
4987 complaint (_(".debug_names entry has bad CU index %s"
4990 objfile_name (objfile
));
4993 per_cu
= per_bfd
->get_cu (ull
);
4995 case DW_IDX_type_unit
:
4996 /* Don't crash on bad data. */
4997 if (ull
>= per_bfd
->tu_stats
.nr_tus
)
4999 complaint (_(".debug_names entry has bad TU index %s"
5002 objfile_name (objfile
));
5005 per_cu
= per_bfd
->get_cu (ull
+ per_bfd
->tu_stats
.nr_tus
);
5007 case DW_IDX_die_offset
:
5008 /* In a per-CU index (as opposed to a per-module index), index
5009 entries without CU attribute implicitly refer to the single CU. */
5011 per_cu
= per_bfd
->get_cu (0);
5013 case DW_IDX_GNU_internal
:
5014 if (!m_map
.augmentation_is_gdb
)
5016 symbol_linkage_
= symbol_linkage::static_
;
5018 case DW_IDX_GNU_external
:
5019 if (!m_map
.augmentation_is_gdb
)
5021 symbol_linkage_
= symbol_linkage::extern_
;
5026 /* Skip if already read in. */
5027 if (m_per_objfile
->symtab_set_p (per_cu
))
5030 /* Check static vs global. */
5031 if (symbol_linkage_
!= symbol_linkage::unknown
)
5033 if (symbol_linkage_
== symbol_linkage::static_
)
5035 if ((m_block_index
& SEARCH_STATIC_BLOCK
) == 0)
5040 if ((m_block_index
& SEARCH_GLOBAL_BLOCK
) == 0)
5045 /* Match dw2_symtab_iter_next, symbol_kind
5046 and debug_names::psymbol_tag. */
5050 switch (indexval
.dwarf_tag
)
5052 case DW_TAG_variable
:
5053 case DW_TAG_subprogram
:
5054 /* Some types are also in VAR_DOMAIN. */
5055 case DW_TAG_typedef
:
5056 case DW_TAG_structure_type
:
5063 switch (indexval
.dwarf_tag
)
5065 case DW_TAG_typedef
:
5066 case DW_TAG_structure_type
:
5073 switch (indexval
.dwarf_tag
)
5076 case DW_TAG_variable
:
5083 switch (indexval
.dwarf_tag
)
5095 /* Match dw2_expand_symtabs_matching, symbol_kind and
5096 debug_names::psymbol_tag. */
5099 case VARIABLES_DOMAIN
:
5100 switch (indexval
.dwarf_tag
)
5102 case DW_TAG_variable
:
5108 case FUNCTIONS_DOMAIN
:
5109 switch (indexval
.dwarf_tag
)
5111 case DW_TAG_subprogram
:
5118 switch (indexval
.dwarf_tag
)
5120 case DW_TAG_typedef
:
5121 case DW_TAG_structure_type
:
5127 case MODULES_DOMAIN
:
5128 switch (indexval
.dwarf_tag
)
5142 /* This dumps minimal information about .debug_names. It is called
5143 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5144 uses this to verify that .debug_names has been loaded. */
5147 dwarf2_debug_names_index::dump (struct objfile
*objfile
)
5149 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5151 gdb_printf (".debug_names:");
5152 if (per_objfile
->per_bfd
->debug_names_table
)
5153 gdb_printf (" exists\n");
5155 gdb_printf (" faked for \"readnow\"\n");
5160 dwarf2_debug_names_index::expand_matching_symbols
5161 (struct objfile
*objfile
,
5162 const lookup_name_info
&name
, domain_enum domain
,
5164 symbol_compare_ftype
*ordered_compare
)
5166 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5168 /* debug_names_table is NULL if OBJF_READNOW. */
5169 if (!per_objfile
->per_bfd
->debug_names_table
)
5172 mapped_debug_names
&map
= *per_objfile
->per_bfd
->debug_names_table
;
5173 const block_search_flags block_flags
5174 = global
? SEARCH_GLOBAL_BLOCK
: SEARCH_STATIC_BLOCK
;
5176 const char *match_name
= name
.ada ().lookup_name ().c_str ();
5177 auto matcher
= [&] (const char *symname
)
5179 if (ordered_compare
== nullptr)
5181 return ordered_compare (symname
, match_name
) == 0;
5184 dw2_expand_symtabs_matching_symbol (map
, name
, matcher
,
5185 [&] (offset_type namei
)
5187 /* The name was matched, now expand corresponding CUs that were
5189 dw2_debug_names_iterator
iter (map
, block_flags
, domain
, namei
,
5192 struct dwarf2_per_cu_data
*per_cu
;
5193 while ((per_cu
= iter
.next ()) != NULL
)
5194 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
5201 dwarf2_debug_names_index::expand_symtabs_matching
5202 (struct objfile
*objfile
,
5203 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
5204 const lookup_name_info
*lookup_name
,
5205 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
5206 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
5207 block_search_flags search_flags
,
5209 enum search_domain kind
)
5211 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5213 /* debug_names_table is NULL if OBJF_READNOW. */
5214 if (!per_objfile
->per_bfd
->debug_names_table
)
5217 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
5219 /* This invariant is documented in quick-functions.h. */
5220 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
5221 if (lookup_name
== nullptr)
5223 for (dwarf2_per_cu_data
*per_cu
5224 : all_comp_units_range (per_objfile
->per_bfd
))
5228 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5236 mapped_debug_names
&map
= *per_objfile
->per_bfd
->debug_names_table
;
5239 = dw2_expand_symtabs_matching_symbol (map
, *lookup_name
,
5241 [&] (offset_type namei
)
5243 /* The name was matched, now expand corresponding CUs that were
5245 dw2_debug_names_iterator
iter (map
, kind
, namei
, per_objfile
, domain
);
5247 struct dwarf2_per_cu_data
*per_cu
;
5248 while ((per_cu
= iter
.next ()) != NULL
)
5249 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5259 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5260 to either a dwarf2_per_bfd or dwz_file object. */
5262 template <typename T
>
5263 static gdb::array_view
<const gdb_byte
>
5264 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
5266 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
5268 if (section
->empty ())
5271 /* Older elfutils strip versions could keep the section in the main
5272 executable while splitting it for the separate debug info file. */
5273 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
5276 section
->read (obj
);
5278 /* dwarf2_section_info::size is a bfd_size_type, while
5279 gdb::array_view works with size_t. On 32-bit hosts, with
5280 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5281 is 32-bit. So we need an explicit narrowing conversion here.
5282 This is fine, because it's impossible to allocate or mmap an
5283 array/buffer larger than what size_t can represent. */
5284 return gdb::make_array_view (section
->buffer
, section
->size
);
5287 /* Lookup the index cache for the contents of the index associated to
5290 static gdb::array_view
<const gdb_byte
>
5291 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
5293 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
5294 if (build_id
== nullptr)
5297 return global_index_cache
.lookup_gdb_index (build_id
,
5298 &dwarf2_per_bfd
->index_cache_res
);
5301 /* Same as the above, but for DWZ. */
5303 static gdb::array_view
<const gdb_byte
>
5304 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
5306 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
5307 if (build_id
== nullptr)
5310 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
5313 static quick_symbol_functions_up
make_cooked_index_funcs ();
5315 /* See dwarf2/public.h. */
5318 dwarf2_initialize_objfile (struct objfile
*objfile
)
5320 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5321 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5323 dwarf_read_debug_printf ("called");
5325 /* If we're about to read full symbols, don't bother with the
5326 indices. In this case we also don't care if some other debug
5327 format is making psymtabs, because they are all about to be
5329 if ((objfile
->flags
& OBJF_READNOW
))
5331 dwarf_read_debug_printf ("readnow requested");
5333 create_all_comp_units (per_objfile
);
5334 per_bfd
->quick_file_names_table
5335 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
5337 /* Arrange for gdb to see the "quick" functions. However, these
5338 functions will be no-ops because we will have expanded all
5340 objfile
->qf
.push_front (make_dwarf_gdb_index ());
5344 /* Was a debug names index already read when we processed an objfile sharing
5346 if (per_bfd
->debug_names_table
!= nullptr)
5348 dwarf_read_debug_printf ("re-using shared debug names table");
5349 objfile
->qf
.push_front (make_dwarf_debug_names ());
5353 /* Was a GDB index already read when we processed an objfile sharing
5355 if (per_bfd
->index_table
!= nullptr)
5357 dwarf_read_debug_printf ("re-using shared index table");
5358 objfile
->qf
.push_front (make_dwarf_gdb_index ());
5362 if (per_bfd
->cooked_index_table
!= nullptr)
5364 dwarf_read_debug_printf ("re-using cooked index table");
5365 objfile
->qf
.push_front (make_cooked_index_funcs ());
5369 if (dwarf2_read_debug_names (per_objfile
))
5371 dwarf_read_debug_printf ("found debug names");
5372 objfile
->qf
.push_front (make_dwarf_debug_names ());
5376 if (dwarf2_read_gdb_index (per_objfile
,
5377 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
5378 get_gdb_index_contents_from_section
<dwz_file
>))
5380 dwarf_read_debug_printf ("found gdb index from file");
5381 objfile
->qf
.push_front (make_dwarf_gdb_index ());
5385 /* ... otherwise, try to find the index in the index cache. */
5386 if (dwarf2_read_gdb_index (per_objfile
,
5387 get_gdb_index_contents_from_cache
,
5388 get_gdb_index_contents_from_cache_dwz
))
5390 dwarf_read_debug_printf ("found gdb index from cache");
5391 global_index_cache
.hit ();
5392 objfile
->qf
.push_front (make_dwarf_gdb_index ());
5396 global_index_cache
.miss ();
5397 objfile
->qf
.push_front (make_cooked_index_funcs ());
5402 /* Build a partial symbol table. */
5405 dwarf2_build_psymtabs (struct objfile
*objfile
, bool already_attached
)
5407 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5409 if (already_attached
)
5411 if (per_objfile
->per_bfd
->cooked_index_table
!= nullptr)
5415 objfile
->qf
.push_front (make_cooked_index_funcs ());
5419 dwarf2_build_psymtabs_hard (per_objfile
);
5421 /* (maybe) store an index in the cache. */
5422 global_index_cache
.store (per_objfile
);
5424 catch (const gdb_exception_error
&except
)
5426 exception_print (gdb_stderr
, except
);
5430 /* Find the base address of the compilation unit for range lists and
5431 location lists. It will normally be specified by DW_AT_low_pc.
5432 In DWARF-3 draft 4, the base address could be overridden by
5433 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5434 compilation units with discontinuous ranges. */
5437 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
5439 struct attribute
*attr
;
5441 cu
->base_address
.reset ();
5443 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
5444 if (attr
!= nullptr)
5445 cu
->base_address
= attr
->as_address ();
5448 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
5449 if (attr
!= nullptr)
5450 cu
->base_address
= attr
->as_address ();
5454 /* Helper function that returns the proper abbrev section for
5457 static struct dwarf2_section_info
*
5458 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
5460 struct dwarf2_section_info
*abbrev
;
5461 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
5463 if (this_cu
->is_dwz
)
5464 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
5466 abbrev
= &per_bfd
->abbrev
;
5471 /* Fetch the abbreviation table offset from a comp or type unit header. */
5474 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
5475 struct dwarf2_section_info
*section
,
5476 sect_offset sect_off
)
5478 bfd
*abfd
= section
->get_bfd_owner ();
5479 const gdb_byte
*info_ptr
;
5480 unsigned int initial_length_size
, offset_size
;
5483 section
->read (per_objfile
->objfile
);
5484 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
5485 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
5486 offset_size
= initial_length_size
== 4 ? 4 : 8;
5487 info_ptr
+= initial_length_size
;
5489 version
= read_2_bytes (abfd
, info_ptr
);
5493 /* Skip unit type and address size. */
5497 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
5501 hash_signatured_type (const void *item
)
5503 const struct signatured_type
*sig_type
5504 = (const struct signatured_type
*) item
;
5506 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5507 return sig_type
->signature
;
5511 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
5513 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
5514 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
5516 return lhs
->signature
== rhs
->signature
;
5519 /* Allocate a hash table for signatured types. */
5522 allocate_signatured_type_table ()
5524 return htab_up (htab_create_alloc (41,
5525 hash_signatured_type
,
5527 NULL
, xcalloc
, xfree
));
5530 /* A helper for create_debug_types_hash_table. Read types from SECTION
5531 and fill them into TYPES_HTAB. It will process only type units,
5532 therefore DW_UT_type. */
5535 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
5536 struct dwo_file
*dwo_file
,
5537 dwarf2_section_info
*section
, htab_up
&types_htab
,
5538 rcuh_kind section_kind
)
5540 struct objfile
*objfile
= per_objfile
->objfile
;
5541 struct dwarf2_section_info
*abbrev_section
;
5543 const gdb_byte
*info_ptr
, *end_ptr
;
5545 abbrev_section
= &dwo_file
->sections
.abbrev
;
5547 dwarf_read_debug_printf ("Reading %s for %s",
5548 section
->get_name (),
5549 abbrev_section
->get_file_name ());
5551 section
->read (objfile
);
5552 info_ptr
= section
->buffer
;
5554 if (info_ptr
== NULL
)
5557 /* We can't set abfd until now because the section may be empty or
5558 not present, in which case the bfd is unknown. */
5559 abfd
= section
->get_bfd_owner ();
5561 /* We don't use cutu_reader here because we don't need to read
5562 any dies: the signature is in the header. */
5564 end_ptr
= info_ptr
+ section
->size
;
5565 while (info_ptr
< end_ptr
)
5567 signatured_type_up sig_type
;
5568 struct dwo_unit
*dwo_tu
;
5570 const gdb_byte
*ptr
= info_ptr
;
5571 struct comp_unit_head header
;
5572 unsigned int length
;
5574 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
5576 /* Initialize it due to a false compiler warning. */
5577 header
.signature
= -1;
5578 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
5580 /* We need to read the type's signature in order to build the hash
5581 table, but we don't need anything else just yet. */
5583 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
5584 abbrev_section
, ptr
, section_kind
);
5586 length
= header
.get_length ();
5588 /* Skip dummy type units. */
5589 if (ptr
>= info_ptr
+ length
5590 || peek_abbrev_code (abfd
, ptr
) == 0
5591 || (header
.unit_type
!= DW_UT_type
5592 && header
.unit_type
!= DW_UT_split_type
))
5598 if (types_htab
== NULL
)
5599 types_htab
= allocate_dwo_unit_table ();
5601 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
5602 dwo_tu
->dwo_file
= dwo_file
;
5603 dwo_tu
->signature
= header
.signature
;
5604 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
5605 dwo_tu
->section
= section
;
5606 dwo_tu
->sect_off
= sect_off
;
5607 dwo_tu
->length
= length
;
5609 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
5610 gdb_assert (slot
!= NULL
);
5612 complaint (_("debug type entry at offset %s is duplicate to"
5613 " the entry at offset %s, signature %s"),
5614 sect_offset_str (sect_off
),
5615 sect_offset_str (dwo_tu
->sect_off
),
5616 hex_string (header
.signature
));
5619 dwarf_read_debug_printf_v (" offset %s, signature %s",
5620 sect_offset_str (sect_off
),
5621 hex_string (header
.signature
));
5627 /* Create the hash table of all entries in the .debug_types
5628 (or .debug_types.dwo) section(s).
5629 DWO_FILE is a pointer to the DWO file object.
5631 The result is a pointer to the hash table or NULL if there are no types.
5633 Note: This function processes DWO files only, not DWP files. */
5636 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
5637 struct dwo_file
*dwo_file
,
5638 gdb::array_view
<dwarf2_section_info
> type_sections
,
5639 htab_up
&types_htab
)
5641 for (dwarf2_section_info
§ion
: type_sections
)
5642 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
5646 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5647 If SLOT is non-NULL, it is the entry to use in the hash table.
5648 Otherwise we find one. */
5650 static struct signatured_type
*
5651 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
5653 if (per_objfile
->per_bfd
->all_comp_units
.size ()
5654 == per_objfile
->per_bfd
->all_comp_units
.capacity ())
5655 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
5657 signatured_type_up sig_type_holder
5658 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
5659 signatured_type
*sig_type
= sig_type_holder
.get ();
5661 per_objfile
->per_bfd
->all_comp_units
.emplace_back
5662 (sig_type_holder
.release ());
5666 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5669 gdb_assert (*slot
== NULL
);
5671 /* The rest of sig_type must be filled in by the caller. */
5675 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5676 Fill in SIG_ENTRY with DWO_ENTRY. */
5679 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
5680 struct signatured_type
*sig_entry
,
5681 struct dwo_unit
*dwo_entry
)
5683 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5685 /* Make sure we're not clobbering something we don't expect to. */
5686 gdb_assert (! sig_entry
->queued
);
5687 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
5688 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
5689 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
5690 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
5691 || (to_underlying (sig_entry
->type_offset_in_section
)
5692 == to_underlying (dwo_entry
->type_offset_in_tu
)));
5693 gdb_assert (sig_entry
->type_unit_group
== NULL
);
5694 gdb_assert (sig_entry
->dwo_unit
== NULL
5695 || sig_entry
->dwo_unit
== dwo_entry
);
5697 sig_entry
->section
= dwo_entry
->section
;
5698 sig_entry
->sect_off
= dwo_entry
->sect_off
;
5699 sig_entry
->length
= dwo_entry
->length
;
5700 sig_entry
->reading_dwo_directly
= 1;
5701 sig_entry
->per_bfd
= per_bfd
;
5702 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
5703 sig_entry
->dwo_unit
= dwo_entry
;
5706 /* Subroutine of lookup_signatured_type.
5707 If we haven't read the TU yet, create the signatured_type data structure
5708 for a TU to be read in directly from a DWO file, bypassing the stub.
5709 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5710 using .gdb_index, then when reading a CU we want to stay in the DWO file
5711 containing that CU. Otherwise we could end up reading several other DWO
5712 files (due to comdat folding) to process the transitive closure of all the
5713 mentioned TUs, and that can be slow. The current DWO file will have every
5714 type signature that it needs.
5715 We only do this for .gdb_index because in the psymtab case we already have
5716 to read all the DWOs to build the type unit groups. */
5718 static struct signatured_type
*
5719 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5721 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5722 struct dwo_file
*dwo_file
;
5723 struct dwo_unit find_dwo_entry
, *dwo_entry
;
5726 gdb_assert (cu
->dwo_unit
);
5728 /* If TU skeletons have been removed then we may not have read in any
5730 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5731 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5733 /* We only ever need to read in one copy of a signatured type.
5734 Use the global signatured_types array to do our own comdat-folding
5735 of types. If this is the first time we're reading this TU, and
5736 the TU has an entry in .gdb_index, replace the recorded data from
5737 .gdb_index with this TU. */
5739 signatured_type
find_sig_entry (sig
);
5740 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5741 &find_sig_entry
, INSERT
);
5742 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5744 /* We can get here with the TU already read, *or* in the process of being
5745 read. Don't reassign the global entry to point to this DWO if that's
5746 the case. Also note that if the TU is already being read, it may not
5747 have come from a DWO, the program may be a mix of Fission-compiled
5748 code and non-Fission-compiled code. */
5750 /* Have we already tried to read this TU?
5751 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5752 needn't exist in the global table yet). */
5753 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
5756 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5757 dwo_unit of the TU itself. */
5758 dwo_file
= cu
->dwo_unit
->dwo_file
;
5760 /* Ok, this is the first time we're reading this TU. */
5761 if (dwo_file
->tus
== NULL
)
5763 find_dwo_entry
.signature
= sig
;
5764 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
5766 if (dwo_entry
== NULL
)
5769 /* If the global table doesn't have an entry for this TU, add one. */
5770 if (sig_entry
== NULL
)
5771 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5773 if (sig_entry
->dwo_unit
== nullptr)
5774 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5775 sig_entry
->tu_read
= 1;
5779 /* Subroutine of lookup_signatured_type.
5780 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5781 then try the DWP file. If the TU stub (skeleton) has been removed then
5782 it won't be in .gdb_index. */
5784 static struct signatured_type
*
5785 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5787 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5788 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
5789 struct dwo_unit
*dwo_entry
;
5792 gdb_assert (cu
->dwo_unit
);
5793 gdb_assert (dwp_file
!= NULL
);
5795 /* If TU skeletons have been removed then we may not have read in any
5797 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5798 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5800 signatured_type
find_sig_entry (sig
);
5801 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5802 &find_sig_entry
, INSERT
);
5803 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5805 /* Have we already tried to read this TU?
5806 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5807 needn't exist in the global table yet). */
5808 if (sig_entry
!= NULL
)
5811 if (dwp_file
->tus
== NULL
)
5813 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
5814 1 /* is_debug_types */);
5815 if (dwo_entry
== NULL
)
5818 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5819 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5824 /* Lookup a signature based type for DW_FORM_ref_sig8.
5825 Returns NULL if signature SIG is not present in the table.
5826 It is up to the caller to complain about this. */
5828 static struct signatured_type
*
5829 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5831 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5835 /* We're in a DWO/DWP file, and we're using .gdb_index.
5836 These cases require special processing. */
5837 if (get_dwp_file (per_objfile
) == NULL
)
5838 return lookup_dwo_signatured_type (cu
, sig
);
5840 return lookup_dwp_signatured_type (cu
, sig
);
5844 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5846 signatured_type
find_entry (sig
);
5847 return ((struct signatured_type
*)
5848 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
5853 /* Low level DIE reading support. */
5855 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5858 init_cu_die_reader (struct die_reader_specs
*reader
,
5859 struct dwarf2_cu
*cu
,
5860 struct dwarf2_section_info
*section
,
5861 struct dwo_file
*dwo_file
,
5862 struct abbrev_table
*abbrev_table
)
5864 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
5865 reader
->abfd
= section
->get_bfd_owner ();
5867 reader
->dwo_file
= dwo_file
;
5868 reader
->die_section
= section
;
5869 reader
->buffer
= section
->buffer
;
5870 reader
->buffer_end
= section
->buffer
+ section
->size
;
5871 reader
->abbrev_table
= abbrev_table
;
5874 /* Subroutine of cutu_reader to simplify it.
5875 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5876 There's just a lot of work to do, and cutu_reader is big enough
5879 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5880 from it to the DIE in the DWO. If NULL we are skipping the stub.
5881 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5882 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5883 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5884 STUB_COMP_DIR may be non-NULL.
5885 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5886 are filled in with the info of the DIE from the DWO file.
5887 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5888 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5889 kept around for at least as long as *RESULT_READER.
5891 The result is non-zero if a valid (non-dummy) DIE was found. */
5894 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
5895 struct dwo_unit
*dwo_unit
,
5896 struct die_info
*stub_comp_unit_die
,
5897 const char *stub_comp_dir
,
5898 struct die_reader_specs
*result_reader
,
5899 const gdb_byte
**result_info_ptr
,
5900 struct die_info
**result_comp_unit_die
,
5901 abbrev_table_up
*result_dwo_abbrev_table
)
5903 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5904 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
5905 struct objfile
*objfile
= per_objfile
->objfile
;
5907 const gdb_byte
*begin_info_ptr
, *info_ptr
;
5908 struct attribute
*comp_dir
, *stmt_list
, *low_pc
, *high_pc
, *ranges
;
5909 int i
,num_extra_attrs
;
5910 struct dwarf2_section_info
*dwo_abbrev_section
;
5911 struct die_info
*comp_unit_die
;
5913 /* At most one of these may be provided. */
5914 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
5916 /* These attributes aren't processed until later:
5917 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5918 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5919 referenced later. However, these attributes are found in the stub
5920 which we won't have later. In order to not impose this complication
5921 on the rest of the code, we read them here and copy them to the
5930 if (stub_comp_unit_die
!= NULL
)
5932 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5934 if (!per_cu
->is_debug_types
)
5935 stmt_list
= dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
);
5936 low_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
);
5937 high_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
);
5938 ranges
= dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
);
5939 comp_dir
= dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
);
5941 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
5943 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5944 We need the value before we can process DW_AT_ranges values from the
5946 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
5948 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5949 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5950 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5951 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5953 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
5955 else if (stub_comp_dir
!= NULL
)
5957 /* Reconstruct the comp_dir attribute to simplify the code below. */
5958 comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
, struct attribute
);
5959 comp_dir
->name
= DW_AT_comp_dir
;
5960 comp_dir
->form
= DW_FORM_string
;
5961 comp_dir
->set_string_noncanonical (stub_comp_dir
);
5964 /* Set up for reading the DWO CU/TU. */
5965 cu
->dwo_unit
= dwo_unit
;
5966 dwarf2_section_info
*section
= dwo_unit
->section
;
5967 section
->read (objfile
);
5968 abfd
= section
->get_bfd_owner ();
5969 begin_info_ptr
= info_ptr
= (section
->buffer
5970 + to_underlying (dwo_unit
->sect_off
));
5971 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
5973 if (per_cu
->is_debug_types
)
5975 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
5977 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5978 section
, dwo_abbrev_section
,
5979 info_ptr
, rcuh_kind::TYPE
);
5980 /* This is not an assert because it can be caused by bad debug info. */
5981 if (sig_type
->signature
!= cu
->header
.signature
)
5983 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5984 " TU at offset %s [in module %s]"),
5985 hex_string (sig_type
->signature
),
5986 hex_string (cu
->header
.signature
),
5987 sect_offset_str (dwo_unit
->sect_off
),
5988 bfd_get_filename (abfd
));
5990 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5991 /* For DWOs coming from DWP files, we don't know the CU length
5992 nor the type's offset in the TU until now. */
5993 dwo_unit
->length
= cu
->header
.get_length ();
5994 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
5996 /* Establish the type offset that can be used to lookup the type.
5997 For DWO files, we don't know it until now. */
5998 sig_type
->type_offset_in_section
5999 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
6003 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6004 section
, dwo_abbrev_section
,
6005 info_ptr
, rcuh_kind::COMPILE
);
6006 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
6007 /* For DWOs coming from DWP files, we don't know the CU length
6009 dwo_unit
->length
= cu
->header
.get_length ();
6012 dwo_abbrev_section
->read (objfile
);
6013 *result_dwo_abbrev_table
6014 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
6015 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
6016 result_dwo_abbrev_table
->get ());
6018 /* Read in the die, but leave space to copy over the attributes
6019 from the stub. This has the benefit of simplifying the rest of
6020 the code - all the work to maintain the illusion of a single
6021 DW_TAG_{compile,type}_unit DIE is done here. */
6022 num_extra_attrs
= ((stmt_list
!= NULL
)
6026 + (comp_dir
!= NULL
));
6027 info_ptr
= read_full_die_1 (result_reader
, result_comp_unit_die
, info_ptr
,
6030 /* Copy over the attributes from the stub to the DIE we just read in. */
6031 comp_unit_die
= *result_comp_unit_die
;
6032 i
= comp_unit_die
->num_attrs
;
6033 if (stmt_list
!= NULL
)
6034 comp_unit_die
->attrs
[i
++] = *stmt_list
;
6036 comp_unit_die
->attrs
[i
++] = *low_pc
;
6037 if (high_pc
!= NULL
)
6038 comp_unit_die
->attrs
[i
++] = *high_pc
;
6040 comp_unit_die
->attrs
[i
++] = *ranges
;
6041 if (comp_dir
!= NULL
)
6042 comp_unit_die
->attrs
[i
++] = *comp_dir
;
6043 comp_unit_die
->num_attrs
+= num_extra_attrs
;
6045 if (dwarf_die_debug
)
6047 gdb_printf (gdb_stdlog
,
6048 "Read die from %s@0x%x of %s:\n",
6049 section
->get_name (),
6050 (unsigned) (begin_info_ptr
- section
->buffer
),
6051 bfd_get_filename (abfd
));
6052 dump_die (comp_unit_die
, dwarf_die_debug
);
6055 /* Skip dummy compilation units. */
6056 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
6057 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6060 *result_info_ptr
= info_ptr
;
6064 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6065 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6066 signature is part of the header. */
6067 static gdb::optional
<ULONGEST
>
6068 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
6070 if (cu
->header
.version
>= 5)
6071 return cu
->header
.signature
;
6072 struct attribute
*attr
;
6073 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
6074 if (attr
== nullptr || !attr
->form_is_unsigned ())
6075 return gdb::optional
<ULONGEST
> ();
6076 return attr
->as_unsigned ();
6079 /* Subroutine of cutu_reader to simplify it.
6080 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6081 Returns NULL if the specified DWO unit cannot be found. */
6083 static struct dwo_unit
*
6084 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
6087 /* We need a lock here both to handle the DWO hash table, and BFD,
6088 which is not thread-safe. */
6089 static std::mutex dwo_lock
;
6091 std::lock_guard
<std::mutex
> guard (dwo_lock
);
6094 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6095 struct dwo_unit
*dwo_unit
;
6096 const char *comp_dir
;
6098 gdb_assert (cu
!= NULL
);
6100 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6101 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6102 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
6104 if (per_cu
->is_debug_types
)
6105 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
6108 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
6110 if (!signature
.has_value ())
6111 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6113 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
6115 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
6121 /* Subroutine of cutu_reader to simplify it.
6122 See it for a description of the parameters.
6123 Read a TU directly from a DWO file, bypassing the stub. */
6126 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
6127 dwarf2_per_objfile
*per_objfile
,
6128 dwarf2_cu
*existing_cu
)
6130 struct signatured_type
*sig_type
;
6132 /* Verify we can do the following downcast, and that we have the
6134 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
6135 sig_type
= (struct signatured_type
*) this_cu
;
6136 gdb_assert (sig_type
->dwo_unit
!= NULL
);
6140 if (existing_cu
!= nullptr)
6143 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
6144 /* There's no need to do the rereading_dwo_cu handling that
6145 cutu_reader does since we don't read the stub. */
6149 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6150 in per_objfile yet. */
6151 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6152 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6153 cu
= m_new_cu
.get ();
6156 /* A future optimization, if needed, would be to use an existing
6157 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6158 could share abbrev tables. */
6160 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
6161 NULL
/* stub_comp_unit_die */,
6162 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
6165 &m_dwo_abbrev_table
) == 0)
6172 /* Initialize a CU (or TU) and read its DIEs.
6173 If the CU defers to a DWO file, read the DWO file as well.
6175 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6176 Otherwise the table specified in the comp unit header is read in and used.
6177 This is an optimization for when we already have the abbrev table.
6179 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6182 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6183 dwarf2_per_objfile
*per_objfile
,
6184 struct abbrev_table
*abbrev_table
,
6185 dwarf2_cu
*existing_cu
,
6187 abbrev_cache
*cache
)
6188 : die_reader_specs
{},
6191 struct objfile
*objfile
= per_objfile
->objfile
;
6192 struct dwarf2_section_info
*section
= this_cu
->section
;
6193 bfd
*abfd
= section
->get_bfd_owner ();
6194 const gdb_byte
*begin_info_ptr
;
6195 struct signatured_type
*sig_type
= NULL
;
6196 struct dwarf2_section_info
*abbrev_section
;
6197 /* Non-zero if CU currently points to a DWO file and we need to
6198 reread it. When this happens we need to reread the skeleton die
6199 before we can reread the DWO file (this only applies to CUs, not TUs). */
6200 int rereading_dwo_cu
= 0;
6202 if (dwarf_die_debug
)
6203 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6204 this_cu
->is_debug_types
? "type" : "comp",
6205 sect_offset_str (this_cu
->sect_off
));
6207 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6208 file (instead of going through the stub), short-circuit all of this. */
6209 if (this_cu
->reading_dwo_directly
)
6211 /* Narrow down the scope of possibilities to have to understand. */
6212 gdb_assert (this_cu
->is_debug_types
);
6213 gdb_assert (abbrev_table
== NULL
);
6214 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
6218 /* This is cheap if the section is already read in. */
6219 section
->read (objfile
);
6221 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6223 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
6227 if (existing_cu
!= nullptr)
6230 /* If this CU is from a DWO file we need to start over, we need to
6231 refetch the attributes from the skeleton CU.
6232 This could be optimized by retrieving those attributes from when we
6233 were here the first time: the previous comp_unit_die was stored in
6234 comp_unit_obstack. But there's no data yet that we need this
6236 if (cu
->dwo_unit
!= NULL
)
6237 rereading_dwo_cu
= 1;
6241 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6242 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6243 let us know that the CU is being scanned using the parallel
6244 indexer. This assert is avoided in this case because (1) it
6245 is irrelevant, and (2) the get_cu method is not
6247 gdb_assert (cache
!= nullptr
6248 || per_objfile
->get_cu (this_cu
) == nullptr);
6249 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6250 cu
= m_new_cu
.get ();
6253 /* Get the header. */
6254 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
6256 /* We already have the header, there's no need to read it in again. */
6257 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
6261 if (this_cu
->is_debug_types
)
6263 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6264 section
, abbrev_section
,
6265 info_ptr
, rcuh_kind::TYPE
);
6267 /* Since per_cu is the first member of struct signatured_type,
6268 we can go from a pointer to one to a pointer to the other. */
6269 sig_type
= (struct signatured_type
*) this_cu
;
6270 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
6271 gdb_assert (sig_type
->type_offset_in_tu
6272 == cu
->header
.type_cu_offset_in_tu
);
6273 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6275 /* LENGTH has not been set yet for type units if we're
6276 using .gdb_index. */
6277 this_cu
->length
= cu
->header
.get_length ();
6279 /* Establish the type offset that can be used to lookup the type. */
6280 sig_type
->type_offset_in_section
=
6281 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
6283 this_cu
->dwarf_version
= cu
->header
.version
;
6287 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6288 section
, abbrev_section
,
6290 rcuh_kind::COMPILE
);
6292 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6293 if (this_cu
->length
== 0)
6294 this_cu
->length
= cu
->header
.get_length ();
6296 gdb_assert (this_cu
->length
== cu
->header
.get_length ());
6297 this_cu
->dwarf_version
= cu
->header
.version
;
6301 /* Skip dummy compilation units. */
6302 if (info_ptr
>= begin_info_ptr
+ this_cu
->length
6303 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6309 /* If we don't have them yet, read the abbrevs for this compilation unit.
6310 And if we need to read them now, make sure they're freed when we're
6312 if (abbrev_table
!= NULL
)
6313 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
6316 if (cache
!= nullptr)
6317 abbrev_table
= cache
->find (abbrev_section
,
6318 cu
->header
.abbrev_sect_off
);
6319 if (abbrev_table
== nullptr)
6321 abbrev_section
->read (objfile
);
6322 m_abbrev_table_holder
6323 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
6324 abbrev_table
= m_abbrev_table_holder
.get ();
6328 /* Read the top level CU/TU die. */
6329 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
6330 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6332 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
6338 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6339 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6340 table from the DWO file and pass the ownership over to us. It will be
6341 referenced from READER, so we must make sure to free it after we're done
6344 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6345 DWO CU, that this test will fail (the attribute will not be present). */
6346 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6347 if (dwo_name
!= nullptr)
6349 struct dwo_unit
*dwo_unit
;
6350 struct die_info
*dwo_comp_unit_die
;
6352 if (comp_unit_die
->has_children
)
6354 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6355 " has children (offset %s) [in module %s]"),
6356 sect_offset_str (this_cu
->sect_off
),
6357 bfd_get_filename (abfd
));
6359 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
6360 if (dwo_unit
!= NULL
)
6362 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
6363 comp_unit_die
, NULL
,
6366 &m_dwo_abbrev_table
) == 0)
6372 comp_unit_die
= dwo_comp_unit_die
;
6376 /* Yikes, we couldn't find the rest of the DIE, we only have
6377 the stub. A complaint has already been logged. There's
6378 not much more we can do except pass on the stub DIE to
6379 die_reader_func. We don't want to throw an error on bad
6386 cutu_reader::keep ()
6388 /* Done, clean up. */
6389 gdb_assert (!dummy_p
);
6390 if (m_new_cu
!= NULL
)
6392 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6394 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
6395 per_objfile
->set_cu (m_this_cu
, m_new_cu
.release ());
6399 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6400 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6401 assumed to have already done the lookup to find the DWO file).
6403 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6404 THIS_CU->is_debug_types, but nothing else.
6406 We fill in THIS_CU->length.
6408 THIS_CU->cu is always freed when done.
6409 This is done in order to not leave THIS_CU->cu in a state where we have
6410 to care whether it refers to the "main" CU or the DWO CU.
6412 When parent_cu is passed, it is used to provide a default value for
6413 str_offsets_base and addr_base from the parent. */
6415 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6416 dwarf2_per_objfile
*per_objfile
,
6417 struct dwarf2_cu
*parent_cu
,
6418 struct dwo_file
*dwo_file
)
6419 : die_reader_specs
{},
6422 struct objfile
*objfile
= per_objfile
->objfile
;
6423 struct dwarf2_section_info
*section
= this_cu
->section
;
6424 bfd
*abfd
= section
->get_bfd_owner ();
6425 struct dwarf2_section_info
*abbrev_section
;
6426 const gdb_byte
*begin_info_ptr
, *info_ptr
;
6428 if (dwarf_die_debug
)
6429 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6430 this_cu
->is_debug_types
? "type" : "comp",
6431 sect_offset_str (this_cu
->sect_off
));
6433 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6435 abbrev_section
= (dwo_file
!= NULL
6436 ? &dwo_file
->sections
.abbrev
6437 : get_abbrev_section_for_cu (this_cu
));
6439 /* This is cheap if the section is already read in. */
6440 section
->read (objfile
);
6442 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6444 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6445 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
6446 section
, abbrev_section
, info_ptr
,
6447 (this_cu
->is_debug_types
6449 : rcuh_kind::COMPILE
));
6451 if (parent_cu
!= nullptr)
6453 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
6454 m_new_cu
->addr_base
= parent_cu
->addr_base
;
6456 this_cu
->length
= m_new_cu
->header
.get_length ();
6458 /* Skip dummy compilation units. */
6459 if (info_ptr
>= begin_info_ptr
+ this_cu
->length
6460 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6466 abbrev_section
->read (objfile
);
6467 m_abbrev_table_holder
6468 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
6470 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
6471 m_abbrev_table_holder
.get ());
6472 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6476 /* Type Unit Groups.
6478 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6479 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6480 so that all types coming from the same compilation (.o file) are grouped
6481 together. A future step could be to put the types in the same symtab as
6482 the CU the types ultimately came from. */
6485 hash_type_unit_group (const void *item
)
6487 const struct type_unit_group
*tu_group
6488 = (const struct type_unit_group
*) item
;
6490 return hash_stmt_list_entry (&tu_group
->hash
);
6494 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
6496 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
6497 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
6499 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
6502 /* Allocate a hash table for type unit groups. */
6505 allocate_type_unit_groups_table ()
6507 return htab_up (htab_create_alloc (3,
6508 hash_type_unit_group
,
6510 htab_delete_entry
<type_unit_group
>,
6514 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6515 partial symtabs. We combine several TUs per psymtab to not let the size
6516 of any one psymtab grow too big. */
6517 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6518 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6520 /* Helper routine for get_type_unit_group.
6521 Create the type_unit_group object used to hold one or more TUs. */
6523 static std::unique_ptr
<type_unit_group
>
6524 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
6526 std::unique_ptr
<type_unit_group
> tu_group (new type_unit_group
);
6528 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
6529 tu_group
->hash
.line_sect_off
= line_offset_struct
;
6534 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6535 STMT_LIST is a DW_AT_stmt_list attribute. */
6537 static struct type_unit_group
*
6538 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
6540 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6541 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6542 struct type_unit_group
*tu_group
;
6544 unsigned int line_offset
;
6545 struct type_unit_group type_unit_group_for_lookup
;
6547 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
6548 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
6550 /* Do we need to create a new group, or can we use an existing one? */
6552 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
6554 line_offset
= stmt_list
->as_unsigned ();
6555 ++tu_stats
->nr_symtab_sharers
;
6559 /* Ugh, no stmt_list. Rare, but we have to handle it.
6560 We can do various things here like create one group per TU or
6561 spread them over multiple groups to split up the expansion work.
6562 To avoid worst case scenarios (too many groups or too large groups)
6563 we, umm, group them in bunches. */
6564 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6565 | (tu_stats
->nr_stmt_less_type_units
6566 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
6567 ++tu_stats
->nr_stmt_less_type_units
;
6570 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
6571 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
6572 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
6573 &type_unit_group_for_lookup
, INSERT
);
6574 if (*slot
== nullptr)
6576 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
6577 std::unique_ptr
<type_unit_group
> grp
6578 = create_type_unit_group (cu
, line_offset_struct
);
6579 *slot
= grp
.release ();
6580 ++tu_stats
->nr_symtabs
;
6583 tu_group
= (struct type_unit_group
*) *slot
;
6584 gdb_assert (tu_group
!= nullptr);
6589 /* An instance of this is created when scanning DWARF to create a
6592 class cooked_index_storage
6596 cooked_index_storage ()
6597 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
6599 htab_delete_entry
<cutu_reader
>,
6601 m_index (new cooked_index
),
6602 m_addrmap_storage (),
6603 m_addrmap (addrmap_create_mutable (&m_addrmap_storage
))
6607 DISABLE_COPY_AND_ASSIGN (cooked_index_storage
);
6609 /* Return the current abbrev cache. */
6610 abbrev_cache
*get_abbrev_cache ()
6612 return &m_abbrev_cache
;
6615 /* Return the DIE reader corresponding to PER_CU. If no such reader
6616 has been registered, return NULL. */
6617 cutu_reader
*get_reader (dwarf2_per_cu_data
*per_cu
)
6619 int index
= per_cu
->index
;
6620 return (cutu_reader
*) htab_find_with_hash (m_reader_hash
.get (),
6624 /* Preserve READER by storing it in the local hash table. */
6625 cutu_reader
*preserve (std::unique_ptr
<cutu_reader
> reader
)
6627 m_abbrev_cache
.add (reader
->release_abbrev_table ());
6629 int index
= reader
->cu
->per_cu
->index
;
6630 void **slot
= htab_find_slot_with_hash (m_reader_hash
.get (), &index
,
6632 gdb_assert (*slot
== nullptr);
6633 cutu_reader
*result
= reader
.get ();
6634 *slot
= reader
.release ();
6638 /* Add an entry to the index. The arguments describe the entry; see
6639 cooked-index.h. The new entry is returned. */
6640 const cooked_index_entry
*add (sect_offset die_offset
, enum dwarf_tag tag
,
6641 cooked_index_flag flags
,
6643 const cooked_index_entry
*parent_entry
,
6644 dwarf2_per_cu_data
*per_cu
)
6646 return m_index
->add (die_offset
, tag
, flags
, name
, parent_entry
, per_cu
);
6649 /* Install the current addrmap into the index being constructed,
6650 then transfer ownership of the index to the caller. */
6651 std::unique_ptr
<cooked_index
> release ()
6653 m_index
->install_addrmap (m_addrmap
);
6654 return std::move (m_index
);
6657 /* Return the mutable addrmap that is currently being created. */
6658 addrmap
*get_addrmap ()
6665 /* Hash function for a cutu_reader. */
6666 static hashval_t
hash_cutu_reader (const void *a
)
6668 const cutu_reader
*reader
= (const cutu_reader
*) a
;
6669 return reader
->cu
->per_cu
->index
;
6672 /* Equality function for cutu_reader. */
6673 static int eq_cutu_reader (const void *a
, const void *b
)
6675 const cutu_reader
*ra
= (const cutu_reader
*) a
;
6676 const int *rb
= (const int *) b
;
6677 return ra
->cu
->per_cu
->index
== *rb
;
6680 /* The abbrev cache used by this indexer. */
6681 abbrev_cache m_abbrev_cache
;
6682 /* A hash table of cutu_reader objects. */
6683 htab_up m_reader_hash
;
6684 /* The index that is being constructed. */
6685 std::unique_ptr
<cooked_index
> m_index
;
6687 /* Storage for the writeable addrmap. */
6688 auto_obstack m_addrmap_storage
;
6689 /* A writeable addrmap being constructed by this scanner. */
6693 /* An instance of this is created to index a CU. */
6695 class cooked_indexer
6699 cooked_indexer (cooked_index_storage
*storage
,
6700 dwarf2_per_cu_data
*per_cu
,
6701 enum language language
)
6702 : m_index_storage (storage
),
6704 m_language (language
),
6706 m_die_range_map (addrmap_create_mutable (&m_obstack
))
6710 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
6712 /* Index the given CU. */
6713 void make_index (cutu_reader
*reader
);
6717 /* A helper function to turn a section offset into an address that
6718 can be used in an addrmap. */
6719 CORE_ADDR
form_addr (sect_offset offset
, bool is_dwz
)
6721 CORE_ADDR value
= to_underlying (offset
);
6723 value
|= ((CORE_ADDR
) 1) << (8 * sizeof (CORE_ADDR
) - 1);
6727 /* A helper function to scan the PC bounds of READER and record them
6728 in the storage's addrmap. */
6729 void check_bounds (cutu_reader
*reader
);
6731 /* Ensure that the indicated CU exists. The cutu_reader for it is
6732 returned. FOR_SCANNING is true if the caller intends to scan all
6733 the DIEs in the CU; when false, this use is assumed to be to look
6734 up just a single DIE. */
6735 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
6736 dwarf2_per_objfile
*per_objfile
,
6737 sect_offset sect_off
,
6741 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6742 the entry for the enclosing scope (nullptr at top level). FULLY
6743 is true when a full scan must be done -- in some languages,
6744 function scopes must be fully explored in order to find nested
6745 functions. This returns a pointer to just after the spot where
6747 const gdb_byte
*index_dies (cutu_reader
*reader
,
6748 const gdb_byte
*info_ptr
,
6749 const cooked_index_entry
*parent_entry
,
6752 /* Scan the attributes for a given DIE and update the out
6753 parameters. Returns a pointer to the byte after the DIE. */
6754 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
6755 cutu_reader
*reader
,
6756 const gdb_byte
*watermark_ptr
,
6757 const gdb_byte
*info_ptr
,
6758 const abbrev_info
*abbrev
,
6760 const char **linkage_name
,
6761 cooked_index_flag
*flags
,
6762 sect_offset
*sibling_offset
,
6763 const cooked_index_entry
**parent_entry
,
6764 CORE_ADDR
*maybe_defer
,
6765 bool for_specification
);
6767 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6768 DW_AT_import, and then scanning the referenced CU. Returns a
6769 pointer to the byte after the DIE. */
6770 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
6771 const gdb_byte
*info_ptr
,
6772 const abbrev_info
*abbrev
);
6774 /* Recursively read DIEs, recording the section offsets in
6775 m_die_range_map and then calling index_dies. */
6776 const gdb_byte
*recurse (cutu_reader
*reader
,
6777 const gdb_byte
*info_ptr
,
6778 const cooked_index_entry
*parent_entry
,
6781 /* The storage object, where the results are kept. */
6782 cooked_index_storage
*m_index_storage
;
6783 /* The CU that we are reading on behalf of. This object might be
6784 asked to index one CU but to treat the results as if they come
6785 from some including CU; in this case the including CU would be
6787 dwarf2_per_cu_data
*m_per_cu
;
6788 /* The language that we're assuming when reading. */
6789 enum language m_language
;
6791 /* Temporary storage. */
6792 auto_obstack m_obstack
;
6793 /* An addrmap that maps from section offsets (see the form_addr
6794 method) to newly-created entries. See m_deferred_entries to
6796 addrmap
*m_die_range_map
;
6798 /* A single deferred entry. */
6799 struct deferred_entry
6801 sect_offset die_offset
;
6803 CORE_ADDR spec_offset
;
6805 cooked_index_flag flags
;
6808 /* The generated DWARF can sometimes have the declaration for a
6809 method in a class (or perhaps namespace) scope, with the
6810 definition appearing outside this scope... just one of the many
6811 bad things about DWARF. In order to handle this situation, we
6812 defer certain entries until the end of scanning, at which point
6813 we'll know the containing context of all the DIEs that we might
6814 have scanned. This vector stores these deferred entries. */
6815 std::vector
<deferred_entry
> m_deferred_entries
;
6818 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6819 Process compilation unit THIS_CU for a psymtab. */
6822 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
6823 dwarf2_per_objfile
*per_objfile
,
6824 cooked_index_storage
*storage
)
6826 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
6827 storage
->get_abbrev_cache ());
6829 if (reader
.comp_unit_die
== nullptr)
6832 switch (reader
.comp_unit_die
->tag
)
6834 case DW_TAG_compile_unit
:
6835 this_cu
->unit_type
= DW_UT_compile
;
6837 case DW_TAG_partial_unit
:
6838 this_cu
->unit_type
= DW_UT_partial
;
6840 case DW_TAG_type_unit
:
6841 this_cu
->unit_type
= DW_UT_type
;
6844 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6845 dwarf_tag_name (reader
.comp_unit_die
->tag
),
6846 sect_offset_str (reader
.cu
->per_cu
->sect_off
),
6847 objfile_name (per_objfile
->objfile
));
6854 else if (this_cu
->is_debug_types
)
6855 build_type_psymtabs_reader (&reader
, storage
);
6856 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
6859 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
6861 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
6863 gdb_assert (storage
!= nullptr);
6864 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->per_cu
->lang
);
6865 indexer
.make_index (&reader
);
6870 /* Reader function for build_type_psymtabs. */
6873 build_type_psymtabs_reader (cutu_reader
*reader
,
6874 cooked_index_storage
*storage
)
6876 struct dwarf2_cu
*cu
= reader
->cu
;
6877 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6878 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
6880 gdb_assert (per_cu
->is_debug_types
);
6882 if (! type_unit_die
->has_children
)
6885 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
6887 gdb_assert (storage
!= nullptr);
6888 cooked_indexer
indexer (storage
, per_cu
, cu
->per_cu
->lang
);
6889 indexer
.make_index (reader
);
6892 /* Struct used to sort TUs by their abbreviation table offset. */
6894 struct tu_abbrev_offset
6896 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
6897 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
6900 /* This is used when sorting. */
6901 bool operator< (const tu_abbrev_offset
&other
) const
6903 return abbrev_offset
< other
.abbrev_offset
;
6906 signatured_type
*sig_type
;
6907 sect_offset abbrev_offset
;
6910 /* Efficiently read all the type units.
6912 The efficiency is because we sort TUs by the abbrev table they use and
6913 only read each abbrev table once. In one program there are 200K TUs
6914 sharing 8K abbrev tables.
6916 The main purpose of this function is to support building the
6917 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6918 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6919 can collapse the search space by grouping them by stmt_list.
6920 The savings can be significant, in the same program from above the 200K TUs
6921 share 8K stmt_list tables.
6923 FUNC is expected to call get_type_unit_group, which will create the
6924 struct type_unit_group if necessary and add it to
6925 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6928 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
6929 cooked_index_storage
*storage
)
6931 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6932 abbrev_table_up abbrev_table
;
6933 sect_offset abbrev_offset
;
6935 /* It's up to the caller to not call us multiple times. */
6936 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
6938 if (per_objfile
->per_bfd
->tu_stats
.nr_tus
== 0)
6941 /* TUs typically share abbrev tables, and there can be way more TUs than
6942 abbrev tables. Sort by abbrev table to reduce the number of times we
6943 read each abbrev table in.
6944 Alternatives are to punt or to maintain a cache of abbrev tables.
6945 This is simpler and efficient enough for now.
6947 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6948 symtab to use). Typically TUs with the same abbrev offset have the same
6949 stmt_list value too so in practice this should work well.
6951 The basic algorithm here is:
6953 sort TUs by abbrev table
6954 for each TU with same abbrev table:
6955 read abbrev table if first user
6956 read TU top level DIE
6957 [IWBN if DWO skeletons had DW_AT_stmt_list]
6960 dwarf_read_debug_printf ("Building type unit groups ...");
6962 /* Sort in a separate table to maintain the order of all_comp_units
6963 for .gdb_index: TU indices directly index all_type_units. */
6964 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
6965 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->tu_stats
.nr_tus
);
6967 for (const auto &cu
: per_objfile
->per_bfd
->all_comp_units
)
6969 if (cu
->is_debug_types
)
6971 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
6972 sorted_by_abbrev
.emplace_back
6973 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
6974 sig_type
->sect_off
));
6978 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
6980 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
6982 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
6984 /* Switch to the next abbrev table if necessary. */
6985 if (abbrev_table
== NULL
6986 || tu
.abbrev_offset
!= abbrev_offset
)
6988 abbrev_offset
= tu
.abbrev_offset
;
6989 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
6991 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
6992 ++tu_stats
->nr_uniq_abbrev_tables
;
6995 cutu_reader
reader (tu
.sig_type
, per_objfile
,
6996 abbrev_table
.get (), nullptr, false);
6997 if (!reader
.dummy_p
)
6998 build_type_psymtabs_reader (&reader
, storage
);
7002 /* Print collected type unit statistics. */
7005 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
7007 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
7009 dwarf_read_debug_printf ("Type unit statistics:");
7010 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
7011 dwarf_read_debug_printf (" %d uniq abbrev tables",
7012 tu_stats
->nr_uniq_abbrev_tables
);
7013 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7014 tu_stats
->nr_symtabs
);
7015 dwarf_read_debug_printf (" %d symtab sharers",
7016 tu_stats
->nr_symtab_sharers
);
7017 dwarf_read_debug_printf (" %d type units without a stmt_list",
7018 tu_stats
->nr_stmt_less_type_units
);
7019 dwarf_read_debug_printf (" %d all_type_units reallocs",
7020 tu_stats
->nr_all_type_units_reallocs
);
7023 struct skeleton_data
7025 dwarf2_per_objfile
*per_objfile
;
7026 cooked_index_storage
*storage
;
7029 /* Traversal function for process_skeletonless_type_unit.
7030 Read a TU in a DWO file and build partial symbols for it. */
7033 process_skeletonless_type_unit (void **slot
, void *info
)
7035 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
7036 skeleton_data
*data
= (skeleton_data
*) info
;
7038 /* If this TU doesn't exist in the global table, add it and read it in. */
7040 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
7041 data
->per_objfile
->per_bfd
->signatured_types
7042 = allocate_signatured_type_table ();
7044 signatured_type
find_entry (dwo_unit
->signature
);
7045 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
7046 &find_entry
, INSERT
);
7047 /* If we've already seen this type there's nothing to do. What's happening
7048 is we're doing our own version of comdat-folding here. */
7052 /* This does the job that create_all_comp_units would have done for
7054 signatured_type
*entry
7055 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
7056 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
7059 /* This does the job that build_type_psymtabs would have done. */
7060 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
7061 if (!reader
.dummy_p
)
7062 build_type_psymtabs_reader (&reader
, data
->storage
);
7067 /* Traversal function for process_skeletonless_type_units. */
7070 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
7072 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
7074 if (dwo_file
->tus
!= NULL
)
7075 htab_traverse_noresize (dwo_file
->tus
.get (),
7076 process_skeletonless_type_unit
, info
);
7081 /* Scan all TUs of DWO files, verifying we've processed them.
7082 This is needed in case a TU was emitted without its skeleton.
7083 Note: This can't be done until we know what all the DWO files are. */
7086 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
7087 cooked_index_storage
*storage
)
7089 skeleton_data data
{ per_objfile
, storage
};
7091 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7092 if (get_dwp_file (per_objfile
) == NULL
7093 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
7095 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
7096 process_dwo_file_for_skeletonless_type_units
,
7101 /* Build the partial symbol table by doing a quick pass through the
7102 .debug_info and .debug_abbrev sections. */
7105 dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
)
7107 struct objfile
*objfile
= per_objfile
->objfile
;
7108 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7110 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7111 objfile_name (objfile
));
7113 per_bfd
->map_info_sections (objfile
);
7115 cooked_index_storage index_storage
;
7116 create_all_comp_units (per_objfile
);
7117 build_type_psymtabs (per_objfile
, &index_storage
);
7118 std::vector
<std::unique_ptr
<cooked_index
>> indexes
;
7120 per_bfd
->quick_file_names_table
7121 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
7122 if (!per_bfd
->debug_aranges
.empty ())
7123 read_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
,
7124 index_storage
.get_addrmap ());
7127 /* Ensure that complaints are handled correctly. */
7128 complaint_interceptor complaint_handler
;
7130 using iter_type
= decltype (per_bfd
->all_comp_units
.begin ());
7132 /* Each thread returns a pair holding a cooked index, and a vector
7133 of errors that should be printed. The latter is done because
7134 GDB's I/O system is not thread-safe. run_on_main_thread could be
7135 used, but that would mean the messages are printed after the
7136 prompt, which looks weird. */
7137 using result_type
= std::pair
<std::unique_ptr
<cooked_index
>,
7138 std::vector
<gdb_exception
>>;
7139 std::vector
<result_type
> results
7140 = gdb::parallel_for_each (1, per_bfd
->all_comp_units
.begin (),
7141 per_bfd
->all_comp_units
.end (),
7142 [=] (iter_type iter
, iter_type end
)
7144 std::vector
<gdb_exception
> errors
;
7145 cooked_index_storage thread_storage
;
7146 for (; iter
!= end
; ++iter
)
7148 dwarf2_per_cu_data
*per_cu
= iter
->get ();
7151 process_psymtab_comp_unit (per_cu
, per_objfile
,
7154 catch (gdb_exception
&except
)
7156 errors
.push_back (std::move (except
));
7159 return result_type (thread_storage
.release (), std::move (errors
));
7162 /* Only show a given exception a single time. */
7163 std::unordered_set
<gdb_exception
> seen_exceptions
;
7164 for (auto &one_result
: results
)
7166 indexes
.push_back (std::move (one_result
.first
));
7167 for (auto &one_exc
: one_result
.second
)
7168 if (seen_exceptions
.insert (one_exc
).second
)
7169 exception_print (gdb_stderr
, one_exc
);
7173 /* This has to wait until we read the CUs, we need the list of DWOs. */
7174 process_skeletonless_type_units (per_objfile
, &index_storage
);
7176 if (dwarf_read_debug
> 0)
7177 print_tu_stats (per_objfile
);
7179 indexes
.push_back (index_storage
.release ());
7180 /* Remove any NULL entries. This might happen if parallel-for
7181 decides to throttle the number of threads that were used. */
7183 (std::remove_if (indexes
.begin (),
7185 [] (const std::unique_ptr
<cooked_index
> &entry
)
7187 return entry
== nullptr;
7190 indexes
.shrink_to_fit ();
7191 per_bfd
->cooked_index_table
.reset
7192 (new cooked_index_vector (std::move (indexes
)));
7194 const cooked_index_entry
*main_entry
7195 = per_bfd
->cooked_index_table
->get_main ();
7196 if (main_entry
!= nullptr)
7197 set_objfile_main_name (objfile
, main_entry
->name
,
7198 main_entry
->per_cu
->lang
);
7200 dwarf_read_debug_printf ("Done building psymtabs of %s",
7201 objfile_name (objfile
));
7205 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
7206 struct dwarf2_section_info
*section
,
7207 struct dwarf2_section_info
*abbrev_section
,
7208 unsigned int is_dwz
,
7209 htab_up
&types_htab
,
7210 rcuh_kind section_kind
)
7212 const gdb_byte
*info_ptr
;
7213 struct objfile
*objfile
= per_objfile
->objfile
;
7215 dwarf_read_debug_printf ("Reading %s for %s",
7216 section
->get_name (),
7217 section
->get_file_name ());
7219 section
->read (objfile
);
7221 info_ptr
= section
->buffer
;
7223 while (info_ptr
< section
->buffer
+ section
->size
)
7225 dwarf2_per_cu_data_up this_cu
;
7227 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
7229 comp_unit_head cu_header
;
7230 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
7231 abbrev_section
, info_ptr
,
7234 /* Save the compilation unit for later lookup. */
7235 if (cu_header
.unit_type
!= DW_UT_type
)
7236 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
7239 if (types_htab
== nullptr)
7240 types_htab
= allocate_signatured_type_table ();
7242 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
7243 (cu_header
.signature
);
7244 signatured_type
*sig_ptr
= sig_type
.get ();
7245 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
7246 this_cu
.reset (sig_type
.release ());
7248 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
7249 gdb_assert (slot
!= nullptr);
7250 if (*slot
!= nullptr)
7251 complaint (_("debug type entry at offset %s is duplicate to"
7252 " the entry at offset %s, signature %s"),
7253 sect_offset_str (sect_off
),
7254 sect_offset_str (sig_ptr
->sect_off
),
7255 hex_string (sig_ptr
->signature
));
7258 this_cu
->sect_off
= sect_off
;
7259 this_cu
->length
= cu_header
.length
+ cu_header
.initial_length_size
;
7260 this_cu
->is_dwz
= is_dwz
;
7261 this_cu
->section
= section
;
7263 info_ptr
= info_ptr
+ this_cu
->length
;
7264 per_objfile
->per_bfd
->all_comp_units
.push_back (std::move (this_cu
));
7268 /* Create a list of all compilation units in OBJFILE.
7269 This is only done for -readnow and building partial symtabs. */
7272 create_all_comp_units (dwarf2_per_objfile
*per_objfile
)
7276 read_comp_units_from_section (per_objfile
, &per_objfile
->per_bfd
->info
,
7277 &per_objfile
->per_bfd
->abbrev
, 0,
7278 types_htab
, rcuh_kind::COMPILE
);
7279 for (dwarf2_section_info
§ion
: per_objfile
->per_bfd
->types
)
7280 read_comp_units_from_section (per_objfile
, §ion
,
7281 &per_objfile
->per_bfd
->abbrev
, 0,
7282 types_htab
, rcuh_kind::TYPE
);
7284 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
7287 /* Pre-read the sections we'll need to construct an index. */
7288 struct objfile
*objfile
= per_objfile
->objfile
;
7289 dwz
->abbrev
.read (objfile
);
7290 dwz
->info
.read (objfile
);
7291 dwz
->str
.read (objfile
);
7292 dwz
->line
.read (objfile
);
7293 read_comp_units_from_section (per_objfile
, &dwz
->info
, &dwz
->abbrev
, 1,
7294 types_htab
, rcuh_kind::COMPILE
);
7297 per_objfile
->per_bfd
->signatured_types
= std::move (types_htab
);
7300 /* Return the initial uleb128 in the die at INFO_PTR. */
7303 peek_abbrev_code (bfd
*abfd
, const gdb_byte
*info_ptr
)
7305 unsigned int bytes_read
;
7307 return read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7310 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7311 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7313 Return the corresponding abbrev, or NULL if the number is zero (indicating
7314 an empty DIE). In either case *BYTES_READ will be set to the length of
7315 the initial number. */
7317 static const struct abbrev_info
*
7318 peek_die_abbrev (const die_reader_specs
&reader
,
7319 const gdb_byte
*info_ptr
, unsigned int *bytes_read
)
7321 dwarf2_cu
*cu
= reader
.cu
;
7322 bfd
*abfd
= reader
.abfd
;
7323 unsigned int abbrev_number
7324 = read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
7326 if (abbrev_number
== 0)
7329 const abbrev_info
*abbrev
7330 = reader
.abbrev_table
->lookup_abbrev (abbrev_number
);
7333 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7334 " at offset %s [in module %s]"),
7335 abbrev_number
, cu
->per_cu
->is_debug_types
? "TU" : "CU",
7336 sect_offset_str (cu
->header
.sect_off
), bfd_get_filename (abfd
));
7342 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7343 Returns a pointer to the end of a series of DIEs, terminated by an empty
7344 DIE. Any children of the skipped DIEs will also be skipped. */
7346 static const gdb_byte
*
7347 skip_children (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
)
7351 unsigned int bytes_read
;
7352 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
7356 return info_ptr
+ bytes_read
;
7358 info_ptr
= skip_one_die (reader
, info_ptr
+ bytes_read
, abbrev
);
7362 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7363 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7364 abbrev corresponding to that skipped uleb128 should be passed in
7367 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7368 returns a pointer to this DIE's sibling, skipping any children.
7369 Otherwise, returns a pointer to the DIE's first child. */
7371 static const gdb_byte
*
7372 skip_one_die (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
,
7373 const struct abbrev_info
*abbrev
, bool do_skip_children
)
7375 unsigned int bytes_read
;
7376 struct attribute attr
;
7377 bfd
*abfd
= reader
->abfd
;
7378 struct dwarf2_cu
*cu
= reader
->cu
;
7379 const gdb_byte
*buffer
= reader
->buffer
;
7380 const gdb_byte
*buffer_end
= reader
->buffer_end
;
7381 unsigned int form
, i
;
7383 if (do_skip_children
&& abbrev
->sibling_offset
!= (unsigned short) -1)
7385 /* We only handle DW_FORM_ref4 here. */
7386 const gdb_byte
*sibling_data
= info_ptr
+ abbrev
->sibling_offset
;
7387 unsigned int offset
= read_4_bytes (abfd
, sibling_data
);
7388 const gdb_byte
*sibling_ptr
7389 = buffer
+ to_underlying (cu
->header
.sect_off
) + offset
;
7390 if (sibling_ptr
>= info_ptr
&& sibling_ptr
< reader
->buffer_end
)
7392 /* Fall through to the slow way. */
7394 else if (abbrev
->size_if_constant
!= 0)
7396 info_ptr
+= abbrev
->size_if_constant
;
7397 if (do_skip_children
&& abbrev
->has_children
)
7398 return skip_children (reader
, info_ptr
);
7402 for (i
= 0; i
< abbrev
->num_attrs
; i
++)
7404 /* The only abbrev we care about is DW_AT_sibling. */
7405 if (do_skip_children
&& abbrev
->attrs
[i
].name
== DW_AT_sibling
)
7407 read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
7408 if (attr
.form
== DW_FORM_ref_addr
)
7409 complaint (_("ignoring absolute DW_AT_sibling"));
7412 sect_offset off
= attr
.get_ref_die_offset ();
7413 const gdb_byte
*sibling_ptr
= buffer
+ to_underlying (off
);
7415 if (sibling_ptr
< info_ptr
)
7416 complaint (_("DW_AT_sibling points backwards"));
7417 else if (sibling_ptr
> reader
->buffer_end
)
7418 reader
->die_section
->overflow_complaint ();
7424 /* If it isn't DW_AT_sibling, skip this attribute. */
7425 form
= abbrev
->attrs
[i
].form
;
7429 case DW_FORM_ref_addr
:
7430 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7431 and later it is offset sized. */
7432 if (cu
->header
.version
== 2)
7433 info_ptr
+= cu
->header
.addr_size
;
7435 info_ptr
+= cu
->header
.offset_size
;
7437 case DW_FORM_GNU_ref_alt
:
7438 info_ptr
+= cu
->header
.offset_size
;
7441 info_ptr
+= cu
->header
.addr_size
;
7449 case DW_FORM_flag_present
:
7450 case DW_FORM_implicit_const
:
7467 case DW_FORM_ref_sig8
:
7470 case DW_FORM_data16
:
7473 case DW_FORM_string
:
7474 read_direct_string (abfd
, info_ptr
, &bytes_read
);
7475 info_ptr
+= bytes_read
;
7477 case DW_FORM_sec_offset
:
7479 case DW_FORM_GNU_strp_alt
:
7480 info_ptr
+= cu
->header
.offset_size
;
7482 case DW_FORM_exprloc
:
7484 info_ptr
+= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7485 info_ptr
+= bytes_read
;
7487 case DW_FORM_block1
:
7488 info_ptr
+= 1 + read_1_byte (abfd
, info_ptr
);
7490 case DW_FORM_block2
:
7491 info_ptr
+= 2 + read_2_bytes (abfd
, info_ptr
);
7493 case DW_FORM_block4
:
7494 info_ptr
+= 4 + read_4_bytes (abfd
, info_ptr
);
7500 case DW_FORM_ref_udata
:
7501 case DW_FORM_GNU_addr_index
:
7502 case DW_FORM_GNU_str_index
:
7503 case DW_FORM_rnglistx
:
7504 case DW_FORM_loclistx
:
7505 info_ptr
= safe_skip_leb128 (info_ptr
, buffer_end
);
7507 case DW_FORM_indirect
:
7508 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7509 info_ptr
+= bytes_read
;
7510 /* We need to continue parsing from here, so just go back to
7512 goto skip_attribute
;
7515 error (_("Dwarf Error: Cannot handle %s "
7516 "in DWARF reader [in module %s]"),
7517 dwarf_form_name (form
),
7518 bfd_get_filename (abfd
));
7522 if (do_skip_children
&& abbrev
->has_children
)
7523 return skip_children (reader
, info_ptr
);
7528 /* Reading in full CUs. */
7530 /* Add PER_CU to the queue. */
7533 queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
7534 dwarf2_per_objfile
*per_objfile
,
7535 enum language pretend_language
)
7539 gdb_assert (per_objfile
->per_bfd
->queue
.has_value ());
7540 per_cu
->per_bfd
->queue
->emplace (per_cu
, per_objfile
, pretend_language
);
7543 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7545 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7548 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7549 DIEs, false otherwise.
7551 Explanation: there is an invariant that if a CU is queued for expansion
7552 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7553 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7554 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7555 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7556 invariant is respected.
7558 The caller is therefore not required to load the CU's DIEs (we return false)
7561 - the CU is already expanded, and therefore does not get enqueued
7562 - the CU gets enqueued for expansion, but its DIEs are already loaded
7564 Note that the caller should not use this function's return value as an
7565 indicator of whether the CU's DIEs are loaded right now, it should check
7566 that by calling `dwarf2_per_objfile::get_cu` instead. */
7569 maybe_queue_comp_unit (struct dwarf2_cu
*dependent_cu
,
7570 dwarf2_per_cu_data
*per_cu
,
7571 dwarf2_per_objfile
*per_objfile
,
7572 enum language pretend_language
)
7574 /* Mark the dependence relation so that we don't flush PER_CU
7576 if (dependent_cu
!= NULL
)
7577 dependent_cu
->add_dependence (per_cu
);
7579 /* If it's already on the queue, we have nothing to do. */
7582 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7584 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
7586 /* If the CU is queued for expansion, it should not already be
7588 gdb_assert (!per_objfile
->symtab_set_p (per_cu
));
7590 /* The DIEs are already loaded, the caller doesn't need to do it. */
7594 bool queued
= false;
7595 if (!per_objfile
->symtab_set_p (per_cu
))
7597 /* Add it to the queue. */
7598 queue_comp_unit (per_cu
, per_objfile
, pretend_language
);
7602 /* If the compilation unit is already loaded, just mark it as
7604 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7608 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7609 and the DIEs are not already loaded. */
7610 return queued
&& cu
== nullptr;
7613 /* Process the queue. */
7616 process_queue (dwarf2_per_objfile
*per_objfile
)
7618 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7619 objfile_name (per_objfile
->objfile
));
7621 /* The queue starts out with one item, but following a DIE reference
7622 may load a new CU, adding it to the end of the queue. */
7623 while (!per_objfile
->per_bfd
->queue
->empty ())
7625 dwarf2_queue_item
&item
= per_objfile
->per_bfd
->queue
->front ();
7626 dwarf2_per_cu_data
*per_cu
= item
.per_cu
;
7628 if (!per_objfile
->symtab_set_p (per_cu
))
7630 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7632 /* Skip dummy CUs. */
7635 unsigned int debug_print_threshold
;
7638 if (per_cu
->is_debug_types
)
7640 struct signatured_type
*sig_type
=
7641 (struct signatured_type
*) per_cu
;
7643 sprintf (buf
, "TU %s at offset %s",
7644 hex_string (sig_type
->signature
),
7645 sect_offset_str (per_cu
->sect_off
));
7646 /* There can be 100s of TUs.
7647 Only print them in verbose mode. */
7648 debug_print_threshold
= 2;
7652 sprintf (buf
, "CU at offset %s",
7653 sect_offset_str (per_cu
->sect_off
));
7654 debug_print_threshold
= 1;
7657 if (dwarf_read_debug
>= debug_print_threshold
)
7658 dwarf_read_debug_printf ("Expanding symtab of %s", buf
);
7660 if (per_cu
->is_debug_types
)
7661 process_full_type_unit (cu
, item
.pretend_language
);
7663 process_full_comp_unit (cu
, item
.pretend_language
);
7665 if (dwarf_read_debug
>= debug_print_threshold
)
7666 dwarf_read_debug_printf ("Done expanding %s", buf
);
7671 per_objfile
->per_bfd
->queue
->pop ();
7674 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7675 objfile_name (per_objfile
->objfile
));
7678 /* Trivial hash function for die_info: the hash value of a DIE
7679 is its offset in .debug_info for this objfile. */
7682 die_hash (const void *item
)
7684 const struct die_info
*die
= (const struct die_info
*) item
;
7686 return to_underlying (die
->sect_off
);
7689 /* Trivial comparison function for die_info structures: two DIEs
7690 are equal if they have the same offset. */
7693 die_eq (const void *item_lhs
, const void *item_rhs
)
7695 const struct die_info
*die_lhs
= (const struct die_info
*) item_lhs
;
7696 const struct die_info
*die_rhs
= (const struct die_info
*) item_rhs
;
7698 return die_lhs
->sect_off
== die_rhs
->sect_off
;
7701 /* Load the DIEs associated with PER_CU into memory.
7703 In some cases, the caller, while reading partial symbols, will need to load
7704 the full symbols for the CU for some reason. It will already have a
7705 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7706 rather than creating a new one. */
7709 load_full_comp_unit (dwarf2_per_cu_data
*this_cu
,
7710 dwarf2_per_objfile
*per_objfile
,
7711 dwarf2_cu
*existing_cu
,
7713 enum language pretend_language
)
7715 gdb_assert (! this_cu
->is_debug_types
);
7717 cutu_reader
reader (this_cu
, per_objfile
, NULL
, existing_cu
, skip_partial
);
7721 struct dwarf2_cu
*cu
= reader
.cu
;
7722 const gdb_byte
*info_ptr
= reader
.info_ptr
;
7724 gdb_assert (cu
->die_hash
== NULL
);
7726 htab_create_alloc_ex (cu
->header
.length
/ 12,
7730 &cu
->comp_unit_obstack
,
7731 hashtab_obstack_allocate
,
7732 dummy_obstack_deallocate
);
7734 if (reader
.comp_unit_die
->has_children
)
7735 reader
.comp_unit_die
->child
7736 = read_die_and_siblings (&reader
, reader
.info_ptr
,
7737 &info_ptr
, reader
.comp_unit_die
);
7738 cu
->dies
= reader
.comp_unit_die
;
7739 /* comp_unit_die is not stored in die_hash, no need. */
7741 /* We try not to read any attributes in this function, because not
7742 all CUs needed for references have been loaded yet, and symbol
7743 table processing isn't initialized. But we have to set the CU language,
7744 or we won't be able to build types correctly.
7745 Similarly, if we do not read the producer, we can not apply
7746 producer-specific interpretation. */
7747 prepare_one_comp_unit (cu
, cu
->dies
, pretend_language
);
7752 /* Add a DIE to the delayed physname list. */
7755 add_to_method_list (struct type
*type
, int fnfield_index
, int index
,
7756 const char *name
, struct die_info
*die
,
7757 struct dwarf2_cu
*cu
)
7759 struct delayed_method_info mi
;
7761 mi
.fnfield_index
= fnfield_index
;
7765 cu
->method_list
.push_back (mi
);
7768 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7769 "const" / "volatile". If so, decrements LEN by the length of the
7770 modifier and return true. Otherwise return false. */
7774 check_modifier (const char *physname
, size_t &len
, const char (&mod
)[N
])
7776 size_t mod_len
= sizeof (mod
) - 1;
7777 if (len
> mod_len
&& startswith (physname
+ (len
- mod_len
), mod
))
7785 /* Compute the physnames of any methods on the CU's method list.
7787 The computation of method physnames is delayed in order to avoid the
7788 (bad) condition that one of the method's formal parameters is of an as yet
7792 compute_delayed_physnames (struct dwarf2_cu
*cu
)
7794 /* Only C++ delays computing physnames. */
7795 if (cu
->method_list
.empty ())
7797 gdb_assert (cu
->per_cu
->lang
== language_cplus
);
7799 for (const delayed_method_info
&mi
: cu
->method_list
)
7801 const char *physname
;
7802 struct fn_fieldlist
*fn_flp
7803 = &TYPE_FN_FIELDLIST (mi
.type
, mi
.fnfield_index
);
7804 physname
= dwarf2_physname (mi
.name
, mi
.die
, cu
);
7805 TYPE_FN_FIELD_PHYSNAME (fn_flp
->fn_fields
, mi
.index
)
7806 = physname
? physname
: "";
7808 /* Since there's no tag to indicate whether a method is a
7809 const/volatile overload, extract that information out of the
7811 if (physname
!= NULL
)
7813 size_t len
= strlen (physname
);
7817 if (physname
[len
] == ')') /* shortcut */
7819 else if (check_modifier (physname
, len
, " const"))
7820 TYPE_FN_FIELD_CONST (fn_flp
->fn_fields
, mi
.index
) = 1;
7821 else if (check_modifier (physname
, len
, " volatile"))
7822 TYPE_FN_FIELD_VOLATILE (fn_flp
->fn_fields
, mi
.index
) = 1;
7829 /* The list is no longer needed. */
7830 cu
->method_list
.clear ();
7833 /* Go objects should be embedded in a DW_TAG_module DIE,
7834 and it's not clear if/how imported objects will appear.
7835 To keep Go support simple until that's worked out,
7836 go back through what we've read and create something usable.
7837 We could do this while processing each DIE, and feels kinda cleaner,
7838 but that way is more invasive.
7839 This is to, for example, allow the user to type "p var" or "b main"
7840 without having to specify the package name, and allow lookups
7841 of module.object to work in contexts that use the expression
7845 fixup_go_packaging (struct dwarf2_cu
*cu
)
7847 gdb::unique_xmalloc_ptr
<char> package_name
;
7848 struct pending
*list
;
7851 for (list
= *cu
->get_builder ()->get_global_symbols ();
7855 for (i
= 0; i
< list
->nsyms
; ++i
)
7857 struct symbol
*sym
= list
->symbol
[i
];
7859 if (sym
->language () == language_go
7860 && sym
->aclass () == LOC_BLOCK
)
7862 gdb::unique_xmalloc_ptr
<char> this_package_name
7863 (go_symbol_package_name (sym
));
7865 if (this_package_name
== NULL
)
7867 if (package_name
== NULL
)
7868 package_name
= std::move (this_package_name
);
7871 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7872 if (strcmp (package_name
.get (), this_package_name
.get ()) != 0)
7873 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7874 (symbol_symtab (sym
) != NULL
7875 ? symtab_to_filename_for_display
7876 (symbol_symtab (sym
))
7877 : objfile_name (objfile
)),
7878 this_package_name
.get (), package_name
.get ());
7884 if (package_name
!= NULL
)
7886 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7887 const char *saved_package_name
= objfile
->intern (package_name
.get ());
7888 struct type
*type
= init_type (objfile
, TYPE_CODE_MODULE
, 0,
7889 saved_package_name
);
7892 sym
= new (&objfile
->objfile_obstack
) symbol
;
7893 sym
->set_language (language_go
, &objfile
->objfile_obstack
);
7894 sym
->compute_and_set_names (saved_package_name
, false, objfile
->per_bfd
);
7895 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7896 e.g., "main" finds the "main" module and not C's main(). */
7897 sym
->set_domain (STRUCT_DOMAIN
);
7898 sym
->set_aclass_index (LOC_TYPEDEF
);
7899 sym
->set_type (type
);
7901 add_symbol_to_list (sym
, cu
->get_builder ()->get_global_symbols ());
7905 /* Allocate a fully-qualified name consisting of the two parts on the
7909 rust_fully_qualify (struct obstack
*obstack
, const char *p1
, const char *p2
)
7911 return obconcat (obstack
, p1
, "::", p2
, (char *) NULL
);
7914 /* A helper that allocates a variant part to attach to a Rust enum
7915 type. OBSTACK is where the results should be allocated. TYPE is
7916 the type we're processing. DISCRIMINANT_INDEX is the index of the
7917 discriminant. It must be the index of one of the fields of TYPE,
7918 or -1 to mean there is no discriminant (univariant enum).
7919 DEFAULT_INDEX is the index of the default field; or -1 if there is
7920 no default. RANGES is indexed by "effective" field number (the
7921 field index, but omitting the discriminant and default fields) and
7922 must hold the discriminant values used by the variants. Note that
7923 RANGES must have a lifetime at least as long as OBSTACK -- either
7924 already allocated on it, or static. */
7927 alloc_rust_variant (struct obstack
*obstack
, struct type
*type
,
7928 int discriminant_index
, int default_index
,
7929 gdb::array_view
<discriminant_range
> ranges
)
7931 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7932 gdb_assert (discriminant_index
== -1
7933 || (discriminant_index
>= 0
7934 && discriminant_index
< type
->num_fields ()));
7935 gdb_assert (default_index
== -1
7936 || (default_index
>= 0 && default_index
< type
->num_fields ()));
7938 /* We have one variant for each non-discriminant field. */
7939 int n_variants
= type
->num_fields ();
7940 if (discriminant_index
!= -1)
7943 variant
*variants
= new (obstack
) variant
[n_variants
];
7946 for (int i
= 0; i
< type
->num_fields (); ++i
)
7948 if (i
== discriminant_index
)
7951 variants
[var_idx
].first_field
= i
;
7952 variants
[var_idx
].last_field
= i
+ 1;
7954 /* The default field does not need a range, but other fields do.
7955 We skipped the discriminant above. */
7956 if (i
!= default_index
)
7958 variants
[var_idx
].discriminants
= ranges
.slice (range_idx
, 1);
7965 gdb_assert (range_idx
== ranges
.size ());
7966 gdb_assert (var_idx
== n_variants
);
7968 variant_part
*part
= new (obstack
) variant_part
;
7969 part
->discriminant_index
= discriminant_index
;
7970 /* If there is no discriminant, then whether it is signed is of no
7973 = (discriminant_index
== -1
7975 : type
->field (discriminant_index
).type ()->is_unsigned ());
7976 part
->variants
= gdb::array_view
<variant
> (variants
, n_variants
);
7978 void *storage
= obstack_alloc (obstack
, sizeof (gdb::array_view
<variant_part
>));
7979 gdb::array_view
<variant_part
> *prop_value
7980 = new (storage
) gdb::array_view
<variant_part
> (part
, 1);
7982 struct dynamic_prop prop
;
7983 prop
.set_variant_parts (prop_value
);
7985 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
7988 /* Some versions of rustc emitted enums in an unusual way.
7990 Ordinary enums were emitted as unions. The first element of each
7991 structure in the union was named "RUST$ENUM$DISR". This element
7992 held the discriminant.
7994 These versions of Rust also implemented the "non-zero"
7995 optimization. When the enum had two values, and one is empty and
7996 the other holds a pointer that cannot be zero, the pointer is used
7997 as the discriminant, with a zero value meaning the empty variant.
7998 Here, the union's first member is of the form
7999 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
8000 where the fieldnos are the indices of the fields that should be
8001 traversed in order to find the field (which may be several fields deep)
8002 and the variantname is the name of the variant of the case when the
8005 This function recognizes whether TYPE is of one of these forms,
8006 and, if so, smashes it to be a variant type. */
8009 quirk_rust_enum (struct type
*type
, struct objfile
*objfile
)
8011 gdb_assert (type
->code () == TYPE_CODE_UNION
);
8013 /* We don't need to deal with empty enums. */
8014 if (type
->num_fields () == 0)
8017 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
8018 if (type
->num_fields () == 1
8019 && startswith (type
->field (0).name (), RUST_ENUM_PREFIX
))
8021 const char *name
= type
->field (0).name () + strlen (RUST_ENUM_PREFIX
);
8023 /* Decode the field name to find the offset of the
8025 ULONGEST bit_offset
= 0;
8026 struct type
*field_type
= type
->field (0).type ();
8027 while (name
[0] >= '0' && name
[0] <= '9')
8030 unsigned long index
= strtoul (name
, &tail
, 10);
8033 || index
>= field_type
->num_fields ()
8034 || (field_type
->field (index
).loc_kind ()
8035 != FIELD_LOC_KIND_BITPOS
))
8037 complaint (_("Could not parse Rust enum encoding string \"%s\""
8039 type
->field (0).name (),
8040 objfile_name (objfile
));
8045 bit_offset
+= field_type
->field (index
).loc_bitpos ();
8046 field_type
= field_type
->field (index
).type ();
8049 /* Smash this type to be a structure type. We have to do this
8050 because the type has already been recorded. */
8051 type
->set_code (TYPE_CODE_STRUCT
);
8052 type
->set_num_fields (3);
8053 /* Save the field we care about. */
8054 struct field saved_field
= type
->field (0);
8056 ((struct field
*) TYPE_ZALLOC (type
, 3 * sizeof (struct field
)));
8058 /* Put the discriminant at index 0. */
8059 type
->field (0).set_type (field_type
);
8060 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8061 type
->field (0).set_name ("<<discriminant>>");
8062 type
->field (0).set_loc_bitpos (bit_offset
);
8064 /* The order of fields doesn't really matter, so put the real
8065 field at index 1 and the data-less field at index 2. */
8066 type
->field (1) = saved_field
;
8067 type
->field (1).set_name
8068 (rust_last_path_segment (type
->field (1).type ()->name ()));
8069 type
->field (1).type ()->set_name
8070 (rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8071 type
->field (1).name ()));
8073 const char *dataless_name
8074 = rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8076 struct type
*dataless_type
= init_type (objfile
, TYPE_CODE_VOID
, 0,
8078 type
->field (2).set_type (dataless_type
);
8079 /* NAME points into the original discriminant name, which
8080 already has the correct lifetime. */
8081 type
->field (2).set_name (name
);
8082 type
->field (2).set_loc_bitpos (0);
8084 /* Indicate that this is a variant type. */
8085 static discriminant_range ranges
[1] = { { 0, 0 } };
8086 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, 1, ranges
);
8088 /* A union with a single anonymous field is probably an old-style
8090 else if (type
->num_fields () == 1 && streq (type
->field (0).name (), ""))
8092 /* Smash this type to be a structure type. We have to do this
8093 because the type has already been recorded. */
8094 type
->set_code (TYPE_CODE_STRUCT
);
8096 struct type
*field_type
= type
->field (0).type ();
8097 const char *variant_name
8098 = rust_last_path_segment (field_type
->name ());
8099 type
->field (0).set_name (variant_name
);
8100 field_type
->set_name
8101 (rust_fully_qualify (&objfile
->objfile_obstack
,
8102 type
->name (), variant_name
));
8104 alloc_rust_variant (&objfile
->objfile_obstack
, type
, -1, 0, {});
8108 struct type
*disr_type
= nullptr;
8109 for (int i
= 0; i
< type
->num_fields (); ++i
)
8111 disr_type
= type
->field (i
).type ();
8113 if (disr_type
->code () != TYPE_CODE_STRUCT
)
8115 /* All fields of a true enum will be structs. */
8118 else if (disr_type
->num_fields () == 0)
8120 /* Could be data-less variant, so keep going. */
8121 disr_type
= nullptr;
8123 else if (strcmp (disr_type
->field (0).name (),
8124 "RUST$ENUM$DISR") != 0)
8126 /* Not a Rust enum. */
8136 /* If we got here without a discriminant, then it's probably
8138 if (disr_type
== nullptr)
8141 /* Smash this type to be a structure type. We have to do this
8142 because the type has already been recorded. */
8143 type
->set_code (TYPE_CODE_STRUCT
);
8145 /* Make space for the discriminant field. */
8146 struct field
*disr_field
= &disr_type
->field (0);
8148 = (struct field
*) TYPE_ZALLOC (type
, ((type
->num_fields () + 1)
8149 * sizeof (struct field
)));
8150 memcpy (new_fields
+ 1, type
->fields (),
8151 type
->num_fields () * sizeof (struct field
));
8152 type
->set_fields (new_fields
);
8153 type
->set_num_fields (type
->num_fields () + 1);
8155 /* Install the discriminant at index 0 in the union. */
8156 type
->field (0) = *disr_field
;
8157 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8158 type
->field (0).set_name ("<<discriminant>>");
8160 /* We need a way to find the correct discriminant given a
8161 variant name. For convenience we build a map here. */
8162 struct type
*enum_type
= disr_field
->type ();
8163 std::unordered_map
<std::string
, ULONGEST
> discriminant_map
;
8164 for (int i
= 0; i
< enum_type
->num_fields (); ++i
)
8166 if (enum_type
->field (i
).loc_kind () == FIELD_LOC_KIND_ENUMVAL
)
8169 = rust_last_path_segment (enum_type
->field (i
).name ());
8170 discriminant_map
[name
] = enum_type
->field (i
).loc_enumval ();
8174 int n_fields
= type
->num_fields ();
8175 /* We don't need a range entry for the discriminant, but we do
8176 need one for every other field, as there is no default
8178 discriminant_range
*ranges
= XOBNEWVEC (&objfile
->objfile_obstack
,
8181 /* Skip the discriminant here. */
8182 for (int i
= 1; i
< n_fields
; ++i
)
8184 /* Find the final word in the name of this variant's type.
8185 That name can be used to look up the correct
8187 const char *variant_name
8188 = rust_last_path_segment (type
->field (i
).type ()->name ());
8190 auto iter
= discriminant_map
.find (variant_name
);
8191 if (iter
!= discriminant_map
.end ())
8193 ranges
[i
- 1].low
= iter
->second
;
8194 ranges
[i
- 1].high
= iter
->second
;
8197 /* In Rust, each element should have the size of the
8199 TYPE_LENGTH (type
->field (i
).type ()) = TYPE_LENGTH (type
);
8201 /* Remove the discriminant field, if it exists. */
8202 struct type
*sub_type
= type
->field (i
).type ();
8203 if (sub_type
->num_fields () > 0)
8205 sub_type
->set_num_fields (sub_type
->num_fields () - 1);
8206 sub_type
->set_fields (sub_type
->fields () + 1);
8208 type
->field (i
).set_name (variant_name
);
8210 (rust_fully_qualify (&objfile
->objfile_obstack
,
8211 type
->name (), variant_name
));
8214 /* Indicate that this is a variant type. */
8215 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, -1,
8216 gdb::array_view
<discriminant_range
> (ranges
,
8221 /* Rewrite some Rust unions to be structures with variants parts. */
8224 rust_union_quirks (struct dwarf2_cu
*cu
)
8226 gdb_assert (cu
->per_cu
->lang
== language_rust
);
8227 for (type
*type_
: cu
->rust_unions
)
8228 quirk_rust_enum (type_
, cu
->per_objfile
->objfile
);
8229 /* We don't need this any more. */
8230 cu
->rust_unions
.clear ();
8235 type_unit_group_unshareable
*
8236 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group
*tu_group
)
8238 auto iter
= this->m_type_units
.find (tu_group
);
8239 if (iter
!= this->m_type_units
.end ())
8240 return iter
->second
.get ();
8242 type_unit_group_unshareable_up
uniq (new type_unit_group_unshareable
);
8243 type_unit_group_unshareable
*result
= uniq
.get ();
8244 this->m_type_units
[tu_group
] = std::move (uniq
);
8249 dwarf2_per_objfile::get_type_for_signatured_type
8250 (signatured_type
*sig_type
) const
8252 auto iter
= this->m_type_map
.find (sig_type
);
8253 if (iter
== this->m_type_map
.end ())
8256 return iter
->second
;
8259 void dwarf2_per_objfile::set_type_for_signatured_type
8260 (signatured_type
*sig_type
, struct type
*type
)
8262 gdb_assert (this->m_type_map
.find (sig_type
) == this->m_type_map
.end ());
8264 this->m_type_map
[sig_type
] = type
;
8267 /* A helper function for computing the list of all symbol tables
8268 included by PER_CU. */
8271 recursively_compute_inclusions (std::vector
<compunit_symtab
*> *result
,
8272 htab_t all_children
, htab_t all_type_symtabs
,
8273 dwarf2_per_cu_data
*per_cu
,
8274 dwarf2_per_objfile
*per_objfile
,
8275 struct compunit_symtab
*immediate_parent
)
8277 void **slot
= htab_find_slot (all_children
, per_cu
, INSERT
);
8280 /* This inclusion and its children have been processed. */
8286 /* Only add a CU if it has a symbol table. */
8287 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8290 /* If this is a type unit only add its symbol table if we haven't
8291 seen it yet (type unit per_cu's can share symtabs). */
8292 if (per_cu
->is_debug_types
)
8294 slot
= htab_find_slot (all_type_symtabs
, cust
, INSERT
);
8298 result
->push_back (cust
);
8299 if (cust
->user
== NULL
)
8300 cust
->user
= immediate_parent
;
8305 result
->push_back (cust
);
8306 if (cust
->user
== NULL
)
8307 cust
->user
= immediate_parent
;
8311 if (!per_cu
->imported_symtabs_empty ())
8312 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8314 recursively_compute_inclusions (result
, all_children
,
8315 all_type_symtabs
, ptr
, per_objfile
,
8320 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8324 compute_compunit_symtab_includes (dwarf2_per_cu_data
*per_cu
,
8325 dwarf2_per_objfile
*per_objfile
)
8327 gdb_assert (! per_cu
->is_debug_types
);
8329 if (!per_cu
->imported_symtabs_empty ())
8332 std::vector
<compunit_symtab
*> result_symtabs
;
8333 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8335 /* If we don't have a symtab, we can just skip this case. */
8339 htab_up
all_children (htab_create_alloc (1, htab_hash_pointer
,
8341 NULL
, xcalloc
, xfree
));
8342 htab_up
all_type_symtabs (htab_create_alloc (1, htab_hash_pointer
,
8344 NULL
, xcalloc
, xfree
));
8346 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8348 recursively_compute_inclusions (&result_symtabs
, all_children
.get (),
8349 all_type_symtabs
.get (), ptr
,
8353 /* Now we have a transitive closure of all the included symtabs. */
8354 len
= result_symtabs
.size ();
8356 = XOBNEWVEC (&per_objfile
->objfile
->objfile_obstack
,
8357 struct compunit_symtab
*, len
+ 1);
8358 memcpy (cust
->includes
, result_symtabs
.data (),
8359 len
* sizeof (compunit_symtab
*));
8360 cust
->includes
[len
] = NULL
;
8364 /* Compute the 'includes' field for the symtabs of all the CUs we just
8368 process_cu_includes (dwarf2_per_objfile
*per_objfile
)
8370 for (dwarf2_per_cu_data
*iter
: per_objfile
->per_bfd
->just_read_cus
)
8372 if (! iter
->is_debug_types
)
8373 compute_compunit_symtab_includes (iter
, per_objfile
);
8376 per_objfile
->per_bfd
->just_read_cus
.clear ();
8379 /* Generate full symbol information for CU, whose DIEs have
8380 already been loaded into memory. */
8383 process_full_comp_unit (dwarf2_cu
*cu
, enum language pretend_language
)
8385 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8386 struct objfile
*objfile
= per_objfile
->objfile
;
8387 struct gdbarch
*gdbarch
= objfile
->arch ();
8388 CORE_ADDR lowpc
, highpc
;
8389 struct compunit_symtab
*cust
;
8391 struct block
*static_block
;
8394 baseaddr
= objfile
->text_section_offset ();
8396 /* Clear the list here in case something was left over. */
8397 cu
->method_list
.clear ();
8399 dwarf2_find_base_address (cu
->dies
, cu
);
8401 /* Before we start reading the top-level DIE, ensure it has a valid tag
8403 switch (cu
->dies
->tag
)
8405 case DW_TAG_compile_unit
:
8406 case DW_TAG_partial_unit
:
8407 case DW_TAG_type_unit
:
8410 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8411 dwarf_tag_name (cu
->dies
->tag
),
8412 sect_offset_str (cu
->per_cu
->sect_off
),
8413 objfile_name (per_objfile
->objfile
));
8416 /* Do line number decoding in read_file_scope () */
8417 process_die (cu
->dies
, cu
);
8419 /* For now fudge the Go package. */
8420 if (cu
->per_cu
->lang
== language_go
)
8421 fixup_go_packaging (cu
);
8423 /* Now that we have processed all the DIEs in the CU, all the types
8424 should be complete, and it should now be safe to compute all of the
8426 compute_delayed_physnames (cu
);
8428 if (cu
->per_cu
->lang
== language_rust
)
8429 rust_union_quirks (cu
);
8431 /* Some compilers don't define a DW_AT_high_pc attribute for the
8432 compilation unit. If the DW_AT_high_pc is missing, synthesize
8433 it, by scanning the DIE's below the compilation unit. */
8434 get_scope_pc_bounds (cu
->dies
, &lowpc
, &highpc
, cu
);
8436 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
8438 = cu
->get_builder ()->end_compunit_symtab_get_static_block (addr
, 0, 1);
8440 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8441 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8442 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8443 addrmap to help ensure it has an accurate map of pc values belonging to
8445 dwarf2_record_block_ranges (cu
->dies
, static_block
, baseaddr
, cu
);
8447 cust
= cu
->get_builder ()->end_compunit_symtab_from_static_block
8448 (static_block
, SECT_OFF_TEXT (objfile
), 0);
8452 int gcc_4_minor
= producer_is_gcc_ge_4 (cu
->producer
);
8454 /* Set symtab language to language from DW_AT_language. If the
8455 compilation is from a C file generated by language preprocessors, do
8456 not set the language if it was already deduced by start_subfile. */
8457 if (!(cu
->per_cu
->lang
== language_c
8458 && cust
->primary_filetab ()->language () != language_unknown
))
8459 cust
->primary_filetab ()->set_language (cu
->per_cu
->lang
);
8461 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8462 produce DW_AT_location with location lists but it can be possibly
8463 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8464 there were bugs in prologue debug info, fixed later in GCC-4.5
8465 by "unwind info for epilogues" patch (which is not directly related).
8467 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8468 needed, it would be wrong due to missing DW_AT_producer there.
8470 Still one can confuse GDB by using non-standard GCC compilation
8471 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8473 if (cu
->has_loclist
&& gcc_4_minor
>= 5)
8474 cust
->set_locations_valid (true);
8476 if (gcc_4_minor
>= 5)
8477 cust
->set_epilogue_unwind_valid (true);
8479 cust
->set_call_site_htab (cu
->call_site_htab
);
8482 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8484 /* Push it for inclusion processing later. */
8485 per_objfile
->per_bfd
->just_read_cus
.push_back (cu
->per_cu
);
8487 /* Not needed any more. */
8488 cu
->reset_builder ();
8491 /* Generate full symbol information for type unit CU, whose DIEs have
8492 already been loaded into memory. */
8495 process_full_type_unit (dwarf2_cu
*cu
,
8496 enum language pretend_language
)
8498 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8499 struct objfile
*objfile
= per_objfile
->objfile
;
8500 struct compunit_symtab
*cust
;
8501 struct signatured_type
*sig_type
;
8503 gdb_assert (cu
->per_cu
->is_debug_types
);
8504 sig_type
= (struct signatured_type
*) cu
->per_cu
;
8506 /* Clear the list here in case something was left over. */
8507 cu
->method_list
.clear ();
8509 /* The symbol tables are set up in read_type_unit_scope. */
8510 process_die (cu
->dies
, cu
);
8512 /* For now fudge the Go package. */
8513 if (cu
->per_cu
->lang
== language_go
)
8514 fixup_go_packaging (cu
);
8516 /* Now that we have processed all the DIEs in the CU, all the types
8517 should be complete, and it should now be safe to compute all of the
8519 compute_delayed_physnames (cu
);
8521 if (cu
->per_cu
->lang
== language_rust
)
8522 rust_union_quirks (cu
);
8524 /* TUs share symbol tables.
8525 If this is the first TU to use this symtab, complete the construction
8526 of it with end_expandable_symtab. Otherwise, complete the addition of
8527 this TU's symbols to the existing symtab. */
8528 type_unit_group_unshareable
*tug_unshare
=
8529 per_objfile
->get_type_unit_group_unshareable (sig_type
->type_unit_group
);
8530 if (tug_unshare
->compunit_symtab
== NULL
)
8532 buildsym_compunit
*builder
= cu
->get_builder ();
8533 cust
= builder
->end_expandable_symtab (0, SECT_OFF_TEXT (objfile
));
8534 tug_unshare
->compunit_symtab
= cust
;
8538 /* Set symtab language to language from DW_AT_language. If the
8539 compilation is from a C file generated by language preprocessors,
8540 do not set the language if it was already deduced by
8542 if (!(cu
->per_cu
->lang
== language_c
8543 && cust
->primary_filetab ()->language () != language_c
))
8544 cust
->primary_filetab ()->set_language (cu
->per_cu
->lang
);
8549 cu
->get_builder ()->augment_type_symtab ();
8550 cust
= tug_unshare
->compunit_symtab
;
8553 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8555 /* Not needed any more. */
8556 cu
->reset_builder ();
8559 /* Process an imported unit DIE. */
8562 process_imported_unit_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8564 struct attribute
*attr
;
8566 /* For now we don't handle imported units in type units. */
8567 if (cu
->per_cu
->is_debug_types
)
8569 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8570 " supported in type units [in module %s]"),
8571 objfile_name (cu
->per_objfile
->objfile
));
8574 attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
8577 sect_offset sect_off
= attr
->get_ref_die_offset ();
8578 bool is_dwz
= (attr
->form
== DW_FORM_GNU_ref_alt
|| cu
->per_cu
->is_dwz
);
8579 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8580 dwarf2_per_cu_data
*per_cu
8581 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
8582 per_objfile
->per_bfd
);
8584 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8585 into another compilation unit, at root level. Regard this as a hint,
8587 if (die
->parent
&& die
->parent
->parent
== NULL
8588 && per_cu
->unit_type
== DW_UT_compile
8589 && per_cu
->lang
== language_cplus
)
8592 /* If necessary, add it to the queue and load its DIEs. */
8593 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
8595 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
8596 false, cu
->per_cu
->lang
);
8598 cu
->per_cu
->imported_symtabs_push (per_cu
);
8602 /* RAII object that represents a process_die scope: i.e.,
8603 starts/finishes processing a DIE. */
8604 class process_die_scope
8607 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
8608 : m_die (die
), m_cu (cu
)
8610 /* We should only be processing DIEs not already in process. */
8611 gdb_assert (!m_die
->in_process
);
8612 m_die
->in_process
= true;
8615 ~process_die_scope ()
8617 m_die
->in_process
= false;
8619 /* If we're done processing the DIE for the CU that owns the line
8620 header, we don't need the line header anymore. */
8621 if (m_cu
->line_header_die_owner
== m_die
)
8623 delete m_cu
->line_header
;
8624 m_cu
->line_header
= NULL
;
8625 m_cu
->line_header_die_owner
= NULL
;
8634 /* Process a die and its children. */
8637 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8639 process_die_scope
scope (die
, cu
);
8643 case DW_TAG_padding
:
8645 case DW_TAG_compile_unit
:
8646 case DW_TAG_partial_unit
:
8647 read_file_scope (die
, cu
);
8649 case DW_TAG_type_unit
:
8650 read_type_unit_scope (die
, cu
);
8652 case DW_TAG_subprogram
:
8653 /* Nested subprograms in Fortran get a prefix. */
8654 if (cu
->per_cu
->lang
== language_fortran
8655 && die
->parent
!= NULL
8656 && die
->parent
->tag
== DW_TAG_subprogram
)
8657 cu
->processing_has_namespace_info
= true;
8659 case DW_TAG_inlined_subroutine
:
8660 read_func_scope (die
, cu
);
8662 case DW_TAG_lexical_block
:
8663 case DW_TAG_try_block
:
8664 case DW_TAG_catch_block
:
8665 read_lexical_block_scope (die
, cu
);
8667 case DW_TAG_call_site
:
8668 case DW_TAG_GNU_call_site
:
8669 read_call_site_scope (die
, cu
);
8671 case DW_TAG_class_type
:
8672 case DW_TAG_interface_type
:
8673 case DW_TAG_structure_type
:
8674 case DW_TAG_union_type
:
8675 case DW_TAG_namelist
:
8676 process_structure_scope (die
, cu
);
8678 case DW_TAG_enumeration_type
:
8679 process_enumeration_scope (die
, cu
);
8682 /* These dies have a type, but processing them does not create
8683 a symbol or recurse to process the children. Therefore we can
8684 read them on-demand through read_type_die. */
8685 case DW_TAG_subroutine_type
:
8686 case DW_TAG_set_type
:
8687 case DW_TAG_pointer_type
:
8688 case DW_TAG_ptr_to_member_type
:
8689 case DW_TAG_reference_type
:
8690 case DW_TAG_rvalue_reference_type
:
8691 case DW_TAG_string_type
:
8694 case DW_TAG_array_type
:
8695 /* We only need to handle this case for Ada -- in other
8696 languages, it's normal for the compiler to emit a typedef
8698 if (cu
->per_cu
->lang
!= language_ada
)
8701 case DW_TAG_base_type
:
8702 case DW_TAG_subrange_type
:
8703 case DW_TAG_generic_subrange
:
8704 case DW_TAG_typedef
:
8705 /* Add a typedef symbol for the type definition, if it has a
8707 new_symbol (die
, read_type_die (die
, cu
), cu
);
8709 case DW_TAG_common_block
:
8710 read_common_block (die
, cu
);
8712 case DW_TAG_common_inclusion
:
8714 case DW_TAG_namespace
:
8715 cu
->processing_has_namespace_info
= true;
8716 read_namespace (die
, cu
);
8719 cu
->processing_has_namespace_info
= true;
8720 read_module (die
, cu
);
8722 case DW_TAG_imported_declaration
:
8723 cu
->processing_has_namespace_info
= true;
8724 if (read_namespace_alias (die
, cu
))
8726 /* The declaration is not a global namespace alias. */
8728 case DW_TAG_imported_module
:
8729 cu
->processing_has_namespace_info
= true;
8730 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
8731 || cu
->per_cu
->lang
!= language_fortran
))
8732 complaint (_("Tag '%s' has unexpected children"),
8733 dwarf_tag_name (die
->tag
));
8734 read_import_statement (die
, cu
);
8737 case DW_TAG_imported_unit
:
8738 process_imported_unit_die (die
, cu
);
8741 case DW_TAG_variable
:
8742 read_variable (die
, cu
);
8746 new_symbol (die
, NULL
, cu
);
8751 /* DWARF name computation. */
8753 /* A helper function for dwarf2_compute_name which determines whether DIE
8754 needs to have the name of the scope prepended to the name listed in the
8758 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
8760 struct attribute
*attr
;
8764 case DW_TAG_namespace
:
8765 case DW_TAG_typedef
:
8766 case DW_TAG_class_type
:
8767 case DW_TAG_interface_type
:
8768 case DW_TAG_structure_type
:
8769 case DW_TAG_union_type
:
8770 case DW_TAG_enumeration_type
:
8771 case DW_TAG_enumerator
:
8772 case DW_TAG_subprogram
:
8773 case DW_TAG_inlined_subroutine
:
8775 case DW_TAG_imported_declaration
:
8778 case DW_TAG_variable
:
8779 case DW_TAG_constant
:
8780 /* We only need to prefix "globally" visible variables. These include
8781 any variable marked with DW_AT_external or any variable that
8782 lives in a namespace. [Variables in anonymous namespaces
8783 require prefixing, but they are not DW_AT_external.] */
8785 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
8787 struct dwarf2_cu
*spec_cu
= cu
;
8789 return die_needs_namespace (die_specification (die
, &spec_cu
),
8793 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
8794 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
8795 && die
->parent
->tag
!= DW_TAG_module
)
8797 /* A variable in a lexical block of some kind does not need a
8798 namespace, even though in C++ such variables may be external
8799 and have a mangled name. */
8800 if (die
->parent
->tag
== DW_TAG_lexical_block
8801 || die
->parent
->tag
== DW_TAG_try_block
8802 || die
->parent
->tag
== DW_TAG_catch_block
8803 || die
->parent
->tag
== DW_TAG_subprogram
)
8812 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8813 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8814 defined for the given DIE. */
8816 static struct attribute
*
8817 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
8819 struct attribute
*attr
;
8821 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
8823 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8828 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8829 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8830 defined for the given DIE. */
8833 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
8835 const char *linkage_name
;
8837 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
8838 if (linkage_name
== NULL
)
8839 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8841 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8842 See https://github.com/rust-lang/rust/issues/32925. */
8843 if (cu
->per_cu
->lang
== language_rust
&& linkage_name
!= NULL
8844 && strchr (linkage_name
, '{') != NULL
)
8845 linkage_name
= NULL
;
8847 return linkage_name
;
8850 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8851 compute the physname for the object, which include a method's:
8852 - formal parameters (C++),
8853 - receiver type (Go),
8855 The term "physname" is a bit confusing.
8856 For C++, for example, it is the demangled name.
8857 For Go, for example, it's the mangled name.
8859 For Ada, return the DIE's linkage name rather than the fully qualified
8860 name. PHYSNAME is ignored..
8862 The result is allocated on the objfile->per_bfd's obstack and
8866 dwarf2_compute_name (const char *name
,
8867 struct die_info
*die
, struct dwarf2_cu
*cu
,
8870 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
8873 name
= dwarf2_name (die
, cu
);
8875 enum language lang
= cu
->per_cu
->lang
;
8877 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8878 but otherwise compute it by typename_concat inside GDB.
8879 FIXME: Actually this is not really true, or at least not always true.
8880 It's all very confusing. compute_and_set_names doesn't try to demangle
8881 Fortran names because there is no mangling standard. So new_symbol
8882 will set the demangled name to the result of dwarf2_full_name, and it is
8883 the demangled name that GDB uses if it exists. */
8884 if (lang
== language_ada
8885 || (lang
== language_fortran
&& physname
))
8887 /* For Ada unit, we prefer the linkage name over the name, as
8888 the former contains the exported name, which the user expects
8889 to be able to reference. Ideally, we want the user to be able
8890 to reference this entity using either natural or linkage name,
8891 but we haven't started looking at this enhancement yet. */
8892 const char *linkage_name
= dw2_linkage_name (die
, cu
);
8894 if (linkage_name
!= NULL
)
8895 return linkage_name
;
8898 /* These are the only languages we know how to qualify names in. */
8900 && (lang
== language_cplus
8901 || lang
== language_fortran
|| lang
== language_d
8902 || lang
== language_rust
))
8904 if (die_needs_namespace (die
, cu
))
8907 const char *canonical_name
= NULL
;
8911 prefix
= determine_prefix (die
, cu
);
8912 if (*prefix
!= '\0')
8914 gdb::unique_xmalloc_ptr
<char> prefixed_name
8915 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
8917 buf
.puts (prefixed_name
.get ());
8922 /* Template parameters may be specified in the DIE's DW_AT_name, or
8923 as children with DW_TAG_template_type_param or
8924 DW_TAG_value_type_param. If the latter, add them to the name
8925 here. If the name already has template parameters, then
8926 skip this step; some versions of GCC emit both, and
8927 it is more efficient to use the pre-computed name.
8929 Something to keep in mind about this process: it is very
8930 unlikely, or in some cases downright impossible, to produce
8931 something that will match the mangled name of a function.
8932 If the definition of the function has the same debug info,
8933 we should be able to match up with it anyway. But fallbacks
8934 using the minimal symbol, for instance to find a method
8935 implemented in a stripped copy of libstdc++, will not work.
8936 If we do not have debug info for the definition, we will have to
8937 match them up some other way.
8939 When we do name matching there is a related problem with function
8940 templates; two instantiated function templates are allowed to
8941 differ only by their return types, which we do not add here. */
8943 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
8945 struct attribute
*attr
;
8946 struct die_info
*child
;
8949 die
->building_fullname
= 1;
8951 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
8955 const gdb_byte
*bytes
;
8956 struct dwarf2_locexpr_baton
*baton
;
8959 if (child
->tag
!= DW_TAG_template_type_param
8960 && child
->tag
!= DW_TAG_template_value_param
)
8971 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
8974 complaint (_("template parameter missing DW_AT_type"));
8975 buf
.puts ("UNKNOWN_TYPE");
8978 type
= die_type (child
, cu
);
8980 if (child
->tag
== DW_TAG_template_type_param
)
8982 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
8983 &type_print_raw_options
);
8987 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
8990 complaint (_("template parameter missing "
8991 "DW_AT_const_value"));
8992 buf
.puts ("UNKNOWN_VALUE");
8996 dwarf2_const_value_attr (attr
, type
, name
,
8997 &cu
->comp_unit_obstack
, cu
,
8998 &value
, &bytes
, &baton
);
9000 if (type
->has_no_signedness ())
9001 /* GDB prints characters as NUMBER 'CHAR'. If that's
9002 changed, this can use value_print instead. */
9003 cu
->language_defn
->printchar (value
, type
, &buf
);
9006 struct value_print_options opts
;
9009 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
9013 baton
->per_objfile
);
9014 else if (bytes
!= NULL
)
9016 v
= allocate_value (type
);
9017 memcpy (value_contents_writeable (v
).data (), bytes
,
9018 TYPE_LENGTH (type
));
9021 v
= value_from_longest (type
, value
);
9023 /* Specify decimal so that we do not depend on
9025 get_formatted_print_options (&opts
, 'd');
9027 value_print (v
, &buf
, &opts
);
9032 die
->building_fullname
= 0;
9036 /* Close the argument list, with a space if necessary
9037 (nested templates). */
9038 if (!buf
.empty () && buf
.string ().back () == '>')
9045 /* For C++ methods, append formal parameter type
9046 information, if PHYSNAME. */
9048 if (physname
&& die
->tag
== DW_TAG_subprogram
9049 && lang
== language_cplus
)
9051 struct type
*type
= read_type_die (die
, cu
);
9053 c_type_print_args (type
, &buf
, 1, lang
,
9054 &type_print_raw_options
);
9056 if (lang
== language_cplus
)
9058 /* Assume that an artificial first parameter is
9059 "this", but do not crash if it is not. RealView
9060 marks unnamed (and thus unused) parameters as
9061 artificial; there is no way to differentiate
9063 if (type
->num_fields () > 0
9064 && TYPE_FIELD_ARTIFICIAL (type
, 0)
9065 && type
->field (0).type ()->code () == TYPE_CODE_PTR
9066 && TYPE_CONST (TYPE_TARGET_TYPE (type
->field (0).type ())))
9067 buf
.puts (" const");
9071 const std::string
&intermediate_name
= buf
.string ();
9073 if (lang
== language_cplus
)
9075 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
9078 /* If we only computed INTERMEDIATE_NAME, or if
9079 INTERMEDIATE_NAME is already canonical, then we need to
9081 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
9082 name
= objfile
->intern (intermediate_name
);
9084 name
= canonical_name
;
9091 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9092 If scope qualifiers are appropriate they will be added. The result
9093 will be allocated on the storage_obstack, or NULL if the DIE does
9094 not have a name. NAME may either be from a previous call to
9095 dwarf2_name or NULL.
9097 The output string will be canonicalized (if C++). */
9100 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9102 return dwarf2_compute_name (name
, die
, cu
, 0);
9105 /* Construct a physname for the given DIE in CU. NAME may either be
9106 from a previous call to dwarf2_name or NULL. The result will be
9107 allocated on the objfile_objstack or NULL if the DIE does not have a
9110 The output string will be canonicalized (if C++). */
9113 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9115 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9116 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
9119 /* In this case dwarf2_compute_name is just a shortcut not building anything
9121 if (!die_needs_namespace (die
, cu
))
9122 return dwarf2_compute_name (name
, die
, cu
, 1);
9124 if (cu
->per_cu
->lang
!= language_rust
)
9125 mangled
= dw2_linkage_name (die
, cu
);
9127 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9129 gdb::unique_xmalloc_ptr
<char> demangled
;
9130 if (mangled
!= NULL
)
9132 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
9134 /* Do nothing (do not demangle the symbol name). */
9138 /* Use DMGL_RET_DROP for C++ template functions to suppress
9139 their return type. It is easier for GDB users to search
9140 for such functions as `name(params)' than `long name(params)'.
9141 In such case the minimal symbol names do not match the full
9142 symbol names but for template functions there is never a need
9143 to look up their definition from their declaration so
9144 the only disadvantage remains the minimal symbol variant
9145 `long name(params)' does not have the proper inferior type. */
9146 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
9150 canon
= demangled
.get ();
9158 if (canon
== NULL
|| check_physname
)
9160 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
9162 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
9164 /* It may not mean a bug in GDB. The compiler could also
9165 compute DW_AT_linkage_name incorrectly. But in such case
9166 GDB would need to be bug-to-bug compatible. */
9168 complaint (_("Computed physname <%s> does not match demangled <%s> "
9169 "(from linkage <%s>) - DIE at %s [in module %s]"),
9170 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
9171 objfile_name (objfile
));
9173 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9174 is available here - over computed PHYSNAME. It is safer
9175 against both buggy GDB and buggy compilers. */
9189 retval
= objfile
->intern (retval
);
9194 /* Inspect DIE in CU for a namespace alias. If one exists, record
9195 a new symbol for it.
9197 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9200 read_namespace_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
9202 struct attribute
*attr
;
9204 /* If the die does not have a name, this is not a namespace
9206 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
9210 struct die_info
*d
= die
;
9211 struct dwarf2_cu
*imported_cu
= cu
;
9213 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9214 keep inspecting DIEs until we hit the underlying import. */
9215 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9216 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
9218 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
9222 d
= follow_die_ref (d
, attr
, &imported_cu
);
9223 if (d
->tag
!= DW_TAG_imported_declaration
)
9227 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
9229 complaint (_("DIE at %s has too many recursively imported "
9230 "declarations"), sect_offset_str (d
->sect_off
));
9237 sect_offset sect_off
= attr
->get_ref_die_offset ();
9239 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, cu
->per_objfile
);
9240 if (type
!= NULL
&& type
->code () == TYPE_CODE_NAMESPACE
)
9242 /* This declaration is a global namespace alias. Add
9243 a symbol for it whose type is the aliased namespace. */
9244 new_symbol (die
, type
, cu
);
9253 /* Return the using directives repository (global or local?) to use in the
9254 current context for CU.
9256 For Ada, imported declarations can materialize renamings, which *may* be
9257 global. However it is impossible (for now?) in DWARF to distinguish
9258 "external" imported declarations and "static" ones. As all imported
9259 declarations seem to be static in all other languages, make them all CU-wide
9260 global only in Ada. */
9262 static struct using_direct
**
9263 using_directives (struct dwarf2_cu
*cu
)
9265 if (cu
->per_cu
->lang
== language_ada
9266 && cu
->get_builder ()->outermost_context_p ())
9267 return cu
->get_builder ()->get_global_using_directives ();
9269 return cu
->get_builder ()->get_local_using_directives ();
9272 /* Read the import statement specified by the given die and record it. */
9275 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
9277 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9278 struct attribute
*import_attr
;
9279 struct die_info
*imported_die
, *child_die
;
9280 struct dwarf2_cu
*imported_cu
;
9281 const char *imported_name
;
9282 const char *imported_name_prefix
;
9283 const char *canonical_name
;
9284 const char *import_alias
;
9285 const char *imported_declaration
= NULL
;
9286 const char *import_prefix
;
9287 std::vector
<const char *> excludes
;
9289 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
9290 if (import_attr
== NULL
)
9292 complaint (_("Tag '%s' has no DW_AT_import"),
9293 dwarf_tag_name (die
->tag
));
9298 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
9299 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9300 if (imported_name
== NULL
)
9302 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9304 The import in the following code:
9318 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9319 <52> DW_AT_decl_file : 1
9320 <53> DW_AT_decl_line : 6
9321 <54> DW_AT_import : <0x75>
9322 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9324 <5b> DW_AT_decl_file : 1
9325 <5c> DW_AT_decl_line : 2
9326 <5d> DW_AT_type : <0x6e>
9328 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9329 <76> DW_AT_byte_size : 4
9330 <77> DW_AT_encoding : 5 (signed)
9332 imports the wrong die ( 0x75 instead of 0x58 ).
9333 This case will be ignored until the gcc bug is fixed. */
9337 /* Figure out the local name after import. */
9338 import_alias
= dwarf2_name (die
, cu
);
9340 /* Figure out where the statement is being imported to. */
9341 import_prefix
= determine_prefix (die
, cu
);
9343 /* Figure out what the scope of the imported die is and prepend it
9344 to the name of the imported die. */
9345 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
9347 if (imported_die
->tag
!= DW_TAG_namespace
9348 && imported_die
->tag
!= DW_TAG_module
)
9350 imported_declaration
= imported_name
;
9351 canonical_name
= imported_name_prefix
;
9353 else if (strlen (imported_name_prefix
) > 0)
9354 canonical_name
= obconcat (&objfile
->objfile_obstack
,
9355 imported_name_prefix
,
9356 (cu
->per_cu
->lang
== language_d
9359 imported_name
, (char *) NULL
);
9361 canonical_name
= imported_name
;
9363 if (die
->tag
== DW_TAG_imported_module
9364 && cu
->per_cu
->lang
== language_fortran
)
9365 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
9366 child_die
= child_die
->sibling
)
9368 /* DWARF-4: A Fortran use statement with a “rename list” may be
9369 represented by an imported module entry with an import attribute
9370 referring to the module and owned entries corresponding to those
9371 entities that are renamed as part of being imported. */
9373 if (child_die
->tag
!= DW_TAG_imported_declaration
)
9375 complaint (_("child DW_TAG_imported_declaration expected "
9376 "- DIE at %s [in module %s]"),
9377 sect_offset_str (child_die
->sect_off
),
9378 objfile_name (objfile
));
9382 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
9383 if (import_attr
== NULL
)
9385 complaint (_("Tag '%s' has no DW_AT_import"),
9386 dwarf_tag_name (child_die
->tag
));
9391 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
9393 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9394 if (imported_name
== NULL
)
9396 complaint (_("child DW_TAG_imported_declaration has unknown "
9397 "imported name - DIE at %s [in module %s]"),
9398 sect_offset_str (child_die
->sect_off
),
9399 objfile_name (objfile
));
9403 excludes
.push_back (imported_name
);
9405 process_die (child_die
, cu
);
9408 add_using_directive (using_directives (cu
),
9412 imported_declaration
,
9415 &objfile
->objfile_obstack
);
9418 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9419 types, but gives them a size of zero. Starting with version 14,
9420 ICC is compatible with GCC. */
9423 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
9425 if (!cu
->checked_producer
)
9426 check_producer (cu
);
9428 return cu
->producer_is_icc_lt_14
;
9431 /* ICC generates a DW_AT_type for C void functions. This was observed on
9432 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9433 which says that void functions should not have a DW_AT_type. */
9436 producer_is_icc (struct dwarf2_cu
*cu
)
9438 if (!cu
->checked_producer
)
9439 check_producer (cu
);
9441 return cu
->producer_is_icc
;
9444 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9445 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9446 this, it was first present in GCC release 4.3.0. */
9449 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
9451 if (!cu
->checked_producer
)
9452 check_producer (cu
);
9454 return cu
->producer_is_gcc_lt_4_3
;
9457 static file_and_directory
&
9458 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
9460 if (cu
->per_cu
->fnd
!= nullptr)
9461 return *cu
->per_cu
->fnd
;
9463 /* Find the filename. Do not use dwarf2_name here, since the filename
9464 is not a source language identifier. */
9465 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
9466 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
9468 if (res
.get_comp_dir () == nullptr
9469 && producer_is_gcc_lt_4_3 (cu
)
9470 && res
.get_name () != nullptr
9471 && IS_ABSOLUTE_PATH (res
.get_name ()))
9472 res
.set_comp_dir (ldirname (res
.get_name ()));
9474 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
9475 return *cu
->per_cu
->fnd
;
9478 /* Handle DW_AT_stmt_list for a compilation unit.
9479 DIE is the DW_TAG_compile_unit die for CU.
9480 COMP_DIR is the compilation directory. LOWPC is passed to
9481 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9484 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
9485 CORE_ADDR lowpc
) /* ARI: editCase function */
9487 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9488 struct attribute
*attr
;
9489 struct line_header line_header_local
;
9490 hashval_t line_header_local_hash
;
9494 gdb_assert (! cu
->per_cu
->is_debug_types
);
9496 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
9497 if (attr
== NULL
|| !attr
->form_is_unsigned ())
9500 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9502 /* The line header hash table is only created if needed (it exists to
9503 prevent redundant reading of the line table for partial_units).
9504 If we're given a partial_unit, we'll need it. If we're given a
9505 compile_unit, then use the line header hash table if it's already
9506 created, but don't create one just yet. */
9508 if (per_objfile
->line_header_hash
== NULL
9509 && die
->tag
== DW_TAG_partial_unit
)
9511 per_objfile
->line_header_hash
9512 .reset (htab_create_alloc (127, line_header_hash_voidp
,
9513 line_header_eq_voidp
,
9514 htab_delete_entry
<line_header
>,
9518 line_header_local
.sect_off
= line_offset
;
9519 line_header_local
.offset_in_dwz
= cu
->per_cu
->is_dwz
;
9520 line_header_local_hash
= line_header_hash (&line_header_local
);
9521 if (per_objfile
->line_header_hash
!= NULL
)
9523 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9525 line_header_local_hash
, NO_INSERT
);
9527 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9528 is not present in *SLOT (since if there is something in *SLOT then
9529 it will be for a partial_unit). */
9530 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
9532 gdb_assert (*slot
!= NULL
);
9533 cu
->line_header
= (struct line_header
*) *slot
;
9538 /* dwarf_decode_line_header does not yet provide sufficient information.
9539 We always have to call also dwarf_decode_lines for it. */
9540 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
);
9544 cu
->line_header
= lh
.release ();
9545 cu
->line_header_die_owner
= die
;
9547 if (per_objfile
->line_header_hash
== NULL
)
9551 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9553 line_header_local_hash
, INSERT
);
9554 gdb_assert (slot
!= NULL
);
9556 if (slot
!= NULL
&& *slot
== NULL
)
9558 /* This newly decoded line number information unit will be owned
9559 by line_header_hash hash table. */
9560 *slot
= cu
->line_header
;
9561 cu
->line_header_die_owner
= NULL
;
9565 /* We cannot free any current entry in (*slot) as that struct line_header
9566 may be already used by multiple CUs. Create only temporary decoded
9567 line_header for this CU - it may happen at most once for each line
9568 number information unit. And if we're not using line_header_hash
9569 then this is what we want as well. */
9570 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
9572 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
9573 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
);
9576 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9579 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9581 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9582 struct objfile
*objfile
= per_objfile
->objfile
;
9583 struct gdbarch
*gdbarch
= objfile
->arch ();
9584 CORE_ADDR lowpc
= ((CORE_ADDR
) -1);
9585 CORE_ADDR highpc
= ((CORE_ADDR
) 0);
9586 struct attribute
*attr
;
9587 struct die_info
*child_die
;
9590 prepare_one_comp_unit (cu
, die
, cu
->per_cu
->lang
);
9591 baseaddr
= objfile
->text_section_offset ();
9593 get_scope_pc_bounds (die
, &lowpc
, &highpc
, cu
);
9595 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9596 from finish_block. */
9597 if (lowpc
== ((CORE_ADDR
) -1))
9599 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
9601 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
9603 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
9606 gdb_assert (per_objfile
->sym_cu
== nullptr);
9607 scoped_restore restore_sym_cu
9608 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
9610 /* Decode line number information if present. We do this before
9611 processing child DIEs, so that the line header table is available
9612 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9613 HIGHPC are both 0x0, then there won't be any interesting code in
9614 the CU, but a check later on (in
9615 lnp_state_machine::check_line_address) will fail to properly
9616 exclude an entry that was removed via --gc-sections. */
9617 if (lowpc
!= highpc
)
9618 handle_DW_AT_stmt_list (die
, cu
, lowpc
);
9620 /* Process all dies in compilation unit. */
9621 if (die
->child
!= NULL
)
9623 child_die
= die
->child
;
9624 while (child_die
&& child_die
->tag
)
9626 process_die (child_die
, cu
);
9627 child_die
= child_die
->sibling
;
9630 per_objfile
->sym_cu
= nullptr;
9632 /* Decode macro information, if present. Dwarf 2 macro information
9633 refers to information in the line number info statement program
9634 header, so we can only read it if we've read the header
9636 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
9638 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
9639 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9641 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
9642 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9644 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
9648 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
9649 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9651 unsigned int macro_offset
= attr
->as_unsigned ();
9653 dwarf_decode_macros (cu
, macro_offset
, 0);
9659 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
9661 struct type_unit_group
*tu_group
;
9663 struct attribute
*attr
;
9665 struct signatured_type
*sig_type
;
9667 gdb_assert (per_cu
->is_debug_types
);
9668 sig_type
= (struct signatured_type
*) per_cu
;
9670 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
9672 /* If we're using .gdb_index (includes -readnow) then
9673 per_cu->type_unit_group may not have been set up yet. */
9674 if (sig_type
->type_unit_group
== NULL
)
9675 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
9676 tu_group
= sig_type
->type_unit_group
;
9678 /* If we've already processed this stmt_list there's no real need to
9679 do it again, we could fake it and just recreate the part we need
9680 (file name,index -> symtab mapping). If data shows this optimization
9681 is useful we can do it then. */
9682 type_unit_group_unshareable
*tug_unshare
9683 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
9684 first_time
= tug_unshare
->compunit_symtab
== NULL
;
9686 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9689 if (attr
!= NULL
&& attr
->form_is_unsigned ())
9691 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9692 lh
= dwarf_decode_line_header (line_offset
, this);
9697 start_compunit_symtab ("", NULL
, 0);
9700 gdb_assert (tug_unshare
->symtabs
== NULL
);
9701 gdb_assert (m_builder
== nullptr);
9702 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9703 m_builder
.reset (new struct buildsym_compunit
9704 (cust
->objfile (), "",
9706 compunit_language (cust
),
9708 list_in_scope
= get_builder ()->get_file_symbols ();
9713 line_header
= lh
.release ();
9714 line_header_die_owner
= die
;
9718 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
9720 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9721 still initializing it, and our caller (a few levels up)
9722 process_full_type_unit still needs to know if this is the first
9725 tug_unshare
->symtabs
9726 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
9727 struct symtab
*, line_header
->file_names_size ());
9729 auto &file_names
= line_header
->file_names ();
9730 for (i
= 0; i
< file_names
.size (); ++i
)
9732 file_entry
&fe
= file_names
[i
];
9733 dwarf2_start_subfile (this, fe
.name
,
9734 fe
.include_dir (line_header
));
9735 buildsym_compunit
*b
= get_builder ();
9736 if (b
->get_current_subfile ()->symtab
== NULL
)
9738 /* NOTE: start_subfile will recognize when it's been
9739 passed a file it has already seen. So we can't
9740 assume there's a simple mapping from
9741 cu->line_header->file_names to subfiles, plus
9742 cu->line_header->file_names may contain dups. */
9743 b
->get_current_subfile ()->symtab
9744 = allocate_symtab (cust
, b
->get_current_subfile ()->name
);
9747 fe
.symtab
= b
->get_current_subfile ()->symtab
;
9748 tug_unshare
->symtabs
[i
] = fe
.symtab
;
9753 gdb_assert (m_builder
== nullptr);
9754 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9755 m_builder
.reset (new struct buildsym_compunit
9756 (cust
->objfile (), "",
9758 compunit_language (cust
),
9760 list_in_scope
= get_builder ()->get_file_symbols ();
9762 auto &file_names
= line_header
->file_names ();
9763 for (i
= 0; i
< file_names
.size (); ++i
)
9765 file_entry
&fe
= file_names
[i
];
9766 fe
.symtab
= tug_unshare
->symtabs
[i
];
9770 /* The main symtab is allocated last. Type units don't have DW_AT_name
9771 so they don't have a "real" (so to speak) symtab anyway.
9772 There is later code that will assign the main symtab to all symbols
9773 that don't have one. We need to handle the case of a symbol with a
9774 missing symtab (DW_AT_decl_file) anyway. */
9777 /* Process DW_TAG_type_unit.
9778 For TUs we want to skip the first top level sibling if it's not the
9779 actual type being defined by this TU. In this case the first top
9780 level sibling is there to provide context only. */
9783 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9785 struct die_info
*child_die
;
9787 prepare_one_comp_unit (cu
, die
, language_minimal
);
9789 /* Initialize (or reinitialize) the machinery for building symtabs.
9790 We do this before processing child DIEs, so that the line header table
9791 is available for DW_AT_decl_file. */
9792 cu
->setup_type_unit_groups (die
);
9794 if (die
->child
!= NULL
)
9796 child_die
= die
->child
;
9797 while (child_die
&& child_die
->tag
)
9799 process_die (child_die
, cu
);
9800 child_die
= child_die
->sibling
;
9807 http://gcc.gnu.org/wiki/DebugFission
9808 http://gcc.gnu.org/wiki/DebugFissionDWP
9810 To simplify handling of both DWO files ("object" files with the DWARF info)
9811 and DWP files (a file with the DWOs packaged up into one file), we treat
9812 DWP files as having a collection of virtual DWO files. */
9815 hash_dwo_file (const void *item
)
9817 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
9820 hash
= htab_hash_string (dwo_file
->dwo_name
);
9821 if (dwo_file
->comp_dir
!= NULL
)
9822 hash
+= htab_hash_string (dwo_file
->comp_dir
);
9827 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
9829 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
9830 const struct dwo_file
*rhs
= (const struct dwo_file
*) item_rhs
;
9832 if (strcmp (lhs
->dwo_name
, rhs
->dwo_name
) != 0)
9834 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
9835 return lhs
->comp_dir
== rhs
->comp_dir
;
9836 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
9839 /* Allocate a hash table for DWO files. */
9842 allocate_dwo_file_hash_table ()
9844 return htab_up (htab_create_alloc (41,
9847 htab_delete_entry
<dwo_file
>,
9851 /* Lookup DWO file DWO_NAME. */
9854 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
9855 const char *dwo_name
,
9856 const char *comp_dir
)
9858 struct dwo_file find_entry
;
9861 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
9862 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
9864 find_entry
.dwo_name
= dwo_name
;
9865 find_entry
.comp_dir
= comp_dir
;
9866 slot
= htab_find_slot (per_objfile
->per_bfd
->dwo_files
.get (), &find_entry
,
9873 hash_dwo_unit (const void *item
)
9875 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9877 /* This drops the top 32 bits of the id, but is ok for a hash. */
9878 return dwo_unit
->signature
;
9882 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
9884 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
9885 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
9887 /* The signature is assumed to be unique within the DWO file.
9888 So while object file CU dwo_id's always have the value zero,
9889 that's OK, assuming each object file DWO file has only one CU,
9890 and that's the rule for now. */
9891 return lhs
->signature
== rhs
->signature
;
9894 /* Allocate a hash table for DWO CUs,TUs.
9895 There is one of these tables for each of CUs,TUs for each DWO file. */
9898 allocate_dwo_unit_table ()
9900 /* Start out with a pretty small number.
9901 Generally DWO files contain only one CU and maybe some TUs. */
9902 return htab_up (htab_create_alloc (3,
9905 NULL
, xcalloc
, xfree
));
9908 /* die_reader_func for create_dwo_cu. */
9911 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
9912 const gdb_byte
*info_ptr
,
9913 struct die_info
*comp_unit_die
,
9914 struct dwo_file
*dwo_file
,
9915 struct dwo_unit
*dwo_unit
)
9917 struct dwarf2_cu
*cu
= reader
->cu
;
9918 sect_offset sect_off
= cu
->per_cu
->sect_off
;
9919 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
9921 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
9922 if (!signature
.has_value ())
9924 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9925 " its dwo_id [in module %s]"),
9926 sect_offset_str (sect_off
), dwo_file
->dwo_name
);
9930 dwo_unit
->dwo_file
= dwo_file
;
9931 dwo_unit
->signature
= *signature
;
9932 dwo_unit
->section
= section
;
9933 dwo_unit
->sect_off
= sect_off
;
9934 dwo_unit
->length
= cu
->per_cu
->length
;
9936 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9937 sect_offset_str (sect_off
),
9938 hex_string (dwo_unit
->signature
));
9941 /* Create the dwo_units for the CUs in a DWO_FILE.
9942 Note: This function processes DWO files only, not DWP files. */
9945 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
9946 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
9947 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
9949 struct objfile
*objfile
= per_objfile
->objfile
;
9950 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
9951 const gdb_byte
*info_ptr
, *end_ptr
;
9953 section
.read (objfile
);
9954 info_ptr
= section
.buffer
;
9956 if (info_ptr
== NULL
)
9959 dwarf_read_debug_printf ("Reading %s for %s:",
9960 section
.get_name (),
9961 section
.get_file_name ());
9963 end_ptr
= info_ptr
+ section
.size
;
9964 while (info_ptr
< end_ptr
)
9966 struct dwarf2_per_cu_data per_cu
;
9967 struct dwo_unit read_unit
{};
9968 struct dwo_unit
*dwo_unit
;
9970 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
9972 per_cu
.per_bfd
= per_bfd
;
9973 per_cu
.is_debug_types
= 0;
9974 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
9975 per_cu
.section
= §ion
;
9977 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
9978 if (!reader
.dummy_p
)
9979 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
9980 &dwo_file
, &read_unit
);
9981 info_ptr
+= per_cu
.length
;
9983 // If the unit could not be parsed, skip it.
9984 if (read_unit
.dwo_file
== NULL
)
9987 if (cus_htab
== NULL
)
9988 cus_htab
= allocate_dwo_unit_table ();
9990 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
9992 *dwo_unit
= read_unit
;
9993 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
9994 gdb_assert (slot
!= NULL
);
9997 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
9998 sect_offset dup_sect_off
= dup_cu
->sect_off
;
10000 complaint (_("debug cu entry at offset %s is duplicate to"
10001 " the entry at offset %s, signature %s"),
10002 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
10003 hex_string (dwo_unit
->signature
));
10005 *slot
= (void *)dwo_unit
;
10009 /* DWP file .debug_{cu,tu}_index section format:
10010 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10011 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10013 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10014 officially standard DWP format was published with DWARF v5 and is called
10015 Version 5. There are no versions 3 or 4.
10019 Both index sections have the same format, and serve to map a 64-bit
10020 signature to a set of section numbers. Each section begins with a header,
10021 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10022 indexes, and a pool of 32-bit section numbers. The index sections will be
10023 aligned at 8-byte boundaries in the file.
10025 The index section header consists of:
10027 V, 32 bit version number
10029 N, 32 bit number of compilation units or type units in the index
10030 M, 32 bit number of slots in the hash table
10032 Numbers are recorded using the byte order of the application binary.
10034 The hash table begins at offset 16 in the section, and consists of an array
10035 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10036 order of the application binary). Unused slots in the hash table are 0.
10037 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10039 The parallel table begins immediately after the hash table
10040 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10041 array of 32-bit indexes (using the byte order of the application binary),
10042 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10043 table contains a 32-bit index into the pool of section numbers. For unused
10044 hash table slots, the corresponding entry in the parallel table will be 0.
10046 The pool of section numbers begins immediately following the hash table
10047 (at offset 16 + 12 * M from the beginning of the section). The pool of
10048 section numbers consists of an array of 32-bit words (using the byte order
10049 of the application binary). Each item in the array is indexed starting
10050 from 0. The hash table entry provides the index of the first section
10051 number in the set. Additional section numbers in the set follow, and the
10052 set is terminated by a 0 entry (section number 0 is not used in ELF).
10054 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10055 section must be the first entry in the set, and the .debug_abbrev.dwo must
10056 be the second entry. Other members of the set may follow in any order.
10060 DWP Versions 2 and 5:
10062 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10063 and the entries in the index tables are now offsets into these sections.
10064 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10067 Index Section Contents:
10069 Hash Table of Signatures dwp_hash_table.hash_table
10070 Parallel Table of Indices dwp_hash_table.unit_table
10071 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10072 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10074 The index section header consists of:
10076 V, 32 bit version number
10077 L, 32 bit number of columns in the table of section offsets
10078 N, 32 bit number of compilation units or type units in the index
10079 M, 32 bit number of slots in the hash table
10081 Numbers are recorded using the byte order of the application binary.
10083 The hash table has the same format as version 1.
10084 The parallel table of indices has the same format as version 1,
10085 except that the entries are origin-1 indices into the table of sections
10086 offsets and the table of section sizes.
10088 The table of offsets begins immediately following the parallel table
10089 (at offset 16 + 12 * M from the beginning of the section). The table is
10090 a two-dimensional array of 32-bit words (using the byte order of the
10091 application binary), with L columns and N+1 rows, in row-major order.
10092 Each row in the array is indexed starting from 0. The first row provides
10093 a key to the remaining rows: each column in this row provides an identifier
10094 for a debug section, and the offsets in the same column of subsequent rows
10095 refer to that section. The section identifiers for Version 2 are:
10097 DW_SECT_INFO 1 .debug_info.dwo
10098 DW_SECT_TYPES 2 .debug_types.dwo
10099 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10100 DW_SECT_LINE 4 .debug_line.dwo
10101 DW_SECT_LOC 5 .debug_loc.dwo
10102 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10103 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10104 DW_SECT_MACRO 8 .debug_macro.dwo
10106 The section identifiers for Version 5 are:
10108 DW_SECT_INFO_V5 1 .debug_info.dwo
10109 DW_SECT_RESERVED_V5 2 --
10110 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10111 DW_SECT_LINE_V5 4 .debug_line.dwo
10112 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10113 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10114 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10115 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10117 The offsets provided by the CU and TU index sections are the base offsets
10118 for the contributions made by each CU or TU to the corresponding section
10119 in the package file. Each CU and TU header contains an abbrev_offset
10120 field, used to find the abbreviations table for that CU or TU within the
10121 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10122 be interpreted as relative to the base offset given in the index section.
10123 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10124 should be interpreted as relative to the base offset for .debug_line.dwo,
10125 and offsets into other debug sections obtained from DWARF attributes should
10126 also be interpreted as relative to the corresponding base offset.
10128 The table of sizes begins immediately following the table of offsets.
10129 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10130 with L columns and N rows, in row-major order. Each row in the array is
10131 indexed starting from 1 (row 0 is shared by the two tables).
10135 Hash table lookup is handled the same in version 1 and 2:
10137 We assume that N and M will not exceed 2^32 - 1.
10138 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10140 Given a 64-bit compilation unit signature or a type signature S, an entry
10141 in the hash table is located as follows:
10143 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10144 the low-order k bits all set to 1.
10146 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10148 3) If the hash table entry at index H matches the signature, use that
10149 entry. If the hash table entry at index H is unused (all zeroes),
10150 terminate the search: the signature is not present in the table.
10152 4) Let H = (H + H') modulo M. Repeat at Step 3.
10154 Because M > N and H' and M are relatively prime, the search is guaranteed
10155 to stop at an unused slot or find the match. */
10157 /* Create a hash table to map DWO IDs to their CU/TU entry in
10158 .debug_{info,types}.dwo in DWP_FILE.
10159 Returns NULL if there isn't one.
10160 Note: This function processes DWP files only, not DWO files. */
10162 static struct dwp_hash_table
*
10163 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
10164 struct dwp_file
*dwp_file
, int is_debug_types
)
10166 struct objfile
*objfile
= per_objfile
->objfile
;
10167 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10168 const gdb_byte
*index_ptr
, *index_end
;
10169 struct dwarf2_section_info
*index
;
10170 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
10171 struct dwp_hash_table
*htab
;
10173 if (is_debug_types
)
10174 index
= &dwp_file
->sections
.tu_index
;
10176 index
= &dwp_file
->sections
.cu_index
;
10178 if (index
->empty ())
10180 index
->read (objfile
);
10182 index_ptr
= index
->buffer
;
10183 index_end
= index_ptr
+ index
->size
;
10185 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10186 For now it's safe to just read 4 bytes (particularly as it's difficult to
10187 tell if you're dealing with Version 5 before you've read the version). */
10188 version
= read_4_bytes (dbfd
, index_ptr
);
10190 if (version
== 2 || version
== 5)
10191 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
10195 nr_units
= read_4_bytes (dbfd
, index_ptr
);
10197 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
10200 if (version
!= 1 && version
!= 2 && version
!= 5)
10202 error (_("Dwarf Error: unsupported DWP file version (%s)"
10203 " [in module %s]"),
10204 pulongest (version
), dwp_file
->name
);
10206 if (nr_slots
!= (nr_slots
& -nr_slots
))
10208 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10209 " is not power of 2 [in module %s]"),
10210 pulongest (nr_slots
), dwp_file
->name
);
10213 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
10214 htab
->version
= version
;
10215 htab
->nr_columns
= nr_columns
;
10216 htab
->nr_units
= nr_units
;
10217 htab
->nr_slots
= nr_slots
;
10218 htab
->hash_table
= index_ptr
;
10219 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
10221 /* Exit early if the table is empty. */
10222 if (nr_slots
== 0 || nr_units
== 0
10223 || (version
== 2 && nr_columns
== 0)
10224 || (version
== 5 && nr_columns
== 0))
10226 /* All must be zero. */
10227 if (nr_slots
!= 0 || nr_units
!= 0
10228 || (version
== 2 && nr_columns
!= 0)
10229 || (version
== 5 && nr_columns
!= 0))
10231 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10232 " all zero [in modules %s]"),
10240 htab
->section_pool
.v1
.indices
=
10241 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10242 /* It's harder to decide whether the section is too small in v1.
10243 V1 is deprecated anyway so we punt. */
10245 else if (version
== 2)
10247 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10248 int *ids
= htab
->section_pool
.v2
.section_ids
;
10249 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
10250 /* Reverse map for error checking. */
10251 int ids_seen
[DW_SECT_MAX
+ 1];
10254 if (nr_columns
< 2)
10256 error (_("Dwarf Error: bad DWP hash table, too few columns"
10257 " in section table [in module %s]"),
10260 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
10262 error (_("Dwarf Error: bad DWP hash table, too many columns"
10263 " in section table [in module %s]"),
10266 memset (ids
, 255, sizeof_ids
);
10267 memset (ids_seen
, 255, sizeof (ids_seen
));
10268 for (i
= 0; i
< nr_columns
; ++i
)
10270 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10272 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
10274 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10275 " in section table [in module %s]"),
10276 id
, dwp_file
->name
);
10278 if (ids_seen
[id
] != -1)
10280 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10281 " id %d in section table [in module %s]"),
10282 id
, dwp_file
->name
);
10287 /* Must have exactly one info or types section. */
10288 if (((ids_seen
[DW_SECT_INFO
] != -1)
10289 + (ids_seen
[DW_SECT_TYPES
] != -1))
10292 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10293 " DWO info/types section [in module %s]"),
10296 /* Must have an abbrev section. */
10297 if (ids_seen
[DW_SECT_ABBREV
] == -1)
10299 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10300 " section [in module %s]"),
10303 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10304 htab
->section_pool
.v2
.sizes
=
10305 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
10306 * nr_units
* nr_columns
);
10307 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
10308 * nr_units
* nr_columns
))
10311 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10312 " [in module %s]"),
10316 else /* version == 5 */
10318 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10319 int *ids
= htab
->section_pool
.v5
.section_ids
;
10320 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
10321 /* Reverse map for error checking. */
10322 int ids_seen
[DW_SECT_MAX_V5
+ 1];
10324 if (nr_columns
< 2)
10326 error (_("Dwarf Error: bad DWP hash table, too few columns"
10327 " in section table [in module %s]"),
10330 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
10332 error (_("Dwarf Error: bad DWP hash table, too many columns"
10333 " in section table [in module %s]"),
10336 memset (ids
, 255, sizeof_ids
);
10337 memset (ids_seen
, 255, sizeof (ids_seen
));
10338 for (int i
= 0; i
< nr_columns
; ++i
)
10340 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10342 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
10344 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10345 " in section table [in module %s]"),
10346 id
, dwp_file
->name
);
10348 if (ids_seen
[id
] != -1)
10350 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10351 " id %d in section table [in module %s]"),
10352 id
, dwp_file
->name
);
10357 /* Must have seen an info section. */
10358 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
10360 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10361 " DWO info/types section [in module %s]"),
10364 /* Must have an abbrev section. */
10365 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
10367 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10368 " section [in module %s]"),
10371 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10372 htab
->section_pool
.v5
.sizes
10373 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
10374 * nr_units
* nr_columns
);
10375 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
10376 * nr_units
* nr_columns
))
10379 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10380 " [in module %s]"),
10388 /* Update SECTIONS with the data from SECTP.
10390 This function is like the other "locate" section routines, but in
10391 this context the sections to read comes from the DWP V1 hash table,
10392 not the full ELF section table.
10394 The result is non-zero for success, or zero if an error was found. */
10397 locate_v1_virtual_dwo_sections (asection
*sectp
,
10398 struct virtual_v1_dwo_sections
*sections
)
10400 const struct dwop_section_names
*names
= &dwop_section_names
;
10402 if (names
->abbrev_dwo
.matches (sectp
->name
))
10404 /* There can be only one. */
10405 if (sections
->abbrev
.s
.section
!= NULL
)
10407 sections
->abbrev
.s
.section
= sectp
;
10408 sections
->abbrev
.size
= bfd_section_size (sectp
);
10410 else if (names
->info_dwo
.matches (sectp
->name
)
10411 || names
->types_dwo
.matches (sectp
->name
))
10413 /* There can be only one. */
10414 if (sections
->info_or_types
.s
.section
!= NULL
)
10416 sections
->info_or_types
.s
.section
= sectp
;
10417 sections
->info_or_types
.size
= bfd_section_size (sectp
);
10419 else if (names
->line_dwo
.matches (sectp
->name
))
10421 /* There can be only one. */
10422 if (sections
->line
.s
.section
!= NULL
)
10424 sections
->line
.s
.section
= sectp
;
10425 sections
->line
.size
= bfd_section_size (sectp
);
10427 else if (names
->loc_dwo
.matches (sectp
->name
))
10429 /* There can be only one. */
10430 if (sections
->loc
.s
.section
!= NULL
)
10432 sections
->loc
.s
.section
= sectp
;
10433 sections
->loc
.size
= bfd_section_size (sectp
);
10435 else if (names
->macinfo_dwo
.matches (sectp
->name
))
10437 /* There can be only one. */
10438 if (sections
->macinfo
.s
.section
!= NULL
)
10440 sections
->macinfo
.s
.section
= sectp
;
10441 sections
->macinfo
.size
= bfd_section_size (sectp
);
10443 else if (names
->macro_dwo
.matches (sectp
->name
))
10445 /* There can be only one. */
10446 if (sections
->macro
.s
.section
!= NULL
)
10448 sections
->macro
.s
.section
= sectp
;
10449 sections
->macro
.size
= bfd_section_size (sectp
);
10451 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
10453 /* There can be only one. */
10454 if (sections
->str_offsets
.s
.section
!= NULL
)
10456 sections
->str_offsets
.s
.section
= sectp
;
10457 sections
->str_offsets
.size
= bfd_section_size (sectp
);
10461 /* No other kind of section is valid. */
10468 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10469 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10470 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10471 This is for DWP version 1 files. */
10473 static struct dwo_unit
*
10474 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
10475 struct dwp_file
*dwp_file
,
10476 uint32_t unit_index
,
10477 const char *comp_dir
,
10478 ULONGEST signature
, int is_debug_types
)
10480 const struct dwp_hash_table
*dwp_htab
=
10481 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10482 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10483 const char *kind
= is_debug_types
? "TU" : "CU";
10484 struct dwo_file
*dwo_file
;
10485 struct dwo_unit
*dwo_unit
;
10486 struct virtual_v1_dwo_sections sections
;
10487 void **dwo_file_slot
;
10490 gdb_assert (dwp_file
->version
== 1);
10492 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10493 kind
, pulongest (unit_index
), hex_string (signature
),
10496 /* Fetch the sections of this DWO unit.
10497 Put a limit on the number of sections we look for so that bad data
10498 doesn't cause us to loop forever. */
10500 #define MAX_NR_V1_DWO_SECTIONS \
10501 (1 /* .debug_info or .debug_types */ \
10502 + 1 /* .debug_abbrev */ \
10503 + 1 /* .debug_line */ \
10504 + 1 /* .debug_loc */ \
10505 + 1 /* .debug_str_offsets */ \
10506 + 1 /* .debug_macro or .debug_macinfo */ \
10507 + 1 /* trailing zero */)
10509 memset (§ions
, 0, sizeof (sections
));
10511 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
10514 uint32_t section_nr
=
10515 read_4_bytes (dbfd
,
10516 dwp_htab
->section_pool
.v1
.indices
10517 + (unit_index
+ i
) * sizeof (uint32_t));
10519 if (section_nr
== 0)
10521 if (section_nr
>= dwp_file
->num_sections
)
10523 error (_("Dwarf Error: bad DWP hash table, section number too large"
10524 " [in module %s]"),
10528 sectp
= dwp_file
->elf_sections
[section_nr
];
10529 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
10531 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10532 " [in module %s]"),
10538 || sections
.info_or_types
.empty ()
10539 || sections
.abbrev
.empty ())
10541 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10542 " [in module %s]"),
10545 if (i
== MAX_NR_V1_DWO_SECTIONS
)
10547 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10548 " [in module %s]"),
10552 /* It's easier for the rest of the code if we fake a struct dwo_file and
10553 have dwo_unit "live" in that. At least for now.
10555 The DWP file can be made up of a random collection of CUs and TUs.
10556 However, for each CU + set of TUs that came from the same original DWO
10557 file, we can combine them back into a virtual DWO file to save space
10558 (fewer struct dwo_file objects to allocate). Remember that for really
10559 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10561 std::string virtual_dwo_name
=
10562 string_printf ("virtual-dwo/%d-%d-%d-%d",
10563 sections
.abbrev
.get_id (),
10564 sections
.line
.get_id (),
10565 sections
.loc
.get_id (),
10566 sections
.str_offsets
.get_id ());
10567 /* Can we use an existing virtual DWO file? */
10568 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10570 /* Create one if necessary. */
10571 if (*dwo_file_slot
== NULL
)
10573 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10574 virtual_dwo_name
.c_str ());
10576 dwo_file
= new struct dwo_file
;
10577 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10578 dwo_file
->comp_dir
= comp_dir
;
10579 dwo_file
->sections
.abbrev
= sections
.abbrev
;
10580 dwo_file
->sections
.line
= sections
.line
;
10581 dwo_file
->sections
.loc
= sections
.loc
;
10582 dwo_file
->sections
.macinfo
= sections
.macinfo
;
10583 dwo_file
->sections
.macro
= sections
.macro
;
10584 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
10585 /* The "str" section is global to the entire DWP file. */
10586 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10587 /* The info or types section is assigned below to dwo_unit,
10588 there's no need to record it in dwo_file.
10589 Also, we can't simply record type sections in dwo_file because
10590 we record a pointer into the vector in dwo_unit. As we collect more
10591 types we'll grow the vector and eventually have to reallocate space
10592 for it, invalidating all copies of pointers into the previous
10594 *dwo_file_slot
= dwo_file
;
10598 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10599 virtual_dwo_name
.c_str ());
10601 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10604 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10605 dwo_unit
->dwo_file
= dwo_file
;
10606 dwo_unit
->signature
= signature
;
10607 dwo_unit
->section
=
10608 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10609 *dwo_unit
->section
= sections
.info_or_types
;
10610 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10615 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10616 simplify them. Given a pointer to the containing section SECTION, and
10617 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10618 virtual section of just that piece. */
10620 static struct dwarf2_section_info
10621 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
10622 struct dwarf2_section_info
*section
,
10623 bfd_size_type offset
, bfd_size_type size
)
10625 struct dwarf2_section_info result
;
10628 gdb_assert (section
!= NULL
);
10629 gdb_assert (!section
->is_virtual
);
10631 memset (&result
, 0, sizeof (result
));
10632 result
.s
.containing_section
= section
;
10633 result
.is_virtual
= true;
10638 sectp
= section
->get_bfd_section ();
10640 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10641 bounds of the real section. This is a pretty-rare event, so just
10642 flag an error (easier) instead of a warning and trying to cope. */
10644 || offset
+ size
> bfd_section_size (sectp
))
10646 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10647 " in section %s [in module %s]"),
10648 sectp
? bfd_section_name (sectp
) : "<unknown>",
10649 objfile_name (per_objfile
->objfile
));
10652 result
.virtual_offset
= offset
;
10653 result
.size
= size
;
10657 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10658 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10659 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10660 This is for DWP version 2 files. */
10662 static struct dwo_unit
*
10663 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
10664 struct dwp_file
*dwp_file
,
10665 uint32_t unit_index
,
10666 const char *comp_dir
,
10667 ULONGEST signature
, int is_debug_types
)
10669 const struct dwp_hash_table
*dwp_htab
=
10670 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10671 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10672 const char *kind
= is_debug_types
? "TU" : "CU";
10673 struct dwo_file
*dwo_file
;
10674 struct dwo_unit
*dwo_unit
;
10675 struct virtual_v2_or_v5_dwo_sections sections
;
10676 void **dwo_file_slot
;
10679 gdb_assert (dwp_file
->version
== 2);
10681 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10682 kind
, pulongest (unit_index
), hex_string (signature
),
10685 /* Fetch the section offsets of this DWO unit. */
10687 memset (§ions
, 0, sizeof (sections
));
10689 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10691 uint32_t offset
= read_4_bytes (dbfd
,
10692 dwp_htab
->section_pool
.v2
.offsets
10693 + (((unit_index
- 1) * dwp_htab
->nr_columns
10695 * sizeof (uint32_t)));
10696 uint32_t size
= read_4_bytes (dbfd
,
10697 dwp_htab
->section_pool
.v2
.sizes
10698 + (((unit_index
- 1) * dwp_htab
->nr_columns
10700 * sizeof (uint32_t)));
10702 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
10705 case DW_SECT_TYPES
:
10706 sections
.info_or_types_offset
= offset
;
10707 sections
.info_or_types_size
= size
;
10709 case DW_SECT_ABBREV
:
10710 sections
.abbrev_offset
= offset
;
10711 sections
.abbrev_size
= size
;
10714 sections
.line_offset
= offset
;
10715 sections
.line_size
= size
;
10718 sections
.loc_offset
= offset
;
10719 sections
.loc_size
= size
;
10721 case DW_SECT_STR_OFFSETS
:
10722 sections
.str_offsets_offset
= offset
;
10723 sections
.str_offsets_size
= size
;
10725 case DW_SECT_MACINFO
:
10726 sections
.macinfo_offset
= offset
;
10727 sections
.macinfo_size
= size
;
10729 case DW_SECT_MACRO
:
10730 sections
.macro_offset
= offset
;
10731 sections
.macro_size
= size
;
10736 /* It's easier for the rest of the code if we fake a struct dwo_file and
10737 have dwo_unit "live" in that. At least for now.
10739 The DWP file can be made up of a random collection of CUs and TUs.
10740 However, for each CU + set of TUs that came from the same original DWO
10741 file, we can combine them back into a virtual DWO file to save space
10742 (fewer struct dwo_file objects to allocate). Remember that for really
10743 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10745 std::string virtual_dwo_name
=
10746 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10747 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10748 (long) (sections
.line_size
? sections
.line_offset
: 0),
10749 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
10750 (long) (sections
.str_offsets_size
10751 ? sections
.str_offsets_offset
: 0));
10752 /* Can we use an existing virtual DWO file? */
10753 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10755 /* Create one if necessary. */
10756 if (*dwo_file_slot
== NULL
)
10758 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10759 virtual_dwo_name
.c_str ());
10761 dwo_file
= new struct dwo_file
;
10762 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10763 dwo_file
->comp_dir
= comp_dir
;
10764 dwo_file
->sections
.abbrev
=
10765 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
10766 sections
.abbrev_offset
,
10767 sections
.abbrev_size
);
10768 dwo_file
->sections
.line
=
10769 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
10770 sections
.line_offset
,
10771 sections
.line_size
);
10772 dwo_file
->sections
.loc
=
10773 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
10774 sections
.loc_offset
, sections
.loc_size
);
10775 dwo_file
->sections
.macinfo
=
10776 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
10777 sections
.macinfo_offset
,
10778 sections
.macinfo_size
);
10779 dwo_file
->sections
.macro
=
10780 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
10781 sections
.macro_offset
,
10782 sections
.macro_size
);
10783 dwo_file
->sections
.str_offsets
=
10784 create_dwp_v2_or_v5_section (per_objfile
,
10785 &dwp_file
->sections
.str_offsets
,
10786 sections
.str_offsets_offset
,
10787 sections
.str_offsets_size
);
10788 /* The "str" section is global to the entire DWP file. */
10789 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10790 /* The info or types section is assigned below to dwo_unit,
10791 there's no need to record it in dwo_file.
10792 Also, we can't simply record type sections in dwo_file because
10793 we record a pointer into the vector in dwo_unit. As we collect more
10794 types we'll grow the vector and eventually have to reallocate space
10795 for it, invalidating all copies of pointers into the previous
10797 *dwo_file_slot
= dwo_file
;
10801 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10802 virtual_dwo_name
.c_str ());
10804 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10807 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10808 dwo_unit
->dwo_file
= dwo_file
;
10809 dwo_unit
->signature
= signature
;
10810 dwo_unit
->section
=
10811 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10812 *dwo_unit
->section
= create_dwp_v2_or_v5_section
10815 ? &dwp_file
->sections
.types
10816 : &dwp_file
->sections
.info
,
10817 sections
.info_or_types_offset
,
10818 sections
.info_or_types_size
);
10819 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10824 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10825 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10826 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10827 This is for DWP version 5 files. */
10829 static struct dwo_unit
*
10830 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
10831 struct dwp_file
*dwp_file
,
10832 uint32_t unit_index
,
10833 const char *comp_dir
,
10834 ULONGEST signature
, int is_debug_types
)
10836 const struct dwp_hash_table
*dwp_htab
10837 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10838 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10839 const char *kind
= is_debug_types
? "TU" : "CU";
10840 struct dwo_file
*dwo_file
;
10841 struct dwo_unit
*dwo_unit
;
10842 struct virtual_v2_or_v5_dwo_sections sections
{};
10843 void **dwo_file_slot
;
10845 gdb_assert (dwp_file
->version
== 5);
10847 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10848 kind
, pulongest (unit_index
), hex_string (signature
),
10851 /* Fetch the section offsets of this DWO unit. */
10853 /* memset (§ions, 0, sizeof (sections)); */
10855 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10857 uint32_t offset
= read_4_bytes (dbfd
,
10858 dwp_htab
->section_pool
.v5
.offsets
10859 + (((unit_index
- 1)
10860 * dwp_htab
->nr_columns
10862 * sizeof (uint32_t)));
10863 uint32_t size
= read_4_bytes (dbfd
,
10864 dwp_htab
->section_pool
.v5
.sizes
10865 + (((unit_index
- 1) * dwp_htab
->nr_columns
10867 * sizeof (uint32_t)));
10869 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
10871 case DW_SECT_ABBREV_V5
:
10872 sections
.abbrev_offset
= offset
;
10873 sections
.abbrev_size
= size
;
10875 case DW_SECT_INFO_V5
:
10876 sections
.info_or_types_offset
= offset
;
10877 sections
.info_or_types_size
= size
;
10879 case DW_SECT_LINE_V5
:
10880 sections
.line_offset
= offset
;
10881 sections
.line_size
= size
;
10883 case DW_SECT_LOCLISTS_V5
:
10884 sections
.loclists_offset
= offset
;
10885 sections
.loclists_size
= size
;
10887 case DW_SECT_MACRO_V5
:
10888 sections
.macro_offset
= offset
;
10889 sections
.macro_size
= size
;
10891 case DW_SECT_RNGLISTS_V5
:
10892 sections
.rnglists_offset
= offset
;
10893 sections
.rnglists_size
= size
;
10895 case DW_SECT_STR_OFFSETS_V5
:
10896 sections
.str_offsets_offset
= offset
;
10897 sections
.str_offsets_size
= size
;
10899 case DW_SECT_RESERVED_V5
:
10905 /* It's easier for the rest of the code if we fake a struct dwo_file and
10906 have dwo_unit "live" in that. At least for now.
10908 The DWP file can be made up of a random collection of CUs and TUs.
10909 However, for each CU + set of TUs that came from the same original DWO
10910 file, we can combine them back into a virtual DWO file to save space
10911 (fewer struct dwo_file objects to allocate). Remember that for really
10912 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10914 std::string virtual_dwo_name
=
10915 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10916 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10917 (long) (sections
.line_size
? sections
.line_offset
: 0),
10918 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
10919 (long) (sections
.str_offsets_size
10920 ? sections
.str_offsets_offset
: 0),
10921 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
10922 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
10923 /* Can we use an existing virtual DWO file? */
10924 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
10925 virtual_dwo_name
.c_str (),
10927 /* Create one if necessary. */
10928 if (*dwo_file_slot
== NULL
)
10930 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10931 virtual_dwo_name
.c_str ());
10933 dwo_file
= new struct dwo_file
;
10934 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10935 dwo_file
->comp_dir
= comp_dir
;
10936 dwo_file
->sections
.abbrev
=
10937 create_dwp_v2_or_v5_section (per_objfile
,
10938 &dwp_file
->sections
.abbrev
,
10939 sections
.abbrev_offset
,
10940 sections
.abbrev_size
);
10941 dwo_file
->sections
.line
=
10942 create_dwp_v2_or_v5_section (per_objfile
,
10943 &dwp_file
->sections
.line
,
10944 sections
.line_offset
, sections
.line_size
);
10945 dwo_file
->sections
.macro
=
10946 create_dwp_v2_or_v5_section (per_objfile
,
10947 &dwp_file
->sections
.macro
,
10948 sections
.macro_offset
,
10949 sections
.macro_size
);
10950 dwo_file
->sections
.loclists
=
10951 create_dwp_v2_or_v5_section (per_objfile
,
10952 &dwp_file
->sections
.loclists
,
10953 sections
.loclists_offset
,
10954 sections
.loclists_size
);
10955 dwo_file
->sections
.rnglists
=
10956 create_dwp_v2_or_v5_section (per_objfile
,
10957 &dwp_file
->sections
.rnglists
,
10958 sections
.rnglists_offset
,
10959 sections
.rnglists_size
);
10960 dwo_file
->sections
.str_offsets
=
10961 create_dwp_v2_or_v5_section (per_objfile
,
10962 &dwp_file
->sections
.str_offsets
,
10963 sections
.str_offsets_offset
,
10964 sections
.str_offsets_size
);
10965 /* The "str" section is global to the entire DWP file. */
10966 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10967 /* The info or types section is assigned below to dwo_unit,
10968 there's no need to record it in dwo_file.
10969 Also, we can't simply record type sections in dwo_file because
10970 we record a pointer into the vector in dwo_unit. As we collect more
10971 types we'll grow the vector and eventually have to reallocate space
10972 for it, invalidating all copies of pointers into the previous
10974 *dwo_file_slot
= dwo_file
;
10978 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10979 virtual_dwo_name
.c_str ());
10981 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10984 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10985 dwo_unit
->dwo_file
= dwo_file
;
10986 dwo_unit
->signature
= signature
;
10988 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10989 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
10990 &dwp_file
->sections
.info
,
10991 sections
.info_or_types_offset
,
10992 sections
.info_or_types_size
);
10993 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10998 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10999 Returns NULL if the signature isn't found. */
11001 static struct dwo_unit
*
11002 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
11003 struct dwp_file
*dwp_file
, const char *comp_dir
,
11004 ULONGEST signature
, int is_debug_types
)
11006 const struct dwp_hash_table
*dwp_htab
=
11007 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11008 bfd
*dbfd
= dwp_file
->dbfd
.get ();
11009 uint32_t mask
= dwp_htab
->nr_slots
- 1;
11010 uint32_t hash
= signature
& mask
;
11011 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
11014 struct dwo_unit find_dwo_cu
;
11016 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
11017 find_dwo_cu
.signature
= signature
;
11018 slot
= htab_find_slot (is_debug_types
11019 ? dwp_file
->loaded_tus
.get ()
11020 : dwp_file
->loaded_cus
.get (),
11021 &find_dwo_cu
, INSERT
);
11024 return (struct dwo_unit
*) *slot
;
11026 /* Use a for loop so that we don't loop forever on bad debug info. */
11027 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
11029 ULONGEST signature_in_table
;
11031 signature_in_table
=
11032 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
11033 if (signature_in_table
== signature
)
11035 uint32_t unit_index
=
11036 read_4_bytes (dbfd
,
11037 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
11039 if (dwp_file
->version
== 1)
11041 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
11042 unit_index
, comp_dir
,
11043 signature
, is_debug_types
);
11045 else if (dwp_file
->version
== 2)
11047 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
11048 unit_index
, comp_dir
,
11049 signature
, is_debug_types
);
11051 else /* version == 5 */
11053 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
11054 unit_index
, comp_dir
,
11055 signature
, is_debug_types
);
11057 return (struct dwo_unit
*) *slot
;
11059 if (signature_in_table
== 0)
11061 hash
= (hash
+ hash2
) & mask
;
11064 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11065 " [in module %s]"),
11069 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11070 Open the file specified by FILE_NAME and hand it off to BFD for
11071 preliminary analysis. Return a newly initialized bfd *, which
11072 includes a canonicalized copy of FILE_NAME.
11073 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11074 SEARCH_CWD is true if the current directory is to be searched.
11075 It will be searched before debug-file-directory.
11076 If successful, the file is added to the bfd include table of the
11077 objfile's bfd (see gdb_bfd_record_inclusion).
11078 If unable to find/open the file, return NULL.
11079 NOTE: This function is derived from symfile_bfd_open. */
11081 static gdb_bfd_ref_ptr
11082 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
11083 const char *file_name
, int is_dwp
, int search_cwd
)
11086 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11087 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11088 to debug_file_directory. */
11089 const char *search_path
;
11090 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
11092 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
11095 if (!debug_file_directory
.empty ())
11097 search_path_holder
.reset (concat (".", dirname_separator_string
,
11098 debug_file_directory
.c_str (),
11100 search_path
= search_path_holder
.get ();
11106 search_path
= debug_file_directory
.c_str ();
11108 /* Add the path for the executable binary to the list of search paths. */
11109 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
11110 search_path_holder
.reset (concat (objfile_dir
.c_str (),
11111 dirname_separator_string
,
11112 search_path
, nullptr));
11113 search_path
= search_path_holder
.get ();
11115 openp_flags flags
= OPF_RETURN_REALPATH
;
11117 flags
|= OPF_SEARCH_IN_PATH
;
11119 gdb::unique_xmalloc_ptr
<char> absolute_name
;
11120 desc
= openp (search_path
, flags
, file_name
,
11121 O_RDONLY
| O_BINARY
, &absolute_name
);
11125 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
11127 if (sym_bfd
== NULL
)
11129 bfd_set_cacheable (sym_bfd
.get (), 1);
11131 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
11134 /* Success. Record the bfd as having been included by the objfile's bfd.
11135 This is important because things like demangled_names_hash lives in the
11136 objfile's per_bfd space and may have references to things like symbol
11137 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11138 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
, sym_bfd
.get ());
11143 /* Try to open DWO file FILE_NAME.
11144 COMP_DIR is the DW_AT_comp_dir attribute.
11145 The result is the bfd handle of the file.
11146 If there is a problem finding or opening the file, return NULL.
11147 Upon success, the canonicalized path of the file is stored in the bfd,
11148 same as symfile_bfd_open. */
11150 static gdb_bfd_ref_ptr
11151 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
11152 const char *file_name
, const char *comp_dir
)
11154 if (IS_ABSOLUTE_PATH (file_name
))
11155 return try_open_dwop_file (per_objfile
, file_name
,
11156 0 /*is_dwp*/, 0 /*search_cwd*/);
11158 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11160 if (comp_dir
!= NULL
)
11162 gdb::unique_xmalloc_ptr
<char> path_to_try
11163 (concat (comp_dir
, SLASH_STRING
, file_name
, (char *) NULL
));
11165 /* NOTE: If comp_dir is a relative path, this will also try the
11166 search path, which seems useful. */
11167 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, path_to_try
.get (),
11169 1 /*search_cwd*/));
11174 /* That didn't work, try debug-file-directory, which, despite its name,
11175 is a list of paths. */
11177 if (debug_file_directory
.empty ())
11180 return try_open_dwop_file (per_objfile
, file_name
,
11181 0 /*is_dwp*/, 1 /*search_cwd*/);
11184 /* This function is mapped across the sections and remembers the offset and
11185 size of each of the DWO debugging sections we are interested in. */
11188 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
11189 asection
*sectp
, dwo_sections
*dwo_sections
)
11191 const struct dwop_section_names
*names
= &dwop_section_names
;
11193 struct dwarf2_section_info
*dw_sect
= nullptr;
11195 if (names
->abbrev_dwo
.matches (sectp
->name
))
11196 dw_sect
= &dwo_sections
->abbrev
;
11197 else if (names
->info_dwo
.matches (sectp
->name
))
11198 dw_sect
= &dwo_sections
->info
;
11199 else if (names
->line_dwo
.matches (sectp
->name
))
11200 dw_sect
= &dwo_sections
->line
;
11201 else if (names
->loc_dwo
.matches (sectp
->name
))
11202 dw_sect
= &dwo_sections
->loc
;
11203 else if (names
->loclists_dwo
.matches (sectp
->name
))
11204 dw_sect
= &dwo_sections
->loclists
;
11205 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11206 dw_sect
= &dwo_sections
->macinfo
;
11207 else if (names
->macro_dwo
.matches (sectp
->name
))
11208 dw_sect
= &dwo_sections
->macro
;
11209 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11210 dw_sect
= &dwo_sections
->rnglists
;
11211 else if (names
->str_dwo
.matches (sectp
->name
))
11212 dw_sect
= &dwo_sections
->str
;
11213 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11214 dw_sect
= &dwo_sections
->str_offsets
;
11215 else if (names
->types_dwo
.matches (sectp
->name
))
11217 struct dwarf2_section_info type_section
;
11219 memset (&type_section
, 0, sizeof (type_section
));
11220 dwo_sections
->types
.push_back (type_section
);
11221 dw_sect
= &dwo_sections
->types
.back ();
11224 if (dw_sect
!= nullptr)
11226 dw_sect
->s
.section
= sectp
;
11227 dw_sect
->size
= bfd_section_size (sectp
);
11228 dw_sect
->read (objfile
);
11232 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11233 by PER_CU. This is for the non-DWP case.
11234 The result is NULL if DWO_NAME can't be found. */
11236 static struct dwo_file
*
11237 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
11238 const char *comp_dir
)
11240 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11242 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
11245 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
11250 dwo_file_up
dwo_file (new struct dwo_file
);
11251 dwo_file
->dwo_name
= dwo_name
;
11252 dwo_file
->comp_dir
= comp_dir
;
11253 dwo_file
->dbfd
= std::move (dbfd
);
11255 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
11256 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
11257 sec
, &dwo_file
->sections
);
11259 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
11262 if (cu
->per_cu
->dwarf_version
< 5)
11264 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
11265 dwo_file
->sections
.types
, dwo_file
->tus
);
11269 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
11270 &dwo_file
->sections
.info
, dwo_file
->tus
,
11271 rcuh_kind::COMPILE
);
11274 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
11276 return dwo_file
.release ();
11279 /* This function is mapped across the sections and remembers the offset and
11280 size of each of the DWP debugging sections common to version 1 and 2 that
11281 we are interested in. */
11284 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11285 asection
*sectp
, dwp_file
*dwp_file
)
11287 const struct dwop_section_names
*names
= &dwop_section_names
;
11288 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11290 /* Record the ELF section number for later lookup: this is what the
11291 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11292 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11293 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11295 /* Look for specific sections that we need. */
11296 struct dwarf2_section_info
*dw_sect
= nullptr;
11297 if (names
->str_dwo
.matches (sectp
->name
))
11298 dw_sect
= &dwp_file
->sections
.str
;
11299 else if (names
->cu_index
.matches (sectp
->name
))
11300 dw_sect
= &dwp_file
->sections
.cu_index
;
11301 else if (names
->tu_index
.matches (sectp
->name
))
11302 dw_sect
= &dwp_file
->sections
.tu_index
;
11304 if (dw_sect
!= nullptr)
11306 dw_sect
->s
.section
= sectp
;
11307 dw_sect
->size
= bfd_section_size (sectp
);
11308 dw_sect
->read (objfile
);
11312 /* This function is mapped across the sections and remembers the offset and
11313 size of each of the DWP version 2 debugging sections that we are interested
11314 in. This is split into a separate function because we don't know if we
11315 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11318 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11319 asection
*sectp
, void *dwp_file_ptr
)
11321 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11322 const struct dwop_section_names
*names
= &dwop_section_names
;
11323 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11325 /* Record the ELF section number for later lookup: this is what the
11326 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11327 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11328 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11330 /* Look for specific sections that we need. */
11331 struct dwarf2_section_info
*dw_sect
= nullptr;
11332 if (names
->abbrev_dwo
.matches (sectp
->name
))
11333 dw_sect
= &dwp_file
->sections
.abbrev
;
11334 else if (names
->info_dwo
.matches (sectp
->name
))
11335 dw_sect
= &dwp_file
->sections
.info
;
11336 else if (names
->line_dwo
.matches (sectp
->name
))
11337 dw_sect
= &dwp_file
->sections
.line
;
11338 else if (names
->loc_dwo
.matches (sectp
->name
))
11339 dw_sect
= &dwp_file
->sections
.loc
;
11340 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11341 dw_sect
= &dwp_file
->sections
.macinfo
;
11342 else if (names
->macro_dwo
.matches (sectp
->name
))
11343 dw_sect
= &dwp_file
->sections
.macro
;
11344 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11345 dw_sect
= &dwp_file
->sections
.str_offsets
;
11346 else if (names
->types_dwo
.matches (sectp
->name
))
11347 dw_sect
= &dwp_file
->sections
.types
;
11349 if (dw_sect
!= nullptr)
11351 dw_sect
->s
.section
= sectp
;
11352 dw_sect
->size
= bfd_section_size (sectp
);
11353 dw_sect
->read (objfile
);
11357 /* This function is mapped across the sections and remembers the offset and
11358 size of each of the DWP version 5 debugging sections that we are interested
11359 in. This is split into a separate function because we don't know if we
11360 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11363 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11364 asection
*sectp
, void *dwp_file_ptr
)
11366 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11367 const struct dwop_section_names
*names
= &dwop_section_names
;
11368 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11370 /* Record the ELF section number for later lookup: this is what the
11371 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11372 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11373 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11375 /* Look for specific sections that we need. */
11376 struct dwarf2_section_info
*dw_sect
= nullptr;
11377 if (names
->abbrev_dwo
.matches (sectp
->name
))
11378 dw_sect
= &dwp_file
->sections
.abbrev
;
11379 else if (names
->info_dwo
.matches (sectp
->name
))
11380 dw_sect
= &dwp_file
->sections
.info
;
11381 else if (names
->line_dwo
.matches (sectp
->name
))
11382 dw_sect
= &dwp_file
->sections
.line
;
11383 else if (names
->loclists_dwo
.matches (sectp
->name
))
11384 dw_sect
= &dwp_file
->sections
.loclists
;
11385 else if (names
->macro_dwo
.matches (sectp
->name
))
11386 dw_sect
= &dwp_file
->sections
.macro
;
11387 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11388 dw_sect
= &dwp_file
->sections
.rnglists
;
11389 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11390 dw_sect
= &dwp_file
->sections
.str_offsets
;
11392 if (dw_sect
!= nullptr)
11394 dw_sect
->s
.section
= sectp
;
11395 dw_sect
->size
= bfd_section_size (sectp
);
11396 dw_sect
->read (objfile
);
11400 /* Hash function for dwp_file loaded CUs/TUs. */
11403 hash_dwp_loaded_cutus (const void *item
)
11405 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
11407 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11408 return dwo_unit
->signature
;
11411 /* Equality function for dwp_file loaded CUs/TUs. */
11414 eq_dwp_loaded_cutus (const void *a
, const void *b
)
11416 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
11417 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
11419 return dua
->signature
== dub
->signature
;
11422 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11425 allocate_dwp_loaded_cutus_table ()
11427 return htab_up (htab_create_alloc (3,
11428 hash_dwp_loaded_cutus
,
11429 eq_dwp_loaded_cutus
,
11430 NULL
, xcalloc
, xfree
));
11433 /* Try to open DWP file FILE_NAME.
11434 The result is the bfd handle of the file.
11435 If there is a problem finding or opening the file, return NULL.
11436 Upon success, the canonicalized path of the file is stored in the bfd,
11437 same as symfile_bfd_open. */
11439 static gdb_bfd_ref_ptr
11440 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
11442 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
11444 1 /*search_cwd*/));
11448 /* Work around upstream bug 15652.
11449 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11450 [Whether that's a "bug" is debatable, but it is getting in our way.]
11451 We have no real idea where the dwp file is, because gdb's realpath-ing
11452 of the executable's path may have discarded the needed info.
11453 [IWBN if the dwp file name was recorded in the executable, akin to
11454 .gnu_debuglink, but that doesn't exist yet.]
11455 Strip the directory from FILE_NAME and search again. */
11456 if (!debug_file_directory
.empty ())
11458 /* Don't implicitly search the current directory here.
11459 If the user wants to search "." to handle this case,
11460 it must be added to debug-file-directory. */
11461 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
11469 /* Initialize the use of the DWP file for the current objfile.
11470 By convention the name of the DWP file is ${objfile}.dwp.
11471 The result is NULL if it can't be found. */
11473 static std::unique_ptr
<struct dwp_file
>
11474 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
11476 struct objfile
*objfile
= per_objfile
->objfile
;
11478 /* Try to find first .dwp for the binary file before any symbolic links
11481 /* If the objfile is a debug file, find the name of the real binary
11482 file and get the name of dwp file from there. */
11483 std::string dwp_name
;
11484 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
11486 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
11487 const char *backlink_basename
= lbasename (backlink
->original_name
);
11489 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
11492 dwp_name
= objfile
->original_name
;
11494 dwp_name
+= ".dwp";
11496 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
11498 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
11500 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11501 dwp_name
= objfile_name (objfile
);
11502 dwp_name
+= ".dwp";
11503 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
11508 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
11510 return std::unique_ptr
<dwp_file
> ();
11513 const char *name
= bfd_get_filename (dbfd
.get ());
11514 std::unique_ptr
<struct dwp_file
> dwp_file
11515 (new struct dwp_file (name
, std::move (dbfd
)));
11517 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
11518 dwp_file
->elf_sections
=
11519 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
11520 dwp_file
->num_sections
, asection
*);
11522 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11523 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11526 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
11528 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
11530 /* The DWP file version is stored in the hash table. Oh well. */
11531 if (dwp_file
->cus
&& dwp_file
->tus
11532 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
11534 /* Technically speaking, we should try to limp along, but this is
11535 pretty bizarre. We use pulongest here because that's the established
11536 portability solution (e.g, we cannot use %u for uint32_t). */
11537 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11538 " TU version %s [in DWP file %s]"),
11539 pulongest (dwp_file
->cus
->version
),
11540 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
11544 dwp_file
->version
= dwp_file
->cus
->version
;
11545 else if (dwp_file
->tus
)
11546 dwp_file
->version
= dwp_file
->tus
->version
;
11548 dwp_file
->version
= 2;
11550 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11552 if (dwp_file
->version
== 2)
11553 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11556 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11560 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
11561 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
11563 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
11564 dwarf_read_debug_printf (" %s CUs, %s TUs",
11565 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
11566 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
11571 /* Wrapper around open_and_init_dwp_file, only open it once. */
11573 static struct dwp_file
*
11574 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
11576 if (!per_objfile
->per_bfd
->dwp_checked
)
11578 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
11579 per_objfile
->per_bfd
->dwp_checked
= 1;
11581 return per_objfile
->per_bfd
->dwp_file
.get ();
11584 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11585 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11586 or in the DWP file for the objfile, referenced by THIS_UNIT.
11587 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11588 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11590 This is called, for example, when wanting to read a variable with a
11591 complex location. Therefore we don't want to do file i/o for every call.
11592 Therefore we don't want to look for a DWO file on every call.
11593 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11594 then we check if we've already seen DWO_NAME, and only THEN do we check
11597 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11598 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11600 static struct dwo_unit
*
11601 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11602 ULONGEST signature
, int is_debug_types
)
11604 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11605 struct objfile
*objfile
= per_objfile
->objfile
;
11606 const char *kind
= is_debug_types
? "TU" : "CU";
11607 void **dwo_file_slot
;
11608 struct dwo_file
*dwo_file
;
11609 struct dwp_file
*dwp_file
;
11611 /* First see if there's a DWP file.
11612 If we have a DWP file but didn't find the DWO inside it, don't
11613 look for the original DWO file. It makes gdb behave differently
11614 depending on whether one is debugging in the build tree. */
11616 dwp_file
= get_dwp_file (per_objfile
);
11617 if (dwp_file
!= NULL
)
11619 const struct dwp_hash_table
*dwp_htab
=
11620 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11622 if (dwp_htab
!= NULL
)
11624 struct dwo_unit
*dwo_cutu
=
11625 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
11628 if (dwo_cutu
!= NULL
)
11630 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11631 kind
, hex_string (signature
),
11632 host_address_to_string (dwo_cutu
));
11640 /* No DWP file, look for the DWO file. */
11642 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
11643 if (*dwo_file_slot
== NULL
)
11645 /* Read in the file and build a table of the CUs/TUs it contains. */
11646 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
11648 /* NOTE: This will be NULL if unable to open the file. */
11649 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
11651 if (dwo_file
!= NULL
)
11653 struct dwo_unit
*dwo_cutu
= NULL
;
11655 if (is_debug_types
&& dwo_file
->tus
)
11657 struct dwo_unit find_dwo_cutu
;
11659 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11660 find_dwo_cutu
.signature
= signature
;
11662 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
11665 else if (!is_debug_types
&& dwo_file
->cus
)
11667 struct dwo_unit find_dwo_cutu
;
11669 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11670 find_dwo_cutu
.signature
= signature
;
11671 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
11675 if (dwo_cutu
!= NULL
)
11677 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11678 kind
, dwo_name
, hex_string (signature
),
11679 host_address_to_string (dwo_cutu
));
11686 /* We didn't find it. This could mean a dwo_id mismatch, or
11687 someone deleted the DWO/DWP file, or the search path isn't set up
11688 correctly to find the file. */
11690 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11691 kind
, dwo_name
, hex_string (signature
));
11693 /* This is a warning and not a complaint because it can be caused by
11694 pilot error (e.g., user accidentally deleting the DWO). */
11696 /* Print the name of the DWP file if we looked there, helps the user
11697 better diagnose the problem. */
11698 std::string dwp_text
;
11700 if (dwp_file
!= NULL
)
11701 dwp_text
= string_printf (" [in DWP file %s]",
11702 lbasename (dwp_file
->name
));
11704 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11705 " [in module %s]"),
11706 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
11707 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
11712 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11713 See lookup_dwo_cutu_unit for details. */
11715 static struct dwo_unit
*
11716 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11717 ULONGEST signature
)
11719 gdb_assert (!cu
->per_cu
->is_debug_types
);
11721 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
11724 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11725 See lookup_dwo_cutu_unit for details. */
11727 static struct dwo_unit
*
11728 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
11730 gdb_assert (cu
->per_cu
->is_debug_types
);
11732 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
11734 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
11737 /* Traversal function for queue_and_load_all_dwo_tus. */
11740 queue_and_load_dwo_tu (void **slot
, void *info
)
11742 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
11743 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
11744 ULONGEST signature
= dwo_unit
->signature
;
11745 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
11747 if (sig_type
!= NULL
)
11749 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11750 a real dependency of PER_CU on SIG_TYPE. That is detected later
11751 while processing PER_CU. */
11752 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
11754 load_full_type_unit (sig_type
, cu
->per_objfile
);
11755 cu
->per_cu
->imported_symtabs_push (sig_type
);
11761 /* Queue all TUs contained in the DWO of CU to be read in.
11762 The DWO may have the only definition of the type, though it may not be
11763 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11764 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11767 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
11769 struct dwo_unit
*dwo_unit
;
11770 struct dwo_file
*dwo_file
;
11772 gdb_assert (cu
!= nullptr);
11773 gdb_assert (!cu
->per_cu
->is_debug_types
);
11774 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
11776 dwo_unit
= cu
->dwo_unit
;
11777 gdb_assert (dwo_unit
!= NULL
);
11779 dwo_file
= dwo_unit
->dwo_file
;
11780 if (dwo_file
->tus
!= NULL
)
11781 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
11784 /* Read in various DIEs. */
11786 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11787 Inherit only the children of the DW_AT_abstract_origin DIE not being
11788 already referenced by DW_AT_abstract_origin from the children of the
11792 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
11794 struct die_info
*child_die
;
11795 sect_offset
*offsetp
;
11796 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11797 struct die_info
*origin_die
;
11798 /* Iterator of the ORIGIN_DIE children. */
11799 struct die_info
*origin_child_die
;
11800 struct attribute
*attr
;
11801 struct dwarf2_cu
*origin_cu
;
11802 struct pending
**origin_previous_list_in_scope
;
11804 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
11808 /* Note that following die references may follow to a die in a
11812 origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
11814 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11816 origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
11817 origin_cu
->list_in_scope
= cu
->list_in_scope
;
11819 if (die
->tag
!= origin_die
->tag
11820 && !(die
->tag
== DW_TAG_inlined_subroutine
11821 && origin_die
->tag
== DW_TAG_subprogram
))
11822 complaint (_("DIE %s and its abstract origin %s have different tags"),
11823 sect_offset_str (die
->sect_off
),
11824 sect_offset_str (origin_die
->sect_off
));
11826 /* Find if the concrete and abstract trees are structurally the
11827 same. This is a shallow traversal and it is not bullet-proof;
11828 the compiler can trick the debugger into believing that the trees
11829 are isomorphic, whereas they actually are not. However, the
11830 likelyhood of this happening is pretty low, and a full-fledged
11831 check would be an overkill. */
11832 bool are_isomorphic
= true;
11833 die_info
*concrete_child
= die
->child
;
11834 die_info
*abstract_child
= origin_die
->child
;
11835 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
11837 if (concrete_child
== nullptr
11838 || abstract_child
== nullptr
11839 || concrete_child
->tag
!= abstract_child
->tag
)
11841 are_isomorphic
= false;
11845 concrete_child
= concrete_child
->sibling
;
11846 abstract_child
= abstract_child
->sibling
;
11849 /* Walk the origin's children in parallel to the concrete children.
11850 This helps match an origin child in case the debug info misses
11851 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11852 origin tree may not have the same tree structure as the concrete
11854 die_info
*corresponding_abstract_child
11855 = are_isomorphic
? origin_die
->child
: nullptr;
11857 std::vector
<sect_offset
> offsets
;
11859 for (child_die
= die
->child
;
11860 child_die
&& child_die
->tag
;
11861 child_die
= child_die
->sibling
)
11863 struct die_info
*child_origin_die
;
11864 struct dwarf2_cu
*child_origin_cu
;
11866 /* We are trying to process concrete instance entries:
11867 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11868 it's not relevant to our analysis here. i.e. detecting DIEs that are
11869 present in the abstract instance but not referenced in the concrete
11871 if (child_die
->tag
== DW_TAG_call_site
11872 || child_die
->tag
== DW_TAG_GNU_call_site
)
11874 if (are_isomorphic
)
11875 corresponding_abstract_child
11876 = corresponding_abstract_child
->sibling
;
11880 /* For each CHILD_DIE, find the corresponding child of
11881 ORIGIN_DIE. If there is more than one layer of
11882 DW_AT_abstract_origin, follow them all; there shouldn't be,
11883 but GCC versions at least through 4.4 generate this (GCC PR
11885 child_origin_die
= child_die
;
11886 child_origin_cu
= cu
;
11889 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
11893 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
11897 /* If missing DW_AT_abstract_origin, try the corresponding child
11898 of the origin. Clang emits such lexical scopes. */
11899 if (child_origin_die
== child_die
11900 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
11902 && child_die
->tag
== DW_TAG_lexical_block
)
11903 child_origin_die
= corresponding_abstract_child
;
11905 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11906 counterpart may exist. */
11907 if (child_origin_die
!= child_die
)
11909 if (child_die
->tag
!= child_origin_die
->tag
11910 && !(child_die
->tag
== DW_TAG_inlined_subroutine
11911 && child_origin_die
->tag
== DW_TAG_subprogram
))
11912 complaint (_("Child DIE %s and its abstract origin %s have "
11914 sect_offset_str (child_die
->sect_off
),
11915 sect_offset_str (child_origin_die
->sect_off
));
11916 if (child_origin_die
->parent
!= origin_die
)
11917 complaint (_("Child DIE %s and its abstract origin %s have "
11918 "different parents"),
11919 sect_offset_str (child_die
->sect_off
),
11920 sect_offset_str (child_origin_die
->sect_off
));
11922 offsets
.push_back (child_origin_die
->sect_off
);
11925 if (are_isomorphic
)
11926 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
11928 std::sort (offsets
.begin (), offsets
.end ());
11929 sect_offset
*offsets_end
= offsets
.data () + offsets
.size ();
11930 for (offsetp
= offsets
.data () + 1; offsetp
< offsets_end
; offsetp
++)
11931 if (offsetp
[-1] == *offsetp
)
11932 complaint (_("Multiple children of DIE %s refer "
11933 "to DIE %s as their abstract origin"),
11934 sect_offset_str (die
->sect_off
), sect_offset_str (*offsetp
));
11936 offsetp
= offsets
.data ();
11937 origin_child_die
= origin_die
->child
;
11938 while (origin_child_die
&& origin_child_die
->tag
)
11940 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11941 while (offsetp
< offsets_end
11942 && *offsetp
< origin_child_die
->sect_off
)
11944 if (offsetp
>= offsets_end
11945 || *offsetp
> origin_child_die
->sect_off
)
11947 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11948 Check whether we're already processing ORIGIN_CHILD_DIE.
11949 This can happen with mutually referenced abstract_origins.
11951 if (!origin_child_die
->in_process
)
11952 process_die (origin_child_die
, origin_cu
);
11954 origin_child_die
= origin_child_die
->sibling
;
11956 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
11958 if (cu
!= origin_cu
)
11959 compute_delayed_physnames (origin_cu
);
11963 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
11965 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11966 struct gdbarch
*gdbarch
= objfile
->arch ();
11967 struct context_stack
*newobj
;
11970 struct die_info
*child_die
;
11971 struct attribute
*attr
, *call_line
, *call_file
;
11973 CORE_ADDR baseaddr
;
11974 struct block
*block
;
11975 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
11976 std::vector
<struct symbol
*> template_args
;
11977 struct template_symbol
*templ_func
= NULL
;
11981 /* If we do not have call site information, we can't show the
11982 caller of this inlined function. That's too confusing, so
11983 only use the scope for local variables. */
11984 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
11985 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
11986 if (call_line
== NULL
|| call_file
== NULL
)
11988 read_lexical_block_scope (die
, cu
);
11993 baseaddr
= objfile
->text_section_offset ();
11995 name
= dwarf2_name (die
, cu
);
11997 /* Ignore functions with missing or empty names. These are actually
11998 illegal according to the DWARF standard. */
12001 complaint (_("missing name for subprogram DIE at %s"),
12002 sect_offset_str (die
->sect_off
));
12006 /* Ignore functions with missing or invalid low and high pc attributes. */
12007 if (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr)
12008 <= PC_BOUNDS_INVALID
)
12010 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
12011 if (attr
== nullptr || !attr
->as_boolean ())
12012 complaint (_("cannot get low and high bounds "
12013 "for subprogram DIE at %s"),
12014 sect_offset_str (die
->sect_off
));
12018 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12019 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12021 /* If we have any template arguments, then we must allocate a
12022 different sort of symbol. */
12023 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
12025 if (child_die
->tag
== DW_TAG_template_type_param
12026 || child_die
->tag
== DW_TAG_template_value_param
)
12028 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
12029 templ_func
->subclass
= SYMBOL_TEMPLATE
;
12034 gdb_assert (cu
->get_builder () != nullptr);
12035 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
12036 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
,
12037 (struct symbol
*) templ_func
);
12039 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
12040 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
12043 /* If there is a location expression for DW_AT_frame_base, record
12045 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
12046 if (attr
!= nullptr)
12047 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
12049 /* If there is a location for the static link, record it. */
12050 newobj
->static_link
= NULL
;
12051 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
12052 if (attr
!= nullptr)
12054 newobj
->static_link
12055 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
12056 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
12060 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
12062 if (die
->child
!= NULL
)
12064 child_die
= die
->child
;
12065 while (child_die
&& child_die
->tag
)
12067 if (child_die
->tag
== DW_TAG_template_type_param
12068 || child_die
->tag
== DW_TAG_template_value_param
)
12070 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12073 template_args
.push_back (arg
);
12076 process_die (child_die
, cu
);
12077 child_die
= child_die
->sibling
;
12081 inherit_abstract_dies (die
, cu
);
12083 /* If we have a DW_AT_specification, we might need to import using
12084 directives from the context of the specification DIE. See the
12085 comment in determine_prefix. */
12086 if (cu
->per_cu
->lang
== language_cplus
12087 && dwarf2_attr (die
, DW_AT_specification
, cu
))
12089 struct dwarf2_cu
*spec_cu
= cu
;
12090 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
12094 child_die
= spec_die
->child
;
12095 while (child_die
&& child_die
->tag
)
12097 if (child_die
->tag
== DW_TAG_imported_module
)
12098 process_die (child_die
, spec_cu
);
12099 child_die
= child_die
->sibling
;
12102 /* In some cases, GCC generates specification DIEs that
12103 themselves contain DW_AT_specification attributes. */
12104 spec_die
= die_specification (spec_die
, &spec_cu
);
12108 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12109 /* Make a block for the local symbols within. */
12110 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
12111 cstk
.static_link
, lowpc
, highpc
);
12113 /* For C++, set the block's scope. */
12114 if ((cu
->per_cu
->lang
== language_cplus
12115 || cu
->per_cu
->lang
== language_fortran
12116 || cu
->per_cu
->lang
== language_d
12117 || cu
->per_cu
->lang
== language_rust
)
12118 && cu
->processing_has_namespace_info
)
12119 block_set_scope (block
, determine_prefix (die
, cu
),
12120 &objfile
->objfile_obstack
);
12122 /* If we have address ranges, record them. */
12123 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12125 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
12127 /* Attach template arguments to function. */
12128 if (!template_args
.empty ())
12130 gdb_assert (templ_func
!= NULL
);
12132 templ_func
->n_template_arguments
= template_args
.size ();
12133 templ_func
->template_arguments
12134 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
12135 templ_func
->n_template_arguments
);
12136 memcpy (templ_func
->template_arguments
,
12137 template_args
.data (),
12138 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
12140 /* Make sure that the symtab is set on the new symbols. Even
12141 though they don't appear in this symtab directly, other parts
12142 of gdb assume that symbols do, and this is reasonably
12144 for (symbol
*sym
: template_args
)
12145 symbol_set_symtab (sym
, symbol_symtab (templ_func
));
12148 /* In C++, we can have functions nested inside functions (e.g., when
12149 a function declares a class that has methods). This means that
12150 when we finish processing a function scope, we may need to go
12151 back to building a containing block's symbol lists. */
12152 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12153 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12155 /* If we've finished processing a top-level function, subsequent
12156 symbols go in the file symbol list. */
12157 if (cu
->get_builder ()->outermost_context_p ())
12158 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
12161 /* Process all the DIES contained within a lexical block scope. Start
12162 a new scope, process the dies, and then close the scope. */
12165 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12167 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12168 struct gdbarch
*gdbarch
= objfile
->arch ();
12169 CORE_ADDR lowpc
, highpc
;
12170 struct die_info
*child_die
;
12171 CORE_ADDR baseaddr
;
12173 baseaddr
= objfile
->text_section_offset ();
12175 /* Ignore blocks with missing or invalid low and high pc attributes. */
12176 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12177 as multiple lexical blocks? Handling children in a sane way would
12178 be nasty. Might be easier to properly extend generic blocks to
12179 describe ranges. */
12180 switch (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr))
12182 case PC_BOUNDS_NOT_PRESENT
:
12183 /* DW_TAG_lexical_block has no attributes, process its children as if
12184 there was no wrapping by that DW_TAG_lexical_block.
12185 GCC does no longer produces such DWARF since GCC r224161. */
12186 for (child_die
= die
->child
;
12187 child_die
!= NULL
&& child_die
->tag
;
12188 child_die
= child_die
->sibling
)
12190 /* We might already be processing this DIE. This can happen
12191 in an unusual circumstance -- where a subroutine A
12192 appears lexically in another subroutine B, but A actually
12193 inlines B. The recursion is broken here, rather than in
12194 inherit_abstract_dies, because it seems better to simply
12195 drop concrete children here. */
12196 if (!child_die
->in_process
)
12197 process_die (child_die
, cu
);
12200 case PC_BOUNDS_INVALID
:
12203 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12204 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12206 cu
->get_builder ()->push_context (0, lowpc
);
12207 if (die
->child
!= NULL
)
12209 child_die
= die
->child
;
12210 while (child_die
&& child_die
->tag
)
12212 process_die (child_die
, cu
);
12213 child_die
= child_die
->sibling
;
12216 inherit_abstract_dies (die
, cu
);
12217 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12219 if (*cu
->get_builder ()->get_local_symbols () != NULL
12220 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
12222 struct block
*block
12223 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
12224 cstk
.start_addr
, highpc
);
12226 /* Note that recording ranges after traversing children, as we
12227 do here, means that recording a parent's ranges entails
12228 walking across all its children's ranges as they appear in
12229 the address map, which is quadratic behavior.
12231 It would be nicer to record the parent's ranges before
12232 traversing its children, simply overriding whatever you find
12233 there. But since we don't even decide whether to create a
12234 block until after we've traversed its children, that's hard
12236 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12238 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12239 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12242 static void dwarf2_ranges_read_low_addrs (unsigned offset
,
12243 struct dwarf2_cu
*cu
,
12245 std::vector
<CORE_ADDR
> &result
);
12247 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12250 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12252 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12253 struct objfile
*objfile
= per_objfile
->objfile
;
12254 struct gdbarch
*gdbarch
= objfile
->arch ();
12255 CORE_ADDR pc
, baseaddr
;
12256 struct attribute
*attr
;
12259 struct die_info
*child_die
;
12261 baseaddr
= objfile
->text_section_offset ();
12263 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
12266 /* This was a pre-DWARF-5 GNU extension alias
12267 for DW_AT_call_return_pc. */
12268 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12272 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12273 "DIE %s [in module %s]"),
12274 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12277 pc
= attr
->as_address () + baseaddr
;
12278 pc
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc
);
12281 if (cu
->call_site_htab
== NULL
)
12282 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
12283 call_site::eq
, NULL
,
12284 &objfile
->objfile_obstack
,
12285 hashtab_obstack_allocate
, NULL
);
12286 struct call_site
call_site_local (pc
, nullptr, nullptr);
12287 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
12290 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12291 "DIE %s [in module %s]"),
12292 paddress (gdbarch
, pc
), sect_offset_str (die
->sect_off
),
12293 objfile_name (objfile
));
12297 /* Count parameters at the caller. */
12300 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
12301 child_die
= child_die
->sibling
)
12303 if (child_die
->tag
!= DW_TAG_call_site_parameter
12304 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12306 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12307 "DW_TAG_call_site child DIE %s [in module %s]"),
12308 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
12309 objfile_name (objfile
));
12316 struct call_site
*call_site
12317 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
12319 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
12320 struct call_site (pc
, cu
->per_cu
, per_objfile
);
12323 /* We never call the destructor of call_site, so we must ensure it is
12324 trivially destructible. */
12325 gdb_static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
12327 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
12328 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
12330 struct die_info
*func_die
;
12332 /* Skip also over DW_TAG_inlined_subroutine. */
12333 for (func_die
= die
->parent
;
12334 func_die
&& func_die
->tag
!= DW_TAG_subprogram
12335 && func_die
->tag
!= DW_TAG_subroutine_type
;
12336 func_die
= func_die
->parent
);
12338 /* DW_AT_call_all_calls is a superset
12339 of DW_AT_call_all_tail_calls. */
12341 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
12342 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
12343 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
12344 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
12346 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12347 not complete. But keep CALL_SITE for look ups via call_site_htab,
12348 both the initial caller containing the real return address PC and
12349 the final callee containing the current PC of a chain of tail
12350 calls do not need to have the tail call list complete. But any
12351 function candidate for a virtual tail call frame searched via
12352 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12353 determined unambiguously. */
12357 struct type
*func_type
= NULL
;
12360 func_type
= get_die_type (func_die
, cu
);
12361 if (func_type
!= NULL
)
12363 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
12365 /* Enlist this call site to the function. */
12366 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
12367 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
12370 complaint (_("Cannot find function owning DW_TAG_call_site "
12371 "DIE %s [in module %s]"),
12372 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12376 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
12378 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
12380 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
12383 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12384 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12387 call_site
->target
.set_loc_dwarf_block (nullptr);
12388 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
12389 /* Keep NULL DWARF_BLOCK. */;
12390 else if (attr
->form_is_block ())
12392 struct dwarf2_locexpr_baton
*dlbaton
;
12393 struct dwarf_block
*block
= attr
->as_block ();
12395 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
12396 dlbaton
->data
= block
->data
;
12397 dlbaton
->size
= block
->size
;
12398 dlbaton
->per_objfile
= per_objfile
;
12399 dlbaton
->per_cu
= cu
->per_cu
;
12401 call_site
->target
.set_loc_dwarf_block (dlbaton
);
12403 else if (attr
->form_is_ref ())
12405 struct dwarf2_cu
*target_cu
= cu
;
12406 struct die_info
*target_die
;
12408 target_die
= follow_die_ref (die
, attr
, &target_cu
);
12409 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
12411 struct attribute
*ranges_attr
12412 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
12414 if (die_is_declaration (target_die
, target_cu
))
12416 const char *target_physname
;
12418 /* Prefer the mangled name; otherwise compute the demangled one. */
12419 target_physname
= dw2_linkage_name (target_die
, target_cu
);
12420 if (target_physname
== NULL
)
12421 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
12422 if (target_physname
== NULL
)
12423 complaint (_("DW_AT_call_target target DIE has invalid "
12424 "physname, for referencing DIE %s [in module %s]"),
12425 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12427 call_site
->target
.set_loc_physname (target_physname
);
12429 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
12431 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
12432 + target_cu
->gnu_ranges_base
);
12433 std::vector
<CORE_ADDR
> addresses
;
12434 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
12435 target_die
->tag
, addresses
);
12436 CORE_ADDR
*saved
= XOBNEWVAR (&objfile
->objfile_obstack
, CORE_ADDR
,
12437 addresses
.size ());
12438 std::copy (addresses
.begin (), addresses
.end (), saved
);
12439 call_site
->target
.set_loc_array (addresses
.size (), saved
);
12445 /* DW_AT_entry_pc should be preferred. */
12446 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
12448 <= PC_BOUNDS_INVALID
)
12449 complaint (_("DW_AT_call_target target DIE has invalid "
12450 "low pc, for referencing DIE %s [in module %s]"),
12451 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12454 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
)
12456 call_site
->target
.set_loc_physaddr (lowpc
);
12461 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12462 "block nor reference, for DIE %s [in module %s]"),
12463 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12465 for (child_die
= die
->child
;
12466 child_die
&& child_die
->tag
;
12467 child_die
= child_die
->sibling
)
12469 struct call_site_parameter
*parameter
;
12470 struct attribute
*loc
, *origin
;
12472 if (child_die
->tag
!= DW_TAG_call_site_parameter
12473 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12475 /* Already printed the complaint above. */
12479 gdb_assert (call_site
->parameter_count
< nparams
);
12480 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
12482 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12483 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12484 register is contained in DW_AT_call_value. */
12486 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
12487 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
12488 if (origin
== NULL
)
12490 /* This was a pre-DWARF-5 GNU extension alias
12491 for DW_AT_call_parameter. */
12492 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
12494 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
12496 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
12498 sect_offset sect_off
= origin
->get_ref_die_offset ();
12499 if (!cu
->header
.offset_in_cu_p (sect_off
))
12501 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12502 binding can be done only inside one CU. Such referenced DIE
12503 therefore cannot be even moved to DW_TAG_partial_unit. */
12504 complaint (_("DW_AT_call_parameter offset is not in CU for "
12505 "DW_TAG_call_site child DIE %s [in module %s]"),
12506 sect_offset_str (child_die
->sect_off
),
12507 objfile_name (objfile
));
12510 parameter
->u
.param_cu_off
12511 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
12513 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
12515 complaint (_("No DW_FORM_block* DW_AT_location for "
12516 "DW_TAG_call_site child DIE %s [in module %s]"),
12517 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
12522 struct dwarf_block
*block
= loc
->as_block ();
12524 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
12525 (block
->data
, &block
->data
[block
->size
]);
12526 if (parameter
->u
.dwarf_reg
!= -1)
12527 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
12528 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
12529 &block
->data
[block
->size
],
12530 ¶meter
->u
.fb_offset
))
12531 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
12534 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12535 "for DW_FORM_block* DW_AT_location is supported for "
12536 "DW_TAG_call_site child DIE %s "
12538 sect_offset_str (child_die
->sect_off
),
12539 objfile_name (objfile
));
12544 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
12546 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
12547 if (attr
== NULL
|| !attr
->form_is_block ())
12549 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12550 "DW_TAG_call_site child DIE %s [in module %s]"),
12551 sect_offset_str (child_die
->sect_off
),
12552 objfile_name (objfile
));
12556 struct dwarf_block
*block
= attr
->as_block ();
12557 parameter
->value
= block
->data
;
12558 parameter
->value_size
= block
->size
;
12560 /* Parameters are not pre-cleared by memset above. */
12561 parameter
->data_value
= NULL
;
12562 parameter
->data_value_size
= 0;
12563 call_site
->parameter_count
++;
12565 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
12567 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
12568 if (attr
!= nullptr)
12570 if (!attr
->form_is_block ())
12571 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12572 "DW_TAG_call_site child DIE %s [in module %s]"),
12573 sect_offset_str (child_die
->sect_off
),
12574 objfile_name (objfile
));
12577 block
= attr
->as_block ();
12578 parameter
->data_value
= block
->data
;
12579 parameter
->data_value_size
= block
->size
;
12585 /* Helper function for read_variable. If DIE represents a virtual
12586 table, then return the type of the concrete object that is
12587 associated with the virtual table. Otherwise, return NULL. */
12589 static struct type
*
12590 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12592 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
12596 /* Find the type DIE. */
12597 struct die_info
*type_die
= NULL
;
12598 struct dwarf2_cu
*type_cu
= cu
;
12600 if (attr
->form_is_ref ())
12601 type_die
= follow_die_ref (die
, attr
, &type_cu
);
12602 if (type_die
== NULL
)
12605 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
12607 return die_containing_type (type_die
, type_cu
);
12610 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12613 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
12615 struct rust_vtable_symbol
*storage
= NULL
;
12617 if (cu
->per_cu
->lang
== language_rust
)
12619 struct type
*containing_type
= rust_containing_type (die
, cu
);
12621 if (containing_type
!= NULL
)
12623 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12625 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
12626 storage
->concrete_type
= containing_type
;
12627 storage
->subclass
= SYMBOL_RUST_VTABLE
;
12631 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
12632 struct attribute
*abstract_origin
12633 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12634 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
12635 if (res
== NULL
&& loc
&& abstract_origin
)
12637 /* We have a variable without a name, but with a location and an abstract
12638 origin. This may be a concrete instance of an abstract variable
12639 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12641 struct dwarf2_cu
*origin_cu
= cu
;
12642 struct die_info
*origin_die
12643 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
12644 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12645 per_objfile
->per_bfd
->abstract_to_concrete
12646 [origin_die
->sect_off
].push_back (die
->sect_off
);
12650 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12651 reading .debug_rnglists.
12652 Callback's type should be:
12653 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12654 Return true if the attributes are present and valid, otherwise,
12657 template <typename Callback
>
12659 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
12660 dwarf_tag tag
, Callback
&&callback
)
12662 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12663 struct objfile
*objfile
= per_objfile
->objfile
;
12664 bfd
*obfd
= objfile
->obfd
;
12665 /* Base address selection entry. */
12666 gdb::optional
<CORE_ADDR
> base
;
12667 const gdb_byte
*buffer
;
12668 bool overflow
= false;
12669 ULONGEST addr_index
;
12670 struct dwarf2_section_info
*rnglists_section
;
12672 base
= cu
->base_address
;
12673 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
12674 rnglists_section
->read (objfile
);
12676 if (offset
>= rnglists_section
->size
)
12678 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12682 buffer
= rnglists_section
->buffer
+ offset
;
12686 /* Initialize it due to a false compiler warning. */
12687 CORE_ADDR range_beginning
= 0, range_end
= 0;
12688 const gdb_byte
*buf_end
= (rnglists_section
->buffer
12689 + rnglists_section
->size
);
12690 unsigned int bytes_read
;
12692 if (buffer
== buf_end
)
12697 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
12700 case DW_RLE_end_of_list
:
12702 case DW_RLE_base_address
:
12703 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12708 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12709 buffer
+= bytes_read
;
12711 case DW_RLE_base_addressx
:
12712 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12713 buffer
+= bytes_read
;
12714 base
= read_addr_index (cu
, addr_index
);
12716 case DW_RLE_start_length
:
12717 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12722 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12724 buffer
+= bytes_read
;
12725 range_end
= (range_beginning
12726 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12727 buffer
+= bytes_read
;
12728 if (buffer
> buf_end
)
12734 case DW_RLE_startx_length
:
12735 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12736 buffer
+= bytes_read
;
12737 range_beginning
= read_addr_index (cu
, addr_index
);
12738 if (buffer
> buf_end
)
12743 range_end
= (range_beginning
12744 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12745 buffer
+= bytes_read
;
12747 case DW_RLE_offset_pair
:
12748 range_beginning
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12749 buffer
+= bytes_read
;
12750 if (buffer
> buf_end
)
12755 range_end
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12756 buffer
+= bytes_read
;
12757 if (buffer
> buf_end
)
12763 case DW_RLE_start_end
:
12764 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
12769 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12771 buffer
+= bytes_read
;
12772 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12773 buffer
+= bytes_read
;
12775 case DW_RLE_startx_endx
:
12776 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12777 buffer
+= bytes_read
;
12778 range_beginning
= read_addr_index (cu
, addr_index
);
12779 if (buffer
> buf_end
)
12784 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12785 buffer
+= bytes_read
;
12786 range_end
= read_addr_index (cu
, addr_index
);
12789 complaint (_("Invalid .debug_rnglists data (no base address)"));
12792 if (rlet
== DW_RLE_end_of_list
|| overflow
)
12794 if (rlet
== DW_RLE_base_address
)
12797 if (range_beginning
> range_end
)
12799 /* Inverted range entries are invalid. */
12800 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12804 /* Empty range entries have no effect. */
12805 if (range_beginning
== range_end
)
12808 /* Only DW_RLE_offset_pair needs the base address added. */
12809 if (rlet
== DW_RLE_offset_pair
)
12811 if (!base
.has_value ())
12813 /* We have no valid base address for the DW_RLE_offset_pair. */
12814 complaint (_("Invalid .debug_rnglists data (no base address for "
12815 "DW_RLE_offset_pair)"));
12819 range_beginning
+= *base
;
12820 range_end
+= *base
;
12823 /* A not-uncommon case of bad debug info.
12824 Don't pollute the addrmap with bad data. */
12825 if (range_beginning
== 0
12826 && !per_objfile
->per_bfd
->has_section_at_zero
)
12828 complaint (_(".debug_rnglists entry has start address of zero"
12829 " [in module %s]"), objfile_name (objfile
));
12833 callback (range_beginning
, range_end
);
12838 complaint (_("Offset %d is not terminated "
12839 "for DW_AT_ranges attribute"),
12847 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12848 Callback's type should be:
12849 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12850 Return 1 if the attributes are present and valid, otherwise, return 0. */
12852 template <typename Callback
>
12854 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
12855 Callback
&&callback
)
12857 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12858 struct objfile
*objfile
= per_objfile
->objfile
;
12859 struct comp_unit_head
*cu_header
= &cu
->header
;
12860 bfd
*obfd
= objfile
->obfd
;
12861 unsigned int addr_size
= cu_header
->addr_size
;
12862 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
12863 /* Base address selection entry. */
12864 gdb::optional
<CORE_ADDR
> base
;
12865 unsigned int dummy
;
12866 const gdb_byte
*buffer
;
12868 if (cu_header
->version
>= 5)
12869 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
12871 base
= cu
->base_address
;
12873 per_objfile
->per_bfd
->ranges
.read (objfile
);
12874 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
12876 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12880 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
12884 CORE_ADDR range_beginning
, range_end
;
12886 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12887 buffer
+= addr_size
;
12888 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12889 buffer
+= addr_size
;
12890 offset
+= 2 * addr_size
;
12892 /* An end of list marker is a pair of zero addresses. */
12893 if (range_beginning
== 0 && range_end
== 0)
12894 /* Found the end of list entry. */
12897 /* Each base address selection entry is a pair of 2 values.
12898 The first is the largest possible address, the second is
12899 the base address. Check for a base address here. */
12900 if ((range_beginning
& mask
) == mask
)
12902 /* If we found the largest possible address, then we already
12903 have the base address in range_end. */
12908 if (!base
.has_value ())
12910 /* We have no valid base address for the ranges
12912 complaint (_("Invalid .debug_ranges data (no base address)"));
12916 if (range_beginning
> range_end
)
12918 /* Inverted range entries are invalid. */
12919 complaint (_("Invalid .debug_ranges data (inverted range)"));
12923 /* Empty range entries have no effect. */
12924 if (range_beginning
== range_end
)
12927 range_beginning
+= *base
;
12928 range_end
+= *base
;
12930 /* A not-uncommon case of bad debug info.
12931 Don't pollute the addrmap with bad data. */
12932 if (range_beginning
== 0
12933 && !per_objfile
->per_bfd
->has_section_at_zero
)
12935 complaint (_(".debug_ranges entry has start address of zero"
12936 " [in module %s]"), objfile_name (objfile
));
12940 callback (range_beginning
, range_end
);
12946 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12947 Return 1 if the attributes are present and valid, otherwise, return 0.
12948 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12949 ranges in MAP are set, using DATUM as the value. */
12952 dwarf2_ranges_read (unsigned offset
, CORE_ADDR
*low_return
,
12953 CORE_ADDR
*high_return
, struct dwarf2_cu
*cu
,
12954 addrmap
*map
, void *datum
, dwarf_tag tag
)
12956 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12957 struct gdbarch
*gdbarch
= objfile
->arch ();
12958 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
12961 CORE_ADDR high
= 0;
12964 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
12965 [&] (CORE_ADDR range_beginning
, CORE_ADDR range_end
)
12967 if (map
!= nullptr)
12972 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12973 range_beginning
+ baseaddr
)
12975 highpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12976 range_end
+ baseaddr
)
12978 addrmap_set_empty (map
, lowpc
, highpc
- 1, datum
);
12981 /* FIXME: This is recording everything as a low-high
12982 segment of consecutive addresses. We should have a
12983 data structure for discontiguous block ranges
12987 low
= range_beginning
;
12993 if (range_beginning
< low
)
12994 low
= range_beginning
;
12995 if (range_end
> high
)
13003 /* If the first entry is an end-of-list marker, the range
13004 describes an empty scope, i.e. no instructions. */
13010 *high_return
= high
;
13014 /* Process ranges and fill in a vector of the low PC values only. */
13017 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
13019 std::vector
<CORE_ADDR
> &result
)
13021 dwarf2_ranges_process (offset
, cu
, tag
,
13022 [&] (CORE_ADDR start
, CORE_ADDR end
)
13024 result
.push_back (start
);
13028 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13029 definition for the return value. *LOWPC and *HIGHPC are set iff
13030 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13032 static enum pc_bounds_kind
13033 dwarf2_get_pc_bounds (struct die_info
*die
, CORE_ADDR
*lowpc
,
13034 CORE_ADDR
*highpc
, struct dwarf2_cu
*cu
,
13035 addrmap
*map
, void *datum
)
13037 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13038 struct attribute
*attr
;
13039 struct attribute
*attr_high
;
13041 CORE_ADDR high
= 0;
13042 enum pc_bounds_kind ret
;
13044 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13047 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13048 if (attr
!= nullptr)
13050 low
= attr
->as_address ();
13051 high
= attr_high
->as_address ();
13052 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13056 /* Found high w/o low attribute. */
13057 return PC_BOUNDS_INVALID
;
13059 /* Found consecutive range of addresses. */
13060 ret
= PC_BOUNDS_HIGH_LOW
;
13064 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13065 if (attr
!= nullptr && attr
->form_is_unsigned ())
13067 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13068 on DWARF version). */
13069 ULONGEST ranges_offset
= attr
->as_unsigned ();
13071 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13073 if (die
->tag
!= DW_TAG_compile_unit
)
13074 ranges_offset
+= cu
->gnu_ranges_base
;
13076 /* Value of the DW_AT_ranges attribute is the offset in the
13077 .debug_ranges section. */
13078 if (!dwarf2_ranges_read (ranges_offset
, &low
, &high
, cu
,
13079 map
, datum
, die
->tag
))
13080 return PC_BOUNDS_INVALID
;
13081 /* Found discontinuous range of addresses. */
13082 ret
= PC_BOUNDS_RANGES
;
13085 return PC_BOUNDS_NOT_PRESENT
;
13088 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13090 return PC_BOUNDS_INVALID
;
13092 /* When using the GNU linker, .gnu.linkonce. sections are used to
13093 eliminate duplicate copies of functions and vtables and such.
13094 The linker will arbitrarily choose one and discard the others.
13095 The AT_*_pc values for such functions refer to local labels in
13096 these sections. If the section from that file was discarded, the
13097 labels are not in the output, so the relocs get a value of 0.
13098 If this is a discarded function, mark the pc bounds as invalid,
13099 so that GDB will ignore it. */
13100 if (low
== 0 && !per_objfile
->per_bfd
->has_section_at_zero
)
13101 return PC_BOUNDS_INVALID
;
13109 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13110 its low and high PC addresses. Do nothing if these addresses could not
13111 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13112 and HIGHPC to the high address if greater than HIGHPC. */
13115 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
13116 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13117 struct dwarf2_cu
*cu
)
13119 CORE_ADDR low
, high
;
13120 struct die_info
*child
= die
->child
;
13122 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
13123 >= PC_BOUNDS_RANGES
)
13125 *lowpc
= std::min (*lowpc
, low
);
13126 *highpc
= std::max (*highpc
, high
);
13129 /* If the language does not allow nested subprograms (either inside
13130 subprograms or lexical blocks), we're done. */
13131 if (cu
->per_cu
->lang
!= language_ada
)
13134 /* Check all the children of the given DIE. If it contains nested
13135 subprograms, then check their pc bounds. Likewise, we need to
13136 check lexical blocks as well, as they may also contain subprogram
13138 while (child
&& child
->tag
)
13140 if (child
->tag
== DW_TAG_subprogram
13141 || child
->tag
== DW_TAG_lexical_block
)
13142 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
13143 child
= child
->sibling
;
13147 /* Get the low and high pc's represented by the scope DIE, and store
13148 them in *LOWPC and *HIGHPC. If the correct values can't be
13149 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13152 get_scope_pc_bounds (struct die_info
*die
,
13153 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13154 struct dwarf2_cu
*cu
)
13156 CORE_ADDR best_low
= (CORE_ADDR
) -1;
13157 CORE_ADDR best_high
= (CORE_ADDR
) 0;
13158 CORE_ADDR current_low
, current_high
;
13160 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
13162 >= PC_BOUNDS_RANGES
)
13164 best_low
= current_low
;
13165 best_high
= current_high
;
13169 struct die_info
*child
= die
->child
;
13171 while (child
&& child
->tag
)
13173 switch (child
->tag
) {
13174 case DW_TAG_subprogram
:
13175 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
13177 case DW_TAG_namespace
:
13178 case DW_TAG_module
:
13179 /* FIXME: carlton/2004-01-16: Should we do this for
13180 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13181 that current GCC's always emit the DIEs corresponding
13182 to definitions of methods of classes as children of a
13183 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13184 the DIEs giving the declarations, which could be
13185 anywhere). But I don't see any reason why the
13186 standards says that they have to be there. */
13187 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
13189 if (current_low
!= ((CORE_ADDR
) -1))
13191 best_low
= std::min (best_low
, current_low
);
13192 best_high
= std::max (best_high
, current_high
);
13200 child
= child
->sibling
;
13205 *highpc
= best_high
;
13208 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13212 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
13213 CORE_ADDR baseaddr
, struct dwarf2_cu
*cu
)
13215 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13216 struct gdbarch
*gdbarch
= objfile
->arch ();
13217 struct attribute
*attr
;
13218 struct attribute
*attr_high
;
13220 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13223 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13224 if (attr
!= nullptr)
13226 CORE_ADDR low
= attr
->as_address ();
13227 CORE_ADDR high
= attr_high
->as_address ();
13229 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13232 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
+ baseaddr
);
13233 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
+ baseaddr
);
13234 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
13238 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13239 if (attr
!= nullptr && attr
->form_is_unsigned ())
13241 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13242 on DWARF version). */
13243 ULONGEST ranges_offset
= attr
->as_unsigned ();
13245 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13247 if (die
->tag
!= DW_TAG_compile_unit
)
13248 ranges_offset
+= cu
->gnu_ranges_base
;
13250 std::vector
<blockrange
> blockvec
;
13251 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
13252 [&] (CORE_ADDR start
, CORE_ADDR end
)
13256 start
= gdbarch_adjust_dwarf2_addr (gdbarch
, start
);
13257 end
= gdbarch_adjust_dwarf2_addr (gdbarch
, end
);
13258 cu
->get_builder ()->record_block_range (block
, start
, end
- 1);
13259 blockvec
.emplace_back (start
, end
);
13262 BLOCK_RANGES(block
) = make_blockranges (objfile
, blockvec
);
13266 /* Check whether the producer field indicates either of GCC < 4.6, or the
13267 Intel C/C++ compiler, and cache the result in CU. */
13270 check_producer (struct dwarf2_cu
*cu
)
13274 if (cu
->producer
== NULL
)
13276 /* For unknown compilers expect their behavior is DWARF version
13279 GCC started to support .debug_types sections by -gdwarf-4 since
13280 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13281 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13282 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13283 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13285 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
13287 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
13288 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
13289 cu
->producer_is_gcc_11
= major
== 11;
13291 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
13293 cu
->producer_is_icc
= true;
13294 cu
->producer_is_icc_lt_14
= major
< 14;
13296 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
13297 cu
->producer_is_codewarrior
= true;
13300 /* For other non-GCC compilers, expect their behavior is DWARF version
13304 cu
->checked_producer
= true;
13307 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13308 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13309 during 4.6.0 experimental. */
13312 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
13314 if (!cu
->checked_producer
)
13315 check_producer (cu
);
13317 return cu
->producer_is_gxx_lt_4_6
;
13321 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13322 with incorrect is_stmt attributes. */
13325 producer_is_codewarrior (struct dwarf2_cu
*cu
)
13327 if (!cu
->checked_producer
)
13328 check_producer (cu
);
13330 return cu
->producer_is_codewarrior
;
13333 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13334 If that attribute is not available, return the appropriate
13337 static enum dwarf_access_attribute
13338 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
13340 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
13341 if (attr
!= nullptr)
13343 LONGEST value
= attr
->constant_value (-1);
13344 if (value
== DW_ACCESS_public
13345 || value
== DW_ACCESS_protected
13346 || value
== DW_ACCESS_private
)
13347 return (dwarf_access_attribute
) value
;
13348 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13352 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
13354 /* The default DWARF 2 accessibility for members is public, the default
13355 accessibility for inheritance is private. */
13357 if (die
->tag
!= DW_TAG_inheritance
)
13358 return DW_ACCESS_public
;
13360 return DW_ACCESS_private
;
13364 /* DWARF 3+ defines the default accessibility a different way. The same
13365 rules apply now for DW_TAG_inheritance as for the members and it only
13366 depends on the container kind. */
13368 if (die
->parent
->tag
== DW_TAG_class_type
)
13369 return DW_ACCESS_private
;
13371 return DW_ACCESS_public
;
13375 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13376 *OFFSET to the byte offset. If the attribute was not found return
13377 0, otherwise return 1. If it was found but could not properly be
13378 handled, set *OFFSET to 0. */
13381 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13384 struct attribute
*attr
;
13386 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13391 /* Note that we do not check for a section offset first here.
13392 This is because DW_AT_data_member_location is new in DWARF 4,
13393 so if we see it, we can assume that a constant form is really
13394 a constant and not a section offset. */
13395 if (attr
->form_is_constant ())
13396 *offset
= attr
->constant_value (0);
13397 else if (attr
->form_is_section_offset ())
13398 dwarf2_complex_location_expr_complaint ();
13399 else if (attr
->form_is_block ())
13400 *offset
= decode_locdesc (attr
->as_block (), cu
);
13402 dwarf2_complex_location_expr_complaint ();
13408 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13409 if (attr
!= nullptr)
13411 *offset
= attr
->constant_value (0);
13419 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13420 store the results in FIELD. */
13423 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13424 struct field
*field
)
13426 struct attribute
*attr
;
13428 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13431 if (attr
->form_is_constant ())
13433 LONGEST offset
= attr
->constant_value (0);
13435 /* Work around this GCC 11 bug, where it would erroneously use -1
13436 data member locations, instead of 0:
13438 Negative DW_AT_data_member_location
13439 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13441 if (offset
== -1 && cu
->producer_is_gcc_11
)
13443 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13447 field
->set_loc_bitpos (offset
* bits_per_byte
);
13449 else if (attr
->form_is_section_offset ())
13450 dwarf2_complex_location_expr_complaint ();
13451 else if (attr
->form_is_block ())
13454 CORE_ADDR offset
= decode_locdesc (attr
->as_block (), cu
, &handled
);
13456 field
->set_loc_bitpos (offset
* bits_per_byte
);
13459 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13460 struct objfile
*objfile
= per_objfile
->objfile
;
13461 struct dwarf2_locexpr_baton
*dlbaton
13462 = XOBNEW (&objfile
->objfile_obstack
,
13463 struct dwarf2_locexpr_baton
);
13464 dlbaton
->data
= attr
->as_block ()->data
;
13465 dlbaton
->size
= attr
->as_block ()->size
;
13466 /* When using this baton, we want to compute the address
13467 of the field, not the value. This is why
13468 is_reference is set to false here. */
13469 dlbaton
->is_reference
= false;
13470 dlbaton
->per_objfile
= per_objfile
;
13471 dlbaton
->per_cu
= cu
->per_cu
;
13473 field
->set_loc_dwarf_block (dlbaton
);
13477 dwarf2_complex_location_expr_complaint ();
13481 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13482 if (attr
!= nullptr)
13483 field
->set_loc_bitpos (attr
->constant_value (0));
13487 /* Add an aggregate field to the field list. */
13490 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
13491 struct dwarf2_cu
*cu
)
13493 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13494 struct gdbarch
*gdbarch
= objfile
->arch ();
13495 struct nextfield
*new_field
;
13496 struct attribute
*attr
;
13498 const char *fieldname
= "";
13500 if (die
->tag
== DW_TAG_inheritance
)
13502 fip
->baseclasses
.emplace_back ();
13503 new_field
= &fip
->baseclasses
.back ();
13507 fip
->fields
.emplace_back ();
13508 new_field
= &fip
->fields
.back ();
13511 new_field
->offset
= die
->sect_off
;
13513 new_field
->accessibility
= dwarf2_access_attribute (die
, cu
);
13514 if (new_field
->accessibility
!= DW_ACCESS_public
)
13515 fip
->non_public_fields
= true;
13517 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
13518 if (attr
!= nullptr)
13519 new_field
->virtuality
= attr
->as_virtuality ();
13521 new_field
->virtuality
= DW_VIRTUALITY_none
;
13523 fp
= &new_field
->field
;
13525 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
13526 && !die_is_declaration (die
, cu
))
13528 if (die
->tag
== DW_TAG_namelist_item
)
13530 /* Typically, DW_TAG_namelist_item are references to namelist items.
13531 If so, follow that reference. */
13532 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
13533 struct die_info
*item_die
= nullptr;
13534 struct dwarf2_cu
*item_cu
= cu
;
13535 if (attr1
->form_is_ref ())
13536 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
13537 if (item_die
!= nullptr)
13540 /* Data member other than a C++ static data member. */
13542 /* Get type of field. */
13543 fp
->set_type (die_type (die
, cu
));
13545 fp
->set_loc_bitpos (0);
13547 /* Get bit size of field (zero if none). */
13548 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
13549 if (attr
!= nullptr)
13551 FIELD_BITSIZE (*fp
) = attr
->constant_value (0);
13555 FIELD_BITSIZE (*fp
) = 0;
13558 /* Get bit offset of field. */
13559 handle_member_location (die
, cu
, fp
);
13560 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
13561 if (attr
!= nullptr && attr
->form_is_constant ())
13563 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
13565 /* For big endian bits, the DW_AT_bit_offset gives the
13566 additional bit offset from the MSB of the containing
13567 anonymous object to the MSB of the field. We don't
13568 have to do anything special since we don't need to
13569 know the size of the anonymous object. */
13570 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
13574 /* For little endian bits, compute the bit offset to the
13575 MSB of the anonymous object, subtract off the number of
13576 bits from the MSB of the field to the MSB of the
13577 object, and then subtract off the number of bits of
13578 the field itself. The result is the bit offset of
13579 the LSB of the field. */
13580 int anonymous_size
;
13581 int bit_offset
= attr
->constant_value (0);
13583 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13584 if (attr
!= nullptr && attr
->form_is_constant ())
13586 /* The size of the anonymous object containing
13587 the bit field is explicit, so use the
13588 indicated size (in bytes). */
13589 anonymous_size
= attr
->constant_value (0);
13593 /* The size of the anonymous object containing
13594 the bit field must be inferred from the type
13595 attribute of the data member containing the
13597 anonymous_size
= TYPE_LENGTH (fp
->type ());
13599 fp
->set_loc_bitpos (fp
->loc_bitpos ()
13600 + anonymous_size
* bits_per_byte
13601 - bit_offset
- FIELD_BITSIZE (*fp
));
13605 /* Get name of field. */
13606 fieldname
= dwarf2_name (die
, cu
);
13607 if (fieldname
== NULL
)
13610 /* The name is already allocated along with this objfile, so we don't
13611 need to duplicate it for the type. */
13612 fp
->set_name (fieldname
);
13614 /* Change accessibility for artificial fields (e.g. virtual table
13615 pointer or virtual base class pointer) to private. */
13616 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
13618 FIELD_ARTIFICIAL (*fp
) = 1;
13619 new_field
->accessibility
= DW_ACCESS_private
;
13620 fip
->non_public_fields
= true;
13623 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
13625 /* C++ static member. */
13627 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13628 is a declaration, but all versions of G++ as of this writing
13629 (so through at least 3.2.1) incorrectly generate
13630 DW_TAG_variable tags. */
13632 const char *physname
;
13634 /* Get name of field. */
13635 fieldname
= dwarf2_name (die
, cu
);
13636 if (fieldname
== NULL
)
13639 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
13641 /* Only create a symbol if this is an external value.
13642 new_symbol checks this and puts the value in the global symbol
13643 table, which we want. If it is not external, new_symbol
13644 will try to put the value in cu->list_in_scope which is wrong. */
13645 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
13647 /* A static const member, not much different than an enum as far as
13648 we're concerned, except that we can support more types. */
13649 new_symbol (die
, NULL
, cu
);
13652 /* Get physical name. */
13653 physname
= dwarf2_physname (fieldname
, die
, cu
);
13655 /* The name is already allocated along with this objfile, so we don't
13656 need to duplicate it for the type. */
13657 fp
->set_loc_physname (physname
? physname
: "");
13658 fp
->set_type (die_type (die
, cu
));
13659 fp
->set_name (fieldname
);
13661 else if (die
->tag
== DW_TAG_inheritance
)
13663 /* C++ base class field. */
13664 handle_member_location (die
, cu
, fp
);
13665 FIELD_BITSIZE (*fp
) = 0;
13666 fp
->set_type (die_type (die
, cu
));
13667 fp
->set_name (fp
->type ()->name ());
13670 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13673 /* Can the type given by DIE define another type? */
13676 type_can_define_types (const struct die_info
*die
)
13680 case DW_TAG_typedef
:
13681 case DW_TAG_class_type
:
13682 case DW_TAG_structure_type
:
13683 case DW_TAG_union_type
:
13684 case DW_TAG_enumeration_type
:
13692 /* Add a type definition defined in the scope of the FIP's class. */
13695 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
13696 struct dwarf2_cu
*cu
)
13698 struct decl_field fp
;
13699 memset (&fp
, 0, sizeof (fp
));
13701 gdb_assert (type_can_define_types (die
));
13703 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13704 fp
.name
= dwarf2_name (die
, cu
);
13705 fp
.type
= read_type_die (die
, cu
);
13707 /* Save accessibility. */
13708 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
13709 switch (accessibility
)
13711 case DW_ACCESS_public
:
13712 /* The assumed value if neither private nor protected. */
13714 case DW_ACCESS_private
:
13717 case DW_ACCESS_protected
:
13718 fp
.is_protected
= 1;
13722 if (die
->tag
== DW_TAG_typedef
)
13723 fip
->typedef_field_list
.push_back (fp
);
13725 fip
->nested_types_list
.push_back (fp
);
13728 /* A convenience typedef that's used when finding the discriminant
13729 field for a variant part. */
13730 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
13733 /* Compute the discriminant range for a given variant. OBSTACK is
13734 where the results will be stored. VARIANT is the variant to
13735 process. IS_UNSIGNED indicates whether the discriminant is signed
13738 static const gdb::array_view
<discriminant_range
>
13739 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
13742 std::vector
<discriminant_range
> ranges
;
13744 if (variant
.default_branch
)
13747 if (variant
.discr_list_data
== nullptr)
13749 discriminant_range r
13750 = {variant
.discriminant_value
, variant
.discriminant_value
};
13751 ranges
.push_back (r
);
13755 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
13756 variant
.discr_list_data
->size
);
13757 while (!data
.empty ())
13759 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
13761 complaint (_("invalid discriminant marker: %d"), data
[0]);
13764 bool is_range
= data
[0] == DW_DSC_range
;
13765 data
= data
.slice (1);
13767 ULONGEST low
, high
;
13768 unsigned int bytes_read
;
13772 complaint (_("DW_AT_discr_list missing low value"));
13776 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
13778 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
13780 data
= data
.slice (bytes_read
);
13786 complaint (_("DW_AT_discr_list missing high value"));
13790 high
= read_unsigned_leb128 (nullptr, data
.data (),
13793 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
13795 data
= data
.slice (bytes_read
);
13800 ranges
.push_back ({ low
, high
});
13804 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
13806 std::copy (ranges
.begin (), ranges
.end (), result
);
13807 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
13810 static const gdb::array_view
<variant_part
> create_variant_parts
13811 (struct obstack
*obstack
,
13812 const offset_map_type
&offset_map
,
13813 struct field_info
*fi
,
13814 const std::vector
<variant_part_builder
> &variant_parts
);
13816 /* Fill in a "struct variant" for a given variant field. RESULT is
13817 the variant to fill in. OBSTACK is where any needed allocations
13818 will be done. OFFSET_MAP holds the mapping from section offsets to
13819 fields for the type. FI describes the fields of the type we're
13820 processing. FIELD is the variant field we're converting. */
13823 create_one_variant (variant
&result
, struct obstack
*obstack
,
13824 const offset_map_type
&offset_map
,
13825 struct field_info
*fi
, const variant_field
&field
)
13827 result
.discriminants
= convert_variant_range (obstack
, field
, false);
13828 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
13829 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
13830 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
13831 field
.variant_parts
);
13834 /* Fill in a "struct variant_part" for a given variant part. RESULT
13835 is the variant part to fill in. OBSTACK is where any needed
13836 allocations will be done. OFFSET_MAP holds the mapping from
13837 section offsets to fields for the type. FI describes the fields of
13838 the type we're processing. BUILDER is the variant part to be
13842 create_one_variant_part (variant_part
&result
,
13843 struct obstack
*obstack
,
13844 const offset_map_type
&offset_map
,
13845 struct field_info
*fi
,
13846 const variant_part_builder
&builder
)
13848 auto iter
= offset_map
.find (builder
.discriminant_offset
);
13849 if (iter
== offset_map
.end ())
13851 result
.discriminant_index
= -1;
13852 /* Doesn't matter. */
13853 result
.is_unsigned
= false;
13857 result
.discriminant_index
= iter
->second
;
13859 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
13862 size_t n
= builder
.variants
.size ();
13863 variant
*output
= new (obstack
) variant
[n
];
13864 for (size_t i
= 0; i
< n
; ++i
)
13865 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
13866 builder
.variants
[i
]);
13868 result
.variants
= gdb::array_view
<variant
> (output
, n
);
13871 /* Create a vector of variant parts that can be attached to a type.
13872 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13873 holds the mapping from section offsets to fields for the type. FI
13874 describes the fields of the type we're processing. VARIANT_PARTS
13875 is the vector to convert. */
13877 static const gdb::array_view
<variant_part
>
13878 create_variant_parts (struct obstack
*obstack
,
13879 const offset_map_type
&offset_map
,
13880 struct field_info
*fi
,
13881 const std::vector
<variant_part_builder
> &variant_parts
)
13883 if (variant_parts
.empty ())
13886 size_t n
= variant_parts
.size ();
13887 variant_part
*result
= new (obstack
) variant_part
[n
];
13888 for (size_t i
= 0; i
< n
; ++i
)
13889 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
13892 return gdb::array_view
<variant_part
> (result
, n
);
13895 /* Compute the variant part vector for FIP, attaching it to TYPE when
13899 add_variant_property (struct field_info
*fip
, struct type
*type
,
13900 struct dwarf2_cu
*cu
)
13902 /* Map section offsets of fields to their field index. Note the
13903 field index here does not take the number of baseclasses into
13905 offset_map_type offset_map
;
13906 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
13907 offset_map
[fip
->fields
[i
].offset
] = i
;
13909 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13910 gdb::array_view
<const variant_part
> parts
13911 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
13912 fip
->variant_parts
);
13914 struct dynamic_prop prop
;
13915 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
13916 obstack_copy (&objfile
->objfile_obstack
, &parts
,
13919 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
13922 /* Create the vector of fields, and attach it to the type. */
13925 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
13926 struct dwarf2_cu
*cu
)
13928 int nfields
= fip
->nfields ();
13930 /* Record the field count, allocate space for the array of fields,
13931 and create blank accessibility bitfields if necessary. */
13932 type
->set_num_fields (nfields
);
13934 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
13936 if (fip
->non_public_fields
&& cu
->per_cu
->lang
!= language_ada
)
13938 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13940 TYPE_FIELD_PRIVATE_BITS (type
) =
13941 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13942 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type
), nfields
);
13944 TYPE_FIELD_PROTECTED_BITS (type
) =
13945 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13946 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type
), nfields
);
13948 TYPE_FIELD_IGNORE_BITS (type
) =
13949 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13950 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type
), nfields
);
13953 /* If the type has baseclasses, allocate and clear a bit vector for
13954 TYPE_FIELD_VIRTUAL_BITS. */
13955 if (!fip
->baseclasses
.empty () && cu
->per_cu
->lang
!= language_ada
)
13957 int num_bytes
= B_BYTES (fip
->baseclasses
.size ());
13958 unsigned char *pointer
;
13960 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13961 pointer
= (unsigned char *) TYPE_ALLOC (type
, num_bytes
);
13962 TYPE_FIELD_VIRTUAL_BITS (type
) = pointer
;
13963 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type
), fip
->baseclasses
.size ());
13964 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
13967 if (!fip
->variant_parts
.empty ())
13968 add_variant_property (fip
, type
, cu
);
13970 /* Copy the saved-up fields into the field vector. */
13971 for (int i
= 0; i
< nfields
; ++i
)
13973 struct nextfield
&field
13974 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
13975 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
13977 type
->field (i
) = field
.field
;
13978 switch (field
.accessibility
)
13980 case DW_ACCESS_private
:
13981 if (cu
->per_cu
->lang
!= language_ada
)
13982 SET_TYPE_FIELD_PRIVATE (type
, i
);
13985 case DW_ACCESS_protected
:
13986 if (cu
->per_cu
->lang
!= language_ada
)
13987 SET_TYPE_FIELD_PROTECTED (type
, i
);
13990 case DW_ACCESS_public
:
13994 /* Unknown accessibility. Complain and treat it as public. */
13996 complaint (_("unsupported accessibility %d"),
13997 field
.accessibility
);
14001 if (i
< fip
->baseclasses
.size ())
14003 switch (field
.virtuality
)
14005 case DW_VIRTUALITY_virtual
:
14006 case DW_VIRTUALITY_pure_virtual
:
14007 if (cu
->per_cu
->lang
== language_ada
)
14008 error (_("unexpected virtuality in component of Ada type"));
14009 SET_TYPE_FIELD_VIRTUAL (type
, i
);
14016 /* Return true if this member function is a constructor, false
14020 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
14022 const char *fieldname
;
14023 const char *type_name
;
14026 if (die
->parent
== NULL
)
14029 if (die
->parent
->tag
!= DW_TAG_structure_type
14030 && die
->parent
->tag
!= DW_TAG_union_type
14031 && die
->parent
->tag
!= DW_TAG_class_type
)
14034 fieldname
= dwarf2_name (die
, cu
);
14035 type_name
= dwarf2_name (die
->parent
, cu
);
14036 if (fieldname
== NULL
|| type_name
== NULL
)
14039 len
= strlen (fieldname
);
14040 return (strncmp (fieldname
, type_name
, len
) == 0
14041 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
14044 /* Add a member function to the proper fieldlist. */
14047 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
14048 struct type
*type
, struct dwarf2_cu
*cu
)
14050 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14051 struct attribute
*attr
;
14053 struct fnfieldlist
*flp
= nullptr;
14054 struct fn_field
*fnp
;
14055 const char *fieldname
;
14056 struct type
*this_type
;
14058 if (cu
->per_cu
->lang
== language_ada
)
14059 error (_("unexpected member function in Ada type"));
14061 /* Get name of member function. */
14062 fieldname
= dwarf2_name (die
, cu
);
14063 if (fieldname
== NULL
)
14066 /* Look up member function name in fieldlist. */
14067 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14069 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
14071 flp
= &fip
->fnfieldlists
[i
];
14076 /* Create a new fnfieldlist if necessary. */
14077 if (flp
== nullptr)
14079 fip
->fnfieldlists
.emplace_back ();
14080 flp
= &fip
->fnfieldlists
.back ();
14081 flp
->name
= fieldname
;
14082 i
= fip
->fnfieldlists
.size () - 1;
14085 /* Create a new member function field and add it to the vector of
14087 flp
->fnfields
.emplace_back ();
14088 fnp
= &flp
->fnfields
.back ();
14090 /* Delay processing of the physname until later. */
14091 if (cu
->per_cu
->lang
== language_cplus
)
14092 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
14096 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
14097 fnp
->physname
= physname
? physname
: "";
14100 fnp
->type
= alloc_type (objfile
);
14101 this_type
= read_type_die (die
, cu
);
14102 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
14104 int nparams
= this_type
->num_fields ();
14106 /* TYPE is the domain of this method, and THIS_TYPE is the type
14107 of the method itself (TYPE_CODE_METHOD). */
14108 smash_to_method_type (fnp
->type
, type
,
14109 TYPE_TARGET_TYPE (this_type
),
14110 this_type
->fields (),
14111 this_type
->num_fields (),
14112 this_type
->has_varargs ());
14114 /* Handle static member functions.
14115 Dwarf2 has no clean way to discern C++ static and non-static
14116 member functions. G++ helps GDB by marking the first
14117 parameter for non-static member functions (which is the this
14118 pointer) as artificial. We obtain this information from
14119 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14120 if (nparams
== 0 || TYPE_FIELD_ARTIFICIAL (this_type
, 0) == 0)
14121 fnp
->voffset
= VOFFSET_STATIC
;
14124 complaint (_("member function type missing for '%s'"),
14125 dwarf2_full_name (fieldname
, die
, cu
));
14127 /* Get fcontext from DW_AT_containing_type if present. */
14128 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14129 fnp
->fcontext
= die_containing_type (die
, cu
);
14131 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14132 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14134 /* Get accessibility. */
14135 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
14136 switch (accessibility
)
14138 case DW_ACCESS_private
:
14139 fnp
->is_private
= 1;
14141 case DW_ACCESS_protected
:
14142 fnp
->is_protected
= 1;
14146 /* Check for artificial methods. */
14147 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
14148 if (attr
&& attr
->as_boolean ())
14149 fnp
->is_artificial
= 1;
14151 /* Check for defaulted methods. */
14152 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
14153 if (attr
!= nullptr)
14154 fnp
->defaulted
= attr
->defaulted ();
14156 /* Check for deleted methods. */
14157 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
14158 if (attr
!= nullptr && attr
->as_boolean ())
14159 fnp
->is_deleted
= 1;
14161 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
14163 /* Get index in virtual function table if it is a virtual member
14164 function. For older versions of GCC, this is an offset in the
14165 appropriate virtual table, as specified by DW_AT_containing_type.
14166 For everyone else, it is an expression to be evaluated relative
14167 to the object address. */
14169 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
14170 if (attr
!= nullptr)
14172 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
14174 struct dwarf_block
*block
= attr
->as_block ();
14176 if (block
->data
[0] == DW_OP_constu
)
14178 /* Old-style GCC. */
14179 fnp
->voffset
= decode_locdesc (block
, cu
) + 2;
14181 else if (block
->data
[0] == DW_OP_deref
14182 || (block
->size
> 1
14183 && block
->data
[0] == DW_OP_deref_size
14184 && block
->data
[1] == cu
->header
.addr_size
))
14186 fnp
->voffset
= decode_locdesc (block
, cu
);
14187 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
14188 dwarf2_complex_location_expr_complaint ();
14190 fnp
->voffset
/= cu
->header
.addr_size
;
14194 dwarf2_complex_location_expr_complaint ();
14196 if (!fnp
->fcontext
)
14198 /* If there is no `this' field and no DW_AT_containing_type,
14199 we cannot actually find a base class context for the
14201 if (this_type
->num_fields () == 0
14202 || !TYPE_FIELD_ARTIFICIAL (this_type
, 0))
14204 complaint (_("cannot determine context for virtual member "
14205 "function \"%s\" (offset %s)"),
14206 fieldname
, sect_offset_str (die
->sect_off
));
14211 = TYPE_TARGET_TYPE (this_type
->field (0).type ());
14215 else if (attr
->form_is_section_offset ())
14217 dwarf2_complex_location_expr_complaint ();
14221 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14227 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
14228 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
14230 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14231 complaint (_("Member function \"%s\" (offset %s) is virtual "
14232 "but the vtable offset is not specified"),
14233 fieldname
, sect_offset_str (die
->sect_off
));
14234 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14235 TYPE_CPLUS_DYNAMIC (type
) = 1;
14240 /* Create the vector of member function fields, and attach it to the type. */
14243 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
14244 struct dwarf2_cu
*cu
)
14246 if (cu
->per_cu
->lang
== language_ada
)
14247 error (_("unexpected member functions in Ada type"));
14249 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14250 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
14252 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
14254 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14256 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
14257 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
14259 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
14260 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
14261 fn_flp
->fn_fields
= (struct fn_field
*)
14262 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
14264 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
14265 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
14268 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
14271 /* Returns non-zero if NAME is the name of a vtable member in CU's
14272 language, zero otherwise. */
14274 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
14276 static const char vptr
[] = "_vptr";
14278 /* Look for the C++ form of the vtable. */
14279 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
14285 /* GCC outputs unnamed structures that are really pointers to member
14286 functions, with the ABI-specified layout. If TYPE describes
14287 such a structure, smash it into a member function type.
14289 GCC shouldn't do this; it should just output pointer to member DIEs.
14290 This is GCC PR debug/28767. */
14293 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
14295 struct type
*pfn_type
, *self_type
, *new_type
;
14297 /* Check for a structure with no name and two children. */
14298 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14301 /* Check for __pfn and __delta members. */
14302 if (type
->field (0).name () == NULL
14303 || strcmp (type
->field (0).name (), "__pfn") != 0
14304 || type
->field (1).name () == NULL
14305 || strcmp (type
->field (1).name (), "__delta") != 0)
14308 /* Find the type of the method. */
14309 pfn_type
= type
->field (0).type ();
14310 if (pfn_type
== NULL
14311 || pfn_type
->code () != TYPE_CODE_PTR
14312 || TYPE_TARGET_TYPE (pfn_type
)->code () != TYPE_CODE_FUNC
)
14315 /* Look for the "this" argument. */
14316 pfn_type
= TYPE_TARGET_TYPE (pfn_type
);
14317 if (pfn_type
->num_fields () == 0
14318 /* || pfn_type->field (0).type () == NULL */
14319 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14322 self_type
= TYPE_TARGET_TYPE (pfn_type
->field (0).type ());
14323 new_type
= alloc_type (objfile
);
14324 smash_to_method_type (new_type
, self_type
, TYPE_TARGET_TYPE (pfn_type
),
14325 pfn_type
->fields (), pfn_type
->num_fields (),
14326 pfn_type
->has_varargs ());
14327 smash_to_methodptr_type (type
, new_type
);
14330 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14331 requires rewriting, then copy it and return the updated copy.
14332 Otherwise return nullptr. */
14334 static struct type
*
14335 rewrite_array_type (struct type
*type
)
14337 if (type
->code () != TYPE_CODE_ARRAY
)
14340 struct type
*index_type
= type
->index_type ();
14341 range_bounds
*current_bounds
= index_type
->bounds ();
14343 /* Handle multi-dimensional arrays. */
14344 struct type
*new_target
= rewrite_array_type (TYPE_TARGET_TYPE (type
));
14345 if (new_target
== nullptr)
14347 /* Maybe we don't need to rewrite this array. */
14348 if (current_bounds
->low
.kind () == PROP_CONST
14349 && current_bounds
->high
.kind () == PROP_CONST
)
14353 /* Either the target type was rewritten, or the bounds have to be
14354 updated. Either way we want to copy the type and update
14356 struct type
*copy
= copy_type (type
);
14357 int nfields
= copy
->num_fields ();
14359 = ((struct field
*) TYPE_ZALLOC (copy
,
14360 nfields
* sizeof (struct field
)));
14361 memcpy (new_fields
, copy
->fields (), nfields
* sizeof (struct field
));
14362 copy
->set_fields (new_fields
);
14363 if (new_target
!= nullptr)
14364 TYPE_TARGET_TYPE (copy
) = new_target
;
14366 struct type
*index_copy
= copy_type (index_type
);
14367 range_bounds
*bounds
14368 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
14369 sizeof (range_bounds
));
14370 *bounds
= *current_bounds
;
14371 bounds
->low
.set_const_val (1);
14372 bounds
->high
.set_const_val (0);
14373 index_copy
->set_bounds (bounds
);
14374 copy
->set_index_type (index_copy
);
14379 /* While some versions of GCC will generate complicated DWARF for an
14380 array (see quirk_ada_thick_pointer), more recent versions were
14381 modified to emit an explicit thick pointer structure. However, in
14382 this case, the array still has DWARF expressions for its ranges,
14383 and these must be ignored. */
14386 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
14389 gdb_assert (cu
->per_cu
->lang
== language_ada
);
14391 /* Check for a structure with two children. */
14392 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14395 /* Check for P_ARRAY and P_BOUNDS members. */
14396 if (type
->field (0).name () == NULL
14397 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
14398 || type
->field (1).name () == NULL
14399 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
14402 /* Make sure we're looking at a pointer to an array. */
14403 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14406 /* The Ada code already knows how to handle these types, so all that
14407 we need to do is turn the bounds into static bounds. However, we
14408 don't want to rewrite existing array or index types in-place,
14409 because those may be referenced in other contexts where this
14410 rewriting is undesirable. */
14411 struct type
*new_ary_type
14412 = rewrite_array_type (TYPE_TARGET_TYPE (type
->field (0).type ()));
14413 if (new_ary_type
!= nullptr)
14414 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
14417 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14418 appropriate error checking and issuing complaints if there is a
14422 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
14424 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
14426 if (attr
== nullptr)
14429 if (!attr
->form_is_constant ())
14431 complaint (_("DW_AT_alignment must have constant form"
14432 " - DIE at %s [in module %s]"),
14433 sect_offset_str (die
->sect_off
),
14434 objfile_name (cu
->per_objfile
->objfile
));
14438 LONGEST val
= attr
->constant_value (0);
14441 complaint (_("DW_AT_alignment value must not be negative"
14442 " - DIE at %s [in module %s]"),
14443 sect_offset_str (die
->sect_off
),
14444 objfile_name (cu
->per_objfile
->objfile
));
14447 ULONGEST align
= val
;
14451 complaint (_("DW_AT_alignment value must not be zero"
14452 " - DIE at %s [in module %s]"),
14453 sect_offset_str (die
->sect_off
),
14454 objfile_name (cu
->per_objfile
->objfile
));
14457 if ((align
& (align
- 1)) != 0)
14459 complaint (_("DW_AT_alignment value must be a power of 2"
14460 " - DIE at %s [in module %s]"),
14461 sect_offset_str (die
->sect_off
),
14462 objfile_name (cu
->per_objfile
->objfile
));
14469 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14470 the alignment for TYPE. */
14473 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
14476 if (!set_type_align (type
, get_alignment (cu
, die
)))
14477 complaint (_("DW_AT_alignment value too large"
14478 " - DIE at %s [in module %s]"),
14479 sect_offset_str (die
->sect_off
),
14480 objfile_name (cu
->per_objfile
->objfile
));
14483 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14484 constant for a type, according to DWARF5 spec, Table 5.5. */
14487 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
14492 case DW_CC_pass_by_reference
:
14493 case DW_CC_pass_by_value
:
14497 complaint (_("unrecognized DW_AT_calling_convention value "
14498 "(%s) for a type"), pulongest (value
));
14503 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14504 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14505 also according to GNU-specific values (see include/dwarf2.h). */
14508 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
14513 case DW_CC_program
:
14517 case DW_CC_GNU_renesas_sh
:
14518 case DW_CC_GNU_borland_fastcall_i386
:
14519 case DW_CC_GDB_IBM_OpenCL
:
14523 complaint (_("unrecognized DW_AT_calling_convention value "
14524 "(%s) for a subroutine"), pulongest (value
));
14529 /* Called when we find the DIE that starts a structure or union scope
14530 (definition) to create a type for the structure or union. Fill in
14531 the type's name and general properties; the members will not be
14532 processed until process_structure_scope. A symbol table entry for
14533 the type will also not be done until process_structure_scope (assuming
14534 the type has a name).
14536 NOTE: we need to call these functions regardless of whether or not the
14537 DIE has a DW_AT_name attribute, since it might be an anonymous
14538 structure or union. This gets the type entered into our set of
14539 user defined types. */
14541 static struct type
*
14542 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14544 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14546 struct attribute
*attr
;
14549 /* If the definition of this type lives in .debug_types, read that type.
14550 Don't follow DW_AT_specification though, that will take us back up
14551 the chain and we want to go down. */
14552 attr
= die
->attr (DW_AT_signature
);
14553 if (attr
!= nullptr)
14555 type
= get_DW_AT_signature_type (die
, attr
, cu
);
14557 /* The type's CU may not be the same as CU.
14558 Ensure TYPE is recorded with CU in die_type_hash. */
14559 return set_die_type (die
, type
, cu
);
14562 type
= alloc_type (objfile
);
14563 INIT_CPLUS_SPECIFIC (type
);
14565 name
= dwarf2_name (die
, cu
);
14568 if (cu
->per_cu
->lang
== language_cplus
14569 || cu
->per_cu
->lang
== language_d
14570 || cu
->per_cu
->lang
== language_rust
)
14572 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
14574 /* dwarf2_full_name might have already finished building the DIE's
14575 type. If so, there is no need to continue. */
14576 if (get_die_type (die
, cu
) != NULL
)
14577 return get_die_type (die
, cu
);
14579 type
->set_name (full_name
);
14583 /* The name is already allocated along with this objfile, so
14584 we don't need to duplicate it for the type. */
14585 type
->set_name (name
);
14589 if (die
->tag
== DW_TAG_structure_type
)
14591 type
->set_code (TYPE_CODE_STRUCT
);
14593 else if (die
->tag
== DW_TAG_union_type
)
14595 type
->set_code (TYPE_CODE_UNION
);
14597 else if (die
->tag
== DW_TAG_namelist
)
14599 type
->set_code (TYPE_CODE_NAMELIST
);
14603 type
->set_code (TYPE_CODE_STRUCT
);
14606 if (cu
->per_cu
->lang
== language_cplus
&& die
->tag
== DW_TAG_class_type
)
14607 type
->set_is_declared_class (true);
14609 /* Store the calling convention in the type if it's available in
14610 the die. Otherwise the calling convention remains set to
14611 the default value DW_CC_normal. */
14612 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14613 if (attr
!= nullptr
14614 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
14616 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14617 TYPE_CPLUS_CALLING_CONVENTION (type
)
14618 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
14621 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14622 if (attr
!= nullptr)
14624 if (attr
->form_is_constant ())
14625 TYPE_LENGTH (type
) = attr
->constant_value (0);
14628 struct dynamic_prop prop
;
14629 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
14630 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
14631 TYPE_LENGTH (type
) = 0;
14636 TYPE_LENGTH (type
) = 0;
14639 maybe_set_alignment (cu
, die
, type
);
14641 if (producer_is_icc_lt_14 (cu
) && (TYPE_LENGTH (type
) == 0))
14643 /* ICC<14 does not output the required DW_AT_declaration on
14644 incomplete types, but gives them a size of zero. */
14645 type
->set_is_stub (true);
14648 type
->set_stub_is_supported (true);
14650 if (die_is_declaration (die
, cu
))
14651 type
->set_is_stub (true);
14652 else if (attr
== NULL
&& die
->child
== NULL
14653 && producer_is_realview (cu
->producer
))
14654 /* RealView does not output the required DW_AT_declaration
14655 on incomplete types. */
14656 type
->set_is_stub (true);
14658 /* We need to add the type field to the die immediately so we don't
14659 infinitely recurse when dealing with pointers to the structure
14660 type within the structure itself. */
14661 set_die_type (die
, type
, cu
);
14663 /* set_die_type should be already done. */
14664 set_descriptive_type (type
, die
, cu
);
14669 static void handle_struct_member_die
14670 (struct die_info
*child_die
,
14672 struct field_info
*fi
,
14673 std::vector
<struct symbol
*> *template_args
,
14674 struct dwarf2_cu
*cu
);
14676 /* A helper for handle_struct_member_die that handles
14677 DW_TAG_variant_part. */
14680 handle_variant_part (struct die_info
*die
, struct type
*type
,
14681 struct field_info
*fi
,
14682 std::vector
<struct symbol
*> *template_args
,
14683 struct dwarf2_cu
*cu
)
14685 variant_part_builder
*new_part
;
14686 if (fi
->current_variant_part
== nullptr)
14688 fi
->variant_parts
.emplace_back ();
14689 new_part
= &fi
->variant_parts
.back ();
14691 else if (!fi
->current_variant_part
->processing_variant
)
14693 complaint (_("nested DW_TAG_variant_part seen "
14694 "- DIE at %s [in module %s]"),
14695 sect_offset_str (die
->sect_off
),
14696 objfile_name (cu
->per_objfile
->objfile
));
14701 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
14702 current
.variant_parts
.emplace_back ();
14703 new_part
= ¤t
.variant_parts
.back ();
14706 /* When we recurse, we want callees to add to this new variant
14708 scoped_restore save_current_variant_part
14709 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
14711 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
14714 /* It's a univariant form, an extension we support. */
14716 else if (discr
->form_is_ref ())
14718 struct dwarf2_cu
*target_cu
= cu
;
14719 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
14721 new_part
->discriminant_offset
= target_die
->sect_off
;
14725 complaint (_("DW_AT_discr does not have DIE reference form"
14726 " - DIE at %s [in module %s]"),
14727 sect_offset_str (die
->sect_off
),
14728 objfile_name (cu
->per_objfile
->objfile
));
14731 for (die_info
*child_die
= die
->child
;
14733 child_die
= child_die
->sibling
)
14734 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
14737 /* A helper for handle_struct_member_die that handles
14741 handle_variant (struct die_info
*die
, struct type
*type
,
14742 struct field_info
*fi
,
14743 std::vector
<struct symbol
*> *template_args
,
14744 struct dwarf2_cu
*cu
)
14746 if (fi
->current_variant_part
== nullptr)
14748 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14749 "- DIE at %s [in module %s]"),
14750 sect_offset_str (die
->sect_off
),
14751 objfile_name (cu
->per_objfile
->objfile
));
14754 if (fi
->current_variant_part
->processing_variant
)
14756 complaint (_("nested DW_TAG_variant seen "
14757 "- DIE at %s [in module %s]"),
14758 sect_offset_str (die
->sect_off
),
14759 objfile_name (cu
->per_objfile
->objfile
));
14763 scoped_restore save_processing_variant
14764 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
14767 fi
->current_variant_part
->variants
.emplace_back ();
14768 variant_field
&variant
= fi
->current_variant_part
->variants
.back ();
14769 variant
.first_field
= fi
->fields
.size ();
14771 /* In a variant we want to get the discriminant and also add a
14772 field for our sole member child. */
14773 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
14774 if (discr
== nullptr || !discr
->form_is_constant ())
14776 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
14777 if (discr
== nullptr || discr
->as_block ()->size
== 0)
14778 variant
.default_branch
= true;
14780 variant
.discr_list_data
= discr
->as_block ();
14783 variant
.discriminant_value
= discr
->constant_value (0);
14785 for (die_info
*variant_child
= die
->child
;
14786 variant_child
!= NULL
;
14787 variant_child
= variant_child
->sibling
)
14788 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
14790 variant
.last_field
= fi
->fields
.size ();
14793 /* A helper for process_structure_scope that handles a single member
14797 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
14798 struct field_info
*fi
,
14799 std::vector
<struct symbol
*> *template_args
,
14800 struct dwarf2_cu
*cu
)
14802 if (child_die
->tag
== DW_TAG_member
14803 || child_die
->tag
== DW_TAG_variable
14804 || child_die
->tag
== DW_TAG_namelist_item
)
14806 /* NOTE: carlton/2002-11-05: A C++ static data member
14807 should be a DW_TAG_member that is a declaration, but
14808 all versions of G++ as of this writing (so through at
14809 least 3.2.1) incorrectly generate DW_TAG_variable
14810 tags for them instead. */
14811 dwarf2_add_field (fi
, child_die
, cu
);
14813 else if (child_die
->tag
== DW_TAG_subprogram
)
14815 /* Rust doesn't have member functions in the C++ sense.
14816 However, it does emit ordinary functions as children
14817 of a struct DIE. */
14818 if (cu
->per_cu
->lang
== language_rust
)
14819 read_func_scope (child_die
, cu
);
14822 /* C++ member function. */
14823 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
14826 else if (child_die
->tag
== DW_TAG_inheritance
)
14828 /* C++ base class field. */
14829 dwarf2_add_field (fi
, child_die
, cu
);
14831 else if (type_can_define_types (child_die
))
14832 dwarf2_add_type_defn (fi
, child_die
, cu
);
14833 else if (child_die
->tag
== DW_TAG_template_type_param
14834 || child_die
->tag
== DW_TAG_template_value_param
)
14836 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
14839 template_args
->push_back (arg
);
14841 else if (child_die
->tag
== DW_TAG_variant_part
)
14842 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
14843 else if (child_die
->tag
== DW_TAG_variant
)
14844 handle_variant (child_die
, type
, fi
, template_args
, cu
);
14847 /* Finish creating a structure or union type, including filling in its
14848 members and creating a symbol for it. This function also handles Fortran
14849 namelist variables, their items or members and creating a symbol for
14853 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
14855 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14856 struct die_info
*child_die
;
14859 type
= get_die_type (die
, cu
);
14861 type
= read_structure_type (die
, cu
);
14863 bool has_template_parameters
= false;
14864 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
14866 struct field_info fi
;
14867 std::vector
<struct symbol
*> template_args
;
14869 child_die
= die
->child
;
14871 while (child_die
&& child_die
->tag
)
14873 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
14874 child_die
= child_die
->sibling
;
14877 /* Attach template arguments to type. */
14878 if (!template_args
.empty ())
14880 has_template_parameters
= true;
14881 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14882 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
14883 TYPE_TEMPLATE_ARGUMENTS (type
)
14884 = XOBNEWVEC (&objfile
->objfile_obstack
,
14886 TYPE_N_TEMPLATE_ARGUMENTS (type
));
14887 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
14888 template_args
.data (),
14889 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
14890 * sizeof (struct symbol
*)));
14893 /* Attach fields and member functions to the type. */
14894 if (fi
.nfields () > 0)
14895 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
14896 if (!fi
.fnfieldlists
.empty ())
14898 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
14900 /* Get the type which refers to the base class (possibly this
14901 class itself) which contains the vtable pointer for the current
14902 class from the DW_AT_containing_type attribute. This use of
14903 DW_AT_containing_type is a GNU extension. */
14905 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14907 struct type
*t
= die_containing_type (die
, cu
);
14909 set_type_vptr_basetype (type
, t
);
14914 /* Our own class provides vtbl ptr. */
14915 for (i
= t
->num_fields () - 1;
14916 i
>= TYPE_N_BASECLASSES (t
);
14919 const char *fieldname
= t
->field (i
).name ();
14921 if (is_vtable_name (fieldname
, cu
))
14923 set_type_vptr_fieldno (type
, i
);
14928 /* Complain if virtual function table field not found. */
14929 if (i
< TYPE_N_BASECLASSES (t
))
14930 complaint (_("virtual function table pointer "
14931 "not found when defining class '%s'"),
14932 type
->name () ? type
->name () : "");
14936 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
14939 else if (cu
->producer
14940 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
14942 /* The IBM XLC compiler does not provide direct indication
14943 of the containing type, but the vtable pointer is
14944 always named __vfp. */
14948 for (i
= type
->num_fields () - 1;
14949 i
>= TYPE_N_BASECLASSES (type
);
14952 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
14954 set_type_vptr_fieldno (type
, i
);
14955 set_type_vptr_basetype (type
, type
);
14962 /* Copy fi.typedef_field_list linked list elements content into the
14963 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14964 if (!fi
.typedef_field_list
.empty ())
14966 int count
= fi
.typedef_field_list
.size ();
14968 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14969 TYPE_TYPEDEF_FIELD_ARRAY (type
)
14970 = ((struct decl_field
*)
14972 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
14973 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
14975 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
14976 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
14979 /* Copy fi.nested_types_list linked list elements content into the
14980 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14981 if (!fi
.nested_types_list
.empty ()
14982 && cu
->per_cu
->lang
!= language_ada
)
14984 int count
= fi
.nested_types_list
.size ();
14986 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14987 TYPE_NESTED_TYPES_ARRAY (type
)
14988 = ((struct decl_field
*)
14989 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
14990 TYPE_NESTED_TYPES_COUNT (type
) = count
;
14992 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
14993 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
14997 quirk_gcc_member_function_pointer (type
, objfile
);
14998 if (cu
->per_cu
->lang
== language_rust
&& die
->tag
== DW_TAG_union_type
)
14999 cu
->rust_unions
.push_back (type
);
15000 else if (cu
->per_cu
->lang
== language_ada
)
15001 quirk_ada_thick_pointer_struct (die
, cu
, type
);
15003 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15004 snapshots) has been known to create a die giving a declaration
15005 for a class that has, as a child, a die giving a definition for a
15006 nested class. So we have to process our children even if the
15007 current die is a declaration. Normally, of course, a declaration
15008 won't have any children at all. */
15010 child_die
= die
->child
;
15012 while (child_die
!= NULL
&& child_die
->tag
)
15014 if (child_die
->tag
== DW_TAG_member
15015 || child_die
->tag
== DW_TAG_variable
15016 || child_die
->tag
== DW_TAG_inheritance
15017 || child_die
->tag
== DW_TAG_template_value_param
15018 || child_die
->tag
== DW_TAG_template_type_param
)
15023 process_die (child_die
, cu
);
15025 child_die
= child_die
->sibling
;
15028 /* Do not consider external references. According to the DWARF standard,
15029 these DIEs are identified by the fact that they have no byte_size
15030 attribute, and a declaration attribute. */
15031 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
15032 || !die_is_declaration (die
, cu
)
15033 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
15035 struct symbol
*sym
= new_symbol (die
, type
, cu
);
15037 if (has_template_parameters
)
15039 struct symtab
*symtab
;
15040 if (sym
!= nullptr)
15041 symtab
= symbol_symtab (sym
);
15042 else if (cu
->line_header
!= nullptr)
15044 /* Any related symtab will do. */
15046 = cu
->line_header
->file_names ()[0].symtab
;
15051 complaint (_("could not find suitable "
15052 "symtab for template parameter"
15053 " - DIE at %s [in module %s]"),
15054 sect_offset_str (die
->sect_off
),
15055 objfile_name (objfile
));
15058 if (symtab
!= nullptr)
15060 /* Make sure that the symtab is set on the new symbols.
15061 Even though they don't appear in this symtab directly,
15062 other parts of gdb assume that symbols do, and this is
15063 reasonably true. */
15064 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
15065 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type
, i
), symtab
);
15071 /* Assuming DIE is an enumeration type, and TYPE is its associated
15072 type, update TYPE using some information only available in DIE's
15073 children. In particular, the fields are computed. */
15076 update_enumeration_type_from_children (struct die_info
*die
,
15078 struct dwarf2_cu
*cu
)
15080 struct die_info
*child_die
;
15081 int unsigned_enum
= 1;
15084 auto_obstack obstack
;
15085 std::vector
<struct field
> fields
;
15087 for (child_die
= die
->child
;
15088 child_die
!= NULL
&& child_die
->tag
;
15089 child_die
= child_die
->sibling
)
15091 struct attribute
*attr
;
15093 const gdb_byte
*bytes
;
15094 struct dwarf2_locexpr_baton
*baton
;
15097 if (child_die
->tag
!= DW_TAG_enumerator
)
15100 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
15104 name
= dwarf2_name (child_die
, cu
);
15106 name
= "<anonymous enumerator>";
15108 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
15109 &value
, &bytes
, &baton
);
15117 if (count_one_bits_ll (value
) >= 2)
15121 fields
.emplace_back ();
15122 struct field
&field
= fields
.back ();
15123 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
15124 field
.set_loc_enumval (value
);
15127 if (!fields
.empty ())
15129 type
->set_num_fields (fields
.size ());
15132 TYPE_ALLOC (type
, sizeof (struct field
) * fields
.size ()));
15133 memcpy (type
->fields (), fields
.data (),
15134 sizeof (struct field
) * fields
.size ());
15138 type
->set_is_unsigned (true);
15141 type
->set_is_flag_enum (true);
15144 /* Given a DW_AT_enumeration_type die, set its type. We do not
15145 complete the type's fields yet, or create any symbols. */
15147 static struct type
*
15148 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15150 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15152 struct attribute
*attr
;
15155 /* If the definition of this type lives in .debug_types, read that type.
15156 Don't follow DW_AT_specification though, that will take us back up
15157 the chain and we want to go down. */
15158 attr
= die
->attr (DW_AT_signature
);
15159 if (attr
!= nullptr)
15161 type
= get_DW_AT_signature_type (die
, attr
, cu
);
15163 /* The type's CU may not be the same as CU.
15164 Ensure TYPE is recorded with CU in die_type_hash. */
15165 return set_die_type (die
, type
, cu
);
15168 type
= alloc_type (objfile
);
15170 type
->set_code (TYPE_CODE_ENUM
);
15171 name
= dwarf2_full_name (NULL
, die
, cu
);
15173 type
->set_name (name
);
15175 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
15178 struct type
*underlying_type
= die_type (die
, cu
);
15180 TYPE_TARGET_TYPE (type
) = underlying_type
;
15183 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15184 if (attr
!= nullptr)
15186 TYPE_LENGTH (type
) = attr
->constant_value (0);
15190 TYPE_LENGTH (type
) = 0;
15193 maybe_set_alignment (cu
, die
, type
);
15195 /* The enumeration DIE can be incomplete. In Ada, any type can be
15196 declared as private in the package spec, and then defined only
15197 inside the package body. Such types are known as Taft Amendment
15198 Types. When another package uses such a type, an incomplete DIE
15199 may be generated by the compiler. */
15200 if (die_is_declaration (die
, cu
))
15201 type
->set_is_stub (true);
15203 /* If this type has an underlying type that is not a stub, then we
15204 may use its attributes. We always use the "unsigned" attribute
15205 in this situation, because ordinarily we guess whether the type
15206 is unsigned -- but the guess can be wrong and the underlying type
15207 can tell us the reality. However, we defer to a local size
15208 attribute if one exists, because this lets the compiler override
15209 the underlying type if needed. */
15210 if (TYPE_TARGET_TYPE (type
) != NULL
&& !TYPE_TARGET_TYPE (type
)->is_stub ())
15212 struct type
*underlying_type
= TYPE_TARGET_TYPE (type
);
15213 underlying_type
= check_typedef (underlying_type
);
15215 type
->set_is_unsigned (underlying_type
->is_unsigned ());
15217 if (TYPE_LENGTH (type
) == 0)
15218 TYPE_LENGTH (type
) = TYPE_LENGTH (underlying_type
);
15220 if (TYPE_RAW_ALIGN (type
) == 0
15221 && TYPE_RAW_ALIGN (underlying_type
) != 0)
15222 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
15225 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
15227 set_die_type (die
, type
, cu
);
15229 /* Finish the creation of this type by using the enum's children.
15230 Note that, as usual, this must come after set_die_type to avoid
15231 infinite recursion when trying to compute the names of the
15233 update_enumeration_type_from_children (die
, type
, cu
);
15238 /* Given a pointer to a die which begins an enumeration, process all
15239 the dies that define the members of the enumeration, and create the
15240 symbol for the enumeration type.
15242 NOTE: We reverse the order of the element list. */
15245 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
15247 struct type
*this_type
;
15249 this_type
= get_die_type (die
, cu
);
15250 if (this_type
== NULL
)
15251 this_type
= read_enumeration_type (die
, cu
);
15253 if (die
->child
!= NULL
)
15255 struct die_info
*child_die
;
15258 child_die
= die
->child
;
15259 while (child_die
&& child_die
->tag
)
15261 if (child_die
->tag
!= DW_TAG_enumerator
)
15263 process_die (child_die
, cu
);
15267 name
= dwarf2_name (child_die
, cu
);
15269 new_symbol (child_die
, this_type
, cu
);
15272 child_die
= child_die
->sibling
;
15276 /* If we are reading an enum from a .debug_types unit, and the enum
15277 is a declaration, and the enum is not the signatured type in the
15278 unit, then we do not want to add a symbol for it. Adding a
15279 symbol would in some cases obscure the true definition of the
15280 enum, giving users an incomplete type when the definition is
15281 actually available. Note that we do not want to do this for all
15282 enums which are just declarations, because C++0x allows forward
15283 enum declarations. */
15284 if (cu
->per_cu
->is_debug_types
15285 && die_is_declaration (die
, cu
))
15287 struct signatured_type
*sig_type
;
15289 sig_type
= (struct signatured_type
*) cu
->per_cu
;
15290 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
15291 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
15295 new_symbol (die
, this_type
, cu
);
15298 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15299 expression for an index type and finds the corresponding field
15300 offset in the hidden "P_BOUNDS" structure. Returns true on success
15301 and updates *FIELD, false if it fails to recognize an
15305 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
15306 int *bounds_offset
, struct field
*field
,
15307 struct dwarf2_cu
*cu
)
15309 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
15310 if (attr
== nullptr || !attr
->form_is_block ())
15313 const struct dwarf_block
*block
= attr
->as_block ();
15314 const gdb_byte
*start
= block
->data
;
15315 const gdb_byte
*end
= block
->data
+ block
->size
;
15317 /* The expression to recognize generally looks like:
15319 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15320 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15322 However, the second "plus_uconst" may be missing:
15324 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15325 DW_OP_deref_size: 4)
15327 This happens when the field is at the start of the structure.
15329 Also, the final deref may not be sized:
15331 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15334 This happens when the size of the index type happens to be the
15335 same as the architecture's word size. This can occur with or
15336 without the second plus_uconst. */
15338 if (end
- start
< 2)
15340 if (*start
++ != DW_OP_push_object_address
)
15342 if (*start
++ != DW_OP_plus_uconst
)
15345 uint64_t this_bound_off
;
15346 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
15347 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
15349 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15350 is consistent among all bounds. */
15351 if (*bounds_offset
== -1)
15352 *bounds_offset
= this_bound_off
;
15353 else if (*bounds_offset
!= this_bound_off
)
15356 if (start
== end
|| *start
++ != DW_OP_deref
)
15362 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
15364 /* This means an offset of 0. */
15366 else if (*start
++ != DW_OP_plus_uconst
)
15370 /* The size is the parameter to DW_OP_plus_uconst. */
15372 start
= gdb_read_uleb128 (start
, end
, &val
);
15373 if (start
== nullptr)
15375 if ((int) val
!= val
)
15384 if (*start
== DW_OP_deref_size
)
15386 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
15387 if (start
== nullptr)
15390 else if (*start
== DW_OP_deref
)
15392 size
= cu
->header
.addr_size
;
15398 field
->set_loc_bitpos (8 * offset
);
15399 if (size
!= TYPE_LENGTH (field
->type ()))
15400 FIELD_BITSIZE (*field
) = 8 * size
;
15405 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15406 some kinds of Ada arrays:
15408 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15409 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15410 <11e0> DW_AT_data_location: 2 byte block: 97 6
15411 (DW_OP_push_object_address; DW_OP_deref)
15412 <11e3> DW_AT_type : <0x1173>
15413 <11e7> DW_AT_sibling : <0x1201>
15414 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15415 <11ec> DW_AT_type : <0x1206>
15416 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15417 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15418 DW_OP_deref_size: 4)
15419 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15420 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15421 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15423 This actually represents a "thick pointer", which is a structure
15424 with two elements: one that is a pointer to the array data, and one
15425 that is a pointer to another structure; this second structure holds
15428 This returns a new type on success, or nullptr if this didn't
15429 recognize the type. */
15431 static struct type
*
15432 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
15435 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
15436 /* So far we've only seen this with block form. */
15437 if (attr
== nullptr || !attr
->form_is_block ())
15440 /* Note that this will fail if the structure layout is changed by
15441 the compiler. However, we have no good way to recognize some
15442 other layout, because we don't know what expression the compiler
15443 might choose to emit should this happen. */
15444 struct dwarf_block
*blk
= attr
->as_block ();
15446 || blk
->data
[0] != DW_OP_push_object_address
15447 || blk
->data
[1] != DW_OP_deref
)
15450 int bounds_offset
= -1;
15451 int max_align
= -1;
15452 std::vector
<struct field
> range_fields
;
15453 for (struct die_info
*child_die
= die
->child
;
15455 child_die
= child_die
->sibling
)
15457 if (child_die
->tag
== DW_TAG_subrange_type
)
15459 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
15461 int this_align
= type_align (underlying
);
15462 if (this_align
> max_align
)
15463 max_align
= this_align
;
15465 range_fields
.emplace_back ();
15466 range_fields
.emplace_back ();
15468 struct field
&lower
= range_fields
[range_fields
.size () - 2];
15469 struct field
&upper
= range_fields
[range_fields
.size () - 1];
15471 lower
.set_type (underlying
);
15472 FIELD_ARTIFICIAL (lower
) = 1;
15474 upper
.set_type (underlying
);
15475 FIELD_ARTIFICIAL (upper
) = 1;
15477 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
15478 &bounds_offset
, &lower
, cu
)
15479 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
15480 &bounds_offset
, &upper
, cu
))
15485 /* This shouldn't really happen, but double-check that we found
15486 where the bounds are stored. */
15487 if (bounds_offset
== -1)
15490 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15491 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15495 /* Set the name of each field in the bounds. */
15496 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
15497 range_fields
[i
].set_name (objfile
->intern (name
));
15498 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
15499 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
15502 struct type
*bounds
= alloc_type (objfile
);
15503 bounds
->set_code (TYPE_CODE_STRUCT
);
15505 bounds
->set_num_fields (range_fields
.size ());
15507 ((struct field
*) TYPE_ALLOC (bounds
, (bounds
->num_fields ()
15508 * sizeof (struct field
))));
15509 memcpy (bounds
->fields (), range_fields
.data (),
15510 bounds
->num_fields () * sizeof (struct field
));
15512 int last_fieldno
= range_fields
.size () - 1;
15513 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
15514 + TYPE_LENGTH (bounds
->field (last_fieldno
).type ()));
15515 TYPE_LENGTH (bounds
) = align_up (bounds_size
, max_align
);
15517 /* Rewrite the existing array type in place. Specifically, we
15518 remove any dynamic properties we might have read, and we replace
15519 the index types. */
15520 struct type
*iter
= type
;
15521 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15523 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
15524 iter
->main_type
->dyn_prop_list
= nullptr;
15525 iter
->set_index_type
15526 (create_static_range_type (NULL
, bounds
->field (i
).type (), 1, 0));
15527 iter
= TYPE_TARGET_TYPE (iter
);
15530 struct type
*result
= alloc_type (objfile
);
15531 result
->set_code (TYPE_CODE_STRUCT
);
15533 result
->set_num_fields (2);
15535 ((struct field
*) TYPE_ZALLOC (result
, (result
->num_fields ()
15536 * sizeof (struct field
))));
15538 /* The names are chosen to coincide with what the compiler does with
15539 -fgnat-encodings=all, which the Ada code in gdb already
15541 result
->field (0).set_name ("P_ARRAY");
15542 result
->field (0).set_type (lookup_pointer_type (type
));
15544 result
->field (1).set_name ("P_BOUNDS");
15545 result
->field (1).set_type (lookup_pointer_type (bounds
));
15546 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
15548 result
->set_name (type
->name ());
15549 TYPE_LENGTH (result
) = (TYPE_LENGTH (result
->field (0).type ())
15550 + TYPE_LENGTH (result
->field (1).type ()));
15555 /* Extract all information from a DW_TAG_array_type DIE and put it in
15556 the DIE's type field. For now, this only handles one dimensional
15559 static struct type
*
15560 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15562 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15563 struct die_info
*child_die
;
15565 struct type
*element_type
, *range_type
, *index_type
;
15566 struct attribute
*attr
;
15568 struct dynamic_prop
*byte_stride_prop
= NULL
;
15569 unsigned int bit_stride
= 0;
15571 element_type
= die_type (die
, cu
);
15573 /* The die_type call above may have already set the type for this DIE. */
15574 type
= get_die_type (die
, cu
);
15578 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15582 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15585 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
15586 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
15590 complaint (_("unable to read array DW_AT_byte_stride "
15591 " - DIE at %s [in module %s]"),
15592 sect_offset_str (die
->sect_off
),
15593 objfile_name (cu
->per_objfile
->objfile
));
15594 /* Ignore this attribute. We will likely not be able to print
15595 arrays of this type correctly, but there is little we can do
15596 to help if we cannot read the attribute's value. */
15597 byte_stride_prop
= NULL
;
15601 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15603 bit_stride
= attr
->constant_value (0);
15605 /* Irix 6.2 native cc creates array types without children for
15606 arrays with unspecified length. */
15607 if (die
->child
== NULL
)
15609 index_type
= objfile_type (objfile
)->builtin_int
;
15610 range_type
= create_static_range_type (NULL
, index_type
, 0, -1);
15611 type
= create_array_type_with_stride (NULL
, element_type
, range_type
,
15612 byte_stride_prop
, bit_stride
);
15613 return set_die_type (die
, type
, cu
);
15616 std::vector
<struct type
*> range_types
;
15617 child_die
= die
->child
;
15618 while (child_die
&& child_die
->tag
)
15620 if (child_die
->tag
== DW_TAG_subrange_type
15621 || child_die
->tag
== DW_TAG_generic_subrange
)
15623 struct type
*child_type
= read_type_die (child_die
, cu
);
15625 if (child_type
!= NULL
)
15627 /* The range type was succesfully read. Save it for the
15628 array type creation. */
15629 range_types
.push_back (child_type
);
15632 child_die
= child_die
->sibling
;
15635 if (range_types
.empty ())
15637 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15638 sect_offset_str (die
->sect_off
),
15639 objfile_name (cu
->per_objfile
->objfile
));
15643 /* Dwarf2 dimensions are output from left to right, create the
15644 necessary array types in backwards order. */
15646 type
= element_type
;
15648 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
15652 while (i
< range_types
.size ())
15654 type
= create_array_type_with_stride (NULL
, type
, range_types
[i
++],
15655 byte_stride_prop
, bit_stride
);
15657 byte_stride_prop
= nullptr;
15662 size_t ndim
= range_types
.size ();
15665 type
= create_array_type_with_stride (NULL
, type
, range_types
[ndim
],
15666 byte_stride_prop
, bit_stride
);
15668 byte_stride_prop
= nullptr;
15672 gdb_assert (type
!= element_type
);
15674 /* Understand Dwarf2 support for vector types (like they occur on
15675 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15676 array type. This is not part of the Dwarf2/3 standard yet, but a
15677 custom vendor extension. The main difference between a regular
15678 array and the vector variant is that vectors are passed by value
15680 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
15681 if (attr
!= nullptr)
15682 make_vector_type (type
);
15684 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15685 implementation may choose to implement triple vectors using this
15687 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15688 if (attr
!= nullptr && attr
->form_is_unsigned ())
15690 if (attr
->as_unsigned () >= TYPE_LENGTH (type
))
15691 TYPE_LENGTH (type
) = attr
->as_unsigned ();
15693 complaint (_("DW_AT_byte_size for array type smaller "
15694 "than the total size of elements"));
15697 name
= dwarf2_name (die
, cu
);
15699 type
->set_name (name
);
15701 maybe_set_alignment (cu
, die
, type
);
15703 struct type
*replacement_type
= nullptr;
15704 if (cu
->per_cu
->lang
== language_ada
)
15706 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
15707 if (replacement_type
!= nullptr)
15708 type
= replacement_type
;
15711 /* Install the type in the die. */
15712 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
15714 /* set_die_type should be already done. */
15715 set_descriptive_type (type
, die
, cu
);
15720 static enum dwarf_array_dim_ordering
15721 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
15723 struct attribute
*attr
;
15725 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
15727 if (attr
!= nullptr)
15729 LONGEST val
= attr
->constant_value (-1);
15730 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
15731 return (enum dwarf_array_dim_ordering
) val
;
15734 /* GNU F77 is a special case, as at 08/2004 array type info is the
15735 opposite order to the dwarf2 specification, but data is still
15736 laid out as per normal fortran.
15738 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15739 version checking. */
15741 if (cu
->per_cu
->lang
== language_fortran
15742 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
15744 return DW_ORD_row_major
;
15747 switch (cu
->language_defn
->array_ordering ())
15749 case array_column_major
:
15750 return DW_ORD_col_major
;
15751 case array_row_major
:
15753 return DW_ORD_row_major
;
15757 /* Extract all information from a DW_TAG_set_type DIE and put it in
15758 the DIE's type field. */
15760 static struct type
*
15761 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15763 struct type
*domain_type
, *set_type
;
15764 struct attribute
*attr
;
15766 domain_type
= die_type (die
, cu
);
15768 /* The die_type call above may have already set the type for this DIE. */
15769 set_type
= get_die_type (die
, cu
);
15773 set_type
= create_set_type (NULL
, domain_type
);
15775 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15776 if (attr
!= nullptr && attr
->form_is_unsigned ())
15777 TYPE_LENGTH (set_type
) = attr
->as_unsigned ();
15779 maybe_set_alignment (cu
, die
, set_type
);
15781 return set_die_type (die
, set_type
, cu
);
15784 /* A helper for read_common_block that creates a locexpr baton.
15785 SYM is the symbol which we are marking as computed.
15786 COMMON_DIE is the DIE for the common block.
15787 COMMON_LOC is the location expression attribute for the common
15789 MEMBER_LOC is the location expression attribute for the particular
15790 member of the common block that we are processing.
15791 CU is the CU from which the above come. */
15794 mark_common_block_symbol_computed (struct symbol
*sym
,
15795 struct die_info
*common_die
,
15796 struct attribute
*common_loc
,
15797 struct attribute
*member_loc
,
15798 struct dwarf2_cu
*cu
)
15800 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15801 struct objfile
*objfile
= per_objfile
->objfile
;
15802 struct dwarf2_locexpr_baton
*baton
;
15804 unsigned int cu_off
;
15805 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
15806 LONGEST offset
= 0;
15808 gdb_assert (common_loc
&& member_loc
);
15809 gdb_assert (common_loc
->form_is_block ());
15810 gdb_assert (member_loc
->form_is_block ()
15811 || member_loc
->form_is_constant ());
15813 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
15814 baton
->per_objfile
= per_objfile
;
15815 baton
->per_cu
= cu
->per_cu
;
15816 gdb_assert (baton
->per_cu
);
15818 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15820 if (member_loc
->form_is_constant ())
15822 offset
= member_loc
->constant_value (0);
15823 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
15826 baton
->size
+= member_loc
->as_block ()->size
;
15828 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
15831 *ptr
++ = DW_OP_call4
;
15832 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
15833 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
15836 if (member_loc
->form_is_constant ())
15838 *ptr
++ = DW_OP_addr
;
15839 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
15840 ptr
+= cu
->header
.addr_size
;
15844 /* We have to copy the data here, because DW_OP_call4 will only
15845 use a DW_AT_location attribute. */
15846 struct dwarf_block
*block
= member_loc
->as_block ();
15847 memcpy (ptr
, block
->data
, block
->size
);
15848 ptr
+= block
->size
;
15851 *ptr
++ = DW_OP_plus
;
15852 gdb_assert (ptr
- baton
->data
== baton
->size
);
15854 SYMBOL_LOCATION_BATON (sym
) = baton
;
15855 sym
->set_aclass_index (dwarf2_locexpr_index
);
15858 /* Create appropriate locally-scoped variables for all the
15859 DW_TAG_common_block entries. Also create a struct common_block
15860 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15861 is used to separate the common blocks name namespace from regular
15865 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
15867 struct attribute
*attr
;
15869 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
15870 if (attr
!= nullptr)
15872 /* Support the .debug_loc offsets. */
15873 if (attr
->form_is_block ())
15877 else if (attr
->form_is_section_offset ())
15879 dwarf2_complex_location_expr_complaint ();
15884 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15885 "common block member");
15890 if (die
->child
!= NULL
)
15892 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15893 struct die_info
*child_die
;
15894 size_t n_entries
= 0, size
;
15895 struct common_block
*common_block
;
15896 struct symbol
*sym
;
15898 for (child_die
= die
->child
;
15899 child_die
&& child_die
->tag
;
15900 child_die
= child_die
->sibling
)
15903 size
= (sizeof (struct common_block
)
15904 + (n_entries
- 1) * sizeof (struct symbol
*));
15906 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
15908 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
15909 common_block
->n_entries
= 0;
15911 for (child_die
= die
->child
;
15912 child_die
&& child_die
->tag
;
15913 child_die
= child_die
->sibling
)
15915 /* Create the symbol in the DW_TAG_common_block block in the current
15917 sym
= new_symbol (child_die
, NULL
, cu
);
15920 struct attribute
*member_loc
;
15922 common_block
->contents
[common_block
->n_entries
++] = sym
;
15924 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
15928 /* GDB has handled this for a long time, but it is
15929 not specified by DWARF. It seems to have been
15930 emitted by gfortran at least as recently as:
15931 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15932 complaint (_("Variable in common block has "
15933 "DW_AT_data_member_location "
15934 "- DIE at %s [in module %s]"),
15935 sect_offset_str (child_die
->sect_off
),
15936 objfile_name (objfile
));
15938 if (member_loc
->form_is_section_offset ())
15939 dwarf2_complex_location_expr_complaint ();
15940 else if (member_loc
->form_is_constant ()
15941 || member_loc
->form_is_block ())
15943 if (attr
!= nullptr)
15944 mark_common_block_symbol_computed (sym
, die
, attr
,
15948 dwarf2_complex_location_expr_complaint ();
15953 sym
= new_symbol (die
, objfile_type (objfile
)->builtin_void
, cu
);
15954 sym
->set_value_common_block (common_block
);
15958 /* Create a type for a C++ namespace. */
15960 static struct type
*
15961 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15963 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15964 const char *previous_prefix
, *name
;
15968 /* For extensions, reuse the type of the original namespace. */
15969 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
15971 struct die_info
*ext_die
;
15972 struct dwarf2_cu
*ext_cu
= cu
;
15974 ext_die
= dwarf2_extension (die
, &ext_cu
);
15975 type
= read_type_die (ext_die
, ext_cu
);
15977 /* EXT_CU may not be the same as CU.
15978 Ensure TYPE is recorded with CU in die_type_hash. */
15979 return set_die_type (die
, type
, cu
);
15982 name
= namespace_name (die
, &is_anonymous
, cu
);
15984 /* Now build the name of the current namespace. */
15986 previous_prefix
= determine_prefix (die
, cu
);
15987 if (previous_prefix
[0] != '\0')
15988 name
= typename_concat (&objfile
->objfile_obstack
,
15989 previous_prefix
, name
, 0, cu
);
15991 /* Create the type. */
15992 type
= init_type (objfile
, TYPE_CODE_NAMESPACE
, 0, name
);
15994 return set_die_type (die
, type
, cu
);
15997 /* Read a namespace scope. */
16000 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
16002 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16005 /* Add a symbol associated to this if we haven't seen the namespace
16006 before. Also, add a using directive if it's an anonymous
16009 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
16013 type
= read_type_die (die
, cu
);
16014 new_symbol (die
, type
, cu
);
16016 namespace_name (die
, &is_anonymous
, cu
);
16019 const char *previous_prefix
= determine_prefix (die
, cu
);
16021 std::vector
<const char *> excludes
;
16022 add_using_directive (using_directives (cu
),
16023 previous_prefix
, type
->name (), NULL
,
16024 NULL
, excludes
, 0, &objfile
->objfile_obstack
);
16028 if (die
->child
!= NULL
)
16030 struct die_info
*child_die
= die
->child
;
16032 while (child_die
&& child_die
->tag
)
16034 process_die (child_die
, cu
);
16035 child_die
= child_die
->sibling
;
16040 /* Read a Fortran module as type. This DIE can be only a declaration used for
16041 imported module. Still we need that type as local Fortran "use ... only"
16042 declaration imports depend on the created type in determine_prefix. */
16044 static struct type
*
16045 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16047 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16048 const char *module_name
;
16051 module_name
= dwarf2_name (die
, cu
);
16052 type
= init_type (objfile
, TYPE_CODE_MODULE
, 0, module_name
);
16054 return set_die_type (die
, type
, cu
);
16057 /* Read a Fortran module. */
16060 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
16062 struct die_info
*child_die
= die
->child
;
16065 type
= read_type_die (die
, cu
);
16066 new_symbol (die
, type
, cu
);
16068 while (child_die
&& child_die
->tag
)
16070 process_die (child_die
, cu
);
16071 child_die
= child_die
->sibling
;
16075 /* Return the name of the namespace represented by DIE. Set
16076 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16079 static const char *
16080 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
16082 struct die_info
*current_die
;
16083 const char *name
= NULL
;
16085 /* Loop through the extensions until we find a name. */
16087 for (current_die
= die
;
16088 current_die
!= NULL
;
16089 current_die
= dwarf2_extension (die
, &cu
))
16091 /* We don't use dwarf2_name here so that we can detect the absence
16092 of a name -> anonymous namespace. */
16093 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
16099 /* Is it an anonymous namespace? */
16101 *is_anonymous
= (name
== NULL
);
16103 name
= CP_ANONYMOUS_NAMESPACE_STR
;
16108 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16109 the user defined type vector. */
16111 static struct type
*
16112 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16114 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
16115 struct comp_unit_head
*cu_header
= &cu
->header
;
16117 struct attribute
*attr_byte_size
;
16118 struct attribute
*attr_address_class
;
16119 int byte_size
, addr_class
;
16120 struct type
*target_type
;
16122 target_type
= die_type (die
, cu
);
16124 /* The die_type call above may have already set the type for this DIE. */
16125 type
= get_die_type (die
, cu
);
16129 type
= lookup_pointer_type (target_type
);
16131 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16132 if (attr_byte_size
)
16133 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
16135 byte_size
= cu_header
->addr_size
;
16137 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
16138 if (attr_address_class
)
16139 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
16141 addr_class
= DW_ADDR_none
;
16143 ULONGEST alignment
= get_alignment (cu
, die
);
16145 /* If the pointer size, alignment, or address class is different
16146 than the default, create a type variant marked as such and set
16147 the length accordingly. */
16148 if (TYPE_LENGTH (type
) != byte_size
16149 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
16150 && alignment
!= TYPE_RAW_ALIGN (type
))
16151 || addr_class
!= DW_ADDR_none
)
16153 if (gdbarch_address_class_type_flags_p (gdbarch
))
16155 type_instance_flags type_flags
16156 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
16158 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
16160 type
= make_type_with_address_space (type
, type_flags
);
16162 else if (TYPE_LENGTH (type
) != byte_size
)
16164 complaint (_("invalid pointer size %d"), byte_size
);
16166 else if (TYPE_RAW_ALIGN (type
) != alignment
)
16168 complaint (_("Invalid DW_AT_alignment"
16169 " - DIE at %s [in module %s]"),
16170 sect_offset_str (die
->sect_off
),
16171 objfile_name (cu
->per_objfile
->objfile
));
16175 /* Should we also complain about unhandled address classes? */
16179 TYPE_LENGTH (type
) = byte_size
;
16180 set_type_align (type
, alignment
);
16181 return set_die_type (die
, type
, cu
);
16184 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16185 the user defined type vector. */
16187 static struct type
*
16188 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16191 struct type
*to_type
;
16192 struct type
*domain
;
16194 to_type
= die_type (die
, cu
);
16195 domain
= die_containing_type (die
, cu
);
16197 /* The calls above may have already set the type for this DIE. */
16198 type
= get_die_type (die
, cu
);
16202 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
16203 type
= lookup_methodptr_type (to_type
);
16204 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
16206 struct type
*new_type
= alloc_type (cu
->per_objfile
->objfile
);
16208 smash_to_method_type (new_type
, domain
, TYPE_TARGET_TYPE (to_type
),
16209 to_type
->fields (), to_type
->num_fields (),
16210 to_type
->has_varargs ());
16211 type
= lookup_methodptr_type (new_type
);
16214 type
= lookup_memberptr_type (to_type
, domain
);
16216 return set_die_type (die
, type
, cu
);
16219 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16220 the user defined type vector. */
16222 static struct type
*
16223 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16224 enum type_code refcode
)
16226 struct comp_unit_head
*cu_header
= &cu
->header
;
16227 struct type
*type
, *target_type
;
16228 struct attribute
*attr
;
16230 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
16232 target_type
= die_type (die
, cu
);
16234 /* The die_type call above may have already set the type for this DIE. */
16235 type
= get_die_type (die
, cu
);
16239 type
= lookup_reference_type (target_type
, refcode
);
16240 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16241 if (attr
!= nullptr)
16243 TYPE_LENGTH (type
) = attr
->constant_value (cu_header
->addr_size
);
16247 TYPE_LENGTH (type
) = cu_header
->addr_size
;
16249 maybe_set_alignment (cu
, die
, type
);
16250 return set_die_type (die
, type
, cu
);
16253 /* Add the given cv-qualifiers to the element type of the array. GCC
16254 outputs DWARF type qualifiers that apply to an array, not the
16255 element type. But GDB relies on the array element type to carry
16256 the cv-qualifiers. This mimics section 6.7.3 of the C99
16259 static struct type
*
16260 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16261 struct type
*base_type
, int cnst
, int voltl
)
16263 struct type
*el_type
, *inner_array
;
16265 base_type
= copy_type (base_type
);
16266 inner_array
= base_type
;
16268 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
16270 TYPE_TARGET_TYPE (inner_array
) =
16271 copy_type (TYPE_TARGET_TYPE (inner_array
));
16272 inner_array
= TYPE_TARGET_TYPE (inner_array
);
16275 el_type
= TYPE_TARGET_TYPE (inner_array
);
16276 cnst
|= TYPE_CONST (el_type
);
16277 voltl
|= TYPE_VOLATILE (el_type
);
16278 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
16280 return set_die_type (die
, base_type
, cu
);
16283 static struct type
*
16284 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16286 struct type
*base_type
, *cv_type
;
16288 base_type
= die_type (die
, cu
);
16290 /* The die_type call above may have already set the type for this DIE. */
16291 cv_type
= get_die_type (die
, cu
);
16295 /* In case the const qualifier is applied to an array type, the element type
16296 is so qualified, not the array type (section 6.7.3 of C99). */
16297 if (base_type
->code () == TYPE_CODE_ARRAY
)
16298 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
16300 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
16301 return set_die_type (die
, cv_type
, cu
);
16304 static struct type
*
16305 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16307 struct type
*base_type
, *cv_type
;
16309 base_type
= die_type (die
, cu
);
16311 /* The die_type call above may have already set the type for this DIE. */
16312 cv_type
= get_die_type (die
, cu
);
16316 /* In case the volatile qualifier is applied to an array type, the
16317 element type is so qualified, not the array type (section 6.7.3
16319 if (base_type
->code () == TYPE_CODE_ARRAY
)
16320 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
16322 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
16323 return set_die_type (die
, cv_type
, cu
);
16326 /* Handle DW_TAG_restrict_type. */
16328 static struct type
*
16329 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16331 struct type
*base_type
, *cv_type
;
16333 base_type
= die_type (die
, cu
);
16335 /* The die_type call above may have already set the type for this DIE. */
16336 cv_type
= get_die_type (die
, cu
);
16340 cv_type
= make_restrict_type (base_type
);
16341 return set_die_type (die
, cv_type
, cu
);
16344 /* Handle DW_TAG_atomic_type. */
16346 static struct type
*
16347 read_tag_atomic_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16349 struct type
*base_type
, *cv_type
;
16351 base_type
= die_type (die
, cu
);
16353 /* The die_type call above may have already set the type for this DIE. */
16354 cv_type
= get_die_type (die
, cu
);
16358 cv_type
= make_atomic_type (base_type
);
16359 return set_die_type (die
, cv_type
, cu
);
16362 /* Extract all information from a DW_TAG_string_type DIE and add to
16363 the user defined type vector. It isn't really a user defined type,
16364 but it behaves like one, with other DIE's using an AT_user_def_type
16365 attribute to reference it. */
16367 static struct type
*
16368 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16370 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16371 struct gdbarch
*gdbarch
= objfile
->arch ();
16372 struct type
*type
, *range_type
, *index_type
, *char_type
;
16373 struct attribute
*attr
;
16374 struct dynamic_prop prop
;
16375 bool length_is_constant
= true;
16378 /* There are a couple of places where bit sizes might be made use of
16379 when parsing a DW_TAG_string_type, however, no producer that we know
16380 of make use of these. Handling bit sizes that are a multiple of the
16381 byte size is easy enough, but what about other bit sizes? Lets deal
16382 with that problem when we have to. Warn about these attributes being
16383 unsupported, then parse the type and ignore them like we always
16385 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
16386 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
16388 static bool warning_printed
= false;
16389 if (!warning_printed
)
16391 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16392 "currently supported on DW_TAG_string_type."));
16393 warning_printed
= true;
16397 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
16398 if (attr
!= nullptr && !attr
->form_is_constant ())
16400 /* The string length describes the location at which the length of
16401 the string can be found. The size of the length field can be
16402 specified with one of the attributes below. */
16403 struct type
*prop_type
;
16404 struct attribute
*len
16405 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
16406 if (len
== nullptr)
16407 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16408 if (len
!= nullptr && len
->form_is_constant ())
16410 /* Pass 0 as the default as we know this attribute is constant
16411 and the default value will not be returned. */
16412 LONGEST sz
= len
->constant_value (0);
16413 prop_type
= objfile_int_type (objfile
, sz
, true);
16417 /* If the size is not specified then we assume it is the size of
16418 an address on this target. */
16419 prop_type
= cu
->addr_sized_int_type (true);
16422 /* Convert the attribute into a dynamic property. */
16423 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
16426 length_is_constant
= false;
16428 else if (attr
!= nullptr)
16430 /* This DW_AT_string_length just contains the length with no
16431 indirection. There's no need to create a dynamic property in this
16432 case. Pass 0 for the default value as we know it will not be
16433 returned in this case. */
16434 length
= attr
->constant_value (0);
16436 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
16438 /* We don't currently support non-constant byte sizes for strings. */
16439 length
= attr
->constant_value (1);
16443 /* Use 1 as a fallback length if we have nothing else. */
16447 index_type
= objfile_type (objfile
)->builtin_int
;
16448 if (length_is_constant
)
16449 range_type
= create_static_range_type (NULL
, index_type
, 1, length
);
16452 struct dynamic_prop low_bound
;
16454 low_bound
.set_const_val (1);
16455 range_type
= create_range_type (NULL
, index_type
, &low_bound
, &prop
, 0);
16457 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
16458 type
= create_string_type (NULL
, char_type
, range_type
);
16460 return set_die_type (die
, type
, cu
);
16463 /* Assuming that DIE corresponds to a function, returns nonzero
16464 if the function is prototyped. */
16467 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
16469 struct attribute
*attr
;
16471 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
16472 if (attr
&& attr
->as_boolean ())
16475 /* The DWARF standard implies that the DW_AT_prototyped attribute
16476 is only meaningful for C, but the concept also extends to other
16477 languages that allow unprototyped functions (Eg: Objective C).
16478 For all other languages, assume that functions are always
16480 if (cu
->per_cu
->lang
!= language_c
16481 && cu
->per_cu
->lang
!= language_objc
16482 && cu
->per_cu
->lang
!= language_opencl
)
16485 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16486 prototyped and unprototyped functions; default to prototyped,
16487 since that is more common in modern code (and RealView warns
16488 about unprototyped functions). */
16489 if (producer_is_realview (cu
->producer
))
16495 /* Handle DIES due to C code like:
16499 int (*funcp)(int a, long l);
16503 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16505 static struct type
*
16506 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16508 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16509 struct type
*type
; /* Type that this function returns. */
16510 struct type
*ftype
; /* Function that returns above type. */
16511 struct attribute
*attr
;
16513 type
= die_type (die
, cu
);
16515 /* The die_type call above may have already set the type for this DIE. */
16516 ftype
= get_die_type (die
, cu
);
16520 ftype
= lookup_function_type (type
);
16522 if (prototyped_function_p (die
, cu
))
16523 ftype
->set_is_prototyped (true);
16525 /* Store the calling convention in the type if it's available in
16526 the subroutine die. Otherwise set the calling convention to
16527 the default value DW_CC_normal. */
16528 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
16529 if (attr
!= nullptr
16530 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
16531 TYPE_CALLING_CONVENTION (ftype
)
16532 = (enum dwarf_calling_convention
) attr
->constant_value (0);
16533 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
16534 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
16536 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
16538 /* Record whether the function returns normally to its caller or not
16539 if the DWARF producer set that information. */
16540 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
16541 if (attr
&& attr
->as_boolean ())
16542 TYPE_NO_RETURN (ftype
) = 1;
16544 /* We need to add the subroutine type to the die immediately so
16545 we don't infinitely recurse when dealing with parameters
16546 declared as the same subroutine type. */
16547 set_die_type (die
, ftype
, cu
);
16549 if (die
->child
!= NULL
)
16551 struct type
*void_type
= objfile_type (objfile
)->builtin_void
;
16552 struct die_info
*child_die
;
16553 int nparams
, iparams
;
16555 /* Count the number of parameters.
16556 FIXME: GDB currently ignores vararg functions, but knows about
16557 vararg member functions. */
16559 child_die
= die
->child
;
16560 while (child_die
&& child_die
->tag
)
16562 if (child_die
->tag
== DW_TAG_formal_parameter
)
16564 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
16565 ftype
->set_has_varargs (true);
16567 child_die
= child_die
->sibling
;
16570 /* Allocate storage for parameters and fill them in. */
16571 ftype
->set_num_fields (nparams
);
16573 ((struct field
*) TYPE_ZALLOC (ftype
, nparams
* sizeof (struct field
)));
16575 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16576 even if we error out during the parameters reading below. */
16577 for (iparams
= 0; iparams
< nparams
; iparams
++)
16578 ftype
->field (iparams
).set_type (void_type
);
16581 child_die
= die
->child
;
16582 while (child_die
&& child_die
->tag
)
16584 if (child_die
->tag
== DW_TAG_formal_parameter
)
16586 struct type
*arg_type
;
16588 /* DWARF version 2 has no clean way to discern C++
16589 static and non-static member functions. G++ helps
16590 GDB by marking the first parameter for non-static
16591 member functions (which is the this pointer) as
16592 artificial. We pass this information to
16593 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16595 DWARF version 3 added DW_AT_object_pointer, which GCC
16596 4.5 does not yet generate. */
16597 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
16598 if (attr
!= nullptr)
16599 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = attr
->as_boolean ();
16601 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
16602 arg_type
= die_type (child_die
, cu
);
16604 /* RealView does not mark THIS as const, which the testsuite
16605 expects. GCC marks THIS as const in method definitions,
16606 but not in the class specifications (GCC PR 43053). */
16607 if (cu
->per_cu
->lang
== language_cplus
16608 && !TYPE_CONST (arg_type
)
16609 && TYPE_FIELD_ARTIFICIAL (ftype
, iparams
))
16612 struct dwarf2_cu
*arg_cu
= cu
;
16613 const char *name
= dwarf2_name (child_die
, cu
);
16615 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
16616 if (attr
!= nullptr)
16618 /* If the compiler emits this, use it. */
16619 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
16622 else if (name
&& strcmp (name
, "this") == 0)
16623 /* Function definitions will have the argument names. */
16625 else if (name
== NULL
&& iparams
== 0)
16626 /* Declarations may not have the names, so like
16627 elsewhere in GDB, assume an artificial first
16628 argument is "this". */
16632 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
16636 ftype
->field (iparams
).set_type (arg_type
);
16639 child_die
= child_die
->sibling
;
16646 static struct type
*
16647 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
16649 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16650 const char *name
= NULL
;
16651 struct type
*this_type
, *target_type
;
16653 name
= dwarf2_full_name (NULL
, die
, cu
);
16654 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, name
);
16655 this_type
->set_target_is_stub (true);
16656 set_die_type (die
, this_type
, cu
);
16657 target_type
= die_type (die
, cu
);
16658 if (target_type
!= this_type
)
16659 TYPE_TARGET_TYPE (this_type
) = target_type
;
16662 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16663 spec and cause infinite loops in GDB. */
16664 complaint (_("Self-referential DW_TAG_typedef "
16665 "- DIE at %s [in module %s]"),
16666 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
16667 TYPE_TARGET_TYPE (this_type
) = NULL
;
16671 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16672 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16673 Handle these by just returning the target type, rather than
16674 constructing an anonymous typedef type and trying to handle this
16676 set_die_type (die
, target_type
, cu
);
16677 return target_type
;
16682 /* Helper for get_dwarf2_rational_constant that computes the value of
16683 a given gmp_mpz given an attribute. */
16686 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
16688 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16689 location expression that pushes an implicit value. */
16690 if (attr
->form
== DW_FORM_exprloc
)
16692 dwarf_block
*blk
= attr
->as_block ();
16693 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
16696 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
16697 blk
->data
+ blk
->size
,
16699 if (ptr
- blk
->data
+ len
<= blk
->size
)
16701 mpz_import (value
->val
, len
,
16702 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16708 /* On failure set it to 1. */
16709 *value
= gdb_mpz (1);
16711 else if (attr
->form_is_block ())
16713 dwarf_block
*blk
= attr
->as_block ();
16714 mpz_import (value
->val
, blk
->size
,
16715 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16716 1, 0, 0, blk
->data
);
16719 *value
= gdb_mpz (attr
->constant_value (1));
16722 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16723 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16725 If the numerator and/or numerator attribute is missing,
16726 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16730 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
16731 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
16733 struct attribute
*num_attr
, *denom_attr
;
16735 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
16736 if (num_attr
== nullptr)
16737 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16738 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16740 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
16741 if (denom_attr
== nullptr)
16742 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16743 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16745 if (num_attr
== nullptr || denom_attr
== nullptr)
16748 get_mpz (cu
, numerator
, num_attr
);
16749 get_mpz (cu
, denominator
, denom_attr
);
16752 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16753 rational constant, rather than a signed one.
16755 If the rational constant has a negative value, a complaint
16756 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16759 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
16760 struct dwarf2_cu
*cu
,
16761 gdb_mpz
*numerator
,
16762 gdb_mpz
*denominator
)
16767 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
16768 if (mpz_sgn (num
.val
) == -1 && mpz_sgn (denom
.val
) == -1)
16770 mpz_neg (num
.val
, num
.val
);
16771 mpz_neg (denom
.val
, denom
.val
);
16773 else if (mpz_sgn (num
.val
) == -1)
16775 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16777 sect_offset_str (die
->sect_off
));
16780 else if (mpz_sgn (denom
.val
) == -1)
16782 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16784 sect_offset_str (die
->sect_off
));
16788 *numerator
= std::move (num
);
16789 *denominator
= std::move (denom
);
16792 /* Assuming that ENCODING is a string whose contents starting at the
16793 K'th character is "_nn" where "nn" is a decimal number, scan that
16794 number and set RESULT to the value. K is updated to point to the
16795 character immediately following the number.
16797 If the string does not conform to the format described above, false
16798 is returned, and K may or may not be changed. */
16801 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
16803 /* The next character should be an underscore ('_') followed
16805 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
16808 /* Skip the underscore. */
16812 /* Determine the number of digits for our number. */
16813 while (isdigit (encoding
[k
]))
16818 std::string
copy (&encoding
[start
], k
- start
);
16819 if (mpz_set_str (result
->val
, copy
.c_str (), 10) == -1)
16825 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16826 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16827 DENOM, update OFFSET, and return true on success. Return false on
16831 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
16832 gdb_mpz
*num
, gdb_mpz
*denom
)
16834 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
16836 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
16839 /* Assuming DIE corresponds to a fixed point type, finish the creation
16840 of the corresponding TYPE by setting its type-specific data. CU is
16841 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16842 encodings. It is nullptr if the GNAT encoding should be
16846 finish_fixed_point_type (struct type
*type
, const char *suffix
,
16847 struct die_info
*die
, struct dwarf2_cu
*cu
)
16849 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
16850 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
16852 /* If GNAT encodings are preferred, don't examine the
16854 struct attribute
*attr
= nullptr;
16855 if (suffix
== nullptr)
16857 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
16858 if (attr
== nullptr)
16859 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
16860 if (attr
== nullptr)
16861 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16864 /* Numerator and denominator of our fixed-point type's scaling factor.
16865 The default is a scaling factor of 1, which we use as a fallback
16866 when we are not able to decode it (problem with the debugging info,
16867 unsupported forms, bug in GDB, etc...). Using that as the default
16868 allows us to at least print the unscaled value, which might still
16869 be useful to a user. */
16870 gdb_mpz
scale_num (1);
16871 gdb_mpz
scale_denom (1);
16873 if (attr
== nullptr)
16876 if (suffix
!= nullptr
16877 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16879 /* The number might be encoded as _nn_dd_nn_dd, where the
16880 second ratio is the 'small value. In this situation, we
16881 want the second value. */
16882 && (suffix
[offset
] != '_'
16883 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16890 /* Scaling factor not found. Assume a scaling factor of 1,
16891 and hope for the best. At least the user will be able to
16892 see the encoded value. */
16895 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16896 sect_offset_str (die
->sect_off
));
16899 else if (attr
->name
== DW_AT_binary_scale
)
16901 LONGEST scale_exp
= attr
->constant_value (0);
16902 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16904 mpz_mul_2exp (num_or_denom
->val
, num_or_denom
->val
, std::abs (scale_exp
));
16906 else if (attr
->name
== DW_AT_decimal_scale
)
16908 LONGEST scale_exp
= attr
->constant_value (0);
16909 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16911 mpz_ui_pow_ui (num_or_denom
->val
, 10, std::abs (scale_exp
));
16913 else if (attr
->name
== DW_AT_small
)
16915 struct die_info
*scale_die
;
16916 struct dwarf2_cu
*scale_cu
= cu
;
16918 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
16919 if (scale_die
->tag
== DW_TAG_constant
)
16920 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
16921 &scale_num
, &scale_denom
);
16923 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16925 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16929 complaint (_("unsupported scale attribute %s for fixed-point type"
16931 dwarf_attr_name (attr
->name
),
16932 sect_offset_str (die
->sect_off
));
16935 gdb_mpq
&scaling_factor
= type
->fixed_point_info ().scaling_factor
;
16936 mpz_set (mpq_numref (scaling_factor
.val
), scale_num
.val
);
16937 mpz_set (mpq_denref (scaling_factor
.val
), scale_denom
.val
);
16938 mpq_canonicalize (scaling_factor
.val
);
16941 /* The gnat-encoding suffix for fixed point. */
16943 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16945 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16946 "XF" suffix of the name. The text after this is what encodes the
16947 'small and 'delta information. Otherwise, return nullptr. */
16949 static const char *
16950 gnat_encoded_fixed_point_type_info (const char *name
)
16952 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
16955 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16956 (which may be different from NAME) to the architecture back-end to allow
16957 it to guess the correct format if necessary. */
16959 static struct type
*
16960 dwarf2_init_float_type (struct objfile
*objfile
, int bits
, const char *name
,
16961 const char *name_hint
, enum bfd_endian byte_order
)
16963 struct gdbarch
*gdbarch
= objfile
->arch ();
16964 const struct floatformat
**format
;
16967 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
16969 type
= init_float_type (objfile
, bits
, name
, format
, byte_order
);
16971 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
16976 /* Allocate an integer type of size BITS and name NAME. */
16978 static struct type
*
16979 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, struct objfile
*objfile
,
16980 int bits
, int unsigned_p
, const char *name
)
16984 /* Versions of Intel's C Compiler generate an integer type called "void"
16985 instead of using DW_TAG_unspecified_type. This has been seen on
16986 at least versions 14, 17, and 18. */
16987 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
16988 && strcmp (name
, "void") == 0)
16989 type
= objfile_type (objfile
)->builtin_void
;
16991 type
= init_integer_type (objfile
, bits
, unsigned_p
, name
);
16996 /* Return true if DIE has a DW_AT_small attribute whose value is
16997 a constant rational, where both the numerator and denominator
17000 CU is the DIE's Compilation Unit. */
17003 has_zero_over_zero_small_attribute (struct die_info
*die
,
17004 struct dwarf2_cu
*cu
)
17006 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
17007 if (attr
== nullptr)
17010 struct dwarf2_cu
*scale_cu
= cu
;
17011 struct die_info
*scale_die
17012 = follow_die_ref (die
, attr
, &scale_cu
);
17014 if (scale_die
->tag
!= DW_TAG_constant
)
17017 gdb_mpz
num (1), denom (1);
17018 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
17019 return mpz_sgn (num
.val
) == 0 && mpz_sgn (denom
.val
) == 0;
17022 /* Initialise and return a floating point type of size BITS suitable for
17023 use as a component of a complex number. The NAME_HINT is passed through
17024 when initialising the floating point type and is the name of the complex
17027 As DWARF doesn't currently provide an explicit name for the components
17028 of a complex number, but it can be helpful to have these components
17029 named, we try to select a suitable name based on the size of the
17031 static struct type
*
17032 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
17033 struct objfile
*objfile
,
17034 int bits
, const char *name_hint
,
17035 enum bfd_endian byte_order
)
17037 gdbarch
*gdbarch
= objfile
->arch ();
17038 struct type
*tt
= nullptr;
17040 /* Try to find a suitable floating point builtin type of size BITS.
17041 We're going to use the name of this type as the name for the complex
17042 target type that we are about to create. */
17043 switch (cu
->per_cu
->lang
)
17045 case language_fortran
:
17049 tt
= builtin_f_type (gdbarch
)->builtin_real
;
17052 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
17054 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17056 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
17064 tt
= builtin_type (gdbarch
)->builtin_float
;
17067 tt
= builtin_type (gdbarch
)->builtin_double
;
17069 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17071 tt
= builtin_type (gdbarch
)->builtin_long_double
;
17077 /* If the type we found doesn't match the size we were looking for, then
17078 pretend we didn't find a type at all, the complex target type we
17079 create will then be nameless. */
17080 if (tt
!= nullptr && TYPE_LENGTH (tt
) * TARGET_CHAR_BIT
!= bits
)
17083 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
17084 return dwarf2_init_float_type (objfile
, bits
, name
, name_hint
, byte_order
);
17087 /* Find a representation of a given base type and install
17088 it in the TYPE field of the die. */
17090 static struct type
*
17091 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17093 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17095 struct attribute
*attr
;
17096 int encoding
= 0, bits
= 0;
17100 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
17101 if (attr
!= nullptr && attr
->form_is_constant ())
17102 encoding
= attr
->constant_value (0);
17103 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17104 if (attr
!= nullptr)
17105 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
17106 name
= dwarf2_name (die
, cu
);
17108 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17110 arch
= objfile
->arch ();
17111 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
17113 attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
17114 if (attr
!= nullptr && attr
->form_is_constant ())
17116 int endianity
= attr
->constant_value (0);
17121 byte_order
= BFD_ENDIAN_BIG
;
17123 case DW_END_little
:
17124 byte_order
= BFD_ENDIAN_LITTLE
;
17127 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
17132 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
17133 && cu
->per_cu
->lang
== language_ada
17134 && has_zero_over_zero_small_attribute (die
, cu
))
17136 /* brobecker/2018-02-24: This is a fixed point type for which
17137 the scaling factor is represented as fraction whose value
17138 does not make sense (zero divided by zero), so we should
17139 normally never see these. However, there is a small category
17140 of fixed point types for which GNAT is unable to provide
17141 the scaling factor via the standard DWARF mechanisms, and
17142 for which the info is provided via the GNAT encodings instead.
17143 This is likely what this DIE is about. */
17144 encoding
= (encoding
== DW_ATE_signed_fixed
17146 : DW_ATE_unsigned
);
17149 /* With GNAT encodings, fixed-point information will be encoded in
17150 the type name. Note that this can also occur with the above
17151 zero-over-zero case, which is why this is a separate "if" rather
17152 than an "else if". */
17153 const char *gnat_encoding_suffix
= nullptr;
17154 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
17155 && cu
->per_cu
->lang
== language_ada
17156 && name
!= nullptr)
17158 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
17159 if (gnat_encoding_suffix
!= nullptr)
17161 gdb_assert (startswith (gnat_encoding_suffix
,
17162 GNAT_FIXED_POINT_SUFFIX
));
17163 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
17164 name
, gnat_encoding_suffix
- name
);
17165 /* Use -1 here so that SUFFIX points at the "_" after the
17167 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
17169 encoding
= (encoding
== DW_ATE_signed
17170 ? DW_ATE_signed_fixed
17171 : DW_ATE_unsigned_fixed
);
17177 case DW_ATE_address
:
17178 /* Turn DW_ATE_address into a void * pointer. */
17179 type
= init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
17180 type
= init_pointer_type (objfile
, bits
, name
, type
);
17182 case DW_ATE_boolean
:
17183 type
= init_boolean_type (objfile
, bits
, 1, name
);
17185 case DW_ATE_complex_float
:
17186 type
= dwarf2_init_complex_target_type (cu
, objfile
, bits
/ 2, name
,
17188 if (type
->code () == TYPE_CODE_ERROR
)
17190 if (name
== nullptr)
17192 struct obstack
*obstack
17193 = &cu
->per_objfile
->objfile
->objfile_obstack
;
17194 name
= obconcat (obstack
, "_Complex ", type
->name (),
17197 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17200 type
= init_complex_type (name
, type
);
17202 case DW_ATE_decimal_float
:
17203 type
= init_decfloat_type (objfile
, bits
, name
);
17206 type
= dwarf2_init_float_type (objfile
, bits
, name
, name
, byte_order
);
17208 case DW_ATE_signed
:
17209 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17211 case DW_ATE_unsigned
:
17212 if (cu
->per_cu
->lang
== language_fortran
17214 && startswith (name
, "character("))
17215 type
= init_character_type (objfile
, bits
, 1, name
);
17217 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17219 case DW_ATE_signed_char
:
17220 if (cu
->per_cu
->lang
== language_ada
17221 || cu
->per_cu
->lang
== language_m2
17222 || cu
->per_cu
->lang
== language_pascal
17223 || cu
->per_cu
->lang
== language_fortran
)
17224 type
= init_character_type (objfile
, bits
, 0, name
);
17226 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17228 case DW_ATE_unsigned_char
:
17229 if (cu
->per_cu
->lang
== language_ada
17230 || cu
->per_cu
->lang
== language_m2
17231 || cu
->per_cu
->lang
== language_pascal
17232 || cu
->per_cu
->lang
== language_fortran
17233 || cu
->per_cu
->lang
== language_rust
)
17234 type
= init_character_type (objfile
, bits
, 1, name
);
17236 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17240 type
= init_character_type (objfile
, bits
, 1, name
);
17241 return set_die_type (die
, type
, cu
);
17244 case DW_ATE_signed_fixed
:
17245 type
= init_fixed_point_type (objfile
, bits
, 0, name
);
17246 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17248 case DW_ATE_unsigned_fixed
:
17249 type
= init_fixed_point_type (objfile
, bits
, 1, name
);
17250 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17254 complaint (_("unsupported DW_AT_encoding: '%s'"),
17255 dwarf_type_encoding_name (encoding
));
17256 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17260 if (type
->code () == TYPE_CODE_INT
17262 && strcmp (name
, "char") == 0)
17263 type
->set_has_no_signedness (true);
17265 maybe_set_alignment (cu
, die
, type
);
17267 type
->set_endianity_is_not_default (gdbarch_byte_order (arch
) != byte_order
);
17269 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
17271 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
17272 if (attr
!= nullptr && attr
->as_unsigned () <= 8 * TYPE_LENGTH (type
))
17274 unsigned real_bit_size
= attr
->as_unsigned ();
17275 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
17276 /* Only use the attributes if they make sense together. */
17277 if (attr
== nullptr
17278 || (attr
->as_unsigned () + real_bit_size
17279 <= 8 * TYPE_LENGTH (type
)))
17281 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
17283 if (attr
!= nullptr)
17284 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
17285 = attr
->as_unsigned ();
17290 return set_die_type (die
, type
, cu
);
17293 /* A helper function that returns the name of DIE, if it refers to a
17294 variable declaration. */
17296 static const char *
17297 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17299 if (die
->tag
!= DW_TAG_variable
)
17302 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
17303 if (attr
== nullptr || !attr
->as_boolean ())
17306 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
17307 if (attr
== nullptr)
17309 return attr
->as_string ();
17312 /* Parse dwarf attribute if it's a block, reference or constant and put the
17313 resulting value of the attribute into struct bound_prop.
17314 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17317 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
17318 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
17319 struct type
*default_type
)
17321 struct dwarf2_property_baton
*baton
;
17322 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17323 struct objfile
*objfile
= per_objfile
->objfile
;
17324 struct obstack
*obstack
= &objfile
->objfile_obstack
;
17326 gdb_assert (default_type
!= NULL
);
17328 if (attr
== NULL
|| prop
== NULL
)
17331 if (attr
->form_is_block ())
17333 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17334 baton
->property_type
= default_type
;
17335 baton
->locexpr
.per_cu
= cu
->per_cu
;
17336 baton
->locexpr
.per_objfile
= per_objfile
;
17338 struct dwarf_block
*block
;
17339 if (attr
->form
== DW_FORM_data16
)
17341 size_t data_size
= 16;
17342 block
= XOBNEW (obstack
, struct dwarf_block
);
17343 block
->size
= (data_size
17344 + 2 /* Extra bytes for DW_OP and arg. */);
17345 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
->size
);
17346 data
[0] = DW_OP_implicit_value
;
17347 data
[1] = data_size
;
17348 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
17349 block
->data
= data
;
17352 block
= attr
->as_block ();
17354 baton
->locexpr
.size
= block
->size
;
17355 baton
->locexpr
.data
= block
->data
;
17356 switch (attr
->name
)
17358 case DW_AT_string_length
:
17359 baton
->locexpr
.is_reference
= true;
17362 baton
->locexpr
.is_reference
= false;
17366 prop
->set_locexpr (baton
);
17367 gdb_assert (prop
->baton () != NULL
);
17369 else if (attr
->form_is_ref ())
17371 struct dwarf2_cu
*target_cu
= cu
;
17372 struct die_info
*target_die
;
17373 struct attribute
*target_attr
;
17375 target_die
= follow_die_ref (die
, attr
, &target_cu
);
17376 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
17377 if (target_attr
== NULL
)
17378 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
17380 if (target_attr
== nullptr)
17381 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
17383 if (target_attr
== NULL
)
17385 const char *name
= var_decl_name (target_die
, target_cu
);
17386 if (name
!= nullptr)
17388 prop
->set_variable_name (name
);
17394 switch (target_attr
->name
)
17396 case DW_AT_location
:
17397 if (target_attr
->form_is_section_offset ())
17399 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17400 baton
->property_type
= die_type (target_die
, target_cu
);
17401 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
17402 prop
->set_loclist (baton
);
17403 gdb_assert (prop
->baton () != NULL
);
17405 else if (target_attr
->form_is_block ())
17407 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17408 baton
->property_type
= die_type (target_die
, target_cu
);
17409 baton
->locexpr
.per_cu
= cu
->per_cu
;
17410 baton
->locexpr
.per_objfile
= per_objfile
;
17411 struct dwarf_block
*block
= target_attr
->as_block ();
17412 baton
->locexpr
.size
= block
->size
;
17413 baton
->locexpr
.data
= block
->data
;
17414 baton
->locexpr
.is_reference
= true;
17415 prop
->set_locexpr (baton
);
17416 gdb_assert (prop
->baton () != NULL
);
17420 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17421 "dynamic property");
17425 case DW_AT_data_member_location
:
17426 case DW_AT_data_bit_offset
:
17430 if (!handle_member_location (target_die
, target_cu
, &offset
))
17433 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17434 baton
->property_type
= read_type_die (target_die
->parent
,
17436 baton
->offset_info
.offset
= offset
;
17437 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
17438 prop
->set_addr_offset (baton
);
17443 else if (attr
->form_is_constant ())
17444 prop
->set_const_val (attr
->constant_value (0));
17445 else if (attr
->form_is_section_offset ())
17447 switch (attr
->name
)
17449 case DW_AT_string_length
:
17450 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17451 baton
->property_type
= default_type
;
17452 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
17453 prop
->set_loclist (baton
);
17454 gdb_assert (prop
->baton () != NULL
);
17466 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
17467 dwarf2_name (die
, cu
));
17473 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17474 present (which is valid) then compute the default type based on the
17475 compilation units address size. */
17477 static struct type
*
17478 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17480 struct type
*index_type
= die_type (die
, cu
);
17482 /* Dwarf-2 specifications explicitly allows to create subrange types
17483 without specifying a base type.
17484 In that case, the base type must be set to the type of
17485 the lower bound, upper bound or count, in that order, if any of these
17486 three attributes references an object that has a type.
17487 If no base type is found, the Dwarf-2 specifications say that
17488 a signed integer type of size equal to the size of an address should
17490 For the following C code: `extern char gdb_int [];'
17491 GCC produces an empty range DIE.
17492 FIXME: muller/2010-05-28: Possible references to object for low bound,
17493 high bound or count are not yet handled by this code. */
17494 if (index_type
->code () == TYPE_CODE_VOID
)
17495 index_type
= cu
->addr_sized_int_type (false);
17500 /* Read the given DW_AT_subrange DIE. */
17502 static struct type
*
17503 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17505 struct type
*base_type
, *orig_base_type
;
17506 struct type
*range_type
;
17507 struct attribute
*attr
;
17508 struct dynamic_prop low
, high
;
17509 int low_default_is_valid
;
17510 int high_bound_is_count
= 0;
17512 ULONGEST negative_mask
;
17514 orig_base_type
= read_subrange_index_type (die
, cu
);
17516 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17517 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17518 creating the range type, but we use the result of check_typedef
17519 when examining properties of the type. */
17520 base_type
= check_typedef (orig_base_type
);
17522 /* The die_type call above may have already set the type for this DIE. */
17523 range_type
= get_die_type (die
, cu
);
17527 high
.set_const_val (0);
17529 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17530 omitting DW_AT_lower_bound. */
17531 switch (cu
->per_cu
->lang
)
17534 case language_cplus
:
17535 low
.set_const_val (0);
17536 low_default_is_valid
= 1;
17538 case language_fortran
:
17539 low
.set_const_val (1);
17540 low_default_is_valid
= 1;
17543 case language_objc
:
17544 case language_rust
:
17545 low
.set_const_val (0);
17546 low_default_is_valid
= (cu
->header
.version
>= 4);
17550 case language_pascal
:
17551 low
.set_const_val (1);
17552 low_default_is_valid
= (cu
->header
.version
>= 4);
17555 low
.set_const_val (0);
17556 low_default_is_valid
= 0;
17560 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
17561 if (attr
!= nullptr)
17562 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
17563 else if (!low_default_is_valid
)
17564 complaint (_("Missing DW_AT_lower_bound "
17565 "- DIE at %s [in module %s]"),
17566 sect_offset_str (die
->sect_off
),
17567 objfile_name (cu
->per_objfile
->objfile
));
17569 struct attribute
*attr_ub
, *attr_count
;
17570 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
17571 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17573 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
17574 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17576 /* If bounds are constant do the final calculation here. */
17577 if (low
.kind () == PROP_CONST
&& high
.kind () == PROP_CONST
)
17578 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
17580 high_bound_is_count
= 1;
17584 if (attr_ub
!= NULL
)
17585 complaint (_("Unresolved DW_AT_upper_bound "
17586 "- DIE at %s [in module %s]"),
17587 sect_offset_str (die
->sect_off
),
17588 objfile_name (cu
->per_objfile
->objfile
));
17589 if (attr_count
!= NULL
)
17590 complaint (_("Unresolved DW_AT_count "
17591 "- DIE at %s [in module %s]"),
17592 sect_offset_str (die
->sect_off
),
17593 objfile_name (cu
->per_objfile
->objfile
));
17598 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
17599 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
17600 bias
= bias_attr
->constant_value (0);
17602 /* Normally, the DWARF producers are expected to use a signed
17603 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17604 But this is unfortunately not always the case, as witnessed
17605 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17606 is used instead. To work around that ambiguity, we treat
17607 the bounds as signed, and thus sign-extend their values, when
17608 the base type is signed. */
17610 -((ULONGEST
) 1 << (TYPE_LENGTH (base_type
) * TARGET_CHAR_BIT
- 1));
17611 if (low
.kind () == PROP_CONST
17612 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
17613 low
.set_const_val (low
.const_val () | negative_mask
);
17614 if (high
.kind () == PROP_CONST
17615 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
17616 high
.set_const_val (high
.const_val () | negative_mask
);
17618 /* Check for bit and byte strides. */
17619 struct dynamic_prop byte_stride_prop
;
17620 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
17621 if (attr_byte_stride
!= nullptr)
17623 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17624 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
17628 struct dynamic_prop bit_stride_prop
;
17629 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
17630 if (attr_bit_stride
!= nullptr)
17632 /* It only makes sense to have either a bit or byte stride. */
17633 if (attr_byte_stride
!= nullptr)
17635 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17636 "- DIE at %s [in module %s]"),
17637 sect_offset_str (die
->sect_off
),
17638 objfile_name (cu
->per_objfile
->objfile
));
17639 attr_bit_stride
= nullptr;
17643 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17644 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
17649 if (attr_byte_stride
!= nullptr
17650 || attr_bit_stride
!= nullptr)
17652 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
17653 struct dynamic_prop
*stride
17654 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
17657 = create_range_type_with_stride (NULL
, orig_base_type
, &low
,
17658 &high
, bias
, stride
, byte_stride_p
);
17661 range_type
= create_range_type (NULL
, orig_base_type
, &low
, &high
, bias
);
17663 if (high_bound_is_count
)
17664 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
17666 /* Ada expects an empty array on no boundary attributes. */
17667 if (attr
== NULL
&& cu
->per_cu
->lang
!= language_ada
)
17668 range_type
->bounds ()->high
.set_undefined ();
17670 name
= dwarf2_name (die
, cu
);
17672 range_type
->set_name (name
);
17674 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17675 if (attr
!= nullptr)
17676 TYPE_LENGTH (range_type
) = attr
->constant_value (0);
17678 maybe_set_alignment (cu
, die
, range_type
);
17680 set_die_type (die
, range_type
, cu
);
17682 /* set_die_type should be already done. */
17683 set_descriptive_type (range_type
, die
, cu
);
17688 static struct type
*
17689 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17693 type
= init_type (cu
->per_objfile
->objfile
, TYPE_CODE_VOID
, 0, NULL
);
17694 type
->set_name (dwarf2_name (die
, cu
));
17696 /* In Ada, an unspecified type is typically used when the description
17697 of the type is deferred to a different unit. When encountering
17698 such a type, we treat it as a stub, and try to resolve it later on,
17700 if (cu
->per_cu
->lang
== language_ada
)
17701 type
->set_is_stub (true);
17703 return set_die_type (die
, type
, cu
);
17706 /* Read a single die and all its descendents. Set the die's sibling
17707 field to NULL; set other fields in the die correctly, and set all
17708 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17709 location of the info_ptr after reading all of those dies. PARENT
17710 is the parent of the die in question. */
17712 static struct die_info
*
17713 read_die_and_children (const struct die_reader_specs
*reader
,
17714 const gdb_byte
*info_ptr
,
17715 const gdb_byte
**new_info_ptr
,
17716 struct die_info
*parent
)
17718 struct die_info
*die
;
17719 const gdb_byte
*cur_ptr
;
17721 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0);
17724 *new_info_ptr
= cur_ptr
;
17727 store_in_ref_table (die
, reader
->cu
);
17729 if (die
->has_children
)
17730 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
17734 *new_info_ptr
= cur_ptr
;
17737 die
->sibling
= NULL
;
17738 die
->parent
= parent
;
17742 /* Read a die, all of its descendents, and all of its siblings; set
17743 all of the fields of all of the dies correctly. Arguments are as
17744 in read_die_and_children. */
17746 static struct die_info
*
17747 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
17748 const gdb_byte
*info_ptr
,
17749 const gdb_byte
**new_info_ptr
,
17750 struct die_info
*parent
)
17752 struct die_info
*first_die
, *last_sibling
;
17753 const gdb_byte
*cur_ptr
;
17755 cur_ptr
= info_ptr
;
17756 first_die
= last_sibling
= NULL
;
17760 struct die_info
*die
17761 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
17765 *new_info_ptr
= cur_ptr
;
17772 last_sibling
->sibling
= die
;
17774 last_sibling
= die
;
17778 /* Read a die, all of its descendents, and all of its siblings; set
17779 all of the fields of all of the dies correctly. Arguments are as
17780 in read_die_and_children.
17781 This the main entry point for reading a DIE and all its children. */
17783 static struct die_info
*
17784 read_die_and_siblings (const struct die_reader_specs
*reader
,
17785 const gdb_byte
*info_ptr
,
17786 const gdb_byte
**new_info_ptr
,
17787 struct die_info
*parent
)
17789 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
17790 new_info_ptr
, parent
);
17792 if (dwarf_die_debug
)
17794 gdb_printf (gdb_stdlog
,
17795 "Read die from %s@0x%x of %s:\n",
17796 reader
->die_section
->get_name (),
17797 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17798 bfd_get_filename (reader
->abfd
));
17799 dump_die (die
, dwarf_die_debug
);
17805 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17807 The caller is responsible for filling in the extra attributes
17808 and updating (*DIEP)->num_attrs.
17809 Set DIEP to point to a newly allocated die with its information,
17810 except for its child, sibling, and parent fields. */
17812 static const gdb_byte
*
17813 read_full_die_1 (const struct die_reader_specs
*reader
,
17814 struct die_info
**diep
, const gdb_byte
*info_ptr
,
17815 int num_extra_attrs
)
17817 unsigned int abbrev_number
, bytes_read
, i
;
17818 const struct abbrev_info
*abbrev
;
17819 struct die_info
*die
;
17820 struct dwarf2_cu
*cu
= reader
->cu
;
17821 bfd
*abfd
= reader
->abfd
;
17823 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
17824 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17825 info_ptr
+= bytes_read
;
17826 if (!abbrev_number
)
17832 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
17834 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17836 bfd_get_filename (abfd
));
17838 die
= dwarf_alloc_die (cu
, abbrev
->num_attrs
+ num_extra_attrs
);
17839 die
->sect_off
= sect_off
;
17840 die
->tag
= abbrev
->tag
;
17841 die
->abbrev
= abbrev_number
;
17842 die
->has_children
= abbrev
->has_children
;
17844 /* Make the result usable.
17845 The caller needs to update num_attrs after adding the extra
17847 die
->num_attrs
= abbrev
->num_attrs
;
17849 bool any_need_reprocess
= false;
17850 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17852 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
17854 if (die
->attrs
[i
].requires_reprocessing_p ())
17855 any_need_reprocess
= true;
17858 struct attribute
*attr
= die
->attr (DW_AT_str_offsets_base
);
17859 if (attr
!= nullptr && attr
->form_is_unsigned ())
17860 cu
->str_offsets_base
= attr
->as_unsigned ();
17862 attr
= die
->attr (DW_AT_loclists_base
);
17863 if (attr
!= nullptr)
17864 cu
->loclist_base
= attr
->as_unsigned ();
17866 auto maybe_addr_base
= die
->addr_base ();
17867 if (maybe_addr_base
.has_value ())
17868 cu
->addr_base
= *maybe_addr_base
;
17870 attr
= die
->attr (DW_AT_rnglists_base
);
17871 if (attr
!= nullptr)
17872 cu
->rnglists_base
= attr
->as_unsigned ();
17874 if (any_need_reprocess
)
17876 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17878 if (die
->attrs
[i
].requires_reprocessing_p ())
17879 read_attribute_reprocess (reader
, &die
->attrs
[i
], die
->tag
);
17886 /* Read a die and all its attributes.
17887 Set DIEP to point to a newly allocated die with its information,
17888 except for its child, sibling, and parent fields. */
17890 static const gdb_byte
*
17891 read_full_die (const struct die_reader_specs
*reader
,
17892 struct die_info
**diep
, const gdb_byte
*info_ptr
)
17894 const gdb_byte
*result
;
17896 result
= read_full_die_1 (reader
, diep
, info_ptr
, 0);
17898 if (dwarf_die_debug
)
17900 gdb_printf (gdb_stdlog
,
17901 "Read die from %s@0x%x of %s:\n",
17902 reader
->die_section
->get_name (),
17903 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17904 bfd_get_filename (reader
->abfd
));
17905 dump_die (*diep
, dwarf_die_debug
);
17913 cooked_indexer::check_bounds (cutu_reader
*reader
)
17915 if (reader
->cu
->per_cu
->addresses_seen
)
17918 dwarf2_cu
*cu
= reader
->cu
;
17920 CORE_ADDR best_lowpc
= 0, best_highpc
= 0;
17921 /* Possibly set the default values of LOWPC and HIGHPC from
17923 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
17924 enum pc_bounds_kind cu_bounds_kind
17925 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
17926 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
17927 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
17929 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17930 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
17931 struct gdbarch
*gdbarch
= objfile
->arch ();
17933 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_lowpc
+ baseaddr
)
17936 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_highpc
+ baseaddr
)
17938 /* Store the contiguous range if it is not empty; it can be
17939 empty for CUs with no code. */
17940 addrmap_set_empty (m_index_storage
->get_addrmap (), low
, high
,
17943 cu
->per_cu
->addresses_seen
= true;
17947 /* Helper function that returns true if TAG can have a linkage
17951 tag_can_have_linkage_name (enum dwarf_tag tag
)
17955 /* We include types here because an anonymous C++ type might
17956 have a name for linkage purposes. */
17957 case DW_TAG_class_type
:
17958 case DW_TAG_structure_type
:
17959 case DW_TAG_union_type
:
17960 case DW_TAG_variable
:
17961 case DW_TAG_subprogram
:
17970 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
17971 dwarf2_per_objfile
*per_objfile
,
17972 sect_offset sect_off
, bool is_dwz
,
17975 /* Lookups for type unit references are always in the CU, and
17976 cross-CU references will crash. */
17977 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
17978 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
17981 dwarf2_per_cu_data
*per_cu
17982 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
17983 per_objfile
->per_bfd
);
17985 /* When scanning, we only want to visit a given CU a single time.
17986 Doing this check here avoids self-imports as well. */
17990 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
17993 if (per_cu
== m_per_cu
)
17996 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
17997 if (result
== nullptr)
17999 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
18000 m_index_storage
->get_abbrev_cache ());
18002 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
18004 std::unique_ptr
<cutu_reader
> copy
18005 (new cutu_reader (std::move (new_reader
)));
18006 result
= m_index_storage
->preserve (std::move (copy
));
18009 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
18013 check_bounds (result
);
18019 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
18020 cutu_reader
*reader
,
18021 const gdb_byte
*watermark_ptr
,
18022 const gdb_byte
*info_ptr
,
18023 const abbrev_info
*abbrev
,
18025 const char **linkage_name
,
18026 cooked_index_flag
*flags
,
18027 sect_offset
*sibling_offset
,
18028 const cooked_index_entry
**parent_entry
,
18029 CORE_ADDR
*maybe_defer
,
18030 bool for_specification
)
18032 bool origin_is_dwz
= false;
18033 bool is_declaration
= false;
18034 sect_offset origin_offset
{};
18036 gdb::optional
<CORE_ADDR
> low_pc
;
18037 gdb::optional
<CORE_ADDR
> high_pc
;
18038 bool high_pc_relative
= false;
18040 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18043 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18044 if (attr
.requires_reprocessing_p ())
18045 read_attribute_reprocess (reader
, &attr
, abbrev
->tag
);
18047 /* Store the data if it is of an attribute we want to keep in a
18048 partial symbol table. */
18052 switch (abbrev
->tag
)
18054 case DW_TAG_compile_unit
:
18055 case DW_TAG_partial_unit
:
18056 case DW_TAG_type_unit
:
18057 /* Compilation units have a DW_AT_name that is a filename, not
18058 a source language identifier. */
18062 if (*name
== nullptr)
18063 *name
= attr
.as_string ();
18068 case DW_AT_linkage_name
:
18069 case DW_AT_MIPS_linkage_name
:
18070 /* Note that both forms of linkage name might appear. We
18071 assume they will be the same, and we only store the last
18073 if (*linkage_name
== nullptr)
18074 *linkage_name
= attr
.as_string ();
18077 case DW_AT_main_subprogram
:
18078 if (attr
.as_boolean ())
18082 case DW_AT_declaration
:
18083 is_declaration
= attr
.as_boolean ();
18086 case DW_AT_sibling
:
18087 if (sibling_offset
!= nullptr)
18088 *sibling_offset
= attr
.get_ref_die_offset ();
18091 case DW_AT_specification
:
18092 case DW_AT_abstract_origin
:
18093 case DW_AT_extension
:
18094 origin_offset
= attr
.get_ref_die_offset ();
18095 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
18098 case DW_AT_external
:
18099 if (attr
.as_boolean ())
18100 *flags
&= ~IS_STATIC
;
18103 case DW_AT_enum_class
:
18104 if (attr
.as_boolean ())
18105 *flags
|= IS_ENUM_CLASS
;
18109 low_pc
= attr
.as_address ();
18112 case DW_AT_high_pc
:
18113 high_pc
= attr
.as_address ();
18114 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
18115 high_pc_relative
= true;
18118 case DW_AT_location
:
18119 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
18121 struct dwarf_block
*locdesc
= attr
.as_block ();
18122 CORE_ADDR addr
= decode_locdesc (locdesc
, reader
->cu
);
18124 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
)
18127 /* For variables, we don't want to try decoding the
18128 type just to find the size -- for gdb's purposes
18129 we only need the address of a variable. */
18130 high_pc
= addr
+ 1;
18131 high_pc_relative
= false;
18137 if (!scanning_per_cu
->addresses_seen
)
18139 /* Offset in the .debug_ranges or .debug_rnglist section
18140 (depending on DWARF version). */
18141 ULONGEST ranges_offset
= attr
.as_unsigned ();
18143 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18144 want to add this value. */
18145 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
18147 CORE_ADDR lowpc
, highpc
;
18148 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
18149 m_index_storage
->get_addrmap (),
18150 scanning_per_cu
, abbrev
->tag
);
18156 /* We don't want to examine declarations, but if we found a
18157 declaration when handling DW_AT_specification or the like, then
18158 that is ok. Similarly, we allow an external variable without a
18159 location; those are resolved via minimal symbols. */
18160 if (is_declaration
&& !for_specification
18161 && (abbrev
->tag
!= DW_TAG_variable
18162 || (*flags
& IS_STATIC
) != 0))
18164 *linkage_name
= nullptr;
18167 else if ((*name
== nullptr
18168 || (*linkage_name
== nullptr
18169 && tag_can_have_linkage_name (abbrev
->tag
))
18170 || (*parent_entry
== nullptr && m_language
!= language_c
))
18171 && origin_offset
!= sect_offset (0))
18173 cutu_reader
*new_reader
18174 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
18175 origin_is_dwz
, false);
18176 if (new_reader
!= nullptr)
18178 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
18179 + to_underlying (origin_offset
));
18181 if (new_reader
->cu
== reader
->cu
18182 && new_info_ptr
> watermark_ptr
18183 && maybe_defer
!= nullptr
18184 && *parent_entry
== nullptr)
18185 *maybe_defer
= form_addr (origin_offset
, origin_is_dwz
);
18186 else if (*parent_entry
== nullptr)
18188 CORE_ADDR lookup
= form_addr (origin_offset
, origin_is_dwz
);
18190 = (cooked_index_entry
*) addrmap_find (m_die_range_map
,
18194 unsigned int bytes_read
;
18195 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
18198 new_info_ptr
+= bytes_read
;
18199 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
, new_info_ptr
,
18200 new_abbrev
, name
, linkage_name
, flags
, nullptr,
18201 parent_entry
, maybe_defer
, true);
18205 if (!for_specification
)
18207 if (m_language
== language_ada
18208 && *linkage_name
== nullptr)
18209 *linkage_name
= *name
;
18211 if (!scanning_per_cu
->addresses_seen
18212 && low_pc
.has_value ()
18213 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
18215 && high_pc
.has_value ())
18217 if (high_pc_relative
)
18218 high_pc
= *high_pc
+ *low_pc
;
18220 if (*high_pc
> *low_pc
)
18222 struct objfile
*objfile
= reader
->cu
->per_objfile
->objfile
;
18223 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18224 struct gdbarch
*gdbarch
= objfile
->arch ();
18226 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *low_pc
+ baseaddr
)
18229 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *high_pc
+ baseaddr
)
18231 addrmap_set_empty (m_index_storage
->get_addrmap (), lo
, hi
- 1,
18236 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
18237 *flags
&= ~IS_STATIC
;
18239 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
18240 *name
= "(anonymous namespace)";
18242 if (m_language
== language_cplus
18243 && (abbrev
->tag
== DW_TAG_class_type
18244 || abbrev
->tag
== DW_TAG_interface_type
18245 || abbrev
->tag
== DW_TAG_structure_type
18246 || abbrev
->tag
== DW_TAG_union_type
18247 || abbrev
->tag
== DW_TAG_enumeration_type
18248 || abbrev
->tag
== DW_TAG_enumerator
))
18249 *flags
&= ~IS_STATIC
;
18256 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
18257 const gdb_byte
*info_ptr
,
18258 const abbrev_info
*abbrev
)
18260 sect_offset sect_off
{};
18261 bool is_dwz
= false;
18263 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18265 /* Note that we never need to reprocess attributes here. */
18267 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18269 if (attr
.name
== DW_AT_import
)
18271 sect_off
= attr
.get_ref_die_offset ();
18272 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
18273 || reader
->cu
->per_cu
->is_dwz
);
18277 /* Did not find DW_AT_import. */
18278 if (sect_off
== sect_offset (0))
18281 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
18282 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
18284 if (new_reader
!= nullptr)
18286 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
18288 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
18295 cooked_indexer::recurse (cutu_reader
*reader
,
18296 const gdb_byte
*info_ptr
,
18297 const cooked_index_entry
*parent_entry
,
18300 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18302 if (parent_entry
!= nullptr)
18304 CORE_ADDR start
= form_addr (parent_entry
->die_offset
,
18305 reader
->cu
->per_cu
->is_dwz
);
18306 CORE_ADDR end
= form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
18307 reader
->cu
->per_cu
->is_dwz
);
18308 addrmap_set_empty (m_die_range_map
, start
, end
, (void *) parent_entry
);
18315 cooked_indexer::index_dies (cutu_reader
*reader
,
18316 const gdb_byte
*info_ptr
,
18317 const cooked_index_entry
*parent_entry
,
18320 const gdb_byte
*end_ptr
= info_ptr
+ reader
->cu
->header
.get_length ();
18322 while (info_ptr
< end_ptr
)
18324 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
18325 unsigned int bytes_read
;
18326 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
18328 info_ptr
+= bytes_read
;
18329 if (abbrev
== nullptr)
18332 if (abbrev
->tag
== DW_TAG_imported_unit
)
18334 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
18338 if (!abbrev
->interesting
)
18340 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
18341 if (fully
&& abbrev
->has_children
)
18342 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18346 const char *name
= nullptr;
18347 const char *linkage_name
= nullptr;
18348 CORE_ADDR defer
= 0;
18349 cooked_index_flag flags
= IS_STATIC
;
18350 sect_offset sibling
{};
18351 const cooked_index_entry
*this_parent_entry
= parent_entry
;
18352 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
18353 info_ptr
, abbrev
, &name
, &linkage_name
,
18354 &flags
, &sibling
, &this_parent_entry
,
18357 if (abbrev
->tag
== DW_TAG_namespace
18358 && m_language
== language_cplus
18359 && strcmp (name
, "::") == 0)
18361 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18362 generated bogus DW_TAG_namespace DIEs with a name of "::"
18363 for the global namespace. Work around this problem
18368 const cooked_index_entry
*this_entry
= nullptr;
18369 if (name
!= nullptr)
18372 m_deferred_entries
.push_back ({
18373 this_die
, name
, defer
, abbrev
->tag
, flags
18376 this_entry
= m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18377 name
, this_parent_entry
,
18381 if (linkage_name
!= nullptr)
18383 /* We only want this to be "main" if it has a linkage name
18384 but not an ordinary name. */
18385 if (name
!= nullptr)
18386 flags
= flags
& ~IS_MAIN
;
18387 m_index_storage
->add (this_die
, abbrev
->tag
, flags
| IS_LINKAGE
,
18388 linkage_name
, nullptr, m_per_cu
);
18391 if (abbrev
->has_children
)
18393 switch (abbrev
->tag
)
18395 case DW_TAG_class_type
:
18396 case DW_TAG_interface_type
:
18397 case DW_TAG_structure_type
:
18398 case DW_TAG_union_type
:
18399 if (m_language
!= language_c
&& this_entry
!= nullptr)
18401 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18406 case DW_TAG_enumeration_type
:
18407 /* We need to recurse even for an anonymous enumeration.
18408 Which scope we record as the parent scope depends on
18409 whether we're reading an "enum class". If so, we use
18410 the enum itself as the parent, yielding names like
18411 "enum_class::enumerator"; otherwise we inject the
18412 names into our own parent scope. */
18413 info_ptr
= recurse (reader
, info_ptr
,
18414 ((flags
& IS_ENUM_CLASS
) == 0)
18420 case DW_TAG_module
:
18421 if (this_entry
== nullptr)
18424 case DW_TAG_namespace
:
18425 /* We don't check THIS_ENTRY for a namespace, to handle
18426 the ancient G++ workaround pointed out above. */
18427 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18430 case DW_TAG_subprogram
:
18431 if ((m_language
== language_fortran
18432 || m_language
== language_ada
)
18433 && this_entry
!= nullptr)
18435 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
18441 if (sibling
!= sect_offset (0))
18443 const gdb_byte
*sibling_ptr
18444 = reader
->buffer
+ to_underlying (sibling
);
18446 if (sibling_ptr
< info_ptr
)
18447 complaint (_("DW_AT_sibling points backwards"));
18448 else if (sibling_ptr
> reader
->buffer_end
)
18449 reader
->die_section
->overflow_complaint ();
18451 info_ptr
= sibling_ptr
;
18454 info_ptr
= skip_children (reader
, info_ptr
);
18462 cooked_indexer::make_index (cutu_reader
*reader
)
18464 check_bounds (reader
);
18465 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
18466 if (!reader
->comp_unit_die
->has_children
)
18468 index_dies (reader
, reader
->info_ptr
, nullptr, false);
18470 for (const auto &entry
: m_deferred_entries
)
18472 CORE_ADDR key
= form_addr (entry
.die_offset
, m_per_cu
->is_dwz
);
18473 cooked_index_entry
*parent
18474 = (cooked_index_entry
*) addrmap_find (m_die_range_map
, key
);
18475 m_index_storage
->add (entry
.die_offset
, entry
.tag
, entry
.flags
,
18476 entry
.name
, parent
, m_per_cu
);
18480 /* An implementation of quick_symbol_functions for the cooked DWARF
18483 struct cooked_index_functions
: public dwarf2_base_index_functions
18485 struct compunit_symtab
*find_pc_sect_compunit_symtab
18486 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
18487 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
) override
;
18489 struct compunit_symtab
*find_compunit_symtab_by_address
18490 (struct objfile
*objfile
, CORE_ADDR address
) override
;
18492 void dump (struct objfile
*objfile
) override
18494 gdb_printf ("Cooked index in use\n");
18497 void expand_matching_symbols
18499 const lookup_name_info
&lookup_name
,
18500 domain_enum domain
,
18502 symbol_compare_ftype
*ordered_compare
) override
;
18504 bool expand_symtabs_matching
18505 (struct objfile
*objfile
,
18506 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18507 const lookup_name_info
*lookup_name
,
18508 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18509 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18510 block_search_flags search_flags
,
18511 domain_enum domain
,
18512 enum search_domain kind
) override
;
18514 bool can_lazily_read_symbols () override
18519 void read_partial_symbols (struct objfile
*objfile
) override
18521 if (dwarf2_has_info (objfile
, nullptr))
18522 dwarf2_build_psymtabs (objfile
, true);
18526 struct compunit_symtab
*
18527 cooked_index_functions::find_pc_sect_compunit_symtab
18528 (struct objfile
*objfile
,
18529 struct bound_minimal_symbol msymbol
,
18531 struct obj_section
*section
,
18532 int warn_if_readin
)
18534 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18535 if (per_objfile
->per_bfd
->cooked_index_table
== nullptr)
18538 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18539 dwarf2_per_cu_data
*per_cu
18540 = per_objfile
->per_bfd
->cooked_index_table
->lookup (pc
- baseaddr
);
18541 if (per_cu
== nullptr)
18544 if (warn_if_readin
&& per_objfile
->symtab_set_p (per_cu
))
18545 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
18546 paddress (objfile
->arch (), pc
));
18548 compunit_symtab
*result
= (recursively_find_pc_sect_compunit_symtab
18549 (dw2_instantiate_symtab (per_cu
, per_objfile
,
18552 gdb_assert (result
!= nullptr);
18556 struct compunit_symtab
*
18557 cooked_index_functions::find_compunit_symtab_by_address
18558 (struct objfile
*objfile
, CORE_ADDR address
)
18560 if (objfile
->sect_index_data
== -1)
18563 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18564 if (per_objfile
->per_bfd
->cooked_index_table
== nullptr)
18567 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
18568 dwarf2_per_cu_data
*per_cu
18569 = per_objfile
->per_bfd
->cooked_index_table
->lookup (address
- baseaddr
);
18570 if (per_cu
== nullptr)
18573 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
18577 cooked_index_functions::expand_matching_symbols
18578 (struct objfile
*objfile
,
18579 const lookup_name_info
&lookup_name
,
18580 domain_enum domain
,
18582 symbol_compare_ftype
*ordered_compare
)
18584 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18585 if (per_objfile
->per_bfd
->cooked_index_table
== nullptr)
18587 const block_search_flags search_flags
= (global
18588 ? SEARCH_GLOBAL_BLOCK
18589 : SEARCH_STATIC_BLOCK
);
18590 const language_defn
*lang
= language_def (language_ada
);
18591 symbol_name_matcher_ftype
*name_match
18592 = lang
->get_symbol_name_matcher (lookup_name
);
18594 for (const cooked_index_entry
*entry
18595 : per_objfile
->per_bfd
->cooked_index_table
->all_entries ())
18597 if (entry
->parent_entry
!= nullptr)
18600 if (!entry
->matches (search_flags
)
18601 || !entry
->matches (domain
))
18604 if (name_match (entry
->canonical
, lookup_name
, nullptr))
18605 dw2_instantiate_symtab (entry
->per_cu
, per_objfile
, false);
18610 cooked_index_functions::expand_symtabs_matching
18611 (struct objfile
*objfile
,
18612 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18613 const lookup_name_info
*lookup_name
,
18614 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18615 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18616 block_search_flags search_flags
,
18617 domain_enum domain
,
18618 enum search_domain kind
)
18620 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18621 if (per_objfile
->per_bfd
->cooked_index_table
== nullptr)
18624 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
18626 /* This invariant is documented in quick-functions.h. */
18627 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
18628 if (lookup_name
== nullptr)
18630 for (dwarf2_per_cu_data
*per_cu
18631 : all_comp_units_range (per_objfile
->per_bfd
))
18635 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
18643 lookup_name_info lookup_name_without_params
18644 = lookup_name
->make_ignore_params ();
18645 bool completing
= lookup_name
->completion_mode ();
18647 /* Unique styles of language splitting. */
18648 static const enum language unique_styles
[] =
18650 /* No splitting is also a style. */
18652 /* This includes Rust. */
18654 /* This includes Go. */
18659 for (enum language lang
: unique_styles
)
18661 std::vector
<gdb::string_view
> name_vec
18662 = lookup_name_without_params
.split_name (lang
);
18664 for (const cooked_index_entry
*entry
18665 : per_objfile
->per_bfd
->cooked_index_table
->find (name_vec
.back (),
18668 /* No need to consider symbols from expanded CUs. */
18669 if (per_objfile
->symtab_set_p (entry
->per_cu
))
18672 /* If file-matching was done, we don't need to consider
18673 symbols from unmarked CUs. */
18674 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
18677 /* See if the symbol matches the type filter. */
18678 if (!entry
->matches (search_flags
)
18679 || !entry
->matches (domain
)
18680 || !entry
->matches (kind
))
18683 /* We've found the base name of the symbol; now walk its
18684 parentage chain, ensuring that each component
18688 const cooked_index_entry
*parent
= entry
->parent_entry
;
18689 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
18691 /* If we ran out of entries, or if this segment doesn't
18692 match, this did not match. */
18693 if (parent
== nullptr
18694 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
18695 name_vec
[i
- 1].length ()) != 0)
18701 parent
= parent
->parent_entry
;
18707 /* Might have been looking for "a::b" and found
18709 if (symbol_matcher
== nullptr)
18711 symbol_name_match_type match_type
18712 = lookup_name_without_params
.match_type ();
18713 if ((match_type
== symbol_name_match_type::FULL
18714 || (lang
!= language_ada
18715 && match_type
== symbol_name_match_type::EXPRESSION
))
18716 && parent
!= nullptr)
18721 auto_obstack temp_storage
;
18722 const char *full_name
= entry
->full_name (&temp_storage
);
18723 if (!symbol_matcher (full_name
))
18727 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
18737 /* Return a new cooked_index_functions object. */
18739 static quick_symbol_functions_up
18740 make_cooked_index_funcs ()
18742 return quick_symbol_functions_up (new cooked_index_functions
);
18747 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18748 contents from the given SECTION in the HEADER.
18750 HEADER_OFFSET is the offset of the header in the section. */
18752 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
18753 struct dwarf2_section_info
*section
,
18754 sect_offset header_offset
)
18756 unsigned int bytes_read
;
18757 bfd
*abfd
= section
->get_bfd_owner ();
18758 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
18760 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
18761 info_ptr
+= bytes_read
;
18763 header
->version
= read_2_bytes (abfd
, info_ptr
);
18766 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
18769 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
18772 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
18775 /* Return the DW_AT_loclists_base value for the CU. */
18777 lookup_loclist_base (struct dwarf2_cu
*cu
)
18779 /* For the .dwo unit, the loclist_base points to the first offset following
18780 the header. The header consists of the following entities-
18781 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18783 2. version (2 bytes)
18784 3. address size (1 byte)
18785 4. segment selector size (1 byte)
18786 5. offset entry count (4 bytes)
18787 These sizes are derived as per the DWARFv5 standard. */
18788 if (cu
->dwo_unit
!= nullptr)
18790 if (cu
->header
.initial_length_size
== 4)
18791 return LOCLIST_HEADER_SIZE32
;
18792 return LOCLIST_HEADER_SIZE64
;
18794 return cu
->loclist_base
;
18797 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18798 array of offsets in the .debug_loclists section. */
18801 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
18803 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18804 struct objfile
*objfile
= per_objfile
->objfile
;
18805 bfd
*abfd
= objfile
->obfd
;
18806 ULONGEST loclist_header_size
=
18807 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
18808 : LOCLIST_HEADER_SIZE64
);
18809 ULONGEST loclist_base
= lookup_loclist_base (cu
);
18811 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18812 ULONGEST start_offset
=
18813 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
18815 /* Get loclists section. */
18816 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
18818 /* Read the loclists section content. */
18819 section
->read (objfile
);
18820 if (section
->buffer
== NULL
)
18821 error (_("DW_FORM_loclistx used without .debug_loclists "
18822 "section [in module %s]"), objfile_name (objfile
));
18824 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18825 so if loclist_base is smaller than the header size, we have a problem. */
18826 if (loclist_base
< loclist_header_size
)
18827 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18828 objfile_name (objfile
));
18830 /* Read the header of the loclists contribution. */
18831 struct loclists_rnglists_header header
;
18832 read_loclists_rnglists_header (&header
, section
,
18833 (sect_offset
) (loclist_base
- loclist_header_size
));
18835 /* Verify the loclist index is valid. */
18836 if (loclist_index
>= header
.offset_entry_count
)
18837 error (_("DW_FORM_loclistx pointing outside of "
18838 ".debug_loclists offset array [in module %s]"),
18839 objfile_name (objfile
));
18841 /* Validate that reading won't go beyond the end of the section. */
18842 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18843 error (_("Reading DW_FORM_loclistx index beyond end of"
18844 ".debug_loclists section [in module %s]"),
18845 objfile_name (objfile
));
18847 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18849 if (cu
->header
.offset_size
== 4)
18850 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
18852 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
18855 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18856 array of offsets in the .debug_rnglists section. */
18859 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
18862 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
18863 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
18864 bfd
*abfd
= objfile
->obfd
;
18865 ULONGEST rnglist_header_size
=
18866 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
18867 : RNGLIST_HEADER_SIZE64
);
18869 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18870 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18872 ULONGEST rnglist_base
=
18873 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
18875 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18876 ULONGEST start_offset
=
18877 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
18879 /* Get rnglists section. */
18880 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
18882 /* Read the rnglists section content. */
18883 section
->read (objfile
);
18884 if (section
->buffer
== nullptr)
18885 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18887 objfile_name (objfile
));
18889 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18890 so if rnglist_base is smaller than the header size, we have a problem. */
18891 if (rnglist_base
< rnglist_header_size
)
18892 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18893 objfile_name (objfile
));
18895 /* Read the header of the rnglists contribution. */
18896 struct loclists_rnglists_header header
;
18897 read_loclists_rnglists_header (&header
, section
,
18898 (sect_offset
) (rnglist_base
- rnglist_header_size
));
18900 /* Verify the rnglist index is valid. */
18901 if (rnglist_index
>= header
.offset_entry_count
)
18902 error (_("DW_FORM_rnglistx index pointing outside of "
18903 ".debug_rnglists offset array [in module %s]"),
18904 objfile_name (objfile
));
18906 /* Validate that reading won't go beyond the end of the section. */
18907 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18908 error (_("Reading DW_FORM_rnglistx index beyond end of"
18909 ".debug_rnglists section [in module %s]"),
18910 objfile_name (objfile
));
18912 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18914 if (cu
->header
.offset_size
== 4)
18915 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
18917 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
18920 /* Process the attributes that had to be skipped in the first round. These
18921 attributes are the ones that need str_offsets_base or addr_base attributes.
18922 They could not have been processed in the first round, because at the time
18923 the values of str_offsets_base or addr_base may not have been known. */
18925 read_attribute_reprocess (const struct die_reader_specs
*reader
,
18926 struct attribute
*attr
, dwarf_tag tag
)
18928 struct dwarf2_cu
*cu
= reader
->cu
;
18929 switch (attr
->form
)
18931 case DW_FORM_addrx
:
18932 case DW_FORM_GNU_addr_index
:
18933 attr
->set_address (read_addr_index (cu
,
18934 attr
->as_unsigned_reprocess ()));
18936 case DW_FORM_loclistx
:
18938 sect_offset loclists_sect_off
18939 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
18941 attr
->set_unsigned (to_underlying (loclists_sect_off
));
18944 case DW_FORM_rnglistx
:
18946 sect_offset rnglists_sect_off
18947 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
18949 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
18953 case DW_FORM_strx1
:
18954 case DW_FORM_strx2
:
18955 case DW_FORM_strx3
:
18956 case DW_FORM_strx4
:
18957 case DW_FORM_GNU_str_index
:
18959 unsigned int str_index
= attr
->as_unsigned_reprocess ();
18960 gdb_assert (!attr
->canonical_string_p ());
18961 if (reader
->dwo_file
!= NULL
)
18962 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
18965 attr
->set_string_noncanonical (read_stub_str_index (cu
,
18970 gdb_assert_not_reached ("Unexpected DWARF form.");
18974 /* Read an attribute value described by an attribute form. */
18976 static const gdb_byte
*
18977 read_attribute_value (const struct die_reader_specs
*reader
,
18978 struct attribute
*attr
, unsigned form
,
18979 LONGEST implicit_const
, const gdb_byte
*info_ptr
)
18981 struct dwarf2_cu
*cu
= reader
->cu
;
18982 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18983 struct objfile
*objfile
= per_objfile
->objfile
;
18984 bfd
*abfd
= reader
->abfd
;
18985 struct comp_unit_head
*cu_header
= &cu
->header
;
18986 unsigned int bytes_read
;
18987 struct dwarf_block
*blk
;
18989 attr
->form
= (enum dwarf_form
) form
;
18992 case DW_FORM_ref_addr
:
18993 if (cu_header
->version
== 2)
18994 attr
->set_unsigned (cu_header
->read_address (abfd
, info_ptr
,
18997 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18999 info_ptr
+= bytes_read
;
19001 case DW_FORM_GNU_ref_alt
:
19002 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19004 info_ptr
+= bytes_read
;
19008 struct gdbarch
*gdbarch
= objfile
->arch ();
19009 CORE_ADDR addr
= cu_header
->read_address (abfd
, info_ptr
, &bytes_read
);
19010 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
19011 attr
->set_address (addr
);
19012 info_ptr
+= bytes_read
;
19015 case DW_FORM_block2
:
19016 blk
= dwarf_alloc_block (cu
);
19017 blk
->size
= read_2_bytes (abfd
, info_ptr
);
19019 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19020 info_ptr
+= blk
->size
;
19021 attr
->set_block (blk
);
19023 case DW_FORM_block4
:
19024 blk
= dwarf_alloc_block (cu
);
19025 blk
->size
= read_4_bytes (abfd
, info_ptr
);
19027 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19028 info_ptr
+= blk
->size
;
19029 attr
->set_block (blk
);
19031 case DW_FORM_data2
:
19032 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
19035 case DW_FORM_data4
:
19036 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
19039 case DW_FORM_data8
:
19040 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
19043 case DW_FORM_data16
:
19044 blk
= dwarf_alloc_block (cu
);
19046 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
19048 attr
->set_block (blk
);
19050 case DW_FORM_sec_offset
:
19051 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19053 info_ptr
+= bytes_read
;
19055 case DW_FORM_loclistx
:
19057 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19059 info_ptr
+= bytes_read
;
19062 case DW_FORM_string
:
19063 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
19065 info_ptr
+= bytes_read
;
19068 if (!cu
->per_cu
->is_dwz
)
19070 attr
->set_string_noncanonical
19071 (read_indirect_string (per_objfile
,
19072 abfd
, info_ptr
, cu_header
,
19074 info_ptr
+= bytes_read
;
19078 case DW_FORM_line_strp
:
19079 if (!cu
->per_cu
->is_dwz
)
19081 attr
->set_string_noncanonical
19082 (per_objfile
->read_line_string (info_ptr
, cu_header
,
19084 info_ptr
+= bytes_read
;
19088 case DW_FORM_GNU_strp_alt
:
19090 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19091 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
19094 attr
->set_string_noncanonical
19095 (dwz
->read_string (objfile
, str_offset
));
19096 info_ptr
+= bytes_read
;
19099 case DW_FORM_exprloc
:
19100 case DW_FORM_block
:
19101 blk
= dwarf_alloc_block (cu
);
19102 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19103 info_ptr
+= bytes_read
;
19104 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19105 info_ptr
+= blk
->size
;
19106 attr
->set_block (blk
);
19108 case DW_FORM_block1
:
19109 blk
= dwarf_alloc_block (cu
);
19110 blk
->size
= read_1_byte (abfd
, info_ptr
);
19112 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19113 info_ptr
+= blk
->size
;
19114 attr
->set_block (blk
);
19116 case DW_FORM_data1
:
19118 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
19121 case DW_FORM_flag_present
:
19122 attr
->set_unsigned (1);
19124 case DW_FORM_sdata
:
19125 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
19126 info_ptr
+= bytes_read
;
19128 case DW_FORM_rnglistx
:
19130 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19132 info_ptr
+= bytes_read
;
19135 case DW_FORM_udata
:
19136 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
19137 info_ptr
+= bytes_read
;
19140 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19141 + read_1_byte (abfd
, info_ptr
)));
19145 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19146 + read_2_bytes (abfd
, info_ptr
)));
19150 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19151 + read_4_bytes (abfd
, info_ptr
)));
19155 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19156 + read_8_bytes (abfd
, info_ptr
)));
19159 case DW_FORM_ref_sig8
:
19160 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
19163 case DW_FORM_ref_udata
:
19164 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19165 + read_unsigned_leb128 (abfd
, info_ptr
,
19167 info_ptr
+= bytes_read
;
19169 case DW_FORM_indirect
:
19170 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19171 info_ptr
+= bytes_read
;
19172 if (form
== DW_FORM_implicit_const
)
19174 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
19175 info_ptr
+= bytes_read
;
19177 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
19180 case DW_FORM_implicit_const
:
19181 attr
->set_signed (implicit_const
);
19183 case DW_FORM_addrx
:
19184 case DW_FORM_GNU_addr_index
:
19185 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19187 info_ptr
+= bytes_read
;
19190 case DW_FORM_strx1
:
19191 case DW_FORM_strx2
:
19192 case DW_FORM_strx3
:
19193 case DW_FORM_strx4
:
19194 case DW_FORM_GNU_str_index
:
19196 ULONGEST str_index
;
19197 if (form
== DW_FORM_strx1
)
19199 str_index
= read_1_byte (abfd
, info_ptr
);
19202 else if (form
== DW_FORM_strx2
)
19204 str_index
= read_2_bytes (abfd
, info_ptr
);
19207 else if (form
== DW_FORM_strx3
)
19209 str_index
= read_3_bytes (abfd
, info_ptr
);
19212 else if (form
== DW_FORM_strx4
)
19214 str_index
= read_4_bytes (abfd
, info_ptr
);
19219 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19220 info_ptr
+= bytes_read
;
19222 attr
->set_unsigned_reprocess (str_index
);
19226 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19227 dwarf_form_name (form
),
19228 bfd_get_filename (abfd
));
19232 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
19233 attr
->form
= DW_FORM_GNU_ref_alt
;
19235 /* We have seen instances where the compiler tried to emit a byte
19236 size attribute of -1 which ended up being encoded as an unsigned
19237 0xffffffff. Although 0xffffffff is technically a valid size value,
19238 an object of this size seems pretty unlikely so we can relatively
19239 safely treat these cases as if the size attribute was invalid and
19240 treat them as zero by default. */
19241 if (attr
->name
== DW_AT_byte_size
19242 && form
== DW_FORM_data4
19243 && attr
->as_unsigned () >= 0xffffffff)
19246 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19247 hex_string (attr
->as_unsigned ()));
19248 attr
->set_unsigned (0);
19254 /* Read an attribute described by an abbreviated attribute. */
19256 static const gdb_byte
*
19257 read_attribute (const struct die_reader_specs
*reader
,
19258 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
19259 const gdb_byte
*info_ptr
)
19261 attr
->name
= abbrev
->name
;
19262 attr
->string_is_canonical
= 0;
19263 attr
->requires_reprocessing
= 0;
19264 return read_attribute_value (reader
, attr
, abbrev
->form
,
19265 abbrev
->implicit_const
, info_ptr
);
19268 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19270 static const char *
19271 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
19272 LONGEST str_offset
)
19274 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
19275 str_offset
, "DW_FORM_strp");
19278 /* Return pointer to string at .debug_str offset as read from BUF.
19279 BUF is assumed to be in a compilation unit described by CU_HEADER.
19280 Return *BYTES_READ_PTR count of bytes read from BUF. */
19282 static const char *
19283 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
19284 const gdb_byte
*buf
,
19285 const struct comp_unit_head
*cu_header
,
19286 unsigned int *bytes_read_ptr
)
19288 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19290 return read_indirect_string_at_offset (per_objfile
, str_offset
);
19296 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19297 unsigned int offset_size
)
19299 bfd
*abfd
= objfile
->obfd
;
19300 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
19302 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19308 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19309 const struct comp_unit_head
*cu_header
,
19310 unsigned int *bytes_read_ptr
)
19312 bfd
*abfd
= objfile
->obfd
;
19313 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19315 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19318 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19319 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19320 ADDR_SIZE is the size of addresses from the CU header. */
19323 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
19324 gdb::optional
<ULONGEST
> addr_base
, int addr_size
)
19326 struct objfile
*objfile
= per_objfile
->objfile
;
19327 bfd
*abfd
= objfile
->obfd
;
19328 const gdb_byte
*info_ptr
;
19329 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
19331 per_objfile
->per_bfd
->addr
.read (objfile
);
19332 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
19333 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19334 objfile_name (objfile
));
19335 if (addr_base_or_zero
+ addr_index
* addr_size
19336 >= per_objfile
->per_bfd
->addr
.size
)
19337 error (_("DW_FORM_addr_index pointing outside of "
19338 ".debug_addr section [in module %s]"),
19339 objfile_name (objfile
));
19340 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
19341 + addr_index
* addr_size
);
19342 if (addr_size
== 4)
19343 return bfd_get_32 (abfd
, info_ptr
);
19345 return bfd_get_64 (abfd
, info_ptr
);
19348 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19351 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
19353 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
19354 cu
->addr_base
, cu
->header
.addr_size
);
19357 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19360 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
19361 unsigned int *bytes_read
)
19363 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
;
19364 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
19366 return read_addr_index (cu
, addr_index
);
19372 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
19373 dwarf2_per_objfile
*per_objfile
,
19374 unsigned int addr_index
)
19376 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
19377 gdb::optional
<ULONGEST
> addr_base
;
19380 /* We need addr_base and addr_size.
19381 If we don't have PER_CU->cu, we have to get it.
19382 Nasty, but the alternative is storing the needed info in PER_CU,
19383 which at this point doesn't seem justified: it's not clear how frequently
19384 it would get used and it would increase the size of every PER_CU.
19385 Entry points like dwarf2_per_cu_addr_size do a similar thing
19386 so we're not in uncharted territory here.
19387 Alas we need to be a bit more complicated as addr_base is contained
19390 We don't need to read the entire CU(/TU).
19391 We just need the header and top level die.
19393 IWBN to use the aging mechanism to let us lazily later discard the CU.
19394 For now we skip this optimization. */
19398 addr_base
= cu
->addr_base
;
19399 addr_size
= cu
->header
.addr_size
;
19403 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
19404 addr_base
= reader
.cu
->addr_base
;
19405 addr_size
= reader
.cu
->header
.addr_size
;
19408 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
19411 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19412 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19415 static const char *
19416 read_str_index (struct dwarf2_cu
*cu
,
19417 struct dwarf2_section_info
*str_section
,
19418 struct dwarf2_section_info
*str_offsets_section
,
19419 ULONGEST str_offsets_base
, ULONGEST str_index
)
19421 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19422 struct objfile
*objfile
= per_objfile
->objfile
;
19423 const char *objf_name
= objfile_name (objfile
);
19424 bfd
*abfd
= objfile
->obfd
;
19425 const gdb_byte
*info_ptr
;
19426 ULONGEST str_offset
;
19427 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19429 str_section
->read (objfile
);
19430 str_offsets_section
->read (objfile
);
19431 if (str_section
->buffer
== NULL
)
19432 error (_("%s used without %s section"
19433 " in CU at offset %s [in module %s]"),
19434 form_name
, str_section
->get_name (),
19435 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19436 if (str_offsets_section
->buffer
== NULL
)
19437 error (_("%s used without %s section"
19438 " in CU at offset %s [in module %s]"),
19439 form_name
, str_section
->get_name (),
19440 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19441 info_ptr
= (str_offsets_section
->buffer
19443 + str_index
* cu
->header
.offset_size
);
19444 if (cu
->header
.offset_size
== 4)
19445 str_offset
= bfd_get_32 (abfd
, info_ptr
);
19447 str_offset
= bfd_get_64 (abfd
, info_ptr
);
19448 if (str_offset
>= str_section
->size
)
19449 error (_("Offset from %s pointing outside of"
19450 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19451 form_name
, sect_offset_str (cu
->header
.sect_off
), objf_name
);
19452 return (const char *) (str_section
->buffer
+ str_offset
);
19455 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19457 static const char *
19458 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
19460 ULONGEST str_offsets_base
= reader
->cu
->header
.version
>= 5
19461 ? reader
->cu
->header
.addr_size
: 0;
19462 return read_str_index (reader
->cu
,
19463 &reader
->dwo_file
->sections
.str
,
19464 &reader
->dwo_file
->sections
.str_offsets
,
19465 str_offsets_base
, str_index
);
19468 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19470 static const char *
19471 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
19473 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19474 const char *objf_name
= objfile_name (objfile
);
19475 static const char form_name
[] = "DW_FORM_GNU_str_index";
19476 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
19478 if (!cu
->str_offsets_base
.has_value ())
19479 error (_("%s used in Fission stub without %s"
19480 " in CU at offset 0x%lx [in module %s]"),
19481 form_name
, str_offsets_attr_name
,
19482 (long) cu
->header
.offset_size
, objf_name
);
19484 return read_str_index (cu
,
19485 &cu
->per_objfile
->per_bfd
->str
,
19486 &cu
->per_objfile
->per_bfd
->str_offsets
,
19487 *cu
->str_offsets_base
, str_index
);
19490 /* Return the length of an LEB128 number in BUF. */
19493 leb128_size (const gdb_byte
*buf
)
19495 const gdb_byte
*begin
= buf
;
19501 if ((byte
& 128) == 0)
19502 return buf
- begin
;
19506 static enum language
19507 dwarf_lang_to_enum_language (unsigned int lang
)
19509 enum language language
;
19518 language
= language_c
;
19521 case DW_LANG_C_plus_plus
:
19522 case DW_LANG_C_plus_plus_11
:
19523 case DW_LANG_C_plus_plus_14
:
19524 language
= language_cplus
;
19527 language
= language_d
;
19529 case DW_LANG_Fortran77
:
19530 case DW_LANG_Fortran90
:
19531 case DW_LANG_Fortran95
:
19532 case DW_LANG_Fortran03
:
19533 case DW_LANG_Fortran08
:
19534 language
= language_fortran
;
19537 language
= language_go
;
19539 case DW_LANG_Mips_Assembler
:
19540 language
= language_asm
;
19542 case DW_LANG_Ada83
:
19543 case DW_LANG_Ada95
:
19544 language
= language_ada
;
19546 case DW_LANG_Modula2
:
19547 language
= language_m2
;
19549 case DW_LANG_Pascal83
:
19550 language
= language_pascal
;
19553 language
= language_objc
;
19556 case DW_LANG_Rust_old
:
19557 language
= language_rust
;
19559 case DW_LANG_OpenCL
:
19560 language
= language_opencl
;
19562 case DW_LANG_Cobol74
:
19563 case DW_LANG_Cobol85
:
19565 language
= language_minimal
;
19572 /* Return the named attribute or NULL if not there. */
19574 static struct attribute
*
19575 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19580 struct attribute
*spec
= NULL
;
19582 for (i
= 0; i
< die
->num_attrs
; ++i
)
19584 if (die
->attrs
[i
].name
== name
)
19585 return &die
->attrs
[i
];
19586 if (die
->attrs
[i
].name
== DW_AT_specification
19587 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
19588 spec
= &die
->attrs
[i
];
19594 die
= follow_die_ref (die
, spec
, &cu
);
19600 /* Return the string associated with a string-typed attribute, or NULL if it
19601 is either not found or is of an incorrect type. */
19603 static const char *
19604 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19606 struct attribute
*attr
;
19607 const char *str
= NULL
;
19609 attr
= dwarf2_attr (die
, name
, cu
);
19613 str
= attr
->as_string ();
19614 if (str
== nullptr)
19615 complaint (_("string type expected for attribute %s for "
19616 "DIE at %s in module %s"),
19617 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
19618 objfile_name (cu
->per_objfile
->objfile
));
19624 /* Return the dwo name or NULL if not present. If present, it is in either
19625 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19626 static const char *
19627 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19629 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
19630 if (dwo_name
== nullptr)
19631 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
19635 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19636 and holds a non-zero value. This function should only be used for
19637 DW_FORM_flag or DW_FORM_flag_present attributes. */
19640 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
19642 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
19644 return attr
!= nullptr && attr
->as_boolean ();
19648 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
19650 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19651 which value is non-zero. However, we have to be careful with
19652 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19653 (via dwarf2_flag_true_p) follows this attribute. So we may
19654 end up accidently finding a declaration attribute that belongs
19655 to a different DIE referenced by the specification attribute,
19656 even though the given DIE does not have a declaration attribute. */
19657 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
19658 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
19661 /* Return the die giving the specification for DIE, if there is
19662 one. *SPEC_CU is the CU containing DIE on input, and the CU
19663 containing the return value on output. If there is no
19664 specification, but there is an abstract origin, that is
19667 static struct die_info
*
19668 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
19670 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
19673 if (spec_attr
== NULL
)
19674 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
19676 if (spec_attr
== NULL
)
19679 return follow_die_ref (die
, spec_attr
, spec_cu
);
19682 /* A convenience function to find the proper .debug_line section for a CU. */
19684 static struct dwarf2_section_info
*
19685 get_debug_line_section (struct dwarf2_cu
*cu
)
19687 struct dwarf2_section_info
*section
;
19688 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19690 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19692 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19693 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
19694 else if (cu
->per_cu
->is_dwz
)
19696 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19698 section
= &dwz
->line
;
19701 section
= &per_objfile
->per_bfd
->line
;
19706 /* Read the statement program header starting at OFFSET in
19707 .debug_line, or .debug_line.dwo. Return a pointer
19708 to a struct line_header, allocated using xmalloc.
19709 Returns NULL if there is a problem reading the header, e.g., if it
19710 has a version we don't understand.
19712 NOTE: the strings in the include directory and file name tables of
19713 the returned object point into the dwarf line section buffer,
19714 and must not be freed. */
19716 static line_header_up
19717 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
)
19719 struct dwarf2_section_info
*section
;
19720 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19722 section
= get_debug_line_section (cu
);
19723 section
->read (per_objfile
->objfile
);
19724 if (section
->buffer
== NULL
)
19726 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19727 complaint (_("missing .debug_line.dwo section"));
19729 complaint (_("missing .debug_line section"));
19733 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
19734 per_objfile
, section
, &cu
->header
);
19737 /* Subroutine of dwarf_decode_lines to simplify it.
19738 Return the file name for the given file_entry.
19739 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19740 If space for the result is malloc'd, *NAME_HOLDER will be set.
19741 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19742 equivalent to CU_INFO. */
19744 static const char *
19745 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
19746 const file_and_directory
&cu_info
,
19747 gdb::unique_xmalloc_ptr
<char> *name_holder
)
19749 const char *include_name
= fe
.name
;
19750 const char *include_name_to_compare
= include_name
;
19752 const char *dir_name
= fe
.include_dir (lh
);
19754 gdb::unique_xmalloc_ptr
<char> hold_compare
;
19755 if (!IS_ABSOLUTE_PATH (include_name
)
19756 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
19758 /* Avoid creating a duplicate name for CU_INFO.
19759 We do this by comparing INCLUDE_NAME and CU_INFO.
19760 Before we do the comparison, however, we need to account
19761 for DIR_NAME and COMP_DIR.
19762 First prepend dir_name (if non-NULL). If we still don't
19763 have an absolute path prepend comp_dir (if non-NULL).
19764 However, the directory we record in the include-file's
19765 psymtab does not contain COMP_DIR (to match the
19766 corresponding symtab(s)).
19771 bash$ gcc -g ./hello.c
19772 include_name = "hello.c"
19774 DW_AT_comp_dir = comp_dir = "/tmp"
19775 DW_AT_name = "./hello.c"
19779 if (dir_name
!= NULL
)
19781 name_holder
->reset (concat (dir_name
, SLASH_STRING
,
19782 include_name
, (char *) NULL
));
19783 include_name
= name_holder
->get ();
19784 include_name_to_compare
= include_name
;
19786 if (!IS_ABSOLUTE_PATH (include_name
)
19787 && cu_info
.get_comp_dir () != nullptr)
19789 hold_compare
.reset (concat (cu_info
.get_comp_dir (), SLASH_STRING
,
19790 include_name
, (char *) NULL
));
19791 include_name_to_compare
= hold_compare
.get ();
19795 gdb::unique_xmalloc_ptr
<char> copied_name
;
19796 const char *cu_filename
= cu_info
.get_name ();
19797 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
19799 copied_name
.reset (concat (cu_info
.get_comp_dir (), SLASH_STRING
,
19800 cu_filename
, (char *) NULL
));
19801 cu_filename
= copied_name
.get ();
19804 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
19806 return include_name
;
19809 /* State machine to track the state of the line number program. */
19811 class lnp_state_machine
19814 /* Initialize a machine state for the start of a line number
19816 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
19818 file_entry
*current_file ()
19820 /* lh->file_names is 0-based, but the file name numbers in the
19821 statement program are 1-based. */
19822 return m_line_header
->file_name_at (m_file
);
19825 /* Record the line in the state machine. END_SEQUENCE is true if
19826 we're processing the end of a sequence. */
19827 void record_line (bool end_sequence
);
19829 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19830 nop-out rest of the lines in this sequence. */
19831 void check_line_address (struct dwarf2_cu
*cu
,
19832 const gdb_byte
*line_ptr
,
19833 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
);
19835 void handle_set_discriminator (unsigned int discriminator
)
19837 m_discriminator
= discriminator
;
19838 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
19841 /* Handle DW_LNE_set_address. */
19842 void handle_set_address (CORE_ADDR baseaddr
, CORE_ADDR address
)
19845 address
+= baseaddr
;
19846 m_address
= gdbarch_adjust_dwarf2_line (m_gdbarch
, address
, false);
19849 /* Handle DW_LNS_advance_pc. */
19850 void handle_advance_pc (CORE_ADDR adjust
);
19852 /* Handle a special opcode. */
19853 void handle_special_opcode (unsigned char op_code
);
19855 /* Handle DW_LNS_advance_line. */
19856 void handle_advance_line (int line_delta
)
19858 advance_line (line_delta
);
19861 /* Handle DW_LNS_set_file. */
19862 void handle_set_file (file_name_index file
);
19864 /* Handle DW_LNS_negate_stmt. */
19865 void handle_negate_stmt ()
19867 m_flags
^= LEF_IS_STMT
;
19870 /* Handle DW_LNS_const_add_pc. */
19871 void handle_const_add_pc ();
19873 /* Handle DW_LNS_fixed_advance_pc. */
19874 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
19876 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19880 /* Handle DW_LNS_copy. */
19881 void handle_copy ()
19883 record_line (false);
19884 m_discriminator
= 0;
19885 m_flags
&= ~LEF_PROLOGUE_END
;
19888 /* Handle DW_LNE_end_sequence. */
19889 void handle_end_sequence ()
19891 m_currently_recording_lines
= true;
19894 /* Handle DW_LNS_set_prologue_end. */
19895 void handle_set_prologue_end ()
19897 m_flags
|= LEF_PROLOGUE_END
;
19901 /* Advance the line by LINE_DELTA. */
19902 void advance_line (int line_delta
)
19904 m_line
+= line_delta
;
19906 if (line_delta
!= 0)
19907 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
19910 struct dwarf2_cu
*m_cu
;
19912 gdbarch
*m_gdbarch
;
19914 /* The line number header. */
19915 line_header
*m_line_header
;
19917 /* These are part of the standard DWARF line number state machine,
19918 and initialized according to the DWARF spec. */
19920 unsigned char m_op_index
= 0;
19921 /* The line table index of the current file. */
19922 file_name_index m_file
= 1;
19923 unsigned int m_line
= 1;
19925 /* These are initialized in the constructor. */
19927 CORE_ADDR m_address
;
19928 linetable_entry_flags m_flags
;
19929 unsigned int m_discriminator
;
19931 /* Additional bits of state we need to track. */
19933 /* The last file that we called dwarf2_start_subfile for.
19934 This is only used for TLLs. */
19935 unsigned int m_last_file
= 0;
19936 /* The last file a line number was recorded for. */
19937 struct subfile
*m_last_subfile
= NULL
;
19939 /* The address of the last line entry. */
19940 CORE_ADDR m_last_address
;
19942 /* Set to true when a previous line at the same address (using
19943 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19944 when a line entry at a new address (m_address different to
19945 m_last_address) is processed. */
19946 bool m_stmt_at_address
= false;
19948 /* When true, record the lines we decode. */
19949 bool m_currently_recording_lines
= false;
19951 /* The last line number that was recorded, used to coalesce
19952 consecutive entries for the same line. This can happen, for
19953 example, when discriminators are present. PR 17276. */
19954 unsigned int m_last_line
= 0;
19955 bool m_line_has_non_zero_discriminator
= false;
19959 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
19961 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
19962 / m_line_header
->maximum_ops_per_instruction
)
19963 * m_line_header
->minimum_instruction_length
);
19964 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19965 m_op_index
= ((m_op_index
+ adjust
)
19966 % m_line_header
->maximum_ops_per_instruction
);
19970 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
19972 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
19973 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
19974 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
19975 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
19976 / m_line_header
->maximum_ops_per_instruction
)
19977 * m_line_header
->minimum_instruction_length
);
19978 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19979 m_op_index
= ((m_op_index
+ adj_opcode_d
)
19980 % m_line_header
->maximum_ops_per_instruction
);
19982 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
19983 advance_line (line_delta
);
19984 record_line (false);
19985 m_discriminator
= 0;
19986 m_flags
&= ~LEF_PROLOGUE_END
;
19990 lnp_state_machine::handle_set_file (file_name_index file
)
19994 const file_entry
*fe
= current_file ();
19996 dwarf2_debug_line_missing_file_complaint ();
19999 const char *dir
= fe
->include_dir (m_line_header
);
20001 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20002 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
20003 dwarf2_start_subfile (m_cu
, fe
->name
, dir
);
20008 lnp_state_machine::handle_const_add_pc ()
20011 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
20014 = (((m_op_index
+ adjust
)
20015 / m_line_header
->maximum_ops_per_instruction
)
20016 * m_line_header
->minimum_instruction_length
);
20018 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
20019 m_op_index
= ((m_op_index
+ adjust
)
20020 % m_line_header
->maximum_ops_per_instruction
);
20023 /* Return non-zero if we should add LINE to the line number table.
20024 LINE is the line to add, LAST_LINE is the last line that was added,
20025 LAST_SUBFILE is the subfile for LAST_LINE.
20026 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20027 had a non-zero discriminator.
20029 We have to be careful in the presence of discriminators.
20030 E.g., for this line:
20032 for (i = 0; i < 100000; i++);
20034 clang can emit four line number entries for that one line,
20035 each with a different discriminator.
20036 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20038 However, we want gdb to coalesce all four entries into one.
20039 Otherwise the user could stepi into the middle of the line and
20040 gdb would get confused about whether the pc really was in the
20041 middle of the line.
20043 Things are further complicated by the fact that two consecutive
20044 line number entries for the same line is a heuristic used by gcc
20045 to denote the end of the prologue. So we can't just discard duplicate
20046 entries, we have to be selective about it. The heuristic we use is
20047 that we only collapse consecutive entries for the same line if at least
20048 one of those entries has a non-zero discriminator. PR 17276.
20050 Note: Addresses in the line number state machine can never go backwards
20051 within one sequence, thus this coalescing is ok. */
20054 dwarf_record_line_p (struct dwarf2_cu
*cu
,
20055 unsigned int line
, unsigned int last_line
,
20056 int line_has_non_zero_discriminator
,
20057 struct subfile
*last_subfile
)
20059 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
20061 if (line
!= last_line
)
20063 /* Same line for the same file that we've seen already.
20064 As a last check, for pr 17276, only record the line if the line
20065 has never had a non-zero discriminator. */
20066 if (!line_has_non_zero_discriminator
)
20071 /* Use the CU's builder to record line number LINE beginning at
20072 address ADDRESS in the line table of subfile SUBFILE. */
20075 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20076 unsigned int line
, CORE_ADDR address
,
20077 linetable_entry_flags flags
,
20078 struct dwarf2_cu
*cu
)
20080 CORE_ADDR addr
= gdbarch_addr_bits_remove (gdbarch
, address
);
20082 if (dwarf_line_debug
)
20084 gdb_printf (gdb_stdlog
,
20085 "Recording line %u, file %s, address %s\n",
20086 line
, lbasename (subfile
->name
),
20087 paddress (gdbarch
, address
));
20091 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
20094 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20095 Mark the end of a set of line number records.
20096 The arguments are the same as for dwarf_record_line_1.
20097 If SUBFILE is NULL the request is ignored. */
20100 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20101 CORE_ADDR address
, struct dwarf2_cu
*cu
)
20103 if (subfile
== NULL
)
20106 if (dwarf_line_debug
)
20108 gdb_printf (gdb_stdlog
,
20109 "Finishing current line, file %s, address %s\n",
20110 lbasename (subfile
->name
),
20111 paddress (gdbarch
, address
));
20114 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
20118 lnp_state_machine::record_line (bool end_sequence
)
20120 if (dwarf_line_debug
)
20122 gdb_printf (gdb_stdlog
,
20123 "Processing actual line %u: file %u,"
20124 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20126 paddress (m_gdbarch
, m_address
),
20127 (m_flags
& LEF_IS_STMT
) != 0,
20128 (m_flags
& LEF_PROLOGUE_END
) != 0,
20130 (end_sequence
? "\t(end sequence)" : ""));
20133 file_entry
*fe
= current_file ();
20136 dwarf2_debug_line_missing_file_complaint ();
20137 /* For now we ignore lines not starting on an instruction boundary.
20138 But not when processing end_sequence for compatibility with the
20139 previous version of the code. */
20140 else if (m_op_index
== 0 || end_sequence
)
20142 /* When we switch files we insert an end maker in the first file,
20143 switch to the second file and add a new line entry. The
20144 problem is that the end marker inserted in the first file will
20145 discard any previous line entries at the same address. If the
20146 line entries in the first file are marked as is-stmt, while
20147 the new line in the second file is non-stmt, then this means
20148 the end marker will discard is-stmt lines so we can have a
20149 non-stmt line. This means that there are less addresses at
20150 which the user can insert a breakpoint.
20152 To improve this we track the last address in m_last_address,
20153 and whether we have seen an is-stmt at this address. Then
20154 when switching files, if we have seen a stmt at the current
20155 address, and we are switching to create a non-stmt line, then
20156 discard the new line. */
20158 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
20159 bool ignore_this_line
20160 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
20161 && ((m_flags
& LEF_IS_STMT
) == 0)
20162 && m_stmt_at_address
)
20163 || (!end_sequence
&& m_line
== 0));
20165 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
20167 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
20168 m_currently_recording_lines
? m_cu
: nullptr);
20171 if (!end_sequence
&& !ignore_this_line
)
20173 linetable_entry_flags lte_flags
= m_flags
;
20174 if (producer_is_codewarrior (m_cu
))
20175 lte_flags
|= LEF_IS_STMT
;
20177 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
20178 m_line_has_non_zero_discriminator
,
20181 buildsym_compunit
*builder
= m_cu
->get_builder ();
20182 dwarf_record_line_1 (m_gdbarch
,
20183 builder
->get_current_subfile (),
20184 m_line
, m_address
, lte_flags
,
20185 m_currently_recording_lines
? m_cu
: nullptr);
20187 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20188 m_last_line
= m_line
;
20192 /* Track whether we have seen any IS_STMT true at m_address in case we
20193 have multiple line table entries all at m_address. */
20194 if (m_last_address
!= m_address
)
20196 m_stmt_at_address
= false;
20197 m_last_address
= m_address
;
20199 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
20202 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
20207 m_line_header
= lh
;
20209 m_currently_recording_lines
= true;
20211 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20212 was a line entry for it so that the backend has a chance to adjust it
20213 and also record it in case it needs it. This is currently used by MIPS
20214 code, cf. `mips_adjust_dwarf2_line'. */
20215 m_address
= gdbarch_adjust_dwarf2_line (arch
, 0, 0);
20217 if (lh
->default_is_stmt
)
20218 m_flags
|= LEF_IS_STMT
;
20219 m_discriminator
= 0;
20221 m_last_address
= m_address
;
20222 m_stmt_at_address
= false;
20226 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
20227 const gdb_byte
*line_ptr
,
20228 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
)
20230 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20231 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20232 located at 0x0. In this case, additionally check that if
20233 ADDRESS < UNRELOCATED_LOWPC. */
20235 if ((address
== 0 && address
< unrelocated_lowpc
)
20236 || address
== (CORE_ADDR
) -1)
20238 /* This line table is for a function which has been
20239 GCd by the linker. Ignore it. PR gdb/12528 */
20241 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20242 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
20244 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20245 line_offset
, objfile_name (objfile
));
20246 m_currently_recording_lines
= false;
20247 /* Note: m_currently_recording_lines is left as false until we see
20248 DW_LNE_end_sequence. */
20252 /* Subroutine of dwarf_decode_lines to simplify it.
20253 Process the line number information in LH. */
20256 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20259 const gdb_byte
*line_ptr
, *extended_end
;
20260 const gdb_byte
*line_end
;
20261 unsigned int bytes_read
, extended_len
;
20262 unsigned char op_code
, extended_op
;
20263 CORE_ADDR baseaddr
;
20264 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20265 bfd
*abfd
= objfile
->obfd
;
20266 struct gdbarch
*gdbarch
= objfile
->arch ();
20268 baseaddr
= objfile
->text_section_offset ();
20270 line_ptr
= lh
->statement_program_start
;
20271 line_end
= lh
->statement_program_end
;
20273 /* Read the statement sequences until there's nothing left. */
20274 while (line_ptr
< line_end
)
20276 /* The DWARF line number program state machine. Reset the state
20277 machine at the start of each sequence. */
20278 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
20279 bool end_sequence
= false;
20281 /* Start a subfile for the current file of the state
20283 const file_entry
*fe
= state_machine
.current_file ();
20286 dwarf2_start_subfile (cu
, fe
->name
, fe
->include_dir (lh
));
20288 /* Decode the table. */
20289 while (line_ptr
< line_end
&& !end_sequence
)
20291 op_code
= read_1_byte (abfd
, line_ptr
);
20294 if (op_code
>= lh
->opcode_base
)
20296 /* Special opcode. */
20297 state_machine
.handle_special_opcode (op_code
);
20299 else switch (op_code
)
20301 case DW_LNS_extended_op
:
20302 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
20304 line_ptr
+= bytes_read
;
20305 extended_end
= line_ptr
+ extended_len
;
20306 extended_op
= read_1_byte (abfd
, line_ptr
);
20308 if (DW_LNE_lo_user
<= extended_op
20309 && extended_op
<= DW_LNE_hi_user
)
20311 /* Vendor extension, ignore. */
20312 line_ptr
= extended_end
;
20315 switch (extended_op
)
20317 case DW_LNE_end_sequence
:
20318 state_machine
.handle_end_sequence ();
20319 end_sequence
= true;
20321 case DW_LNE_set_address
:
20324 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
20325 line_ptr
+= bytes_read
;
20327 state_machine
.check_line_address (cu
, line_ptr
,
20328 lowpc
- baseaddr
, address
);
20329 state_machine
.handle_set_address (baseaddr
, address
);
20332 case DW_LNE_define_file
:
20334 const char *cur_file
;
20335 unsigned int mod_time
, length
;
20338 cur_file
= read_direct_string (abfd
, line_ptr
,
20340 line_ptr
+= bytes_read
;
20341 dindex
= (dir_index
)
20342 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20343 line_ptr
+= bytes_read
;
20345 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20346 line_ptr
+= bytes_read
;
20348 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20349 line_ptr
+= bytes_read
;
20350 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
20353 case DW_LNE_set_discriminator
:
20355 /* The discriminator is not interesting to the
20356 debugger; just ignore it. We still need to
20357 check its value though:
20358 if there are consecutive entries for the same
20359 (non-prologue) line we want to coalesce them.
20362 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20363 line_ptr
+= bytes_read
;
20365 state_machine
.handle_set_discriminator (discr
);
20369 complaint (_("mangled .debug_line section"));
20372 /* Make sure that we parsed the extended op correctly. If e.g.
20373 we expected a different address size than the producer used,
20374 we may have read the wrong number of bytes. */
20375 if (line_ptr
!= extended_end
)
20377 complaint (_("mangled .debug_line section"));
20382 state_machine
.handle_copy ();
20384 case DW_LNS_advance_pc
:
20387 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20388 line_ptr
+= bytes_read
;
20390 state_machine
.handle_advance_pc (adjust
);
20393 case DW_LNS_advance_line
:
20396 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
20397 line_ptr
+= bytes_read
;
20399 state_machine
.handle_advance_line (line_delta
);
20402 case DW_LNS_set_file
:
20404 file_name_index file
20405 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
20407 line_ptr
+= bytes_read
;
20409 state_machine
.handle_set_file (file
);
20412 case DW_LNS_set_column
:
20413 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20414 line_ptr
+= bytes_read
;
20416 case DW_LNS_negate_stmt
:
20417 state_machine
.handle_negate_stmt ();
20419 case DW_LNS_set_basic_block
:
20421 /* Add to the address register of the state machine the
20422 address increment value corresponding to special opcode
20423 255. I.e., this value is scaled by the minimum
20424 instruction length since special opcode 255 would have
20425 scaled the increment. */
20426 case DW_LNS_const_add_pc
:
20427 state_machine
.handle_const_add_pc ();
20429 case DW_LNS_fixed_advance_pc
:
20431 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
20434 state_machine
.handle_fixed_advance_pc (addr_adj
);
20437 case DW_LNS_set_prologue_end
:
20438 state_machine
.handle_set_prologue_end ();
20442 /* Unknown standard opcode, ignore it. */
20445 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
20447 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20448 line_ptr
+= bytes_read
;
20455 dwarf2_debug_line_missing_end_sequence_complaint ();
20457 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20458 in which case we still finish recording the last line). */
20459 state_machine
.record_line (true);
20463 /* Decode the Line Number Program (LNP) for the given line_header
20464 structure and CU. The actual information extracted and the type
20465 of structures created from the LNP depends on the value of PST.
20467 FND holds the CU file name and directory, if known.
20468 It is used for relative paths in the line table.
20470 NOTE: It is important that psymtabs have the same file name (via
20471 strcmp) as the corresponding symtab. Since the directory is not
20472 used in the name of the symtab we don't use it in the name of the
20473 psymtabs we create. E.g. expand_line_sal requires this when
20474 finding psymtabs to expand. A good testcase for this is
20477 LOWPC is the lowest address in CU (or 0 if not known).
20479 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20480 for its PC<->lines mapping information. Otherwise only the filename
20481 table is read in. */
20484 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20485 CORE_ADDR lowpc
, int decode_mapping
)
20487 if (decode_mapping
)
20488 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
20490 /* Make sure a symtab is created for every file, even files
20491 which contain only variables (i.e. no code with associated
20493 buildsym_compunit
*builder
= cu
->get_builder ();
20494 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
20496 for (auto &fe
: lh
->file_names ())
20498 dwarf2_start_subfile (cu
, fe
.name
, fe
.include_dir (lh
));
20499 if (builder
->get_current_subfile ()->symtab
== NULL
)
20501 builder
->get_current_subfile ()->symtab
20502 = allocate_symtab (cust
,
20503 builder
->get_current_subfile ()->name
);
20505 fe
.symtab
= builder
->get_current_subfile ()->symtab
;
20509 /* Start a subfile for DWARF. FILENAME is the name of the file and
20510 DIRNAME the name of the source directory which contains FILENAME
20511 or NULL if not known.
20512 This routine tries to keep line numbers from identical absolute and
20513 relative file names in a common subfile.
20515 Using the `list' example from the GDB testsuite, which resides in
20516 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20517 of /srcdir/list0.c yields the following debugging information for list0.c:
20519 DW_AT_name: /srcdir/list0.c
20520 DW_AT_comp_dir: /compdir
20521 files.files[0].name: list0.h
20522 files.files[0].dir: /srcdir
20523 files.files[1].name: list0.c
20524 files.files[1].dir: /srcdir
20526 The line number information for list0.c has to end up in a single
20527 subfile, so that `break /srcdir/list0.c:1' works as expected.
20528 start_subfile will ensure that this happens provided that we pass the
20529 concatenation of files.files[1].dir and files.files[1].name as the
20533 dwarf2_start_subfile (struct dwarf2_cu
*cu
, const char *filename
,
20534 const char *dirname
)
20536 gdb::unique_xmalloc_ptr
<char> copy
;
20538 /* In order not to lose the line information directory,
20539 we concatenate it to the filename when it makes sense.
20540 Note that the Dwarf3 standard says (speaking of filenames in line
20541 information): ``The directory index is ignored for file names
20542 that represent full path names''. Thus ignoring dirname in the
20543 `else' branch below isn't an issue. */
20545 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
20547 copy
.reset (concat (dirname
, SLASH_STRING
, filename
, (char *) NULL
));
20548 filename
= copy
.get ();
20551 cu
->get_builder ()->start_subfile (filename
);
20555 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
20556 struct dwarf2_cu
*cu
)
20558 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20559 struct comp_unit_head
*cu_header
= &cu
->header
;
20561 /* NOTE drow/2003-01-30: There used to be a comment and some special
20562 code here to turn a symbol with DW_AT_external and a
20563 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20564 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20565 with some versions of binutils) where shared libraries could have
20566 relocations against symbols in their debug information - the
20567 minimal symbol would have the right address, but the debug info
20568 would not. It's no longer necessary, because we will explicitly
20569 apply relocations when we read in the debug information now. */
20571 /* A DW_AT_location attribute with no contents indicates that a
20572 variable has been optimized away. */
20573 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
20575 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20579 /* Handle one degenerate form of location expression specially, to
20580 preserve GDB's previous behavior when section offsets are
20581 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20582 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20584 if (attr
->form_is_block ())
20586 struct dwarf_block
*block
= attr
->as_block ();
20588 if ((block
->data
[0] == DW_OP_addr
20589 && block
->size
== 1 + cu_header
->addr_size
)
20590 || ((block
->data
[0] == DW_OP_GNU_addr_index
20591 || block
->data
[0] == DW_OP_addrx
)
20593 == 1 + leb128_size (&block
->data
[1]))))
20595 unsigned int dummy
;
20597 if (block
->data
[0] == DW_OP_addr
)
20598 sym
->set_value_address
20599 (cu
->header
.read_address (objfile
->obfd
, block
->data
+ 1,
20602 sym
->set_value_address
20603 (read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
));
20604 sym
->set_aclass_index (LOC_STATIC
);
20605 fixup_symbol_section (sym
, objfile
);
20606 sym
->set_value_address
20607 (sym
->value_address ()
20608 + objfile
->section_offsets
[sym
->section_index ()]);
20613 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20614 expression evaluator, and use LOC_COMPUTED only when necessary
20615 (i.e. when the value of a register or memory location is
20616 referenced, or a thread-local block, etc.). Then again, it might
20617 not be worthwhile. I'm assuming that it isn't unless performance
20618 or memory numbers show me otherwise. */
20620 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
20622 if (SYMBOL_COMPUTED_OPS (sym
)->location_has_loclist
)
20623 cu
->has_loclist
= true;
20626 /* Given a pointer to a DWARF information entry, figure out if we need
20627 to make a symbol table entry for it, and if so, create a new entry
20628 and return a pointer to it.
20629 If TYPE is NULL, determine symbol type from the die, otherwise
20630 used the passed type.
20631 If SPACE is not NULL, use it to hold the new symbol. If it is
20632 NULL, allocate a new symbol on the objfile's obstack. */
20634 static struct symbol
*
20635 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
20636 struct symbol
*space
)
20638 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20639 struct objfile
*objfile
= per_objfile
->objfile
;
20640 struct gdbarch
*gdbarch
= objfile
->arch ();
20641 struct symbol
*sym
= NULL
;
20643 struct attribute
*attr
= NULL
;
20644 struct attribute
*attr2
= NULL
;
20645 CORE_ADDR baseaddr
;
20646 struct pending
**list_to_add
= NULL
;
20648 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
20650 baseaddr
= objfile
->text_section_offset ();
20652 name
= dwarf2_name (die
, cu
);
20655 int suppress_add
= 0;
20660 sym
= new (&objfile
->objfile_obstack
) symbol
;
20661 OBJSTAT (objfile
, n_syms
++);
20663 /* Cache this symbol's name and the name's demangled form (if any). */
20664 sym
->set_language (cu
->per_cu
->lang
, &objfile
->objfile_obstack
);
20665 /* Fortran does not have mangling standard and the mangling does differ
20666 between gfortran, iFort etc. */
20667 const char *physname
20668 = (cu
->per_cu
->lang
== language_fortran
20669 ? dwarf2_full_name (name
, die
, cu
)
20670 : dwarf2_physname (name
, die
, cu
));
20671 const char *linkagename
= dw2_linkage_name (die
, cu
);
20673 if (linkagename
== nullptr || cu
->per_cu
->lang
== language_ada
)
20674 sym
->set_linkage_name (physname
);
20677 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
20678 sym
->set_linkage_name (linkagename
);
20681 /* Handle DW_AT_artificial. */
20682 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
20683 if (attr
!= nullptr)
20684 sym
->artificial
= attr
->as_boolean ();
20686 /* Default assumptions.
20687 Use the passed type or decode it from the die. */
20688 sym
->set_domain (VAR_DOMAIN
);
20689 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20691 sym
->set_type (type
);
20693 sym
->set_type (die_type (die
, cu
));
20694 attr
= dwarf2_attr (die
,
20695 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
20697 if (attr
!= nullptr)
20698 sym
->set_line (attr
->constant_value (0));
20700 attr
= dwarf2_attr (die
,
20701 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
20703 if (attr
!= nullptr && attr
->is_nonnegative ())
20705 file_name_index file_index
20706 = (file_name_index
) attr
->as_nonnegative ();
20707 struct file_entry
*fe
;
20709 if (cu
->line_header
!= NULL
)
20710 fe
= cu
->line_header
->file_name_at (file_index
);
20715 complaint (_("file index out of range"));
20717 symbol_set_symtab (sym
, fe
->symtab
);
20723 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
20724 if (attr
!= nullptr)
20728 addr
= attr
->as_address ();
20729 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ baseaddr
);
20730 sym
->set_value_address (addr
);
20731 sym
->set_aclass_index (LOC_LABEL
);
20734 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20735 sym
->set_type (objfile_type (objfile
)->builtin_core_addr
);
20736 sym
->set_domain (LABEL_DOMAIN
);
20737 add_symbol_to_list (sym
, cu
->list_in_scope
);
20739 case DW_TAG_subprogram
:
20740 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20742 sym
->set_aclass_index (LOC_BLOCK
);
20743 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20744 if ((attr2
!= nullptr && attr2
->as_boolean ())
20745 || cu
->per_cu
->lang
== language_ada
20746 || cu
->per_cu
->lang
== language_fortran
)
20748 /* Subprograms marked external are stored as a global symbol.
20749 Ada and Fortran subprograms, whether marked external or
20750 not, are always stored as a global symbol, because we want
20751 to be able to access them globally. For instance, we want
20752 to be able to break on a nested subprogram without having
20753 to specify the context. */
20754 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20758 list_to_add
= cu
->list_in_scope
;
20761 case DW_TAG_inlined_subroutine
:
20762 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20764 sym
->set_aclass_index (LOC_BLOCK
);
20765 sym
->set_is_inlined (1);
20766 list_to_add
= cu
->list_in_scope
;
20768 case DW_TAG_template_value_param
:
20770 /* Fall through. */
20771 case DW_TAG_constant
:
20772 case DW_TAG_variable
:
20773 case DW_TAG_member
:
20774 /* Compilation with minimal debug info may result in
20775 variables with missing type entries. Change the
20776 misleading `void' type to something sensible. */
20777 if (sym
->type ()->code () == TYPE_CODE_VOID
)
20778 sym
->set_type (objfile_type (objfile
)->builtin_int
);
20780 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20781 /* In the case of DW_TAG_member, we should only be called for
20782 static const members. */
20783 if (die
->tag
== DW_TAG_member
)
20785 /* dwarf2_add_field uses die_is_declaration,
20786 so we do the same. */
20787 gdb_assert (die_is_declaration (die
, cu
));
20790 if (attr
!= nullptr)
20792 dwarf2_const_value (attr
, sym
, cu
);
20793 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20796 if (attr2
!= nullptr && attr2
->as_boolean ())
20797 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20799 list_to_add
= cu
->list_in_scope
;
20803 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20804 if (attr
!= nullptr)
20806 var_decode_location (attr
, sym
, cu
);
20807 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20809 /* Fortran explicitly imports any global symbols to the local
20810 scope by DW_TAG_common_block. */
20811 if (cu
->per_cu
->lang
== language_fortran
&& die
->parent
20812 && die
->parent
->tag
== DW_TAG_common_block
)
20815 if (sym
->aclass () == LOC_STATIC
20816 && sym
->value_address () == 0
20817 && !per_objfile
->per_bfd
->has_section_at_zero
)
20819 /* When a static variable is eliminated by the linker,
20820 the corresponding debug information is not stripped
20821 out, but the variable address is set to null;
20822 do not add such variables into symbol table. */
20824 else if (attr2
!= nullptr && attr2
->as_boolean ())
20826 if (sym
->aclass () == LOC_STATIC
20827 && (objfile
->flags
& OBJF_MAINLINE
) == 0
20828 && per_objfile
->per_bfd
->can_copy
)
20830 /* A global static variable might be subject to
20831 copy relocation. We first check for a local
20832 minsym, though, because maybe the symbol was
20833 marked hidden, in which case this would not
20835 bound_minimal_symbol found
20836 = (lookup_minimal_symbol_linkage
20837 (sym
->linkage_name (), objfile
));
20838 if (found
.minsym
!= nullptr)
20839 sym
->maybe_copied
= 1;
20842 /* A variable with DW_AT_external is never static,
20843 but it may be block-scoped. */
20845 = ((cu
->list_in_scope
20846 == cu
->get_builder ()->get_file_symbols ())
20847 ? cu
->get_builder ()->get_global_symbols ()
20848 : cu
->list_in_scope
);
20851 list_to_add
= cu
->list_in_scope
;
20855 /* We do not know the address of this symbol.
20856 If it is an external symbol and we have type information
20857 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20858 The address of the variable will then be determined from
20859 the minimal symbol table whenever the variable is
20861 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20863 /* Fortran explicitly imports any global symbols to the local
20864 scope by DW_TAG_common_block. */
20865 if (cu
->per_cu
->lang
== language_fortran
&& die
->parent
20866 && die
->parent
->tag
== DW_TAG_common_block
)
20868 /* SYMBOL_CLASS doesn't matter here because
20869 read_common_block is going to reset it. */
20871 list_to_add
= cu
->list_in_scope
;
20873 else if (attr2
!= nullptr && attr2
->as_boolean ()
20874 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
20876 /* A variable with DW_AT_external is never static, but it
20877 may be block-scoped. */
20879 = ((cu
->list_in_scope
20880 == cu
->get_builder ()->get_file_symbols ())
20881 ? cu
->get_builder ()->get_global_symbols ()
20882 : cu
->list_in_scope
);
20884 sym
->set_aclass_index (LOC_UNRESOLVED
);
20886 else if (!die_is_declaration (die
, cu
))
20888 /* Use the default LOC_OPTIMIZED_OUT class. */
20889 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
20891 list_to_add
= cu
->list_in_scope
;
20895 case DW_TAG_formal_parameter
:
20897 /* If we are inside a function, mark this as an argument. If
20898 not, we might be looking at an argument to an inlined function
20899 when we do not have enough information to show inlined frames;
20900 pretend it's a local variable in that case so that the user can
20902 struct context_stack
*curr
20903 = cu
->get_builder ()->get_current_context_stack ();
20904 if (curr
!= nullptr && curr
->name
!= nullptr)
20905 sym
->set_is_argument (1);
20906 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20907 if (attr
!= nullptr)
20909 var_decode_location (attr
, sym
, cu
);
20911 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20912 if (attr
!= nullptr)
20914 dwarf2_const_value (attr
, sym
, cu
);
20917 list_to_add
= cu
->list_in_scope
;
20920 case DW_TAG_unspecified_parameters
:
20921 /* From varargs functions; gdb doesn't seem to have any
20922 interest in this information, so just ignore it for now.
20925 case DW_TAG_template_type_param
:
20927 /* Fall through. */
20928 case DW_TAG_class_type
:
20929 case DW_TAG_interface_type
:
20930 case DW_TAG_structure_type
:
20931 case DW_TAG_union_type
:
20932 case DW_TAG_set_type
:
20933 case DW_TAG_enumeration_type
:
20934 case DW_TAG_namelist
:
20935 if (die
->tag
== DW_TAG_namelist
)
20937 sym
->set_aclass_index (LOC_STATIC
);
20938 sym
->set_domain (VAR_DOMAIN
);
20942 sym
->set_aclass_index (LOC_TYPEDEF
);
20943 sym
->set_domain (STRUCT_DOMAIN
);
20946 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20947 really ever be static objects: otherwise, if you try
20948 to, say, break of a class's method and you're in a file
20949 which doesn't mention that class, it won't work unless
20950 the check for all static symbols in lookup_symbol_aux
20951 saves you. See the OtherFileClass tests in
20952 gdb.c++/namespace.exp. */
20956 buildsym_compunit
*builder
= cu
->get_builder ();
20958 = (cu
->list_in_scope
== builder
->get_file_symbols ()
20959 && cu
->per_cu
->lang
== language_cplus
20960 ? builder
->get_global_symbols ()
20961 : cu
->list_in_scope
);
20963 /* The semantics of C++ state that "struct foo {
20964 ... }" also defines a typedef for "foo". */
20965 if (cu
->per_cu
->lang
== language_cplus
20966 || cu
->per_cu
->lang
== language_ada
20967 || cu
->per_cu
->lang
== language_d
20968 || cu
->per_cu
->lang
== language_rust
)
20970 /* The symbol's name is already allocated along
20971 with this objfile, so we don't need to
20972 duplicate it for the type. */
20973 if (sym
->type ()->name () == 0)
20974 sym
->type ()->set_name (sym
->search_name ());
20979 case DW_TAG_typedef
:
20980 sym
->set_aclass_index (LOC_TYPEDEF
);
20981 sym
->set_domain (VAR_DOMAIN
);
20982 list_to_add
= cu
->list_in_scope
;
20984 case DW_TAG_array_type
:
20985 case DW_TAG_base_type
:
20986 case DW_TAG_subrange_type
:
20987 case DW_TAG_generic_subrange
:
20988 sym
->set_aclass_index (LOC_TYPEDEF
);
20989 sym
->set_domain (VAR_DOMAIN
);
20990 list_to_add
= cu
->list_in_scope
;
20992 case DW_TAG_enumerator
:
20993 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20994 if (attr
!= nullptr)
20996 dwarf2_const_value (attr
, sym
, cu
);
20999 /* NOTE: carlton/2003-11-10: See comment above in the
21000 DW_TAG_class_type, etc. block. */
21003 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
21004 && cu
->per_cu
->lang
== language_cplus
21005 ? cu
->get_builder ()->get_global_symbols ()
21006 : cu
->list_in_scope
);
21009 case DW_TAG_imported_declaration
:
21010 case DW_TAG_namespace
:
21011 sym
->set_aclass_index (LOC_TYPEDEF
);
21012 list_to_add
= cu
->get_builder ()->get_global_symbols ();
21014 case DW_TAG_module
:
21015 sym
->set_aclass_index (LOC_TYPEDEF
);
21016 sym
->set_domain (MODULE_DOMAIN
);
21017 list_to_add
= cu
->get_builder ()->get_global_symbols ();
21019 case DW_TAG_common_block
:
21020 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
21021 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
21022 add_symbol_to_list (sym
, cu
->list_in_scope
);
21025 /* Not a tag we recognize. Hopefully we aren't processing
21026 trash data, but since we must specifically ignore things
21027 we don't recognize, there is nothing else we should do at
21029 complaint (_("unsupported tag: '%s'"),
21030 dwarf_tag_name (die
->tag
));
21036 sym
->hash_next
= objfile
->template_symbols
;
21037 objfile
->template_symbols
= sym
;
21038 list_to_add
= NULL
;
21041 if (list_to_add
!= NULL
)
21042 add_symbol_to_list (sym
, list_to_add
);
21044 /* For the benefit of old versions of GCC, check for anonymous
21045 namespaces based on the demangled name. */
21046 if (!cu
->processing_has_namespace_info
21047 && cu
->per_cu
->lang
== language_cplus
)
21048 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
21053 /* Given an attr with a DW_FORM_dataN value in host byte order,
21054 zero-extend it as appropriate for the symbol's type. The DWARF
21055 standard (v4) is not entirely clear about the meaning of using
21056 DW_FORM_dataN for a constant with a signed type, where the type is
21057 wider than the data. The conclusion of a discussion on the DWARF
21058 list was that this is unspecified. We choose to always zero-extend
21059 because that is the interpretation long in use by GCC. */
21062 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
21063 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
21065 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21066 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
) ?
21067 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
21068 LONGEST l
= attr
->constant_value (0);
21070 if (bits
< sizeof (*value
) * 8)
21072 l
&= ((LONGEST
) 1 << bits
) - 1;
21075 else if (bits
== sizeof (*value
) * 8)
21079 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
21080 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
21087 /* Read a constant value from an attribute. Either set *VALUE, or if
21088 the value does not fit in *VALUE, set *BYTES - either already
21089 allocated on the objfile obstack, or newly allocated on OBSTACK,
21090 or, set *BATON, if we translated the constant to a location
21094 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
21095 const char *name
, struct obstack
*obstack
,
21096 struct dwarf2_cu
*cu
,
21097 LONGEST
*value
, const gdb_byte
**bytes
,
21098 struct dwarf2_locexpr_baton
**baton
)
21100 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21101 struct objfile
*objfile
= per_objfile
->objfile
;
21102 struct comp_unit_head
*cu_header
= &cu
->header
;
21103 struct dwarf_block
*blk
;
21104 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
) ?
21105 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
21111 switch (attr
->form
)
21114 case DW_FORM_addrx
:
21115 case DW_FORM_GNU_addr_index
:
21119 if (TYPE_LENGTH (type
) != cu_header
->addr_size
)
21120 dwarf2_const_value_length_mismatch_complaint (name
,
21121 cu_header
->addr_size
,
21122 TYPE_LENGTH (type
));
21123 /* Symbols of this form are reasonably rare, so we just
21124 piggyback on the existing location code rather than writing
21125 a new implementation of symbol_computed_ops. */
21126 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
21127 (*baton
)->per_objfile
= per_objfile
;
21128 (*baton
)->per_cu
= cu
->per_cu
;
21129 gdb_assert ((*baton
)->per_cu
);
21131 (*baton
)->size
= 2 + cu_header
->addr_size
;
21132 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
21133 (*baton
)->data
= data
;
21135 data
[0] = DW_OP_addr
;
21136 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
21137 byte_order
, attr
->as_address ());
21138 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
21141 case DW_FORM_string
:
21144 case DW_FORM_GNU_str_index
:
21145 case DW_FORM_GNU_strp_alt
:
21146 /* The string is already allocated on the objfile obstack, point
21148 *bytes
= (const gdb_byte
*) attr
->as_string ();
21150 case DW_FORM_block1
:
21151 case DW_FORM_block2
:
21152 case DW_FORM_block4
:
21153 case DW_FORM_block
:
21154 case DW_FORM_exprloc
:
21155 case DW_FORM_data16
:
21156 blk
= attr
->as_block ();
21157 if (TYPE_LENGTH (type
) != blk
->size
)
21158 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
21159 TYPE_LENGTH (type
));
21160 *bytes
= blk
->data
;
21163 /* The DW_AT_const_value attributes are supposed to carry the
21164 symbol's value "represented as it would be on the target
21165 architecture." By the time we get here, it's already been
21166 converted to host endianness, so we just need to sign- or
21167 zero-extend it as appropriate. */
21168 case DW_FORM_data1
:
21169 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
21171 case DW_FORM_data2
:
21172 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
21174 case DW_FORM_data4
:
21175 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
21177 case DW_FORM_data8
:
21178 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
21181 case DW_FORM_sdata
:
21182 case DW_FORM_implicit_const
:
21183 *value
= attr
->as_signed ();
21186 case DW_FORM_udata
:
21187 *value
= attr
->as_unsigned ();
21191 complaint (_("unsupported const value attribute form: '%s'"),
21192 dwarf_form_name (attr
->form
));
21199 /* Copy constant value from an attribute to a symbol. */
21202 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
21203 struct dwarf2_cu
*cu
)
21205 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21207 const gdb_byte
*bytes
;
21208 struct dwarf2_locexpr_baton
*baton
;
21210 dwarf2_const_value_attr (attr
, sym
->type (),
21211 sym
->print_name (),
21212 &objfile
->objfile_obstack
, cu
,
21213 &value
, &bytes
, &baton
);
21217 SYMBOL_LOCATION_BATON (sym
) = baton
;
21218 sym
->set_aclass_index (dwarf2_locexpr_index
);
21220 else if (bytes
!= NULL
)
21222 sym
->set_value_bytes (bytes
);
21223 sym
->set_aclass_index (LOC_CONST_BYTES
);
21227 sym
->set_value_longest (value
);
21228 sym
->set_aclass_index (LOC_CONST
);
21232 /* Return the type of the die in question using its DW_AT_type attribute. */
21234 static struct type
*
21235 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21237 struct attribute
*type_attr
;
21239 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
21242 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21243 /* A missing DW_AT_type represents a void type. */
21244 return objfile_type (objfile
)->builtin_void
;
21247 return lookup_die_type (die
, type_attr
, cu
);
21250 /* True iff CU's producer generates GNAT Ada auxiliary information
21251 that allows to find parallel types through that information instead
21252 of having to do expensive parallel lookups by type name. */
21255 need_gnat_info (struct dwarf2_cu
*cu
)
21257 /* Assume that the Ada compiler was GNAT, which always produces
21258 the auxiliary information. */
21259 return (cu
->per_cu
->lang
== language_ada
);
21262 /* Return the auxiliary type of the die in question using its
21263 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21264 attribute is not present. */
21266 static struct type
*
21267 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21269 struct attribute
*type_attr
;
21271 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
21275 return lookup_die_type (die
, type_attr
, cu
);
21278 /* If DIE has a descriptive_type attribute, then set the TYPE's
21279 descriptive type accordingly. */
21282 set_descriptive_type (struct type
*type
, struct die_info
*die
,
21283 struct dwarf2_cu
*cu
)
21285 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
21287 if (descriptive_type
)
21289 ALLOCATE_GNAT_AUX_TYPE (type
);
21290 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
21294 /* Return the containing type of the die in question using its
21295 DW_AT_containing_type attribute. */
21297 static struct type
*
21298 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21300 struct attribute
*type_attr
;
21301 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21303 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
21305 error (_("Dwarf Error: Problem turning containing type into gdb type "
21306 "[in module %s]"), objfile_name (objfile
));
21308 return lookup_die_type (die
, type_attr
, cu
);
21311 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21313 static struct type
*
21314 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
21316 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21317 struct objfile
*objfile
= per_objfile
->objfile
;
21320 std::string message
21321 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21322 objfile_name (objfile
),
21323 sect_offset_str (cu
->header
.sect_off
),
21324 sect_offset_str (die
->sect_off
));
21325 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
21327 return init_type (objfile
, TYPE_CODE_ERROR
, 0, saved
);
21330 /* Look up the type of DIE in CU using its type attribute ATTR.
21331 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21332 DW_AT_containing_type.
21333 If there is no type substitute an error marker. */
21335 static struct type
*
21336 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
21337 struct dwarf2_cu
*cu
)
21339 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21340 struct objfile
*objfile
= per_objfile
->objfile
;
21341 struct type
*this_type
;
21343 gdb_assert (attr
->name
== DW_AT_type
21344 || attr
->name
== DW_AT_GNAT_descriptive_type
21345 || attr
->name
== DW_AT_containing_type
);
21347 /* First see if we have it cached. */
21349 if (attr
->form
== DW_FORM_GNU_ref_alt
)
21351 struct dwarf2_per_cu_data
*per_cu
;
21352 sect_offset sect_off
= attr
->get_ref_die_offset ();
21354 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
21355 per_objfile
->per_bfd
);
21356 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
21358 else if (attr
->form_is_ref ())
21360 sect_offset sect_off
= attr
->get_ref_die_offset ();
21362 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
21364 else if (attr
->form
== DW_FORM_ref_sig8
)
21366 ULONGEST signature
= attr
->as_signature ();
21368 return get_signatured_type (die
, signature
, cu
);
21372 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21373 " at %s [in module %s]"),
21374 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
21375 objfile_name (objfile
));
21376 return build_error_marker_type (cu
, die
);
21379 /* If not cached we need to read it in. */
21381 if (this_type
== NULL
)
21383 struct die_info
*type_die
= NULL
;
21384 struct dwarf2_cu
*type_cu
= cu
;
21386 if (attr
->form_is_ref ())
21387 type_die
= follow_die_ref (die
, attr
, &type_cu
);
21388 if (type_die
== NULL
)
21389 return build_error_marker_type (cu
, die
);
21390 /* If we find the type now, it's probably because the type came
21391 from an inter-CU reference and the type's CU got expanded before
21393 this_type
= read_type_die (type_die
, type_cu
);
21396 /* If we still don't have a type use an error marker. */
21398 if (this_type
== NULL
)
21399 return build_error_marker_type (cu
, die
);
21404 /* Return the type in DIE, CU.
21405 Returns NULL for invalid types.
21407 This first does a lookup in die_type_hash,
21408 and only reads the die in if necessary.
21410 NOTE: This can be called when reading in partial or full symbols. */
21412 static struct type
*
21413 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
21415 struct type
*this_type
;
21417 this_type
= get_die_type (die
, cu
);
21421 return read_type_die_1 (die
, cu
);
21424 /* Read the type in DIE, CU.
21425 Returns NULL for invalid types. */
21427 static struct type
*
21428 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
21430 struct type
*this_type
= NULL
;
21434 case DW_TAG_class_type
:
21435 case DW_TAG_interface_type
:
21436 case DW_TAG_structure_type
:
21437 case DW_TAG_union_type
:
21438 this_type
= read_structure_type (die
, cu
);
21440 case DW_TAG_enumeration_type
:
21441 this_type
= read_enumeration_type (die
, cu
);
21443 case DW_TAG_subprogram
:
21444 case DW_TAG_subroutine_type
:
21445 case DW_TAG_inlined_subroutine
:
21446 this_type
= read_subroutine_type (die
, cu
);
21448 case DW_TAG_array_type
:
21449 this_type
= read_array_type (die
, cu
);
21451 case DW_TAG_set_type
:
21452 this_type
= read_set_type (die
, cu
);
21454 case DW_TAG_pointer_type
:
21455 this_type
= read_tag_pointer_type (die
, cu
);
21457 case DW_TAG_ptr_to_member_type
:
21458 this_type
= read_tag_ptr_to_member_type (die
, cu
);
21460 case DW_TAG_reference_type
:
21461 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
21463 case DW_TAG_rvalue_reference_type
:
21464 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
21466 case DW_TAG_const_type
:
21467 this_type
= read_tag_const_type (die
, cu
);
21469 case DW_TAG_volatile_type
:
21470 this_type
= read_tag_volatile_type (die
, cu
);
21472 case DW_TAG_restrict_type
:
21473 this_type
= read_tag_restrict_type (die
, cu
);
21475 case DW_TAG_string_type
:
21476 this_type
= read_tag_string_type (die
, cu
);
21478 case DW_TAG_typedef
:
21479 this_type
= read_typedef (die
, cu
);
21481 case DW_TAG_generic_subrange
:
21482 case DW_TAG_subrange_type
:
21483 this_type
= read_subrange_type (die
, cu
);
21485 case DW_TAG_base_type
:
21486 this_type
= read_base_type (die
, cu
);
21488 case DW_TAG_unspecified_type
:
21489 this_type
= read_unspecified_type (die
, cu
);
21491 case DW_TAG_namespace
:
21492 this_type
= read_namespace_type (die
, cu
);
21494 case DW_TAG_module
:
21495 this_type
= read_module_type (die
, cu
);
21497 case DW_TAG_atomic_type
:
21498 this_type
= read_tag_atomic_type (die
, cu
);
21501 complaint (_("unexpected tag in read_type_die: '%s'"),
21502 dwarf_tag_name (die
->tag
));
21509 /* See if we can figure out if the class lives in a namespace. We do
21510 this by looking for a member function; its demangled name will
21511 contain namespace info, if there is any.
21512 Return the computed name or NULL.
21513 Space for the result is allocated on the objfile's obstack.
21514 This is the full-die version of guess_partial_die_structure_name.
21515 In this case we know DIE has no useful parent. */
21517 static const char *
21518 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21520 struct die_info
*spec_die
;
21521 struct dwarf2_cu
*spec_cu
;
21522 struct die_info
*child
;
21523 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21526 spec_die
= die_specification (die
, &spec_cu
);
21527 if (spec_die
!= NULL
)
21533 for (child
= die
->child
;
21535 child
= child
->sibling
)
21537 if (child
->tag
== DW_TAG_subprogram
)
21539 const char *linkage_name
= dw2_linkage_name (child
, cu
);
21541 if (linkage_name
!= NULL
)
21543 gdb::unique_xmalloc_ptr
<char> actual_name
21544 (cu
->language_defn
->class_name_from_physname (linkage_name
));
21545 const char *name
= NULL
;
21547 if (actual_name
!= NULL
)
21549 const char *die_name
= dwarf2_name (die
, cu
);
21551 if (die_name
!= NULL
21552 && strcmp (die_name
, actual_name
.get ()) != 0)
21554 /* Strip off the class name from the full name.
21555 We want the prefix. */
21556 int die_name_len
= strlen (die_name
);
21557 int actual_name_len
= strlen (actual_name
.get ());
21558 const char *ptr
= actual_name
.get ();
21560 /* Test for '::' as a sanity check. */
21561 if (actual_name_len
> die_name_len
+ 2
21562 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
21563 name
= obstack_strndup (
21564 &objfile
->per_bfd
->storage_obstack
,
21565 ptr
, actual_name_len
- die_name_len
- 2);
21576 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21577 prefix part in such case. See
21578 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21580 static const char *
21581 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21583 struct attribute
*attr
;
21586 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
21587 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
21590 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
21593 attr
= dw2_linkage_name_attr (die
, cu
);
21594 const char *attr_name
= attr
->as_string ();
21595 if (attr
== NULL
|| attr_name
== NULL
)
21598 /* dwarf2_name had to be already called. */
21599 gdb_assert (attr
->canonical_string_p ());
21601 /* Strip the base name, keep any leading namespaces/classes. */
21602 base
= strrchr (attr_name
, ':');
21603 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
21606 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21607 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
21609 &base
[-1] - attr_name
);
21612 /* Return the name of the namespace/class that DIE is defined within,
21613 or "" if we can't tell. The caller should not xfree the result.
21615 For example, if we're within the method foo() in the following
21625 then determine_prefix on foo's die will return "N::C". */
21627 static const char *
21628 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21630 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21631 struct die_info
*parent
, *spec_die
;
21632 struct dwarf2_cu
*spec_cu
;
21633 struct type
*parent_type
;
21634 const char *retval
;
21636 if (cu
->per_cu
->lang
!= language_cplus
21637 && cu
->per_cu
->lang
!= language_fortran
21638 && cu
->per_cu
->lang
!= language_d
21639 && cu
->per_cu
->lang
!= language_rust
)
21642 retval
= anonymous_struct_prefix (die
, cu
);
21646 /* We have to be careful in the presence of DW_AT_specification.
21647 For example, with GCC 3.4, given the code
21651 // Definition of N::foo.
21655 then we'll have a tree of DIEs like this:
21657 1: DW_TAG_compile_unit
21658 2: DW_TAG_namespace // N
21659 3: DW_TAG_subprogram // declaration of N::foo
21660 4: DW_TAG_subprogram // definition of N::foo
21661 DW_AT_specification // refers to die #3
21663 Thus, when processing die #4, we have to pretend that we're in
21664 the context of its DW_AT_specification, namely the contex of die
21667 spec_die
= die_specification (die
, &spec_cu
);
21668 if (spec_die
== NULL
)
21669 parent
= die
->parent
;
21672 parent
= spec_die
->parent
;
21676 if (parent
== NULL
)
21678 else if (parent
->building_fullname
)
21681 const char *parent_name
;
21683 /* It has been seen on RealView 2.2 built binaries,
21684 DW_TAG_template_type_param types actually _defined_ as
21685 children of the parent class:
21688 template class <class Enum> Class{};
21689 Class<enum E> class_e;
21691 1: DW_TAG_class_type (Class)
21692 2: DW_TAG_enumeration_type (E)
21693 3: DW_TAG_enumerator (enum1:0)
21694 3: DW_TAG_enumerator (enum2:1)
21696 2: DW_TAG_template_type_param
21697 DW_AT_type DW_FORM_ref_udata (E)
21699 Besides being broken debug info, it can put GDB into an
21700 infinite loop. Consider:
21702 When we're building the full name for Class<E>, we'll start
21703 at Class, and go look over its template type parameters,
21704 finding E. We'll then try to build the full name of E, and
21705 reach here. We're now trying to build the full name of E,
21706 and look over the parent DIE for containing scope. In the
21707 broken case, if we followed the parent DIE of E, we'd again
21708 find Class, and once again go look at its template type
21709 arguments, etc., etc. Simply don't consider such parent die
21710 as source-level parent of this die (it can't be, the language
21711 doesn't allow it), and break the loop here. */
21712 name
= dwarf2_name (die
, cu
);
21713 parent_name
= dwarf2_name (parent
, cu
);
21714 complaint (_("template param type '%s' defined within parent '%s'"),
21715 name
? name
: "<unknown>",
21716 parent_name
? parent_name
: "<unknown>");
21720 switch (parent
->tag
)
21722 case DW_TAG_namespace
:
21723 parent_type
= read_type_die (parent
, cu
);
21724 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21725 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21726 Work around this problem here. */
21727 if (cu
->per_cu
->lang
== language_cplus
21728 && strcmp (parent_type
->name (), "::") == 0)
21730 /* We give a name to even anonymous namespaces. */
21731 return parent_type
->name ();
21732 case DW_TAG_class_type
:
21733 case DW_TAG_interface_type
:
21734 case DW_TAG_structure_type
:
21735 case DW_TAG_union_type
:
21736 case DW_TAG_module
:
21737 parent_type
= read_type_die (parent
, cu
);
21738 if (parent_type
->name () != NULL
)
21739 return parent_type
->name ();
21741 /* An anonymous structure is only allowed non-static data
21742 members; no typedefs, no member functions, et cetera.
21743 So it does not need a prefix. */
21745 case DW_TAG_compile_unit
:
21746 case DW_TAG_partial_unit
:
21747 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21748 if (cu
->per_cu
->lang
== language_cplus
21749 && !per_objfile
->per_bfd
->types
.empty ()
21750 && die
->child
!= NULL
21751 && (die
->tag
== DW_TAG_class_type
21752 || die
->tag
== DW_TAG_structure_type
21753 || die
->tag
== DW_TAG_union_type
))
21755 const char *name
= guess_full_die_structure_name (die
, cu
);
21760 case DW_TAG_subprogram
:
21761 /* Nested subroutines in Fortran get a prefix with the name
21762 of the parent's subroutine. */
21763 if (cu
->per_cu
->lang
== language_fortran
)
21765 if ((die
->tag
== DW_TAG_subprogram
)
21766 && (dwarf2_name (parent
, cu
) != NULL
))
21767 return dwarf2_name (parent
, cu
);
21770 case DW_TAG_enumeration_type
:
21771 parent_type
= read_type_die (parent
, cu
);
21772 if (parent_type
->is_declared_class ())
21774 if (parent_type
->name () != NULL
)
21775 return parent_type
->name ();
21778 /* Fall through. */
21780 return determine_prefix (parent
, cu
);
21784 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21785 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21786 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21787 an obconcat, otherwise allocate storage for the result. The CU argument is
21788 used to determine the language and hence, the appropriate separator. */
21790 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21793 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
21794 int physname
, struct dwarf2_cu
*cu
)
21796 const char *lead
= "";
21799 if (suffix
== NULL
|| suffix
[0] == '\0'
21800 || prefix
== NULL
|| prefix
[0] == '\0')
21802 else if (cu
->per_cu
->lang
== language_d
)
21804 /* For D, the 'main' function could be defined in any module, but it
21805 should never be prefixed. */
21806 if (strcmp (suffix
, "D main") == 0)
21814 else if (cu
->per_cu
->lang
== language_fortran
&& physname
)
21816 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21817 DW_AT_MIPS_linkage_name is preferred and used instead. */
21825 if (prefix
== NULL
)
21827 if (suffix
== NULL
)
21834 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
21836 strcpy (retval
, lead
);
21837 strcat (retval
, prefix
);
21838 strcat (retval
, sep
);
21839 strcat (retval
, suffix
);
21844 /* We have an obstack. */
21845 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
21849 /* Get name of a die, return NULL if not found. */
21851 static const char *
21852 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
21853 struct objfile
*objfile
)
21855 if (name
&& cu
->per_cu
->lang
== language_cplus
)
21857 gdb::unique_xmalloc_ptr
<char> canon_name
21858 = cp_canonicalize_string (name
);
21860 if (canon_name
!= nullptr)
21861 name
= objfile
->intern (canon_name
.get ());
21867 /* Get name of a die, return NULL if not found.
21868 Anonymous namespaces are converted to their magic string. */
21870 static const char *
21871 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21873 struct attribute
*attr
;
21874 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21876 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
21877 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21878 if (attr_name
== nullptr
21879 && die
->tag
!= DW_TAG_namespace
21880 && die
->tag
!= DW_TAG_class_type
21881 && die
->tag
!= DW_TAG_interface_type
21882 && die
->tag
!= DW_TAG_structure_type
21883 && die
->tag
!= DW_TAG_namelist
21884 && die
->tag
!= DW_TAG_union_type
)
21889 case DW_TAG_compile_unit
:
21890 case DW_TAG_partial_unit
:
21891 /* Compilation units have a DW_AT_name that is a filename, not
21892 a source language identifier. */
21893 case DW_TAG_enumeration_type
:
21894 case DW_TAG_enumerator
:
21895 /* These tags always have simple identifiers already; no need
21896 to canonicalize them. */
21899 case DW_TAG_namespace
:
21900 if (attr_name
!= nullptr)
21902 return CP_ANONYMOUS_NAMESPACE_STR
;
21904 case DW_TAG_class_type
:
21905 case DW_TAG_interface_type
:
21906 case DW_TAG_structure_type
:
21907 case DW_TAG_union_type
:
21908 case DW_TAG_namelist
:
21909 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21910 structures or unions. These were of the form "._%d" in GCC 4.1,
21911 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21912 and GCC 4.4. We work around this problem by ignoring these. */
21913 if (attr_name
!= nullptr
21914 && (startswith (attr_name
, "._")
21915 || startswith (attr_name
, "<anonymous")))
21918 /* GCC might emit a nameless typedef that has a linkage name. See
21919 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21920 if (!attr
|| attr_name
== NULL
)
21922 attr
= dw2_linkage_name_attr (die
, cu
);
21923 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21924 if (attr
== NULL
|| attr_name
== NULL
)
21927 /* Avoid demangling attr_name the second time on a second
21928 call for the same DIE. */
21929 if (!attr
->canonical_string_p ())
21931 gdb::unique_xmalloc_ptr
<char> demangled
21932 (gdb_demangle (attr_name
, DMGL_TYPES
));
21933 if (demangled
== nullptr)
21936 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
21937 attr_name
= attr
->as_string ();
21940 /* Strip any leading namespaces/classes, keep only the
21941 base name. DW_AT_name for named DIEs does not
21942 contain the prefixes. */
21943 const char *base
= strrchr (attr_name
, ':');
21944 if (base
&& base
> attr_name
&& base
[-1] == ':')
21955 if (!attr
->canonical_string_p ())
21956 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
21958 return attr
->as_string ();
21961 /* Return the die that this die in an extension of, or NULL if there
21962 is none. *EXT_CU is the CU containing DIE on input, and the CU
21963 containing the return value on output. */
21965 static struct die_info
*
21966 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
21968 struct attribute
*attr
;
21970 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
21974 return follow_die_ref (die
, attr
, ext_cu
);
21978 dump_die_shallow (struct ui_file
*f
, int indent
, struct die_info
*die
)
21982 gdb_printf (f
, "%*sDie: %s (abbrev %d, offset %s)\n",
21984 dwarf_tag_name (die
->tag
), die
->abbrev
,
21985 sect_offset_str (die
->sect_off
));
21987 if (die
->parent
!= NULL
)
21988 gdb_printf (f
, "%*s parent at offset: %s\n",
21990 sect_offset_str (die
->parent
->sect_off
));
21992 gdb_printf (f
, "%*s has children: %s\n",
21994 dwarf_bool_name (die
->child
!= NULL
));
21996 gdb_printf (f
, "%*s attributes:\n", indent
, "");
21998 for (i
= 0; i
< die
->num_attrs
; ++i
)
22000 gdb_printf (f
, "%*s %s (%s) ",
22002 dwarf_attr_name (die
->attrs
[i
].name
),
22003 dwarf_form_name (die
->attrs
[i
].form
));
22005 switch (die
->attrs
[i
].form
)
22008 case DW_FORM_addrx
:
22009 case DW_FORM_GNU_addr_index
:
22010 gdb_printf (f
, "address: ");
22011 gdb_puts (hex_string (die
->attrs
[i
].as_address ()), f
);
22013 case DW_FORM_block2
:
22014 case DW_FORM_block4
:
22015 case DW_FORM_block
:
22016 case DW_FORM_block1
:
22017 gdb_printf (f
, "block: size %s",
22018 pulongest (die
->attrs
[i
].as_block ()->size
));
22020 case DW_FORM_exprloc
:
22021 gdb_printf (f
, "expression: size %s",
22022 pulongest (die
->attrs
[i
].as_block ()->size
));
22024 case DW_FORM_data16
:
22025 gdb_printf (f
, "constant of 16 bytes");
22027 case DW_FORM_ref_addr
:
22028 gdb_printf (f
, "ref address: ");
22029 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
22031 case DW_FORM_GNU_ref_alt
:
22032 gdb_printf (f
, "alt ref address: ");
22033 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
22039 case DW_FORM_ref_udata
:
22040 gdb_printf (f
, "constant ref: 0x%lx (adjusted)",
22041 (long) (die
->attrs
[i
].as_unsigned ()));
22043 case DW_FORM_data1
:
22044 case DW_FORM_data2
:
22045 case DW_FORM_data4
:
22046 case DW_FORM_data8
:
22047 case DW_FORM_udata
:
22048 gdb_printf (f
, "constant: %s",
22049 pulongest (die
->attrs
[i
].as_unsigned ()));
22051 case DW_FORM_sec_offset
:
22052 gdb_printf (f
, "section offset: %s",
22053 pulongest (die
->attrs
[i
].as_unsigned ()));
22055 case DW_FORM_ref_sig8
:
22056 gdb_printf (f
, "signature: %s",
22057 hex_string (die
->attrs
[i
].as_signature ()));
22059 case DW_FORM_string
:
22061 case DW_FORM_line_strp
:
22063 case DW_FORM_GNU_str_index
:
22064 case DW_FORM_GNU_strp_alt
:
22065 gdb_printf (f
, "string: \"%s\" (%s canonicalized)",
22066 die
->attrs
[i
].as_string ()
22067 ? die
->attrs
[i
].as_string () : "",
22068 die
->attrs
[i
].canonical_string_p () ? "is" : "not");
22071 if (die
->attrs
[i
].as_boolean ())
22072 gdb_printf (f
, "flag: TRUE");
22074 gdb_printf (f
, "flag: FALSE");
22076 case DW_FORM_flag_present
:
22077 gdb_printf (f
, "flag: TRUE");
22079 case DW_FORM_indirect
:
22080 /* The reader will have reduced the indirect form to
22081 the "base form" so this form should not occur. */
22083 "unexpected attribute form: DW_FORM_indirect");
22085 case DW_FORM_sdata
:
22086 case DW_FORM_implicit_const
:
22087 gdb_printf (f
, "constant: %s",
22088 plongest (die
->attrs
[i
].as_signed ()));
22091 gdb_printf (f
, "unsupported attribute form: %d.",
22092 die
->attrs
[i
].form
);
22095 gdb_printf (f
, "\n");
22100 dump_die_for_error (struct die_info
*die
)
22102 dump_die_shallow (gdb_stderr
, 0, die
);
22106 dump_die_1 (struct ui_file
*f
, int level
, int max_level
, struct die_info
*die
)
22108 int indent
= level
* 4;
22110 gdb_assert (die
!= NULL
);
22112 if (level
>= max_level
)
22115 dump_die_shallow (f
, indent
, die
);
22117 if (die
->child
!= NULL
)
22119 gdb_printf (f
, "%*s Children:", indent
, "");
22120 if (level
+ 1 < max_level
)
22122 gdb_printf (f
, "\n");
22123 dump_die_1 (f
, level
+ 1, max_level
, die
->child
);
22128 " [not printed, max nesting level reached]\n");
22132 if (die
->sibling
!= NULL
&& level
> 0)
22134 dump_die_1 (f
, level
, max_level
, die
->sibling
);
22138 /* This is called from the pdie macro in gdbinit.in.
22139 It's not static so gcc will keep a copy callable from gdb. */
22142 dump_die (struct die_info
*die
, int max_level
)
22144 dump_die_1 (gdb_stdlog
, 0, max_level
, die
);
22148 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
22152 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
22153 to_underlying (die
->sect_off
),
22159 /* Follow reference or signature attribute ATTR of SRC_DIE.
22160 On entry *REF_CU is the CU of SRC_DIE.
22161 On exit *REF_CU is the CU of the result. */
22163 static struct die_info
*
22164 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22165 struct dwarf2_cu
**ref_cu
)
22167 struct die_info
*die
;
22169 if (attr
->form_is_ref ())
22170 die
= follow_die_ref (src_die
, attr
, ref_cu
);
22171 else if (attr
->form
== DW_FORM_ref_sig8
)
22172 die
= follow_die_sig (src_die
, attr
, ref_cu
);
22175 dump_die_for_error (src_die
);
22176 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22177 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22183 /* Follow reference OFFSET.
22184 On entry *REF_CU is the CU of the source die referencing OFFSET.
22185 On exit *REF_CU is the CU of the result.
22186 Returns NULL if OFFSET is invalid. */
22188 static struct die_info
*
22189 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
22190 struct dwarf2_cu
**ref_cu
)
22192 struct die_info temp_die
;
22193 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
22194 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22196 gdb_assert (cu
->per_cu
!= NULL
);
22200 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22201 "source CU contains target offset: %d",
22202 sect_offset_str (cu
->per_cu
->sect_off
),
22203 sect_offset_str (sect_off
),
22204 cu
->header
.offset_in_cu_p (sect_off
));
22206 if (cu
->per_cu
->is_debug_types
)
22208 /* .debug_types CUs cannot reference anything outside their CU.
22209 If they need to, they have to reference a signatured type via
22210 DW_FORM_ref_sig8. */
22211 if (!cu
->header
.offset_in_cu_p (sect_off
))
22214 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
22215 || !cu
->header
.offset_in_cu_p (sect_off
))
22217 struct dwarf2_per_cu_data
*per_cu
;
22219 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
22220 per_objfile
->per_bfd
);
22222 dwarf_read_debug_printf_v ("target CU offset: %s, "
22223 "target CU DIEs loaded: %d",
22224 sect_offset_str (per_cu
->sect_off
),
22225 per_objfile
->get_cu (per_cu
) != nullptr);
22227 /* If necessary, add it to the queue and load its DIEs.
22229 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22230 it doesn't mean they are currently loaded. Since we require them
22231 to be loaded, we must check for ourselves. */
22232 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->per_cu
->lang
)
22233 || per_objfile
->get_cu (per_cu
) == nullptr)
22234 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
22235 false, cu
->per_cu
->lang
);
22237 target_cu
= per_objfile
->get_cu (per_cu
);
22238 gdb_assert (target_cu
!= nullptr);
22240 else if (cu
->dies
== NULL
)
22242 /* We're loading full DIEs during partial symbol reading. */
22243 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
22247 *ref_cu
= target_cu
;
22248 temp_die
.sect_off
= sect_off
;
22250 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
22252 to_underlying (sect_off
));
22255 /* Follow reference attribute ATTR of SRC_DIE.
22256 On entry *REF_CU is the CU of SRC_DIE.
22257 On exit *REF_CU is the CU of the result. */
22259 static struct die_info
*
22260 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
22261 struct dwarf2_cu
**ref_cu
)
22263 sect_offset sect_off
= attr
->get_ref_die_offset ();
22264 struct dwarf2_cu
*cu
= *ref_cu
;
22265 struct die_info
*die
;
22267 die
= follow_die_offset (sect_off
,
22268 (attr
->form
== DW_FORM_GNU_ref_alt
22269 || cu
->per_cu
->is_dwz
),
22272 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22273 "at %s [in module %s]"),
22274 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
22275 objfile_name (cu
->per_objfile
->objfile
));
22282 struct dwarf2_locexpr_baton
22283 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
22284 dwarf2_per_cu_data
*per_cu
,
22285 dwarf2_per_objfile
*per_objfile
,
22286 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
22287 bool resolve_abstract_p
)
22289 struct die_info
*die
;
22290 struct attribute
*attr
;
22291 struct dwarf2_locexpr_baton retval
;
22292 struct objfile
*objfile
= per_objfile
->objfile
;
22294 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22296 cu
= load_cu (per_cu
, per_objfile
, false);
22300 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22301 Instead just throw an error, not much else we can do. */
22302 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22303 sect_offset_str (sect_off
), objfile_name (objfile
));
22306 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22308 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22309 sect_offset_str (sect_off
), objfile_name (objfile
));
22311 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22312 if (!attr
&& resolve_abstract_p
22313 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
22314 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
22316 CORE_ADDR pc
= get_frame_pc ();
22317 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
22318 struct gdbarch
*gdbarch
= objfile
->arch ();
22320 for (const auto &cand_off
22321 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
22323 struct dwarf2_cu
*cand_cu
= cu
;
22324 struct die_info
*cand
22325 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
22328 || cand
->parent
->tag
!= DW_TAG_subprogram
)
22331 CORE_ADDR pc_low
, pc_high
;
22332 get_scope_pc_bounds (cand
->parent
, &pc_low
, &pc_high
, cu
);
22333 if (pc_low
== ((CORE_ADDR
) -1))
22335 pc_low
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_low
+ baseaddr
);
22336 pc_high
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_high
+ baseaddr
);
22337 if (!(pc_low
<= pc
&& pc
< pc_high
))
22341 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22348 /* DWARF: "If there is no such attribute, then there is no effect.".
22349 DATA is ignored if SIZE is 0. */
22351 retval
.data
= NULL
;
22354 else if (attr
->form_is_section_offset ())
22356 struct dwarf2_loclist_baton loclist_baton
;
22357 CORE_ADDR pc
= get_frame_pc ();
22360 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
22362 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
22364 retval
.size
= size
;
22368 if (!attr
->form_is_block ())
22369 error (_("Dwarf Error: DIE at %s referenced in module %s "
22370 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22371 sect_offset_str (sect_off
), objfile_name (objfile
));
22373 struct dwarf_block
*block
= attr
->as_block ();
22374 retval
.data
= block
->data
;
22375 retval
.size
= block
->size
;
22377 retval
.per_objfile
= per_objfile
;
22378 retval
.per_cu
= cu
->per_cu
;
22380 per_objfile
->age_comp_units ();
22387 struct dwarf2_locexpr_baton
22388 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
22389 dwarf2_per_cu_data
*per_cu
,
22390 dwarf2_per_objfile
*per_objfile
,
22391 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
22393 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
22395 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
22399 /* Write a constant of a given type as target-ordered bytes into
22402 static const gdb_byte
*
22403 write_constant_as_bytes (struct obstack
*obstack
,
22404 enum bfd_endian byte_order
,
22411 *len
= TYPE_LENGTH (type
);
22412 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22413 store_unsigned_integer (result
, *len
, byte_order
, value
);
22421 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
22422 dwarf2_per_cu_data
*per_cu
,
22423 dwarf2_per_objfile
*per_objfile
,
22427 struct die_info
*die
;
22428 struct attribute
*attr
;
22429 const gdb_byte
*result
= NULL
;
22432 enum bfd_endian byte_order
;
22433 struct objfile
*objfile
= per_objfile
->objfile
;
22435 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22437 cu
= load_cu (per_cu
, per_objfile
, false);
22441 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22442 Instead just throw an error, not much else we can do. */
22443 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22444 sect_offset_str (sect_off
), objfile_name (objfile
));
22447 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22449 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22450 sect_offset_str (sect_off
), objfile_name (objfile
));
22452 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
22456 byte_order
= (bfd_big_endian (objfile
->obfd
)
22457 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
22459 switch (attr
->form
)
22462 case DW_FORM_addrx
:
22463 case DW_FORM_GNU_addr_index
:
22467 *len
= cu
->header
.addr_size
;
22468 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22469 store_unsigned_integer (tem
, *len
, byte_order
, attr
->as_address ());
22473 case DW_FORM_string
:
22476 case DW_FORM_GNU_str_index
:
22477 case DW_FORM_GNU_strp_alt
:
22478 /* The string is already allocated on the objfile obstack, point
22481 const char *attr_name
= attr
->as_string ();
22482 result
= (const gdb_byte
*) attr_name
;
22483 *len
= strlen (attr_name
);
22486 case DW_FORM_block1
:
22487 case DW_FORM_block2
:
22488 case DW_FORM_block4
:
22489 case DW_FORM_block
:
22490 case DW_FORM_exprloc
:
22491 case DW_FORM_data16
:
22493 struct dwarf_block
*block
= attr
->as_block ();
22494 result
= block
->data
;
22495 *len
= block
->size
;
22499 /* The DW_AT_const_value attributes are supposed to carry the
22500 symbol's value "represented as it would be on the target
22501 architecture." By the time we get here, it's already been
22502 converted to host endianness, so we just need to sign- or
22503 zero-extend it as appropriate. */
22504 case DW_FORM_data1
:
22505 type
= die_type (die
, cu
);
22506 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
22507 if (result
== NULL
)
22508 result
= write_constant_as_bytes (obstack
, byte_order
,
22511 case DW_FORM_data2
:
22512 type
= die_type (die
, cu
);
22513 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
22514 if (result
== NULL
)
22515 result
= write_constant_as_bytes (obstack
, byte_order
,
22518 case DW_FORM_data4
:
22519 type
= die_type (die
, cu
);
22520 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
22521 if (result
== NULL
)
22522 result
= write_constant_as_bytes (obstack
, byte_order
,
22525 case DW_FORM_data8
:
22526 type
= die_type (die
, cu
);
22527 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
22528 if (result
== NULL
)
22529 result
= write_constant_as_bytes (obstack
, byte_order
,
22533 case DW_FORM_sdata
:
22534 case DW_FORM_implicit_const
:
22535 type
= die_type (die
, cu
);
22536 result
= write_constant_as_bytes (obstack
, byte_order
,
22537 type
, attr
->as_signed (), len
);
22540 case DW_FORM_udata
:
22541 type
= die_type (die
, cu
);
22542 result
= write_constant_as_bytes (obstack
, byte_order
,
22543 type
, attr
->as_unsigned (), len
);
22547 complaint (_("unsupported const value attribute form: '%s'"),
22548 dwarf_form_name (attr
->form
));
22558 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
22559 dwarf2_per_cu_data
*per_cu
,
22560 dwarf2_per_objfile
*per_objfile
,
22561 const char **var_name
)
22563 struct die_info
*die
;
22565 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22567 cu
= load_cu (per_cu
, per_objfile
, false);
22572 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22576 if (var_name
!= nullptr)
22577 *var_name
= var_decl_name (die
, cu
);
22578 return die_type (die
, cu
);
22584 dwarf2_get_die_type (cu_offset die_offset
,
22585 dwarf2_per_cu_data
*per_cu
,
22586 dwarf2_per_objfile
*per_objfile
)
22588 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
22589 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
22592 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22593 On entry *REF_CU is the CU of SRC_DIE.
22594 On exit *REF_CU is the CU of the result.
22595 Returns NULL if the referenced DIE isn't found. */
22597 static struct die_info
*
22598 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
22599 struct dwarf2_cu
**ref_cu
)
22601 struct die_info temp_die
;
22602 struct dwarf2_cu
*sig_cu
;
22603 struct die_info
*die
;
22604 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
22607 /* While it might be nice to assert sig_type->type == NULL here,
22608 we can get here for DW_AT_imported_declaration where we need
22609 the DIE not the type. */
22611 /* If necessary, add it to the queue and load its DIEs.
22613 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22614 it doesn't mean they are currently loaded. Since we require them
22615 to be loaded, we must check for ourselves. */
22616 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
22618 || per_objfile
->get_cu (sig_type
) == nullptr)
22619 read_signatured_type (sig_type
, per_objfile
);
22621 sig_cu
= per_objfile
->get_cu (sig_type
);
22622 gdb_assert (sig_cu
!= NULL
);
22623 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
22624 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
22625 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
22626 to_underlying (temp_die
.sect_off
));
22629 /* For .gdb_index version 7 keep track of included TUs.
22630 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22631 if (per_objfile
->per_bfd
->index_table
!= NULL
22632 && per_objfile
->per_bfd
->index_table
->version
<= 7)
22634 (*ref_cu
)->per_cu
->imported_symtabs_push (sig_cu
->per_cu
);
22644 /* Follow signatured type referenced by ATTR in SRC_DIE.
22645 On entry *REF_CU is the CU of SRC_DIE.
22646 On exit *REF_CU is the CU of the result.
22647 The result is the DIE of the type.
22648 If the referenced type cannot be found an error is thrown. */
22650 static struct die_info
*
22651 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22652 struct dwarf2_cu
**ref_cu
)
22654 ULONGEST signature
= attr
->as_signature ();
22655 struct signatured_type
*sig_type
;
22656 struct die_info
*die
;
22658 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
22660 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
22661 /* sig_type will be NULL if the signatured type is missing from
22663 if (sig_type
== NULL
)
22665 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22666 " from DIE at %s [in module %s]"),
22667 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22668 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22671 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
22674 dump_die_for_error (src_die
);
22675 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22676 " from DIE at %s [in module %s]"),
22677 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22678 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22684 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22685 reading in and processing the type unit if necessary. */
22687 static struct type
*
22688 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
22689 struct dwarf2_cu
*cu
)
22691 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22692 struct signatured_type
*sig_type
;
22693 struct dwarf2_cu
*type_cu
;
22694 struct die_info
*type_die
;
22697 sig_type
= lookup_signatured_type (cu
, signature
);
22698 /* sig_type will be NULL if the signatured type is missing from
22700 if (sig_type
== NULL
)
22702 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22703 " from DIE at %s [in module %s]"),
22704 hex_string (signature
), sect_offset_str (die
->sect_off
),
22705 objfile_name (per_objfile
->objfile
));
22706 return build_error_marker_type (cu
, die
);
22709 /* If we already know the type we're done. */
22710 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
22711 if (type
!= nullptr)
22715 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
22716 if (type_die
!= NULL
)
22718 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22719 is created. This is important, for example, because for c++ classes
22720 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22721 type
= read_type_die (type_die
, type_cu
);
22724 complaint (_("Dwarf Error: Cannot build signatured type %s"
22725 " referenced from DIE at %s [in module %s]"),
22726 hex_string (signature
), sect_offset_str (die
->sect_off
),
22727 objfile_name (per_objfile
->objfile
));
22728 type
= build_error_marker_type (cu
, die
);
22733 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22734 " from DIE at %s [in module %s]"),
22735 hex_string (signature
), sect_offset_str (die
->sect_off
),
22736 objfile_name (per_objfile
->objfile
));
22737 type
= build_error_marker_type (cu
, die
);
22740 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
22745 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22746 reading in and processing the type unit if necessary. */
22748 static struct type
*
22749 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
22750 struct dwarf2_cu
*cu
) /* ARI: editCase function */
22752 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22753 if (attr
->form_is_ref ())
22755 struct dwarf2_cu
*type_cu
= cu
;
22756 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
22758 return read_type_die (type_die
, type_cu
);
22760 else if (attr
->form
== DW_FORM_ref_sig8
)
22762 return get_signatured_type (die
, attr
->as_signature (), cu
);
22766 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22768 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22769 " at %s [in module %s]"),
22770 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
22771 objfile_name (per_objfile
->objfile
));
22772 return build_error_marker_type (cu
, die
);
22776 /* Load the DIEs associated with type unit PER_CU into memory. */
22779 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
22780 dwarf2_per_objfile
*per_objfile
)
22782 struct signatured_type
*sig_type
;
22784 /* We have the per_cu, but we need the signatured_type.
22785 Fortunately this is an easy translation. */
22786 gdb_assert (per_cu
->is_debug_types
);
22787 sig_type
= (struct signatured_type
*) per_cu
;
22789 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
22791 read_signatured_type (sig_type
, per_objfile
);
22793 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
22796 /* Read in a signatured type and build its CU and DIEs.
22797 If the type is a stub for the real type in a DWO file,
22798 read in the real type from the DWO file as well. */
22801 read_signatured_type (signatured_type
*sig_type
,
22802 dwarf2_per_objfile
*per_objfile
)
22804 gdb_assert (sig_type
->is_debug_types
);
22805 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
22807 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
22809 if (!reader
.dummy_p
)
22811 struct dwarf2_cu
*cu
= reader
.cu
;
22812 const gdb_byte
*info_ptr
= reader
.info_ptr
;
22814 gdb_assert (cu
->die_hash
== NULL
);
22816 htab_create_alloc_ex (cu
->header
.length
/ 12,
22820 &cu
->comp_unit_obstack
,
22821 hashtab_obstack_allocate
,
22822 dummy_obstack_deallocate
);
22824 if (reader
.comp_unit_die
->has_children
)
22825 reader
.comp_unit_die
->child
22826 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
22827 reader
.comp_unit_die
);
22828 cu
->dies
= reader
.comp_unit_die
;
22829 /* comp_unit_die is not stored in die_hash, no need. */
22831 /* We try not to read any attributes in this function, because
22832 not all CUs needed for references have been loaded yet, and
22833 symbol table processing isn't initialized. But we have to
22834 set the CU language, or we won't be able to build types
22835 correctly. Similarly, if we do not read the producer, we can
22836 not apply producer-specific interpretation. */
22837 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
22842 sig_type
->tu_read
= 1;
22845 /* Decode simple location descriptions.
22846 Given a pointer to a dwarf block that defines a location, compute
22847 the location and return the value. If COMPUTED is non-null, it is
22848 set to true to indicate that decoding was successful, and false
22849 otherwise. If COMPUTED is null, then this function may emit a
22853 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
, bool *computed
)
22855 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
22857 size_t size
= blk
->size
;
22858 const gdb_byte
*data
= blk
->data
;
22859 CORE_ADDR stack
[64];
22861 unsigned int bytes_read
, unsnd
;
22864 if (computed
!= nullptr)
22870 stack
[++stacki
] = 0;
22909 stack
[++stacki
] = op
- DW_OP_lit0
;
22944 stack
[++stacki
] = op
- DW_OP_reg0
;
22947 if (computed
== nullptr)
22948 dwarf2_complex_location_expr_complaint ();
22955 unsnd
= read_unsigned_leb128 (NULL
, (data
+ i
), &bytes_read
);
22957 stack
[++stacki
] = unsnd
;
22960 if (computed
== nullptr)
22961 dwarf2_complex_location_expr_complaint ();
22968 stack
[++stacki
] = cu
->header
.read_address (objfile
->obfd
, &data
[i
],
22973 case DW_OP_const1u
:
22974 stack
[++stacki
] = read_1_byte (objfile
->obfd
, &data
[i
]);
22978 case DW_OP_const1s
:
22979 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
, &data
[i
]);
22983 case DW_OP_const2u
:
22984 stack
[++stacki
] = read_2_bytes (objfile
->obfd
, &data
[i
]);
22988 case DW_OP_const2s
:
22989 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
, &data
[i
]);
22993 case DW_OP_const4u
:
22994 stack
[++stacki
] = read_4_bytes (objfile
->obfd
, &data
[i
]);
22998 case DW_OP_const4s
:
22999 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
, &data
[i
]);
23003 case DW_OP_const8u
:
23004 stack
[++stacki
] = read_8_bytes (objfile
->obfd
, &data
[i
]);
23009 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
23015 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
23020 stack
[stacki
+ 1] = stack
[stacki
];
23025 stack
[stacki
- 1] += stack
[stacki
];
23029 case DW_OP_plus_uconst
:
23030 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
23036 stack
[stacki
- 1] -= stack
[stacki
];
23041 /* If we're not the last op, then we definitely can't encode
23042 this using GDB's address_class enum. This is valid for partial
23043 global symbols, although the variable's address will be bogus
23047 if (computed
== nullptr)
23048 dwarf2_complex_location_expr_complaint ();
23054 case DW_OP_GNU_push_tls_address
:
23055 case DW_OP_form_tls_address
:
23056 /* The top of the stack has the offset from the beginning
23057 of the thread control block at which the variable is located. */
23058 /* Nothing should follow this operator, so the top of stack would
23060 /* This is valid for partial global symbols, but the variable's
23061 address will be bogus in the psymtab. Make it always at least
23062 non-zero to not look as a variable garbage collected by linker
23063 which have DW_OP_addr 0. */
23066 if (computed
== nullptr)
23067 dwarf2_complex_location_expr_complaint ();
23074 case DW_OP_GNU_uninit
:
23075 if (computed
!= nullptr)
23080 case DW_OP_GNU_addr_index
:
23081 case DW_OP_GNU_const_index
:
23082 stack
[++stacki
] = read_addr_index_from_leb128 (cu
, &data
[i
],
23088 if (computed
== nullptr)
23090 const char *name
= get_DW_OP_name (op
);
23093 complaint (_("unsupported stack op: '%s'"),
23096 complaint (_("unsupported stack op: '%02x'"),
23100 return (stack
[stacki
]);
23103 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23104 outside of the allocated space. Also enforce minimum>0. */
23105 if (stacki
>= ARRAY_SIZE (stack
) - 1)
23107 if (computed
== nullptr)
23108 complaint (_("location description stack overflow"));
23114 if (computed
== nullptr)
23115 complaint (_("location description stack underflow"));
23120 if (computed
!= nullptr)
23122 return (stack
[stacki
]);
23125 /* memory allocation interface */
23127 static struct dwarf_block
*
23128 dwarf_alloc_block (struct dwarf2_cu
*cu
)
23130 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
23133 static struct die_info
*
23134 dwarf_alloc_die (struct dwarf2_cu
*cu
, int num_attrs
)
23136 struct die_info
*die
;
23137 size_t size
= sizeof (struct die_info
);
23140 size
+= (num_attrs
- 1) * sizeof (struct attribute
);
23142 die
= (struct die_info
*) obstack_alloc (&cu
->comp_unit_obstack
, size
);
23143 memset (die
, 0, sizeof (struct die_info
));
23149 /* Macro support. */
23151 /* An overload of dwarf_decode_macros that finds the correct section
23152 and ensures it is read in before calling the other overload. */
23155 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
23156 int section_is_gnu
)
23158 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23159 struct objfile
*objfile
= per_objfile
->objfile
;
23160 const struct line_header
*lh
= cu
->line_header
;
23161 unsigned int offset_size
= cu
->header
.offset_size
;
23162 struct dwarf2_section_info
*section
;
23163 const char *section_name
;
23165 if (cu
->dwo_unit
!= nullptr)
23167 if (section_is_gnu
)
23169 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
23170 section_name
= ".debug_macro.dwo";
23174 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
23175 section_name
= ".debug_macinfo.dwo";
23180 if (section_is_gnu
)
23182 section
= &per_objfile
->per_bfd
->macro
;
23183 section_name
= ".debug_macro";
23187 section
= &per_objfile
->per_bfd
->macinfo
;
23188 section_name
= ".debug_macinfo";
23192 section
->read (objfile
);
23193 if (section
->buffer
== nullptr)
23195 complaint (_("missing %s section"), section_name
);
23199 buildsym_compunit
*builder
= cu
->get_builder ();
23201 struct dwarf2_section_info
*str_offsets_section
;
23202 struct dwarf2_section_info
*str_section
;
23203 gdb::optional
<ULONGEST
> str_offsets_base
;
23205 if (cu
->dwo_unit
!= nullptr)
23207 str_offsets_section
= &cu
->dwo_unit
->dwo_file
23208 ->sections
.str_offsets
;
23209 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
23210 str_offsets_base
= cu
->header
.addr_size
;
23214 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
23215 str_section
= &per_objfile
->per_bfd
->str
;
23216 str_offsets_base
= cu
->str_offsets_base
;
23219 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
23220 offset_size
, offset
, str_section
, str_offsets_section
,
23221 str_offsets_base
, section_is_gnu
);
23224 /* Return the .debug_loc section to use for CU.
23225 For DWO files use .debug_loc.dwo. */
23227 static struct dwarf2_section_info
*
23228 cu_debug_loc_section (struct dwarf2_cu
*cu
)
23230 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23234 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23236 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
23238 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
23239 : &per_objfile
->per_bfd
->loc
);
23242 /* Return the .debug_rnglists section to use for CU. */
23243 static struct dwarf2_section_info
*
23244 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
23246 if (cu
->header
.version
< 5)
23247 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23248 cu
->header
.version
);
23249 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
23251 /* Make sure we read the .debug_rnglists section from the file that
23252 contains the DW_AT_ranges attribute we are reading. Normally that
23253 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23254 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23256 if (cu
->dwo_unit
!= nullptr
23257 && tag
!= DW_TAG_compile_unit
23258 && tag
!= DW_TAG_skeleton_unit
)
23260 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23262 if (sections
->rnglists
.size
> 0)
23263 return §ions
->rnglists
;
23265 error (_(".debug_rnglists section is missing from .dwo file."));
23267 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
23270 /* A helper function that fills in a dwarf2_loclist_baton. */
23273 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
23274 struct dwarf2_loclist_baton
*baton
,
23275 const struct attribute
*attr
)
23277 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23278 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23280 section
->read (per_objfile
->objfile
);
23282 baton
->per_objfile
= per_objfile
;
23283 baton
->per_cu
= cu
->per_cu
;
23284 gdb_assert (baton
->per_cu
);
23285 /* We don't know how long the location list is, but make sure we
23286 don't run off the edge of the section. */
23287 baton
->size
= section
->size
- attr
->as_unsigned ();
23288 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
23289 if (cu
->base_address
.has_value ())
23290 baton
->base_address
= *cu
->base_address
;
23292 baton
->base_address
= 0;
23293 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
23297 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
23298 struct dwarf2_cu
*cu
, int is_block
)
23300 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23301 struct objfile
*objfile
= per_objfile
->objfile
;
23302 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23304 if (attr
->form_is_section_offset ()
23305 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23306 the section. If so, fall through to the complaint in the
23308 && attr
->as_unsigned () < section
->get_size (objfile
))
23310 struct dwarf2_loclist_baton
*baton
;
23312 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
23314 fill_in_loclist_baton (cu
, baton
, attr
);
23316 if (!cu
->base_address
.has_value ())
23317 complaint (_("Location list used without "
23318 "specifying the CU base address."));
23320 sym
->set_aclass_index ((is_block
23321 ? dwarf2_loclist_block_index
23322 : dwarf2_loclist_index
));
23323 SYMBOL_LOCATION_BATON (sym
) = baton
;
23327 struct dwarf2_locexpr_baton
*baton
;
23329 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
23330 baton
->per_objfile
= per_objfile
;
23331 baton
->per_cu
= cu
->per_cu
;
23332 gdb_assert (baton
->per_cu
);
23334 if (attr
->form_is_block ())
23336 /* Note that we're just copying the block's data pointer
23337 here, not the actual data. We're still pointing into the
23338 info_buffer for SYM's objfile; right now we never release
23339 that buffer, but when we do clean up properly this may
23341 struct dwarf_block
*block
= attr
->as_block ();
23342 baton
->size
= block
->size
;
23343 baton
->data
= block
->data
;
23347 dwarf2_invalid_attrib_class_complaint ("location description",
23348 sym
->natural_name ());
23352 sym
->set_aclass_index ((is_block
23353 ? dwarf2_locexpr_block_index
23354 : dwarf2_locexpr_index
));
23355 SYMBOL_LOCATION_BATON (sym
) = baton
;
23361 const comp_unit_head
*
23362 dwarf2_per_cu_data::get_header () const
23364 if (!m_header_read_in
)
23366 const gdb_byte
*info_ptr
23367 = this->section
->buffer
+ to_underlying (this->sect_off
);
23369 memset (&m_header
, 0, sizeof (m_header
));
23371 read_comp_unit_head (&m_header
, info_ptr
, this->section
,
23372 rcuh_kind::COMPILE
);
23374 m_header_read_in
= true;
23383 dwarf2_per_cu_data::addr_size () const
23385 return this->get_header ()->addr_size
;
23391 dwarf2_per_cu_data::offset_size () const
23393 return this->get_header ()->offset_size
;
23399 dwarf2_per_cu_data::ref_addr_size () const
23401 const comp_unit_head
*header
= this->get_header ();
23403 if (header
->version
== 2)
23404 return header
->addr_size
;
23406 return header
->offset_size
;
23409 /* A helper function for dwarf2_find_containing_comp_unit that returns
23410 the index of the result, and that searches a vector. It will
23411 return a result even if the offset in question does not actually
23412 occur in any CU. This is separate so that it can be unit
23416 dwarf2_find_containing_comp_unit
23417 (sect_offset sect_off
,
23418 unsigned int offset_in_dwz
,
23419 const std::vector
<dwarf2_per_cu_data_up
> &all_comp_units
)
23424 high
= all_comp_units
.size () - 1;
23427 struct dwarf2_per_cu_data
*mid_cu
;
23428 int mid
= low
+ (high
- low
) / 2;
23430 mid_cu
= all_comp_units
[mid
].get ();
23431 if (mid_cu
->is_dwz
> offset_in_dwz
23432 || (mid_cu
->is_dwz
== offset_in_dwz
23433 && mid_cu
->sect_off
+ mid_cu
->length
> sect_off
))
23438 gdb_assert (low
== high
);
23442 /* Locate the .debug_info compilation unit from CU's objfile which contains
23443 the DIE at OFFSET. Raises an error on failure. */
23445 static struct dwarf2_per_cu_data
*
23446 dwarf2_find_containing_comp_unit (sect_offset sect_off
,
23447 unsigned int offset_in_dwz
,
23448 dwarf2_per_bfd
*per_bfd
)
23450 int low
= dwarf2_find_containing_comp_unit
23451 (sect_off
, offset_in_dwz
, per_bfd
->all_comp_units
);
23452 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_comp_units
[low
].get ();
23454 if (this_cu
->is_dwz
!= offset_in_dwz
|| this_cu
->sect_off
> sect_off
)
23456 if (low
== 0 || this_cu
->is_dwz
!= offset_in_dwz
)
23457 error (_("Dwarf Error: could not find partial DIE containing "
23458 "offset %s [in module %s]"),
23459 sect_offset_str (sect_off
),
23460 bfd_get_filename (per_bfd
->obfd
));
23462 gdb_assert (per_bfd
->all_comp_units
[low
-1]->sect_off
23464 return per_bfd
->all_comp_units
[low
- 1].get ();
23468 if (low
== per_bfd
->all_comp_units
.size () - 1
23469 && sect_off
>= this_cu
->sect_off
+ this_cu
->length
)
23470 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off
));
23471 gdb_assert (sect_off
< this_cu
->sect_off
+ this_cu
->length
);
23478 namespace selftests
{
23479 namespace find_containing_comp_unit
{
23484 dwarf2_per_cu_data_up
one (new dwarf2_per_cu_data
);
23485 dwarf2_per_cu_data
*one_ptr
= one
.get ();
23486 dwarf2_per_cu_data_up
two (new dwarf2_per_cu_data
);
23487 dwarf2_per_cu_data
*two_ptr
= two
.get ();
23488 dwarf2_per_cu_data_up
three (new dwarf2_per_cu_data
);
23489 dwarf2_per_cu_data
*three_ptr
= three
.get ();
23490 dwarf2_per_cu_data_up
four (new dwarf2_per_cu_data
);
23491 dwarf2_per_cu_data
*four_ptr
= four
.get ();
23494 two
->sect_off
= sect_offset (one
->length
);
23499 four
->sect_off
= sect_offset (three
->length
);
23503 std::vector
<dwarf2_per_cu_data_up
> units
;
23504 units
.push_back (std::move (one
));
23505 units
.push_back (std::move (two
));
23506 units
.push_back (std::move (three
));
23507 units
.push_back (std::move (four
));
23511 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 0, units
);
23512 SELF_CHECK (units
[result
].get () == one_ptr
);
23513 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 0, units
);
23514 SELF_CHECK (units
[result
].get () == one_ptr
);
23515 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 0, units
);
23516 SELF_CHECK (units
[result
].get () == two_ptr
);
23518 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 1, units
);
23519 SELF_CHECK (units
[result
].get () == three_ptr
);
23520 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 1, units
);
23521 SELF_CHECK (units
[result
].get () == three_ptr
);
23522 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 1, units
);
23523 SELF_CHECK (units
[result
].get () == four_ptr
);
23529 #endif /* GDB_SELF_TEST */
23531 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23534 prepare_one_comp_unit (struct dwarf2_cu
*cu
, struct die_info
*comp_unit_die
,
23535 enum language pretend_language
)
23537 struct attribute
*attr
;
23539 cu
->producer
= dwarf2_string_attr (comp_unit_die
, DW_AT_producer
, cu
);
23541 /* Set the language we're debugging. */
23542 attr
= dwarf2_attr (comp_unit_die
, DW_AT_language
, cu
);
23543 if (cu
->producer
!= nullptr
23544 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
23546 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23547 attribute is not standardised yet. As a workaround for the
23548 language detection we fall back to the DW_AT_producer
23550 cu
->per_cu
->lang
= language_opencl
;
23552 else if (cu
->producer
!= nullptr
23553 && strstr (cu
->producer
, "GNU Go ") != NULL
)
23555 /* Similar hack for Go. */
23556 cu
->per_cu
->lang
= language_go
;
23558 else if (attr
!= nullptr)
23559 cu
->per_cu
->lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
23561 cu
->per_cu
->lang
= pretend_language
;
23562 cu
->language_defn
= language_def (cu
->per_cu
->lang
);
23568 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
23570 auto it
= m_dwarf2_cus
.find (per_cu
);
23571 if (it
== m_dwarf2_cus
.end ())
23580 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_cu
*cu
)
23582 gdb_assert (this->get_cu (per_cu
) == nullptr);
23584 m_dwarf2_cus
[per_cu
] = cu
;
23590 dwarf2_per_objfile::age_comp_units ()
23592 dwarf_read_debug_printf_v ("running");
23594 /* This is not expected to be called in the middle of CU expansion. There is
23595 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23596 loaded in memory. Calling age_comp_units while the queue is in use could
23597 make us free the DIEs for a CU that is in the queue and therefore break
23599 gdb_assert (!this->per_bfd
->queue
.has_value ());
23601 /* Start by clearing all marks. */
23602 for (auto pair
: m_dwarf2_cus
)
23603 pair
.second
->clear_mark ();
23605 /* Traverse all CUs, mark them and their dependencies if used recently
23607 for (auto pair
: m_dwarf2_cus
)
23609 dwarf2_cu
*cu
= pair
.second
;
23612 if (cu
->last_used
<= dwarf_max_cache_age
)
23616 /* Delete all CUs still not marked. */
23617 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
23619 dwarf2_cu
*cu
= it
->second
;
23621 if (!cu
->is_marked ())
23623 dwarf_read_debug_printf_v ("deleting old CU %s",
23624 sect_offset_str (cu
->per_cu
->sect_off
));
23626 it
= m_dwarf2_cus
.erase (it
);
23636 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
23638 auto it
= m_dwarf2_cus
.find (per_cu
);
23639 if (it
== m_dwarf2_cus
.end ())
23644 m_dwarf2_cus
.erase (it
);
23647 dwarf2_per_objfile::~dwarf2_per_objfile ()
23652 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23653 We store these in a hash table separate from the DIEs, and preserve them
23654 when the DIEs are flushed out of cache.
23656 The CU "per_cu" pointer is needed because offset alone is not enough to
23657 uniquely identify the type. A file may have multiple .debug_types sections,
23658 or the type may come from a DWO file. Furthermore, while it's more logical
23659 to use per_cu->section+offset, with Fission the section with the data is in
23660 the DWO file but we don't know that section at the point we need it.
23661 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23662 because we can enter the lookup routine, get_die_type_at_offset, from
23663 outside this file, and thus won't necessarily have PER_CU->cu.
23664 Fortunately, PER_CU is stable for the life of the objfile. */
23666 struct dwarf2_per_cu_offset_and_type
23668 const struct dwarf2_per_cu_data
*per_cu
;
23669 sect_offset sect_off
;
23673 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23676 per_cu_offset_and_type_hash (const void *item
)
23678 const struct dwarf2_per_cu_offset_and_type
*ofs
23679 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
23681 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
23684 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23687 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
23689 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
23690 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
23691 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
23692 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
23694 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
23695 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
23698 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23699 table if necessary. For convenience, return TYPE.
23701 The DIEs reading must have careful ordering to:
23702 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23703 reading current DIE.
23704 * Not trying to dereference contents of still incompletely read in types
23705 while reading in other DIEs.
23706 * Enable referencing still incompletely read in types just by a pointer to
23707 the type without accessing its fields.
23709 Therefore caller should follow these rules:
23710 * Try to fetch any prerequisite types we may need to build this DIE type
23711 before building the type and calling set_die_type.
23712 * After building type call set_die_type for current DIE as soon as
23713 possible before fetching more types to complete the current type.
23714 * Make the type as complete as possible before fetching more types. */
23716 static struct type
*
23717 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
23718 bool skip_data_location
)
23720 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23721 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
23722 struct objfile
*objfile
= per_objfile
->objfile
;
23723 struct attribute
*attr
;
23724 struct dynamic_prop prop
;
23726 /* For Ada types, make sure that the gnat-specific data is always
23727 initialized (if not already set). There are a few types where
23728 we should not be doing so, because the type-specific area is
23729 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23730 where the type-specific area is used to store the floatformat).
23731 But this is not a problem, because the gnat-specific information
23732 is actually not needed for these types. */
23733 if (need_gnat_info (cu
)
23734 && type
->code () != TYPE_CODE_FUNC
23735 && type
->code () != TYPE_CODE_FLT
23736 && type
->code () != TYPE_CODE_METHODPTR
23737 && type
->code () != TYPE_CODE_MEMBERPTR
23738 && type
->code () != TYPE_CODE_METHOD
23739 && type
->code () != TYPE_CODE_FIXED_POINT
23740 && !HAVE_GNAT_AUX_INFO (type
))
23741 INIT_GNAT_SPECIFIC (type
);
23743 /* Read DW_AT_allocated and set in type. */
23744 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
23747 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23748 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23749 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
23752 /* Read DW_AT_associated and set in type. */
23753 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
23756 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23757 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23758 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
23761 /* Read DW_AT_rank and set in type. */
23762 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
23765 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23766 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23767 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
23770 /* Read DW_AT_data_location and set in type. */
23771 if (!skip_data_location
)
23773 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
23774 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
23775 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
23778 if (per_objfile
->die_type_hash
== NULL
)
23779 per_objfile
->die_type_hash
23780 = htab_up (htab_create_alloc (127,
23781 per_cu_offset_and_type_hash
,
23782 per_cu_offset_and_type_eq
,
23783 NULL
, xcalloc
, xfree
));
23785 ofs
.per_cu
= cu
->per_cu
;
23786 ofs
.sect_off
= die
->sect_off
;
23788 slot
= (struct dwarf2_per_cu_offset_and_type
**)
23789 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
23791 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23792 sect_offset_str (die
->sect_off
));
23793 *slot
= XOBNEW (&objfile
->objfile_obstack
,
23794 struct dwarf2_per_cu_offset_and_type
);
23799 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23800 or return NULL if the die does not have a saved type. */
23802 static struct type
*
23803 get_die_type_at_offset (sect_offset sect_off
,
23804 dwarf2_per_cu_data
*per_cu
,
23805 dwarf2_per_objfile
*per_objfile
)
23807 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
23809 if (per_objfile
->die_type_hash
== NULL
)
23812 ofs
.per_cu
= per_cu
;
23813 ofs
.sect_off
= sect_off
;
23814 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
23815 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
23822 /* Look up the type for DIE in CU in die_type_hash,
23823 or return NULL if DIE does not have a saved type. */
23825 static struct type
*
23826 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
23828 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
23831 struct cmd_list_element
*set_dwarf_cmdlist
;
23832 struct cmd_list_element
*show_dwarf_cmdlist
;
23835 show_check_physname (struct ui_file
*file
, int from_tty
,
23836 struct cmd_list_element
*c
, const char *value
)
23839 _("Whether to check \"physname\" is %s.\n"),
23843 void _initialize_dwarf2_read ();
23845 _initialize_dwarf2_read ()
23847 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
23849 Set DWARF specific variables.\n\
23850 Configure DWARF variables such as the cache size."),
23852 Show DWARF specific variables.\n\
23853 Show DWARF variables such as the cache size."),
23854 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
23855 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
23857 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
23858 &dwarf_max_cache_age
, _("\
23859 Set the upper bound on the age of cached DWARF compilation units."), _("\
23860 Show the upper bound on the age of cached DWARF compilation units."), _("\
23861 A higher limit means that cached compilation units will be stored\n\
23862 in memory longer, and more total memory will be used. Zero disables\n\
23863 caching, which can slow down startup."),
23865 show_dwarf_max_cache_age
,
23866 &set_dwarf_cmdlist
,
23867 &show_dwarf_cmdlist
);
23869 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
23870 Set debugging of the DWARF reader."), _("\
23871 Show debugging of the DWARF reader."), _("\
23872 When enabled (non-zero), debugging messages are printed during DWARF\n\
23873 reading and symtab expansion. A value of 1 (one) provides basic\n\
23874 information. A value greater than 1 provides more verbose information."),
23877 &setdebuglist
, &showdebuglist
);
23879 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
23880 Set debugging of the DWARF DIE reader."), _("\
23881 Show debugging of the DWARF DIE reader."), _("\
23882 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23883 The value is the maximum depth to print."),
23886 &setdebuglist
, &showdebuglist
);
23888 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
23889 Set debugging of the dwarf line reader."), _("\
23890 Show debugging of the dwarf line reader."), _("\
23891 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23892 A value of 1 (one) provides basic information.\n\
23893 A value greater than 1 provides more verbose information."),
23896 &setdebuglist
, &showdebuglist
);
23898 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
23899 Set cross-checking of \"physname\" code against demangler."), _("\
23900 Show cross-checking of \"physname\" code against demangler."), _("\
23901 When enabled, GDB's internal \"physname\" code is checked against\n\
23903 NULL
, show_check_physname
,
23904 &setdebuglist
, &showdebuglist
);
23906 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23907 no_class
, &use_deprecated_index_sections
, _("\
23908 Set whether to use deprecated gdb_index sections."), _("\
23909 Show whether to use deprecated gdb_index sections."), _("\
23910 When enabled, deprecated .gdb_index sections are used anyway.\n\
23911 Normally they are ignored either because of a missing feature or\n\
23912 performance issue.\n\
23913 Warning: This option must be enabled before gdb reads the file."),
23916 &setlist
, &showlist
);
23918 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23919 &dwarf2_locexpr_funcs
);
23920 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23921 &dwarf2_loclist_funcs
);
23923 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23924 &dwarf2_block_frame_base_locexpr_funcs
);
23925 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23926 &dwarf2_block_frame_base_loclist_funcs
);
23929 selftests::register_test ("dw2_expand_symtabs_matching",
23930 selftests::dw2_expand_symtabs_matching::run_test
);
23931 selftests::register_test ("dwarf2_find_containing_comp_unit",
23932 selftests::find_containing_comp_unit::run_test
);