1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/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 registry
<objfile
>::key
<dwarf2_per_objfile
>
128 dwarf2_objfile_data_key
;
130 /* These are used to store the dwarf2_per_bfd objects.
132 objfiles having the same BFD, which doesn't require relocations, are going to
133 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
135 Other objfiles are not going to share a dwarf2_per_bfd with any other
136 objfiles, so they'll have their own version kept in the _objfile_data_key
138 static const registry
<bfd
>::key
<dwarf2_per_bfd
> dwarf2_per_bfd_bfd_data_key
;
139 static const registry
<objfile
>::key
<dwarf2_per_bfd
>
140 dwarf2_per_bfd_objfile_data_key
;
142 /* The "aclass" indices for various kinds of computed DWARF symbols. */
144 static int dwarf2_locexpr_index
;
145 static int dwarf2_loclist_index
;
146 static int dwarf2_locexpr_block_index
;
147 static int dwarf2_loclist_block_index
;
149 /* Size of .debug_loclists section header for 32-bit DWARF format. */
150 #define LOCLIST_HEADER_SIZE32 12
152 /* Size of .debug_loclists section header for 64-bit DWARF format. */
153 #define LOCLIST_HEADER_SIZE64 20
155 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
156 #define RNGLIST_HEADER_SIZE32 12
158 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
159 #define RNGLIST_HEADER_SIZE64 20
161 /* This is a view into the index that converts from bytes to an
162 offset_type, and allows indexing. Unaligned bytes are specifically
163 allowed here, and handled via unpacking. */
168 offset_view () = default;
170 explicit offset_view (gdb::array_view
<const gdb_byte
> bytes
)
175 /* Extract the INDEXth offset_type from the array. */
176 offset_type
operator[] (size_t index
) const
178 const gdb_byte
*bytes
= &m_bytes
[index
* sizeof (offset_type
)];
179 return (offset_type
) extract_unsigned_integer (bytes
,
180 sizeof (offset_type
),
184 /* Return the number of offset_types in this array. */
187 return m_bytes
.size () / sizeof (offset_type
);
190 /* Return true if this view is empty. */
193 return m_bytes
.empty ();
197 /* The underlying bytes. */
198 gdb::array_view
<const gdb_byte
> m_bytes
;
201 /* A description of the mapped index. The file format is described in
202 a comment by the code that writes the index. */
203 struct mapped_index final
: public mapped_index_base
205 /* Index data format version. */
208 /* The address table data. */
209 gdb::array_view
<const gdb_byte
> address_table
;
211 /* The symbol table, implemented as a hash table. */
212 offset_view symbol_table
;
214 /* A pointer to the constant pool. */
215 gdb::array_view
<const gdb_byte
> constant_pool
;
217 /* Return the index into the constant pool of the name of the IDXth
218 symbol in the symbol table. */
219 offset_type
symbol_name_index (offset_type idx
) const
221 return symbol_table
[2 * idx
];
224 /* Return the index into the constant pool of the CU vector of the
225 IDXth symbol in the symbol table. */
226 offset_type
symbol_vec_index (offset_type idx
) const
228 return symbol_table
[2 * idx
+ 1];
231 bool symbol_name_slot_invalid (offset_type idx
) const override
233 return (symbol_name_index (idx
) == 0
234 && symbol_vec_index (idx
) == 0);
237 /* Convenience method to get at the name of the symbol at IDX in the
239 const char *symbol_name_at
240 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
242 return (const char *) (this->constant_pool
.data ()
243 + symbol_name_index (idx
));
246 size_t symbol_name_count () const override
247 { return this->symbol_table
.size () / 2; }
249 quick_symbol_functions_up
make_quick_functions () const override
;
251 bool version_check () const override
257 /* A description of the mapped .debug_names.
258 Uninitialized map has CU_COUNT 0. */
259 struct mapped_debug_names final
: public mapped_index_base
261 bfd_endian dwarf5_byte_order
;
262 bool dwarf5_is_dwarf64
;
263 bool augmentation_is_gdb
;
265 uint32_t cu_count
= 0;
266 uint32_t tu_count
, bucket_count
, name_count
;
267 const gdb_byte
*cu_table_reordered
, *tu_table_reordered
;
268 const uint32_t *bucket_table_reordered
, *hash_table_reordered
;
269 const gdb_byte
*name_table_string_offs_reordered
;
270 const gdb_byte
*name_table_entry_offs_reordered
;
271 const gdb_byte
*entry_pool
;
278 /* Attribute name DW_IDX_*. */
281 /* Attribute form DW_FORM_*. */
284 /* Value if FORM is DW_FORM_implicit_const. */
285 LONGEST implicit_const
;
287 std::vector
<attr
> attr_vec
;
290 std::unordered_map
<ULONGEST
, index_val
> abbrev_map
;
292 const char *namei_to_name
293 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const;
295 /* Implementation of the mapped_index_base virtual interface, for
296 the name_components cache. */
298 const char *symbol_name_at
299 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
300 { return namei_to_name (idx
, per_objfile
); }
302 size_t symbol_name_count () const override
303 { return this->name_count
; }
305 quick_symbol_functions_up
make_quick_functions () const override
;
308 /* See dwarf2/read.h. */
311 get_dwarf2_per_objfile (struct objfile
*objfile
)
313 return dwarf2_objfile_data_key
.get (objfile
);
316 /* Default names of the debugging sections. */
318 /* Note that if the debugging section has been compressed, it might
319 have a name like .zdebug_info. */
321 const struct dwarf2_debug_sections dwarf2_elf_names
=
323 { ".debug_info", ".zdebug_info" },
324 { ".debug_abbrev", ".zdebug_abbrev" },
325 { ".debug_line", ".zdebug_line" },
326 { ".debug_loc", ".zdebug_loc" },
327 { ".debug_loclists", ".zdebug_loclists" },
328 { ".debug_macinfo", ".zdebug_macinfo" },
329 { ".debug_macro", ".zdebug_macro" },
330 { ".debug_str", ".zdebug_str" },
331 { ".debug_str_offsets", ".zdebug_str_offsets" },
332 { ".debug_line_str", ".zdebug_line_str" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_rnglists", ".zdebug_rnglists" },
335 { ".debug_types", ".zdebug_types" },
336 { ".debug_addr", ".zdebug_addr" },
337 { ".debug_frame", ".zdebug_frame" },
338 { ".eh_frame", NULL
},
339 { ".gdb_index", ".zgdb_index" },
340 { ".debug_names", ".zdebug_names" },
341 { ".debug_aranges", ".zdebug_aranges" },
345 /* List of DWO/DWP sections. */
347 static const struct dwop_section_names
349 struct dwarf2_section_names abbrev_dwo
;
350 struct dwarf2_section_names info_dwo
;
351 struct dwarf2_section_names line_dwo
;
352 struct dwarf2_section_names loc_dwo
;
353 struct dwarf2_section_names loclists_dwo
;
354 struct dwarf2_section_names macinfo_dwo
;
355 struct dwarf2_section_names macro_dwo
;
356 struct dwarf2_section_names rnglists_dwo
;
357 struct dwarf2_section_names str_dwo
;
358 struct dwarf2_section_names str_offsets_dwo
;
359 struct dwarf2_section_names types_dwo
;
360 struct dwarf2_section_names cu_index
;
361 struct dwarf2_section_names tu_index
;
365 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
366 { ".debug_info.dwo", ".zdebug_info.dwo" },
367 { ".debug_line.dwo", ".zdebug_line.dwo" },
368 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
369 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
370 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
371 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
372 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
373 { ".debug_str.dwo", ".zdebug_str.dwo" },
374 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
375 { ".debug_types.dwo", ".zdebug_types.dwo" },
376 { ".debug_cu_index", ".zdebug_cu_index" },
377 { ".debug_tu_index", ".zdebug_tu_index" },
380 /* local data types */
382 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
383 begin with a header, which contains the following information. */
384 struct loclists_rnglists_header
386 /* A 4-byte or 12-byte length containing the length of the
387 set of entries for this compilation unit, not including the
388 length field itself. */
391 /* A 2-byte version identifier. */
394 /* A 1-byte unsigned integer containing the size in bytes of an address on
395 the target system. */
396 unsigned char addr_size
;
398 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
399 on the target system. */
400 unsigned char segment_collector_size
;
402 /* A 4-byte count of the number of offsets that follow the header. */
403 unsigned int offset_entry_count
;
406 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
407 This includes type_unit_group and quick_file_names. */
409 struct stmt_list_hash
411 /* The DWO unit this table is from or NULL if there is none. */
412 struct dwo_unit
*dwo_unit
;
414 /* Offset in .debug_line or .debug_line.dwo. */
415 sect_offset line_sect_off
;
418 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
419 an object of this type. This contains elements of type unit groups
420 that can be shared across objfiles. The non-shareable parts are in
421 type_unit_group_unshareable. */
423 struct type_unit_group
425 /* The data used to construct the hash key. */
426 struct stmt_list_hash hash
{};
429 /* These sections are what may appear in a (real or virtual) DWO file. */
433 struct dwarf2_section_info abbrev
;
434 struct dwarf2_section_info line
;
435 struct dwarf2_section_info loc
;
436 struct dwarf2_section_info loclists
;
437 struct dwarf2_section_info macinfo
;
438 struct dwarf2_section_info macro
;
439 struct dwarf2_section_info rnglists
;
440 struct dwarf2_section_info str
;
441 struct dwarf2_section_info str_offsets
;
442 /* In the case of a virtual DWO file, these two are unused. */
443 struct dwarf2_section_info info
;
444 std::vector
<dwarf2_section_info
> types
;
447 /* CUs/TUs in DWP/DWO files. */
451 /* Backlink to the containing struct dwo_file. */
452 struct dwo_file
*dwo_file
;
454 /* The "id" that distinguishes this CU/TU.
455 .debug_info calls this "dwo_id", .debug_types calls this "signature".
456 Since signatures came first, we stick with it for consistency. */
459 /* The section this CU/TU lives in, in the DWO file. */
460 struct dwarf2_section_info
*section
;
462 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
463 sect_offset sect_off
;
466 /* For types, offset in the type's DIE of the type defined by this TU. */
467 cu_offset type_offset_in_tu
;
470 /* include/dwarf2.h defines the DWP section codes.
471 It defines a max value but it doesn't define a min value, which we
472 use for error checking, so provide one. */
474 enum dwp_v2_section_ids
479 /* Data for one DWO file.
481 This includes virtual DWO files (a virtual DWO file is a DWO file as it
482 appears in a DWP file). DWP files don't really have DWO files per se -
483 comdat folding of types "loses" the DWO file they came from, and from
484 a high level view DWP files appear to contain a mass of random types.
485 However, to maintain consistency with the non-DWP case we pretend DWP
486 files contain virtual DWO files, and we assign each TU with one virtual
487 DWO file (generally based on the line and abbrev section offsets -
488 a heuristic that seems to work in practice). */
492 dwo_file () = default;
493 DISABLE_COPY_AND_ASSIGN (dwo_file
);
495 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
496 For virtual DWO files the name is constructed from the section offsets
497 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
498 from related CU+TUs. */
499 const char *dwo_name
= nullptr;
501 /* The DW_AT_comp_dir attribute. */
502 const char *comp_dir
= nullptr;
504 /* The bfd, when the file is open. Otherwise this is NULL.
505 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
506 gdb_bfd_ref_ptr dbfd
;
508 /* The sections that make up this DWO file.
509 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
510 sections (for lack of a better name). */
511 struct dwo_sections sections
{};
513 /* The CUs in the file.
514 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
515 an extension to handle LLVM's Link Time Optimization output (where
516 multiple source files may be compiled into a single object/dwo pair). */
519 /* Table of TUs in the file.
520 Each element is a struct dwo_unit. */
524 /* These sections are what may appear in a DWP file. */
528 /* These are used by all DWP versions (1, 2 and 5). */
529 struct dwarf2_section_info str
;
530 struct dwarf2_section_info cu_index
;
531 struct dwarf2_section_info tu_index
;
533 /* These are only used by DWP version 2 and version 5 files.
534 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
535 sections are referenced by section number, and are not recorded here.
536 In DWP version 2 or 5 there is at most one copy of all these sections,
537 each section being (effectively) comprised of the concatenation of all of
538 the individual sections that exist in the version 1 format.
539 To keep the code simple we treat each of these concatenated pieces as a
540 section itself (a virtual section?). */
541 struct dwarf2_section_info abbrev
;
542 struct dwarf2_section_info info
;
543 struct dwarf2_section_info line
;
544 struct dwarf2_section_info loc
;
545 struct dwarf2_section_info loclists
;
546 struct dwarf2_section_info macinfo
;
547 struct dwarf2_section_info macro
;
548 struct dwarf2_section_info rnglists
;
549 struct dwarf2_section_info str_offsets
;
550 struct dwarf2_section_info types
;
553 /* These sections are what may appear in a virtual DWO file in DWP version 1.
554 A virtual DWO file is a DWO file as it appears in a DWP file. */
556 struct virtual_v1_dwo_sections
558 struct dwarf2_section_info abbrev
;
559 struct dwarf2_section_info line
;
560 struct dwarf2_section_info loc
;
561 struct dwarf2_section_info macinfo
;
562 struct dwarf2_section_info macro
;
563 struct dwarf2_section_info str_offsets
;
564 /* Each DWP hash table entry records one CU or one TU.
565 That is recorded here, and copied to dwo_unit.section. */
566 struct dwarf2_section_info info_or_types
;
569 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
570 In version 2, the sections of the DWO files are concatenated together
571 and stored in one section of that name. Thus each ELF section contains
572 several "virtual" sections. */
574 struct virtual_v2_or_v5_dwo_sections
576 bfd_size_type abbrev_offset
;
577 bfd_size_type abbrev_size
;
579 bfd_size_type line_offset
;
580 bfd_size_type line_size
;
582 bfd_size_type loc_offset
;
583 bfd_size_type loc_size
;
585 bfd_size_type loclists_offset
;
586 bfd_size_type loclists_size
;
588 bfd_size_type macinfo_offset
;
589 bfd_size_type macinfo_size
;
591 bfd_size_type macro_offset
;
592 bfd_size_type macro_size
;
594 bfd_size_type rnglists_offset
;
595 bfd_size_type rnglists_size
;
597 bfd_size_type str_offsets_offset
;
598 bfd_size_type str_offsets_size
;
600 /* Each DWP hash table entry records one CU or one TU.
601 That is recorded here, and copied to dwo_unit.section. */
602 bfd_size_type info_or_types_offset
;
603 bfd_size_type info_or_types_size
;
606 /* Contents of DWP hash tables. */
608 struct dwp_hash_table
610 uint32_t version
, nr_columns
;
611 uint32_t nr_units
, nr_slots
;
612 const gdb_byte
*hash_table
, *unit_table
;
617 const gdb_byte
*indices
;
621 /* This is indexed by column number and gives the id of the section
623 #define MAX_NR_V2_DWO_SECTIONS \
624 (1 /* .debug_info or .debug_types */ \
625 + 1 /* .debug_abbrev */ \
626 + 1 /* .debug_line */ \
627 + 1 /* .debug_loc */ \
628 + 1 /* .debug_str_offsets */ \
629 + 1 /* .debug_macro or .debug_macinfo */)
630 int section_ids
[MAX_NR_V2_DWO_SECTIONS
];
631 const gdb_byte
*offsets
;
632 const gdb_byte
*sizes
;
636 /* This is indexed by column number and gives the id of the section
638 #define MAX_NR_V5_DWO_SECTIONS \
639 (1 /* .debug_info */ \
640 + 1 /* .debug_abbrev */ \
641 + 1 /* .debug_line */ \
642 + 1 /* .debug_loclists */ \
643 + 1 /* .debug_str_offsets */ \
644 + 1 /* .debug_macro */ \
645 + 1 /* .debug_rnglists */)
646 int section_ids
[MAX_NR_V5_DWO_SECTIONS
];
647 const gdb_byte
*offsets
;
648 const gdb_byte
*sizes
;
653 /* Data for one DWP file. */
657 dwp_file (const char *name_
, gdb_bfd_ref_ptr
&&abfd
)
659 dbfd (std::move (abfd
))
663 /* Name of the file. */
666 /* File format version. */
670 gdb_bfd_ref_ptr dbfd
;
672 /* Section info for this file. */
673 struct dwp_sections sections
{};
675 /* Table of CUs in the file. */
676 const struct dwp_hash_table
*cus
= nullptr;
678 /* Table of TUs in the file. */
679 const struct dwp_hash_table
*tus
= nullptr;
681 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
685 /* Table to map ELF section numbers to their sections.
686 This is only needed for the DWP V1 file format. */
687 unsigned int num_sections
= 0;
688 asection
**elf_sections
= nullptr;
691 /* Struct used to pass misc. parameters to read_die_and_children, et
692 al. which are used for both .debug_info and .debug_types dies.
693 All parameters here are unchanging for the life of the call. This
694 struct exists to abstract away the constant parameters of die reading. */
696 struct die_reader_specs
698 /* The bfd of die_section. */
701 /* The CU of the DIE we are parsing. */
702 struct dwarf2_cu
*cu
;
704 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
705 struct dwo_file
*dwo_file
;
707 /* The section the die comes from.
708 This is either .debug_info or .debug_types, or the .dwo variants. */
709 struct dwarf2_section_info
*die_section
;
711 /* die_section->buffer. */
712 const gdb_byte
*buffer
;
714 /* The end of the buffer. */
715 const gdb_byte
*buffer_end
;
717 /* The abbreviation table to use when reading the DIEs. */
718 struct abbrev_table
*abbrev_table
;
721 /* A subclass of die_reader_specs that holds storage and has complex
722 constructor and destructor behavior. */
724 class cutu_reader
: public die_reader_specs
728 cutu_reader (dwarf2_per_cu_data
*this_cu
,
729 dwarf2_per_objfile
*per_objfile
,
730 struct abbrev_table
*abbrev_table
,
731 dwarf2_cu
*existing_cu
,
733 abbrev_cache
*cache
= nullptr);
735 explicit cutu_reader (struct dwarf2_per_cu_data
*this_cu
,
736 dwarf2_per_objfile
*per_objfile
,
737 struct dwarf2_cu
*parent_cu
= nullptr,
738 struct dwo_file
*dwo_file
= nullptr);
740 DISABLE_COPY_AND_ASSIGN (cutu_reader
);
742 cutu_reader (cutu_reader
&&) = default;
744 const gdb_byte
*info_ptr
= nullptr;
745 struct die_info
*comp_unit_die
= nullptr;
746 bool dummy_p
= false;
748 /* Release the new CU, putting it on the chain. This cannot be done
752 /* Release the abbrev table, transferring ownership to the
754 abbrev_table_up
release_abbrev_table ()
756 return std::move (m_abbrev_table_holder
);
760 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
761 dwarf2_per_objfile
*per_objfile
,
762 dwarf2_cu
*existing_cu
);
764 struct dwarf2_per_cu_data
*m_this_cu
;
765 std::unique_ptr
<dwarf2_cu
> m_new_cu
;
767 /* The ordinary abbreviation table. */
768 abbrev_table_up m_abbrev_table_holder
;
770 /* The DWO abbreviation table. */
771 abbrev_table_up m_dwo_abbrev_table
;
774 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
775 but this would require a corresponding change in unpack_field_as_long
777 static int bits_per_byte
= 8;
779 struct variant_part_builder
;
781 /* When reading a variant, we track a bit more information about the
782 field, and store it in an object of this type. */
786 int first_field
= -1;
789 /* A variant can contain other variant parts. */
790 std::vector
<variant_part_builder
> variant_parts
;
792 /* If we see a DW_TAG_variant, then this will be set if this is the
794 bool default_branch
= false;
795 /* If we see a DW_AT_discr_value, then this will be the discriminant
797 ULONGEST discriminant_value
= 0;
798 /* If we see a DW_AT_discr_list, then this is a pointer to the list
800 struct dwarf_block
*discr_list_data
= nullptr;
803 /* This represents a DW_TAG_variant_part. */
805 struct variant_part_builder
807 /* The offset of the discriminant field. */
808 sect_offset discriminant_offset
{};
810 /* Variants that are direct children of this variant part. */
811 std::vector
<variant_field
> variants
;
813 /* True if we're currently reading a variant. */
814 bool processing_variant
= false;
819 int accessibility
= 0;
821 /* Variant parts need to find the discriminant, which is a DIE
822 reference. We track the section offset of each field to make
825 struct field field
{};
830 const char *name
= nullptr;
831 std::vector
<struct fn_field
> fnfields
;
834 /* The routines that read and process dies for a C struct or C++ class
835 pass lists of data member fields and lists of member function fields
836 in an instance of a field_info structure, as defined below. */
839 /* List of data member and baseclasses fields. */
840 std::vector
<struct nextfield
> fields
;
841 std::vector
<struct nextfield
> baseclasses
;
843 /* Set if the accessibility of one of the fields is not public. */
844 bool non_public_fields
= false;
846 /* Member function fieldlist array, contains name of possibly overloaded
847 member function, number of overloaded member functions and a pointer
848 to the head of the member function field chain. */
849 std::vector
<struct fnfieldlist
> fnfieldlists
;
851 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
852 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
853 std::vector
<struct decl_field
> typedef_field_list
;
855 /* Nested types defined by this class and the number of elements in this
857 std::vector
<struct decl_field
> nested_types_list
;
859 /* If non-null, this is the variant part we are currently
861 variant_part_builder
*current_variant_part
= nullptr;
862 /* This holds all the top-level variant parts attached to the type
864 std::vector
<variant_part_builder
> variant_parts
;
866 /* Return the total number of fields (including baseclasses). */
869 return fields
.size () + baseclasses
.size ();
873 /* Loaded secondary compilation units are kept in memory until they
874 have not been referenced for the processing of this many
875 compilation units. Set this to zero to disable caching. Cache
876 sizes of up to at least twenty will improve startup time for
877 typical inter-CU-reference binaries, at an obvious memory cost. */
878 static int dwarf_max_cache_age
= 5;
880 show_dwarf_max_cache_age (struct ui_file
*file
, int from_tty
,
881 struct cmd_list_element
*c
, const char *value
)
883 gdb_printf (file
, _("The upper bound on the age of cached "
884 "DWARF compilation units is %s.\n"),
888 /* local function prototypes */
890 static void dwarf2_find_base_address (struct die_info
*die
,
891 struct dwarf2_cu
*cu
);
893 class cooked_index_storage
;
894 static void build_type_psymtabs_reader (cutu_reader
*reader
,
895 cooked_index_storage
*storage
);
897 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
);
899 static void var_decode_location (struct attribute
*attr
,
901 struct dwarf2_cu
*cu
);
903 static unsigned int peek_abbrev_code (bfd
*, const gdb_byte
*);
905 static const gdb_byte
*read_attribute (const struct die_reader_specs
*,
907 const struct attr_abbrev
*,
910 static void read_attribute_reprocess (const struct die_reader_specs
*reader
,
911 struct attribute
*attr
, dwarf_tag tag
);
913 static CORE_ADDR
read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
);
915 static sect_offset
read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
916 dwarf2_section_info
*, sect_offset
);
918 static const char *read_indirect_string
919 (dwarf2_per_objfile
*per_objfile
, bfd
*, const gdb_byte
*,
920 const struct comp_unit_head
*, unsigned int *);
922 static const char *read_indirect_string_at_offset
923 (dwarf2_per_objfile
*per_objfile
, LONGEST str_offset
);
925 static CORE_ADDR
read_addr_index_from_leb128 (struct dwarf2_cu
*,
929 static const char *read_dwo_str_index (const struct die_reader_specs
*reader
,
932 static const char *read_stub_str_index (struct dwarf2_cu
*cu
,
935 static struct attribute
*dwarf2_attr (struct die_info
*, unsigned int,
938 static const char *dwarf2_string_attr (struct die_info
*die
, unsigned int name
,
939 struct dwarf2_cu
*cu
);
941 static const char *dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
);
943 static int dwarf2_flag_true_p (struct die_info
*die
, unsigned name
,
944 struct dwarf2_cu
*cu
);
946 static int die_is_declaration (struct die_info
*, struct dwarf2_cu
*cu
);
948 static struct die_info
*die_specification (struct die_info
*die
,
949 struct dwarf2_cu
**);
951 static line_header_up
dwarf_decode_line_header (sect_offset sect_off
,
952 struct dwarf2_cu
*cu
,
953 const char *comp_dir
);
955 static void dwarf_decode_lines (struct line_header
*,
957 CORE_ADDR
, int decode_mapping
);
959 static void dwarf2_start_subfile (dwarf2_cu
*cu
, const file_entry
&fe
,
960 const line_header
&lh
);
962 static struct symbol
*new_symbol (struct die_info
*, struct type
*,
963 struct dwarf2_cu
*, struct symbol
* = NULL
);
965 static void dwarf2_const_value (const struct attribute
*, struct symbol
*,
968 static void dwarf2_const_value_attr (const struct attribute
*attr
,
971 struct obstack
*obstack
,
972 struct dwarf2_cu
*cu
, LONGEST
*value
,
973 const gdb_byte
**bytes
,
974 struct dwarf2_locexpr_baton
**baton
);
976 static struct type
*read_subrange_index_type (struct die_info
*die
,
977 struct dwarf2_cu
*cu
);
979 static struct type
*die_type (struct die_info
*, struct dwarf2_cu
*);
981 static int need_gnat_info (struct dwarf2_cu
*);
983 static struct type
*die_descriptive_type (struct die_info
*,
986 static void set_descriptive_type (struct type
*, struct die_info
*,
989 static struct type
*die_containing_type (struct die_info
*,
992 static struct type
*lookup_die_type (struct die_info
*, const struct attribute
*,
995 static struct type
*read_type_die (struct die_info
*, struct dwarf2_cu
*);
997 static struct type
*read_type_die_1 (struct die_info
*, struct dwarf2_cu
*);
999 static const char *determine_prefix (struct die_info
*die
, struct dwarf2_cu
*);
1001 static char *typename_concat (struct obstack
*obs
, const char *prefix
,
1002 const char *suffix
, int physname
,
1003 struct dwarf2_cu
*cu
);
1005 static void read_file_scope (struct die_info
*, struct dwarf2_cu
*);
1007 static void read_type_unit_scope (struct die_info
*, struct dwarf2_cu
*);
1009 static void read_func_scope (struct die_info
*, struct dwarf2_cu
*);
1011 static void read_lexical_block_scope (struct die_info
*, struct dwarf2_cu
*);
1013 static void read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
);
1015 static void read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
);
1017 /* Return the .debug_loclists section to use for cu. */
1018 static struct dwarf2_section_info
*cu_debug_loc_section (struct dwarf2_cu
*cu
);
1020 /* Return the .debug_rnglists section to use for cu. */
1021 static struct dwarf2_section_info
*cu_debug_rnglists_section
1022 (struct dwarf2_cu
*cu
, dwarf_tag tag
);
1024 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1025 values. Keep the items ordered with increasing constraints compliance. */
1028 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1029 PC_BOUNDS_NOT_PRESENT
,
1031 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1032 were present but they do not form a valid range of PC addresses. */
1035 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1038 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1042 static enum pc_bounds_kind
dwarf2_get_pc_bounds (struct die_info
*,
1043 CORE_ADDR
*, CORE_ADDR
*,
1048 static void get_scope_pc_bounds (struct die_info
*,
1049 CORE_ADDR
*, CORE_ADDR
*,
1050 struct dwarf2_cu
*);
1052 static void dwarf2_record_block_ranges (struct die_info
*, struct block
*,
1053 CORE_ADDR
, struct dwarf2_cu
*);
1055 static void dwarf2_add_field (struct field_info
*, struct die_info
*,
1056 struct dwarf2_cu
*);
1058 static void dwarf2_attach_fields_to_type (struct field_info
*,
1059 struct type
*, struct dwarf2_cu
*);
1061 static void dwarf2_add_member_fn (struct field_info
*,
1062 struct die_info
*, struct type
*,
1063 struct dwarf2_cu
*);
1065 static void dwarf2_attach_fn_fields_to_type (struct field_info
*,
1067 struct dwarf2_cu
*);
1069 static void process_structure_scope (struct die_info
*, struct dwarf2_cu
*);
1071 static void read_common_block (struct die_info
*, struct dwarf2_cu
*);
1073 static void read_namespace (struct die_info
*die
, struct dwarf2_cu
*);
1075 static void read_module (struct die_info
*die
, struct dwarf2_cu
*cu
);
1077 static struct using_direct
**using_directives (struct dwarf2_cu
*cu
);
1079 static void read_import_statement (struct die_info
*die
, struct dwarf2_cu
*);
1081 static bool read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
);
1083 static struct type
*read_module_type (struct die_info
*die
,
1084 struct dwarf2_cu
*cu
);
1086 static const char *namespace_name (struct die_info
*die
,
1087 int *is_anonymous
, struct dwarf2_cu
*);
1089 static void process_enumeration_scope (struct die_info
*, struct dwarf2_cu
*);
1091 static CORE_ADDR
decode_locdesc (struct dwarf_block
*, struct dwarf2_cu
*,
1094 static enum dwarf_array_dim_ordering
read_array_order (struct die_info
*,
1095 struct dwarf2_cu
*);
1097 static struct die_info
*read_die_and_siblings_1
1098 (const struct die_reader_specs
*, const gdb_byte
*, const gdb_byte
**,
1101 static struct die_info
*read_die_and_siblings (const struct die_reader_specs
*,
1102 const gdb_byte
*info_ptr
,
1103 const gdb_byte
**new_info_ptr
,
1104 struct die_info
*parent
);
1106 static const gdb_byte
*read_full_die_1 (const struct die_reader_specs
*,
1107 struct die_info
**, const gdb_byte
*,
1110 static const gdb_byte
*read_full_die (const struct die_reader_specs
*,
1111 struct die_info
**, const gdb_byte
*);
1113 static void process_die (struct die_info
*, struct dwarf2_cu
*);
1115 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu
*,
1118 static const char *dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*);
1120 static const char *dwarf2_full_name (const char *name
,
1121 struct die_info
*die
,
1122 struct dwarf2_cu
*cu
);
1124 static const char *dwarf2_physname (const char *name
, struct die_info
*die
,
1125 struct dwarf2_cu
*cu
);
1127 static struct die_info
*dwarf2_extension (struct die_info
*die
,
1128 struct dwarf2_cu
**);
1130 static void store_in_ref_table (struct die_info
*,
1131 struct dwarf2_cu
*);
1133 static struct die_info
*follow_die_ref_or_sig (struct die_info
*,
1134 const struct attribute
*,
1135 struct dwarf2_cu
**);
1137 static struct die_info
*follow_die_ref (struct die_info
*,
1138 const struct attribute
*,
1139 struct dwarf2_cu
**);
1141 static struct die_info
*follow_die_sig (struct die_info
*,
1142 const struct attribute
*,
1143 struct dwarf2_cu
**);
1145 static struct type
*get_signatured_type (struct die_info
*, ULONGEST
,
1146 struct dwarf2_cu
*);
1148 static struct type
*get_DW_AT_signature_type (struct die_info
*,
1149 const struct attribute
*,
1150 struct dwarf2_cu
*);
1152 static void load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
1153 dwarf2_per_objfile
*per_objfile
);
1155 static void read_signatured_type (signatured_type
*sig_type
,
1156 dwarf2_per_objfile
*per_objfile
);
1158 static int attr_to_dynamic_prop (const struct attribute
*attr
,
1159 struct die_info
*die
, struct dwarf2_cu
*cu
,
1160 struct dynamic_prop
*prop
, struct type
*type
);
1162 /* memory allocation interface */
1164 static struct dwarf_block
*dwarf_alloc_block (struct dwarf2_cu
*);
1166 static void dwarf_decode_macros (struct dwarf2_cu
*, unsigned int, int);
1168 static void fill_in_loclist_baton (struct dwarf2_cu
*cu
,
1169 struct dwarf2_loclist_baton
*baton
,
1170 const struct attribute
*attr
);
1172 static void dwarf2_symbol_mark_computed (const struct attribute
*attr
,
1174 struct dwarf2_cu
*cu
,
1177 static const gdb_byte
*skip_one_die (const struct die_reader_specs
*reader
,
1178 const gdb_byte
*info_ptr
,
1179 const struct abbrev_info
*abbrev
,
1180 bool do_skip_children
= true);
1182 static struct dwarf2_per_cu_data
*dwarf2_find_containing_comp_unit
1183 (sect_offset sect_off
, unsigned int offset_in_dwz
,
1184 dwarf2_per_bfd
*per_bfd
);
1186 static void prepare_one_comp_unit (struct dwarf2_cu
*cu
,
1187 struct die_info
*comp_unit_die
,
1188 enum language pretend_language
);
1190 static struct type
*set_die_type (struct die_info
*, struct type
*,
1191 struct dwarf2_cu
*, bool = false);
1193 static void create_all_units (dwarf2_per_objfile
*per_objfile
);
1195 static void load_full_comp_unit (dwarf2_per_cu_data
*per_cu
,
1196 dwarf2_per_objfile
*per_objfile
,
1197 dwarf2_cu
*existing_cu
,
1199 enum language pretend_language
);
1201 static void process_full_comp_unit (dwarf2_cu
*cu
,
1202 enum language pretend_language
);
1204 static void process_full_type_unit (dwarf2_cu
*cu
,
1205 enum language pretend_language
);
1207 static struct type
*get_die_type_at_offset (sect_offset
,
1208 dwarf2_per_cu_data
*per_cu
,
1209 dwarf2_per_objfile
*per_objfile
);
1211 static struct type
*get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
);
1213 static void queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
1214 dwarf2_per_objfile
*per_objfile
,
1215 enum language pretend_language
);
1217 static void process_queue (dwarf2_per_objfile
*per_objfile
);
1219 /* Class, the destructor of which frees all allocated queue entries. This
1220 will only have work to do if an error was thrown while processing the
1221 dwarf. If no error was thrown then the queue entries should have all
1222 been processed, and freed, as we went along. */
1224 class dwarf2_queue_guard
1227 explicit dwarf2_queue_guard (dwarf2_per_objfile
*per_objfile
)
1228 : m_per_objfile (per_objfile
)
1230 gdb_assert (!m_per_objfile
->queue
.has_value ());
1232 m_per_objfile
->queue
.emplace ();
1235 /* Free any entries remaining on the queue. There should only be
1236 entries left if we hit an error while processing the dwarf. */
1237 ~dwarf2_queue_guard ()
1239 gdb_assert (m_per_objfile
->queue
.has_value ());
1241 m_per_objfile
->queue
.reset ();
1244 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard
);
1247 dwarf2_per_objfile
*m_per_objfile
;
1250 dwarf2_queue_item::~dwarf2_queue_item ()
1252 /* Anything still marked queued is likely to be in an
1253 inconsistent state, so discard it. */
1256 per_objfile
->remove_cu (per_cu
);
1261 /* See dwarf2/read.h. */
1264 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data
*data
)
1266 if (data
->is_debug_types
)
1267 delete static_cast<signatured_type
*> (data
);
1272 static file_and_directory
&find_file_and_directory
1273 (struct die_info
*die
, struct dwarf2_cu
*cu
);
1275 static const char *compute_include_file_name
1276 (const struct line_header
*lh
,
1277 const file_entry
&fe
,
1278 const file_and_directory
&cu_info
,
1279 std::string
&name_holder
);
1281 static htab_up
allocate_signatured_type_table ();
1283 static htab_up
allocate_dwo_unit_table ();
1285 static struct dwo_unit
*lookup_dwo_unit_in_dwp
1286 (dwarf2_per_objfile
*per_objfile
, struct dwp_file
*dwp_file
,
1287 const char *comp_dir
, ULONGEST signature
, int is_debug_types
);
1289 static struct dwp_file
*get_dwp_file (dwarf2_per_objfile
*per_objfile
);
1291 static struct dwo_unit
*lookup_dwo_comp_unit
1292 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
1293 ULONGEST signature
);
1295 static struct dwo_unit
*lookup_dwo_type_unit
1296 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
);
1298 static void queue_and_load_all_dwo_tus (dwarf2_cu
*cu
);
1300 /* A unique pointer to a dwo_file. */
1302 typedef std::unique_ptr
<struct dwo_file
> dwo_file_up
;
1304 static void process_cu_includes (dwarf2_per_objfile
*per_objfile
);
1306 static void check_producer (struct dwarf2_cu
*cu
);
1308 /* Various complaints about symbol reading that don't abort the process. */
1311 dwarf2_debug_line_missing_file_complaint (void)
1313 complaint (_(".debug_line section has line data without a file"));
1317 dwarf2_debug_line_missing_end_sequence_complaint (void)
1319 complaint (_(".debug_line section has line "
1320 "program sequence without an end"));
1324 dwarf2_complex_location_expr_complaint (void)
1326 complaint (_("location expression too complex"));
1330 dwarf2_const_value_length_mismatch_complaint (const char *arg1
, int arg2
,
1333 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1338 dwarf2_invalid_attrib_class_complaint (const char *arg1
, const char *arg2
)
1340 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1344 /* Hash function for line_header_hash. */
1347 line_header_hash (const struct line_header
*ofs
)
1349 return to_underlying (ofs
->sect_off
) ^ ofs
->offset_in_dwz
;
1352 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1355 line_header_hash_voidp (const void *item
)
1357 const struct line_header
*ofs
= (const struct line_header
*) item
;
1359 return line_header_hash (ofs
);
1362 /* Equality function for line_header_hash. */
1365 line_header_eq_voidp (const void *item_lhs
, const void *item_rhs
)
1367 const struct line_header
*ofs_lhs
= (const struct line_header
*) item_lhs
;
1368 const struct line_header
*ofs_rhs
= (const struct line_header
*) item_rhs
;
1370 return (ofs_lhs
->sect_off
== ofs_rhs
->sect_off
1371 && ofs_lhs
->offset_in_dwz
== ofs_rhs
->offset_in_dwz
);
1376 /* An iterator for all_units that is based on index. This
1377 approach makes it possible to iterate over all_units safely,
1378 when some caller in the loop may add new units. */
1380 class all_units_iterator
1384 all_units_iterator (dwarf2_per_bfd
*per_bfd
, bool start
)
1385 : m_per_bfd (per_bfd
),
1386 m_index (start
? 0 : per_bfd
->all_units
.size ())
1390 all_units_iterator
&operator++ ()
1396 dwarf2_per_cu_data
*operator* () const
1398 return m_per_bfd
->get_cu (m_index
);
1401 bool operator== (const all_units_iterator
&other
) const
1403 return m_index
== other
.m_index
;
1407 bool operator!= (const all_units_iterator
&other
) const
1409 return m_index
!= other
.m_index
;
1414 dwarf2_per_bfd
*m_per_bfd
;
1418 /* A range adapter for the all_units_iterator. */
1419 class all_units_range
1423 all_units_range (dwarf2_per_bfd
*per_bfd
)
1424 : m_per_bfd (per_bfd
)
1428 all_units_iterator
begin ()
1430 return all_units_iterator (m_per_bfd
, true);
1433 all_units_iterator
end ()
1435 return all_units_iterator (m_per_bfd
, false);
1440 dwarf2_per_bfd
*m_per_bfd
;
1443 /* See declaration. */
1445 dwarf2_per_bfd::dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
,
1448 can_copy (can_copy_
)
1451 names
= &dwarf2_elf_names
;
1453 for (asection
*sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
1454 locate_sections (obfd
, sec
, *names
);
1457 dwarf2_per_bfd::~dwarf2_per_bfd ()
1459 for (auto &per_cu
: all_units
)
1461 per_cu
->imported_symtabs_free ();
1462 per_cu
->free_cached_file_names ();
1465 /* Everything else should be on this->obstack. */
1471 dwarf2_per_objfile::remove_all_cus ()
1473 gdb_assert (!queue
.has_value ());
1475 m_dwarf2_cus
.clear ();
1478 /* A helper class that calls free_cached_comp_units on
1481 class free_cached_comp_units
1485 explicit free_cached_comp_units (dwarf2_per_objfile
*per_objfile
)
1486 : m_per_objfile (per_objfile
)
1490 ~free_cached_comp_units ()
1492 m_per_objfile
->remove_all_cus ();
1495 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units
);
1499 dwarf2_per_objfile
*m_per_objfile
;
1505 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const
1507 if (per_cu
->index
< this->m_symtabs
.size ())
1508 return this->m_symtabs
[per_cu
->index
] != nullptr;
1515 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data
*per_cu
) const
1517 if (per_cu
->index
< this->m_symtabs
.size ())
1518 return this->m_symtabs
[per_cu
->index
];
1525 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data
*per_cu
,
1526 compunit_symtab
*symtab
)
1528 if (per_cu
->index
>= this->m_symtabs
.size ())
1529 this->m_symtabs
.resize (per_cu
->index
+ 1);
1530 gdb_assert (this->m_symtabs
[per_cu
->index
] == nullptr);
1531 this->m_symtabs
[per_cu
->index
] = symtab
;
1534 /* Try to locate the sections we need for DWARF 2 debugging
1535 information and return true if we have enough to do something.
1536 NAMES points to the dwarf2 section names, or is NULL if the standard
1537 ELF names are used. CAN_COPY is true for formats where symbol
1538 interposition is possible and so symbol values must follow copy
1539 relocation rules. */
1542 dwarf2_has_info (struct objfile
*objfile
,
1543 const struct dwarf2_debug_sections
*names
,
1546 if (objfile
->flags
& OBJF_READNEVER
)
1549 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1551 if (per_objfile
== NULL
)
1553 dwarf2_per_bfd
*per_bfd
;
1555 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1556 BFD doesn't require relocations.
1558 We don't share with objfiles for which -readnow was requested,
1559 because it would complicate things when loading the same BFD with
1560 -readnow and then without -readnow. */
1561 if (!gdb_bfd_requires_relocations (objfile
->obfd
.get ())
1562 && (objfile
->flags
& OBJF_READNOW
) == 0)
1564 /* See if one has been created for this BFD yet. */
1565 per_bfd
= dwarf2_per_bfd_bfd_data_key
.get (objfile
->obfd
.get ());
1567 if (per_bfd
== nullptr)
1569 /* No, create it now. */
1570 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
.get (), names
,
1572 dwarf2_per_bfd_bfd_data_key
.set (objfile
->obfd
.get (), per_bfd
);
1577 /* No sharing possible, create one specifically for this objfile. */
1578 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
.get (), names
, can_copy
);
1579 dwarf2_per_bfd_objfile_data_key
.set (objfile
, per_bfd
);
1582 per_objfile
= dwarf2_objfile_data_key
.emplace (objfile
, objfile
, per_bfd
);
1585 return (!per_objfile
->per_bfd
->info
.is_virtual
1586 && per_objfile
->per_bfd
->info
.s
.section
!= NULL
1587 && !per_objfile
->per_bfd
->abbrev
.is_virtual
1588 && per_objfile
->per_bfd
->abbrev
.s
.section
!= NULL
);
1591 /* See declaration. */
1594 dwarf2_per_bfd::locate_sections (bfd
*abfd
, asection
*sectp
,
1595 const dwarf2_debug_sections
&names
)
1597 flagword aflag
= bfd_section_flags (sectp
);
1599 if ((aflag
& SEC_HAS_CONTENTS
) == 0)
1602 else if (elf_section_data (sectp
)->this_hdr
.sh_size
1603 > bfd_get_file_size (abfd
))
1605 bfd_size_type size
= elf_section_data (sectp
)->this_hdr
.sh_size
;
1606 warning (_("Discarding section %s which has a section size (%s"
1607 ") larger than the file size [in module %s]"),
1608 bfd_section_name (sectp
), phex_nz (size
, sizeof (size
)),
1609 bfd_get_filename (abfd
));
1611 else if (names
.info
.matches (sectp
->name
))
1613 this->info
.s
.section
= sectp
;
1614 this->info
.size
= bfd_section_size (sectp
);
1616 else if (names
.abbrev
.matches (sectp
->name
))
1618 this->abbrev
.s
.section
= sectp
;
1619 this->abbrev
.size
= bfd_section_size (sectp
);
1621 else if (names
.line
.matches (sectp
->name
))
1623 this->line
.s
.section
= sectp
;
1624 this->line
.size
= bfd_section_size (sectp
);
1626 else if (names
.loc
.matches (sectp
->name
))
1628 this->loc
.s
.section
= sectp
;
1629 this->loc
.size
= bfd_section_size (sectp
);
1631 else if (names
.loclists
.matches (sectp
->name
))
1633 this->loclists
.s
.section
= sectp
;
1634 this->loclists
.size
= bfd_section_size (sectp
);
1636 else if (names
.macinfo
.matches (sectp
->name
))
1638 this->macinfo
.s
.section
= sectp
;
1639 this->macinfo
.size
= bfd_section_size (sectp
);
1641 else if (names
.macro
.matches (sectp
->name
))
1643 this->macro
.s
.section
= sectp
;
1644 this->macro
.size
= bfd_section_size (sectp
);
1646 else if (names
.str
.matches (sectp
->name
))
1648 this->str
.s
.section
= sectp
;
1649 this->str
.size
= bfd_section_size (sectp
);
1651 else if (names
.str_offsets
.matches (sectp
->name
))
1653 this->str_offsets
.s
.section
= sectp
;
1654 this->str_offsets
.size
= bfd_section_size (sectp
);
1656 else if (names
.line_str
.matches (sectp
->name
))
1658 this->line_str
.s
.section
= sectp
;
1659 this->line_str
.size
= bfd_section_size (sectp
);
1661 else if (names
.addr
.matches (sectp
->name
))
1663 this->addr
.s
.section
= sectp
;
1664 this->addr
.size
= bfd_section_size (sectp
);
1666 else if (names
.frame
.matches (sectp
->name
))
1668 this->frame
.s
.section
= sectp
;
1669 this->frame
.size
= bfd_section_size (sectp
);
1671 else if (names
.eh_frame
.matches (sectp
->name
))
1673 this->eh_frame
.s
.section
= sectp
;
1674 this->eh_frame
.size
= bfd_section_size (sectp
);
1676 else if (names
.ranges
.matches (sectp
->name
))
1678 this->ranges
.s
.section
= sectp
;
1679 this->ranges
.size
= bfd_section_size (sectp
);
1681 else if (names
.rnglists
.matches (sectp
->name
))
1683 this->rnglists
.s
.section
= sectp
;
1684 this->rnglists
.size
= bfd_section_size (sectp
);
1686 else if (names
.types
.matches (sectp
->name
))
1688 struct dwarf2_section_info type_section
;
1690 memset (&type_section
, 0, sizeof (type_section
));
1691 type_section
.s
.section
= sectp
;
1692 type_section
.size
= bfd_section_size (sectp
);
1694 this->types
.push_back (type_section
);
1696 else if (names
.gdb_index
.matches (sectp
->name
))
1698 this->gdb_index
.s
.section
= sectp
;
1699 this->gdb_index
.size
= bfd_section_size (sectp
);
1701 else if (names
.debug_names
.matches (sectp
->name
))
1703 this->debug_names
.s
.section
= sectp
;
1704 this->debug_names
.size
= bfd_section_size (sectp
);
1706 else if (names
.debug_aranges
.matches (sectp
->name
))
1708 this->debug_aranges
.s
.section
= sectp
;
1709 this->debug_aranges
.size
= bfd_section_size (sectp
);
1712 if ((bfd_section_flags (sectp
) & (SEC_LOAD
| SEC_ALLOC
))
1713 && bfd_section_vma (sectp
) == 0)
1714 this->has_section_at_zero
= true;
1717 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1721 dwarf2_get_section_info (struct objfile
*objfile
,
1722 enum dwarf2_section_enum sect
,
1723 asection
**sectp
, const gdb_byte
**bufp
,
1724 bfd_size_type
*sizep
)
1726 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1727 struct dwarf2_section_info
*info
;
1729 /* We may see an objfile without any DWARF, in which case we just
1731 if (per_objfile
== NULL
)
1740 case DWARF2_DEBUG_FRAME
:
1741 info
= &per_objfile
->per_bfd
->frame
;
1743 case DWARF2_EH_FRAME
:
1744 info
= &per_objfile
->per_bfd
->eh_frame
;
1747 gdb_assert_not_reached ("unexpected section");
1750 info
->read (objfile
);
1752 *sectp
= info
->get_bfd_section ();
1753 *bufp
= info
->buffer
;
1754 *sizep
= info
->size
;
1757 /* See dwarf2/read.h. */
1760 dwarf2_per_bfd::map_info_sections (struct objfile
*objfile
)
1762 info
.read (objfile
);
1763 abbrev
.read (objfile
);
1764 line
.read (objfile
);
1766 str_offsets
.read (objfile
);
1767 line_str
.read (objfile
);
1768 ranges
.read (objfile
);
1769 rnglists
.read (objfile
);
1770 addr
.read (objfile
);
1772 for (auto §ion
: types
)
1773 section
.read (objfile
);
1777 /* DWARF quick_symbol_functions support. */
1779 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1780 unique line tables, so we maintain a separate table of all .debug_line
1781 derived entries to support the sharing.
1782 All the quick functions need is the list of file names. We discard the
1783 line_header when we're done and don't need to record it here. */
1784 struct quick_file_names
1786 /* The data used to construct the hash key. */
1787 struct stmt_list_hash hash
;
1789 /* The number of entries in file_names, real_names. */
1790 unsigned int num_file_names
;
1792 /* The CU directory, as given by DW_AT_comp_dir. May be
1794 const char *comp_dir
;
1796 /* The file names from the line table, after being run through
1798 const char **file_names
;
1800 /* The file names from the line table after being run through
1801 gdb_realpath. These are computed lazily. */
1802 const char **real_names
;
1805 struct dwarf2_base_index_functions
: public quick_symbol_functions
1807 bool has_symbols (struct objfile
*objfile
) override
;
1809 bool has_unexpanded_symtabs (struct objfile
*objfile
) override
;
1811 struct symtab
*find_last_source_symtab (struct objfile
*objfile
) override
;
1813 void forget_cached_source_info (struct objfile
*objfile
) override
;
1815 enum language
lookup_global_symbol_language (struct objfile
*objfile
,
1818 bool *symbol_found_p
) override
1820 *symbol_found_p
= false;
1821 return language_unknown
;
1824 void print_stats (struct objfile
*objfile
, bool print_bcache
) override
;
1826 void expand_all_symtabs (struct objfile
*objfile
) override
;
1828 /* A helper function that finds the per-cu object from an "adjusted"
1829 PC -- a PC with the base text offset removed. */
1830 virtual dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
1831 CORE_ADDR adjusted_pc
);
1833 struct compunit_symtab
*find_pc_sect_compunit_symtab
1834 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
1835 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
)
1838 struct compunit_symtab
*find_compunit_symtab_by_address
1839 (struct objfile
*objfile
, CORE_ADDR address
) override
1844 void map_symbol_filenames (struct objfile
*objfile
,
1845 gdb::function_view
<symbol_filename_ftype
> fun
,
1846 bool need_fullname
) override
;
1849 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1850 It's handy in this case to have an empty implementation of the
1851 quick symbol functions, to avoid special cases in the rest of the
1854 struct readnow_functions
: public dwarf2_base_index_functions
1856 void dump (struct objfile
*objfile
) override
1860 void expand_matching_symbols
1862 const lookup_name_info
&lookup_name
,
1865 symbol_compare_ftype
*ordered_compare
) override
1869 bool expand_symtabs_matching
1870 (struct objfile
*objfile
,
1871 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1872 const lookup_name_info
*lookup_name
,
1873 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1874 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1875 block_search_flags search_flags
,
1877 enum search_domain kind
) override
1883 struct dwarf2_gdb_index
: public dwarf2_base_index_functions
1885 void dump (struct objfile
*objfile
) override
;
1887 void expand_matching_symbols
1889 const lookup_name_info
&lookup_name
,
1892 symbol_compare_ftype
*ordered_compare
) override
;
1894 bool expand_symtabs_matching
1895 (struct objfile
*objfile
,
1896 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1897 const lookup_name_info
*lookup_name
,
1898 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1899 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1900 block_search_flags search_flags
,
1902 enum search_domain kind
) override
;
1905 struct dwarf2_debug_names_index
: public dwarf2_base_index_functions
1907 void dump (struct objfile
*objfile
) override
;
1909 void expand_matching_symbols
1911 const lookup_name_info
&lookup_name
,
1914 symbol_compare_ftype
*ordered_compare
) override
;
1916 bool expand_symtabs_matching
1917 (struct objfile
*objfile
,
1918 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1919 const lookup_name_info
*lookup_name
,
1920 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1921 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1922 block_search_flags search_flags
,
1924 enum search_domain kind
) override
;
1927 quick_symbol_functions_up
1928 mapped_index::make_quick_functions () const
1930 return quick_symbol_functions_up (new dwarf2_gdb_index
);
1933 quick_symbol_functions_up
1934 mapped_debug_names::make_quick_functions () const
1936 return quick_symbol_functions_up (new dwarf2_debug_names_index
);
1939 /* Utility hash function for a stmt_list_hash. */
1942 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1946 if (stmt_list_hash
->dwo_unit
!= NULL
)
1947 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1948 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1952 /* Utility equality function for a stmt_list_hash. */
1955 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1956 const struct stmt_list_hash
*rhs
)
1958 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1960 if (lhs
->dwo_unit
!= NULL
1961 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1964 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1967 /* Hash function for a quick_file_names. */
1970 hash_file_name_entry (const void *e
)
1972 const struct quick_file_names
*file_data
1973 = (const struct quick_file_names
*) e
;
1975 return hash_stmt_list_entry (&file_data
->hash
);
1978 /* Equality function for a quick_file_names. */
1981 eq_file_name_entry (const void *a
, const void *b
)
1983 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
1984 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
1986 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
1989 /* Create a quick_file_names hash table. */
1992 create_quick_file_names_table (unsigned int nr_initial_entries
)
1994 return htab_up (htab_create_alloc (nr_initial_entries
,
1995 hash_file_name_entry
, eq_file_name_entry
,
1996 nullptr, xcalloc
, xfree
));
1999 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2000 function is unrelated to symtabs, symtab would have to be created afterwards.
2001 You should call age_cached_comp_units after processing the CU. */
2004 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
2007 if (per_cu
->is_debug_types
)
2008 load_full_type_unit (per_cu
, per_objfile
);
2010 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
2011 skip_partial
, language_minimal
);
2013 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
2015 return nullptr; /* Dummy CU. */
2017 dwarf2_find_base_address (cu
->dies
, cu
);
2022 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2025 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2026 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
2029 /* The destructor of dwarf2_queue_guard frees any entries left on
2030 the queue. After this point we're guaranteed to leave this function
2031 with the dwarf queue empty. */
2032 dwarf2_queue_guard
q_guard (per_objfile
);
2034 if (!per_objfile
->symtab_set_p (per_cu
))
2036 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
2037 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
2039 /* If we just loaded a CU from a DWO, and we're working with an index
2040 that may badly handle TUs, load all the TUs in that DWO as well.
2041 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2042 if (!per_cu
->is_debug_types
2044 && cu
->dwo_unit
!= NULL
2045 && per_objfile
->per_bfd
->index_table
!= NULL
2046 && !per_objfile
->per_bfd
->index_table
->version_check ()
2047 /* DWP files aren't supported yet. */
2048 && get_dwp_file (per_objfile
) == NULL
)
2049 queue_and_load_all_dwo_tus (cu
);
2052 process_queue (per_objfile
);
2055 /* Age the cache, releasing compilation units that have not
2056 been used recently. */
2057 per_objfile
->age_comp_units ();
2060 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2061 the per-objfile for which this symtab is instantiated.
2063 Returns the resulting symbol table. */
2065 static struct compunit_symtab
*
2066 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2067 dwarf2_per_objfile
*per_objfile
,
2070 if (!per_objfile
->symtab_set_p (per_cu
))
2072 free_cached_comp_units
freer (per_objfile
);
2073 scoped_restore decrementer
= increment_reading_symtab ();
2074 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
2075 process_cu_includes (per_objfile
);
2078 return per_objfile
->get_symtab (per_cu
);
2083 dwarf2_per_cu_data_up
2084 dwarf2_per_bfd::allocate_per_cu ()
2086 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
2087 result
->per_bfd
= this;
2088 result
->index
= all_units
.size ();
2095 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
2097 signatured_type_up
result (new signatured_type (signature
));
2098 result
->per_bfd
= this;
2099 result
->index
= all_units
.size ();
2100 result
->is_debug_types
= true;
2105 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2106 obstack, and constructed with the specified field values. */
2108 static dwarf2_per_cu_data_up
2109 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
2110 struct dwarf2_section_info
*section
,
2112 sect_offset sect_off
, ULONGEST length
)
2114 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
2115 the_cu
->sect_off
= sect_off
;
2116 the_cu
->set_length (length
);
2117 the_cu
->section
= section
;
2118 the_cu
->is_dwz
= is_dwz
;
2122 /* A helper for create_cus_from_index that handles a given list of
2126 create_cus_from_index_list (dwarf2_per_bfd
*per_bfd
,
2127 const gdb_byte
*cu_list
, offset_type n_elements
,
2128 struct dwarf2_section_info
*section
,
2131 for (offset_type i
= 0; i
< n_elements
; i
+= 2)
2133 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2135 sect_offset sect_off
2136 = (sect_offset
) extract_unsigned_integer (cu_list
, 8, BFD_ENDIAN_LITTLE
);
2137 ULONGEST length
= extract_unsigned_integer (cu_list
+ 8, 8, BFD_ENDIAN_LITTLE
);
2140 dwarf2_per_cu_data_up per_cu
2141 = create_cu_from_index_list (per_bfd
, section
, is_dwz
, sect_off
,
2143 per_bfd
->all_units
.push_back (std::move (per_cu
));
2147 /* Read the CU list from the mapped index, and use it to create all
2148 the CU objects for PER_BFD. */
2151 create_cus_from_index (dwarf2_per_bfd
*per_bfd
,
2152 const gdb_byte
*cu_list
, offset_type cu_list_elements
,
2153 const gdb_byte
*dwz_list
, offset_type dwz_elements
)
2155 gdb_assert (per_bfd
->all_units
.empty ());
2156 per_bfd
->all_units
.reserve ((cu_list_elements
+ dwz_elements
) / 2);
2158 create_cus_from_index_list (per_bfd
, cu_list
, cu_list_elements
,
2161 if (dwz_elements
== 0)
2164 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
2165 create_cus_from_index_list (per_bfd
, dwz_list
, dwz_elements
,
2169 /* Create the signatured type hash table from the index. */
2172 create_signatured_type_table_from_index
2173 (dwarf2_per_bfd
*per_bfd
, struct dwarf2_section_info
*section
,
2174 const gdb_byte
*bytes
, offset_type elements
)
2176 htab_up sig_types_hash
= allocate_signatured_type_table ();
2178 for (offset_type i
= 0; i
< elements
; i
+= 3)
2180 signatured_type_up sig_type
;
2183 cu_offset type_offset_in_tu
;
2185 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2186 sect_offset sect_off
2187 = (sect_offset
) extract_unsigned_integer (bytes
, 8, BFD_ENDIAN_LITTLE
);
2189 = (cu_offset
) extract_unsigned_integer (bytes
+ 8, 8,
2191 signature
= extract_unsigned_integer (bytes
+ 16, 8, BFD_ENDIAN_LITTLE
);
2194 sig_type
= per_bfd
->allocate_signatured_type (signature
);
2195 sig_type
->type_offset_in_tu
= type_offset_in_tu
;
2196 sig_type
->section
= section
;
2197 sig_type
->sect_off
= sect_off
;
2199 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2200 *slot
= sig_type
.get ();
2202 per_bfd
->all_units
.emplace_back (sig_type
.release ());
2205 per_bfd
->signatured_types
= std::move (sig_types_hash
);
2208 /* Create the signatured type hash table from .debug_names. */
2211 create_signatured_type_table_from_debug_names
2212 (dwarf2_per_objfile
*per_objfile
,
2213 const mapped_debug_names
&map
,
2214 struct dwarf2_section_info
*section
,
2215 struct dwarf2_section_info
*abbrev_section
)
2217 struct objfile
*objfile
= per_objfile
->objfile
;
2219 section
->read (objfile
);
2220 abbrev_section
->read (objfile
);
2222 htab_up sig_types_hash
= allocate_signatured_type_table ();
2224 for (uint32_t i
= 0; i
< map
.tu_count
; ++i
)
2226 signatured_type_up sig_type
;
2229 sect_offset sect_off
2230 = (sect_offset
) (extract_unsigned_integer
2231 (map
.tu_table_reordered
+ i
* map
.offset_size
,
2233 map
.dwarf5_byte_order
));
2235 comp_unit_head cu_header
;
2236 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
2238 section
->buffer
+ to_underlying (sect_off
),
2241 sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
2242 (cu_header
.signature
);
2243 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
2244 sig_type
->section
= section
;
2245 sig_type
->sect_off
= sect_off
;
2247 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2248 *slot
= sig_type
.get ();
2250 per_objfile
->per_bfd
->all_units
.emplace_back (sig_type
.release ());
2253 per_objfile
->per_bfd
->signatured_types
= std::move (sig_types_hash
);
2256 /* Read the address map data from the mapped index, and use it to
2257 populate the index_addrmap. */
2260 create_addrmap_from_index (dwarf2_per_objfile
*per_objfile
,
2261 struct mapped_index
*index
)
2263 struct objfile
*objfile
= per_objfile
->objfile
;
2264 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2265 struct gdbarch
*gdbarch
= objfile
->arch ();
2266 const gdb_byte
*iter
, *end
;
2269 addrmap_mutable mutable_map
;
2271 iter
= index
->address_table
.data ();
2272 end
= iter
+ index
->address_table
.size ();
2274 baseaddr
= objfile
->text_section_offset ();
2278 ULONGEST hi
, lo
, cu_index
;
2279 lo
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2281 hi
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2283 cu_index
= extract_unsigned_integer (iter
, 4, BFD_ENDIAN_LITTLE
);
2288 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2289 hex_string (lo
), hex_string (hi
));
2293 if (cu_index
>= per_bfd
->all_units
.size ())
2295 complaint (_(".gdb_index address table has invalid CU number %u"),
2296 (unsigned) cu_index
);
2300 lo
= gdbarch_adjust_dwarf2_addr (gdbarch
, lo
+ baseaddr
) - baseaddr
;
2301 hi
= gdbarch_adjust_dwarf2_addr (gdbarch
, hi
+ baseaddr
) - baseaddr
;
2302 mutable_map
.set_empty (lo
, hi
- 1, per_bfd
->get_cu (cu_index
));
2305 per_bfd
->index_addrmap
2306 = new (&per_bfd
->obstack
) addrmap_fixed (&per_bfd
->obstack
, &mutable_map
);
2309 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2310 to populate given addrmap. Returns true on success, false on
2314 read_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2315 struct dwarf2_section_info
*section
,
2316 addrmap
*mutable_map
)
2318 struct objfile
*objfile
= per_objfile
->objfile
;
2319 bfd
*abfd
= objfile
->obfd
.get ();
2320 struct gdbarch
*gdbarch
= objfile
->arch ();
2321 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
2322 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2324 std::unordered_map
<sect_offset
,
2325 dwarf2_per_cu_data
*,
2326 gdb::hash_enum
<sect_offset
>>
2327 debug_info_offset_to_per_cu
;
2328 for (const auto &per_cu
: per_bfd
->all_units
)
2330 /* A TU will not need aranges, and skipping them here is an easy
2331 way of ignoring .debug_types -- and possibly seeing a
2332 duplicate section offset -- entirely. The same applies to
2333 units coming from a dwz file. */
2334 if (per_cu
->is_debug_types
|| per_cu
->is_dwz
)
2337 const auto insertpair
2338 = debug_info_offset_to_per_cu
.emplace (per_cu
->sect_off
,
2341 /* Assume no duplicate offsets in all_units. */
2342 gdb_assert (insertpair
.second
);
2345 std::set
<sect_offset
> debug_info_offset_seen
;
2347 section
->read (objfile
);
2349 const bfd_endian dwarf5_byte_order
= gdbarch_byte_order (gdbarch
);
2351 const gdb_byte
*addr
= section
->buffer
;
2353 while (addr
< section
->buffer
+ section
->size
)
2355 const gdb_byte
*const entry_addr
= addr
;
2356 unsigned int bytes_read
;
2358 const LONGEST entry_length
= read_initial_length (abfd
, addr
,
2362 const gdb_byte
*const entry_end
= addr
+ entry_length
;
2363 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
2364 const uint8_t offset_size
= dwarf5_is_dwarf64
? 8 : 4;
2365 if (addr
+ entry_length
> section
->buffer
+ section
->size
)
2367 warning (_("Section .debug_aranges in %s entry at offset %s "
2368 "length %s exceeds section length %s, "
2369 "ignoring .debug_aranges."),
2370 objfile_name (objfile
),
2371 plongest (entry_addr
- section
->buffer
),
2372 plongest (bytes_read
+ entry_length
),
2373 pulongest (section
->size
));
2377 /* The version number. */
2378 const uint16_t version
= read_2_bytes (abfd
, addr
);
2382 warning (_("Section .debug_aranges in %s entry at offset %s "
2383 "has unsupported version %d, ignoring .debug_aranges."),
2384 objfile_name (objfile
),
2385 plongest (entry_addr
- section
->buffer
), version
);
2389 const uint64_t debug_info_offset
2390 = extract_unsigned_integer (addr
, offset_size
, dwarf5_byte_order
);
2391 addr
+= offset_size
;
2392 const auto per_cu_it
2393 = debug_info_offset_to_per_cu
.find (sect_offset (debug_info_offset
));
2394 if (per_cu_it
== debug_info_offset_to_per_cu
.cend ())
2396 warning (_("Section .debug_aranges in %s entry at offset %s "
2397 "debug_info_offset %s does not exists, "
2398 "ignoring .debug_aranges."),
2399 objfile_name (objfile
),
2400 plongest (entry_addr
- section
->buffer
),
2401 pulongest (debug_info_offset
));
2404 const auto insertpair
2405 = debug_info_offset_seen
.insert (sect_offset (debug_info_offset
));
2406 if (!insertpair
.second
)
2408 warning (_("Section .debug_aranges in %s has duplicate "
2409 "debug_info_offset %s, ignoring .debug_aranges."),
2410 objfile_name (objfile
),
2411 sect_offset_str (sect_offset (debug_info_offset
)));
2414 dwarf2_per_cu_data
*const per_cu
= per_cu_it
->second
;
2416 const uint8_t address_size
= *addr
++;
2417 if (address_size
< 1 || address_size
> 8)
2419 warning (_("Section .debug_aranges in %s entry at offset %s "
2420 "address_size %u is invalid, ignoring .debug_aranges."),
2421 objfile_name (objfile
),
2422 plongest (entry_addr
- section
->buffer
), address_size
);
2426 const uint8_t segment_selector_size
= *addr
++;
2427 if (segment_selector_size
!= 0)
2429 warning (_("Section .debug_aranges in %s entry at offset %s "
2430 "segment_selector_size %u is not supported, "
2431 "ignoring .debug_aranges."),
2432 objfile_name (objfile
),
2433 plongest (entry_addr
- section
->buffer
),
2434 segment_selector_size
);
2438 /* Must pad to an alignment boundary that is twice the address
2439 size. It is undocumented by the DWARF standard but GCC does
2440 use it. However, not every compiler does this. We can see
2441 whether it has happened by looking at the total length of the
2442 contents of the aranges for this CU -- it if isn't a multiple
2443 of twice the address size, then we skip any leftover
2445 addr
+= (entry_end
- addr
) % (2 * address_size
);
2447 while (addr
< entry_end
)
2449 if (addr
+ 2 * address_size
> entry_end
)
2451 warning (_("Section .debug_aranges in %s entry at offset %s "
2452 "address list is not properly terminated, "
2453 "ignoring .debug_aranges."),
2454 objfile_name (objfile
),
2455 plongest (entry_addr
- section
->buffer
));
2458 ULONGEST start
= extract_unsigned_integer (addr
, address_size
,
2460 addr
+= address_size
;
2461 ULONGEST length
= extract_unsigned_integer (addr
, address_size
,
2463 addr
+= address_size
;
2464 if (start
== 0 && length
== 0)
2466 /* This can happen on some targets with --gc-sections.
2467 This pair of values is also used to mark the end of
2468 the entries for a given CU, but we ignore it and
2469 instead handle termination using the check at the top
2473 if (start
== 0 && !per_bfd
->has_section_at_zero
)
2475 /* Symbol was eliminated due to a COMDAT group. */
2478 ULONGEST end
= start
+ length
;
2479 start
= (gdbarch_adjust_dwarf2_addr (gdbarch
, start
+ baseaddr
)
2481 end
= (gdbarch_adjust_dwarf2_addr (gdbarch
, end
+ baseaddr
)
2483 mutable_map
->set_empty (start
, end
- 1, per_cu
);
2486 per_cu
->addresses_seen
= true;
2492 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2493 populate the index_addrmap. */
2496 create_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2497 struct dwarf2_section_info
*section
)
2499 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2501 addrmap_mutable mutable_map
;
2503 if (read_addrmap_from_aranges (per_objfile
, section
, &mutable_map
))
2504 per_bfd
->index_addrmap
2505 = new (&per_bfd
->obstack
) addrmap_fixed (&per_bfd
->obstack
,
2509 /* A helper function that reads the .gdb_index from BUFFER and fills
2510 in MAP. FILENAME is the name of the file containing the data;
2511 it is used for error reporting. DEPRECATED_OK is true if it is
2512 ok to use deprecated sections.
2514 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2515 out parameters that are filled in with information about the CU and
2516 TU lists in the section.
2518 Returns true if all went well, false otherwise. */
2521 read_gdb_index_from_buffer (const char *filename
,
2523 gdb::array_view
<const gdb_byte
> buffer
,
2524 struct mapped_index
*map
,
2525 const gdb_byte
**cu_list
,
2526 offset_type
*cu_list_elements
,
2527 const gdb_byte
**types_list
,
2528 offset_type
*types_list_elements
)
2530 const gdb_byte
*addr
= &buffer
[0];
2531 offset_view
metadata (buffer
);
2533 /* Version check. */
2534 offset_type version
= metadata
[0];
2535 /* Versions earlier than 3 emitted every copy of a psymbol. This
2536 causes the index to behave very poorly for certain requests. Version 3
2537 contained incomplete addrmap. So, it seems better to just ignore such
2541 static int warning_printed
= 0;
2542 if (!warning_printed
)
2544 warning (_("Skipping obsolete .gdb_index section in %s."),
2546 warning_printed
= 1;
2550 /* Index version 4 uses a different hash function than index version
2553 Versions earlier than 6 did not emit psymbols for inlined
2554 functions. Using these files will cause GDB not to be able to
2555 set breakpoints on inlined functions by name, so we ignore these
2556 indices unless the user has done
2557 "set use-deprecated-index-sections on". */
2558 if (version
< 6 && !deprecated_ok
)
2560 static int warning_printed
= 0;
2561 if (!warning_printed
)
2564 Skipping deprecated .gdb_index section in %s.\n\
2565 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2566 to use the section anyway."),
2568 warning_printed
= 1;
2572 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2573 of the TU (for symbols coming from TUs),
2574 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2575 Plus gold-generated indices can have duplicate entries for global symbols,
2576 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2577 These are just performance bugs, and we can't distinguish gdb-generated
2578 indices from gold-generated ones, so issue no warning here. */
2580 /* Indexes with higher version than the one supported by GDB may be no
2581 longer backward compatible. */
2585 map
->version
= version
;
2588 *cu_list
= addr
+ metadata
[i
];
2589 *cu_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2592 *types_list
= addr
+ metadata
[i
];
2593 *types_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2596 const gdb_byte
*address_table
= addr
+ metadata
[i
];
2597 const gdb_byte
*address_table_end
= addr
+ metadata
[i
+ 1];
2599 = gdb::array_view
<const gdb_byte
> (address_table
, address_table_end
);
2602 const gdb_byte
*symbol_table
= addr
+ metadata
[i
];
2603 const gdb_byte
*symbol_table_end
= addr
+ metadata
[i
+ 1];
2605 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2609 map
->constant_pool
= buffer
.slice (metadata
[i
]);
2611 if (map
->constant_pool
.empty () && !map
->symbol_table
.empty ())
2613 /* An empty constant pool implies that all symbol table entries are
2614 empty. Make map->symbol_table.empty () == true. */
2616 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2623 static void finalize_all_units (dwarf2_per_bfd
*per_bfd
);
2625 /* Callback types for dwarf2_read_gdb_index. */
2627 typedef gdb::function_view
2628 <gdb::array_view
<const gdb_byte
>(objfile
*, dwarf2_per_bfd
*)>
2629 get_gdb_index_contents_ftype
;
2630 typedef gdb::function_view
2631 <gdb::array_view
<const gdb_byte
>(objfile
*, dwz_file
*)>
2632 get_gdb_index_contents_dwz_ftype
;
2634 /* Read .gdb_index. If everything went ok, initialize the "quick"
2635 elements of all the CUs and return 1. Otherwise, return 0. */
2638 dwarf2_read_gdb_index
2639 (dwarf2_per_objfile
*per_objfile
,
2640 get_gdb_index_contents_ftype get_gdb_index_contents
,
2641 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz
)
2643 const gdb_byte
*cu_list
, *types_list
, *dwz_list
= NULL
;
2644 offset_type cu_list_elements
, types_list_elements
, dwz_list_elements
= 0;
2645 struct dwz_file
*dwz
;
2646 struct objfile
*objfile
= per_objfile
->objfile
;
2647 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2649 gdb::array_view
<const gdb_byte
> main_index_contents
2650 = get_gdb_index_contents (objfile
, per_bfd
);
2652 if (main_index_contents
.empty ())
2655 std::unique_ptr
<struct mapped_index
> map (new struct mapped_index
);
2656 if (!read_gdb_index_from_buffer (objfile_name (objfile
),
2657 use_deprecated_index_sections
,
2658 main_index_contents
, map
.get (), &cu_list
,
2659 &cu_list_elements
, &types_list
,
2660 &types_list_elements
))
2663 /* Don't use the index if it's empty. */
2664 if (map
->symbol_table
.empty ())
2667 /* If there is a .dwz file, read it so we can get its CU list as
2669 dwz
= dwarf2_get_dwz_file (per_bfd
);
2672 struct mapped_index dwz_map
;
2673 const gdb_byte
*dwz_types_ignore
;
2674 offset_type dwz_types_elements_ignore
;
2676 gdb::array_view
<const gdb_byte
> dwz_index_content
2677 = get_gdb_index_contents_dwz (objfile
, dwz
);
2679 if (dwz_index_content
.empty ())
2682 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz
->dwz_bfd
.get ()),
2683 1, dwz_index_content
, &dwz_map
,
2684 &dwz_list
, &dwz_list_elements
,
2686 &dwz_types_elements_ignore
))
2688 warning (_("could not read '.gdb_index' section from %s; skipping"),
2689 bfd_get_filename (dwz
->dwz_bfd
.get ()));
2694 create_cus_from_index (per_bfd
, cu_list
, cu_list_elements
, dwz_list
,
2697 if (types_list_elements
)
2699 /* We can only handle a single .debug_types when we have an
2701 if (per_bfd
->types
.size () > 1)
2703 per_bfd
->all_units
.clear ();
2707 dwarf2_section_info
*section
2708 = (per_bfd
->types
.size () == 1
2709 ? &per_bfd
->types
[0]
2712 create_signatured_type_table_from_index (per_bfd
, section
, types_list
,
2713 types_list_elements
);
2716 finalize_all_units (per_bfd
);
2718 create_addrmap_from_index (per_objfile
, map
.get ());
2720 per_bfd
->index_table
= std::move (map
);
2721 per_bfd
->quick_file_names_table
=
2722 create_quick_file_names_table (per_bfd
->all_units
.size ());
2727 /* die_reader_func for dw2_get_file_names. */
2730 dw2_get_file_names_reader (const struct die_reader_specs
*reader
,
2731 struct die_info
*comp_unit_die
)
2733 struct dwarf2_cu
*cu
= reader
->cu
;
2734 struct dwarf2_per_cu_data
*this_cu
= cu
->per_cu
;
2735 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
2736 struct dwarf2_per_cu_data
*lh_cu
;
2737 struct attribute
*attr
;
2739 struct quick_file_names
*qfn
;
2741 gdb_assert (! this_cu
->is_debug_types
);
2743 this_cu
->files_read
= true;
2744 /* Our callers never want to match partial units -- instead they
2745 will match the enclosing full CU. */
2746 if (comp_unit_die
->tag
== DW_TAG_partial_unit
)
2753 sect_offset line_offset
{};
2755 file_and_directory
&fnd
= find_file_and_directory (comp_unit_die
, cu
);
2757 attr
= dwarf2_attr (comp_unit_die
, DW_AT_stmt_list
, cu
);
2758 if (attr
!= nullptr && attr
->form_is_unsigned ())
2760 struct quick_file_names find_entry
;
2762 line_offset
= (sect_offset
) attr
->as_unsigned ();
2764 /* We may have already read in this line header (TU line header sharing).
2765 If we have we're done. */
2766 find_entry
.hash
.dwo_unit
= cu
->dwo_unit
;
2767 find_entry
.hash
.line_sect_off
= line_offset
;
2768 slot
= htab_find_slot (per_objfile
->per_bfd
->quick_file_names_table
.get (),
2769 &find_entry
, INSERT
);
2772 lh_cu
->file_names
= (struct quick_file_names
*) *slot
;
2776 lh
= dwarf_decode_line_header (line_offset
, cu
, fnd
.get_comp_dir ());
2780 if (!fnd
.is_unknown ())
2782 else if (lh
== nullptr)
2785 qfn
= XOBNEW (&per_objfile
->per_bfd
->obstack
, struct quick_file_names
);
2786 qfn
->hash
.dwo_unit
= cu
->dwo_unit
;
2787 qfn
->hash
.line_sect_off
= line_offset
;
2788 /* There may not be a DW_AT_stmt_list. */
2789 if (slot
!= nullptr)
2792 std::vector
<const char *> include_names
;
2795 for (const auto &entry
: lh
->file_names ())
2797 std::string name_holder
;
2798 const char *include_name
=
2799 compute_include_file_name (lh
.get (), entry
, fnd
, name_holder
);
2800 if (include_name
!= nullptr)
2802 include_name
= per_objfile
->objfile
->intern (include_name
);
2803 include_names
.push_back (include_name
);
2808 qfn
->num_file_names
= offset
+ include_names
.size ();
2809 qfn
->comp_dir
= fnd
.intern_comp_dir (per_objfile
->objfile
);
2811 XOBNEWVEC (&per_objfile
->per_bfd
->obstack
, const char *,
2812 qfn
->num_file_names
);
2814 qfn
->file_names
[0] = per_objfile
->objfile
->intern (fnd
.get_name ());
2816 if (!include_names
.empty ())
2817 memcpy (&qfn
->file_names
[offset
], include_names
.data (),
2818 include_names
.size () * sizeof (const char *));
2820 qfn
->real_names
= NULL
;
2822 lh_cu
->file_names
= qfn
;
2825 /* A helper for the "quick" functions which attempts to read the line
2826 table for THIS_CU. */
2828 static struct quick_file_names
*
2829 dw2_get_file_names (dwarf2_per_cu_data
*this_cu
,
2830 dwarf2_per_objfile
*per_objfile
)
2832 /* This should never be called for TUs. */
2833 gdb_assert (! this_cu
->is_debug_types
);
2835 if (this_cu
->files_read
)
2836 return this_cu
->file_names
;
2838 cutu_reader
reader (this_cu
, per_objfile
);
2839 if (!reader
.dummy_p
)
2840 dw2_get_file_names_reader (&reader
, reader
.comp_unit_die
);
2842 return this_cu
->file_names
;
2845 /* A helper for the "quick" functions which computes and caches the
2846 real path for a given file name from the line table. */
2849 dw2_get_real_path (dwarf2_per_objfile
*per_objfile
,
2850 struct quick_file_names
*qfn
, int index
)
2852 if (qfn
->real_names
== NULL
)
2853 qfn
->real_names
= OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
2854 qfn
->num_file_names
, const char *);
2856 if (qfn
->real_names
[index
] == NULL
)
2858 const char *dirname
= nullptr;
2860 if (!IS_ABSOLUTE_PATH (qfn
->file_names
[index
]))
2861 dirname
= qfn
->comp_dir
;
2863 gdb::unique_xmalloc_ptr
<char> fullname
;
2864 fullname
= find_source_or_rewrite (qfn
->file_names
[index
], dirname
);
2866 qfn
->real_names
[index
] = fullname
.release ();
2869 return qfn
->real_names
[index
];
2873 dwarf2_base_index_functions::find_last_source_symtab (struct objfile
*objfile
)
2875 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2876 dwarf2_per_cu_data
*dwarf_cu
2877 = per_objfile
->per_bfd
->all_units
.back ().get ();
2878 compunit_symtab
*cust
= dw2_instantiate_symtab (dwarf_cu
, per_objfile
, false);
2883 return cust
->primary_filetab ();
2889 dwarf2_per_cu_data::free_cached_file_names ()
2892 fnd
->forget_fullname ();
2894 if (per_bfd
== nullptr)
2897 struct quick_file_names
*file_data
= file_names
;
2898 if (file_data
!= nullptr && file_data
->real_names
!= nullptr)
2900 for (int i
= 0; i
< file_data
->num_file_names
; ++i
)
2902 xfree ((void *) file_data
->real_names
[i
]);
2903 file_data
->real_names
[i
] = nullptr;
2909 dwarf2_base_index_functions::forget_cached_source_info
2910 (struct objfile
*objfile
)
2912 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2914 for (auto &per_cu
: per_objfile
->per_bfd
->all_units
)
2915 per_cu
->free_cached_file_names ();
2918 /* Struct used to manage iterating over all CUs looking for a symbol. */
2920 struct dw2_symtab_iterator
2922 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2923 dwarf2_per_objfile
*per_objfile
;
2924 /* If set, only look for symbols that match that block. Valid values are
2925 GLOBAL_BLOCK and STATIC_BLOCK. */
2926 gdb::optional
<block_enum
> block_index
;
2927 /* The kind of symbol we're looking for. */
2929 /* The list of CUs from the index entry of the symbol,
2930 or NULL if not found. */
2932 /* The next element in VEC to look at. */
2934 /* The number of elements in VEC, or zero if there is no match. */
2936 /* Have we seen a global version of the symbol?
2937 If so we can ignore all further global instances.
2938 This is to work around gold/15646, inefficient gold-generated
2943 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2946 dw2_symtab_iter_init (struct dw2_symtab_iterator
*iter
,
2947 dwarf2_per_objfile
*per_objfile
,
2948 gdb::optional
<block_enum
> block_index
,
2949 domain_enum domain
, offset_type namei
,
2950 mapped_index
&index
)
2952 iter
->per_objfile
= per_objfile
;
2953 iter
->block_index
= block_index
;
2954 iter
->domain
= domain
;
2956 iter
->global_seen
= 0;
2960 gdb_assert (!index
.symbol_name_slot_invalid (namei
));
2961 offset_type vec_idx
= index
.symbol_vec_index (namei
);
2963 iter
->vec
= offset_view (index
.constant_pool
.slice (vec_idx
));
2964 iter
->length
= iter
->vec
[0];
2967 /* Return the next matching CU or NULL if there are no more. */
2969 static struct dwarf2_per_cu_data
*
2970 dw2_symtab_iter_next (struct dw2_symtab_iterator
*iter
,
2971 mapped_index
&index
)
2973 dwarf2_per_objfile
*per_objfile
= iter
->per_objfile
;
2975 for ( ; iter
->next
< iter
->length
; ++iter
->next
)
2977 offset_type cu_index_and_attrs
= iter
->vec
[iter
->next
+ 1];
2978 offset_type cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
2979 gdb_index_symbol_kind symbol_kind
=
2980 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
2981 /* Only check the symbol attributes if they're present.
2982 Indices prior to version 7 don't record them,
2983 and indices >= 7 may elide them for certain symbols
2984 (gold does this). */
2985 int attrs_valid
= (index
.version
>= 7
2986 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
2988 /* Don't crash on bad data. */
2989 if (cu_index
>= per_objfile
->per_bfd
->all_units
.size ())
2991 complaint (_(".gdb_index entry has bad CU index"
2992 " [in module %s]"), objfile_name (per_objfile
->objfile
));
2996 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
2998 /* Skip if already read in. */
2999 if (per_objfile
->symtab_set_p (per_cu
))
3002 /* Check static vs global. */
3005 bool is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
3007 if (iter
->block_index
.has_value ())
3009 bool want_static
= *iter
->block_index
== STATIC_BLOCK
;
3011 if (is_static
!= want_static
)
3015 /* Work around gold/15646. */
3017 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
3019 if (iter
->global_seen
)
3022 iter
->global_seen
= 1;
3026 /* Only check the symbol's kind if it has one. */
3029 switch (iter
->domain
)
3032 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
3033 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
3034 /* Some types are also in VAR_DOMAIN. */
3035 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3039 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3043 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3047 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3063 dwarf2_base_index_functions::print_stats (struct objfile
*objfile
,
3069 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3070 int total
= per_objfile
->per_bfd
->all_units
.size ();
3073 for (int i
= 0; i
< total
; ++i
)
3075 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3077 if (!per_objfile
->symtab_set_p (per_cu
))
3080 gdb_printf (_(" Number of read CUs: %d\n"), total
- count
);
3081 gdb_printf (_(" Number of unread CUs: %d\n"), count
);
3084 /* This dumps minimal information about the index.
3085 It is called via "mt print objfiles".
3086 One use is to verify .gdb_index has been loaded by the
3087 gdb.dwarf2/gdb-index.exp testcase. */
3090 dwarf2_gdb_index::dump (struct objfile
*objfile
)
3092 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3094 mapped_index
*index
= (gdb::checked_static_cast
<mapped_index
*>
3095 (per_objfile
->per_bfd
->index_table
.get ()));
3096 gdb_printf (".gdb_index: version %d\n", index
->version
);
3101 dwarf2_base_index_functions::expand_all_symtabs (struct objfile
*objfile
)
3103 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3104 int total_units
= per_objfile
->per_bfd
->all_units
.size ();
3106 for (int i
= 0; i
< total_units
; ++i
)
3108 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3110 /* We don't want to directly expand a partial CU, because if we
3111 read it with the wrong language, then assertion failures can
3112 be triggered later on. See PR symtab/23010. So, tell
3113 dw2_instantiate_symtab to skip partial CUs -- any important
3114 partial CU will be read via DW_TAG_imported_unit anyway. */
3115 dw2_instantiate_symtab (per_cu
, per_objfile
, true);
3120 dw2_expand_symtabs_matching_symbol
3121 (mapped_index_base
&index
,
3122 const lookup_name_info
&lookup_name_in
,
3123 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3124 gdb::function_view
<bool (offset_type
)> match_callback
,
3125 dwarf2_per_objfile
*per_objfile
);
3128 dw2_expand_symtabs_matching_one
3129 (dwarf2_per_cu_data
*per_cu
,
3130 dwarf2_per_objfile
*per_objfile
,
3131 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3132 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
);
3135 dwarf2_gdb_index::expand_matching_symbols
3136 (struct objfile
*objfile
,
3137 const lookup_name_info
&name
, domain_enum domain
,
3139 symbol_compare_ftype
*ordered_compare
)
3142 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3144 const block_enum block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
3147 = *(gdb::checked_static_cast
<mapped_index
*>
3148 (per_objfile
->per_bfd
->index_table
.get ()));
3150 const char *match_name
= name
.ada ().lookup_name ().c_str ();
3151 auto matcher
= [&] (const char *symname
)
3153 if (ordered_compare
== nullptr)
3155 return ordered_compare (symname
, match_name
) == 0;
3158 dw2_expand_symtabs_matching_symbol (index
, name
, matcher
,
3159 [&] (offset_type namei
)
3161 struct dw2_symtab_iterator iter
;
3162 struct dwarf2_per_cu_data
*per_cu
;
3164 dw2_symtab_iter_init (&iter
, per_objfile
, block_kind
, domain
, namei
,
3166 while ((per_cu
= dw2_symtab_iter_next (&iter
, index
)) != NULL
)
3167 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
3173 /* Starting from a search name, return the string that finds the upper
3174 bound of all strings that start with SEARCH_NAME in a sorted name
3175 list. Returns the empty string to indicate that the upper bound is
3176 the end of the list. */
3179 make_sort_after_prefix_name (const char *search_name
)
3181 /* When looking to complete "func", we find the upper bound of all
3182 symbols that start with "func" by looking for where we'd insert
3183 the closest string that would follow "func" in lexicographical
3184 order. Usually, that's "func"-with-last-character-incremented,
3185 i.e. "fund". Mind non-ASCII characters, though. Usually those
3186 will be UTF-8 multi-byte sequences, but we can't be certain.
3187 Especially mind the 0xff character, which is a valid character in
3188 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3189 rule out compilers allowing it in identifiers. Note that
3190 conveniently, strcmp/strcasecmp are specified to compare
3191 characters interpreted as unsigned char. So what we do is treat
3192 the whole string as a base 256 number composed of a sequence of
3193 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3194 to 0, and carries 1 to the following more-significant position.
3195 If the very first character in SEARCH_NAME ends up incremented
3196 and carries/overflows, then the upper bound is the end of the
3197 list. The string after the empty string is also the empty
3200 Some examples of this operation:
3202 SEARCH_NAME => "+1" RESULT
3206 "\xff" "a" "\xff" => "\xff" "b"
3211 Then, with these symbols for example:
3217 completing "func" looks for symbols between "func" and
3218 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3219 which finds "func" and "func1", but not "fund".
3223 funcÿ (Latin1 'ÿ' [0xff])
3227 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3228 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3232 ÿÿ (Latin1 'ÿ' [0xff])
3235 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3236 the end of the list.
3238 std::string after
= search_name
;
3239 while (!after
.empty () && (unsigned char) after
.back () == 0xff)
3241 if (!after
.empty ())
3242 after
.back () = (unsigned char) after
.back () + 1;
3246 /* See declaration. */
3248 std::pair
<std::vector
<name_component
>::const_iterator
,
3249 std::vector
<name_component
>::const_iterator
>
3250 mapped_index_base::find_name_components_bounds
3251 (const lookup_name_info
&lookup_name_without_params
, language lang
,
3252 dwarf2_per_objfile
*per_objfile
) const
3255 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3257 const char *lang_name
3258 = lookup_name_without_params
.language_lookup_name (lang
);
3260 /* Comparison function object for lower_bound that matches against a
3261 given symbol name. */
3262 auto lookup_compare_lower
= [&] (const name_component
&elem
,
3265 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3266 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3267 return name_cmp (elem_name
, name
) < 0;
3270 /* Comparison function object for upper_bound that matches against a
3271 given symbol name. */
3272 auto lookup_compare_upper
= [&] (const char *name
,
3273 const name_component
&elem
)
3275 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3276 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3277 return name_cmp (name
, elem_name
) < 0;
3280 auto begin
= this->name_components
.begin ();
3281 auto end
= this->name_components
.end ();
3283 /* Find the lower bound. */
3286 if (lookup_name_without_params
.completion_mode () && lang_name
[0] == '\0')
3289 return std::lower_bound (begin
, end
, lang_name
, lookup_compare_lower
);
3292 /* Find the upper bound. */
3295 if (lookup_name_without_params
.completion_mode ())
3297 /* In completion mode, we want UPPER to point past all
3298 symbols names that have the same prefix. I.e., with
3299 these symbols, and completing "func":
3301 function << lower bound
3303 other_function << upper bound
3305 We find the upper bound by looking for the insertion
3306 point of "func"-with-last-character-incremented,
3308 std::string after
= make_sort_after_prefix_name (lang_name
);
3311 return std::lower_bound (lower
, end
, after
.c_str (),
3312 lookup_compare_lower
);
3315 return std::upper_bound (lower
, end
, lang_name
, lookup_compare_upper
);
3318 return {lower
, upper
};
3321 /* See declaration. */
3324 mapped_index_base::build_name_components (dwarf2_per_objfile
*per_objfile
)
3326 if (!this->name_components
.empty ())
3329 this->name_components_casing
= case_sensitivity
;
3331 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3333 /* The code below only knows how to break apart components of C++
3334 symbol names (and other languages that use '::' as
3335 namespace/module separator) and Ada symbol names. */
3336 auto count
= this->symbol_name_count ();
3337 for (offset_type idx
= 0; idx
< count
; idx
++)
3339 if (this->symbol_name_slot_invalid (idx
))
3342 const char *name
= this->symbol_name_at (idx
, per_objfile
);
3344 /* Add each name component to the name component table. */
3345 unsigned int previous_len
= 0;
3347 if (strstr (name
, "::") != nullptr)
3349 for (unsigned int current_len
= cp_find_first_component (name
);
3350 name
[current_len
] != '\0';
3351 current_len
+= cp_find_first_component (name
+ current_len
))
3353 gdb_assert (name
[current_len
] == ':');
3354 this->name_components
.push_back ({previous_len
, idx
});
3355 /* Skip the '::'. */
3357 previous_len
= current_len
;
3362 /* Handle the Ada encoded (aka mangled) form here. */
3363 for (const char *iter
= strstr (name
, "__");
3365 iter
= strstr (iter
, "__"))
3367 this->name_components
.push_back ({previous_len
, idx
});
3369 previous_len
= iter
- name
;
3373 this->name_components
.push_back ({previous_len
, idx
});
3376 /* Sort name_components elements by name. */
3377 auto name_comp_compare
= [&] (const name_component
&left
,
3378 const name_component
&right
)
3380 const char *left_qualified
3381 = this->symbol_name_at (left
.idx
, per_objfile
);
3382 const char *right_qualified
3383 = this->symbol_name_at (right
.idx
, per_objfile
);
3385 const char *left_name
= left_qualified
+ left
.name_offset
;
3386 const char *right_name
= right_qualified
+ right
.name_offset
;
3388 return name_cmp (left_name
, right_name
) < 0;
3391 std::sort (this->name_components
.begin (),
3392 this->name_components
.end (),
3396 /* Helper for dw2_expand_symtabs_matching that works with a
3397 mapped_index_base instead of the containing objfile. This is split
3398 to a separate function in order to be able to unit test the
3399 name_components matching using a mock mapped_index_base. For each
3400 symbol name that matches, calls MATCH_CALLBACK, passing it the
3401 symbol's index in the mapped_index_base symbol table. */
3404 dw2_expand_symtabs_matching_symbol
3405 (mapped_index_base
&index
,
3406 const lookup_name_info
&lookup_name_in
,
3407 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3408 gdb::function_view
<bool (offset_type
)> match_callback
,
3409 dwarf2_per_objfile
*per_objfile
)
3411 lookup_name_info lookup_name_without_params
3412 = lookup_name_in
.make_ignore_params ();
3414 /* Build the symbol name component sorted vector, if we haven't
3416 index
.build_name_components (per_objfile
);
3418 /* The same symbol may appear more than once in the range though.
3419 E.g., if we're looking for symbols that complete "w", and we have
3420 a symbol named "w1::w2", we'll find the two name components for
3421 that same symbol in the range. To be sure we only call the
3422 callback once per symbol, we first collect the symbol name
3423 indexes that matched in a temporary vector and ignore
3425 std::vector
<offset_type
> matches
;
3427 struct name_and_matcher
3429 symbol_name_matcher_ftype
*matcher
;
3432 bool operator== (const name_and_matcher
&other
) const
3434 return matcher
== other
.matcher
&& strcmp (name
, other
.name
) == 0;
3438 /* A vector holding all the different symbol name matchers, for all
3440 std::vector
<name_and_matcher
> matchers
;
3442 for (int i
= 0; i
< nr_languages
; i
++)
3444 enum language lang_e
= (enum language
) i
;
3446 const language_defn
*lang
= language_def (lang_e
);
3447 symbol_name_matcher_ftype
*name_matcher
3448 = lang
->get_symbol_name_matcher (lookup_name_without_params
);
3450 name_and_matcher key
{
3452 lookup_name_without_params
.language_lookup_name (lang_e
)
3455 /* Don't insert the same comparison routine more than once.
3456 Note that we do this linear walk. This is not a problem in
3457 practice because the number of supported languages is
3459 if (std::find (matchers
.begin (), matchers
.end (), key
)
3462 matchers
.push_back (std::move (key
));
3465 = index
.find_name_components_bounds (lookup_name_without_params
,
3466 lang_e
, per_objfile
);
3468 /* Now for each symbol name in range, check to see if we have a name
3469 match, and if so, call the MATCH_CALLBACK callback. */
3471 for (; bounds
.first
!= bounds
.second
; ++bounds
.first
)
3473 const char *qualified
3474 = index
.symbol_name_at (bounds
.first
->idx
, per_objfile
);
3476 if (!name_matcher (qualified
, lookup_name_without_params
, NULL
)
3477 || (symbol_matcher
!= NULL
&& !symbol_matcher (qualified
)))
3480 matches
.push_back (bounds
.first
->idx
);
3484 std::sort (matches
.begin (), matches
.end ());
3486 /* Finally call the callback, once per match. */
3489 for (offset_type idx
: matches
)
3493 if (!match_callback (idx
))
3502 /* Above we use a type wider than idx's for 'prev', since 0 and
3503 (offset_type)-1 are both possible values. */
3504 static_assert (sizeof (prev
) > sizeof (offset_type
), "");
3511 namespace selftests
{ namespace dw2_expand_symtabs_matching
{
3513 /* A mock .gdb_index/.debug_names-like name index table, enough to
3514 exercise dw2_expand_symtabs_matching_symbol, which works with the
3515 mapped_index_base interface. Builds an index from the symbol list
3516 passed as parameter to the constructor. */
3517 class mock_mapped_index
: public mapped_index_base
3520 mock_mapped_index (gdb::array_view
<const char *> symbols
)
3521 : m_symbol_table (symbols
)
3524 DISABLE_COPY_AND_ASSIGN (mock_mapped_index
);
3526 /* Return the number of names in the symbol table. */
3527 size_t symbol_name_count () const override
3529 return m_symbol_table
.size ();
3532 /* Get the name of the symbol at IDX in the symbol table. */
3533 const char *symbol_name_at
3534 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
3536 return m_symbol_table
[idx
];
3539 quick_symbol_functions_up
make_quick_functions () const override
3545 gdb::array_view
<const char *> m_symbol_table
;
3548 /* Convenience function that converts a NULL pointer to a "<null>"
3549 string, to pass to print routines. */
3552 string_or_null (const char *str
)
3554 return str
!= NULL
? str
: "<null>";
3557 /* Check if a lookup_name_info built from
3558 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3559 index. EXPECTED_LIST is the list of expected matches, in expected
3560 matching order. If no match expected, then an empty list is
3561 specified. Returns true on success. On failure prints a warning
3562 indicating the file:line that failed, and returns false. */
3565 check_match (const char *file
, int line
,
3566 mock_mapped_index
&mock_index
,
3567 const char *name
, symbol_name_match_type match_type
,
3568 bool completion_mode
,
3569 std::initializer_list
<const char *> expected_list
,
3570 dwarf2_per_objfile
*per_objfile
)
3572 lookup_name_info
lookup_name (name
, match_type
, completion_mode
);
3574 bool matched
= true;
3576 auto mismatch
= [&] (const char *expected_str
,
3579 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3580 "expected=\"%s\", got=\"%s\"\n"),
3582 (match_type
== symbol_name_match_type::FULL
3584 name
, string_or_null (expected_str
), string_or_null (got
));
3588 auto expected_it
= expected_list
.begin ();
3589 auto expected_end
= expected_list
.end ();
3591 dw2_expand_symtabs_matching_symbol (mock_index
, lookup_name
,
3593 [&] (offset_type idx
)
3595 const char *matched_name
= mock_index
.symbol_name_at (idx
, per_objfile
);
3596 const char *expected_str
3597 = expected_it
== expected_end
? NULL
: *expected_it
++;
3599 if (expected_str
== NULL
|| strcmp (expected_str
, matched_name
) != 0)
3600 mismatch (expected_str
, matched_name
);
3604 const char *expected_str
3605 = expected_it
== expected_end
? NULL
: *expected_it
++;
3606 if (expected_str
!= NULL
)
3607 mismatch (expected_str
, NULL
);
3612 /* The symbols added to the mock mapped_index for testing (in
3614 static const char *test_symbols
[] = {
3623 "ns2::tmpl<int>::foo2",
3624 "(anonymous namespace)::A::B::C",
3626 /* These are used to check that the increment-last-char in the
3627 matching algorithm for completion doesn't match "t1_fund" when
3628 completing "t1_func". */
3634 /* A UTF-8 name with multi-byte sequences to make sure that
3635 cp-name-parser understands this as a single identifier ("função"
3636 is "function" in PT). */
3639 /* Test a symbol name that ends with a 0xff character, which is a
3640 valid character in non-UTF-8 source character sets (e.g. Latin1
3641 'ÿ'), and we can't rule out compilers allowing it in identifiers.
3642 We test this because the completion algorithm finds the upper
3643 bound of symbols by looking for the insertion point of
3644 "func"-with-last-character-incremented, i.e. "fund", and adding 1
3645 to 0xff should wraparound and carry to the previous character.
3646 See comments in make_sort_after_prefix_name. */
3649 /* Some more symbols with \377 (0xff). See above. */
3653 /* A name with all sorts of complications. Starts with "z" to make
3654 it easier for the completion tests below. */
3655 #define Z_SYM_NAME \
3656 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3657 "::tuple<(anonymous namespace)::ui*, " \
3658 "std::default_delete<(anonymous namespace)::ui>, void>"
3663 /* Returns true if the mapped_index_base::find_name_component_bounds
3664 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3665 in completion mode. */
3668 check_find_bounds_finds (mapped_index_base
&index
,
3669 const char *search_name
,
3670 gdb::array_view
<const char *> expected_syms
,
3671 dwarf2_per_objfile
*per_objfile
)
3673 lookup_name_info
lookup_name (search_name
,
3674 symbol_name_match_type::FULL
, true);
3676 auto bounds
= index
.find_name_components_bounds (lookup_name
,
3680 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
3681 if (distance
!= expected_syms
.size ())
3684 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
3686 auto nc_elem
= bounds
.first
+ exp_elem
;
3687 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
3688 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
3695 /* Test the lower-level mapped_index::find_name_component_bounds
3699 test_mapped_index_find_name_component_bounds ()
3701 mock_mapped_index
mock_index (test_symbols
);
3703 mock_index
.build_name_components (NULL
/* per_objfile */);
3705 /* Test the lower-level mapped_index::find_name_component_bounds
3706 method in completion mode. */
3708 static const char *expected_syms
[] = {
3713 SELF_CHECK (check_find_bounds_finds
3714 (mock_index
, "t1_func", expected_syms
,
3715 NULL
/* per_objfile */));
3718 /* Check that the increment-last-char in the name matching algorithm
3719 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
3720 make_sort_after_prefix_name. */
3722 static const char *expected_syms1
[] = {
3726 SELF_CHECK (check_find_bounds_finds
3727 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
3729 static const char *expected_syms2
[] = {
3732 SELF_CHECK (check_find_bounds_finds
3733 (mock_index
, "\377\377", expected_syms2
,
3734 NULL
/* per_objfile */));
3738 /* Test dw2_expand_symtabs_matching_symbol. */
3741 test_dw2_expand_symtabs_matching_symbol ()
3743 mock_mapped_index
mock_index (test_symbols
);
3745 /* We let all tests run until the end even if some fails, for debug
3747 bool any_mismatch
= false;
3749 /* Create the expected symbols list (an initializer_list). Needed
3750 because lists have commas, and we need to pass them to CHECK,
3751 which is a macro. */
3752 #define EXPECT(...) { __VA_ARGS__ }
3754 /* Wrapper for check_match that passes down the current
3755 __FILE__/__LINE__. */
3756 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3757 any_mismatch |= !check_match (__FILE__, __LINE__, \
3759 NAME, MATCH_TYPE, COMPLETION_MODE, \
3760 EXPECTED_LIST, NULL)
3762 /* Identity checks. */
3763 for (const char *sym
: test_symbols
)
3765 /* Should be able to match all existing symbols. */
3766 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
3769 /* Should be able to match all existing symbols with
3771 std::string with_params
= std::string (sym
) + "(int)";
3772 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3775 /* Should be able to match all existing symbols with
3776 parameters and qualifiers. */
3777 with_params
= std::string (sym
) + " ( int ) const";
3778 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3781 /* This should really find sym, but cp-name-parser.y doesn't
3782 know about lvalue/rvalue qualifiers yet. */
3783 with_params
= std::string (sym
) + " ( int ) &&";
3784 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3788 /* Check that the name matching algorithm for completion doesn't get
3789 confused with Latin1 'ÿ' / 0xff. See
3790 make_sort_after_prefix_name. */
3792 static const char str
[] = "\377";
3793 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3794 EXPECT ("\377", "\377\377123"));
3797 /* Check that the increment-last-char in the matching algorithm for
3798 completion doesn't match "t1_fund" when completing "t1_func". */
3800 static const char str
[] = "t1_func";
3801 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3802 EXPECT ("t1_func", "t1_func1"));
3805 /* Check that completion mode works at each prefix of the expected
3808 static const char str
[] = "function(int)";
3809 size_t len
= strlen (str
);
3812 for (size_t i
= 1; i
< len
; i
++)
3814 lookup
.assign (str
, i
);
3815 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3816 EXPECT ("function"));
3820 /* While "w" is a prefix of both components, the match function
3821 should still only be called once. */
3823 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
3825 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
3829 /* Same, with a "complicated" symbol. */
3831 static const char str
[] = Z_SYM_NAME
;
3832 size_t len
= strlen (str
);
3835 for (size_t i
= 1; i
< len
; i
++)
3837 lookup
.assign (str
, i
);
3838 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3839 EXPECT (Z_SYM_NAME
));
3843 /* In FULL mode, an incomplete symbol doesn't match. */
3845 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
3849 /* A complete symbol with parameters matches any overload, since the
3850 index has no overload info. */
3852 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
3853 EXPECT ("std::zfunction", "std::zfunction2"));
3854 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
3855 EXPECT ("std::zfunction", "std::zfunction2"));
3856 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
3857 EXPECT ("std::zfunction", "std::zfunction2"));
3860 /* Check that whitespace is ignored appropriately. A symbol with a
3861 template argument list. */
3863 static const char expected
[] = "ns::foo<int>";
3864 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
3866 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
3870 /* Check that whitespace is ignored appropriately. A symbol with a
3871 template argument list that includes a pointer. */
3873 static const char expected
[] = "ns::foo<char*>";
3874 /* Try both completion and non-completion modes. */
3875 static const bool completion_mode
[2] = {false, true};
3876 for (size_t i
= 0; i
< 2; i
++)
3878 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
3879 completion_mode
[i
], EXPECT (expected
));
3880 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
3881 completion_mode
[i
], EXPECT (expected
));
3883 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
3884 completion_mode
[i
], EXPECT (expected
));
3885 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
3886 completion_mode
[i
], EXPECT (expected
));
3891 /* Check method qualifiers are ignored. */
3892 static const char expected
[] = "ns::foo<char*>";
3893 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3894 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3895 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3896 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3897 CHECK_MATCH ("foo < char * > ( int ) const",
3898 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3899 CHECK_MATCH ("foo < char * > ( int ) &&",
3900 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3903 /* Test lookup names that don't match anything. */
3905 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
3908 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
3912 /* Some wild matching tests, exercising "(anonymous namespace)",
3913 which should not be confused with a parameter list. */
3915 static const char *syms
[] = {
3919 "A :: B :: C ( int )",
3924 for (const char *s
: syms
)
3926 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
3927 EXPECT ("(anonymous namespace)::A::B::C"));
3932 static const char expected
[] = "ns2::tmpl<int>::foo2";
3933 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
3935 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
3939 SELF_CHECK (!any_mismatch
);
3948 test_mapped_index_find_name_component_bounds ();
3949 test_dw2_expand_symtabs_matching_symbol ();
3952 }} // namespace selftests::dw2_expand_symtabs_matching
3954 #endif /* GDB_SELF_TEST */
3956 /* If FILE_MATCHER is NULL or if PER_CU has
3957 dwarf2_per_cu_quick_data::MARK set (see
3958 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3959 EXPANSION_NOTIFY on it. */
3962 dw2_expand_symtabs_matching_one
3963 (dwarf2_per_cu_data
*per_cu
,
3964 dwarf2_per_objfile
*per_objfile
,
3965 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3966 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
3968 if (file_matcher
== NULL
|| per_cu
->mark
)
3970 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
3972 compunit_symtab
*symtab
3973 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
3974 gdb_assert (symtab
!= nullptr);
3976 if (expansion_notify
!= NULL
&& symtab_was_null
)
3977 return expansion_notify (symtab
);
3982 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3983 matched, to expand corresponding CUs that were marked. IDX is the
3984 index of the symbol name that matched. */
3987 dw2_expand_marked_cus
3988 (dwarf2_per_objfile
*per_objfile
, offset_type idx
,
3989 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3990 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
3991 block_search_flags search_flags
,
3994 offset_type vec_len
, vec_idx
;
3995 bool global_seen
= false;
3997 = *(gdb::checked_static_cast
<mapped_index
*>
3998 (per_objfile
->per_bfd
->index_table
.get ()));
4000 offset_view
vec (index
.constant_pool
.slice (index
.symbol_vec_index (idx
)));
4002 for (vec_idx
= 0; vec_idx
< vec_len
; ++vec_idx
)
4004 offset_type cu_index_and_attrs
= vec
[vec_idx
+ 1];
4005 /* This value is only valid for index versions >= 7. */
4006 int is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
4007 gdb_index_symbol_kind symbol_kind
=
4008 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
4009 int cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
4010 /* Only check the symbol attributes if they're present.
4011 Indices prior to version 7 don't record them,
4012 and indices >= 7 may elide them for certain symbols
4013 (gold does this). */
4016 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
4018 /* Work around gold/15646. */
4021 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
4029 /* Only check the symbol's kind if it has one. */
4034 if ((search_flags
& SEARCH_STATIC_BLOCK
) == 0)
4039 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
4045 case VARIABLES_DOMAIN
:
4046 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
)
4049 case FUNCTIONS_DOMAIN
:
4050 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
)
4054 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
4057 case MODULES_DOMAIN
:
4058 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
4066 /* Don't crash on bad data. */
4067 if (cu_index
>= per_objfile
->per_bfd
->all_units
.size ())
4069 complaint (_(".gdb_index entry has bad CU index"
4070 " [in module %s]"), objfile_name (per_objfile
->objfile
));
4074 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
4075 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, file_matcher
,
4083 /* If FILE_MATCHER is non-NULL, set all the
4084 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4085 that match FILE_MATCHER. */
4088 dw_expand_symtabs_matching_file_matcher
4089 (dwarf2_per_objfile
*per_objfile
,
4090 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
4092 if (file_matcher
== NULL
)
4095 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
4097 NULL
, xcalloc
, xfree
));
4098 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
4100 NULL
, xcalloc
, xfree
));
4102 /* The rule is CUs specify all the files, including those used by
4103 any TU, so there's no need to scan TUs here. */
4105 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
4109 if (per_cu
->is_debug_types
)
4113 /* We only need to look at symtabs not already expanded. */
4114 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4117 if (per_cu
->fnd
!= nullptr)
4119 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4121 if (file_matcher (fnd
->get_name (), false))
4127 /* Before we invoke realpath, which can get expensive when many
4128 files are involved, do a quick comparison of the basenames. */
4129 if ((basenames_may_differ
4130 || file_matcher (lbasename (fnd
->get_name ()), true))
4131 && file_matcher (fnd
->get_fullname (), false))
4138 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
4140 if (file_data
== NULL
)
4143 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
4145 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
4151 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4153 const char *this_real_name
;
4155 if (file_matcher (file_data
->file_names
[j
], false))
4161 /* Before we invoke realpath, which can get expensive when many
4162 files are involved, do a quick comparison of the basenames. */
4163 if (!basenames_may_differ
4164 && !file_matcher (lbasename (file_data
->file_names
[j
]),
4168 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
4169 if (file_matcher (this_real_name
, false))
4176 void **slot
= htab_find_slot (per_cu
->mark
4177 ? visited_found
.get ()
4178 : visited_not_found
.get (),
4185 dwarf2_gdb_index::expand_symtabs_matching
4186 (struct objfile
*objfile
,
4187 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
4188 const lookup_name_info
*lookup_name
,
4189 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
4190 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
4191 block_search_flags search_flags
,
4193 enum search_domain kind
)
4195 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4197 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
4199 /* This invariant is documented in quick-functions.h. */
4200 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
4201 if (lookup_name
== nullptr)
4203 for (dwarf2_per_cu_data
*per_cu
4204 : all_units_range (per_objfile
->per_bfd
))
4208 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
4217 = *(gdb::checked_static_cast
<mapped_index
*>
4218 (per_objfile
->per_bfd
->index_table
.get ()));
4221 = dw2_expand_symtabs_matching_symbol (index
, *lookup_name
,
4223 [&] (offset_type idx
)
4225 if (!dw2_expand_marked_cus (per_objfile
, idx
, file_matcher
,
4226 expansion_notify
, search_flags
, kind
))
4234 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4237 static struct compunit_symtab
*
4238 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
4243 if (cust
->blockvector () != nullptr
4244 && blockvector_contains_pc (cust
->blockvector (), pc
))
4247 if (cust
->includes
== NULL
)
4250 for (i
= 0; cust
->includes
[i
]; ++i
)
4252 struct compunit_symtab
*s
= cust
->includes
[i
];
4254 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
4262 dwarf2_per_cu_data
*
4263 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
4264 CORE_ADDR adjusted_pc
)
4266 if (per_bfd
->index_addrmap
== nullptr)
4269 void *obj
= per_bfd
->index_addrmap
->find (adjusted_pc
);
4270 return static_cast<dwarf2_per_cu_data
*> (obj
);
4273 struct compunit_symtab
*
4274 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4275 (struct objfile
*objfile
,
4276 struct bound_minimal_symbol msymbol
,
4278 struct obj_section
*section
,
4281 struct compunit_symtab
*result
;
4283 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4285 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
4286 struct dwarf2_per_cu_data
*data
= find_per_cu (per_objfile
->per_bfd
,
4288 if (data
== nullptr)
4291 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
4292 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4293 paddress (objfile
->arch (), pc
));
4295 result
= recursively_find_pc_sect_compunit_symtab
4296 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
4298 if (warn_if_readin
&& result
== nullptr)
4299 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4300 paddress (objfile
->arch (), pc
));
4306 dwarf2_base_index_functions::map_symbol_filenames
4307 (struct objfile
*objfile
,
4308 gdb::function_view
<symbol_filename_ftype
> fun
,
4311 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4313 /* Use caches to ensure we only call FUN once for each filename. */
4314 filename_seen_cache filenames_cache
;
4315 std::unordered_set
<quick_file_names
*> qfn_cache
;
4317 /* The rule is CUs specify all the files, including those used by any TU,
4318 so there's no need to scan TUs here. We can ignore file names coming
4319 from already-expanded CUs. It is possible that an expanded CU might
4320 reuse the file names data from a currently unexpanded CU, in this
4321 case we don't want to report the files from the unexpanded CU. */
4323 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
4325 if (!per_cu
->is_debug_types
4326 && per_objfile
->symtab_set_p (per_cu
.get ()))
4328 if (per_cu
->file_names
!= nullptr)
4329 qfn_cache
.insert (per_cu
->file_names
);
4333 for (dwarf2_per_cu_data
*per_cu
4334 : all_units_range (per_objfile
->per_bfd
))
4336 /* We only need to look at symtabs not already expanded. */
4337 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
4340 if (per_cu
->fnd
!= nullptr)
4342 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4344 const char *filename
= fnd
->get_name ();
4345 const char *key
= filename
;
4346 const char *fullname
= nullptr;
4350 fullname
= fnd
->get_fullname ();
4354 if (!filenames_cache
.seen (key
))
4355 fun (filename
, fullname
);
4358 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
4359 if (file_data
== nullptr
4360 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
4363 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4365 const char *filename
= file_data
->file_names
[j
];
4366 const char *key
= filename
;
4367 const char *fullname
= nullptr;
4371 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
4375 if (!filenames_cache
.seen (key
))
4376 fun (filename
, fullname
);
4382 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
4387 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4390 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
4392 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4394 for (const auto &per_cu
: per_objfile
->per_bfd
->all_units
)
4396 /* Is this already expanded? */
4397 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4400 /* It has not yet been expanded. */
4407 /* DWARF-5 debug_names reader. */
4409 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4410 static const gdb_byte dwarf5_augmentation
[] = { 'G', 'D', 'B', 0 };
4412 /* A helper function that reads the .debug_names section in SECTION
4413 and fills in MAP. FILENAME is the name of the file containing the
4414 section; it is used for error reporting.
4416 Returns true if all went well, false otherwise. */
4419 read_debug_names_from_section (struct objfile
*objfile
,
4420 const char *filename
,
4421 struct dwarf2_section_info
*section
,
4422 mapped_debug_names
&map
)
4424 if (section
->empty ())
4427 /* Older elfutils strip versions could keep the section in the main
4428 executable while splitting it for the separate debug info file. */
4429 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
4432 section
->read (objfile
);
4434 map
.dwarf5_byte_order
= gdbarch_byte_order (objfile
->arch ());
4436 const gdb_byte
*addr
= section
->buffer
;
4438 bfd
*const abfd
= section
->get_bfd_owner ();
4440 unsigned int bytes_read
;
4441 LONGEST length
= read_initial_length (abfd
, addr
, &bytes_read
);
4444 map
.dwarf5_is_dwarf64
= bytes_read
!= 4;
4445 map
.offset_size
= map
.dwarf5_is_dwarf64
? 8 : 4;
4446 if (bytes_read
+ length
!= section
->size
)
4448 /* There may be multiple per-CU indices. */
4449 warning (_("Section .debug_names in %s length %s does not match "
4450 "section length %s, ignoring .debug_names."),
4451 filename
, plongest (bytes_read
+ length
),
4452 pulongest (section
->size
));
4456 /* The version number. */
4457 uint16_t version
= read_2_bytes (abfd
, addr
);
4461 warning (_("Section .debug_names in %s has unsupported version %d, "
4462 "ignoring .debug_names."),
4468 uint16_t padding
= read_2_bytes (abfd
, addr
);
4472 warning (_("Section .debug_names in %s has unsupported padding %d, "
4473 "ignoring .debug_names."),
4478 /* comp_unit_count - The number of CUs in the CU list. */
4479 map
.cu_count
= read_4_bytes (abfd
, addr
);
4482 /* local_type_unit_count - The number of TUs in the local TU
4484 map
.tu_count
= read_4_bytes (abfd
, addr
);
4487 /* foreign_type_unit_count - The number of TUs in the foreign TU
4489 uint32_t foreign_tu_count
= read_4_bytes (abfd
, addr
);
4491 if (foreign_tu_count
!= 0)
4493 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4494 "ignoring .debug_names."),
4495 filename
, static_cast<unsigned long> (foreign_tu_count
));
4499 /* bucket_count - The number of hash buckets in the hash lookup
4501 map
.bucket_count
= read_4_bytes (abfd
, addr
);
4504 /* name_count - The number of unique names in the index. */
4505 map
.name_count
= read_4_bytes (abfd
, addr
);
4508 /* abbrev_table_size - The size in bytes of the abbreviations
4510 uint32_t abbrev_table_size
= read_4_bytes (abfd
, addr
);
4513 /* augmentation_string_size - The size in bytes of the augmentation
4514 string. This value is rounded up to a multiple of 4. */
4515 uint32_t augmentation_string_size
= read_4_bytes (abfd
, addr
);
4517 map
.augmentation_is_gdb
= ((augmentation_string_size
4518 == sizeof (dwarf5_augmentation
))
4519 && memcmp (addr
, dwarf5_augmentation
,
4520 sizeof (dwarf5_augmentation
)) == 0);
4521 augmentation_string_size
+= (-augmentation_string_size
) & 3;
4522 addr
+= augmentation_string_size
;
4525 map
.cu_table_reordered
= addr
;
4526 addr
+= map
.cu_count
* map
.offset_size
;
4528 /* List of Local TUs */
4529 map
.tu_table_reordered
= addr
;
4530 addr
+= map
.tu_count
* map
.offset_size
;
4532 /* Hash Lookup Table */
4533 map
.bucket_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4534 addr
+= map
.bucket_count
* 4;
4535 map
.hash_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4536 addr
+= map
.name_count
* 4;
4539 map
.name_table_string_offs_reordered
= addr
;
4540 addr
+= map
.name_count
* map
.offset_size
;
4541 map
.name_table_entry_offs_reordered
= addr
;
4542 addr
+= map
.name_count
* map
.offset_size
;
4544 const gdb_byte
*abbrev_table_start
= addr
;
4547 const ULONGEST index_num
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4552 const auto insertpair
4553 = map
.abbrev_map
.emplace (index_num
, mapped_debug_names::index_val ());
4554 if (!insertpair
.second
)
4556 warning (_("Section .debug_names in %s has duplicate index %s, "
4557 "ignoring .debug_names."),
4558 filename
, pulongest (index_num
));
4561 mapped_debug_names::index_val
&indexval
= insertpair
.first
->second
;
4562 indexval
.dwarf_tag
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4567 mapped_debug_names::index_val::attr attr
;
4568 attr
.dw_idx
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4570 attr
.form
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4572 if (attr
.form
== DW_FORM_implicit_const
)
4574 attr
.implicit_const
= read_signed_leb128 (abfd
, addr
,
4578 if (attr
.dw_idx
== 0 && attr
.form
== 0)
4580 indexval
.attr_vec
.push_back (std::move (attr
));
4583 if (addr
!= abbrev_table_start
+ abbrev_table_size
)
4585 warning (_("Section .debug_names in %s has abbreviation_table "
4586 "of size %s vs. written as %u, ignoring .debug_names."),
4587 filename
, plongest (addr
- abbrev_table_start
),
4591 map
.entry_pool
= addr
;
4596 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4600 create_cus_from_debug_names_list (dwarf2_per_bfd
*per_bfd
,
4601 const mapped_debug_names
&map
,
4602 dwarf2_section_info
§ion
,
4605 if (!map
.augmentation_is_gdb
)
4607 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
4609 sect_offset sect_off
4610 = (sect_offset
) (extract_unsigned_integer
4611 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4613 map
.dwarf5_byte_order
));
4614 /* We don't know the length of the CU, because the CU list in a
4615 .debug_names index can be incomplete, so we can't use the start
4616 of the next CU as end of this CU. We create the CUs here with
4617 length 0, and in cutu_reader::cutu_reader we'll fill in the
4619 dwarf2_per_cu_data_up per_cu
4620 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4622 per_bfd
->all_units
.push_back (std::move (per_cu
));
4627 sect_offset sect_off_prev
;
4628 for (uint32_t i
= 0; i
<= map
.cu_count
; ++i
)
4630 sect_offset sect_off_next
;
4631 if (i
< map
.cu_count
)
4634 = (sect_offset
) (extract_unsigned_integer
4635 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4637 map
.dwarf5_byte_order
));
4640 sect_off_next
= (sect_offset
) section
.size
;
4643 if (sect_off_next
== sect_off_prev
)
4645 warning (_("Section .debug_names has duplicate entry in CU table,"
4646 " ignoring .debug_names."));
4649 if (sect_off_next
< sect_off_prev
)
4651 warning (_("Section .debug_names has non-ascending CU table,"
4652 " ignoring .debug_names."));
4655 /* Note: we're not using length = sect_off_next - sect_off_prev,
4656 to gracefully handle an incomplete CU list. */
4657 const ULONGEST length
= 0;
4658 dwarf2_per_cu_data_up per_cu
4659 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4660 sect_off_prev
, length
);
4661 per_bfd
->all_units
.push_back (std::move (per_cu
));
4663 sect_off_prev
= sect_off_next
;
4669 /* Read the CU list from the mapped index, and use it to create all
4670 the CU objects for this dwarf2_per_objfile. */
4673 create_cus_from_debug_names (dwarf2_per_bfd
*per_bfd
,
4674 const mapped_debug_names
&map
,
4675 const mapped_debug_names
&dwz_map
)
4677 gdb_assert (per_bfd
->all_units
.empty ());
4678 per_bfd
->all_units
.reserve (map
.cu_count
+ dwz_map
.cu_count
);
4680 if (!create_cus_from_debug_names_list (per_bfd
, map
, per_bfd
->info
,
4681 false /* is_dwz */))
4684 if (dwz_map
.cu_count
== 0)
4687 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4688 return create_cus_from_debug_names_list (per_bfd
, dwz_map
, dwz
->info
,
4692 /* Read .debug_names. If everything went ok, initialize the "quick"
4693 elements of all the CUs and return true. Otherwise, return false. */
4696 dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
4698 std::unique_ptr
<mapped_debug_names
> map (new mapped_debug_names
);
4699 mapped_debug_names dwz_map
;
4700 struct objfile
*objfile
= per_objfile
->objfile
;
4701 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4703 if (!read_debug_names_from_section (objfile
, objfile_name (objfile
),
4704 &per_bfd
->debug_names
, *map
))
4707 /* Don't use the index if it's empty. */
4708 if (map
->name_count
== 0)
4711 /* If there is a .dwz file, read it so we can get its CU list as
4713 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4716 if (!read_debug_names_from_section (objfile
,
4717 bfd_get_filename (dwz
->dwz_bfd
.get ()),
4718 &dwz
->debug_names
, dwz_map
))
4720 warning (_("could not read '.debug_names' section from %s; skipping"),
4721 bfd_get_filename (dwz
->dwz_bfd
.get ()));
4726 if (!create_cus_from_debug_names (per_bfd
, *map
, dwz_map
))
4728 per_bfd
->all_units
.clear ();
4732 if (map
->tu_count
!= 0)
4734 /* We can only handle a single .debug_types when we have an
4736 if (per_bfd
->types
.size () > 1)
4738 per_bfd
->all_units
.clear ();
4742 dwarf2_section_info
*section
4743 = (per_bfd
->types
.size () == 1
4744 ? &per_bfd
->types
[0]
4747 create_signatured_type_table_from_debug_names
4748 (per_objfile
, *map
, section
, &per_bfd
->abbrev
);
4751 finalize_all_units (per_bfd
);
4753 create_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
);
4755 per_bfd
->index_table
= std::move (map
);
4756 per_bfd
->quick_file_names_table
=
4757 create_quick_file_names_table (per_bfd
->all_units
.size ());
4762 /* Type used to manage iterating over all CUs looking for a symbol for
4765 class dw2_debug_names_iterator
4768 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4769 block_search_flags block_index
,
4771 const char *name
, dwarf2_per_objfile
*per_objfile
)
4772 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4773 m_addr (find_vec_in_debug_names (map
, name
, per_objfile
)),
4774 m_per_objfile (per_objfile
)
4777 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4778 search_domain search
, uint32_t namei
,
4779 dwarf2_per_objfile
*per_objfile
,
4780 domain_enum domain
= UNDEF_DOMAIN
)
4784 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4785 m_per_objfile (per_objfile
)
4788 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4789 block_search_flags block_index
, domain_enum domain
,
4790 uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4791 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4792 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4793 m_per_objfile (per_objfile
)
4796 /* Return the next matching CU or NULL if there are no more. */
4797 dwarf2_per_cu_data
*next ();
4800 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4802 dwarf2_per_objfile
*per_objfile
);
4803 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4805 dwarf2_per_objfile
*per_objfile
);
4807 /* The internalized form of .debug_names. */
4808 const mapped_debug_names
&m_map
;
4810 /* Restrict the search to these blocks. */
4811 block_search_flags m_block_index
= (SEARCH_GLOBAL_BLOCK
4812 | SEARCH_STATIC_BLOCK
);
4814 /* The kind of symbol we're looking for. */
4815 const domain_enum m_domain
= UNDEF_DOMAIN
;
4816 const search_domain m_search
= ALL_DOMAIN
;
4818 /* The list of CUs from the index entry of the symbol, or NULL if
4820 const gdb_byte
*m_addr
;
4822 dwarf2_per_objfile
*m_per_objfile
;
4826 mapped_debug_names::namei_to_name
4827 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const
4829 const ULONGEST namei_string_offs
4830 = extract_unsigned_integer ((name_table_string_offs_reordered
4831 + namei
* offset_size
),
4834 return read_indirect_string_at_offset (per_objfile
, namei_string_offs
);
4837 /* Find a slot in .debug_names for the object named NAME. If NAME is
4838 found, return pointer to its pool data. If NAME cannot be found,
4842 dw2_debug_names_iterator::find_vec_in_debug_names
4843 (const mapped_debug_names
&map
, const char *name
,
4844 dwarf2_per_objfile
*per_objfile
)
4846 int (*cmp
) (const char *, const char *);
4848 gdb::unique_xmalloc_ptr
<char> without_params
;
4849 if (current_language
->la_language
== language_cplus
4850 || current_language
->la_language
== language_fortran
4851 || current_language
->la_language
== language_d
)
4853 /* NAME is already canonical. Drop any qualifiers as
4854 .debug_names does not contain any. */
4856 if (strchr (name
, '(') != NULL
)
4858 without_params
= cp_remove_params (name
);
4859 if (without_params
!= NULL
)
4860 name
= without_params
.get ();
4864 cmp
= (case_sensitivity
== case_sensitive_on
? strcmp
: strcasecmp
);
4866 const uint32_t full_hash
= dwarf5_djb_hash (name
);
4868 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4869 (map
.bucket_table_reordered
4870 + (full_hash
% map
.bucket_count
)), 4,
4871 map
.dwarf5_byte_order
);
4875 if (namei
>= map
.name_count
)
4877 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4879 namei
, map
.name_count
,
4880 objfile_name (per_objfile
->objfile
));
4886 const uint32_t namei_full_hash
4887 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4888 (map
.hash_table_reordered
+ namei
), 4,
4889 map
.dwarf5_byte_order
);
4890 if (full_hash
% map
.bucket_count
!= namei_full_hash
% map
.bucket_count
)
4893 if (full_hash
== namei_full_hash
)
4895 const char *const namei_string
= map
.namei_to_name (namei
, per_objfile
);
4897 #if 0 /* An expensive sanity check. */
4898 if (namei_full_hash
!= dwarf5_djb_hash (namei_string
))
4900 complaint (_("Wrong .debug_names hash for string at index %u "
4902 namei
, objfile_name (dwarf2_per_objfile
->objfile
));
4907 if (cmp (namei_string
, name
) == 0)
4909 const ULONGEST namei_entry_offs
4910 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4911 + namei
* map
.offset_size
),
4912 map
.offset_size
, map
.dwarf5_byte_order
);
4913 return map
.entry_pool
+ namei_entry_offs
;
4918 if (namei
>= map
.name_count
)
4924 dw2_debug_names_iterator::find_vec_in_debug_names
4925 (const mapped_debug_names
&map
, uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4927 if (namei
>= map
.name_count
)
4929 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4931 namei
, map
.name_count
,
4932 objfile_name (per_objfile
->objfile
));
4936 const ULONGEST namei_entry_offs
4937 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4938 + namei
* map
.offset_size
),
4939 map
.offset_size
, map
.dwarf5_byte_order
);
4940 return map
.entry_pool
+ namei_entry_offs
;
4943 /* See dw2_debug_names_iterator. */
4945 dwarf2_per_cu_data
*
4946 dw2_debug_names_iterator::next ()
4951 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4952 struct objfile
*objfile
= m_per_objfile
->objfile
;
4953 bfd
*const abfd
= objfile
->obfd
.get ();
4957 unsigned int bytes_read
;
4958 const ULONGEST abbrev
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4959 m_addr
+= bytes_read
;
4963 const auto indexval_it
= m_map
.abbrev_map
.find (abbrev
);
4964 if (indexval_it
== m_map
.abbrev_map
.cend ())
4966 complaint (_("Wrong .debug_names undefined abbrev code %s "
4968 pulongest (abbrev
), objfile_name (objfile
));
4971 const mapped_debug_names::index_val
&indexval
= indexval_it
->second
;
4972 enum class symbol_linkage
{
4976 } symbol_linkage_
= symbol_linkage::unknown
;
4977 dwarf2_per_cu_data
*per_cu
= NULL
;
4978 for (const mapped_debug_names::index_val::attr
&attr
: indexval
.attr_vec
)
4983 case DW_FORM_implicit_const
:
4984 ull
= attr
.implicit_const
;
4986 case DW_FORM_flag_present
:
4990 ull
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4991 m_addr
+= bytes_read
;
4994 ull
= read_4_bytes (abfd
, m_addr
);
4998 ull
= read_8_bytes (abfd
, m_addr
);
5001 case DW_FORM_ref_sig8
:
5002 ull
= read_8_bytes (abfd
, m_addr
);
5006 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5007 dwarf_form_name (attr
.form
),
5008 objfile_name (objfile
));
5011 switch (attr
.dw_idx
)
5013 case DW_IDX_compile_unit
:
5015 /* Don't crash on bad data. */
5016 if (ull
>= per_bfd
->all_comp_units
.size ())
5018 complaint (_(".debug_names entry has bad CU index %s"
5021 objfile_name (objfile
));
5025 per_cu
= per_bfd
->get_cu (ull
);
5027 case DW_IDX_type_unit
:
5028 /* Don't crash on bad data. */
5029 if (ull
>= per_bfd
->all_type_units
.size ())
5031 complaint (_(".debug_names entry has bad TU index %s"
5034 objfile_name (objfile
));
5038 int nr_cus
= per_bfd
->all_comp_units
.size ();
5039 per_cu
= per_bfd
->get_cu (nr_cus
+ ull
);
5042 case DW_IDX_die_offset
:
5043 /* In a per-CU index (as opposed to a per-module index), index
5044 entries without CU attribute implicitly refer to the single CU. */
5046 per_cu
= per_bfd
->get_cu (0);
5048 case DW_IDX_GNU_internal
:
5049 if (!m_map
.augmentation_is_gdb
)
5051 symbol_linkage_
= symbol_linkage::static_
;
5053 case DW_IDX_GNU_external
:
5054 if (!m_map
.augmentation_is_gdb
)
5056 symbol_linkage_
= symbol_linkage::extern_
;
5061 /* Skip if we couldn't find a valid CU/TU index. */
5062 if (per_cu
== nullptr)
5065 /* Skip if already read in. */
5066 if (m_per_objfile
->symtab_set_p (per_cu
))
5069 /* Check static vs global. */
5070 if (symbol_linkage_
!= symbol_linkage::unknown
)
5072 if (symbol_linkage_
== symbol_linkage::static_
)
5074 if ((m_block_index
& SEARCH_STATIC_BLOCK
) == 0)
5079 if ((m_block_index
& SEARCH_GLOBAL_BLOCK
) == 0)
5084 /* Match dw2_symtab_iter_next, symbol_kind
5085 and debug_names::psymbol_tag. */
5089 switch (indexval
.dwarf_tag
)
5091 case DW_TAG_variable
:
5092 case DW_TAG_subprogram
:
5093 /* Some types are also in VAR_DOMAIN. */
5094 case DW_TAG_typedef
:
5095 case DW_TAG_structure_type
:
5102 switch (indexval
.dwarf_tag
)
5104 case DW_TAG_typedef
:
5105 case DW_TAG_structure_type
:
5112 switch (indexval
.dwarf_tag
)
5115 case DW_TAG_variable
:
5122 switch (indexval
.dwarf_tag
)
5134 /* Match dw2_expand_symtabs_matching, symbol_kind and
5135 debug_names::psymbol_tag. */
5138 case VARIABLES_DOMAIN
:
5139 switch (indexval
.dwarf_tag
)
5141 case DW_TAG_variable
:
5147 case FUNCTIONS_DOMAIN
:
5148 switch (indexval
.dwarf_tag
)
5150 case DW_TAG_subprogram
:
5157 switch (indexval
.dwarf_tag
)
5159 case DW_TAG_typedef
:
5160 case DW_TAG_structure_type
:
5166 case MODULES_DOMAIN
:
5167 switch (indexval
.dwarf_tag
)
5181 /* This dumps minimal information about .debug_names. It is called
5182 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5183 uses this to verify that .debug_names has been loaded. */
5186 dwarf2_debug_names_index::dump (struct objfile
*objfile
)
5188 gdb_printf (".debug_names: exists\n");
5192 dwarf2_debug_names_index::expand_matching_symbols
5193 (struct objfile
*objfile
,
5194 const lookup_name_info
&name
, domain_enum domain
,
5196 symbol_compare_ftype
*ordered_compare
)
5198 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5200 mapped_debug_names
&map
5201 = *(gdb::checked_static_cast
<mapped_debug_names
*>
5202 (per_objfile
->per_bfd
->index_table
.get ()));
5203 const block_search_flags block_flags
5204 = global
? SEARCH_GLOBAL_BLOCK
: SEARCH_STATIC_BLOCK
;
5206 const char *match_name
= name
.ada ().lookup_name ().c_str ();
5207 auto matcher
= [&] (const char *symname
)
5209 if (ordered_compare
== nullptr)
5211 return ordered_compare (symname
, match_name
) == 0;
5214 dw2_expand_symtabs_matching_symbol (map
, name
, matcher
,
5215 [&] (offset_type namei
)
5217 /* The name was matched, now expand corresponding CUs that were
5219 dw2_debug_names_iterator
iter (map
, block_flags
, domain
, namei
,
5222 struct dwarf2_per_cu_data
*per_cu
;
5223 while ((per_cu
= iter
.next ()) != NULL
)
5224 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
5231 dwarf2_debug_names_index::expand_symtabs_matching
5232 (struct objfile
*objfile
,
5233 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
5234 const lookup_name_info
*lookup_name
,
5235 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
5236 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
5237 block_search_flags search_flags
,
5239 enum search_domain kind
)
5241 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5243 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
5245 /* This invariant is documented in quick-functions.h. */
5246 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
5247 if (lookup_name
== nullptr)
5249 for (dwarf2_per_cu_data
*per_cu
5250 : all_units_range (per_objfile
->per_bfd
))
5254 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5262 mapped_debug_names
&map
5263 = *(gdb::checked_static_cast
<mapped_debug_names
*>
5264 (per_objfile
->per_bfd
->index_table
.get ()));
5267 = dw2_expand_symtabs_matching_symbol (map
, *lookup_name
,
5269 [&] (offset_type namei
)
5271 /* The name was matched, now expand corresponding CUs that were
5273 dw2_debug_names_iterator
iter (map
, kind
, namei
, per_objfile
, domain
);
5275 struct dwarf2_per_cu_data
*per_cu
;
5276 while ((per_cu
= iter
.next ()) != NULL
)
5277 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5287 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5288 to either a dwarf2_per_bfd or dwz_file object. */
5290 template <typename T
>
5291 static gdb::array_view
<const gdb_byte
>
5292 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
5294 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
5296 if (section
->empty ())
5299 /* Older elfutils strip versions could keep the section in the main
5300 executable while splitting it for the separate debug info file. */
5301 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
5304 section
->read (obj
);
5306 /* dwarf2_section_info::size is a bfd_size_type, while
5307 gdb::array_view works with size_t. On 32-bit hosts, with
5308 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5309 is 32-bit. So we need an explicit narrowing conversion here.
5310 This is fine, because it's impossible to allocate or mmap an
5311 array/buffer larger than what size_t can represent. */
5312 return gdb::make_array_view (section
->buffer
, section
->size
);
5315 /* Lookup the index cache for the contents of the index associated to
5318 static gdb::array_view
<const gdb_byte
>
5319 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
5321 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
.get ());
5322 if (build_id
== nullptr)
5325 return global_index_cache
.lookup_gdb_index (build_id
,
5326 &dwarf2_per_bfd
->index_cache_res
);
5329 /* Same as the above, but for DWZ. */
5331 static gdb::array_view
<const gdb_byte
>
5332 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
5334 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
5335 if (build_id
== nullptr)
5338 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
5341 static quick_symbol_functions_up
make_cooked_index_funcs ();
5343 /* See dwarf2/public.h. */
5346 dwarf2_initialize_objfile (struct objfile
*objfile
)
5348 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5349 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5351 dwarf_read_debug_printf ("called");
5353 /* If we're about to read full symbols, don't bother with the
5354 indices. In this case we also don't care if some other debug
5355 format is making psymtabs, because they are all about to be
5357 if ((objfile
->flags
& OBJF_READNOW
))
5359 dwarf_read_debug_printf ("readnow requested");
5361 create_all_units (per_objfile
);
5362 per_bfd
->quick_file_names_table
5363 = create_quick_file_names_table (per_bfd
->all_units
.size ());
5365 objfile
->qf
.emplace_front (new readnow_functions
);
5369 /* Was a GDB index already read when we processed an objfile sharing
5371 if (per_bfd
->index_table
!= nullptr)
5373 dwarf_read_debug_printf ("re-using symbols");
5374 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5378 if (dwarf2_read_debug_names (per_objfile
))
5380 dwarf_read_debug_printf ("found debug names");
5381 objfile
->qf
.push_front
5382 (per_bfd
->index_table
->make_quick_functions ());
5386 if (dwarf2_read_gdb_index (per_objfile
,
5387 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
5388 get_gdb_index_contents_from_section
<dwz_file
>))
5390 dwarf_read_debug_printf ("found gdb index from file");
5391 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5395 /* ... otherwise, try to find the index in the index cache. */
5396 if (dwarf2_read_gdb_index (per_objfile
,
5397 get_gdb_index_contents_from_cache
,
5398 get_gdb_index_contents_from_cache_dwz
))
5400 dwarf_read_debug_printf ("found gdb index from cache");
5401 global_index_cache
.hit ();
5402 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5406 global_index_cache
.miss ();
5407 objfile
->qf
.push_front (make_cooked_index_funcs ());
5412 /* Build a partial symbol table. */
5415 dwarf2_build_psymtabs (struct objfile
*objfile
)
5417 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5419 if (per_objfile
->per_bfd
->index_table
!= nullptr)
5424 dwarf2_build_psymtabs_hard (per_objfile
);
5426 /* (maybe) store an index in the cache. */
5427 global_index_cache
.store (per_objfile
);
5429 catch (const gdb_exception_error
&except
)
5431 exception_print (gdb_stderr
, except
);
5435 /* Find the base address of the compilation unit for range lists and
5436 location lists. It will normally be specified by DW_AT_low_pc.
5437 In DWARF-3 draft 4, the base address could be overridden by
5438 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5439 compilation units with discontinuous ranges. */
5442 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
5444 struct attribute
*attr
;
5446 cu
->base_address
.reset ();
5448 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
5449 if (attr
!= nullptr)
5450 cu
->base_address
= attr
->as_address ();
5453 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
5454 if (attr
!= nullptr)
5455 cu
->base_address
= attr
->as_address ();
5459 /* Helper function that returns the proper abbrev section for
5462 static struct dwarf2_section_info
*
5463 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
5465 struct dwarf2_section_info
*abbrev
;
5466 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
5468 if (this_cu
->is_dwz
)
5469 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
5471 abbrev
= &per_bfd
->abbrev
;
5476 /* Fetch the abbreviation table offset from a comp or type unit header. */
5479 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
5480 struct dwarf2_section_info
*section
,
5481 sect_offset sect_off
)
5483 bfd
*abfd
= section
->get_bfd_owner ();
5484 const gdb_byte
*info_ptr
;
5485 unsigned int initial_length_size
, offset_size
;
5488 section
->read (per_objfile
->objfile
);
5489 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
5490 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
5491 offset_size
= initial_length_size
== 4 ? 4 : 8;
5492 info_ptr
+= initial_length_size
;
5494 version
= read_2_bytes (abfd
, info_ptr
);
5498 /* Skip unit type and address size. */
5502 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
5506 hash_signatured_type (const void *item
)
5508 const struct signatured_type
*sig_type
5509 = (const struct signatured_type
*) item
;
5511 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5512 return sig_type
->signature
;
5516 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
5518 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
5519 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
5521 return lhs
->signature
== rhs
->signature
;
5524 /* Allocate a hash table for signatured types. */
5527 allocate_signatured_type_table ()
5529 return htab_up (htab_create_alloc (41,
5530 hash_signatured_type
,
5532 NULL
, xcalloc
, xfree
));
5535 /* A helper for create_debug_types_hash_table. Read types from SECTION
5536 and fill them into TYPES_HTAB. It will process only type units,
5537 therefore DW_UT_type. */
5540 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
5541 struct dwo_file
*dwo_file
,
5542 dwarf2_section_info
*section
, htab_up
&types_htab
,
5543 rcuh_kind section_kind
)
5545 struct objfile
*objfile
= per_objfile
->objfile
;
5546 struct dwarf2_section_info
*abbrev_section
;
5548 const gdb_byte
*info_ptr
, *end_ptr
;
5550 abbrev_section
= &dwo_file
->sections
.abbrev
;
5552 dwarf_read_debug_printf ("Reading %s for %s",
5553 section
->get_name (),
5554 abbrev_section
->get_file_name ());
5556 section
->read (objfile
);
5557 info_ptr
= section
->buffer
;
5559 if (info_ptr
== NULL
)
5562 /* We can't set abfd until now because the section may be empty or
5563 not present, in which case the bfd is unknown. */
5564 abfd
= section
->get_bfd_owner ();
5566 /* We don't use cutu_reader here because we don't need to read
5567 any dies: the signature is in the header. */
5569 end_ptr
= info_ptr
+ section
->size
;
5570 while (info_ptr
< end_ptr
)
5572 signatured_type_up sig_type
;
5573 struct dwo_unit
*dwo_tu
;
5575 const gdb_byte
*ptr
= info_ptr
;
5576 struct comp_unit_head header
;
5577 unsigned int length
;
5579 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
5581 /* Initialize it due to a false compiler warning. */
5582 header
.signature
= -1;
5583 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
5585 /* We need to read the type's signature in order to build the hash
5586 table, but we don't need anything else just yet. */
5588 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
5589 abbrev_section
, ptr
, section_kind
);
5591 length
= header
.get_length_with_initial ();
5593 /* Skip dummy type units. */
5594 if (ptr
>= info_ptr
+ length
5595 || peek_abbrev_code (abfd
, ptr
) == 0
5596 || (header
.unit_type
!= DW_UT_type
5597 && header
.unit_type
!= DW_UT_split_type
))
5603 if (types_htab
== NULL
)
5604 types_htab
= allocate_dwo_unit_table ();
5606 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
5607 dwo_tu
->dwo_file
= dwo_file
;
5608 dwo_tu
->signature
= header
.signature
;
5609 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
5610 dwo_tu
->section
= section
;
5611 dwo_tu
->sect_off
= sect_off
;
5612 dwo_tu
->length
= length
;
5614 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
5615 gdb_assert (slot
!= NULL
);
5617 complaint (_("debug type entry at offset %s is duplicate to"
5618 " the entry at offset %s, signature %s"),
5619 sect_offset_str (sect_off
),
5620 sect_offset_str (dwo_tu
->sect_off
),
5621 hex_string (header
.signature
));
5624 dwarf_read_debug_printf_v (" offset %s, signature %s",
5625 sect_offset_str (sect_off
),
5626 hex_string (header
.signature
));
5632 /* Create the hash table of all entries in the .debug_types
5633 (or .debug_types.dwo) section(s).
5634 DWO_FILE is a pointer to the DWO file object.
5636 The result is a pointer to the hash table or NULL if there are no types.
5638 Note: This function processes DWO files only, not DWP files. */
5641 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
5642 struct dwo_file
*dwo_file
,
5643 gdb::array_view
<dwarf2_section_info
> type_sections
,
5644 htab_up
&types_htab
)
5646 for (dwarf2_section_info
§ion
: type_sections
)
5647 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
5651 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5652 If SLOT is non-NULL, it is the entry to use in the hash table.
5653 Otherwise we find one. */
5655 static struct signatured_type
*
5656 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
5658 if (per_objfile
->per_bfd
->all_units
.size ()
5659 == per_objfile
->per_bfd
->all_units
.capacity ())
5660 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
5662 signatured_type_up sig_type_holder
5663 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
5664 signatured_type
*sig_type
= sig_type_holder
.get ();
5666 per_objfile
->per_bfd
->all_units
.emplace_back
5667 (sig_type_holder
.release ());
5671 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5674 gdb_assert (*slot
== NULL
);
5676 /* The rest of sig_type must be filled in by the caller. */
5680 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5681 Fill in SIG_ENTRY with DWO_ENTRY. */
5684 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
5685 struct signatured_type
*sig_entry
,
5686 struct dwo_unit
*dwo_entry
)
5688 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5690 /* Make sure we're not clobbering something we don't expect to. */
5691 gdb_assert (! sig_entry
->queued
);
5692 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
5693 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
5694 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
5695 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
5696 || (to_underlying (sig_entry
->type_offset_in_section
)
5697 == to_underlying (dwo_entry
->type_offset_in_tu
)));
5698 gdb_assert (sig_entry
->type_unit_group
== NULL
);
5699 gdb_assert (sig_entry
->dwo_unit
== NULL
5700 || sig_entry
->dwo_unit
== dwo_entry
);
5702 sig_entry
->section
= dwo_entry
->section
;
5703 sig_entry
->sect_off
= dwo_entry
->sect_off
;
5704 sig_entry
->set_length (dwo_entry
->length
, false);
5705 sig_entry
->reading_dwo_directly
= 1;
5706 sig_entry
->per_bfd
= per_bfd
;
5707 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
5708 sig_entry
->dwo_unit
= dwo_entry
;
5711 /* Subroutine of lookup_signatured_type.
5712 If we haven't read the TU yet, create the signatured_type data structure
5713 for a TU to be read in directly from a DWO file, bypassing the stub.
5714 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5715 using .gdb_index, then when reading a CU we want to stay in the DWO file
5716 containing that CU. Otherwise we could end up reading several other DWO
5717 files (due to comdat folding) to process the transitive closure of all the
5718 mentioned TUs, and that can be slow. The current DWO file will have every
5719 type signature that it needs.
5720 We only do this for .gdb_index because in the psymtab case we already have
5721 to read all the DWOs to build the type unit groups. */
5723 static struct signatured_type
*
5724 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5726 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5727 struct dwo_file
*dwo_file
;
5728 struct dwo_unit find_dwo_entry
, *dwo_entry
;
5731 gdb_assert (cu
->dwo_unit
);
5733 /* If TU skeletons have been removed then we may not have read in any
5735 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5736 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5738 /* We only ever need to read in one copy of a signatured type.
5739 Use the global signatured_types array to do our own comdat-folding
5740 of types. If this is the first time we're reading this TU, and
5741 the TU has an entry in .gdb_index, replace the recorded data from
5742 .gdb_index with this TU. */
5744 signatured_type
find_sig_entry (sig
);
5745 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5746 &find_sig_entry
, INSERT
);
5747 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5749 /* We can get here with the TU already read, *or* in the process of being
5750 read. Don't reassign the global entry to point to this DWO if that's
5751 the case. Also note that if the TU is already being read, it may not
5752 have come from a DWO, the program may be a mix of Fission-compiled
5753 code and non-Fission-compiled code. */
5755 /* Have we already tried to read this TU?
5756 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5757 needn't exist in the global table yet). */
5758 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
5761 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5762 dwo_unit of the TU itself. */
5763 dwo_file
= cu
->dwo_unit
->dwo_file
;
5765 /* Ok, this is the first time we're reading this TU. */
5766 if (dwo_file
->tus
== NULL
)
5768 find_dwo_entry
.signature
= sig
;
5769 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
5771 if (dwo_entry
== NULL
)
5774 /* If the global table doesn't have an entry for this TU, add one. */
5775 if (sig_entry
== NULL
)
5776 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5778 if (sig_entry
->dwo_unit
== nullptr)
5779 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5780 sig_entry
->tu_read
= 1;
5784 /* Subroutine of lookup_signatured_type.
5785 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5786 then try the DWP file. If the TU stub (skeleton) has been removed then
5787 it won't be in .gdb_index. */
5789 static struct signatured_type
*
5790 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5792 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5793 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
5794 struct dwo_unit
*dwo_entry
;
5797 gdb_assert (cu
->dwo_unit
);
5798 gdb_assert (dwp_file
!= NULL
);
5800 /* If TU skeletons have been removed then we may not have read in any
5802 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5803 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5805 signatured_type
find_sig_entry (sig
);
5806 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5807 &find_sig_entry
, INSERT
);
5808 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5810 /* Have we already tried to read this TU?
5811 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5812 needn't exist in the global table yet). */
5813 if (sig_entry
!= NULL
)
5816 if (dwp_file
->tus
== NULL
)
5818 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
5819 1 /* is_debug_types */);
5820 if (dwo_entry
== NULL
)
5823 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5824 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5829 /* Lookup a signature based type for DW_FORM_ref_sig8.
5830 Returns NULL if signature SIG is not present in the table.
5831 It is up to the caller to complain about this. */
5833 static struct signatured_type
*
5834 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5836 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5840 /* We're in a DWO/DWP file, and we're using .gdb_index.
5841 These cases require special processing. */
5842 if (get_dwp_file (per_objfile
) == NULL
)
5843 return lookup_dwo_signatured_type (cu
, sig
);
5845 return lookup_dwp_signatured_type (cu
, sig
);
5849 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5851 signatured_type
find_entry (sig
);
5852 return ((struct signatured_type
*)
5853 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
5858 /* Low level DIE reading support. */
5860 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5863 init_cu_die_reader (struct die_reader_specs
*reader
,
5864 struct dwarf2_cu
*cu
,
5865 struct dwarf2_section_info
*section
,
5866 struct dwo_file
*dwo_file
,
5867 struct abbrev_table
*abbrev_table
)
5869 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
5870 reader
->abfd
= section
->get_bfd_owner ();
5872 reader
->dwo_file
= dwo_file
;
5873 reader
->die_section
= section
;
5874 reader
->buffer
= section
->buffer
;
5875 reader
->buffer_end
= section
->buffer
+ section
->size
;
5876 reader
->abbrev_table
= abbrev_table
;
5879 /* Subroutine of cutu_reader to simplify it.
5880 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5881 There's just a lot of work to do, and cutu_reader is big enough
5884 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5885 from it to the DIE in the DWO. If NULL we are skipping the stub.
5886 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5887 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5888 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5889 STUB_COMP_DIR may be non-NULL.
5890 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5891 are filled in with the info of the DIE from the DWO file.
5892 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5893 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5894 kept around for at least as long as *RESULT_READER.
5896 The result is non-zero if a valid (non-dummy) DIE was found. */
5899 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
5900 struct dwo_unit
*dwo_unit
,
5901 struct die_info
*stub_comp_unit_die
,
5902 const char *stub_comp_dir
,
5903 struct die_reader_specs
*result_reader
,
5904 const gdb_byte
**result_info_ptr
,
5905 struct die_info
**result_comp_unit_die
,
5906 abbrev_table_up
*result_dwo_abbrev_table
)
5908 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5909 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
5910 struct objfile
*objfile
= per_objfile
->objfile
;
5912 const gdb_byte
*begin_info_ptr
, *info_ptr
;
5913 struct attribute
*comp_dir
, *stmt_list
, *low_pc
, *high_pc
, *ranges
;
5914 int i
,num_extra_attrs
;
5915 struct dwarf2_section_info
*dwo_abbrev_section
;
5916 struct die_info
*comp_unit_die
;
5918 /* At most one of these may be provided. */
5919 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
5921 /* These attributes aren't processed until later:
5922 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5923 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5924 referenced later. However, these attributes are found in the stub
5925 which we won't have later. In order to not impose this complication
5926 on the rest of the code, we read them here and copy them to the
5935 if (stub_comp_unit_die
!= NULL
)
5937 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5939 if (!per_cu
->is_debug_types
)
5940 stmt_list
= dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
);
5941 low_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
);
5942 high_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
);
5943 ranges
= dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
);
5944 comp_dir
= dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
);
5946 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
5948 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5949 We need the value before we can process DW_AT_ranges values from the
5951 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
5953 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5954 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5955 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5956 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5958 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
5960 else if (stub_comp_dir
!= NULL
)
5962 /* Reconstruct the comp_dir attribute to simplify the code below. */
5963 comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
, struct attribute
);
5964 comp_dir
->name
= DW_AT_comp_dir
;
5965 comp_dir
->form
= DW_FORM_string
;
5966 comp_dir
->set_string_noncanonical (stub_comp_dir
);
5969 /* Set up for reading the DWO CU/TU. */
5970 cu
->dwo_unit
= dwo_unit
;
5971 dwarf2_section_info
*section
= dwo_unit
->section
;
5972 section
->read (objfile
);
5973 abfd
= section
->get_bfd_owner ();
5974 begin_info_ptr
= info_ptr
= (section
->buffer
5975 + to_underlying (dwo_unit
->sect_off
));
5976 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
5978 if (per_cu
->is_debug_types
)
5980 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
5982 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5983 section
, dwo_abbrev_section
,
5984 info_ptr
, rcuh_kind::TYPE
);
5985 /* This is not an assert because it can be caused by bad debug info. */
5986 if (sig_type
->signature
!= cu
->header
.signature
)
5988 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5989 " TU at offset %s [in module %s]"),
5990 hex_string (sig_type
->signature
),
5991 hex_string (cu
->header
.signature
),
5992 sect_offset_str (dwo_unit
->sect_off
),
5993 bfd_get_filename (abfd
));
5995 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5996 /* For DWOs coming from DWP files, we don't know the CU length
5997 nor the type's offset in the TU until now. */
5998 dwo_unit
->length
= cu
->header
.get_length_with_initial ();
5999 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
6001 /* Establish the type offset that can be used to lookup the type.
6002 For DWO files, we don't know it until now. */
6003 sig_type
->type_offset_in_section
6004 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
6008 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6009 section
, dwo_abbrev_section
,
6010 info_ptr
, rcuh_kind::COMPILE
);
6011 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
6012 /* For DWOs coming from DWP files, we don't know the CU length
6014 dwo_unit
->length
= cu
->header
.get_length_with_initial ();
6017 dwo_abbrev_section
->read (objfile
);
6018 *result_dwo_abbrev_table
6019 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
6020 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
6021 result_dwo_abbrev_table
->get ());
6023 /* Read in the die, but leave space to copy over the attributes
6024 from the stub. This has the benefit of simplifying the rest of
6025 the code - all the work to maintain the illusion of a single
6026 DW_TAG_{compile,type}_unit DIE is done here. */
6027 num_extra_attrs
= ((stmt_list
!= NULL
)
6031 + (comp_dir
!= NULL
));
6032 info_ptr
= read_full_die_1 (result_reader
, result_comp_unit_die
, info_ptr
,
6035 /* Copy over the attributes from the stub to the DIE we just read in. */
6036 comp_unit_die
= *result_comp_unit_die
;
6037 i
= comp_unit_die
->num_attrs
;
6038 if (stmt_list
!= NULL
)
6039 comp_unit_die
->attrs
[i
++] = *stmt_list
;
6041 comp_unit_die
->attrs
[i
++] = *low_pc
;
6042 if (high_pc
!= NULL
)
6043 comp_unit_die
->attrs
[i
++] = *high_pc
;
6045 comp_unit_die
->attrs
[i
++] = *ranges
;
6046 if (comp_dir
!= NULL
)
6047 comp_unit_die
->attrs
[i
++] = *comp_dir
;
6048 comp_unit_die
->num_attrs
+= num_extra_attrs
;
6050 if (dwarf_die_debug
)
6052 gdb_printf (gdb_stdlog
,
6053 "Read die from %s@0x%x of %s:\n",
6054 section
->get_name (),
6055 (unsigned) (begin_info_ptr
- section
->buffer
),
6056 bfd_get_filename (abfd
));
6057 comp_unit_die
->dump (dwarf_die_debug
);
6060 /* Skip dummy compilation units. */
6061 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
6062 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6065 *result_info_ptr
= info_ptr
;
6069 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6070 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6071 signature is part of the header. */
6072 static gdb::optional
<ULONGEST
>
6073 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
6075 if (cu
->header
.version
>= 5)
6076 return cu
->header
.signature
;
6077 struct attribute
*attr
;
6078 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
6079 if (attr
== nullptr || !attr
->form_is_unsigned ())
6080 return gdb::optional
<ULONGEST
> ();
6081 return attr
->as_unsigned ();
6084 /* Subroutine of cutu_reader to simplify it.
6085 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6086 Returns NULL if the specified DWO unit cannot be found. */
6088 static struct dwo_unit
*
6089 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
6092 /* We need a lock here both to handle the DWO hash table, and BFD,
6093 which is not thread-safe. */
6094 static std::mutex dwo_lock
;
6096 std::lock_guard
<std::mutex
> guard (dwo_lock
);
6099 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6100 struct dwo_unit
*dwo_unit
;
6101 const char *comp_dir
;
6103 gdb_assert (cu
!= NULL
);
6105 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6106 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6107 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
6109 if (per_cu
->is_debug_types
)
6110 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
6113 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
6115 if (!signature
.has_value ())
6116 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6118 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
6120 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
6126 /* Subroutine of cutu_reader to simplify it.
6127 See it for a description of the parameters.
6128 Read a TU directly from a DWO file, bypassing the stub. */
6131 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
6132 dwarf2_per_objfile
*per_objfile
,
6133 dwarf2_cu
*existing_cu
)
6135 struct signatured_type
*sig_type
;
6137 /* Verify we can do the following downcast, and that we have the
6139 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
6140 sig_type
= (struct signatured_type
*) this_cu
;
6141 gdb_assert (sig_type
->dwo_unit
!= NULL
);
6145 if (existing_cu
!= nullptr)
6148 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
6149 /* There's no need to do the rereading_dwo_cu handling that
6150 cutu_reader does since we don't read the stub. */
6154 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6155 in per_objfile yet. */
6156 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6157 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6158 cu
= m_new_cu
.get ();
6161 /* A future optimization, if needed, would be to use an existing
6162 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6163 could share abbrev tables. */
6165 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
6166 NULL
/* stub_comp_unit_die */,
6167 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
6170 &m_dwo_abbrev_table
) == 0)
6177 /* Initialize a CU (or TU) and read its DIEs.
6178 If the CU defers to a DWO file, read the DWO file as well.
6180 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6181 Otherwise the table specified in the comp unit header is read in and used.
6182 This is an optimization for when we already have the abbrev table.
6184 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6187 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6188 dwarf2_per_objfile
*per_objfile
,
6189 struct abbrev_table
*abbrev_table
,
6190 dwarf2_cu
*existing_cu
,
6192 abbrev_cache
*cache
)
6193 : die_reader_specs
{},
6196 struct objfile
*objfile
= per_objfile
->objfile
;
6197 struct dwarf2_section_info
*section
= this_cu
->section
;
6198 bfd
*abfd
= section
->get_bfd_owner ();
6199 const gdb_byte
*begin_info_ptr
;
6200 struct signatured_type
*sig_type
= NULL
;
6201 struct dwarf2_section_info
*abbrev_section
;
6202 /* Non-zero if CU currently points to a DWO file and we need to
6203 reread it. When this happens we need to reread the skeleton die
6204 before we can reread the DWO file (this only applies to CUs, not TUs). */
6205 int rereading_dwo_cu
= 0;
6207 if (dwarf_die_debug
)
6208 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6209 this_cu
->is_debug_types
? "type" : "comp",
6210 sect_offset_str (this_cu
->sect_off
));
6212 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6213 file (instead of going through the stub), short-circuit all of this. */
6214 if (this_cu
->reading_dwo_directly
)
6216 /* Narrow down the scope of possibilities to have to understand. */
6217 gdb_assert (this_cu
->is_debug_types
);
6218 gdb_assert (abbrev_table
== NULL
);
6219 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
6223 /* This is cheap if the section is already read in. */
6224 section
->read (objfile
);
6226 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6228 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
6232 if (existing_cu
!= nullptr)
6235 /* If this CU is from a DWO file we need to start over, we need to
6236 refetch the attributes from the skeleton CU.
6237 This could be optimized by retrieving those attributes from when we
6238 were here the first time: the previous comp_unit_die was stored in
6239 comp_unit_obstack. But there's no data yet that we need this
6241 if (cu
->dwo_unit
!= NULL
)
6242 rereading_dwo_cu
= 1;
6246 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6247 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6248 let us know that the CU is being scanned using the parallel
6249 indexer. This assert is avoided in this case because (1) it
6250 is irrelevant, and (2) the get_cu method is not
6252 gdb_assert (cache
!= nullptr
6253 || per_objfile
->get_cu (this_cu
) == nullptr);
6254 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6255 cu
= m_new_cu
.get ();
6258 /* Get the header. */
6259 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
6261 /* We already have the header, there's no need to read it in again. */
6262 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
6266 if (this_cu
->is_debug_types
)
6268 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6269 section
, abbrev_section
,
6270 info_ptr
, rcuh_kind::TYPE
);
6272 /* Since per_cu is the first member of struct signatured_type,
6273 we can go from a pointer to one to a pointer to the other. */
6274 sig_type
= (struct signatured_type
*) this_cu
;
6275 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
6276 gdb_assert (sig_type
->type_offset_in_tu
6277 == cu
->header
.type_cu_offset_in_tu
);
6278 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6280 /* LENGTH has not been set yet for type units if we're
6281 using .gdb_index. */
6282 this_cu
->set_length (cu
->header
.get_length_with_initial ());
6284 /* Establish the type offset that can be used to lookup the type. */
6285 sig_type
->type_offset_in_section
=
6286 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
6288 this_cu
->set_version (cu
->header
.version
);
6292 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6293 section
, abbrev_section
,
6295 rcuh_kind::COMPILE
);
6297 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6298 this_cu
->set_length (cu
->header
.get_length_with_initial ());
6299 this_cu
->set_version (cu
->header
.version
);
6303 /* Skip dummy compilation units. */
6304 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
6305 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6311 /* If we don't have them yet, read the abbrevs for this compilation unit.
6312 And if we need to read them now, make sure they're freed when we're
6314 if (abbrev_table
!= NULL
)
6315 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
6318 if (cache
!= nullptr)
6319 abbrev_table
= cache
->find (abbrev_section
,
6320 cu
->header
.abbrev_sect_off
);
6321 if (abbrev_table
== nullptr)
6323 abbrev_section
->read (objfile
);
6324 m_abbrev_table_holder
6325 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
6326 abbrev_table
= m_abbrev_table_holder
.get ();
6330 /* Read the top level CU/TU die. */
6331 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
6332 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6334 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
6340 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6341 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6342 table from the DWO file and pass the ownership over to us. It will be
6343 referenced from READER, so we must make sure to free it after we're done
6346 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6347 DWO CU, that this test will fail (the attribute will not be present). */
6348 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6349 if (dwo_name
!= nullptr)
6351 struct dwo_unit
*dwo_unit
;
6352 struct die_info
*dwo_comp_unit_die
;
6354 if (comp_unit_die
->has_children
)
6356 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6357 " has children (offset %s) [in module %s]"),
6358 sect_offset_str (this_cu
->sect_off
),
6359 bfd_get_filename (abfd
));
6361 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
6362 if (dwo_unit
!= NULL
)
6364 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
6365 comp_unit_die
, NULL
,
6368 &m_dwo_abbrev_table
) == 0)
6374 comp_unit_die
= dwo_comp_unit_die
;
6378 /* Yikes, we couldn't find the rest of the DIE, we only have
6379 the stub. A complaint has already been logged. There's
6380 not much more we can do except pass on the stub DIE to
6381 die_reader_func. We don't want to throw an error on bad
6388 cutu_reader::keep ()
6390 /* Done, clean up. */
6391 gdb_assert (!dummy_p
);
6392 if (m_new_cu
!= NULL
)
6394 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6396 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
6397 per_objfile
->set_cu (m_this_cu
, std::move (m_new_cu
));
6401 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6402 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6403 assumed to have already done the lookup to find the DWO file).
6405 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6406 THIS_CU->is_debug_types, but nothing else.
6408 We fill in THIS_CU->length.
6410 THIS_CU->cu is always freed when done.
6411 This is done in order to not leave THIS_CU->cu in a state where we have
6412 to care whether it refers to the "main" CU or the DWO CU.
6414 When parent_cu is passed, it is used to provide a default value for
6415 str_offsets_base and addr_base from the parent. */
6417 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6418 dwarf2_per_objfile
*per_objfile
,
6419 struct dwarf2_cu
*parent_cu
,
6420 struct dwo_file
*dwo_file
)
6421 : die_reader_specs
{},
6424 struct objfile
*objfile
= per_objfile
->objfile
;
6425 struct dwarf2_section_info
*section
= this_cu
->section
;
6426 bfd
*abfd
= section
->get_bfd_owner ();
6427 struct dwarf2_section_info
*abbrev_section
;
6428 const gdb_byte
*begin_info_ptr
, *info_ptr
;
6430 if (dwarf_die_debug
)
6431 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6432 this_cu
->is_debug_types
? "type" : "comp",
6433 sect_offset_str (this_cu
->sect_off
));
6435 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6437 abbrev_section
= (dwo_file
!= NULL
6438 ? &dwo_file
->sections
.abbrev
6439 : get_abbrev_section_for_cu (this_cu
));
6441 /* This is cheap if the section is already read in. */
6442 section
->read (objfile
);
6444 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6446 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6447 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
6448 section
, abbrev_section
, info_ptr
,
6449 (this_cu
->is_debug_types
6451 : rcuh_kind::COMPILE
));
6453 if (parent_cu
!= nullptr)
6455 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
6456 m_new_cu
->addr_base
= parent_cu
->addr_base
;
6458 this_cu
->set_length (m_new_cu
->header
.get_length_with_initial ());
6460 /* Skip dummy compilation units. */
6461 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
6462 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6468 abbrev_section
->read (objfile
);
6469 m_abbrev_table_holder
6470 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
6472 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
6473 m_abbrev_table_holder
.get ());
6474 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6478 /* Type Unit Groups.
6480 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6481 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6482 so that all types coming from the same compilation (.o file) are grouped
6483 together. A future step could be to put the types in the same symtab as
6484 the CU the types ultimately came from. */
6487 hash_type_unit_group (const void *item
)
6489 const struct type_unit_group
*tu_group
6490 = (const struct type_unit_group
*) item
;
6492 return hash_stmt_list_entry (&tu_group
->hash
);
6496 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
6498 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
6499 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
6501 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
6504 /* Allocate a hash table for type unit groups. */
6507 allocate_type_unit_groups_table ()
6509 return htab_up (htab_create_alloc (3,
6510 hash_type_unit_group
,
6512 htab_delete_entry
<type_unit_group
>,
6516 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6517 partial symtabs. We combine several TUs per psymtab to not let the size
6518 of any one psymtab grow too big. */
6519 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6520 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6522 /* Helper routine for get_type_unit_group.
6523 Create the type_unit_group object used to hold one or more TUs. */
6525 static std::unique_ptr
<type_unit_group
>
6526 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
6528 std::unique_ptr
<type_unit_group
> tu_group (new type_unit_group
);
6530 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
6531 tu_group
->hash
.line_sect_off
= line_offset_struct
;
6536 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6537 STMT_LIST is a DW_AT_stmt_list attribute. */
6539 static struct type_unit_group
*
6540 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
6542 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6543 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6544 struct type_unit_group
*tu_group
;
6546 unsigned int line_offset
;
6547 struct type_unit_group type_unit_group_for_lookup
;
6549 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
6550 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
6552 /* Do we need to create a new group, or can we use an existing one? */
6554 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
6556 line_offset
= stmt_list
->as_unsigned ();
6557 ++tu_stats
->nr_symtab_sharers
;
6561 /* Ugh, no stmt_list. Rare, but we have to handle it.
6562 We can do various things here like create one group per TU or
6563 spread them over multiple groups to split up the expansion work.
6564 To avoid worst case scenarios (too many groups or too large groups)
6565 we, umm, group them in bunches. */
6566 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6567 | (tu_stats
->nr_stmt_less_type_units
6568 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
6569 ++tu_stats
->nr_stmt_less_type_units
;
6572 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
6573 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
6574 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
6575 &type_unit_group_for_lookup
, INSERT
);
6576 if (*slot
== nullptr)
6578 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
6579 std::unique_ptr
<type_unit_group
> grp
6580 = create_type_unit_group (cu
, line_offset_struct
);
6581 *slot
= grp
.release ();
6582 ++tu_stats
->nr_symtabs
;
6585 tu_group
= (struct type_unit_group
*) *slot
;
6586 gdb_assert (tu_group
!= nullptr);
6591 /* An instance of this is created when scanning DWARF to create a
6594 class cooked_index_storage
6598 cooked_index_storage ()
6599 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
6601 htab_delete_entry
<cutu_reader
>,
6603 m_index (new cooked_index_shard
)
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 shard being constructed,
6650 then transfer ownership of the index to the caller. */
6651 std::unique_ptr
<cooked_index_shard
> 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_mutable
*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 shard that is being constructed. */
6685 std::unique_ptr
<cooked_index_shard
> m_index
;
6687 /* A writeable addrmap being constructed by this scanner. */
6688 addrmap_mutable m_addrmap
;
6691 /* An instance of this is created to index a CU. */
6693 class cooked_indexer
6697 cooked_indexer (cooked_index_storage
*storage
,
6698 dwarf2_per_cu_data
*per_cu
,
6699 enum language language
)
6700 : m_index_storage (storage
),
6702 m_language (language
)
6706 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
6708 /* Index the given CU. */
6709 void make_index (cutu_reader
*reader
);
6713 /* A helper function to turn a section offset into an address that
6714 can be used in an addrmap. */
6715 CORE_ADDR
form_addr (sect_offset offset
, bool is_dwz
)
6717 CORE_ADDR value
= to_underlying (offset
);
6719 value
|= ((CORE_ADDR
) 1) << (8 * sizeof (CORE_ADDR
) - 1);
6723 /* A helper function to scan the PC bounds of READER and record them
6724 in the storage's addrmap. */
6725 void check_bounds (cutu_reader
*reader
);
6727 /* Ensure that the indicated CU exists. The cutu_reader for it is
6728 returned. FOR_SCANNING is true if the caller intends to scan all
6729 the DIEs in the CU; when false, this use is assumed to be to look
6730 up just a single DIE. */
6731 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
6732 dwarf2_per_objfile
*per_objfile
,
6733 sect_offset sect_off
,
6737 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6738 the entry for the enclosing scope (nullptr at top level). FULLY
6739 is true when a full scan must be done -- in some languages,
6740 function scopes must be fully explored in order to find nested
6741 functions. This returns a pointer to just after the spot where
6743 const gdb_byte
*index_dies (cutu_reader
*reader
,
6744 const gdb_byte
*info_ptr
,
6745 const cooked_index_entry
*parent_entry
,
6748 /* Scan the attributes for a given DIE and update the out
6749 parameters. Returns a pointer to the byte after the DIE. */
6750 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
6751 cutu_reader
*reader
,
6752 const gdb_byte
*watermark_ptr
,
6753 const gdb_byte
*info_ptr
,
6754 const abbrev_info
*abbrev
,
6756 const char **linkage_name
,
6757 cooked_index_flag
*flags
,
6758 sect_offset
*sibling_offset
,
6759 const cooked_index_entry
**parent_entry
,
6760 CORE_ADDR
*maybe_defer
,
6761 bool for_specification
);
6763 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6764 DW_AT_import, and then scanning the referenced CU. Returns a
6765 pointer to the byte after the DIE. */
6766 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
6767 const gdb_byte
*info_ptr
,
6768 const abbrev_info
*abbrev
);
6770 /* Recursively read DIEs, recording the section offsets in
6771 m_die_range_map and then calling index_dies. */
6772 const gdb_byte
*recurse (cutu_reader
*reader
,
6773 const gdb_byte
*info_ptr
,
6774 const cooked_index_entry
*parent_entry
,
6777 /* The storage object, where the results are kept. */
6778 cooked_index_storage
*m_index_storage
;
6779 /* The CU that we are reading on behalf of. This object might be
6780 asked to index one CU but to treat the results as if they come
6781 from some including CU; in this case the including CU would be
6783 dwarf2_per_cu_data
*m_per_cu
;
6784 /* The language that we're assuming when reading. */
6785 enum language m_language
;
6787 /* An addrmap that maps from section offsets (see the form_addr
6788 method) to newly-created entries. See m_deferred_entries to
6790 addrmap_mutable m_die_range_map
;
6792 /* A single deferred entry. */
6793 struct deferred_entry
6795 sect_offset die_offset
;
6797 CORE_ADDR spec_offset
;
6799 cooked_index_flag flags
;
6802 /* The generated DWARF can sometimes have the declaration for a
6803 method in a class (or perhaps namespace) scope, with the
6804 definition appearing outside this scope... just one of the many
6805 bad things about DWARF. In order to handle this situation, we
6806 defer certain entries until the end of scanning, at which point
6807 we'll know the containing context of all the DIEs that we might
6808 have scanned. This vector stores these deferred entries. */
6809 std::vector
<deferred_entry
> m_deferred_entries
;
6812 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6813 Process compilation unit THIS_CU for a psymtab. */
6816 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
6817 dwarf2_per_objfile
*per_objfile
,
6818 cooked_index_storage
*storage
)
6820 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
6821 storage
->get_abbrev_cache ());
6823 if (reader
.comp_unit_die
== nullptr)
6830 else if (this_cu
->is_debug_types
)
6831 build_type_psymtabs_reader (&reader
, storage
);
6832 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
6835 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
6837 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
6839 gdb_assert (storage
!= nullptr);
6840 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->lang ());
6841 indexer
.make_index (&reader
);
6846 /* Reader function for build_type_psymtabs. */
6849 build_type_psymtabs_reader (cutu_reader
*reader
,
6850 cooked_index_storage
*storage
)
6852 struct dwarf2_cu
*cu
= reader
->cu
;
6853 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6854 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
6856 gdb_assert (per_cu
->is_debug_types
);
6858 if (! type_unit_die
->has_children
)
6861 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
6863 gdb_assert (storage
!= nullptr);
6864 cooked_indexer
indexer (storage
, per_cu
, cu
->lang ());
6865 indexer
.make_index (reader
);
6868 /* Struct used to sort TUs by their abbreviation table offset. */
6870 struct tu_abbrev_offset
6872 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
6873 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
6876 /* This is used when sorting. */
6877 bool operator< (const tu_abbrev_offset
&other
) const
6879 return abbrev_offset
< other
.abbrev_offset
;
6882 signatured_type
*sig_type
;
6883 sect_offset abbrev_offset
;
6886 /* Efficiently read all the type units.
6888 The efficiency is because we sort TUs by the abbrev table they use and
6889 only read each abbrev table once. In one program there are 200K TUs
6890 sharing 8K abbrev tables.
6892 The main purpose of this function is to support building the
6893 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6894 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6895 can collapse the search space by grouping them by stmt_list.
6896 The savings can be significant, in the same program from above the 200K TUs
6897 share 8K stmt_list tables.
6899 FUNC is expected to call get_type_unit_group, which will create the
6900 struct type_unit_group if necessary and add it to
6901 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6904 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
6905 cooked_index_storage
*storage
)
6907 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6908 abbrev_table_up abbrev_table
;
6909 sect_offset abbrev_offset
;
6911 /* It's up to the caller to not call us multiple times. */
6912 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
6914 if (per_objfile
->per_bfd
->all_type_units
.size () == 0)
6917 /* TUs typically share abbrev tables, and there can be way more TUs than
6918 abbrev tables. Sort by abbrev table to reduce the number of times we
6919 read each abbrev table in.
6920 Alternatives are to punt or to maintain a cache of abbrev tables.
6921 This is simpler and efficient enough for now.
6923 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6924 symtab to use). Typically TUs with the same abbrev offset have the same
6925 stmt_list value too so in practice this should work well.
6927 The basic algorithm here is:
6929 sort TUs by abbrev table
6930 for each TU with same abbrev table:
6931 read abbrev table if first user
6932 read TU top level DIE
6933 [IWBN if DWO skeletons had DW_AT_stmt_list]
6936 dwarf_read_debug_printf ("Building type unit groups ...");
6938 /* Sort in a separate table to maintain the order of all_units
6939 for .gdb_index: TU indices directly index all_type_units. */
6940 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
6941 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->all_type_units
.size ());
6943 for (const auto &cu
: per_objfile
->per_bfd
->all_units
)
6945 if (cu
->is_debug_types
)
6947 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
6948 sorted_by_abbrev
.emplace_back
6949 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
6950 sig_type
->sect_off
));
6954 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
6956 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
6958 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
6960 /* Switch to the next abbrev table if necessary. */
6961 if (abbrev_table
== NULL
6962 || tu
.abbrev_offset
!= abbrev_offset
)
6964 abbrev_offset
= tu
.abbrev_offset
;
6965 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
6967 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
6968 ++tu_stats
->nr_uniq_abbrev_tables
;
6971 cutu_reader
reader (tu
.sig_type
, per_objfile
,
6972 abbrev_table
.get (), nullptr, false);
6973 if (!reader
.dummy_p
)
6974 build_type_psymtabs_reader (&reader
, storage
);
6978 /* Print collected type unit statistics. */
6981 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
6983 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6985 dwarf_read_debug_printf ("Type unit statistics:");
6986 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
6987 dwarf_read_debug_printf (" %d uniq abbrev tables",
6988 tu_stats
->nr_uniq_abbrev_tables
);
6989 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6990 tu_stats
->nr_symtabs
);
6991 dwarf_read_debug_printf (" %d symtab sharers",
6992 tu_stats
->nr_symtab_sharers
);
6993 dwarf_read_debug_printf (" %d type units without a stmt_list",
6994 tu_stats
->nr_stmt_less_type_units
);
6995 dwarf_read_debug_printf (" %d all_type_units reallocs",
6996 tu_stats
->nr_all_type_units_reallocs
);
6999 struct skeleton_data
7001 dwarf2_per_objfile
*per_objfile
;
7002 cooked_index_storage
*storage
;
7005 /* Traversal function for process_skeletonless_type_unit.
7006 Read a TU in a DWO file and build partial symbols for it. */
7009 process_skeletonless_type_unit (void **slot
, void *info
)
7011 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
7012 skeleton_data
*data
= (skeleton_data
*) info
;
7014 /* If this TU doesn't exist in the global table, add it and read it in. */
7016 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
7017 data
->per_objfile
->per_bfd
->signatured_types
7018 = allocate_signatured_type_table ();
7020 signatured_type
find_entry (dwo_unit
->signature
);
7021 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
7022 &find_entry
, INSERT
);
7023 /* If we've already seen this type there's nothing to do. What's happening
7024 is we're doing our own version of comdat-folding here. */
7028 /* This does the job that create_all_units would have done for
7030 signatured_type
*entry
7031 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
7032 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
7035 /* This does the job that build_type_psymtabs would have done. */
7036 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
7037 if (!reader
.dummy_p
)
7038 build_type_psymtabs_reader (&reader
, data
->storage
);
7043 /* Traversal function for process_skeletonless_type_units. */
7046 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
7048 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
7050 if (dwo_file
->tus
!= NULL
)
7051 htab_traverse_noresize (dwo_file
->tus
.get (),
7052 process_skeletonless_type_unit
, info
);
7057 /* Scan all TUs of DWO files, verifying we've processed them.
7058 This is needed in case a TU was emitted without its skeleton.
7059 Note: This can't be done until we know what all the DWO files are. */
7062 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
7063 cooked_index_storage
*storage
)
7065 skeleton_data data
{ per_objfile
, storage
};
7067 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7068 if (get_dwp_file (per_objfile
) == NULL
7069 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
7071 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
7072 process_dwo_file_for_skeletonless_type_units
,
7077 /* Build the partial symbol table by doing a quick pass through the
7078 .debug_info and .debug_abbrev sections. */
7081 dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
)
7083 struct objfile
*objfile
= per_objfile
->objfile
;
7084 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7086 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7087 objfile_name (objfile
));
7089 per_bfd
->map_info_sections (objfile
);
7091 cooked_index_storage index_storage
;
7092 create_all_units (per_objfile
);
7093 build_type_psymtabs (per_objfile
, &index_storage
);
7094 std::vector
<std::unique_ptr
<cooked_index_shard
>> indexes
;
7096 per_bfd
->quick_file_names_table
7097 = create_quick_file_names_table (per_bfd
->all_units
.size ());
7098 if (!per_bfd
->debug_aranges
.empty ())
7099 read_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
,
7100 index_storage
.get_addrmap ());
7103 /* Ensure that complaints are handled correctly. */
7104 complaint_interceptor complaint_handler
;
7106 using iter_type
= decltype (per_bfd
->all_units
.begin ());
7108 auto task_size_
= [] (iter_type iter
)
7110 dwarf2_per_cu_data
*per_cu
= iter
->get ();
7111 return (size_t)per_cu
->length ();
7113 auto task_size
= gdb::make_function_view (task_size_
);
7115 /* Each thread returns a pair holding a cooked index, and a vector
7116 of errors that should be printed. The latter is done because
7117 GDB's I/O system is not thread-safe. run_on_main_thread could be
7118 used, but that would mean the messages are printed after the
7119 prompt, which looks weird. */
7120 using result_type
= std::pair
<std::unique_ptr
<cooked_index_shard
>,
7121 std::vector
<gdb_exception
>>;
7122 std::vector
<result_type
> results
7123 = gdb::parallel_for_each (1, per_bfd
->all_units
.begin (),
7124 per_bfd
->all_units
.end (),
7125 [=] (iter_type iter
, iter_type end
)
7127 std::vector
<gdb_exception
> errors
;
7128 cooked_index_storage thread_storage
;
7129 for (; iter
!= end
; ++iter
)
7131 dwarf2_per_cu_data
*per_cu
= iter
->get ();
7134 process_psymtab_comp_unit (per_cu
, per_objfile
,
7137 catch (gdb_exception
&except
)
7139 errors
.push_back (std::move (except
));
7142 return result_type (thread_storage
.release (), std::move (errors
));
7145 /* Only show a given exception a single time. */
7146 std::unordered_set
<gdb_exception
> seen_exceptions
;
7147 for (auto &one_result
: results
)
7149 indexes
.push_back (std::move (one_result
.first
));
7150 for (auto &one_exc
: one_result
.second
)
7151 if (seen_exceptions
.insert (one_exc
).second
)
7152 exception_print (gdb_stderr
, one_exc
);
7156 /* This has to wait until we read the CUs, we need the list of DWOs. */
7157 process_skeletonless_type_units (per_objfile
, &index_storage
);
7159 if (dwarf_read_debug
> 0)
7160 print_tu_stats (per_objfile
);
7162 indexes
.push_back (index_storage
.release ());
7163 indexes
.shrink_to_fit ();
7165 cooked_index
*vec
= new cooked_index (std::move (indexes
));
7166 per_bfd
->index_table
.reset (vec
);
7168 const cooked_index_entry
*main_entry
= vec
->get_main ();
7169 if (main_entry
!= nullptr)
7170 set_objfile_main_name (objfile
, main_entry
->name
,
7171 main_entry
->per_cu
->lang ());
7173 dwarf_read_debug_printf ("Done building psymtabs of %s",
7174 objfile_name (objfile
));
7178 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
7179 struct dwarf2_section_info
*section
,
7180 struct dwarf2_section_info
*abbrev_section
,
7181 unsigned int is_dwz
,
7182 htab_up
&types_htab
,
7183 rcuh_kind section_kind
)
7185 const gdb_byte
*info_ptr
;
7186 struct objfile
*objfile
= per_objfile
->objfile
;
7188 dwarf_read_debug_printf ("Reading %s for %s",
7189 section
->get_name (),
7190 section
->get_file_name ());
7192 section
->read (objfile
);
7194 info_ptr
= section
->buffer
;
7196 while (info_ptr
< section
->buffer
+ section
->size
)
7198 dwarf2_per_cu_data_up this_cu
;
7200 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
7202 comp_unit_head cu_header
;
7203 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
7204 abbrev_section
, info_ptr
,
7207 /* Save the compilation unit for later lookup. */
7208 if (cu_header
.unit_type
!= DW_UT_type
)
7209 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
7212 if (types_htab
== nullptr)
7213 types_htab
= allocate_signatured_type_table ();
7215 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
7216 (cu_header
.signature
);
7217 signatured_type
*sig_ptr
= sig_type
.get ();
7218 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
7219 this_cu
.reset (sig_type
.release ());
7221 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
7222 gdb_assert (slot
!= nullptr);
7223 if (*slot
!= nullptr)
7224 complaint (_("debug type entry at offset %s is duplicate to"
7225 " the entry at offset %s, signature %s"),
7226 sect_offset_str (sect_off
),
7227 sect_offset_str (sig_ptr
->sect_off
),
7228 hex_string (sig_ptr
->signature
));
7231 this_cu
->sect_off
= sect_off
;
7232 this_cu
->set_length (cu_header
.get_length_with_initial ());
7233 this_cu
->is_dwz
= is_dwz
;
7234 this_cu
->section
= section
;
7235 /* Init this asap, to avoid a data race in the set_version in
7236 cutu_reader::cutu_reader (which may be run in parallel for the cooked
7238 this_cu
->set_version (cu_header
.version
);
7240 info_ptr
= info_ptr
+ this_cu
->length ();
7241 per_objfile
->per_bfd
->all_units
.push_back (std::move (this_cu
));
7245 /* Initialize the views on all_units. */
7248 finalize_all_units (dwarf2_per_bfd
*per_bfd
)
7250 size_t nr_tus
= per_bfd
->tu_stats
.nr_tus
;
7251 size_t nr_cus
= per_bfd
->all_units
.size () - nr_tus
;
7252 gdb::array_view
<dwarf2_per_cu_data_up
> tmp
= per_bfd
->all_units
;
7253 per_bfd
->all_comp_units
= tmp
.slice (0, nr_cus
);
7254 per_bfd
->all_type_units
= tmp
.slice (nr_cus
, nr_tus
);
7257 /* Create a list of all compilation units in OBJFILE.
7258 This is only done for -readnow and building partial symtabs. */
7261 create_all_units (dwarf2_per_objfile
*per_objfile
)
7264 gdb_assert (per_objfile
->per_bfd
->all_units
.empty ());
7266 read_comp_units_from_section (per_objfile
, &per_objfile
->per_bfd
->info
,
7267 &per_objfile
->per_bfd
->abbrev
, 0,
7268 types_htab
, rcuh_kind::COMPILE
);
7269 for (dwarf2_section_info
§ion
: per_objfile
->per_bfd
->types
)
7270 read_comp_units_from_section (per_objfile
, §ion
,
7271 &per_objfile
->per_bfd
->abbrev
, 0,
7272 types_htab
, rcuh_kind::TYPE
);
7277 dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
7279 catch (const gdb_exception_error
&)
7281 per_objfile
->per_bfd
->all_units
.clear ();
7286 /* Pre-read the sections we'll need to construct an index. */
7287 struct objfile
*objfile
= per_objfile
->objfile
;
7288 dwz
->abbrev
.read (objfile
);
7289 dwz
->info
.read (objfile
);
7290 dwz
->str
.read (objfile
);
7291 dwz
->line
.read (objfile
);
7292 read_comp_units_from_section (per_objfile
, &dwz
->info
, &dwz
->abbrev
, 1,
7293 types_htab
, rcuh_kind::COMPILE
);
7296 per_objfile
->per_bfd
->signatured_types
= std::move (types_htab
);
7298 finalize_all_units (per_objfile
->per_bfd
);
7301 /* Return the initial uleb128 in the die at INFO_PTR. */
7304 peek_abbrev_code (bfd
*abfd
, const gdb_byte
*info_ptr
)
7306 unsigned int bytes_read
;
7308 return read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7311 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7312 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7314 Return the corresponding abbrev, or NULL if the number is zero (indicating
7315 an empty DIE). In either case *BYTES_READ will be set to the length of
7316 the initial number. */
7318 static const struct abbrev_info
*
7319 peek_die_abbrev (const die_reader_specs
&reader
,
7320 const gdb_byte
*info_ptr
, unsigned int *bytes_read
)
7322 dwarf2_cu
*cu
= reader
.cu
;
7323 bfd
*abfd
= reader
.abfd
;
7324 unsigned int abbrev_number
7325 = read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
7327 if (abbrev_number
== 0)
7330 const abbrev_info
*abbrev
7331 = reader
.abbrev_table
->lookup_abbrev (abbrev_number
);
7334 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7335 " at offset %s [in module %s]"),
7336 abbrev_number
, cu
->per_cu
->is_debug_types
? "TU" : "CU",
7337 sect_offset_str (cu
->header
.sect_off
), bfd_get_filename (abfd
));
7343 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7344 Returns a pointer to the end of a series of DIEs, terminated by an empty
7345 DIE. Any children of the skipped DIEs will also be skipped. */
7347 static const gdb_byte
*
7348 skip_children (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
)
7352 unsigned int bytes_read
;
7353 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
7357 return info_ptr
+ bytes_read
;
7359 info_ptr
= skip_one_die (reader
, info_ptr
+ bytes_read
, abbrev
);
7363 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7364 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7365 abbrev corresponding to that skipped uleb128 should be passed in
7368 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7369 returns a pointer to this DIE's sibling, skipping any children.
7370 Otherwise, returns a pointer to the DIE's first child. */
7372 static const gdb_byte
*
7373 skip_one_die (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
,
7374 const struct abbrev_info
*abbrev
, bool do_skip_children
)
7376 unsigned int bytes_read
;
7377 struct attribute attr
;
7378 bfd
*abfd
= reader
->abfd
;
7379 struct dwarf2_cu
*cu
= reader
->cu
;
7380 const gdb_byte
*buffer
= reader
->buffer
;
7381 const gdb_byte
*buffer_end
= reader
->buffer_end
;
7382 unsigned int form
, i
;
7384 if (do_skip_children
&& abbrev
->sibling_offset
!= (unsigned short) -1)
7386 /* We only handle DW_FORM_ref4 here. */
7387 const gdb_byte
*sibling_data
= info_ptr
+ abbrev
->sibling_offset
;
7388 unsigned int offset
= read_4_bytes (abfd
, sibling_data
);
7389 const gdb_byte
*sibling_ptr
7390 = buffer
+ to_underlying (cu
->header
.sect_off
) + offset
;
7391 if (sibling_ptr
>= info_ptr
&& sibling_ptr
< reader
->buffer_end
)
7393 /* Fall through to the slow way. */
7395 else if (abbrev
->size_if_constant
!= 0)
7397 info_ptr
+= abbrev
->size_if_constant
;
7398 if (do_skip_children
&& abbrev
->has_children
)
7399 return skip_children (reader
, info_ptr
);
7403 for (i
= 0; i
< abbrev
->num_attrs
; i
++)
7405 /* The only abbrev we care about is DW_AT_sibling. */
7406 if (do_skip_children
&& abbrev
->attrs
[i
].name
== DW_AT_sibling
)
7408 read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
7409 if (attr
.form
== DW_FORM_ref_addr
)
7410 complaint (_("ignoring absolute DW_AT_sibling"));
7413 sect_offset off
= attr
.get_ref_die_offset ();
7414 const gdb_byte
*sibling_ptr
= buffer
+ to_underlying (off
);
7416 if (sibling_ptr
< info_ptr
)
7417 complaint (_("DW_AT_sibling points backwards"));
7418 else if (sibling_ptr
> reader
->buffer_end
)
7419 reader
->die_section
->overflow_complaint ();
7425 /* If it isn't DW_AT_sibling, skip this attribute. */
7426 form
= abbrev
->attrs
[i
].form
;
7430 case DW_FORM_ref_addr
:
7431 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7432 and later it is offset sized. */
7433 if (cu
->header
.version
== 2)
7434 info_ptr
+= cu
->header
.addr_size
;
7436 info_ptr
+= cu
->header
.offset_size
;
7438 case DW_FORM_GNU_ref_alt
:
7439 info_ptr
+= cu
->header
.offset_size
;
7442 info_ptr
+= cu
->header
.addr_size
;
7450 case DW_FORM_flag_present
:
7451 case DW_FORM_implicit_const
:
7468 case DW_FORM_ref_sig8
:
7471 case DW_FORM_data16
:
7474 case DW_FORM_string
:
7475 read_direct_string (abfd
, info_ptr
, &bytes_read
);
7476 info_ptr
+= bytes_read
;
7478 case DW_FORM_sec_offset
:
7480 case DW_FORM_GNU_strp_alt
:
7481 info_ptr
+= cu
->header
.offset_size
;
7483 case DW_FORM_exprloc
:
7485 info_ptr
+= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7486 info_ptr
+= bytes_read
;
7488 case DW_FORM_block1
:
7489 info_ptr
+= 1 + read_1_byte (abfd
, info_ptr
);
7491 case DW_FORM_block2
:
7492 info_ptr
+= 2 + read_2_bytes (abfd
, info_ptr
);
7494 case DW_FORM_block4
:
7495 info_ptr
+= 4 + read_4_bytes (abfd
, info_ptr
);
7501 case DW_FORM_ref_udata
:
7502 case DW_FORM_GNU_addr_index
:
7503 case DW_FORM_GNU_str_index
:
7504 case DW_FORM_rnglistx
:
7505 case DW_FORM_loclistx
:
7506 info_ptr
= safe_skip_leb128 (info_ptr
, buffer_end
);
7508 case DW_FORM_indirect
:
7509 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7510 info_ptr
+= bytes_read
;
7511 /* We need to continue parsing from here, so just go back to
7513 goto skip_attribute
;
7516 error (_("Dwarf Error: Cannot handle %s "
7517 "in DWARF reader [in module %s]"),
7518 dwarf_form_name (form
),
7519 bfd_get_filename (abfd
));
7523 if (do_skip_children
&& abbrev
->has_children
)
7524 return skip_children (reader
, info_ptr
);
7529 /* Reading in full CUs. */
7531 /* Add PER_CU to the queue. */
7534 queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
7535 dwarf2_per_objfile
*per_objfile
,
7536 enum language pretend_language
)
7540 gdb_assert (per_objfile
->queue
.has_value ());
7541 per_objfile
->queue
->emplace (per_cu
, per_objfile
, pretend_language
);
7544 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7546 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7549 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7550 DIEs, false otherwise.
7552 Explanation: there is an invariant that if a CU is queued for expansion
7553 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7554 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7555 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7556 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7557 invariant is respected.
7559 The caller is therefore not required to load the CU's DIEs (we return false)
7562 - the CU is already expanded, and therefore does not get enqueued
7563 - the CU gets enqueued for expansion, but its DIEs are already loaded
7565 Note that the caller should not use this function's return value as an
7566 indicator of whether the CU's DIEs are loaded right now, it should check
7567 that by calling `dwarf2_per_objfile::get_cu` instead. */
7570 maybe_queue_comp_unit (struct dwarf2_cu
*dependent_cu
,
7571 dwarf2_per_cu_data
*per_cu
,
7572 dwarf2_per_objfile
*per_objfile
,
7573 enum language pretend_language
)
7575 /* Mark the dependence relation so that we don't flush PER_CU
7577 if (dependent_cu
!= NULL
)
7578 dependent_cu
->add_dependence (per_cu
);
7580 /* If it's already on the queue, we have nothing to do. */
7583 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7585 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
7587 /* If the CU is queued for expansion, it should not already be
7589 gdb_assert (!per_objfile
->symtab_set_p (per_cu
));
7591 /* The DIEs are already loaded, the caller doesn't need to do it. */
7595 bool queued
= false;
7596 if (!per_objfile
->symtab_set_p (per_cu
))
7598 /* Add it to the queue. */
7599 queue_comp_unit (per_cu
, per_objfile
, pretend_language
);
7603 /* If the compilation unit is already loaded, just mark it as
7605 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7609 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7610 and the DIEs are not already loaded. */
7611 return queued
&& cu
== nullptr;
7614 /* Process the queue. */
7617 process_queue (dwarf2_per_objfile
*per_objfile
)
7619 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7620 objfile_name (per_objfile
->objfile
));
7622 /* The queue starts out with one item, but following a DIE reference
7623 may load a new CU, adding it to the end of the queue. */
7624 while (!per_objfile
->queue
->empty ())
7626 dwarf2_queue_item
&item
= per_objfile
->queue
->front ();
7627 dwarf2_per_cu_data
*per_cu
= item
.per_cu
;
7629 if (!per_objfile
->symtab_set_p (per_cu
))
7631 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7633 /* Skip dummy CUs. */
7636 unsigned int debug_print_threshold
;
7639 if (per_cu
->is_debug_types
)
7641 struct signatured_type
*sig_type
=
7642 (struct signatured_type
*) per_cu
;
7644 sprintf (buf
, "TU %s at offset %s",
7645 hex_string (sig_type
->signature
),
7646 sect_offset_str (per_cu
->sect_off
));
7647 /* There can be 100s of TUs.
7648 Only print them in verbose mode. */
7649 debug_print_threshold
= 2;
7653 sprintf (buf
, "CU at offset %s",
7654 sect_offset_str (per_cu
->sect_off
));
7655 debug_print_threshold
= 1;
7658 if (dwarf_read_debug
>= debug_print_threshold
)
7659 dwarf_read_debug_printf ("Expanding symtab of %s", buf
);
7661 if (per_cu
->is_debug_types
)
7662 process_full_type_unit (cu
, item
.pretend_language
);
7664 process_full_comp_unit (cu
, item
.pretend_language
);
7666 if (dwarf_read_debug
>= debug_print_threshold
)
7667 dwarf_read_debug_printf ("Done expanding %s", buf
);
7672 per_objfile
->queue
->pop ();
7675 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7676 objfile_name (per_objfile
->objfile
));
7679 /* Load the DIEs associated with PER_CU into memory.
7681 In some cases, the caller, while reading partial symbols, will need to load
7682 the full symbols for the CU for some reason. It will already have a
7683 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7684 rather than creating a new one. */
7687 load_full_comp_unit (dwarf2_per_cu_data
*this_cu
,
7688 dwarf2_per_objfile
*per_objfile
,
7689 dwarf2_cu
*existing_cu
,
7691 enum language pretend_language
)
7693 gdb_assert (! this_cu
->is_debug_types
);
7695 cutu_reader
reader (this_cu
, per_objfile
, NULL
, existing_cu
, skip_partial
);
7699 struct dwarf2_cu
*cu
= reader
.cu
;
7700 const gdb_byte
*info_ptr
= reader
.info_ptr
;
7702 gdb_assert (cu
->die_hash
== NULL
);
7704 htab_create_alloc_ex (cu
->header
.get_length_without_initial () / 12,
7708 &cu
->comp_unit_obstack
,
7709 hashtab_obstack_allocate
,
7710 dummy_obstack_deallocate
);
7712 if (reader
.comp_unit_die
->has_children
)
7713 reader
.comp_unit_die
->child
7714 = read_die_and_siblings (&reader
, reader
.info_ptr
,
7715 &info_ptr
, reader
.comp_unit_die
);
7716 cu
->dies
= reader
.comp_unit_die
;
7717 /* comp_unit_die is not stored in die_hash, no need. */
7719 /* We try not to read any attributes in this function, because not
7720 all CUs needed for references have been loaded yet, and symbol
7721 table processing isn't initialized. But we have to set the CU language,
7722 or we won't be able to build types correctly.
7723 Similarly, if we do not read the producer, we can not apply
7724 producer-specific interpretation. */
7725 prepare_one_comp_unit (cu
, cu
->dies
, pretend_language
);
7730 /* Add a DIE to the delayed physname list. */
7733 add_to_method_list (struct type
*type
, int fnfield_index
, int index
,
7734 const char *name
, struct die_info
*die
,
7735 struct dwarf2_cu
*cu
)
7737 struct delayed_method_info mi
;
7739 mi
.fnfield_index
= fnfield_index
;
7743 cu
->method_list
.push_back (mi
);
7746 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7747 "const" / "volatile". If so, decrements LEN by the length of the
7748 modifier and return true. Otherwise return false. */
7752 check_modifier (const char *physname
, size_t &len
, const char (&mod
)[N
])
7754 size_t mod_len
= sizeof (mod
) - 1;
7755 if (len
> mod_len
&& startswith (physname
+ (len
- mod_len
), mod
))
7763 /* Compute the physnames of any methods on the CU's method list.
7765 The computation of method physnames is delayed in order to avoid the
7766 (bad) condition that one of the method's formal parameters is of an as yet
7770 compute_delayed_physnames (struct dwarf2_cu
*cu
)
7772 /* Only C++ delays computing physnames. */
7773 if (cu
->method_list
.empty ())
7775 gdb_assert (cu
->lang () == language_cplus
);
7777 for (const delayed_method_info
&mi
: cu
->method_list
)
7779 const char *physname
;
7780 struct fn_fieldlist
*fn_flp
7781 = &TYPE_FN_FIELDLIST (mi
.type
, mi
.fnfield_index
);
7782 physname
= dwarf2_physname (mi
.name
, mi
.die
, cu
);
7783 TYPE_FN_FIELD_PHYSNAME (fn_flp
->fn_fields
, mi
.index
)
7784 = physname
? physname
: "";
7786 /* Since there's no tag to indicate whether a method is a
7787 const/volatile overload, extract that information out of the
7789 if (physname
!= NULL
)
7791 size_t len
= strlen (physname
);
7795 if (physname
[len
] == ')') /* shortcut */
7797 else if (check_modifier (physname
, len
, " const"))
7798 TYPE_FN_FIELD_CONST (fn_flp
->fn_fields
, mi
.index
) = 1;
7799 else if (check_modifier (physname
, len
, " volatile"))
7800 TYPE_FN_FIELD_VOLATILE (fn_flp
->fn_fields
, mi
.index
) = 1;
7807 /* The list is no longer needed. */
7808 cu
->method_list
.clear ();
7811 /* Go objects should be embedded in a DW_TAG_module DIE,
7812 and it's not clear if/how imported objects will appear.
7813 To keep Go support simple until that's worked out,
7814 go back through what we've read and create something usable.
7815 We could do this while processing each DIE, and feels kinda cleaner,
7816 but that way is more invasive.
7817 This is to, for example, allow the user to type "p var" or "b main"
7818 without having to specify the package name, and allow lookups
7819 of module.object to work in contexts that use the expression
7823 fixup_go_packaging (struct dwarf2_cu
*cu
)
7825 gdb::unique_xmalloc_ptr
<char> package_name
;
7826 struct pending
*list
;
7829 for (list
= *cu
->get_builder ()->get_global_symbols ();
7833 for (i
= 0; i
< list
->nsyms
; ++i
)
7835 struct symbol
*sym
= list
->symbol
[i
];
7837 if (sym
->language () == language_go
7838 && sym
->aclass () == LOC_BLOCK
)
7840 gdb::unique_xmalloc_ptr
<char> this_package_name
7841 (go_symbol_package_name (sym
));
7843 if (this_package_name
== NULL
)
7845 if (package_name
== NULL
)
7846 package_name
= std::move (this_package_name
);
7849 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7850 if (strcmp (package_name
.get (), this_package_name
.get ()) != 0)
7851 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7852 (sym
->symtab () != NULL
7853 ? symtab_to_filename_for_display
7855 : objfile_name (objfile
)),
7856 this_package_name
.get (), package_name
.get ());
7862 if (package_name
!= NULL
)
7864 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7865 const char *saved_package_name
= objfile
->intern (package_name
.get ());
7866 struct type
*type
= init_type (objfile
, TYPE_CODE_MODULE
, 0,
7867 saved_package_name
);
7870 sym
= new (&objfile
->objfile_obstack
) symbol
;
7871 sym
->set_language (language_go
, &objfile
->objfile_obstack
);
7872 sym
->compute_and_set_names (saved_package_name
, false, objfile
->per_bfd
);
7873 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7874 e.g., "main" finds the "main" module and not C's main(). */
7875 sym
->set_domain (STRUCT_DOMAIN
);
7876 sym
->set_aclass_index (LOC_TYPEDEF
);
7877 sym
->set_type (type
);
7879 add_symbol_to_list (sym
, cu
->get_builder ()->get_global_symbols ());
7883 /* Allocate a fully-qualified name consisting of the two parts on the
7887 rust_fully_qualify (struct obstack
*obstack
, const char *p1
, const char *p2
)
7889 return obconcat (obstack
, p1
, "::", p2
, (char *) NULL
);
7892 /* A helper that allocates a variant part to attach to a Rust enum
7893 type. OBSTACK is where the results should be allocated. TYPE is
7894 the type we're processing. DISCRIMINANT_INDEX is the index of the
7895 discriminant. It must be the index of one of the fields of TYPE,
7896 or -1 to mean there is no discriminant (univariant enum).
7897 DEFAULT_INDEX is the index of the default field; or -1 if there is
7898 no default. RANGES is indexed by "effective" field number (the
7899 field index, but omitting the discriminant and default fields) and
7900 must hold the discriminant values used by the variants. Note that
7901 RANGES must have a lifetime at least as long as OBSTACK -- either
7902 already allocated on it, or static. */
7905 alloc_rust_variant (struct obstack
*obstack
, struct type
*type
,
7906 int discriminant_index
, int default_index
,
7907 gdb::array_view
<discriminant_range
> ranges
)
7909 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7910 gdb_assert (discriminant_index
== -1
7911 || (discriminant_index
>= 0
7912 && discriminant_index
< type
->num_fields ()));
7913 gdb_assert (default_index
== -1
7914 || (default_index
>= 0 && default_index
< type
->num_fields ()));
7916 /* We have one variant for each non-discriminant field. */
7917 int n_variants
= type
->num_fields ();
7918 if (discriminant_index
!= -1)
7921 variant
*variants
= new (obstack
) variant
[n_variants
];
7924 for (int i
= 0; i
< type
->num_fields (); ++i
)
7926 if (i
== discriminant_index
)
7929 variants
[var_idx
].first_field
= i
;
7930 variants
[var_idx
].last_field
= i
+ 1;
7932 /* The default field does not need a range, but other fields do.
7933 We skipped the discriminant above. */
7934 if (i
!= default_index
)
7936 variants
[var_idx
].discriminants
= ranges
.slice (range_idx
, 1);
7943 gdb_assert (range_idx
== ranges
.size ());
7944 gdb_assert (var_idx
== n_variants
);
7946 variant_part
*part
= new (obstack
) variant_part
;
7947 part
->discriminant_index
= discriminant_index
;
7948 /* If there is no discriminant, then whether it is signed is of no
7951 = (discriminant_index
== -1
7953 : type
->field (discriminant_index
).type ()->is_unsigned ());
7954 part
->variants
= gdb::array_view
<variant
> (variants
, n_variants
);
7956 void *storage
= obstack_alloc (obstack
, sizeof (gdb::array_view
<variant_part
>));
7957 gdb::array_view
<variant_part
> *prop_value
7958 = new (storage
) gdb::array_view
<variant_part
> (part
, 1);
7960 struct dynamic_prop prop
;
7961 prop
.set_variant_parts (prop_value
);
7963 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
7966 /* Some versions of rustc emitted enums in an unusual way.
7968 Ordinary enums were emitted as unions. The first element of each
7969 structure in the union was named "RUST$ENUM$DISR". This element
7970 held the discriminant.
7972 These versions of Rust also implemented the "non-zero"
7973 optimization. When the enum had two values, and one is empty and
7974 the other holds a pointer that cannot be zero, the pointer is used
7975 as the discriminant, with a zero value meaning the empty variant.
7976 Here, the union's first member is of the form
7977 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7978 where the fieldnos are the indices of the fields that should be
7979 traversed in order to find the field (which may be several fields deep)
7980 and the variantname is the name of the variant of the case when the
7983 This function recognizes whether TYPE is of one of these forms,
7984 and, if so, smashes it to be a variant type. */
7987 quirk_rust_enum (struct type
*type
, struct objfile
*objfile
)
7989 gdb_assert (type
->code () == TYPE_CODE_UNION
);
7991 /* We don't need to deal with empty enums. */
7992 if (type
->num_fields () == 0)
7995 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7996 if (type
->num_fields () == 1
7997 && startswith (type
->field (0).name (), RUST_ENUM_PREFIX
))
7999 const char *name
= type
->field (0).name () + strlen (RUST_ENUM_PREFIX
);
8001 /* Decode the field name to find the offset of the
8003 ULONGEST bit_offset
= 0;
8004 struct type
*field_type
= type
->field (0).type ();
8005 while (name
[0] >= '0' && name
[0] <= '9')
8008 unsigned long index
= strtoul (name
, &tail
, 10);
8011 || index
>= field_type
->num_fields ()
8012 || (field_type
->field (index
).loc_kind ()
8013 != FIELD_LOC_KIND_BITPOS
))
8015 complaint (_("Could not parse Rust enum encoding string \"%s\""
8017 type
->field (0).name (),
8018 objfile_name (objfile
));
8023 bit_offset
+= field_type
->field (index
).loc_bitpos ();
8024 field_type
= field_type
->field (index
).type ();
8027 /* Smash this type to be a structure type. We have to do this
8028 because the type has already been recorded. */
8029 type
->set_code (TYPE_CODE_STRUCT
);
8030 type
->set_num_fields (3);
8031 /* Save the field we care about. */
8032 struct field saved_field
= type
->field (0);
8034 ((struct field
*) TYPE_ZALLOC (type
, 3 * sizeof (struct field
)));
8036 /* Put the discriminant at index 0. */
8037 type
->field (0).set_type (field_type
);
8038 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8039 type
->field (0).set_name ("<<discriminant>>");
8040 type
->field (0).set_loc_bitpos (bit_offset
);
8042 /* The order of fields doesn't really matter, so put the real
8043 field at index 1 and the data-less field at index 2. */
8044 type
->field (1) = saved_field
;
8045 type
->field (1).set_name
8046 (rust_last_path_segment (type
->field (1).type ()->name ()));
8047 type
->field (1).type ()->set_name
8048 (rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8049 type
->field (1).name ()));
8051 const char *dataless_name
8052 = rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8054 struct type
*dataless_type
= init_type (objfile
, TYPE_CODE_VOID
, 0,
8056 type
->field (2).set_type (dataless_type
);
8057 /* NAME points into the original discriminant name, which
8058 already has the correct lifetime. */
8059 type
->field (2).set_name (name
);
8060 type
->field (2).set_loc_bitpos (0);
8062 /* Indicate that this is a variant type. */
8063 static discriminant_range ranges
[1] = { { 0, 0 } };
8064 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, 1, ranges
);
8066 /* A union with a single anonymous field is probably an old-style
8068 else if (type
->num_fields () == 1 && streq (type
->field (0).name (), ""))
8070 /* Smash this type to be a structure type. We have to do this
8071 because the type has already been recorded. */
8072 type
->set_code (TYPE_CODE_STRUCT
);
8074 struct type
*field_type
= type
->field (0).type ();
8075 const char *variant_name
8076 = rust_last_path_segment (field_type
->name ());
8077 type
->field (0).set_name (variant_name
);
8078 field_type
->set_name
8079 (rust_fully_qualify (&objfile
->objfile_obstack
,
8080 type
->name (), variant_name
));
8082 alloc_rust_variant (&objfile
->objfile_obstack
, type
, -1, 0, {});
8086 struct type
*disr_type
= nullptr;
8087 for (int i
= 0; i
< type
->num_fields (); ++i
)
8089 disr_type
= type
->field (i
).type ();
8091 if (disr_type
->code () != TYPE_CODE_STRUCT
)
8093 /* All fields of a true enum will be structs. */
8096 else if (disr_type
->num_fields () == 0)
8098 /* Could be data-less variant, so keep going. */
8099 disr_type
= nullptr;
8101 else if (strcmp (disr_type
->field (0).name (),
8102 "RUST$ENUM$DISR") != 0)
8104 /* Not a Rust enum. */
8114 /* If we got here without a discriminant, then it's probably
8116 if (disr_type
== nullptr)
8119 /* Smash this type to be a structure type. We have to do this
8120 because the type has already been recorded. */
8121 type
->set_code (TYPE_CODE_STRUCT
);
8123 /* Make space for the discriminant field. */
8124 struct field
*disr_field
= &disr_type
->field (0);
8126 = (struct field
*) TYPE_ZALLOC (type
, ((type
->num_fields () + 1)
8127 * sizeof (struct field
)));
8128 memcpy (new_fields
+ 1, type
->fields (),
8129 type
->num_fields () * sizeof (struct field
));
8130 type
->set_fields (new_fields
);
8131 type
->set_num_fields (type
->num_fields () + 1);
8133 /* Install the discriminant at index 0 in the union. */
8134 type
->field (0) = *disr_field
;
8135 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8136 type
->field (0).set_name ("<<discriminant>>");
8138 /* We need a way to find the correct discriminant given a
8139 variant name. For convenience we build a map here. */
8140 struct type
*enum_type
= disr_field
->type ();
8141 std::unordered_map
<std::string
, ULONGEST
> discriminant_map
;
8142 for (int i
= 0; i
< enum_type
->num_fields (); ++i
)
8144 if (enum_type
->field (i
).loc_kind () == FIELD_LOC_KIND_ENUMVAL
)
8147 = rust_last_path_segment (enum_type
->field (i
).name ());
8148 discriminant_map
[name
] = enum_type
->field (i
).loc_enumval ();
8152 int n_fields
= type
->num_fields ();
8153 /* We don't need a range entry for the discriminant, but we do
8154 need one for every other field, as there is no default
8156 discriminant_range
*ranges
= XOBNEWVEC (&objfile
->objfile_obstack
,
8159 /* Skip the discriminant here. */
8160 for (int i
= 1; i
< n_fields
; ++i
)
8162 /* Find the final word in the name of this variant's type.
8163 That name can be used to look up the correct
8165 const char *variant_name
8166 = rust_last_path_segment (type
->field (i
).type ()->name ());
8168 auto iter
= discriminant_map
.find (variant_name
);
8169 if (iter
!= discriminant_map
.end ())
8171 ranges
[i
- 1].low
= iter
->second
;
8172 ranges
[i
- 1].high
= iter
->second
;
8175 /* In Rust, each element should have the size of the
8177 type
->field (i
).type ()->set_length (type
->length ());
8179 /* Remove the discriminant field, if it exists. */
8180 struct type
*sub_type
= type
->field (i
).type ();
8181 if (sub_type
->num_fields () > 0)
8183 sub_type
->set_num_fields (sub_type
->num_fields () - 1);
8184 sub_type
->set_fields (sub_type
->fields () + 1);
8186 type
->field (i
).set_name (variant_name
);
8188 (rust_fully_qualify (&objfile
->objfile_obstack
,
8189 type
->name (), variant_name
));
8192 /* Indicate that this is a variant type. */
8193 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, -1,
8194 gdb::array_view
<discriminant_range
> (ranges
,
8199 /* Rewrite some Rust unions to be structures with variants parts. */
8202 rust_union_quirks (struct dwarf2_cu
*cu
)
8204 gdb_assert (cu
->lang () == language_rust
);
8205 for (type
*type_
: cu
->rust_unions
)
8206 quirk_rust_enum (type_
, cu
->per_objfile
->objfile
);
8207 /* We don't need this any more. */
8208 cu
->rust_unions
.clear ();
8213 type_unit_group_unshareable
*
8214 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group
*tu_group
)
8216 auto iter
= this->m_type_units
.find (tu_group
);
8217 if (iter
!= this->m_type_units
.end ())
8218 return iter
->second
.get ();
8220 type_unit_group_unshareable_up
uniq (new type_unit_group_unshareable
);
8221 type_unit_group_unshareable
*result
= uniq
.get ();
8222 this->m_type_units
[tu_group
] = std::move (uniq
);
8227 dwarf2_per_objfile::get_type_for_signatured_type
8228 (signatured_type
*sig_type
) const
8230 auto iter
= this->m_type_map
.find (sig_type
);
8231 if (iter
== this->m_type_map
.end ())
8234 return iter
->second
;
8237 void dwarf2_per_objfile::set_type_for_signatured_type
8238 (signatured_type
*sig_type
, struct type
*type
)
8240 gdb_assert (this->m_type_map
.find (sig_type
) == this->m_type_map
.end ());
8242 this->m_type_map
[sig_type
] = type
;
8245 /* A helper function for computing the list of all symbol tables
8246 included by PER_CU. */
8249 recursively_compute_inclusions (std::vector
<compunit_symtab
*> *result
,
8250 htab_t all_children
, htab_t all_type_symtabs
,
8251 dwarf2_per_cu_data
*per_cu
,
8252 dwarf2_per_objfile
*per_objfile
,
8253 struct compunit_symtab
*immediate_parent
)
8255 void **slot
= htab_find_slot (all_children
, per_cu
, INSERT
);
8258 /* This inclusion and its children have been processed. */
8264 /* Only add a CU if it has a symbol table. */
8265 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8268 /* If this is a type unit only add its symbol table if we haven't
8269 seen it yet (type unit per_cu's can share symtabs). */
8270 if (per_cu
->is_debug_types
)
8272 slot
= htab_find_slot (all_type_symtabs
, cust
, INSERT
);
8276 result
->push_back (cust
);
8277 if (cust
->user
== NULL
)
8278 cust
->user
= immediate_parent
;
8283 result
->push_back (cust
);
8284 if (cust
->user
== NULL
)
8285 cust
->user
= immediate_parent
;
8289 if (!per_cu
->imported_symtabs_empty ())
8290 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8292 recursively_compute_inclusions (result
, all_children
,
8293 all_type_symtabs
, ptr
, per_objfile
,
8298 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8302 compute_compunit_symtab_includes (dwarf2_per_cu_data
*per_cu
,
8303 dwarf2_per_objfile
*per_objfile
)
8305 gdb_assert (! per_cu
->is_debug_types
);
8307 if (!per_cu
->imported_symtabs_empty ())
8310 std::vector
<compunit_symtab
*> result_symtabs
;
8311 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8313 /* If we don't have a symtab, we can just skip this case. */
8317 htab_up
all_children (htab_create_alloc (1, htab_hash_pointer
,
8319 NULL
, xcalloc
, xfree
));
8320 htab_up
all_type_symtabs (htab_create_alloc (1, htab_hash_pointer
,
8322 NULL
, xcalloc
, xfree
));
8324 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8326 recursively_compute_inclusions (&result_symtabs
, all_children
.get (),
8327 all_type_symtabs
.get (), ptr
,
8331 /* Now we have a transitive closure of all the included symtabs. */
8332 len
= result_symtabs
.size ();
8334 = XOBNEWVEC (&per_objfile
->objfile
->objfile_obstack
,
8335 struct compunit_symtab
*, len
+ 1);
8336 memcpy (cust
->includes
, result_symtabs
.data (),
8337 len
* sizeof (compunit_symtab
*));
8338 cust
->includes
[len
] = NULL
;
8342 /* Compute the 'includes' field for the symtabs of all the CUs we just
8346 process_cu_includes (dwarf2_per_objfile
*per_objfile
)
8348 for (dwarf2_per_cu_data
*iter
: per_objfile
->per_bfd
->just_read_cus
)
8350 if (! iter
->is_debug_types
)
8351 compute_compunit_symtab_includes (iter
, per_objfile
);
8354 per_objfile
->per_bfd
->just_read_cus
.clear ();
8357 /* Generate full symbol information for CU, whose DIEs have
8358 already been loaded into memory. */
8361 process_full_comp_unit (dwarf2_cu
*cu
, enum language pretend_language
)
8363 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8364 struct objfile
*objfile
= per_objfile
->objfile
;
8365 struct gdbarch
*gdbarch
= objfile
->arch ();
8366 CORE_ADDR lowpc
, highpc
;
8367 struct compunit_symtab
*cust
;
8369 struct block
*static_block
;
8372 baseaddr
= objfile
->text_section_offset ();
8374 /* Clear the list here in case something was left over. */
8375 cu
->method_list
.clear ();
8377 dwarf2_find_base_address (cu
->dies
, cu
);
8379 /* Before we start reading the top-level DIE, ensure it has a valid tag
8381 switch (cu
->dies
->tag
)
8383 case DW_TAG_compile_unit
:
8384 case DW_TAG_partial_unit
:
8385 case DW_TAG_type_unit
:
8388 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8389 dwarf_tag_name (cu
->dies
->tag
),
8390 sect_offset_str (cu
->per_cu
->sect_off
),
8391 objfile_name (per_objfile
->objfile
));
8394 /* Do line number decoding in read_file_scope () */
8395 process_die (cu
->dies
, cu
);
8397 /* For now fudge the Go package. */
8398 if (cu
->lang () == language_go
)
8399 fixup_go_packaging (cu
);
8401 /* Now that we have processed all the DIEs in the CU, all the types
8402 should be complete, and it should now be safe to compute all of the
8404 compute_delayed_physnames (cu
);
8406 if (cu
->lang () == language_rust
)
8407 rust_union_quirks (cu
);
8409 /* Some compilers don't define a DW_AT_high_pc attribute for the
8410 compilation unit. If the DW_AT_high_pc is missing, synthesize
8411 it, by scanning the DIE's below the compilation unit. */
8412 get_scope_pc_bounds (cu
->dies
, &lowpc
, &highpc
, cu
);
8414 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
8416 = cu
->get_builder ()->end_compunit_symtab_get_static_block (addr
, 0, 1);
8418 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8419 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8420 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8421 addrmap to help ensure it has an accurate map of pc values belonging to
8423 dwarf2_record_block_ranges (cu
->dies
, static_block
, baseaddr
, cu
);
8425 cust
= cu
->get_builder ()->end_compunit_symtab_from_static_block
8430 int gcc_4_minor
= producer_is_gcc_ge_4 (cu
->producer
);
8432 /* Set symtab language to language from DW_AT_language. If the
8433 compilation is from a C file generated by language preprocessors, do
8434 not set the language if it was already deduced by start_subfile. */
8435 if (!(cu
->lang () == language_c
8436 && cust
->primary_filetab ()->language () != language_unknown
))
8437 cust
->primary_filetab ()->set_language (cu
->lang ());
8439 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8440 produce DW_AT_location with location lists but it can be possibly
8441 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8442 there were bugs in prologue debug info, fixed later in GCC-4.5
8443 by "unwind info for epilogues" patch (which is not directly related).
8445 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8446 needed, it would be wrong due to missing DW_AT_producer there.
8448 Still one can confuse GDB by using non-standard GCC compilation
8449 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8451 if (cu
->has_loclist
&& gcc_4_minor
>= 5)
8452 cust
->set_locations_valid (true);
8454 if (gcc_4_minor
>= 5)
8455 cust
->set_epilogue_unwind_valid (true);
8457 cust
->set_call_site_htab (cu
->call_site_htab
);
8460 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8462 /* Push it for inclusion processing later. */
8463 per_objfile
->per_bfd
->just_read_cus
.push_back (cu
->per_cu
);
8465 /* Not needed any more. */
8466 cu
->reset_builder ();
8469 /* Generate full symbol information for type unit CU, whose DIEs have
8470 already been loaded into memory. */
8473 process_full_type_unit (dwarf2_cu
*cu
,
8474 enum language pretend_language
)
8476 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8477 struct compunit_symtab
*cust
;
8478 struct signatured_type
*sig_type
;
8480 gdb_assert (cu
->per_cu
->is_debug_types
);
8481 sig_type
= (struct signatured_type
*) cu
->per_cu
;
8483 /* Clear the list here in case something was left over. */
8484 cu
->method_list
.clear ();
8486 /* The symbol tables are set up in read_type_unit_scope. */
8487 process_die (cu
->dies
, cu
);
8489 /* For now fudge the Go package. */
8490 if (cu
->lang () == language_go
)
8491 fixup_go_packaging (cu
);
8493 /* Now that we have processed all the DIEs in the CU, all the types
8494 should be complete, and it should now be safe to compute all of the
8496 compute_delayed_physnames (cu
);
8498 if (cu
->lang () == language_rust
)
8499 rust_union_quirks (cu
);
8501 /* TUs share symbol tables.
8502 If this is the first TU to use this symtab, complete the construction
8503 of it with end_expandable_symtab. Otherwise, complete the addition of
8504 this TU's symbols to the existing symtab. */
8505 type_unit_group_unshareable
*tug_unshare
=
8506 per_objfile
->get_type_unit_group_unshareable (sig_type
->type_unit_group
);
8507 if (tug_unshare
->compunit_symtab
== NULL
)
8509 buildsym_compunit
*builder
= cu
->get_builder ();
8510 cust
= builder
->end_expandable_symtab (0);
8511 tug_unshare
->compunit_symtab
= cust
;
8515 /* Set symtab language to language from DW_AT_language. If the
8516 compilation is from a C file generated by language preprocessors,
8517 do not set the language if it was already deduced by
8519 if (!(cu
->lang () == language_c
8520 && cust
->primary_filetab ()->language () != language_c
))
8521 cust
->primary_filetab ()->set_language (cu
->lang ());
8526 cu
->get_builder ()->augment_type_symtab ();
8527 cust
= tug_unshare
->compunit_symtab
;
8530 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8532 /* Not needed any more. */
8533 cu
->reset_builder ();
8536 /* Process an imported unit DIE. */
8539 process_imported_unit_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8541 struct attribute
*attr
;
8543 /* For now we don't handle imported units in type units. */
8544 if (cu
->per_cu
->is_debug_types
)
8546 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8547 " supported in type units [in module %s]"),
8548 objfile_name (cu
->per_objfile
->objfile
));
8551 attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
8554 sect_offset sect_off
= attr
->get_ref_die_offset ();
8555 bool is_dwz
= (attr
->form
== DW_FORM_GNU_ref_alt
|| cu
->per_cu
->is_dwz
);
8556 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8557 dwarf2_per_cu_data
*per_cu
8558 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
8559 per_objfile
->per_bfd
);
8561 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8562 into another compilation unit, at root level. Regard this as a hint,
8563 and ignore it. This is a best effort, it only works if unit_type and
8564 lang are already set. */
8565 if (die
->parent
&& die
->parent
->parent
== NULL
8566 && per_cu
->unit_type (false) == DW_UT_compile
8567 && per_cu
->lang (false) == language_cplus
)
8570 /* If necessary, add it to the queue and load its DIEs. */
8571 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
8573 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
8574 false, cu
->lang ());
8576 cu
->per_cu
->imported_symtabs_push (per_cu
);
8580 /* RAII object that represents a process_die scope: i.e.,
8581 starts/finishes processing a DIE. */
8582 class process_die_scope
8585 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
8586 : m_die (die
), m_cu (cu
)
8588 /* We should only be processing DIEs not already in process. */
8589 gdb_assert (!m_die
->in_process
);
8590 m_die
->in_process
= true;
8593 ~process_die_scope ()
8595 m_die
->in_process
= false;
8597 /* If we're done processing the DIE for the CU that owns the line
8598 header, we don't need the line header anymore. */
8599 if (m_cu
->line_header_die_owner
== m_die
)
8601 delete m_cu
->line_header
;
8602 m_cu
->line_header
= NULL
;
8603 m_cu
->line_header_die_owner
= NULL
;
8612 /* Process a die and its children. */
8615 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8617 process_die_scope
scope (die
, cu
);
8621 case DW_TAG_padding
:
8623 case DW_TAG_compile_unit
:
8624 case DW_TAG_partial_unit
:
8625 read_file_scope (die
, cu
);
8627 case DW_TAG_type_unit
:
8628 read_type_unit_scope (die
, cu
);
8630 case DW_TAG_subprogram
:
8631 /* Nested subprograms in Fortran get a prefix. */
8632 if (cu
->lang () == language_fortran
8633 && die
->parent
!= NULL
8634 && die
->parent
->tag
== DW_TAG_subprogram
)
8635 cu
->processing_has_namespace_info
= true;
8637 case DW_TAG_inlined_subroutine
:
8638 read_func_scope (die
, cu
);
8640 case DW_TAG_lexical_block
:
8641 case DW_TAG_try_block
:
8642 case DW_TAG_catch_block
:
8643 read_lexical_block_scope (die
, cu
);
8645 case DW_TAG_call_site
:
8646 case DW_TAG_GNU_call_site
:
8647 read_call_site_scope (die
, cu
);
8649 case DW_TAG_class_type
:
8650 case DW_TAG_interface_type
:
8651 case DW_TAG_structure_type
:
8652 case DW_TAG_union_type
:
8653 case DW_TAG_namelist
:
8654 process_structure_scope (die
, cu
);
8656 case DW_TAG_enumeration_type
:
8657 process_enumeration_scope (die
, cu
);
8660 /* These dies have a type, but processing them does not create
8661 a symbol or recurse to process the children. Therefore we can
8662 read them on-demand through read_type_die. */
8663 case DW_TAG_subroutine_type
:
8664 case DW_TAG_set_type
:
8665 case DW_TAG_pointer_type
:
8666 case DW_TAG_ptr_to_member_type
:
8667 case DW_TAG_reference_type
:
8668 case DW_TAG_rvalue_reference_type
:
8669 case DW_TAG_string_type
:
8672 case DW_TAG_array_type
:
8673 /* We only need to handle this case for Ada -- in other
8674 languages, it's normal for the compiler to emit a typedef
8676 if (cu
->lang () != language_ada
)
8679 case DW_TAG_base_type
:
8680 case DW_TAG_subrange_type
:
8681 case DW_TAG_generic_subrange
:
8682 case DW_TAG_typedef
:
8683 /* Add a typedef symbol for the type definition, if it has a
8685 new_symbol (die
, read_type_die (die
, cu
), cu
);
8687 case DW_TAG_common_block
:
8688 read_common_block (die
, cu
);
8690 case DW_TAG_common_inclusion
:
8692 case DW_TAG_namespace
:
8693 cu
->processing_has_namespace_info
= true;
8694 read_namespace (die
, cu
);
8697 cu
->processing_has_namespace_info
= true;
8698 read_module (die
, cu
);
8700 case DW_TAG_imported_declaration
:
8701 cu
->processing_has_namespace_info
= true;
8702 if (read_alias (die
, cu
))
8704 /* The declaration is neither a global namespace nor a variable
8707 case DW_TAG_imported_module
:
8708 cu
->processing_has_namespace_info
= true;
8709 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
8710 || cu
->lang () != language_fortran
))
8711 complaint (_("Tag '%s' has unexpected children"),
8712 dwarf_tag_name (die
->tag
));
8713 read_import_statement (die
, cu
);
8716 case DW_TAG_imported_unit
:
8717 process_imported_unit_die (die
, cu
);
8720 case DW_TAG_variable
:
8721 read_variable (die
, cu
);
8725 new_symbol (die
, NULL
, cu
);
8730 /* DWARF name computation. */
8732 /* A helper function for dwarf2_compute_name which determines whether DIE
8733 needs to have the name of the scope prepended to the name listed in the
8737 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
8739 struct attribute
*attr
;
8743 case DW_TAG_namespace
:
8744 case DW_TAG_typedef
:
8745 case DW_TAG_class_type
:
8746 case DW_TAG_interface_type
:
8747 case DW_TAG_structure_type
:
8748 case DW_TAG_union_type
:
8749 case DW_TAG_enumeration_type
:
8750 case DW_TAG_enumerator
:
8751 case DW_TAG_subprogram
:
8752 case DW_TAG_inlined_subroutine
:
8754 case DW_TAG_imported_declaration
:
8757 case DW_TAG_variable
:
8758 case DW_TAG_constant
:
8759 /* We only need to prefix "globally" visible variables. These include
8760 any variable marked with DW_AT_external or any variable that
8761 lives in a namespace. [Variables in anonymous namespaces
8762 require prefixing, but they are not DW_AT_external.] */
8764 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
8766 struct dwarf2_cu
*spec_cu
= cu
;
8768 return die_needs_namespace (die_specification (die
, &spec_cu
),
8772 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
8773 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
8774 && die
->parent
->tag
!= DW_TAG_module
)
8776 /* A variable in a lexical block of some kind does not need a
8777 namespace, even though in C++ such variables may be external
8778 and have a mangled name. */
8779 if (die
->parent
->tag
== DW_TAG_lexical_block
8780 || die
->parent
->tag
== DW_TAG_try_block
8781 || die
->parent
->tag
== DW_TAG_catch_block
8782 || die
->parent
->tag
== DW_TAG_subprogram
)
8791 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8792 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8793 defined for the given DIE. */
8795 static struct attribute
*
8796 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
8798 struct attribute
*attr
;
8800 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
8802 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8807 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8808 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8809 defined for the given DIE. */
8812 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
8814 const char *linkage_name
;
8816 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
8817 if (linkage_name
== NULL
)
8818 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8820 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8821 See https://github.com/rust-lang/rust/issues/32925. */
8822 if (cu
->lang () == language_rust
&& linkage_name
!= NULL
8823 && strchr (linkage_name
, '{') != NULL
)
8824 linkage_name
= NULL
;
8826 return linkage_name
;
8829 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8830 compute the physname for the object, which include a method's:
8831 - formal parameters (C++),
8832 - receiver type (Go),
8834 The term "physname" is a bit confusing.
8835 For C++, for example, it is the demangled name.
8836 For Go, for example, it's the mangled name.
8838 For Ada, return the DIE's linkage name rather than the fully qualified
8839 name. PHYSNAME is ignored..
8841 The result is allocated on the objfile->per_bfd's obstack and
8845 dwarf2_compute_name (const char *name
,
8846 struct die_info
*die
, struct dwarf2_cu
*cu
,
8849 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
8852 name
= dwarf2_name (die
, cu
);
8854 enum language lang
= cu
->lang ();
8856 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8857 but otherwise compute it by typename_concat inside GDB.
8858 FIXME: Actually this is not really true, or at least not always true.
8859 It's all very confusing. compute_and_set_names doesn't try to demangle
8860 Fortran names because there is no mangling standard. So new_symbol
8861 will set the demangled name to the result of dwarf2_full_name, and it is
8862 the demangled name that GDB uses if it exists. */
8863 if (lang
== language_ada
8864 || (lang
== language_fortran
&& physname
))
8866 /* For Ada unit, we prefer the linkage name over the name, as
8867 the former contains the exported name, which the user expects
8868 to be able to reference. Ideally, we want the user to be able
8869 to reference this entity using either natural or linkage name,
8870 but we haven't started looking at this enhancement yet. */
8871 const char *linkage_name
= dw2_linkage_name (die
, cu
);
8873 if (linkage_name
!= NULL
)
8874 return linkage_name
;
8877 /* These are the only languages we know how to qualify names in. */
8879 && (lang
== language_cplus
8880 || lang
== language_fortran
|| lang
== language_d
8881 || lang
== language_rust
))
8883 if (die_needs_namespace (die
, cu
))
8889 prefix
= determine_prefix (die
, cu
);
8890 if (*prefix
!= '\0')
8892 gdb::unique_xmalloc_ptr
<char> prefixed_name
8893 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
8895 buf
.puts (prefixed_name
.get ());
8900 /* Template parameters may be specified in the DIE's DW_AT_name, or
8901 as children with DW_TAG_template_type_param or
8902 DW_TAG_value_type_param. If the latter, add them to the name
8903 here. If the name already has template parameters, then
8904 skip this step; some versions of GCC emit both, and
8905 it is more efficient to use the pre-computed name.
8907 Something to keep in mind about this process: it is very
8908 unlikely, or in some cases downright impossible, to produce
8909 something that will match the mangled name of a function.
8910 If the definition of the function has the same debug info,
8911 we should be able to match up with it anyway. But fallbacks
8912 using the minimal symbol, for instance to find a method
8913 implemented in a stripped copy of libstdc++, will not work.
8914 If we do not have debug info for the definition, we will have to
8915 match them up some other way.
8917 When we do name matching there is a related problem with function
8918 templates; two instantiated function templates are allowed to
8919 differ only by their return types, which we do not add here. */
8921 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
8923 struct attribute
*attr
;
8924 struct die_info
*child
;
8927 die
->building_fullname
= 1;
8929 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
8933 const gdb_byte
*bytes
;
8934 struct dwarf2_locexpr_baton
*baton
;
8937 if (child
->tag
!= DW_TAG_template_type_param
8938 && child
->tag
!= DW_TAG_template_value_param
)
8949 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
8952 complaint (_("template parameter missing DW_AT_type"));
8953 buf
.puts ("UNKNOWN_TYPE");
8956 type
= die_type (child
, cu
);
8958 if (child
->tag
== DW_TAG_template_type_param
)
8960 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
8961 &type_print_raw_options
);
8965 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
8968 complaint (_("template parameter missing "
8969 "DW_AT_const_value"));
8970 buf
.puts ("UNKNOWN_VALUE");
8974 dwarf2_const_value_attr (attr
, type
, name
,
8975 &cu
->comp_unit_obstack
, cu
,
8976 &value
, &bytes
, &baton
);
8978 if (type
->has_no_signedness ())
8979 /* GDB prints characters as NUMBER 'CHAR'. If that's
8980 changed, this can use value_print instead. */
8981 cu
->language_defn
->printchar (value
, type
, &buf
);
8984 struct value_print_options opts
;
8987 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
8991 baton
->per_objfile
);
8992 else if (bytes
!= NULL
)
8994 v
= value::allocate (type
);
8995 memcpy (v
->contents_writeable ().data (), bytes
,
8999 v
= value_from_longest (type
, value
);
9001 /* Specify decimal so that we do not depend on
9003 get_formatted_print_options (&opts
, 'd');
9005 value_print (v
, &buf
, &opts
);
9010 die
->building_fullname
= 0;
9014 /* Close the argument list, with a space if necessary
9015 (nested templates). */
9016 if (!buf
.empty () && buf
.string ().back () == '>')
9023 /* For C++ methods, append formal parameter type
9024 information, if PHYSNAME. */
9026 if (physname
&& die
->tag
== DW_TAG_subprogram
9027 && lang
== language_cplus
)
9029 struct type
*type
= read_type_die (die
, cu
);
9031 c_type_print_args (type
, &buf
, 1, lang
,
9032 &type_print_raw_options
);
9034 if (lang
== language_cplus
)
9036 /* Assume that an artificial first parameter is
9037 "this", but do not crash if it is not. RealView
9038 marks unnamed (and thus unused) parameters as
9039 artificial; there is no way to differentiate
9041 if (type
->num_fields () > 0
9042 && TYPE_FIELD_ARTIFICIAL (type
, 0)
9043 && type
->field (0).type ()->code () == TYPE_CODE_PTR
9044 && TYPE_CONST (type
->field (0).type ()->target_type ()))
9045 buf
.puts (" const");
9049 const std::string
&intermediate_name
= buf
.string ();
9051 const char *canonical_name
9052 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
9055 /* If we only computed INTERMEDIATE_NAME, or if
9056 INTERMEDIATE_NAME is already canonical, then we need to
9058 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
9059 name
= objfile
->intern (intermediate_name
);
9061 name
= canonical_name
;
9068 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9069 If scope qualifiers are appropriate they will be added. The result
9070 will be allocated on the storage_obstack, or NULL if the DIE does
9071 not have a name. NAME may either be from a previous call to
9072 dwarf2_name or NULL.
9074 The output string will be canonicalized (if C++). */
9077 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9079 return dwarf2_compute_name (name
, die
, cu
, 0);
9082 /* Construct a physname for the given DIE in CU. NAME may either be
9083 from a previous call to dwarf2_name or NULL. The result will be
9084 allocated on the objfile_objstack or NULL if the DIE does not have a
9087 The output string will be canonicalized (if C++). */
9090 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9092 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9093 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
9096 /* In this case dwarf2_compute_name is just a shortcut not building anything
9098 if (!die_needs_namespace (die
, cu
))
9099 return dwarf2_compute_name (name
, die
, cu
, 1);
9101 if (cu
->lang () != language_rust
)
9102 mangled
= dw2_linkage_name (die
, cu
);
9104 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9106 gdb::unique_xmalloc_ptr
<char> demangled
;
9107 if (mangled
!= NULL
)
9109 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
9111 /* Do nothing (do not demangle the symbol name). */
9115 /* Use DMGL_RET_DROP for C++ template functions to suppress
9116 their return type. It is easier for GDB users to search
9117 for such functions as `name(params)' than `long name(params)'.
9118 In such case the minimal symbol names do not match the full
9119 symbol names but for template functions there is never a need
9120 to look up their definition from their declaration so
9121 the only disadvantage remains the minimal symbol variant
9122 `long name(params)' does not have the proper inferior type. */
9123 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
9127 canon
= demangled
.get ();
9135 if (canon
== NULL
|| check_physname
)
9137 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
9139 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
9141 /* It may not mean a bug in GDB. The compiler could also
9142 compute DW_AT_linkage_name incorrectly. But in such case
9143 GDB would need to be bug-to-bug compatible. */
9145 complaint (_("Computed physname <%s> does not match demangled <%s> "
9146 "(from linkage <%s>) - DIE at %s [in module %s]"),
9147 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
9148 objfile_name (objfile
));
9150 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9151 is available here - over computed PHYSNAME. It is safer
9152 against both buggy GDB and buggy compilers. */
9166 retval
= objfile
->intern (retval
);
9171 /* Inspect DIE in CU for a namespace alias or a variable with alias
9172 attribute. If one exists, record a new symbol for it.
9174 Returns true if an alias was recorded, false otherwise. */
9177 read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
9179 struct attribute
*attr
;
9181 /* If the die does not have a name, this is neither a namespace
9182 alias nor a variable alias. */
9183 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
9187 struct die_info
*d
= die
;
9188 struct dwarf2_cu
*imported_cu
= cu
;
9190 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9191 keep inspecting DIEs until we hit the underlying import. */
9192 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9193 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
9195 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
9199 d
= follow_die_ref (d
, attr
, &imported_cu
);
9200 if (d
->tag
!= DW_TAG_imported_declaration
)
9204 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
9206 complaint (_("DIE at %s has too many recursively imported "
9207 "declarations"), sect_offset_str (d
->sect_off
));
9214 if (d
->tag
== DW_TAG_variable
)
9216 /* This declaration is a C/C++ global variable alias.
9217 Add a symbol for it whose type is the same as the
9218 aliased variable's. */
9219 type
= die_type (d
, imported_cu
);
9220 struct symbol
*sym
= new_symbol (die
, type
, cu
);
9221 attr
= dwarf2_attr (d
, DW_AT_location
, imported_cu
);
9222 sym
->set_aclass_index (LOC_UNRESOLVED
);
9223 if (attr
!= nullptr)
9224 var_decode_location (attr
, sym
, cu
);
9229 sect_offset sect_off
= attr
->get_ref_die_offset ();
9230 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
,
9232 if (type
!= nullptr && type
->code () == TYPE_CODE_NAMESPACE
)
9234 /* This declaration is a global namespace alias. Add
9235 a symbol for it whose type is the aliased
9237 new_symbol (die
, type
, cu
);
9246 /* Return the using directives repository (global or local?) to use in the
9247 current context for CU.
9249 For Ada, imported declarations can materialize renamings, which *may* be
9250 global. However it is impossible (for now?) in DWARF to distinguish
9251 "external" imported declarations and "static" ones. As all imported
9252 declarations seem to be static in all other languages, make them all CU-wide
9253 global only in Ada. */
9255 static struct using_direct
**
9256 using_directives (struct dwarf2_cu
*cu
)
9258 if (cu
->lang () == language_ada
9259 && cu
->get_builder ()->outermost_context_p ())
9260 return cu
->get_builder ()->get_global_using_directives ();
9262 return cu
->get_builder ()->get_local_using_directives ();
9265 /* Read the DW_ATTR_decl_line attribute for the given DIE in the
9266 given CU. If the format is not recognized or the attribute is
9267 not present, set it to 0. */
9270 read_decl_line (struct die_info
*die
, struct dwarf2_cu
*cu
)
9272 struct attribute
*decl_line
= dwarf2_attr (die
, DW_AT_decl_line
, cu
);
9273 if (decl_line
== nullptr)
9275 if (decl_line
->form_is_constant ())
9277 LONGEST val
= decl_line
->constant_value (0);
9278 if (0 <= val
&& val
<= UINT_MAX
)
9279 return (unsigned int) val
;
9281 complaint (_("Declared line for using directive is too large"));
9285 complaint (_("Declared line for using directive is of incorrect format"));
9289 /* Read the import statement specified by the given die and record it. */
9292 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
9294 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9295 struct attribute
*import_attr
;
9296 struct die_info
*imported_die
, *child_die
;
9297 struct dwarf2_cu
*imported_cu
;
9298 const char *imported_name
;
9299 const char *imported_name_prefix
;
9300 const char *canonical_name
;
9301 const char *import_alias
;
9302 const char *imported_declaration
= NULL
;
9303 const char *import_prefix
;
9304 std::vector
<const char *> excludes
;
9306 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
9307 if (import_attr
== NULL
)
9309 complaint (_("Tag '%s' has no DW_AT_import"),
9310 dwarf_tag_name (die
->tag
));
9315 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
9316 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9317 if (imported_name
== NULL
)
9319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9321 The import in the following code:
9335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9336 <52> DW_AT_decl_file : 1
9337 <53> DW_AT_decl_line : 6
9338 <54> DW_AT_import : <0x75>
9339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9341 <5b> DW_AT_decl_file : 1
9342 <5c> DW_AT_decl_line : 2
9343 <5d> DW_AT_type : <0x6e>
9345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9346 <76> DW_AT_byte_size : 4
9347 <77> DW_AT_encoding : 5 (signed)
9349 imports the wrong die ( 0x75 instead of 0x58 ).
9350 This case will be ignored until the gcc bug is fixed. */
9354 /* Figure out the local name after import. */
9355 import_alias
= dwarf2_name (die
, cu
);
9357 /* Figure out where the statement is being imported to. */
9358 import_prefix
= determine_prefix (die
, cu
);
9360 /* Figure out what the scope of the imported die is and prepend it
9361 to the name of the imported die. */
9362 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
9364 if (imported_die
->tag
!= DW_TAG_namespace
9365 && imported_die
->tag
!= DW_TAG_module
)
9367 imported_declaration
= imported_name
;
9368 canonical_name
= imported_name_prefix
;
9370 else if (strlen (imported_name_prefix
) > 0)
9371 canonical_name
= obconcat (&objfile
->objfile_obstack
,
9372 imported_name_prefix
,
9373 (cu
->lang () == language_d
9376 imported_name
, (char *) NULL
);
9378 canonical_name
= imported_name
;
9380 if (die
->tag
== DW_TAG_imported_module
9381 && cu
->lang () == language_fortran
)
9382 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
9383 child_die
= child_die
->sibling
)
9385 /* DWARF-4: A Fortran use statement with a “rename list” may be
9386 represented by an imported module entry with an import attribute
9387 referring to the module and owned entries corresponding to those
9388 entities that are renamed as part of being imported. */
9390 if (child_die
->tag
!= DW_TAG_imported_declaration
)
9392 complaint (_("child DW_TAG_imported_declaration expected "
9393 "- DIE at %s [in module %s]"),
9394 sect_offset_str (child_die
->sect_off
),
9395 objfile_name (objfile
));
9399 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
9400 if (import_attr
== NULL
)
9402 complaint (_("Tag '%s' has no DW_AT_import"),
9403 dwarf_tag_name (child_die
->tag
));
9408 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
9410 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9411 if (imported_name
== NULL
)
9413 complaint (_("child DW_TAG_imported_declaration has unknown "
9414 "imported name - DIE at %s [in module %s]"),
9415 sect_offset_str (child_die
->sect_off
),
9416 objfile_name (objfile
));
9420 excludes
.push_back (imported_name
);
9422 process_die (child_die
, cu
);
9425 add_using_directive (using_directives (cu
),
9429 imported_declaration
,
9431 read_decl_line (die
, cu
),
9433 &objfile
->objfile_obstack
);
9436 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9437 types, but gives them a size of zero. Starting with version 14,
9438 ICC is compatible with GCC. */
9441 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
9443 if (!cu
->checked_producer
)
9444 check_producer (cu
);
9446 return cu
->producer_is_icc_lt_14
;
9449 /* ICC generates a DW_AT_type for C void functions. This was observed on
9450 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9451 which says that void functions should not have a DW_AT_type. */
9454 producer_is_icc (struct dwarf2_cu
*cu
)
9456 if (!cu
->checked_producer
)
9457 check_producer (cu
);
9459 return cu
->producer_is_icc
;
9462 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9463 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9464 this, it was first present in GCC release 4.3.0. */
9467 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
9469 if (!cu
->checked_producer
)
9470 check_producer (cu
);
9472 return cu
->producer_is_gcc_lt_4_3
;
9475 /* See dwarf2/read.h. */
9477 producer_is_clang (struct dwarf2_cu
*cu
)
9479 if (!cu
->checked_producer
)
9480 check_producer (cu
);
9482 return cu
->producer_is_clang
;
9485 static file_and_directory
&
9486 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
9488 if (cu
->per_cu
->fnd
!= nullptr)
9489 return *cu
->per_cu
->fnd
;
9491 /* Find the filename. Do not use dwarf2_name here, since the filename
9492 is not a source language identifier. */
9493 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
9494 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
9496 if (res
.get_comp_dir () == nullptr
9497 && producer_is_gcc_lt_4_3 (cu
)
9498 && res
.get_name () != nullptr
9499 && IS_ABSOLUTE_PATH (res
.get_name ()))
9501 res
.set_comp_dir (ldirname (res
.get_name ()));
9502 res
.set_name (make_unique_xstrdup (lbasename (res
.get_name ())));
9505 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
9506 return *cu
->per_cu
->fnd
;
9509 /* Handle DW_AT_stmt_list for a compilation unit.
9510 DIE is the DW_TAG_compile_unit die for CU.
9511 COMP_DIR is the compilation directory. LOWPC is passed to
9512 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9515 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
9516 const file_and_directory
&fnd
, CORE_ADDR lowpc
,
9517 bool have_code
) /* ARI: editCase function */
9519 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9520 struct attribute
*attr
;
9521 hashval_t line_header_local_hash
;
9525 gdb_assert (! cu
->per_cu
->is_debug_types
);
9527 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
9528 if (attr
== NULL
|| !attr
->form_is_unsigned ())
9531 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9533 /* The line header hash table is only created if needed (it exists to
9534 prevent redundant reading of the line table for partial_units).
9535 If we're given a partial_unit, we'll need it. If we're given a
9536 compile_unit, then use the line header hash table if it's already
9537 created, but don't create one just yet. */
9539 if (per_objfile
->line_header_hash
== NULL
9540 && die
->tag
== DW_TAG_partial_unit
)
9542 per_objfile
->line_header_hash
9543 .reset (htab_create_alloc (127, line_header_hash_voidp
,
9544 line_header_eq_voidp
,
9545 htab_delete_entry
<line_header
>,
9549 line_header
line_header_local (line_offset
, cu
->per_cu
->is_dwz
);
9550 line_header_local_hash
= line_header_hash (&line_header_local
);
9551 if (per_objfile
->line_header_hash
!= NULL
)
9553 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9555 line_header_local_hash
, NO_INSERT
);
9557 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9558 is not present in *SLOT (since if there is something in *SLOT then
9559 it will be for a partial_unit). */
9560 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
9562 gdb_assert (*slot
!= NULL
);
9563 cu
->line_header
= (struct line_header
*) *slot
;
9568 /* dwarf_decode_line_header does not yet provide sufficient information.
9569 We always have to call also dwarf_decode_lines for it. */
9570 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
,
9571 fnd
.get_comp_dir ());
9575 cu
->line_header
= lh
.release ();
9576 cu
->line_header_die_owner
= die
;
9578 if (per_objfile
->line_header_hash
== NULL
)
9582 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9584 line_header_local_hash
, INSERT
);
9585 gdb_assert (slot
!= NULL
);
9587 if (slot
!= NULL
&& *slot
== NULL
)
9589 /* This newly decoded line number information unit will be owned
9590 by line_header_hash hash table. */
9591 *slot
= cu
->line_header
;
9592 cu
->line_header_die_owner
= NULL
;
9596 /* We cannot free any current entry in (*slot) as that struct line_header
9597 may be already used by multiple CUs. Create only temporary decoded
9598 line_header for this CU - it may happen at most once for each line
9599 number information unit. And if we're not using line_header_hash
9600 then this is what we want as well. */
9601 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
9603 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
9604 /* The have_code check is here because, if LOWPC and HIGHPC are both 0x0,
9605 then there won't be any interesting code in the CU, but a check later on
9606 (in lnp_state_machine::check_line_address) will fail to properly exclude
9607 an entry that was removed via --gc-sections. */
9609 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
);
9612 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9615 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9617 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9618 struct objfile
*objfile
= per_objfile
->objfile
;
9619 struct gdbarch
*gdbarch
= objfile
->arch ();
9620 CORE_ADDR lowpc
= ((CORE_ADDR
) -1);
9621 CORE_ADDR highpc
= ((CORE_ADDR
) 0);
9622 struct attribute
*attr
;
9623 struct die_info
*child_die
;
9626 prepare_one_comp_unit (cu
, die
, cu
->lang ());
9627 baseaddr
= objfile
->text_section_offset ();
9629 get_scope_pc_bounds (die
, &lowpc
, &highpc
, cu
);
9631 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9632 from finish_block. */
9633 if (lowpc
== ((CORE_ADDR
) -1))
9635 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
9637 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
9639 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
9642 gdb_assert (per_objfile
->sym_cu
== nullptr);
9643 scoped_restore restore_sym_cu
9644 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
9646 /* Decode line number information if present. We do this before
9647 processing child DIEs, so that the line header table is available
9648 for DW_AT_decl_file. */
9649 handle_DW_AT_stmt_list (die
, cu
, fnd
, lowpc
, lowpc
!= highpc
);
9651 /* Process all dies in compilation unit. */
9652 if (die
->child
!= NULL
)
9654 child_die
= die
->child
;
9655 while (child_die
&& child_die
->tag
)
9657 process_die (child_die
, cu
);
9658 child_die
= child_die
->sibling
;
9661 per_objfile
->sym_cu
= nullptr;
9663 /* Decode macro information, if present. Dwarf 2 macro information
9664 refers to information in the line number info statement program
9665 header, so we can only read it if we've read the header
9667 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
9669 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
9670 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9672 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
9673 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9675 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
9679 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
9680 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9682 unsigned int macro_offset
= attr
->as_unsigned ();
9684 dwarf_decode_macros (cu
, macro_offset
, 0);
9690 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
9692 struct type_unit_group
*tu_group
;
9694 struct attribute
*attr
;
9696 struct signatured_type
*sig_type
;
9698 gdb_assert (per_cu
->is_debug_types
);
9699 sig_type
= (struct signatured_type
*) per_cu
;
9701 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
9703 /* If we're using .gdb_index (includes -readnow) then
9704 per_cu->type_unit_group may not have been set up yet. */
9705 if (sig_type
->type_unit_group
== NULL
)
9706 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
9707 tu_group
= sig_type
->type_unit_group
;
9709 /* If we've already processed this stmt_list there's no real need to
9710 do it again, we could fake it and just recreate the part we need
9711 (file name,index -> symtab mapping). If data shows this optimization
9712 is useful we can do it then. */
9713 type_unit_group_unshareable
*tug_unshare
9714 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
9715 first_time
= tug_unshare
->compunit_symtab
== NULL
;
9717 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9720 if (attr
!= NULL
&& attr
->form_is_unsigned ())
9722 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9723 lh
= dwarf_decode_line_header (line_offset
, this, nullptr);
9728 start_compunit_symtab ("", NULL
, 0);
9731 gdb_assert (tug_unshare
->symtabs
== NULL
);
9732 gdb_assert (m_builder
== nullptr);
9733 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9734 m_builder
.reset (new struct buildsym_compunit
9735 (cust
->objfile (), "",
9739 list_in_scope
= get_builder ()->get_file_symbols ();
9744 line_header
= lh
.release ();
9745 line_header_die_owner
= die
;
9749 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
9751 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9752 still initializing it, and our caller (a few levels up)
9753 process_full_type_unit still needs to know if this is the first
9756 tug_unshare
->symtabs
9757 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
9758 struct symtab
*, line_header
->file_names_size ());
9760 auto &file_names
= line_header
->file_names ();
9761 for (i
= 0; i
< file_names
.size (); ++i
)
9763 file_entry
&fe
= file_names
[i
];
9764 dwarf2_start_subfile (this, fe
, *line_header
);
9765 buildsym_compunit
*b
= get_builder ();
9766 subfile
*sf
= b
->get_current_subfile ();
9768 if (sf
->symtab
== nullptr)
9770 /* NOTE: start_subfile will recognize when it's been
9771 passed a file it has already seen. So we can't
9772 assume there's a simple mapping from
9773 cu->line_header->file_names to subfiles, plus
9774 cu->line_header->file_names may contain dups. */
9775 const char *name
= sf
->name
.c_str ();
9776 const char *name_for_id
= sf
->name_for_id
.c_str ();
9777 sf
->symtab
= allocate_symtab (cust
, name
, name_for_id
);
9780 fe
.symtab
= b
->get_current_subfile ()->symtab
;
9781 tug_unshare
->symtabs
[i
] = fe
.symtab
;
9786 gdb_assert (m_builder
== nullptr);
9787 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9788 m_builder
.reset (new struct buildsym_compunit
9789 (cust
->objfile (), "",
9793 list_in_scope
= get_builder ()->get_file_symbols ();
9795 auto &file_names
= line_header
->file_names ();
9796 for (i
= 0; i
< file_names
.size (); ++i
)
9798 file_entry
&fe
= file_names
[i
];
9799 fe
.symtab
= tug_unshare
->symtabs
[i
];
9803 /* The main symtab is allocated last. Type units don't have DW_AT_name
9804 so they don't have a "real" (so to speak) symtab anyway.
9805 There is later code that will assign the main symtab to all symbols
9806 that don't have one. We need to handle the case of a symbol with a
9807 missing symtab (DW_AT_decl_file) anyway. */
9810 /* Process DW_TAG_type_unit.
9811 For TUs we want to skip the first top level sibling if it's not the
9812 actual type being defined by this TU. In this case the first top
9813 level sibling is there to provide context only. */
9816 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9818 struct die_info
*child_die
;
9820 prepare_one_comp_unit (cu
, die
, language_minimal
);
9822 /* Initialize (or reinitialize) the machinery for building symtabs.
9823 We do this before processing child DIEs, so that the line header table
9824 is available for DW_AT_decl_file. */
9825 cu
->setup_type_unit_groups (die
);
9827 if (die
->child
!= NULL
)
9829 child_die
= die
->child
;
9830 while (child_die
&& child_die
->tag
)
9832 process_die (child_die
, cu
);
9833 child_die
= child_die
->sibling
;
9840 http://gcc.gnu.org/wiki/DebugFission
9841 http://gcc.gnu.org/wiki/DebugFissionDWP
9843 To simplify handling of both DWO files ("object" files with the DWARF info)
9844 and DWP files (a file with the DWOs packaged up into one file), we treat
9845 DWP files as having a collection of virtual DWO files. */
9848 hash_dwo_file (const void *item
)
9850 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
9853 hash
= htab_hash_string (dwo_file
->dwo_name
);
9854 if (dwo_file
->comp_dir
!= NULL
)
9855 hash
+= htab_hash_string (dwo_file
->comp_dir
);
9860 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
9862 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
9863 const struct dwo_file
*rhs
= (const struct dwo_file
*) item_rhs
;
9865 if (strcmp (lhs
->dwo_name
, rhs
->dwo_name
) != 0)
9867 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
9868 return lhs
->comp_dir
== rhs
->comp_dir
;
9869 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
9872 /* Allocate a hash table for DWO files. */
9875 allocate_dwo_file_hash_table ()
9877 return htab_up (htab_create_alloc (41,
9880 htab_delete_entry
<dwo_file
>,
9884 /* Lookup DWO file DWO_NAME. */
9887 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
9888 const char *dwo_name
,
9889 const char *comp_dir
)
9891 struct dwo_file find_entry
;
9894 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
9895 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
9897 find_entry
.dwo_name
= dwo_name
;
9898 find_entry
.comp_dir
= comp_dir
;
9899 slot
= htab_find_slot (per_objfile
->per_bfd
->dwo_files
.get (), &find_entry
,
9906 hash_dwo_unit (const void *item
)
9908 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9910 /* This drops the top 32 bits of the id, but is ok for a hash. */
9911 return dwo_unit
->signature
;
9915 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
9917 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
9918 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
9920 /* The signature is assumed to be unique within the DWO file.
9921 So while object file CU dwo_id's always have the value zero,
9922 that's OK, assuming each object file DWO file has only one CU,
9923 and that's the rule for now. */
9924 return lhs
->signature
== rhs
->signature
;
9927 /* Allocate a hash table for DWO CUs,TUs.
9928 There is one of these tables for each of CUs,TUs for each DWO file. */
9931 allocate_dwo_unit_table ()
9933 /* Start out with a pretty small number.
9934 Generally DWO files contain only one CU and maybe some TUs. */
9935 return htab_up (htab_create_alloc (3,
9938 NULL
, xcalloc
, xfree
));
9941 /* die_reader_func for create_dwo_cu. */
9944 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
9945 const gdb_byte
*info_ptr
,
9946 struct die_info
*comp_unit_die
,
9947 struct dwo_file
*dwo_file
,
9948 struct dwo_unit
*dwo_unit
)
9950 struct dwarf2_cu
*cu
= reader
->cu
;
9951 sect_offset sect_off
= cu
->per_cu
->sect_off
;
9952 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
9954 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
9955 if (!signature
.has_value ())
9957 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9958 " its dwo_id [in module %s]"),
9959 sect_offset_str (sect_off
), dwo_file
->dwo_name
);
9963 dwo_unit
->dwo_file
= dwo_file
;
9964 dwo_unit
->signature
= *signature
;
9965 dwo_unit
->section
= section
;
9966 dwo_unit
->sect_off
= sect_off
;
9967 dwo_unit
->length
= cu
->per_cu
->length ();
9969 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9970 sect_offset_str (sect_off
),
9971 hex_string (dwo_unit
->signature
));
9974 /* Create the dwo_units for the CUs in a DWO_FILE.
9975 Note: This function processes DWO files only, not DWP files. */
9978 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
9979 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
9980 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
9982 struct objfile
*objfile
= per_objfile
->objfile
;
9983 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
9984 const gdb_byte
*info_ptr
, *end_ptr
;
9986 section
.read (objfile
);
9987 info_ptr
= section
.buffer
;
9989 if (info_ptr
== NULL
)
9992 dwarf_read_debug_printf ("Reading %s for %s:",
9993 section
.get_name (),
9994 section
.get_file_name ());
9996 end_ptr
= info_ptr
+ section
.size
;
9997 while (info_ptr
< end_ptr
)
9999 struct dwarf2_per_cu_data per_cu
;
10000 struct dwo_unit read_unit
{};
10001 struct dwo_unit
*dwo_unit
;
10003 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
10005 per_cu
.per_bfd
= per_bfd
;
10006 per_cu
.is_debug_types
= 0;
10007 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
10008 per_cu
.section
= §ion
;
10010 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
10011 if (!reader
.dummy_p
)
10012 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
10013 &dwo_file
, &read_unit
);
10014 info_ptr
+= per_cu
.length ();
10016 // If the unit could not be parsed, skip it.
10017 if (read_unit
.dwo_file
== NULL
)
10020 if (cus_htab
== NULL
)
10021 cus_htab
= allocate_dwo_unit_table ();
10023 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
10025 *dwo_unit
= read_unit
;
10026 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
10027 gdb_assert (slot
!= NULL
);
10030 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
10031 sect_offset dup_sect_off
= dup_cu
->sect_off
;
10033 complaint (_("debug cu entry at offset %s is duplicate to"
10034 " the entry at offset %s, signature %s"),
10035 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
10036 hex_string (dwo_unit
->signature
));
10038 *slot
= (void *)dwo_unit
;
10042 /* DWP file .debug_{cu,tu}_index section format:
10043 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
10044 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
10046 DWP Versions 1 & 2 are older, pre-standard format versions. The first
10047 officially standard DWP format was published with DWARF v5 and is called
10048 Version 5. There are no versions 3 or 4.
10052 Both index sections have the same format, and serve to map a 64-bit
10053 signature to a set of section numbers. Each section begins with a header,
10054 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
10055 indexes, and a pool of 32-bit section numbers. The index sections will be
10056 aligned at 8-byte boundaries in the file.
10058 The index section header consists of:
10060 V, 32 bit version number
10062 N, 32 bit number of compilation units or type units in the index
10063 M, 32 bit number of slots in the hash table
10065 Numbers are recorded using the byte order of the application binary.
10067 The hash table begins at offset 16 in the section, and consists of an array
10068 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10069 order of the application binary). Unused slots in the hash table are 0.
10070 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10072 The parallel table begins immediately after the hash table
10073 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10074 array of 32-bit indexes (using the byte order of the application binary),
10075 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10076 table contains a 32-bit index into the pool of section numbers. For unused
10077 hash table slots, the corresponding entry in the parallel table will be 0.
10079 The pool of section numbers begins immediately following the hash table
10080 (at offset 16 + 12 * M from the beginning of the section). The pool of
10081 section numbers consists of an array of 32-bit words (using the byte order
10082 of the application binary). Each item in the array is indexed starting
10083 from 0. The hash table entry provides the index of the first section
10084 number in the set. Additional section numbers in the set follow, and the
10085 set is terminated by a 0 entry (section number 0 is not used in ELF).
10087 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10088 section must be the first entry in the set, and the .debug_abbrev.dwo must
10089 be the second entry. Other members of the set may follow in any order.
10093 DWP Versions 2 and 5:
10095 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10096 and the entries in the index tables are now offsets into these sections.
10097 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10100 Index Section Contents:
10102 Hash Table of Signatures dwp_hash_table.hash_table
10103 Parallel Table of Indices dwp_hash_table.unit_table
10104 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10105 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10107 The index section header consists of:
10109 V, 32 bit version number
10110 L, 32 bit number of columns in the table of section offsets
10111 N, 32 bit number of compilation units or type units in the index
10112 M, 32 bit number of slots in the hash table
10114 Numbers are recorded using the byte order of the application binary.
10116 The hash table has the same format as version 1.
10117 The parallel table of indices has the same format as version 1,
10118 except that the entries are origin-1 indices into the table of sections
10119 offsets and the table of section sizes.
10121 The table of offsets begins immediately following the parallel table
10122 (at offset 16 + 12 * M from the beginning of the section). The table is
10123 a two-dimensional array of 32-bit words (using the byte order of the
10124 application binary), with L columns and N+1 rows, in row-major order.
10125 Each row in the array is indexed starting from 0. The first row provides
10126 a key to the remaining rows: each column in this row provides an identifier
10127 for a debug section, and the offsets in the same column of subsequent rows
10128 refer to that section. The section identifiers for Version 2 are:
10130 DW_SECT_INFO 1 .debug_info.dwo
10131 DW_SECT_TYPES 2 .debug_types.dwo
10132 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10133 DW_SECT_LINE 4 .debug_line.dwo
10134 DW_SECT_LOC 5 .debug_loc.dwo
10135 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10136 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10137 DW_SECT_MACRO 8 .debug_macro.dwo
10139 The section identifiers for Version 5 are:
10141 DW_SECT_INFO_V5 1 .debug_info.dwo
10142 DW_SECT_RESERVED_V5 2 --
10143 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10144 DW_SECT_LINE_V5 4 .debug_line.dwo
10145 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10146 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10147 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10148 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10150 The offsets provided by the CU and TU index sections are the base offsets
10151 for the contributions made by each CU or TU to the corresponding section
10152 in the package file. Each CU and TU header contains an abbrev_offset
10153 field, used to find the abbreviations table for that CU or TU within the
10154 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10155 be interpreted as relative to the base offset given in the index section.
10156 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10157 should be interpreted as relative to the base offset for .debug_line.dwo,
10158 and offsets into other debug sections obtained from DWARF attributes should
10159 also be interpreted as relative to the corresponding base offset.
10161 The table of sizes begins immediately following the table of offsets.
10162 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10163 with L columns and N rows, in row-major order. Each row in the array is
10164 indexed starting from 1 (row 0 is shared by the two tables).
10168 Hash table lookup is handled the same in version 1 and 2:
10170 We assume that N and M will not exceed 2^32 - 1.
10171 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10173 Given a 64-bit compilation unit signature or a type signature S, an entry
10174 in the hash table is located as follows:
10176 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10177 the low-order k bits all set to 1.
10179 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10181 3) If the hash table entry at index H matches the signature, use that
10182 entry. If the hash table entry at index H is unused (all zeroes),
10183 terminate the search: the signature is not present in the table.
10185 4) Let H = (H + H') modulo M. Repeat at Step 3.
10187 Because M > N and H' and M are relatively prime, the search is guaranteed
10188 to stop at an unused slot or find the match. */
10190 /* Create a hash table to map DWO IDs to their CU/TU entry in
10191 .debug_{info,types}.dwo in DWP_FILE.
10192 Returns NULL if there isn't one.
10193 Note: This function processes DWP files only, not DWO files. */
10195 static struct dwp_hash_table
*
10196 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
10197 struct dwp_file
*dwp_file
, int is_debug_types
)
10199 struct objfile
*objfile
= per_objfile
->objfile
;
10200 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10201 const gdb_byte
*index_ptr
, *index_end
;
10202 struct dwarf2_section_info
*index
;
10203 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
10204 struct dwp_hash_table
*htab
;
10206 if (is_debug_types
)
10207 index
= &dwp_file
->sections
.tu_index
;
10209 index
= &dwp_file
->sections
.cu_index
;
10211 if (index
->empty ())
10213 index
->read (objfile
);
10215 index_ptr
= index
->buffer
;
10216 index_end
= index_ptr
+ index
->size
;
10218 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10219 For now it's safe to just read 4 bytes (particularly as it's difficult to
10220 tell if you're dealing with Version 5 before you've read the version). */
10221 version
= read_4_bytes (dbfd
, index_ptr
);
10223 if (version
== 2 || version
== 5)
10224 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
10228 nr_units
= read_4_bytes (dbfd
, index_ptr
);
10230 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
10233 if (version
!= 1 && version
!= 2 && version
!= 5)
10235 error (_("Dwarf Error: unsupported DWP file version (%s)"
10236 " [in module %s]"),
10237 pulongest (version
), dwp_file
->name
);
10239 if (nr_slots
!= (nr_slots
& -nr_slots
))
10241 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10242 " is not power of 2 [in module %s]"),
10243 pulongest (nr_slots
), dwp_file
->name
);
10246 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
10247 htab
->version
= version
;
10248 htab
->nr_columns
= nr_columns
;
10249 htab
->nr_units
= nr_units
;
10250 htab
->nr_slots
= nr_slots
;
10251 htab
->hash_table
= index_ptr
;
10252 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
10254 /* Exit early if the table is empty. */
10255 if (nr_slots
== 0 || nr_units
== 0
10256 || (version
== 2 && nr_columns
== 0)
10257 || (version
== 5 && nr_columns
== 0))
10259 /* All must be zero. */
10260 if (nr_slots
!= 0 || nr_units
!= 0
10261 || (version
== 2 && nr_columns
!= 0)
10262 || (version
== 5 && nr_columns
!= 0))
10264 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10265 " all zero [in modules %s]"),
10273 htab
->section_pool
.v1
.indices
=
10274 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10275 /* It's harder to decide whether the section is too small in v1.
10276 V1 is deprecated anyway so we punt. */
10278 else if (version
== 2)
10280 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10281 int *ids
= htab
->section_pool
.v2
.section_ids
;
10282 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
10283 /* Reverse map for error checking. */
10284 int ids_seen
[DW_SECT_MAX
+ 1];
10287 if (nr_columns
< 2)
10289 error (_("Dwarf Error: bad DWP hash table, too few columns"
10290 " in section table [in module %s]"),
10293 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
10295 error (_("Dwarf Error: bad DWP hash table, too many columns"
10296 " in section table [in module %s]"),
10299 memset (ids
, 255, sizeof_ids
);
10300 memset (ids_seen
, 255, sizeof (ids_seen
));
10301 for (i
= 0; i
< nr_columns
; ++i
)
10303 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10305 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
10307 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10308 " in section table [in module %s]"),
10309 id
, dwp_file
->name
);
10311 if (ids_seen
[id
] != -1)
10313 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10314 " id %d in section table [in module %s]"),
10315 id
, dwp_file
->name
);
10320 /* Must have exactly one info or types section. */
10321 if (((ids_seen
[DW_SECT_INFO
] != -1)
10322 + (ids_seen
[DW_SECT_TYPES
] != -1))
10325 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10326 " DWO info/types section [in module %s]"),
10329 /* Must have an abbrev section. */
10330 if (ids_seen
[DW_SECT_ABBREV
] == -1)
10332 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10333 " section [in module %s]"),
10336 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10337 htab
->section_pool
.v2
.sizes
=
10338 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
10339 * nr_units
* nr_columns
);
10340 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
10341 * nr_units
* nr_columns
))
10344 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10345 " [in module %s]"),
10349 else /* version == 5 */
10351 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10352 int *ids
= htab
->section_pool
.v5
.section_ids
;
10353 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
10354 /* Reverse map for error checking. */
10355 int ids_seen
[DW_SECT_MAX_V5
+ 1];
10357 if (nr_columns
< 2)
10359 error (_("Dwarf Error: bad DWP hash table, too few columns"
10360 " in section table [in module %s]"),
10363 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
10365 error (_("Dwarf Error: bad DWP hash table, too many columns"
10366 " in section table [in module %s]"),
10369 memset (ids
, 255, sizeof_ids
);
10370 memset (ids_seen
, 255, sizeof (ids_seen
));
10371 for (int i
= 0; i
< nr_columns
; ++i
)
10373 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10375 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
10377 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10378 " in section table [in module %s]"),
10379 id
, dwp_file
->name
);
10381 if (ids_seen
[id
] != -1)
10383 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10384 " id %d in section table [in module %s]"),
10385 id
, dwp_file
->name
);
10390 /* Must have seen an info section. */
10391 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
10393 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10394 " DWO info/types section [in module %s]"),
10397 /* Must have an abbrev section. */
10398 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
10400 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10401 " section [in module %s]"),
10404 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10405 htab
->section_pool
.v5
.sizes
10406 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
10407 * nr_units
* nr_columns
);
10408 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
10409 * nr_units
* nr_columns
))
10412 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10413 " [in module %s]"),
10421 /* Update SECTIONS with the data from SECTP.
10423 This function is like the other "locate" section routines, but in
10424 this context the sections to read comes from the DWP V1 hash table,
10425 not the full ELF section table.
10427 The result is non-zero for success, or zero if an error was found. */
10430 locate_v1_virtual_dwo_sections (asection
*sectp
,
10431 struct virtual_v1_dwo_sections
*sections
)
10433 const struct dwop_section_names
*names
= &dwop_section_names
;
10435 if (names
->abbrev_dwo
.matches (sectp
->name
))
10437 /* There can be only one. */
10438 if (sections
->abbrev
.s
.section
!= NULL
)
10440 sections
->abbrev
.s
.section
= sectp
;
10441 sections
->abbrev
.size
= bfd_section_size (sectp
);
10443 else if (names
->info_dwo
.matches (sectp
->name
)
10444 || names
->types_dwo
.matches (sectp
->name
))
10446 /* There can be only one. */
10447 if (sections
->info_or_types
.s
.section
!= NULL
)
10449 sections
->info_or_types
.s
.section
= sectp
;
10450 sections
->info_or_types
.size
= bfd_section_size (sectp
);
10452 else if (names
->line_dwo
.matches (sectp
->name
))
10454 /* There can be only one. */
10455 if (sections
->line
.s
.section
!= NULL
)
10457 sections
->line
.s
.section
= sectp
;
10458 sections
->line
.size
= bfd_section_size (sectp
);
10460 else if (names
->loc_dwo
.matches (sectp
->name
))
10462 /* There can be only one. */
10463 if (sections
->loc
.s
.section
!= NULL
)
10465 sections
->loc
.s
.section
= sectp
;
10466 sections
->loc
.size
= bfd_section_size (sectp
);
10468 else if (names
->macinfo_dwo
.matches (sectp
->name
))
10470 /* There can be only one. */
10471 if (sections
->macinfo
.s
.section
!= NULL
)
10473 sections
->macinfo
.s
.section
= sectp
;
10474 sections
->macinfo
.size
= bfd_section_size (sectp
);
10476 else if (names
->macro_dwo
.matches (sectp
->name
))
10478 /* There can be only one. */
10479 if (sections
->macro
.s
.section
!= NULL
)
10481 sections
->macro
.s
.section
= sectp
;
10482 sections
->macro
.size
= bfd_section_size (sectp
);
10484 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
10486 /* There can be only one. */
10487 if (sections
->str_offsets
.s
.section
!= NULL
)
10489 sections
->str_offsets
.s
.section
= sectp
;
10490 sections
->str_offsets
.size
= bfd_section_size (sectp
);
10494 /* No other kind of section is valid. */
10501 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10502 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10503 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10504 This is for DWP version 1 files. */
10506 static struct dwo_unit
*
10507 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
10508 struct dwp_file
*dwp_file
,
10509 uint32_t unit_index
,
10510 const char *comp_dir
,
10511 ULONGEST signature
, int is_debug_types
)
10513 const struct dwp_hash_table
*dwp_htab
=
10514 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10515 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10516 const char *kind
= is_debug_types
? "TU" : "CU";
10517 struct dwo_file
*dwo_file
;
10518 struct dwo_unit
*dwo_unit
;
10519 struct virtual_v1_dwo_sections sections
;
10520 void **dwo_file_slot
;
10523 gdb_assert (dwp_file
->version
== 1);
10525 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10526 kind
, pulongest (unit_index
), hex_string (signature
),
10529 /* Fetch the sections of this DWO unit.
10530 Put a limit on the number of sections we look for so that bad data
10531 doesn't cause us to loop forever. */
10533 #define MAX_NR_V1_DWO_SECTIONS \
10534 (1 /* .debug_info or .debug_types */ \
10535 + 1 /* .debug_abbrev */ \
10536 + 1 /* .debug_line */ \
10537 + 1 /* .debug_loc */ \
10538 + 1 /* .debug_str_offsets */ \
10539 + 1 /* .debug_macro or .debug_macinfo */ \
10540 + 1 /* trailing zero */)
10542 memset (§ions
, 0, sizeof (sections
));
10544 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
10547 uint32_t section_nr
=
10548 read_4_bytes (dbfd
,
10549 dwp_htab
->section_pool
.v1
.indices
10550 + (unit_index
+ i
) * sizeof (uint32_t));
10552 if (section_nr
== 0)
10554 if (section_nr
>= dwp_file
->num_sections
)
10556 error (_("Dwarf Error: bad DWP hash table, section number too large"
10557 " [in module %s]"),
10561 sectp
= dwp_file
->elf_sections
[section_nr
];
10562 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
10564 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10565 " [in module %s]"),
10571 || sections
.info_or_types
.empty ()
10572 || sections
.abbrev
.empty ())
10574 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10575 " [in module %s]"),
10578 if (i
== MAX_NR_V1_DWO_SECTIONS
)
10580 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10581 " [in module %s]"),
10585 /* It's easier for the rest of the code if we fake a struct dwo_file and
10586 have dwo_unit "live" in that. At least for now.
10588 The DWP file can be made up of a random collection of CUs and TUs.
10589 However, for each CU + set of TUs that came from the same original DWO
10590 file, we can combine them back into a virtual DWO file to save space
10591 (fewer struct dwo_file objects to allocate). Remember that for really
10592 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10594 std::string virtual_dwo_name
=
10595 string_printf ("virtual-dwo/%d-%d-%d-%d",
10596 sections
.abbrev
.get_id (),
10597 sections
.line
.get_id (),
10598 sections
.loc
.get_id (),
10599 sections
.str_offsets
.get_id ());
10600 /* Can we use an existing virtual DWO file? */
10601 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10603 /* Create one if necessary. */
10604 if (*dwo_file_slot
== NULL
)
10606 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10607 virtual_dwo_name
.c_str ());
10609 dwo_file
= new struct dwo_file
;
10610 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10611 dwo_file
->comp_dir
= comp_dir
;
10612 dwo_file
->sections
.abbrev
= sections
.abbrev
;
10613 dwo_file
->sections
.line
= sections
.line
;
10614 dwo_file
->sections
.loc
= sections
.loc
;
10615 dwo_file
->sections
.macinfo
= sections
.macinfo
;
10616 dwo_file
->sections
.macro
= sections
.macro
;
10617 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
10618 /* The "str" section is global to the entire DWP file. */
10619 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10620 /* The info or types section is assigned below to dwo_unit,
10621 there's no need to record it in dwo_file.
10622 Also, we can't simply record type sections in dwo_file because
10623 we record a pointer into the vector in dwo_unit. As we collect more
10624 types we'll grow the vector and eventually have to reallocate space
10625 for it, invalidating all copies of pointers into the previous
10627 *dwo_file_slot
= dwo_file
;
10631 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10632 virtual_dwo_name
.c_str ());
10634 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10637 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10638 dwo_unit
->dwo_file
= dwo_file
;
10639 dwo_unit
->signature
= signature
;
10640 dwo_unit
->section
=
10641 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10642 *dwo_unit
->section
= sections
.info_or_types
;
10643 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10648 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10649 simplify them. Given a pointer to the containing section SECTION, and
10650 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10651 virtual section of just that piece. */
10653 static struct dwarf2_section_info
10654 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
10655 struct dwarf2_section_info
*section
,
10656 bfd_size_type offset
, bfd_size_type size
)
10658 struct dwarf2_section_info result
;
10661 gdb_assert (section
!= NULL
);
10662 gdb_assert (!section
->is_virtual
);
10664 memset (&result
, 0, sizeof (result
));
10665 result
.s
.containing_section
= section
;
10666 result
.is_virtual
= true;
10671 sectp
= section
->get_bfd_section ();
10673 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10674 bounds of the real section. This is a pretty-rare event, so just
10675 flag an error (easier) instead of a warning and trying to cope. */
10677 || offset
+ size
> bfd_section_size (sectp
))
10679 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10680 " in section %s [in module %s]"),
10681 sectp
? bfd_section_name (sectp
) : "<unknown>",
10682 objfile_name (per_objfile
->objfile
));
10685 result
.virtual_offset
= offset
;
10686 result
.size
= size
;
10690 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10691 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10692 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10693 This is for DWP version 2 files. */
10695 static struct dwo_unit
*
10696 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
10697 struct dwp_file
*dwp_file
,
10698 uint32_t unit_index
,
10699 const char *comp_dir
,
10700 ULONGEST signature
, int is_debug_types
)
10702 const struct dwp_hash_table
*dwp_htab
=
10703 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10704 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10705 const char *kind
= is_debug_types
? "TU" : "CU";
10706 struct dwo_file
*dwo_file
;
10707 struct dwo_unit
*dwo_unit
;
10708 struct virtual_v2_or_v5_dwo_sections sections
;
10709 void **dwo_file_slot
;
10712 gdb_assert (dwp_file
->version
== 2);
10714 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10715 kind
, pulongest (unit_index
), hex_string (signature
),
10718 /* Fetch the section offsets of this DWO unit. */
10720 memset (§ions
, 0, sizeof (sections
));
10722 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10724 uint32_t offset
= read_4_bytes (dbfd
,
10725 dwp_htab
->section_pool
.v2
.offsets
10726 + (((unit_index
- 1) * dwp_htab
->nr_columns
10728 * sizeof (uint32_t)));
10729 uint32_t size
= read_4_bytes (dbfd
,
10730 dwp_htab
->section_pool
.v2
.sizes
10731 + (((unit_index
- 1) * dwp_htab
->nr_columns
10733 * sizeof (uint32_t)));
10735 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
10738 case DW_SECT_TYPES
:
10739 sections
.info_or_types_offset
= offset
;
10740 sections
.info_or_types_size
= size
;
10742 case DW_SECT_ABBREV
:
10743 sections
.abbrev_offset
= offset
;
10744 sections
.abbrev_size
= size
;
10747 sections
.line_offset
= offset
;
10748 sections
.line_size
= size
;
10751 sections
.loc_offset
= offset
;
10752 sections
.loc_size
= size
;
10754 case DW_SECT_STR_OFFSETS
:
10755 sections
.str_offsets_offset
= offset
;
10756 sections
.str_offsets_size
= size
;
10758 case DW_SECT_MACINFO
:
10759 sections
.macinfo_offset
= offset
;
10760 sections
.macinfo_size
= size
;
10762 case DW_SECT_MACRO
:
10763 sections
.macro_offset
= offset
;
10764 sections
.macro_size
= size
;
10769 /* It's easier for the rest of the code if we fake a struct dwo_file and
10770 have dwo_unit "live" in that. At least for now.
10772 The DWP file can be made up of a random collection of CUs and TUs.
10773 However, for each CU + set of TUs that came from the same original DWO
10774 file, we can combine them back into a virtual DWO file to save space
10775 (fewer struct dwo_file objects to allocate). Remember that for really
10776 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10778 std::string virtual_dwo_name
=
10779 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10780 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10781 (long) (sections
.line_size
? sections
.line_offset
: 0),
10782 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
10783 (long) (sections
.str_offsets_size
10784 ? sections
.str_offsets_offset
: 0));
10785 /* Can we use an existing virtual DWO file? */
10786 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10788 /* Create one if necessary. */
10789 if (*dwo_file_slot
== NULL
)
10791 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10792 virtual_dwo_name
.c_str ());
10794 dwo_file
= new struct dwo_file
;
10795 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10796 dwo_file
->comp_dir
= comp_dir
;
10797 dwo_file
->sections
.abbrev
=
10798 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
10799 sections
.abbrev_offset
,
10800 sections
.abbrev_size
);
10801 dwo_file
->sections
.line
=
10802 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
10803 sections
.line_offset
,
10804 sections
.line_size
);
10805 dwo_file
->sections
.loc
=
10806 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
10807 sections
.loc_offset
, sections
.loc_size
);
10808 dwo_file
->sections
.macinfo
=
10809 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
10810 sections
.macinfo_offset
,
10811 sections
.macinfo_size
);
10812 dwo_file
->sections
.macro
=
10813 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
10814 sections
.macro_offset
,
10815 sections
.macro_size
);
10816 dwo_file
->sections
.str_offsets
=
10817 create_dwp_v2_or_v5_section (per_objfile
,
10818 &dwp_file
->sections
.str_offsets
,
10819 sections
.str_offsets_offset
,
10820 sections
.str_offsets_size
);
10821 /* The "str" section is global to the entire DWP file. */
10822 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10823 /* The info or types section is assigned below to dwo_unit,
10824 there's no need to record it in dwo_file.
10825 Also, we can't simply record type sections in dwo_file because
10826 we record a pointer into the vector in dwo_unit. As we collect more
10827 types we'll grow the vector and eventually have to reallocate space
10828 for it, invalidating all copies of pointers into the previous
10830 *dwo_file_slot
= dwo_file
;
10834 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10835 virtual_dwo_name
.c_str ());
10837 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10840 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10841 dwo_unit
->dwo_file
= dwo_file
;
10842 dwo_unit
->signature
= signature
;
10843 dwo_unit
->section
=
10844 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10845 *dwo_unit
->section
= create_dwp_v2_or_v5_section
10848 ? &dwp_file
->sections
.types
10849 : &dwp_file
->sections
.info
,
10850 sections
.info_or_types_offset
,
10851 sections
.info_or_types_size
);
10852 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10857 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10858 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10859 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10860 This is for DWP version 5 files. */
10862 static struct dwo_unit
*
10863 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
10864 struct dwp_file
*dwp_file
,
10865 uint32_t unit_index
,
10866 const char *comp_dir
,
10867 ULONGEST signature
, int is_debug_types
)
10869 const struct dwp_hash_table
*dwp_htab
10870 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10871 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10872 const char *kind
= is_debug_types
? "TU" : "CU";
10873 struct dwo_file
*dwo_file
;
10874 struct dwo_unit
*dwo_unit
;
10875 struct virtual_v2_or_v5_dwo_sections sections
{};
10876 void **dwo_file_slot
;
10878 gdb_assert (dwp_file
->version
== 5);
10880 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10881 kind
, pulongest (unit_index
), hex_string (signature
),
10884 /* Fetch the section offsets of this DWO unit. */
10886 /* memset (§ions, 0, sizeof (sections)); */
10888 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10890 uint32_t offset
= read_4_bytes (dbfd
,
10891 dwp_htab
->section_pool
.v5
.offsets
10892 + (((unit_index
- 1)
10893 * dwp_htab
->nr_columns
10895 * sizeof (uint32_t)));
10896 uint32_t size
= read_4_bytes (dbfd
,
10897 dwp_htab
->section_pool
.v5
.sizes
10898 + (((unit_index
- 1) * dwp_htab
->nr_columns
10900 * sizeof (uint32_t)));
10902 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
10904 case DW_SECT_ABBREV_V5
:
10905 sections
.abbrev_offset
= offset
;
10906 sections
.abbrev_size
= size
;
10908 case DW_SECT_INFO_V5
:
10909 sections
.info_or_types_offset
= offset
;
10910 sections
.info_or_types_size
= size
;
10912 case DW_SECT_LINE_V5
:
10913 sections
.line_offset
= offset
;
10914 sections
.line_size
= size
;
10916 case DW_SECT_LOCLISTS_V5
:
10917 sections
.loclists_offset
= offset
;
10918 sections
.loclists_size
= size
;
10920 case DW_SECT_MACRO_V5
:
10921 sections
.macro_offset
= offset
;
10922 sections
.macro_size
= size
;
10924 case DW_SECT_RNGLISTS_V5
:
10925 sections
.rnglists_offset
= offset
;
10926 sections
.rnglists_size
= size
;
10928 case DW_SECT_STR_OFFSETS_V5
:
10929 sections
.str_offsets_offset
= offset
;
10930 sections
.str_offsets_size
= size
;
10932 case DW_SECT_RESERVED_V5
:
10938 /* It's easier for the rest of the code if we fake a struct dwo_file and
10939 have dwo_unit "live" in that. At least for now.
10941 The DWP file can be made up of a random collection of CUs and TUs.
10942 However, for each CU + set of TUs that came from the same original DWO
10943 file, we can combine them back into a virtual DWO file to save space
10944 (fewer struct dwo_file objects to allocate). Remember that for really
10945 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10947 std::string virtual_dwo_name
=
10948 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10949 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10950 (long) (sections
.line_size
? sections
.line_offset
: 0),
10951 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
10952 (long) (sections
.str_offsets_size
10953 ? sections
.str_offsets_offset
: 0),
10954 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
10955 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
10956 /* Can we use an existing virtual DWO file? */
10957 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
10958 virtual_dwo_name
.c_str (),
10960 /* Create one if necessary. */
10961 if (*dwo_file_slot
== NULL
)
10963 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10964 virtual_dwo_name
.c_str ());
10966 dwo_file
= new struct dwo_file
;
10967 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10968 dwo_file
->comp_dir
= comp_dir
;
10969 dwo_file
->sections
.abbrev
=
10970 create_dwp_v2_or_v5_section (per_objfile
,
10971 &dwp_file
->sections
.abbrev
,
10972 sections
.abbrev_offset
,
10973 sections
.abbrev_size
);
10974 dwo_file
->sections
.line
=
10975 create_dwp_v2_or_v5_section (per_objfile
,
10976 &dwp_file
->sections
.line
,
10977 sections
.line_offset
, sections
.line_size
);
10978 dwo_file
->sections
.macro
=
10979 create_dwp_v2_or_v5_section (per_objfile
,
10980 &dwp_file
->sections
.macro
,
10981 sections
.macro_offset
,
10982 sections
.macro_size
);
10983 dwo_file
->sections
.loclists
=
10984 create_dwp_v2_or_v5_section (per_objfile
,
10985 &dwp_file
->sections
.loclists
,
10986 sections
.loclists_offset
,
10987 sections
.loclists_size
);
10988 dwo_file
->sections
.rnglists
=
10989 create_dwp_v2_or_v5_section (per_objfile
,
10990 &dwp_file
->sections
.rnglists
,
10991 sections
.rnglists_offset
,
10992 sections
.rnglists_size
);
10993 dwo_file
->sections
.str_offsets
=
10994 create_dwp_v2_or_v5_section (per_objfile
,
10995 &dwp_file
->sections
.str_offsets
,
10996 sections
.str_offsets_offset
,
10997 sections
.str_offsets_size
);
10998 /* The "str" section is global to the entire DWP file. */
10999 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
11000 /* The info or types section is assigned below to dwo_unit,
11001 there's no need to record it in dwo_file.
11002 Also, we can't simply record type sections in dwo_file because
11003 we record a pointer into the vector in dwo_unit. As we collect more
11004 types we'll grow the vector and eventually have to reallocate space
11005 for it, invalidating all copies of pointers into the previous
11007 *dwo_file_slot
= dwo_file
;
11011 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11012 virtual_dwo_name
.c_str ());
11014 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
11017 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
11018 dwo_unit
->dwo_file
= dwo_file
;
11019 dwo_unit
->signature
= signature
;
11021 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
11022 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
11023 &dwp_file
->sections
.info
,
11024 sections
.info_or_types_offset
,
11025 sections
.info_or_types_size
);
11026 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11031 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11032 Returns NULL if the signature isn't found. */
11034 static struct dwo_unit
*
11035 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
11036 struct dwp_file
*dwp_file
, const char *comp_dir
,
11037 ULONGEST signature
, int is_debug_types
)
11039 const struct dwp_hash_table
*dwp_htab
=
11040 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11041 bfd
*dbfd
= dwp_file
->dbfd
.get ();
11042 uint32_t mask
= dwp_htab
->nr_slots
- 1;
11043 uint32_t hash
= signature
& mask
;
11044 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
11047 struct dwo_unit find_dwo_cu
;
11049 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
11050 find_dwo_cu
.signature
= signature
;
11051 slot
= htab_find_slot (is_debug_types
11052 ? dwp_file
->loaded_tus
.get ()
11053 : dwp_file
->loaded_cus
.get (),
11054 &find_dwo_cu
, INSERT
);
11057 return (struct dwo_unit
*) *slot
;
11059 /* Use a for loop so that we don't loop forever on bad debug info. */
11060 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
11062 ULONGEST signature_in_table
;
11064 signature_in_table
=
11065 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
11066 if (signature_in_table
== signature
)
11068 uint32_t unit_index
=
11069 read_4_bytes (dbfd
,
11070 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
11072 if (dwp_file
->version
== 1)
11074 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
11075 unit_index
, comp_dir
,
11076 signature
, is_debug_types
);
11078 else if (dwp_file
->version
== 2)
11080 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
11081 unit_index
, comp_dir
,
11082 signature
, is_debug_types
);
11084 else /* version == 5 */
11086 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
11087 unit_index
, comp_dir
,
11088 signature
, is_debug_types
);
11090 return (struct dwo_unit
*) *slot
;
11092 if (signature_in_table
== 0)
11094 hash
= (hash
+ hash2
) & mask
;
11097 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11098 " [in module %s]"),
11102 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11103 Open the file specified by FILE_NAME and hand it off to BFD for
11104 preliminary analysis. Return a newly initialized bfd *, which
11105 includes a canonicalized copy of FILE_NAME.
11106 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11107 SEARCH_CWD is true if the current directory is to be searched.
11108 It will be searched before debug-file-directory.
11109 If successful, the file is added to the bfd include table of the
11110 objfile's bfd (see gdb_bfd_record_inclusion).
11111 If unable to find/open the file, return NULL.
11112 NOTE: This function is derived from symfile_bfd_open. */
11114 static gdb_bfd_ref_ptr
11115 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
11116 const char *file_name
, int is_dwp
, int search_cwd
)
11119 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11120 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11121 to debug_file_directory. */
11122 const char *search_path
;
11123 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
11125 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
11128 if (!debug_file_directory
.empty ())
11130 search_path_holder
.reset (concat (".", dirname_separator_string
,
11131 debug_file_directory
.c_str (),
11133 search_path
= search_path_holder
.get ();
11139 search_path
= debug_file_directory
.c_str ();
11141 /* Add the path for the executable binary to the list of search paths. */
11142 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
11143 search_path_holder
.reset (concat (objfile_dir
.c_str (),
11144 dirname_separator_string
,
11145 search_path
, nullptr));
11146 search_path
= search_path_holder
.get ();
11148 openp_flags flags
= OPF_RETURN_REALPATH
;
11150 flags
|= OPF_SEARCH_IN_PATH
;
11152 gdb::unique_xmalloc_ptr
<char> absolute_name
;
11153 desc
= openp (search_path
, flags
, file_name
,
11154 O_RDONLY
| O_BINARY
, &absolute_name
);
11158 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
11160 if (sym_bfd
== NULL
)
11162 bfd_set_cacheable (sym_bfd
.get (), 1);
11164 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
11167 /* Success. Record the bfd as having been included by the objfile's bfd.
11168 This is important because things like demangled_names_hash lives in the
11169 objfile's per_bfd space and may have references to things like symbol
11170 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11171 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
.get (), sym_bfd
.get ());
11176 /* Try to open DWO file FILE_NAME.
11177 COMP_DIR is the DW_AT_comp_dir attribute.
11178 The result is the bfd handle of the file.
11179 If there is a problem finding or opening the file, return NULL.
11180 Upon success, the canonicalized path of the file is stored in the bfd,
11181 same as symfile_bfd_open. */
11183 static gdb_bfd_ref_ptr
11184 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
11185 const char *file_name
, const char *comp_dir
)
11187 if (IS_ABSOLUTE_PATH (file_name
))
11188 return try_open_dwop_file (per_objfile
, file_name
,
11189 0 /*is_dwp*/, 0 /*search_cwd*/);
11191 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11193 if (comp_dir
!= NULL
)
11195 std::string path_to_try
= path_join (comp_dir
, file_name
);
11197 /* NOTE: If comp_dir is a relative path, this will also try the
11198 search path, which seems useful. */
11199 gdb_bfd_ref_ptr
abfd (try_open_dwop_file
11200 (per_objfile
, path_to_try
.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11206 /* That didn't work, try debug-file-directory, which, despite its name,
11207 is a list of paths. */
11209 if (debug_file_directory
.empty ())
11212 return try_open_dwop_file (per_objfile
, file_name
,
11213 0 /*is_dwp*/, 1 /*search_cwd*/);
11216 /* This function is mapped across the sections and remembers the offset and
11217 size of each of the DWO debugging sections we are interested in. */
11220 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
11221 asection
*sectp
, dwo_sections
*dwo_sections
)
11223 const struct dwop_section_names
*names
= &dwop_section_names
;
11225 struct dwarf2_section_info
*dw_sect
= nullptr;
11227 if (names
->abbrev_dwo
.matches (sectp
->name
))
11228 dw_sect
= &dwo_sections
->abbrev
;
11229 else if (names
->info_dwo
.matches (sectp
->name
))
11230 dw_sect
= &dwo_sections
->info
;
11231 else if (names
->line_dwo
.matches (sectp
->name
))
11232 dw_sect
= &dwo_sections
->line
;
11233 else if (names
->loc_dwo
.matches (sectp
->name
))
11234 dw_sect
= &dwo_sections
->loc
;
11235 else if (names
->loclists_dwo
.matches (sectp
->name
))
11236 dw_sect
= &dwo_sections
->loclists
;
11237 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11238 dw_sect
= &dwo_sections
->macinfo
;
11239 else if (names
->macro_dwo
.matches (sectp
->name
))
11240 dw_sect
= &dwo_sections
->macro
;
11241 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11242 dw_sect
= &dwo_sections
->rnglists
;
11243 else if (names
->str_dwo
.matches (sectp
->name
))
11244 dw_sect
= &dwo_sections
->str
;
11245 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11246 dw_sect
= &dwo_sections
->str_offsets
;
11247 else if (names
->types_dwo
.matches (sectp
->name
))
11249 struct dwarf2_section_info type_section
;
11251 memset (&type_section
, 0, sizeof (type_section
));
11252 dwo_sections
->types
.push_back (type_section
);
11253 dw_sect
= &dwo_sections
->types
.back ();
11256 if (dw_sect
!= nullptr)
11258 dw_sect
->s
.section
= sectp
;
11259 dw_sect
->size
= bfd_section_size (sectp
);
11260 dw_sect
->read (objfile
);
11264 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11265 by PER_CU. This is for the non-DWP case.
11266 The result is NULL if DWO_NAME can't be found. */
11268 static struct dwo_file
*
11269 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
11270 const char *comp_dir
)
11272 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11274 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
11277 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
11282 dwo_file_up
dwo_file (new struct dwo_file
);
11283 dwo_file
->dwo_name
= dwo_name
;
11284 dwo_file
->comp_dir
= comp_dir
;
11285 dwo_file
->dbfd
= std::move (dbfd
);
11287 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
11288 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
11289 sec
, &dwo_file
->sections
);
11291 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
11294 if (cu
->per_cu
->version () < 5)
11296 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
11297 dwo_file
->sections
.types
, dwo_file
->tus
);
11301 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
11302 &dwo_file
->sections
.info
, dwo_file
->tus
,
11303 rcuh_kind::COMPILE
);
11306 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
11308 return dwo_file
.release ();
11311 /* This function is mapped across the sections and remembers the offset and
11312 size of each of the DWP debugging sections common to version 1 and 2 that
11313 we are interested in. */
11316 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11317 asection
*sectp
, dwp_file
*dwp_file
)
11319 const struct dwop_section_names
*names
= &dwop_section_names
;
11320 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11322 /* Record the ELF section number for later lookup: this is what the
11323 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11324 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11325 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11327 /* Look for specific sections that we need. */
11328 struct dwarf2_section_info
*dw_sect
= nullptr;
11329 if (names
->str_dwo
.matches (sectp
->name
))
11330 dw_sect
= &dwp_file
->sections
.str
;
11331 else if (names
->cu_index
.matches (sectp
->name
))
11332 dw_sect
= &dwp_file
->sections
.cu_index
;
11333 else if (names
->tu_index
.matches (sectp
->name
))
11334 dw_sect
= &dwp_file
->sections
.tu_index
;
11336 if (dw_sect
!= nullptr)
11338 dw_sect
->s
.section
= sectp
;
11339 dw_sect
->size
= bfd_section_size (sectp
);
11340 dw_sect
->read (objfile
);
11344 /* This function is mapped across the sections and remembers the offset and
11345 size of each of the DWP version 2 debugging sections that we are interested
11346 in. This is split into a separate function because we don't know if we
11347 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11350 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11351 asection
*sectp
, void *dwp_file_ptr
)
11353 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11354 const struct dwop_section_names
*names
= &dwop_section_names
;
11355 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11357 /* Record the ELF section number for later lookup: this is what the
11358 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11359 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11360 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11362 /* Look for specific sections that we need. */
11363 struct dwarf2_section_info
*dw_sect
= nullptr;
11364 if (names
->abbrev_dwo
.matches (sectp
->name
))
11365 dw_sect
= &dwp_file
->sections
.abbrev
;
11366 else if (names
->info_dwo
.matches (sectp
->name
))
11367 dw_sect
= &dwp_file
->sections
.info
;
11368 else if (names
->line_dwo
.matches (sectp
->name
))
11369 dw_sect
= &dwp_file
->sections
.line
;
11370 else if (names
->loc_dwo
.matches (sectp
->name
))
11371 dw_sect
= &dwp_file
->sections
.loc
;
11372 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11373 dw_sect
= &dwp_file
->sections
.macinfo
;
11374 else if (names
->macro_dwo
.matches (sectp
->name
))
11375 dw_sect
= &dwp_file
->sections
.macro
;
11376 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11377 dw_sect
= &dwp_file
->sections
.str_offsets
;
11378 else if (names
->types_dwo
.matches (sectp
->name
))
11379 dw_sect
= &dwp_file
->sections
.types
;
11381 if (dw_sect
!= nullptr)
11383 dw_sect
->s
.section
= sectp
;
11384 dw_sect
->size
= bfd_section_size (sectp
);
11385 dw_sect
->read (objfile
);
11389 /* This function is mapped across the sections and remembers the offset and
11390 size of each of the DWP version 5 debugging sections that we are interested
11391 in. This is split into a separate function because we don't know if we
11392 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11395 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11396 asection
*sectp
, void *dwp_file_ptr
)
11398 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11399 const struct dwop_section_names
*names
= &dwop_section_names
;
11400 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11402 /* Record the ELF section number for later lookup: this is what the
11403 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11404 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11405 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11407 /* Look for specific sections that we need. */
11408 struct dwarf2_section_info
*dw_sect
= nullptr;
11409 if (names
->abbrev_dwo
.matches (sectp
->name
))
11410 dw_sect
= &dwp_file
->sections
.abbrev
;
11411 else if (names
->info_dwo
.matches (sectp
->name
))
11412 dw_sect
= &dwp_file
->sections
.info
;
11413 else if (names
->line_dwo
.matches (sectp
->name
))
11414 dw_sect
= &dwp_file
->sections
.line
;
11415 else if (names
->loclists_dwo
.matches (sectp
->name
))
11416 dw_sect
= &dwp_file
->sections
.loclists
;
11417 else if (names
->macro_dwo
.matches (sectp
->name
))
11418 dw_sect
= &dwp_file
->sections
.macro
;
11419 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11420 dw_sect
= &dwp_file
->sections
.rnglists
;
11421 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11422 dw_sect
= &dwp_file
->sections
.str_offsets
;
11424 if (dw_sect
!= nullptr)
11426 dw_sect
->s
.section
= sectp
;
11427 dw_sect
->size
= bfd_section_size (sectp
);
11428 dw_sect
->read (objfile
);
11432 /* Hash function for dwp_file loaded CUs/TUs. */
11435 hash_dwp_loaded_cutus (const void *item
)
11437 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
11439 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11440 return dwo_unit
->signature
;
11443 /* Equality function for dwp_file loaded CUs/TUs. */
11446 eq_dwp_loaded_cutus (const void *a
, const void *b
)
11448 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
11449 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
11451 return dua
->signature
== dub
->signature
;
11454 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11457 allocate_dwp_loaded_cutus_table ()
11459 return htab_up (htab_create_alloc (3,
11460 hash_dwp_loaded_cutus
,
11461 eq_dwp_loaded_cutus
,
11462 NULL
, xcalloc
, xfree
));
11465 /* Try to open DWP file FILE_NAME.
11466 The result is the bfd handle of the file.
11467 If there is a problem finding or opening the file, return NULL.
11468 Upon success, the canonicalized path of the file is stored in the bfd,
11469 same as symfile_bfd_open. */
11471 static gdb_bfd_ref_ptr
11472 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
11474 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
11476 1 /*search_cwd*/));
11480 /* Work around upstream bug 15652.
11481 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11482 [Whether that's a "bug" is debatable, but it is getting in our way.]
11483 We have no real idea where the dwp file is, because gdb's realpath-ing
11484 of the executable's path may have discarded the needed info.
11485 [IWBN if the dwp file name was recorded in the executable, akin to
11486 .gnu_debuglink, but that doesn't exist yet.]
11487 Strip the directory from FILE_NAME and search again. */
11488 if (!debug_file_directory
.empty ())
11490 /* Don't implicitly search the current directory here.
11491 If the user wants to search "." to handle this case,
11492 it must be added to debug-file-directory. */
11493 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
11501 /* Initialize the use of the DWP file for the current objfile.
11502 By convention the name of the DWP file is ${objfile}.dwp.
11503 The result is NULL if it can't be found. */
11505 static std::unique_ptr
<struct dwp_file
>
11506 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
11508 struct objfile
*objfile
= per_objfile
->objfile
;
11510 /* Try to find first .dwp for the binary file before any symbolic links
11513 /* If the objfile is a debug file, find the name of the real binary
11514 file and get the name of dwp file from there. */
11515 std::string dwp_name
;
11516 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
11518 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
11519 const char *backlink_basename
= lbasename (backlink
->original_name
);
11521 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
11524 dwp_name
= objfile
->original_name
;
11526 dwp_name
+= ".dwp";
11528 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
11530 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
11532 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11533 dwp_name
= objfile_name (objfile
);
11534 dwp_name
+= ".dwp";
11535 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
11540 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
11542 return std::unique_ptr
<dwp_file
> ();
11545 const char *name
= bfd_get_filename (dbfd
.get ());
11546 std::unique_ptr
<struct dwp_file
> dwp_file
11547 (new struct dwp_file (name
, std::move (dbfd
)));
11549 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
11550 dwp_file
->elf_sections
=
11551 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
11552 dwp_file
->num_sections
, asection
*);
11554 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11555 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11558 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
11560 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
11562 /* The DWP file version is stored in the hash table. Oh well. */
11563 if (dwp_file
->cus
&& dwp_file
->tus
11564 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
11566 /* Technically speaking, we should try to limp along, but this is
11567 pretty bizarre. We use pulongest here because that's the established
11568 portability solution (e.g, we cannot use %u for uint32_t). */
11569 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11570 " TU version %s [in DWP file %s]"),
11571 pulongest (dwp_file
->cus
->version
),
11572 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
11576 dwp_file
->version
= dwp_file
->cus
->version
;
11577 else if (dwp_file
->tus
)
11578 dwp_file
->version
= dwp_file
->tus
->version
;
11580 dwp_file
->version
= 2;
11582 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11584 if (dwp_file
->version
== 2)
11585 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11588 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11592 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
11593 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
11595 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
11596 dwarf_read_debug_printf (" %s CUs, %s TUs",
11597 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
11598 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
11603 /* Wrapper around open_and_init_dwp_file, only open it once. */
11605 static struct dwp_file
*
11606 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
11608 if (!per_objfile
->per_bfd
->dwp_checked
)
11610 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
11611 per_objfile
->per_bfd
->dwp_checked
= 1;
11613 return per_objfile
->per_bfd
->dwp_file
.get ();
11616 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11617 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11618 or in the DWP file for the objfile, referenced by THIS_UNIT.
11619 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11620 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11622 This is called, for example, when wanting to read a variable with a
11623 complex location. Therefore we don't want to do file i/o for every call.
11624 Therefore we don't want to look for a DWO file on every call.
11625 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11626 then we check if we've already seen DWO_NAME, and only THEN do we check
11629 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11630 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11632 static struct dwo_unit
*
11633 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11634 ULONGEST signature
, int is_debug_types
)
11636 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11637 struct objfile
*objfile
= per_objfile
->objfile
;
11638 const char *kind
= is_debug_types
? "TU" : "CU";
11639 void **dwo_file_slot
;
11640 struct dwo_file
*dwo_file
;
11641 struct dwp_file
*dwp_file
;
11643 /* First see if there's a DWP file.
11644 If we have a DWP file but didn't find the DWO inside it, don't
11645 look for the original DWO file. It makes gdb behave differently
11646 depending on whether one is debugging in the build tree. */
11648 dwp_file
= get_dwp_file (per_objfile
);
11649 if (dwp_file
!= NULL
)
11651 const struct dwp_hash_table
*dwp_htab
=
11652 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11654 if (dwp_htab
!= NULL
)
11656 struct dwo_unit
*dwo_cutu
=
11657 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
11660 if (dwo_cutu
!= NULL
)
11662 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11663 kind
, hex_string (signature
),
11664 host_address_to_string (dwo_cutu
));
11672 /* No DWP file, look for the DWO file. */
11674 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
11675 if (*dwo_file_slot
== NULL
)
11677 /* Read in the file and build a table of the CUs/TUs it contains. */
11678 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
11680 /* NOTE: This will be NULL if unable to open the file. */
11681 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
11683 if (dwo_file
!= NULL
)
11685 struct dwo_unit
*dwo_cutu
= NULL
;
11687 if (is_debug_types
&& dwo_file
->tus
)
11689 struct dwo_unit find_dwo_cutu
;
11691 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11692 find_dwo_cutu
.signature
= signature
;
11694 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
11697 else if (!is_debug_types
&& dwo_file
->cus
)
11699 struct dwo_unit find_dwo_cutu
;
11701 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11702 find_dwo_cutu
.signature
= signature
;
11703 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
11707 if (dwo_cutu
!= NULL
)
11709 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11710 kind
, dwo_name
, hex_string (signature
),
11711 host_address_to_string (dwo_cutu
));
11718 /* We didn't find it. This could mean a dwo_id mismatch, or
11719 someone deleted the DWO/DWP file, or the search path isn't set up
11720 correctly to find the file. */
11722 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11723 kind
, dwo_name
, hex_string (signature
));
11725 /* This is a warning and not a complaint because it can be caused by
11726 pilot error (e.g., user accidentally deleting the DWO). */
11728 /* Print the name of the DWP file if we looked there, helps the user
11729 better diagnose the problem. */
11730 std::string dwp_text
;
11732 if (dwp_file
!= NULL
)
11733 dwp_text
= string_printf (" [in DWP file %s]",
11734 lbasename (dwp_file
->name
));
11736 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11737 " [in module %s]"),
11738 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
11739 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
11744 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11745 See lookup_dwo_cutu_unit for details. */
11747 static struct dwo_unit
*
11748 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11749 ULONGEST signature
)
11751 gdb_assert (!cu
->per_cu
->is_debug_types
);
11753 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
11756 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11757 See lookup_dwo_cutu_unit for details. */
11759 static struct dwo_unit
*
11760 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
11762 gdb_assert (cu
->per_cu
->is_debug_types
);
11764 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
11766 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
11769 /* Traversal function for queue_and_load_all_dwo_tus. */
11772 queue_and_load_dwo_tu (void **slot
, void *info
)
11774 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
11775 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
11776 ULONGEST signature
= dwo_unit
->signature
;
11777 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
11779 if (sig_type
!= NULL
)
11781 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11782 a real dependency of PER_CU on SIG_TYPE. That is detected later
11783 while processing PER_CU. */
11784 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
11786 load_full_type_unit (sig_type
, cu
->per_objfile
);
11787 cu
->per_cu
->imported_symtabs_push (sig_type
);
11793 /* Queue all TUs contained in the DWO of CU to be read in.
11794 The DWO may have the only definition of the type, though it may not be
11795 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11796 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11799 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
11801 struct dwo_unit
*dwo_unit
;
11802 struct dwo_file
*dwo_file
;
11804 gdb_assert (cu
!= nullptr);
11805 gdb_assert (!cu
->per_cu
->is_debug_types
);
11806 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
11808 dwo_unit
= cu
->dwo_unit
;
11809 gdb_assert (dwo_unit
!= NULL
);
11811 dwo_file
= dwo_unit
->dwo_file
;
11812 if (dwo_file
->tus
!= NULL
)
11813 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
11816 /* Read in various DIEs. */
11818 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11819 Inherit only the children of the DW_AT_abstract_origin DIE not being
11820 already referenced by DW_AT_abstract_origin from the children of the
11824 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
11826 attribute
*attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
11827 if (attr
== nullptr)
11830 /* Note that following die references may follow to a die in a
11832 dwarf2_cu
*origin_cu
= cu
;
11834 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11835 die_info
*origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
11837 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11839 struct pending
**origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
11840 origin_cu
->list_in_scope
= cu
->list_in_scope
;
11842 if (die
->tag
!= origin_die
->tag
11843 && !(die
->tag
== DW_TAG_inlined_subroutine
11844 && origin_die
->tag
== DW_TAG_subprogram
))
11845 complaint (_("DIE %s and its abstract origin %s have different tags"),
11846 sect_offset_str (die
->sect_off
),
11847 sect_offset_str (origin_die
->sect_off
));
11849 /* Find if the concrete and abstract trees are structurally the
11850 same. This is a shallow traversal and it is not bullet-proof;
11851 the compiler can trick the debugger into believing that the trees
11852 are isomorphic, whereas they actually are not. However, the
11853 likelyhood of this happening is pretty low, and a full-fledged
11854 check would be an overkill. */
11855 bool are_isomorphic
= true;
11856 die_info
*concrete_child
= die
->child
;
11857 die_info
*abstract_child
= origin_die
->child
;
11858 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
11860 if (concrete_child
== nullptr
11861 || abstract_child
== nullptr
11862 || concrete_child
->tag
!= abstract_child
->tag
)
11864 are_isomorphic
= false;
11868 concrete_child
= concrete_child
->sibling
;
11869 abstract_child
= abstract_child
->sibling
;
11872 /* Walk the origin's children in parallel to the concrete children.
11873 This helps match an origin child in case the debug info misses
11874 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11875 origin tree may not have the same tree structure as the concrete
11877 die_info
*corresponding_abstract_child
11878 = are_isomorphic
? origin_die
->child
: nullptr;
11880 std::vector
<sect_offset
> offsets
;
11882 for (die_info
*child_die
= die
->child
;
11883 child_die
&& child_die
->tag
;
11884 child_die
= child_die
->sibling
)
11886 /* We are trying to process concrete instance entries:
11887 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11888 it's not relevant to our analysis here. i.e. detecting DIEs that are
11889 present in the abstract instance but not referenced in the concrete
11891 if (child_die
->tag
== DW_TAG_call_site
11892 || child_die
->tag
== DW_TAG_GNU_call_site
)
11894 if (are_isomorphic
)
11895 corresponding_abstract_child
11896 = corresponding_abstract_child
->sibling
;
11900 /* For each CHILD_DIE, find the corresponding child of
11901 ORIGIN_DIE. If there is more than one layer of
11902 DW_AT_abstract_origin, follow them all; there shouldn't be,
11903 but GCC versions at least through 4.4 generate this (GCC PR
11905 die_info
*child_origin_die
= child_die
;
11906 dwarf2_cu
*child_origin_cu
= cu
;
11909 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
11911 if (attr
== nullptr)
11914 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
11918 /* If missing DW_AT_abstract_origin, try the corresponding child
11919 of the origin. Clang emits such lexical scopes. */
11920 if (child_origin_die
== child_die
11921 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
11923 && child_die
->tag
== DW_TAG_lexical_block
)
11924 child_origin_die
= corresponding_abstract_child
;
11926 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11927 counterpart may exist. */
11928 if (child_origin_die
!= child_die
)
11930 if (child_die
->tag
!= child_origin_die
->tag
11931 && !(child_die
->tag
== DW_TAG_inlined_subroutine
11932 && child_origin_die
->tag
== DW_TAG_subprogram
))
11933 complaint (_("Child DIE %s and its abstract origin %s have "
11935 sect_offset_str (child_die
->sect_off
),
11936 sect_offset_str (child_origin_die
->sect_off
));
11937 if (child_origin_die
->parent
!= origin_die
)
11938 complaint (_("Child DIE %s and its abstract origin %s have "
11939 "different parents"),
11940 sect_offset_str (child_die
->sect_off
),
11941 sect_offset_str (child_origin_die
->sect_off
));
11943 offsets
.push_back (child_origin_die
->sect_off
);
11946 if (are_isomorphic
)
11947 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
11950 if (!offsets
.empty ())
11952 std::sort (offsets
.begin (), offsets
.end ());
11954 for (auto offsets_it
= offsets
.begin () + 1;
11955 offsets_it
< offsets
.end ();
11957 if (*(offsets_it
- 1) == *offsets_it
)
11958 complaint (_("Multiple children of DIE %s refer "
11959 "to DIE %s as their abstract origin"),
11960 sect_offset_str (die
->sect_off
),
11961 sect_offset_str (*offsets_it
));
11964 auto offsets_it
= offsets
.begin ();
11965 die_info
*origin_child_die
= origin_die
->child
;
11966 while (origin_child_die
!= nullptr && origin_child_die
->tag
!= 0)
11968 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11969 while (offsets_it
< offsets
.end ()
11970 && *offsets_it
< origin_child_die
->sect_off
)
11973 if (offsets_it
== offsets
.end ()
11974 || *offsets_it
> origin_child_die
->sect_off
)
11976 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11977 Check whether we're already processing ORIGIN_CHILD_DIE.
11978 This can happen with mutually referenced abstract_origins.
11980 if (!origin_child_die
->in_process
)
11981 process_die (origin_child_die
, origin_cu
);
11984 origin_child_die
= origin_child_die
->sibling
;
11987 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
11989 if (cu
!= origin_cu
)
11990 compute_delayed_physnames (origin_cu
);
11994 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
11996 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11997 struct gdbarch
*gdbarch
= objfile
->arch ();
11998 struct context_stack
*newobj
;
12001 struct die_info
*child_die
;
12002 struct attribute
*attr
, *call_line
, *call_file
;
12004 CORE_ADDR baseaddr
;
12005 struct block
*block
;
12006 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
12007 std::vector
<struct symbol
*> template_args
;
12008 struct template_symbol
*templ_func
= NULL
;
12012 /* If we do not have call site information, we can't show the
12013 caller of this inlined function. That's too confusing, so
12014 only use the scope for local variables. */
12015 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
12016 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
12017 if (call_line
== NULL
|| call_file
== NULL
)
12019 read_lexical_block_scope (die
, cu
);
12024 baseaddr
= objfile
->text_section_offset ();
12026 name
= dwarf2_name (die
, cu
);
12027 if (name
== nullptr)
12028 name
= dw2_linkage_name (die
, cu
);
12030 /* Ignore functions with missing or empty names. These are actually
12031 illegal according to the DWARF standard. */
12034 complaint (_("missing name for subprogram DIE at %s"),
12035 sect_offset_str (die
->sect_off
));
12039 /* Ignore functions with missing or invalid low and high pc attributes. */
12040 if (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr)
12041 <= PC_BOUNDS_INVALID
)
12043 if (have_complaint ())
12045 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
12046 bool external_p
= attr
!= nullptr && attr
->as_boolean ();
12047 attr
= dwarf2_attr (die
, DW_AT_inline
, cu
);
12050 && attr
->is_nonnegative ()
12051 && (attr
->as_nonnegative () == DW_INL_inlined
12052 || attr
->as_nonnegative () == DW_INL_declared_inlined
));
12053 attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
12054 bool decl_p
= attr
!= nullptr && attr
->as_boolean ();
12055 if (!external_p
&& !inlined_p
&& !decl_p
)
12056 complaint (_("cannot get low and high bounds "
12057 "for subprogram DIE at %s"),
12058 sect_offset_str (die
->sect_off
));
12063 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12064 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12066 /* If we have any template arguments, then we must allocate a
12067 different sort of symbol. */
12068 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
12070 if (child_die
->tag
== DW_TAG_template_type_param
12071 || child_die
->tag
== DW_TAG_template_value_param
)
12073 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
12074 templ_func
->subclass
= SYMBOL_TEMPLATE
;
12079 gdb_assert (cu
->get_builder () != nullptr);
12080 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
12081 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
,
12082 (struct symbol
*) templ_func
);
12084 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
12085 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
12088 /* If there is a location expression for DW_AT_frame_base, record
12090 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
12091 if (attr
!= nullptr)
12092 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
12094 /* If there is a location for the static link, record it. */
12095 newobj
->static_link
= NULL
;
12096 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
12097 if (attr
!= nullptr)
12099 newobj
->static_link
12100 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
12101 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
12105 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
12107 if (die
->child
!= NULL
)
12109 child_die
= die
->child
;
12110 while (child_die
&& child_die
->tag
)
12112 if (child_die
->tag
== DW_TAG_template_type_param
12113 || child_die
->tag
== DW_TAG_template_value_param
)
12115 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12118 template_args
.push_back (arg
);
12121 process_die (child_die
, cu
);
12122 child_die
= child_die
->sibling
;
12126 inherit_abstract_dies (die
, cu
);
12128 /* If we have a DW_AT_specification, we might need to import using
12129 directives from the context of the specification DIE. See the
12130 comment in determine_prefix. */
12131 if (cu
->lang () == language_cplus
12132 && dwarf2_attr (die
, DW_AT_specification
, cu
))
12134 struct dwarf2_cu
*spec_cu
= cu
;
12135 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
12139 child_die
= spec_die
->child
;
12140 while (child_die
&& child_die
->tag
)
12142 if (child_die
->tag
== DW_TAG_imported_module
)
12143 process_die (child_die
, spec_cu
);
12144 child_die
= child_die
->sibling
;
12147 /* In some cases, GCC generates specification DIEs that
12148 themselves contain DW_AT_specification attributes. */
12149 spec_die
= die_specification (spec_die
, &spec_cu
);
12153 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12154 /* Make a block for the local symbols within. */
12155 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
12156 cstk
.static_link
, lowpc
, highpc
);
12158 /* For C++, set the block's scope. */
12159 if ((cu
->lang () == language_cplus
12160 || cu
->lang () == language_fortran
12161 || cu
->lang () == language_d
12162 || cu
->lang () == language_rust
)
12163 && cu
->processing_has_namespace_info
)
12164 block_set_scope (block
, determine_prefix (die
, cu
),
12165 &objfile
->objfile_obstack
);
12167 /* If we have address ranges, record them. */
12168 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12170 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
12172 /* Attach template arguments to function. */
12173 if (!template_args
.empty ())
12175 gdb_assert (templ_func
!= NULL
);
12177 templ_func
->n_template_arguments
= template_args
.size ();
12178 templ_func
->template_arguments
12179 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
12180 templ_func
->n_template_arguments
);
12181 memcpy (templ_func
->template_arguments
,
12182 template_args
.data (),
12183 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
12185 /* Make sure that the symtab is set on the new symbols. Even
12186 though they don't appear in this symtab directly, other parts
12187 of gdb assume that symbols do, and this is reasonably
12189 for (symbol
*sym
: template_args
)
12190 sym
->set_symtab (templ_func
->symtab ());
12193 /* In C++, we can have functions nested inside functions (e.g., when
12194 a function declares a class that has methods). This means that
12195 when we finish processing a function scope, we may need to go
12196 back to building a containing block's symbol lists. */
12197 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12198 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12200 /* If we've finished processing a top-level function, subsequent
12201 symbols go in the file symbol list. */
12202 if (cu
->get_builder ()->outermost_context_p ())
12203 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
12206 /* Process all the DIES contained within a lexical block scope. Start
12207 a new scope, process the dies, and then close the scope. */
12210 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12212 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12213 struct gdbarch
*gdbarch
= objfile
->arch ();
12214 CORE_ADDR lowpc
, highpc
;
12215 struct die_info
*child_die
;
12216 CORE_ADDR baseaddr
;
12218 baseaddr
= objfile
->text_section_offset ();
12220 /* Ignore blocks with missing or invalid low and high pc attributes. */
12221 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12222 as multiple lexical blocks? Handling children in a sane way would
12223 be nasty. Might be easier to properly extend generic blocks to
12224 describe ranges. */
12225 switch (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr))
12227 case PC_BOUNDS_NOT_PRESENT
:
12228 /* DW_TAG_lexical_block has no attributes, process its children as if
12229 there was no wrapping by that DW_TAG_lexical_block.
12230 GCC does no longer produces such DWARF since GCC r224161. */
12231 for (child_die
= die
->child
;
12232 child_die
!= NULL
&& child_die
->tag
;
12233 child_die
= child_die
->sibling
)
12235 /* We might already be processing this DIE. This can happen
12236 in an unusual circumstance -- where a subroutine A
12237 appears lexically in another subroutine B, but A actually
12238 inlines B. The recursion is broken here, rather than in
12239 inherit_abstract_dies, because it seems better to simply
12240 drop concrete children here. */
12241 if (!child_die
->in_process
)
12242 process_die (child_die
, cu
);
12245 case PC_BOUNDS_INVALID
:
12248 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12249 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12251 cu
->get_builder ()->push_context (0, lowpc
);
12252 if (die
->child
!= NULL
)
12254 child_die
= die
->child
;
12255 while (child_die
&& child_die
->tag
)
12257 process_die (child_die
, cu
);
12258 child_die
= child_die
->sibling
;
12261 inherit_abstract_dies (die
, cu
);
12262 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12264 if (*cu
->get_builder ()->get_local_symbols () != NULL
12265 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
12267 struct block
*block
12268 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
12269 cstk
.start_addr
, highpc
);
12271 /* Note that recording ranges after traversing children, as we
12272 do here, means that recording a parent's ranges entails
12273 walking across all its children's ranges as they appear in
12274 the address map, which is quadratic behavior.
12276 It would be nicer to record the parent's ranges before
12277 traversing its children, simply overriding whatever you find
12278 there. But since we don't even decide whether to create a
12279 block until after we've traversed its children, that's hard
12281 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12283 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12284 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12287 static void dwarf2_ranges_read_low_addrs (unsigned offset
,
12288 struct dwarf2_cu
*cu
,
12290 std::vector
<CORE_ADDR
> &result
);
12292 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12295 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12297 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12298 struct objfile
*objfile
= per_objfile
->objfile
;
12299 struct gdbarch
*gdbarch
= objfile
->arch ();
12300 CORE_ADDR pc
, baseaddr
;
12301 struct attribute
*attr
;
12304 struct die_info
*child_die
;
12306 baseaddr
= objfile
->text_section_offset ();
12308 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
12311 /* This was a pre-DWARF-5 GNU extension alias
12312 for DW_AT_call_return_pc. */
12313 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12317 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12318 "DIE %s [in module %s]"),
12319 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12322 pc
= attr
->as_address () + baseaddr
;
12323 pc
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc
);
12326 if (cu
->call_site_htab
== NULL
)
12327 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
12328 call_site::eq
, NULL
,
12329 &objfile
->objfile_obstack
,
12330 hashtab_obstack_allocate
, NULL
);
12331 struct call_site
call_site_local (pc
, nullptr, nullptr);
12332 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
12335 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12336 "DIE %s [in module %s]"),
12337 paddress (gdbarch
, pc
), sect_offset_str (die
->sect_off
),
12338 objfile_name (objfile
));
12342 /* Count parameters at the caller. */
12345 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
12346 child_die
= child_die
->sibling
)
12348 if (child_die
->tag
!= DW_TAG_call_site_parameter
12349 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12351 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12352 "DW_TAG_call_site child DIE %s [in module %s]"),
12353 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
12354 objfile_name (objfile
));
12361 struct call_site
*call_site
12362 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
12364 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
12365 struct call_site (pc
, cu
->per_cu
, per_objfile
);
12368 /* We never call the destructor of call_site, so we must ensure it is
12369 trivially destructible. */
12370 gdb_static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
12372 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
12373 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
12375 struct die_info
*func_die
;
12377 /* Skip also over DW_TAG_inlined_subroutine. */
12378 for (func_die
= die
->parent
;
12379 func_die
&& func_die
->tag
!= DW_TAG_subprogram
12380 && func_die
->tag
!= DW_TAG_subroutine_type
;
12381 func_die
= func_die
->parent
);
12383 /* DW_AT_call_all_calls is a superset
12384 of DW_AT_call_all_tail_calls. */
12386 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
12387 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
12388 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
12389 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
12391 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12392 not complete. But keep CALL_SITE for look ups via call_site_htab,
12393 both the initial caller containing the real return address PC and
12394 the final callee containing the current PC of a chain of tail
12395 calls do not need to have the tail call list complete. But any
12396 function candidate for a virtual tail call frame searched via
12397 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12398 determined unambiguously. */
12402 struct type
*func_type
= NULL
;
12405 func_type
= get_die_type (func_die
, cu
);
12406 if (func_type
!= NULL
)
12408 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
12410 /* Enlist this call site to the function. */
12411 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
12412 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
12415 complaint (_("Cannot find function owning DW_TAG_call_site "
12416 "DIE %s [in module %s]"),
12417 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12421 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
12423 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
12425 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
12428 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12429 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12432 call_site
->target
.set_loc_dwarf_block (nullptr);
12433 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
12434 /* Keep NULL DWARF_BLOCK. */;
12435 else if (attr
->form_is_block ())
12437 struct dwarf2_locexpr_baton
*dlbaton
;
12438 struct dwarf_block
*block
= attr
->as_block ();
12440 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
12441 dlbaton
->data
= block
->data
;
12442 dlbaton
->size
= block
->size
;
12443 dlbaton
->per_objfile
= per_objfile
;
12444 dlbaton
->per_cu
= cu
->per_cu
;
12446 call_site
->target
.set_loc_dwarf_block (dlbaton
);
12448 else if (attr
->form_is_ref ())
12450 struct dwarf2_cu
*target_cu
= cu
;
12451 struct die_info
*target_die
;
12453 target_die
= follow_die_ref (die
, attr
, &target_cu
);
12454 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
12456 struct attribute
*ranges_attr
12457 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
12459 if (die_is_declaration (target_die
, target_cu
))
12461 const char *target_physname
;
12463 /* Prefer the mangled name; otherwise compute the demangled one. */
12464 target_physname
= dw2_linkage_name (target_die
, target_cu
);
12465 if (target_physname
== NULL
)
12466 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
12467 if (target_physname
== NULL
)
12468 complaint (_("DW_AT_call_target target DIE has invalid "
12469 "physname, for referencing DIE %s [in module %s]"),
12470 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12472 call_site
->target
.set_loc_physname (target_physname
);
12474 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
12476 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
12477 + target_cu
->gnu_ranges_base
);
12478 std::vector
<CORE_ADDR
> addresses
;
12479 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
12480 target_die
->tag
, addresses
);
12481 CORE_ADDR
*saved
= XOBNEWVAR (&objfile
->objfile_obstack
, CORE_ADDR
,
12482 addresses
.size ());
12483 std::copy (addresses
.begin (), addresses
.end (), saved
);
12484 call_site
->target
.set_loc_array (addresses
.size (), saved
);
12490 /* DW_AT_entry_pc should be preferred. */
12491 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
12493 <= PC_BOUNDS_INVALID
)
12494 complaint (_("DW_AT_call_target target DIE has invalid "
12495 "low pc, for referencing DIE %s [in module %s]"),
12496 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12499 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
)
12501 call_site
->target
.set_loc_physaddr (lowpc
);
12506 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12507 "block nor reference, for DIE %s [in module %s]"),
12508 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12510 for (child_die
= die
->child
;
12511 child_die
&& child_die
->tag
;
12512 child_die
= child_die
->sibling
)
12514 struct call_site_parameter
*parameter
;
12515 struct attribute
*loc
, *origin
;
12517 if (child_die
->tag
!= DW_TAG_call_site_parameter
12518 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12520 /* Already printed the complaint above. */
12524 gdb_assert (call_site
->parameter_count
< nparams
);
12525 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
12527 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12528 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12529 register is contained in DW_AT_call_value. */
12531 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
12532 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
12533 if (origin
== NULL
)
12535 /* This was a pre-DWARF-5 GNU extension alias
12536 for DW_AT_call_parameter. */
12537 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
12539 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
12541 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
12543 sect_offset sect_off
= origin
->get_ref_die_offset ();
12544 if (!cu
->header
.offset_in_cu_p (sect_off
))
12546 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12547 binding can be done only inside one CU. Such referenced DIE
12548 therefore cannot be even moved to DW_TAG_partial_unit. */
12549 complaint (_("DW_AT_call_parameter offset is not in CU for "
12550 "DW_TAG_call_site child DIE %s [in module %s]"),
12551 sect_offset_str (child_die
->sect_off
),
12552 objfile_name (objfile
));
12555 parameter
->u
.param_cu_off
12556 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
12558 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
12560 complaint (_("No DW_FORM_block* DW_AT_location for "
12561 "DW_TAG_call_site child DIE %s [in module %s]"),
12562 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
12567 struct dwarf_block
*block
= loc
->as_block ();
12569 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
12570 (block
->data
, &block
->data
[block
->size
]);
12571 if (parameter
->u
.dwarf_reg
!= -1)
12572 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
12573 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
12574 &block
->data
[block
->size
],
12575 ¶meter
->u
.fb_offset
))
12576 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
12579 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12580 "for DW_FORM_block* DW_AT_location is supported for "
12581 "DW_TAG_call_site child DIE %s "
12583 sect_offset_str (child_die
->sect_off
),
12584 objfile_name (objfile
));
12589 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
12591 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
12592 if (attr
== NULL
|| !attr
->form_is_block ())
12594 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12595 "DW_TAG_call_site child DIE %s [in module %s]"),
12596 sect_offset_str (child_die
->sect_off
),
12597 objfile_name (objfile
));
12601 struct dwarf_block
*block
= attr
->as_block ();
12602 parameter
->value
= block
->data
;
12603 parameter
->value_size
= block
->size
;
12605 /* Parameters are not pre-cleared by memset above. */
12606 parameter
->data_value
= NULL
;
12607 parameter
->data_value_size
= 0;
12608 call_site
->parameter_count
++;
12610 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
12612 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
12613 if (attr
!= nullptr)
12615 if (!attr
->form_is_block ())
12616 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12617 "DW_TAG_call_site child DIE %s [in module %s]"),
12618 sect_offset_str (child_die
->sect_off
),
12619 objfile_name (objfile
));
12622 block
= attr
->as_block ();
12623 parameter
->data_value
= block
->data
;
12624 parameter
->data_value_size
= block
->size
;
12630 /* Helper function for read_variable. If DIE represents a virtual
12631 table, then return the type of the concrete object that is
12632 associated with the virtual table. Otherwise, return NULL. */
12634 static struct type
*
12635 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12637 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
12641 /* Find the type DIE. */
12642 struct die_info
*type_die
= NULL
;
12643 struct dwarf2_cu
*type_cu
= cu
;
12645 if (attr
->form_is_ref ())
12646 type_die
= follow_die_ref (die
, attr
, &type_cu
);
12647 if (type_die
== NULL
)
12650 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
12652 return die_containing_type (type_die
, type_cu
);
12655 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12658 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
12660 struct rust_vtable_symbol
*storage
= NULL
;
12662 if (cu
->lang () == language_rust
)
12664 struct type
*containing_type
= rust_containing_type (die
, cu
);
12666 if (containing_type
!= NULL
)
12668 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12670 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
12671 storage
->concrete_type
= containing_type
;
12672 storage
->subclass
= SYMBOL_RUST_VTABLE
;
12676 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
12677 struct attribute
*abstract_origin
12678 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12679 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
12680 if (res
== NULL
&& loc
&& abstract_origin
)
12682 /* We have a variable without a name, but with a location and an abstract
12683 origin. This may be a concrete instance of an abstract variable
12684 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12686 struct dwarf2_cu
*origin_cu
= cu
;
12687 struct die_info
*origin_die
12688 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
12689 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12690 per_objfile
->per_bfd
->abstract_to_concrete
12691 [origin_die
->sect_off
].push_back (die
->sect_off
);
12695 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12696 reading .debug_rnglists.
12697 Callback's type should be:
12698 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12699 Return true if the attributes are present and valid, otherwise,
12702 template <typename Callback
>
12704 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
12705 dwarf_tag tag
, Callback
&&callback
)
12707 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12708 struct objfile
*objfile
= per_objfile
->objfile
;
12709 bfd
*obfd
= objfile
->obfd
.get ();
12710 /* Base address selection entry. */
12711 gdb::optional
<CORE_ADDR
> base
;
12712 const gdb_byte
*buffer
;
12713 bool overflow
= false;
12714 ULONGEST addr_index
;
12715 struct dwarf2_section_info
*rnglists_section
;
12717 base
= cu
->base_address
;
12718 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
12719 rnglists_section
->read (objfile
);
12721 if (offset
>= rnglists_section
->size
)
12723 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12727 buffer
= rnglists_section
->buffer
+ offset
;
12731 /* Initialize it due to a false compiler warning. */
12732 CORE_ADDR range_beginning
= 0, range_end
= 0;
12733 const gdb_byte
*buf_end
= (rnglists_section
->buffer
12734 + rnglists_section
->size
);
12735 unsigned int bytes_read
;
12737 if (buffer
== buf_end
)
12742 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
12745 case DW_RLE_end_of_list
:
12747 case DW_RLE_base_address
:
12748 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12753 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12754 buffer
+= bytes_read
;
12756 case DW_RLE_base_addressx
:
12757 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12758 buffer
+= bytes_read
;
12759 base
= read_addr_index (cu
, addr_index
);
12761 case DW_RLE_start_length
:
12762 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12767 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12769 buffer
+= bytes_read
;
12770 range_end
= (range_beginning
12771 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12772 buffer
+= bytes_read
;
12773 if (buffer
> buf_end
)
12779 case DW_RLE_startx_length
:
12780 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12781 buffer
+= bytes_read
;
12782 range_beginning
= read_addr_index (cu
, addr_index
);
12783 if (buffer
> buf_end
)
12788 range_end
= (range_beginning
12789 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12790 buffer
+= bytes_read
;
12792 case DW_RLE_offset_pair
:
12793 range_beginning
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12794 buffer
+= bytes_read
;
12795 if (buffer
> buf_end
)
12800 range_end
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12801 buffer
+= bytes_read
;
12802 if (buffer
> buf_end
)
12808 case DW_RLE_start_end
:
12809 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
12814 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12816 buffer
+= bytes_read
;
12817 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12818 buffer
+= bytes_read
;
12820 case DW_RLE_startx_endx
:
12821 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12822 buffer
+= bytes_read
;
12823 range_beginning
= read_addr_index (cu
, addr_index
);
12824 if (buffer
> buf_end
)
12829 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12830 buffer
+= bytes_read
;
12831 range_end
= read_addr_index (cu
, addr_index
);
12834 complaint (_("Invalid .debug_rnglists data (no base address)"));
12837 if (rlet
== DW_RLE_end_of_list
|| overflow
)
12839 if (rlet
== DW_RLE_base_address
)
12842 if (range_beginning
> range_end
)
12844 /* Inverted range entries are invalid. */
12845 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12849 /* Empty range entries have no effect. */
12850 if (range_beginning
== range_end
)
12853 /* Only DW_RLE_offset_pair needs the base address added. */
12854 if (rlet
== DW_RLE_offset_pair
)
12856 if (!base
.has_value ())
12858 /* We have no valid base address for the DW_RLE_offset_pair. */
12859 complaint (_("Invalid .debug_rnglists data (no base address for "
12860 "DW_RLE_offset_pair)"));
12864 range_beginning
+= *base
;
12865 range_end
+= *base
;
12868 /* A not-uncommon case of bad debug info.
12869 Don't pollute the addrmap with bad data. */
12870 if (range_beginning
== 0
12871 && !per_objfile
->per_bfd
->has_section_at_zero
)
12873 complaint (_(".debug_rnglists entry has start address of zero"
12874 " [in module %s]"), objfile_name (objfile
));
12878 callback (range_beginning
, range_end
);
12883 complaint (_("Offset %d is not terminated "
12884 "for DW_AT_ranges attribute"),
12892 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12893 Callback's type should be:
12894 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12895 Return 1 if the attributes are present and valid, otherwise, return 0. */
12897 template <typename Callback
>
12899 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
12900 Callback
&&callback
)
12902 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12903 struct objfile
*objfile
= per_objfile
->objfile
;
12904 struct comp_unit_head
*cu_header
= &cu
->header
;
12905 bfd
*obfd
= objfile
->obfd
.get ();
12906 unsigned int addr_size
= cu_header
->addr_size
;
12907 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
12908 /* Base address selection entry. */
12909 gdb::optional
<CORE_ADDR
> base
;
12910 unsigned int dummy
;
12911 const gdb_byte
*buffer
;
12913 if (cu_header
->version
>= 5)
12914 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
12916 base
= cu
->base_address
;
12918 per_objfile
->per_bfd
->ranges
.read (objfile
);
12919 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
12921 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12925 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
12929 CORE_ADDR range_beginning
, range_end
;
12931 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12932 buffer
+= addr_size
;
12933 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12934 buffer
+= addr_size
;
12935 offset
+= 2 * addr_size
;
12937 /* An end of list marker is a pair of zero addresses. */
12938 if (range_beginning
== 0 && range_end
== 0)
12939 /* Found the end of list entry. */
12942 /* Each base address selection entry is a pair of 2 values.
12943 The first is the largest possible address, the second is
12944 the base address. Check for a base address here. */
12945 if ((range_beginning
& mask
) == mask
)
12947 /* If we found the largest possible address, then we already
12948 have the base address in range_end. */
12953 if (!base
.has_value ())
12955 /* We have no valid base address for the ranges
12957 complaint (_("Invalid .debug_ranges data (no base address)"));
12961 if (range_beginning
> range_end
)
12963 /* Inverted range entries are invalid. */
12964 complaint (_("Invalid .debug_ranges data (inverted range)"));
12968 /* Empty range entries have no effect. */
12969 if (range_beginning
== range_end
)
12972 range_beginning
+= *base
;
12973 range_end
+= *base
;
12975 /* A not-uncommon case of bad debug info.
12976 Don't pollute the addrmap with bad data. */
12977 if (range_beginning
== 0
12978 && !per_objfile
->per_bfd
->has_section_at_zero
)
12980 complaint (_(".debug_ranges entry has start address of zero"
12981 " [in module %s]"), objfile_name (objfile
));
12985 callback (range_beginning
, range_end
);
12991 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12992 Return 1 if the attributes are present and valid, otherwise, return 0.
12993 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12994 ranges in MAP are set, using DATUM as the value. */
12997 dwarf2_ranges_read (unsigned offset
, CORE_ADDR
*low_return
,
12998 CORE_ADDR
*high_return
, struct dwarf2_cu
*cu
,
12999 addrmap
*map
, void *datum
, dwarf_tag tag
)
13001 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13002 struct gdbarch
*gdbarch
= objfile
->arch ();
13003 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
13006 CORE_ADDR high
= 0;
13009 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
13010 [&] (CORE_ADDR range_beginning
, CORE_ADDR range_end
)
13012 if (map
!= nullptr)
13017 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
13018 range_beginning
+ baseaddr
)
13020 highpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
13021 range_end
+ baseaddr
)
13023 map
->set_empty (lowpc
, highpc
- 1, datum
);
13026 /* FIXME: This is recording everything as a low-high
13027 segment of consecutive addresses. We should have a
13028 data structure for discontiguous block ranges
13032 low
= range_beginning
;
13038 if (range_beginning
< low
)
13039 low
= range_beginning
;
13040 if (range_end
> high
)
13048 /* If the first entry is an end-of-list marker, the range
13049 describes an empty scope, i.e. no instructions. */
13055 *high_return
= high
;
13059 /* Process ranges and fill in a vector of the low PC values only. */
13062 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
13064 std::vector
<CORE_ADDR
> &result
)
13066 dwarf2_ranges_process (offset
, cu
, tag
,
13067 [&] (CORE_ADDR start
, CORE_ADDR end
)
13069 result
.push_back (start
);
13073 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13074 definition for the return value. *LOWPC and *HIGHPC are set iff
13075 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13077 static enum pc_bounds_kind
13078 dwarf2_get_pc_bounds (struct die_info
*die
, CORE_ADDR
*lowpc
,
13079 CORE_ADDR
*highpc
, struct dwarf2_cu
*cu
,
13080 addrmap
*map
, void *datum
)
13082 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13083 struct attribute
*attr
;
13084 struct attribute
*attr_high
;
13086 CORE_ADDR high
= 0;
13087 enum pc_bounds_kind ret
;
13089 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13092 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13093 if (attr
!= nullptr)
13095 low
= attr
->as_address ();
13096 high
= attr_high
->as_address ();
13097 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13101 /* Found high w/o low attribute. */
13102 return PC_BOUNDS_INVALID
;
13104 /* Found consecutive range of addresses. */
13105 ret
= PC_BOUNDS_HIGH_LOW
;
13109 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13110 if (attr
!= nullptr && attr
->form_is_unsigned ())
13112 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13113 on DWARF version). */
13114 ULONGEST ranges_offset
= attr
->as_unsigned ();
13116 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13118 if (die
->tag
!= DW_TAG_compile_unit
)
13119 ranges_offset
+= cu
->gnu_ranges_base
;
13121 /* Value of the DW_AT_ranges attribute is the offset in the
13122 .debug_ranges section. */
13123 if (!dwarf2_ranges_read (ranges_offset
, &low
, &high
, cu
,
13124 map
, datum
, die
->tag
))
13125 return PC_BOUNDS_INVALID
;
13126 /* Found discontinuous range of addresses. */
13127 ret
= PC_BOUNDS_RANGES
;
13130 return PC_BOUNDS_NOT_PRESENT
;
13133 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13135 return PC_BOUNDS_INVALID
;
13137 /* When using the GNU linker, .gnu.linkonce. sections are used to
13138 eliminate duplicate copies of functions and vtables and such.
13139 The linker will arbitrarily choose one and discard the others.
13140 The AT_*_pc values for such functions refer to local labels in
13141 these sections. If the section from that file was discarded, the
13142 labels are not in the output, so the relocs get a value of 0.
13143 If this is a discarded function, mark the pc bounds as invalid,
13144 so that GDB will ignore it. */
13145 if (low
== 0 && !per_objfile
->per_bfd
->has_section_at_zero
)
13146 return PC_BOUNDS_INVALID
;
13154 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13155 its low and high PC addresses. Do nothing if these addresses could not
13156 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13157 and HIGHPC to the high address if greater than HIGHPC. */
13160 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
13161 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13162 struct dwarf2_cu
*cu
)
13164 CORE_ADDR low
, high
;
13165 struct die_info
*child
= die
->child
;
13167 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
13168 >= PC_BOUNDS_RANGES
)
13170 *lowpc
= std::min (*lowpc
, low
);
13171 *highpc
= std::max (*highpc
, high
);
13174 /* If the language does not allow nested subprograms (either inside
13175 subprograms or lexical blocks), we're done. */
13176 if (cu
->lang () != language_ada
)
13179 /* Check all the children of the given DIE. If it contains nested
13180 subprograms, then check their pc bounds. Likewise, we need to
13181 check lexical blocks as well, as they may also contain subprogram
13183 while (child
&& child
->tag
)
13185 if (child
->tag
== DW_TAG_subprogram
13186 || child
->tag
== DW_TAG_lexical_block
)
13187 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
13188 child
= child
->sibling
;
13192 /* Get the low and high pc's represented by the scope DIE, and store
13193 them in *LOWPC and *HIGHPC. If the correct values can't be
13194 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13197 get_scope_pc_bounds (struct die_info
*die
,
13198 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13199 struct dwarf2_cu
*cu
)
13201 CORE_ADDR best_low
= (CORE_ADDR
) -1;
13202 CORE_ADDR best_high
= (CORE_ADDR
) 0;
13203 CORE_ADDR current_low
, current_high
;
13205 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
13207 >= PC_BOUNDS_RANGES
)
13209 best_low
= current_low
;
13210 best_high
= current_high
;
13214 struct die_info
*child
= die
->child
;
13216 while (child
&& child
->tag
)
13218 switch (child
->tag
) {
13219 case DW_TAG_subprogram
:
13220 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
13222 case DW_TAG_namespace
:
13223 case DW_TAG_module
:
13224 /* FIXME: carlton/2004-01-16: Should we do this for
13225 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13226 that current GCC's always emit the DIEs corresponding
13227 to definitions of methods of classes as children of a
13228 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13229 the DIEs giving the declarations, which could be
13230 anywhere). But I don't see any reason why the
13231 standards says that they have to be there. */
13232 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
13234 if (current_low
!= ((CORE_ADDR
) -1))
13236 best_low
= std::min (best_low
, current_low
);
13237 best_high
= std::max (best_high
, current_high
);
13245 child
= child
->sibling
;
13250 *highpc
= best_high
;
13253 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13257 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
13258 CORE_ADDR baseaddr
, struct dwarf2_cu
*cu
)
13260 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13261 struct gdbarch
*gdbarch
= objfile
->arch ();
13262 struct attribute
*attr
;
13263 struct attribute
*attr_high
;
13265 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13268 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13269 if (attr
!= nullptr)
13271 CORE_ADDR low
= attr
->as_address ();
13272 CORE_ADDR high
= attr_high
->as_address ();
13274 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13277 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
+ baseaddr
);
13278 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
+ baseaddr
);
13279 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
13283 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13284 if (attr
!= nullptr && attr
->form_is_unsigned ())
13286 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13287 on DWARF version). */
13288 ULONGEST ranges_offset
= attr
->as_unsigned ();
13290 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13292 if (die
->tag
!= DW_TAG_compile_unit
)
13293 ranges_offset
+= cu
->gnu_ranges_base
;
13295 std::vector
<blockrange
> blockvec
;
13296 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
13297 [&] (CORE_ADDR start
, CORE_ADDR end
)
13301 start
= gdbarch_adjust_dwarf2_addr (gdbarch
, start
);
13302 end
= gdbarch_adjust_dwarf2_addr (gdbarch
, end
);
13303 cu
->get_builder ()->record_block_range (block
, start
, end
- 1);
13304 blockvec
.emplace_back (start
, end
);
13307 block
->set_ranges (make_blockranges (objfile
, blockvec
));
13311 /* Check whether the producer field indicates either of GCC < 4.6, or the
13312 Intel C/C++ compiler, and cache the result in CU. */
13315 check_producer (struct dwarf2_cu
*cu
)
13319 if (cu
->producer
== NULL
)
13321 /* For unknown compilers expect their behavior is DWARF version
13324 GCC started to support .debug_types sections by -gdwarf-4 since
13325 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13326 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13327 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13328 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13330 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
13332 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
13333 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
13334 cu
->producer_is_gcc_11
= major
== 11;
13336 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
13338 cu
->producer_is_icc
= true;
13339 cu
->producer_is_icc_lt_14
= major
< 14;
13341 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
13342 cu
->producer_is_codewarrior
= true;
13343 else if (producer_is_clang (cu
->producer
, &major
, &minor
))
13344 cu
->producer_is_clang
= true;
13347 /* For other non-GCC compilers, expect their behavior is DWARF version
13351 cu
->checked_producer
= true;
13354 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13355 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13356 during 4.6.0 experimental. */
13359 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
13361 if (!cu
->checked_producer
)
13362 check_producer (cu
);
13364 return cu
->producer_is_gxx_lt_4_6
;
13368 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13369 with incorrect is_stmt attributes. */
13372 producer_is_codewarrior (struct dwarf2_cu
*cu
)
13374 if (!cu
->checked_producer
)
13375 check_producer (cu
);
13377 return cu
->producer_is_codewarrior
;
13380 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13381 If that attribute is not available, return the appropriate
13384 static enum dwarf_access_attribute
13385 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
13387 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
13388 if (attr
!= nullptr)
13390 LONGEST value
= attr
->constant_value (-1);
13391 if (value
== DW_ACCESS_public
13392 || value
== DW_ACCESS_protected
13393 || value
== DW_ACCESS_private
)
13394 return (dwarf_access_attribute
) value
;
13395 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13399 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
13401 /* The default DWARF 2 accessibility for members is public, the default
13402 accessibility for inheritance is private. */
13404 if (die
->tag
!= DW_TAG_inheritance
)
13405 return DW_ACCESS_public
;
13407 return DW_ACCESS_private
;
13411 /* DWARF 3+ defines the default accessibility a different way. The same
13412 rules apply now for DW_TAG_inheritance as for the members and it only
13413 depends on the container kind. */
13415 if (die
->parent
->tag
== DW_TAG_class_type
)
13416 return DW_ACCESS_private
;
13418 return DW_ACCESS_public
;
13422 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13423 *OFFSET to the byte offset. If the attribute was not found return
13424 0, otherwise return 1. If it was found but could not properly be
13425 handled, set *OFFSET to 0. */
13428 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13431 struct attribute
*attr
;
13433 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13438 /* Note that we do not check for a section offset first here.
13439 This is because DW_AT_data_member_location is new in DWARF 4,
13440 so if we see it, we can assume that a constant form is really
13441 a constant and not a section offset. */
13442 if (attr
->form_is_constant ())
13443 *offset
= attr
->constant_value (0);
13444 else if (attr
->form_is_section_offset ())
13445 dwarf2_complex_location_expr_complaint ();
13446 else if (attr
->form_is_block ())
13447 *offset
= decode_locdesc (attr
->as_block (), cu
);
13449 dwarf2_complex_location_expr_complaint ();
13455 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13456 if (attr
!= nullptr)
13458 *offset
= attr
->constant_value (0);
13466 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13467 store the results in FIELD. */
13470 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13471 struct field
*field
)
13473 struct attribute
*attr
;
13475 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13478 if (attr
->form_is_constant ())
13480 LONGEST offset
= attr
->constant_value (0);
13482 /* Work around this GCC 11 bug, where it would erroneously use -1
13483 data member locations, instead of 0:
13485 Negative DW_AT_data_member_location
13486 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13488 if (offset
== -1 && cu
->producer_is_gcc_11
)
13490 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13494 field
->set_loc_bitpos (offset
* bits_per_byte
);
13496 else if (attr
->form_is_section_offset ())
13497 dwarf2_complex_location_expr_complaint ();
13498 else if (attr
->form_is_block ())
13501 CORE_ADDR offset
= decode_locdesc (attr
->as_block (), cu
, &handled
);
13503 field
->set_loc_bitpos (offset
* bits_per_byte
);
13506 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13507 struct objfile
*objfile
= per_objfile
->objfile
;
13508 struct dwarf2_locexpr_baton
*dlbaton
13509 = XOBNEW (&objfile
->objfile_obstack
,
13510 struct dwarf2_locexpr_baton
);
13511 dlbaton
->data
= attr
->as_block ()->data
;
13512 dlbaton
->size
= attr
->as_block ()->size
;
13513 /* When using this baton, we want to compute the address
13514 of the field, not the value. This is why
13515 is_reference is set to false here. */
13516 dlbaton
->is_reference
= false;
13517 dlbaton
->per_objfile
= per_objfile
;
13518 dlbaton
->per_cu
= cu
->per_cu
;
13520 field
->set_loc_dwarf_block (dlbaton
);
13524 dwarf2_complex_location_expr_complaint ();
13528 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13529 if (attr
!= nullptr)
13530 field
->set_loc_bitpos (attr
->constant_value (0));
13534 /* Add an aggregate field to the field list. */
13537 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
13538 struct dwarf2_cu
*cu
)
13540 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13541 struct gdbarch
*gdbarch
= objfile
->arch ();
13542 struct nextfield
*new_field
;
13543 struct attribute
*attr
;
13545 const char *fieldname
= "";
13547 if (die
->tag
== DW_TAG_inheritance
)
13549 fip
->baseclasses
.emplace_back ();
13550 new_field
= &fip
->baseclasses
.back ();
13554 fip
->fields
.emplace_back ();
13555 new_field
= &fip
->fields
.back ();
13558 new_field
->offset
= die
->sect_off
;
13560 new_field
->accessibility
= dwarf2_access_attribute (die
, cu
);
13561 if (new_field
->accessibility
!= DW_ACCESS_public
)
13562 fip
->non_public_fields
= true;
13564 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
13565 if (attr
!= nullptr)
13566 new_field
->virtuality
= attr
->as_virtuality ();
13568 new_field
->virtuality
= DW_VIRTUALITY_none
;
13570 fp
= &new_field
->field
;
13572 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
13573 && !die_is_declaration (die
, cu
))
13575 if (die
->tag
== DW_TAG_namelist_item
)
13577 /* Typically, DW_TAG_namelist_item are references to namelist items.
13578 If so, follow that reference. */
13579 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
13580 struct die_info
*item_die
= nullptr;
13581 struct dwarf2_cu
*item_cu
= cu
;
13582 if (attr1
->form_is_ref ())
13583 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
13584 if (item_die
!= nullptr)
13587 /* Data member other than a C++ static data member. */
13589 /* Get type of field. */
13590 fp
->set_type (die_type (die
, cu
));
13592 fp
->set_loc_bitpos (0);
13594 /* Get bit size of field (zero if none). */
13595 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
13596 if (attr
!= nullptr)
13598 FIELD_BITSIZE (*fp
) = attr
->constant_value (0);
13602 FIELD_BITSIZE (*fp
) = 0;
13605 /* Get bit offset of field. */
13606 handle_member_location (die
, cu
, fp
);
13607 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
13608 if (attr
!= nullptr && attr
->form_is_constant ())
13610 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
13612 /* For big endian bits, the DW_AT_bit_offset gives the
13613 additional bit offset from the MSB of the containing
13614 anonymous object to the MSB of the field. We don't
13615 have to do anything special since we don't need to
13616 know the size of the anonymous object. */
13617 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
13621 /* For little endian bits, compute the bit offset to the
13622 MSB of the anonymous object, subtract off the number of
13623 bits from the MSB of the field to the MSB of the
13624 object, and then subtract off the number of bits of
13625 the field itself. The result is the bit offset of
13626 the LSB of the field. */
13627 int anonymous_size
;
13628 int bit_offset
= attr
->constant_value (0);
13630 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13631 if (attr
!= nullptr && attr
->form_is_constant ())
13633 /* The size of the anonymous object containing
13634 the bit field is explicit, so use the
13635 indicated size (in bytes). */
13636 anonymous_size
= attr
->constant_value (0);
13640 /* The size of the anonymous object containing
13641 the bit field must be inferred from the type
13642 attribute of the data member containing the
13644 anonymous_size
= fp
->type ()->length ();
13646 fp
->set_loc_bitpos (fp
->loc_bitpos ()
13647 + anonymous_size
* bits_per_byte
13648 - bit_offset
- FIELD_BITSIZE (*fp
));
13652 /* Get name of field. */
13653 fieldname
= dwarf2_name (die
, cu
);
13654 if (fieldname
== NULL
)
13657 /* The name is already allocated along with this objfile, so we don't
13658 need to duplicate it for the type. */
13659 fp
->set_name (fieldname
);
13661 /* Change accessibility for artificial fields (e.g. virtual table
13662 pointer or virtual base class pointer) to private. */
13663 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
13665 FIELD_ARTIFICIAL (*fp
) = 1;
13666 new_field
->accessibility
= DW_ACCESS_private
;
13667 fip
->non_public_fields
= true;
13670 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
13672 /* C++ static member. */
13674 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13675 is a declaration, but all versions of G++ as of this writing
13676 (so through at least 3.2.1) incorrectly generate
13677 DW_TAG_variable tags. */
13679 const char *physname
;
13681 /* Get name of field. */
13682 fieldname
= dwarf2_name (die
, cu
);
13683 if (fieldname
== NULL
)
13686 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
13688 /* Only create a symbol if this is an external value.
13689 new_symbol checks this and puts the value in the global symbol
13690 table, which we want. If it is not external, new_symbol
13691 will try to put the value in cu->list_in_scope which is wrong. */
13692 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
13694 /* A static const member, not much different than an enum as far as
13695 we're concerned, except that we can support more types. */
13696 new_symbol (die
, NULL
, cu
);
13699 /* Get physical name. */
13700 physname
= dwarf2_physname (fieldname
, die
, cu
);
13702 /* The name is already allocated along with this objfile, so we don't
13703 need to duplicate it for the type. */
13704 fp
->set_loc_physname (physname
? physname
: "");
13705 fp
->set_type (die_type (die
, cu
));
13706 fp
->set_name (fieldname
);
13708 else if (die
->tag
== DW_TAG_inheritance
)
13710 /* C++ base class field. */
13711 handle_member_location (die
, cu
, fp
);
13712 FIELD_BITSIZE (*fp
) = 0;
13713 fp
->set_type (die_type (die
, cu
));
13714 fp
->set_name (fp
->type ()->name ());
13717 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13720 /* Can the type given by DIE define another type? */
13723 type_can_define_types (const struct die_info
*die
)
13727 case DW_TAG_typedef
:
13728 case DW_TAG_class_type
:
13729 case DW_TAG_structure_type
:
13730 case DW_TAG_union_type
:
13731 case DW_TAG_enumeration_type
:
13739 /* Add a type definition defined in the scope of the FIP's class. */
13742 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
13743 struct dwarf2_cu
*cu
)
13745 struct decl_field fp
;
13746 memset (&fp
, 0, sizeof (fp
));
13748 gdb_assert (type_can_define_types (die
));
13750 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13751 fp
.name
= dwarf2_name (die
, cu
);
13752 fp
.type
= read_type_die (die
, cu
);
13754 /* Save accessibility. */
13755 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
13756 switch (accessibility
)
13758 case DW_ACCESS_public
:
13759 /* The assumed value if neither private nor protected. */
13761 case DW_ACCESS_private
:
13764 case DW_ACCESS_protected
:
13765 fp
.is_protected
= 1;
13769 if (die
->tag
== DW_TAG_typedef
)
13770 fip
->typedef_field_list
.push_back (fp
);
13772 fip
->nested_types_list
.push_back (fp
);
13775 /* A convenience typedef that's used when finding the discriminant
13776 field for a variant part. */
13777 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
13780 /* Compute the discriminant range for a given variant. OBSTACK is
13781 where the results will be stored. VARIANT is the variant to
13782 process. IS_UNSIGNED indicates whether the discriminant is signed
13785 static const gdb::array_view
<discriminant_range
>
13786 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
13789 std::vector
<discriminant_range
> ranges
;
13791 if (variant
.default_branch
)
13794 if (variant
.discr_list_data
== nullptr)
13796 discriminant_range r
13797 = {variant
.discriminant_value
, variant
.discriminant_value
};
13798 ranges
.push_back (r
);
13802 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
13803 variant
.discr_list_data
->size
);
13804 while (!data
.empty ())
13806 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
13808 complaint (_("invalid discriminant marker: %d"), data
[0]);
13811 bool is_range
= data
[0] == DW_DSC_range
;
13812 data
= data
.slice (1);
13814 ULONGEST low
, high
;
13815 unsigned int bytes_read
;
13819 complaint (_("DW_AT_discr_list missing low value"));
13823 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
13825 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
13827 data
= data
.slice (bytes_read
);
13833 complaint (_("DW_AT_discr_list missing high value"));
13837 high
= read_unsigned_leb128 (nullptr, data
.data (),
13840 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
13842 data
= data
.slice (bytes_read
);
13847 ranges
.push_back ({ low
, high
});
13851 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
13853 std::copy (ranges
.begin (), ranges
.end (), result
);
13854 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
13857 static const gdb::array_view
<variant_part
> create_variant_parts
13858 (struct obstack
*obstack
,
13859 const offset_map_type
&offset_map
,
13860 struct field_info
*fi
,
13861 const std::vector
<variant_part_builder
> &variant_parts
);
13863 /* Fill in a "struct variant" for a given variant field. RESULT is
13864 the variant to fill in. OBSTACK is where any needed allocations
13865 will be done. OFFSET_MAP holds the mapping from section offsets to
13866 fields for the type. FI describes the fields of the type we're
13867 processing. FIELD is the variant field we're converting. */
13870 create_one_variant (variant
&result
, struct obstack
*obstack
,
13871 const offset_map_type
&offset_map
,
13872 struct field_info
*fi
, const variant_field
&field
)
13874 result
.discriminants
= convert_variant_range (obstack
, field
, false);
13875 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
13876 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
13877 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
13878 field
.variant_parts
);
13881 /* Fill in a "struct variant_part" for a given variant part. RESULT
13882 is the variant part to fill in. OBSTACK is where any needed
13883 allocations will be done. OFFSET_MAP holds the mapping from
13884 section offsets to fields for the type. FI describes the fields of
13885 the type we're processing. BUILDER is the variant part to be
13889 create_one_variant_part (variant_part
&result
,
13890 struct obstack
*obstack
,
13891 const offset_map_type
&offset_map
,
13892 struct field_info
*fi
,
13893 const variant_part_builder
&builder
)
13895 auto iter
= offset_map
.find (builder
.discriminant_offset
);
13896 if (iter
== offset_map
.end ())
13898 result
.discriminant_index
= -1;
13899 /* Doesn't matter. */
13900 result
.is_unsigned
= false;
13904 result
.discriminant_index
= iter
->second
;
13906 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
13909 size_t n
= builder
.variants
.size ();
13910 variant
*output
= new (obstack
) variant
[n
];
13911 for (size_t i
= 0; i
< n
; ++i
)
13912 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
13913 builder
.variants
[i
]);
13915 result
.variants
= gdb::array_view
<variant
> (output
, n
);
13918 /* Create a vector of variant parts that can be attached to a type.
13919 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13920 holds the mapping from section offsets to fields for the type. FI
13921 describes the fields of the type we're processing. VARIANT_PARTS
13922 is the vector to convert. */
13924 static const gdb::array_view
<variant_part
>
13925 create_variant_parts (struct obstack
*obstack
,
13926 const offset_map_type
&offset_map
,
13927 struct field_info
*fi
,
13928 const std::vector
<variant_part_builder
> &variant_parts
)
13930 if (variant_parts
.empty ())
13933 size_t n
= variant_parts
.size ();
13934 variant_part
*result
= new (obstack
) variant_part
[n
];
13935 for (size_t i
= 0; i
< n
; ++i
)
13936 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
13939 return gdb::array_view
<variant_part
> (result
, n
);
13942 /* Compute the variant part vector for FIP, attaching it to TYPE when
13946 add_variant_property (struct field_info
*fip
, struct type
*type
,
13947 struct dwarf2_cu
*cu
)
13949 /* Map section offsets of fields to their field index. Note the
13950 field index here does not take the number of baseclasses into
13952 offset_map_type offset_map
;
13953 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
13954 offset_map
[fip
->fields
[i
].offset
] = i
;
13956 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13957 gdb::array_view
<const variant_part
> parts
13958 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
13959 fip
->variant_parts
);
13961 struct dynamic_prop prop
;
13962 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
13963 obstack_copy (&objfile
->objfile_obstack
, &parts
,
13966 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
13969 /* Create the vector of fields, and attach it to the type. */
13972 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
13973 struct dwarf2_cu
*cu
)
13975 int nfields
= fip
->nfields ();
13977 /* Record the field count, allocate space for the array of fields,
13978 and create blank accessibility bitfields if necessary. */
13979 type
->set_num_fields (nfields
);
13981 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
13983 if (fip
->non_public_fields
&& cu
->lang () != language_ada
)
13985 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13987 TYPE_FIELD_PRIVATE_BITS (type
) =
13988 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13989 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type
), nfields
);
13991 TYPE_FIELD_PROTECTED_BITS (type
) =
13992 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13993 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type
), nfields
);
13995 TYPE_FIELD_IGNORE_BITS (type
) =
13996 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13997 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type
), nfields
);
14000 /* If the type has baseclasses, allocate and clear a bit vector for
14001 TYPE_FIELD_VIRTUAL_BITS. */
14002 if (!fip
->baseclasses
.empty () && cu
->lang () != language_ada
)
14004 int num_bytes
= B_BYTES (fip
->baseclasses
.size ());
14005 unsigned char *pointer
;
14007 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14008 pointer
= (unsigned char *) TYPE_ALLOC (type
, num_bytes
);
14009 TYPE_FIELD_VIRTUAL_BITS (type
) = pointer
;
14010 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type
), fip
->baseclasses
.size ());
14011 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
14014 if (!fip
->variant_parts
.empty ())
14015 add_variant_property (fip
, type
, cu
);
14017 /* Copy the saved-up fields into the field vector. */
14018 for (int i
= 0; i
< nfields
; ++i
)
14020 struct nextfield
&field
14021 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
14022 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
14024 type
->field (i
) = field
.field
;
14025 switch (field
.accessibility
)
14027 case DW_ACCESS_private
:
14028 if (cu
->lang () != language_ada
)
14029 SET_TYPE_FIELD_PRIVATE (type
, i
);
14032 case DW_ACCESS_protected
:
14033 if (cu
->lang () != language_ada
)
14034 SET_TYPE_FIELD_PROTECTED (type
, i
);
14037 case DW_ACCESS_public
:
14041 /* Unknown accessibility. Complain and treat it as public. */
14043 complaint (_("unsupported accessibility %d"),
14044 field
.accessibility
);
14048 if (i
< fip
->baseclasses
.size ())
14050 switch (field
.virtuality
)
14052 case DW_VIRTUALITY_virtual
:
14053 case DW_VIRTUALITY_pure_virtual
:
14054 if (cu
->lang () == language_ada
)
14055 error (_("unexpected virtuality in component of Ada type"));
14056 SET_TYPE_FIELD_VIRTUAL (type
, i
);
14063 /* Return true if this member function is a constructor, false
14067 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
14069 const char *fieldname
;
14070 const char *type_name
;
14073 if (die
->parent
== NULL
)
14076 if (die
->parent
->tag
!= DW_TAG_structure_type
14077 && die
->parent
->tag
!= DW_TAG_union_type
14078 && die
->parent
->tag
!= DW_TAG_class_type
)
14081 fieldname
= dwarf2_name (die
, cu
);
14082 type_name
= dwarf2_name (die
->parent
, cu
);
14083 if (fieldname
== NULL
|| type_name
== NULL
)
14086 len
= strlen (fieldname
);
14087 return (strncmp (fieldname
, type_name
, len
) == 0
14088 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
14091 /* Add a member function to the proper fieldlist. */
14094 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
14095 struct type
*type
, struct dwarf2_cu
*cu
)
14097 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14098 struct attribute
*attr
;
14100 struct fnfieldlist
*flp
= nullptr;
14101 struct fn_field
*fnp
;
14102 const char *fieldname
;
14103 struct type
*this_type
;
14105 if (cu
->lang () == language_ada
)
14106 error (_("unexpected member function in Ada type"));
14108 /* Get name of member function. */
14109 fieldname
= dwarf2_name (die
, cu
);
14110 if (fieldname
== NULL
)
14113 /* Look up member function name in fieldlist. */
14114 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14116 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
14118 flp
= &fip
->fnfieldlists
[i
];
14123 /* Create a new fnfieldlist if necessary. */
14124 if (flp
== nullptr)
14126 fip
->fnfieldlists
.emplace_back ();
14127 flp
= &fip
->fnfieldlists
.back ();
14128 flp
->name
= fieldname
;
14129 i
= fip
->fnfieldlists
.size () - 1;
14132 /* Create a new member function field and add it to the vector of
14134 flp
->fnfields
.emplace_back ();
14135 fnp
= &flp
->fnfields
.back ();
14137 /* Delay processing of the physname until later. */
14138 if (cu
->lang () == language_cplus
)
14139 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
14143 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
14144 fnp
->physname
= physname
? physname
: "";
14147 fnp
->type
= alloc_type (objfile
);
14148 this_type
= read_type_die (die
, cu
);
14149 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
14151 int nparams
= this_type
->num_fields ();
14153 /* TYPE is the domain of this method, and THIS_TYPE is the type
14154 of the method itself (TYPE_CODE_METHOD). */
14155 smash_to_method_type (fnp
->type
, type
,
14156 this_type
->target_type (),
14157 this_type
->fields (),
14158 this_type
->num_fields (),
14159 this_type
->has_varargs ());
14161 /* Handle static member functions.
14162 Dwarf2 has no clean way to discern C++ static and non-static
14163 member functions. G++ helps GDB by marking the first
14164 parameter for non-static member functions (which is the this
14165 pointer) as artificial. We obtain this information from
14166 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14167 if (nparams
== 0 || TYPE_FIELD_ARTIFICIAL (this_type
, 0) == 0)
14168 fnp
->voffset
= VOFFSET_STATIC
;
14171 complaint (_("member function type missing for '%s'"),
14172 dwarf2_full_name (fieldname
, die
, cu
));
14174 /* Get fcontext from DW_AT_containing_type if present. */
14175 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14176 fnp
->fcontext
= die_containing_type (die
, cu
);
14178 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14179 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14181 /* Get accessibility. */
14182 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
14183 switch (accessibility
)
14185 case DW_ACCESS_private
:
14186 fnp
->is_private
= 1;
14188 case DW_ACCESS_protected
:
14189 fnp
->is_protected
= 1;
14193 /* Check for artificial methods. */
14194 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
14195 if (attr
&& attr
->as_boolean ())
14196 fnp
->is_artificial
= 1;
14198 /* Check for defaulted methods. */
14199 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
14200 if (attr
!= nullptr)
14201 fnp
->defaulted
= attr
->defaulted ();
14203 /* Check for deleted methods. */
14204 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
14205 if (attr
!= nullptr && attr
->as_boolean ())
14206 fnp
->is_deleted
= 1;
14208 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
14210 /* Get index in virtual function table if it is a virtual member
14211 function. For older versions of GCC, this is an offset in the
14212 appropriate virtual table, as specified by DW_AT_containing_type.
14213 For everyone else, it is an expression to be evaluated relative
14214 to the object address. */
14216 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
14217 if (attr
!= nullptr)
14219 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
14221 struct dwarf_block
*block
= attr
->as_block ();
14223 if (block
->data
[0] == DW_OP_constu
)
14225 /* Old-style GCC. */
14226 fnp
->voffset
= decode_locdesc (block
, cu
) + 2;
14228 else if (block
->data
[0] == DW_OP_deref
14229 || (block
->size
> 1
14230 && block
->data
[0] == DW_OP_deref_size
14231 && block
->data
[1] == cu
->header
.addr_size
))
14233 fnp
->voffset
= decode_locdesc (block
, cu
);
14234 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
14235 dwarf2_complex_location_expr_complaint ();
14237 fnp
->voffset
/= cu
->header
.addr_size
;
14241 dwarf2_complex_location_expr_complaint ();
14243 if (!fnp
->fcontext
)
14245 /* If there is no `this' field and no DW_AT_containing_type,
14246 we cannot actually find a base class context for the
14248 if (this_type
->num_fields () == 0
14249 || !TYPE_FIELD_ARTIFICIAL (this_type
, 0))
14251 complaint (_("cannot determine context for virtual member "
14252 "function \"%s\" (offset %s)"),
14253 fieldname
, sect_offset_str (die
->sect_off
));
14257 fnp
->fcontext
= this_type
->field (0).type ()->target_type ();
14261 else if (attr
->form_is_section_offset ())
14263 dwarf2_complex_location_expr_complaint ();
14267 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14273 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
14274 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
14276 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14277 complaint (_("Member function \"%s\" (offset %s) is virtual "
14278 "but the vtable offset is not specified"),
14279 fieldname
, sect_offset_str (die
->sect_off
));
14280 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14281 TYPE_CPLUS_DYNAMIC (type
) = 1;
14286 /* Create the vector of member function fields, and attach it to the type. */
14289 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
14290 struct dwarf2_cu
*cu
)
14292 if (cu
->lang () == language_ada
)
14293 error (_("unexpected member functions in Ada type"));
14295 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14296 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
14298 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
14300 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14302 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
14303 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
14305 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
14306 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
14307 fn_flp
->fn_fields
= (struct fn_field
*)
14308 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
14310 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
14311 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
14314 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
14317 /* Returns non-zero if NAME is the name of a vtable member in CU's
14318 language, zero otherwise. */
14320 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
14322 static const char vptr
[] = "_vptr";
14324 /* Look for the C++ form of the vtable. */
14325 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
14331 /* GCC outputs unnamed structures that are really pointers to member
14332 functions, with the ABI-specified layout. If TYPE describes
14333 such a structure, smash it into a member function type.
14335 GCC shouldn't do this; it should just output pointer to member DIEs.
14336 This is GCC PR debug/28767. */
14339 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
14341 struct type
*pfn_type
, *self_type
, *new_type
;
14343 /* Check for a structure with no name and two children. */
14344 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14347 /* Check for __pfn and __delta members. */
14348 if (type
->field (0).name () == NULL
14349 || strcmp (type
->field (0).name (), "__pfn") != 0
14350 || type
->field (1).name () == NULL
14351 || strcmp (type
->field (1).name (), "__delta") != 0)
14354 /* Find the type of the method. */
14355 pfn_type
= type
->field (0).type ();
14356 if (pfn_type
== NULL
14357 || pfn_type
->code () != TYPE_CODE_PTR
14358 || pfn_type
->target_type ()->code () != TYPE_CODE_FUNC
)
14361 /* Look for the "this" argument. */
14362 pfn_type
= pfn_type
->target_type ();
14363 if (pfn_type
->num_fields () == 0
14364 /* || pfn_type->field (0).type () == NULL */
14365 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14368 self_type
= pfn_type
->field (0).type ()->target_type ();
14369 new_type
= alloc_type (objfile
);
14370 smash_to_method_type (new_type
, self_type
, pfn_type
->target_type (),
14371 pfn_type
->fields (), pfn_type
->num_fields (),
14372 pfn_type
->has_varargs ());
14373 smash_to_methodptr_type (type
, new_type
);
14376 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14377 requires rewriting, then copy it and return the updated copy.
14378 Otherwise return nullptr. */
14380 static struct type
*
14381 rewrite_array_type (struct type
*type
)
14383 if (type
->code () != TYPE_CODE_ARRAY
)
14386 struct type
*index_type
= type
->index_type ();
14387 range_bounds
*current_bounds
= index_type
->bounds ();
14389 /* Handle multi-dimensional arrays. */
14390 struct type
*new_target
= rewrite_array_type (type
->target_type ());
14391 if (new_target
== nullptr)
14393 /* Maybe we don't need to rewrite this array. */
14394 if (current_bounds
->low
.kind () == PROP_CONST
14395 && current_bounds
->high
.kind () == PROP_CONST
)
14399 /* Either the target type was rewritten, or the bounds have to be
14400 updated. Either way we want to copy the type and update
14402 struct type
*copy
= copy_type (type
);
14403 int nfields
= copy
->num_fields ();
14405 = ((struct field
*) TYPE_ZALLOC (copy
,
14406 nfields
* sizeof (struct field
)));
14407 memcpy (new_fields
, copy
->fields (), nfields
* sizeof (struct field
));
14408 copy
->set_fields (new_fields
);
14409 if (new_target
!= nullptr)
14410 copy
->set_target_type (new_target
);
14412 struct type
*index_copy
= copy_type (index_type
);
14413 range_bounds
*bounds
14414 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
14415 sizeof (range_bounds
));
14416 *bounds
= *current_bounds
;
14417 bounds
->low
.set_const_val (1);
14418 bounds
->high
.set_const_val (0);
14419 index_copy
->set_bounds (bounds
);
14420 copy
->set_index_type (index_copy
);
14425 /* While some versions of GCC will generate complicated DWARF for an
14426 array (see quirk_ada_thick_pointer), more recent versions were
14427 modified to emit an explicit thick pointer structure. However, in
14428 this case, the array still has DWARF expressions for its ranges,
14429 and these must be ignored. */
14432 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
14435 gdb_assert (cu
->lang () == language_ada
);
14437 /* Check for a structure with two children. */
14438 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14441 /* Check for P_ARRAY and P_BOUNDS members. */
14442 if (type
->field (0).name () == NULL
14443 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
14444 || type
->field (1).name () == NULL
14445 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
14448 /* Make sure we're looking at a pointer to an array. */
14449 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14452 /* The Ada code already knows how to handle these types, so all that
14453 we need to do is turn the bounds into static bounds. However, we
14454 don't want to rewrite existing array or index types in-place,
14455 because those may be referenced in other contexts where this
14456 rewriting is undesirable. */
14457 struct type
*new_ary_type
14458 = rewrite_array_type (type
->field (0).type ()->target_type ());
14459 if (new_ary_type
!= nullptr)
14460 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
14463 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14464 appropriate error checking and issuing complaints if there is a
14468 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
14470 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
14472 if (attr
== nullptr)
14475 if (!attr
->form_is_constant ())
14477 complaint (_("DW_AT_alignment must have constant form"
14478 " - DIE at %s [in module %s]"),
14479 sect_offset_str (die
->sect_off
),
14480 objfile_name (cu
->per_objfile
->objfile
));
14484 LONGEST val
= attr
->constant_value (0);
14487 complaint (_("DW_AT_alignment value must not be negative"
14488 " - DIE at %s [in module %s]"),
14489 sect_offset_str (die
->sect_off
),
14490 objfile_name (cu
->per_objfile
->objfile
));
14493 ULONGEST align
= val
;
14497 complaint (_("DW_AT_alignment value must not be zero"
14498 " - DIE at %s [in module %s]"),
14499 sect_offset_str (die
->sect_off
),
14500 objfile_name (cu
->per_objfile
->objfile
));
14503 if ((align
& (align
- 1)) != 0)
14505 complaint (_("DW_AT_alignment value must be a power of 2"
14506 " - DIE at %s [in module %s]"),
14507 sect_offset_str (die
->sect_off
),
14508 objfile_name (cu
->per_objfile
->objfile
));
14515 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14516 the alignment for TYPE. */
14519 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
14522 if (!set_type_align (type
, get_alignment (cu
, die
)))
14523 complaint (_("DW_AT_alignment value too large"
14524 " - DIE at %s [in module %s]"),
14525 sect_offset_str (die
->sect_off
),
14526 objfile_name (cu
->per_objfile
->objfile
));
14529 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14530 constant for a type, according to DWARF5 spec, Table 5.5. */
14533 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
14538 case DW_CC_pass_by_reference
:
14539 case DW_CC_pass_by_value
:
14543 complaint (_("unrecognized DW_AT_calling_convention value "
14544 "(%s) for a type"), pulongest (value
));
14549 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14550 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14551 also according to GNU-specific values (see include/dwarf2.h). */
14554 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
14559 case DW_CC_program
:
14563 case DW_CC_GNU_renesas_sh
:
14564 case DW_CC_GNU_borland_fastcall_i386
:
14565 case DW_CC_GDB_IBM_OpenCL
:
14569 complaint (_("unrecognized DW_AT_calling_convention value "
14570 "(%s) for a subroutine"), pulongest (value
));
14575 /* Called when we find the DIE that starts a structure or union scope
14576 (definition) to create a type for the structure or union. Fill in
14577 the type's name and general properties; the members will not be
14578 processed until process_structure_scope. A symbol table entry for
14579 the type will also not be done until process_structure_scope (assuming
14580 the type has a name).
14582 NOTE: we need to call these functions regardless of whether or not the
14583 DIE has a DW_AT_name attribute, since it might be an anonymous
14584 structure or union. This gets the type entered into our set of
14585 user defined types. */
14587 static struct type
*
14588 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14590 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14592 struct attribute
*attr
;
14595 /* If the definition of this type lives in .debug_types, read that type.
14596 Don't follow DW_AT_specification though, that will take us back up
14597 the chain and we want to go down. */
14598 attr
= die
->attr (DW_AT_signature
);
14599 if (attr
!= nullptr)
14601 type
= get_DW_AT_signature_type (die
, attr
, cu
);
14603 /* The type's CU may not be the same as CU.
14604 Ensure TYPE is recorded with CU in die_type_hash. */
14605 return set_die_type (die
, type
, cu
);
14608 type
= alloc_type (objfile
);
14609 INIT_CPLUS_SPECIFIC (type
);
14611 name
= dwarf2_name (die
, cu
);
14614 if (cu
->lang () == language_cplus
14615 || cu
->lang () == language_d
14616 || cu
->lang () == language_rust
)
14618 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
14620 /* dwarf2_full_name might have already finished building the DIE's
14621 type. If so, there is no need to continue. */
14622 if (get_die_type (die
, cu
) != NULL
)
14623 return get_die_type (die
, cu
);
14625 type
->set_name (full_name
);
14629 /* The name is already allocated along with this objfile, so
14630 we don't need to duplicate it for the type. */
14631 type
->set_name (name
);
14635 if (die
->tag
== DW_TAG_structure_type
)
14637 type
->set_code (TYPE_CODE_STRUCT
);
14639 else if (die
->tag
== DW_TAG_union_type
)
14641 type
->set_code (TYPE_CODE_UNION
);
14643 else if (die
->tag
== DW_TAG_namelist
)
14645 type
->set_code (TYPE_CODE_NAMELIST
);
14649 type
->set_code (TYPE_CODE_STRUCT
);
14652 if (cu
->lang () == language_cplus
&& die
->tag
== DW_TAG_class_type
)
14653 type
->set_is_declared_class (true);
14655 /* Store the calling convention in the type if it's available in
14656 the die. Otherwise the calling convention remains set to
14657 the default value DW_CC_normal. */
14658 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14659 if (attr
!= nullptr
14660 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
14662 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14663 TYPE_CPLUS_CALLING_CONVENTION (type
)
14664 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
14667 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14668 if (attr
!= nullptr)
14670 if (attr
->form_is_constant ())
14671 type
->set_length (attr
->constant_value (0));
14674 struct dynamic_prop prop
;
14675 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
14676 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
14678 type
->set_length (0);
14682 type
->set_length (0);
14684 maybe_set_alignment (cu
, die
, type
);
14686 if (producer_is_icc_lt_14 (cu
) && (type
->length () == 0))
14688 /* ICC<14 does not output the required DW_AT_declaration on
14689 incomplete types, but gives them a size of zero. */
14690 type
->set_is_stub (true);
14693 type
->set_stub_is_supported (true);
14695 if (die_is_declaration (die
, cu
))
14696 type
->set_is_stub (true);
14697 else if (attr
== NULL
&& die
->child
== NULL
14698 && producer_is_realview (cu
->producer
))
14699 /* RealView does not output the required DW_AT_declaration
14700 on incomplete types. */
14701 type
->set_is_stub (true);
14703 /* We need to add the type field to the die immediately so we don't
14704 infinitely recurse when dealing with pointers to the structure
14705 type within the structure itself. */
14706 set_die_type (die
, type
, cu
);
14708 /* set_die_type should be already done. */
14709 set_descriptive_type (type
, die
, cu
);
14714 static void handle_struct_member_die
14715 (struct die_info
*child_die
,
14717 struct field_info
*fi
,
14718 std::vector
<struct symbol
*> *template_args
,
14719 struct dwarf2_cu
*cu
);
14721 /* A helper for handle_struct_member_die that handles
14722 DW_TAG_variant_part. */
14725 handle_variant_part (struct die_info
*die
, struct type
*type
,
14726 struct field_info
*fi
,
14727 std::vector
<struct symbol
*> *template_args
,
14728 struct dwarf2_cu
*cu
)
14730 variant_part_builder
*new_part
;
14731 if (fi
->current_variant_part
== nullptr)
14733 fi
->variant_parts
.emplace_back ();
14734 new_part
= &fi
->variant_parts
.back ();
14736 else if (!fi
->current_variant_part
->processing_variant
)
14738 complaint (_("nested DW_TAG_variant_part seen "
14739 "- DIE at %s [in module %s]"),
14740 sect_offset_str (die
->sect_off
),
14741 objfile_name (cu
->per_objfile
->objfile
));
14746 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
14747 current
.variant_parts
.emplace_back ();
14748 new_part
= ¤t
.variant_parts
.back ();
14751 /* When we recurse, we want callees to add to this new variant
14753 scoped_restore save_current_variant_part
14754 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
14756 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
14759 /* It's a univariant form, an extension we support. */
14761 else if (discr
->form_is_ref ())
14763 struct dwarf2_cu
*target_cu
= cu
;
14764 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
14766 new_part
->discriminant_offset
= target_die
->sect_off
;
14770 complaint (_("DW_AT_discr does not have DIE reference form"
14771 " - DIE at %s [in module %s]"),
14772 sect_offset_str (die
->sect_off
),
14773 objfile_name (cu
->per_objfile
->objfile
));
14776 for (die_info
*child_die
= die
->child
;
14778 child_die
= child_die
->sibling
)
14779 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
14782 /* A helper for handle_struct_member_die that handles
14786 handle_variant (struct die_info
*die
, struct type
*type
,
14787 struct field_info
*fi
,
14788 std::vector
<struct symbol
*> *template_args
,
14789 struct dwarf2_cu
*cu
)
14791 if (fi
->current_variant_part
== nullptr)
14793 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14794 "- DIE at %s [in module %s]"),
14795 sect_offset_str (die
->sect_off
),
14796 objfile_name (cu
->per_objfile
->objfile
));
14799 if (fi
->current_variant_part
->processing_variant
)
14801 complaint (_("nested DW_TAG_variant seen "
14802 "- DIE at %s [in module %s]"),
14803 sect_offset_str (die
->sect_off
),
14804 objfile_name (cu
->per_objfile
->objfile
));
14808 scoped_restore save_processing_variant
14809 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
14812 fi
->current_variant_part
->variants
.emplace_back ();
14813 variant_field
&variant
= fi
->current_variant_part
->variants
.back ();
14814 variant
.first_field
= fi
->fields
.size ();
14816 /* In a variant we want to get the discriminant and also add a
14817 field for our sole member child. */
14818 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
14819 if (discr
== nullptr || !discr
->form_is_constant ())
14821 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
14822 if (discr
== nullptr || discr
->as_block ()->size
== 0)
14823 variant
.default_branch
= true;
14825 variant
.discr_list_data
= discr
->as_block ();
14828 variant
.discriminant_value
= discr
->constant_value (0);
14830 for (die_info
*variant_child
= die
->child
;
14831 variant_child
!= NULL
;
14832 variant_child
= variant_child
->sibling
)
14833 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
14835 variant
.last_field
= fi
->fields
.size ();
14838 /* A helper for process_structure_scope that handles a single member
14842 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
14843 struct field_info
*fi
,
14844 std::vector
<struct symbol
*> *template_args
,
14845 struct dwarf2_cu
*cu
)
14847 if (child_die
->tag
== DW_TAG_member
14848 || child_die
->tag
== DW_TAG_variable
14849 || child_die
->tag
== DW_TAG_namelist_item
)
14851 /* NOTE: carlton/2002-11-05: A C++ static data member
14852 should be a DW_TAG_member that is a declaration, but
14853 all versions of G++ as of this writing (so through at
14854 least 3.2.1) incorrectly generate DW_TAG_variable
14855 tags for them instead. */
14856 dwarf2_add_field (fi
, child_die
, cu
);
14858 else if (child_die
->tag
== DW_TAG_subprogram
)
14860 /* Rust doesn't have member functions in the C++ sense.
14861 However, it does emit ordinary functions as children
14862 of a struct DIE. */
14863 if (cu
->lang () == language_rust
)
14864 read_func_scope (child_die
, cu
);
14867 /* C++ member function. */
14868 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
14871 else if (child_die
->tag
== DW_TAG_inheritance
)
14873 /* C++ base class field. */
14874 dwarf2_add_field (fi
, child_die
, cu
);
14876 else if (type_can_define_types (child_die
))
14877 dwarf2_add_type_defn (fi
, child_die
, cu
);
14878 else if (child_die
->tag
== DW_TAG_template_type_param
14879 || child_die
->tag
== DW_TAG_template_value_param
)
14881 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
14884 template_args
->push_back (arg
);
14886 else if (child_die
->tag
== DW_TAG_variant_part
)
14887 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
14888 else if (child_die
->tag
== DW_TAG_variant
)
14889 handle_variant (child_die
, type
, fi
, template_args
, cu
);
14892 /* Finish creating a structure or union type, including filling in its
14893 members and creating a symbol for it. This function also handles Fortran
14894 namelist variables, their items or members and creating a symbol for
14898 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
14900 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14901 struct die_info
*child_die
;
14904 type
= get_die_type (die
, cu
);
14906 type
= read_structure_type (die
, cu
);
14908 bool has_template_parameters
= false;
14909 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
14911 struct field_info fi
;
14912 std::vector
<struct symbol
*> template_args
;
14914 child_die
= die
->child
;
14916 while (child_die
&& child_die
->tag
)
14918 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
14919 child_die
= child_die
->sibling
;
14922 /* Attach template arguments to type. */
14923 if (!template_args
.empty ())
14925 has_template_parameters
= true;
14926 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14927 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
14928 TYPE_TEMPLATE_ARGUMENTS (type
)
14929 = XOBNEWVEC (&objfile
->objfile_obstack
,
14931 TYPE_N_TEMPLATE_ARGUMENTS (type
));
14932 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
14933 template_args
.data (),
14934 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
14935 * sizeof (struct symbol
*)));
14938 /* Attach fields and member functions to the type. */
14939 if (fi
.nfields () > 0)
14940 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
14941 if (!fi
.fnfieldlists
.empty ())
14943 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
14945 /* Get the type which refers to the base class (possibly this
14946 class itself) which contains the vtable pointer for the current
14947 class from the DW_AT_containing_type attribute. This use of
14948 DW_AT_containing_type is a GNU extension. */
14950 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14952 struct type
*t
= die_containing_type (die
, cu
);
14954 set_type_vptr_basetype (type
, t
);
14959 /* Our own class provides vtbl ptr. */
14960 for (i
= t
->num_fields () - 1;
14961 i
>= TYPE_N_BASECLASSES (t
);
14964 const char *fieldname
= t
->field (i
).name ();
14966 if (is_vtable_name (fieldname
, cu
))
14968 set_type_vptr_fieldno (type
, i
);
14973 /* Complain if virtual function table field not found. */
14974 if (i
< TYPE_N_BASECLASSES (t
))
14975 complaint (_("virtual function table pointer "
14976 "not found when defining class '%s'"),
14977 type
->name () ? type
->name () : "");
14981 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
14984 else if (cu
->producer
14985 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
14987 /* The IBM XLC compiler does not provide direct indication
14988 of the containing type, but the vtable pointer is
14989 always named __vfp. */
14993 for (i
= type
->num_fields () - 1;
14994 i
>= TYPE_N_BASECLASSES (type
);
14997 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
14999 set_type_vptr_fieldno (type
, i
);
15000 set_type_vptr_basetype (type
, type
);
15007 /* Copy fi.typedef_field_list linked list elements content into the
15008 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15009 if (!fi
.typedef_field_list
.empty ())
15011 int count
= fi
.typedef_field_list
.size ();
15013 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
15014 TYPE_TYPEDEF_FIELD_ARRAY (type
)
15015 = ((struct decl_field
*)
15017 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
15018 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
15020 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
15021 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
15024 /* Copy fi.nested_types_list linked list elements content into the
15025 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15026 if (!fi
.nested_types_list
.empty ()
15027 && cu
->lang () != language_ada
)
15029 int count
= fi
.nested_types_list
.size ();
15031 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
15032 TYPE_NESTED_TYPES_ARRAY (type
)
15033 = ((struct decl_field
*)
15034 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
15035 TYPE_NESTED_TYPES_COUNT (type
) = count
;
15037 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
15038 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
15042 quirk_gcc_member_function_pointer (type
, objfile
);
15043 if (cu
->lang () == language_rust
&& die
->tag
== DW_TAG_union_type
)
15044 cu
->rust_unions
.push_back (type
);
15045 else if (cu
->lang () == language_ada
)
15046 quirk_ada_thick_pointer_struct (die
, cu
, type
);
15048 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15049 snapshots) has been known to create a die giving a declaration
15050 for a class that has, as a child, a die giving a definition for a
15051 nested class. So we have to process our children even if the
15052 current die is a declaration. Normally, of course, a declaration
15053 won't have any children at all. */
15055 child_die
= die
->child
;
15057 while (child_die
!= NULL
&& child_die
->tag
)
15059 if (child_die
->tag
== DW_TAG_member
15060 || child_die
->tag
== DW_TAG_variable
15061 || child_die
->tag
== DW_TAG_inheritance
15062 || child_die
->tag
== DW_TAG_template_value_param
15063 || child_die
->tag
== DW_TAG_template_type_param
)
15068 process_die (child_die
, cu
);
15070 child_die
= child_die
->sibling
;
15073 /* Do not consider external references. According to the DWARF standard,
15074 these DIEs are identified by the fact that they have no byte_size
15075 attribute, and a declaration attribute. */
15076 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
15077 || !die_is_declaration (die
, cu
)
15078 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
15080 struct symbol
*sym
= new_symbol (die
, type
, cu
);
15082 if (has_template_parameters
)
15084 struct symtab
*symtab
;
15085 if (sym
!= nullptr)
15086 symtab
= sym
->symtab ();
15087 else if (cu
->line_header
!= nullptr)
15089 /* Any related symtab will do. */
15091 = cu
->line_header
->file_names ()[0].symtab
;
15096 complaint (_("could not find suitable "
15097 "symtab for template parameter"
15098 " - DIE at %s [in module %s]"),
15099 sect_offset_str (die
->sect_off
),
15100 objfile_name (objfile
));
15103 if (symtab
!= nullptr)
15105 /* Make sure that the symtab is set on the new symbols.
15106 Even though they don't appear in this symtab directly,
15107 other parts of gdb assume that symbols do, and this is
15108 reasonably true. */
15109 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
15110 TYPE_TEMPLATE_ARGUMENT (type
, i
)->set_symtab (symtab
);
15116 /* Assuming DIE is an enumeration type, and TYPE is its associated
15117 type, update TYPE using some information only available in DIE's
15118 children. In particular, the fields are computed. */
15121 update_enumeration_type_from_children (struct die_info
*die
,
15123 struct dwarf2_cu
*cu
)
15125 struct die_info
*child_die
;
15126 int unsigned_enum
= 1;
15129 auto_obstack obstack
;
15130 std::vector
<struct field
> fields
;
15132 for (child_die
= die
->child
;
15133 child_die
!= NULL
&& child_die
->tag
;
15134 child_die
= child_die
->sibling
)
15136 struct attribute
*attr
;
15138 const gdb_byte
*bytes
;
15139 struct dwarf2_locexpr_baton
*baton
;
15142 if (child_die
->tag
!= DW_TAG_enumerator
)
15145 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
15149 name
= dwarf2_name (child_die
, cu
);
15151 name
= "<anonymous enumerator>";
15153 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
15154 &value
, &bytes
, &baton
);
15162 if (count_one_bits_ll (value
) >= 2)
15166 fields
.emplace_back ();
15167 struct field
&field
= fields
.back ();
15168 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
15169 field
.set_loc_enumval (value
);
15172 if (!fields
.empty ())
15174 type
->set_num_fields (fields
.size ());
15177 TYPE_ALLOC (type
, sizeof (struct field
) * fields
.size ()));
15178 memcpy (type
->fields (), fields
.data (),
15179 sizeof (struct field
) * fields
.size ());
15183 type
->set_is_unsigned (true);
15186 type
->set_is_flag_enum (true);
15189 /* Given a DW_AT_enumeration_type die, set its type. We do not
15190 complete the type's fields yet, or create any symbols. */
15192 static struct type
*
15193 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15195 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15197 struct attribute
*attr
;
15200 /* If the definition of this type lives in .debug_types, read that type.
15201 Don't follow DW_AT_specification though, that will take us back up
15202 the chain and we want to go down. */
15203 attr
= die
->attr (DW_AT_signature
);
15204 if (attr
!= nullptr)
15206 type
= get_DW_AT_signature_type (die
, attr
, cu
);
15208 /* The type's CU may not be the same as CU.
15209 Ensure TYPE is recorded with CU in die_type_hash. */
15210 return set_die_type (die
, type
, cu
);
15213 type
= alloc_type (objfile
);
15215 type
->set_code (TYPE_CODE_ENUM
);
15216 name
= dwarf2_full_name (NULL
, die
, cu
);
15218 type
->set_name (name
);
15220 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
15223 struct type
*underlying_type
= die_type (die
, cu
);
15225 type
->set_target_type (underlying_type
);
15228 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15229 if (attr
!= nullptr)
15230 type
->set_length (attr
->constant_value (0));
15232 type
->set_length (0);
15234 maybe_set_alignment (cu
, die
, type
);
15236 /* The enumeration DIE can be incomplete. In Ada, any type can be
15237 declared as private in the package spec, and then defined only
15238 inside the package body. Such types are known as Taft Amendment
15239 Types. When another package uses such a type, an incomplete DIE
15240 may be generated by the compiler. */
15241 if (die_is_declaration (die
, cu
))
15242 type
->set_is_stub (true);
15244 /* If this type has an underlying type that is not a stub, then we
15245 may use its attributes. We always use the "unsigned" attribute
15246 in this situation, because ordinarily we guess whether the type
15247 is unsigned -- but the guess can be wrong and the underlying type
15248 can tell us the reality. However, we defer to a local size
15249 attribute if one exists, because this lets the compiler override
15250 the underlying type if needed. */
15251 if (type
->target_type () != NULL
&& !type
->target_type ()->is_stub ())
15253 struct type
*underlying_type
= type
->target_type ();
15254 underlying_type
= check_typedef (underlying_type
);
15256 type
->set_is_unsigned (underlying_type
->is_unsigned ());
15258 if (type
->length () == 0)
15259 type
->set_length (underlying_type
->length ());
15261 if (TYPE_RAW_ALIGN (type
) == 0
15262 && TYPE_RAW_ALIGN (underlying_type
) != 0)
15263 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
15266 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
15268 set_die_type (die
, type
, cu
);
15270 /* Finish the creation of this type by using the enum's children.
15271 Note that, as usual, this must come after set_die_type to avoid
15272 infinite recursion when trying to compute the names of the
15274 update_enumeration_type_from_children (die
, type
, cu
);
15279 /* Given a pointer to a die which begins an enumeration, process all
15280 the dies that define the members of the enumeration, and create the
15281 symbol for the enumeration type.
15283 NOTE: We reverse the order of the element list. */
15286 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
15288 struct type
*this_type
;
15290 this_type
= get_die_type (die
, cu
);
15291 if (this_type
== NULL
)
15292 this_type
= read_enumeration_type (die
, cu
);
15294 if (die
->child
!= NULL
)
15296 struct die_info
*child_die
;
15299 child_die
= die
->child
;
15300 while (child_die
&& child_die
->tag
)
15302 if (child_die
->tag
!= DW_TAG_enumerator
)
15304 process_die (child_die
, cu
);
15308 name
= dwarf2_name (child_die
, cu
);
15310 new_symbol (child_die
, this_type
, cu
);
15313 child_die
= child_die
->sibling
;
15317 /* If we are reading an enum from a .debug_types unit, and the enum
15318 is a declaration, and the enum is not the signatured type in the
15319 unit, then we do not want to add a symbol for it. Adding a
15320 symbol would in some cases obscure the true definition of the
15321 enum, giving users an incomplete type when the definition is
15322 actually available. Note that we do not want to do this for all
15323 enums which are just declarations, because C++0x allows forward
15324 enum declarations. */
15325 if (cu
->per_cu
->is_debug_types
15326 && die_is_declaration (die
, cu
))
15328 struct signatured_type
*sig_type
;
15330 sig_type
= (struct signatured_type
*) cu
->per_cu
;
15331 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
15332 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
15336 new_symbol (die
, this_type
, cu
);
15339 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15340 expression for an index type and finds the corresponding field
15341 offset in the hidden "P_BOUNDS" structure. Returns true on success
15342 and updates *FIELD, false if it fails to recognize an
15346 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
15347 int *bounds_offset
, struct field
*field
,
15348 struct dwarf2_cu
*cu
)
15350 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
15351 if (attr
== nullptr || !attr
->form_is_block ())
15354 const struct dwarf_block
*block
= attr
->as_block ();
15355 const gdb_byte
*start
= block
->data
;
15356 const gdb_byte
*end
= block
->data
+ block
->size
;
15358 /* The expression to recognize generally looks like:
15360 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15361 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15363 However, the second "plus_uconst" may be missing:
15365 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15366 DW_OP_deref_size: 4)
15368 This happens when the field is at the start of the structure.
15370 Also, the final deref may not be sized:
15372 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15375 This happens when the size of the index type happens to be the
15376 same as the architecture's word size. This can occur with or
15377 without the second plus_uconst. */
15379 if (end
- start
< 2)
15381 if (*start
++ != DW_OP_push_object_address
)
15383 if (*start
++ != DW_OP_plus_uconst
)
15386 uint64_t this_bound_off
;
15387 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
15388 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
15390 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15391 is consistent among all bounds. */
15392 if (*bounds_offset
== -1)
15393 *bounds_offset
= this_bound_off
;
15394 else if (*bounds_offset
!= this_bound_off
)
15397 if (start
== end
|| *start
++ != DW_OP_deref
)
15403 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
15405 /* This means an offset of 0. */
15407 else if (*start
++ != DW_OP_plus_uconst
)
15411 /* The size is the parameter to DW_OP_plus_uconst. */
15413 start
= gdb_read_uleb128 (start
, end
, &val
);
15414 if (start
== nullptr)
15416 if ((int) val
!= val
)
15425 if (*start
== DW_OP_deref_size
)
15427 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
15428 if (start
== nullptr)
15431 else if (*start
== DW_OP_deref
)
15433 size
= cu
->header
.addr_size
;
15439 field
->set_loc_bitpos (8 * offset
);
15440 if (size
!= field
->type ()->length ())
15441 FIELD_BITSIZE (*field
) = 8 * size
;
15446 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15447 some kinds of Ada arrays:
15449 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15450 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15451 <11e0> DW_AT_data_location: 2 byte block: 97 6
15452 (DW_OP_push_object_address; DW_OP_deref)
15453 <11e3> DW_AT_type : <0x1173>
15454 <11e7> DW_AT_sibling : <0x1201>
15455 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15456 <11ec> DW_AT_type : <0x1206>
15457 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15458 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15459 DW_OP_deref_size: 4)
15460 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15461 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15462 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15464 This actually represents a "thick pointer", which is a structure
15465 with two elements: one that is a pointer to the array data, and one
15466 that is a pointer to another structure; this second structure holds
15469 This returns a new type on success, or nullptr if this didn't
15470 recognize the type. */
15472 static struct type
*
15473 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
15476 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
15477 /* So far we've only seen this with block form. */
15478 if (attr
== nullptr || !attr
->form_is_block ())
15481 /* Note that this will fail if the structure layout is changed by
15482 the compiler. However, we have no good way to recognize some
15483 other layout, because we don't know what expression the compiler
15484 might choose to emit should this happen. */
15485 struct dwarf_block
*blk
= attr
->as_block ();
15487 || blk
->data
[0] != DW_OP_push_object_address
15488 || blk
->data
[1] != DW_OP_deref
)
15491 int bounds_offset
= -1;
15492 int max_align
= -1;
15493 std::vector
<struct field
> range_fields
;
15494 for (struct die_info
*child_die
= die
->child
;
15496 child_die
= child_die
->sibling
)
15498 if (child_die
->tag
== DW_TAG_subrange_type
)
15500 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
15502 int this_align
= type_align (underlying
);
15503 if (this_align
> max_align
)
15504 max_align
= this_align
;
15506 range_fields
.emplace_back ();
15507 range_fields
.emplace_back ();
15509 struct field
&lower
= range_fields
[range_fields
.size () - 2];
15510 struct field
&upper
= range_fields
[range_fields
.size () - 1];
15512 lower
.set_type (underlying
);
15513 FIELD_ARTIFICIAL (lower
) = 1;
15515 upper
.set_type (underlying
);
15516 FIELD_ARTIFICIAL (upper
) = 1;
15518 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
15519 &bounds_offset
, &lower
, cu
)
15520 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
15521 &bounds_offset
, &upper
, cu
))
15526 /* This shouldn't really happen, but double-check that we found
15527 where the bounds are stored. */
15528 if (bounds_offset
== -1)
15531 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15532 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15536 /* Set the name of each field in the bounds. */
15537 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
15538 range_fields
[i
].set_name (objfile
->intern (name
));
15539 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
15540 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
15543 struct type
*bounds
= alloc_type (objfile
);
15544 bounds
->set_code (TYPE_CODE_STRUCT
);
15546 bounds
->set_num_fields (range_fields
.size ());
15548 ((struct field
*) TYPE_ALLOC (bounds
, (bounds
->num_fields ()
15549 * sizeof (struct field
))));
15550 memcpy (bounds
->fields (), range_fields
.data (),
15551 bounds
->num_fields () * sizeof (struct field
));
15553 int last_fieldno
= range_fields
.size () - 1;
15554 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
15555 + bounds
->field (last_fieldno
).type ()->length ());
15556 bounds
->set_length (align_up (bounds_size
, max_align
));
15558 /* Rewrite the existing array type in place. Specifically, we
15559 remove any dynamic properties we might have read, and we replace
15560 the index types. */
15561 struct type
*iter
= type
;
15562 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15564 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
15565 iter
->main_type
->dyn_prop_list
= nullptr;
15566 iter
->set_index_type
15567 (create_static_range_type (NULL
, bounds
->field (i
).type (), 1, 0));
15568 iter
= iter
->target_type ();
15571 struct type
*result
= alloc_type (objfile
);
15572 result
->set_code (TYPE_CODE_STRUCT
);
15574 result
->set_num_fields (2);
15576 ((struct field
*) TYPE_ZALLOC (result
, (result
->num_fields ()
15577 * sizeof (struct field
))));
15579 /* The names are chosen to coincide with what the compiler does with
15580 -fgnat-encodings=all, which the Ada code in gdb already
15582 result
->field (0).set_name ("P_ARRAY");
15583 result
->field (0).set_type (lookup_pointer_type (type
));
15585 result
->field (1).set_name ("P_BOUNDS");
15586 result
->field (1).set_type (lookup_pointer_type (bounds
));
15587 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
15589 result
->set_name (type
->name ());
15590 result
->set_length (result
->field (0).type ()->length ()
15591 + result
->field (1).type ()->length ());
15596 /* Extract all information from a DW_TAG_array_type DIE and put it in
15597 the DIE's type field. For now, this only handles one dimensional
15600 static struct type
*
15601 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15603 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15604 struct die_info
*child_die
;
15606 struct type
*element_type
, *range_type
, *index_type
;
15607 struct attribute
*attr
;
15609 struct dynamic_prop
*byte_stride_prop
= NULL
;
15610 unsigned int bit_stride
= 0;
15612 element_type
= die_type (die
, cu
);
15614 /* The die_type call above may have already set the type for this DIE. */
15615 type
= get_die_type (die
, cu
);
15619 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15623 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15626 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
15627 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
15631 complaint (_("unable to read array DW_AT_byte_stride "
15632 " - DIE at %s [in module %s]"),
15633 sect_offset_str (die
->sect_off
),
15634 objfile_name (cu
->per_objfile
->objfile
));
15635 /* Ignore this attribute. We will likely not be able to print
15636 arrays of this type correctly, but there is little we can do
15637 to help if we cannot read the attribute's value. */
15638 byte_stride_prop
= NULL
;
15642 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15644 bit_stride
= attr
->constant_value (0);
15646 /* Irix 6.2 native cc creates array types without children for
15647 arrays with unspecified length. */
15648 if (die
->child
== NULL
)
15650 index_type
= objfile_type (objfile
)->builtin_int
;
15651 range_type
= create_static_range_type (NULL
, index_type
, 0, -1);
15652 type
= create_array_type_with_stride (NULL
, element_type
, range_type
,
15653 byte_stride_prop
, bit_stride
);
15654 return set_die_type (die
, type
, cu
);
15657 std::vector
<struct type
*> range_types
;
15658 child_die
= die
->child
;
15659 while (child_die
&& child_die
->tag
)
15661 if (child_die
->tag
== DW_TAG_subrange_type
15662 || child_die
->tag
== DW_TAG_generic_subrange
)
15664 struct type
*child_type
= read_type_die (child_die
, cu
);
15666 if (child_type
!= NULL
)
15668 /* The range type was succesfully read. Save it for the
15669 array type creation. */
15670 range_types
.push_back (child_type
);
15673 child_die
= child_die
->sibling
;
15676 if (range_types
.empty ())
15678 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15679 sect_offset_str (die
->sect_off
),
15680 objfile_name (cu
->per_objfile
->objfile
));
15684 /* Dwarf2 dimensions are output from left to right, create the
15685 necessary array types in backwards order. */
15687 type
= element_type
;
15689 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
15693 while (i
< range_types
.size ())
15695 type
= create_array_type_with_stride (NULL
, type
, range_types
[i
++],
15696 byte_stride_prop
, bit_stride
);
15697 type
->set_is_multi_dimensional (true);
15699 byte_stride_prop
= nullptr;
15704 size_t ndim
= range_types
.size ();
15707 type
= create_array_type_with_stride (NULL
, type
, range_types
[ndim
],
15708 byte_stride_prop
, bit_stride
);
15709 type
->set_is_multi_dimensional (true);
15711 byte_stride_prop
= nullptr;
15715 /* Clear the flag on the outermost array type. */
15716 type
->set_is_multi_dimensional (false);
15717 gdb_assert (type
!= element_type
);
15719 /* Understand Dwarf2 support for vector types (like they occur on
15720 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15721 array type. This is not part of the Dwarf2/3 standard yet, but a
15722 custom vendor extension. The main difference between a regular
15723 array and the vector variant is that vectors are passed by value
15725 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
15726 if (attr
!= nullptr)
15727 make_vector_type (type
);
15729 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15730 implementation may choose to implement triple vectors using this
15732 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15733 if (attr
!= nullptr && attr
->form_is_unsigned ())
15735 if (attr
->as_unsigned () >= type
->length ())
15736 type
->set_length (attr
->as_unsigned ());
15738 complaint (_("DW_AT_byte_size for array type smaller "
15739 "than the total size of elements"));
15742 name
= dwarf2_name (die
, cu
);
15744 type
->set_name (name
);
15746 maybe_set_alignment (cu
, die
, type
);
15748 struct type
*replacement_type
= nullptr;
15749 if (cu
->lang () == language_ada
)
15751 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
15752 if (replacement_type
!= nullptr)
15753 type
= replacement_type
;
15756 /* Install the type in the die. */
15757 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
15759 /* set_die_type should be already done. */
15760 set_descriptive_type (type
, die
, cu
);
15765 static enum dwarf_array_dim_ordering
15766 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
15768 struct attribute
*attr
;
15770 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
15772 if (attr
!= nullptr)
15774 LONGEST val
= attr
->constant_value (-1);
15775 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
15776 return (enum dwarf_array_dim_ordering
) val
;
15779 /* GNU F77 is a special case, as at 08/2004 array type info is the
15780 opposite order to the dwarf2 specification, but data is still
15781 laid out as per normal fortran.
15783 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15784 version checking. */
15786 if (cu
->lang () == language_fortran
15787 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
15789 return DW_ORD_row_major
;
15792 switch (cu
->language_defn
->array_ordering ())
15794 case array_column_major
:
15795 return DW_ORD_col_major
;
15796 case array_row_major
:
15798 return DW_ORD_row_major
;
15802 /* Extract all information from a DW_TAG_set_type DIE and put it in
15803 the DIE's type field. */
15805 static struct type
*
15806 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15808 struct type
*domain_type
, *set_type
;
15809 struct attribute
*attr
;
15811 domain_type
= die_type (die
, cu
);
15813 /* The die_type call above may have already set the type for this DIE. */
15814 set_type
= get_die_type (die
, cu
);
15818 set_type
= create_set_type (NULL
, domain_type
);
15820 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15821 if (attr
!= nullptr && attr
->form_is_unsigned ())
15822 set_type
->set_length (attr
->as_unsigned ());
15824 maybe_set_alignment (cu
, die
, set_type
);
15826 return set_die_type (die
, set_type
, cu
);
15829 /* A helper for read_common_block that creates a locexpr baton.
15830 SYM is the symbol which we are marking as computed.
15831 COMMON_DIE is the DIE for the common block.
15832 COMMON_LOC is the location expression attribute for the common
15834 MEMBER_LOC is the location expression attribute for the particular
15835 member of the common block that we are processing.
15836 CU is the CU from which the above come. */
15839 mark_common_block_symbol_computed (struct symbol
*sym
,
15840 struct die_info
*common_die
,
15841 struct attribute
*common_loc
,
15842 struct attribute
*member_loc
,
15843 struct dwarf2_cu
*cu
)
15845 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15846 struct objfile
*objfile
= per_objfile
->objfile
;
15847 struct dwarf2_locexpr_baton
*baton
;
15849 unsigned int cu_off
;
15850 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
15851 LONGEST offset
= 0;
15853 gdb_assert (common_loc
&& member_loc
);
15854 gdb_assert (common_loc
->form_is_block ());
15855 gdb_assert (member_loc
->form_is_block ()
15856 || member_loc
->form_is_constant ());
15858 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
15859 baton
->per_objfile
= per_objfile
;
15860 baton
->per_cu
= cu
->per_cu
;
15861 gdb_assert (baton
->per_cu
);
15863 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15865 if (member_loc
->form_is_constant ())
15867 offset
= member_loc
->constant_value (0);
15868 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
15871 baton
->size
+= member_loc
->as_block ()->size
;
15873 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
15876 *ptr
++ = DW_OP_call4
;
15877 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
15878 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
15881 if (member_loc
->form_is_constant ())
15883 *ptr
++ = DW_OP_addr
;
15884 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
15885 ptr
+= cu
->header
.addr_size
;
15889 /* We have to copy the data here, because DW_OP_call4 will only
15890 use a DW_AT_location attribute. */
15891 struct dwarf_block
*block
= member_loc
->as_block ();
15892 memcpy (ptr
, block
->data
, block
->size
);
15893 ptr
+= block
->size
;
15896 *ptr
++ = DW_OP_plus
;
15897 gdb_assert (ptr
- baton
->data
== baton
->size
);
15899 SYMBOL_LOCATION_BATON (sym
) = baton
;
15900 sym
->set_aclass_index (dwarf2_locexpr_index
);
15903 /* Create appropriate locally-scoped variables for all the
15904 DW_TAG_common_block entries. Also create a struct common_block
15905 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15906 is used to separate the common blocks name namespace from regular
15910 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
15912 struct attribute
*attr
;
15914 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
15915 if (attr
!= nullptr)
15917 /* Support the .debug_loc offsets. */
15918 if (attr
->form_is_block ())
15922 else if (attr
->form_is_section_offset ())
15924 dwarf2_complex_location_expr_complaint ();
15929 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15930 "common block member");
15935 if (die
->child
!= NULL
)
15937 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15938 struct die_info
*child_die
;
15939 size_t n_entries
= 0, size
;
15940 struct common_block
*common_block
;
15941 struct symbol
*sym
;
15943 for (child_die
= die
->child
;
15944 child_die
&& child_die
->tag
;
15945 child_die
= child_die
->sibling
)
15948 size
= (sizeof (struct common_block
)
15949 + (n_entries
- 1) * sizeof (struct symbol
*));
15951 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
15953 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
15954 common_block
->n_entries
= 0;
15956 for (child_die
= die
->child
;
15957 child_die
&& child_die
->tag
;
15958 child_die
= child_die
->sibling
)
15960 /* Create the symbol in the DW_TAG_common_block block in the current
15962 sym
= new_symbol (child_die
, NULL
, cu
);
15965 struct attribute
*member_loc
;
15967 common_block
->contents
[common_block
->n_entries
++] = sym
;
15969 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
15973 /* GDB has handled this for a long time, but it is
15974 not specified by DWARF. It seems to have been
15975 emitted by gfortran at least as recently as:
15976 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15977 complaint (_("Variable in common block has "
15978 "DW_AT_data_member_location "
15979 "- DIE at %s [in module %s]"),
15980 sect_offset_str (child_die
->sect_off
),
15981 objfile_name (objfile
));
15983 if (member_loc
->form_is_section_offset ())
15984 dwarf2_complex_location_expr_complaint ();
15985 else if (member_loc
->form_is_constant ()
15986 || member_loc
->form_is_block ())
15988 if (attr
!= nullptr)
15989 mark_common_block_symbol_computed (sym
, die
, attr
,
15993 dwarf2_complex_location_expr_complaint ();
15998 sym
= new_symbol (die
, objfile_type (objfile
)->builtin_void
, cu
);
15999 sym
->set_value_common_block (common_block
);
16003 /* Create a type for a C++ namespace. */
16005 static struct type
*
16006 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16008 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16009 const char *previous_prefix
, *name
;
16013 /* For extensions, reuse the type of the original namespace. */
16014 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
16016 struct die_info
*ext_die
;
16017 struct dwarf2_cu
*ext_cu
= cu
;
16019 ext_die
= dwarf2_extension (die
, &ext_cu
);
16020 type
= read_type_die (ext_die
, ext_cu
);
16022 /* EXT_CU may not be the same as CU.
16023 Ensure TYPE is recorded with CU in die_type_hash. */
16024 return set_die_type (die
, type
, cu
);
16027 name
= namespace_name (die
, &is_anonymous
, cu
);
16029 /* Now build the name of the current namespace. */
16031 previous_prefix
= determine_prefix (die
, cu
);
16032 if (previous_prefix
[0] != '\0')
16033 name
= typename_concat (&objfile
->objfile_obstack
,
16034 previous_prefix
, name
, 0, cu
);
16036 /* Create the type. */
16037 type
= init_type (objfile
, TYPE_CODE_NAMESPACE
, 0, name
);
16039 return set_die_type (die
, type
, cu
);
16042 /* Read a namespace scope. */
16045 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
16047 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16050 /* Add a symbol associated to this if we haven't seen the namespace
16051 before. Also, add a using directive if it's an anonymous
16054 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
16058 type
= read_type_die (die
, cu
);
16059 new_symbol (die
, type
, cu
);
16061 namespace_name (die
, &is_anonymous
, cu
);
16064 const char *previous_prefix
= determine_prefix (die
, cu
);
16066 std::vector
<const char *> excludes
;
16067 add_using_directive (using_directives (cu
),
16068 previous_prefix
, type
->name (), NULL
,
16070 read_decl_line (die
, cu
),
16071 0, &objfile
->objfile_obstack
);
16075 if (die
->child
!= NULL
)
16077 struct die_info
*child_die
= die
->child
;
16079 while (child_die
&& child_die
->tag
)
16081 process_die (child_die
, cu
);
16082 child_die
= child_die
->sibling
;
16087 /* Read a Fortran module as type. This DIE can be only a declaration used for
16088 imported module. Still we need that type as local Fortran "use ... only"
16089 declaration imports depend on the created type in determine_prefix. */
16091 static struct type
*
16092 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16094 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16095 const char *module_name
;
16098 module_name
= dwarf2_name (die
, cu
);
16099 type
= init_type (objfile
, TYPE_CODE_MODULE
, 0, module_name
);
16101 return set_die_type (die
, type
, cu
);
16104 /* Read a Fortran module. */
16107 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
16109 struct die_info
*child_die
= die
->child
;
16112 type
= read_type_die (die
, cu
);
16113 new_symbol (die
, type
, cu
);
16115 while (child_die
&& child_die
->tag
)
16117 process_die (child_die
, cu
);
16118 child_die
= child_die
->sibling
;
16122 /* Return the name of the namespace represented by DIE. Set
16123 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16126 static const char *
16127 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
16129 struct die_info
*current_die
;
16130 const char *name
= NULL
;
16132 /* Loop through the extensions until we find a name. */
16134 for (current_die
= die
;
16135 current_die
!= NULL
;
16136 current_die
= dwarf2_extension (die
, &cu
))
16138 /* We don't use dwarf2_name here so that we can detect the absence
16139 of a name -> anonymous namespace. */
16140 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
16146 /* Is it an anonymous namespace? */
16148 *is_anonymous
= (name
== NULL
);
16150 name
= CP_ANONYMOUS_NAMESPACE_STR
;
16155 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16156 the user defined type vector. */
16158 static struct type
*
16159 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16161 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
16162 struct comp_unit_head
*cu_header
= &cu
->header
;
16164 struct attribute
*attr_byte_size
;
16165 struct attribute
*attr_address_class
;
16166 int byte_size
, addr_class
;
16167 struct type
*target_type
;
16169 target_type
= die_type (die
, cu
);
16171 /* The die_type call above may have already set the type for this DIE. */
16172 type
= get_die_type (die
, cu
);
16176 type
= lookup_pointer_type (target_type
);
16178 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16179 if (attr_byte_size
)
16180 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
16182 byte_size
= cu_header
->addr_size
;
16184 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
16185 if (attr_address_class
)
16186 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
16188 addr_class
= DW_ADDR_none
;
16190 ULONGEST alignment
= get_alignment (cu
, die
);
16192 /* If the pointer size, alignment, or address class is different
16193 than the default, create a type variant marked as such and set
16194 the length accordingly. */
16195 if (type
->length () != byte_size
16196 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
16197 && alignment
!= TYPE_RAW_ALIGN (type
))
16198 || addr_class
!= DW_ADDR_none
)
16200 if (gdbarch_address_class_type_flags_p (gdbarch
))
16202 type_instance_flags type_flags
16203 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
16205 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
16207 type
= make_type_with_address_space (type
, type_flags
);
16209 else if (type
->length () != byte_size
)
16211 complaint (_("invalid pointer size %d"), byte_size
);
16213 else if (TYPE_RAW_ALIGN (type
) != alignment
)
16215 complaint (_("Invalid DW_AT_alignment"
16216 " - DIE at %s [in module %s]"),
16217 sect_offset_str (die
->sect_off
),
16218 objfile_name (cu
->per_objfile
->objfile
));
16222 /* Should we also complain about unhandled address classes? */
16226 type
->set_length (byte_size
);
16227 set_type_align (type
, alignment
);
16228 return set_die_type (die
, type
, cu
);
16231 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16232 the user defined type vector. */
16234 static struct type
*
16235 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16238 struct type
*to_type
;
16239 struct type
*domain
;
16241 to_type
= die_type (die
, cu
);
16242 domain
= die_containing_type (die
, cu
);
16244 /* The calls above may have already set the type for this DIE. */
16245 type
= get_die_type (die
, cu
);
16249 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
16250 type
= lookup_methodptr_type (to_type
);
16251 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
16253 struct type
*new_type
= alloc_type (cu
->per_objfile
->objfile
);
16255 smash_to_method_type (new_type
, domain
, to_type
->target_type (),
16256 to_type
->fields (), to_type
->num_fields (),
16257 to_type
->has_varargs ());
16258 type
= lookup_methodptr_type (new_type
);
16261 type
= lookup_memberptr_type (to_type
, domain
);
16263 return set_die_type (die
, type
, cu
);
16266 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16267 the user defined type vector. */
16269 static struct type
*
16270 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16271 enum type_code refcode
)
16273 struct comp_unit_head
*cu_header
= &cu
->header
;
16274 struct type
*type
, *target_type
;
16275 struct attribute
*attr
;
16277 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
16279 target_type
= die_type (die
, cu
);
16281 /* The die_type call above may have already set the type for this DIE. */
16282 type
= get_die_type (die
, cu
);
16286 type
= lookup_reference_type (target_type
, refcode
);
16287 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16288 if (attr
!= nullptr)
16289 type
->set_length (attr
->constant_value (cu_header
->addr_size
));
16291 type
->set_length (cu_header
->addr_size
);
16293 maybe_set_alignment (cu
, die
, type
);
16294 return set_die_type (die
, type
, cu
);
16297 /* Add the given cv-qualifiers to the element type of the array. GCC
16298 outputs DWARF type qualifiers that apply to an array, not the
16299 element type. But GDB relies on the array element type to carry
16300 the cv-qualifiers. This mimics section 6.7.3 of the C99
16303 static struct type
*
16304 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16305 struct type
*base_type
, int cnst
, int voltl
)
16307 struct type
*el_type
, *inner_array
;
16309 base_type
= copy_type (base_type
);
16310 inner_array
= base_type
;
16312 while (inner_array
->target_type ()->code () == TYPE_CODE_ARRAY
)
16314 inner_array
->set_target_type (copy_type (inner_array
->target_type ()));
16315 inner_array
= inner_array
->target_type ();
16318 el_type
= inner_array
->target_type ();
16319 cnst
|= TYPE_CONST (el_type
);
16320 voltl
|= TYPE_VOLATILE (el_type
);
16321 inner_array
->set_target_type (make_cv_type (cnst
, voltl
, el_type
, NULL
));
16323 return set_die_type (die
, base_type
, cu
);
16326 static struct type
*
16327 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16329 struct type
*base_type
, *cv_type
;
16331 base_type
= die_type (die
, cu
);
16333 /* The die_type call above may have already set the type for this DIE. */
16334 cv_type
= get_die_type (die
, cu
);
16338 /* In case the const qualifier is applied to an array type, the element type
16339 is so qualified, not the array type (section 6.7.3 of C99). */
16340 if (base_type
->code () == TYPE_CODE_ARRAY
)
16341 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
16343 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
16344 return set_die_type (die
, cv_type
, cu
);
16347 static struct type
*
16348 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16350 struct type
*base_type
, *cv_type
;
16352 base_type
= die_type (die
, cu
);
16354 /* The die_type call above may have already set the type for this DIE. */
16355 cv_type
= get_die_type (die
, cu
);
16359 /* In case the volatile qualifier is applied to an array type, the
16360 element type is so qualified, not the array type (section 6.7.3
16362 if (base_type
->code () == TYPE_CODE_ARRAY
)
16363 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
16365 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
16366 return set_die_type (die
, cv_type
, cu
);
16369 /* Handle DW_TAG_restrict_type. */
16371 static struct type
*
16372 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16374 struct type
*base_type
, *cv_type
;
16376 base_type
= die_type (die
, cu
);
16378 /* The die_type call above may have already set the type for this DIE. */
16379 cv_type
= get_die_type (die
, cu
);
16383 cv_type
= make_restrict_type (base_type
);
16384 return set_die_type (die
, cv_type
, cu
);
16387 /* Handle DW_TAG_atomic_type. */
16389 static struct type
*
16390 read_tag_atomic_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16392 struct type
*base_type
, *cv_type
;
16394 base_type
= die_type (die
, cu
);
16396 /* The die_type call above may have already set the type for this DIE. */
16397 cv_type
= get_die_type (die
, cu
);
16401 cv_type
= make_atomic_type (base_type
);
16402 return set_die_type (die
, cv_type
, cu
);
16405 /* Extract all information from a DW_TAG_string_type DIE and add to
16406 the user defined type vector. It isn't really a user defined type,
16407 but it behaves like one, with other DIE's using an AT_user_def_type
16408 attribute to reference it. */
16410 static struct type
*
16411 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16413 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16414 struct gdbarch
*gdbarch
= objfile
->arch ();
16415 struct type
*type
, *range_type
, *index_type
, *char_type
;
16416 struct attribute
*attr
;
16417 struct dynamic_prop prop
;
16418 bool length_is_constant
= true;
16421 /* There are a couple of places where bit sizes might be made use of
16422 when parsing a DW_TAG_string_type, however, no producer that we know
16423 of make use of these. Handling bit sizes that are a multiple of the
16424 byte size is easy enough, but what about other bit sizes? Lets deal
16425 with that problem when we have to. Warn about these attributes being
16426 unsupported, then parse the type and ignore them like we always
16428 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
16429 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
16431 static bool warning_printed
= false;
16432 if (!warning_printed
)
16434 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16435 "currently supported on DW_TAG_string_type."));
16436 warning_printed
= true;
16440 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
16441 if (attr
!= nullptr && !attr
->form_is_constant ())
16443 /* The string length describes the location at which the length of
16444 the string can be found. The size of the length field can be
16445 specified with one of the attributes below. */
16446 struct type
*prop_type
;
16447 struct attribute
*len
16448 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
16449 if (len
== nullptr)
16450 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16451 if (len
!= nullptr && len
->form_is_constant ())
16453 /* Pass 0 as the default as we know this attribute is constant
16454 and the default value will not be returned. */
16455 LONGEST sz
= len
->constant_value (0);
16456 prop_type
= objfile_int_type (objfile
, sz
, true);
16460 /* If the size is not specified then we assume it is the size of
16461 an address on this target. */
16462 prop_type
= cu
->addr_sized_int_type (true);
16465 /* Convert the attribute into a dynamic property. */
16466 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
16469 length_is_constant
= false;
16471 else if (attr
!= nullptr)
16473 /* This DW_AT_string_length just contains the length with no
16474 indirection. There's no need to create a dynamic property in this
16475 case. Pass 0 for the default value as we know it will not be
16476 returned in this case. */
16477 length
= attr
->constant_value (0);
16479 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
16481 /* We don't currently support non-constant byte sizes for strings. */
16482 length
= attr
->constant_value (1);
16486 /* Use 1 as a fallback length if we have nothing else. */
16490 index_type
= objfile_type (objfile
)->builtin_int
;
16491 if (length_is_constant
)
16492 range_type
= create_static_range_type (NULL
, index_type
, 1, length
);
16495 struct dynamic_prop low_bound
;
16497 low_bound
.set_const_val (1);
16498 range_type
= create_range_type (NULL
, index_type
, &low_bound
, &prop
, 0);
16500 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
16501 type
= create_string_type (NULL
, char_type
, range_type
);
16503 return set_die_type (die
, type
, cu
);
16506 /* Assuming that DIE corresponds to a function, returns nonzero
16507 if the function is prototyped. */
16510 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
16512 struct attribute
*attr
;
16514 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
16515 if (attr
&& attr
->as_boolean ())
16518 /* The DWARF standard implies that the DW_AT_prototyped attribute
16519 is only meaningful for C, but the concept also extends to other
16520 languages that allow unprototyped functions (Eg: Objective C).
16521 For all other languages, assume that functions are always
16523 if (cu
->lang () != language_c
16524 && cu
->lang () != language_objc
16525 && cu
->lang () != language_opencl
)
16528 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16529 prototyped and unprototyped functions; default to prototyped,
16530 since that is more common in modern code (and RealView warns
16531 about unprototyped functions). */
16532 if (producer_is_realview (cu
->producer
))
16538 /* Handle DIES due to C code like:
16542 int (*funcp)(int a, long l);
16546 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16548 static struct type
*
16549 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16551 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16552 struct type
*type
; /* Type that this function returns. */
16553 struct type
*ftype
; /* Function that returns above type. */
16554 struct attribute
*attr
;
16556 type
= die_type (die
, cu
);
16558 /* The die_type call above may have already set the type for this DIE. */
16559 ftype
= get_die_type (die
, cu
);
16563 ftype
= lookup_function_type (type
);
16565 if (prototyped_function_p (die
, cu
))
16566 ftype
->set_is_prototyped (true);
16568 /* Store the calling convention in the type if it's available in
16569 the subroutine die. Otherwise set the calling convention to
16570 the default value DW_CC_normal. */
16571 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
16572 if (attr
!= nullptr
16573 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
16574 TYPE_CALLING_CONVENTION (ftype
)
16575 = (enum dwarf_calling_convention
) attr
->constant_value (0);
16576 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
16577 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
16579 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
16581 /* Record whether the function returns normally to its caller or not
16582 if the DWARF producer set that information. */
16583 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
16584 if (attr
&& attr
->as_boolean ())
16585 TYPE_NO_RETURN (ftype
) = 1;
16587 /* We need to add the subroutine type to the die immediately so
16588 we don't infinitely recurse when dealing with parameters
16589 declared as the same subroutine type. */
16590 set_die_type (die
, ftype
, cu
);
16592 if (die
->child
!= NULL
)
16594 struct type
*void_type
= objfile_type (objfile
)->builtin_void
;
16595 struct die_info
*child_die
;
16596 int nparams
, iparams
;
16598 /* Count the number of parameters.
16599 FIXME: GDB currently ignores vararg functions, but knows about
16600 vararg member functions. */
16602 child_die
= die
->child
;
16603 while (child_die
&& child_die
->tag
)
16605 if (child_die
->tag
== DW_TAG_formal_parameter
)
16607 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
16608 ftype
->set_has_varargs (true);
16610 child_die
= child_die
->sibling
;
16613 /* Allocate storage for parameters and fill them in. */
16614 ftype
->set_num_fields (nparams
);
16616 ((struct field
*) TYPE_ZALLOC (ftype
, nparams
* sizeof (struct field
)));
16618 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16619 even if we error out during the parameters reading below. */
16620 for (iparams
= 0; iparams
< nparams
; iparams
++)
16621 ftype
->field (iparams
).set_type (void_type
);
16624 child_die
= die
->child
;
16625 while (child_die
&& child_die
->tag
)
16627 if (child_die
->tag
== DW_TAG_formal_parameter
)
16629 struct type
*arg_type
;
16631 /* DWARF version 2 has no clean way to discern C++
16632 static and non-static member functions. G++ helps
16633 GDB by marking the first parameter for non-static
16634 member functions (which is the this pointer) as
16635 artificial. We pass this information to
16636 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16638 DWARF version 3 added DW_AT_object_pointer, which GCC
16639 4.5 does not yet generate. */
16640 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
16641 if (attr
!= nullptr)
16642 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = attr
->as_boolean ();
16644 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
16645 arg_type
= die_type (child_die
, cu
);
16647 /* RealView does not mark THIS as const, which the testsuite
16648 expects. GCC marks THIS as const in method definitions,
16649 but not in the class specifications (GCC PR 43053). */
16650 if (cu
->lang () == language_cplus
16651 && !TYPE_CONST (arg_type
)
16652 && TYPE_FIELD_ARTIFICIAL (ftype
, iparams
))
16655 struct dwarf2_cu
*arg_cu
= cu
;
16656 const char *name
= dwarf2_name (child_die
, cu
);
16658 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
16659 if (attr
!= nullptr)
16661 /* If the compiler emits this, use it. */
16662 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
16665 else if (name
&& strcmp (name
, "this") == 0)
16666 /* Function definitions will have the argument names. */
16668 else if (name
== NULL
&& iparams
== 0)
16669 /* Declarations may not have the names, so like
16670 elsewhere in GDB, assume an artificial first
16671 argument is "this". */
16675 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
16679 ftype
->field (iparams
).set_type (arg_type
);
16682 child_die
= child_die
->sibling
;
16689 static struct type
*
16690 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
16692 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16693 const char *name
= NULL
;
16694 struct type
*this_type
, *target_type
;
16696 name
= dwarf2_full_name (NULL
, die
, cu
);
16697 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, name
);
16698 this_type
->set_target_is_stub (true);
16699 set_die_type (die
, this_type
, cu
);
16700 target_type
= die_type (die
, cu
);
16701 if (target_type
!= this_type
)
16702 this_type
->set_target_type (target_type
);
16705 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16706 spec and cause infinite loops in GDB. */
16707 complaint (_("Self-referential DW_TAG_typedef "
16708 "- DIE at %s [in module %s]"),
16709 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
16710 this_type
->set_target_type (nullptr);
16714 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16715 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16716 Handle these by just returning the target type, rather than
16717 constructing an anonymous typedef type and trying to handle this
16719 set_die_type (die
, target_type
, cu
);
16720 return target_type
;
16725 /* Helper for get_dwarf2_rational_constant that computes the value of
16726 a given gmp_mpz given an attribute. */
16729 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
16731 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16732 location expression that pushes an implicit value. */
16733 if (attr
->form
== DW_FORM_exprloc
)
16735 dwarf_block
*blk
= attr
->as_block ();
16736 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
16739 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
16740 blk
->data
+ blk
->size
,
16742 if (ptr
- blk
->data
+ len
<= blk
->size
)
16744 mpz_import (value
->val
, len
,
16745 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
.get ())
16752 /* On failure set it to 1. */
16753 *value
= gdb_mpz (1);
16755 else if (attr
->form_is_block ())
16757 dwarf_block
*blk
= attr
->as_block ();
16758 mpz_import (value
->val
, blk
->size
,
16759 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
.get ())
16761 1, 0, 0, blk
->data
);
16764 *value
= gdb_mpz (attr
->constant_value (1));
16767 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16768 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16770 If the numerator and/or numerator attribute is missing,
16771 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16775 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
16776 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
16778 struct attribute
*num_attr
, *denom_attr
;
16780 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
16781 if (num_attr
== nullptr)
16782 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16783 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16785 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
16786 if (denom_attr
== nullptr)
16787 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16788 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16790 if (num_attr
== nullptr || denom_attr
== nullptr)
16793 get_mpz (cu
, numerator
, num_attr
);
16794 get_mpz (cu
, denominator
, denom_attr
);
16797 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16798 rational constant, rather than a signed one.
16800 If the rational constant has a negative value, a complaint
16801 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16804 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
16805 struct dwarf2_cu
*cu
,
16806 gdb_mpz
*numerator
,
16807 gdb_mpz
*denominator
)
16812 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
16813 if (mpz_sgn (num
.val
) == -1 && mpz_sgn (denom
.val
) == -1)
16815 mpz_neg (num
.val
, num
.val
);
16816 mpz_neg (denom
.val
, denom
.val
);
16818 else if (mpz_sgn (num
.val
) == -1)
16820 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16822 sect_offset_str (die
->sect_off
));
16825 else if (mpz_sgn (denom
.val
) == -1)
16827 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16829 sect_offset_str (die
->sect_off
));
16833 *numerator
= std::move (num
);
16834 *denominator
= std::move (denom
);
16837 /* Assuming that ENCODING is a string whose contents starting at the
16838 K'th character is "_nn" where "nn" is a decimal number, scan that
16839 number and set RESULT to the value. K is updated to point to the
16840 character immediately following the number.
16842 If the string does not conform to the format described above, false
16843 is returned, and K may or may not be changed. */
16846 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
16848 /* The next character should be an underscore ('_') followed
16850 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
16853 /* Skip the underscore. */
16857 /* Determine the number of digits for our number. */
16858 while (isdigit (encoding
[k
]))
16863 std::string
copy (&encoding
[start
], k
- start
);
16864 if (mpz_set_str (result
->val
, copy
.c_str (), 10) == -1)
16870 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16871 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16872 DENOM, update OFFSET, and return true on success. Return false on
16876 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
16877 gdb_mpz
*num
, gdb_mpz
*denom
)
16879 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
16881 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
16884 /* Assuming DIE corresponds to a fixed point type, finish the creation
16885 of the corresponding TYPE by setting its type-specific data. CU is
16886 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16887 encodings. It is nullptr if the GNAT encoding should be
16891 finish_fixed_point_type (struct type
*type
, const char *suffix
,
16892 struct die_info
*die
, struct dwarf2_cu
*cu
)
16894 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
16895 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
16897 /* If GNAT encodings are preferred, don't examine the
16899 struct attribute
*attr
= nullptr;
16900 if (suffix
== nullptr)
16902 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
16903 if (attr
== nullptr)
16904 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
16905 if (attr
== nullptr)
16906 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16909 /* Numerator and denominator of our fixed-point type's scaling factor.
16910 The default is a scaling factor of 1, which we use as a fallback
16911 when we are not able to decode it (problem with the debugging info,
16912 unsupported forms, bug in GDB, etc...). Using that as the default
16913 allows us to at least print the unscaled value, which might still
16914 be useful to a user. */
16915 gdb_mpz
scale_num (1);
16916 gdb_mpz
scale_denom (1);
16918 if (attr
== nullptr)
16921 if (suffix
!= nullptr
16922 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16924 /* The number might be encoded as _nn_dd_nn_dd, where the
16925 second ratio is the 'small value. In this situation, we
16926 want the second value. */
16927 && (suffix
[offset
] != '_'
16928 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16935 /* Scaling factor not found. Assume a scaling factor of 1,
16936 and hope for the best. At least the user will be able to
16937 see the encoded value. */
16940 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16941 sect_offset_str (die
->sect_off
));
16944 else if (attr
->name
== DW_AT_binary_scale
)
16946 LONGEST scale_exp
= attr
->constant_value (0);
16947 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16949 mpz_mul_2exp (num_or_denom
->val
, num_or_denom
->val
, std::abs (scale_exp
));
16951 else if (attr
->name
== DW_AT_decimal_scale
)
16953 LONGEST scale_exp
= attr
->constant_value (0);
16954 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16956 mpz_ui_pow_ui (num_or_denom
->val
, 10, std::abs (scale_exp
));
16958 else if (attr
->name
== DW_AT_small
)
16960 struct die_info
*scale_die
;
16961 struct dwarf2_cu
*scale_cu
= cu
;
16963 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
16964 if (scale_die
->tag
== DW_TAG_constant
)
16965 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
16966 &scale_num
, &scale_denom
);
16968 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16970 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16974 complaint (_("unsupported scale attribute %s for fixed-point type"
16976 dwarf_attr_name (attr
->name
),
16977 sect_offset_str (die
->sect_off
));
16980 gdb_mpq
&scaling_factor
= type
->fixed_point_info ().scaling_factor
;
16981 mpz_set (mpq_numref (scaling_factor
.val
), scale_num
.val
);
16982 mpz_set (mpq_denref (scaling_factor
.val
), scale_denom
.val
);
16983 mpq_canonicalize (scaling_factor
.val
);
16986 /* The gnat-encoding suffix for fixed point. */
16988 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16990 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16991 "XF" suffix of the name. The text after this is what encodes the
16992 'small and 'delta information. Otherwise, return nullptr. */
16994 static const char *
16995 gnat_encoded_fixed_point_type_info (const char *name
)
16997 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
17000 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17001 (which may be different from NAME) to the architecture back-end to allow
17002 it to guess the correct format if necessary. */
17004 static struct type
*
17005 dwarf2_init_float_type (struct objfile
*objfile
, int bits
, const char *name
,
17006 const char *name_hint
, enum bfd_endian byte_order
)
17008 struct gdbarch
*gdbarch
= objfile
->arch ();
17009 const struct floatformat
**format
;
17012 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
17014 type
= init_float_type (objfile
, bits
, name
, format
, byte_order
);
17016 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17021 /* Allocate an integer type of size BITS and name NAME. */
17023 static struct type
*
17024 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, struct objfile
*objfile
,
17025 int bits
, int unsigned_p
, const char *name
)
17029 /* Versions of Intel's C Compiler generate an integer type called "void"
17030 instead of using DW_TAG_unspecified_type. This has been seen on
17031 at least versions 14, 17, and 18. */
17032 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
17033 && strcmp (name
, "void") == 0)
17034 type
= objfile_type (objfile
)->builtin_void
;
17036 type
= init_integer_type (objfile
, bits
, unsigned_p
, name
);
17041 /* Return true if DIE has a DW_AT_small attribute whose value is
17042 a constant rational, where both the numerator and denominator
17045 CU is the DIE's Compilation Unit. */
17048 has_zero_over_zero_small_attribute (struct die_info
*die
,
17049 struct dwarf2_cu
*cu
)
17051 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
17052 if (attr
== nullptr)
17055 struct dwarf2_cu
*scale_cu
= cu
;
17056 struct die_info
*scale_die
17057 = follow_die_ref (die
, attr
, &scale_cu
);
17059 if (scale_die
->tag
!= DW_TAG_constant
)
17062 gdb_mpz
num (1), denom (1);
17063 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
17064 return mpz_sgn (num
.val
) == 0 && mpz_sgn (denom
.val
) == 0;
17067 /* Initialise and return a floating point type of size BITS suitable for
17068 use as a component of a complex number. The NAME_HINT is passed through
17069 when initialising the floating point type and is the name of the complex
17072 As DWARF doesn't currently provide an explicit name for the components
17073 of a complex number, but it can be helpful to have these components
17074 named, we try to select a suitable name based on the size of the
17076 static struct type
*
17077 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
17078 struct objfile
*objfile
,
17079 int bits
, const char *name_hint
,
17080 enum bfd_endian byte_order
)
17082 gdbarch
*gdbarch
= objfile
->arch ();
17083 struct type
*tt
= nullptr;
17085 /* Try to find a suitable floating point builtin type of size BITS.
17086 We're going to use the name of this type as the name for the complex
17087 target type that we are about to create. */
17088 switch (cu
->lang ())
17090 case language_fortran
:
17094 tt
= builtin_f_type (gdbarch
)->builtin_real
;
17097 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
17099 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17101 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
17109 tt
= builtin_type (gdbarch
)->builtin_float
;
17112 tt
= builtin_type (gdbarch
)->builtin_double
;
17114 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17116 tt
= builtin_type (gdbarch
)->builtin_long_double
;
17122 /* If the type we found doesn't match the size we were looking for, then
17123 pretend we didn't find a type at all, the complex target type we
17124 create will then be nameless. */
17125 if (tt
!= nullptr && tt
->length () * TARGET_CHAR_BIT
!= bits
)
17128 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
17129 return dwarf2_init_float_type (objfile
, bits
, name
, name_hint
, byte_order
);
17132 /* Find a representation of a given base type and install
17133 it in the TYPE field of the die. */
17135 static struct type
*
17136 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17138 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17140 struct attribute
*attr
;
17141 int encoding
= 0, bits
= 0;
17145 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
17146 if (attr
!= nullptr && attr
->form_is_constant ())
17147 encoding
= attr
->constant_value (0);
17148 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17149 if (attr
!= nullptr)
17150 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
17151 name
= dwarf2_name (die
, cu
);
17153 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17155 arch
= objfile
->arch ();
17156 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
17158 attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
17159 if (attr
!= nullptr && attr
->form_is_constant ())
17161 int endianity
= attr
->constant_value (0);
17166 byte_order
= BFD_ENDIAN_BIG
;
17168 case DW_END_little
:
17169 byte_order
= BFD_ENDIAN_LITTLE
;
17172 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
17177 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
17178 && cu
->lang () == language_ada
17179 && has_zero_over_zero_small_attribute (die
, cu
))
17181 /* brobecker/2018-02-24: This is a fixed point type for which
17182 the scaling factor is represented as fraction whose value
17183 does not make sense (zero divided by zero), so we should
17184 normally never see these. However, there is a small category
17185 of fixed point types for which GNAT is unable to provide
17186 the scaling factor via the standard DWARF mechanisms, and
17187 for which the info is provided via the GNAT encodings instead.
17188 This is likely what this DIE is about. */
17189 encoding
= (encoding
== DW_ATE_signed_fixed
17191 : DW_ATE_unsigned
);
17194 /* With GNAT encodings, fixed-point information will be encoded in
17195 the type name. Note that this can also occur with the above
17196 zero-over-zero case, which is why this is a separate "if" rather
17197 than an "else if". */
17198 const char *gnat_encoding_suffix
= nullptr;
17199 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
17200 && cu
->lang () == language_ada
17201 && name
!= nullptr)
17203 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
17204 if (gnat_encoding_suffix
!= nullptr)
17206 gdb_assert (startswith (gnat_encoding_suffix
,
17207 GNAT_FIXED_POINT_SUFFIX
));
17208 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
17209 name
, gnat_encoding_suffix
- name
);
17210 /* Use -1 here so that SUFFIX points at the "_" after the
17212 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
17214 encoding
= (encoding
== DW_ATE_signed
17215 ? DW_ATE_signed_fixed
17216 : DW_ATE_unsigned_fixed
);
17222 case DW_ATE_address
:
17223 /* Turn DW_ATE_address into a void * pointer. */
17224 type
= init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
17225 type
= init_pointer_type (objfile
, bits
, name
, type
);
17227 case DW_ATE_boolean
:
17228 type
= init_boolean_type (objfile
, bits
, 1, name
);
17230 case DW_ATE_complex_float
:
17231 type
= dwarf2_init_complex_target_type (cu
, objfile
, bits
/ 2, name
,
17233 if (type
->code () == TYPE_CODE_ERROR
)
17235 if (name
== nullptr)
17237 struct obstack
*obstack
17238 = &cu
->per_objfile
->objfile
->objfile_obstack
;
17239 name
= obconcat (obstack
, "_Complex ", type
->name (),
17242 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17245 type
= init_complex_type (name
, type
);
17247 case DW_ATE_decimal_float
:
17248 type
= init_decfloat_type (objfile
, bits
, name
);
17251 type
= dwarf2_init_float_type (objfile
, bits
, name
, name
, byte_order
);
17253 case DW_ATE_signed
:
17254 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17256 case DW_ATE_unsigned
:
17257 if (cu
->lang () == language_fortran
17259 && startswith (name
, "character("))
17260 type
= init_character_type (objfile
, bits
, 1, name
);
17262 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17264 case DW_ATE_signed_char
:
17265 if (cu
->lang () == language_ada
17266 || cu
->lang () == language_m2
17267 || cu
->lang () == language_pascal
17268 || cu
->lang () == language_fortran
)
17269 type
= init_character_type (objfile
, bits
, 0, name
);
17271 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17273 case DW_ATE_unsigned_char
:
17274 if (cu
->lang () == language_ada
17275 || cu
->lang () == language_m2
17276 || cu
->lang () == language_pascal
17277 || cu
->lang () == language_fortran
17278 || cu
->lang () == language_rust
)
17279 type
= init_character_type (objfile
, bits
, 1, name
);
17281 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17285 type
= init_character_type (objfile
, bits
, 1, name
);
17286 return set_die_type (die
, type
, cu
);
17289 case DW_ATE_signed_fixed
:
17290 type
= init_fixed_point_type (objfile
, bits
, 0, name
);
17291 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17293 case DW_ATE_unsigned_fixed
:
17294 type
= init_fixed_point_type (objfile
, bits
, 1, name
);
17295 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17299 complaint (_("unsupported DW_AT_encoding: '%s'"),
17300 dwarf_type_encoding_name (encoding
));
17301 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17305 if (type
->code () == TYPE_CODE_INT
17307 && strcmp (name
, "char") == 0)
17308 type
->set_has_no_signedness (true);
17310 maybe_set_alignment (cu
, die
, type
);
17312 type
->set_endianity_is_not_default (gdbarch_byte_order (arch
) != byte_order
);
17314 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
17316 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
17317 if (attr
!= nullptr && attr
->as_unsigned () <= 8 * type
->length ())
17319 unsigned real_bit_size
= attr
->as_unsigned ();
17320 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
17321 /* Only use the attributes if they make sense together. */
17322 if (attr
== nullptr
17323 || (attr
->as_unsigned () + real_bit_size
17324 <= 8 * type
->length ()))
17326 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
17328 if (attr
!= nullptr)
17329 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
17330 = attr
->as_unsigned ();
17335 return set_die_type (die
, type
, cu
);
17338 /* A helper function that returns the name of DIE, if it refers to a
17339 variable declaration. */
17341 static const char *
17342 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17344 if (die
->tag
!= DW_TAG_variable
)
17347 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
17348 if (attr
== nullptr || !attr
->as_boolean ())
17351 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
17352 if (attr
== nullptr)
17354 return attr
->as_string ();
17357 /* Parse dwarf attribute if it's a block, reference or constant and put the
17358 resulting value of the attribute into struct bound_prop.
17359 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17362 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
17363 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
17364 struct type
*default_type
)
17366 struct dwarf2_property_baton
*baton
;
17367 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17368 struct objfile
*objfile
= per_objfile
->objfile
;
17369 struct obstack
*obstack
= &objfile
->objfile_obstack
;
17371 gdb_assert (default_type
!= NULL
);
17373 if (attr
== NULL
|| prop
== NULL
)
17376 if (attr
->form_is_block ())
17378 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17379 baton
->property_type
= default_type
;
17380 baton
->locexpr
.per_cu
= cu
->per_cu
;
17381 baton
->locexpr
.per_objfile
= per_objfile
;
17383 struct dwarf_block
*block
;
17384 if (attr
->form
== DW_FORM_data16
)
17386 size_t data_size
= 16;
17387 block
= XOBNEW (obstack
, struct dwarf_block
);
17388 block
->size
= (data_size
17389 + 2 /* Extra bytes for DW_OP and arg. */);
17390 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
->size
);
17391 data
[0] = DW_OP_implicit_value
;
17392 data
[1] = data_size
;
17393 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
17394 block
->data
= data
;
17397 block
= attr
->as_block ();
17399 baton
->locexpr
.size
= block
->size
;
17400 baton
->locexpr
.data
= block
->data
;
17401 switch (attr
->name
)
17403 case DW_AT_string_length
:
17404 baton
->locexpr
.is_reference
= true;
17407 baton
->locexpr
.is_reference
= false;
17411 prop
->set_locexpr (baton
);
17412 gdb_assert (prop
->baton () != NULL
);
17414 else if (attr
->form_is_ref ())
17416 struct dwarf2_cu
*target_cu
= cu
;
17417 struct die_info
*target_die
;
17418 struct attribute
*target_attr
;
17420 target_die
= follow_die_ref (die
, attr
, &target_cu
);
17421 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
17422 if (target_attr
== NULL
)
17423 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
17425 if (target_attr
== nullptr)
17426 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
17428 if (target_attr
== NULL
)
17430 const char *name
= var_decl_name (target_die
, target_cu
);
17431 if (name
!= nullptr)
17433 prop
->set_variable_name (name
);
17439 switch (target_attr
->name
)
17441 case DW_AT_location
:
17442 if (target_attr
->form_is_section_offset ())
17444 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17445 baton
->property_type
= die_type (target_die
, target_cu
);
17446 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
17447 prop
->set_loclist (baton
);
17448 gdb_assert (prop
->baton () != NULL
);
17450 else if (target_attr
->form_is_block ())
17452 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17453 baton
->property_type
= die_type (target_die
, target_cu
);
17454 baton
->locexpr
.per_cu
= cu
->per_cu
;
17455 baton
->locexpr
.per_objfile
= per_objfile
;
17456 struct dwarf_block
*block
= target_attr
->as_block ();
17457 baton
->locexpr
.size
= block
->size
;
17458 baton
->locexpr
.data
= block
->data
;
17459 baton
->locexpr
.is_reference
= true;
17460 prop
->set_locexpr (baton
);
17461 gdb_assert (prop
->baton () != NULL
);
17465 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17466 "dynamic property");
17470 case DW_AT_data_member_location
:
17471 case DW_AT_data_bit_offset
:
17475 if (!handle_member_location (target_die
, target_cu
, &offset
))
17478 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17479 baton
->property_type
= read_type_die (target_die
->parent
,
17481 baton
->offset_info
.offset
= offset
;
17482 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
17483 prop
->set_addr_offset (baton
);
17488 else if (attr
->form_is_constant ())
17489 prop
->set_const_val (attr
->constant_value (0));
17490 else if (attr
->form_is_section_offset ())
17492 switch (attr
->name
)
17494 case DW_AT_string_length
:
17495 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17496 baton
->property_type
= default_type
;
17497 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
17498 prop
->set_loclist (baton
);
17499 gdb_assert (prop
->baton () != NULL
);
17511 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
17512 dwarf2_name (die
, cu
));
17518 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17519 present (which is valid) then compute the default type based on the
17520 compilation units address size. */
17522 static struct type
*
17523 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17525 struct type
*index_type
= die_type (die
, cu
);
17527 /* Dwarf-2 specifications explicitly allows to create subrange types
17528 without specifying a base type.
17529 In that case, the base type must be set to the type of
17530 the lower bound, upper bound or count, in that order, if any of these
17531 three attributes references an object that has a type.
17532 If no base type is found, the Dwarf-2 specifications say that
17533 a signed integer type of size equal to the size of an address should
17535 For the following C code: `extern char gdb_int [];'
17536 GCC produces an empty range DIE.
17537 FIXME: muller/2010-05-28: Possible references to object for low bound,
17538 high bound or count are not yet handled by this code. */
17539 if (index_type
->code () == TYPE_CODE_VOID
)
17540 index_type
= cu
->addr_sized_int_type (false);
17545 /* Read the given DW_AT_subrange DIE. */
17547 static struct type
*
17548 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17550 struct type
*base_type
, *orig_base_type
;
17551 struct type
*range_type
;
17552 struct attribute
*attr
;
17553 struct dynamic_prop low
, high
;
17554 int low_default_is_valid
;
17555 int high_bound_is_count
= 0;
17558 orig_base_type
= read_subrange_index_type (die
, cu
);
17560 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17561 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17562 creating the range type, but we use the result of check_typedef
17563 when examining properties of the type. */
17564 base_type
= check_typedef (orig_base_type
);
17566 /* The die_type call above may have already set the type for this DIE. */
17567 range_type
= get_die_type (die
, cu
);
17571 high
.set_const_val (0);
17573 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17574 omitting DW_AT_lower_bound. */
17575 switch (cu
->lang ())
17578 case language_cplus
:
17579 low
.set_const_val (0);
17580 low_default_is_valid
= 1;
17582 case language_fortran
:
17583 low
.set_const_val (1);
17584 low_default_is_valid
= 1;
17587 case language_objc
:
17588 case language_rust
:
17589 low
.set_const_val (0);
17590 low_default_is_valid
= (cu
->header
.version
>= 4);
17594 case language_pascal
:
17595 low
.set_const_val (1);
17596 low_default_is_valid
= (cu
->header
.version
>= 4);
17599 low
.set_const_val (0);
17600 low_default_is_valid
= 0;
17604 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
17605 if (attr
!= nullptr)
17606 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
17607 else if (!low_default_is_valid
)
17608 complaint (_("Missing DW_AT_lower_bound "
17609 "- DIE at %s [in module %s]"),
17610 sect_offset_str (die
->sect_off
),
17611 objfile_name (cu
->per_objfile
->objfile
));
17613 struct attribute
*attr_ub
, *attr_count
;
17614 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
17615 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17617 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
17618 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17620 /* If bounds are constant do the final calculation here. */
17621 if (low
.kind () == PROP_CONST
&& high
.kind () == PROP_CONST
)
17622 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
17624 high_bound_is_count
= 1;
17628 if (attr_ub
!= NULL
)
17629 complaint (_("Unresolved DW_AT_upper_bound "
17630 "- DIE at %s [in module %s]"),
17631 sect_offset_str (die
->sect_off
),
17632 objfile_name (cu
->per_objfile
->objfile
));
17633 if (attr_count
!= NULL
)
17634 complaint (_("Unresolved DW_AT_count "
17635 "- DIE at %s [in module %s]"),
17636 sect_offset_str (die
->sect_off
),
17637 objfile_name (cu
->per_objfile
->objfile
));
17642 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
17643 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
17644 bias
= bias_attr
->constant_value (0);
17646 /* Normally, the DWARF producers are expected to use a signed
17647 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17648 But this is unfortunately not always the case, as witnessed
17649 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17650 is used instead. To work around that ambiguity, we treat
17651 the bounds as signed, and thus sign-extend their values, when
17652 the base type is signed.
17654 Skip it if the base type's length is larger than ULONGEST, to avoid
17655 the undefined behavior of a too large left shift. We don't really handle
17656 constants larger than 8 bytes anyway, at the moment. */
17658 if (base_type
->length () <= sizeof (ULONGEST
))
17660 ULONGEST negative_mask
17661 = -((ULONGEST
) 1 << (base_type
->length () * TARGET_CHAR_BIT
- 1));
17663 if (low
.kind () == PROP_CONST
17664 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
17665 low
.set_const_val (low
.const_val () | negative_mask
);
17667 if (high
.kind () == PROP_CONST
17668 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
17669 high
.set_const_val (high
.const_val () | negative_mask
);
17672 /* Check for bit and byte strides. */
17673 struct dynamic_prop byte_stride_prop
;
17674 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
17675 if (attr_byte_stride
!= nullptr)
17677 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17678 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
17682 struct dynamic_prop bit_stride_prop
;
17683 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
17684 if (attr_bit_stride
!= nullptr)
17686 /* It only makes sense to have either a bit or byte stride. */
17687 if (attr_byte_stride
!= nullptr)
17689 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17690 "- DIE at %s [in module %s]"),
17691 sect_offset_str (die
->sect_off
),
17692 objfile_name (cu
->per_objfile
->objfile
));
17693 attr_bit_stride
= nullptr;
17697 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17698 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
17703 if (attr_byte_stride
!= nullptr
17704 || attr_bit_stride
!= nullptr)
17706 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
17707 struct dynamic_prop
*stride
17708 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
17711 = create_range_type_with_stride (NULL
, orig_base_type
, &low
,
17712 &high
, bias
, stride
, byte_stride_p
);
17715 range_type
= create_range_type (NULL
, orig_base_type
, &low
, &high
, bias
);
17717 if (high_bound_is_count
)
17718 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
17720 /* Ada expects an empty array on no boundary attributes. */
17721 if (attr
== NULL
&& cu
->lang () != language_ada
)
17722 range_type
->bounds ()->high
.set_undefined ();
17724 name
= dwarf2_name (die
, cu
);
17726 range_type
->set_name (name
);
17728 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17729 if (attr
!= nullptr)
17730 range_type
->set_length (attr
->constant_value (0));
17732 maybe_set_alignment (cu
, die
, range_type
);
17734 set_die_type (die
, range_type
, cu
);
17736 /* set_die_type should be already done. */
17737 set_descriptive_type (range_type
, die
, cu
);
17742 static struct type
*
17743 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17747 type
= init_type (cu
->per_objfile
->objfile
, TYPE_CODE_VOID
, 0, NULL
);
17748 type
->set_name (dwarf2_name (die
, cu
));
17750 /* In Ada, an unspecified type is typically used when the description
17751 of the type is deferred to a different unit. When encountering
17752 such a type, we treat it as a stub, and try to resolve it later on,
17754 Mark this as a stub type for all languages though. */
17755 type
->set_is_stub (true);
17757 return set_die_type (die
, type
, cu
);
17760 /* Read a single die and all its descendents. Set the die's sibling
17761 field to NULL; set other fields in the die correctly, and set all
17762 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17763 location of the info_ptr after reading all of those dies. PARENT
17764 is the parent of the die in question. */
17766 static struct die_info
*
17767 read_die_and_children (const struct die_reader_specs
*reader
,
17768 const gdb_byte
*info_ptr
,
17769 const gdb_byte
**new_info_ptr
,
17770 struct die_info
*parent
)
17772 struct die_info
*die
;
17773 const gdb_byte
*cur_ptr
;
17775 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0);
17778 *new_info_ptr
= cur_ptr
;
17781 store_in_ref_table (die
, reader
->cu
);
17783 if (die
->has_children
)
17784 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
17788 *new_info_ptr
= cur_ptr
;
17791 die
->sibling
= NULL
;
17792 die
->parent
= parent
;
17796 /* Read a die, all of its descendents, and all of its siblings; set
17797 all of the fields of all of the dies correctly. Arguments are as
17798 in read_die_and_children. */
17800 static struct die_info
*
17801 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
17802 const gdb_byte
*info_ptr
,
17803 const gdb_byte
**new_info_ptr
,
17804 struct die_info
*parent
)
17806 struct die_info
*first_die
, *last_sibling
;
17807 const gdb_byte
*cur_ptr
;
17809 cur_ptr
= info_ptr
;
17810 first_die
= last_sibling
= NULL
;
17814 struct die_info
*die
17815 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
17819 *new_info_ptr
= cur_ptr
;
17826 last_sibling
->sibling
= die
;
17828 last_sibling
= die
;
17832 /* Read a die, all of its descendents, and all of its siblings; set
17833 all of the fields of all of the dies correctly. Arguments are as
17834 in read_die_and_children.
17835 This the main entry point for reading a DIE and all its children. */
17837 static struct die_info
*
17838 read_die_and_siblings (const struct die_reader_specs
*reader
,
17839 const gdb_byte
*info_ptr
,
17840 const gdb_byte
**new_info_ptr
,
17841 struct die_info
*parent
)
17843 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
17844 new_info_ptr
, parent
);
17846 if (dwarf_die_debug
)
17848 gdb_printf (gdb_stdlog
,
17849 "Read die from %s@0x%x of %s:\n",
17850 reader
->die_section
->get_name (),
17851 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17852 bfd_get_filename (reader
->abfd
));
17853 die
->dump (dwarf_die_debug
);
17859 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17861 The caller is responsible for filling in the extra attributes
17862 and updating (*DIEP)->num_attrs.
17863 Set DIEP to point to a newly allocated die with its information,
17864 except for its child, sibling, and parent fields. */
17866 static const gdb_byte
*
17867 read_full_die_1 (const struct die_reader_specs
*reader
,
17868 struct die_info
**diep
, const gdb_byte
*info_ptr
,
17869 int num_extra_attrs
)
17871 unsigned int abbrev_number
, bytes_read
, i
;
17872 const struct abbrev_info
*abbrev
;
17873 struct die_info
*die
;
17874 struct dwarf2_cu
*cu
= reader
->cu
;
17875 bfd
*abfd
= reader
->abfd
;
17877 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
17878 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17879 info_ptr
+= bytes_read
;
17880 if (!abbrev_number
)
17886 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
17888 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17890 bfd_get_filename (abfd
));
17892 die
= die_info::allocate (&cu
->comp_unit_obstack
,
17893 abbrev
->num_attrs
+ num_extra_attrs
);
17894 die
->sect_off
= sect_off
;
17895 die
->tag
= abbrev
->tag
;
17896 die
->abbrev
= abbrev_number
;
17897 die
->has_children
= abbrev
->has_children
;
17899 /* Make the result usable.
17900 The caller needs to update num_attrs after adding the extra
17902 die
->num_attrs
= abbrev
->num_attrs
;
17904 bool any_need_reprocess
= false;
17905 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17907 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
17909 if (die
->attrs
[i
].requires_reprocessing_p ())
17910 any_need_reprocess
= true;
17913 struct attribute
*attr
= die
->attr (DW_AT_str_offsets_base
);
17914 if (attr
!= nullptr && attr
->form_is_unsigned ())
17915 cu
->str_offsets_base
= attr
->as_unsigned ();
17917 attr
= die
->attr (DW_AT_loclists_base
);
17918 if (attr
!= nullptr)
17919 cu
->loclist_base
= attr
->as_unsigned ();
17921 auto maybe_addr_base
= die
->addr_base ();
17922 if (maybe_addr_base
.has_value ())
17923 cu
->addr_base
= *maybe_addr_base
;
17925 attr
= die
->attr (DW_AT_rnglists_base
);
17926 if (attr
!= nullptr)
17927 cu
->rnglists_base
= attr
->as_unsigned ();
17929 if (any_need_reprocess
)
17931 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17933 if (die
->attrs
[i
].requires_reprocessing_p ())
17934 read_attribute_reprocess (reader
, &die
->attrs
[i
], die
->tag
);
17941 /* Read a die and all its attributes.
17942 Set DIEP to point to a newly allocated die with its information,
17943 except for its child, sibling, and parent fields. */
17945 static const gdb_byte
*
17946 read_full_die (const struct die_reader_specs
*reader
,
17947 struct die_info
**diep
, const gdb_byte
*info_ptr
)
17949 const gdb_byte
*result
;
17951 result
= read_full_die_1 (reader
, diep
, info_ptr
, 0);
17953 if (dwarf_die_debug
)
17955 gdb_printf (gdb_stdlog
,
17956 "Read die from %s@0x%x of %s:\n",
17957 reader
->die_section
->get_name (),
17958 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17959 bfd_get_filename (reader
->abfd
));
17960 (*diep
)->dump (dwarf_die_debug
);
17968 cooked_indexer::check_bounds (cutu_reader
*reader
)
17970 if (reader
->cu
->per_cu
->addresses_seen
)
17973 dwarf2_cu
*cu
= reader
->cu
;
17975 CORE_ADDR best_lowpc
= 0, best_highpc
= 0;
17976 /* Possibly set the default values of LOWPC and HIGHPC from
17978 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
17979 enum pc_bounds_kind cu_bounds_kind
17980 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
17981 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
17982 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
17984 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17985 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
17986 struct gdbarch
*gdbarch
= objfile
->arch ();
17988 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_lowpc
+ baseaddr
)
17991 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_highpc
+ baseaddr
)
17993 /* Store the contiguous range if it is not empty; it can be
17994 empty for CUs with no code. */
17995 m_index_storage
->get_addrmap ()->set_empty (low
, high
, cu
->per_cu
);
17997 cu
->per_cu
->addresses_seen
= true;
18001 /* Helper function that returns true if TAG can have a linkage
18005 tag_can_have_linkage_name (enum dwarf_tag tag
)
18009 case DW_TAG_variable
:
18010 case DW_TAG_subprogram
:
18019 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
18020 dwarf2_per_objfile
*per_objfile
,
18021 sect_offset sect_off
, bool is_dwz
,
18024 /* Lookups for type unit references are always in the CU, and
18025 cross-CU references will crash. */
18026 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
18027 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
18030 dwarf2_per_cu_data
*per_cu
18031 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
18032 per_objfile
->per_bfd
);
18034 /* When scanning, we only want to visit a given CU a single time.
18035 Doing this check here avoids self-imports as well. */
18039 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
18042 if (per_cu
== m_per_cu
)
18045 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
18046 if (result
== nullptr)
18048 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
18049 m_index_storage
->get_abbrev_cache ());
18051 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
18053 std::unique_ptr
<cutu_reader
> copy
18054 (new cutu_reader (std::move (new_reader
)));
18055 result
= m_index_storage
->preserve (std::move (copy
));
18058 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
18062 check_bounds (result
);
18068 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
18069 cutu_reader
*reader
,
18070 const gdb_byte
*watermark_ptr
,
18071 const gdb_byte
*info_ptr
,
18072 const abbrev_info
*abbrev
,
18074 const char **linkage_name
,
18075 cooked_index_flag
*flags
,
18076 sect_offset
*sibling_offset
,
18077 const cooked_index_entry
**parent_entry
,
18078 CORE_ADDR
*maybe_defer
,
18079 bool for_specification
)
18081 bool origin_is_dwz
= false;
18082 bool is_declaration
= false;
18083 sect_offset origin_offset
{};
18085 gdb::optional
<CORE_ADDR
> low_pc
;
18086 gdb::optional
<CORE_ADDR
> high_pc
;
18087 bool high_pc_relative
= false;
18089 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18092 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18093 if (attr
.requires_reprocessing_p ())
18094 read_attribute_reprocess (reader
, &attr
, abbrev
->tag
);
18096 /* Store the data if it is of an attribute we want to keep in a
18097 partial symbol table. */
18101 switch (abbrev
->tag
)
18103 case DW_TAG_compile_unit
:
18104 case DW_TAG_partial_unit
:
18105 case DW_TAG_type_unit
:
18106 /* Compilation units have a DW_AT_name that is a filename, not
18107 a source language identifier. */
18111 if (*name
== nullptr)
18112 *name
= attr
.as_string ();
18117 case DW_AT_linkage_name
:
18118 case DW_AT_MIPS_linkage_name
:
18119 /* Note that both forms of linkage name might appear. We
18120 assume they will be the same, and we only store the last
18122 if (*linkage_name
== nullptr)
18123 *linkage_name
= attr
.as_string ();
18126 case DW_AT_main_subprogram
:
18127 if (attr
.as_boolean ())
18131 case DW_AT_declaration
:
18132 is_declaration
= attr
.as_boolean ();
18135 case DW_AT_sibling
:
18136 if (sibling_offset
!= nullptr)
18137 *sibling_offset
= attr
.get_ref_die_offset ();
18140 case DW_AT_specification
:
18141 case DW_AT_abstract_origin
:
18142 case DW_AT_extension
:
18143 origin_offset
= attr
.get_ref_die_offset ();
18144 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
18147 case DW_AT_external
:
18148 if (attr
.as_boolean ())
18149 *flags
&= ~IS_STATIC
;
18152 case DW_AT_enum_class
:
18153 if (attr
.as_boolean ())
18154 *flags
|= IS_ENUM_CLASS
;
18158 low_pc
= attr
.as_address ();
18161 case DW_AT_high_pc
:
18162 high_pc
= attr
.as_address ();
18163 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
18164 high_pc_relative
= true;
18167 case DW_AT_location
:
18168 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
18170 struct dwarf_block
*locdesc
= attr
.as_block ();
18171 CORE_ADDR addr
= decode_locdesc (locdesc
, reader
->cu
);
18173 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
)
18176 /* For variables, we don't want to try decoding the
18177 type just to find the size -- for gdb's purposes
18178 we only need the address of a variable. */
18179 high_pc
= addr
+ 1;
18180 high_pc_relative
= false;
18186 if (!scanning_per_cu
->addresses_seen
)
18188 /* Offset in the .debug_ranges or .debug_rnglist section
18189 (depending on DWARF version). */
18190 ULONGEST ranges_offset
= attr
.as_unsigned ();
18192 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18193 want to add this value. */
18194 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
18196 CORE_ADDR lowpc
, highpc
;
18197 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
18198 m_index_storage
->get_addrmap (),
18199 scanning_per_cu
, abbrev
->tag
);
18205 /* We don't want to examine declarations, but if we found a
18206 declaration when handling DW_AT_specification or the like, then
18207 that is ok. Similarly, we allow an external variable without a
18208 location; those are resolved via minimal symbols. */
18209 if (is_declaration
&& !for_specification
18210 && !(abbrev
->tag
== DW_TAG_variable
&& (*flags
& IS_STATIC
) == 0))
18212 /* We always want to recurse into some types, but we may not
18213 want to treat them as definitions. */
18214 if ((abbrev
->tag
== DW_TAG_class_type
18215 || abbrev
->tag
== DW_TAG_structure_type
18216 || abbrev
->tag
== DW_TAG_union_type
)
18217 && abbrev
->has_children
)
18218 *flags
|= IS_TYPE_DECLARATION
;
18221 *linkage_name
= nullptr;
18225 else if ((*name
== nullptr
18226 || (*linkage_name
== nullptr
18227 && tag_can_have_linkage_name (abbrev
->tag
))
18228 || (*parent_entry
== nullptr && m_language
!= language_c
))
18229 && origin_offset
!= sect_offset (0))
18231 cutu_reader
*new_reader
18232 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
18233 origin_is_dwz
, false);
18234 if (new_reader
!= nullptr)
18236 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
18237 + to_underlying (origin_offset
));
18239 if (new_reader
->cu
== reader
->cu
18240 && new_info_ptr
> watermark_ptr
18241 && *parent_entry
== nullptr)
18242 *maybe_defer
= form_addr (origin_offset
, origin_is_dwz
);
18243 else if (*parent_entry
== nullptr)
18245 CORE_ADDR lookup
= form_addr (origin_offset
, origin_is_dwz
);
18246 void *obj
= m_die_range_map
.find (lookup
);
18247 *parent_entry
= static_cast <cooked_index_entry
*> (obj
);
18250 unsigned int bytes_read
;
18251 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
18254 new_info_ptr
+= bytes_read
;
18255 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
, new_info_ptr
,
18256 new_abbrev
, name
, linkage_name
, flags
, nullptr,
18257 parent_entry
, maybe_defer
, true);
18261 if (!for_specification
)
18263 if (m_language
== language_ada
18264 && *linkage_name
== nullptr)
18265 *linkage_name
= *name
;
18267 if (!scanning_per_cu
->addresses_seen
18268 && low_pc
.has_value ()
18269 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
18271 && high_pc
.has_value ())
18273 if (high_pc_relative
)
18274 high_pc
= *high_pc
+ *low_pc
;
18276 if (*high_pc
> *low_pc
)
18278 struct objfile
*objfile
= reader
->cu
->per_objfile
->objfile
;
18279 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18280 struct gdbarch
*gdbarch
= objfile
->arch ();
18282 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *low_pc
+ baseaddr
)
18285 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *high_pc
+ baseaddr
)
18287 m_index_storage
->get_addrmap ()->set_empty (lo
, hi
- 1,
18292 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
18293 *flags
&= ~IS_STATIC
;
18295 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
18296 *name
= "(anonymous namespace)";
18298 if (m_language
== language_cplus
18299 && (abbrev
->tag
== DW_TAG_class_type
18300 || abbrev
->tag
== DW_TAG_interface_type
18301 || abbrev
->tag
== DW_TAG_structure_type
18302 || abbrev
->tag
== DW_TAG_union_type
18303 || abbrev
->tag
== DW_TAG_enumeration_type
18304 || abbrev
->tag
== DW_TAG_enumerator
))
18305 *flags
&= ~IS_STATIC
;
18312 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
18313 const gdb_byte
*info_ptr
,
18314 const abbrev_info
*abbrev
)
18316 sect_offset sect_off
{};
18317 bool is_dwz
= false;
18319 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18321 /* Note that we never need to reprocess attributes here. */
18323 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18325 if (attr
.name
== DW_AT_import
)
18327 sect_off
= attr
.get_ref_die_offset ();
18328 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
18329 || reader
->cu
->per_cu
->is_dwz
);
18333 /* Did not find DW_AT_import. */
18334 if (sect_off
== sect_offset (0))
18337 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
18338 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
18340 if (new_reader
!= nullptr)
18342 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
18344 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
18351 cooked_indexer::recurse (cutu_reader
*reader
,
18352 const gdb_byte
*info_ptr
,
18353 const cooked_index_entry
*parent_entry
,
18356 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18358 if (parent_entry
!= nullptr)
18360 CORE_ADDR start
= form_addr (parent_entry
->die_offset
,
18361 reader
->cu
->per_cu
->is_dwz
);
18362 CORE_ADDR end
= form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
18363 reader
->cu
->per_cu
->is_dwz
);
18364 m_die_range_map
.set_empty (start
, end
, (void *) parent_entry
);
18371 cooked_indexer::index_dies (cutu_reader
*reader
,
18372 const gdb_byte
*info_ptr
,
18373 const cooked_index_entry
*parent_entry
,
18376 const gdb_byte
*end_ptr
= (reader
->buffer
18377 + to_underlying (reader
->cu
->header
.sect_off
)
18378 + reader
->cu
->header
.get_length_with_initial ());
18380 while (info_ptr
< end_ptr
)
18382 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
18383 unsigned int bytes_read
;
18384 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
18386 info_ptr
+= bytes_read
;
18387 if (abbrev
== nullptr)
18390 if (abbrev
->tag
== DW_TAG_imported_unit
)
18392 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
18396 if (!abbrev
->interesting
)
18398 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
18399 if (fully
&& abbrev
->has_children
)
18400 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18404 const char *name
= nullptr;
18405 const char *linkage_name
= nullptr;
18406 CORE_ADDR defer
= 0;
18407 cooked_index_flag flags
= IS_STATIC
;
18408 sect_offset sibling
{};
18409 const cooked_index_entry
*this_parent_entry
= parent_entry
;
18410 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
18411 info_ptr
, abbrev
, &name
, &linkage_name
,
18412 &flags
, &sibling
, &this_parent_entry
,
18415 if (abbrev
->tag
== DW_TAG_namespace
18416 && m_language
== language_cplus
18417 && strcmp (name
, "::") == 0)
18419 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18420 generated bogus DW_TAG_namespace DIEs with a name of "::"
18421 for the global namespace. Work around this problem
18426 const cooked_index_entry
*this_entry
= nullptr;
18427 if (name
!= nullptr)
18430 m_deferred_entries
.push_back ({
18431 this_die
, name
, defer
, abbrev
->tag
, flags
18434 this_entry
= m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18435 name
, this_parent_entry
,
18439 if (linkage_name
!= nullptr)
18441 /* We only want this to be "main" if it has a linkage name
18442 but not an ordinary name. */
18443 if (name
!= nullptr)
18444 flags
= flags
& ~IS_MAIN
;
18445 /* Set the IS_LINKAGE on for everything except when functions
18446 have linkage name present but name is absent. */
18447 if (name
!= nullptr
18448 || (abbrev
->tag
!= DW_TAG_subprogram
18449 && abbrev
->tag
!= DW_TAG_inlined_subroutine
18450 && abbrev
->tag
!= DW_TAG_entry_point
))
18451 flags
= flags
| IS_LINKAGE
;
18452 m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18453 linkage_name
, nullptr, m_per_cu
);
18456 if (abbrev
->has_children
)
18458 switch (abbrev
->tag
)
18460 case DW_TAG_class_type
:
18461 case DW_TAG_interface_type
:
18462 case DW_TAG_structure_type
:
18463 case DW_TAG_union_type
:
18464 if (m_language
!= language_c
&& this_entry
!= nullptr)
18466 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18471 case DW_TAG_enumeration_type
:
18472 /* We need to recurse even for an anonymous enumeration.
18473 Which scope we record as the parent scope depends on
18474 whether we're reading an "enum class". If so, we use
18475 the enum itself as the parent, yielding names like
18476 "enum_class::enumerator"; otherwise we inject the
18477 names into our own parent scope. */
18478 info_ptr
= recurse (reader
, info_ptr
,
18479 ((flags
& IS_ENUM_CLASS
) == 0)
18485 case DW_TAG_module
:
18486 if (this_entry
== nullptr)
18489 case DW_TAG_namespace
:
18490 /* We don't check THIS_ENTRY for a namespace, to handle
18491 the ancient G++ workaround pointed out above. */
18492 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18495 case DW_TAG_subprogram
:
18496 if ((m_language
== language_fortran
18497 || m_language
== language_ada
)
18498 && this_entry
!= nullptr)
18500 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
18506 if (sibling
!= sect_offset (0))
18508 const gdb_byte
*sibling_ptr
18509 = reader
->buffer
+ to_underlying (sibling
);
18511 if (sibling_ptr
< info_ptr
)
18512 complaint (_("DW_AT_sibling points backwards"));
18513 else if (sibling_ptr
> reader
->buffer_end
)
18514 reader
->die_section
->overflow_complaint ();
18516 info_ptr
= sibling_ptr
;
18519 info_ptr
= skip_children (reader
, info_ptr
);
18527 cooked_indexer::make_index (cutu_reader
*reader
)
18529 check_bounds (reader
);
18530 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
18531 if (!reader
->comp_unit_die
->has_children
)
18533 index_dies (reader
, reader
->info_ptr
, nullptr, false);
18535 for (const auto &entry
: m_deferred_entries
)
18537 CORE_ADDR key
= form_addr (entry
.die_offset
, m_per_cu
->is_dwz
);
18538 void *obj
= m_die_range_map
.find (key
);
18539 cooked_index_entry
*parent
= static_cast <cooked_index_entry
*> (obj
);
18540 m_index_storage
->add (entry
.die_offset
, entry
.tag
, entry
.flags
,
18541 entry
.name
, parent
, m_per_cu
);
18545 /* An implementation of quick_symbol_functions for the cooked DWARF
18548 struct cooked_index_functions
: public dwarf2_base_index_functions
18550 dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
18551 CORE_ADDR adjusted_pc
) override
;
18553 struct compunit_symtab
*find_compunit_symtab_by_address
18554 (struct objfile
*objfile
, CORE_ADDR address
) override
;
18556 void dump (struct objfile
*objfile
) override
18558 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18559 cooked_index
*index
18560 = (gdb::checked_static_cast
<cooked_index
*>
18561 (per_objfile
->per_bfd
->index_table
.get ()));
18562 if (index
== nullptr)
18565 gdb_printf ("Cooked index in use:\n");
18567 index
->dump (objfile
->arch ());
18570 void expand_matching_symbols
18572 const lookup_name_info
&lookup_name
,
18573 domain_enum domain
,
18575 symbol_compare_ftype
*ordered_compare
) override
;
18577 bool expand_symtabs_matching
18578 (struct objfile
*objfile
,
18579 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18580 const lookup_name_info
*lookup_name
,
18581 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18582 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18583 block_search_flags search_flags
,
18584 domain_enum domain
,
18585 enum search_domain kind
) override
;
18587 bool can_lazily_read_symbols () override
18592 void read_partial_symbols (struct objfile
*objfile
) override
18594 if (dwarf2_has_info (objfile
, nullptr))
18595 dwarf2_build_psymtabs (objfile
);
18599 dwarf2_per_cu_data
*
18600 cooked_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
18601 CORE_ADDR adjusted_pc
)
18603 cooked_index
*table
18604 = (gdb::checked_static_cast
<cooked_index
*>
18605 (per_bfd
->index_table
.get ()));
18606 if (table
== nullptr)
18608 return table
->lookup (adjusted_pc
);
18611 struct compunit_symtab
*
18612 cooked_index_functions::find_compunit_symtab_by_address
18613 (struct objfile
*objfile
, CORE_ADDR address
)
18615 if (objfile
->sect_index_data
== -1)
18618 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18619 cooked_index
*table
18620 = (gdb::checked_static_cast
<cooked_index
*>
18621 (per_objfile
->per_bfd
->index_table
.get ()));
18622 if (table
== nullptr)
18625 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
18626 dwarf2_per_cu_data
*per_cu
= table
->lookup (address
- baseaddr
);
18627 if (per_cu
== nullptr)
18630 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
18634 cooked_index_functions::expand_matching_symbols
18635 (struct objfile
*objfile
,
18636 const lookup_name_info
&lookup_name
,
18637 domain_enum domain
,
18639 symbol_compare_ftype
*ordered_compare
)
18641 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18642 cooked_index
*table
18643 = (gdb::checked_static_cast
<cooked_index
*>
18644 (per_objfile
->per_bfd
->index_table
.get ()));
18645 if (table
== nullptr)
18648 const block_search_flags search_flags
= (global
18649 ? SEARCH_GLOBAL_BLOCK
18650 : SEARCH_STATIC_BLOCK
);
18651 const language_defn
*lang
= language_def (language_ada
);
18652 symbol_name_matcher_ftype
*name_match
18653 = lang
->get_symbol_name_matcher (lookup_name
);
18655 for (const cooked_index_entry
*entry
: table
->all_entries ())
18659 if (entry
->parent_entry
!= nullptr)
18662 if (!entry
->matches (search_flags
)
18663 || !entry
->matches (domain
))
18666 if (name_match (entry
->canonical
, lookup_name
, nullptr))
18667 dw2_instantiate_symtab (entry
->per_cu
, per_objfile
, false);
18672 cooked_index_functions::expand_symtabs_matching
18673 (struct objfile
*objfile
,
18674 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18675 const lookup_name_info
*lookup_name
,
18676 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18677 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18678 block_search_flags search_flags
,
18679 domain_enum domain
,
18680 enum search_domain kind
)
18682 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18684 cooked_index
*table
18685 = (gdb::checked_static_cast
<cooked_index
*>
18686 (per_objfile
->per_bfd
->index_table
.get ()));
18687 if (table
== nullptr)
18692 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
18694 /* This invariant is documented in quick-functions.h. */
18695 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
18696 if (lookup_name
== nullptr)
18698 for (dwarf2_per_cu_data
*per_cu
18699 : all_units_range (per_objfile
->per_bfd
))
18703 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
18711 lookup_name_info lookup_name_without_params
18712 = lookup_name
->make_ignore_params ();
18713 bool completing
= lookup_name
->completion_mode ();
18715 /* Unique styles of language splitting. */
18716 static const enum language unique_styles
[] =
18718 /* No splitting is also a style. */
18720 /* This includes Rust. */
18722 /* This includes Go. */
18727 for (enum language lang
: unique_styles
)
18729 std::vector
<gdb::string_view
> name_vec
18730 = lookup_name_without_params
.split_name (lang
);
18731 std::string last_name
= gdb::to_string (name_vec
.back ());
18733 for (const cooked_index_entry
*entry
: table
->find (last_name
,
18738 /* No need to consider symbols from expanded CUs. */
18739 if (per_objfile
->symtab_set_p (entry
->per_cu
))
18742 /* If file-matching was done, we don't need to consider
18743 symbols from unmarked CUs. */
18744 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
18747 /* See if the symbol matches the type filter. */
18748 if (!entry
->matches (search_flags
)
18749 || !entry
->matches (domain
)
18750 || !entry
->matches (kind
))
18753 /* We've found the base name of the symbol; now walk its
18754 parentage chain, ensuring that each component
18758 const cooked_index_entry
*parent
= entry
->parent_entry
;
18759 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
18761 /* If we ran out of entries, or if this segment doesn't
18762 match, this did not match. */
18763 if (parent
== nullptr
18764 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
18765 name_vec
[i
- 1].length ()) != 0)
18771 parent
= parent
->parent_entry
;
18777 /* Might have been looking for "a::b" and found
18779 if (symbol_matcher
== nullptr)
18781 symbol_name_match_type match_type
18782 = lookup_name_without_params
.match_type ();
18783 if ((match_type
== symbol_name_match_type::FULL
18784 || (lang
!= language_ada
18785 && match_type
== symbol_name_match_type::EXPRESSION
))
18786 && parent
!= nullptr)
18791 auto_obstack temp_storage
;
18792 const char *full_name
= entry
->full_name (&temp_storage
);
18793 if (!symbol_matcher (full_name
))
18797 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
18807 /* Return a new cooked_index_functions object. */
18809 static quick_symbol_functions_up
18810 make_cooked_index_funcs ()
18812 return quick_symbol_functions_up (new cooked_index_functions
);
18815 quick_symbol_functions_up
18816 cooked_index::make_quick_functions () const
18818 return make_cooked_index_funcs ();
18823 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18824 contents from the given SECTION in the HEADER.
18826 HEADER_OFFSET is the offset of the header in the section. */
18828 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
18829 struct dwarf2_section_info
*section
,
18830 sect_offset header_offset
)
18832 unsigned int bytes_read
;
18833 bfd
*abfd
= section
->get_bfd_owner ();
18834 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
18836 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
18837 info_ptr
+= bytes_read
;
18839 header
->version
= read_2_bytes (abfd
, info_ptr
);
18842 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
18845 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
18848 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
18851 /* Return the DW_AT_loclists_base value for the CU. */
18853 lookup_loclist_base (struct dwarf2_cu
*cu
)
18855 /* For the .dwo unit, the loclist_base points to the first offset following
18856 the header. The header consists of the following entities-
18857 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18859 2. version (2 bytes)
18860 3. address size (1 byte)
18861 4. segment selector size (1 byte)
18862 5. offset entry count (4 bytes)
18863 These sizes are derived as per the DWARFv5 standard. */
18864 if (cu
->dwo_unit
!= nullptr)
18866 if (cu
->header
.initial_length_size
== 4)
18867 return LOCLIST_HEADER_SIZE32
;
18868 return LOCLIST_HEADER_SIZE64
;
18870 return cu
->loclist_base
;
18873 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18874 array of offsets in the .debug_loclists section. */
18877 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
18879 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18880 struct objfile
*objfile
= per_objfile
->objfile
;
18881 bfd
*abfd
= objfile
->obfd
.get ();
18882 ULONGEST loclist_header_size
=
18883 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
18884 : LOCLIST_HEADER_SIZE64
);
18885 ULONGEST loclist_base
= lookup_loclist_base (cu
);
18887 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18888 ULONGEST start_offset
=
18889 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
18891 /* Get loclists section. */
18892 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
18894 /* Read the loclists section content. */
18895 section
->read (objfile
);
18896 if (section
->buffer
== NULL
)
18897 error (_("DW_FORM_loclistx used without .debug_loclists "
18898 "section [in module %s]"), objfile_name (objfile
));
18900 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18901 so if loclist_base is smaller than the header size, we have a problem. */
18902 if (loclist_base
< loclist_header_size
)
18903 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18904 objfile_name (objfile
));
18906 /* Read the header of the loclists contribution. */
18907 struct loclists_rnglists_header header
;
18908 read_loclists_rnglists_header (&header
, section
,
18909 (sect_offset
) (loclist_base
- loclist_header_size
));
18911 /* Verify the loclist index is valid. */
18912 if (loclist_index
>= header
.offset_entry_count
)
18913 error (_("DW_FORM_loclistx pointing outside of "
18914 ".debug_loclists offset array [in module %s]"),
18915 objfile_name (objfile
));
18917 /* Validate that reading won't go beyond the end of the section. */
18918 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18919 error (_("Reading DW_FORM_loclistx index beyond end of"
18920 ".debug_loclists section [in module %s]"),
18921 objfile_name (objfile
));
18923 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18925 if (cu
->header
.offset_size
== 4)
18926 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
18928 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
18931 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18932 array of offsets in the .debug_rnglists section. */
18935 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
18938 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
18939 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
18940 bfd
*abfd
= objfile
->obfd
.get ();
18941 ULONGEST rnglist_header_size
=
18942 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
18943 : RNGLIST_HEADER_SIZE64
);
18945 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18946 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18948 ULONGEST rnglist_base
=
18949 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
18951 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18952 ULONGEST start_offset
=
18953 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
18955 /* Get rnglists section. */
18956 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
18958 /* Read the rnglists section content. */
18959 section
->read (objfile
);
18960 if (section
->buffer
== nullptr)
18961 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18963 objfile_name (objfile
));
18965 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18966 so if rnglist_base is smaller than the header size, we have a problem. */
18967 if (rnglist_base
< rnglist_header_size
)
18968 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18969 objfile_name (objfile
));
18971 /* Read the header of the rnglists contribution. */
18972 struct loclists_rnglists_header header
;
18973 read_loclists_rnglists_header (&header
, section
,
18974 (sect_offset
) (rnglist_base
- rnglist_header_size
));
18976 /* Verify the rnglist index is valid. */
18977 if (rnglist_index
>= header
.offset_entry_count
)
18978 error (_("DW_FORM_rnglistx index pointing outside of "
18979 ".debug_rnglists offset array [in module %s]"),
18980 objfile_name (objfile
));
18982 /* Validate that reading won't go beyond the end of the section. */
18983 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18984 error (_("Reading DW_FORM_rnglistx index beyond end of"
18985 ".debug_rnglists section [in module %s]"),
18986 objfile_name (objfile
));
18988 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18990 if (cu
->header
.offset_size
== 4)
18991 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
18993 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
18996 /* Process the attributes that had to be skipped in the first round. These
18997 attributes are the ones that need str_offsets_base or addr_base attributes.
18998 They could not have been processed in the first round, because at the time
18999 the values of str_offsets_base or addr_base may not have been known. */
19001 read_attribute_reprocess (const struct die_reader_specs
*reader
,
19002 struct attribute
*attr
, dwarf_tag tag
)
19004 struct dwarf2_cu
*cu
= reader
->cu
;
19005 switch (attr
->form
)
19007 case DW_FORM_addrx
:
19008 case DW_FORM_GNU_addr_index
:
19009 attr
->set_address (read_addr_index (cu
,
19010 attr
->as_unsigned_reprocess ()));
19012 case DW_FORM_loclistx
:
19014 sect_offset loclists_sect_off
19015 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
19017 attr
->set_unsigned (to_underlying (loclists_sect_off
));
19020 case DW_FORM_rnglistx
:
19022 sect_offset rnglists_sect_off
19023 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
19025 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
19029 case DW_FORM_strx1
:
19030 case DW_FORM_strx2
:
19031 case DW_FORM_strx3
:
19032 case DW_FORM_strx4
:
19033 case DW_FORM_GNU_str_index
:
19035 unsigned int str_index
= attr
->as_unsigned_reprocess ();
19036 gdb_assert (!attr
->canonical_string_p ());
19037 if (reader
->dwo_file
!= NULL
)
19038 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
19041 attr
->set_string_noncanonical (read_stub_str_index (cu
,
19046 gdb_assert_not_reached ("Unexpected DWARF form.");
19050 /* Read an attribute value described by an attribute form. */
19052 static const gdb_byte
*
19053 read_attribute_value (const struct die_reader_specs
*reader
,
19054 struct attribute
*attr
, unsigned form
,
19055 LONGEST implicit_const
, const gdb_byte
*info_ptr
)
19057 struct dwarf2_cu
*cu
= reader
->cu
;
19058 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19059 struct objfile
*objfile
= per_objfile
->objfile
;
19060 bfd
*abfd
= reader
->abfd
;
19061 struct comp_unit_head
*cu_header
= &cu
->header
;
19062 unsigned int bytes_read
;
19063 struct dwarf_block
*blk
;
19065 attr
->form
= (enum dwarf_form
) form
;
19068 case DW_FORM_ref_addr
:
19069 if (cu_header
->version
== 2)
19070 attr
->set_unsigned (cu_header
->read_address (abfd
, info_ptr
,
19073 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19075 info_ptr
+= bytes_read
;
19077 case DW_FORM_GNU_ref_alt
:
19078 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19080 info_ptr
+= bytes_read
;
19084 struct gdbarch
*gdbarch
= objfile
->arch ();
19085 CORE_ADDR addr
= cu_header
->read_address (abfd
, info_ptr
, &bytes_read
);
19086 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
19087 attr
->set_address (addr
);
19088 info_ptr
+= bytes_read
;
19091 case DW_FORM_block2
:
19092 blk
= dwarf_alloc_block (cu
);
19093 blk
->size
= read_2_bytes (abfd
, info_ptr
);
19095 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19096 info_ptr
+= blk
->size
;
19097 attr
->set_block (blk
);
19099 case DW_FORM_block4
:
19100 blk
= dwarf_alloc_block (cu
);
19101 blk
->size
= read_4_bytes (abfd
, info_ptr
);
19103 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19104 info_ptr
+= blk
->size
;
19105 attr
->set_block (blk
);
19107 case DW_FORM_data2
:
19108 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
19111 case DW_FORM_data4
:
19112 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
19115 case DW_FORM_data8
:
19116 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
19119 case DW_FORM_data16
:
19120 blk
= dwarf_alloc_block (cu
);
19122 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
19124 attr
->set_block (blk
);
19126 case DW_FORM_sec_offset
:
19127 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19129 info_ptr
+= bytes_read
;
19131 case DW_FORM_loclistx
:
19133 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19135 info_ptr
+= bytes_read
;
19138 case DW_FORM_string
:
19139 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
19141 info_ptr
+= bytes_read
;
19144 if (!cu
->per_cu
->is_dwz
)
19146 attr
->set_string_noncanonical
19147 (read_indirect_string (per_objfile
,
19148 abfd
, info_ptr
, cu_header
,
19150 info_ptr
+= bytes_read
;
19154 case DW_FORM_line_strp
:
19155 if (!cu
->per_cu
->is_dwz
)
19157 attr
->set_string_noncanonical
19158 (per_objfile
->read_line_string (info_ptr
, cu_header
,
19160 info_ptr
+= bytes_read
;
19164 case DW_FORM_GNU_strp_alt
:
19166 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19167 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
19170 attr
->set_string_noncanonical
19171 (dwz
->read_string (objfile
, str_offset
));
19172 info_ptr
+= bytes_read
;
19175 case DW_FORM_exprloc
:
19176 case DW_FORM_block
:
19177 blk
= dwarf_alloc_block (cu
);
19178 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19179 info_ptr
+= bytes_read
;
19180 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19181 info_ptr
+= blk
->size
;
19182 attr
->set_block (blk
);
19184 case DW_FORM_block1
:
19185 blk
= dwarf_alloc_block (cu
);
19186 blk
->size
= read_1_byte (abfd
, info_ptr
);
19188 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19189 info_ptr
+= blk
->size
;
19190 attr
->set_block (blk
);
19192 case DW_FORM_data1
:
19194 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
19197 case DW_FORM_flag_present
:
19198 attr
->set_unsigned (1);
19200 case DW_FORM_sdata
:
19201 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
19202 info_ptr
+= bytes_read
;
19204 case DW_FORM_rnglistx
:
19206 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19208 info_ptr
+= bytes_read
;
19211 case DW_FORM_udata
:
19212 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
19213 info_ptr
+= bytes_read
;
19216 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19217 + read_1_byte (abfd
, info_ptr
)));
19221 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19222 + read_2_bytes (abfd
, info_ptr
)));
19226 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19227 + read_4_bytes (abfd
, info_ptr
)));
19231 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19232 + read_8_bytes (abfd
, info_ptr
)));
19235 case DW_FORM_ref_sig8
:
19236 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
19239 case DW_FORM_ref_udata
:
19240 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19241 + read_unsigned_leb128 (abfd
, info_ptr
,
19243 info_ptr
+= bytes_read
;
19245 case DW_FORM_indirect
:
19246 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19247 info_ptr
+= bytes_read
;
19248 if (form
== DW_FORM_implicit_const
)
19250 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
19251 info_ptr
+= bytes_read
;
19253 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
19256 case DW_FORM_implicit_const
:
19257 attr
->set_signed (implicit_const
);
19259 case DW_FORM_addrx
:
19260 case DW_FORM_GNU_addr_index
:
19261 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19263 info_ptr
+= bytes_read
;
19266 case DW_FORM_strx1
:
19267 case DW_FORM_strx2
:
19268 case DW_FORM_strx3
:
19269 case DW_FORM_strx4
:
19270 case DW_FORM_GNU_str_index
:
19272 ULONGEST str_index
;
19273 if (form
== DW_FORM_strx1
)
19275 str_index
= read_1_byte (abfd
, info_ptr
);
19278 else if (form
== DW_FORM_strx2
)
19280 str_index
= read_2_bytes (abfd
, info_ptr
);
19283 else if (form
== DW_FORM_strx3
)
19285 str_index
= read_3_bytes (abfd
, info_ptr
);
19288 else if (form
== DW_FORM_strx4
)
19290 str_index
= read_4_bytes (abfd
, info_ptr
);
19295 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19296 info_ptr
+= bytes_read
;
19298 attr
->set_unsigned_reprocess (str_index
);
19302 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19303 dwarf_form_name (form
),
19304 bfd_get_filename (abfd
));
19308 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
19309 attr
->form
= DW_FORM_GNU_ref_alt
;
19311 /* We have seen instances where the compiler tried to emit a byte
19312 size attribute of -1 which ended up being encoded as an unsigned
19313 0xffffffff. Although 0xffffffff is technically a valid size value,
19314 an object of this size seems pretty unlikely so we can relatively
19315 safely treat these cases as if the size attribute was invalid and
19316 treat them as zero by default. */
19317 if (attr
->name
== DW_AT_byte_size
19318 && form
== DW_FORM_data4
19319 && attr
->as_unsigned () >= 0xffffffff)
19322 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19323 hex_string (attr
->as_unsigned ()));
19324 attr
->set_unsigned (0);
19330 /* Read an attribute described by an abbreviated attribute. */
19332 static const gdb_byte
*
19333 read_attribute (const struct die_reader_specs
*reader
,
19334 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
19335 const gdb_byte
*info_ptr
)
19337 attr
->name
= abbrev
->name
;
19338 attr
->string_is_canonical
= 0;
19339 attr
->requires_reprocessing
= 0;
19340 return read_attribute_value (reader
, attr
, abbrev
->form
,
19341 abbrev
->implicit_const
, info_ptr
);
19344 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19346 static const char *
19347 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
19348 LONGEST str_offset
)
19350 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
19351 str_offset
, "DW_FORM_strp");
19354 /* Return pointer to string at .debug_str offset as read from BUF.
19355 BUF is assumed to be in a compilation unit described by CU_HEADER.
19356 Return *BYTES_READ_PTR count of bytes read from BUF. */
19358 static const char *
19359 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
19360 const gdb_byte
*buf
,
19361 const struct comp_unit_head
*cu_header
,
19362 unsigned int *bytes_read_ptr
)
19364 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19366 return read_indirect_string_at_offset (per_objfile
, str_offset
);
19372 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19373 unsigned int offset_size
)
19375 bfd
*abfd
= objfile
->obfd
.get ();
19376 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
19378 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19384 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19385 const struct comp_unit_head
*cu_header
,
19386 unsigned int *bytes_read_ptr
)
19388 bfd
*abfd
= objfile
->obfd
.get ();
19389 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19391 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19394 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19395 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19396 ADDR_SIZE is the size of addresses from the CU header. */
19399 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
19400 gdb::optional
<ULONGEST
> addr_base
, int addr_size
)
19402 struct objfile
*objfile
= per_objfile
->objfile
;
19403 bfd
*abfd
= objfile
->obfd
.get ();
19404 const gdb_byte
*info_ptr
;
19405 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
19407 per_objfile
->per_bfd
->addr
.read (objfile
);
19408 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
19409 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19410 objfile_name (objfile
));
19411 if (addr_base_or_zero
+ addr_index
* addr_size
19412 >= per_objfile
->per_bfd
->addr
.size
)
19413 error (_("DW_FORM_addr_index pointing outside of "
19414 ".debug_addr section [in module %s]"),
19415 objfile_name (objfile
));
19416 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
19417 + addr_index
* addr_size
);
19418 if (addr_size
== 4)
19419 return bfd_get_32 (abfd
, info_ptr
);
19421 return bfd_get_64 (abfd
, info_ptr
);
19424 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19427 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
19429 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
19430 cu
->addr_base
, cu
->header
.addr_size
);
19433 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19436 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
19437 unsigned int *bytes_read
)
19439 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
.get ();
19440 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
19442 return read_addr_index (cu
, addr_index
);
19448 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
19449 dwarf2_per_objfile
*per_objfile
,
19450 unsigned int addr_index
)
19452 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
19453 gdb::optional
<ULONGEST
> addr_base
;
19456 /* We need addr_base and addr_size.
19457 If we don't have PER_CU->cu, we have to get it.
19458 Nasty, but the alternative is storing the needed info in PER_CU,
19459 which at this point doesn't seem justified: it's not clear how frequently
19460 it would get used and it would increase the size of every PER_CU.
19461 Entry points like dwarf2_per_cu_addr_size do a similar thing
19462 so we're not in uncharted territory here.
19463 Alas we need to be a bit more complicated as addr_base is contained
19466 We don't need to read the entire CU(/TU).
19467 We just need the header and top level die.
19469 IWBN to use the aging mechanism to let us lazily later discard the CU.
19470 For now we skip this optimization. */
19474 addr_base
= cu
->addr_base
;
19475 addr_size
= cu
->header
.addr_size
;
19479 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
19480 addr_base
= reader
.cu
->addr_base
;
19481 addr_size
= reader
.cu
->header
.addr_size
;
19484 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
19487 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19488 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19491 static const char *
19492 read_str_index (struct dwarf2_cu
*cu
,
19493 struct dwarf2_section_info
*str_section
,
19494 struct dwarf2_section_info
*str_offsets_section
,
19495 ULONGEST str_offsets_base
, ULONGEST str_index
,
19496 unsigned offset_size
)
19498 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19499 struct objfile
*objfile
= per_objfile
->objfile
;
19500 const char *objf_name
= objfile_name (objfile
);
19501 bfd
*abfd
= objfile
->obfd
.get ();
19502 const gdb_byte
*info_ptr
;
19503 ULONGEST str_offset
;
19504 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19506 str_section
->read (objfile
);
19507 str_offsets_section
->read (objfile
);
19508 if (str_section
->buffer
== NULL
)
19509 error (_("%s used without %s section"
19510 " in CU at offset %s [in module %s]"),
19511 form_name
, str_section
->get_name (),
19512 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19513 if (str_offsets_section
->buffer
== NULL
)
19514 error (_("%s used without %s section"
19515 " in CU at offset %s [in module %s]"),
19516 form_name
, str_section
->get_name (),
19517 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19518 info_ptr
= (str_offsets_section
->buffer
19520 + str_index
* offset_size
);
19521 if (offset_size
== 4)
19522 str_offset
= bfd_get_32 (abfd
, info_ptr
);
19524 str_offset
= bfd_get_64 (abfd
, info_ptr
);
19525 if (str_offset
>= str_section
->size
)
19526 error (_("Offset from %s pointing outside of"
19527 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19528 form_name
, sect_offset_str (cu
->header
.sect_off
), objf_name
);
19529 return (const char *) (str_section
->buffer
+ str_offset
);
19532 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19534 static const char *
19535 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
19537 unsigned offset_size
;
19538 ULONGEST str_offsets_base
;
19539 if (reader
->cu
->header
.version
>= 5)
19541 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
19542 so assume the .debug_str_offsets section is DWARF5 as well, and
19543 parse the header. FIXME: Parse the header only once. */
19544 unsigned int bytes_read
= 0;
19545 bfd
*abfd
= reader
->dwo_file
->sections
.str_offsets
.get_bfd_owner ();
19546 const gdb_byte
*p
= reader
->dwo_file
->sections
.str_offsets
.buffer
;
19548 /* Header: Initial length. */
19549 read_initial_length (abfd
, p
+ bytes_read
, &bytes_read
);
19551 /* Determine offset_size based on the .debug_str_offsets header. */
19552 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
19553 offset_size
= dwarf5_is_dwarf64
? 8 : 4;
19555 /* Header: Version. */
19556 unsigned version
= read_2_bytes (abfd
, p
+ bytes_read
);
19561 /* We'd like one warning here about ignoring the section, but
19562 because we parse the header more than once (see FIXME above)
19563 we'd have many warnings, so use a complaint instead, which at
19564 least has a limit. */
19565 complaint (_("Section .debug_str_offsets in %s has unsupported"
19566 " version %d, use empty string."),
19567 reader
->dwo_file
->dwo_name
, version
);
19571 /* Header: Padding. */
19574 str_offsets_base
= bytes_read
;
19578 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
19579 section, assume the .debug_str_offsets section is pre-DWARF5 as
19580 well, which doesn't have a header. */
19581 str_offsets_base
= 0;
19583 /* Determine offset_size based on the .debug_info header. */
19584 offset_size
= reader
->cu
->header
.offset_size
;
19587 return read_str_index (reader
->cu
,
19588 &reader
->dwo_file
->sections
.str
,
19589 &reader
->dwo_file
->sections
.str_offsets
,
19590 str_offsets_base
, str_index
, offset_size
);
19593 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19595 static const char *
19596 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
19598 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19599 const char *objf_name
= objfile_name (objfile
);
19600 static const char form_name
[] = "DW_FORM_GNU_str_index";
19601 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
19603 if (!cu
->str_offsets_base
.has_value ())
19604 error (_("%s used in Fission stub without %s"
19605 " in CU at offset 0x%lx [in module %s]"),
19606 form_name
, str_offsets_attr_name
,
19607 (long) cu
->header
.offset_size
, objf_name
);
19609 return read_str_index (cu
,
19610 &cu
->per_objfile
->per_bfd
->str
,
19611 &cu
->per_objfile
->per_bfd
->str_offsets
,
19612 *cu
->str_offsets_base
, str_index
,
19613 cu
->header
.offset_size
);
19616 /* Return the length of an LEB128 number in BUF. */
19619 leb128_size (const gdb_byte
*buf
)
19621 const gdb_byte
*begin
= buf
;
19627 if ((byte
& 128) == 0)
19628 return buf
- begin
;
19632 static enum language
19633 dwarf_lang_to_enum_language (unsigned int lang
)
19635 enum language language
;
19644 language
= language_c
;
19647 case DW_LANG_C_plus_plus
:
19648 case DW_LANG_C_plus_plus_11
:
19649 case DW_LANG_C_plus_plus_14
:
19650 language
= language_cplus
;
19653 language
= language_d
;
19655 case DW_LANG_Fortran77
:
19656 case DW_LANG_Fortran90
:
19657 case DW_LANG_Fortran95
:
19658 case DW_LANG_Fortran03
:
19659 case DW_LANG_Fortran08
:
19660 language
= language_fortran
;
19663 language
= language_go
;
19665 case DW_LANG_Mips_Assembler
:
19666 language
= language_asm
;
19668 case DW_LANG_Ada83
:
19669 case DW_LANG_Ada95
:
19670 language
= language_ada
;
19672 case DW_LANG_Modula2
:
19673 language
= language_m2
;
19675 case DW_LANG_Pascal83
:
19676 language
= language_pascal
;
19679 language
= language_objc
;
19682 case DW_LANG_Rust_old
:
19683 language
= language_rust
;
19685 case DW_LANG_OpenCL
:
19686 language
= language_opencl
;
19688 case DW_LANG_Cobol74
:
19689 case DW_LANG_Cobol85
:
19691 language
= language_minimal
;
19698 /* Return the named attribute or NULL if not there. */
19700 static struct attribute
*
19701 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19706 struct attribute
*spec
= NULL
;
19708 for (i
= 0; i
< die
->num_attrs
; ++i
)
19710 if (die
->attrs
[i
].name
== name
)
19711 return &die
->attrs
[i
];
19712 if (die
->attrs
[i
].name
== DW_AT_specification
19713 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
19714 spec
= &die
->attrs
[i
];
19720 die
= follow_die_ref (die
, spec
, &cu
);
19726 /* Return the string associated with a string-typed attribute, or NULL if it
19727 is either not found or is of an incorrect type. */
19729 static const char *
19730 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19732 struct attribute
*attr
;
19733 const char *str
= NULL
;
19735 attr
= dwarf2_attr (die
, name
, cu
);
19739 str
= attr
->as_string ();
19740 if (str
== nullptr)
19741 complaint (_("string type expected for attribute %s for "
19742 "DIE at %s in module %s"),
19743 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
19744 objfile_name (cu
->per_objfile
->objfile
));
19750 /* Return the dwo name or NULL if not present. If present, it is in either
19751 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19752 static const char *
19753 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19755 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
19756 if (dwo_name
== nullptr)
19757 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
19761 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19762 and holds a non-zero value. This function should only be used for
19763 DW_FORM_flag or DW_FORM_flag_present attributes. */
19766 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
19768 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
19770 return attr
!= nullptr && attr
->as_boolean ();
19774 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
19776 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19777 which value is non-zero. However, we have to be careful with
19778 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19779 (via dwarf2_flag_true_p) follows this attribute. So we may
19780 end up accidently finding a declaration attribute that belongs
19781 to a different DIE referenced by the specification attribute,
19782 even though the given DIE does not have a declaration attribute. */
19783 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
19784 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
19787 /* Return the die giving the specification for DIE, if there is
19788 one. *SPEC_CU is the CU containing DIE on input, and the CU
19789 containing the return value on output. If there is no
19790 specification, but there is an abstract origin, that is
19793 static struct die_info
*
19794 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
19796 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
19799 if (spec_attr
== NULL
)
19800 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
19802 if (spec_attr
== NULL
)
19805 return follow_die_ref (die
, spec_attr
, spec_cu
);
19808 /* A convenience function to find the proper .debug_line section for a CU. */
19810 static struct dwarf2_section_info
*
19811 get_debug_line_section (struct dwarf2_cu
*cu
)
19813 struct dwarf2_section_info
*section
;
19814 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19816 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19818 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19819 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
19820 else if (cu
->per_cu
->is_dwz
)
19822 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19824 section
= &dwz
->line
;
19827 section
= &per_objfile
->per_bfd
->line
;
19832 /* Read the statement program header starting at OFFSET in
19833 .debug_line, or .debug_line.dwo. Return a pointer
19834 to a struct line_header, allocated using xmalloc.
19835 Returns NULL if there is a problem reading the header, e.g., if it
19836 has a version we don't understand.
19838 NOTE: the strings in the include directory and file name tables of
19839 the returned object point into the dwarf line section buffer,
19840 and must not be freed. */
19842 static line_header_up
19843 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
,
19844 const char *comp_dir
)
19846 struct dwarf2_section_info
*section
;
19847 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19849 section
= get_debug_line_section (cu
);
19850 section
->read (per_objfile
->objfile
);
19851 if (section
->buffer
== NULL
)
19853 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19854 complaint (_("missing .debug_line.dwo section"));
19856 complaint (_("missing .debug_line section"));
19860 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
19861 per_objfile
, section
, &cu
->header
,
19865 /* Subroutine of dwarf_decode_lines to simplify it.
19866 Return the file name for the given file_entry.
19867 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19868 If space for the result is malloc'd, *NAME_HOLDER will be set.
19869 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19870 equivalent to CU_INFO. */
19872 static const char *
19873 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
19874 const file_and_directory
&cu_info
,
19875 std::string
&name_holder
)
19877 const char *include_name
= fe
.name
;
19878 const char *include_name_to_compare
= include_name
;
19880 const char *dir_name
= fe
.include_dir (lh
);
19882 std::string hold_compare
;
19883 if (!IS_ABSOLUTE_PATH (include_name
)
19884 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
19886 /* Avoid creating a duplicate name for CU_INFO.
19887 We do this by comparing INCLUDE_NAME and CU_INFO.
19888 Before we do the comparison, however, we need to account
19889 for DIR_NAME and COMP_DIR.
19890 First prepend dir_name (if non-NULL). If we still don't
19891 have an absolute path prepend comp_dir (if non-NULL).
19892 However, the directory we record in the include-file's
19893 psymtab does not contain COMP_DIR (to match the
19894 corresponding symtab(s)).
19899 bash$ gcc -g ./hello.c
19900 include_name = "hello.c"
19902 DW_AT_comp_dir = comp_dir = "/tmp"
19903 DW_AT_name = "./hello.c"
19907 if (dir_name
!= NULL
)
19909 name_holder
= path_join (dir_name
, include_name
);
19910 include_name
= name_holder
.c_str ();
19911 include_name_to_compare
= include_name
;
19913 if (!IS_ABSOLUTE_PATH (include_name
)
19914 && cu_info
.get_comp_dir () != nullptr)
19916 hold_compare
= path_join (cu_info
.get_comp_dir (), include_name
);
19917 include_name_to_compare
= hold_compare
.c_str ();
19921 std::string copied_name
;
19922 const char *cu_filename
= cu_info
.get_name ();
19923 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
19925 copied_name
= path_join (cu_info
.get_comp_dir (), cu_filename
);
19926 cu_filename
= copied_name
.c_str ();
19929 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
19931 return include_name
;
19934 /* State machine to track the state of the line number program. */
19936 class lnp_state_machine
19939 /* Initialize a machine state for the start of a line number
19941 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
19943 file_entry
*current_file ()
19945 /* lh->file_names is 0-based, but the file name numbers in the
19946 statement program are 1-based. */
19947 return m_line_header
->file_name_at (m_file
);
19950 /* Record the line in the state machine. END_SEQUENCE is true if
19951 we're processing the end of a sequence. */
19952 void record_line (bool end_sequence
);
19954 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19955 nop-out rest of the lines in this sequence. */
19956 void check_line_address (struct dwarf2_cu
*cu
,
19957 const gdb_byte
*line_ptr
,
19958 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
);
19960 void handle_set_discriminator (unsigned int discriminator
)
19962 m_discriminator
= discriminator
;
19963 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
19966 /* Handle DW_LNE_set_address. */
19967 void handle_set_address (CORE_ADDR baseaddr
, CORE_ADDR address
)
19970 address
+= baseaddr
;
19971 m_address
= gdbarch_adjust_dwarf2_line (m_gdbarch
, address
, false);
19974 /* Handle DW_LNS_advance_pc. */
19975 void handle_advance_pc (CORE_ADDR adjust
);
19977 /* Handle a special opcode. */
19978 void handle_special_opcode (unsigned char op_code
);
19980 /* Handle DW_LNS_advance_line. */
19981 void handle_advance_line (int line_delta
)
19983 advance_line (line_delta
);
19986 /* Handle DW_LNS_set_file. */
19987 void handle_set_file (file_name_index file
);
19989 /* Handle DW_LNS_negate_stmt. */
19990 void handle_negate_stmt ()
19992 m_flags
^= LEF_IS_STMT
;
19995 /* Handle DW_LNS_const_add_pc. */
19996 void handle_const_add_pc ();
19998 /* Handle DW_LNS_fixed_advance_pc. */
19999 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
20001 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
20005 /* Handle DW_LNS_copy. */
20006 void handle_copy ()
20008 record_line (false);
20009 m_discriminator
= 0;
20010 m_flags
&= ~LEF_PROLOGUE_END
;
20013 /* Handle DW_LNE_end_sequence. */
20014 void handle_end_sequence ()
20016 m_currently_recording_lines
= true;
20019 /* Handle DW_LNS_set_prologue_end. */
20020 void handle_set_prologue_end ()
20022 m_flags
|= LEF_PROLOGUE_END
;
20026 /* Advance the line by LINE_DELTA. */
20027 void advance_line (int line_delta
)
20029 m_line
+= line_delta
;
20031 if (line_delta
!= 0)
20032 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
20035 struct dwarf2_cu
*m_cu
;
20037 gdbarch
*m_gdbarch
;
20039 /* The line number header. */
20040 line_header
*m_line_header
;
20042 /* These are part of the standard DWARF line number state machine,
20043 and initialized according to the DWARF spec. */
20045 unsigned char m_op_index
= 0;
20046 /* The line table index of the current file. */
20047 file_name_index m_file
= 1;
20048 unsigned int m_line
= 1;
20050 /* These are initialized in the constructor. */
20052 CORE_ADDR m_address
;
20053 linetable_entry_flags m_flags
;
20054 unsigned int m_discriminator
= 0;
20056 /* Additional bits of state we need to track. */
20058 /* The last file that we called dwarf2_start_subfile for.
20059 This is only used for TLLs. */
20060 unsigned int m_last_file
= 0;
20061 /* The last file a line number was recorded for. */
20062 struct subfile
*m_last_subfile
= NULL
;
20064 /* The address of the last line entry. */
20065 CORE_ADDR m_last_address
;
20067 /* Set to true when a previous line at the same address (using
20068 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
20069 when a line entry at a new address (m_address different to
20070 m_last_address) is processed. */
20071 bool m_stmt_at_address
= false;
20073 /* When true, record the lines we decode. */
20074 bool m_currently_recording_lines
= true;
20076 /* The last line number that was recorded, used to coalesce
20077 consecutive entries for the same line. This can happen, for
20078 example, when discriminators are present. PR 17276. */
20079 unsigned int m_last_line
= 0;
20080 bool m_line_has_non_zero_discriminator
= false;
20084 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
20086 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
20087 / m_line_header
->maximum_ops_per_instruction
)
20088 * m_line_header
->minimum_instruction_length
);
20089 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
20090 m_op_index
= ((m_op_index
+ adjust
)
20091 % m_line_header
->maximum_ops_per_instruction
);
20095 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
20097 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
20098 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
20099 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
20100 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
20101 / m_line_header
->maximum_ops_per_instruction
)
20102 * m_line_header
->minimum_instruction_length
);
20103 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
20104 m_op_index
= ((m_op_index
+ adj_opcode_d
)
20105 % m_line_header
->maximum_ops_per_instruction
);
20107 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
20108 advance_line (line_delta
);
20109 record_line (false);
20110 m_discriminator
= 0;
20111 m_flags
&= ~LEF_PROLOGUE_END
;
20115 lnp_state_machine::handle_set_file (file_name_index file
)
20119 const file_entry
*fe
= current_file ();
20121 dwarf2_debug_line_missing_file_complaint ();
20124 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20125 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
20126 dwarf2_start_subfile (m_cu
, *fe
, *m_line_header
);
20131 lnp_state_machine::handle_const_add_pc ()
20134 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
20137 = (((m_op_index
+ adjust
)
20138 / m_line_header
->maximum_ops_per_instruction
)
20139 * m_line_header
->minimum_instruction_length
);
20141 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
20142 m_op_index
= ((m_op_index
+ adjust
)
20143 % m_line_header
->maximum_ops_per_instruction
);
20146 /* Return non-zero if we should add LINE to the line number table.
20147 LINE is the line to add, LAST_LINE is the last line that was added,
20148 LAST_SUBFILE is the subfile for LAST_LINE.
20149 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20150 had a non-zero discriminator.
20152 We have to be careful in the presence of discriminators.
20153 E.g., for this line:
20155 for (i = 0; i < 100000; i++);
20157 clang can emit four line number entries for that one line,
20158 each with a different discriminator.
20159 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20161 However, we want gdb to coalesce all four entries into one.
20162 Otherwise the user could stepi into the middle of the line and
20163 gdb would get confused about whether the pc really was in the
20164 middle of the line.
20166 Things are further complicated by the fact that two consecutive
20167 line number entries for the same line is a heuristic used by gcc
20168 to denote the end of the prologue. So we can't just discard duplicate
20169 entries, we have to be selective about it. The heuristic we use is
20170 that we only collapse consecutive entries for the same line if at least
20171 one of those entries has a non-zero discriminator. PR 17276.
20173 Note: Addresses in the line number state machine can never go backwards
20174 within one sequence, thus this coalescing is ok. */
20177 dwarf_record_line_p (struct dwarf2_cu
*cu
,
20178 unsigned int line
, unsigned int last_line
,
20179 int line_has_non_zero_discriminator
,
20180 struct subfile
*last_subfile
)
20182 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
20184 if (line
!= last_line
)
20186 /* Same line for the same file that we've seen already.
20187 As a last check, for pr 17276, only record the line if the line
20188 has never had a non-zero discriminator. */
20189 if (!line_has_non_zero_discriminator
)
20194 /* Use the CU's builder to record line number LINE beginning at
20195 address ADDRESS in the line table of subfile SUBFILE. */
20198 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20199 unsigned int line
, CORE_ADDR address
,
20200 linetable_entry_flags flags
,
20201 struct dwarf2_cu
*cu
)
20203 CORE_ADDR addr
= gdbarch_addr_bits_remove (gdbarch
, address
);
20205 if (dwarf_line_debug
)
20207 gdb_printf (gdb_stdlog
,
20208 "Recording line %u, file %s, address %s\n",
20209 line
, lbasename (subfile
->name
.c_str ()),
20210 paddress (gdbarch
, address
));
20214 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
20217 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20218 Mark the end of a set of line number records.
20219 The arguments are the same as for dwarf_record_line_1.
20220 If SUBFILE is NULL the request is ignored. */
20223 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20224 CORE_ADDR address
, struct dwarf2_cu
*cu
)
20226 if (subfile
== NULL
)
20229 if (dwarf_line_debug
)
20231 gdb_printf (gdb_stdlog
,
20232 "Finishing current line, file %s, address %s\n",
20233 lbasename (subfile
->name
.c_str ()),
20234 paddress (gdbarch
, address
));
20237 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
20241 lnp_state_machine::record_line (bool end_sequence
)
20243 if (dwarf_line_debug
)
20245 gdb_printf (gdb_stdlog
,
20246 "Processing actual line %u: file %u,"
20247 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20249 paddress (m_gdbarch
, m_address
),
20250 (m_flags
& LEF_IS_STMT
) != 0,
20251 (m_flags
& LEF_PROLOGUE_END
) != 0,
20253 (end_sequence
? "\t(end sequence)" : ""));
20256 file_entry
*fe
= current_file ();
20259 dwarf2_debug_line_missing_file_complaint ();
20260 /* For now we ignore lines not starting on an instruction boundary.
20261 But not when processing end_sequence for compatibility with the
20262 previous version of the code. */
20263 else if (m_op_index
== 0 || end_sequence
)
20265 /* When we switch files we insert an end maker in the first file,
20266 switch to the second file and add a new line entry. The
20267 problem is that the end marker inserted in the first file will
20268 discard any previous line entries at the same address. If the
20269 line entries in the first file are marked as is-stmt, while
20270 the new line in the second file is non-stmt, then this means
20271 the end marker will discard is-stmt lines so we can have a
20272 non-stmt line. This means that there are less addresses at
20273 which the user can insert a breakpoint.
20275 To improve this we track the last address in m_last_address,
20276 and whether we have seen an is-stmt at this address. Then
20277 when switching files, if we have seen a stmt at the current
20278 address, and we are switching to create a non-stmt line, then
20279 discard the new line. */
20281 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
20282 bool ignore_this_line
20283 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
20284 && ((m_flags
& LEF_IS_STMT
) == 0)
20285 && m_stmt_at_address
)
20286 || (!end_sequence
&& m_line
== 0));
20288 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
20290 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
20291 m_currently_recording_lines
? m_cu
: nullptr);
20294 if (!end_sequence
&& !ignore_this_line
)
20296 linetable_entry_flags lte_flags
= m_flags
;
20297 if (producer_is_codewarrior (m_cu
))
20298 lte_flags
|= LEF_IS_STMT
;
20300 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
20301 m_line_has_non_zero_discriminator
,
20304 buildsym_compunit
*builder
= m_cu
->get_builder ();
20305 dwarf_record_line_1 (m_gdbarch
,
20306 builder
->get_current_subfile (),
20307 m_line
, m_address
, lte_flags
,
20308 m_currently_recording_lines
? m_cu
: nullptr);
20310 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20311 m_last_line
= m_line
;
20315 /* Track whether we have seen any IS_STMT true at m_address in case we
20316 have multiple line table entries all at m_address. */
20317 if (m_last_address
!= m_address
)
20319 m_stmt_at_address
= false;
20320 m_last_address
= m_address
;
20322 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
20325 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
20329 m_line_header (lh
),
20330 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as
20331 if there was a line entry for it so that the backend has a
20332 chance to adjust it and also record it in case it needs it.
20333 This is currently used by MIPS code,
20334 cf. `mips_adjust_dwarf2_line'. */
20335 m_address (gdbarch_adjust_dwarf2_line (arch
, 0, 0)),
20336 m_flags (lh
->default_is_stmt
? LEF_IS_STMT
: (linetable_entry_flags
) 0),
20337 m_last_address (m_address
)
20342 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
20343 const gdb_byte
*line_ptr
,
20344 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
)
20346 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20347 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20348 located at 0x0. In this case, additionally check that if
20349 ADDRESS < UNRELOCATED_LOWPC. */
20351 if ((address
== 0 && address
< unrelocated_lowpc
)
20352 || address
== (CORE_ADDR
) -1)
20354 /* This line table is for a function which has been
20355 GCd by the linker. Ignore it. PR gdb/12528 */
20357 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20358 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
20360 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20361 line_offset
, objfile_name (objfile
));
20362 m_currently_recording_lines
= false;
20363 /* Note: m_currently_recording_lines is left as false until we see
20364 DW_LNE_end_sequence. */
20368 /* Subroutine of dwarf_decode_lines to simplify it.
20369 Process the line number information in LH. */
20372 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20375 const gdb_byte
*line_ptr
, *extended_end
;
20376 const gdb_byte
*line_end
;
20377 unsigned int bytes_read
, extended_len
;
20378 unsigned char op_code
, extended_op
;
20379 CORE_ADDR baseaddr
;
20380 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20381 bfd
*abfd
= objfile
->obfd
.get ();
20382 struct gdbarch
*gdbarch
= objfile
->arch ();
20384 baseaddr
= objfile
->text_section_offset ();
20386 line_ptr
= lh
->statement_program_start
;
20387 line_end
= lh
->statement_program_end
;
20389 /* Read the statement sequences until there's nothing left. */
20390 while (line_ptr
< line_end
)
20392 /* The DWARF line number program state machine. Reset the state
20393 machine at the start of each sequence. */
20394 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
20395 bool end_sequence
= false;
20397 /* Start a subfile for the current file of the state
20399 const file_entry
*fe
= state_machine
.current_file ();
20402 dwarf2_start_subfile (cu
, *fe
, *lh
);
20404 /* Decode the table. */
20405 while (line_ptr
< line_end
&& !end_sequence
)
20407 op_code
= read_1_byte (abfd
, line_ptr
);
20410 if (op_code
>= lh
->opcode_base
)
20412 /* Special opcode. */
20413 state_machine
.handle_special_opcode (op_code
);
20415 else switch (op_code
)
20417 case DW_LNS_extended_op
:
20418 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
20420 line_ptr
+= bytes_read
;
20421 extended_end
= line_ptr
+ extended_len
;
20422 extended_op
= read_1_byte (abfd
, line_ptr
);
20424 if (DW_LNE_lo_user
<= extended_op
20425 && extended_op
<= DW_LNE_hi_user
)
20427 /* Vendor extension, ignore. */
20428 line_ptr
= extended_end
;
20431 switch (extended_op
)
20433 case DW_LNE_end_sequence
:
20434 state_machine
.handle_end_sequence ();
20435 end_sequence
= true;
20437 case DW_LNE_set_address
:
20440 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
20441 line_ptr
+= bytes_read
;
20443 state_machine
.check_line_address (cu
, line_ptr
,
20444 lowpc
- baseaddr
, address
);
20445 state_machine
.handle_set_address (baseaddr
, address
);
20448 case DW_LNE_define_file
:
20450 const char *cur_file
;
20451 unsigned int mod_time
, length
;
20454 cur_file
= read_direct_string (abfd
, line_ptr
,
20456 line_ptr
+= bytes_read
;
20457 dindex
= (dir_index
)
20458 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20459 line_ptr
+= bytes_read
;
20461 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20462 line_ptr
+= bytes_read
;
20464 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20465 line_ptr
+= bytes_read
;
20466 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
20469 case DW_LNE_set_discriminator
:
20471 /* The discriminator is not interesting to the
20472 debugger; just ignore it. We still need to
20473 check its value though:
20474 if there are consecutive entries for the same
20475 (non-prologue) line we want to coalesce them.
20478 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20479 line_ptr
+= bytes_read
;
20481 state_machine
.handle_set_discriminator (discr
);
20485 complaint (_("mangled .debug_line section"));
20488 /* Make sure that we parsed the extended op correctly. If e.g.
20489 we expected a different address size than the producer used,
20490 we may have read the wrong number of bytes. */
20491 if (line_ptr
!= extended_end
)
20493 complaint (_("mangled .debug_line section"));
20498 state_machine
.handle_copy ();
20500 case DW_LNS_advance_pc
:
20503 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20504 line_ptr
+= bytes_read
;
20506 state_machine
.handle_advance_pc (adjust
);
20509 case DW_LNS_advance_line
:
20512 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
20513 line_ptr
+= bytes_read
;
20515 state_machine
.handle_advance_line (line_delta
);
20518 case DW_LNS_set_file
:
20520 file_name_index file
20521 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
20523 line_ptr
+= bytes_read
;
20525 state_machine
.handle_set_file (file
);
20528 case DW_LNS_set_column
:
20529 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20530 line_ptr
+= bytes_read
;
20532 case DW_LNS_negate_stmt
:
20533 state_machine
.handle_negate_stmt ();
20535 case DW_LNS_set_basic_block
:
20537 /* Add to the address register of the state machine the
20538 address increment value corresponding to special opcode
20539 255. I.e., this value is scaled by the minimum
20540 instruction length since special opcode 255 would have
20541 scaled the increment. */
20542 case DW_LNS_const_add_pc
:
20543 state_machine
.handle_const_add_pc ();
20545 case DW_LNS_fixed_advance_pc
:
20547 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
20550 state_machine
.handle_fixed_advance_pc (addr_adj
);
20553 case DW_LNS_set_prologue_end
:
20554 state_machine
.handle_set_prologue_end ();
20558 /* Unknown standard opcode, ignore it. */
20561 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
20563 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20564 line_ptr
+= bytes_read
;
20571 dwarf2_debug_line_missing_end_sequence_complaint ();
20573 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20574 in which case we still finish recording the last line). */
20575 state_machine
.record_line (true);
20579 /* Decode the Line Number Program (LNP) for the given line_header
20580 structure and CU. The actual information extracted and the type
20581 of structures created from the LNP depends on the value of PST.
20583 FND holds the CU file name and directory, if known.
20584 It is used for relative paths in the line table.
20586 NOTE: It is important that psymtabs have the same file name (via
20587 strcmp) as the corresponding symtab. Since the directory is not
20588 used in the name of the symtab we don't use it in the name of the
20589 psymtabs we create. E.g. expand_line_sal requires this when
20590 finding psymtabs to expand. A good testcase for this is
20593 LOWPC is the lowest address in CU (or 0 if not known).
20595 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20596 for its PC<->lines mapping information. Otherwise only the filename
20597 table is read in. */
20600 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20601 CORE_ADDR lowpc
, int decode_mapping
)
20603 if (decode_mapping
)
20604 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
20606 /* Make sure a symtab is created for every file, even files
20607 which contain only variables (i.e. no code with associated
20609 buildsym_compunit
*builder
= cu
->get_builder ();
20610 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
20612 for (auto &fe
: lh
->file_names ())
20614 dwarf2_start_subfile (cu
, fe
, *lh
);
20615 subfile
*sf
= builder
->get_current_subfile ();
20617 if (sf
->symtab
== nullptr)
20618 sf
->symtab
= allocate_symtab (cust
, sf
->name
.c_str (),
20619 sf
->name_for_id
.c_str ());
20621 fe
.symtab
= sf
->symtab
;
20625 /* Start a subfile for DWARF. FILENAME is the name of the file and
20626 DIRNAME the name of the source directory which contains FILENAME
20627 or NULL if not known.
20628 This routine tries to keep line numbers from identical absolute and
20629 relative file names in a common subfile.
20631 Using the `list' example from the GDB testsuite, which resides in
20632 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20633 of /srcdir/list0.c yields the following debugging information for list0.c:
20635 DW_AT_name: /srcdir/list0.c
20636 DW_AT_comp_dir: /compdir
20637 files.files[0].name: list0.h
20638 files.files[0].dir: /srcdir
20639 files.files[1].name: list0.c
20640 files.files[1].dir: /srcdir
20642 The line number information for list0.c has to end up in a single
20643 subfile, so that `break /srcdir/list0.c:1' works as expected.
20644 start_subfile will ensure that this happens provided that we pass the
20645 concatenation of files.files[1].dir and files.files[1].name as the
20649 dwarf2_start_subfile (dwarf2_cu
*cu
, const file_entry
&fe
,
20650 const line_header
&lh
)
20652 std::string filename_holder
;
20653 const char *filename
= fe
.name
;
20654 const char *dirname
= lh
.include_dir_at (fe
.d_index
);
20656 /* In order not to lose the line information directory,
20657 we concatenate it to the filename when it makes sense.
20658 Note that the Dwarf3 standard says (speaking of filenames in line
20659 information): ``The directory index is ignored for file names
20660 that represent full path names''. Thus ignoring dirname in the
20661 `else' branch below isn't an issue. */
20663 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
20665 filename_holder
= path_join (dirname
, filename
);
20666 filename
= filename_holder
.c_str ();
20669 std::string filename_for_id
= lh
.file_file_name (fe
);
20670 cu
->get_builder ()->start_subfile (filename
, filename_for_id
.c_str ());
20674 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
20675 struct dwarf2_cu
*cu
)
20677 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20678 struct comp_unit_head
*cu_header
= &cu
->header
;
20680 /* NOTE drow/2003-01-30: There used to be a comment and some special
20681 code here to turn a symbol with DW_AT_external and a
20682 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20683 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20684 with some versions of binutils) where shared libraries could have
20685 relocations against symbols in their debug information - the
20686 minimal symbol would have the right address, but the debug info
20687 would not. It's no longer necessary, because we will explicitly
20688 apply relocations when we read in the debug information now. */
20690 /* A DW_AT_location attribute with no contents indicates that a
20691 variable has been optimized away. */
20692 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
20694 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20698 /* Handle one degenerate form of location expression specially, to
20699 preserve GDB's previous behavior when section offsets are
20700 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20701 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20703 if (attr
->form_is_block ())
20705 struct dwarf_block
*block
= attr
->as_block ();
20707 if ((block
->data
[0] == DW_OP_addr
20708 && block
->size
== 1 + cu_header
->addr_size
)
20709 || ((block
->data
[0] == DW_OP_GNU_addr_index
20710 || block
->data
[0] == DW_OP_addrx
)
20712 == 1 + leb128_size (&block
->data
[1]))))
20714 unsigned int dummy
;
20716 if (block
->data
[0] == DW_OP_addr
)
20717 sym
->set_value_address
20718 (cu
->header
.read_address (objfile
->obfd
.get (), block
->data
+ 1,
20721 sym
->set_value_address
20722 (read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
));
20723 sym
->set_aclass_index (LOC_STATIC
);
20724 fixup_symbol_section (sym
, objfile
);
20725 sym
->set_value_address
20726 (sym
->value_address ()
20727 + objfile
->section_offsets
[sym
->section_index ()]);
20732 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20733 expression evaluator, and use LOC_COMPUTED only when necessary
20734 (i.e. when the value of a register or memory location is
20735 referenced, or a thread-local block, etc.). Then again, it might
20736 not be worthwhile. I'm assuming that it isn't unless performance
20737 or memory numbers show me otherwise. */
20739 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
20741 if (SYMBOL_COMPUTED_OPS (sym
)->location_has_loclist
)
20742 cu
->has_loclist
= true;
20745 /* Given a pointer to a DWARF information entry, figure out if we need
20746 to make a symbol table entry for it, and if so, create a new entry
20747 and return a pointer to it.
20748 If TYPE is NULL, determine symbol type from the die, otherwise
20749 used the passed type.
20750 If SPACE is not NULL, use it to hold the new symbol. If it is
20751 NULL, allocate a new symbol on the objfile's obstack. */
20753 static struct symbol
*
20754 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
20755 struct symbol
*space
)
20757 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20758 struct objfile
*objfile
= per_objfile
->objfile
;
20759 struct gdbarch
*gdbarch
= objfile
->arch ();
20760 struct symbol
*sym
= NULL
;
20762 struct attribute
*attr
= NULL
;
20763 struct attribute
*attr2
= NULL
;
20764 CORE_ADDR baseaddr
;
20765 struct pending
**list_to_add
= NULL
;
20767 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
20769 baseaddr
= objfile
->text_section_offset ();
20771 name
= dwarf2_name (die
, cu
);
20772 if (name
== nullptr && (die
->tag
== DW_TAG_subprogram
20773 || die
->tag
== DW_TAG_inlined_subroutine
20774 || die
->tag
== DW_TAG_entry_point
))
20775 name
= dw2_linkage_name (die
, cu
);
20779 int suppress_add
= 0;
20784 sym
= new (&objfile
->objfile_obstack
) symbol
;
20785 OBJSTAT (objfile
, n_syms
++);
20787 /* Cache this symbol's name and the name's demangled form (if any). */
20788 sym
->set_language (cu
->lang (), &objfile
->objfile_obstack
);
20789 /* Fortran does not have mangling standard and the mangling does differ
20790 between gfortran, iFort etc. */
20791 const char *physname
20792 = (cu
->lang () == language_fortran
20793 ? dwarf2_full_name (name
, die
, cu
)
20794 : dwarf2_physname (name
, die
, cu
));
20795 const char *linkagename
= dw2_linkage_name (die
, cu
);
20797 if (linkagename
== nullptr || cu
->lang () == language_ada
)
20798 sym
->set_linkage_name (physname
);
20801 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
20802 sym
->set_linkage_name (linkagename
);
20805 /* Handle DW_AT_artificial. */
20806 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
20807 if (attr
!= nullptr)
20808 sym
->set_is_artificial (attr
->as_boolean ());
20810 /* Default assumptions.
20811 Use the passed type or decode it from the die. */
20812 sym
->set_domain (VAR_DOMAIN
);
20813 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20815 sym
->set_type (type
);
20817 sym
->set_type (die_type (die
, cu
));
20818 attr
= dwarf2_attr (die
,
20819 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
20821 if (attr
!= nullptr)
20822 sym
->set_line (attr
->constant_value (0));
20824 attr
= dwarf2_attr (die
,
20825 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
20827 if (attr
!= nullptr && attr
->is_nonnegative ())
20829 file_name_index file_index
20830 = (file_name_index
) attr
->as_nonnegative ();
20831 struct file_entry
*fe
;
20833 if (cu
->line_header
!= NULL
)
20834 fe
= cu
->line_header
->file_name_at (file_index
);
20839 complaint (_("file index out of range"));
20841 sym
->set_symtab (fe
->symtab
);
20847 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
20848 if (attr
!= nullptr)
20852 addr
= attr
->as_address ();
20853 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ baseaddr
);
20854 sym
->set_section_index (SECT_OFF_TEXT (objfile
));
20855 sym
->set_value_address (addr
);
20856 sym
->set_aclass_index (LOC_LABEL
);
20859 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20860 sym
->set_type (objfile_type (objfile
)->builtin_core_addr
);
20861 sym
->set_domain (LABEL_DOMAIN
);
20862 add_symbol_to_list (sym
, cu
->list_in_scope
);
20864 case DW_TAG_subprogram
:
20865 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20867 sym
->set_aclass_index (LOC_BLOCK
);
20868 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20869 if ((attr2
!= nullptr && attr2
->as_boolean ())
20870 || cu
->lang () == language_ada
20871 || cu
->lang () == language_fortran
)
20873 /* Subprograms marked external are stored as a global symbol.
20874 Ada and Fortran subprograms, whether marked external or
20875 not, are always stored as a global symbol, because we want
20876 to be able to access them globally. For instance, we want
20877 to be able to break on a nested subprogram without having
20878 to specify the context. */
20879 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20883 list_to_add
= cu
->list_in_scope
;
20886 case DW_TAG_inlined_subroutine
:
20887 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20889 sym
->set_aclass_index (LOC_BLOCK
);
20890 sym
->set_is_inlined (1);
20891 list_to_add
= cu
->list_in_scope
;
20893 case DW_TAG_template_value_param
:
20895 /* Fall through. */
20896 case DW_TAG_constant
:
20897 case DW_TAG_variable
:
20898 case DW_TAG_member
:
20899 /* Compilation with minimal debug info may result in
20900 variables with missing type entries. Change the
20901 misleading `void' type to something sensible. */
20902 if (sym
->type ()->code () == TYPE_CODE_VOID
)
20903 sym
->set_type (objfile_type (objfile
)->builtin_int
);
20905 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20906 /* In the case of DW_TAG_member, we should only be called for
20907 static const members. */
20908 if (die
->tag
== DW_TAG_member
)
20910 /* dwarf2_add_field uses die_is_declaration,
20911 so we do the same. */
20912 gdb_assert (die_is_declaration (die
, cu
));
20915 if (attr
!= nullptr)
20917 dwarf2_const_value (attr
, sym
, cu
);
20918 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20921 if (attr2
!= nullptr && attr2
->as_boolean ())
20922 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20924 list_to_add
= cu
->list_in_scope
;
20928 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20929 if (attr
!= nullptr)
20931 var_decode_location (attr
, sym
, cu
);
20932 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20934 /* Fortran explicitly imports any global symbols to the local
20935 scope by DW_TAG_common_block. */
20936 if (cu
->lang () == language_fortran
&& die
->parent
20937 && die
->parent
->tag
== DW_TAG_common_block
)
20940 if (sym
->aclass () == LOC_STATIC
20941 && sym
->value_address () == 0
20942 && !per_objfile
->per_bfd
->has_section_at_zero
)
20944 /* When a static variable is eliminated by the linker,
20945 the corresponding debug information is not stripped
20946 out, but the variable address is set to null;
20947 do not add such variables into symbol table. */
20949 else if (attr2
!= nullptr && attr2
->as_boolean ())
20951 if (sym
->aclass () == LOC_STATIC
20952 && (objfile
->flags
& OBJF_MAINLINE
) == 0
20953 && per_objfile
->per_bfd
->can_copy
)
20955 /* A global static variable might be subject to
20956 copy relocation. We first check for a local
20957 minsym, though, because maybe the symbol was
20958 marked hidden, in which case this would not
20960 bound_minimal_symbol found
20961 = (lookup_minimal_symbol_linkage
20962 (sym
->linkage_name (), objfile
));
20963 if (found
.minsym
!= nullptr)
20964 sym
->maybe_copied
= 1;
20967 /* A variable with DW_AT_external is never static,
20968 but it may be block-scoped. */
20970 = ((cu
->list_in_scope
20971 == cu
->get_builder ()->get_file_symbols ())
20972 ? cu
->get_builder ()->get_global_symbols ()
20973 : cu
->list_in_scope
);
20976 list_to_add
= cu
->list_in_scope
;
20980 /* We do not know the address of this symbol.
20981 If it is an external symbol and we have type information
20982 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20983 The address of the variable will then be determined from
20984 the minimal symbol table whenever the variable is
20986 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20988 /* Fortran explicitly imports any global symbols to the local
20989 scope by DW_TAG_common_block. */
20990 if (cu
->lang () == language_fortran
&& die
->parent
20991 && die
->parent
->tag
== DW_TAG_common_block
)
20993 /* SYMBOL_CLASS doesn't matter here because
20994 read_common_block is going to reset it. */
20996 list_to_add
= cu
->list_in_scope
;
20998 else if (attr2
!= nullptr && attr2
->as_boolean ()
20999 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
21001 /* A variable with DW_AT_external is never static, but it
21002 may be block-scoped. */
21004 = ((cu
->list_in_scope
21005 == cu
->get_builder ()->get_file_symbols ())
21006 ? cu
->get_builder ()->get_global_symbols ()
21007 : cu
->list_in_scope
);
21009 sym
->set_aclass_index (LOC_UNRESOLVED
);
21011 else if (!die_is_declaration (die
, cu
))
21013 /* Use the default LOC_OPTIMIZED_OUT class. */
21014 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
21016 list_to_add
= cu
->list_in_scope
;
21020 case DW_TAG_formal_parameter
:
21022 /* If we are inside a function, mark this as an argument. If
21023 not, we might be looking at an argument to an inlined function
21024 when we do not have enough information to show inlined frames;
21025 pretend it's a local variable in that case so that the user can
21027 struct context_stack
*curr
21028 = cu
->get_builder ()->get_current_context_stack ();
21029 if (curr
!= nullptr && curr
->name
!= nullptr)
21030 sym
->set_is_argument (1);
21031 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
21032 if (attr
!= nullptr)
21034 var_decode_location (attr
, sym
, cu
);
21036 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
21037 if (attr
!= nullptr)
21039 dwarf2_const_value (attr
, sym
, cu
);
21042 list_to_add
= cu
->list_in_scope
;
21045 case DW_TAG_unspecified_parameters
:
21046 /* From varargs functions; gdb doesn't seem to have any
21047 interest in this information, so just ignore it for now.
21050 case DW_TAG_template_type_param
:
21052 /* Fall through. */
21053 case DW_TAG_class_type
:
21054 case DW_TAG_interface_type
:
21055 case DW_TAG_structure_type
:
21056 case DW_TAG_union_type
:
21057 case DW_TAG_set_type
:
21058 case DW_TAG_enumeration_type
:
21059 case DW_TAG_namelist
:
21060 if (die
->tag
== DW_TAG_namelist
)
21062 sym
->set_aclass_index (LOC_STATIC
);
21063 sym
->set_domain (VAR_DOMAIN
);
21067 sym
->set_aclass_index (LOC_TYPEDEF
);
21068 sym
->set_domain (STRUCT_DOMAIN
);
21071 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21072 really ever be static objects: otherwise, if you try
21073 to, say, break of a class's method and you're in a file
21074 which doesn't mention that class, it won't work unless
21075 the check for all static symbols in lookup_symbol_aux
21076 saves you. See the OtherFileClass tests in
21077 gdb.c++/namespace.exp. */
21081 buildsym_compunit
*builder
= cu
->get_builder ();
21083 = (cu
->list_in_scope
== builder
->get_file_symbols ()
21084 && cu
->lang () == language_cplus
21085 ? builder
->get_global_symbols ()
21086 : cu
->list_in_scope
);
21088 /* The semantics of C++ state that "struct foo {
21089 ... }" also defines a typedef for "foo". */
21090 if (cu
->lang () == language_cplus
21091 || cu
->lang () == language_ada
21092 || cu
->lang () == language_d
21093 || cu
->lang () == language_rust
)
21095 /* The symbol's name is already allocated along
21096 with this objfile, so we don't need to
21097 duplicate it for the type. */
21098 if (sym
->type ()->name () == 0)
21099 sym
->type ()->set_name (sym
->search_name ());
21104 case DW_TAG_typedef
:
21105 sym
->set_aclass_index (LOC_TYPEDEF
);
21106 sym
->set_domain (VAR_DOMAIN
);
21107 list_to_add
= cu
->list_in_scope
;
21109 case DW_TAG_array_type
:
21110 case DW_TAG_base_type
:
21111 case DW_TAG_subrange_type
:
21112 case DW_TAG_generic_subrange
:
21113 case DW_TAG_unspecified_type
:
21114 sym
->set_aclass_index (LOC_TYPEDEF
);
21115 sym
->set_domain (VAR_DOMAIN
);
21116 list_to_add
= cu
->list_in_scope
;
21118 case DW_TAG_enumerator
:
21119 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
21120 if (attr
!= nullptr)
21122 dwarf2_const_value (attr
, sym
, cu
);
21125 /* NOTE: carlton/2003-11-10: See comment above in the
21126 DW_TAG_class_type, etc. block. */
21129 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
21130 && cu
->lang () == language_cplus
21131 ? cu
->get_builder ()->get_global_symbols ()
21132 : cu
->list_in_scope
);
21135 case DW_TAG_imported_declaration
:
21136 case DW_TAG_namespace
:
21137 sym
->set_aclass_index (LOC_TYPEDEF
);
21138 list_to_add
= cu
->get_builder ()->get_global_symbols ();
21140 case DW_TAG_module
:
21141 sym
->set_aclass_index (LOC_TYPEDEF
);
21142 sym
->set_domain (MODULE_DOMAIN
);
21143 list_to_add
= cu
->get_builder ()->get_global_symbols ();
21145 case DW_TAG_common_block
:
21146 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
21147 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
21148 add_symbol_to_list (sym
, cu
->list_in_scope
);
21151 /* Not a tag we recognize. Hopefully we aren't processing
21152 trash data, but since we must specifically ignore things
21153 we don't recognize, there is nothing else we should do at
21155 complaint (_("unsupported tag: '%s'"),
21156 dwarf_tag_name (die
->tag
));
21162 sym
->hash_next
= objfile
->template_symbols
;
21163 objfile
->template_symbols
= sym
;
21164 list_to_add
= NULL
;
21167 if (list_to_add
!= NULL
)
21168 add_symbol_to_list (sym
, list_to_add
);
21170 /* For the benefit of old versions of GCC, check for anonymous
21171 namespaces based on the demangled name. */
21172 if (!cu
->processing_has_namespace_info
21173 && cu
->lang () == language_cplus
)
21174 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
21179 /* Given an attr with a DW_FORM_dataN value in host byte order,
21180 zero-extend it as appropriate for the symbol's type. The DWARF
21181 standard (v4) is not entirely clear about the meaning of using
21182 DW_FORM_dataN for a constant with a signed type, where the type is
21183 wider than the data. The conclusion of a discussion on the DWARF
21184 list was that this is unspecified. We choose to always zero-extend
21185 because that is the interpretation long in use by GCC. */
21188 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
21189 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
21191 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21192 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
.get ()) ?
21193 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
21194 LONGEST l
= attr
->constant_value (0);
21196 if (bits
< sizeof (*value
) * 8)
21198 l
&= ((LONGEST
) 1 << bits
) - 1;
21201 else if (bits
== sizeof (*value
) * 8)
21205 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
21206 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
21213 /* Read a constant value from an attribute. Either set *VALUE, or if
21214 the value does not fit in *VALUE, set *BYTES - either already
21215 allocated on the objfile obstack, or newly allocated on OBSTACK,
21216 or, set *BATON, if we translated the constant to a location
21220 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
21221 const char *name
, struct obstack
*obstack
,
21222 struct dwarf2_cu
*cu
,
21223 LONGEST
*value
, const gdb_byte
**bytes
,
21224 struct dwarf2_locexpr_baton
**baton
)
21226 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21227 struct objfile
*objfile
= per_objfile
->objfile
;
21228 struct comp_unit_head
*cu_header
= &cu
->header
;
21229 struct dwarf_block
*blk
;
21230 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
.get ()) ?
21231 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
21237 switch (attr
->form
)
21240 case DW_FORM_addrx
:
21241 case DW_FORM_GNU_addr_index
:
21245 if (type
->length () != cu_header
->addr_size
)
21246 dwarf2_const_value_length_mismatch_complaint (name
,
21247 cu_header
->addr_size
,
21249 /* Symbols of this form are reasonably rare, so we just
21250 piggyback on the existing location code rather than writing
21251 a new implementation of symbol_computed_ops. */
21252 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
21253 (*baton
)->per_objfile
= per_objfile
;
21254 (*baton
)->per_cu
= cu
->per_cu
;
21255 gdb_assert ((*baton
)->per_cu
);
21257 (*baton
)->size
= 2 + cu_header
->addr_size
;
21258 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
21259 (*baton
)->data
= data
;
21261 data
[0] = DW_OP_addr
;
21262 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
21263 byte_order
, attr
->as_address ());
21264 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
21267 case DW_FORM_string
:
21270 case DW_FORM_GNU_str_index
:
21271 case DW_FORM_GNU_strp_alt
:
21272 /* The string is already allocated on the objfile obstack, point
21274 *bytes
= (const gdb_byte
*) attr
->as_string ();
21276 case DW_FORM_block1
:
21277 case DW_FORM_block2
:
21278 case DW_FORM_block4
:
21279 case DW_FORM_block
:
21280 case DW_FORM_exprloc
:
21281 case DW_FORM_data16
:
21282 blk
= attr
->as_block ();
21283 if (type
->length () != blk
->size
)
21284 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
21286 *bytes
= blk
->data
;
21289 /* The DW_AT_const_value attributes are supposed to carry the
21290 symbol's value "represented as it would be on the target
21291 architecture." By the time we get here, it's already been
21292 converted to host endianness, so we just need to sign- or
21293 zero-extend it as appropriate. */
21294 case DW_FORM_data1
:
21295 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
21297 case DW_FORM_data2
:
21298 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
21300 case DW_FORM_data4
:
21301 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
21303 case DW_FORM_data8
:
21304 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
21307 case DW_FORM_sdata
:
21308 case DW_FORM_implicit_const
:
21309 *value
= attr
->as_signed ();
21312 case DW_FORM_udata
:
21313 *value
= attr
->as_unsigned ();
21317 complaint (_("unsupported const value attribute form: '%s'"),
21318 dwarf_form_name (attr
->form
));
21325 /* Copy constant value from an attribute to a symbol. */
21328 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
21329 struct dwarf2_cu
*cu
)
21331 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21333 const gdb_byte
*bytes
;
21334 struct dwarf2_locexpr_baton
*baton
;
21336 dwarf2_const_value_attr (attr
, sym
->type (),
21337 sym
->print_name (),
21338 &objfile
->objfile_obstack
, cu
,
21339 &value
, &bytes
, &baton
);
21343 SYMBOL_LOCATION_BATON (sym
) = baton
;
21344 sym
->set_aclass_index (dwarf2_locexpr_index
);
21346 else if (bytes
!= NULL
)
21348 sym
->set_value_bytes (bytes
);
21349 sym
->set_aclass_index (LOC_CONST_BYTES
);
21353 sym
->set_value_longest (value
);
21354 sym
->set_aclass_index (LOC_CONST
);
21358 /* Return the type of the die in question using its DW_AT_type attribute. */
21360 static struct type
*
21361 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21363 struct attribute
*type_attr
;
21365 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
21368 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21369 /* A missing DW_AT_type represents a void type. */
21370 return objfile_type (objfile
)->builtin_void
;
21373 return lookup_die_type (die
, type_attr
, cu
);
21376 /* True iff CU's producer generates GNAT Ada auxiliary information
21377 that allows to find parallel types through that information instead
21378 of having to do expensive parallel lookups by type name. */
21381 need_gnat_info (struct dwarf2_cu
*cu
)
21383 /* Assume that the Ada compiler was GNAT, which always produces
21384 the auxiliary information. */
21385 return (cu
->lang () == language_ada
);
21388 /* Return the auxiliary type of the die in question using its
21389 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21390 attribute is not present. */
21392 static struct type
*
21393 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21395 struct attribute
*type_attr
;
21397 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
21401 return lookup_die_type (die
, type_attr
, cu
);
21404 /* If DIE has a descriptive_type attribute, then set the TYPE's
21405 descriptive type accordingly. */
21408 set_descriptive_type (struct type
*type
, struct die_info
*die
,
21409 struct dwarf2_cu
*cu
)
21411 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
21413 if (descriptive_type
)
21415 ALLOCATE_GNAT_AUX_TYPE (type
);
21416 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
21420 /* Return the containing type of the die in question using its
21421 DW_AT_containing_type attribute. */
21423 static struct type
*
21424 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21426 struct attribute
*type_attr
;
21427 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21429 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
21431 error (_("Dwarf Error: Problem turning containing type into gdb type "
21432 "[in module %s]"), objfile_name (objfile
));
21434 return lookup_die_type (die
, type_attr
, cu
);
21437 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21439 static struct type
*
21440 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
21442 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21443 struct objfile
*objfile
= per_objfile
->objfile
;
21446 std::string message
21447 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21448 objfile_name (objfile
),
21449 sect_offset_str (cu
->header
.sect_off
),
21450 sect_offset_str (die
->sect_off
));
21451 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
21453 return init_type (objfile
, TYPE_CODE_ERROR
, 0, saved
);
21456 /* Look up the type of DIE in CU using its type attribute ATTR.
21457 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21458 DW_AT_containing_type.
21459 If there is no type substitute an error marker. */
21461 static struct type
*
21462 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
21463 struct dwarf2_cu
*cu
)
21465 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21466 struct objfile
*objfile
= per_objfile
->objfile
;
21467 struct type
*this_type
;
21469 gdb_assert (attr
->name
== DW_AT_type
21470 || attr
->name
== DW_AT_GNAT_descriptive_type
21471 || attr
->name
== DW_AT_containing_type
);
21473 /* First see if we have it cached. */
21475 if (attr
->form
== DW_FORM_GNU_ref_alt
)
21477 struct dwarf2_per_cu_data
*per_cu
;
21478 sect_offset sect_off
= attr
->get_ref_die_offset ();
21480 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
21481 per_objfile
->per_bfd
);
21482 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
21484 else if (attr
->form_is_ref ())
21486 sect_offset sect_off
= attr
->get_ref_die_offset ();
21488 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
21490 else if (attr
->form
== DW_FORM_ref_sig8
)
21492 ULONGEST signature
= attr
->as_signature ();
21494 return get_signatured_type (die
, signature
, cu
);
21498 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21499 " at %s [in module %s]"),
21500 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
21501 objfile_name (objfile
));
21502 return build_error_marker_type (cu
, die
);
21505 /* If not cached we need to read it in. */
21507 if (this_type
== NULL
)
21509 struct die_info
*type_die
= NULL
;
21510 struct dwarf2_cu
*type_cu
= cu
;
21512 if (attr
->form_is_ref ())
21513 type_die
= follow_die_ref (die
, attr
, &type_cu
);
21514 if (type_die
== NULL
)
21515 return build_error_marker_type (cu
, die
);
21516 /* If we find the type now, it's probably because the type came
21517 from an inter-CU reference and the type's CU got expanded before
21519 this_type
= read_type_die (type_die
, type_cu
);
21522 /* If we still don't have a type use an error marker. */
21524 if (this_type
== NULL
)
21525 return build_error_marker_type (cu
, die
);
21530 /* Return the type in DIE, CU.
21531 Returns NULL for invalid types.
21533 This first does a lookup in die_type_hash,
21534 and only reads the die in if necessary.
21536 NOTE: This can be called when reading in partial or full symbols. */
21538 static struct type
*
21539 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
21541 struct type
*this_type
;
21543 this_type
= get_die_type (die
, cu
);
21547 return read_type_die_1 (die
, cu
);
21550 /* Read the type in DIE, CU.
21551 Returns NULL for invalid types. */
21553 static struct type
*
21554 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
21556 struct type
*this_type
= NULL
;
21560 case DW_TAG_class_type
:
21561 case DW_TAG_interface_type
:
21562 case DW_TAG_structure_type
:
21563 case DW_TAG_union_type
:
21564 this_type
= read_structure_type (die
, cu
);
21566 case DW_TAG_enumeration_type
:
21567 this_type
= read_enumeration_type (die
, cu
);
21569 case DW_TAG_subprogram
:
21570 case DW_TAG_subroutine_type
:
21571 case DW_TAG_inlined_subroutine
:
21572 this_type
= read_subroutine_type (die
, cu
);
21574 case DW_TAG_array_type
:
21575 this_type
= read_array_type (die
, cu
);
21577 case DW_TAG_set_type
:
21578 this_type
= read_set_type (die
, cu
);
21580 case DW_TAG_pointer_type
:
21581 this_type
= read_tag_pointer_type (die
, cu
);
21583 case DW_TAG_ptr_to_member_type
:
21584 this_type
= read_tag_ptr_to_member_type (die
, cu
);
21586 case DW_TAG_reference_type
:
21587 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
21589 case DW_TAG_rvalue_reference_type
:
21590 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
21592 case DW_TAG_const_type
:
21593 this_type
= read_tag_const_type (die
, cu
);
21595 case DW_TAG_volatile_type
:
21596 this_type
= read_tag_volatile_type (die
, cu
);
21598 case DW_TAG_restrict_type
:
21599 this_type
= read_tag_restrict_type (die
, cu
);
21601 case DW_TAG_string_type
:
21602 this_type
= read_tag_string_type (die
, cu
);
21604 case DW_TAG_typedef
:
21605 this_type
= read_typedef (die
, cu
);
21607 case DW_TAG_generic_subrange
:
21608 case DW_TAG_subrange_type
:
21609 this_type
= read_subrange_type (die
, cu
);
21611 case DW_TAG_base_type
:
21612 this_type
= read_base_type (die
, cu
);
21614 case DW_TAG_unspecified_type
:
21615 this_type
= read_unspecified_type (die
, cu
);
21617 case DW_TAG_namespace
:
21618 this_type
= read_namespace_type (die
, cu
);
21620 case DW_TAG_module
:
21621 this_type
= read_module_type (die
, cu
);
21623 case DW_TAG_atomic_type
:
21624 this_type
= read_tag_atomic_type (die
, cu
);
21627 complaint (_("unexpected tag in read_type_die: '%s'"),
21628 dwarf_tag_name (die
->tag
));
21635 /* See if we can figure out if the class lives in a namespace. We do
21636 this by looking for a member function; its demangled name will
21637 contain namespace info, if there is any.
21638 Return the computed name or NULL.
21639 Space for the result is allocated on the objfile's obstack.
21640 This is the full-die version of guess_partial_die_structure_name.
21641 In this case we know DIE has no useful parent. */
21643 static const char *
21644 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21646 struct die_info
*spec_die
;
21647 struct dwarf2_cu
*spec_cu
;
21648 struct die_info
*child
;
21649 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21652 spec_die
= die_specification (die
, &spec_cu
);
21653 if (spec_die
!= NULL
)
21659 for (child
= die
->child
;
21661 child
= child
->sibling
)
21663 if (child
->tag
== DW_TAG_subprogram
)
21665 const char *linkage_name
= dw2_linkage_name (child
, cu
);
21667 if (linkage_name
!= NULL
)
21669 gdb::unique_xmalloc_ptr
<char> actual_name
21670 (cu
->language_defn
->class_name_from_physname (linkage_name
));
21671 const char *name
= NULL
;
21673 if (actual_name
!= NULL
)
21675 const char *die_name
= dwarf2_name (die
, cu
);
21677 if (die_name
!= NULL
21678 && strcmp (die_name
, actual_name
.get ()) != 0)
21680 /* Strip off the class name from the full name.
21681 We want the prefix. */
21682 int die_name_len
= strlen (die_name
);
21683 int actual_name_len
= strlen (actual_name
.get ());
21684 const char *ptr
= actual_name
.get ();
21686 /* Test for '::' as a sanity check. */
21687 if (actual_name_len
> die_name_len
+ 2
21688 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
21689 name
= obstack_strndup (
21690 &objfile
->per_bfd
->storage_obstack
,
21691 ptr
, actual_name_len
- die_name_len
- 2);
21702 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21703 prefix part in such case. See
21704 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21706 static const char *
21707 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21709 struct attribute
*attr
;
21712 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
21713 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
21716 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
21719 attr
= dw2_linkage_name_attr (die
, cu
);
21720 const char *attr_name
= attr
->as_string ();
21721 if (attr
== NULL
|| attr_name
== NULL
)
21724 /* dwarf2_name had to be already called. */
21725 gdb_assert (attr
->canonical_string_p ());
21727 /* Strip the base name, keep any leading namespaces/classes. */
21728 base
= strrchr (attr_name
, ':');
21729 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
21732 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21733 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
21735 &base
[-1] - attr_name
);
21738 /* Return the name of the namespace/class that DIE is defined within,
21739 or "" if we can't tell. The caller should not xfree the result.
21741 For example, if we're within the method foo() in the following
21751 then determine_prefix on foo's die will return "N::C". */
21753 static const char *
21754 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21756 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21757 struct die_info
*parent
, *spec_die
;
21758 struct dwarf2_cu
*spec_cu
;
21759 struct type
*parent_type
;
21760 const char *retval
;
21762 if (cu
->lang () != language_cplus
21763 && cu
->lang () != language_fortran
21764 && cu
->lang () != language_d
21765 && cu
->lang () != language_rust
)
21768 retval
= anonymous_struct_prefix (die
, cu
);
21772 /* We have to be careful in the presence of DW_AT_specification.
21773 For example, with GCC 3.4, given the code
21777 // Definition of N::foo.
21781 then we'll have a tree of DIEs like this:
21783 1: DW_TAG_compile_unit
21784 2: DW_TAG_namespace // N
21785 3: DW_TAG_subprogram // declaration of N::foo
21786 4: DW_TAG_subprogram // definition of N::foo
21787 DW_AT_specification // refers to die #3
21789 Thus, when processing die #4, we have to pretend that we're in
21790 the context of its DW_AT_specification, namely the contex of die
21793 spec_die
= die_specification (die
, &spec_cu
);
21794 if (spec_die
== NULL
)
21795 parent
= die
->parent
;
21798 parent
= spec_die
->parent
;
21802 if (parent
== NULL
)
21804 else if (parent
->building_fullname
)
21807 const char *parent_name
;
21809 /* It has been seen on RealView 2.2 built binaries,
21810 DW_TAG_template_type_param types actually _defined_ as
21811 children of the parent class:
21814 template class <class Enum> Class{};
21815 Class<enum E> class_e;
21817 1: DW_TAG_class_type (Class)
21818 2: DW_TAG_enumeration_type (E)
21819 3: DW_TAG_enumerator (enum1:0)
21820 3: DW_TAG_enumerator (enum2:1)
21822 2: DW_TAG_template_type_param
21823 DW_AT_type DW_FORM_ref_udata (E)
21825 Besides being broken debug info, it can put GDB into an
21826 infinite loop. Consider:
21828 When we're building the full name for Class<E>, we'll start
21829 at Class, and go look over its template type parameters,
21830 finding E. We'll then try to build the full name of E, and
21831 reach here. We're now trying to build the full name of E,
21832 and look over the parent DIE for containing scope. In the
21833 broken case, if we followed the parent DIE of E, we'd again
21834 find Class, and once again go look at its template type
21835 arguments, etc., etc. Simply don't consider such parent die
21836 as source-level parent of this die (it can't be, the language
21837 doesn't allow it), and break the loop here. */
21838 name
= dwarf2_name (die
, cu
);
21839 parent_name
= dwarf2_name (parent
, cu
);
21840 complaint (_("template param type '%s' defined within parent '%s'"),
21841 name
? name
: "<unknown>",
21842 parent_name
? parent_name
: "<unknown>");
21846 switch (parent
->tag
)
21848 case DW_TAG_namespace
:
21849 parent_type
= read_type_die (parent
, cu
);
21850 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21851 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21852 Work around this problem here. */
21853 if (cu
->lang () == language_cplus
21854 && strcmp (parent_type
->name (), "::") == 0)
21856 /* We give a name to even anonymous namespaces. */
21857 return parent_type
->name ();
21858 case DW_TAG_class_type
:
21859 case DW_TAG_interface_type
:
21860 case DW_TAG_structure_type
:
21861 case DW_TAG_union_type
:
21862 case DW_TAG_module
:
21863 parent_type
= read_type_die (parent
, cu
);
21864 if (parent_type
->name () != NULL
)
21865 return parent_type
->name ();
21867 /* An anonymous structure is only allowed non-static data
21868 members; no typedefs, no member functions, et cetera.
21869 So it does not need a prefix. */
21871 case DW_TAG_compile_unit
:
21872 case DW_TAG_partial_unit
:
21873 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21874 if (cu
->lang () == language_cplus
21875 && !per_objfile
->per_bfd
->types
.empty ()
21876 && die
->child
!= NULL
21877 && (die
->tag
== DW_TAG_class_type
21878 || die
->tag
== DW_TAG_structure_type
21879 || die
->tag
== DW_TAG_union_type
))
21881 const char *name
= guess_full_die_structure_name (die
, cu
);
21886 case DW_TAG_subprogram
:
21887 /* Nested subroutines in Fortran get a prefix with the name
21888 of the parent's subroutine. */
21889 if (cu
->lang () == language_fortran
)
21891 if ((die
->tag
== DW_TAG_subprogram
)
21892 && (dwarf2_name (parent
, cu
) != NULL
))
21893 return dwarf2_name (parent
, cu
);
21896 case DW_TAG_enumeration_type
:
21897 parent_type
= read_type_die (parent
, cu
);
21898 if (parent_type
->is_declared_class ())
21900 if (parent_type
->name () != NULL
)
21901 return parent_type
->name ();
21904 /* Fall through. */
21906 return determine_prefix (parent
, cu
);
21910 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21911 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21912 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21913 an obconcat, otherwise allocate storage for the result. The CU argument is
21914 used to determine the language and hence, the appropriate separator. */
21916 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21919 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
21920 int physname
, struct dwarf2_cu
*cu
)
21922 const char *lead
= "";
21925 if (suffix
== NULL
|| suffix
[0] == '\0'
21926 || prefix
== NULL
|| prefix
[0] == '\0')
21928 else if (cu
->lang () == language_d
)
21930 /* For D, the 'main' function could be defined in any module, but it
21931 should never be prefixed. */
21932 if (strcmp (suffix
, "D main") == 0)
21940 else if (cu
->lang () == language_fortran
&& physname
)
21942 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21943 DW_AT_MIPS_linkage_name is preferred and used instead. */
21951 if (prefix
== NULL
)
21953 if (suffix
== NULL
)
21960 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
21962 strcpy (retval
, lead
);
21963 strcat (retval
, prefix
);
21964 strcat (retval
, sep
);
21965 strcat (retval
, suffix
);
21970 /* We have an obstack. */
21971 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
21975 /* Return a generic name for a DW_TAG_template_type_param or
21976 DW_TAG_template_value_param tag, missing a DW_AT_name attribute. We do this
21977 per parent, so each function/class/struct template will have their own set
21978 of template parameters named <unnnamed0>, <unnamed1>, ... where the
21979 enumeration starts at 0 and represents the position of the template tag in
21980 the list of unnamed template tags for this parent, counting both, type and
21983 static const char *
21984 unnamed_template_tag_name (die_info
*die
, dwarf2_cu
*cu
)
21986 if (die
->parent
== nullptr)
21989 /* Count the parent types unnamed template type and value children until, we
21990 arrive at our entry. */
21991 size_t nth_unnamed
= 0;
21993 die_info
*child
= die
->parent
->child
;
21994 while (child
!= die
)
21996 gdb_assert (child
!= nullptr);
21997 if (child
->tag
== DW_TAG_template_type_param
21998 || child
->tag
== DW_TAG_template_value_param
)
22000 if (dwarf2_attr (child
, DW_AT_name
, cu
) == nullptr)
22003 child
= child
->sibling
;
22006 const std::string name_str
= "<unnamed" + std::to_string (nth_unnamed
) + ">";
22007 return cu
->per_objfile
->objfile
->intern (name_str
.c_str ());
22010 /* Get name of a die, return NULL if not found. */
22012 static const char *
22013 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
22014 struct objfile
*objfile
)
22016 if (name
== nullptr)
22019 if (cu
->lang () == language_cplus
)
22021 gdb::unique_xmalloc_ptr
<char> canon_name
22022 = cp_canonicalize_string (name
);
22024 if (canon_name
!= nullptr)
22025 name
= objfile
->intern (canon_name
.get ());
22027 else if (cu
->lang () == language_c
)
22029 gdb::unique_xmalloc_ptr
<char> canon_name
22030 = c_canonicalize_name (name
);
22032 if (canon_name
!= nullptr)
22033 name
= objfile
->intern (canon_name
.get ());
22039 /* Get name of a die, return NULL if not found.
22040 Anonymous namespaces are converted to their magic string. */
22042 static const char *
22043 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
22045 struct attribute
*attr
;
22046 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
22048 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
22049 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
22050 if (attr_name
== nullptr
22051 && die
->tag
!= DW_TAG_namespace
22052 && die
->tag
!= DW_TAG_class_type
22053 && die
->tag
!= DW_TAG_interface_type
22054 && die
->tag
!= DW_TAG_structure_type
22055 && die
->tag
!= DW_TAG_namelist
22056 && die
->tag
!= DW_TAG_union_type
22057 && die
->tag
!= DW_TAG_template_type_param
22058 && die
->tag
!= DW_TAG_template_value_param
)
22063 /* A member's name should not be canonicalized. This is a bit
22064 of a hack, in that normally it should not be possible to run
22065 into this situation; however, the dw2-unusual-field-names.exp
22066 test creates custom DWARF that does. */
22067 case DW_TAG_member
:
22068 case DW_TAG_compile_unit
:
22069 case DW_TAG_partial_unit
:
22070 /* Compilation units have a DW_AT_name that is a filename, not
22071 a source language identifier. */
22072 case DW_TAG_enumeration_type
:
22073 case DW_TAG_enumerator
:
22074 /* These tags always have simple identifiers already; no need
22075 to canonicalize them. */
22078 case DW_TAG_namespace
:
22079 if (attr_name
!= nullptr)
22081 return CP_ANONYMOUS_NAMESPACE_STR
;
22083 /* DWARF does not actually require template tags to have a name. */
22084 case DW_TAG_template_type_param
:
22085 case DW_TAG_template_value_param
:
22086 if (attr_name
== nullptr)
22087 return unnamed_template_tag_name (die
, cu
);
22089 case DW_TAG_class_type
:
22090 case DW_TAG_interface_type
:
22091 case DW_TAG_structure_type
:
22092 case DW_TAG_union_type
:
22093 case DW_TAG_namelist
:
22094 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22095 structures or unions. These were of the form "._%d" in GCC 4.1,
22096 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22097 and GCC 4.4. We work around this problem by ignoring these. */
22098 if (attr_name
!= nullptr
22099 && (startswith (attr_name
, "._")
22100 || startswith (attr_name
, "<anonymous")))
22103 /* GCC might emit a nameless typedef that has a linkage name. See
22104 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22105 if (!attr
|| attr_name
== NULL
)
22107 attr
= dw2_linkage_name_attr (die
, cu
);
22108 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
22109 if (attr
== NULL
|| attr_name
== NULL
)
22112 /* Avoid demangling attr_name the second time on a second
22113 call for the same DIE. */
22114 if (!attr
->canonical_string_p ())
22116 gdb::unique_xmalloc_ptr
<char> demangled
22117 (gdb_demangle (attr_name
, DMGL_TYPES
));
22118 if (demangled
== nullptr)
22121 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
22122 attr_name
= attr
->as_string ();
22125 /* Strip any leading namespaces/classes, keep only the
22126 base name. DW_AT_name for named DIEs does not
22127 contain the prefixes. */
22128 const char *base
= strrchr (attr_name
, ':');
22129 if (base
&& base
> attr_name
&& base
[-1] == ':')
22140 if (!attr
->canonical_string_p ())
22141 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
22143 return attr
->as_string ();
22146 /* Return the die that this die in an extension of, or NULL if there
22147 is none. *EXT_CU is the CU containing DIE on input, and the CU
22148 containing the return value on output. */
22150 static struct die_info
*
22151 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
22153 struct attribute
*attr
;
22155 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
22159 return follow_die_ref (die
, attr
, ext_cu
);
22163 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
22167 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
22168 to_underlying (die
->sect_off
),
22174 /* Follow reference or signature attribute ATTR of SRC_DIE.
22175 On entry *REF_CU is the CU of SRC_DIE.
22176 On exit *REF_CU is the CU of the result. */
22178 static struct die_info
*
22179 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22180 struct dwarf2_cu
**ref_cu
)
22182 struct die_info
*die
;
22184 if (attr
->form_is_ref ())
22185 die
= follow_die_ref (src_die
, attr
, ref_cu
);
22186 else if (attr
->form
== DW_FORM_ref_sig8
)
22187 die
= follow_die_sig (src_die
, attr
, ref_cu
);
22190 src_die
->error_dump ();
22191 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22192 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22198 /* Follow reference OFFSET.
22199 On entry *REF_CU is the CU of the source die referencing OFFSET.
22200 On exit *REF_CU is the CU of the result.
22201 Returns NULL if OFFSET is invalid. */
22203 static struct die_info
*
22204 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
22205 struct dwarf2_cu
**ref_cu
)
22207 struct die_info temp_die
;
22208 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
22209 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22211 gdb_assert (cu
->per_cu
!= NULL
);
22215 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22216 "source CU contains target offset: %d",
22217 sect_offset_str (cu
->per_cu
->sect_off
),
22218 sect_offset_str (sect_off
),
22219 cu
->header
.offset_in_cu_p (sect_off
));
22221 if (cu
->per_cu
->is_debug_types
)
22223 /* .debug_types CUs cannot reference anything outside their CU.
22224 If they need to, they have to reference a signatured type via
22225 DW_FORM_ref_sig8. */
22226 if (!cu
->header
.offset_in_cu_p (sect_off
))
22229 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
22230 || !cu
->header
.offset_in_cu_p (sect_off
))
22232 struct dwarf2_per_cu_data
*per_cu
;
22234 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
22235 per_objfile
->per_bfd
);
22237 dwarf_read_debug_printf_v ("target CU offset: %s, "
22238 "target CU DIEs loaded: %d",
22239 sect_offset_str (per_cu
->sect_off
),
22240 per_objfile
->get_cu (per_cu
) != nullptr);
22242 /* If necessary, add it to the queue and load its DIEs.
22244 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22245 it doesn't mean they are currently loaded. Since we require them
22246 to be loaded, we must check for ourselves. */
22247 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->lang ())
22248 || per_objfile
->get_cu (per_cu
) == nullptr)
22249 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
22250 false, cu
->lang ());
22252 target_cu
= per_objfile
->get_cu (per_cu
);
22253 gdb_assert (target_cu
!= nullptr);
22255 else if (cu
->dies
== NULL
)
22257 /* We're loading full DIEs during partial symbol reading. */
22258 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
22262 *ref_cu
= target_cu
;
22263 temp_die
.sect_off
= sect_off
;
22265 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
22267 to_underlying (sect_off
));
22270 /* Follow reference attribute ATTR of SRC_DIE.
22271 On entry *REF_CU is the CU of SRC_DIE.
22272 On exit *REF_CU is the CU of the result. */
22274 static struct die_info
*
22275 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
22276 struct dwarf2_cu
**ref_cu
)
22278 sect_offset sect_off
= attr
->get_ref_die_offset ();
22279 struct dwarf2_cu
*cu
= *ref_cu
;
22280 struct die_info
*die
;
22282 die
= follow_die_offset (sect_off
,
22283 (attr
->form
== DW_FORM_GNU_ref_alt
22284 || cu
->per_cu
->is_dwz
),
22287 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22288 "at %s [in module %s]"),
22289 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
22290 objfile_name (cu
->per_objfile
->objfile
));
22297 struct dwarf2_locexpr_baton
22298 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
22299 dwarf2_per_cu_data
*per_cu
,
22300 dwarf2_per_objfile
*per_objfile
,
22301 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
22302 bool resolve_abstract_p
)
22304 struct die_info
*die
;
22305 struct attribute
*attr
;
22306 struct dwarf2_locexpr_baton retval
;
22307 struct objfile
*objfile
= per_objfile
->objfile
;
22309 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22311 cu
= load_cu (per_cu
, per_objfile
, false);
22315 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22316 Instead just throw an error, not much else we can do. */
22317 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22318 sect_offset_str (sect_off
), objfile_name (objfile
));
22321 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22323 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22324 sect_offset_str (sect_off
), objfile_name (objfile
));
22326 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22327 if (!attr
&& resolve_abstract_p
22328 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
22329 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
22331 CORE_ADDR pc
= get_frame_pc ();
22332 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
22333 struct gdbarch
*gdbarch
= objfile
->arch ();
22335 for (const auto &cand_off
22336 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
22338 struct dwarf2_cu
*cand_cu
= cu
;
22339 struct die_info
*cand
22340 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
22343 || cand
->parent
->tag
!= DW_TAG_subprogram
)
22346 CORE_ADDR pc_low
, pc_high
;
22347 get_scope_pc_bounds (cand
->parent
, &pc_low
, &pc_high
, cu
);
22348 if (pc_low
== ((CORE_ADDR
) -1))
22350 pc_low
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_low
+ baseaddr
);
22351 pc_high
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_high
+ baseaddr
);
22352 if (!(pc_low
<= pc
&& pc
< pc_high
))
22356 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22363 /* DWARF: "If there is no such attribute, then there is no effect.".
22364 DATA is ignored if SIZE is 0. */
22366 retval
.data
= NULL
;
22369 else if (attr
->form_is_section_offset ())
22371 struct dwarf2_loclist_baton loclist_baton
;
22372 CORE_ADDR pc
= get_frame_pc ();
22375 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
22377 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
22379 retval
.size
= size
;
22383 if (!attr
->form_is_block ())
22384 error (_("Dwarf Error: DIE at %s referenced in module %s "
22385 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22386 sect_offset_str (sect_off
), objfile_name (objfile
));
22388 struct dwarf_block
*block
= attr
->as_block ();
22389 retval
.data
= block
->data
;
22390 retval
.size
= block
->size
;
22392 retval
.per_objfile
= per_objfile
;
22393 retval
.per_cu
= cu
->per_cu
;
22395 per_objfile
->age_comp_units ();
22402 struct dwarf2_locexpr_baton
22403 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
22404 dwarf2_per_cu_data
*per_cu
,
22405 dwarf2_per_objfile
*per_objfile
,
22406 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
22408 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
22410 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
22414 /* Write a constant of a given type as target-ordered bytes into
22417 static const gdb_byte
*
22418 write_constant_as_bytes (struct obstack
*obstack
,
22419 enum bfd_endian byte_order
,
22426 *len
= type
->length ();
22427 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22428 store_unsigned_integer (result
, *len
, byte_order
, value
);
22436 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
22437 dwarf2_per_cu_data
*per_cu
,
22438 dwarf2_per_objfile
*per_objfile
,
22442 struct die_info
*die
;
22443 struct attribute
*attr
;
22444 const gdb_byte
*result
= NULL
;
22447 enum bfd_endian byte_order
;
22448 struct objfile
*objfile
= per_objfile
->objfile
;
22450 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22452 cu
= load_cu (per_cu
, per_objfile
, false);
22456 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22457 Instead just throw an error, not much else we can do. */
22458 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22459 sect_offset_str (sect_off
), objfile_name (objfile
));
22462 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22464 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22465 sect_offset_str (sect_off
), objfile_name (objfile
));
22467 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
22471 byte_order
= (bfd_big_endian (objfile
->obfd
.get ())
22472 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
22474 switch (attr
->form
)
22477 case DW_FORM_addrx
:
22478 case DW_FORM_GNU_addr_index
:
22482 *len
= cu
->header
.addr_size
;
22483 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22484 store_unsigned_integer (tem
, *len
, byte_order
, attr
->as_address ());
22488 case DW_FORM_string
:
22491 case DW_FORM_GNU_str_index
:
22492 case DW_FORM_GNU_strp_alt
:
22493 /* The string is already allocated on the objfile obstack, point
22496 const char *attr_name
= attr
->as_string ();
22497 result
= (const gdb_byte
*) attr_name
;
22498 *len
= strlen (attr_name
);
22501 case DW_FORM_block1
:
22502 case DW_FORM_block2
:
22503 case DW_FORM_block4
:
22504 case DW_FORM_block
:
22505 case DW_FORM_exprloc
:
22506 case DW_FORM_data16
:
22508 struct dwarf_block
*block
= attr
->as_block ();
22509 result
= block
->data
;
22510 *len
= block
->size
;
22514 /* The DW_AT_const_value attributes are supposed to carry the
22515 symbol's value "represented as it would be on the target
22516 architecture." By the time we get here, it's already been
22517 converted to host endianness, so we just need to sign- or
22518 zero-extend it as appropriate. */
22519 case DW_FORM_data1
:
22520 type
= die_type (die
, cu
);
22521 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
22522 if (result
== NULL
)
22523 result
= write_constant_as_bytes (obstack
, byte_order
,
22526 case DW_FORM_data2
:
22527 type
= die_type (die
, cu
);
22528 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
22529 if (result
== NULL
)
22530 result
= write_constant_as_bytes (obstack
, byte_order
,
22533 case DW_FORM_data4
:
22534 type
= die_type (die
, cu
);
22535 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
22536 if (result
== NULL
)
22537 result
= write_constant_as_bytes (obstack
, byte_order
,
22540 case DW_FORM_data8
:
22541 type
= die_type (die
, cu
);
22542 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
22543 if (result
== NULL
)
22544 result
= write_constant_as_bytes (obstack
, byte_order
,
22548 case DW_FORM_sdata
:
22549 case DW_FORM_implicit_const
:
22550 type
= die_type (die
, cu
);
22551 result
= write_constant_as_bytes (obstack
, byte_order
,
22552 type
, attr
->as_signed (), len
);
22555 case DW_FORM_udata
:
22556 type
= die_type (die
, cu
);
22557 result
= write_constant_as_bytes (obstack
, byte_order
,
22558 type
, attr
->as_unsigned (), len
);
22562 complaint (_("unsupported const value attribute form: '%s'"),
22563 dwarf_form_name (attr
->form
));
22573 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
22574 dwarf2_per_cu_data
*per_cu
,
22575 dwarf2_per_objfile
*per_objfile
,
22576 const char **var_name
)
22578 struct die_info
*die
;
22580 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22582 cu
= load_cu (per_cu
, per_objfile
, false);
22587 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22591 if (var_name
!= nullptr)
22592 *var_name
= var_decl_name (die
, cu
);
22593 return die_type (die
, cu
);
22599 dwarf2_get_die_type (cu_offset die_offset
,
22600 dwarf2_per_cu_data
*per_cu
,
22601 dwarf2_per_objfile
*per_objfile
)
22603 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
22604 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
22607 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22608 On entry *REF_CU is the CU of SRC_DIE.
22609 On exit *REF_CU is the CU of the result.
22610 Returns NULL if the referenced DIE isn't found. */
22612 static struct die_info
*
22613 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
22614 struct dwarf2_cu
**ref_cu
)
22616 struct die_info temp_die
;
22617 struct dwarf2_cu
*sig_cu
;
22618 struct die_info
*die
;
22619 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
22622 /* While it might be nice to assert sig_type->type == NULL here,
22623 we can get here for DW_AT_imported_declaration where we need
22624 the DIE not the type. */
22626 /* If necessary, add it to the queue and load its DIEs.
22628 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22629 it doesn't mean they are currently loaded. Since we require them
22630 to be loaded, we must check for ourselves. */
22631 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
22633 || per_objfile
->get_cu (sig_type
) == nullptr)
22634 read_signatured_type (sig_type
, per_objfile
);
22636 sig_cu
= per_objfile
->get_cu (sig_type
);
22637 gdb_assert (sig_cu
!= NULL
);
22638 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
22639 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
22640 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
22641 to_underlying (temp_die
.sect_off
));
22644 /* For .gdb_index version 7 keep track of included TUs.
22645 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22646 if (per_objfile
->per_bfd
->index_table
!= NULL
22647 && !per_objfile
->per_bfd
->index_table
->version_check ())
22649 (*ref_cu
)->per_cu
->imported_symtabs_push (sig_cu
->per_cu
);
22659 /* Follow signatured type referenced by ATTR in SRC_DIE.
22660 On entry *REF_CU is the CU of SRC_DIE.
22661 On exit *REF_CU is the CU of the result.
22662 The result is the DIE of the type.
22663 If the referenced type cannot be found an error is thrown. */
22665 static struct die_info
*
22666 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22667 struct dwarf2_cu
**ref_cu
)
22669 ULONGEST signature
= attr
->as_signature ();
22670 struct signatured_type
*sig_type
;
22671 struct die_info
*die
;
22673 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
22675 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
22676 /* sig_type will be NULL if the signatured type is missing from
22678 if (sig_type
== NULL
)
22680 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22681 " from DIE at %s [in module %s]"),
22682 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22683 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22686 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
22689 src_die
->error_dump ();
22690 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22691 " from DIE at %s [in module %s]"),
22692 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22693 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22699 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22700 reading in and processing the type unit if necessary. */
22702 static struct type
*
22703 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
22704 struct dwarf2_cu
*cu
)
22706 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22707 struct signatured_type
*sig_type
;
22708 struct dwarf2_cu
*type_cu
;
22709 struct die_info
*type_die
;
22712 sig_type
= lookup_signatured_type (cu
, signature
);
22713 /* sig_type will be NULL if the signatured type is missing from
22715 if (sig_type
== NULL
)
22717 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22718 " from DIE at %s [in module %s]"),
22719 hex_string (signature
), sect_offset_str (die
->sect_off
),
22720 objfile_name (per_objfile
->objfile
));
22721 return build_error_marker_type (cu
, die
);
22724 /* If we already know the type we're done. */
22725 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
22726 if (type
!= nullptr)
22730 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
22731 if (type_die
!= NULL
)
22733 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22734 is created. This is important, for example, because for c++ classes
22735 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22736 type
= read_type_die (type_die
, type_cu
);
22739 complaint (_("Dwarf Error: Cannot build signatured type %s"
22740 " referenced from DIE at %s [in module %s]"),
22741 hex_string (signature
), sect_offset_str (die
->sect_off
),
22742 objfile_name (per_objfile
->objfile
));
22743 type
= build_error_marker_type (cu
, die
);
22748 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22749 " from DIE at %s [in module %s]"),
22750 hex_string (signature
), sect_offset_str (die
->sect_off
),
22751 objfile_name (per_objfile
->objfile
));
22752 type
= build_error_marker_type (cu
, die
);
22755 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
22760 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22761 reading in and processing the type unit if necessary. */
22763 static struct type
*
22764 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
22765 struct dwarf2_cu
*cu
) /* ARI: editCase function */
22767 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22768 if (attr
->form_is_ref ())
22770 struct dwarf2_cu
*type_cu
= cu
;
22771 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
22773 return read_type_die (type_die
, type_cu
);
22775 else if (attr
->form
== DW_FORM_ref_sig8
)
22777 return get_signatured_type (die
, attr
->as_signature (), cu
);
22781 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22783 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22784 " at %s [in module %s]"),
22785 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
22786 objfile_name (per_objfile
->objfile
));
22787 return build_error_marker_type (cu
, die
);
22791 /* Load the DIEs associated with type unit PER_CU into memory. */
22794 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
22795 dwarf2_per_objfile
*per_objfile
)
22797 struct signatured_type
*sig_type
;
22799 /* We have the per_cu, but we need the signatured_type.
22800 Fortunately this is an easy translation. */
22801 gdb_assert (per_cu
->is_debug_types
);
22802 sig_type
= (struct signatured_type
*) per_cu
;
22804 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
22806 read_signatured_type (sig_type
, per_objfile
);
22808 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
22811 /* Read in a signatured type and build its CU and DIEs.
22812 If the type is a stub for the real type in a DWO file,
22813 read in the real type from the DWO file as well. */
22816 read_signatured_type (signatured_type
*sig_type
,
22817 dwarf2_per_objfile
*per_objfile
)
22819 gdb_assert (sig_type
->is_debug_types
);
22820 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
22822 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
22824 if (!reader
.dummy_p
)
22826 struct dwarf2_cu
*cu
= reader
.cu
;
22827 const gdb_byte
*info_ptr
= reader
.info_ptr
;
22829 gdb_assert (cu
->die_hash
== NULL
);
22831 htab_create_alloc_ex (cu
->header
.get_length_without_initial () / 12,
22835 &cu
->comp_unit_obstack
,
22836 hashtab_obstack_allocate
,
22837 dummy_obstack_deallocate
);
22839 if (reader
.comp_unit_die
->has_children
)
22840 reader
.comp_unit_die
->child
22841 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
22842 reader
.comp_unit_die
);
22843 cu
->dies
= reader
.comp_unit_die
;
22844 /* comp_unit_die is not stored in die_hash, no need. */
22846 /* We try not to read any attributes in this function, because
22847 not all CUs needed for references have been loaded yet, and
22848 symbol table processing isn't initialized. But we have to
22849 set the CU language, or we won't be able to build types
22850 correctly. Similarly, if we do not read the producer, we can
22851 not apply producer-specific interpretation. */
22852 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
22857 sig_type
->tu_read
= 1;
22860 /* Decode simple location descriptions.
22861 Given a pointer to a dwarf block that defines a location, compute
22862 the location and return the value. If COMPUTED is non-null, it is
22863 set to true to indicate that decoding was successful, and false
22864 otherwise. If COMPUTED is null, then this function may emit a
22868 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
, bool *computed
)
22870 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
22872 size_t size
= blk
->size
;
22873 const gdb_byte
*data
= blk
->data
;
22874 CORE_ADDR stack
[64];
22876 unsigned int bytes_read
, unsnd
;
22879 if (computed
!= nullptr)
22885 stack
[++stacki
] = 0;
22924 stack
[++stacki
] = op
- DW_OP_lit0
;
22959 stack
[++stacki
] = op
- DW_OP_reg0
;
22962 if (computed
== nullptr)
22963 dwarf2_complex_location_expr_complaint ();
22970 unsnd
= read_unsigned_leb128 (NULL
, (data
+ i
), &bytes_read
);
22972 stack
[++stacki
] = unsnd
;
22975 if (computed
== nullptr)
22976 dwarf2_complex_location_expr_complaint ();
22983 stack
[++stacki
] = cu
->header
.read_address (objfile
->obfd
.get (),
22989 case DW_OP_const1u
:
22990 stack
[++stacki
] = read_1_byte (objfile
->obfd
.get (), &data
[i
]);
22994 case DW_OP_const1s
:
22995 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
.get (), &data
[i
]);
22999 case DW_OP_const2u
:
23000 stack
[++stacki
] = read_2_bytes (objfile
->obfd
.get (), &data
[i
]);
23004 case DW_OP_const2s
:
23005 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
.get (), &data
[i
]);
23009 case DW_OP_const4u
:
23010 stack
[++stacki
] = read_4_bytes (objfile
->obfd
.get (), &data
[i
]);
23014 case DW_OP_const4s
:
23015 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
.get (), &data
[i
]);
23019 case DW_OP_const8u
:
23020 stack
[++stacki
] = read_8_bytes (objfile
->obfd
.get (), &data
[i
]);
23025 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
23031 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
23036 stack
[stacki
+ 1] = stack
[stacki
];
23041 stack
[stacki
- 1] += stack
[stacki
];
23045 case DW_OP_plus_uconst
:
23046 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
23052 stack
[stacki
- 1] -= stack
[stacki
];
23057 /* If we're not the last op, then we definitely can't encode
23058 this using GDB's address_class enum. This is valid for partial
23059 global symbols, although the variable's address will be bogus
23063 if (computed
== nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23070 case DW_OP_GNU_push_tls_address
:
23071 case DW_OP_form_tls_address
:
23072 /* The top of the stack has the offset from the beginning
23073 of the thread control block at which the variable is located. */
23074 /* Nothing should follow this operator, so the top of stack would
23076 /* This is valid for partial global symbols, but the variable's
23077 address will be bogus in the psymtab. Make it always at least
23078 non-zero to not look as a variable garbage collected by linker
23079 which have DW_OP_addr 0. */
23082 if (computed
== nullptr)
23083 dwarf2_complex_location_expr_complaint ();
23090 case DW_OP_GNU_uninit
:
23091 if (computed
!= nullptr)
23096 case DW_OP_GNU_addr_index
:
23097 case DW_OP_GNU_const_index
:
23098 stack
[++stacki
] = read_addr_index_from_leb128 (cu
, &data
[i
],
23104 if (computed
== nullptr)
23106 const char *name
= get_DW_OP_name (op
);
23109 complaint (_("unsupported stack op: '%s'"),
23112 complaint (_("unsupported stack op: '%02x'"),
23116 return (stack
[stacki
]);
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki
>= ARRAY_SIZE (stack
) - 1)
23123 if (computed
== nullptr)
23124 complaint (_("location description stack overflow"));
23130 if (computed
== nullptr)
23131 complaint (_("location description stack underflow"));
23136 if (computed
!= nullptr)
23138 return (stack
[stacki
]);
23141 /* memory allocation interface */
23143 static struct dwarf_block
*
23144 dwarf_alloc_block (struct dwarf2_cu
*cu
)
23146 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
23151 /* Macro support. */
23153 /* An overload of dwarf_decode_macros that finds the correct section
23154 and ensures it is read in before calling the other overload. */
23157 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
23158 int section_is_gnu
)
23160 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23161 struct objfile
*objfile
= per_objfile
->objfile
;
23162 const struct line_header
*lh
= cu
->line_header
;
23163 unsigned int offset_size
= cu
->header
.offset_size
;
23164 struct dwarf2_section_info
*section
;
23165 const char *section_name
;
23167 if (cu
->dwo_unit
!= nullptr)
23169 if (section_is_gnu
)
23171 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
23172 section_name
= ".debug_macro.dwo";
23176 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
23177 section_name
= ".debug_macinfo.dwo";
23182 if (section_is_gnu
)
23184 section
= &per_objfile
->per_bfd
->macro
;
23185 section_name
= ".debug_macro";
23189 section
= &per_objfile
->per_bfd
->macinfo
;
23190 section_name
= ".debug_macinfo";
23194 section
->read (objfile
);
23195 if (section
->buffer
== nullptr)
23197 complaint (_("missing %s section"), section_name
);
23201 buildsym_compunit
*builder
= cu
->get_builder ();
23203 struct dwarf2_section_info
*str_offsets_section
;
23204 struct dwarf2_section_info
*str_section
;
23205 gdb::optional
<ULONGEST
> str_offsets_base
;
23207 if (cu
->dwo_unit
!= nullptr)
23209 str_offsets_section
= &cu
->dwo_unit
->dwo_file
23210 ->sections
.str_offsets
;
23211 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
23212 str_offsets_base
= cu
->header
.addr_size
;
23216 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
23217 str_section
= &per_objfile
->per_bfd
->str
;
23218 str_offsets_base
= cu
->str_offsets_base
;
23221 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
23222 offset_size
, offset
, str_section
, str_offsets_section
,
23223 str_offsets_base
, section_is_gnu
, cu
);
23226 /* Return the .debug_loc section to use for CU.
23227 For DWO files use .debug_loc.dwo. */
23229 static struct dwarf2_section_info
*
23230 cu_debug_loc_section (struct dwarf2_cu
*cu
)
23232 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23236 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23238 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
23240 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
23241 : &per_objfile
->per_bfd
->loc
);
23244 /* Return the .debug_rnglists section to use for CU. */
23245 static struct dwarf2_section_info
*
23246 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
23248 if (cu
->header
.version
< 5)
23249 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23250 cu
->header
.version
);
23251 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
23253 /* Make sure we read the .debug_rnglists section from the file that
23254 contains the DW_AT_ranges attribute we are reading. Normally that
23255 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23256 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23258 if (cu
->dwo_unit
!= nullptr
23259 && tag
!= DW_TAG_compile_unit
23260 && tag
!= DW_TAG_skeleton_unit
)
23262 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23264 if (sections
->rnglists
.size
> 0)
23265 return §ions
->rnglists
;
23267 error (_(".debug_rnglists section is missing from .dwo file."));
23269 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
23272 /* A helper function that fills in a dwarf2_loclist_baton. */
23275 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
23276 struct dwarf2_loclist_baton
*baton
,
23277 const struct attribute
*attr
)
23279 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23280 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23282 section
->read (per_objfile
->objfile
);
23284 baton
->per_objfile
= per_objfile
;
23285 baton
->per_cu
= cu
->per_cu
;
23286 gdb_assert (baton
->per_cu
);
23287 /* We don't know how long the location list is, but make sure we
23288 don't run off the edge of the section. */
23289 baton
->size
= section
->size
- attr
->as_unsigned ();
23290 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
23291 if (cu
->base_address
.has_value ())
23292 baton
->base_address
= *cu
->base_address
;
23294 baton
->base_address
= 0;
23295 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
23299 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
23300 struct dwarf2_cu
*cu
, int is_block
)
23302 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23303 struct objfile
*objfile
= per_objfile
->objfile
;
23304 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23306 if (attr
->form_is_section_offset ()
23307 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23308 the section. If so, fall through to the complaint in the
23310 && attr
->as_unsigned () < section
->get_size (objfile
))
23312 struct dwarf2_loclist_baton
*baton
;
23314 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
23316 fill_in_loclist_baton (cu
, baton
, attr
);
23318 if (!cu
->base_address
.has_value ())
23319 complaint (_("Location list used without "
23320 "specifying the CU base address."));
23322 sym
->set_aclass_index ((is_block
23323 ? dwarf2_loclist_block_index
23324 : dwarf2_loclist_index
));
23325 SYMBOL_LOCATION_BATON (sym
) = baton
;
23329 struct dwarf2_locexpr_baton
*baton
;
23331 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
23332 baton
->per_objfile
= per_objfile
;
23333 baton
->per_cu
= cu
->per_cu
;
23334 gdb_assert (baton
->per_cu
);
23336 if (attr
->form_is_block ())
23338 /* Note that we're just copying the block's data pointer
23339 here, not the actual data. We're still pointing into the
23340 info_buffer for SYM's objfile; right now we never release
23341 that buffer, but when we do clean up properly this may
23343 struct dwarf_block
*block
= attr
->as_block ();
23344 baton
->size
= block
->size
;
23345 baton
->data
= block
->data
;
23349 dwarf2_invalid_attrib_class_complaint ("location description",
23350 sym
->natural_name ());
23354 sym
->set_aclass_index ((is_block
23355 ? dwarf2_locexpr_block_index
23356 : dwarf2_locexpr_index
));
23357 SYMBOL_LOCATION_BATON (sym
) = baton
;
23363 const comp_unit_head
*
23364 dwarf2_per_cu_data::get_header () const
23366 if (!m_header_read_in
)
23368 const gdb_byte
*info_ptr
23369 = this->section
->buffer
+ to_underlying (this->sect_off
);
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_units
)
23424 high
= all_units
.size () - 1;
23427 struct dwarf2_per_cu_data
*mid_cu
;
23428 int mid
= low
+ (high
- low
) / 2;
23430 mid_cu
= all_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_units
);
23452 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_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_units
[low
-1]->sect_off
23464 return per_bfd
->all_units
[low
- 1].get ();
23468 if (low
== per_bfd
->all_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 ();
23493 one
->set_length (5);
23494 two
->sect_off
= sect_offset (one
->length ());
23495 two
->set_length (7);
23497 three
->set_length (5);
23499 four
->sect_off
= sect_offset (three
->length ());
23500 four
->set_length (7);
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 enum language lang
;
23544 if (cu
->producer
!= nullptr
23545 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
23547 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23548 attribute is not standardised yet. As a workaround for the
23549 language detection we fall back to the DW_AT_producer
23551 lang
= language_opencl
;
23553 else if (cu
->producer
!= nullptr
23554 && strstr (cu
->producer
, "GNU Go ") != NULL
)
23556 /* Similar hack for Go. */
23557 lang
= language_go
;
23559 else if (attr
!= nullptr)
23560 lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
23562 lang
= pretend_language
;
23564 cu
->language_defn
= language_def (lang
);
23566 switch (comp_unit_die
->tag
)
23568 case DW_TAG_compile_unit
:
23569 cu
->per_cu
->set_unit_type (DW_UT_compile
);
23571 case DW_TAG_partial_unit
:
23572 cu
->per_cu
->set_unit_type (DW_UT_partial
);
23574 case DW_TAG_type_unit
:
23575 cu
->per_cu
->set_unit_type (DW_UT_type
);
23578 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
23579 dwarf_tag_name (comp_unit_die
->tag
),
23580 sect_offset_str (cu
->per_cu
->sect_off
));
23583 cu
->per_cu
->set_lang (lang
);
23589 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
23591 auto it
= m_dwarf2_cus
.find (per_cu
);
23592 if (it
== m_dwarf2_cus
.end ())
23595 return it
->second
.get ();
23601 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
,
23602 std::unique_ptr
<dwarf2_cu
> cu
)
23604 gdb_assert (this->get_cu (per_cu
) == nullptr);
23606 m_dwarf2_cus
[per_cu
] = std::move (cu
);
23612 dwarf2_per_objfile::age_comp_units ()
23614 dwarf_read_debug_printf_v ("running");
23616 /* This is not expected to be called in the middle of CU expansion. There is
23617 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23618 loaded in memory. Calling age_comp_units while the queue is in use could
23619 make us free the DIEs for a CU that is in the queue and therefore break
23621 gdb_assert (!queue
.has_value ());
23623 /* Start by clearing all marks. */
23624 for (const auto &pair
: m_dwarf2_cus
)
23625 pair
.second
->clear_mark ();
23627 /* Traverse all CUs, mark them and their dependencies if used recently
23629 for (const auto &pair
: m_dwarf2_cus
)
23631 dwarf2_cu
*cu
= pair
.second
.get ();
23634 if (cu
->last_used
<= dwarf_max_cache_age
)
23638 /* Delete all CUs still not marked. */
23639 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
23641 dwarf2_cu
*cu
= it
->second
.get ();
23643 if (!cu
->is_marked ())
23645 dwarf_read_debug_printf_v ("deleting old CU %s",
23646 sect_offset_str (cu
->per_cu
->sect_off
));
23647 it
= m_dwarf2_cus
.erase (it
);
23657 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
23659 auto it
= m_dwarf2_cus
.find (per_cu
);
23660 if (it
== m_dwarf2_cus
.end ())
23663 m_dwarf2_cus
.erase (it
);
23666 dwarf2_per_objfile::~dwarf2_per_objfile ()
23671 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23672 We store these in a hash table separate from the DIEs, and preserve them
23673 when the DIEs are flushed out of cache.
23675 The CU "per_cu" pointer is needed because offset alone is not enough to
23676 uniquely identify the type. A file may have multiple .debug_types sections,
23677 or the type may come from a DWO file. Furthermore, while it's more logical
23678 to use per_cu->section+offset, with Fission the section with the data is in
23679 the DWO file but we don't know that section at the point we need it.
23680 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23681 because we can enter the lookup routine, get_die_type_at_offset, from
23682 outside this file, and thus won't necessarily have PER_CU->cu.
23683 Fortunately, PER_CU is stable for the life of the objfile. */
23685 struct dwarf2_per_cu_offset_and_type
23687 const struct dwarf2_per_cu_data
*per_cu
;
23688 sect_offset sect_off
;
23692 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23695 per_cu_offset_and_type_hash (const void *item
)
23697 const struct dwarf2_per_cu_offset_and_type
*ofs
23698 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
23700 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
23703 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23706 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
23708 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
23709 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
23710 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
23711 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
23713 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
23714 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
23717 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23718 table if necessary. For convenience, return TYPE.
23720 The DIEs reading must have careful ordering to:
23721 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23722 reading current DIE.
23723 * Not trying to dereference contents of still incompletely read in types
23724 while reading in other DIEs.
23725 * Enable referencing still incompletely read in types just by a pointer to
23726 the type without accessing its fields.
23728 Therefore caller should follow these rules:
23729 * Try to fetch any prerequisite types we may need to build this DIE type
23730 before building the type and calling set_die_type.
23731 * After building type call set_die_type for current DIE as soon as
23732 possible before fetching more types to complete the current type.
23733 * Make the type as complete as possible before fetching more types. */
23735 static struct type
*
23736 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
23737 bool skip_data_location
)
23739 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23740 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
23741 struct objfile
*objfile
= per_objfile
->objfile
;
23742 struct attribute
*attr
;
23743 struct dynamic_prop prop
;
23745 /* For Ada types, make sure that the gnat-specific data is always
23746 initialized (if not already set). There are a few types where
23747 we should not be doing so, because the type-specific area is
23748 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23749 where the type-specific area is used to store the floatformat).
23750 But this is not a problem, because the gnat-specific information
23751 is actually not needed for these types. */
23752 if (need_gnat_info (cu
)
23753 && type
->code () != TYPE_CODE_FUNC
23754 && type
->code () != TYPE_CODE_FLT
23755 && type
->code () != TYPE_CODE_METHODPTR
23756 && type
->code () != TYPE_CODE_MEMBERPTR
23757 && type
->code () != TYPE_CODE_METHOD
23758 && type
->code () != TYPE_CODE_FIXED_POINT
23759 && !HAVE_GNAT_AUX_INFO (type
))
23760 INIT_GNAT_SPECIFIC (type
);
23762 /* Read DW_AT_allocated and set in type. */
23763 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
23766 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23767 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23768 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
23771 /* Read DW_AT_associated and set in type. */
23772 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
23775 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23776 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23777 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
23780 /* Read DW_AT_rank and set in type. */
23781 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
23784 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23785 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23786 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
23789 /* Read DW_AT_data_location and set in type. */
23790 if (!skip_data_location
)
23792 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
23793 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
23794 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
23797 if (per_objfile
->die_type_hash
== NULL
)
23798 per_objfile
->die_type_hash
23799 = htab_up (htab_create_alloc (127,
23800 per_cu_offset_and_type_hash
,
23801 per_cu_offset_and_type_eq
,
23802 NULL
, xcalloc
, xfree
));
23804 ofs
.per_cu
= cu
->per_cu
;
23805 ofs
.sect_off
= die
->sect_off
;
23807 slot
= (struct dwarf2_per_cu_offset_and_type
**)
23808 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
23810 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23811 sect_offset_str (die
->sect_off
));
23812 *slot
= XOBNEW (&objfile
->objfile_obstack
,
23813 struct dwarf2_per_cu_offset_and_type
);
23818 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23819 or return NULL if the die does not have a saved type. */
23821 static struct type
*
23822 get_die_type_at_offset (sect_offset sect_off
,
23823 dwarf2_per_cu_data
*per_cu
,
23824 dwarf2_per_objfile
*per_objfile
)
23826 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
23828 if (per_objfile
->die_type_hash
== NULL
)
23831 ofs
.per_cu
= per_cu
;
23832 ofs
.sect_off
= sect_off
;
23833 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
23834 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
23841 /* Look up the type for DIE in CU in die_type_hash,
23842 or return NULL if DIE does not have a saved type. */
23844 static struct type
*
23845 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
23847 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
23850 struct cmd_list_element
*set_dwarf_cmdlist
;
23851 struct cmd_list_element
*show_dwarf_cmdlist
;
23854 show_check_physname (struct ui_file
*file
, int from_tty
,
23855 struct cmd_list_element
*c
, const char *value
)
23858 _("Whether to check \"physname\" is %s.\n"),
23862 void _initialize_dwarf2_read ();
23864 _initialize_dwarf2_read ()
23866 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
23868 Set DWARF specific variables.\n\
23869 Configure DWARF variables such as the cache size."),
23871 Show DWARF specific variables.\n\
23872 Show DWARF variables such as the cache size."),
23873 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
23874 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
23876 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
23877 &dwarf_max_cache_age
, _("\
23878 Set the upper bound on the age of cached DWARF compilation units."), _("\
23879 Show the upper bound on the age of cached DWARF compilation units."), _("\
23880 A higher limit means that cached compilation units will be stored\n\
23881 in memory longer, and more total memory will be used. Zero disables\n\
23882 caching, which can slow down startup."),
23884 show_dwarf_max_cache_age
,
23885 &set_dwarf_cmdlist
,
23886 &show_dwarf_cmdlist
);
23888 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
23889 Set debugging of the DWARF reader."), _("\
23890 Show debugging of the DWARF reader."), _("\
23891 When enabled (non-zero), debugging messages are printed during DWARF\n\
23892 reading and symtab expansion. A value of 1 (one) provides basic\n\
23893 information. A value greater than 1 provides more verbose information."),
23896 &setdebuglist
, &showdebuglist
);
23898 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
23899 Set debugging of the DWARF DIE reader."), _("\
23900 Show debugging of the DWARF DIE reader."), _("\
23901 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23902 The value is the maximum depth to print."),
23905 &setdebuglist
, &showdebuglist
);
23907 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
23908 Set debugging of the dwarf line reader."), _("\
23909 Show debugging of the dwarf line reader."), _("\
23910 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23911 A value of 1 (one) provides basic information.\n\
23912 A value greater than 1 provides more verbose information."),
23915 &setdebuglist
, &showdebuglist
);
23917 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
23918 Set cross-checking of \"physname\" code against demangler."), _("\
23919 Show cross-checking of \"physname\" code against demangler."), _("\
23920 When enabled, GDB's internal \"physname\" code is checked against\n\
23922 NULL
, show_check_physname
,
23923 &setdebuglist
, &showdebuglist
);
23925 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23926 no_class
, &use_deprecated_index_sections
, _("\
23927 Set whether to use deprecated gdb_index sections."), _("\
23928 Show whether to use deprecated gdb_index sections."), _("\
23929 When enabled, deprecated .gdb_index sections are used anyway.\n\
23930 Normally they are ignored either because of a missing feature or\n\
23931 performance issue.\n\
23932 Warning: This option must be enabled before gdb reads the file."),
23935 &setlist
, &showlist
);
23937 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23938 &dwarf2_locexpr_funcs
);
23939 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23940 &dwarf2_loclist_funcs
);
23942 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23943 &dwarf2_block_frame_base_locexpr_funcs
);
23944 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23945 &dwarf2_block_frame_base_loclist_funcs
);
23948 selftests::register_test ("dw2_expand_symtabs_matching",
23949 selftests::dw2_expand_symtabs_matching::run_test
);
23950 selftests::register_test ("dwarf2_find_containing_comp_unit",
23951 selftests::find_containing_comp_unit::run_test
);