1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2022 Free Software Foundation, Inc.
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 This file is part of GDB.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit-head.h"
36 #include "dwarf2/cu.h"
37 #include "dwarf2/index-cache.h"
38 #include "dwarf2/index-common.h"
39 #include "dwarf2/leb.h"
40 #include "dwarf2/line-header.h"
41 #include "dwarf2/dwz.h"
42 #include "dwarf2/macro.h"
43 #include "dwarf2/die.h"
44 #include "dwarf2/sect-names.h"
45 #include "dwarf2/stringify.h"
46 #include "dwarf2/public.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
66 #include "typeprint.h"
70 #include "gdbcore.h" /* for gnutarget */
71 #include "gdb/gdb-index.h"
76 #include "namespace.h"
77 #include "gdbsupport/function-view.h"
78 #include "gdbsupport/gdb_optional.h"
79 #include "gdbsupport/underlying.h"
80 #include "gdbsupport/hash_enum.h"
81 #include "filename-seen-cache.h"
85 #include <unordered_map>
86 #include "gdbsupport/selftest.h"
87 #include "rust-lang.h"
88 #include "gdbsupport/pathstuff.h"
89 #include "count-one-bits.h"
90 #include <unordered_set>
91 #include "dwarf2/abbrev-cache.h"
92 #include "cooked-index.h"
93 #include "split-name.h"
94 #include "gdbsupport/parallel-for.h"
95 #include "gdbsupport/thread-pool.h"
97 /* When == 1, print basic high level tracing messages.
98 When > 1, be more verbose.
99 This is in contrast to the low level DIE reading of dwarf_die_debug. */
100 static unsigned int dwarf_read_debug
= 0;
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
104 #define dwarf_read_debug_printf(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
108 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
110 #define dwarf_read_debug_printf_v(fmt, ...) \
111 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
114 /* When non-zero, dump DIEs after they are read in. */
115 static unsigned int dwarf_die_debug
= 0;
117 /* When non-zero, dump line number entries as they are read in. */
118 unsigned int dwarf_line_debug
= 0;
120 /* When true, cross-check physname against demangler. */
121 static bool check_physname
= false;
123 /* When true, do not reject deprecated .gdb_index sections. */
124 static bool use_deprecated_index_sections
= false;
126 /* This is used to store the data that is always per objfile. */
127 static const objfile_key
<dwarf2_per_objfile
> dwarf2_objfile_data_key
;
129 /* These are used to store the dwarf2_per_bfd objects.
131 objfiles having the same BFD, which doesn't require relocations, are going to
132 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
134 Other objfiles are not going to share a dwarf2_per_bfd with any other
135 objfiles, so they'll have their own version kept in the _objfile_data_key
137 static const struct bfd_key
<dwarf2_per_bfd
> dwarf2_per_bfd_bfd_data_key
;
138 static const struct objfile_key
<dwarf2_per_bfd
> dwarf2_per_bfd_objfile_data_key
;
140 /* The "aclass" indices for various kinds of computed DWARF symbols. */
142 static int dwarf2_locexpr_index
;
143 static int dwarf2_loclist_index
;
144 static int dwarf2_locexpr_block_index
;
145 static int dwarf2_loclist_block_index
;
147 /* Size of .debug_loclists section header for 32-bit DWARF format. */
148 #define LOCLIST_HEADER_SIZE32 12
150 /* Size of .debug_loclists section header for 64-bit DWARF format. */
151 #define LOCLIST_HEADER_SIZE64 20
153 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
154 #define RNGLIST_HEADER_SIZE32 12
156 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
157 #define RNGLIST_HEADER_SIZE64 20
159 /* This is a view into the index that converts from bytes to an
160 offset_type, and allows indexing. Unaligned bytes are specifically
161 allowed here, and handled via unpacking. */
166 offset_view () = default;
168 explicit offset_view (gdb::array_view
<const gdb_byte
> bytes
)
173 /* Extract the INDEXth offset_type from the array. */
174 offset_type
operator[] (size_t index
) const
176 const gdb_byte
*bytes
= &m_bytes
[index
* sizeof (offset_type
)];
177 return (offset_type
) extract_unsigned_integer (bytes
,
178 sizeof (offset_type
),
182 /* Return the number of offset_types in this array. */
185 return m_bytes
.size () / sizeof (offset_type
);
188 /* Return true if this view is empty. */
191 return m_bytes
.empty ();
195 /* The underlying bytes. */
196 gdb::array_view
<const gdb_byte
> m_bytes
;
199 /* A description of the mapped index. The file format is described in
200 a comment by the code that writes the index. */
201 struct mapped_index final
: public mapped_index_base
203 /* Index data format version. */
206 /* The address table data. */
207 gdb::array_view
<const gdb_byte
> address_table
;
209 /* The symbol table, implemented as a hash table. */
210 offset_view symbol_table
;
212 /* A pointer to the constant pool. */
213 gdb::array_view
<const gdb_byte
> constant_pool
;
215 /* Return the index into the constant pool of the name of the IDXth
216 symbol in the symbol table. */
217 offset_type
symbol_name_index (offset_type idx
) const
219 return symbol_table
[2 * idx
];
222 /* Return the index into the constant pool of the CU vector of the
223 IDXth symbol in the symbol table. */
224 offset_type
symbol_vec_index (offset_type idx
) const
226 return symbol_table
[2 * idx
+ 1];
229 bool symbol_name_slot_invalid (offset_type idx
) const override
231 return (symbol_name_index (idx
) == 0
232 && symbol_vec_index (idx
) == 0);
235 /* Convenience method to get at the name of the symbol at IDX in the
237 const char *symbol_name_at
238 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
240 return (const char *) (this->constant_pool
.data ()
241 + symbol_name_index (idx
));
244 size_t symbol_name_count () const override
245 { return this->symbol_table
.size () / 2; }
247 quick_symbol_functions_up
make_quick_functions () const override
;
249 bool version_check () const override
255 /* A description of the mapped .debug_names.
256 Uninitialized map has CU_COUNT 0. */
257 struct mapped_debug_names final
: public mapped_index_base
259 bfd_endian dwarf5_byte_order
;
260 bool dwarf5_is_dwarf64
;
261 bool augmentation_is_gdb
;
263 uint32_t cu_count
= 0;
264 uint32_t tu_count
, bucket_count
, name_count
;
265 const gdb_byte
*cu_table_reordered
, *tu_table_reordered
;
266 const uint32_t *bucket_table_reordered
, *hash_table_reordered
;
267 const gdb_byte
*name_table_string_offs_reordered
;
268 const gdb_byte
*name_table_entry_offs_reordered
;
269 const gdb_byte
*entry_pool
;
276 /* Attribute name DW_IDX_*. */
279 /* Attribute form DW_FORM_*. */
282 /* Value if FORM is DW_FORM_implicit_const. */
283 LONGEST implicit_const
;
285 std::vector
<attr
> attr_vec
;
288 std::unordered_map
<ULONGEST
, index_val
> abbrev_map
;
290 const char *namei_to_name
291 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const;
293 /* Implementation of the mapped_index_base virtual interface, for
294 the name_components cache. */
296 const char *symbol_name_at
297 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
298 { return namei_to_name (idx
, per_objfile
); }
300 size_t symbol_name_count () const override
301 { return this->name_count
; }
303 quick_symbol_functions_up
make_quick_functions () const override
;
306 /* See dwarf2/read.h. */
309 get_dwarf2_per_objfile (struct objfile
*objfile
)
311 return dwarf2_objfile_data_key
.get (objfile
);
314 /* Default names of the debugging sections. */
316 /* Note that if the debugging section has been compressed, it might
317 have a name like .zdebug_info. */
319 const struct dwarf2_debug_sections dwarf2_elf_names
=
321 { ".debug_info", ".zdebug_info" },
322 { ".debug_abbrev", ".zdebug_abbrev" },
323 { ".debug_line", ".zdebug_line" },
324 { ".debug_loc", ".zdebug_loc" },
325 { ".debug_loclists", ".zdebug_loclists" },
326 { ".debug_macinfo", ".zdebug_macinfo" },
327 { ".debug_macro", ".zdebug_macro" },
328 { ".debug_str", ".zdebug_str" },
329 { ".debug_str_offsets", ".zdebug_str_offsets" },
330 { ".debug_line_str", ".zdebug_line_str" },
331 { ".debug_ranges", ".zdebug_ranges" },
332 { ".debug_rnglists", ".zdebug_rnglists" },
333 { ".debug_types", ".zdebug_types" },
334 { ".debug_addr", ".zdebug_addr" },
335 { ".debug_frame", ".zdebug_frame" },
336 { ".eh_frame", NULL
},
337 { ".gdb_index", ".zgdb_index" },
338 { ".debug_names", ".zdebug_names" },
339 { ".debug_aranges", ".zdebug_aranges" },
343 /* List of DWO/DWP sections. */
345 static const struct dwop_section_names
347 struct dwarf2_section_names abbrev_dwo
;
348 struct dwarf2_section_names info_dwo
;
349 struct dwarf2_section_names line_dwo
;
350 struct dwarf2_section_names loc_dwo
;
351 struct dwarf2_section_names loclists_dwo
;
352 struct dwarf2_section_names macinfo_dwo
;
353 struct dwarf2_section_names macro_dwo
;
354 struct dwarf2_section_names rnglists_dwo
;
355 struct dwarf2_section_names str_dwo
;
356 struct dwarf2_section_names str_offsets_dwo
;
357 struct dwarf2_section_names types_dwo
;
358 struct dwarf2_section_names cu_index
;
359 struct dwarf2_section_names tu_index
;
363 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
364 { ".debug_info.dwo", ".zdebug_info.dwo" },
365 { ".debug_line.dwo", ".zdebug_line.dwo" },
366 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
367 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
368 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
369 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
370 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
371 { ".debug_str.dwo", ".zdebug_str.dwo" },
372 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
373 { ".debug_types.dwo", ".zdebug_types.dwo" },
374 { ".debug_cu_index", ".zdebug_cu_index" },
375 { ".debug_tu_index", ".zdebug_tu_index" },
378 /* local data types */
380 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
381 begin with a header, which contains the following information. */
382 struct loclists_rnglists_header
384 /* A 4-byte or 12-byte length containing the length of the
385 set of entries for this compilation unit, not including the
386 length field itself. */
389 /* A 2-byte version identifier. */
392 /* A 1-byte unsigned integer containing the size in bytes of an address on
393 the target system. */
394 unsigned char addr_size
;
396 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
397 on the target system. */
398 unsigned char segment_collector_size
;
400 /* A 4-byte count of the number of offsets that follow the header. */
401 unsigned int offset_entry_count
;
404 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
405 This includes type_unit_group and quick_file_names. */
407 struct stmt_list_hash
409 /* The DWO unit this table is from or NULL if there is none. */
410 struct dwo_unit
*dwo_unit
;
412 /* Offset in .debug_line or .debug_line.dwo. */
413 sect_offset line_sect_off
;
416 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
417 an object of this type. This contains elements of type unit groups
418 that can be shared across objfiles. The non-shareable parts are in
419 type_unit_group_unshareable. */
421 struct type_unit_group
423 /* The data used to construct the hash key. */
424 struct stmt_list_hash hash
{};
427 /* These sections are what may appear in a (real or virtual) DWO file. */
431 struct dwarf2_section_info abbrev
;
432 struct dwarf2_section_info line
;
433 struct dwarf2_section_info loc
;
434 struct dwarf2_section_info loclists
;
435 struct dwarf2_section_info macinfo
;
436 struct dwarf2_section_info macro
;
437 struct dwarf2_section_info rnglists
;
438 struct dwarf2_section_info str
;
439 struct dwarf2_section_info str_offsets
;
440 /* In the case of a virtual DWO file, these two are unused. */
441 struct dwarf2_section_info info
;
442 std::vector
<dwarf2_section_info
> types
;
445 /* CUs/TUs in DWP/DWO files. */
449 /* Backlink to the containing struct dwo_file. */
450 struct dwo_file
*dwo_file
;
452 /* The "id" that distinguishes this CU/TU.
453 .debug_info calls this "dwo_id", .debug_types calls this "signature".
454 Since signatures came first, we stick with it for consistency. */
457 /* The section this CU/TU lives in, in the DWO file. */
458 struct dwarf2_section_info
*section
;
460 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
461 sect_offset sect_off
;
464 /* For types, offset in the type's DIE of the type defined by this TU. */
465 cu_offset type_offset_in_tu
;
468 /* include/dwarf2.h defines the DWP section codes.
469 It defines a max value but it doesn't define a min value, which we
470 use for error checking, so provide one. */
472 enum dwp_v2_section_ids
477 /* Data for one DWO file.
479 This includes virtual DWO files (a virtual DWO file is a DWO file as it
480 appears in a DWP file). DWP files don't really have DWO files per se -
481 comdat folding of types "loses" the DWO file they came from, and from
482 a high level view DWP files appear to contain a mass of random types.
483 However, to maintain consistency with the non-DWP case we pretend DWP
484 files contain virtual DWO files, and we assign each TU with one virtual
485 DWO file (generally based on the line and abbrev section offsets -
486 a heuristic that seems to work in practice). */
490 dwo_file () = default;
491 DISABLE_COPY_AND_ASSIGN (dwo_file
);
493 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
494 For virtual DWO files the name is constructed from the section offsets
495 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
496 from related CU+TUs. */
497 const char *dwo_name
= nullptr;
499 /* The DW_AT_comp_dir attribute. */
500 const char *comp_dir
= nullptr;
502 /* The bfd, when the file is open. Otherwise this is NULL.
503 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
504 gdb_bfd_ref_ptr dbfd
;
506 /* The sections that make up this DWO file.
507 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
508 sections (for lack of a better name). */
509 struct dwo_sections sections
{};
511 /* The CUs in the file.
512 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
513 an extension to handle LLVM's Link Time Optimization output (where
514 multiple source files may be compiled into a single object/dwo pair). */
517 /* Table of TUs in the file.
518 Each element is a struct dwo_unit. */
522 /* These sections are what may appear in a DWP file. */
526 /* These are used by all DWP versions (1, 2 and 5). */
527 struct dwarf2_section_info str
;
528 struct dwarf2_section_info cu_index
;
529 struct dwarf2_section_info tu_index
;
531 /* These are only used by DWP version 2 and version 5 files.
532 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
533 sections are referenced by section number, and are not recorded here.
534 In DWP version 2 or 5 there is at most one copy of all these sections,
535 each section being (effectively) comprised of the concatenation of all of
536 the individual sections that exist in the version 1 format.
537 To keep the code simple we treat each of these concatenated pieces as a
538 section itself (a virtual section?). */
539 struct dwarf2_section_info abbrev
;
540 struct dwarf2_section_info info
;
541 struct dwarf2_section_info line
;
542 struct dwarf2_section_info loc
;
543 struct dwarf2_section_info loclists
;
544 struct dwarf2_section_info macinfo
;
545 struct dwarf2_section_info macro
;
546 struct dwarf2_section_info rnglists
;
547 struct dwarf2_section_info str_offsets
;
548 struct dwarf2_section_info types
;
551 /* These sections are what may appear in a virtual DWO file in DWP version 1.
552 A virtual DWO file is a DWO file as it appears in a DWP file. */
554 struct virtual_v1_dwo_sections
556 struct dwarf2_section_info abbrev
;
557 struct dwarf2_section_info line
;
558 struct dwarf2_section_info loc
;
559 struct dwarf2_section_info macinfo
;
560 struct dwarf2_section_info macro
;
561 struct dwarf2_section_info str_offsets
;
562 /* Each DWP hash table entry records one CU or one TU.
563 That is recorded here, and copied to dwo_unit.section. */
564 struct dwarf2_section_info info_or_types
;
567 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
568 In version 2, the sections of the DWO files are concatenated together
569 and stored in one section of that name. Thus each ELF section contains
570 several "virtual" sections. */
572 struct virtual_v2_or_v5_dwo_sections
574 bfd_size_type abbrev_offset
;
575 bfd_size_type abbrev_size
;
577 bfd_size_type line_offset
;
578 bfd_size_type line_size
;
580 bfd_size_type loc_offset
;
581 bfd_size_type loc_size
;
583 bfd_size_type loclists_offset
;
584 bfd_size_type loclists_size
;
586 bfd_size_type macinfo_offset
;
587 bfd_size_type macinfo_size
;
589 bfd_size_type macro_offset
;
590 bfd_size_type macro_size
;
592 bfd_size_type rnglists_offset
;
593 bfd_size_type rnglists_size
;
595 bfd_size_type str_offsets_offset
;
596 bfd_size_type str_offsets_size
;
598 /* Each DWP hash table entry records one CU or one TU.
599 That is recorded here, and copied to dwo_unit.section. */
600 bfd_size_type info_or_types_offset
;
601 bfd_size_type info_or_types_size
;
604 /* Contents of DWP hash tables. */
606 struct dwp_hash_table
608 uint32_t version
, nr_columns
;
609 uint32_t nr_units
, nr_slots
;
610 const gdb_byte
*hash_table
, *unit_table
;
615 const gdb_byte
*indices
;
619 /* This is indexed by column number and gives the id of the section
621 #define MAX_NR_V2_DWO_SECTIONS \
622 (1 /* .debug_info or .debug_types */ \
623 + 1 /* .debug_abbrev */ \
624 + 1 /* .debug_line */ \
625 + 1 /* .debug_loc */ \
626 + 1 /* .debug_str_offsets */ \
627 + 1 /* .debug_macro or .debug_macinfo */)
628 int section_ids
[MAX_NR_V2_DWO_SECTIONS
];
629 const gdb_byte
*offsets
;
630 const gdb_byte
*sizes
;
634 /* This is indexed by column number and gives the id of the section
636 #define MAX_NR_V5_DWO_SECTIONS \
637 (1 /* .debug_info */ \
638 + 1 /* .debug_abbrev */ \
639 + 1 /* .debug_line */ \
640 + 1 /* .debug_loclists */ \
641 + 1 /* .debug_str_offsets */ \
642 + 1 /* .debug_macro */ \
643 + 1 /* .debug_rnglists */)
644 int section_ids
[MAX_NR_V5_DWO_SECTIONS
];
645 const gdb_byte
*offsets
;
646 const gdb_byte
*sizes
;
651 /* Data for one DWP file. */
655 dwp_file (const char *name_
, gdb_bfd_ref_ptr
&&abfd
)
657 dbfd (std::move (abfd
))
661 /* Name of the file. */
664 /* File format version. */
668 gdb_bfd_ref_ptr dbfd
;
670 /* Section info for this file. */
671 struct dwp_sections sections
{};
673 /* Table of CUs in the file. */
674 const struct dwp_hash_table
*cus
= nullptr;
676 /* Table of TUs in the file. */
677 const struct dwp_hash_table
*tus
= nullptr;
679 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
683 /* Table to map ELF section numbers to their sections.
684 This is only needed for the DWP V1 file format. */
685 unsigned int num_sections
= 0;
686 asection
**elf_sections
= nullptr;
689 /* Struct used to pass misc. parameters to read_die_and_children, et
690 al. which are used for both .debug_info and .debug_types dies.
691 All parameters here are unchanging for the life of the call. This
692 struct exists to abstract away the constant parameters of die reading. */
694 struct die_reader_specs
696 /* The bfd of die_section. */
699 /* The CU of the DIE we are parsing. */
700 struct dwarf2_cu
*cu
;
702 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
703 struct dwo_file
*dwo_file
;
705 /* The section the die comes from.
706 This is either .debug_info or .debug_types, or the .dwo variants. */
707 struct dwarf2_section_info
*die_section
;
709 /* die_section->buffer. */
710 const gdb_byte
*buffer
;
712 /* The end of the buffer. */
713 const gdb_byte
*buffer_end
;
715 /* The abbreviation table to use when reading the DIEs. */
716 struct abbrev_table
*abbrev_table
;
719 /* A subclass of die_reader_specs that holds storage and has complex
720 constructor and destructor behavior. */
722 class cutu_reader
: public die_reader_specs
726 cutu_reader (dwarf2_per_cu_data
*this_cu
,
727 dwarf2_per_objfile
*per_objfile
,
728 struct abbrev_table
*abbrev_table
,
729 dwarf2_cu
*existing_cu
,
731 abbrev_cache
*cache
= nullptr);
733 explicit cutu_reader (struct dwarf2_per_cu_data
*this_cu
,
734 dwarf2_per_objfile
*per_objfile
,
735 struct dwarf2_cu
*parent_cu
= nullptr,
736 struct dwo_file
*dwo_file
= nullptr);
738 DISABLE_COPY_AND_ASSIGN (cutu_reader
);
740 cutu_reader (cutu_reader
&&) = default;
742 const gdb_byte
*info_ptr
= nullptr;
743 struct die_info
*comp_unit_die
= nullptr;
744 bool dummy_p
= false;
746 /* Release the new CU, putting it on the chain. This cannot be done
750 /* Release the abbrev table, transferring ownership to the
752 abbrev_table_up
release_abbrev_table ()
754 return std::move (m_abbrev_table_holder
);
758 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
759 dwarf2_per_objfile
*per_objfile
,
760 dwarf2_cu
*existing_cu
);
762 struct dwarf2_per_cu_data
*m_this_cu
;
763 std::unique_ptr
<dwarf2_cu
> m_new_cu
;
765 /* The ordinary abbreviation table. */
766 abbrev_table_up m_abbrev_table_holder
;
768 /* The DWO abbreviation table. */
769 abbrev_table_up m_dwo_abbrev_table
;
772 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
773 but this would require a corresponding change in unpack_field_as_long
775 static int bits_per_byte
= 8;
777 struct variant_part_builder
;
779 /* When reading a variant, we track a bit more information about the
780 field, and store it in an object of this type. */
784 int first_field
= -1;
787 /* A variant can contain other variant parts. */
788 std::vector
<variant_part_builder
> variant_parts
;
790 /* If we see a DW_TAG_variant, then this will be set if this is the
792 bool default_branch
= false;
793 /* If we see a DW_AT_discr_value, then this will be the discriminant
795 ULONGEST discriminant_value
= 0;
796 /* If we see a DW_AT_discr_list, then this is a pointer to the list
798 struct dwarf_block
*discr_list_data
= nullptr;
801 /* This represents a DW_TAG_variant_part. */
803 struct variant_part_builder
805 /* The offset of the discriminant field. */
806 sect_offset discriminant_offset
{};
808 /* Variants that are direct children of this variant part. */
809 std::vector
<variant_field
> variants
;
811 /* True if we're currently reading a variant. */
812 bool processing_variant
= false;
817 int accessibility
= 0;
819 /* Variant parts need to find the discriminant, which is a DIE
820 reference. We track the section offset of each field to make
823 struct field field
{};
828 const char *name
= nullptr;
829 std::vector
<struct fn_field
> fnfields
;
832 /* The routines that read and process dies for a C struct or C++ class
833 pass lists of data member fields and lists of member function fields
834 in an instance of a field_info structure, as defined below. */
837 /* List of data member and baseclasses fields. */
838 std::vector
<struct nextfield
> fields
;
839 std::vector
<struct nextfield
> baseclasses
;
841 /* Set if the accessibility of one of the fields is not public. */
842 bool non_public_fields
= false;
844 /* Member function fieldlist array, contains name of possibly overloaded
845 member function, number of overloaded member functions and a pointer
846 to the head of the member function field chain. */
847 std::vector
<struct fnfieldlist
> fnfieldlists
;
849 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
850 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
851 std::vector
<struct decl_field
> typedef_field_list
;
853 /* Nested types defined by this class and the number of elements in this
855 std::vector
<struct decl_field
> nested_types_list
;
857 /* If non-null, this is the variant part we are currently
859 variant_part_builder
*current_variant_part
= nullptr;
860 /* This holds all the top-level variant parts attached to the type
862 std::vector
<variant_part_builder
> variant_parts
;
864 /* Return the total number of fields (including baseclasses). */
867 return fields
.size () + baseclasses
.size ();
871 /* Loaded secondary compilation units are kept in memory until they
872 have not been referenced for the processing of this many
873 compilation units. Set this to zero to disable caching. Cache
874 sizes of up to at least twenty will improve startup time for
875 typical inter-CU-reference binaries, at an obvious memory cost. */
876 static int dwarf_max_cache_age
= 5;
878 show_dwarf_max_cache_age (struct ui_file
*file
, int from_tty
,
879 struct cmd_list_element
*c
, const char *value
)
881 gdb_printf (file
, _("The upper bound on the age of cached "
882 "DWARF compilation units is %s.\n"),
886 /* local function prototypes */
888 static void dwarf2_find_base_address (struct die_info
*die
,
889 struct dwarf2_cu
*cu
);
891 class cooked_index_storage
;
892 static void build_type_psymtabs_reader (cutu_reader
*reader
,
893 cooked_index_storage
*storage
);
895 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
);
897 static void var_decode_location (struct attribute
*attr
,
899 struct dwarf2_cu
*cu
);
901 static unsigned int peek_abbrev_code (bfd
*, const gdb_byte
*);
903 static const gdb_byte
*read_attribute (const struct die_reader_specs
*,
905 const struct attr_abbrev
*,
908 static void read_attribute_reprocess (const struct die_reader_specs
*reader
,
909 struct attribute
*attr
, dwarf_tag tag
);
911 static CORE_ADDR
read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
);
913 static sect_offset
read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
914 dwarf2_section_info
*, sect_offset
);
916 static const char *read_indirect_string
917 (dwarf2_per_objfile
*per_objfile
, bfd
*, const gdb_byte
*,
918 const struct comp_unit_head
*, unsigned int *);
920 static const char *read_indirect_string_at_offset
921 (dwarf2_per_objfile
*per_objfile
, LONGEST str_offset
);
923 static CORE_ADDR
read_addr_index_from_leb128 (struct dwarf2_cu
*,
927 static const char *read_dwo_str_index (const struct die_reader_specs
*reader
,
930 static const char *read_stub_str_index (struct dwarf2_cu
*cu
,
933 static struct attribute
*dwarf2_attr (struct die_info
*, unsigned int,
936 static const char *dwarf2_string_attr (struct die_info
*die
, unsigned int name
,
937 struct dwarf2_cu
*cu
);
939 static const char *dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
);
941 static int dwarf2_flag_true_p (struct die_info
*die
, unsigned name
,
942 struct dwarf2_cu
*cu
);
944 static int die_is_declaration (struct die_info
*, struct dwarf2_cu
*cu
);
946 static struct die_info
*die_specification (struct die_info
*die
,
947 struct dwarf2_cu
**);
949 static line_header_up
dwarf_decode_line_header (sect_offset sect_off
,
950 struct dwarf2_cu
*cu
);
952 static void dwarf_decode_lines (struct line_header
*,
954 CORE_ADDR
, int decode_mapping
);
956 static void dwarf2_start_subfile (struct dwarf2_cu
*, const char *,
959 static struct symbol
*new_symbol (struct die_info
*, struct type
*,
960 struct dwarf2_cu
*, struct symbol
* = NULL
);
962 static void dwarf2_const_value (const struct attribute
*, struct symbol
*,
965 static void dwarf2_const_value_attr (const struct attribute
*attr
,
968 struct obstack
*obstack
,
969 struct dwarf2_cu
*cu
, LONGEST
*value
,
970 const gdb_byte
**bytes
,
971 struct dwarf2_locexpr_baton
**baton
);
973 static struct type
*read_subrange_index_type (struct die_info
*die
,
974 struct dwarf2_cu
*cu
);
976 static struct type
*die_type (struct die_info
*, struct dwarf2_cu
*);
978 static int need_gnat_info (struct dwarf2_cu
*);
980 static struct type
*die_descriptive_type (struct die_info
*,
983 static void set_descriptive_type (struct type
*, struct die_info
*,
986 static struct type
*die_containing_type (struct die_info
*,
989 static struct type
*lookup_die_type (struct die_info
*, const struct attribute
*,
992 static struct type
*read_type_die (struct die_info
*, struct dwarf2_cu
*);
994 static struct type
*read_type_die_1 (struct die_info
*, struct dwarf2_cu
*);
996 static const char *determine_prefix (struct die_info
*die
, struct dwarf2_cu
*);
998 static char *typename_concat (struct obstack
*obs
, const char *prefix
,
999 const char *suffix
, int physname
,
1000 struct dwarf2_cu
*cu
);
1002 static void read_file_scope (struct die_info
*, struct dwarf2_cu
*);
1004 static void read_type_unit_scope (struct die_info
*, struct dwarf2_cu
*);
1006 static void read_func_scope (struct die_info
*, struct dwarf2_cu
*);
1008 static void read_lexical_block_scope (struct die_info
*, struct dwarf2_cu
*);
1010 static void read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
);
1012 static void read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
);
1014 /* Return the .debug_loclists section to use for cu. */
1015 static struct dwarf2_section_info
*cu_debug_loc_section (struct dwarf2_cu
*cu
);
1017 /* Return the .debug_rnglists section to use for cu. */
1018 static struct dwarf2_section_info
*cu_debug_rnglists_section
1019 (struct dwarf2_cu
*cu
, dwarf_tag tag
);
1021 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1022 values. Keep the items ordered with increasing constraints compliance. */
1025 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1026 PC_BOUNDS_NOT_PRESENT
,
1028 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1029 were present but they do not form a valid range of PC addresses. */
1032 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1035 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1039 static enum pc_bounds_kind
dwarf2_get_pc_bounds (struct die_info
*,
1040 CORE_ADDR
*, CORE_ADDR
*,
1045 static void get_scope_pc_bounds (struct die_info
*,
1046 CORE_ADDR
*, CORE_ADDR
*,
1047 struct dwarf2_cu
*);
1049 static void dwarf2_record_block_ranges (struct die_info
*, struct block
*,
1050 CORE_ADDR
, struct dwarf2_cu
*);
1052 static void dwarf2_add_field (struct field_info
*, struct die_info
*,
1053 struct dwarf2_cu
*);
1055 static void dwarf2_attach_fields_to_type (struct field_info
*,
1056 struct type
*, struct dwarf2_cu
*);
1058 static void dwarf2_add_member_fn (struct field_info
*,
1059 struct die_info
*, struct type
*,
1060 struct dwarf2_cu
*);
1062 static void dwarf2_attach_fn_fields_to_type (struct field_info
*,
1064 struct dwarf2_cu
*);
1066 static void process_structure_scope (struct die_info
*, struct dwarf2_cu
*);
1068 static void read_common_block (struct die_info
*, struct dwarf2_cu
*);
1070 static void read_namespace (struct die_info
*die
, struct dwarf2_cu
*);
1072 static void read_module (struct die_info
*die
, struct dwarf2_cu
*cu
);
1074 static struct using_direct
**using_directives (struct dwarf2_cu
*cu
);
1076 static void read_import_statement (struct die_info
*die
, struct dwarf2_cu
*);
1078 static bool read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
);
1080 static struct type
*read_module_type (struct die_info
*die
,
1081 struct dwarf2_cu
*cu
);
1083 static const char *namespace_name (struct die_info
*die
,
1084 int *is_anonymous
, struct dwarf2_cu
*);
1086 static void process_enumeration_scope (struct die_info
*, struct dwarf2_cu
*);
1088 static CORE_ADDR
decode_locdesc (struct dwarf_block
*, struct dwarf2_cu
*,
1091 static enum dwarf_array_dim_ordering
read_array_order (struct die_info
*,
1092 struct dwarf2_cu
*);
1094 static struct die_info
*read_die_and_siblings_1
1095 (const struct die_reader_specs
*, const gdb_byte
*, const gdb_byte
**,
1098 static struct die_info
*read_die_and_siblings (const struct die_reader_specs
*,
1099 const gdb_byte
*info_ptr
,
1100 const gdb_byte
**new_info_ptr
,
1101 struct die_info
*parent
);
1103 static const gdb_byte
*read_full_die_1 (const struct die_reader_specs
*,
1104 struct die_info
**, const gdb_byte
*,
1107 static const gdb_byte
*read_full_die (const struct die_reader_specs
*,
1108 struct die_info
**, const gdb_byte
*);
1110 static void process_die (struct die_info
*, struct dwarf2_cu
*);
1112 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu
*,
1115 static const char *dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*);
1117 static const char *dwarf2_full_name (const char *name
,
1118 struct die_info
*die
,
1119 struct dwarf2_cu
*cu
);
1121 static const char *dwarf2_physname (const char *name
, struct die_info
*die
,
1122 struct dwarf2_cu
*cu
);
1124 static struct die_info
*dwarf2_extension (struct die_info
*die
,
1125 struct dwarf2_cu
**);
1127 static void dump_die_shallow (struct ui_file
*, int indent
, struct die_info
*);
1129 static void dump_die_for_error (struct die_info
*);
1131 static void dump_die_1 (struct ui_file
*, int level
, int max_level
,
1134 /*static*/ void dump_die (struct die_info
*, int max_level
);
1136 static void store_in_ref_table (struct die_info
*,
1137 struct dwarf2_cu
*);
1139 static struct die_info
*follow_die_ref_or_sig (struct die_info
*,
1140 const struct attribute
*,
1141 struct dwarf2_cu
**);
1143 static struct die_info
*follow_die_ref (struct die_info
*,
1144 const struct attribute
*,
1145 struct dwarf2_cu
**);
1147 static struct die_info
*follow_die_sig (struct die_info
*,
1148 const struct attribute
*,
1149 struct dwarf2_cu
**);
1151 static struct type
*get_signatured_type (struct die_info
*, ULONGEST
,
1152 struct dwarf2_cu
*);
1154 static struct type
*get_DW_AT_signature_type (struct die_info
*,
1155 const struct attribute
*,
1156 struct dwarf2_cu
*);
1158 static void load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
1159 dwarf2_per_objfile
*per_objfile
);
1161 static void read_signatured_type (signatured_type
*sig_type
,
1162 dwarf2_per_objfile
*per_objfile
);
1164 static int attr_to_dynamic_prop (const struct attribute
*attr
,
1165 struct die_info
*die
, struct dwarf2_cu
*cu
,
1166 struct dynamic_prop
*prop
, struct type
*type
);
1168 /* memory allocation interface */
1170 static struct dwarf_block
*dwarf_alloc_block (struct dwarf2_cu
*);
1172 static struct die_info
*dwarf_alloc_die (struct dwarf2_cu
*, int);
1174 static void dwarf_decode_macros (struct dwarf2_cu
*, unsigned int, int);
1176 static void fill_in_loclist_baton (struct dwarf2_cu
*cu
,
1177 struct dwarf2_loclist_baton
*baton
,
1178 const struct attribute
*attr
);
1180 static void dwarf2_symbol_mark_computed (const struct attribute
*attr
,
1182 struct dwarf2_cu
*cu
,
1185 static const gdb_byte
*skip_one_die (const struct die_reader_specs
*reader
,
1186 const gdb_byte
*info_ptr
,
1187 const struct abbrev_info
*abbrev
,
1188 bool do_skip_children
= true);
1190 static struct dwarf2_per_cu_data
*dwarf2_find_containing_comp_unit
1191 (sect_offset sect_off
, unsigned int offset_in_dwz
,
1192 dwarf2_per_bfd
*per_bfd
);
1194 static void prepare_one_comp_unit (struct dwarf2_cu
*cu
,
1195 struct die_info
*comp_unit_die
,
1196 enum language pretend_language
);
1198 static struct type
*set_die_type (struct die_info
*, struct type
*,
1199 struct dwarf2_cu
*, bool = false);
1201 static void create_all_comp_units (dwarf2_per_objfile
*per_objfile
);
1203 static void load_full_comp_unit (dwarf2_per_cu_data
*per_cu
,
1204 dwarf2_per_objfile
*per_objfile
,
1205 dwarf2_cu
*existing_cu
,
1207 enum language pretend_language
);
1209 static void process_full_comp_unit (dwarf2_cu
*cu
,
1210 enum language pretend_language
);
1212 static void process_full_type_unit (dwarf2_cu
*cu
,
1213 enum language pretend_language
);
1215 static struct type
*get_die_type_at_offset (sect_offset
,
1216 dwarf2_per_cu_data
*per_cu
,
1217 dwarf2_per_objfile
*per_objfile
);
1219 static struct type
*get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
);
1221 static void queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
1222 dwarf2_per_objfile
*per_objfile
,
1223 enum language pretend_language
);
1225 static void process_queue (dwarf2_per_objfile
*per_objfile
);
1227 /* Class, the destructor of which frees all allocated queue entries. This
1228 will only have work to do if an error was thrown while processing the
1229 dwarf. If no error was thrown then the queue entries should have all
1230 been processed, and freed, as we went along. */
1232 class dwarf2_queue_guard
1235 explicit dwarf2_queue_guard (dwarf2_per_objfile
*per_objfile
)
1236 : m_per_objfile (per_objfile
)
1238 gdb_assert (!m_per_objfile
->queue
.has_value ());
1240 m_per_objfile
->queue
.emplace ();
1243 /* Free any entries remaining on the queue. There should only be
1244 entries left if we hit an error while processing the dwarf. */
1245 ~dwarf2_queue_guard ()
1247 gdb_assert (m_per_objfile
->queue
.has_value ());
1249 m_per_objfile
->queue
.reset ();
1252 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard
);
1255 dwarf2_per_objfile
*m_per_objfile
;
1258 dwarf2_queue_item::~dwarf2_queue_item ()
1260 /* Anything still marked queued is likely to be in an
1261 inconsistent state, so discard it. */
1264 per_objfile
->remove_cu (per_cu
);
1269 /* See dwarf2/read.h. */
1272 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data
*data
)
1274 if (data
->is_debug_types
)
1275 delete static_cast<signatured_type
*> (data
);
1280 static file_and_directory
&find_file_and_directory
1281 (struct die_info
*die
, struct dwarf2_cu
*cu
);
1283 static const char *compute_include_file_name
1284 (const struct line_header
*lh
,
1285 const file_entry
&fe
,
1286 const file_and_directory
&cu_info
,
1287 std::string
&name_holder
);
1289 static htab_up
allocate_signatured_type_table ();
1291 static htab_up
allocate_dwo_unit_table ();
1293 static struct dwo_unit
*lookup_dwo_unit_in_dwp
1294 (dwarf2_per_objfile
*per_objfile
, struct dwp_file
*dwp_file
,
1295 const char *comp_dir
, ULONGEST signature
, int is_debug_types
);
1297 static struct dwp_file
*get_dwp_file (dwarf2_per_objfile
*per_objfile
);
1299 static struct dwo_unit
*lookup_dwo_comp_unit
1300 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
1301 ULONGEST signature
);
1303 static struct dwo_unit
*lookup_dwo_type_unit
1304 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
);
1306 static void queue_and_load_all_dwo_tus (dwarf2_cu
*cu
);
1308 /* A unique pointer to a dwo_file. */
1310 typedef std::unique_ptr
<struct dwo_file
> dwo_file_up
;
1312 static void process_cu_includes (dwarf2_per_objfile
*per_objfile
);
1314 static void check_producer (struct dwarf2_cu
*cu
);
1316 /* Various complaints about symbol reading that don't abort the process. */
1319 dwarf2_debug_line_missing_file_complaint (void)
1321 complaint (_(".debug_line section has line data without a file"));
1325 dwarf2_debug_line_missing_end_sequence_complaint (void)
1327 complaint (_(".debug_line section has line "
1328 "program sequence without an end"));
1332 dwarf2_complex_location_expr_complaint (void)
1334 complaint (_("location expression too complex"));
1338 dwarf2_const_value_length_mismatch_complaint (const char *arg1
, int arg2
,
1341 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1346 dwarf2_invalid_attrib_class_complaint (const char *arg1
, const char *arg2
)
1348 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1352 /* Hash function for line_header_hash. */
1355 line_header_hash (const struct line_header
*ofs
)
1357 return to_underlying (ofs
->sect_off
) ^ ofs
->offset_in_dwz
;
1360 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1363 line_header_hash_voidp (const void *item
)
1365 const struct line_header
*ofs
= (const struct line_header
*) item
;
1367 return line_header_hash (ofs
);
1370 /* Equality function for line_header_hash. */
1373 line_header_eq_voidp (const void *item_lhs
, const void *item_rhs
)
1375 const struct line_header
*ofs_lhs
= (const struct line_header
*) item_lhs
;
1376 const struct line_header
*ofs_rhs
= (const struct line_header
*) item_rhs
;
1378 return (ofs_lhs
->sect_off
== ofs_rhs
->sect_off
1379 && ofs_lhs
->offset_in_dwz
== ofs_rhs
->offset_in_dwz
);
1384 /* An iterator for all_comp_units that is based on index. This
1385 approach makes it possible to iterate over all_comp_units safely,
1386 when some caller in the loop may add new units. */
1388 class all_comp_units_iterator
1392 all_comp_units_iterator (dwarf2_per_bfd
*per_bfd
, bool start
)
1393 : m_per_bfd (per_bfd
),
1394 m_index (start
? 0 : per_bfd
->all_comp_units
.size ())
1398 all_comp_units_iterator
&operator++ ()
1404 dwarf2_per_cu_data
*operator* () const
1406 return m_per_bfd
->get_cu (m_index
);
1409 bool operator== (const all_comp_units_iterator
&other
) const
1411 return m_index
== other
.m_index
;
1415 bool operator!= (const all_comp_units_iterator
&other
) const
1417 return m_index
!= other
.m_index
;
1422 dwarf2_per_bfd
*m_per_bfd
;
1426 /* A range adapter for the all_comp_units_iterator. */
1427 class all_comp_units_range
1431 all_comp_units_range (dwarf2_per_bfd
*per_bfd
)
1432 : m_per_bfd (per_bfd
)
1436 all_comp_units_iterator
begin ()
1438 return all_comp_units_iterator (m_per_bfd
, true);
1441 all_comp_units_iterator
end ()
1443 return all_comp_units_iterator (m_per_bfd
, false);
1448 dwarf2_per_bfd
*m_per_bfd
;
1451 /* See declaration. */
1453 dwarf2_per_bfd::dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
,
1456 can_copy (can_copy_
)
1459 names
= &dwarf2_elf_names
;
1461 for (asection
*sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
1462 locate_sections (obfd
, sec
, *names
);
1465 dwarf2_per_bfd::~dwarf2_per_bfd ()
1467 for (auto &per_cu
: all_comp_units
)
1469 per_cu
->imported_symtabs_free ();
1470 per_cu
->free_cached_file_names ();
1473 /* Everything else should be on this->obstack. */
1479 dwarf2_per_objfile::remove_all_cus ()
1481 gdb_assert (!queue
.has_value ());
1483 m_dwarf2_cus
.clear ();
1486 /* A helper class that calls free_cached_comp_units on
1489 class free_cached_comp_units
1493 explicit free_cached_comp_units (dwarf2_per_objfile
*per_objfile
)
1494 : m_per_objfile (per_objfile
)
1498 ~free_cached_comp_units ()
1500 m_per_objfile
->remove_all_cus ();
1503 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units
);
1507 dwarf2_per_objfile
*m_per_objfile
;
1513 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const
1515 if (per_cu
->index
< this->m_symtabs
.size ())
1516 return this->m_symtabs
[per_cu
->index
] != nullptr;
1523 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data
*per_cu
) const
1525 if (per_cu
->index
< this->m_symtabs
.size ())
1526 return this->m_symtabs
[per_cu
->index
];
1533 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data
*per_cu
,
1534 compunit_symtab
*symtab
)
1536 if (per_cu
->index
>= this->m_symtabs
.size ())
1537 this->m_symtabs
.resize (per_cu
->index
+ 1);
1538 gdb_assert (this->m_symtabs
[per_cu
->index
] == nullptr);
1539 this->m_symtabs
[per_cu
->index
] = symtab
;
1542 /* Try to locate the sections we need for DWARF 2 debugging
1543 information and return true if we have enough to do something.
1544 NAMES points to the dwarf2 section names, or is NULL if the standard
1545 ELF names are used. CAN_COPY is true for formats where symbol
1546 interposition is possible and so symbol values must follow copy
1547 relocation rules. */
1550 dwarf2_has_info (struct objfile
*objfile
,
1551 const struct dwarf2_debug_sections
*names
,
1554 if (objfile
->flags
& OBJF_READNEVER
)
1557 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1559 if (per_objfile
== NULL
)
1561 dwarf2_per_bfd
*per_bfd
;
1563 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1564 BFD doesn't require relocations.
1566 We don't share with objfiles for which -readnow was requested,
1567 because it would complicate things when loading the same BFD with
1568 -readnow and then without -readnow. */
1569 if (!gdb_bfd_requires_relocations (objfile
->obfd
)
1570 && (objfile
->flags
& OBJF_READNOW
) == 0)
1572 /* See if one has been created for this BFD yet. */
1573 per_bfd
= dwarf2_per_bfd_bfd_data_key
.get (objfile
->obfd
);
1575 if (per_bfd
== nullptr)
1577 /* No, create it now. */
1578 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
, names
, can_copy
);
1579 dwarf2_per_bfd_bfd_data_key
.set (objfile
->obfd
, per_bfd
);
1584 /* No sharing possible, create one specifically for this objfile. */
1585 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
, names
, can_copy
);
1586 dwarf2_per_bfd_objfile_data_key
.set (objfile
, per_bfd
);
1589 per_objfile
= dwarf2_objfile_data_key
.emplace (objfile
, objfile
, per_bfd
);
1592 return (!per_objfile
->per_bfd
->info
.is_virtual
1593 && per_objfile
->per_bfd
->info
.s
.section
!= NULL
1594 && !per_objfile
->per_bfd
->abbrev
.is_virtual
1595 && per_objfile
->per_bfd
->abbrev
.s
.section
!= NULL
);
1598 /* See declaration. */
1601 dwarf2_per_bfd::locate_sections (bfd
*abfd
, asection
*sectp
,
1602 const dwarf2_debug_sections
&names
)
1604 flagword aflag
= bfd_section_flags (sectp
);
1606 if ((aflag
& SEC_HAS_CONTENTS
) == 0)
1609 else if (elf_section_data (sectp
)->this_hdr
.sh_size
1610 > bfd_get_file_size (abfd
))
1612 bfd_size_type size
= elf_section_data (sectp
)->this_hdr
.sh_size
;
1613 warning (_("Discarding section %s which has a section size (%s"
1614 ") larger than the file size [in module %s]"),
1615 bfd_section_name (sectp
), phex_nz (size
, sizeof (size
)),
1616 bfd_get_filename (abfd
));
1618 else if (names
.info
.matches (sectp
->name
))
1620 this->info
.s
.section
= sectp
;
1621 this->info
.size
= bfd_section_size (sectp
);
1623 else if (names
.abbrev
.matches (sectp
->name
))
1625 this->abbrev
.s
.section
= sectp
;
1626 this->abbrev
.size
= bfd_section_size (sectp
);
1628 else if (names
.line
.matches (sectp
->name
))
1630 this->line
.s
.section
= sectp
;
1631 this->line
.size
= bfd_section_size (sectp
);
1633 else if (names
.loc
.matches (sectp
->name
))
1635 this->loc
.s
.section
= sectp
;
1636 this->loc
.size
= bfd_section_size (sectp
);
1638 else if (names
.loclists
.matches (sectp
->name
))
1640 this->loclists
.s
.section
= sectp
;
1641 this->loclists
.size
= bfd_section_size (sectp
);
1643 else if (names
.macinfo
.matches (sectp
->name
))
1645 this->macinfo
.s
.section
= sectp
;
1646 this->macinfo
.size
= bfd_section_size (sectp
);
1648 else if (names
.macro
.matches (sectp
->name
))
1650 this->macro
.s
.section
= sectp
;
1651 this->macro
.size
= bfd_section_size (sectp
);
1653 else if (names
.str
.matches (sectp
->name
))
1655 this->str
.s
.section
= sectp
;
1656 this->str
.size
= bfd_section_size (sectp
);
1658 else if (names
.str_offsets
.matches (sectp
->name
))
1660 this->str_offsets
.s
.section
= sectp
;
1661 this->str_offsets
.size
= bfd_section_size (sectp
);
1663 else if (names
.line_str
.matches (sectp
->name
))
1665 this->line_str
.s
.section
= sectp
;
1666 this->line_str
.size
= bfd_section_size (sectp
);
1668 else if (names
.addr
.matches (sectp
->name
))
1670 this->addr
.s
.section
= sectp
;
1671 this->addr
.size
= bfd_section_size (sectp
);
1673 else if (names
.frame
.matches (sectp
->name
))
1675 this->frame
.s
.section
= sectp
;
1676 this->frame
.size
= bfd_section_size (sectp
);
1678 else if (names
.eh_frame
.matches (sectp
->name
))
1680 this->eh_frame
.s
.section
= sectp
;
1681 this->eh_frame
.size
= bfd_section_size (sectp
);
1683 else if (names
.ranges
.matches (sectp
->name
))
1685 this->ranges
.s
.section
= sectp
;
1686 this->ranges
.size
= bfd_section_size (sectp
);
1688 else if (names
.rnglists
.matches (sectp
->name
))
1690 this->rnglists
.s
.section
= sectp
;
1691 this->rnglists
.size
= bfd_section_size (sectp
);
1693 else if (names
.types
.matches (sectp
->name
))
1695 struct dwarf2_section_info type_section
;
1697 memset (&type_section
, 0, sizeof (type_section
));
1698 type_section
.s
.section
= sectp
;
1699 type_section
.size
= bfd_section_size (sectp
);
1701 this->types
.push_back (type_section
);
1703 else if (names
.gdb_index
.matches (sectp
->name
))
1705 this->gdb_index
.s
.section
= sectp
;
1706 this->gdb_index
.size
= bfd_section_size (sectp
);
1708 else if (names
.debug_names
.matches (sectp
->name
))
1710 this->debug_names
.s
.section
= sectp
;
1711 this->debug_names
.size
= bfd_section_size (sectp
);
1713 else if (names
.debug_aranges
.matches (sectp
->name
))
1715 this->debug_aranges
.s
.section
= sectp
;
1716 this->debug_aranges
.size
= bfd_section_size (sectp
);
1719 if ((bfd_section_flags (sectp
) & (SEC_LOAD
| SEC_ALLOC
))
1720 && bfd_section_vma (sectp
) == 0)
1721 this->has_section_at_zero
= true;
1724 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1728 dwarf2_get_section_info (struct objfile
*objfile
,
1729 enum dwarf2_section_enum sect
,
1730 asection
**sectp
, const gdb_byte
**bufp
,
1731 bfd_size_type
*sizep
)
1733 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1734 struct dwarf2_section_info
*info
;
1736 /* We may see an objfile without any DWARF, in which case we just
1738 if (per_objfile
== NULL
)
1747 case DWARF2_DEBUG_FRAME
:
1748 info
= &per_objfile
->per_bfd
->frame
;
1750 case DWARF2_EH_FRAME
:
1751 info
= &per_objfile
->per_bfd
->eh_frame
;
1754 gdb_assert_not_reached ("unexpected section");
1757 info
->read (objfile
);
1759 *sectp
= info
->get_bfd_section ();
1760 *bufp
= info
->buffer
;
1761 *sizep
= info
->size
;
1764 /* See dwarf2/read.h. */
1767 dwarf2_per_bfd::map_info_sections (struct objfile
*objfile
)
1769 info
.read (objfile
);
1770 abbrev
.read (objfile
);
1771 line
.read (objfile
);
1773 str_offsets
.read (objfile
);
1774 line_str
.read (objfile
);
1775 ranges
.read (objfile
);
1776 rnglists
.read (objfile
);
1777 addr
.read (objfile
);
1779 for (auto §ion
: types
)
1780 section
.read (objfile
);
1784 /* DWARF quick_symbol_functions support. */
1786 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1787 unique line tables, so we maintain a separate table of all .debug_line
1788 derived entries to support the sharing.
1789 All the quick functions need is the list of file names. We discard the
1790 line_header when we're done and don't need to record it here. */
1791 struct quick_file_names
1793 /* The data used to construct the hash key. */
1794 struct stmt_list_hash hash
;
1796 /* The number of entries in file_names, real_names. */
1797 unsigned int num_file_names
;
1799 /* The CU directory, as given by DW_AT_comp_dir. May be
1801 const char *comp_dir
;
1803 /* The file names from the line table, after being run through
1805 const char **file_names
;
1807 /* The file names from the line table after being run through
1808 gdb_realpath. These are computed lazily. */
1809 const char **real_names
;
1812 struct dwarf2_base_index_functions
: public quick_symbol_functions
1814 bool has_symbols (struct objfile
*objfile
) override
;
1816 bool has_unexpanded_symtabs (struct objfile
*objfile
) override
;
1818 struct symtab
*find_last_source_symtab (struct objfile
*objfile
) override
;
1820 void forget_cached_source_info (struct objfile
*objfile
) override
;
1822 enum language
lookup_global_symbol_language (struct objfile
*objfile
,
1825 bool *symbol_found_p
) override
1827 *symbol_found_p
= false;
1828 return language_unknown
;
1831 void print_stats (struct objfile
*objfile
, bool print_bcache
) override
;
1833 void expand_all_symtabs (struct objfile
*objfile
) override
;
1835 /* A helper function that finds the per-cu object from an "adjusted"
1836 PC -- a PC with the base text offset removed. */
1837 virtual dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
1838 CORE_ADDR adjusted_pc
);
1840 struct compunit_symtab
*find_pc_sect_compunit_symtab
1841 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
1842 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
)
1845 struct compunit_symtab
*find_compunit_symtab_by_address
1846 (struct objfile
*objfile
, CORE_ADDR address
) override
1851 void map_symbol_filenames (struct objfile
*objfile
,
1852 gdb::function_view
<symbol_filename_ftype
> fun
,
1853 bool need_fullname
) override
;
1856 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1857 It's handy in this case to have an empty implementation of the
1858 quick symbol functions, to avoid special cases in the rest of the
1861 struct readnow_functions
: public dwarf2_base_index_functions
1863 void dump (struct objfile
*objfile
) override
1867 void expand_matching_symbols
1869 const lookup_name_info
&lookup_name
,
1872 symbol_compare_ftype
*ordered_compare
) override
1876 bool expand_symtabs_matching
1877 (struct objfile
*objfile
,
1878 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1879 const lookup_name_info
*lookup_name
,
1880 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1881 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1882 block_search_flags search_flags
,
1884 enum search_domain kind
) override
1890 struct dwarf2_gdb_index
: public dwarf2_base_index_functions
1892 void dump (struct objfile
*objfile
) override
;
1894 void expand_matching_symbols
1896 const lookup_name_info
&lookup_name
,
1899 symbol_compare_ftype
*ordered_compare
) override
;
1901 bool expand_symtabs_matching
1902 (struct objfile
*objfile
,
1903 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1904 const lookup_name_info
*lookup_name
,
1905 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1906 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1907 block_search_flags search_flags
,
1909 enum search_domain kind
) override
;
1912 struct dwarf2_debug_names_index
: public dwarf2_base_index_functions
1914 void dump (struct objfile
*objfile
) override
;
1916 void expand_matching_symbols
1918 const lookup_name_info
&lookup_name
,
1921 symbol_compare_ftype
*ordered_compare
) override
;
1923 bool expand_symtabs_matching
1924 (struct objfile
*objfile
,
1925 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1926 const lookup_name_info
*lookup_name
,
1927 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1928 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1929 block_search_flags search_flags
,
1931 enum search_domain kind
) override
;
1934 quick_symbol_functions_up
1935 mapped_index::make_quick_functions () const
1937 return quick_symbol_functions_up (new dwarf2_gdb_index
);
1940 quick_symbol_functions_up
1941 mapped_debug_names::make_quick_functions () const
1943 return quick_symbol_functions_up (new dwarf2_debug_names_index
);
1946 /* Utility hash function for a stmt_list_hash. */
1949 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1953 if (stmt_list_hash
->dwo_unit
!= NULL
)
1954 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1955 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1959 /* Utility equality function for a stmt_list_hash. */
1962 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1963 const struct stmt_list_hash
*rhs
)
1965 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1967 if (lhs
->dwo_unit
!= NULL
1968 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1971 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1974 /* Hash function for a quick_file_names. */
1977 hash_file_name_entry (const void *e
)
1979 const struct quick_file_names
*file_data
1980 = (const struct quick_file_names
*) e
;
1982 return hash_stmt_list_entry (&file_data
->hash
);
1985 /* Equality function for a quick_file_names. */
1988 eq_file_name_entry (const void *a
, const void *b
)
1990 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
1991 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
1993 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
1996 /* Create a quick_file_names hash table. */
1999 create_quick_file_names_table (unsigned int nr_initial_entries
)
2001 return htab_up (htab_create_alloc (nr_initial_entries
,
2002 hash_file_name_entry
, eq_file_name_entry
,
2003 nullptr, xcalloc
, xfree
));
2006 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2007 function is unrelated to symtabs, symtab would have to be created afterwards.
2008 You should call age_cached_comp_units after processing the CU. */
2011 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
2014 if (per_cu
->is_debug_types
)
2015 load_full_type_unit (per_cu
, per_objfile
);
2017 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
2018 skip_partial
, language_minimal
);
2020 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
2022 return nullptr; /* Dummy CU. */
2024 dwarf2_find_base_address (cu
->dies
, cu
);
2029 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2032 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2033 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
2036 /* The destructor of dwarf2_queue_guard frees any entries left on
2037 the queue. After this point we're guaranteed to leave this function
2038 with the dwarf queue empty. */
2039 dwarf2_queue_guard
q_guard (per_objfile
);
2041 if (!per_objfile
->symtab_set_p (per_cu
))
2043 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
2044 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
2046 /* If we just loaded a CU from a DWO, and we're working with an index
2047 that may badly handle TUs, load all the TUs in that DWO as well.
2048 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2049 if (!per_cu
->is_debug_types
2051 && cu
->dwo_unit
!= NULL
2052 && per_objfile
->per_bfd
->index_table
!= NULL
2053 && !per_objfile
->per_bfd
->index_table
->version_check ()
2054 /* DWP files aren't supported yet. */
2055 && get_dwp_file (per_objfile
) == NULL
)
2056 queue_and_load_all_dwo_tus (cu
);
2059 process_queue (per_objfile
);
2062 /* Age the cache, releasing compilation units that have not
2063 been used recently. */
2064 per_objfile
->age_comp_units ();
2067 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2068 the per-objfile for which this symtab is instantiated.
2070 Returns the resulting symbol table. */
2072 static struct compunit_symtab
*
2073 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2074 dwarf2_per_objfile
*per_objfile
,
2077 if (!per_objfile
->symtab_set_p (per_cu
))
2079 free_cached_comp_units
freer (per_objfile
);
2080 scoped_restore decrementer
= increment_reading_symtab ();
2081 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
2082 process_cu_includes (per_objfile
);
2085 return per_objfile
->get_symtab (per_cu
);
2090 dwarf2_per_cu_data_up
2091 dwarf2_per_bfd::allocate_per_cu ()
2093 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
2094 result
->per_bfd
= this;
2095 result
->index
= all_comp_units
.size ();
2102 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
2104 signatured_type_up
result (new signatured_type (signature
));
2105 result
->per_bfd
= this;
2106 result
->index
= all_comp_units
.size ();
2107 result
->is_debug_types
= true;
2112 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2113 obstack, and constructed with the specified field values. */
2115 static dwarf2_per_cu_data_up
2116 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
2117 struct dwarf2_section_info
*section
,
2119 sect_offset sect_off
, ULONGEST length
)
2121 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
2122 the_cu
->sect_off
= sect_off
;
2123 the_cu
->set_length (length
);
2124 the_cu
->section
= section
;
2125 the_cu
->is_dwz
= is_dwz
;
2129 /* A helper for create_cus_from_index that handles a given list of
2133 create_cus_from_index_list (dwarf2_per_bfd
*per_bfd
,
2134 const gdb_byte
*cu_list
, offset_type n_elements
,
2135 struct dwarf2_section_info
*section
,
2138 for (offset_type i
= 0; i
< n_elements
; i
+= 2)
2140 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2142 sect_offset sect_off
2143 = (sect_offset
) extract_unsigned_integer (cu_list
, 8, BFD_ENDIAN_LITTLE
);
2144 ULONGEST length
= extract_unsigned_integer (cu_list
+ 8, 8, BFD_ENDIAN_LITTLE
);
2147 dwarf2_per_cu_data_up per_cu
2148 = create_cu_from_index_list (per_bfd
, section
, is_dwz
, sect_off
,
2150 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
2154 /* Read the CU list from the mapped index, and use it to create all
2155 the CU objects for PER_BFD. */
2158 create_cus_from_index (dwarf2_per_bfd
*per_bfd
,
2159 const gdb_byte
*cu_list
, offset_type cu_list_elements
,
2160 const gdb_byte
*dwz_list
, offset_type dwz_elements
)
2162 gdb_assert (per_bfd
->all_comp_units
.empty ());
2163 per_bfd
->all_comp_units
.reserve ((cu_list_elements
+ dwz_elements
) / 2);
2165 create_cus_from_index_list (per_bfd
, cu_list
, cu_list_elements
,
2168 if (dwz_elements
== 0)
2171 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
2172 create_cus_from_index_list (per_bfd
, dwz_list
, dwz_elements
,
2176 /* Create the signatured type hash table from the index. */
2179 create_signatured_type_table_from_index
2180 (dwarf2_per_bfd
*per_bfd
, struct dwarf2_section_info
*section
,
2181 const gdb_byte
*bytes
, offset_type elements
)
2183 htab_up sig_types_hash
= allocate_signatured_type_table ();
2185 for (offset_type i
= 0; i
< elements
; i
+= 3)
2187 signatured_type_up sig_type
;
2190 cu_offset type_offset_in_tu
;
2192 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2193 sect_offset sect_off
2194 = (sect_offset
) extract_unsigned_integer (bytes
, 8, BFD_ENDIAN_LITTLE
);
2196 = (cu_offset
) extract_unsigned_integer (bytes
+ 8, 8,
2198 signature
= extract_unsigned_integer (bytes
+ 16, 8, BFD_ENDIAN_LITTLE
);
2201 sig_type
= per_bfd
->allocate_signatured_type (signature
);
2202 sig_type
->type_offset_in_tu
= type_offset_in_tu
;
2203 sig_type
->section
= section
;
2204 sig_type
->sect_off
= sect_off
;
2206 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2207 *slot
= sig_type
.get ();
2209 per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2212 per_bfd
->signatured_types
= std::move (sig_types_hash
);
2215 /* Create the signatured type hash table from .debug_names. */
2218 create_signatured_type_table_from_debug_names
2219 (dwarf2_per_objfile
*per_objfile
,
2220 const mapped_debug_names
&map
,
2221 struct dwarf2_section_info
*section
,
2222 struct dwarf2_section_info
*abbrev_section
)
2224 struct objfile
*objfile
= per_objfile
->objfile
;
2226 section
->read (objfile
);
2227 abbrev_section
->read (objfile
);
2229 htab_up sig_types_hash
= allocate_signatured_type_table ();
2231 for (uint32_t i
= 0; i
< map
.tu_count
; ++i
)
2233 signatured_type_up sig_type
;
2236 sect_offset sect_off
2237 = (sect_offset
) (extract_unsigned_integer
2238 (map
.tu_table_reordered
+ i
* map
.offset_size
,
2240 map
.dwarf5_byte_order
));
2242 comp_unit_head cu_header
;
2243 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
2245 section
->buffer
+ to_underlying (sect_off
),
2248 sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
2249 (cu_header
.signature
);
2250 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
2251 sig_type
->section
= section
;
2252 sig_type
->sect_off
= sect_off
;
2254 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2255 *slot
= sig_type
.get ();
2257 per_objfile
->per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2260 per_objfile
->per_bfd
->signatured_types
= std::move (sig_types_hash
);
2263 /* Read the address map data from the mapped index, and use it to
2264 populate the index_addrmap. */
2267 create_addrmap_from_index (dwarf2_per_objfile
*per_objfile
,
2268 struct mapped_index
*index
)
2270 struct objfile
*objfile
= per_objfile
->objfile
;
2271 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2272 struct gdbarch
*gdbarch
= objfile
->arch ();
2273 const gdb_byte
*iter
, *end
;
2276 addrmap_mutable mutable_map
;
2278 iter
= index
->address_table
.data ();
2279 end
= iter
+ index
->address_table
.size ();
2281 baseaddr
= objfile
->text_section_offset ();
2285 ULONGEST hi
, lo
, cu_index
;
2286 lo
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2288 hi
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2290 cu_index
= extract_unsigned_integer (iter
, 4, BFD_ENDIAN_LITTLE
);
2295 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2296 hex_string (lo
), hex_string (hi
));
2300 if (cu_index
>= per_bfd
->all_comp_units
.size ())
2302 complaint (_(".gdb_index address table has invalid CU number %u"),
2303 (unsigned) cu_index
);
2307 lo
= gdbarch_adjust_dwarf2_addr (gdbarch
, lo
+ baseaddr
) - baseaddr
;
2308 hi
= gdbarch_adjust_dwarf2_addr (gdbarch
, hi
+ baseaddr
) - baseaddr
;
2309 mutable_map
.set_empty (lo
, hi
- 1, per_bfd
->get_cu (cu_index
));
2312 per_bfd
->index_addrmap
2313 = new (&per_bfd
->obstack
) addrmap_fixed (&per_bfd
->obstack
, &mutable_map
);
2316 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2317 to populate given addrmap. Returns true on success, false on
2321 read_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2322 struct dwarf2_section_info
*section
,
2323 addrmap
*mutable_map
)
2325 struct objfile
*objfile
= per_objfile
->objfile
;
2326 bfd
*abfd
= objfile
->obfd
;
2327 struct gdbarch
*gdbarch
= objfile
->arch ();
2328 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
2329 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2331 std::unordered_map
<sect_offset
,
2332 dwarf2_per_cu_data
*,
2333 gdb::hash_enum
<sect_offset
>>
2334 debug_info_offset_to_per_cu
;
2335 for (const auto &per_cu
: per_bfd
->all_comp_units
)
2337 /* A TU will not need aranges, and skipping them here is an easy
2338 way of ignoring .debug_types -- and possibly seeing a
2339 duplicate section offset -- entirely. The same applies to
2340 units coming from a dwz file. */
2341 if (per_cu
->is_debug_types
|| per_cu
->is_dwz
)
2344 const auto insertpair
2345 = debug_info_offset_to_per_cu
.emplace (per_cu
->sect_off
,
2347 if (!insertpair
.second
)
2349 warning (_("Section .debug_aranges in %s has duplicate "
2350 "debug_info_offset %s, ignoring .debug_aranges."),
2351 objfile_name (objfile
), sect_offset_str (per_cu
->sect_off
));
2356 section
->read (objfile
);
2358 const bfd_endian dwarf5_byte_order
= gdbarch_byte_order (gdbarch
);
2360 const gdb_byte
*addr
= section
->buffer
;
2362 while (addr
< section
->buffer
+ section
->size
)
2364 const gdb_byte
*const entry_addr
= addr
;
2365 unsigned int bytes_read
;
2367 const LONGEST entry_length
= read_initial_length (abfd
, addr
,
2371 const gdb_byte
*const entry_end
= addr
+ entry_length
;
2372 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
2373 const uint8_t offset_size
= dwarf5_is_dwarf64
? 8 : 4;
2374 if (addr
+ entry_length
> section
->buffer
+ section
->size
)
2376 warning (_("Section .debug_aranges in %s entry at offset %s "
2377 "length %s exceeds section length %s, "
2378 "ignoring .debug_aranges."),
2379 objfile_name (objfile
),
2380 plongest (entry_addr
- section
->buffer
),
2381 plongest (bytes_read
+ entry_length
),
2382 pulongest (section
->size
));
2386 /* The version number. */
2387 const uint16_t version
= read_2_bytes (abfd
, addr
);
2391 warning (_("Section .debug_aranges in %s entry at offset %s "
2392 "has unsupported version %d, ignoring .debug_aranges."),
2393 objfile_name (objfile
),
2394 plongest (entry_addr
- section
->buffer
), version
);
2398 const uint64_t debug_info_offset
2399 = extract_unsigned_integer (addr
, offset_size
, dwarf5_byte_order
);
2400 addr
+= offset_size
;
2401 const auto per_cu_it
2402 = debug_info_offset_to_per_cu
.find (sect_offset (debug_info_offset
));
2403 if (per_cu_it
== debug_info_offset_to_per_cu
.cend ())
2405 warning (_("Section .debug_aranges in %s entry at offset %s "
2406 "debug_info_offset %s does not exists, "
2407 "ignoring .debug_aranges."),
2408 objfile_name (objfile
),
2409 plongest (entry_addr
- section
->buffer
),
2410 pulongest (debug_info_offset
));
2413 dwarf2_per_cu_data
*const per_cu
= per_cu_it
->second
;
2415 const uint8_t address_size
= *addr
++;
2416 if (address_size
< 1 || address_size
> 8)
2418 warning (_("Section .debug_aranges in %s entry at offset %s "
2419 "address_size %u is invalid, ignoring .debug_aranges."),
2420 objfile_name (objfile
),
2421 plongest (entry_addr
- section
->buffer
), address_size
);
2425 const uint8_t segment_selector_size
= *addr
++;
2426 if (segment_selector_size
!= 0)
2428 warning (_("Section .debug_aranges in %s entry at offset %s "
2429 "segment_selector_size %u is not supported, "
2430 "ignoring .debug_aranges."),
2431 objfile_name (objfile
),
2432 plongest (entry_addr
- section
->buffer
),
2433 segment_selector_size
);
2437 /* Must pad to an alignment boundary that is twice the address
2438 size. It is undocumented by the DWARF standard but GCC does
2439 use it. However, not every compiler does this. We can see
2440 whether it has happened by looking at the total length of the
2441 contents of the aranges for this CU -- it if isn't a multiple
2442 of twice the address size, then we skip any leftover
2444 addr
+= (entry_end
- addr
) % (2 * address_size
);
2446 while (addr
< entry_end
)
2448 if (addr
+ 2 * address_size
> entry_end
)
2450 warning (_("Section .debug_aranges in %s entry at offset %s "
2451 "address list is not properly terminated, "
2452 "ignoring .debug_aranges."),
2453 objfile_name (objfile
),
2454 plongest (entry_addr
- section
->buffer
));
2457 ULONGEST start
= extract_unsigned_integer (addr
, address_size
,
2459 addr
+= address_size
;
2460 ULONGEST length
= extract_unsigned_integer (addr
, address_size
,
2462 addr
+= address_size
;
2463 if (start
== 0 && length
== 0)
2465 /* This can happen on some targets with --gc-sections.
2466 This pair of values is also used to mark the end of
2467 the entries for a given CU, but we ignore it and
2468 instead handle termination using the check at the top
2472 if (start
== 0 && !per_bfd
->has_section_at_zero
)
2474 /* Symbol was eliminated due to a COMDAT group. */
2477 ULONGEST end
= start
+ length
;
2478 start
= (gdbarch_adjust_dwarf2_addr (gdbarch
, start
+ baseaddr
)
2480 end
= (gdbarch_adjust_dwarf2_addr (gdbarch
, end
+ baseaddr
)
2482 mutable_map
->set_empty (start
, end
- 1, per_cu
);
2485 per_cu
->addresses_seen
= true;
2491 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2492 populate the index_addrmap. */
2495 create_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2496 struct dwarf2_section_info
*section
)
2498 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2500 addrmap_mutable mutable_map
;
2502 if (read_addrmap_from_aranges (per_objfile
, section
, &mutable_map
))
2503 per_bfd
->index_addrmap
2504 = new (&per_bfd
->obstack
) addrmap_fixed (&per_bfd
->obstack
,
2508 /* A helper function that reads the .gdb_index from BUFFER and fills
2509 in MAP. FILENAME is the name of the file containing the data;
2510 it is used for error reporting. DEPRECATED_OK is true if it is
2511 ok to use deprecated sections.
2513 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2514 out parameters that are filled in with information about the CU and
2515 TU lists in the section.
2517 Returns true if all went well, false otherwise. */
2520 read_gdb_index_from_buffer (const char *filename
,
2522 gdb::array_view
<const gdb_byte
> buffer
,
2523 struct mapped_index
*map
,
2524 const gdb_byte
**cu_list
,
2525 offset_type
*cu_list_elements
,
2526 const gdb_byte
**types_list
,
2527 offset_type
*types_list_elements
)
2529 const gdb_byte
*addr
= &buffer
[0];
2530 offset_view
metadata (buffer
);
2532 /* Version check. */
2533 offset_type version
= metadata
[0];
2534 /* Versions earlier than 3 emitted every copy of a psymbol. This
2535 causes the index to behave very poorly for certain requests. Version 3
2536 contained incomplete addrmap. So, it seems better to just ignore such
2540 static int warning_printed
= 0;
2541 if (!warning_printed
)
2543 warning (_("Skipping obsolete .gdb_index section in %s."),
2545 warning_printed
= 1;
2549 /* Index version 4 uses a different hash function than index version
2552 Versions earlier than 6 did not emit psymbols for inlined
2553 functions. Using these files will cause GDB not to be able to
2554 set breakpoints on inlined functions by name, so we ignore these
2555 indices unless the user has done
2556 "set use-deprecated-index-sections on". */
2557 if (version
< 6 && !deprecated_ok
)
2559 static int warning_printed
= 0;
2560 if (!warning_printed
)
2563 Skipping deprecated .gdb_index section in %s.\n\
2564 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2565 to use the section anyway."),
2567 warning_printed
= 1;
2571 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2572 of the TU (for symbols coming from TUs),
2573 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2574 Plus gold-generated indices can have duplicate entries for global symbols,
2575 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2576 These are just performance bugs, and we can't distinguish gdb-generated
2577 indices from gold-generated ones, so issue no warning here. */
2579 /* Indexes with higher version than the one supported by GDB may be no
2580 longer backward compatible. */
2584 map
->version
= version
;
2587 *cu_list
= addr
+ metadata
[i
];
2588 *cu_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2591 *types_list
= addr
+ metadata
[i
];
2592 *types_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2595 const gdb_byte
*address_table
= addr
+ metadata
[i
];
2596 const gdb_byte
*address_table_end
= addr
+ metadata
[i
+ 1];
2598 = gdb::array_view
<const gdb_byte
> (address_table
, address_table_end
);
2601 const gdb_byte
*symbol_table
= addr
+ metadata
[i
];
2602 const gdb_byte
*symbol_table_end
= addr
+ metadata
[i
+ 1];
2604 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2608 map
->constant_pool
= buffer
.slice (metadata
[i
]);
2610 if (map
->constant_pool
.empty () && !map
->symbol_table
.empty ())
2612 /* An empty constant pool implies that all symbol table entries are
2613 empty. Make map->symbol_table.empty () == true. */
2615 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2622 /* Callback types for dwarf2_read_gdb_index. */
2624 typedef gdb::function_view
2625 <gdb::array_view
<const gdb_byte
>(objfile
*, dwarf2_per_bfd
*)>
2626 get_gdb_index_contents_ftype
;
2627 typedef gdb::function_view
2628 <gdb::array_view
<const gdb_byte
>(objfile
*, dwz_file
*)>
2629 get_gdb_index_contents_dwz_ftype
;
2631 /* Read .gdb_index. If everything went ok, initialize the "quick"
2632 elements of all the CUs and return 1. Otherwise, return 0. */
2635 dwarf2_read_gdb_index
2636 (dwarf2_per_objfile
*per_objfile
,
2637 get_gdb_index_contents_ftype get_gdb_index_contents
,
2638 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz
)
2640 const gdb_byte
*cu_list
, *types_list
, *dwz_list
= NULL
;
2641 offset_type cu_list_elements
, types_list_elements
, dwz_list_elements
= 0;
2642 struct dwz_file
*dwz
;
2643 struct objfile
*objfile
= per_objfile
->objfile
;
2644 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2646 gdb::array_view
<const gdb_byte
> main_index_contents
2647 = get_gdb_index_contents (objfile
, per_bfd
);
2649 if (main_index_contents
.empty ())
2652 std::unique_ptr
<struct mapped_index
> map (new struct mapped_index
);
2653 if (!read_gdb_index_from_buffer (objfile_name (objfile
),
2654 use_deprecated_index_sections
,
2655 main_index_contents
, map
.get (), &cu_list
,
2656 &cu_list_elements
, &types_list
,
2657 &types_list_elements
))
2660 /* Don't use the index if it's empty. */
2661 if (map
->symbol_table
.empty ())
2664 /* If there is a .dwz file, read it so we can get its CU list as
2666 dwz
= dwarf2_get_dwz_file (per_bfd
);
2669 struct mapped_index dwz_map
;
2670 const gdb_byte
*dwz_types_ignore
;
2671 offset_type dwz_types_elements_ignore
;
2673 gdb::array_view
<const gdb_byte
> dwz_index_content
2674 = get_gdb_index_contents_dwz (objfile
, dwz
);
2676 if (dwz_index_content
.empty ())
2679 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz
->dwz_bfd
.get ()),
2680 1, dwz_index_content
, &dwz_map
,
2681 &dwz_list
, &dwz_list_elements
,
2683 &dwz_types_elements_ignore
))
2685 warning (_("could not read '.gdb_index' section from %s; skipping"),
2686 bfd_get_filename (dwz
->dwz_bfd
.get ()));
2691 create_cus_from_index (per_bfd
, cu_list
, cu_list_elements
, dwz_list
,
2694 if (types_list_elements
)
2696 /* We can only handle a single .debug_types when we have an
2698 if (per_bfd
->types
.size () != 1)
2700 per_bfd
->all_comp_units
.clear ();
2704 dwarf2_section_info
*section
= &per_bfd
->types
[0];
2706 create_signatured_type_table_from_index (per_bfd
, section
, types_list
,
2707 types_list_elements
);
2710 create_addrmap_from_index (per_objfile
, map
.get ());
2712 per_bfd
->index_table
= std::move (map
);
2713 per_bfd
->quick_file_names_table
=
2714 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
2719 /* die_reader_func for dw2_get_file_names. */
2722 dw2_get_file_names_reader (const struct die_reader_specs
*reader
,
2723 struct die_info
*comp_unit_die
)
2725 struct dwarf2_cu
*cu
= reader
->cu
;
2726 struct dwarf2_per_cu_data
*this_cu
= cu
->per_cu
;
2727 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
2728 struct dwarf2_per_cu_data
*lh_cu
;
2729 struct attribute
*attr
;
2731 struct quick_file_names
*qfn
;
2733 gdb_assert (! this_cu
->is_debug_types
);
2735 this_cu
->files_read
= true;
2736 /* Our callers never want to match partial units -- instead they
2737 will match the enclosing full CU. */
2738 if (comp_unit_die
->tag
== DW_TAG_partial_unit
)
2745 sect_offset line_offset
{};
2747 attr
= dwarf2_attr (comp_unit_die
, DW_AT_stmt_list
, cu
);
2748 if (attr
!= nullptr && attr
->form_is_unsigned ())
2750 struct quick_file_names find_entry
;
2752 line_offset
= (sect_offset
) attr
->as_unsigned ();
2754 /* We may have already read in this line header (TU line header sharing).
2755 If we have we're done. */
2756 find_entry
.hash
.dwo_unit
= cu
->dwo_unit
;
2757 find_entry
.hash
.line_sect_off
= line_offset
;
2758 slot
= htab_find_slot (per_objfile
->per_bfd
->quick_file_names_table
.get (),
2759 &find_entry
, INSERT
);
2762 lh_cu
->file_names
= (struct quick_file_names
*) *slot
;
2766 lh
= dwarf_decode_line_header (line_offset
, cu
);
2769 file_and_directory
&fnd
= find_file_and_directory (comp_unit_die
, cu
);
2772 if (!fnd
.is_unknown ())
2774 else if (lh
== nullptr)
2777 qfn
= XOBNEW (&per_objfile
->per_bfd
->obstack
, struct quick_file_names
);
2778 qfn
->hash
.dwo_unit
= cu
->dwo_unit
;
2779 qfn
->hash
.line_sect_off
= line_offset
;
2780 /* There may not be a DW_AT_stmt_list. */
2781 if (slot
!= nullptr)
2784 std::vector
<const char *> include_names
;
2787 for (const auto &entry
: lh
->file_names ())
2789 std::string name_holder
;
2790 const char *include_name
=
2791 compute_include_file_name (lh
.get (), entry
, fnd
, name_holder
);
2792 if (include_name
!= nullptr)
2794 include_name
= per_objfile
->objfile
->intern (include_name
);
2795 include_names
.push_back (include_name
);
2800 qfn
->num_file_names
= offset
+ include_names
.size ();
2801 qfn
->comp_dir
= fnd
.intern_comp_dir (per_objfile
->objfile
);
2803 XOBNEWVEC (&per_objfile
->per_bfd
->obstack
, const char *,
2804 qfn
->num_file_names
);
2806 qfn
->file_names
[0] = xstrdup (fnd
.get_name ());
2808 if (!include_names
.empty ())
2809 memcpy (&qfn
->file_names
[offset
], include_names
.data (),
2810 include_names
.size () * sizeof (const char *));
2812 qfn
->real_names
= NULL
;
2814 lh_cu
->file_names
= qfn
;
2817 /* A helper for the "quick" functions which attempts to read the line
2818 table for THIS_CU. */
2820 static struct quick_file_names
*
2821 dw2_get_file_names (dwarf2_per_cu_data
*this_cu
,
2822 dwarf2_per_objfile
*per_objfile
)
2824 /* This should never be called for TUs. */
2825 gdb_assert (! this_cu
->is_debug_types
);
2827 if (this_cu
->files_read
)
2828 return this_cu
->file_names
;
2830 cutu_reader
reader (this_cu
, per_objfile
);
2831 if (!reader
.dummy_p
)
2832 dw2_get_file_names_reader (&reader
, reader
.comp_unit_die
);
2834 return this_cu
->file_names
;
2837 /* A helper for the "quick" functions which computes and caches the
2838 real path for a given file name from the line table. */
2841 dw2_get_real_path (dwarf2_per_objfile
*per_objfile
,
2842 struct quick_file_names
*qfn
, int index
)
2844 if (qfn
->real_names
== NULL
)
2845 qfn
->real_names
= OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
2846 qfn
->num_file_names
, const char *);
2848 if (qfn
->real_names
[index
] == NULL
)
2850 const char *dirname
= nullptr;
2852 if (!IS_ABSOLUTE_PATH (qfn
->file_names
[index
]))
2853 dirname
= qfn
->comp_dir
;
2855 gdb::unique_xmalloc_ptr
<char> fullname
;
2856 fullname
= find_source_or_rewrite (qfn
->file_names
[index
], dirname
);
2858 qfn
->real_names
[index
] = fullname
.release ();
2861 return qfn
->real_names
[index
];
2865 dwarf2_base_index_functions::find_last_source_symtab (struct objfile
*objfile
)
2867 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2868 dwarf2_per_cu_data
*dwarf_cu
2869 = per_objfile
->per_bfd
->all_comp_units
.back ().get ();
2870 compunit_symtab
*cust
= dw2_instantiate_symtab (dwarf_cu
, per_objfile
, false);
2875 return cust
->primary_filetab ();
2881 dwarf2_per_cu_data::free_cached_file_names ()
2884 fnd
->forget_fullname ();
2886 if (per_bfd
== nullptr)
2889 struct quick_file_names
*file_data
= file_names
;
2890 if (file_data
!= nullptr && file_data
->real_names
!= nullptr)
2892 for (int i
= 0; i
< file_data
->num_file_names
; ++i
)
2894 xfree ((void *) file_data
->real_names
[i
]);
2895 file_data
->real_names
[i
] = nullptr;
2901 dwarf2_base_index_functions::forget_cached_source_info
2902 (struct objfile
*objfile
)
2904 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2906 for (auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
2907 per_cu
->free_cached_file_names ();
2910 /* Struct used to manage iterating over all CUs looking for a symbol. */
2912 struct dw2_symtab_iterator
2914 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2915 dwarf2_per_objfile
*per_objfile
;
2916 /* If set, only look for symbols that match that block. Valid values are
2917 GLOBAL_BLOCK and STATIC_BLOCK. */
2918 gdb::optional
<block_enum
> block_index
;
2919 /* The kind of symbol we're looking for. */
2921 /* The list of CUs from the index entry of the symbol,
2922 or NULL if not found. */
2924 /* The next element in VEC to look at. */
2926 /* The number of elements in VEC, or zero if there is no match. */
2928 /* Have we seen a global version of the symbol?
2929 If so we can ignore all further global instances.
2930 This is to work around gold/15646, inefficient gold-generated
2935 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2938 dw2_symtab_iter_init (struct dw2_symtab_iterator
*iter
,
2939 dwarf2_per_objfile
*per_objfile
,
2940 gdb::optional
<block_enum
> block_index
,
2941 domain_enum domain
, offset_type namei
,
2942 mapped_index
&index
)
2944 iter
->per_objfile
= per_objfile
;
2945 iter
->block_index
= block_index
;
2946 iter
->domain
= domain
;
2948 iter
->global_seen
= 0;
2952 gdb_assert (!index
.symbol_name_slot_invalid (namei
));
2953 offset_type vec_idx
= index
.symbol_vec_index (namei
);
2955 iter
->vec
= offset_view (index
.constant_pool
.slice (vec_idx
));
2956 iter
->length
= iter
->vec
[0];
2959 /* Return the next matching CU or NULL if there are no more. */
2961 static struct dwarf2_per_cu_data
*
2962 dw2_symtab_iter_next (struct dw2_symtab_iterator
*iter
,
2963 mapped_index
&index
)
2965 dwarf2_per_objfile
*per_objfile
= iter
->per_objfile
;
2967 for ( ; iter
->next
< iter
->length
; ++iter
->next
)
2969 offset_type cu_index_and_attrs
= iter
->vec
[iter
->next
+ 1];
2970 offset_type cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
2971 gdb_index_symbol_kind symbol_kind
=
2972 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
2973 /* Only check the symbol attributes if they're present.
2974 Indices prior to version 7 don't record them,
2975 and indices >= 7 may elide them for certain symbols
2976 (gold does this). */
2977 int attrs_valid
= (index
.version
>= 7
2978 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
2980 /* Don't crash on bad data. */
2981 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
2983 complaint (_(".gdb_index entry has bad CU index"
2984 " [in module %s]"), objfile_name (per_objfile
->objfile
));
2988 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
2990 /* Skip if already read in. */
2991 if (per_objfile
->symtab_set_p (per_cu
))
2994 /* Check static vs global. */
2997 bool is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
2999 if (iter
->block_index
.has_value ())
3001 bool want_static
= *iter
->block_index
== STATIC_BLOCK
;
3003 if (is_static
!= want_static
)
3007 /* Work around gold/15646. */
3009 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
3011 if (iter
->global_seen
)
3014 iter
->global_seen
= 1;
3018 /* Only check the symbol's kind if it has one. */
3021 switch (iter
->domain
)
3024 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
3025 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
3026 /* Some types are also in VAR_DOMAIN. */
3027 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3031 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3035 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3039 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3055 dwarf2_base_index_functions::print_stats (struct objfile
*objfile
,
3061 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3062 int total
= per_objfile
->per_bfd
->all_comp_units
.size ();
3065 for (int i
= 0; i
< total
; ++i
)
3067 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3069 if (!per_objfile
->symtab_set_p (per_cu
))
3072 gdb_printf (_(" Number of read CUs: %d\n"), total
- count
);
3073 gdb_printf (_(" Number of unread CUs: %d\n"), count
);
3076 /* This dumps minimal information about the index.
3077 It is called via "mt print objfiles".
3078 One use is to verify .gdb_index has been loaded by the
3079 gdb.dwarf2/gdb-index.exp testcase. */
3082 dwarf2_gdb_index::dump (struct objfile
*objfile
)
3084 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3086 mapped_index
*index
= (static_cast<mapped_index
*>
3087 (per_objfile
->per_bfd
->index_table
.get ()));
3088 gdb_printf (".gdb_index: version %d\n", index
->version
);
3093 dwarf2_base_index_functions::expand_all_symtabs (struct objfile
*objfile
)
3095 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3096 int total_units
= per_objfile
->per_bfd
->all_comp_units
.size ();
3098 for (int i
= 0; i
< total_units
; ++i
)
3100 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3102 /* We don't want to directly expand a partial CU, because if we
3103 read it with the wrong language, then assertion failures can
3104 be triggered later on. See PR symtab/23010. So, tell
3105 dw2_instantiate_symtab to skip partial CUs -- any important
3106 partial CU will be read via DW_TAG_imported_unit anyway. */
3107 dw2_instantiate_symtab (per_cu
, per_objfile
, true);
3112 dw2_expand_symtabs_matching_symbol
3113 (mapped_index_base
&index
,
3114 const lookup_name_info
&lookup_name_in
,
3115 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3116 gdb::function_view
<bool (offset_type
)> match_callback
,
3117 dwarf2_per_objfile
*per_objfile
);
3120 dw2_expand_symtabs_matching_one
3121 (dwarf2_per_cu_data
*per_cu
,
3122 dwarf2_per_objfile
*per_objfile
,
3123 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3124 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
);
3127 dwarf2_gdb_index::expand_matching_symbols
3128 (struct objfile
*objfile
,
3129 const lookup_name_info
&name
, domain_enum domain
,
3131 symbol_compare_ftype
*ordered_compare
)
3134 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3136 const block_enum block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
3139 = (static_cast<mapped_index
&>
3140 (*per_objfile
->per_bfd
->index_table
.get ()));
3142 const char *match_name
= name
.ada ().lookup_name ().c_str ();
3143 auto matcher
= [&] (const char *symname
)
3145 if (ordered_compare
== nullptr)
3147 return ordered_compare (symname
, match_name
) == 0;
3150 dw2_expand_symtabs_matching_symbol (index
, name
, matcher
,
3151 [&] (offset_type namei
)
3153 struct dw2_symtab_iterator iter
;
3154 struct dwarf2_per_cu_data
*per_cu
;
3156 dw2_symtab_iter_init (&iter
, per_objfile
, block_kind
, domain
, namei
,
3158 while ((per_cu
= dw2_symtab_iter_next (&iter
, index
)) != NULL
)
3159 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
3165 /* Starting from a search name, return the string that finds the upper
3166 bound of all strings that start with SEARCH_NAME in a sorted name
3167 list. Returns the empty string to indicate that the upper bound is
3168 the end of the list. */
3171 make_sort_after_prefix_name (const char *search_name
)
3173 /* When looking to complete "func", we find the upper bound of all
3174 symbols that start with "func" by looking for where we'd insert
3175 the closest string that would follow "func" in lexicographical
3176 order. Usually, that's "func"-with-last-character-incremented,
3177 i.e. "fund". Mind non-ASCII characters, though. Usually those
3178 will be UTF-8 multi-byte sequences, but we can't be certain.
3179 Especially mind the 0xff character, which is a valid character in
3180 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3181 rule out compilers allowing it in identifiers. Note that
3182 conveniently, strcmp/strcasecmp are specified to compare
3183 characters interpreted as unsigned char. So what we do is treat
3184 the whole string as a base 256 number composed of a sequence of
3185 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3186 to 0, and carries 1 to the following more-significant position.
3187 If the very first character in SEARCH_NAME ends up incremented
3188 and carries/overflows, then the upper bound is the end of the
3189 list. The string after the empty string is also the empty
3192 Some examples of this operation:
3194 SEARCH_NAME => "+1" RESULT
3198 "\xff" "a" "\xff" => "\xff" "b"
3203 Then, with these symbols for example:
3209 completing "func" looks for symbols between "func" and
3210 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3211 which finds "func" and "func1", but not "fund".
3215 funcÿ (Latin1 'ÿ' [0xff])
3219 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3220 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3224 ÿÿ (Latin1 'ÿ' [0xff])
3227 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3228 the end of the list.
3230 std::string after
= search_name
;
3231 while (!after
.empty () && (unsigned char) after
.back () == 0xff)
3233 if (!after
.empty ())
3234 after
.back () = (unsigned char) after
.back () + 1;
3238 /* See declaration. */
3240 std::pair
<std::vector
<name_component
>::const_iterator
,
3241 std::vector
<name_component
>::const_iterator
>
3242 mapped_index_base::find_name_components_bounds
3243 (const lookup_name_info
&lookup_name_without_params
, language lang
,
3244 dwarf2_per_objfile
*per_objfile
) const
3247 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3249 const char *lang_name
3250 = lookup_name_without_params
.language_lookup_name (lang
);
3252 /* Comparison function object for lower_bound that matches against a
3253 given symbol name. */
3254 auto lookup_compare_lower
= [&] (const name_component
&elem
,
3257 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3258 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3259 return name_cmp (elem_name
, name
) < 0;
3262 /* Comparison function object for upper_bound that matches against a
3263 given symbol name. */
3264 auto lookup_compare_upper
= [&] (const char *name
,
3265 const name_component
&elem
)
3267 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3268 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3269 return name_cmp (name
, elem_name
) < 0;
3272 auto begin
= this->name_components
.begin ();
3273 auto end
= this->name_components
.end ();
3275 /* Find the lower bound. */
3278 if (lookup_name_without_params
.completion_mode () && lang_name
[0] == '\0')
3281 return std::lower_bound (begin
, end
, lang_name
, lookup_compare_lower
);
3284 /* Find the upper bound. */
3287 if (lookup_name_without_params
.completion_mode ())
3289 /* In completion mode, we want UPPER to point past all
3290 symbols names that have the same prefix. I.e., with
3291 these symbols, and completing "func":
3293 function << lower bound
3295 other_function << upper bound
3297 We find the upper bound by looking for the insertion
3298 point of "func"-with-last-character-incremented,
3300 std::string after
= make_sort_after_prefix_name (lang_name
);
3303 return std::lower_bound (lower
, end
, after
.c_str (),
3304 lookup_compare_lower
);
3307 return std::upper_bound (lower
, end
, lang_name
, lookup_compare_upper
);
3310 return {lower
, upper
};
3313 /* See declaration. */
3316 mapped_index_base::build_name_components (dwarf2_per_objfile
*per_objfile
)
3318 if (!this->name_components
.empty ())
3321 this->name_components_casing
= case_sensitivity
;
3323 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3325 /* The code below only knows how to break apart components of C++
3326 symbol names (and other languages that use '::' as
3327 namespace/module separator) and Ada symbol names. */
3328 auto count
= this->symbol_name_count ();
3329 for (offset_type idx
= 0; idx
< count
; idx
++)
3331 if (this->symbol_name_slot_invalid (idx
))
3334 const char *name
= this->symbol_name_at (idx
, per_objfile
);
3336 /* Add each name component to the name component table. */
3337 unsigned int previous_len
= 0;
3339 if (strstr (name
, "::") != nullptr)
3341 for (unsigned int current_len
= cp_find_first_component (name
);
3342 name
[current_len
] != '\0';
3343 current_len
+= cp_find_first_component (name
+ current_len
))
3345 gdb_assert (name
[current_len
] == ':');
3346 this->name_components
.push_back ({previous_len
, idx
});
3347 /* Skip the '::'. */
3349 previous_len
= current_len
;
3354 /* Handle the Ada encoded (aka mangled) form here. */
3355 for (const char *iter
= strstr (name
, "__");
3357 iter
= strstr (iter
, "__"))
3359 this->name_components
.push_back ({previous_len
, idx
});
3361 previous_len
= iter
- name
;
3365 this->name_components
.push_back ({previous_len
, idx
});
3368 /* Sort name_components elements by name. */
3369 auto name_comp_compare
= [&] (const name_component
&left
,
3370 const name_component
&right
)
3372 const char *left_qualified
3373 = this->symbol_name_at (left
.idx
, per_objfile
);
3374 const char *right_qualified
3375 = this->symbol_name_at (right
.idx
, per_objfile
);
3377 const char *left_name
= left_qualified
+ left
.name_offset
;
3378 const char *right_name
= right_qualified
+ right
.name_offset
;
3380 return name_cmp (left_name
, right_name
) < 0;
3383 std::sort (this->name_components
.begin (),
3384 this->name_components
.end (),
3388 /* Helper for dw2_expand_symtabs_matching that works with a
3389 mapped_index_base instead of the containing objfile. This is split
3390 to a separate function in order to be able to unit test the
3391 name_components matching using a mock mapped_index_base. For each
3392 symbol name that matches, calls MATCH_CALLBACK, passing it the
3393 symbol's index in the mapped_index_base symbol table. */
3396 dw2_expand_symtabs_matching_symbol
3397 (mapped_index_base
&index
,
3398 const lookup_name_info
&lookup_name_in
,
3399 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3400 gdb::function_view
<bool (offset_type
)> match_callback
,
3401 dwarf2_per_objfile
*per_objfile
)
3403 lookup_name_info lookup_name_without_params
3404 = lookup_name_in
.make_ignore_params ();
3406 /* Build the symbol name component sorted vector, if we haven't
3408 index
.build_name_components (per_objfile
);
3410 /* The same symbol may appear more than once in the range though.
3411 E.g., if we're looking for symbols that complete "w", and we have
3412 a symbol named "w1::w2", we'll find the two name components for
3413 that same symbol in the range. To be sure we only call the
3414 callback once per symbol, we first collect the symbol name
3415 indexes that matched in a temporary vector and ignore
3417 std::vector
<offset_type
> matches
;
3419 struct name_and_matcher
3421 symbol_name_matcher_ftype
*matcher
;
3424 bool operator== (const name_and_matcher
&other
) const
3426 return matcher
== other
.matcher
&& strcmp (name
, other
.name
) == 0;
3430 /* A vector holding all the different symbol name matchers, for all
3432 std::vector
<name_and_matcher
> matchers
;
3434 for (int i
= 0; i
< nr_languages
; i
++)
3436 enum language lang_e
= (enum language
) i
;
3438 const language_defn
*lang
= language_def (lang_e
);
3439 symbol_name_matcher_ftype
*name_matcher
3440 = lang
->get_symbol_name_matcher (lookup_name_without_params
);
3442 name_and_matcher key
{
3444 lookup_name_without_params
.language_lookup_name (lang_e
)
3447 /* Don't insert the same comparison routine more than once.
3448 Note that we do this linear walk. This is not a problem in
3449 practice because the number of supported languages is
3451 if (std::find (matchers
.begin (), matchers
.end (), key
)
3454 matchers
.push_back (std::move (key
));
3457 = index
.find_name_components_bounds (lookup_name_without_params
,
3458 lang_e
, per_objfile
);
3460 /* Now for each symbol name in range, check to see if we have a name
3461 match, and if so, call the MATCH_CALLBACK callback. */
3463 for (; bounds
.first
!= bounds
.second
; ++bounds
.first
)
3465 const char *qualified
3466 = index
.symbol_name_at (bounds
.first
->idx
, per_objfile
);
3468 if (!name_matcher (qualified
, lookup_name_without_params
, NULL
)
3469 || (symbol_matcher
!= NULL
&& !symbol_matcher (qualified
)))
3472 matches
.push_back (bounds
.first
->idx
);
3476 std::sort (matches
.begin (), matches
.end ());
3478 /* Finally call the callback, once per match. */
3481 for (offset_type idx
: matches
)
3485 if (!match_callback (idx
))
3494 /* Above we use a type wider than idx's for 'prev', since 0 and
3495 (offset_type)-1 are both possible values. */
3496 static_assert (sizeof (prev
) > sizeof (offset_type
), "");
3503 namespace selftests
{ namespace dw2_expand_symtabs_matching
{
3505 /* A mock .gdb_index/.debug_names-like name index table, enough to
3506 exercise dw2_expand_symtabs_matching_symbol, which works with the
3507 mapped_index_base interface. Builds an index from the symbol list
3508 passed as parameter to the constructor. */
3509 class mock_mapped_index
: public mapped_index_base
3512 mock_mapped_index (gdb::array_view
<const char *> symbols
)
3513 : m_symbol_table (symbols
)
3516 DISABLE_COPY_AND_ASSIGN (mock_mapped_index
);
3518 /* Return the number of names in the symbol table. */
3519 size_t symbol_name_count () const override
3521 return m_symbol_table
.size ();
3524 /* Get the name of the symbol at IDX in the symbol table. */
3525 const char *symbol_name_at
3526 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
3528 return m_symbol_table
[idx
];
3531 quick_symbol_functions_up
make_quick_functions () const override
3537 gdb::array_view
<const char *> m_symbol_table
;
3540 /* Convenience function that converts a NULL pointer to a "<null>"
3541 string, to pass to print routines. */
3544 string_or_null (const char *str
)
3546 return str
!= NULL
? str
: "<null>";
3549 /* Check if a lookup_name_info built from
3550 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3551 index. EXPECTED_LIST is the list of expected matches, in expected
3552 matching order. If no match expected, then an empty list is
3553 specified. Returns true on success. On failure prints a warning
3554 indicating the file:line that failed, and returns false. */
3557 check_match (const char *file
, int line
,
3558 mock_mapped_index
&mock_index
,
3559 const char *name
, symbol_name_match_type match_type
,
3560 bool completion_mode
,
3561 std::initializer_list
<const char *> expected_list
,
3562 dwarf2_per_objfile
*per_objfile
)
3564 lookup_name_info
lookup_name (name
, match_type
, completion_mode
);
3566 bool matched
= true;
3568 auto mismatch
= [&] (const char *expected_str
,
3571 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3572 "expected=\"%s\", got=\"%s\"\n"),
3574 (match_type
== symbol_name_match_type::FULL
3576 name
, string_or_null (expected_str
), string_or_null (got
));
3580 auto expected_it
= expected_list
.begin ();
3581 auto expected_end
= expected_list
.end ();
3583 dw2_expand_symtabs_matching_symbol (mock_index
, lookup_name
,
3585 [&] (offset_type idx
)
3587 const char *matched_name
= mock_index
.symbol_name_at (idx
, per_objfile
);
3588 const char *expected_str
3589 = expected_it
== expected_end
? NULL
: *expected_it
++;
3591 if (expected_str
== NULL
|| strcmp (expected_str
, matched_name
) != 0)
3592 mismatch (expected_str
, matched_name
);
3596 const char *expected_str
3597 = expected_it
== expected_end
? NULL
: *expected_it
++;
3598 if (expected_str
!= NULL
)
3599 mismatch (expected_str
, NULL
);
3604 /* The symbols added to the mock mapped_index for testing (in
3606 static const char *test_symbols
[] = {
3615 "ns2::tmpl<int>::foo2",
3616 "(anonymous namespace)::A::B::C",
3618 /* These are used to check that the increment-last-char in the
3619 matching algorithm for completion doesn't match "t1_fund" when
3620 completing "t1_func". */
3626 /* A UTF-8 name with multi-byte sequences to make sure that
3627 cp-name-parser understands this as a single identifier ("função"
3628 is "function" in PT). */
3631 /* Test a symbol name that ends with a 0xff character, which is a
3632 valid character in non-UTF-8 source character sets (e.g. Latin1
3633 'ÿ'), and we can't rule out compilers allowing it in identifiers.
3634 We test this because the completion algorithm finds the upper
3635 bound of symbols by looking for the insertion point of
3636 "func"-with-last-character-incremented, i.e. "fund", and adding 1
3637 to 0xff should wraparound and carry to the previous character.
3638 See comments in make_sort_after_prefix_name. */
3641 /* Some more symbols with \377 (0xff). See above. */
3645 /* A name with all sorts of complications. Starts with "z" to make
3646 it easier for the completion tests below. */
3647 #define Z_SYM_NAME \
3648 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3649 "::tuple<(anonymous namespace)::ui*, " \
3650 "std::default_delete<(anonymous namespace)::ui>, void>"
3655 /* Returns true if the mapped_index_base::find_name_component_bounds
3656 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3657 in completion mode. */
3660 check_find_bounds_finds (mapped_index_base
&index
,
3661 const char *search_name
,
3662 gdb::array_view
<const char *> expected_syms
,
3663 dwarf2_per_objfile
*per_objfile
)
3665 lookup_name_info
lookup_name (search_name
,
3666 symbol_name_match_type::FULL
, true);
3668 auto bounds
= index
.find_name_components_bounds (lookup_name
,
3672 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
3673 if (distance
!= expected_syms
.size ())
3676 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
3678 auto nc_elem
= bounds
.first
+ exp_elem
;
3679 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
3680 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
3687 /* Test the lower-level mapped_index::find_name_component_bounds
3691 test_mapped_index_find_name_component_bounds ()
3693 mock_mapped_index
mock_index (test_symbols
);
3695 mock_index
.build_name_components (NULL
/* per_objfile */);
3697 /* Test the lower-level mapped_index::find_name_component_bounds
3698 method in completion mode. */
3700 static const char *expected_syms
[] = {
3705 SELF_CHECK (check_find_bounds_finds
3706 (mock_index
, "t1_func", expected_syms
,
3707 NULL
/* per_objfile */));
3710 /* Check that the increment-last-char in the name matching algorithm
3711 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. See
3712 make_sort_after_prefix_name. */
3714 static const char *expected_syms1
[] = {
3718 SELF_CHECK (check_find_bounds_finds
3719 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
3721 static const char *expected_syms2
[] = {
3724 SELF_CHECK (check_find_bounds_finds
3725 (mock_index
, "\377\377", expected_syms2
,
3726 NULL
/* per_objfile */));
3730 /* Test dw2_expand_symtabs_matching_symbol. */
3733 test_dw2_expand_symtabs_matching_symbol ()
3735 mock_mapped_index
mock_index (test_symbols
);
3737 /* We let all tests run until the end even if some fails, for debug
3739 bool any_mismatch
= false;
3741 /* Create the expected symbols list (an initializer_list). Needed
3742 because lists have commas, and we need to pass them to CHECK,
3743 which is a macro. */
3744 #define EXPECT(...) { __VA_ARGS__ }
3746 /* Wrapper for check_match that passes down the current
3747 __FILE__/__LINE__. */
3748 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3749 any_mismatch |= !check_match (__FILE__, __LINE__, \
3751 NAME, MATCH_TYPE, COMPLETION_MODE, \
3752 EXPECTED_LIST, NULL)
3754 /* Identity checks. */
3755 for (const char *sym
: test_symbols
)
3757 /* Should be able to match all existing symbols. */
3758 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
3761 /* Should be able to match all existing symbols with
3763 std::string with_params
= std::string (sym
) + "(int)";
3764 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3767 /* Should be able to match all existing symbols with
3768 parameters and qualifiers. */
3769 with_params
= std::string (sym
) + " ( int ) const";
3770 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3773 /* This should really find sym, but cp-name-parser.y doesn't
3774 know about lvalue/rvalue qualifiers yet. */
3775 with_params
= std::string (sym
) + " ( int ) &&";
3776 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3780 /* Check that the name matching algorithm for completion doesn't get
3781 confused with Latin1 'ÿ' / 0xff. See
3782 make_sort_after_prefix_name. */
3784 static const char str
[] = "\377";
3785 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3786 EXPECT ("\377", "\377\377123"));
3789 /* Check that the increment-last-char in the matching algorithm for
3790 completion doesn't match "t1_fund" when completing "t1_func". */
3792 static const char str
[] = "t1_func";
3793 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3794 EXPECT ("t1_func", "t1_func1"));
3797 /* Check that completion mode works at each prefix of the expected
3800 static const char str
[] = "function(int)";
3801 size_t len
= strlen (str
);
3804 for (size_t i
= 1; i
< len
; i
++)
3806 lookup
.assign (str
, i
);
3807 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3808 EXPECT ("function"));
3812 /* While "w" is a prefix of both components, the match function
3813 should still only be called once. */
3815 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
3817 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
3821 /* Same, with a "complicated" symbol. */
3823 static const char str
[] = Z_SYM_NAME
;
3824 size_t len
= strlen (str
);
3827 for (size_t i
= 1; i
< len
; i
++)
3829 lookup
.assign (str
, i
);
3830 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3831 EXPECT (Z_SYM_NAME
));
3835 /* In FULL mode, an incomplete symbol doesn't match. */
3837 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
3841 /* A complete symbol with parameters matches any overload, since the
3842 index has no overload info. */
3844 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
3845 EXPECT ("std::zfunction", "std::zfunction2"));
3846 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
3847 EXPECT ("std::zfunction", "std::zfunction2"));
3848 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
3849 EXPECT ("std::zfunction", "std::zfunction2"));
3852 /* Check that whitespace is ignored appropriately. A symbol with a
3853 template argument list. */
3855 static const char expected
[] = "ns::foo<int>";
3856 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
3858 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
3862 /* Check that whitespace is ignored appropriately. A symbol with a
3863 template argument list that includes a pointer. */
3865 static const char expected
[] = "ns::foo<char*>";
3866 /* Try both completion and non-completion modes. */
3867 static const bool completion_mode
[2] = {false, true};
3868 for (size_t i
= 0; i
< 2; i
++)
3870 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
3871 completion_mode
[i
], EXPECT (expected
));
3872 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
3873 completion_mode
[i
], EXPECT (expected
));
3875 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
3876 completion_mode
[i
], EXPECT (expected
));
3877 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
3878 completion_mode
[i
], EXPECT (expected
));
3883 /* Check method qualifiers are ignored. */
3884 static const char expected
[] = "ns::foo<char*>";
3885 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3886 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3887 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3888 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3889 CHECK_MATCH ("foo < char * > ( int ) const",
3890 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3891 CHECK_MATCH ("foo < char * > ( int ) &&",
3892 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3895 /* Test lookup names that don't match anything. */
3897 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
3900 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
3904 /* Some wild matching tests, exercising "(anonymous namespace)",
3905 which should not be confused with a parameter list. */
3907 static const char *syms
[] = {
3911 "A :: B :: C ( int )",
3916 for (const char *s
: syms
)
3918 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
3919 EXPECT ("(anonymous namespace)::A::B::C"));
3924 static const char expected
[] = "ns2::tmpl<int>::foo2";
3925 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
3927 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
3931 SELF_CHECK (!any_mismatch
);
3940 test_mapped_index_find_name_component_bounds ();
3941 test_dw2_expand_symtabs_matching_symbol ();
3944 }} // namespace selftests::dw2_expand_symtabs_matching
3946 #endif /* GDB_SELF_TEST */
3948 /* If FILE_MATCHER is NULL or if PER_CU has
3949 dwarf2_per_cu_quick_data::MARK set (see
3950 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3951 EXPANSION_NOTIFY on it. */
3954 dw2_expand_symtabs_matching_one
3955 (dwarf2_per_cu_data
*per_cu
,
3956 dwarf2_per_objfile
*per_objfile
,
3957 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3958 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
3960 if (file_matcher
== NULL
|| per_cu
->mark
)
3962 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
3964 compunit_symtab
*symtab
3965 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
3966 gdb_assert (symtab
!= nullptr);
3968 if (expansion_notify
!= NULL
&& symtab_was_null
)
3969 return expansion_notify (symtab
);
3974 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3975 matched, to expand corresponding CUs that were marked. IDX is the
3976 index of the symbol name that matched. */
3979 dw2_expand_marked_cus
3980 (dwarf2_per_objfile
*per_objfile
, offset_type idx
,
3981 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3982 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
3983 block_search_flags search_flags
,
3986 offset_type vec_len
, vec_idx
;
3987 bool global_seen
= false;
3989 = (static_cast<mapped_index
&>
3990 (*per_objfile
->per_bfd
->index_table
.get ()));
3992 offset_view
vec (index
.constant_pool
.slice (index
.symbol_vec_index (idx
)));
3994 for (vec_idx
= 0; vec_idx
< vec_len
; ++vec_idx
)
3996 offset_type cu_index_and_attrs
= vec
[vec_idx
+ 1];
3997 /* This value is only valid for index versions >= 7. */
3998 int is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
3999 gdb_index_symbol_kind symbol_kind
=
4000 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
4001 int cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
4002 /* Only check the symbol attributes if they're present.
4003 Indices prior to version 7 don't record them,
4004 and indices >= 7 may elide them for certain symbols
4005 (gold does this). */
4008 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
4010 /* Work around gold/15646. */
4013 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
4021 /* Only check the symbol's kind if it has one. */
4026 if ((search_flags
& SEARCH_STATIC_BLOCK
) == 0)
4031 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
4037 case VARIABLES_DOMAIN
:
4038 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
)
4041 case FUNCTIONS_DOMAIN
:
4042 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
)
4046 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
4049 case MODULES_DOMAIN
:
4050 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
4058 /* Don't crash on bad data. */
4059 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
4061 complaint (_(".gdb_index entry has bad CU index"
4062 " [in module %s]"), objfile_name (per_objfile
->objfile
));
4066 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
4067 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, file_matcher
,
4075 /* If FILE_MATCHER is non-NULL, set all the
4076 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4077 that match FILE_MATCHER. */
4080 dw_expand_symtabs_matching_file_matcher
4081 (dwarf2_per_objfile
*per_objfile
,
4082 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
4084 if (file_matcher
== NULL
)
4087 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
4089 NULL
, xcalloc
, xfree
));
4090 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
4092 NULL
, xcalloc
, xfree
));
4094 /* The rule is CUs specify all the files, including those used by
4095 any TU, so there's no need to scan TUs here. */
4097 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4101 if (per_cu
->is_debug_types
)
4105 /* We only need to look at symtabs not already expanded. */
4106 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4109 if (per_cu
->fnd
!= nullptr)
4111 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4113 if (file_matcher (fnd
->get_name (), false))
4119 /* Before we invoke realpath, which can get expensive when many
4120 files are involved, do a quick comparison of the basenames. */
4121 if ((basenames_may_differ
4122 || file_matcher (lbasename (fnd
->get_name ()), true))
4123 && file_matcher (fnd
->get_fullname (), false))
4130 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
4132 if (file_data
== NULL
)
4135 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
4137 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
4143 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4145 const char *this_real_name
;
4147 if (file_matcher (file_data
->file_names
[j
], false))
4153 /* Before we invoke realpath, which can get expensive when many
4154 files are involved, do a quick comparison of the basenames. */
4155 if (!basenames_may_differ
4156 && !file_matcher (lbasename (file_data
->file_names
[j
]),
4160 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
4161 if (file_matcher (this_real_name
, false))
4168 void **slot
= htab_find_slot (per_cu
->mark
4169 ? visited_found
.get ()
4170 : visited_not_found
.get (),
4177 dwarf2_gdb_index::expand_symtabs_matching
4178 (struct objfile
*objfile
,
4179 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
4180 const lookup_name_info
*lookup_name
,
4181 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
4182 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
4183 block_search_flags search_flags
,
4185 enum search_domain kind
)
4187 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4189 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
4191 /* This invariant is documented in quick-functions.h. */
4192 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
4193 if (lookup_name
== nullptr)
4195 for (dwarf2_per_cu_data
*per_cu
4196 : all_comp_units_range (per_objfile
->per_bfd
))
4200 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
4209 = (static_cast<mapped_index
&>
4210 (*per_objfile
->per_bfd
->index_table
.get ()));
4213 = dw2_expand_symtabs_matching_symbol (index
, *lookup_name
,
4215 [&] (offset_type idx
)
4217 if (!dw2_expand_marked_cus (per_objfile
, idx
, file_matcher
,
4218 expansion_notify
, search_flags
, kind
))
4226 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4229 static struct compunit_symtab
*
4230 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
4235 if (cust
->blockvector () != nullptr
4236 && blockvector_contains_pc (cust
->blockvector (), pc
))
4239 if (cust
->includes
== NULL
)
4242 for (i
= 0; cust
->includes
[i
]; ++i
)
4244 struct compunit_symtab
*s
= cust
->includes
[i
];
4246 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
4254 dwarf2_per_cu_data
*
4255 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
4256 CORE_ADDR adjusted_pc
)
4258 if (per_bfd
->index_addrmap
== nullptr)
4260 return ((struct dwarf2_per_cu_data
*)
4261 per_bfd
->index_addrmap
->find (adjusted_pc
));
4264 struct compunit_symtab
*
4265 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4266 (struct objfile
*objfile
,
4267 struct bound_minimal_symbol msymbol
,
4269 struct obj_section
*section
,
4272 struct compunit_symtab
*result
;
4274 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4276 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
4277 struct dwarf2_per_cu_data
*data
= find_per_cu (per_objfile
->per_bfd
,
4279 if (data
== nullptr)
4282 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
4283 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4284 paddress (objfile
->arch (), pc
));
4286 result
= recursively_find_pc_sect_compunit_symtab
4287 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
4289 if (warn_if_readin
&& result
== nullptr)
4290 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4291 paddress (objfile
->arch (), pc
));
4297 dwarf2_base_index_functions::map_symbol_filenames
4298 (struct objfile
*objfile
,
4299 gdb::function_view
<symbol_filename_ftype
> fun
,
4302 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4304 /* Use caches to ensure we only call FUN once for each filename. */
4305 filename_seen_cache filenames_cache
;
4306 std::unordered_set
<quick_file_names
*> qfn_cache
;
4308 /* The rule is CUs specify all the files, including those used by any TU,
4309 so there's no need to scan TUs here. We can ignore file names coming
4310 from already-expanded CUs. It is possible that an expanded CU might
4311 reuse the file names data from a currently unexpanded CU, in this
4312 case we don't want to report the files from the unexpanded CU. */
4314 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4316 if (!per_cu
->is_debug_types
4317 && per_objfile
->symtab_set_p (per_cu
.get ()))
4319 if (per_cu
->file_names
!= nullptr)
4320 qfn_cache
.insert (per_cu
->file_names
);
4324 for (dwarf2_per_cu_data
*per_cu
4325 : all_comp_units_range (per_objfile
->per_bfd
))
4327 /* We only need to look at symtabs not already expanded. */
4328 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
4331 if (per_cu
->fnd
!= nullptr)
4333 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4335 const char *filename
= fnd
->get_name ();
4336 const char *key
= filename
;
4337 const char *fullname
= nullptr;
4341 fullname
= fnd
->get_fullname ();
4345 if (!filenames_cache
.seen (key
))
4346 fun (filename
, fullname
);
4349 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
4350 if (file_data
== nullptr
4351 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
4354 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4356 const char *filename
= file_data
->file_names
[j
];
4357 const char *key
= filename
;
4358 const char *fullname
= nullptr;
4362 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
4366 if (!filenames_cache
.seen (key
))
4367 fun (filename
, fullname
);
4373 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
4378 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4381 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
4383 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4385 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4387 /* Is this already expanded? */
4388 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4391 /* It has not yet been expanded. */
4398 /* DWARF-5 debug_names reader. */
4400 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4401 static const gdb_byte dwarf5_augmentation
[] = { 'G', 'D', 'B', 0 };
4403 /* A helper function that reads the .debug_names section in SECTION
4404 and fills in MAP. FILENAME is the name of the file containing the
4405 section; it is used for error reporting.
4407 Returns true if all went well, false otherwise. */
4410 read_debug_names_from_section (struct objfile
*objfile
,
4411 const char *filename
,
4412 struct dwarf2_section_info
*section
,
4413 mapped_debug_names
&map
)
4415 if (section
->empty ())
4418 /* Older elfutils strip versions could keep the section in the main
4419 executable while splitting it for the separate debug info file. */
4420 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
4423 section
->read (objfile
);
4425 map
.dwarf5_byte_order
= gdbarch_byte_order (objfile
->arch ());
4427 const gdb_byte
*addr
= section
->buffer
;
4429 bfd
*const abfd
= section
->get_bfd_owner ();
4431 unsigned int bytes_read
;
4432 LONGEST length
= read_initial_length (abfd
, addr
, &bytes_read
);
4435 map
.dwarf5_is_dwarf64
= bytes_read
!= 4;
4436 map
.offset_size
= map
.dwarf5_is_dwarf64
? 8 : 4;
4437 if (bytes_read
+ length
!= section
->size
)
4439 /* There may be multiple per-CU indices. */
4440 warning (_("Section .debug_names in %s length %s does not match "
4441 "section length %s, ignoring .debug_names."),
4442 filename
, plongest (bytes_read
+ length
),
4443 pulongest (section
->size
));
4447 /* The version number. */
4448 uint16_t version
= read_2_bytes (abfd
, addr
);
4452 warning (_("Section .debug_names in %s has unsupported version %d, "
4453 "ignoring .debug_names."),
4459 uint16_t padding
= read_2_bytes (abfd
, addr
);
4463 warning (_("Section .debug_names in %s has unsupported padding %d, "
4464 "ignoring .debug_names."),
4469 /* comp_unit_count - The number of CUs in the CU list. */
4470 map
.cu_count
= read_4_bytes (abfd
, addr
);
4473 /* local_type_unit_count - The number of TUs in the local TU
4475 map
.tu_count
= read_4_bytes (abfd
, addr
);
4478 /* foreign_type_unit_count - The number of TUs in the foreign TU
4480 uint32_t foreign_tu_count
= read_4_bytes (abfd
, addr
);
4482 if (foreign_tu_count
!= 0)
4484 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4485 "ignoring .debug_names."),
4486 filename
, static_cast<unsigned long> (foreign_tu_count
));
4490 /* bucket_count - The number of hash buckets in the hash lookup
4492 map
.bucket_count
= read_4_bytes (abfd
, addr
);
4495 /* name_count - The number of unique names in the index. */
4496 map
.name_count
= read_4_bytes (abfd
, addr
);
4499 /* abbrev_table_size - The size in bytes of the abbreviations
4501 uint32_t abbrev_table_size
= read_4_bytes (abfd
, addr
);
4504 /* augmentation_string_size - The size in bytes of the augmentation
4505 string. This value is rounded up to a multiple of 4. */
4506 uint32_t augmentation_string_size
= read_4_bytes (abfd
, addr
);
4508 map
.augmentation_is_gdb
= ((augmentation_string_size
4509 == sizeof (dwarf5_augmentation
))
4510 && memcmp (addr
, dwarf5_augmentation
,
4511 sizeof (dwarf5_augmentation
)) == 0);
4512 augmentation_string_size
+= (-augmentation_string_size
) & 3;
4513 addr
+= augmentation_string_size
;
4516 map
.cu_table_reordered
= addr
;
4517 addr
+= map
.cu_count
* map
.offset_size
;
4519 /* List of Local TUs */
4520 map
.tu_table_reordered
= addr
;
4521 addr
+= map
.tu_count
* map
.offset_size
;
4523 /* Hash Lookup Table */
4524 map
.bucket_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4525 addr
+= map
.bucket_count
* 4;
4526 map
.hash_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4527 addr
+= map
.name_count
* 4;
4530 map
.name_table_string_offs_reordered
= addr
;
4531 addr
+= map
.name_count
* map
.offset_size
;
4532 map
.name_table_entry_offs_reordered
= addr
;
4533 addr
+= map
.name_count
* map
.offset_size
;
4535 const gdb_byte
*abbrev_table_start
= addr
;
4538 const ULONGEST index_num
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4543 const auto insertpair
4544 = map
.abbrev_map
.emplace (index_num
, mapped_debug_names::index_val ());
4545 if (!insertpair
.second
)
4547 warning (_("Section .debug_names in %s has duplicate index %s, "
4548 "ignoring .debug_names."),
4549 filename
, pulongest (index_num
));
4552 mapped_debug_names::index_val
&indexval
= insertpair
.first
->second
;
4553 indexval
.dwarf_tag
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4558 mapped_debug_names::index_val::attr attr
;
4559 attr
.dw_idx
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4561 attr
.form
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4563 if (attr
.form
== DW_FORM_implicit_const
)
4565 attr
.implicit_const
= read_signed_leb128 (abfd
, addr
,
4569 if (attr
.dw_idx
== 0 && attr
.form
== 0)
4571 indexval
.attr_vec
.push_back (std::move (attr
));
4574 if (addr
!= abbrev_table_start
+ abbrev_table_size
)
4576 warning (_("Section .debug_names in %s has abbreviation_table "
4577 "of size %s vs. written as %u, ignoring .debug_names."),
4578 filename
, plongest (addr
- abbrev_table_start
),
4582 map
.entry_pool
= addr
;
4587 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4591 create_cus_from_debug_names_list (dwarf2_per_bfd
*per_bfd
,
4592 const mapped_debug_names
&map
,
4593 dwarf2_section_info
§ion
,
4596 if (!map
.augmentation_is_gdb
)
4598 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
4600 sect_offset sect_off
4601 = (sect_offset
) (extract_unsigned_integer
4602 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4604 map
.dwarf5_byte_order
));
4605 /* We don't know the length of the CU, because the CU list in a
4606 .debug_names index can be incomplete, so we can't use the start
4607 of the next CU as end of this CU. We create the CUs here with
4608 length 0, and in cutu_reader::cutu_reader we'll fill in the
4610 dwarf2_per_cu_data_up per_cu
4611 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4613 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4618 sect_offset sect_off_prev
;
4619 for (uint32_t i
= 0; i
<= map
.cu_count
; ++i
)
4621 sect_offset sect_off_next
;
4622 if (i
< map
.cu_count
)
4625 = (sect_offset
) (extract_unsigned_integer
4626 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4628 map
.dwarf5_byte_order
));
4631 sect_off_next
= (sect_offset
) section
.size
;
4634 const ULONGEST length
= sect_off_next
- sect_off_prev
;
4635 dwarf2_per_cu_data_up per_cu
4636 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4637 sect_off_prev
, length
);
4638 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4640 sect_off_prev
= sect_off_next
;
4644 /* Read the CU list from the mapped index, and use it to create all
4645 the CU objects for this dwarf2_per_objfile. */
4648 create_cus_from_debug_names (dwarf2_per_bfd
*per_bfd
,
4649 const mapped_debug_names
&map
,
4650 const mapped_debug_names
&dwz_map
)
4652 gdb_assert (per_bfd
->all_comp_units
.empty ());
4653 per_bfd
->all_comp_units
.reserve (map
.cu_count
+ dwz_map
.cu_count
);
4655 create_cus_from_debug_names_list (per_bfd
, map
, per_bfd
->info
,
4656 false /* is_dwz */);
4658 if (dwz_map
.cu_count
== 0)
4661 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4662 create_cus_from_debug_names_list (per_bfd
, dwz_map
, dwz
->info
,
4666 /* Read .debug_names. If everything went ok, initialize the "quick"
4667 elements of all the CUs and return true. Otherwise, return false. */
4670 dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
4672 std::unique_ptr
<mapped_debug_names
> map (new mapped_debug_names
);
4673 mapped_debug_names dwz_map
;
4674 struct objfile
*objfile
= per_objfile
->objfile
;
4675 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4677 if (!read_debug_names_from_section (objfile
, objfile_name (objfile
),
4678 &per_bfd
->debug_names
, *map
))
4681 /* Don't use the index if it's empty. */
4682 if (map
->name_count
== 0)
4685 /* If there is a .dwz file, read it so we can get its CU list as
4687 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4690 if (!read_debug_names_from_section (objfile
,
4691 bfd_get_filename (dwz
->dwz_bfd
.get ()),
4692 &dwz
->debug_names
, dwz_map
))
4694 warning (_("could not read '.debug_names' section from %s; skipping"),
4695 bfd_get_filename (dwz
->dwz_bfd
.get ()));
4700 create_cus_from_debug_names (per_bfd
, *map
, dwz_map
);
4702 if (map
->tu_count
!= 0)
4704 /* We can only handle a single .debug_types when we have an
4706 if (per_bfd
->types
.size () != 1)
4708 per_bfd
->all_comp_units
.clear ();
4712 dwarf2_section_info
*section
= &per_bfd
->types
[0];
4714 create_signatured_type_table_from_debug_names
4715 (per_objfile
, *map
, section
, &per_bfd
->abbrev
);
4718 create_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
);
4720 per_bfd
->index_table
= std::move (map
);
4721 per_bfd
->quick_file_names_table
=
4722 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
4727 /* Type used to manage iterating over all CUs looking for a symbol for
4730 class dw2_debug_names_iterator
4733 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4734 block_search_flags block_index
,
4736 const char *name
, dwarf2_per_objfile
*per_objfile
)
4737 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4738 m_addr (find_vec_in_debug_names (map
, name
, per_objfile
)),
4739 m_per_objfile (per_objfile
)
4742 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4743 search_domain search
, uint32_t namei
,
4744 dwarf2_per_objfile
*per_objfile
,
4745 domain_enum domain
= UNDEF_DOMAIN
)
4749 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4750 m_per_objfile (per_objfile
)
4753 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4754 block_search_flags block_index
, domain_enum domain
,
4755 uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4756 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4757 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4758 m_per_objfile (per_objfile
)
4761 /* Return the next matching CU or NULL if there are no more. */
4762 dwarf2_per_cu_data
*next ();
4765 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4767 dwarf2_per_objfile
*per_objfile
);
4768 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4770 dwarf2_per_objfile
*per_objfile
);
4772 /* The internalized form of .debug_names. */
4773 const mapped_debug_names
&m_map
;
4775 /* Restrict the search to these blocks. */
4776 block_search_flags m_block_index
= (SEARCH_GLOBAL_BLOCK
4777 | SEARCH_STATIC_BLOCK
);
4779 /* The kind of symbol we're looking for. */
4780 const domain_enum m_domain
= UNDEF_DOMAIN
;
4781 const search_domain m_search
= ALL_DOMAIN
;
4783 /* The list of CUs from the index entry of the symbol, or NULL if
4785 const gdb_byte
*m_addr
;
4787 dwarf2_per_objfile
*m_per_objfile
;
4791 mapped_debug_names::namei_to_name
4792 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const
4794 const ULONGEST namei_string_offs
4795 = extract_unsigned_integer ((name_table_string_offs_reordered
4796 + namei
* offset_size
),
4799 return read_indirect_string_at_offset (per_objfile
, namei_string_offs
);
4802 /* Find a slot in .debug_names for the object named NAME. If NAME is
4803 found, return pointer to its pool data. If NAME cannot be found,
4807 dw2_debug_names_iterator::find_vec_in_debug_names
4808 (const mapped_debug_names
&map
, const char *name
,
4809 dwarf2_per_objfile
*per_objfile
)
4811 int (*cmp
) (const char *, const char *);
4813 gdb::unique_xmalloc_ptr
<char> without_params
;
4814 if (current_language
->la_language
== language_cplus
4815 || current_language
->la_language
== language_fortran
4816 || current_language
->la_language
== language_d
)
4818 /* NAME is already canonical. Drop any qualifiers as
4819 .debug_names does not contain any. */
4821 if (strchr (name
, '(') != NULL
)
4823 without_params
= cp_remove_params (name
);
4824 if (without_params
!= NULL
)
4825 name
= without_params
.get ();
4829 cmp
= (case_sensitivity
== case_sensitive_on
? strcmp
: strcasecmp
);
4831 const uint32_t full_hash
= dwarf5_djb_hash (name
);
4833 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4834 (map
.bucket_table_reordered
4835 + (full_hash
% map
.bucket_count
)), 4,
4836 map
.dwarf5_byte_order
);
4840 if (namei
>= map
.name_count
)
4842 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4844 namei
, map
.name_count
,
4845 objfile_name (per_objfile
->objfile
));
4851 const uint32_t namei_full_hash
4852 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4853 (map
.hash_table_reordered
+ namei
), 4,
4854 map
.dwarf5_byte_order
);
4855 if (full_hash
% map
.bucket_count
!= namei_full_hash
% map
.bucket_count
)
4858 if (full_hash
== namei_full_hash
)
4860 const char *const namei_string
= map
.namei_to_name (namei
, per_objfile
);
4862 #if 0 /* An expensive sanity check. */
4863 if (namei_full_hash
!= dwarf5_djb_hash (namei_string
))
4865 complaint (_("Wrong .debug_names hash for string at index %u "
4867 namei
, objfile_name (dwarf2_per_objfile
->objfile
));
4872 if (cmp (namei_string
, name
) == 0)
4874 const ULONGEST namei_entry_offs
4875 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4876 + namei
* map
.offset_size
),
4877 map
.offset_size
, map
.dwarf5_byte_order
);
4878 return map
.entry_pool
+ namei_entry_offs
;
4883 if (namei
>= map
.name_count
)
4889 dw2_debug_names_iterator::find_vec_in_debug_names
4890 (const mapped_debug_names
&map
, uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4892 if (namei
>= map
.name_count
)
4894 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4896 namei
, map
.name_count
,
4897 objfile_name (per_objfile
->objfile
));
4901 const ULONGEST namei_entry_offs
4902 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4903 + namei
* map
.offset_size
),
4904 map
.offset_size
, map
.dwarf5_byte_order
);
4905 return map
.entry_pool
+ namei_entry_offs
;
4908 /* See dw2_debug_names_iterator. */
4910 dwarf2_per_cu_data
*
4911 dw2_debug_names_iterator::next ()
4916 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4917 struct objfile
*objfile
= m_per_objfile
->objfile
;
4918 bfd
*const abfd
= objfile
->obfd
;
4922 unsigned int bytes_read
;
4923 const ULONGEST abbrev
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4924 m_addr
+= bytes_read
;
4928 const auto indexval_it
= m_map
.abbrev_map
.find (abbrev
);
4929 if (indexval_it
== m_map
.abbrev_map
.cend ())
4931 complaint (_("Wrong .debug_names undefined abbrev code %s "
4933 pulongest (abbrev
), objfile_name (objfile
));
4936 const mapped_debug_names::index_val
&indexval
= indexval_it
->second
;
4937 enum class symbol_linkage
{
4941 } symbol_linkage_
= symbol_linkage::unknown
;
4942 dwarf2_per_cu_data
*per_cu
= NULL
;
4943 for (const mapped_debug_names::index_val::attr
&attr
: indexval
.attr_vec
)
4948 case DW_FORM_implicit_const
:
4949 ull
= attr
.implicit_const
;
4951 case DW_FORM_flag_present
:
4955 ull
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4956 m_addr
+= bytes_read
;
4959 ull
= read_4_bytes (abfd
, m_addr
);
4963 ull
= read_8_bytes (abfd
, m_addr
);
4966 case DW_FORM_ref_sig8
:
4967 ull
= read_8_bytes (abfd
, m_addr
);
4971 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4972 dwarf_form_name (attr
.form
),
4973 objfile_name (objfile
));
4976 switch (attr
.dw_idx
)
4978 case DW_IDX_compile_unit
:
4980 /* Don't crash on bad data. */
4981 int nr_cus
= (per_bfd
->all_comp_units
.size ()
4982 - per_bfd
->tu_stats
.nr_tus
);
4985 complaint (_(".debug_names entry has bad CU index %s"
4988 objfile_name (objfile
));
4992 per_cu
= per_bfd
->get_cu (ull
);
4994 case DW_IDX_type_unit
:
4995 /* Don't crash on bad data. */
4996 if (ull
>= per_bfd
->tu_stats
.nr_tus
)
4998 complaint (_(".debug_names entry has bad TU index %s"
5001 objfile_name (objfile
));
5005 int nr_cus
= (per_bfd
->all_comp_units
.size ()
5006 - per_bfd
->tu_stats
.nr_tus
);
5007 per_cu
= per_bfd
->get_cu (nr_cus
+ ull
);
5010 case DW_IDX_die_offset
:
5011 /* In a per-CU index (as opposed to a per-module index), index
5012 entries without CU attribute implicitly refer to the single CU. */
5014 per_cu
= per_bfd
->get_cu (0);
5016 case DW_IDX_GNU_internal
:
5017 if (!m_map
.augmentation_is_gdb
)
5019 symbol_linkage_
= symbol_linkage::static_
;
5021 case DW_IDX_GNU_external
:
5022 if (!m_map
.augmentation_is_gdb
)
5024 symbol_linkage_
= symbol_linkage::extern_
;
5029 /* Skip if we couldn't find a valid CU/TU index. */
5030 if (per_cu
== nullptr)
5033 /* Skip if already read in. */
5034 if (m_per_objfile
->symtab_set_p (per_cu
))
5037 /* Check static vs global. */
5038 if (symbol_linkage_
!= symbol_linkage::unknown
)
5040 if (symbol_linkage_
== symbol_linkage::static_
)
5042 if ((m_block_index
& SEARCH_STATIC_BLOCK
) == 0)
5047 if ((m_block_index
& SEARCH_GLOBAL_BLOCK
) == 0)
5052 /* Match dw2_symtab_iter_next, symbol_kind
5053 and debug_names::psymbol_tag. */
5057 switch (indexval
.dwarf_tag
)
5059 case DW_TAG_variable
:
5060 case DW_TAG_subprogram
:
5061 /* Some types are also in VAR_DOMAIN. */
5062 case DW_TAG_typedef
:
5063 case DW_TAG_structure_type
:
5070 switch (indexval
.dwarf_tag
)
5072 case DW_TAG_typedef
:
5073 case DW_TAG_structure_type
:
5080 switch (indexval
.dwarf_tag
)
5083 case DW_TAG_variable
:
5090 switch (indexval
.dwarf_tag
)
5102 /* Match dw2_expand_symtabs_matching, symbol_kind and
5103 debug_names::psymbol_tag. */
5106 case VARIABLES_DOMAIN
:
5107 switch (indexval
.dwarf_tag
)
5109 case DW_TAG_variable
:
5115 case FUNCTIONS_DOMAIN
:
5116 switch (indexval
.dwarf_tag
)
5118 case DW_TAG_subprogram
:
5125 switch (indexval
.dwarf_tag
)
5127 case DW_TAG_typedef
:
5128 case DW_TAG_structure_type
:
5134 case MODULES_DOMAIN
:
5135 switch (indexval
.dwarf_tag
)
5149 /* This dumps minimal information about .debug_names. It is called
5150 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5151 uses this to verify that .debug_names has been loaded. */
5154 dwarf2_debug_names_index::dump (struct objfile
*objfile
)
5156 gdb_printf (".debug_names: exists\n");
5160 dwarf2_debug_names_index::expand_matching_symbols
5161 (struct objfile
*objfile
,
5162 const lookup_name_info
&name
, domain_enum domain
,
5164 symbol_compare_ftype
*ordered_compare
)
5166 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5168 mapped_debug_names
&map
5169 = (static_cast<mapped_debug_names
&>
5170 (*per_objfile
->per_bfd
->index_table
.get ()));
5171 const block_search_flags block_flags
5172 = global
? SEARCH_GLOBAL_BLOCK
: SEARCH_STATIC_BLOCK
;
5174 const char *match_name
= name
.ada ().lookup_name ().c_str ();
5175 auto matcher
= [&] (const char *symname
)
5177 if (ordered_compare
== nullptr)
5179 return ordered_compare (symname
, match_name
) == 0;
5182 dw2_expand_symtabs_matching_symbol (map
, name
, matcher
,
5183 [&] (offset_type namei
)
5185 /* The name was matched, now expand corresponding CUs that were
5187 dw2_debug_names_iterator
iter (map
, block_flags
, domain
, namei
,
5190 struct dwarf2_per_cu_data
*per_cu
;
5191 while ((per_cu
= iter
.next ()) != NULL
)
5192 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
5199 dwarf2_debug_names_index::expand_symtabs_matching
5200 (struct objfile
*objfile
,
5201 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
5202 const lookup_name_info
*lookup_name
,
5203 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
5204 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
5205 block_search_flags search_flags
,
5207 enum search_domain kind
)
5209 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5211 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
5213 /* This invariant is documented in quick-functions.h. */
5214 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
5215 if (lookup_name
== nullptr)
5217 for (dwarf2_per_cu_data
*per_cu
5218 : all_comp_units_range (per_objfile
->per_bfd
))
5222 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5230 mapped_debug_names
&map
5231 = (static_cast<mapped_debug_names
&>
5232 (*per_objfile
->per_bfd
->index_table
.get ()));
5235 = dw2_expand_symtabs_matching_symbol (map
, *lookup_name
,
5237 [&] (offset_type namei
)
5239 /* The name was matched, now expand corresponding CUs that were
5241 dw2_debug_names_iterator
iter (map
, kind
, namei
, per_objfile
, domain
);
5243 struct dwarf2_per_cu_data
*per_cu
;
5244 while ((per_cu
= iter
.next ()) != NULL
)
5245 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5255 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5256 to either a dwarf2_per_bfd or dwz_file object. */
5258 template <typename T
>
5259 static gdb::array_view
<const gdb_byte
>
5260 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
5262 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
5264 if (section
->empty ())
5267 /* Older elfutils strip versions could keep the section in the main
5268 executable while splitting it for the separate debug info file. */
5269 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
5272 section
->read (obj
);
5274 /* dwarf2_section_info::size is a bfd_size_type, while
5275 gdb::array_view works with size_t. On 32-bit hosts, with
5276 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5277 is 32-bit. So we need an explicit narrowing conversion here.
5278 This is fine, because it's impossible to allocate or mmap an
5279 array/buffer larger than what size_t can represent. */
5280 return gdb::make_array_view (section
->buffer
, section
->size
);
5283 /* Lookup the index cache for the contents of the index associated to
5286 static gdb::array_view
<const gdb_byte
>
5287 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
5289 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
5290 if (build_id
== nullptr)
5293 return global_index_cache
.lookup_gdb_index (build_id
,
5294 &dwarf2_per_bfd
->index_cache_res
);
5297 /* Same as the above, but for DWZ. */
5299 static gdb::array_view
<const gdb_byte
>
5300 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
5302 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
5303 if (build_id
== nullptr)
5306 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
5309 static quick_symbol_functions_up
make_cooked_index_funcs ();
5311 /* See dwarf2/public.h. */
5314 dwarf2_initialize_objfile (struct objfile
*objfile
)
5316 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5317 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5319 dwarf_read_debug_printf ("called");
5321 /* If we're about to read full symbols, don't bother with the
5322 indices. In this case we also don't care if some other debug
5323 format is making psymtabs, because they are all about to be
5325 if ((objfile
->flags
& OBJF_READNOW
))
5327 dwarf_read_debug_printf ("readnow requested");
5329 create_all_comp_units (per_objfile
);
5330 per_bfd
->quick_file_names_table
5331 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
5333 objfile
->qf
.emplace_front (new readnow_functions
);
5337 /* Was a GDB index already read when we processed an objfile sharing
5339 if (per_bfd
->index_table
!= nullptr)
5341 dwarf_read_debug_printf ("re-using symbols");
5342 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5346 if (dwarf2_read_debug_names (per_objfile
))
5348 dwarf_read_debug_printf ("found debug names");
5349 objfile
->qf
.push_front
5350 (per_bfd
->index_table
->make_quick_functions ());
5354 if (dwarf2_read_gdb_index (per_objfile
,
5355 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
5356 get_gdb_index_contents_from_section
<dwz_file
>))
5358 dwarf_read_debug_printf ("found gdb index from file");
5359 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5363 /* ... otherwise, try to find the index in the index cache. */
5364 if (dwarf2_read_gdb_index (per_objfile
,
5365 get_gdb_index_contents_from_cache
,
5366 get_gdb_index_contents_from_cache_dwz
))
5368 dwarf_read_debug_printf ("found gdb index from cache");
5369 global_index_cache
.hit ();
5370 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5374 global_index_cache
.miss ();
5375 objfile
->qf
.push_front (make_cooked_index_funcs ());
5380 /* Build a partial symbol table. */
5383 dwarf2_build_psymtabs (struct objfile
*objfile
)
5385 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5387 if (per_objfile
->per_bfd
->index_table
!= nullptr)
5392 dwarf2_build_psymtabs_hard (per_objfile
);
5394 /* (maybe) store an index in the cache. */
5395 global_index_cache
.store (per_objfile
);
5397 catch (const gdb_exception_error
&except
)
5399 exception_print (gdb_stderr
, except
);
5403 /* Find the base address of the compilation unit for range lists and
5404 location lists. It will normally be specified by DW_AT_low_pc.
5405 In DWARF-3 draft 4, the base address could be overridden by
5406 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5407 compilation units with discontinuous ranges. */
5410 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
5412 struct attribute
*attr
;
5414 cu
->base_address
.reset ();
5416 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
5417 if (attr
!= nullptr)
5418 cu
->base_address
= attr
->as_address ();
5421 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
5422 if (attr
!= nullptr)
5423 cu
->base_address
= attr
->as_address ();
5427 /* Helper function that returns the proper abbrev section for
5430 static struct dwarf2_section_info
*
5431 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
5433 struct dwarf2_section_info
*abbrev
;
5434 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
5436 if (this_cu
->is_dwz
)
5437 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
5439 abbrev
= &per_bfd
->abbrev
;
5444 /* Fetch the abbreviation table offset from a comp or type unit header. */
5447 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
5448 struct dwarf2_section_info
*section
,
5449 sect_offset sect_off
)
5451 bfd
*abfd
= section
->get_bfd_owner ();
5452 const gdb_byte
*info_ptr
;
5453 unsigned int initial_length_size
, offset_size
;
5456 section
->read (per_objfile
->objfile
);
5457 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
5458 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
5459 offset_size
= initial_length_size
== 4 ? 4 : 8;
5460 info_ptr
+= initial_length_size
;
5462 version
= read_2_bytes (abfd
, info_ptr
);
5466 /* Skip unit type and address size. */
5470 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
5474 hash_signatured_type (const void *item
)
5476 const struct signatured_type
*sig_type
5477 = (const struct signatured_type
*) item
;
5479 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5480 return sig_type
->signature
;
5484 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
5486 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
5487 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
5489 return lhs
->signature
== rhs
->signature
;
5492 /* Allocate a hash table for signatured types. */
5495 allocate_signatured_type_table ()
5497 return htab_up (htab_create_alloc (41,
5498 hash_signatured_type
,
5500 NULL
, xcalloc
, xfree
));
5503 /* A helper for create_debug_types_hash_table. Read types from SECTION
5504 and fill them into TYPES_HTAB. It will process only type units,
5505 therefore DW_UT_type. */
5508 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
5509 struct dwo_file
*dwo_file
,
5510 dwarf2_section_info
*section
, htab_up
&types_htab
,
5511 rcuh_kind section_kind
)
5513 struct objfile
*objfile
= per_objfile
->objfile
;
5514 struct dwarf2_section_info
*abbrev_section
;
5516 const gdb_byte
*info_ptr
, *end_ptr
;
5518 abbrev_section
= &dwo_file
->sections
.abbrev
;
5520 dwarf_read_debug_printf ("Reading %s for %s",
5521 section
->get_name (),
5522 abbrev_section
->get_file_name ());
5524 section
->read (objfile
);
5525 info_ptr
= section
->buffer
;
5527 if (info_ptr
== NULL
)
5530 /* We can't set abfd until now because the section may be empty or
5531 not present, in which case the bfd is unknown. */
5532 abfd
= section
->get_bfd_owner ();
5534 /* We don't use cutu_reader here because we don't need to read
5535 any dies: the signature is in the header. */
5537 end_ptr
= info_ptr
+ section
->size
;
5538 while (info_ptr
< end_ptr
)
5540 signatured_type_up sig_type
;
5541 struct dwo_unit
*dwo_tu
;
5543 const gdb_byte
*ptr
= info_ptr
;
5544 struct comp_unit_head header
;
5545 unsigned int length
;
5547 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
5549 /* Initialize it due to a false compiler warning. */
5550 header
.signature
= -1;
5551 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
5553 /* We need to read the type's signature in order to build the hash
5554 table, but we don't need anything else just yet. */
5556 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
5557 abbrev_section
, ptr
, section_kind
);
5559 length
= header
.get_length ();
5561 /* Skip dummy type units. */
5562 if (ptr
>= info_ptr
+ length
5563 || peek_abbrev_code (abfd
, ptr
) == 0
5564 || (header
.unit_type
!= DW_UT_type
5565 && header
.unit_type
!= DW_UT_split_type
))
5571 if (types_htab
== NULL
)
5572 types_htab
= allocate_dwo_unit_table ();
5574 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
5575 dwo_tu
->dwo_file
= dwo_file
;
5576 dwo_tu
->signature
= header
.signature
;
5577 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
5578 dwo_tu
->section
= section
;
5579 dwo_tu
->sect_off
= sect_off
;
5580 dwo_tu
->length
= length
;
5582 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
5583 gdb_assert (slot
!= NULL
);
5585 complaint (_("debug type entry at offset %s is duplicate to"
5586 " the entry at offset %s, signature %s"),
5587 sect_offset_str (sect_off
),
5588 sect_offset_str (dwo_tu
->sect_off
),
5589 hex_string (header
.signature
));
5592 dwarf_read_debug_printf_v (" offset %s, signature %s",
5593 sect_offset_str (sect_off
),
5594 hex_string (header
.signature
));
5600 /* Create the hash table of all entries in the .debug_types
5601 (or .debug_types.dwo) section(s).
5602 DWO_FILE is a pointer to the DWO file object.
5604 The result is a pointer to the hash table or NULL if there are no types.
5606 Note: This function processes DWO files only, not DWP files. */
5609 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
5610 struct dwo_file
*dwo_file
,
5611 gdb::array_view
<dwarf2_section_info
> type_sections
,
5612 htab_up
&types_htab
)
5614 for (dwarf2_section_info
§ion
: type_sections
)
5615 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
5619 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5620 If SLOT is non-NULL, it is the entry to use in the hash table.
5621 Otherwise we find one. */
5623 static struct signatured_type
*
5624 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
5626 if (per_objfile
->per_bfd
->all_comp_units
.size ()
5627 == per_objfile
->per_bfd
->all_comp_units
.capacity ())
5628 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
5630 signatured_type_up sig_type_holder
5631 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
5632 signatured_type
*sig_type
= sig_type_holder
.get ();
5634 per_objfile
->per_bfd
->all_comp_units
.emplace_back
5635 (sig_type_holder
.release ());
5639 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5642 gdb_assert (*slot
== NULL
);
5644 /* The rest of sig_type must be filled in by the caller. */
5648 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5649 Fill in SIG_ENTRY with DWO_ENTRY. */
5652 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
5653 struct signatured_type
*sig_entry
,
5654 struct dwo_unit
*dwo_entry
)
5656 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5658 /* Make sure we're not clobbering something we don't expect to. */
5659 gdb_assert (! sig_entry
->queued
);
5660 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
5661 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
5662 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
5663 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
5664 || (to_underlying (sig_entry
->type_offset_in_section
)
5665 == to_underlying (dwo_entry
->type_offset_in_tu
)));
5666 gdb_assert (sig_entry
->type_unit_group
== NULL
);
5667 gdb_assert (sig_entry
->dwo_unit
== NULL
5668 || sig_entry
->dwo_unit
== dwo_entry
);
5670 sig_entry
->section
= dwo_entry
->section
;
5671 sig_entry
->sect_off
= dwo_entry
->sect_off
;
5672 sig_entry
->set_length (dwo_entry
->length
, false);
5673 sig_entry
->reading_dwo_directly
= 1;
5674 sig_entry
->per_bfd
= per_bfd
;
5675 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
5676 sig_entry
->dwo_unit
= dwo_entry
;
5679 /* Subroutine of lookup_signatured_type.
5680 If we haven't read the TU yet, create the signatured_type data structure
5681 for a TU to be read in directly from a DWO file, bypassing the stub.
5682 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5683 using .gdb_index, then when reading a CU we want to stay in the DWO file
5684 containing that CU. Otherwise we could end up reading several other DWO
5685 files (due to comdat folding) to process the transitive closure of all the
5686 mentioned TUs, and that can be slow. The current DWO file will have every
5687 type signature that it needs.
5688 We only do this for .gdb_index because in the psymtab case we already have
5689 to read all the DWOs to build the type unit groups. */
5691 static struct signatured_type
*
5692 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5694 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5695 struct dwo_file
*dwo_file
;
5696 struct dwo_unit find_dwo_entry
, *dwo_entry
;
5699 gdb_assert (cu
->dwo_unit
);
5701 /* If TU skeletons have been removed then we may not have read in any
5703 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5704 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5706 /* We only ever need to read in one copy of a signatured type.
5707 Use the global signatured_types array to do our own comdat-folding
5708 of types. If this is the first time we're reading this TU, and
5709 the TU has an entry in .gdb_index, replace the recorded data from
5710 .gdb_index with this TU. */
5712 signatured_type
find_sig_entry (sig
);
5713 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5714 &find_sig_entry
, INSERT
);
5715 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5717 /* We can get here with the TU already read, *or* in the process of being
5718 read. Don't reassign the global entry to point to this DWO if that's
5719 the case. Also note that if the TU is already being read, it may not
5720 have come from a DWO, the program may be a mix of Fission-compiled
5721 code and non-Fission-compiled code. */
5723 /* Have we already tried to read this TU?
5724 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5725 needn't exist in the global table yet). */
5726 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
5729 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5730 dwo_unit of the TU itself. */
5731 dwo_file
= cu
->dwo_unit
->dwo_file
;
5733 /* Ok, this is the first time we're reading this TU. */
5734 if (dwo_file
->tus
== NULL
)
5736 find_dwo_entry
.signature
= sig
;
5737 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
5739 if (dwo_entry
== NULL
)
5742 /* If the global table doesn't have an entry for this TU, add one. */
5743 if (sig_entry
== NULL
)
5744 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5746 if (sig_entry
->dwo_unit
== nullptr)
5747 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5748 sig_entry
->tu_read
= 1;
5752 /* Subroutine of lookup_signatured_type.
5753 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5754 then try the DWP file. If the TU stub (skeleton) has been removed then
5755 it won't be in .gdb_index. */
5757 static struct signatured_type
*
5758 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5760 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5761 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
5762 struct dwo_unit
*dwo_entry
;
5765 gdb_assert (cu
->dwo_unit
);
5766 gdb_assert (dwp_file
!= NULL
);
5768 /* If TU skeletons have been removed then we may not have read in any
5770 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5771 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5773 signatured_type
find_sig_entry (sig
);
5774 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5775 &find_sig_entry
, INSERT
);
5776 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5778 /* Have we already tried to read this TU?
5779 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5780 needn't exist in the global table yet). */
5781 if (sig_entry
!= NULL
)
5784 if (dwp_file
->tus
== NULL
)
5786 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
5787 1 /* is_debug_types */);
5788 if (dwo_entry
== NULL
)
5791 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5792 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5797 /* Lookup a signature based type for DW_FORM_ref_sig8.
5798 Returns NULL if signature SIG is not present in the table.
5799 It is up to the caller to complain about this. */
5801 static struct signatured_type
*
5802 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5804 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5808 /* We're in a DWO/DWP file, and we're using .gdb_index.
5809 These cases require special processing. */
5810 if (get_dwp_file (per_objfile
) == NULL
)
5811 return lookup_dwo_signatured_type (cu
, sig
);
5813 return lookup_dwp_signatured_type (cu
, sig
);
5817 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5819 signatured_type
find_entry (sig
);
5820 return ((struct signatured_type
*)
5821 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
5826 /* Low level DIE reading support. */
5828 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5831 init_cu_die_reader (struct die_reader_specs
*reader
,
5832 struct dwarf2_cu
*cu
,
5833 struct dwarf2_section_info
*section
,
5834 struct dwo_file
*dwo_file
,
5835 struct abbrev_table
*abbrev_table
)
5837 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
5838 reader
->abfd
= section
->get_bfd_owner ();
5840 reader
->dwo_file
= dwo_file
;
5841 reader
->die_section
= section
;
5842 reader
->buffer
= section
->buffer
;
5843 reader
->buffer_end
= section
->buffer
+ section
->size
;
5844 reader
->abbrev_table
= abbrev_table
;
5847 /* Subroutine of cutu_reader to simplify it.
5848 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5849 There's just a lot of work to do, and cutu_reader is big enough
5852 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5853 from it to the DIE in the DWO. If NULL we are skipping the stub.
5854 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5855 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5856 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5857 STUB_COMP_DIR may be non-NULL.
5858 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5859 are filled in with the info of the DIE from the DWO file.
5860 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5861 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5862 kept around for at least as long as *RESULT_READER.
5864 The result is non-zero if a valid (non-dummy) DIE was found. */
5867 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
5868 struct dwo_unit
*dwo_unit
,
5869 struct die_info
*stub_comp_unit_die
,
5870 const char *stub_comp_dir
,
5871 struct die_reader_specs
*result_reader
,
5872 const gdb_byte
**result_info_ptr
,
5873 struct die_info
**result_comp_unit_die
,
5874 abbrev_table_up
*result_dwo_abbrev_table
)
5876 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5877 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
5878 struct objfile
*objfile
= per_objfile
->objfile
;
5880 const gdb_byte
*begin_info_ptr
, *info_ptr
;
5881 struct attribute
*comp_dir
, *stmt_list
, *low_pc
, *high_pc
, *ranges
;
5882 int i
,num_extra_attrs
;
5883 struct dwarf2_section_info
*dwo_abbrev_section
;
5884 struct die_info
*comp_unit_die
;
5886 /* At most one of these may be provided. */
5887 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
5889 /* These attributes aren't processed until later:
5890 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5891 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5892 referenced later. However, these attributes are found in the stub
5893 which we won't have later. In order to not impose this complication
5894 on the rest of the code, we read them here and copy them to the
5903 if (stub_comp_unit_die
!= NULL
)
5905 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5907 if (!per_cu
->is_debug_types
)
5908 stmt_list
= dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
);
5909 low_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
);
5910 high_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
);
5911 ranges
= dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
);
5912 comp_dir
= dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
);
5914 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
5916 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5917 We need the value before we can process DW_AT_ranges values from the
5919 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
5921 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5922 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5923 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5924 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5926 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
5928 else if (stub_comp_dir
!= NULL
)
5930 /* Reconstruct the comp_dir attribute to simplify the code below. */
5931 comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
, struct attribute
);
5932 comp_dir
->name
= DW_AT_comp_dir
;
5933 comp_dir
->form
= DW_FORM_string
;
5934 comp_dir
->set_string_noncanonical (stub_comp_dir
);
5937 /* Set up for reading the DWO CU/TU. */
5938 cu
->dwo_unit
= dwo_unit
;
5939 dwarf2_section_info
*section
= dwo_unit
->section
;
5940 section
->read (objfile
);
5941 abfd
= section
->get_bfd_owner ();
5942 begin_info_ptr
= info_ptr
= (section
->buffer
5943 + to_underlying (dwo_unit
->sect_off
));
5944 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
5946 if (per_cu
->is_debug_types
)
5948 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
5950 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5951 section
, dwo_abbrev_section
,
5952 info_ptr
, rcuh_kind::TYPE
);
5953 /* This is not an assert because it can be caused by bad debug info. */
5954 if (sig_type
->signature
!= cu
->header
.signature
)
5956 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5957 " TU at offset %s [in module %s]"),
5958 hex_string (sig_type
->signature
),
5959 hex_string (cu
->header
.signature
),
5960 sect_offset_str (dwo_unit
->sect_off
),
5961 bfd_get_filename (abfd
));
5963 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5964 /* For DWOs coming from DWP files, we don't know the CU length
5965 nor the type's offset in the TU until now. */
5966 dwo_unit
->length
= cu
->header
.get_length ();
5967 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
5969 /* Establish the type offset that can be used to lookup the type.
5970 For DWO files, we don't know it until now. */
5971 sig_type
->type_offset_in_section
5972 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
5976 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5977 section
, dwo_abbrev_section
,
5978 info_ptr
, rcuh_kind::COMPILE
);
5979 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5980 /* For DWOs coming from DWP files, we don't know the CU length
5982 dwo_unit
->length
= cu
->header
.get_length ();
5985 dwo_abbrev_section
->read (objfile
);
5986 *result_dwo_abbrev_table
5987 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
5988 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
5989 result_dwo_abbrev_table
->get ());
5991 /* Read in the die, but leave space to copy over the attributes
5992 from the stub. This has the benefit of simplifying the rest of
5993 the code - all the work to maintain the illusion of a single
5994 DW_TAG_{compile,type}_unit DIE is done here. */
5995 num_extra_attrs
= ((stmt_list
!= NULL
)
5999 + (comp_dir
!= NULL
));
6000 info_ptr
= read_full_die_1 (result_reader
, result_comp_unit_die
, info_ptr
,
6003 /* Copy over the attributes from the stub to the DIE we just read in. */
6004 comp_unit_die
= *result_comp_unit_die
;
6005 i
= comp_unit_die
->num_attrs
;
6006 if (stmt_list
!= NULL
)
6007 comp_unit_die
->attrs
[i
++] = *stmt_list
;
6009 comp_unit_die
->attrs
[i
++] = *low_pc
;
6010 if (high_pc
!= NULL
)
6011 comp_unit_die
->attrs
[i
++] = *high_pc
;
6013 comp_unit_die
->attrs
[i
++] = *ranges
;
6014 if (comp_dir
!= NULL
)
6015 comp_unit_die
->attrs
[i
++] = *comp_dir
;
6016 comp_unit_die
->num_attrs
+= num_extra_attrs
;
6018 if (dwarf_die_debug
)
6020 gdb_printf (gdb_stdlog
,
6021 "Read die from %s@0x%x of %s:\n",
6022 section
->get_name (),
6023 (unsigned) (begin_info_ptr
- section
->buffer
),
6024 bfd_get_filename (abfd
));
6025 dump_die (comp_unit_die
, dwarf_die_debug
);
6028 /* Skip dummy compilation units. */
6029 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
6030 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6033 *result_info_ptr
= info_ptr
;
6037 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6038 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6039 signature is part of the header. */
6040 static gdb::optional
<ULONGEST
>
6041 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
6043 if (cu
->header
.version
>= 5)
6044 return cu
->header
.signature
;
6045 struct attribute
*attr
;
6046 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
6047 if (attr
== nullptr || !attr
->form_is_unsigned ())
6048 return gdb::optional
<ULONGEST
> ();
6049 return attr
->as_unsigned ();
6052 /* Subroutine of cutu_reader to simplify it.
6053 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6054 Returns NULL if the specified DWO unit cannot be found. */
6056 static struct dwo_unit
*
6057 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
6060 /* We need a lock here both to handle the DWO hash table, and BFD,
6061 which is not thread-safe. */
6062 static std::mutex dwo_lock
;
6064 std::lock_guard
<std::mutex
> guard (dwo_lock
);
6067 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6068 struct dwo_unit
*dwo_unit
;
6069 const char *comp_dir
;
6071 gdb_assert (cu
!= NULL
);
6073 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6074 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6075 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
6077 if (per_cu
->is_debug_types
)
6078 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
6081 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
6083 if (!signature
.has_value ())
6084 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6086 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
6088 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
6094 /* Subroutine of cutu_reader to simplify it.
6095 See it for a description of the parameters.
6096 Read a TU directly from a DWO file, bypassing the stub. */
6099 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
6100 dwarf2_per_objfile
*per_objfile
,
6101 dwarf2_cu
*existing_cu
)
6103 struct signatured_type
*sig_type
;
6105 /* Verify we can do the following downcast, and that we have the
6107 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
6108 sig_type
= (struct signatured_type
*) this_cu
;
6109 gdb_assert (sig_type
->dwo_unit
!= NULL
);
6113 if (existing_cu
!= nullptr)
6116 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
6117 /* There's no need to do the rereading_dwo_cu handling that
6118 cutu_reader does since we don't read the stub. */
6122 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6123 in per_objfile yet. */
6124 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6125 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6126 cu
= m_new_cu
.get ();
6129 /* A future optimization, if needed, would be to use an existing
6130 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6131 could share abbrev tables. */
6133 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
6134 NULL
/* stub_comp_unit_die */,
6135 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
6138 &m_dwo_abbrev_table
) == 0)
6145 /* Initialize a CU (or TU) and read its DIEs.
6146 If the CU defers to a DWO file, read the DWO file as well.
6148 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6149 Otherwise the table specified in the comp unit header is read in and used.
6150 This is an optimization for when we already have the abbrev table.
6152 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6155 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6156 dwarf2_per_objfile
*per_objfile
,
6157 struct abbrev_table
*abbrev_table
,
6158 dwarf2_cu
*existing_cu
,
6160 abbrev_cache
*cache
)
6161 : die_reader_specs
{},
6164 struct objfile
*objfile
= per_objfile
->objfile
;
6165 struct dwarf2_section_info
*section
= this_cu
->section
;
6166 bfd
*abfd
= section
->get_bfd_owner ();
6167 const gdb_byte
*begin_info_ptr
;
6168 struct signatured_type
*sig_type
= NULL
;
6169 struct dwarf2_section_info
*abbrev_section
;
6170 /* Non-zero if CU currently points to a DWO file and we need to
6171 reread it. When this happens we need to reread the skeleton die
6172 before we can reread the DWO file (this only applies to CUs, not TUs). */
6173 int rereading_dwo_cu
= 0;
6175 if (dwarf_die_debug
)
6176 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6177 this_cu
->is_debug_types
? "type" : "comp",
6178 sect_offset_str (this_cu
->sect_off
));
6180 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6181 file (instead of going through the stub), short-circuit all of this. */
6182 if (this_cu
->reading_dwo_directly
)
6184 /* Narrow down the scope of possibilities to have to understand. */
6185 gdb_assert (this_cu
->is_debug_types
);
6186 gdb_assert (abbrev_table
== NULL
);
6187 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
6191 /* This is cheap if the section is already read in. */
6192 section
->read (objfile
);
6194 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6196 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
6200 if (existing_cu
!= nullptr)
6203 /* If this CU is from a DWO file we need to start over, we need to
6204 refetch the attributes from the skeleton CU.
6205 This could be optimized by retrieving those attributes from when we
6206 were here the first time: the previous comp_unit_die was stored in
6207 comp_unit_obstack. But there's no data yet that we need this
6209 if (cu
->dwo_unit
!= NULL
)
6210 rereading_dwo_cu
= 1;
6214 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6215 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6216 let us know that the CU is being scanned using the parallel
6217 indexer. This assert is avoided in this case because (1) it
6218 is irrelevant, and (2) the get_cu method is not
6220 gdb_assert (cache
!= nullptr
6221 || per_objfile
->get_cu (this_cu
) == nullptr);
6222 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6223 cu
= m_new_cu
.get ();
6226 /* Get the header. */
6227 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
6229 /* We already have the header, there's no need to read it in again. */
6230 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
6234 if (this_cu
->is_debug_types
)
6236 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6237 section
, abbrev_section
,
6238 info_ptr
, rcuh_kind::TYPE
);
6240 /* Since per_cu is the first member of struct signatured_type,
6241 we can go from a pointer to one to a pointer to the other. */
6242 sig_type
= (struct signatured_type
*) this_cu
;
6243 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
6244 gdb_assert (sig_type
->type_offset_in_tu
6245 == cu
->header
.type_cu_offset_in_tu
);
6246 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6248 /* LENGTH has not been set yet for type units if we're
6249 using .gdb_index. */
6250 this_cu
->set_length (cu
->header
.get_length ());
6252 /* Establish the type offset that can be used to lookup the type. */
6253 sig_type
->type_offset_in_section
=
6254 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
6256 this_cu
->set_version (cu
->header
.version
);
6260 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6261 section
, abbrev_section
,
6263 rcuh_kind::COMPILE
);
6265 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6266 this_cu
->set_length (cu
->header
.get_length ());
6267 this_cu
->set_version (cu
->header
.version
);
6271 /* Skip dummy compilation units. */
6272 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
6273 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6279 /* If we don't have them yet, read the abbrevs for this compilation unit.
6280 And if we need to read them now, make sure they're freed when we're
6282 if (abbrev_table
!= NULL
)
6283 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
6286 if (cache
!= nullptr)
6287 abbrev_table
= cache
->find (abbrev_section
,
6288 cu
->header
.abbrev_sect_off
);
6289 if (abbrev_table
== nullptr)
6291 abbrev_section
->read (objfile
);
6292 m_abbrev_table_holder
6293 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
6294 abbrev_table
= m_abbrev_table_holder
.get ();
6298 /* Read the top level CU/TU die. */
6299 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
6300 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6302 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
6308 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6309 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6310 table from the DWO file and pass the ownership over to us. It will be
6311 referenced from READER, so we must make sure to free it after we're done
6314 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6315 DWO CU, that this test will fail (the attribute will not be present). */
6316 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6317 if (dwo_name
!= nullptr)
6319 struct dwo_unit
*dwo_unit
;
6320 struct die_info
*dwo_comp_unit_die
;
6322 if (comp_unit_die
->has_children
)
6324 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6325 " has children (offset %s) [in module %s]"),
6326 sect_offset_str (this_cu
->sect_off
),
6327 bfd_get_filename (abfd
));
6329 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
6330 if (dwo_unit
!= NULL
)
6332 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
6333 comp_unit_die
, NULL
,
6336 &m_dwo_abbrev_table
) == 0)
6342 comp_unit_die
= dwo_comp_unit_die
;
6346 /* Yikes, we couldn't find the rest of the DIE, we only have
6347 the stub. A complaint has already been logged. There's
6348 not much more we can do except pass on the stub DIE to
6349 die_reader_func. We don't want to throw an error on bad
6356 cutu_reader::keep ()
6358 /* Done, clean up. */
6359 gdb_assert (!dummy_p
);
6360 if (m_new_cu
!= NULL
)
6362 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6364 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
6365 per_objfile
->set_cu (m_this_cu
, std::move (m_new_cu
));
6369 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6370 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6371 assumed to have already done the lookup to find the DWO file).
6373 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6374 THIS_CU->is_debug_types, but nothing else.
6376 We fill in THIS_CU->length.
6378 THIS_CU->cu is always freed when done.
6379 This is done in order to not leave THIS_CU->cu in a state where we have
6380 to care whether it refers to the "main" CU or the DWO CU.
6382 When parent_cu is passed, it is used to provide a default value for
6383 str_offsets_base and addr_base from the parent. */
6385 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6386 dwarf2_per_objfile
*per_objfile
,
6387 struct dwarf2_cu
*parent_cu
,
6388 struct dwo_file
*dwo_file
)
6389 : die_reader_specs
{},
6392 struct objfile
*objfile
= per_objfile
->objfile
;
6393 struct dwarf2_section_info
*section
= this_cu
->section
;
6394 bfd
*abfd
= section
->get_bfd_owner ();
6395 struct dwarf2_section_info
*abbrev_section
;
6396 const gdb_byte
*begin_info_ptr
, *info_ptr
;
6398 if (dwarf_die_debug
)
6399 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6400 this_cu
->is_debug_types
? "type" : "comp",
6401 sect_offset_str (this_cu
->sect_off
));
6403 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6405 abbrev_section
= (dwo_file
!= NULL
6406 ? &dwo_file
->sections
.abbrev
6407 : get_abbrev_section_for_cu (this_cu
));
6409 /* This is cheap if the section is already read in. */
6410 section
->read (objfile
);
6412 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6414 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6415 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
6416 section
, abbrev_section
, info_ptr
,
6417 (this_cu
->is_debug_types
6419 : rcuh_kind::COMPILE
));
6421 if (parent_cu
!= nullptr)
6423 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
6424 m_new_cu
->addr_base
= parent_cu
->addr_base
;
6426 this_cu
->set_length (m_new_cu
->header
.get_length ());
6428 /* Skip dummy compilation units. */
6429 if (info_ptr
>= begin_info_ptr
+ this_cu
->length ()
6430 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6436 abbrev_section
->read (objfile
);
6437 m_abbrev_table_holder
6438 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
6440 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
6441 m_abbrev_table_holder
.get ());
6442 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6446 /* Type Unit Groups.
6448 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6449 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6450 so that all types coming from the same compilation (.o file) are grouped
6451 together. A future step could be to put the types in the same symtab as
6452 the CU the types ultimately came from. */
6455 hash_type_unit_group (const void *item
)
6457 const struct type_unit_group
*tu_group
6458 = (const struct type_unit_group
*) item
;
6460 return hash_stmt_list_entry (&tu_group
->hash
);
6464 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
6466 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
6467 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
6469 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
6472 /* Allocate a hash table for type unit groups. */
6475 allocate_type_unit_groups_table ()
6477 return htab_up (htab_create_alloc (3,
6478 hash_type_unit_group
,
6480 htab_delete_entry
<type_unit_group
>,
6484 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6485 partial symtabs. We combine several TUs per psymtab to not let the size
6486 of any one psymtab grow too big. */
6487 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6488 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6490 /* Helper routine for get_type_unit_group.
6491 Create the type_unit_group object used to hold one or more TUs. */
6493 static std::unique_ptr
<type_unit_group
>
6494 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
6496 std::unique_ptr
<type_unit_group
> tu_group (new type_unit_group
);
6498 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
6499 tu_group
->hash
.line_sect_off
= line_offset_struct
;
6504 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6505 STMT_LIST is a DW_AT_stmt_list attribute. */
6507 static struct type_unit_group
*
6508 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
6510 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6511 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6512 struct type_unit_group
*tu_group
;
6514 unsigned int line_offset
;
6515 struct type_unit_group type_unit_group_for_lookup
;
6517 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
6518 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
6520 /* Do we need to create a new group, or can we use an existing one? */
6522 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
6524 line_offset
= stmt_list
->as_unsigned ();
6525 ++tu_stats
->nr_symtab_sharers
;
6529 /* Ugh, no stmt_list. Rare, but we have to handle it.
6530 We can do various things here like create one group per TU or
6531 spread them over multiple groups to split up the expansion work.
6532 To avoid worst case scenarios (too many groups or too large groups)
6533 we, umm, group them in bunches. */
6534 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6535 | (tu_stats
->nr_stmt_less_type_units
6536 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
6537 ++tu_stats
->nr_stmt_less_type_units
;
6540 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
6541 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
6542 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
6543 &type_unit_group_for_lookup
, INSERT
);
6544 if (*slot
== nullptr)
6546 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
6547 std::unique_ptr
<type_unit_group
> grp
6548 = create_type_unit_group (cu
, line_offset_struct
);
6549 *slot
= grp
.release ();
6550 ++tu_stats
->nr_symtabs
;
6553 tu_group
= (struct type_unit_group
*) *slot
;
6554 gdb_assert (tu_group
!= nullptr);
6559 /* An instance of this is created when scanning DWARF to create a
6562 class cooked_index_storage
6566 cooked_index_storage ()
6567 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
6569 htab_delete_entry
<cutu_reader
>,
6571 m_index (new cooked_index
)
6575 DISABLE_COPY_AND_ASSIGN (cooked_index_storage
);
6577 /* Return the current abbrev cache. */
6578 abbrev_cache
*get_abbrev_cache ()
6580 return &m_abbrev_cache
;
6583 /* Return the DIE reader corresponding to PER_CU. If no such reader
6584 has been registered, return NULL. */
6585 cutu_reader
*get_reader (dwarf2_per_cu_data
*per_cu
)
6587 int index
= per_cu
->index
;
6588 return (cutu_reader
*) htab_find_with_hash (m_reader_hash
.get (),
6592 /* Preserve READER by storing it in the local hash table. */
6593 cutu_reader
*preserve (std::unique_ptr
<cutu_reader
> reader
)
6595 m_abbrev_cache
.add (reader
->release_abbrev_table ());
6597 int index
= reader
->cu
->per_cu
->index
;
6598 void **slot
= htab_find_slot_with_hash (m_reader_hash
.get (), &index
,
6600 gdb_assert (*slot
== nullptr);
6601 cutu_reader
*result
= reader
.get ();
6602 *slot
= reader
.release ();
6606 /* Add an entry to the index. The arguments describe the entry; see
6607 cooked-index.h. The new entry is returned. */
6608 const cooked_index_entry
*add (sect_offset die_offset
, enum dwarf_tag tag
,
6609 cooked_index_flag flags
,
6611 const cooked_index_entry
*parent_entry
,
6612 dwarf2_per_cu_data
*per_cu
)
6614 return m_index
->add (die_offset
, tag
, flags
, name
, parent_entry
, per_cu
);
6617 /* Install the current addrmap into the index being constructed,
6618 then transfer ownership of the index to the caller. */
6619 std::unique_ptr
<cooked_index
> release ()
6621 m_index
->install_addrmap (&m_addrmap
);
6622 return std::move (m_index
);
6625 /* Return the mutable addrmap that is currently being created. */
6626 addrmap_mutable
*get_addrmap ()
6633 /* Hash function for a cutu_reader. */
6634 static hashval_t
hash_cutu_reader (const void *a
)
6636 const cutu_reader
*reader
= (const cutu_reader
*) a
;
6637 return reader
->cu
->per_cu
->index
;
6640 /* Equality function for cutu_reader. */
6641 static int eq_cutu_reader (const void *a
, const void *b
)
6643 const cutu_reader
*ra
= (const cutu_reader
*) a
;
6644 const int *rb
= (const int *) b
;
6645 return ra
->cu
->per_cu
->index
== *rb
;
6648 /* The abbrev cache used by this indexer. */
6649 abbrev_cache m_abbrev_cache
;
6650 /* A hash table of cutu_reader objects. */
6651 htab_up m_reader_hash
;
6652 /* The index that is being constructed. */
6653 std::unique_ptr
<cooked_index
> m_index
;
6655 /* A writeable addrmap being constructed by this scanner. */
6656 addrmap_mutable m_addrmap
;
6659 /* An instance of this is created to index a CU. */
6661 class cooked_indexer
6665 cooked_indexer (cooked_index_storage
*storage
,
6666 dwarf2_per_cu_data
*per_cu
,
6667 enum language language
)
6668 : m_index_storage (storage
),
6670 m_language (language
)
6674 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
6676 /* Index the given CU. */
6677 void make_index (cutu_reader
*reader
);
6681 /* A helper function to turn a section offset into an address that
6682 can be used in an addrmap. */
6683 CORE_ADDR
form_addr (sect_offset offset
, bool is_dwz
)
6685 CORE_ADDR value
= to_underlying (offset
);
6687 value
|= ((CORE_ADDR
) 1) << (8 * sizeof (CORE_ADDR
) - 1);
6691 /* A helper function to scan the PC bounds of READER and record them
6692 in the storage's addrmap. */
6693 void check_bounds (cutu_reader
*reader
);
6695 /* Ensure that the indicated CU exists. The cutu_reader for it is
6696 returned. FOR_SCANNING is true if the caller intends to scan all
6697 the DIEs in the CU; when false, this use is assumed to be to look
6698 up just a single DIE. */
6699 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
6700 dwarf2_per_objfile
*per_objfile
,
6701 sect_offset sect_off
,
6705 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6706 the entry for the enclosing scope (nullptr at top level). FULLY
6707 is true when a full scan must be done -- in some languages,
6708 function scopes must be fully explored in order to find nested
6709 functions. This returns a pointer to just after the spot where
6711 const gdb_byte
*index_dies (cutu_reader
*reader
,
6712 const gdb_byte
*info_ptr
,
6713 const cooked_index_entry
*parent_entry
,
6716 /* Scan the attributes for a given DIE and update the out
6717 parameters. Returns a pointer to the byte after the DIE. */
6718 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
6719 cutu_reader
*reader
,
6720 const gdb_byte
*watermark_ptr
,
6721 const gdb_byte
*info_ptr
,
6722 const abbrev_info
*abbrev
,
6724 const char **linkage_name
,
6725 cooked_index_flag
*flags
,
6726 sect_offset
*sibling_offset
,
6727 const cooked_index_entry
**parent_entry
,
6728 CORE_ADDR
*maybe_defer
,
6729 bool for_specification
);
6731 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6732 DW_AT_import, and then scanning the referenced CU. Returns a
6733 pointer to the byte after the DIE. */
6734 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
6735 const gdb_byte
*info_ptr
,
6736 const abbrev_info
*abbrev
);
6738 /* Recursively read DIEs, recording the section offsets in
6739 m_die_range_map and then calling index_dies. */
6740 const gdb_byte
*recurse (cutu_reader
*reader
,
6741 const gdb_byte
*info_ptr
,
6742 const cooked_index_entry
*parent_entry
,
6745 /* The storage object, where the results are kept. */
6746 cooked_index_storage
*m_index_storage
;
6747 /* The CU that we are reading on behalf of. This object might be
6748 asked to index one CU but to treat the results as if they come
6749 from some including CU; in this case the including CU would be
6751 dwarf2_per_cu_data
*m_per_cu
;
6752 /* The language that we're assuming when reading. */
6753 enum language m_language
;
6755 /* An addrmap that maps from section offsets (see the form_addr
6756 method) to newly-created entries. See m_deferred_entries to
6758 addrmap_mutable m_die_range_map
;
6760 /* A single deferred entry. */
6761 struct deferred_entry
6763 sect_offset die_offset
;
6765 CORE_ADDR spec_offset
;
6767 cooked_index_flag flags
;
6770 /* The generated DWARF can sometimes have the declaration for a
6771 method in a class (or perhaps namespace) scope, with the
6772 definition appearing outside this scope... just one of the many
6773 bad things about DWARF. In order to handle this situation, we
6774 defer certain entries until the end of scanning, at which point
6775 we'll know the containing context of all the DIEs that we might
6776 have scanned. This vector stores these deferred entries. */
6777 std::vector
<deferred_entry
> m_deferred_entries
;
6780 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6781 Process compilation unit THIS_CU for a psymtab. */
6784 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
6785 dwarf2_per_objfile
*per_objfile
,
6786 cooked_index_storage
*storage
)
6788 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
6789 storage
->get_abbrev_cache ());
6791 if (reader
.comp_unit_die
== nullptr)
6798 else if (this_cu
->is_debug_types
)
6799 build_type_psymtabs_reader (&reader
, storage
);
6800 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
6803 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
6805 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
6807 gdb_assert (storage
!= nullptr);
6808 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->lang ());
6809 indexer
.make_index (&reader
);
6814 /* Reader function for build_type_psymtabs. */
6817 build_type_psymtabs_reader (cutu_reader
*reader
,
6818 cooked_index_storage
*storage
)
6820 struct dwarf2_cu
*cu
= reader
->cu
;
6821 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6822 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
6824 gdb_assert (per_cu
->is_debug_types
);
6826 if (! type_unit_die
->has_children
)
6829 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
6831 gdb_assert (storage
!= nullptr);
6832 cooked_indexer
indexer (storage
, per_cu
, cu
->lang ());
6833 indexer
.make_index (reader
);
6836 /* Struct used to sort TUs by their abbreviation table offset. */
6838 struct tu_abbrev_offset
6840 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
6841 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
6844 /* This is used when sorting. */
6845 bool operator< (const tu_abbrev_offset
&other
) const
6847 return abbrev_offset
< other
.abbrev_offset
;
6850 signatured_type
*sig_type
;
6851 sect_offset abbrev_offset
;
6854 /* Efficiently read all the type units.
6856 The efficiency is because we sort TUs by the abbrev table they use and
6857 only read each abbrev table once. In one program there are 200K TUs
6858 sharing 8K abbrev tables.
6860 The main purpose of this function is to support building the
6861 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6862 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6863 can collapse the search space by grouping them by stmt_list.
6864 The savings can be significant, in the same program from above the 200K TUs
6865 share 8K stmt_list tables.
6867 FUNC is expected to call get_type_unit_group, which will create the
6868 struct type_unit_group if necessary and add it to
6869 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6872 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
6873 cooked_index_storage
*storage
)
6875 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6876 abbrev_table_up abbrev_table
;
6877 sect_offset abbrev_offset
;
6879 /* It's up to the caller to not call us multiple times. */
6880 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
6882 if (per_objfile
->per_bfd
->tu_stats
.nr_tus
== 0)
6885 /* TUs typically share abbrev tables, and there can be way more TUs than
6886 abbrev tables. Sort by abbrev table to reduce the number of times we
6887 read each abbrev table in.
6888 Alternatives are to punt or to maintain a cache of abbrev tables.
6889 This is simpler and efficient enough for now.
6891 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6892 symtab to use). Typically TUs with the same abbrev offset have the same
6893 stmt_list value too so in practice this should work well.
6895 The basic algorithm here is:
6897 sort TUs by abbrev table
6898 for each TU with same abbrev table:
6899 read abbrev table if first user
6900 read TU top level DIE
6901 [IWBN if DWO skeletons had DW_AT_stmt_list]
6904 dwarf_read_debug_printf ("Building type unit groups ...");
6906 /* Sort in a separate table to maintain the order of all_comp_units
6907 for .gdb_index: TU indices directly index all_type_units. */
6908 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
6909 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->tu_stats
.nr_tus
);
6911 for (const auto &cu
: per_objfile
->per_bfd
->all_comp_units
)
6913 if (cu
->is_debug_types
)
6915 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
6916 sorted_by_abbrev
.emplace_back
6917 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
6918 sig_type
->sect_off
));
6922 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
6924 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
6926 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
6928 /* Switch to the next abbrev table if necessary. */
6929 if (abbrev_table
== NULL
6930 || tu
.abbrev_offset
!= abbrev_offset
)
6932 abbrev_offset
= tu
.abbrev_offset
;
6933 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
6935 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
6936 ++tu_stats
->nr_uniq_abbrev_tables
;
6939 cutu_reader
reader (tu
.sig_type
, per_objfile
,
6940 abbrev_table
.get (), nullptr, false);
6941 if (!reader
.dummy_p
)
6942 build_type_psymtabs_reader (&reader
, storage
);
6946 /* Print collected type unit statistics. */
6949 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
6951 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6953 dwarf_read_debug_printf ("Type unit statistics:");
6954 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
6955 dwarf_read_debug_printf (" %d uniq abbrev tables",
6956 tu_stats
->nr_uniq_abbrev_tables
);
6957 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6958 tu_stats
->nr_symtabs
);
6959 dwarf_read_debug_printf (" %d symtab sharers",
6960 tu_stats
->nr_symtab_sharers
);
6961 dwarf_read_debug_printf (" %d type units without a stmt_list",
6962 tu_stats
->nr_stmt_less_type_units
);
6963 dwarf_read_debug_printf (" %d all_type_units reallocs",
6964 tu_stats
->nr_all_type_units_reallocs
);
6967 struct skeleton_data
6969 dwarf2_per_objfile
*per_objfile
;
6970 cooked_index_storage
*storage
;
6973 /* Traversal function for process_skeletonless_type_unit.
6974 Read a TU in a DWO file and build partial symbols for it. */
6977 process_skeletonless_type_unit (void **slot
, void *info
)
6979 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
6980 skeleton_data
*data
= (skeleton_data
*) info
;
6982 /* If this TU doesn't exist in the global table, add it and read it in. */
6984 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
6985 data
->per_objfile
->per_bfd
->signatured_types
6986 = allocate_signatured_type_table ();
6988 signatured_type
find_entry (dwo_unit
->signature
);
6989 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
6990 &find_entry
, INSERT
);
6991 /* If we've already seen this type there's nothing to do. What's happening
6992 is we're doing our own version of comdat-folding here. */
6996 /* This does the job that create_all_comp_units would have done for
6998 signatured_type
*entry
6999 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
7000 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
7003 /* This does the job that build_type_psymtabs would have done. */
7004 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
7005 if (!reader
.dummy_p
)
7006 build_type_psymtabs_reader (&reader
, data
->storage
);
7011 /* Traversal function for process_skeletonless_type_units. */
7014 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
7016 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
7018 if (dwo_file
->tus
!= NULL
)
7019 htab_traverse_noresize (dwo_file
->tus
.get (),
7020 process_skeletonless_type_unit
, info
);
7025 /* Scan all TUs of DWO files, verifying we've processed them.
7026 This is needed in case a TU was emitted without its skeleton.
7027 Note: This can't be done until we know what all the DWO files are. */
7030 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
7031 cooked_index_storage
*storage
)
7033 skeleton_data data
{ per_objfile
, storage
};
7035 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7036 if (get_dwp_file (per_objfile
) == NULL
7037 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
7039 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
7040 process_dwo_file_for_skeletonless_type_units
,
7045 /* Build the partial symbol table by doing a quick pass through the
7046 .debug_info and .debug_abbrev sections. */
7049 dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
)
7051 struct objfile
*objfile
= per_objfile
->objfile
;
7052 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7054 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7055 objfile_name (objfile
));
7057 per_bfd
->map_info_sections (objfile
);
7059 cooked_index_storage index_storage
;
7060 create_all_comp_units (per_objfile
);
7061 build_type_psymtabs (per_objfile
, &index_storage
);
7062 std::vector
<std::unique_ptr
<cooked_index
>> indexes
;
7064 per_bfd
->quick_file_names_table
7065 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
7066 if (!per_bfd
->debug_aranges
.empty ())
7067 read_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
,
7068 index_storage
.get_addrmap ());
7071 /* Ensure that complaints are handled correctly. */
7072 complaint_interceptor complaint_handler
;
7074 using iter_type
= decltype (per_bfd
->all_comp_units
.begin ());
7076 /* Each thread returns a pair holding a cooked index, and a vector
7077 of errors that should be printed. The latter is done because
7078 GDB's I/O system is not thread-safe. run_on_main_thread could be
7079 used, but that would mean the messages are printed after the
7080 prompt, which looks weird. */
7081 using result_type
= std::pair
<std::unique_ptr
<cooked_index
>,
7082 std::vector
<gdb_exception
>>;
7083 std::vector
<result_type
> results
7084 = gdb::parallel_for_each (1, per_bfd
->all_comp_units
.begin (),
7085 per_bfd
->all_comp_units
.end (),
7086 [=] (iter_type iter
, iter_type end
)
7088 std::vector
<gdb_exception
> errors
;
7089 cooked_index_storage thread_storage
;
7090 for (; iter
!= end
; ++iter
)
7092 dwarf2_per_cu_data
*per_cu
= iter
->get ();
7095 process_psymtab_comp_unit (per_cu
, per_objfile
,
7098 catch (gdb_exception
&except
)
7100 errors
.push_back (std::move (except
));
7103 return result_type (thread_storage
.release (), std::move (errors
));
7106 /* Only show a given exception a single time. */
7107 std::unordered_set
<gdb_exception
> seen_exceptions
;
7108 for (auto &one_result
: results
)
7110 indexes
.push_back (std::move (one_result
.first
));
7111 for (auto &one_exc
: one_result
.second
)
7112 if (seen_exceptions
.insert (one_exc
).second
)
7113 exception_print (gdb_stderr
, one_exc
);
7117 /* This has to wait until we read the CUs, we need the list of DWOs. */
7118 process_skeletonless_type_units (per_objfile
, &index_storage
);
7120 if (dwarf_read_debug
> 0)
7121 print_tu_stats (per_objfile
);
7123 indexes
.push_back (index_storage
.release ());
7124 /* Remove any NULL entries. This might happen if parallel-for
7125 decides to throttle the number of threads that were used. */
7127 (std::remove_if (indexes
.begin (),
7129 [] (const std::unique_ptr
<cooked_index
> &entry
)
7131 return entry
== nullptr;
7134 indexes
.shrink_to_fit ();
7136 cooked_index_vector
*vec
= new cooked_index_vector (std::move (indexes
));
7137 per_bfd
->index_table
.reset (vec
);
7139 const cooked_index_entry
*main_entry
= vec
->get_main ();
7140 if (main_entry
!= nullptr)
7141 set_objfile_main_name (objfile
, main_entry
->name
,
7142 main_entry
->per_cu
->lang ());
7144 dwarf_read_debug_printf ("Done building psymtabs of %s",
7145 objfile_name (objfile
));
7149 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
7150 struct dwarf2_section_info
*section
,
7151 struct dwarf2_section_info
*abbrev_section
,
7152 unsigned int is_dwz
,
7153 htab_up
&types_htab
,
7154 rcuh_kind section_kind
)
7156 const gdb_byte
*info_ptr
;
7157 struct objfile
*objfile
= per_objfile
->objfile
;
7159 dwarf_read_debug_printf ("Reading %s for %s",
7160 section
->get_name (),
7161 section
->get_file_name ());
7163 section
->read (objfile
);
7165 info_ptr
= section
->buffer
;
7167 while (info_ptr
< section
->buffer
+ section
->size
)
7169 dwarf2_per_cu_data_up this_cu
;
7171 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
7173 comp_unit_head cu_header
;
7174 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
7175 abbrev_section
, info_ptr
,
7178 /* Save the compilation unit for later lookup. */
7179 if (cu_header
.unit_type
!= DW_UT_type
)
7180 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
7183 if (types_htab
== nullptr)
7184 types_htab
= allocate_signatured_type_table ();
7186 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
7187 (cu_header
.signature
);
7188 signatured_type
*sig_ptr
= sig_type
.get ();
7189 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
7190 this_cu
.reset (sig_type
.release ());
7192 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
7193 gdb_assert (slot
!= nullptr);
7194 if (*slot
!= nullptr)
7195 complaint (_("debug type entry at offset %s is duplicate to"
7196 " the entry at offset %s, signature %s"),
7197 sect_offset_str (sect_off
),
7198 sect_offset_str (sig_ptr
->sect_off
),
7199 hex_string (sig_ptr
->signature
));
7202 this_cu
->sect_off
= sect_off
;
7203 this_cu
->set_length (cu_header
.get_length ());
7204 this_cu
->is_dwz
= is_dwz
;
7205 this_cu
->section
= section
;
7206 /* Init this asap, to avoid a data race in the set_version in
7207 cutu_reader::cutu_reader (which may be run in parallel for the cooked
7209 this_cu
->set_version (cu_header
.version
);
7211 info_ptr
= info_ptr
+ this_cu
->length ();
7212 per_objfile
->per_bfd
->all_comp_units
.push_back (std::move (this_cu
));
7216 /* Create a list of all compilation units in OBJFILE.
7217 This is only done for -readnow and building partial symtabs. */
7220 create_all_comp_units (dwarf2_per_objfile
*per_objfile
)
7223 gdb_assert (per_objfile
->per_bfd
->all_comp_units
.empty ());
7225 read_comp_units_from_section (per_objfile
, &per_objfile
->per_bfd
->info
,
7226 &per_objfile
->per_bfd
->abbrev
, 0,
7227 types_htab
, rcuh_kind::COMPILE
);
7228 for (dwarf2_section_info
§ion
: per_objfile
->per_bfd
->types
)
7229 read_comp_units_from_section (per_objfile
, §ion
,
7230 &per_objfile
->per_bfd
->abbrev
, 0,
7231 types_htab
, rcuh_kind::TYPE
);
7233 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
7236 /* Pre-read the sections we'll need to construct an index. */
7237 struct objfile
*objfile
= per_objfile
->objfile
;
7238 dwz
->abbrev
.read (objfile
);
7239 dwz
->info
.read (objfile
);
7240 dwz
->str
.read (objfile
);
7241 dwz
->line
.read (objfile
);
7242 read_comp_units_from_section (per_objfile
, &dwz
->info
, &dwz
->abbrev
, 1,
7243 types_htab
, rcuh_kind::COMPILE
);
7246 per_objfile
->per_bfd
->signatured_types
= std::move (types_htab
);
7249 /* Return the initial uleb128 in the die at INFO_PTR. */
7252 peek_abbrev_code (bfd
*abfd
, const gdb_byte
*info_ptr
)
7254 unsigned int bytes_read
;
7256 return read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7259 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7260 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7262 Return the corresponding abbrev, or NULL if the number is zero (indicating
7263 an empty DIE). In either case *BYTES_READ will be set to the length of
7264 the initial number. */
7266 static const struct abbrev_info
*
7267 peek_die_abbrev (const die_reader_specs
&reader
,
7268 const gdb_byte
*info_ptr
, unsigned int *bytes_read
)
7270 dwarf2_cu
*cu
= reader
.cu
;
7271 bfd
*abfd
= reader
.abfd
;
7272 unsigned int abbrev_number
7273 = read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
7275 if (abbrev_number
== 0)
7278 const abbrev_info
*abbrev
7279 = reader
.abbrev_table
->lookup_abbrev (abbrev_number
);
7282 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7283 " at offset %s [in module %s]"),
7284 abbrev_number
, cu
->per_cu
->is_debug_types
? "TU" : "CU",
7285 sect_offset_str (cu
->header
.sect_off
), bfd_get_filename (abfd
));
7291 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7292 Returns a pointer to the end of a series of DIEs, terminated by an empty
7293 DIE. Any children of the skipped DIEs will also be skipped. */
7295 static const gdb_byte
*
7296 skip_children (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
)
7300 unsigned int bytes_read
;
7301 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
7305 return info_ptr
+ bytes_read
;
7307 info_ptr
= skip_one_die (reader
, info_ptr
+ bytes_read
, abbrev
);
7311 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7312 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7313 abbrev corresponding to that skipped uleb128 should be passed in
7316 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7317 returns a pointer to this DIE's sibling, skipping any children.
7318 Otherwise, returns a pointer to the DIE's first child. */
7320 static const gdb_byte
*
7321 skip_one_die (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
,
7322 const struct abbrev_info
*abbrev
, bool do_skip_children
)
7324 unsigned int bytes_read
;
7325 struct attribute attr
;
7326 bfd
*abfd
= reader
->abfd
;
7327 struct dwarf2_cu
*cu
= reader
->cu
;
7328 const gdb_byte
*buffer
= reader
->buffer
;
7329 const gdb_byte
*buffer_end
= reader
->buffer_end
;
7330 unsigned int form
, i
;
7332 if (do_skip_children
&& abbrev
->sibling_offset
!= (unsigned short) -1)
7334 /* We only handle DW_FORM_ref4 here. */
7335 const gdb_byte
*sibling_data
= info_ptr
+ abbrev
->sibling_offset
;
7336 unsigned int offset
= read_4_bytes (abfd
, sibling_data
);
7337 const gdb_byte
*sibling_ptr
7338 = buffer
+ to_underlying (cu
->header
.sect_off
) + offset
;
7339 if (sibling_ptr
>= info_ptr
&& sibling_ptr
< reader
->buffer_end
)
7341 /* Fall through to the slow way. */
7343 else if (abbrev
->size_if_constant
!= 0)
7345 info_ptr
+= abbrev
->size_if_constant
;
7346 if (do_skip_children
&& abbrev
->has_children
)
7347 return skip_children (reader
, info_ptr
);
7351 for (i
= 0; i
< abbrev
->num_attrs
; i
++)
7353 /* The only abbrev we care about is DW_AT_sibling. */
7354 if (do_skip_children
&& abbrev
->attrs
[i
].name
== DW_AT_sibling
)
7356 read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
7357 if (attr
.form
== DW_FORM_ref_addr
)
7358 complaint (_("ignoring absolute DW_AT_sibling"));
7361 sect_offset off
= attr
.get_ref_die_offset ();
7362 const gdb_byte
*sibling_ptr
= buffer
+ to_underlying (off
);
7364 if (sibling_ptr
< info_ptr
)
7365 complaint (_("DW_AT_sibling points backwards"));
7366 else if (sibling_ptr
> reader
->buffer_end
)
7367 reader
->die_section
->overflow_complaint ();
7373 /* If it isn't DW_AT_sibling, skip this attribute. */
7374 form
= abbrev
->attrs
[i
].form
;
7378 case DW_FORM_ref_addr
:
7379 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7380 and later it is offset sized. */
7381 if (cu
->header
.version
== 2)
7382 info_ptr
+= cu
->header
.addr_size
;
7384 info_ptr
+= cu
->header
.offset_size
;
7386 case DW_FORM_GNU_ref_alt
:
7387 info_ptr
+= cu
->header
.offset_size
;
7390 info_ptr
+= cu
->header
.addr_size
;
7398 case DW_FORM_flag_present
:
7399 case DW_FORM_implicit_const
:
7416 case DW_FORM_ref_sig8
:
7419 case DW_FORM_data16
:
7422 case DW_FORM_string
:
7423 read_direct_string (abfd
, info_ptr
, &bytes_read
);
7424 info_ptr
+= bytes_read
;
7426 case DW_FORM_sec_offset
:
7428 case DW_FORM_GNU_strp_alt
:
7429 info_ptr
+= cu
->header
.offset_size
;
7431 case DW_FORM_exprloc
:
7433 info_ptr
+= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7434 info_ptr
+= bytes_read
;
7436 case DW_FORM_block1
:
7437 info_ptr
+= 1 + read_1_byte (abfd
, info_ptr
);
7439 case DW_FORM_block2
:
7440 info_ptr
+= 2 + read_2_bytes (abfd
, info_ptr
);
7442 case DW_FORM_block4
:
7443 info_ptr
+= 4 + read_4_bytes (abfd
, info_ptr
);
7449 case DW_FORM_ref_udata
:
7450 case DW_FORM_GNU_addr_index
:
7451 case DW_FORM_GNU_str_index
:
7452 case DW_FORM_rnglistx
:
7453 case DW_FORM_loclistx
:
7454 info_ptr
= safe_skip_leb128 (info_ptr
, buffer_end
);
7456 case DW_FORM_indirect
:
7457 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7458 info_ptr
+= bytes_read
;
7459 /* We need to continue parsing from here, so just go back to
7461 goto skip_attribute
;
7464 error (_("Dwarf Error: Cannot handle %s "
7465 "in DWARF reader [in module %s]"),
7466 dwarf_form_name (form
),
7467 bfd_get_filename (abfd
));
7471 if (do_skip_children
&& abbrev
->has_children
)
7472 return skip_children (reader
, info_ptr
);
7477 /* Reading in full CUs. */
7479 /* Add PER_CU to the queue. */
7482 queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
7483 dwarf2_per_objfile
*per_objfile
,
7484 enum language pretend_language
)
7488 gdb_assert (per_objfile
->queue
.has_value ());
7489 per_objfile
->queue
->emplace (per_cu
, per_objfile
, pretend_language
);
7492 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7494 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7497 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7498 DIEs, false otherwise.
7500 Explanation: there is an invariant that if a CU is queued for expansion
7501 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7502 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7503 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7504 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7505 invariant is respected.
7507 The caller is therefore not required to load the CU's DIEs (we return false)
7510 - the CU is already expanded, and therefore does not get enqueued
7511 - the CU gets enqueued for expansion, but its DIEs are already loaded
7513 Note that the caller should not use this function's return value as an
7514 indicator of whether the CU's DIEs are loaded right now, it should check
7515 that by calling `dwarf2_per_objfile::get_cu` instead. */
7518 maybe_queue_comp_unit (struct dwarf2_cu
*dependent_cu
,
7519 dwarf2_per_cu_data
*per_cu
,
7520 dwarf2_per_objfile
*per_objfile
,
7521 enum language pretend_language
)
7523 /* Mark the dependence relation so that we don't flush PER_CU
7525 if (dependent_cu
!= NULL
)
7526 dependent_cu
->add_dependence (per_cu
);
7528 /* If it's already on the queue, we have nothing to do. */
7531 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7533 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
7535 /* If the CU is queued for expansion, it should not already be
7537 gdb_assert (!per_objfile
->symtab_set_p (per_cu
));
7539 /* The DIEs are already loaded, the caller doesn't need to do it. */
7543 bool queued
= false;
7544 if (!per_objfile
->symtab_set_p (per_cu
))
7546 /* Add it to the queue. */
7547 queue_comp_unit (per_cu
, per_objfile
, pretend_language
);
7551 /* If the compilation unit is already loaded, just mark it as
7553 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7557 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7558 and the DIEs are not already loaded. */
7559 return queued
&& cu
== nullptr;
7562 /* Process the queue. */
7565 process_queue (dwarf2_per_objfile
*per_objfile
)
7567 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7568 objfile_name (per_objfile
->objfile
));
7570 /* The queue starts out with one item, but following a DIE reference
7571 may load a new CU, adding it to the end of the queue. */
7572 while (!per_objfile
->queue
->empty ())
7574 dwarf2_queue_item
&item
= per_objfile
->queue
->front ();
7575 dwarf2_per_cu_data
*per_cu
= item
.per_cu
;
7577 if (!per_objfile
->symtab_set_p (per_cu
))
7579 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7581 /* Skip dummy CUs. */
7584 unsigned int debug_print_threshold
;
7587 if (per_cu
->is_debug_types
)
7589 struct signatured_type
*sig_type
=
7590 (struct signatured_type
*) per_cu
;
7592 sprintf (buf
, "TU %s at offset %s",
7593 hex_string (sig_type
->signature
),
7594 sect_offset_str (per_cu
->sect_off
));
7595 /* There can be 100s of TUs.
7596 Only print them in verbose mode. */
7597 debug_print_threshold
= 2;
7601 sprintf (buf
, "CU at offset %s",
7602 sect_offset_str (per_cu
->sect_off
));
7603 debug_print_threshold
= 1;
7606 if (dwarf_read_debug
>= debug_print_threshold
)
7607 dwarf_read_debug_printf ("Expanding symtab of %s", buf
);
7609 if (per_cu
->is_debug_types
)
7610 process_full_type_unit (cu
, item
.pretend_language
);
7612 process_full_comp_unit (cu
, item
.pretend_language
);
7614 if (dwarf_read_debug
>= debug_print_threshold
)
7615 dwarf_read_debug_printf ("Done expanding %s", buf
);
7620 per_objfile
->queue
->pop ();
7623 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7624 objfile_name (per_objfile
->objfile
));
7627 /* Trivial hash function for die_info: the hash value of a DIE
7628 is its offset in .debug_info for this objfile. */
7631 die_hash (const void *item
)
7633 const struct die_info
*die
= (const struct die_info
*) item
;
7635 return to_underlying (die
->sect_off
);
7638 /* Trivial comparison function for die_info structures: two DIEs
7639 are equal if they have the same offset. */
7642 die_eq (const void *item_lhs
, const void *item_rhs
)
7644 const struct die_info
*die_lhs
= (const struct die_info
*) item_lhs
;
7645 const struct die_info
*die_rhs
= (const struct die_info
*) item_rhs
;
7647 return die_lhs
->sect_off
== die_rhs
->sect_off
;
7650 /* Load the DIEs associated with PER_CU into memory.
7652 In some cases, the caller, while reading partial symbols, will need to load
7653 the full symbols for the CU for some reason. It will already have a
7654 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7655 rather than creating a new one. */
7658 load_full_comp_unit (dwarf2_per_cu_data
*this_cu
,
7659 dwarf2_per_objfile
*per_objfile
,
7660 dwarf2_cu
*existing_cu
,
7662 enum language pretend_language
)
7664 gdb_assert (! this_cu
->is_debug_types
);
7666 cutu_reader
reader (this_cu
, per_objfile
, NULL
, existing_cu
, skip_partial
);
7670 struct dwarf2_cu
*cu
= reader
.cu
;
7671 const gdb_byte
*info_ptr
= reader
.info_ptr
;
7673 gdb_assert (cu
->die_hash
== NULL
);
7675 htab_create_alloc_ex (cu
->header
.length
/ 12,
7679 &cu
->comp_unit_obstack
,
7680 hashtab_obstack_allocate
,
7681 dummy_obstack_deallocate
);
7683 if (reader
.comp_unit_die
->has_children
)
7684 reader
.comp_unit_die
->child
7685 = read_die_and_siblings (&reader
, reader
.info_ptr
,
7686 &info_ptr
, reader
.comp_unit_die
);
7687 cu
->dies
= reader
.comp_unit_die
;
7688 /* comp_unit_die is not stored in die_hash, no need. */
7690 /* We try not to read any attributes in this function, because not
7691 all CUs needed for references have been loaded yet, and symbol
7692 table processing isn't initialized. But we have to set the CU language,
7693 or we won't be able to build types correctly.
7694 Similarly, if we do not read the producer, we can not apply
7695 producer-specific interpretation. */
7696 prepare_one_comp_unit (cu
, cu
->dies
, pretend_language
);
7701 /* Add a DIE to the delayed physname list. */
7704 add_to_method_list (struct type
*type
, int fnfield_index
, int index
,
7705 const char *name
, struct die_info
*die
,
7706 struct dwarf2_cu
*cu
)
7708 struct delayed_method_info mi
;
7710 mi
.fnfield_index
= fnfield_index
;
7714 cu
->method_list
.push_back (mi
);
7717 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7718 "const" / "volatile". If so, decrements LEN by the length of the
7719 modifier and return true. Otherwise return false. */
7723 check_modifier (const char *physname
, size_t &len
, const char (&mod
)[N
])
7725 size_t mod_len
= sizeof (mod
) - 1;
7726 if (len
> mod_len
&& startswith (physname
+ (len
- mod_len
), mod
))
7734 /* Compute the physnames of any methods on the CU's method list.
7736 The computation of method physnames is delayed in order to avoid the
7737 (bad) condition that one of the method's formal parameters is of an as yet
7741 compute_delayed_physnames (struct dwarf2_cu
*cu
)
7743 /* Only C++ delays computing physnames. */
7744 if (cu
->method_list
.empty ())
7746 gdb_assert (cu
->lang () == language_cplus
);
7748 for (const delayed_method_info
&mi
: cu
->method_list
)
7750 const char *physname
;
7751 struct fn_fieldlist
*fn_flp
7752 = &TYPE_FN_FIELDLIST (mi
.type
, mi
.fnfield_index
);
7753 physname
= dwarf2_physname (mi
.name
, mi
.die
, cu
);
7754 TYPE_FN_FIELD_PHYSNAME (fn_flp
->fn_fields
, mi
.index
)
7755 = physname
? physname
: "";
7757 /* Since there's no tag to indicate whether a method is a
7758 const/volatile overload, extract that information out of the
7760 if (physname
!= NULL
)
7762 size_t len
= strlen (physname
);
7766 if (physname
[len
] == ')') /* shortcut */
7768 else if (check_modifier (physname
, len
, " const"))
7769 TYPE_FN_FIELD_CONST (fn_flp
->fn_fields
, mi
.index
) = 1;
7770 else if (check_modifier (physname
, len
, " volatile"))
7771 TYPE_FN_FIELD_VOLATILE (fn_flp
->fn_fields
, mi
.index
) = 1;
7778 /* The list is no longer needed. */
7779 cu
->method_list
.clear ();
7782 /* Go objects should be embedded in a DW_TAG_module DIE,
7783 and it's not clear if/how imported objects will appear.
7784 To keep Go support simple until that's worked out,
7785 go back through what we've read and create something usable.
7786 We could do this while processing each DIE, and feels kinda cleaner,
7787 but that way is more invasive.
7788 This is to, for example, allow the user to type "p var" or "b main"
7789 without having to specify the package name, and allow lookups
7790 of module.object to work in contexts that use the expression
7794 fixup_go_packaging (struct dwarf2_cu
*cu
)
7796 gdb::unique_xmalloc_ptr
<char> package_name
;
7797 struct pending
*list
;
7800 for (list
= *cu
->get_builder ()->get_global_symbols ();
7804 for (i
= 0; i
< list
->nsyms
; ++i
)
7806 struct symbol
*sym
= list
->symbol
[i
];
7808 if (sym
->language () == language_go
7809 && sym
->aclass () == LOC_BLOCK
)
7811 gdb::unique_xmalloc_ptr
<char> this_package_name
7812 (go_symbol_package_name (sym
));
7814 if (this_package_name
== NULL
)
7816 if (package_name
== NULL
)
7817 package_name
= std::move (this_package_name
);
7820 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7821 if (strcmp (package_name
.get (), this_package_name
.get ()) != 0)
7822 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7823 (sym
->symtab () != NULL
7824 ? symtab_to_filename_for_display
7826 : objfile_name (objfile
)),
7827 this_package_name
.get (), package_name
.get ());
7833 if (package_name
!= NULL
)
7835 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7836 const char *saved_package_name
= objfile
->intern (package_name
.get ());
7837 struct type
*type
= init_type (objfile
, TYPE_CODE_MODULE
, 0,
7838 saved_package_name
);
7841 sym
= new (&objfile
->objfile_obstack
) symbol
;
7842 sym
->set_language (language_go
, &objfile
->objfile_obstack
);
7843 sym
->compute_and_set_names (saved_package_name
, false, objfile
->per_bfd
);
7844 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7845 e.g., "main" finds the "main" module and not C's main(). */
7846 sym
->set_domain (STRUCT_DOMAIN
);
7847 sym
->set_aclass_index (LOC_TYPEDEF
);
7848 sym
->set_type (type
);
7850 add_symbol_to_list (sym
, cu
->get_builder ()->get_global_symbols ());
7854 /* Allocate a fully-qualified name consisting of the two parts on the
7858 rust_fully_qualify (struct obstack
*obstack
, const char *p1
, const char *p2
)
7860 return obconcat (obstack
, p1
, "::", p2
, (char *) NULL
);
7863 /* A helper that allocates a variant part to attach to a Rust enum
7864 type. OBSTACK is where the results should be allocated. TYPE is
7865 the type we're processing. DISCRIMINANT_INDEX is the index of the
7866 discriminant. It must be the index of one of the fields of TYPE,
7867 or -1 to mean there is no discriminant (univariant enum).
7868 DEFAULT_INDEX is the index of the default field; or -1 if there is
7869 no default. RANGES is indexed by "effective" field number (the
7870 field index, but omitting the discriminant and default fields) and
7871 must hold the discriminant values used by the variants. Note that
7872 RANGES must have a lifetime at least as long as OBSTACK -- either
7873 already allocated on it, or static. */
7876 alloc_rust_variant (struct obstack
*obstack
, struct type
*type
,
7877 int discriminant_index
, int default_index
,
7878 gdb::array_view
<discriminant_range
> ranges
)
7880 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7881 gdb_assert (discriminant_index
== -1
7882 || (discriminant_index
>= 0
7883 && discriminant_index
< type
->num_fields ()));
7884 gdb_assert (default_index
== -1
7885 || (default_index
>= 0 && default_index
< type
->num_fields ()));
7887 /* We have one variant for each non-discriminant field. */
7888 int n_variants
= type
->num_fields ();
7889 if (discriminant_index
!= -1)
7892 variant
*variants
= new (obstack
) variant
[n_variants
];
7895 for (int i
= 0; i
< type
->num_fields (); ++i
)
7897 if (i
== discriminant_index
)
7900 variants
[var_idx
].first_field
= i
;
7901 variants
[var_idx
].last_field
= i
+ 1;
7903 /* The default field does not need a range, but other fields do.
7904 We skipped the discriminant above. */
7905 if (i
!= default_index
)
7907 variants
[var_idx
].discriminants
= ranges
.slice (range_idx
, 1);
7914 gdb_assert (range_idx
== ranges
.size ());
7915 gdb_assert (var_idx
== n_variants
);
7917 variant_part
*part
= new (obstack
) variant_part
;
7918 part
->discriminant_index
= discriminant_index
;
7919 /* If there is no discriminant, then whether it is signed is of no
7922 = (discriminant_index
== -1
7924 : type
->field (discriminant_index
).type ()->is_unsigned ());
7925 part
->variants
= gdb::array_view
<variant
> (variants
, n_variants
);
7927 void *storage
= obstack_alloc (obstack
, sizeof (gdb::array_view
<variant_part
>));
7928 gdb::array_view
<variant_part
> *prop_value
7929 = new (storage
) gdb::array_view
<variant_part
> (part
, 1);
7931 struct dynamic_prop prop
;
7932 prop
.set_variant_parts (prop_value
);
7934 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
7937 /* Some versions of rustc emitted enums in an unusual way.
7939 Ordinary enums were emitted as unions. The first element of each
7940 structure in the union was named "RUST$ENUM$DISR". This element
7941 held the discriminant.
7943 These versions of Rust also implemented the "non-zero"
7944 optimization. When the enum had two values, and one is empty and
7945 the other holds a pointer that cannot be zero, the pointer is used
7946 as the discriminant, with a zero value meaning the empty variant.
7947 Here, the union's first member is of the form
7948 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7949 where the fieldnos are the indices of the fields that should be
7950 traversed in order to find the field (which may be several fields deep)
7951 and the variantname is the name of the variant of the case when the
7954 This function recognizes whether TYPE is of one of these forms,
7955 and, if so, smashes it to be a variant type. */
7958 quirk_rust_enum (struct type
*type
, struct objfile
*objfile
)
7960 gdb_assert (type
->code () == TYPE_CODE_UNION
);
7962 /* We don't need to deal with empty enums. */
7963 if (type
->num_fields () == 0)
7966 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7967 if (type
->num_fields () == 1
7968 && startswith (type
->field (0).name (), RUST_ENUM_PREFIX
))
7970 const char *name
= type
->field (0).name () + strlen (RUST_ENUM_PREFIX
);
7972 /* Decode the field name to find the offset of the
7974 ULONGEST bit_offset
= 0;
7975 struct type
*field_type
= type
->field (0).type ();
7976 while (name
[0] >= '0' && name
[0] <= '9')
7979 unsigned long index
= strtoul (name
, &tail
, 10);
7982 || index
>= field_type
->num_fields ()
7983 || (field_type
->field (index
).loc_kind ()
7984 != FIELD_LOC_KIND_BITPOS
))
7986 complaint (_("Could not parse Rust enum encoding string \"%s\""
7988 type
->field (0).name (),
7989 objfile_name (objfile
));
7994 bit_offset
+= field_type
->field (index
).loc_bitpos ();
7995 field_type
= field_type
->field (index
).type ();
7998 /* Smash this type to be a structure type. We have to do this
7999 because the type has already been recorded. */
8000 type
->set_code (TYPE_CODE_STRUCT
);
8001 type
->set_num_fields (3);
8002 /* Save the field we care about. */
8003 struct field saved_field
= type
->field (0);
8005 ((struct field
*) TYPE_ZALLOC (type
, 3 * sizeof (struct field
)));
8007 /* Put the discriminant at index 0. */
8008 type
->field (0).set_type (field_type
);
8009 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8010 type
->field (0).set_name ("<<discriminant>>");
8011 type
->field (0).set_loc_bitpos (bit_offset
);
8013 /* The order of fields doesn't really matter, so put the real
8014 field at index 1 and the data-less field at index 2. */
8015 type
->field (1) = saved_field
;
8016 type
->field (1).set_name
8017 (rust_last_path_segment (type
->field (1).type ()->name ()));
8018 type
->field (1).type ()->set_name
8019 (rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8020 type
->field (1).name ()));
8022 const char *dataless_name
8023 = rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8025 struct type
*dataless_type
= init_type (objfile
, TYPE_CODE_VOID
, 0,
8027 type
->field (2).set_type (dataless_type
);
8028 /* NAME points into the original discriminant name, which
8029 already has the correct lifetime. */
8030 type
->field (2).set_name (name
);
8031 type
->field (2).set_loc_bitpos (0);
8033 /* Indicate that this is a variant type. */
8034 static discriminant_range ranges
[1] = { { 0, 0 } };
8035 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, 1, ranges
);
8037 /* A union with a single anonymous field is probably an old-style
8039 else if (type
->num_fields () == 1 && streq (type
->field (0).name (), ""))
8041 /* Smash this type to be a structure type. We have to do this
8042 because the type has already been recorded. */
8043 type
->set_code (TYPE_CODE_STRUCT
);
8045 struct type
*field_type
= type
->field (0).type ();
8046 const char *variant_name
8047 = rust_last_path_segment (field_type
->name ());
8048 type
->field (0).set_name (variant_name
);
8049 field_type
->set_name
8050 (rust_fully_qualify (&objfile
->objfile_obstack
,
8051 type
->name (), variant_name
));
8053 alloc_rust_variant (&objfile
->objfile_obstack
, type
, -1, 0, {});
8057 struct type
*disr_type
= nullptr;
8058 for (int i
= 0; i
< type
->num_fields (); ++i
)
8060 disr_type
= type
->field (i
).type ();
8062 if (disr_type
->code () != TYPE_CODE_STRUCT
)
8064 /* All fields of a true enum will be structs. */
8067 else if (disr_type
->num_fields () == 0)
8069 /* Could be data-less variant, so keep going. */
8070 disr_type
= nullptr;
8072 else if (strcmp (disr_type
->field (0).name (),
8073 "RUST$ENUM$DISR") != 0)
8075 /* Not a Rust enum. */
8085 /* If we got here without a discriminant, then it's probably
8087 if (disr_type
== nullptr)
8090 /* Smash this type to be a structure type. We have to do this
8091 because the type has already been recorded. */
8092 type
->set_code (TYPE_CODE_STRUCT
);
8094 /* Make space for the discriminant field. */
8095 struct field
*disr_field
= &disr_type
->field (0);
8097 = (struct field
*) TYPE_ZALLOC (type
, ((type
->num_fields () + 1)
8098 * sizeof (struct field
)));
8099 memcpy (new_fields
+ 1, type
->fields (),
8100 type
->num_fields () * sizeof (struct field
));
8101 type
->set_fields (new_fields
);
8102 type
->set_num_fields (type
->num_fields () + 1);
8104 /* Install the discriminant at index 0 in the union. */
8105 type
->field (0) = *disr_field
;
8106 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8107 type
->field (0).set_name ("<<discriminant>>");
8109 /* We need a way to find the correct discriminant given a
8110 variant name. For convenience we build a map here. */
8111 struct type
*enum_type
= disr_field
->type ();
8112 std::unordered_map
<std::string
, ULONGEST
> discriminant_map
;
8113 for (int i
= 0; i
< enum_type
->num_fields (); ++i
)
8115 if (enum_type
->field (i
).loc_kind () == FIELD_LOC_KIND_ENUMVAL
)
8118 = rust_last_path_segment (enum_type
->field (i
).name ());
8119 discriminant_map
[name
] = enum_type
->field (i
).loc_enumval ();
8123 int n_fields
= type
->num_fields ();
8124 /* We don't need a range entry for the discriminant, but we do
8125 need one for every other field, as there is no default
8127 discriminant_range
*ranges
= XOBNEWVEC (&objfile
->objfile_obstack
,
8130 /* Skip the discriminant here. */
8131 for (int i
= 1; i
< n_fields
; ++i
)
8133 /* Find the final word in the name of this variant's type.
8134 That name can be used to look up the correct
8136 const char *variant_name
8137 = rust_last_path_segment (type
->field (i
).type ()->name ());
8139 auto iter
= discriminant_map
.find (variant_name
);
8140 if (iter
!= discriminant_map
.end ())
8142 ranges
[i
- 1].low
= iter
->second
;
8143 ranges
[i
- 1].high
= iter
->second
;
8146 /* In Rust, each element should have the size of the
8148 TYPE_LENGTH (type
->field (i
).type ()) = TYPE_LENGTH (type
);
8150 /* Remove the discriminant field, if it exists. */
8151 struct type
*sub_type
= type
->field (i
).type ();
8152 if (sub_type
->num_fields () > 0)
8154 sub_type
->set_num_fields (sub_type
->num_fields () - 1);
8155 sub_type
->set_fields (sub_type
->fields () + 1);
8157 type
->field (i
).set_name (variant_name
);
8159 (rust_fully_qualify (&objfile
->objfile_obstack
,
8160 type
->name (), variant_name
));
8163 /* Indicate that this is a variant type. */
8164 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, -1,
8165 gdb::array_view
<discriminant_range
> (ranges
,
8170 /* Rewrite some Rust unions to be structures with variants parts. */
8173 rust_union_quirks (struct dwarf2_cu
*cu
)
8175 gdb_assert (cu
->lang () == language_rust
);
8176 for (type
*type_
: cu
->rust_unions
)
8177 quirk_rust_enum (type_
, cu
->per_objfile
->objfile
);
8178 /* We don't need this any more. */
8179 cu
->rust_unions
.clear ();
8184 type_unit_group_unshareable
*
8185 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group
*tu_group
)
8187 auto iter
= this->m_type_units
.find (tu_group
);
8188 if (iter
!= this->m_type_units
.end ())
8189 return iter
->second
.get ();
8191 type_unit_group_unshareable_up
uniq (new type_unit_group_unshareable
);
8192 type_unit_group_unshareable
*result
= uniq
.get ();
8193 this->m_type_units
[tu_group
] = std::move (uniq
);
8198 dwarf2_per_objfile::get_type_for_signatured_type
8199 (signatured_type
*sig_type
) const
8201 auto iter
= this->m_type_map
.find (sig_type
);
8202 if (iter
== this->m_type_map
.end ())
8205 return iter
->second
;
8208 void dwarf2_per_objfile::set_type_for_signatured_type
8209 (signatured_type
*sig_type
, struct type
*type
)
8211 gdb_assert (this->m_type_map
.find (sig_type
) == this->m_type_map
.end ());
8213 this->m_type_map
[sig_type
] = type
;
8216 /* A helper function for computing the list of all symbol tables
8217 included by PER_CU. */
8220 recursively_compute_inclusions (std::vector
<compunit_symtab
*> *result
,
8221 htab_t all_children
, htab_t all_type_symtabs
,
8222 dwarf2_per_cu_data
*per_cu
,
8223 dwarf2_per_objfile
*per_objfile
,
8224 struct compunit_symtab
*immediate_parent
)
8226 void **slot
= htab_find_slot (all_children
, per_cu
, INSERT
);
8229 /* This inclusion and its children have been processed. */
8235 /* Only add a CU if it has a symbol table. */
8236 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8239 /* If this is a type unit only add its symbol table if we haven't
8240 seen it yet (type unit per_cu's can share symtabs). */
8241 if (per_cu
->is_debug_types
)
8243 slot
= htab_find_slot (all_type_symtabs
, cust
, INSERT
);
8247 result
->push_back (cust
);
8248 if (cust
->user
== NULL
)
8249 cust
->user
= immediate_parent
;
8254 result
->push_back (cust
);
8255 if (cust
->user
== NULL
)
8256 cust
->user
= immediate_parent
;
8260 if (!per_cu
->imported_symtabs_empty ())
8261 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8263 recursively_compute_inclusions (result
, all_children
,
8264 all_type_symtabs
, ptr
, per_objfile
,
8269 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8273 compute_compunit_symtab_includes (dwarf2_per_cu_data
*per_cu
,
8274 dwarf2_per_objfile
*per_objfile
)
8276 gdb_assert (! per_cu
->is_debug_types
);
8278 if (!per_cu
->imported_symtabs_empty ())
8281 std::vector
<compunit_symtab
*> result_symtabs
;
8282 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8284 /* If we don't have a symtab, we can just skip this case. */
8288 htab_up
all_children (htab_create_alloc (1, htab_hash_pointer
,
8290 NULL
, xcalloc
, xfree
));
8291 htab_up
all_type_symtabs (htab_create_alloc (1, htab_hash_pointer
,
8293 NULL
, xcalloc
, xfree
));
8295 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8297 recursively_compute_inclusions (&result_symtabs
, all_children
.get (),
8298 all_type_symtabs
.get (), ptr
,
8302 /* Now we have a transitive closure of all the included symtabs. */
8303 len
= result_symtabs
.size ();
8305 = XOBNEWVEC (&per_objfile
->objfile
->objfile_obstack
,
8306 struct compunit_symtab
*, len
+ 1);
8307 memcpy (cust
->includes
, result_symtabs
.data (),
8308 len
* sizeof (compunit_symtab
*));
8309 cust
->includes
[len
] = NULL
;
8313 /* Compute the 'includes' field for the symtabs of all the CUs we just
8317 process_cu_includes (dwarf2_per_objfile
*per_objfile
)
8319 for (dwarf2_per_cu_data
*iter
: per_objfile
->per_bfd
->just_read_cus
)
8321 if (! iter
->is_debug_types
)
8322 compute_compunit_symtab_includes (iter
, per_objfile
);
8325 per_objfile
->per_bfd
->just_read_cus
.clear ();
8328 /* Generate full symbol information for CU, whose DIEs have
8329 already been loaded into memory. */
8332 process_full_comp_unit (dwarf2_cu
*cu
, enum language pretend_language
)
8334 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8335 struct objfile
*objfile
= per_objfile
->objfile
;
8336 struct gdbarch
*gdbarch
= objfile
->arch ();
8337 CORE_ADDR lowpc
, highpc
;
8338 struct compunit_symtab
*cust
;
8340 struct block
*static_block
;
8343 baseaddr
= objfile
->text_section_offset ();
8345 /* Clear the list here in case something was left over. */
8346 cu
->method_list
.clear ();
8348 dwarf2_find_base_address (cu
->dies
, cu
);
8350 /* Before we start reading the top-level DIE, ensure it has a valid tag
8352 switch (cu
->dies
->tag
)
8354 case DW_TAG_compile_unit
:
8355 case DW_TAG_partial_unit
:
8356 case DW_TAG_type_unit
:
8359 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8360 dwarf_tag_name (cu
->dies
->tag
),
8361 sect_offset_str (cu
->per_cu
->sect_off
),
8362 objfile_name (per_objfile
->objfile
));
8365 /* Do line number decoding in read_file_scope () */
8366 process_die (cu
->dies
, cu
);
8368 /* For now fudge the Go package. */
8369 if (cu
->lang () == language_go
)
8370 fixup_go_packaging (cu
);
8372 /* Now that we have processed all the DIEs in the CU, all the types
8373 should be complete, and it should now be safe to compute all of the
8375 compute_delayed_physnames (cu
);
8377 if (cu
->lang () == language_rust
)
8378 rust_union_quirks (cu
);
8380 /* Some compilers don't define a DW_AT_high_pc attribute for the
8381 compilation unit. If the DW_AT_high_pc is missing, synthesize
8382 it, by scanning the DIE's below the compilation unit. */
8383 get_scope_pc_bounds (cu
->dies
, &lowpc
, &highpc
, cu
);
8385 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
8387 = cu
->get_builder ()->end_compunit_symtab_get_static_block (addr
, 0, 1);
8389 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8390 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8391 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8392 addrmap to help ensure it has an accurate map of pc values belonging to
8394 dwarf2_record_block_ranges (cu
->dies
, static_block
, baseaddr
, cu
);
8396 cust
= cu
->get_builder ()->end_compunit_symtab_from_static_block
8397 (static_block
, SECT_OFF_TEXT (objfile
), 0);
8401 int gcc_4_minor
= producer_is_gcc_ge_4 (cu
->producer
);
8403 /* Set symtab language to language from DW_AT_language. If the
8404 compilation is from a C file generated by language preprocessors, do
8405 not set the language if it was already deduced by start_subfile. */
8406 if (!(cu
->lang () == language_c
8407 && cust
->primary_filetab ()->language () != language_unknown
))
8408 cust
->primary_filetab ()->set_language (cu
->lang ());
8410 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8411 produce DW_AT_location with location lists but it can be possibly
8412 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8413 there were bugs in prologue debug info, fixed later in GCC-4.5
8414 by "unwind info for epilogues" patch (which is not directly related).
8416 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8417 needed, it would be wrong due to missing DW_AT_producer there.
8419 Still one can confuse GDB by using non-standard GCC compilation
8420 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8422 if (cu
->has_loclist
&& gcc_4_minor
>= 5)
8423 cust
->set_locations_valid (true);
8425 if (gcc_4_minor
>= 5)
8426 cust
->set_epilogue_unwind_valid (true);
8428 cust
->set_call_site_htab (cu
->call_site_htab
);
8431 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8433 /* Push it for inclusion processing later. */
8434 per_objfile
->per_bfd
->just_read_cus
.push_back (cu
->per_cu
);
8436 /* Not needed any more. */
8437 cu
->reset_builder ();
8440 /* Generate full symbol information for type unit CU, whose DIEs have
8441 already been loaded into memory. */
8444 process_full_type_unit (dwarf2_cu
*cu
,
8445 enum language pretend_language
)
8447 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8448 struct objfile
*objfile
= per_objfile
->objfile
;
8449 struct compunit_symtab
*cust
;
8450 struct signatured_type
*sig_type
;
8452 gdb_assert (cu
->per_cu
->is_debug_types
);
8453 sig_type
= (struct signatured_type
*) cu
->per_cu
;
8455 /* Clear the list here in case something was left over. */
8456 cu
->method_list
.clear ();
8458 /* The symbol tables are set up in read_type_unit_scope. */
8459 process_die (cu
->dies
, cu
);
8461 /* For now fudge the Go package. */
8462 if (cu
->lang () == language_go
)
8463 fixup_go_packaging (cu
);
8465 /* Now that we have processed all the DIEs in the CU, all the types
8466 should be complete, and it should now be safe to compute all of the
8468 compute_delayed_physnames (cu
);
8470 if (cu
->lang () == language_rust
)
8471 rust_union_quirks (cu
);
8473 /* TUs share symbol tables.
8474 If this is the first TU to use this symtab, complete the construction
8475 of it with end_expandable_symtab. Otherwise, complete the addition of
8476 this TU's symbols to the existing symtab. */
8477 type_unit_group_unshareable
*tug_unshare
=
8478 per_objfile
->get_type_unit_group_unshareable (sig_type
->type_unit_group
);
8479 if (tug_unshare
->compunit_symtab
== NULL
)
8481 buildsym_compunit
*builder
= cu
->get_builder ();
8482 cust
= builder
->end_expandable_symtab (0, SECT_OFF_TEXT (objfile
));
8483 tug_unshare
->compunit_symtab
= cust
;
8487 /* Set symtab language to language from DW_AT_language. If the
8488 compilation is from a C file generated by language preprocessors,
8489 do not set the language if it was already deduced by
8491 if (!(cu
->lang () == language_c
8492 && cust
->primary_filetab ()->language () != language_c
))
8493 cust
->primary_filetab ()->set_language (cu
->lang ());
8498 cu
->get_builder ()->augment_type_symtab ();
8499 cust
= tug_unshare
->compunit_symtab
;
8502 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8504 /* Not needed any more. */
8505 cu
->reset_builder ();
8508 /* Process an imported unit DIE. */
8511 process_imported_unit_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8513 struct attribute
*attr
;
8515 /* For now we don't handle imported units in type units. */
8516 if (cu
->per_cu
->is_debug_types
)
8518 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8519 " supported in type units [in module %s]"),
8520 objfile_name (cu
->per_objfile
->objfile
));
8523 attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
8526 sect_offset sect_off
= attr
->get_ref_die_offset ();
8527 bool is_dwz
= (attr
->form
== DW_FORM_GNU_ref_alt
|| cu
->per_cu
->is_dwz
);
8528 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8529 dwarf2_per_cu_data
*per_cu
8530 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
8531 per_objfile
->per_bfd
);
8533 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8534 into another compilation unit, at root level. Regard this as a hint,
8535 and ignore it. This is a best effort, it only works if unit_type and
8536 lang are already set. */
8537 if (die
->parent
&& die
->parent
->parent
== NULL
8538 && per_cu
->unit_type (false) == DW_UT_compile
8539 && per_cu
->lang (false) == language_cplus
)
8542 /* If necessary, add it to the queue and load its DIEs. */
8543 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
8545 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
8546 false, cu
->lang ());
8548 cu
->per_cu
->imported_symtabs_push (per_cu
);
8552 /* RAII object that represents a process_die scope: i.e.,
8553 starts/finishes processing a DIE. */
8554 class process_die_scope
8557 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
8558 : m_die (die
), m_cu (cu
)
8560 /* We should only be processing DIEs not already in process. */
8561 gdb_assert (!m_die
->in_process
);
8562 m_die
->in_process
= true;
8565 ~process_die_scope ()
8567 m_die
->in_process
= false;
8569 /* If we're done processing the DIE for the CU that owns the line
8570 header, we don't need the line header anymore. */
8571 if (m_cu
->line_header_die_owner
== m_die
)
8573 delete m_cu
->line_header
;
8574 m_cu
->line_header
= NULL
;
8575 m_cu
->line_header_die_owner
= NULL
;
8584 /* Process a die and its children. */
8587 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8589 process_die_scope
scope (die
, cu
);
8593 case DW_TAG_padding
:
8595 case DW_TAG_compile_unit
:
8596 case DW_TAG_partial_unit
:
8597 read_file_scope (die
, cu
);
8599 case DW_TAG_type_unit
:
8600 read_type_unit_scope (die
, cu
);
8602 case DW_TAG_subprogram
:
8603 /* Nested subprograms in Fortran get a prefix. */
8604 if (cu
->lang () == language_fortran
8605 && die
->parent
!= NULL
8606 && die
->parent
->tag
== DW_TAG_subprogram
)
8607 cu
->processing_has_namespace_info
= true;
8609 case DW_TAG_inlined_subroutine
:
8610 read_func_scope (die
, cu
);
8612 case DW_TAG_lexical_block
:
8613 case DW_TAG_try_block
:
8614 case DW_TAG_catch_block
:
8615 read_lexical_block_scope (die
, cu
);
8617 case DW_TAG_call_site
:
8618 case DW_TAG_GNU_call_site
:
8619 read_call_site_scope (die
, cu
);
8621 case DW_TAG_class_type
:
8622 case DW_TAG_interface_type
:
8623 case DW_TAG_structure_type
:
8624 case DW_TAG_union_type
:
8625 case DW_TAG_namelist
:
8626 process_structure_scope (die
, cu
);
8628 case DW_TAG_enumeration_type
:
8629 process_enumeration_scope (die
, cu
);
8632 /* These dies have a type, but processing them does not create
8633 a symbol or recurse to process the children. Therefore we can
8634 read them on-demand through read_type_die. */
8635 case DW_TAG_subroutine_type
:
8636 case DW_TAG_set_type
:
8637 case DW_TAG_pointer_type
:
8638 case DW_TAG_ptr_to_member_type
:
8639 case DW_TAG_reference_type
:
8640 case DW_TAG_rvalue_reference_type
:
8641 case DW_TAG_string_type
:
8644 case DW_TAG_array_type
:
8645 /* We only need to handle this case for Ada -- in other
8646 languages, it's normal for the compiler to emit a typedef
8648 if (cu
->lang () != language_ada
)
8651 case DW_TAG_base_type
:
8652 case DW_TAG_subrange_type
:
8653 case DW_TAG_generic_subrange
:
8654 case DW_TAG_typedef
:
8655 /* Add a typedef symbol for the type definition, if it has a
8657 new_symbol (die
, read_type_die (die
, cu
), cu
);
8659 case DW_TAG_common_block
:
8660 read_common_block (die
, cu
);
8662 case DW_TAG_common_inclusion
:
8664 case DW_TAG_namespace
:
8665 cu
->processing_has_namespace_info
= true;
8666 read_namespace (die
, cu
);
8669 cu
->processing_has_namespace_info
= true;
8670 read_module (die
, cu
);
8672 case DW_TAG_imported_declaration
:
8673 cu
->processing_has_namespace_info
= true;
8674 if (read_alias (die
, cu
))
8676 /* The declaration is neither a global namespace nor a variable
8679 case DW_TAG_imported_module
:
8680 cu
->processing_has_namespace_info
= true;
8681 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
8682 || cu
->lang () != language_fortran
))
8683 complaint (_("Tag '%s' has unexpected children"),
8684 dwarf_tag_name (die
->tag
));
8685 read_import_statement (die
, cu
);
8688 case DW_TAG_imported_unit
:
8689 process_imported_unit_die (die
, cu
);
8692 case DW_TAG_variable
:
8693 read_variable (die
, cu
);
8697 new_symbol (die
, NULL
, cu
);
8702 /* DWARF name computation. */
8704 /* A helper function for dwarf2_compute_name which determines whether DIE
8705 needs to have the name of the scope prepended to the name listed in the
8709 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
8711 struct attribute
*attr
;
8715 case DW_TAG_namespace
:
8716 case DW_TAG_typedef
:
8717 case DW_TAG_class_type
:
8718 case DW_TAG_interface_type
:
8719 case DW_TAG_structure_type
:
8720 case DW_TAG_union_type
:
8721 case DW_TAG_enumeration_type
:
8722 case DW_TAG_enumerator
:
8723 case DW_TAG_subprogram
:
8724 case DW_TAG_inlined_subroutine
:
8726 case DW_TAG_imported_declaration
:
8729 case DW_TAG_variable
:
8730 case DW_TAG_constant
:
8731 /* We only need to prefix "globally" visible variables. These include
8732 any variable marked with DW_AT_external or any variable that
8733 lives in a namespace. [Variables in anonymous namespaces
8734 require prefixing, but they are not DW_AT_external.] */
8736 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
8738 struct dwarf2_cu
*spec_cu
= cu
;
8740 return die_needs_namespace (die_specification (die
, &spec_cu
),
8744 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
8745 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
8746 && die
->parent
->tag
!= DW_TAG_module
)
8748 /* A variable in a lexical block of some kind does not need a
8749 namespace, even though in C++ such variables may be external
8750 and have a mangled name. */
8751 if (die
->parent
->tag
== DW_TAG_lexical_block
8752 || die
->parent
->tag
== DW_TAG_try_block
8753 || die
->parent
->tag
== DW_TAG_catch_block
8754 || die
->parent
->tag
== DW_TAG_subprogram
)
8763 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8764 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8765 defined for the given DIE. */
8767 static struct attribute
*
8768 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
8770 struct attribute
*attr
;
8772 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
8774 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8779 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8780 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8781 defined for the given DIE. */
8784 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
8786 const char *linkage_name
;
8788 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
8789 if (linkage_name
== NULL
)
8790 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8792 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8793 See https://github.com/rust-lang/rust/issues/32925. */
8794 if (cu
->lang () == language_rust
&& linkage_name
!= NULL
8795 && strchr (linkage_name
, '{') != NULL
)
8796 linkage_name
= NULL
;
8798 return linkage_name
;
8801 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8802 compute the physname for the object, which include a method's:
8803 - formal parameters (C++),
8804 - receiver type (Go),
8806 The term "physname" is a bit confusing.
8807 For C++, for example, it is the demangled name.
8808 For Go, for example, it's the mangled name.
8810 For Ada, return the DIE's linkage name rather than the fully qualified
8811 name. PHYSNAME is ignored..
8813 The result is allocated on the objfile->per_bfd's obstack and
8817 dwarf2_compute_name (const char *name
,
8818 struct die_info
*die
, struct dwarf2_cu
*cu
,
8821 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
8824 name
= dwarf2_name (die
, cu
);
8826 enum language lang
= cu
->lang ();
8828 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8829 but otherwise compute it by typename_concat inside GDB.
8830 FIXME: Actually this is not really true, or at least not always true.
8831 It's all very confusing. compute_and_set_names doesn't try to demangle
8832 Fortran names because there is no mangling standard. So new_symbol
8833 will set the demangled name to the result of dwarf2_full_name, and it is
8834 the demangled name that GDB uses if it exists. */
8835 if (lang
== language_ada
8836 || (lang
== language_fortran
&& physname
))
8838 /* For Ada unit, we prefer the linkage name over the name, as
8839 the former contains the exported name, which the user expects
8840 to be able to reference. Ideally, we want the user to be able
8841 to reference this entity using either natural or linkage name,
8842 but we haven't started looking at this enhancement yet. */
8843 const char *linkage_name
= dw2_linkage_name (die
, cu
);
8845 if (linkage_name
!= NULL
)
8846 return linkage_name
;
8849 /* These are the only languages we know how to qualify names in. */
8851 && (lang
== language_cplus
8852 || lang
== language_fortran
|| lang
== language_d
8853 || lang
== language_rust
))
8855 if (die_needs_namespace (die
, cu
))
8858 const char *canonical_name
= NULL
;
8862 prefix
= determine_prefix (die
, cu
);
8863 if (*prefix
!= '\0')
8865 gdb::unique_xmalloc_ptr
<char> prefixed_name
8866 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
8868 buf
.puts (prefixed_name
.get ());
8873 /* Template parameters may be specified in the DIE's DW_AT_name, or
8874 as children with DW_TAG_template_type_param or
8875 DW_TAG_value_type_param. If the latter, add them to the name
8876 here. If the name already has template parameters, then
8877 skip this step; some versions of GCC emit both, and
8878 it is more efficient to use the pre-computed name.
8880 Something to keep in mind about this process: it is very
8881 unlikely, or in some cases downright impossible, to produce
8882 something that will match the mangled name of a function.
8883 If the definition of the function has the same debug info,
8884 we should be able to match up with it anyway. But fallbacks
8885 using the minimal symbol, for instance to find a method
8886 implemented in a stripped copy of libstdc++, will not work.
8887 If we do not have debug info for the definition, we will have to
8888 match them up some other way.
8890 When we do name matching there is a related problem with function
8891 templates; two instantiated function templates are allowed to
8892 differ only by their return types, which we do not add here. */
8894 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
8896 struct attribute
*attr
;
8897 struct die_info
*child
;
8900 die
->building_fullname
= 1;
8902 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
8906 const gdb_byte
*bytes
;
8907 struct dwarf2_locexpr_baton
*baton
;
8910 if (child
->tag
!= DW_TAG_template_type_param
8911 && child
->tag
!= DW_TAG_template_value_param
)
8922 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
8925 complaint (_("template parameter missing DW_AT_type"));
8926 buf
.puts ("UNKNOWN_TYPE");
8929 type
= die_type (child
, cu
);
8931 if (child
->tag
== DW_TAG_template_type_param
)
8933 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
8934 &type_print_raw_options
);
8938 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
8941 complaint (_("template parameter missing "
8942 "DW_AT_const_value"));
8943 buf
.puts ("UNKNOWN_VALUE");
8947 dwarf2_const_value_attr (attr
, type
, name
,
8948 &cu
->comp_unit_obstack
, cu
,
8949 &value
, &bytes
, &baton
);
8951 if (type
->has_no_signedness ())
8952 /* GDB prints characters as NUMBER 'CHAR'. If that's
8953 changed, this can use value_print instead. */
8954 cu
->language_defn
->printchar (value
, type
, &buf
);
8957 struct value_print_options opts
;
8960 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
8964 baton
->per_objfile
);
8965 else if (bytes
!= NULL
)
8967 v
= allocate_value (type
);
8968 memcpy (value_contents_writeable (v
).data (), bytes
,
8969 TYPE_LENGTH (type
));
8972 v
= value_from_longest (type
, value
);
8974 /* Specify decimal so that we do not depend on
8976 get_formatted_print_options (&opts
, 'd');
8978 value_print (v
, &buf
, &opts
);
8983 die
->building_fullname
= 0;
8987 /* Close the argument list, with a space if necessary
8988 (nested templates). */
8989 if (!buf
.empty () && buf
.string ().back () == '>')
8996 /* For C++ methods, append formal parameter type
8997 information, if PHYSNAME. */
8999 if (physname
&& die
->tag
== DW_TAG_subprogram
9000 && lang
== language_cplus
)
9002 struct type
*type
= read_type_die (die
, cu
);
9004 c_type_print_args (type
, &buf
, 1, lang
,
9005 &type_print_raw_options
);
9007 if (lang
== language_cplus
)
9009 /* Assume that an artificial first parameter is
9010 "this", but do not crash if it is not. RealView
9011 marks unnamed (and thus unused) parameters as
9012 artificial; there is no way to differentiate
9014 if (type
->num_fields () > 0
9015 && TYPE_FIELD_ARTIFICIAL (type
, 0)
9016 && type
->field (0).type ()->code () == TYPE_CODE_PTR
9017 && TYPE_CONST (TYPE_TARGET_TYPE (type
->field (0).type ())))
9018 buf
.puts (" const");
9022 const std::string
&intermediate_name
= buf
.string ();
9024 if (lang
== language_cplus
)
9026 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
9029 /* If we only computed INTERMEDIATE_NAME, or if
9030 INTERMEDIATE_NAME is already canonical, then we need to
9032 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
9033 name
= objfile
->intern (intermediate_name
);
9035 name
= canonical_name
;
9042 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9043 If scope qualifiers are appropriate they will be added. The result
9044 will be allocated on the storage_obstack, or NULL if the DIE does
9045 not have a name. NAME may either be from a previous call to
9046 dwarf2_name or NULL.
9048 The output string will be canonicalized (if C++). */
9051 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9053 return dwarf2_compute_name (name
, die
, cu
, 0);
9056 /* Construct a physname for the given DIE in CU. NAME may either be
9057 from a previous call to dwarf2_name or NULL. The result will be
9058 allocated on the objfile_objstack or NULL if the DIE does not have a
9061 The output string will be canonicalized (if C++). */
9064 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9066 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9067 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
9070 /* In this case dwarf2_compute_name is just a shortcut not building anything
9072 if (!die_needs_namespace (die
, cu
))
9073 return dwarf2_compute_name (name
, die
, cu
, 1);
9075 if (cu
->lang () != language_rust
)
9076 mangled
= dw2_linkage_name (die
, cu
);
9078 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9080 gdb::unique_xmalloc_ptr
<char> demangled
;
9081 if (mangled
!= NULL
)
9083 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
9085 /* Do nothing (do not demangle the symbol name). */
9089 /* Use DMGL_RET_DROP for C++ template functions to suppress
9090 their return type. It is easier for GDB users to search
9091 for such functions as `name(params)' than `long name(params)'.
9092 In such case the minimal symbol names do not match the full
9093 symbol names but for template functions there is never a need
9094 to look up their definition from their declaration so
9095 the only disadvantage remains the minimal symbol variant
9096 `long name(params)' does not have the proper inferior type. */
9097 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
9101 canon
= demangled
.get ();
9109 if (canon
== NULL
|| check_physname
)
9111 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
9113 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
9115 /* It may not mean a bug in GDB. The compiler could also
9116 compute DW_AT_linkage_name incorrectly. But in such case
9117 GDB would need to be bug-to-bug compatible. */
9119 complaint (_("Computed physname <%s> does not match demangled <%s> "
9120 "(from linkage <%s>) - DIE at %s [in module %s]"),
9121 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
9122 objfile_name (objfile
));
9124 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9125 is available here - over computed PHYSNAME. It is safer
9126 against both buggy GDB and buggy compilers. */
9140 retval
= objfile
->intern (retval
);
9145 /* Inspect DIE in CU for a namespace alias or a variable with alias
9146 attribute. If one exists, record a new symbol for it.
9148 Returns true if an alias was recorded, false otherwise. */
9151 read_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
9153 struct attribute
*attr
;
9155 /* If the die does not have a name, this is neither a namespace
9156 alias nor a variable alias. */
9157 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
9161 struct die_info
*d
= die
;
9162 struct dwarf2_cu
*imported_cu
= cu
;
9164 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9165 keep inspecting DIEs until we hit the underlying import. */
9166 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9167 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
9169 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
9173 d
= follow_die_ref (d
, attr
, &imported_cu
);
9174 if (d
->tag
!= DW_TAG_imported_declaration
)
9178 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
9180 complaint (_("DIE at %s has too many recursively imported "
9181 "declarations"), sect_offset_str (d
->sect_off
));
9188 if (d
->tag
== DW_TAG_variable
)
9190 /* This declaration is a C/C++ global variable alias.
9191 Add a symbol for it whose type is the same as the
9192 aliased variable's. */
9193 type
= die_type (d
, imported_cu
);
9194 struct symbol
*sym
= new_symbol (die
, type
, cu
);
9195 attr
= dwarf2_attr (d
, DW_AT_location
, imported_cu
);
9196 sym
->set_aclass_index (LOC_UNRESOLVED
);
9197 if (attr
!= nullptr)
9198 var_decode_location (attr
, sym
, cu
);
9203 sect_offset sect_off
= attr
->get_ref_die_offset ();
9204 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
,
9206 if (type
!= nullptr && type
->code () == TYPE_CODE_NAMESPACE
)
9208 /* This declaration is a global namespace alias. Add
9209 a symbol for it whose type is the aliased
9211 new_symbol (die
, type
, cu
);
9220 /* Return the using directives repository (global or local?) to use in the
9221 current context for CU.
9223 For Ada, imported declarations can materialize renamings, which *may* be
9224 global. However it is impossible (for now?) in DWARF to distinguish
9225 "external" imported declarations and "static" ones. As all imported
9226 declarations seem to be static in all other languages, make them all CU-wide
9227 global only in Ada. */
9229 static struct using_direct
**
9230 using_directives (struct dwarf2_cu
*cu
)
9232 if (cu
->lang () == language_ada
9233 && cu
->get_builder ()->outermost_context_p ())
9234 return cu
->get_builder ()->get_global_using_directives ();
9236 return cu
->get_builder ()->get_local_using_directives ();
9239 /* Read the import statement specified by the given die and record it. */
9242 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
9244 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9245 struct attribute
*import_attr
;
9246 struct die_info
*imported_die
, *child_die
;
9247 struct dwarf2_cu
*imported_cu
;
9248 const char *imported_name
;
9249 const char *imported_name_prefix
;
9250 const char *canonical_name
;
9251 const char *import_alias
;
9252 const char *imported_declaration
= NULL
;
9253 const char *import_prefix
;
9254 std::vector
<const char *> excludes
;
9256 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
9257 if (import_attr
== NULL
)
9259 complaint (_("Tag '%s' has no DW_AT_import"),
9260 dwarf_tag_name (die
->tag
));
9265 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
9266 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9267 if (imported_name
== NULL
)
9269 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9271 The import in the following code:
9285 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9286 <52> DW_AT_decl_file : 1
9287 <53> DW_AT_decl_line : 6
9288 <54> DW_AT_import : <0x75>
9289 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9291 <5b> DW_AT_decl_file : 1
9292 <5c> DW_AT_decl_line : 2
9293 <5d> DW_AT_type : <0x6e>
9295 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9296 <76> DW_AT_byte_size : 4
9297 <77> DW_AT_encoding : 5 (signed)
9299 imports the wrong die ( 0x75 instead of 0x58 ).
9300 This case will be ignored until the gcc bug is fixed. */
9304 /* Figure out the local name after import. */
9305 import_alias
= dwarf2_name (die
, cu
);
9307 /* Figure out where the statement is being imported to. */
9308 import_prefix
= determine_prefix (die
, cu
);
9310 /* Figure out what the scope of the imported die is and prepend it
9311 to the name of the imported die. */
9312 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
9314 if (imported_die
->tag
!= DW_TAG_namespace
9315 && imported_die
->tag
!= DW_TAG_module
)
9317 imported_declaration
= imported_name
;
9318 canonical_name
= imported_name_prefix
;
9320 else if (strlen (imported_name_prefix
) > 0)
9321 canonical_name
= obconcat (&objfile
->objfile_obstack
,
9322 imported_name_prefix
,
9323 (cu
->lang () == language_d
9326 imported_name
, (char *) NULL
);
9328 canonical_name
= imported_name
;
9330 if (die
->tag
== DW_TAG_imported_module
9331 && cu
->lang () == language_fortran
)
9332 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
9333 child_die
= child_die
->sibling
)
9335 /* DWARF-4: A Fortran use statement with a “rename list” may be
9336 represented by an imported module entry with an import attribute
9337 referring to the module and owned entries corresponding to those
9338 entities that are renamed as part of being imported. */
9340 if (child_die
->tag
!= DW_TAG_imported_declaration
)
9342 complaint (_("child DW_TAG_imported_declaration expected "
9343 "- DIE at %s [in module %s]"),
9344 sect_offset_str (child_die
->sect_off
),
9345 objfile_name (objfile
));
9349 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
9350 if (import_attr
== NULL
)
9352 complaint (_("Tag '%s' has no DW_AT_import"),
9353 dwarf_tag_name (child_die
->tag
));
9358 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
9360 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9361 if (imported_name
== NULL
)
9363 complaint (_("child DW_TAG_imported_declaration has unknown "
9364 "imported name - DIE at %s [in module %s]"),
9365 sect_offset_str (child_die
->sect_off
),
9366 objfile_name (objfile
));
9370 excludes
.push_back (imported_name
);
9372 process_die (child_die
, cu
);
9375 add_using_directive (using_directives (cu
),
9379 imported_declaration
,
9382 &objfile
->objfile_obstack
);
9385 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9386 types, but gives them a size of zero. Starting with version 14,
9387 ICC is compatible with GCC. */
9390 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
9392 if (!cu
->checked_producer
)
9393 check_producer (cu
);
9395 return cu
->producer_is_icc_lt_14
;
9398 /* ICC generates a DW_AT_type for C void functions. This was observed on
9399 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9400 which says that void functions should not have a DW_AT_type. */
9403 producer_is_icc (struct dwarf2_cu
*cu
)
9405 if (!cu
->checked_producer
)
9406 check_producer (cu
);
9408 return cu
->producer_is_icc
;
9411 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9412 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9413 this, it was first present in GCC release 4.3.0. */
9416 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
9418 if (!cu
->checked_producer
)
9419 check_producer (cu
);
9421 return cu
->producer_is_gcc_lt_4_3
;
9424 static file_and_directory
&
9425 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
9427 if (cu
->per_cu
->fnd
!= nullptr)
9428 return *cu
->per_cu
->fnd
;
9430 /* Find the filename. Do not use dwarf2_name here, since the filename
9431 is not a source language identifier. */
9432 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
9433 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
9435 if (res
.get_comp_dir () == nullptr
9436 && producer_is_gcc_lt_4_3 (cu
)
9437 && res
.get_name () != nullptr
9438 && IS_ABSOLUTE_PATH (res
.get_name ()))
9439 res
.set_comp_dir (ldirname (res
.get_name ()));
9441 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
9442 return *cu
->per_cu
->fnd
;
9445 /* Handle DW_AT_stmt_list for a compilation unit.
9446 DIE is the DW_TAG_compile_unit die for CU.
9447 COMP_DIR is the compilation directory. LOWPC is passed to
9448 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9451 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
9452 CORE_ADDR lowpc
) /* ARI: editCase function */
9454 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9455 struct attribute
*attr
;
9456 struct line_header line_header_local
;
9457 hashval_t line_header_local_hash
;
9461 gdb_assert (! cu
->per_cu
->is_debug_types
);
9463 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
9464 if (attr
== NULL
|| !attr
->form_is_unsigned ())
9467 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9469 /* The line header hash table is only created if needed (it exists to
9470 prevent redundant reading of the line table for partial_units).
9471 If we're given a partial_unit, we'll need it. If we're given a
9472 compile_unit, then use the line header hash table if it's already
9473 created, but don't create one just yet. */
9475 if (per_objfile
->line_header_hash
== NULL
9476 && die
->tag
== DW_TAG_partial_unit
)
9478 per_objfile
->line_header_hash
9479 .reset (htab_create_alloc (127, line_header_hash_voidp
,
9480 line_header_eq_voidp
,
9481 htab_delete_entry
<line_header
>,
9485 line_header_local
.sect_off
= line_offset
;
9486 line_header_local
.offset_in_dwz
= cu
->per_cu
->is_dwz
;
9487 line_header_local_hash
= line_header_hash (&line_header_local
);
9488 if (per_objfile
->line_header_hash
!= NULL
)
9490 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9492 line_header_local_hash
, NO_INSERT
);
9494 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9495 is not present in *SLOT (since if there is something in *SLOT then
9496 it will be for a partial_unit). */
9497 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
9499 gdb_assert (*slot
!= NULL
);
9500 cu
->line_header
= (struct line_header
*) *slot
;
9505 /* dwarf_decode_line_header does not yet provide sufficient information.
9506 We always have to call also dwarf_decode_lines for it. */
9507 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
);
9511 cu
->line_header
= lh
.release ();
9512 cu
->line_header_die_owner
= die
;
9514 if (per_objfile
->line_header_hash
== NULL
)
9518 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9520 line_header_local_hash
, INSERT
);
9521 gdb_assert (slot
!= NULL
);
9523 if (slot
!= NULL
&& *slot
== NULL
)
9525 /* This newly decoded line number information unit will be owned
9526 by line_header_hash hash table. */
9527 *slot
= cu
->line_header
;
9528 cu
->line_header_die_owner
= NULL
;
9532 /* We cannot free any current entry in (*slot) as that struct line_header
9533 may be already used by multiple CUs. Create only temporary decoded
9534 line_header for this CU - it may happen at most once for each line
9535 number information unit. And if we're not using line_header_hash
9536 then this is what we want as well. */
9537 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
9539 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
9540 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
);
9543 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9546 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9548 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9549 struct objfile
*objfile
= per_objfile
->objfile
;
9550 struct gdbarch
*gdbarch
= objfile
->arch ();
9551 CORE_ADDR lowpc
= ((CORE_ADDR
) -1);
9552 CORE_ADDR highpc
= ((CORE_ADDR
) 0);
9553 struct attribute
*attr
;
9554 struct die_info
*child_die
;
9557 prepare_one_comp_unit (cu
, die
, cu
->lang ());
9558 baseaddr
= objfile
->text_section_offset ();
9560 get_scope_pc_bounds (die
, &lowpc
, &highpc
, cu
);
9562 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9563 from finish_block. */
9564 if (lowpc
== ((CORE_ADDR
) -1))
9566 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
9568 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
9570 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
9573 gdb_assert (per_objfile
->sym_cu
== nullptr);
9574 scoped_restore restore_sym_cu
9575 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
9577 /* Decode line number information if present. We do this before
9578 processing child DIEs, so that the line header table is available
9579 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9580 HIGHPC are both 0x0, then there won't be any interesting code in
9581 the CU, but a check later on (in
9582 lnp_state_machine::check_line_address) will fail to properly
9583 exclude an entry that was removed via --gc-sections. */
9584 if (lowpc
!= highpc
)
9585 handle_DW_AT_stmt_list (die
, cu
, lowpc
);
9587 /* Process all dies in compilation unit. */
9588 if (die
->child
!= NULL
)
9590 child_die
= die
->child
;
9591 while (child_die
&& child_die
->tag
)
9593 process_die (child_die
, cu
);
9594 child_die
= child_die
->sibling
;
9597 per_objfile
->sym_cu
= nullptr;
9599 /* Decode macro information, if present. Dwarf 2 macro information
9600 refers to information in the line number info statement program
9601 header, so we can only read it if we've read the header
9603 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
9605 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
9606 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9608 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
9609 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9611 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
9615 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
9616 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9618 unsigned int macro_offset
= attr
->as_unsigned ();
9620 dwarf_decode_macros (cu
, macro_offset
, 0);
9626 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
9628 struct type_unit_group
*tu_group
;
9630 struct attribute
*attr
;
9632 struct signatured_type
*sig_type
;
9634 gdb_assert (per_cu
->is_debug_types
);
9635 sig_type
= (struct signatured_type
*) per_cu
;
9637 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
9639 /* If we're using .gdb_index (includes -readnow) then
9640 per_cu->type_unit_group may not have been set up yet. */
9641 if (sig_type
->type_unit_group
== NULL
)
9642 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
9643 tu_group
= sig_type
->type_unit_group
;
9645 /* If we've already processed this stmt_list there's no real need to
9646 do it again, we could fake it and just recreate the part we need
9647 (file name,index -> symtab mapping). If data shows this optimization
9648 is useful we can do it then. */
9649 type_unit_group_unshareable
*tug_unshare
9650 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
9651 first_time
= tug_unshare
->compunit_symtab
== NULL
;
9653 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9656 if (attr
!= NULL
&& attr
->form_is_unsigned ())
9658 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9659 lh
= dwarf_decode_line_header (line_offset
, this);
9664 start_compunit_symtab ("", NULL
, 0);
9667 gdb_assert (tug_unshare
->symtabs
== NULL
);
9668 gdb_assert (m_builder
== nullptr);
9669 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9670 m_builder
.reset (new struct buildsym_compunit
9671 (cust
->objfile (), "",
9673 compunit_language (cust
),
9675 list_in_scope
= get_builder ()->get_file_symbols ();
9680 line_header
= lh
.release ();
9681 line_header_die_owner
= die
;
9685 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
9687 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9688 still initializing it, and our caller (a few levels up)
9689 process_full_type_unit still needs to know if this is the first
9692 tug_unshare
->symtabs
9693 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
9694 struct symtab
*, line_header
->file_names_size ());
9696 auto &file_names
= line_header
->file_names ();
9697 for (i
= 0; i
< file_names
.size (); ++i
)
9699 file_entry
&fe
= file_names
[i
];
9700 dwarf2_start_subfile (this, fe
.name
,
9701 fe
.include_dir (line_header
));
9702 buildsym_compunit
*b
= get_builder ();
9703 if (b
->get_current_subfile ()->symtab
== NULL
)
9705 /* NOTE: start_subfile will recognize when it's been
9706 passed a file it has already seen. So we can't
9707 assume there's a simple mapping from
9708 cu->line_header->file_names to subfiles, plus
9709 cu->line_header->file_names may contain dups. */
9710 const char *name
= b
->get_current_subfile ()->name
.c_str ();
9711 b
->get_current_subfile ()->symtab
= allocate_symtab (cust
, name
);
9714 fe
.symtab
= b
->get_current_subfile ()->symtab
;
9715 tug_unshare
->symtabs
[i
] = fe
.symtab
;
9720 gdb_assert (m_builder
== nullptr);
9721 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9722 m_builder
.reset (new struct buildsym_compunit
9723 (cust
->objfile (), "",
9725 compunit_language (cust
),
9727 list_in_scope
= get_builder ()->get_file_symbols ();
9729 auto &file_names
= line_header
->file_names ();
9730 for (i
= 0; i
< file_names
.size (); ++i
)
9732 file_entry
&fe
= file_names
[i
];
9733 fe
.symtab
= tug_unshare
->symtabs
[i
];
9737 /* The main symtab is allocated last. Type units don't have DW_AT_name
9738 so they don't have a "real" (so to speak) symtab anyway.
9739 There is later code that will assign the main symtab to all symbols
9740 that don't have one. We need to handle the case of a symbol with a
9741 missing symtab (DW_AT_decl_file) anyway. */
9744 /* Process DW_TAG_type_unit.
9745 For TUs we want to skip the first top level sibling if it's not the
9746 actual type being defined by this TU. In this case the first top
9747 level sibling is there to provide context only. */
9750 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9752 struct die_info
*child_die
;
9754 prepare_one_comp_unit (cu
, die
, language_minimal
);
9756 /* Initialize (or reinitialize) the machinery for building symtabs.
9757 We do this before processing child DIEs, so that the line header table
9758 is available for DW_AT_decl_file. */
9759 cu
->setup_type_unit_groups (die
);
9761 if (die
->child
!= NULL
)
9763 child_die
= die
->child
;
9764 while (child_die
&& child_die
->tag
)
9766 process_die (child_die
, cu
);
9767 child_die
= child_die
->sibling
;
9774 http://gcc.gnu.org/wiki/DebugFission
9775 http://gcc.gnu.org/wiki/DebugFissionDWP
9777 To simplify handling of both DWO files ("object" files with the DWARF info)
9778 and DWP files (a file with the DWOs packaged up into one file), we treat
9779 DWP files as having a collection of virtual DWO files. */
9782 hash_dwo_file (const void *item
)
9784 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
9787 hash
= htab_hash_string (dwo_file
->dwo_name
);
9788 if (dwo_file
->comp_dir
!= NULL
)
9789 hash
+= htab_hash_string (dwo_file
->comp_dir
);
9794 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
9796 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
9797 const struct dwo_file
*rhs
= (const struct dwo_file
*) item_rhs
;
9799 if (strcmp (lhs
->dwo_name
, rhs
->dwo_name
) != 0)
9801 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
9802 return lhs
->comp_dir
== rhs
->comp_dir
;
9803 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
9806 /* Allocate a hash table for DWO files. */
9809 allocate_dwo_file_hash_table ()
9811 return htab_up (htab_create_alloc (41,
9814 htab_delete_entry
<dwo_file
>,
9818 /* Lookup DWO file DWO_NAME. */
9821 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
9822 const char *dwo_name
,
9823 const char *comp_dir
)
9825 struct dwo_file find_entry
;
9828 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
9829 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
9831 find_entry
.dwo_name
= dwo_name
;
9832 find_entry
.comp_dir
= comp_dir
;
9833 slot
= htab_find_slot (per_objfile
->per_bfd
->dwo_files
.get (), &find_entry
,
9840 hash_dwo_unit (const void *item
)
9842 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9844 /* This drops the top 32 bits of the id, but is ok for a hash. */
9845 return dwo_unit
->signature
;
9849 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
9851 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
9852 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
9854 /* The signature is assumed to be unique within the DWO file.
9855 So while object file CU dwo_id's always have the value zero,
9856 that's OK, assuming each object file DWO file has only one CU,
9857 and that's the rule for now. */
9858 return lhs
->signature
== rhs
->signature
;
9861 /* Allocate a hash table for DWO CUs,TUs.
9862 There is one of these tables for each of CUs,TUs for each DWO file. */
9865 allocate_dwo_unit_table ()
9867 /* Start out with a pretty small number.
9868 Generally DWO files contain only one CU and maybe some TUs. */
9869 return htab_up (htab_create_alloc (3,
9872 NULL
, xcalloc
, xfree
));
9875 /* die_reader_func for create_dwo_cu. */
9878 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
9879 const gdb_byte
*info_ptr
,
9880 struct die_info
*comp_unit_die
,
9881 struct dwo_file
*dwo_file
,
9882 struct dwo_unit
*dwo_unit
)
9884 struct dwarf2_cu
*cu
= reader
->cu
;
9885 sect_offset sect_off
= cu
->per_cu
->sect_off
;
9886 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
9888 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
9889 if (!signature
.has_value ())
9891 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9892 " its dwo_id [in module %s]"),
9893 sect_offset_str (sect_off
), dwo_file
->dwo_name
);
9897 dwo_unit
->dwo_file
= dwo_file
;
9898 dwo_unit
->signature
= *signature
;
9899 dwo_unit
->section
= section
;
9900 dwo_unit
->sect_off
= sect_off
;
9901 dwo_unit
->length
= cu
->per_cu
->length ();
9903 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9904 sect_offset_str (sect_off
),
9905 hex_string (dwo_unit
->signature
));
9908 /* Create the dwo_units for the CUs in a DWO_FILE.
9909 Note: This function processes DWO files only, not DWP files. */
9912 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
9913 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
9914 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
9916 struct objfile
*objfile
= per_objfile
->objfile
;
9917 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
9918 const gdb_byte
*info_ptr
, *end_ptr
;
9920 section
.read (objfile
);
9921 info_ptr
= section
.buffer
;
9923 if (info_ptr
== NULL
)
9926 dwarf_read_debug_printf ("Reading %s for %s:",
9927 section
.get_name (),
9928 section
.get_file_name ());
9930 end_ptr
= info_ptr
+ section
.size
;
9931 while (info_ptr
< end_ptr
)
9933 struct dwarf2_per_cu_data per_cu
;
9934 struct dwo_unit read_unit
{};
9935 struct dwo_unit
*dwo_unit
;
9937 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
9939 per_cu
.per_bfd
= per_bfd
;
9940 per_cu
.is_debug_types
= 0;
9941 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
9942 per_cu
.section
= §ion
;
9944 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
9945 if (!reader
.dummy_p
)
9946 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
9947 &dwo_file
, &read_unit
);
9948 info_ptr
+= per_cu
.length ();
9950 // If the unit could not be parsed, skip it.
9951 if (read_unit
.dwo_file
== NULL
)
9954 if (cus_htab
== NULL
)
9955 cus_htab
= allocate_dwo_unit_table ();
9957 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
9959 *dwo_unit
= read_unit
;
9960 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
9961 gdb_assert (slot
!= NULL
);
9964 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
9965 sect_offset dup_sect_off
= dup_cu
->sect_off
;
9967 complaint (_("debug cu entry at offset %s is duplicate to"
9968 " the entry at offset %s, signature %s"),
9969 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
9970 hex_string (dwo_unit
->signature
));
9972 *slot
= (void *)dwo_unit
;
9976 /* DWP file .debug_{cu,tu}_index section format:
9977 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9978 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
9980 DWP Versions 1 & 2 are older, pre-standard format versions. The first
9981 officially standard DWP format was published with DWARF v5 and is called
9982 Version 5. There are no versions 3 or 4.
9986 Both index sections have the same format, and serve to map a 64-bit
9987 signature to a set of section numbers. Each section begins with a header,
9988 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9989 indexes, and a pool of 32-bit section numbers. The index sections will be
9990 aligned at 8-byte boundaries in the file.
9992 The index section header consists of:
9994 V, 32 bit version number
9996 N, 32 bit number of compilation units or type units in the index
9997 M, 32 bit number of slots in the hash table
9999 Numbers are recorded using the byte order of the application binary.
10001 The hash table begins at offset 16 in the section, and consists of an array
10002 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
10003 order of the application binary). Unused slots in the hash table are 0.
10004 (We rely on the extreme unlikeliness of a signature being exactly 0.)
10006 The parallel table begins immediately after the hash table
10007 (at offset 16 + 8 * M from the beginning of the section), and consists of an
10008 array of 32-bit indexes (using the byte order of the application binary),
10009 corresponding 1-1 with slots in the hash table. Each entry in the parallel
10010 table contains a 32-bit index into the pool of section numbers. For unused
10011 hash table slots, the corresponding entry in the parallel table will be 0.
10013 The pool of section numbers begins immediately following the hash table
10014 (at offset 16 + 12 * M from the beginning of the section). The pool of
10015 section numbers consists of an array of 32-bit words (using the byte order
10016 of the application binary). Each item in the array is indexed starting
10017 from 0. The hash table entry provides the index of the first section
10018 number in the set. Additional section numbers in the set follow, and the
10019 set is terminated by a 0 entry (section number 0 is not used in ELF).
10021 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10022 section must be the first entry in the set, and the .debug_abbrev.dwo must
10023 be the second entry. Other members of the set may follow in any order.
10027 DWP Versions 2 and 5:
10029 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10030 and the entries in the index tables are now offsets into these sections.
10031 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10034 Index Section Contents:
10036 Hash Table of Signatures dwp_hash_table.hash_table
10037 Parallel Table of Indices dwp_hash_table.unit_table
10038 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10039 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10041 The index section header consists of:
10043 V, 32 bit version number
10044 L, 32 bit number of columns in the table of section offsets
10045 N, 32 bit number of compilation units or type units in the index
10046 M, 32 bit number of slots in the hash table
10048 Numbers are recorded using the byte order of the application binary.
10050 The hash table has the same format as version 1.
10051 The parallel table of indices has the same format as version 1,
10052 except that the entries are origin-1 indices into the table of sections
10053 offsets and the table of section sizes.
10055 The table of offsets begins immediately following the parallel table
10056 (at offset 16 + 12 * M from the beginning of the section). The table is
10057 a two-dimensional array of 32-bit words (using the byte order of the
10058 application binary), with L columns and N+1 rows, in row-major order.
10059 Each row in the array is indexed starting from 0. The first row provides
10060 a key to the remaining rows: each column in this row provides an identifier
10061 for a debug section, and the offsets in the same column of subsequent rows
10062 refer to that section. The section identifiers for Version 2 are:
10064 DW_SECT_INFO 1 .debug_info.dwo
10065 DW_SECT_TYPES 2 .debug_types.dwo
10066 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10067 DW_SECT_LINE 4 .debug_line.dwo
10068 DW_SECT_LOC 5 .debug_loc.dwo
10069 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10070 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10071 DW_SECT_MACRO 8 .debug_macro.dwo
10073 The section identifiers for Version 5 are:
10075 DW_SECT_INFO_V5 1 .debug_info.dwo
10076 DW_SECT_RESERVED_V5 2 --
10077 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10078 DW_SECT_LINE_V5 4 .debug_line.dwo
10079 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10080 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10081 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10082 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10084 The offsets provided by the CU and TU index sections are the base offsets
10085 for the contributions made by each CU or TU to the corresponding section
10086 in the package file. Each CU and TU header contains an abbrev_offset
10087 field, used to find the abbreviations table for that CU or TU within the
10088 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10089 be interpreted as relative to the base offset given in the index section.
10090 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10091 should be interpreted as relative to the base offset for .debug_line.dwo,
10092 and offsets into other debug sections obtained from DWARF attributes should
10093 also be interpreted as relative to the corresponding base offset.
10095 The table of sizes begins immediately following the table of offsets.
10096 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10097 with L columns and N rows, in row-major order. Each row in the array is
10098 indexed starting from 1 (row 0 is shared by the two tables).
10102 Hash table lookup is handled the same in version 1 and 2:
10104 We assume that N and M will not exceed 2^32 - 1.
10105 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10107 Given a 64-bit compilation unit signature or a type signature S, an entry
10108 in the hash table is located as follows:
10110 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10111 the low-order k bits all set to 1.
10113 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10115 3) If the hash table entry at index H matches the signature, use that
10116 entry. If the hash table entry at index H is unused (all zeroes),
10117 terminate the search: the signature is not present in the table.
10119 4) Let H = (H + H') modulo M. Repeat at Step 3.
10121 Because M > N and H' and M are relatively prime, the search is guaranteed
10122 to stop at an unused slot or find the match. */
10124 /* Create a hash table to map DWO IDs to their CU/TU entry in
10125 .debug_{info,types}.dwo in DWP_FILE.
10126 Returns NULL if there isn't one.
10127 Note: This function processes DWP files only, not DWO files. */
10129 static struct dwp_hash_table
*
10130 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
10131 struct dwp_file
*dwp_file
, int is_debug_types
)
10133 struct objfile
*objfile
= per_objfile
->objfile
;
10134 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10135 const gdb_byte
*index_ptr
, *index_end
;
10136 struct dwarf2_section_info
*index
;
10137 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
10138 struct dwp_hash_table
*htab
;
10140 if (is_debug_types
)
10141 index
= &dwp_file
->sections
.tu_index
;
10143 index
= &dwp_file
->sections
.cu_index
;
10145 if (index
->empty ())
10147 index
->read (objfile
);
10149 index_ptr
= index
->buffer
;
10150 index_end
= index_ptr
+ index
->size
;
10152 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10153 For now it's safe to just read 4 bytes (particularly as it's difficult to
10154 tell if you're dealing with Version 5 before you've read the version). */
10155 version
= read_4_bytes (dbfd
, index_ptr
);
10157 if (version
== 2 || version
== 5)
10158 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
10162 nr_units
= read_4_bytes (dbfd
, index_ptr
);
10164 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
10167 if (version
!= 1 && version
!= 2 && version
!= 5)
10169 error (_("Dwarf Error: unsupported DWP file version (%s)"
10170 " [in module %s]"),
10171 pulongest (version
), dwp_file
->name
);
10173 if (nr_slots
!= (nr_slots
& -nr_slots
))
10175 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10176 " is not power of 2 [in module %s]"),
10177 pulongest (nr_slots
), dwp_file
->name
);
10180 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
10181 htab
->version
= version
;
10182 htab
->nr_columns
= nr_columns
;
10183 htab
->nr_units
= nr_units
;
10184 htab
->nr_slots
= nr_slots
;
10185 htab
->hash_table
= index_ptr
;
10186 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
10188 /* Exit early if the table is empty. */
10189 if (nr_slots
== 0 || nr_units
== 0
10190 || (version
== 2 && nr_columns
== 0)
10191 || (version
== 5 && nr_columns
== 0))
10193 /* All must be zero. */
10194 if (nr_slots
!= 0 || nr_units
!= 0
10195 || (version
== 2 && nr_columns
!= 0)
10196 || (version
== 5 && nr_columns
!= 0))
10198 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10199 " all zero [in modules %s]"),
10207 htab
->section_pool
.v1
.indices
=
10208 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10209 /* It's harder to decide whether the section is too small in v1.
10210 V1 is deprecated anyway so we punt. */
10212 else if (version
== 2)
10214 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10215 int *ids
= htab
->section_pool
.v2
.section_ids
;
10216 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
10217 /* Reverse map for error checking. */
10218 int ids_seen
[DW_SECT_MAX
+ 1];
10221 if (nr_columns
< 2)
10223 error (_("Dwarf Error: bad DWP hash table, too few columns"
10224 " in section table [in module %s]"),
10227 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
10229 error (_("Dwarf Error: bad DWP hash table, too many columns"
10230 " in section table [in module %s]"),
10233 memset (ids
, 255, sizeof_ids
);
10234 memset (ids_seen
, 255, sizeof (ids_seen
));
10235 for (i
= 0; i
< nr_columns
; ++i
)
10237 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10239 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
10241 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10242 " in section table [in module %s]"),
10243 id
, dwp_file
->name
);
10245 if (ids_seen
[id
] != -1)
10247 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10248 " id %d in section table [in module %s]"),
10249 id
, dwp_file
->name
);
10254 /* Must have exactly one info or types section. */
10255 if (((ids_seen
[DW_SECT_INFO
] != -1)
10256 + (ids_seen
[DW_SECT_TYPES
] != -1))
10259 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10260 " DWO info/types section [in module %s]"),
10263 /* Must have an abbrev section. */
10264 if (ids_seen
[DW_SECT_ABBREV
] == -1)
10266 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10267 " section [in module %s]"),
10270 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10271 htab
->section_pool
.v2
.sizes
=
10272 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
10273 * nr_units
* nr_columns
);
10274 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
10275 * nr_units
* nr_columns
))
10278 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10279 " [in module %s]"),
10283 else /* version == 5 */
10285 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10286 int *ids
= htab
->section_pool
.v5
.section_ids
;
10287 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
10288 /* Reverse map for error checking. */
10289 int ids_seen
[DW_SECT_MAX_V5
+ 1];
10291 if (nr_columns
< 2)
10293 error (_("Dwarf Error: bad DWP hash table, too few columns"
10294 " in section table [in module %s]"),
10297 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
10299 error (_("Dwarf Error: bad DWP hash table, too many columns"
10300 " in section table [in module %s]"),
10303 memset (ids
, 255, sizeof_ids
);
10304 memset (ids_seen
, 255, sizeof (ids_seen
));
10305 for (int i
= 0; i
< nr_columns
; ++i
)
10307 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10309 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
10311 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10312 " in section table [in module %s]"),
10313 id
, dwp_file
->name
);
10315 if (ids_seen
[id
] != -1)
10317 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10318 " id %d in section table [in module %s]"),
10319 id
, dwp_file
->name
);
10324 /* Must have seen an info section. */
10325 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
10327 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10328 " DWO info/types section [in module %s]"),
10331 /* Must have an abbrev section. */
10332 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
10334 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10335 " section [in module %s]"),
10338 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10339 htab
->section_pool
.v5
.sizes
10340 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
10341 * nr_units
* nr_columns
);
10342 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
10343 * nr_units
* nr_columns
))
10346 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10347 " [in module %s]"),
10355 /* Update SECTIONS with the data from SECTP.
10357 This function is like the other "locate" section routines, but in
10358 this context the sections to read comes from the DWP V1 hash table,
10359 not the full ELF section table.
10361 The result is non-zero for success, or zero if an error was found. */
10364 locate_v1_virtual_dwo_sections (asection
*sectp
,
10365 struct virtual_v1_dwo_sections
*sections
)
10367 const struct dwop_section_names
*names
= &dwop_section_names
;
10369 if (names
->abbrev_dwo
.matches (sectp
->name
))
10371 /* There can be only one. */
10372 if (sections
->abbrev
.s
.section
!= NULL
)
10374 sections
->abbrev
.s
.section
= sectp
;
10375 sections
->abbrev
.size
= bfd_section_size (sectp
);
10377 else if (names
->info_dwo
.matches (sectp
->name
)
10378 || names
->types_dwo
.matches (sectp
->name
))
10380 /* There can be only one. */
10381 if (sections
->info_or_types
.s
.section
!= NULL
)
10383 sections
->info_or_types
.s
.section
= sectp
;
10384 sections
->info_or_types
.size
= bfd_section_size (sectp
);
10386 else if (names
->line_dwo
.matches (sectp
->name
))
10388 /* There can be only one. */
10389 if (sections
->line
.s
.section
!= NULL
)
10391 sections
->line
.s
.section
= sectp
;
10392 sections
->line
.size
= bfd_section_size (sectp
);
10394 else if (names
->loc_dwo
.matches (sectp
->name
))
10396 /* There can be only one. */
10397 if (sections
->loc
.s
.section
!= NULL
)
10399 sections
->loc
.s
.section
= sectp
;
10400 sections
->loc
.size
= bfd_section_size (sectp
);
10402 else if (names
->macinfo_dwo
.matches (sectp
->name
))
10404 /* There can be only one. */
10405 if (sections
->macinfo
.s
.section
!= NULL
)
10407 sections
->macinfo
.s
.section
= sectp
;
10408 sections
->macinfo
.size
= bfd_section_size (sectp
);
10410 else if (names
->macro_dwo
.matches (sectp
->name
))
10412 /* There can be only one. */
10413 if (sections
->macro
.s
.section
!= NULL
)
10415 sections
->macro
.s
.section
= sectp
;
10416 sections
->macro
.size
= bfd_section_size (sectp
);
10418 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
10420 /* There can be only one. */
10421 if (sections
->str_offsets
.s
.section
!= NULL
)
10423 sections
->str_offsets
.s
.section
= sectp
;
10424 sections
->str_offsets
.size
= bfd_section_size (sectp
);
10428 /* No other kind of section is valid. */
10435 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10436 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10437 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10438 This is for DWP version 1 files. */
10440 static struct dwo_unit
*
10441 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
10442 struct dwp_file
*dwp_file
,
10443 uint32_t unit_index
,
10444 const char *comp_dir
,
10445 ULONGEST signature
, int is_debug_types
)
10447 const struct dwp_hash_table
*dwp_htab
=
10448 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10449 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10450 const char *kind
= is_debug_types
? "TU" : "CU";
10451 struct dwo_file
*dwo_file
;
10452 struct dwo_unit
*dwo_unit
;
10453 struct virtual_v1_dwo_sections sections
;
10454 void **dwo_file_slot
;
10457 gdb_assert (dwp_file
->version
== 1);
10459 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10460 kind
, pulongest (unit_index
), hex_string (signature
),
10463 /* Fetch the sections of this DWO unit.
10464 Put a limit on the number of sections we look for so that bad data
10465 doesn't cause us to loop forever. */
10467 #define MAX_NR_V1_DWO_SECTIONS \
10468 (1 /* .debug_info or .debug_types */ \
10469 + 1 /* .debug_abbrev */ \
10470 + 1 /* .debug_line */ \
10471 + 1 /* .debug_loc */ \
10472 + 1 /* .debug_str_offsets */ \
10473 + 1 /* .debug_macro or .debug_macinfo */ \
10474 + 1 /* trailing zero */)
10476 memset (§ions
, 0, sizeof (sections
));
10478 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
10481 uint32_t section_nr
=
10482 read_4_bytes (dbfd
,
10483 dwp_htab
->section_pool
.v1
.indices
10484 + (unit_index
+ i
) * sizeof (uint32_t));
10486 if (section_nr
== 0)
10488 if (section_nr
>= dwp_file
->num_sections
)
10490 error (_("Dwarf Error: bad DWP hash table, section number too large"
10491 " [in module %s]"),
10495 sectp
= dwp_file
->elf_sections
[section_nr
];
10496 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
10498 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10499 " [in module %s]"),
10505 || sections
.info_or_types
.empty ()
10506 || sections
.abbrev
.empty ())
10508 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10509 " [in module %s]"),
10512 if (i
== MAX_NR_V1_DWO_SECTIONS
)
10514 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10515 " [in module %s]"),
10519 /* It's easier for the rest of the code if we fake a struct dwo_file and
10520 have dwo_unit "live" in that. At least for now.
10522 The DWP file can be made up of a random collection of CUs and TUs.
10523 However, for each CU + set of TUs that came from the same original DWO
10524 file, we can combine them back into a virtual DWO file to save space
10525 (fewer struct dwo_file objects to allocate). Remember that for really
10526 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10528 std::string virtual_dwo_name
=
10529 string_printf ("virtual-dwo/%d-%d-%d-%d",
10530 sections
.abbrev
.get_id (),
10531 sections
.line
.get_id (),
10532 sections
.loc
.get_id (),
10533 sections
.str_offsets
.get_id ());
10534 /* Can we use an existing virtual DWO file? */
10535 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10537 /* Create one if necessary. */
10538 if (*dwo_file_slot
== NULL
)
10540 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10541 virtual_dwo_name
.c_str ());
10543 dwo_file
= new struct dwo_file
;
10544 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10545 dwo_file
->comp_dir
= comp_dir
;
10546 dwo_file
->sections
.abbrev
= sections
.abbrev
;
10547 dwo_file
->sections
.line
= sections
.line
;
10548 dwo_file
->sections
.loc
= sections
.loc
;
10549 dwo_file
->sections
.macinfo
= sections
.macinfo
;
10550 dwo_file
->sections
.macro
= sections
.macro
;
10551 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
10552 /* The "str" section is global to the entire DWP file. */
10553 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10554 /* The info or types section is assigned below to dwo_unit,
10555 there's no need to record it in dwo_file.
10556 Also, we can't simply record type sections in dwo_file because
10557 we record a pointer into the vector in dwo_unit. As we collect more
10558 types we'll grow the vector and eventually have to reallocate space
10559 for it, invalidating all copies of pointers into the previous
10561 *dwo_file_slot
= dwo_file
;
10565 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10566 virtual_dwo_name
.c_str ());
10568 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10571 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10572 dwo_unit
->dwo_file
= dwo_file
;
10573 dwo_unit
->signature
= signature
;
10574 dwo_unit
->section
=
10575 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10576 *dwo_unit
->section
= sections
.info_or_types
;
10577 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10582 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10583 simplify them. Given a pointer to the containing section SECTION, and
10584 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10585 virtual section of just that piece. */
10587 static struct dwarf2_section_info
10588 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
10589 struct dwarf2_section_info
*section
,
10590 bfd_size_type offset
, bfd_size_type size
)
10592 struct dwarf2_section_info result
;
10595 gdb_assert (section
!= NULL
);
10596 gdb_assert (!section
->is_virtual
);
10598 memset (&result
, 0, sizeof (result
));
10599 result
.s
.containing_section
= section
;
10600 result
.is_virtual
= true;
10605 sectp
= section
->get_bfd_section ();
10607 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10608 bounds of the real section. This is a pretty-rare event, so just
10609 flag an error (easier) instead of a warning and trying to cope. */
10611 || offset
+ size
> bfd_section_size (sectp
))
10613 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10614 " in section %s [in module %s]"),
10615 sectp
? bfd_section_name (sectp
) : "<unknown>",
10616 objfile_name (per_objfile
->objfile
));
10619 result
.virtual_offset
= offset
;
10620 result
.size
= size
;
10624 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10625 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10626 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10627 This is for DWP version 2 files. */
10629 static struct dwo_unit
*
10630 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
10631 struct dwp_file
*dwp_file
,
10632 uint32_t unit_index
,
10633 const char *comp_dir
,
10634 ULONGEST signature
, int is_debug_types
)
10636 const struct dwp_hash_table
*dwp_htab
=
10637 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10638 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10639 const char *kind
= is_debug_types
? "TU" : "CU";
10640 struct dwo_file
*dwo_file
;
10641 struct dwo_unit
*dwo_unit
;
10642 struct virtual_v2_or_v5_dwo_sections sections
;
10643 void **dwo_file_slot
;
10646 gdb_assert (dwp_file
->version
== 2);
10648 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10649 kind
, pulongest (unit_index
), hex_string (signature
),
10652 /* Fetch the section offsets of this DWO unit. */
10654 memset (§ions
, 0, sizeof (sections
));
10656 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10658 uint32_t offset
= read_4_bytes (dbfd
,
10659 dwp_htab
->section_pool
.v2
.offsets
10660 + (((unit_index
- 1) * dwp_htab
->nr_columns
10662 * sizeof (uint32_t)));
10663 uint32_t size
= read_4_bytes (dbfd
,
10664 dwp_htab
->section_pool
.v2
.sizes
10665 + (((unit_index
- 1) * dwp_htab
->nr_columns
10667 * sizeof (uint32_t)));
10669 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
10672 case DW_SECT_TYPES
:
10673 sections
.info_or_types_offset
= offset
;
10674 sections
.info_or_types_size
= size
;
10676 case DW_SECT_ABBREV
:
10677 sections
.abbrev_offset
= offset
;
10678 sections
.abbrev_size
= size
;
10681 sections
.line_offset
= offset
;
10682 sections
.line_size
= size
;
10685 sections
.loc_offset
= offset
;
10686 sections
.loc_size
= size
;
10688 case DW_SECT_STR_OFFSETS
:
10689 sections
.str_offsets_offset
= offset
;
10690 sections
.str_offsets_size
= size
;
10692 case DW_SECT_MACINFO
:
10693 sections
.macinfo_offset
= offset
;
10694 sections
.macinfo_size
= size
;
10696 case DW_SECT_MACRO
:
10697 sections
.macro_offset
= offset
;
10698 sections
.macro_size
= size
;
10703 /* It's easier for the rest of the code if we fake a struct dwo_file and
10704 have dwo_unit "live" in that. At least for now.
10706 The DWP file can be made up of a random collection of CUs and TUs.
10707 However, for each CU + set of TUs that came from the same original DWO
10708 file, we can combine them back into a virtual DWO file to save space
10709 (fewer struct dwo_file objects to allocate). Remember that for really
10710 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10712 std::string virtual_dwo_name
=
10713 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10714 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10715 (long) (sections
.line_size
? sections
.line_offset
: 0),
10716 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
10717 (long) (sections
.str_offsets_size
10718 ? sections
.str_offsets_offset
: 0));
10719 /* Can we use an existing virtual DWO file? */
10720 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10722 /* Create one if necessary. */
10723 if (*dwo_file_slot
== NULL
)
10725 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10726 virtual_dwo_name
.c_str ());
10728 dwo_file
= new struct dwo_file
;
10729 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10730 dwo_file
->comp_dir
= comp_dir
;
10731 dwo_file
->sections
.abbrev
=
10732 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
10733 sections
.abbrev_offset
,
10734 sections
.abbrev_size
);
10735 dwo_file
->sections
.line
=
10736 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
10737 sections
.line_offset
,
10738 sections
.line_size
);
10739 dwo_file
->sections
.loc
=
10740 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
10741 sections
.loc_offset
, sections
.loc_size
);
10742 dwo_file
->sections
.macinfo
=
10743 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
10744 sections
.macinfo_offset
,
10745 sections
.macinfo_size
);
10746 dwo_file
->sections
.macro
=
10747 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
10748 sections
.macro_offset
,
10749 sections
.macro_size
);
10750 dwo_file
->sections
.str_offsets
=
10751 create_dwp_v2_or_v5_section (per_objfile
,
10752 &dwp_file
->sections
.str_offsets
,
10753 sections
.str_offsets_offset
,
10754 sections
.str_offsets_size
);
10755 /* The "str" section is global to the entire DWP file. */
10756 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10757 /* The info or types section is assigned below to dwo_unit,
10758 there's no need to record it in dwo_file.
10759 Also, we can't simply record type sections in dwo_file because
10760 we record a pointer into the vector in dwo_unit. As we collect more
10761 types we'll grow the vector and eventually have to reallocate space
10762 for it, invalidating all copies of pointers into the previous
10764 *dwo_file_slot
= dwo_file
;
10768 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10769 virtual_dwo_name
.c_str ());
10771 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10774 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10775 dwo_unit
->dwo_file
= dwo_file
;
10776 dwo_unit
->signature
= signature
;
10777 dwo_unit
->section
=
10778 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10779 *dwo_unit
->section
= create_dwp_v2_or_v5_section
10782 ? &dwp_file
->sections
.types
10783 : &dwp_file
->sections
.info
,
10784 sections
.info_or_types_offset
,
10785 sections
.info_or_types_size
);
10786 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10791 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10792 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10793 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10794 This is for DWP version 5 files. */
10796 static struct dwo_unit
*
10797 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
10798 struct dwp_file
*dwp_file
,
10799 uint32_t unit_index
,
10800 const char *comp_dir
,
10801 ULONGEST signature
, int is_debug_types
)
10803 const struct dwp_hash_table
*dwp_htab
10804 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10805 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10806 const char *kind
= is_debug_types
? "TU" : "CU";
10807 struct dwo_file
*dwo_file
;
10808 struct dwo_unit
*dwo_unit
;
10809 struct virtual_v2_or_v5_dwo_sections sections
{};
10810 void **dwo_file_slot
;
10812 gdb_assert (dwp_file
->version
== 5);
10814 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10815 kind
, pulongest (unit_index
), hex_string (signature
),
10818 /* Fetch the section offsets of this DWO unit. */
10820 /* memset (§ions, 0, sizeof (sections)); */
10822 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10824 uint32_t offset
= read_4_bytes (dbfd
,
10825 dwp_htab
->section_pool
.v5
.offsets
10826 + (((unit_index
- 1)
10827 * dwp_htab
->nr_columns
10829 * sizeof (uint32_t)));
10830 uint32_t size
= read_4_bytes (dbfd
,
10831 dwp_htab
->section_pool
.v5
.sizes
10832 + (((unit_index
- 1) * dwp_htab
->nr_columns
10834 * sizeof (uint32_t)));
10836 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
10838 case DW_SECT_ABBREV_V5
:
10839 sections
.abbrev_offset
= offset
;
10840 sections
.abbrev_size
= size
;
10842 case DW_SECT_INFO_V5
:
10843 sections
.info_or_types_offset
= offset
;
10844 sections
.info_or_types_size
= size
;
10846 case DW_SECT_LINE_V5
:
10847 sections
.line_offset
= offset
;
10848 sections
.line_size
= size
;
10850 case DW_SECT_LOCLISTS_V5
:
10851 sections
.loclists_offset
= offset
;
10852 sections
.loclists_size
= size
;
10854 case DW_SECT_MACRO_V5
:
10855 sections
.macro_offset
= offset
;
10856 sections
.macro_size
= size
;
10858 case DW_SECT_RNGLISTS_V5
:
10859 sections
.rnglists_offset
= offset
;
10860 sections
.rnglists_size
= size
;
10862 case DW_SECT_STR_OFFSETS_V5
:
10863 sections
.str_offsets_offset
= offset
;
10864 sections
.str_offsets_size
= size
;
10866 case DW_SECT_RESERVED_V5
:
10872 /* It's easier for the rest of the code if we fake a struct dwo_file and
10873 have dwo_unit "live" in that. At least for now.
10875 The DWP file can be made up of a random collection of CUs and TUs.
10876 However, for each CU + set of TUs that came from the same original DWO
10877 file, we can combine them back into a virtual DWO file to save space
10878 (fewer struct dwo_file objects to allocate). Remember that for really
10879 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10881 std::string virtual_dwo_name
=
10882 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10883 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10884 (long) (sections
.line_size
? sections
.line_offset
: 0),
10885 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
10886 (long) (sections
.str_offsets_size
10887 ? sections
.str_offsets_offset
: 0),
10888 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
10889 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
10890 /* Can we use an existing virtual DWO file? */
10891 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
10892 virtual_dwo_name
.c_str (),
10894 /* Create one if necessary. */
10895 if (*dwo_file_slot
== NULL
)
10897 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10898 virtual_dwo_name
.c_str ());
10900 dwo_file
= new struct dwo_file
;
10901 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10902 dwo_file
->comp_dir
= comp_dir
;
10903 dwo_file
->sections
.abbrev
=
10904 create_dwp_v2_or_v5_section (per_objfile
,
10905 &dwp_file
->sections
.abbrev
,
10906 sections
.abbrev_offset
,
10907 sections
.abbrev_size
);
10908 dwo_file
->sections
.line
=
10909 create_dwp_v2_or_v5_section (per_objfile
,
10910 &dwp_file
->sections
.line
,
10911 sections
.line_offset
, sections
.line_size
);
10912 dwo_file
->sections
.macro
=
10913 create_dwp_v2_or_v5_section (per_objfile
,
10914 &dwp_file
->sections
.macro
,
10915 sections
.macro_offset
,
10916 sections
.macro_size
);
10917 dwo_file
->sections
.loclists
=
10918 create_dwp_v2_or_v5_section (per_objfile
,
10919 &dwp_file
->sections
.loclists
,
10920 sections
.loclists_offset
,
10921 sections
.loclists_size
);
10922 dwo_file
->sections
.rnglists
=
10923 create_dwp_v2_or_v5_section (per_objfile
,
10924 &dwp_file
->sections
.rnglists
,
10925 sections
.rnglists_offset
,
10926 sections
.rnglists_size
);
10927 dwo_file
->sections
.str_offsets
=
10928 create_dwp_v2_or_v5_section (per_objfile
,
10929 &dwp_file
->sections
.str_offsets
,
10930 sections
.str_offsets_offset
,
10931 sections
.str_offsets_size
);
10932 /* The "str" section is global to the entire DWP file. */
10933 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10934 /* The info or types section is assigned below to dwo_unit,
10935 there's no need to record it in dwo_file.
10936 Also, we can't simply record type sections in dwo_file because
10937 we record a pointer into the vector in dwo_unit. As we collect more
10938 types we'll grow the vector and eventually have to reallocate space
10939 for it, invalidating all copies of pointers into the previous
10941 *dwo_file_slot
= dwo_file
;
10945 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10946 virtual_dwo_name
.c_str ());
10948 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10951 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10952 dwo_unit
->dwo_file
= dwo_file
;
10953 dwo_unit
->signature
= signature
;
10955 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10956 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
10957 &dwp_file
->sections
.info
,
10958 sections
.info_or_types_offset
,
10959 sections
.info_or_types_size
);
10960 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10965 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10966 Returns NULL if the signature isn't found. */
10968 static struct dwo_unit
*
10969 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
10970 struct dwp_file
*dwp_file
, const char *comp_dir
,
10971 ULONGEST signature
, int is_debug_types
)
10973 const struct dwp_hash_table
*dwp_htab
=
10974 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10975 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10976 uint32_t mask
= dwp_htab
->nr_slots
- 1;
10977 uint32_t hash
= signature
& mask
;
10978 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
10981 struct dwo_unit find_dwo_cu
;
10983 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
10984 find_dwo_cu
.signature
= signature
;
10985 slot
= htab_find_slot (is_debug_types
10986 ? dwp_file
->loaded_tus
.get ()
10987 : dwp_file
->loaded_cus
.get (),
10988 &find_dwo_cu
, INSERT
);
10991 return (struct dwo_unit
*) *slot
;
10993 /* Use a for loop so that we don't loop forever on bad debug info. */
10994 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
10996 ULONGEST signature_in_table
;
10998 signature_in_table
=
10999 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
11000 if (signature_in_table
== signature
)
11002 uint32_t unit_index
=
11003 read_4_bytes (dbfd
,
11004 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
11006 if (dwp_file
->version
== 1)
11008 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
11009 unit_index
, comp_dir
,
11010 signature
, is_debug_types
);
11012 else if (dwp_file
->version
== 2)
11014 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
11015 unit_index
, comp_dir
,
11016 signature
, is_debug_types
);
11018 else /* version == 5 */
11020 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
11021 unit_index
, comp_dir
,
11022 signature
, is_debug_types
);
11024 return (struct dwo_unit
*) *slot
;
11026 if (signature_in_table
== 0)
11028 hash
= (hash
+ hash2
) & mask
;
11031 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11032 " [in module %s]"),
11036 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11037 Open the file specified by FILE_NAME and hand it off to BFD for
11038 preliminary analysis. Return a newly initialized bfd *, which
11039 includes a canonicalized copy of FILE_NAME.
11040 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11041 SEARCH_CWD is true if the current directory is to be searched.
11042 It will be searched before debug-file-directory.
11043 If successful, the file is added to the bfd include table of the
11044 objfile's bfd (see gdb_bfd_record_inclusion).
11045 If unable to find/open the file, return NULL.
11046 NOTE: This function is derived from symfile_bfd_open. */
11048 static gdb_bfd_ref_ptr
11049 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
11050 const char *file_name
, int is_dwp
, int search_cwd
)
11053 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11054 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11055 to debug_file_directory. */
11056 const char *search_path
;
11057 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
11059 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
11062 if (!debug_file_directory
.empty ())
11064 search_path_holder
.reset (concat (".", dirname_separator_string
,
11065 debug_file_directory
.c_str (),
11067 search_path
= search_path_holder
.get ();
11073 search_path
= debug_file_directory
.c_str ();
11075 /* Add the path for the executable binary to the list of search paths. */
11076 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
11077 search_path_holder
.reset (concat (objfile_dir
.c_str (),
11078 dirname_separator_string
,
11079 search_path
, nullptr));
11080 search_path
= search_path_holder
.get ();
11082 openp_flags flags
= OPF_RETURN_REALPATH
;
11084 flags
|= OPF_SEARCH_IN_PATH
;
11086 gdb::unique_xmalloc_ptr
<char> absolute_name
;
11087 desc
= openp (search_path
, flags
, file_name
,
11088 O_RDONLY
| O_BINARY
, &absolute_name
);
11092 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
11094 if (sym_bfd
== NULL
)
11096 bfd_set_cacheable (sym_bfd
.get (), 1);
11098 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
11101 /* Success. Record the bfd as having been included by the objfile's bfd.
11102 This is important because things like demangled_names_hash lives in the
11103 objfile's per_bfd space and may have references to things like symbol
11104 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11105 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
, sym_bfd
.get ());
11110 /* Try to open DWO file FILE_NAME.
11111 COMP_DIR is the DW_AT_comp_dir attribute.
11112 The result is the bfd handle of the file.
11113 If there is a problem finding or opening the file, return NULL.
11114 Upon success, the canonicalized path of the file is stored in the bfd,
11115 same as symfile_bfd_open. */
11117 static gdb_bfd_ref_ptr
11118 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
11119 const char *file_name
, const char *comp_dir
)
11121 if (IS_ABSOLUTE_PATH (file_name
))
11122 return try_open_dwop_file (per_objfile
, file_name
,
11123 0 /*is_dwp*/, 0 /*search_cwd*/);
11125 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11127 if (comp_dir
!= NULL
)
11129 std::string path_to_try
= path_join (comp_dir
, file_name
);
11131 /* NOTE: If comp_dir is a relative path, this will also try the
11132 search path, which seems useful. */
11133 gdb_bfd_ref_ptr
abfd (try_open_dwop_file
11134 (per_objfile
, path_to_try
.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11140 /* That didn't work, try debug-file-directory, which, despite its name,
11141 is a list of paths. */
11143 if (debug_file_directory
.empty ())
11146 return try_open_dwop_file (per_objfile
, file_name
,
11147 0 /*is_dwp*/, 1 /*search_cwd*/);
11150 /* This function is mapped across the sections and remembers the offset and
11151 size of each of the DWO debugging sections we are interested in. */
11154 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
11155 asection
*sectp
, dwo_sections
*dwo_sections
)
11157 const struct dwop_section_names
*names
= &dwop_section_names
;
11159 struct dwarf2_section_info
*dw_sect
= nullptr;
11161 if (names
->abbrev_dwo
.matches (sectp
->name
))
11162 dw_sect
= &dwo_sections
->abbrev
;
11163 else if (names
->info_dwo
.matches (sectp
->name
))
11164 dw_sect
= &dwo_sections
->info
;
11165 else if (names
->line_dwo
.matches (sectp
->name
))
11166 dw_sect
= &dwo_sections
->line
;
11167 else if (names
->loc_dwo
.matches (sectp
->name
))
11168 dw_sect
= &dwo_sections
->loc
;
11169 else if (names
->loclists_dwo
.matches (sectp
->name
))
11170 dw_sect
= &dwo_sections
->loclists
;
11171 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11172 dw_sect
= &dwo_sections
->macinfo
;
11173 else if (names
->macro_dwo
.matches (sectp
->name
))
11174 dw_sect
= &dwo_sections
->macro
;
11175 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11176 dw_sect
= &dwo_sections
->rnglists
;
11177 else if (names
->str_dwo
.matches (sectp
->name
))
11178 dw_sect
= &dwo_sections
->str
;
11179 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11180 dw_sect
= &dwo_sections
->str_offsets
;
11181 else if (names
->types_dwo
.matches (sectp
->name
))
11183 struct dwarf2_section_info type_section
;
11185 memset (&type_section
, 0, sizeof (type_section
));
11186 dwo_sections
->types
.push_back (type_section
);
11187 dw_sect
= &dwo_sections
->types
.back ();
11190 if (dw_sect
!= nullptr)
11192 dw_sect
->s
.section
= sectp
;
11193 dw_sect
->size
= bfd_section_size (sectp
);
11194 dw_sect
->read (objfile
);
11198 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11199 by PER_CU. This is for the non-DWP case.
11200 The result is NULL if DWO_NAME can't be found. */
11202 static struct dwo_file
*
11203 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
11204 const char *comp_dir
)
11206 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11208 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
11211 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
11216 dwo_file_up
dwo_file (new struct dwo_file
);
11217 dwo_file
->dwo_name
= dwo_name
;
11218 dwo_file
->comp_dir
= comp_dir
;
11219 dwo_file
->dbfd
= std::move (dbfd
);
11221 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
11222 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
11223 sec
, &dwo_file
->sections
);
11225 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
11228 if (cu
->per_cu
->version () < 5)
11230 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
11231 dwo_file
->sections
.types
, dwo_file
->tus
);
11235 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
11236 &dwo_file
->sections
.info
, dwo_file
->tus
,
11237 rcuh_kind::COMPILE
);
11240 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
11242 return dwo_file
.release ();
11245 /* This function is mapped across the sections and remembers the offset and
11246 size of each of the DWP debugging sections common to version 1 and 2 that
11247 we are interested in. */
11250 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11251 asection
*sectp
, dwp_file
*dwp_file
)
11253 const struct dwop_section_names
*names
= &dwop_section_names
;
11254 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11256 /* Record the ELF section number for later lookup: this is what the
11257 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11258 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11259 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11261 /* Look for specific sections that we need. */
11262 struct dwarf2_section_info
*dw_sect
= nullptr;
11263 if (names
->str_dwo
.matches (sectp
->name
))
11264 dw_sect
= &dwp_file
->sections
.str
;
11265 else if (names
->cu_index
.matches (sectp
->name
))
11266 dw_sect
= &dwp_file
->sections
.cu_index
;
11267 else if (names
->tu_index
.matches (sectp
->name
))
11268 dw_sect
= &dwp_file
->sections
.tu_index
;
11270 if (dw_sect
!= nullptr)
11272 dw_sect
->s
.section
= sectp
;
11273 dw_sect
->size
= bfd_section_size (sectp
);
11274 dw_sect
->read (objfile
);
11278 /* This function is mapped across the sections and remembers the offset and
11279 size of each of the DWP version 2 debugging sections that we are interested
11280 in. This is split into a separate function because we don't know if we
11281 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11284 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11285 asection
*sectp
, void *dwp_file_ptr
)
11287 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11288 const struct dwop_section_names
*names
= &dwop_section_names
;
11289 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11291 /* Record the ELF section number for later lookup: this is what the
11292 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11293 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11294 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11296 /* Look for specific sections that we need. */
11297 struct dwarf2_section_info
*dw_sect
= nullptr;
11298 if (names
->abbrev_dwo
.matches (sectp
->name
))
11299 dw_sect
= &dwp_file
->sections
.abbrev
;
11300 else if (names
->info_dwo
.matches (sectp
->name
))
11301 dw_sect
= &dwp_file
->sections
.info
;
11302 else if (names
->line_dwo
.matches (sectp
->name
))
11303 dw_sect
= &dwp_file
->sections
.line
;
11304 else if (names
->loc_dwo
.matches (sectp
->name
))
11305 dw_sect
= &dwp_file
->sections
.loc
;
11306 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11307 dw_sect
= &dwp_file
->sections
.macinfo
;
11308 else if (names
->macro_dwo
.matches (sectp
->name
))
11309 dw_sect
= &dwp_file
->sections
.macro
;
11310 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11311 dw_sect
= &dwp_file
->sections
.str_offsets
;
11312 else if (names
->types_dwo
.matches (sectp
->name
))
11313 dw_sect
= &dwp_file
->sections
.types
;
11315 if (dw_sect
!= nullptr)
11317 dw_sect
->s
.section
= sectp
;
11318 dw_sect
->size
= bfd_section_size (sectp
);
11319 dw_sect
->read (objfile
);
11323 /* This function is mapped across the sections and remembers the offset and
11324 size of each of the DWP version 5 debugging sections that we are interested
11325 in. This is split into a separate function because we don't know if we
11326 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11329 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11330 asection
*sectp
, void *dwp_file_ptr
)
11332 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11333 const struct dwop_section_names
*names
= &dwop_section_names
;
11334 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11336 /* Record the ELF section number for later lookup: this is what the
11337 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11338 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11339 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11341 /* Look for specific sections that we need. */
11342 struct dwarf2_section_info
*dw_sect
= nullptr;
11343 if (names
->abbrev_dwo
.matches (sectp
->name
))
11344 dw_sect
= &dwp_file
->sections
.abbrev
;
11345 else if (names
->info_dwo
.matches (sectp
->name
))
11346 dw_sect
= &dwp_file
->sections
.info
;
11347 else if (names
->line_dwo
.matches (sectp
->name
))
11348 dw_sect
= &dwp_file
->sections
.line
;
11349 else if (names
->loclists_dwo
.matches (sectp
->name
))
11350 dw_sect
= &dwp_file
->sections
.loclists
;
11351 else if (names
->macro_dwo
.matches (sectp
->name
))
11352 dw_sect
= &dwp_file
->sections
.macro
;
11353 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11354 dw_sect
= &dwp_file
->sections
.rnglists
;
11355 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11356 dw_sect
= &dwp_file
->sections
.str_offsets
;
11358 if (dw_sect
!= nullptr)
11360 dw_sect
->s
.section
= sectp
;
11361 dw_sect
->size
= bfd_section_size (sectp
);
11362 dw_sect
->read (objfile
);
11366 /* Hash function for dwp_file loaded CUs/TUs. */
11369 hash_dwp_loaded_cutus (const void *item
)
11371 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
11373 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11374 return dwo_unit
->signature
;
11377 /* Equality function for dwp_file loaded CUs/TUs. */
11380 eq_dwp_loaded_cutus (const void *a
, const void *b
)
11382 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
11383 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
11385 return dua
->signature
== dub
->signature
;
11388 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11391 allocate_dwp_loaded_cutus_table ()
11393 return htab_up (htab_create_alloc (3,
11394 hash_dwp_loaded_cutus
,
11395 eq_dwp_loaded_cutus
,
11396 NULL
, xcalloc
, xfree
));
11399 /* Try to open DWP file FILE_NAME.
11400 The result is the bfd handle of the file.
11401 If there is a problem finding or opening the file, return NULL.
11402 Upon success, the canonicalized path of the file is stored in the bfd,
11403 same as symfile_bfd_open. */
11405 static gdb_bfd_ref_ptr
11406 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
11408 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
11410 1 /*search_cwd*/));
11414 /* Work around upstream bug 15652.
11415 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11416 [Whether that's a "bug" is debatable, but it is getting in our way.]
11417 We have no real idea where the dwp file is, because gdb's realpath-ing
11418 of the executable's path may have discarded the needed info.
11419 [IWBN if the dwp file name was recorded in the executable, akin to
11420 .gnu_debuglink, but that doesn't exist yet.]
11421 Strip the directory from FILE_NAME and search again. */
11422 if (!debug_file_directory
.empty ())
11424 /* Don't implicitly search the current directory here.
11425 If the user wants to search "." to handle this case,
11426 it must be added to debug-file-directory. */
11427 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
11435 /* Initialize the use of the DWP file for the current objfile.
11436 By convention the name of the DWP file is ${objfile}.dwp.
11437 The result is NULL if it can't be found. */
11439 static std::unique_ptr
<struct dwp_file
>
11440 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
11442 struct objfile
*objfile
= per_objfile
->objfile
;
11444 /* Try to find first .dwp for the binary file before any symbolic links
11447 /* If the objfile is a debug file, find the name of the real binary
11448 file and get the name of dwp file from there. */
11449 std::string dwp_name
;
11450 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
11452 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
11453 const char *backlink_basename
= lbasename (backlink
->original_name
);
11455 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
11458 dwp_name
= objfile
->original_name
;
11460 dwp_name
+= ".dwp";
11462 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
11464 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
11466 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11467 dwp_name
= objfile_name (objfile
);
11468 dwp_name
+= ".dwp";
11469 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
11474 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
11476 return std::unique_ptr
<dwp_file
> ();
11479 const char *name
= bfd_get_filename (dbfd
.get ());
11480 std::unique_ptr
<struct dwp_file
> dwp_file
11481 (new struct dwp_file (name
, std::move (dbfd
)));
11483 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
11484 dwp_file
->elf_sections
=
11485 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
11486 dwp_file
->num_sections
, asection
*);
11488 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11489 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11492 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
11494 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
11496 /* The DWP file version is stored in the hash table. Oh well. */
11497 if (dwp_file
->cus
&& dwp_file
->tus
11498 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
11500 /* Technically speaking, we should try to limp along, but this is
11501 pretty bizarre. We use pulongest here because that's the established
11502 portability solution (e.g, we cannot use %u for uint32_t). */
11503 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11504 " TU version %s [in DWP file %s]"),
11505 pulongest (dwp_file
->cus
->version
),
11506 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
11510 dwp_file
->version
= dwp_file
->cus
->version
;
11511 else if (dwp_file
->tus
)
11512 dwp_file
->version
= dwp_file
->tus
->version
;
11514 dwp_file
->version
= 2;
11516 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11518 if (dwp_file
->version
== 2)
11519 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11522 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11526 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
11527 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
11529 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
11530 dwarf_read_debug_printf (" %s CUs, %s TUs",
11531 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
11532 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
11537 /* Wrapper around open_and_init_dwp_file, only open it once. */
11539 static struct dwp_file
*
11540 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
11542 if (!per_objfile
->per_bfd
->dwp_checked
)
11544 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
11545 per_objfile
->per_bfd
->dwp_checked
= 1;
11547 return per_objfile
->per_bfd
->dwp_file
.get ();
11550 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11551 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11552 or in the DWP file for the objfile, referenced by THIS_UNIT.
11553 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11554 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11556 This is called, for example, when wanting to read a variable with a
11557 complex location. Therefore we don't want to do file i/o for every call.
11558 Therefore we don't want to look for a DWO file on every call.
11559 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11560 then we check if we've already seen DWO_NAME, and only THEN do we check
11563 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11564 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11566 static struct dwo_unit
*
11567 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11568 ULONGEST signature
, int is_debug_types
)
11570 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11571 struct objfile
*objfile
= per_objfile
->objfile
;
11572 const char *kind
= is_debug_types
? "TU" : "CU";
11573 void **dwo_file_slot
;
11574 struct dwo_file
*dwo_file
;
11575 struct dwp_file
*dwp_file
;
11577 /* First see if there's a DWP file.
11578 If we have a DWP file but didn't find the DWO inside it, don't
11579 look for the original DWO file. It makes gdb behave differently
11580 depending on whether one is debugging in the build tree. */
11582 dwp_file
= get_dwp_file (per_objfile
);
11583 if (dwp_file
!= NULL
)
11585 const struct dwp_hash_table
*dwp_htab
=
11586 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11588 if (dwp_htab
!= NULL
)
11590 struct dwo_unit
*dwo_cutu
=
11591 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
11594 if (dwo_cutu
!= NULL
)
11596 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11597 kind
, hex_string (signature
),
11598 host_address_to_string (dwo_cutu
));
11606 /* No DWP file, look for the DWO file. */
11608 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
11609 if (*dwo_file_slot
== NULL
)
11611 /* Read in the file and build a table of the CUs/TUs it contains. */
11612 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
11614 /* NOTE: This will be NULL if unable to open the file. */
11615 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
11617 if (dwo_file
!= NULL
)
11619 struct dwo_unit
*dwo_cutu
= NULL
;
11621 if (is_debug_types
&& dwo_file
->tus
)
11623 struct dwo_unit find_dwo_cutu
;
11625 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11626 find_dwo_cutu
.signature
= signature
;
11628 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
11631 else if (!is_debug_types
&& dwo_file
->cus
)
11633 struct dwo_unit find_dwo_cutu
;
11635 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11636 find_dwo_cutu
.signature
= signature
;
11637 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
11641 if (dwo_cutu
!= NULL
)
11643 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11644 kind
, dwo_name
, hex_string (signature
),
11645 host_address_to_string (dwo_cutu
));
11652 /* We didn't find it. This could mean a dwo_id mismatch, or
11653 someone deleted the DWO/DWP file, or the search path isn't set up
11654 correctly to find the file. */
11656 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11657 kind
, dwo_name
, hex_string (signature
));
11659 /* This is a warning and not a complaint because it can be caused by
11660 pilot error (e.g., user accidentally deleting the DWO). */
11662 /* Print the name of the DWP file if we looked there, helps the user
11663 better diagnose the problem. */
11664 std::string dwp_text
;
11666 if (dwp_file
!= NULL
)
11667 dwp_text
= string_printf (" [in DWP file %s]",
11668 lbasename (dwp_file
->name
));
11670 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11671 " [in module %s]"),
11672 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
11673 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
11678 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11679 See lookup_dwo_cutu_unit for details. */
11681 static struct dwo_unit
*
11682 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11683 ULONGEST signature
)
11685 gdb_assert (!cu
->per_cu
->is_debug_types
);
11687 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
11690 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11691 See lookup_dwo_cutu_unit for details. */
11693 static struct dwo_unit
*
11694 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
11696 gdb_assert (cu
->per_cu
->is_debug_types
);
11698 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
11700 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
11703 /* Traversal function for queue_and_load_all_dwo_tus. */
11706 queue_and_load_dwo_tu (void **slot
, void *info
)
11708 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
11709 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
11710 ULONGEST signature
= dwo_unit
->signature
;
11711 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
11713 if (sig_type
!= NULL
)
11715 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11716 a real dependency of PER_CU on SIG_TYPE. That is detected later
11717 while processing PER_CU. */
11718 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
11720 load_full_type_unit (sig_type
, cu
->per_objfile
);
11721 cu
->per_cu
->imported_symtabs_push (sig_type
);
11727 /* Queue all TUs contained in the DWO of CU to be read in.
11728 The DWO may have the only definition of the type, though it may not be
11729 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11730 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11733 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
11735 struct dwo_unit
*dwo_unit
;
11736 struct dwo_file
*dwo_file
;
11738 gdb_assert (cu
!= nullptr);
11739 gdb_assert (!cu
->per_cu
->is_debug_types
);
11740 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
11742 dwo_unit
= cu
->dwo_unit
;
11743 gdb_assert (dwo_unit
!= NULL
);
11745 dwo_file
= dwo_unit
->dwo_file
;
11746 if (dwo_file
->tus
!= NULL
)
11747 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
11750 /* Read in various DIEs. */
11752 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11753 Inherit only the children of the DW_AT_abstract_origin DIE not being
11754 already referenced by DW_AT_abstract_origin from the children of the
11758 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
11760 struct die_info
*child_die
;
11761 sect_offset
*offsetp
;
11762 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11763 struct die_info
*origin_die
;
11764 /* Iterator of the ORIGIN_DIE children. */
11765 struct die_info
*origin_child_die
;
11766 struct attribute
*attr
;
11767 struct dwarf2_cu
*origin_cu
;
11768 struct pending
**origin_previous_list_in_scope
;
11770 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
11774 /* Note that following die references may follow to a die in a
11778 origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
11780 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11782 origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
11783 origin_cu
->list_in_scope
= cu
->list_in_scope
;
11785 if (die
->tag
!= origin_die
->tag
11786 && !(die
->tag
== DW_TAG_inlined_subroutine
11787 && origin_die
->tag
== DW_TAG_subprogram
))
11788 complaint (_("DIE %s and its abstract origin %s have different tags"),
11789 sect_offset_str (die
->sect_off
),
11790 sect_offset_str (origin_die
->sect_off
));
11792 /* Find if the concrete and abstract trees are structurally the
11793 same. This is a shallow traversal and it is not bullet-proof;
11794 the compiler can trick the debugger into believing that the trees
11795 are isomorphic, whereas they actually are not. However, the
11796 likelyhood of this happening is pretty low, and a full-fledged
11797 check would be an overkill. */
11798 bool are_isomorphic
= true;
11799 die_info
*concrete_child
= die
->child
;
11800 die_info
*abstract_child
= origin_die
->child
;
11801 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
11803 if (concrete_child
== nullptr
11804 || abstract_child
== nullptr
11805 || concrete_child
->tag
!= abstract_child
->tag
)
11807 are_isomorphic
= false;
11811 concrete_child
= concrete_child
->sibling
;
11812 abstract_child
= abstract_child
->sibling
;
11815 /* Walk the origin's children in parallel to the concrete children.
11816 This helps match an origin child in case the debug info misses
11817 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11818 origin tree may not have the same tree structure as the concrete
11820 die_info
*corresponding_abstract_child
11821 = are_isomorphic
? origin_die
->child
: nullptr;
11823 std::vector
<sect_offset
> offsets
;
11825 for (child_die
= die
->child
;
11826 child_die
&& child_die
->tag
;
11827 child_die
= child_die
->sibling
)
11829 struct die_info
*child_origin_die
;
11830 struct dwarf2_cu
*child_origin_cu
;
11832 /* We are trying to process concrete instance entries:
11833 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11834 it's not relevant to our analysis here. i.e. detecting DIEs that are
11835 present in the abstract instance but not referenced in the concrete
11837 if (child_die
->tag
== DW_TAG_call_site
11838 || child_die
->tag
== DW_TAG_GNU_call_site
)
11840 if (are_isomorphic
)
11841 corresponding_abstract_child
11842 = corresponding_abstract_child
->sibling
;
11846 /* For each CHILD_DIE, find the corresponding child of
11847 ORIGIN_DIE. If there is more than one layer of
11848 DW_AT_abstract_origin, follow them all; there shouldn't be,
11849 but GCC versions at least through 4.4 generate this (GCC PR
11851 child_origin_die
= child_die
;
11852 child_origin_cu
= cu
;
11855 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
11859 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
11863 /* If missing DW_AT_abstract_origin, try the corresponding child
11864 of the origin. Clang emits such lexical scopes. */
11865 if (child_origin_die
== child_die
11866 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
11868 && child_die
->tag
== DW_TAG_lexical_block
)
11869 child_origin_die
= corresponding_abstract_child
;
11871 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11872 counterpart may exist. */
11873 if (child_origin_die
!= child_die
)
11875 if (child_die
->tag
!= child_origin_die
->tag
11876 && !(child_die
->tag
== DW_TAG_inlined_subroutine
11877 && child_origin_die
->tag
== DW_TAG_subprogram
))
11878 complaint (_("Child DIE %s and its abstract origin %s have "
11880 sect_offset_str (child_die
->sect_off
),
11881 sect_offset_str (child_origin_die
->sect_off
));
11882 if (child_origin_die
->parent
!= origin_die
)
11883 complaint (_("Child DIE %s and its abstract origin %s have "
11884 "different parents"),
11885 sect_offset_str (child_die
->sect_off
),
11886 sect_offset_str (child_origin_die
->sect_off
));
11888 offsets
.push_back (child_origin_die
->sect_off
);
11891 if (are_isomorphic
)
11892 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
11894 std::sort (offsets
.begin (), offsets
.end ());
11895 sect_offset
*offsets_end
= offsets
.data () + offsets
.size ();
11896 for (offsetp
= offsets
.data () + 1; offsetp
< offsets_end
; offsetp
++)
11897 if (offsetp
[-1] == *offsetp
)
11898 complaint (_("Multiple children of DIE %s refer "
11899 "to DIE %s as their abstract origin"),
11900 sect_offset_str (die
->sect_off
), sect_offset_str (*offsetp
));
11902 offsetp
= offsets
.data ();
11903 origin_child_die
= origin_die
->child
;
11904 while (origin_child_die
&& origin_child_die
->tag
)
11906 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11907 while (offsetp
< offsets_end
11908 && *offsetp
< origin_child_die
->sect_off
)
11910 if (offsetp
>= offsets_end
11911 || *offsetp
> origin_child_die
->sect_off
)
11913 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11914 Check whether we're already processing ORIGIN_CHILD_DIE.
11915 This can happen with mutually referenced abstract_origins.
11917 if (!origin_child_die
->in_process
)
11918 process_die (origin_child_die
, origin_cu
);
11920 origin_child_die
= origin_child_die
->sibling
;
11922 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
11924 if (cu
!= origin_cu
)
11925 compute_delayed_physnames (origin_cu
);
11929 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
11931 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11932 struct gdbarch
*gdbarch
= objfile
->arch ();
11933 struct context_stack
*newobj
;
11936 struct die_info
*child_die
;
11937 struct attribute
*attr
, *call_line
, *call_file
;
11939 CORE_ADDR baseaddr
;
11940 struct block
*block
;
11941 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
11942 std::vector
<struct symbol
*> template_args
;
11943 struct template_symbol
*templ_func
= NULL
;
11947 /* If we do not have call site information, we can't show the
11948 caller of this inlined function. That's too confusing, so
11949 only use the scope for local variables. */
11950 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
11951 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
11952 if (call_line
== NULL
|| call_file
== NULL
)
11954 read_lexical_block_scope (die
, cu
);
11959 baseaddr
= objfile
->text_section_offset ();
11961 name
= dwarf2_name (die
, cu
);
11962 if (name
== nullptr)
11963 name
= dw2_linkage_name (die
, cu
);
11965 /* Ignore functions with missing or empty names. These are actually
11966 illegal according to the DWARF standard. */
11969 complaint (_("missing name for subprogram DIE at %s"),
11970 sect_offset_str (die
->sect_off
));
11974 /* Ignore functions with missing or invalid low and high pc attributes. */
11975 if (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr)
11976 <= PC_BOUNDS_INVALID
)
11978 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
11979 if (attr
== nullptr || !attr
->as_boolean ())
11980 complaint (_("cannot get low and high bounds "
11981 "for subprogram DIE at %s"),
11982 sect_offset_str (die
->sect_off
));
11986 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
11987 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
11989 /* If we have any template arguments, then we must allocate a
11990 different sort of symbol. */
11991 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
11993 if (child_die
->tag
== DW_TAG_template_type_param
11994 || child_die
->tag
== DW_TAG_template_value_param
)
11996 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
11997 templ_func
->subclass
= SYMBOL_TEMPLATE
;
12002 gdb_assert (cu
->get_builder () != nullptr);
12003 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
12004 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
,
12005 (struct symbol
*) templ_func
);
12007 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
12008 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
12011 /* If there is a location expression for DW_AT_frame_base, record
12013 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
12014 if (attr
!= nullptr)
12015 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
12017 /* If there is a location for the static link, record it. */
12018 newobj
->static_link
= NULL
;
12019 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
12020 if (attr
!= nullptr)
12022 newobj
->static_link
12023 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
12024 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
12028 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
12030 if (die
->child
!= NULL
)
12032 child_die
= die
->child
;
12033 while (child_die
&& child_die
->tag
)
12035 if (child_die
->tag
== DW_TAG_template_type_param
12036 || child_die
->tag
== DW_TAG_template_value_param
)
12038 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12041 template_args
.push_back (arg
);
12044 process_die (child_die
, cu
);
12045 child_die
= child_die
->sibling
;
12049 inherit_abstract_dies (die
, cu
);
12051 /* If we have a DW_AT_specification, we might need to import using
12052 directives from the context of the specification DIE. See the
12053 comment in determine_prefix. */
12054 if (cu
->lang () == language_cplus
12055 && dwarf2_attr (die
, DW_AT_specification
, cu
))
12057 struct dwarf2_cu
*spec_cu
= cu
;
12058 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
12062 child_die
= spec_die
->child
;
12063 while (child_die
&& child_die
->tag
)
12065 if (child_die
->tag
== DW_TAG_imported_module
)
12066 process_die (child_die
, spec_cu
);
12067 child_die
= child_die
->sibling
;
12070 /* In some cases, GCC generates specification DIEs that
12071 themselves contain DW_AT_specification attributes. */
12072 spec_die
= die_specification (spec_die
, &spec_cu
);
12076 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12077 /* Make a block for the local symbols within. */
12078 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
12079 cstk
.static_link
, lowpc
, highpc
);
12081 /* For C++, set the block's scope. */
12082 if ((cu
->lang () == language_cplus
12083 || cu
->lang () == language_fortran
12084 || cu
->lang () == language_d
12085 || cu
->lang () == language_rust
)
12086 && cu
->processing_has_namespace_info
)
12087 block_set_scope (block
, determine_prefix (die
, cu
),
12088 &objfile
->objfile_obstack
);
12090 /* If we have address ranges, record them. */
12091 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12093 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
12095 /* Attach template arguments to function. */
12096 if (!template_args
.empty ())
12098 gdb_assert (templ_func
!= NULL
);
12100 templ_func
->n_template_arguments
= template_args
.size ();
12101 templ_func
->template_arguments
12102 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
12103 templ_func
->n_template_arguments
);
12104 memcpy (templ_func
->template_arguments
,
12105 template_args
.data (),
12106 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
12108 /* Make sure that the symtab is set on the new symbols. Even
12109 though they don't appear in this symtab directly, other parts
12110 of gdb assume that symbols do, and this is reasonably
12112 for (symbol
*sym
: template_args
)
12113 sym
->set_symtab (templ_func
->symtab ());
12116 /* In C++, we can have functions nested inside functions (e.g., when
12117 a function declares a class that has methods). This means that
12118 when we finish processing a function scope, we may need to go
12119 back to building a containing block's symbol lists. */
12120 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12121 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12123 /* If we've finished processing a top-level function, subsequent
12124 symbols go in the file symbol list. */
12125 if (cu
->get_builder ()->outermost_context_p ())
12126 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
12129 /* Process all the DIES contained within a lexical block scope. Start
12130 a new scope, process the dies, and then close the scope. */
12133 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12135 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12136 struct gdbarch
*gdbarch
= objfile
->arch ();
12137 CORE_ADDR lowpc
, highpc
;
12138 struct die_info
*child_die
;
12139 CORE_ADDR baseaddr
;
12141 baseaddr
= objfile
->text_section_offset ();
12143 /* Ignore blocks with missing or invalid low and high pc attributes. */
12144 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12145 as multiple lexical blocks? Handling children in a sane way would
12146 be nasty. Might be easier to properly extend generic blocks to
12147 describe ranges. */
12148 switch (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr))
12150 case PC_BOUNDS_NOT_PRESENT
:
12151 /* DW_TAG_lexical_block has no attributes, process its children as if
12152 there was no wrapping by that DW_TAG_lexical_block.
12153 GCC does no longer produces such DWARF since GCC r224161. */
12154 for (child_die
= die
->child
;
12155 child_die
!= NULL
&& child_die
->tag
;
12156 child_die
= child_die
->sibling
)
12158 /* We might already be processing this DIE. This can happen
12159 in an unusual circumstance -- where a subroutine A
12160 appears lexically in another subroutine B, but A actually
12161 inlines B. The recursion is broken here, rather than in
12162 inherit_abstract_dies, because it seems better to simply
12163 drop concrete children here. */
12164 if (!child_die
->in_process
)
12165 process_die (child_die
, cu
);
12168 case PC_BOUNDS_INVALID
:
12171 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12172 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12174 cu
->get_builder ()->push_context (0, lowpc
);
12175 if (die
->child
!= NULL
)
12177 child_die
= die
->child
;
12178 while (child_die
&& child_die
->tag
)
12180 process_die (child_die
, cu
);
12181 child_die
= child_die
->sibling
;
12184 inherit_abstract_dies (die
, cu
);
12185 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12187 if (*cu
->get_builder ()->get_local_symbols () != NULL
12188 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
12190 struct block
*block
12191 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
12192 cstk
.start_addr
, highpc
);
12194 /* Note that recording ranges after traversing children, as we
12195 do here, means that recording a parent's ranges entails
12196 walking across all its children's ranges as they appear in
12197 the address map, which is quadratic behavior.
12199 It would be nicer to record the parent's ranges before
12200 traversing its children, simply overriding whatever you find
12201 there. But since we don't even decide whether to create a
12202 block until after we've traversed its children, that's hard
12204 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12206 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12207 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12210 static void dwarf2_ranges_read_low_addrs (unsigned offset
,
12211 struct dwarf2_cu
*cu
,
12213 std::vector
<CORE_ADDR
> &result
);
12215 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12218 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12220 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12221 struct objfile
*objfile
= per_objfile
->objfile
;
12222 struct gdbarch
*gdbarch
= objfile
->arch ();
12223 CORE_ADDR pc
, baseaddr
;
12224 struct attribute
*attr
;
12227 struct die_info
*child_die
;
12229 baseaddr
= objfile
->text_section_offset ();
12231 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
12234 /* This was a pre-DWARF-5 GNU extension alias
12235 for DW_AT_call_return_pc. */
12236 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12240 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12241 "DIE %s [in module %s]"),
12242 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12245 pc
= attr
->as_address () + baseaddr
;
12246 pc
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc
);
12249 if (cu
->call_site_htab
== NULL
)
12250 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
12251 call_site::eq
, NULL
,
12252 &objfile
->objfile_obstack
,
12253 hashtab_obstack_allocate
, NULL
);
12254 struct call_site
call_site_local (pc
, nullptr, nullptr);
12255 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
12258 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12259 "DIE %s [in module %s]"),
12260 paddress (gdbarch
, pc
), sect_offset_str (die
->sect_off
),
12261 objfile_name (objfile
));
12265 /* Count parameters at the caller. */
12268 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
12269 child_die
= child_die
->sibling
)
12271 if (child_die
->tag
!= DW_TAG_call_site_parameter
12272 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12274 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12275 "DW_TAG_call_site child DIE %s [in module %s]"),
12276 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
12277 objfile_name (objfile
));
12284 struct call_site
*call_site
12285 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
12287 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
12288 struct call_site (pc
, cu
->per_cu
, per_objfile
);
12291 /* We never call the destructor of call_site, so we must ensure it is
12292 trivially destructible. */
12293 gdb_static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
12295 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
12296 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
12298 struct die_info
*func_die
;
12300 /* Skip also over DW_TAG_inlined_subroutine. */
12301 for (func_die
= die
->parent
;
12302 func_die
&& func_die
->tag
!= DW_TAG_subprogram
12303 && func_die
->tag
!= DW_TAG_subroutine_type
;
12304 func_die
= func_die
->parent
);
12306 /* DW_AT_call_all_calls is a superset
12307 of DW_AT_call_all_tail_calls. */
12309 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
12310 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
12311 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
12312 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
12314 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12315 not complete. But keep CALL_SITE for look ups via call_site_htab,
12316 both the initial caller containing the real return address PC and
12317 the final callee containing the current PC of a chain of tail
12318 calls do not need to have the tail call list complete. But any
12319 function candidate for a virtual tail call frame searched via
12320 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12321 determined unambiguously. */
12325 struct type
*func_type
= NULL
;
12328 func_type
= get_die_type (func_die
, cu
);
12329 if (func_type
!= NULL
)
12331 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
12333 /* Enlist this call site to the function. */
12334 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
12335 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
12338 complaint (_("Cannot find function owning DW_TAG_call_site "
12339 "DIE %s [in module %s]"),
12340 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12344 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
12346 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
12348 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
12351 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12352 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12355 call_site
->target
.set_loc_dwarf_block (nullptr);
12356 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
12357 /* Keep NULL DWARF_BLOCK. */;
12358 else if (attr
->form_is_block ())
12360 struct dwarf2_locexpr_baton
*dlbaton
;
12361 struct dwarf_block
*block
= attr
->as_block ();
12363 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
12364 dlbaton
->data
= block
->data
;
12365 dlbaton
->size
= block
->size
;
12366 dlbaton
->per_objfile
= per_objfile
;
12367 dlbaton
->per_cu
= cu
->per_cu
;
12369 call_site
->target
.set_loc_dwarf_block (dlbaton
);
12371 else if (attr
->form_is_ref ())
12373 struct dwarf2_cu
*target_cu
= cu
;
12374 struct die_info
*target_die
;
12376 target_die
= follow_die_ref (die
, attr
, &target_cu
);
12377 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
12379 struct attribute
*ranges_attr
12380 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
12382 if (die_is_declaration (target_die
, target_cu
))
12384 const char *target_physname
;
12386 /* Prefer the mangled name; otherwise compute the demangled one. */
12387 target_physname
= dw2_linkage_name (target_die
, target_cu
);
12388 if (target_physname
== NULL
)
12389 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
12390 if (target_physname
== NULL
)
12391 complaint (_("DW_AT_call_target target DIE has invalid "
12392 "physname, for referencing DIE %s [in module %s]"),
12393 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12395 call_site
->target
.set_loc_physname (target_physname
);
12397 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
12399 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
12400 + target_cu
->gnu_ranges_base
);
12401 std::vector
<CORE_ADDR
> addresses
;
12402 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
12403 target_die
->tag
, addresses
);
12404 CORE_ADDR
*saved
= XOBNEWVAR (&objfile
->objfile_obstack
, CORE_ADDR
,
12405 addresses
.size ());
12406 std::copy (addresses
.begin (), addresses
.end (), saved
);
12407 call_site
->target
.set_loc_array (addresses
.size (), saved
);
12413 /* DW_AT_entry_pc should be preferred. */
12414 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
12416 <= PC_BOUNDS_INVALID
)
12417 complaint (_("DW_AT_call_target target DIE has invalid "
12418 "low pc, for referencing DIE %s [in module %s]"),
12419 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12422 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
)
12424 call_site
->target
.set_loc_physaddr (lowpc
);
12429 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12430 "block nor reference, for DIE %s [in module %s]"),
12431 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12433 for (child_die
= die
->child
;
12434 child_die
&& child_die
->tag
;
12435 child_die
= child_die
->sibling
)
12437 struct call_site_parameter
*parameter
;
12438 struct attribute
*loc
, *origin
;
12440 if (child_die
->tag
!= DW_TAG_call_site_parameter
12441 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12443 /* Already printed the complaint above. */
12447 gdb_assert (call_site
->parameter_count
< nparams
);
12448 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
12450 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12451 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12452 register is contained in DW_AT_call_value. */
12454 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
12455 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
12456 if (origin
== NULL
)
12458 /* This was a pre-DWARF-5 GNU extension alias
12459 for DW_AT_call_parameter. */
12460 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
12462 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
12464 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
12466 sect_offset sect_off
= origin
->get_ref_die_offset ();
12467 if (!cu
->header
.offset_in_cu_p (sect_off
))
12469 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12470 binding can be done only inside one CU. Such referenced DIE
12471 therefore cannot be even moved to DW_TAG_partial_unit. */
12472 complaint (_("DW_AT_call_parameter offset is not in CU for "
12473 "DW_TAG_call_site child DIE %s [in module %s]"),
12474 sect_offset_str (child_die
->sect_off
),
12475 objfile_name (objfile
));
12478 parameter
->u
.param_cu_off
12479 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
12481 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
12483 complaint (_("No DW_FORM_block* DW_AT_location for "
12484 "DW_TAG_call_site child DIE %s [in module %s]"),
12485 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
12490 struct dwarf_block
*block
= loc
->as_block ();
12492 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
12493 (block
->data
, &block
->data
[block
->size
]);
12494 if (parameter
->u
.dwarf_reg
!= -1)
12495 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
12496 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
12497 &block
->data
[block
->size
],
12498 ¶meter
->u
.fb_offset
))
12499 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
12502 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12503 "for DW_FORM_block* DW_AT_location is supported for "
12504 "DW_TAG_call_site child DIE %s "
12506 sect_offset_str (child_die
->sect_off
),
12507 objfile_name (objfile
));
12512 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
12514 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
12515 if (attr
== NULL
|| !attr
->form_is_block ())
12517 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12518 "DW_TAG_call_site child DIE %s [in module %s]"),
12519 sect_offset_str (child_die
->sect_off
),
12520 objfile_name (objfile
));
12524 struct dwarf_block
*block
= attr
->as_block ();
12525 parameter
->value
= block
->data
;
12526 parameter
->value_size
= block
->size
;
12528 /* Parameters are not pre-cleared by memset above. */
12529 parameter
->data_value
= NULL
;
12530 parameter
->data_value_size
= 0;
12531 call_site
->parameter_count
++;
12533 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
12535 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
12536 if (attr
!= nullptr)
12538 if (!attr
->form_is_block ())
12539 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12540 "DW_TAG_call_site child DIE %s [in module %s]"),
12541 sect_offset_str (child_die
->sect_off
),
12542 objfile_name (objfile
));
12545 block
= attr
->as_block ();
12546 parameter
->data_value
= block
->data
;
12547 parameter
->data_value_size
= block
->size
;
12553 /* Helper function for read_variable. If DIE represents a virtual
12554 table, then return the type of the concrete object that is
12555 associated with the virtual table. Otherwise, return NULL. */
12557 static struct type
*
12558 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12560 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
12564 /* Find the type DIE. */
12565 struct die_info
*type_die
= NULL
;
12566 struct dwarf2_cu
*type_cu
= cu
;
12568 if (attr
->form_is_ref ())
12569 type_die
= follow_die_ref (die
, attr
, &type_cu
);
12570 if (type_die
== NULL
)
12573 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
12575 return die_containing_type (type_die
, type_cu
);
12578 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12581 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
12583 struct rust_vtable_symbol
*storage
= NULL
;
12585 if (cu
->lang () == language_rust
)
12587 struct type
*containing_type
= rust_containing_type (die
, cu
);
12589 if (containing_type
!= NULL
)
12591 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12593 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
12594 storage
->concrete_type
= containing_type
;
12595 storage
->subclass
= SYMBOL_RUST_VTABLE
;
12599 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
12600 struct attribute
*abstract_origin
12601 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12602 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
12603 if (res
== NULL
&& loc
&& abstract_origin
)
12605 /* We have a variable without a name, but with a location and an abstract
12606 origin. This may be a concrete instance of an abstract variable
12607 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12609 struct dwarf2_cu
*origin_cu
= cu
;
12610 struct die_info
*origin_die
12611 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
12612 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12613 per_objfile
->per_bfd
->abstract_to_concrete
12614 [origin_die
->sect_off
].push_back (die
->sect_off
);
12618 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12619 reading .debug_rnglists.
12620 Callback's type should be:
12621 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12622 Return true if the attributes are present and valid, otherwise,
12625 template <typename Callback
>
12627 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
12628 dwarf_tag tag
, Callback
&&callback
)
12630 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12631 struct objfile
*objfile
= per_objfile
->objfile
;
12632 bfd
*obfd
= objfile
->obfd
;
12633 /* Base address selection entry. */
12634 gdb::optional
<CORE_ADDR
> base
;
12635 const gdb_byte
*buffer
;
12636 bool overflow
= false;
12637 ULONGEST addr_index
;
12638 struct dwarf2_section_info
*rnglists_section
;
12640 base
= cu
->base_address
;
12641 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
12642 rnglists_section
->read (objfile
);
12644 if (offset
>= rnglists_section
->size
)
12646 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12650 buffer
= rnglists_section
->buffer
+ offset
;
12654 /* Initialize it due to a false compiler warning. */
12655 CORE_ADDR range_beginning
= 0, range_end
= 0;
12656 const gdb_byte
*buf_end
= (rnglists_section
->buffer
12657 + rnglists_section
->size
);
12658 unsigned int bytes_read
;
12660 if (buffer
== buf_end
)
12665 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
12668 case DW_RLE_end_of_list
:
12670 case DW_RLE_base_address
:
12671 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12676 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12677 buffer
+= bytes_read
;
12679 case DW_RLE_base_addressx
:
12680 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12681 buffer
+= bytes_read
;
12682 base
= read_addr_index (cu
, addr_index
);
12684 case DW_RLE_start_length
:
12685 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12690 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12692 buffer
+= bytes_read
;
12693 range_end
= (range_beginning
12694 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12695 buffer
+= bytes_read
;
12696 if (buffer
> buf_end
)
12702 case DW_RLE_startx_length
:
12703 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12704 buffer
+= bytes_read
;
12705 range_beginning
= read_addr_index (cu
, addr_index
);
12706 if (buffer
> buf_end
)
12711 range_end
= (range_beginning
12712 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12713 buffer
+= bytes_read
;
12715 case DW_RLE_offset_pair
:
12716 range_beginning
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12717 buffer
+= bytes_read
;
12718 if (buffer
> buf_end
)
12723 range_end
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12724 buffer
+= bytes_read
;
12725 if (buffer
> buf_end
)
12731 case DW_RLE_start_end
:
12732 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
12737 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12739 buffer
+= bytes_read
;
12740 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12741 buffer
+= bytes_read
;
12743 case DW_RLE_startx_endx
:
12744 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12745 buffer
+= bytes_read
;
12746 range_beginning
= read_addr_index (cu
, addr_index
);
12747 if (buffer
> buf_end
)
12752 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12753 buffer
+= bytes_read
;
12754 range_end
= read_addr_index (cu
, addr_index
);
12757 complaint (_("Invalid .debug_rnglists data (no base address)"));
12760 if (rlet
== DW_RLE_end_of_list
|| overflow
)
12762 if (rlet
== DW_RLE_base_address
)
12765 if (range_beginning
> range_end
)
12767 /* Inverted range entries are invalid. */
12768 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12772 /* Empty range entries have no effect. */
12773 if (range_beginning
== range_end
)
12776 /* Only DW_RLE_offset_pair needs the base address added. */
12777 if (rlet
== DW_RLE_offset_pair
)
12779 if (!base
.has_value ())
12781 /* We have no valid base address for the DW_RLE_offset_pair. */
12782 complaint (_("Invalid .debug_rnglists data (no base address for "
12783 "DW_RLE_offset_pair)"));
12787 range_beginning
+= *base
;
12788 range_end
+= *base
;
12791 /* A not-uncommon case of bad debug info.
12792 Don't pollute the addrmap with bad data. */
12793 if (range_beginning
== 0
12794 && !per_objfile
->per_bfd
->has_section_at_zero
)
12796 complaint (_(".debug_rnglists entry has start address of zero"
12797 " [in module %s]"), objfile_name (objfile
));
12801 callback (range_beginning
, range_end
);
12806 complaint (_("Offset %d is not terminated "
12807 "for DW_AT_ranges attribute"),
12815 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12816 Callback's type should be:
12817 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12818 Return 1 if the attributes are present and valid, otherwise, return 0. */
12820 template <typename Callback
>
12822 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
12823 Callback
&&callback
)
12825 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12826 struct objfile
*objfile
= per_objfile
->objfile
;
12827 struct comp_unit_head
*cu_header
= &cu
->header
;
12828 bfd
*obfd
= objfile
->obfd
;
12829 unsigned int addr_size
= cu_header
->addr_size
;
12830 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
12831 /* Base address selection entry. */
12832 gdb::optional
<CORE_ADDR
> base
;
12833 unsigned int dummy
;
12834 const gdb_byte
*buffer
;
12836 if (cu_header
->version
>= 5)
12837 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
12839 base
= cu
->base_address
;
12841 per_objfile
->per_bfd
->ranges
.read (objfile
);
12842 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
12844 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12848 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
12852 CORE_ADDR range_beginning
, range_end
;
12854 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12855 buffer
+= addr_size
;
12856 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12857 buffer
+= addr_size
;
12858 offset
+= 2 * addr_size
;
12860 /* An end of list marker is a pair of zero addresses. */
12861 if (range_beginning
== 0 && range_end
== 0)
12862 /* Found the end of list entry. */
12865 /* Each base address selection entry is a pair of 2 values.
12866 The first is the largest possible address, the second is
12867 the base address. Check for a base address here. */
12868 if ((range_beginning
& mask
) == mask
)
12870 /* If we found the largest possible address, then we already
12871 have the base address in range_end. */
12876 if (!base
.has_value ())
12878 /* We have no valid base address for the ranges
12880 complaint (_("Invalid .debug_ranges data (no base address)"));
12884 if (range_beginning
> range_end
)
12886 /* Inverted range entries are invalid. */
12887 complaint (_("Invalid .debug_ranges data (inverted range)"));
12891 /* Empty range entries have no effect. */
12892 if (range_beginning
== range_end
)
12895 range_beginning
+= *base
;
12896 range_end
+= *base
;
12898 /* A not-uncommon case of bad debug info.
12899 Don't pollute the addrmap with bad data. */
12900 if (range_beginning
== 0
12901 && !per_objfile
->per_bfd
->has_section_at_zero
)
12903 complaint (_(".debug_ranges entry has start address of zero"
12904 " [in module %s]"), objfile_name (objfile
));
12908 callback (range_beginning
, range_end
);
12914 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12915 Return 1 if the attributes are present and valid, otherwise, return 0.
12916 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12917 ranges in MAP are set, using DATUM as the value. */
12920 dwarf2_ranges_read (unsigned offset
, CORE_ADDR
*low_return
,
12921 CORE_ADDR
*high_return
, struct dwarf2_cu
*cu
,
12922 addrmap
*map
, void *datum
, dwarf_tag tag
)
12924 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12925 struct gdbarch
*gdbarch
= objfile
->arch ();
12926 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
12929 CORE_ADDR high
= 0;
12932 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
12933 [&] (CORE_ADDR range_beginning
, CORE_ADDR range_end
)
12935 if (map
!= nullptr)
12940 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12941 range_beginning
+ baseaddr
)
12943 highpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12944 range_end
+ baseaddr
)
12946 map
->set_empty (lowpc
, highpc
- 1, datum
);
12949 /* FIXME: This is recording everything as a low-high
12950 segment of consecutive addresses. We should have a
12951 data structure for discontiguous block ranges
12955 low
= range_beginning
;
12961 if (range_beginning
< low
)
12962 low
= range_beginning
;
12963 if (range_end
> high
)
12971 /* If the first entry is an end-of-list marker, the range
12972 describes an empty scope, i.e. no instructions. */
12978 *high_return
= high
;
12982 /* Process ranges and fill in a vector of the low PC values only. */
12985 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
12987 std::vector
<CORE_ADDR
> &result
)
12989 dwarf2_ranges_process (offset
, cu
, tag
,
12990 [&] (CORE_ADDR start
, CORE_ADDR end
)
12992 result
.push_back (start
);
12996 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12997 definition for the return value. *LOWPC and *HIGHPC are set iff
12998 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13000 static enum pc_bounds_kind
13001 dwarf2_get_pc_bounds (struct die_info
*die
, CORE_ADDR
*lowpc
,
13002 CORE_ADDR
*highpc
, struct dwarf2_cu
*cu
,
13003 addrmap
*map
, void *datum
)
13005 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13006 struct attribute
*attr
;
13007 struct attribute
*attr_high
;
13009 CORE_ADDR high
= 0;
13010 enum pc_bounds_kind ret
;
13012 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13015 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13016 if (attr
!= nullptr)
13018 low
= attr
->as_address ();
13019 high
= attr_high
->as_address ();
13020 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13024 /* Found high w/o low attribute. */
13025 return PC_BOUNDS_INVALID
;
13027 /* Found consecutive range of addresses. */
13028 ret
= PC_BOUNDS_HIGH_LOW
;
13032 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13033 if (attr
!= nullptr && attr
->form_is_unsigned ())
13035 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13036 on DWARF version). */
13037 ULONGEST ranges_offset
= attr
->as_unsigned ();
13039 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13041 if (die
->tag
!= DW_TAG_compile_unit
)
13042 ranges_offset
+= cu
->gnu_ranges_base
;
13044 /* Value of the DW_AT_ranges attribute is the offset in the
13045 .debug_ranges section. */
13046 if (!dwarf2_ranges_read (ranges_offset
, &low
, &high
, cu
,
13047 map
, datum
, die
->tag
))
13048 return PC_BOUNDS_INVALID
;
13049 /* Found discontinuous range of addresses. */
13050 ret
= PC_BOUNDS_RANGES
;
13053 return PC_BOUNDS_NOT_PRESENT
;
13056 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13058 return PC_BOUNDS_INVALID
;
13060 /* When using the GNU linker, .gnu.linkonce. sections are used to
13061 eliminate duplicate copies of functions and vtables and such.
13062 The linker will arbitrarily choose one and discard the others.
13063 The AT_*_pc values for such functions refer to local labels in
13064 these sections. If the section from that file was discarded, the
13065 labels are not in the output, so the relocs get a value of 0.
13066 If this is a discarded function, mark the pc bounds as invalid,
13067 so that GDB will ignore it. */
13068 if (low
== 0 && !per_objfile
->per_bfd
->has_section_at_zero
)
13069 return PC_BOUNDS_INVALID
;
13077 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13078 its low and high PC addresses. Do nothing if these addresses could not
13079 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13080 and HIGHPC to the high address if greater than HIGHPC. */
13083 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
13084 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13085 struct dwarf2_cu
*cu
)
13087 CORE_ADDR low
, high
;
13088 struct die_info
*child
= die
->child
;
13090 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
13091 >= PC_BOUNDS_RANGES
)
13093 *lowpc
= std::min (*lowpc
, low
);
13094 *highpc
= std::max (*highpc
, high
);
13097 /* If the language does not allow nested subprograms (either inside
13098 subprograms or lexical blocks), we're done. */
13099 if (cu
->lang () != language_ada
)
13102 /* Check all the children of the given DIE. If it contains nested
13103 subprograms, then check their pc bounds. Likewise, we need to
13104 check lexical blocks as well, as they may also contain subprogram
13106 while (child
&& child
->tag
)
13108 if (child
->tag
== DW_TAG_subprogram
13109 || child
->tag
== DW_TAG_lexical_block
)
13110 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
13111 child
= child
->sibling
;
13115 /* Get the low and high pc's represented by the scope DIE, and store
13116 them in *LOWPC and *HIGHPC. If the correct values can't be
13117 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13120 get_scope_pc_bounds (struct die_info
*die
,
13121 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13122 struct dwarf2_cu
*cu
)
13124 CORE_ADDR best_low
= (CORE_ADDR
) -1;
13125 CORE_ADDR best_high
= (CORE_ADDR
) 0;
13126 CORE_ADDR current_low
, current_high
;
13128 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
13130 >= PC_BOUNDS_RANGES
)
13132 best_low
= current_low
;
13133 best_high
= current_high
;
13137 struct die_info
*child
= die
->child
;
13139 while (child
&& child
->tag
)
13141 switch (child
->tag
) {
13142 case DW_TAG_subprogram
:
13143 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
13145 case DW_TAG_namespace
:
13146 case DW_TAG_module
:
13147 /* FIXME: carlton/2004-01-16: Should we do this for
13148 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13149 that current GCC's always emit the DIEs corresponding
13150 to definitions of methods of classes as children of a
13151 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13152 the DIEs giving the declarations, which could be
13153 anywhere). But I don't see any reason why the
13154 standards says that they have to be there. */
13155 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
13157 if (current_low
!= ((CORE_ADDR
) -1))
13159 best_low
= std::min (best_low
, current_low
);
13160 best_high
= std::max (best_high
, current_high
);
13168 child
= child
->sibling
;
13173 *highpc
= best_high
;
13176 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13180 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
13181 CORE_ADDR baseaddr
, struct dwarf2_cu
*cu
)
13183 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13184 struct gdbarch
*gdbarch
= objfile
->arch ();
13185 struct attribute
*attr
;
13186 struct attribute
*attr_high
;
13188 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13191 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13192 if (attr
!= nullptr)
13194 CORE_ADDR low
= attr
->as_address ();
13195 CORE_ADDR high
= attr_high
->as_address ();
13197 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13200 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
+ baseaddr
);
13201 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
+ baseaddr
);
13202 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
13206 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13207 if (attr
!= nullptr && attr
->form_is_unsigned ())
13209 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13210 on DWARF version). */
13211 ULONGEST ranges_offset
= attr
->as_unsigned ();
13213 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13215 if (die
->tag
!= DW_TAG_compile_unit
)
13216 ranges_offset
+= cu
->gnu_ranges_base
;
13218 std::vector
<blockrange
> blockvec
;
13219 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
13220 [&] (CORE_ADDR start
, CORE_ADDR end
)
13224 start
= gdbarch_adjust_dwarf2_addr (gdbarch
, start
);
13225 end
= gdbarch_adjust_dwarf2_addr (gdbarch
, end
);
13226 cu
->get_builder ()->record_block_range (block
, start
, end
- 1);
13227 blockvec
.emplace_back (start
, end
);
13230 block
->set_ranges (make_blockranges (objfile
, blockvec
));
13234 /* Check whether the producer field indicates either of GCC < 4.6, or the
13235 Intel C/C++ compiler, and cache the result in CU. */
13238 check_producer (struct dwarf2_cu
*cu
)
13242 if (cu
->producer
== NULL
)
13244 /* For unknown compilers expect their behavior is DWARF version
13247 GCC started to support .debug_types sections by -gdwarf-4 since
13248 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13249 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13250 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13251 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13253 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
13255 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
13256 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
13257 cu
->producer_is_gcc_11
= major
== 11;
13259 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
13261 cu
->producer_is_icc
= true;
13262 cu
->producer_is_icc_lt_14
= major
< 14;
13264 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
13265 cu
->producer_is_codewarrior
= true;
13268 /* For other non-GCC compilers, expect their behavior is DWARF version
13272 cu
->checked_producer
= true;
13275 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13276 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13277 during 4.6.0 experimental. */
13280 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
13282 if (!cu
->checked_producer
)
13283 check_producer (cu
);
13285 return cu
->producer_is_gxx_lt_4_6
;
13289 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13290 with incorrect is_stmt attributes. */
13293 producer_is_codewarrior (struct dwarf2_cu
*cu
)
13295 if (!cu
->checked_producer
)
13296 check_producer (cu
);
13298 return cu
->producer_is_codewarrior
;
13301 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13302 If that attribute is not available, return the appropriate
13305 static enum dwarf_access_attribute
13306 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
13308 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
13309 if (attr
!= nullptr)
13311 LONGEST value
= attr
->constant_value (-1);
13312 if (value
== DW_ACCESS_public
13313 || value
== DW_ACCESS_protected
13314 || value
== DW_ACCESS_private
)
13315 return (dwarf_access_attribute
) value
;
13316 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13320 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
13322 /* The default DWARF 2 accessibility for members is public, the default
13323 accessibility for inheritance is private. */
13325 if (die
->tag
!= DW_TAG_inheritance
)
13326 return DW_ACCESS_public
;
13328 return DW_ACCESS_private
;
13332 /* DWARF 3+ defines the default accessibility a different way. The same
13333 rules apply now for DW_TAG_inheritance as for the members and it only
13334 depends on the container kind. */
13336 if (die
->parent
->tag
== DW_TAG_class_type
)
13337 return DW_ACCESS_private
;
13339 return DW_ACCESS_public
;
13343 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13344 *OFFSET to the byte offset. If the attribute was not found return
13345 0, otherwise return 1. If it was found but could not properly be
13346 handled, set *OFFSET to 0. */
13349 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13352 struct attribute
*attr
;
13354 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13359 /* Note that we do not check for a section offset first here.
13360 This is because DW_AT_data_member_location is new in DWARF 4,
13361 so if we see it, we can assume that a constant form is really
13362 a constant and not a section offset. */
13363 if (attr
->form_is_constant ())
13364 *offset
= attr
->constant_value (0);
13365 else if (attr
->form_is_section_offset ())
13366 dwarf2_complex_location_expr_complaint ();
13367 else if (attr
->form_is_block ())
13368 *offset
= decode_locdesc (attr
->as_block (), cu
);
13370 dwarf2_complex_location_expr_complaint ();
13376 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13377 if (attr
!= nullptr)
13379 *offset
= attr
->constant_value (0);
13387 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13388 store the results in FIELD. */
13391 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13392 struct field
*field
)
13394 struct attribute
*attr
;
13396 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13399 if (attr
->form_is_constant ())
13401 LONGEST offset
= attr
->constant_value (0);
13403 /* Work around this GCC 11 bug, where it would erroneously use -1
13404 data member locations, instead of 0:
13406 Negative DW_AT_data_member_location
13407 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13409 if (offset
== -1 && cu
->producer_is_gcc_11
)
13411 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13415 field
->set_loc_bitpos (offset
* bits_per_byte
);
13417 else if (attr
->form_is_section_offset ())
13418 dwarf2_complex_location_expr_complaint ();
13419 else if (attr
->form_is_block ())
13422 CORE_ADDR offset
= decode_locdesc (attr
->as_block (), cu
, &handled
);
13424 field
->set_loc_bitpos (offset
* bits_per_byte
);
13427 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13428 struct objfile
*objfile
= per_objfile
->objfile
;
13429 struct dwarf2_locexpr_baton
*dlbaton
13430 = XOBNEW (&objfile
->objfile_obstack
,
13431 struct dwarf2_locexpr_baton
);
13432 dlbaton
->data
= attr
->as_block ()->data
;
13433 dlbaton
->size
= attr
->as_block ()->size
;
13434 /* When using this baton, we want to compute the address
13435 of the field, not the value. This is why
13436 is_reference is set to false here. */
13437 dlbaton
->is_reference
= false;
13438 dlbaton
->per_objfile
= per_objfile
;
13439 dlbaton
->per_cu
= cu
->per_cu
;
13441 field
->set_loc_dwarf_block (dlbaton
);
13445 dwarf2_complex_location_expr_complaint ();
13449 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13450 if (attr
!= nullptr)
13451 field
->set_loc_bitpos (attr
->constant_value (0));
13455 /* Add an aggregate field to the field list. */
13458 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
13459 struct dwarf2_cu
*cu
)
13461 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13462 struct gdbarch
*gdbarch
= objfile
->arch ();
13463 struct nextfield
*new_field
;
13464 struct attribute
*attr
;
13466 const char *fieldname
= "";
13468 if (die
->tag
== DW_TAG_inheritance
)
13470 fip
->baseclasses
.emplace_back ();
13471 new_field
= &fip
->baseclasses
.back ();
13475 fip
->fields
.emplace_back ();
13476 new_field
= &fip
->fields
.back ();
13479 new_field
->offset
= die
->sect_off
;
13481 new_field
->accessibility
= dwarf2_access_attribute (die
, cu
);
13482 if (new_field
->accessibility
!= DW_ACCESS_public
)
13483 fip
->non_public_fields
= true;
13485 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
13486 if (attr
!= nullptr)
13487 new_field
->virtuality
= attr
->as_virtuality ();
13489 new_field
->virtuality
= DW_VIRTUALITY_none
;
13491 fp
= &new_field
->field
;
13493 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
13494 && !die_is_declaration (die
, cu
))
13496 if (die
->tag
== DW_TAG_namelist_item
)
13498 /* Typically, DW_TAG_namelist_item are references to namelist items.
13499 If so, follow that reference. */
13500 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
13501 struct die_info
*item_die
= nullptr;
13502 struct dwarf2_cu
*item_cu
= cu
;
13503 if (attr1
->form_is_ref ())
13504 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
13505 if (item_die
!= nullptr)
13508 /* Data member other than a C++ static data member. */
13510 /* Get type of field. */
13511 fp
->set_type (die_type (die
, cu
));
13513 fp
->set_loc_bitpos (0);
13515 /* Get bit size of field (zero if none). */
13516 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
13517 if (attr
!= nullptr)
13519 FIELD_BITSIZE (*fp
) = attr
->constant_value (0);
13523 FIELD_BITSIZE (*fp
) = 0;
13526 /* Get bit offset of field. */
13527 handle_member_location (die
, cu
, fp
);
13528 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
13529 if (attr
!= nullptr && attr
->form_is_constant ())
13531 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
13533 /* For big endian bits, the DW_AT_bit_offset gives the
13534 additional bit offset from the MSB of the containing
13535 anonymous object to the MSB of the field. We don't
13536 have to do anything special since we don't need to
13537 know the size of the anonymous object. */
13538 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
13542 /* For little endian bits, compute the bit offset to the
13543 MSB of the anonymous object, subtract off the number of
13544 bits from the MSB of the field to the MSB of the
13545 object, and then subtract off the number of bits of
13546 the field itself. The result is the bit offset of
13547 the LSB of the field. */
13548 int anonymous_size
;
13549 int bit_offset
= attr
->constant_value (0);
13551 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13552 if (attr
!= nullptr && attr
->form_is_constant ())
13554 /* The size of the anonymous object containing
13555 the bit field is explicit, so use the
13556 indicated size (in bytes). */
13557 anonymous_size
= attr
->constant_value (0);
13561 /* The size of the anonymous object containing
13562 the bit field must be inferred from the type
13563 attribute of the data member containing the
13565 anonymous_size
= TYPE_LENGTH (fp
->type ());
13567 fp
->set_loc_bitpos (fp
->loc_bitpos ()
13568 + anonymous_size
* bits_per_byte
13569 - bit_offset
- FIELD_BITSIZE (*fp
));
13573 /* Get name of field. */
13574 fieldname
= dwarf2_name (die
, cu
);
13575 if (fieldname
== NULL
)
13578 /* The name is already allocated along with this objfile, so we don't
13579 need to duplicate it for the type. */
13580 fp
->set_name (fieldname
);
13582 /* Change accessibility for artificial fields (e.g. virtual table
13583 pointer or virtual base class pointer) to private. */
13584 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
13586 FIELD_ARTIFICIAL (*fp
) = 1;
13587 new_field
->accessibility
= DW_ACCESS_private
;
13588 fip
->non_public_fields
= true;
13591 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
13593 /* C++ static member. */
13595 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13596 is a declaration, but all versions of G++ as of this writing
13597 (so through at least 3.2.1) incorrectly generate
13598 DW_TAG_variable tags. */
13600 const char *physname
;
13602 /* Get name of field. */
13603 fieldname
= dwarf2_name (die
, cu
);
13604 if (fieldname
== NULL
)
13607 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
13609 /* Only create a symbol if this is an external value.
13610 new_symbol checks this and puts the value in the global symbol
13611 table, which we want. If it is not external, new_symbol
13612 will try to put the value in cu->list_in_scope which is wrong. */
13613 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
13615 /* A static const member, not much different than an enum as far as
13616 we're concerned, except that we can support more types. */
13617 new_symbol (die
, NULL
, cu
);
13620 /* Get physical name. */
13621 physname
= dwarf2_physname (fieldname
, die
, cu
);
13623 /* The name is already allocated along with this objfile, so we don't
13624 need to duplicate it for the type. */
13625 fp
->set_loc_physname (physname
? physname
: "");
13626 fp
->set_type (die_type (die
, cu
));
13627 fp
->set_name (fieldname
);
13629 else if (die
->tag
== DW_TAG_inheritance
)
13631 /* C++ base class field. */
13632 handle_member_location (die
, cu
, fp
);
13633 FIELD_BITSIZE (*fp
) = 0;
13634 fp
->set_type (die_type (die
, cu
));
13635 fp
->set_name (fp
->type ()->name ());
13638 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13641 /* Can the type given by DIE define another type? */
13644 type_can_define_types (const struct die_info
*die
)
13648 case DW_TAG_typedef
:
13649 case DW_TAG_class_type
:
13650 case DW_TAG_structure_type
:
13651 case DW_TAG_union_type
:
13652 case DW_TAG_enumeration_type
:
13660 /* Add a type definition defined in the scope of the FIP's class. */
13663 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
13664 struct dwarf2_cu
*cu
)
13666 struct decl_field fp
;
13667 memset (&fp
, 0, sizeof (fp
));
13669 gdb_assert (type_can_define_types (die
));
13671 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13672 fp
.name
= dwarf2_name (die
, cu
);
13673 fp
.type
= read_type_die (die
, cu
);
13675 /* Save accessibility. */
13676 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
13677 switch (accessibility
)
13679 case DW_ACCESS_public
:
13680 /* The assumed value if neither private nor protected. */
13682 case DW_ACCESS_private
:
13685 case DW_ACCESS_protected
:
13686 fp
.is_protected
= 1;
13690 if (die
->tag
== DW_TAG_typedef
)
13691 fip
->typedef_field_list
.push_back (fp
);
13693 fip
->nested_types_list
.push_back (fp
);
13696 /* A convenience typedef that's used when finding the discriminant
13697 field for a variant part. */
13698 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
13701 /* Compute the discriminant range for a given variant. OBSTACK is
13702 where the results will be stored. VARIANT is the variant to
13703 process. IS_UNSIGNED indicates whether the discriminant is signed
13706 static const gdb::array_view
<discriminant_range
>
13707 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
13710 std::vector
<discriminant_range
> ranges
;
13712 if (variant
.default_branch
)
13715 if (variant
.discr_list_data
== nullptr)
13717 discriminant_range r
13718 = {variant
.discriminant_value
, variant
.discriminant_value
};
13719 ranges
.push_back (r
);
13723 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
13724 variant
.discr_list_data
->size
);
13725 while (!data
.empty ())
13727 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
13729 complaint (_("invalid discriminant marker: %d"), data
[0]);
13732 bool is_range
= data
[0] == DW_DSC_range
;
13733 data
= data
.slice (1);
13735 ULONGEST low
, high
;
13736 unsigned int bytes_read
;
13740 complaint (_("DW_AT_discr_list missing low value"));
13744 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
13746 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
13748 data
= data
.slice (bytes_read
);
13754 complaint (_("DW_AT_discr_list missing high value"));
13758 high
= read_unsigned_leb128 (nullptr, data
.data (),
13761 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
13763 data
= data
.slice (bytes_read
);
13768 ranges
.push_back ({ low
, high
});
13772 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
13774 std::copy (ranges
.begin (), ranges
.end (), result
);
13775 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
13778 static const gdb::array_view
<variant_part
> create_variant_parts
13779 (struct obstack
*obstack
,
13780 const offset_map_type
&offset_map
,
13781 struct field_info
*fi
,
13782 const std::vector
<variant_part_builder
> &variant_parts
);
13784 /* Fill in a "struct variant" for a given variant field. RESULT is
13785 the variant to fill in. OBSTACK is where any needed allocations
13786 will be done. OFFSET_MAP holds the mapping from section offsets to
13787 fields for the type. FI describes the fields of the type we're
13788 processing. FIELD is the variant field we're converting. */
13791 create_one_variant (variant
&result
, struct obstack
*obstack
,
13792 const offset_map_type
&offset_map
,
13793 struct field_info
*fi
, const variant_field
&field
)
13795 result
.discriminants
= convert_variant_range (obstack
, field
, false);
13796 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
13797 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
13798 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
13799 field
.variant_parts
);
13802 /* Fill in a "struct variant_part" for a given variant part. RESULT
13803 is the variant part to fill in. OBSTACK is where any needed
13804 allocations will be done. OFFSET_MAP holds the mapping from
13805 section offsets to fields for the type. FI describes the fields of
13806 the type we're processing. BUILDER is the variant part to be
13810 create_one_variant_part (variant_part
&result
,
13811 struct obstack
*obstack
,
13812 const offset_map_type
&offset_map
,
13813 struct field_info
*fi
,
13814 const variant_part_builder
&builder
)
13816 auto iter
= offset_map
.find (builder
.discriminant_offset
);
13817 if (iter
== offset_map
.end ())
13819 result
.discriminant_index
= -1;
13820 /* Doesn't matter. */
13821 result
.is_unsigned
= false;
13825 result
.discriminant_index
= iter
->second
;
13827 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
13830 size_t n
= builder
.variants
.size ();
13831 variant
*output
= new (obstack
) variant
[n
];
13832 for (size_t i
= 0; i
< n
; ++i
)
13833 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
13834 builder
.variants
[i
]);
13836 result
.variants
= gdb::array_view
<variant
> (output
, n
);
13839 /* Create a vector of variant parts that can be attached to a type.
13840 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13841 holds the mapping from section offsets to fields for the type. FI
13842 describes the fields of the type we're processing. VARIANT_PARTS
13843 is the vector to convert. */
13845 static const gdb::array_view
<variant_part
>
13846 create_variant_parts (struct obstack
*obstack
,
13847 const offset_map_type
&offset_map
,
13848 struct field_info
*fi
,
13849 const std::vector
<variant_part_builder
> &variant_parts
)
13851 if (variant_parts
.empty ())
13854 size_t n
= variant_parts
.size ();
13855 variant_part
*result
= new (obstack
) variant_part
[n
];
13856 for (size_t i
= 0; i
< n
; ++i
)
13857 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
13860 return gdb::array_view
<variant_part
> (result
, n
);
13863 /* Compute the variant part vector for FIP, attaching it to TYPE when
13867 add_variant_property (struct field_info
*fip
, struct type
*type
,
13868 struct dwarf2_cu
*cu
)
13870 /* Map section offsets of fields to their field index. Note the
13871 field index here does not take the number of baseclasses into
13873 offset_map_type offset_map
;
13874 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
13875 offset_map
[fip
->fields
[i
].offset
] = i
;
13877 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13878 gdb::array_view
<const variant_part
> parts
13879 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
13880 fip
->variant_parts
);
13882 struct dynamic_prop prop
;
13883 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
13884 obstack_copy (&objfile
->objfile_obstack
, &parts
,
13887 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
13890 /* Create the vector of fields, and attach it to the type. */
13893 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
13894 struct dwarf2_cu
*cu
)
13896 int nfields
= fip
->nfields ();
13898 /* Record the field count, allocate space for the array of fields,
13899 and create blank accessibility bitfields if necessary. */
13900 type
->set_num_fields (nfields
);
13902 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
13904 if (fip
->non_public_fields
&& cu
->lang () != language_ada
)
13906 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13908 TYPE_FIELD_PRIVATE_BITS (type
) =
13909 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13910 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type
), nfields
);
13912 TYPE_FIELD_PROTECTED_BITS (type
) =
13913 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13914 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type
), nfields
);
13916 TYPE_FIELD_IGNORE_BITS (type
) =
13917 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13918 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type
), nfields
);
13921 /* If the type has baseclasses, allocate and clear a bit vector for
13922 TYPE_FIELD_VIRTUAL_BITS. */
13923 if (!fip
->baseclasses
.empty () && cu
->lang () != language_ada
)
13925 int num_bytes
= B_BYTES (fip
->baseclasses
.size ());
13926 unsigned char *pointer
;
13928 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13929 pointer
= (unsigned char *) TYPE_ALLOC (type
, num_bytes
);
13930 TYPE_FIELD_VIRTUAL_BITS (type
) = pointer
;
13931 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type
), fip
->baseclasses
.size ());
13932 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
13935 if (!fip
->variant_parts
.empty ())
13936 add_variant_property (fip
, type
, cu
);
13938 /* Copy the saved-up fields into the field vector. */
13939 for (int i
= 0; i
< nfields
; ++i
)
13941 struct nextfield
&field
13942 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
13943 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
13945 type
->field (i
) = field
.field
;
13946 switch (field
.accessibility
)
13948 case DW_ACCESS_private
:
13949 if (cu
->lang () != language_ada
)
13950 SET_TYPE_FIELD_PRIVATE (type
, i
);
13953 case DW_ACCESS_protected
:
13954 if (cu
->lang () != language_ada
)
13955 SET_TYPE_FIELD_PROTECTED (type
, i
);
13958 case DW_ACCESS_public
:
13962 /* Unknown accessibility. Complain and treat it as public. */
13964 complaint (_("unsupported accessibility %d"),
13965 field
.accessibility
);
13969 if (i
< fip
->baseclasses
.size ())
13971 switch (field
.virtuality
)
13973 case DW_VIRTUALITY_virtual
:
13974 case DW_VIRTUALITY_pure_virtual
:
13975 if (cu
->lang () == language_ada
)
13976 error (_("unexpected virtuality in component of Ada type"));
13977 SET_TYPE_FIELD_VIRTUAL (type
, i
);
13984 /* Return true if this member function is a constructor, false
13988 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
13990 const char *fieldname
;
13991 const char *type_name
;
13994 if (die
->parent
== NULL
)
13997 if (die
->parent
->tag
!= DW_TAG_structure_type
13998 && die
->parent
->tag
!= DW_TAG_union_type
13999 && die
->parent
->tag
!= DW_TAG_class_type
)
14002 fieldname
= dwarf2_name (die
, cu
);
14003 type_name
= dwarf2_name (die
->parent
, cu
);
14004 if (fieldname
== NULL
|| type_name
== NULL
)
14007 len
= strlen (fieldname
);
14008 return (strncmp (fieldname
, type_name
, len
) == 0
14009 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
14012 /* Add a member function to the proper fieldlist. */
14015 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
14016 struct type
*type
, struct dwarf2_cu
*cu
)
14018 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14019 struct attribute
*attr
;
14021 struct fnfieldlist
*flp
= nullptr;
14022 struct fn_field
*fnp
;
14023 const char *fieldname
;
14024 struct type
*this_type
;
14026 if (cu
->lang () == language_ada
)
14027 error (_("unexpected member function in Ada type"));
14029 /* Get name of member function. */
14030 fieldname
= dwarf2_name (die
, cu
);
14031 if (fieldname
== NULL
)
14034 /* Look up member function name in fieldlist. */
14035 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14037 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
14039 flp
= &fip
->fnfieldlists
[i
];
14044 /* Create a new fnfieldlist if necessary. */
14045 if (flp
== nullptr)
14047 fip
->fnfieldlists
.emplace_back ();
14048 flp
= &fip
->fnfieldlists
.back ();
14049 flp
->name
= fieldname
;
14050 i
= fip
->fnfieldlists
.size () - 1;
14053 /* Create a new member function field and add it to the vector of
14055 flp
->fnfields
.emplace_back ();
14056 fnp
= &flp
->fnfields
.back ();
14058 /* Delay processing of the physname until later. */
14059 if (cu
->lang () == language_cplus
)
14060 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
14064 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
14065 fnp
->physname
= physname
? physname
: "";
14068 fnp
->type
= alloc_type (objfile
);
14069 this_type
= read_type_die (die
, cu
);
14070 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
14072 int nparams
= this_type
->num_fields ();
14074 /* TYPE is the domain of this method, and THIS_TYPE is the type
14075 of the method itself (TYPE_CODE_METHOD). */
14076 smash_to_method_type (fnp
->type
, type
,
14077 TYPE_TARGET_TYPE (this_type
),
14078 this_type
->fields (),
14079 this_type
->num_fields (),
14080 this_type
->has_varargs ());
14082 /* Handle static member functions.
14083 Dwarf2 has no clean way to discern C++ static and non-static
14084 member functions. G++ helps GDB by marking the first
14085 parameter for non-static member functions (which is the this
14086 pointer) as artificial. We obtain this information from
14087 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14088 if (nparams
== 0 || TYPE_FIELD_ARTIFICIAL (this_type
, 0) == 0)
14089 fnp
->voffset
= VOFFSET_STATIC
;
14092 complaint (_("member function type missing for '%s'"),
14093 dwarf2_full_name (fieldname
, die
, cu
));
14095 /* Get fcontext from DW_AT_containing_type if present. */
14096 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14097 fnp
->fcontext
= die_containing_type (die
, cu
);
14099 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14100 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14102 /* Get accessibility. */
14103 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
14104 switch (accessibility
)
14106 case DW_ACCESS_private
:
14107 fnp
->is_private
= 1;
14109 case DW_ACCESS_protected
:
14110 fnp
->is_protected
= 1;
14114 /* Check for artificial methods. */
14115 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
14116 if (attr
&& attr
->as_boolean ())
14117 fnp
->is_artificial
= 1;
14119 /* Check for defaulted methods. */
14120 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
14121 if (attr
!= nullptr)
14122 fnp
->defaulted
= attr
->defaulted ();
14124 /* Check for deleted methods. */
14125 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
14126 if (attr
!= nullptr && attr
->as_boolean ())
14127 fnp
->is_deleted
= 1;
14129 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
14131 /* Get index in virtual function table if it is a virtual member
14132 function. For older versions of GCC, this is an offset in the
14133 appropriate virtual table, as specified by DW_AT_containing_type.
14134 For everyone else, it is an expression to be evaluated relative
14135 to the object address. */
14137 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
14138 if (attr
!= nullptr)
14140 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
14142 struct dwarf_block
*block
= attr
->as_block ();
14144 if (block
->data
[0] == DW_OP_constu
)
14146 /* Old-style GCC. */
14147 fnp
->voffset
= decode_locdesc (block
, cu
) + 2;
14149 else if (block
->data
[0] == DW_OP_deref
14150 || (block
->size
> 1
14151 && block
->data
[0] == DW_OP_deref_size
14152 && block
->data
[1] == cu
->header
.addr_size
))
14154 fnp
->voffset
= decode_locdesc (block
, cu
);
14155 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
14156 dwarf2_complex_location_expr_complaint ();
14158 fnp
->voffset
/= cu
->header
.addr_size
;
14162 dwarf2_complex_location_expr_complaint ();
14164 if (!fnp
->fcontext
)
14166 /* If there is no `this' field and no DW_AT_containing_type,
14167 we cannot actually find a base class context for the
14169 if (this_type
->num_fields () == 0
14170 || !TYPE_FIELD_ARTIFICIAL (this_type
, 0))
14172 complaint (_("cannot determine context for virtual member "
14173 "function \"%s\" (offset %s)"),
14174 fieldname
, sect_offset_str (die
->sect_off
));
14179 = TYPE_TARGET_TYPE (this_type
->field (0).type ());
14183 else if (attr
->form_is_section_offset ())
14185 dwarf2_complex_location_expr_complaint ();
14189 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14195 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
14196 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
14198 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14199 complaint (_("Member function \"%s\" (offset %s) is virtual "
14200 "but the vtable offset is not specified"),
14201 fieldname
, sect_offset_str (die
->sect_off
));
14202 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14203 TYPE_CPLUS_DYNAMIC (type
) = 1;
14208 /* Create the vector of member function fields, and attach it to the type. */
14211 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
14212 struct dwarf2_cu
*cu
)
14214 if (cu
->lang () == language_ada
)
14215 error (_("unexpected member functions in Ada type"));
14217 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14218 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
14220 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
14222 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14224 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
14225 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
14227 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
14228 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
14229 fn_flp
->fn_fields
= (struct fn_field
*)
14230 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
14232 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
14233 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
14236 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
14239 /* Returns non-zero if NAME is the name of a vtable member in CU's
14240 language, zero otherwise. */
14242 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
14244 static const char vptr
[] = "_vptr";
14246 /* Look for the C++ form of the vtable. */
14247 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
14253 /* GCC outputs unnamed structures that are really pointers to member
14254 functions, with the ABI-specified layout. If TYPE describes
14255 such a structure, smash it into a member function type.
14257 GCC shouldn't do this; it should just output pointer to member DIEs.
14258 This is GCC PR debug/28767. */
14261 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
14263 struct type
*pfn_type
, *self_type
, *new_type
;
14265 /* Check for a structure with no name and two children. */
14266 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14269 /* Check for __pfn and __delta members. */
14270 if (type
->field (0).name () == NULL
14271 || strcmp (type
->field (0).name (), "__pfn") != 0
14272 || type
->field (1).name () == NULL
14273 || strcmp (type
->field (1).name (), "__delta") != 0)
14276 /* Find the type of the method. */
14277 pfn_type
= type
->field (0).type ();
14278 if (pfn_type
== NULL
14279 || pfn_type
->code () != TYPE_CODE_PTR
14280 || TYPE_TARGET_TYPE (pfn_type
)->code () != TYPE_CODE_FUNC
)
14283 /* Look for the "this" argument. */
14284 pfn_type
= TYPE_TARGET_TYPE (pfn_type
);
14285 if (pfn_type
->num_fields () == 0
14286 /* || pfn_type->field (0).type () == NULL */
14287 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14290 self_type
= TYPE_TARGET_TYPE (pfn_type
->field (0).type ());
14291 new_type
= alloc_type (objfile
);
14292 smash_to_method_type (new_type
, self_type
, TYPE_TARGET_TYPE (pfn_type
),
14293 pfn_type
->fields (), pfn_type
->num_fields (),
14294 pfn_type
->has_varargs ());
14295 smash_to_methodptr_type (type
, new_type
);
14298 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14299 requires rewriting, then copy it and return the updated copy.
14300 Otherwise return nullptr. */
14302 static struct type
*
14303 rewrite_array_type (struct type
*type
)
14305 if (type
->code () != TYPE_CODE_ARRAY
)
14308 struct type
*index_type
= type
->index_type ();
14309 range_bounds
*current_bounds
= index_type
->bounds ();
14311 /* Handle multi-dimensional arrays. */
14312 struct type
*new_target
= rewrite_array_type (TYPE_TARGET_TYPE (type
));
14313 if (new_target
== nullptr)
14315 /* Maybe we don't need to rewrite this array. */
14316 if (current_bounds
->low
.kind () == PROP_CONST
14317 && current_bounds
->high
.kind () == PROP_CONST
)
14321 /* Either the target type was rewritten, or the bounds have to be
14322 updated. Either way we want to copy the type and update
14324 struct type
*copy
= copy_type (type
);
14325 int nfields
= copy
->num_fields ();
14327 = ((struct field
*) TYPE_ZALLOC (copy
,
14328 nfields
* sizeof (struct field
)));
14329 memcpy (new_fields
, copy
->fields (), nfields
* sizeof (struct field
));
14330 copy
->set_fields (new_fields
);
14331 if (new_target
!= nullptr)
14332 TYPE_TARGET_TYPE (copy
) = new_target
;
14334 struct type
*index_copy
= copy_type (index_type
);
14335 range_bounds
*bounds
14336 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
14337 sizeof (range_bounds
));
14338 *bounds
= *current_bounds
;
14339 bounds
->low
.set_const_val (1);
14340 bounds
->high
.set_const_val (0);
14341 index_copy
->set_bounds (bounds
);
14342 copy
->set_index_type (index_copy
);
14347 /* While some versions of GCC will generate complicated DWARF for an
14348 array (see quirk_ada_thick_pointer), more recent versions were
14349 modified to emit an explicit thick pointer structure. However, in
14350 this case, the array still has DWARF expressions for its ranges,
14351 and these must be ignored. */
14354 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
14357 gdb_assert (cu
->lang () == language_ada
);
14359 /* Check for a structure with two children. */
14360 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14363 /* Check for P_ARRAY and P_BOUNDS members. */
14364 if (type
->field (0).name () == NULL
14365 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
14366 || type
->field (1).name () == NULL
14367 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
14370 /* Make sure we're looking at a pointer to an array. */
14371 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14374 /* The Ada code already knows how to handle these types, so all that
14375 we need to do is turn the bounds into static bounds. However, we
14376 don't want to rewrite existing array or index types in-place,
14377 because those may be referenced in other contexts where this
14378 rewriting is undesirable. */
14379 struct type
*new_ary_type
14380 = rewrite_array_type (TYPE_TARGET_TYPE (type
->field (0).type ()));
14381 if (new_ary_type
!= nullptr)
14382 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
14385 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14386 appropriate error checking and issuing complaints if there is a
14390 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
14392 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
14394 if (attr
== nullptr)
14397 if (!attr
->form_is_constant ())
14399 complaint (_("DW_AT_alignment must have constant form"
14400 " - DIE at %s [in module %s]"),
14401 sect_offset_str (die
->sect_off
),
14402 objfile_name (cu
->per_objfile
->objfile
));
14406 LONGEST val
= attr
->constant_value (0);
14409 complaint (_("DW_AT_alignment value must not be negative"
14410 " - DIE at %s [in module %s]"),
14411 sect_offset_str (die
->sect_off
),
14412 objfile_name (cu
->per_objfile
->objfile
));
14415 ULONGEST align
= val
;
14419 complaint (_("DW_AT_alignment value must not be zero"
14420 " - DIE at %s [in module %s]"),
14421 sect_offset_str (die
->sect_off
),
14422 objfile_name (cu
->per_objfile
->objfile
));
14425 if ((align
& (align
- 1)) != 0)
14427 complaint (_("DW_AT_alignment value must be a power of 2"
14428 " - DIE at %s [in module %s]"),
14429 sect_offset_str (die
->sect_off
),
14430 objfile_name (cu
->per_objfile
->objfile
));
14437 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14438 the alignment for TYPE. */
14441 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
14444 if (!set_type_align (type
, get_alignment (cu
, die
)))
14445 complaint (_("DW_AT_alignment value too large"
14446 " - DIE at %s [in module %s]"),
14447 sect_offset_str (die
->sect_off
),
14448 objfile_name (cu
->per_objfile
->objfile
));
14451 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14452 constant for a type, according to DWARF5 spec, Table 5.5. */
14455 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
14460 case DW_CC_pass_by_reference
:
14461 case DW_CC_pass_by_value
:
14465 complaint (_("unrecognized DW_AT_calling_convention value "
14466 "(%s) for a type"), pulongest (value
));
14471 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14472 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14473 also according to GNU-specific values (see include/dwarf2.h). */
14476 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
14481 case DW_CC_program
:
14485 case DW_CC_GNU_renesas_sh
:
14486 case DW_CC_GNU_borland_fastcall_i386
:
14487 case DW_CC_GDB_IBM_OpenCL
:
14491 complaint (_("unrecognized DW_AT_calling_convention value "
14492 "(%s) for a subroutine"), pulongest (value
));
14497 /* Called when we find the DIE that starts a structure or union scope
14498 (definition) to create a type for the structure or union. Fill in
14499 the type's name and general properties; the members will not be
14500 processed until process_structure_scope. A symbol table entry for
14501 the type will also not be done until process_structure_scope (assuming
14502 the type has a name).
14504 NOTE: we need to call these functions regardless of whether or not the
14505 DIE has a DW_AT_name attribute, since it might be an anonymous
14506 structure or union. This gets the type entered into our set of
14507 user defined types. */
14509 static struct type
*
14510 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14512 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14514 struct attribute
*attr
;
14517 /* If the definition of this type lives in .debug_types, read that type.
14518 Don't follow DW_AT_specification though, that will take us back up
14519 the chain and we want to go down. */
14520 attr
= die
->attr (DW_AT_signature
);
14521 if (attr
!= nullptr)
14523 type
= get_DW_AT_signature_type (die
, attr
, cu
);
14525 /* The type's CU may not be the same as CU.
14526 Ensure TYPE is recorded with CU in die_type_hash. */
14527 return set_die_type (die
, type
, cu
);
14530 type
= alloc_type (objfile
);
14531 INIT_CPLUS_SPECIFIC (type
);
14533 name
= dwarf2_name (die
, cu
);
14536 if (cu
->lang () == language_cplus
14537 || cu
->lang () == language_d
14538 || cu
->lang () == language_rust
)
14540 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
14542 /* dwarf2_full_name might have already finished building the DIE's
14543 type. If so, there is no need to continue. */
14544 if (get_die_type (die
, cu
) != NULL
)
14545 return get_die_type (die
, cu
);
14547 type
->set_name (full_name
);
14551 /* The name is already allocated along with this objfile, so
14552 we don't need to duplicate it for the type. */
14553 type
->set_name (name
);
14557 if (die
->tag
== DW_TAG_structure_type
)
14559 type
->set_code (TYPE_CODE_STRUCT
);
14561 else if (die
->tag
== DW_TAG_union_type
)
14563 type
->set_code (TYPE_CODE_UNION
);
14565 else if (die
->tag
== DW_TAG_namelist
)
14567 type
->set_code (TYPE_CODE_NAMELIST
);
14571 type
->set_code (TYPE_CODE_STRUCT
);
14574 if (cu
->lang () == language_cplus
&& die
->tag
== DW_TAG_class_type
)
14575 type
->set_is_declared_class (true);
14577 /* Store the calling convention in the type if it's available in
14578 the die. Otherwise the calling convention remains set to
14579 the default value DW_CC_normal. */
14580 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14581 if (attr
!= nullptr
14582 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
14584 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14585 TYPE_CPLUS_CALLING_CONVENTION (type
)
14586 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
14589 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14590 if (attr
!= nullptr)
14592 if (attr
->form_is_constant ())
14593 TYPE_LENGTH (type
) = attr
->constant_value (0);
14596 struct dynamic_prop prop
;
14597 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
14598 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
14599 TYPE_LENGTH (type
) = 0;
14604 TYPE_LENGTH (type
) = 0;
14607 maybe_set_alignment (cu
, die
, type
);
14609 if (producer_is_icc_lt_14 (cu
) && (TYPE_LENGTH (type
) == 0))
14611 /* ICC<14 does not output the required DW_AT_declaration on
14612 incomplete types, but gives them a size of zero. */
14613 type
->set_is_stub (true);
14616 type
->set_stub_is_supported (true);
14618 if (die_is_declaration (die
, cu
))
14619 type
->set_is_stub (true);
14620 else if (attr
== NULL
&& die
->child
== NULL
14621 && producer_is_realview (cu
->producer
))
14622 /* RealView does not output the required DW_AT_declaration
14623 on incomplete types. */
14624 type
->set_is_stub (true);
14626 /* We need to add the type field to the die immediately so we don't
14627 infinitely recurse when dealing with pointers to the structure
14628 type within the structure itself. */
14629 set_die_type (die
, type
, cu
);
14631 /* set_die_type should be already done. */
14632 set_descriptive_type (type
, die
, cu
);
14637 static void handle_struct_member_die
14638 (struct die_info
*child_die
,
14640 struct field_info
*fi
,
14641 std::vector
<struct symbol
*> *template_args
,
14642 struct dwarf2_cu
*cu
);
14644 /* A helper for handle_struct_member_die that handles
14645 DW_TAG_variant_part. */
14648 handle_variant_part (struct die_info
*die
, struct type
*type
,
14649 struct field_info
*fi
,
14650 std::vector
<struct symbol
*> *template_args
,
14651 struct dwarf2_cu
*cu
)
14653 variant_part_builder
*new_part
;
14654 if (fi
->current_variant_part
== nullptr)
14656 fi
->variant_parts
.emplace_back ();
14657 new_part
= &fi
->variant_parts
.back ();
14659 else if (!fi
->current_variant_part
->processing_variant
)
14661 complaint (_("nested DW_TAG_variant_part seen "
14662 "- DIE at %s [in module %s]"),
14663 sect_offset_str (die
->sect_off
),
14664 objfile_name (cu
->per_objfile
->objfile
));
14669 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
14670 current
.variant_parts
.emplace_back ();
14671 new_part
= ¤t
.variant_parts
.back ();
14674 /* When we recurse, we want callees to add to this new variant
14676 scoped_restore save_current_variant_part
14677 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
14679 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
14682 /* It's a univariant form, an extension we support. */
14684 else if (discr
->form_is_ref ())
14686 struct dwarf2_cu
*target_cu
= cu
;
14687 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
14689 new_part
->discriminant_offset
= target_die
->sect_off
;
14693 complaint (_("DW_AT_discr does not have DIE reference form"
14694 " - DIE at %s [in module %s]"),
14695 sect_offset_str (die
->sect_off
),
14696 objfile_name (cu
->per_objfile
->objfile
));
14699 for (die_info
*child_die
= die
->child
;
14701 child_die
= child_die
->sibling
)
14702 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
14705 /* A helper for handle_struct_member_die that handles
14709 handle_variant (struct die_info
*die
, struct type
*type
,
14710 struct field_info
*fi
,
14711 std::vector
<struct symbol
*> *template_args
,
14712 struct dwarf2_cu
*cu
)
14714 if (fi
->current_variant_part
== nullptr)
14716 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14717 "- DIE at %s [in module %s]"),
14718 sect_offset_str (die
->sect_off
),
14719 objfile_name (cu
->per_objfile
->objfile
));
14722 if (fi
->current_variant_part
->processing_variant
)
14724 complaint (_("nested DW_TAG_variant seen "
14725 "- DIE at %s [in module %s]"),
14726 sect_offset_str (die
->sect_off
),
14727 objfile_name (cu
->per_objfile
->objfile
));
14731 scoped_restore save_processing_variant
14732 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
14735 fi
->current_variant_part
->variants
.emplace_back ();
14736 variant_field
&variant
= fi
->current_variant_part
->variants
.back ();
14737 variant
.first_field
= fi
->fields
.size ();
14739 /* In a variant we want to get the discriminant and also add a
14740 field for our sole member child. */
14741 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
14742 if (discr
== nullptr || !discr
->form_is_constant ())
14744 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
14745 if (discr
== nullptr || discr
->as_block ()->size
== 0)
14746 variant
.default_branch
= true;
14748 variant
.discr_list_data
= discr
->as_block ();
14751 variant
.discriminant_value
= discr
->constant_value (0);
14753 for (die_info
*variant_child
= die
->child
;
14754 variant_child
!= NULL
;
14755 variant_child
= variant_child
->sibling
)
14756 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
14758 variant
.last_field
= fi
->fields
.size ();
14761 /* A helper for process_structure_scope that handles a single member
14765 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
14766 struct field_info
*fi
,
14767 std::vector
<struct symbol
*> *template_args
,
14768 struct dwarf2_cu
*cu
)
14770 if (child_die
->tag
== DW_TAG_member
14771 || child_die
->tag
== DW_TAG_variable
14772 || child_die
->tag
== DW_TAG_namelist_item
)
14774 /* NOTE: carlton/2002-11-05: A C++ static data member
14775 should be a DW_TAG_member that is a declaration, but
14776 all versions of G++ as of this writing (so through at
14777 least 3.2.1) incorrectly generate DW_TAG_variable
14778 tags for them instead. */
14779 dwarf2_add_field (fi
, child_die
, cu
);
14781 else if (child_die
->tag
== DW_TAG_subprogram
)
14783 /* Rust doesn't have member functions in the C++ sense.
14784 However, it does emit ordinary functions as children
14785 of a struct DIE. */
14786 if (cu
->lang () == language_rust
)
14787 read_func_scope (child_die
, cu
);
14790 /* C++ member function. */
14791 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
14794 else if (child_die
->tag
== DW_TAG_inheritance
)
14796 /* C++ base class field. */
14797 dwarf2_add_field (fi
, child_die
, cu
);
14799 else if (type_can_define_types (child_die
))
14800 dwarf2_add_type_defn (fi
, child_die
, cu
);
14801 else if (child_die
->tag
== DW_TAG_template_type_param
14802 || child_die
->tag
== DW_TAG_template_value_param
)
14804 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
14807 template_args
->push_back (arg
);
14809 else if (child_die
->tag
== DW_TAG_variant_part
)
14810 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
14811 else if (child_die
->tag
== DW_TAG_variant
)
14812 handle_variant (child_die
, type
, fi
, template_args
, cu
);
14815 /* Finish creating a structure or union type, including filling in its
14816 members and creating a symbol for it. This function also handles Fortran
14817 namelist variables, their items or members and creating a symbol for
14821 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
14823 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14824 struct die_info
*child_die
;
14827 type
= get_die_type (die
, cu
);
14829 type
= read_structure_type (die
, cu
);
14831 bool has_template_parameters
= false;
14832 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
14834 struct field_info fi
;
14835 std::vector
<struct symbol
*> template_args
;
14837 child_die
= die
->child
;
14839 while (child_die
&& child_die
->tag
)
14841 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
14842 child_die
= child_die
->sibling
;
14845 /* Attach template arguments to type. */
14846 if (!template_args
.empty ())
14848 has_template_parameters
= true;
14849 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14850 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
14851 TYPE_TEMPLATE_ARGUMENTS (type
)
14852 = XOBNEWVEC (&objfile
->objfile_obstack
,
14854 TYPE_N_TEMPLATE_ARGUMENTS (type
));
14855 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
14856 template_args
.data (),
14857 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
14858 * sizeof (struct symbol
*)));
14861 /* Attach fields and member functions to the type. */
14862 if (fi
.nfields () > 0)
14863 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
14864 if (!fi
.fnfieldlists
.empty ())
14866 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
14868 /* Get the type which refers to the base class (possibly this
14869 class itself) which contains the vtable pointer for the current
14870 class from the DW_AT_containing_type attribute. This use of
14871 DW_AT_containing_type is a GNU extension. */
14873 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14875 struct type
*t
= die_containing_type (die
, cu
);
14877 set_type_vptr_basetype (type
, t
);
14882 /* Our own class provides vtbl ptr. */
14883 for (i
= t
->num_fields () - 1;
14884 i
>= TYPE_N_BASECLASSES (t
);
14887 const char *fieldname
= t
->field (i
).name ();
14889 if (is_vtable_name (fieldname
, cu
))
14891 set_type_vptr_fieldno (type
, i
);
14896 /* Complain if virtual function table field not found. */
14897 if (i
< TYPE_N_BASECLASSES (t
))
14898 complaint (_("virtual function table pointer "
14899 "not found when defining class '%s'"),
14900 type
->name () ? type
->name () : "");
14904 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
14907 else if (cu
->producer
14908 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
14910 /* The IBM XLC compiler does not provide direct indication
14911 of the containing type, but the vtable pointer is
14912 always named __vfp. */
14916 for (i
= type
->num_fields () - 1;
14917 i
>= TYPE_N_BASECLASSES (type
);
14920 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
14922 set_type_vptr_fieldno (type
, i
);
14923 set_type_vptr_basetype (type
, type
);
14930 /* Copy fi.typedef_field_list linked list elements content into the
14931 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14932 if (!fi
.typedef_field_list
.empty ())
14934 int count
= fi
.typedef_field_list
.size ();
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14937 TYPE_TYPEDEF_FIELD_ARRAY (type
)
14938 = ((struct decl_field
*)
14940 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
14941 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
14943 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
14944 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
14947 /* Copy fi.nested_types_list linked list elements content into the
14948 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14949 if (!fi
.nested_types_list
.empty ()
14950 && cu
->lang () != language_ada
)
14952 int count
= fi
.nested_types_list
.size ();
14954 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14955 TYPE_NESTED_TYPES_ARRAY (type
)
14956 = ((struct decl_field
*)
14957 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
14958 TYPE_NESTED_TYPES_COUNT (type
) = count
;
14960 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
14961 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
14965 quirk_gcc_member_function_pointer (type
, objfile
);
14966 if (cu
->lang () == language_rust
&& die
->tag
== DW_TAG_union_type
)
14967 cu
->rust_unions
.push_back (type
);
14968 else if (cu
->lang () == language_ada
)
14969 quirk_ada_thick_pointer_struct (die
, cu
, type
);
14971 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14972 snapshots) has been known to create a die giving a declaration
14973 for a class that has, as a child, a die giving a definition for a
14974 nested class. So we have to process our children even if the
14975 current die is a declaration. Normally, of course, a declaration
14976 won't have any children at all. */
14978 child_die
= die
->child
;
14980 while (child_die
!= NULL
&& child_die
->tag
)
14982 if (child_die
->tag
== DW_TAG_member
14983 || child_die
->tag
== DW_TAG_variable
14984 || child_die
->tag
== DW_TAG_inheritance
14985 || child_die
->tag
== DW_TAG_template_value_param
14986 || child_die
->tag
== DW_TAG_template_type_param
)
14991 process_die (child_die
, cu
);
14993 child_die
= child_die
->sibling
;
14996 /* Do not consider external references. According to the DWARF standard,
14997 these DIEs are identified by the fact that they have no byte_size
14998 attribute, and a declaration attribute. */
14999 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
15000 || !die_is_declaration (die
, cu
)
15001 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
15003 struct symbol
*sym
= new_symbol (die
, type
, cu
);
15005 if (has_template_parameters
)
15007 struct symtab
*symtab
;
15008 if (sym
!= nullptr)
15009 symtab
= sym
->symtab ();
15010 else if (cu
->line_header
!= nullptr)
15012 /* Any related symtab will do. */
15014 = cu
->line_header
->file_names ()[0].symtab
;
15019 complaint (_("could not find suitable "
15020 "symtab for template parameter"
15021 " - DIE at %s [in module %s]"),
15022 sect_offset_str (die
->sect_off
),
15023 objfile_name (objfile
));
15026 if (symtab
!= nullptr)
15028 /* Make sure that the symtab is set on the new symbols.
15029 Even though they don't appear in this symtab directly,
15030 other parts of gdb assume that symbols do, and this is
15031 reasonably true. */
15032 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
15033 TYPE_TEMPLATE_ARGUMENT (type
, i
)->set_symtab (symtab
);
15039 /* Assuming DIE is an enumeration type, and TYPE is its associated
15040 type, update TYPE using some information only available in DIE's
15041 children. In particular, the fields are computed. */
15044 update_enumeration_type_from_children (struct die_info
*die
,
15046 struct dwarf2_cu
*cu
)
15048 struct die_info
*child_die
;
15049 int unsigned_enum
= 1;
15052 auto_obstack obstack
;
15053 std::vector
<struct field
> fields
;
15055 for (child_die
= die
->child
;
15056 child_die
!= NULL
&& child_die
->tag
;
15057 child_die
= child_die
->sibling
)
15059 struct attribute
*attr
;
15061 const gdb_byte
*bytes
;
15062 struct dwarf2_locexpr_baton
*baton
;
15065 if (child_die
->tag
!= DW_TAG_enumerator
)
15068 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
15072 name
= dwarf2_name (child_die
, cu
);
15074 name
= "<anonymous enumerator>";
15076 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
15077 &value
, &bytes
, &baton
);
15085 if (count_one_bits_ll (value
) >= 2)
15089 fields
.emplace_back ();
15090 struct field
&field
= fields
.back ();
15091 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
15092 field
.set_loc_enumval (value
);
15095 if (!fields
.empty ())
15097 type
->set_num_fields (fields
.size ());
15100 TYPE_ALLOC (type
, sizeof (struct field
) * fields
.size ()));
15101 memcpy (type
->fields (), fields
.data (),
15102 sizeof (struct field
) * fields
.size ());
15106 type
->set_is_unsigned (true);
15109 type
->set_is_flag_enum (true);
15112 /* Given a DW_AT_enumeration_type die, set its type. We do not
15113 complete the type's fields yet, or create any symbols. */
15115 static struct type
*
15116 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15118 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15120 struct attribute
*attr
;
15123 /* If the definition of this type lives in .debug_types, read that type.
15124 Don't follow DW_AT_specification though, that will take us back up
15125 the chain and we want to go down. */
15126 attr
= die
->attr (DW_AT_signature
);
15127 if (attr
!= nullptr)
15129 type
= get_DW_AT_signature_type (die
, attr
, cu
);
15131 /* The type's CU may not be the same as CU.
15132 Ensure TYPE is recorded with CU in die_type_hash. */
15133 return set_die_type (die
, type
, cu
);
15136 type
= alloc_type (objfile
);
15138 type
->set_code (TYPE_CODE_ENUM
);
15139 name
= dwarf2_full_name (NULL
, die
, cu
);
15141 type
->set_name (name
);
15143 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
15146 struct type
*underlying_type
= die_type (die
, cu
);
15148 TYPE_TARGET_TYPE (type
) = underlying_type
;
15151 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15152 if (attr
!= nullptr)
15154 TYPE_LENGTH (type
) = attr
->constant_value (0);
15158 TYPE_LENGTH (type
) = 0;
15161 maybe_set_alignment (cu
, die
, type
);
15163 /* The enumeration DIE can be incomplete. In Ada, any type can be
15164 declared as private in the package spec, and then defined only
15165 inside the package body. Such types are known as Taft Amendment
15166 Types. When another package uses such a type, an incomplete DIE
15167 may be generated by the compiler. */
15168 if (die_is_declaration (die
, cu
))
15169 type
->set_is_stub (true);
15171 /* If this type has an underlying type that is not a stub, then we
15172 may use its attributes. We always use the "unsigned" attribute
15173 in this situation, because ordinarily we guess whether the type
15174 is unsigned -- but the guess can be wrong and the underlying type
15175 can tell us the reality. However, we defer to a local size
15176 attribute if one exists, because this lets the compiler override
15177 the underlying type if needed. */
15178 if (TYPE_TARGET_TYPE (type
) != NULL
&& !TYPE_TARGET_TYPE (type
)->is_stub ())
15180 struct type
*underlying_type
= TYPE_TARGET_TYPE (type
);
15181 underlying_type
= check_typedef (underlying_type
);
15183 type
->set_is_unsigned (underlying_type
->is_unsigned ());
15185 if (TYPE_LENGTH (type
) == 0)
15186 TYPE_LENGTH (type
) = TYPE_LENGTH (underlying_type
);
15188 if (TYPE_RAW_ALIGN (type
) == 0
15189 && TYPE_RAW_ALIGN (underlying_type
) != 0)
15190 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
15193 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
15195 set_die_type (die
, type
, cu
);
15197 /* Finish the creation of this type by using the enum's children.
15198 Note that, as usual, this must come after set_die_type to avoid
15199 infinite recursion when trying to compute the names of the
15201 update_enumeration_type_from_children (die
, type
, cu
);
15206 /* Given a pointer to a die which begins an enumeration, process all
15207 the dies that define the members of the enumeration, and create the
15208 symbol for the enumeration type.
15210 NOTE: We reverse the order of the element list. */
15213 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
15215 struct type
*this_type
;
15217 this_type
= get_die_type (die
, cu
);
15218 if (this_type
== NULL
)
15219 this_type
= read_enumeration_type (die
, cu
);
15221 if (die
->child
!= NULL
)
15223 struct die_info
*child_die
;
15226 child_die
= die
->child
;
15227 while (child_die
&& child_die
->tag
)
15229 if (child_die
->tag
!= DW_TAG_enumerator
)
15231 process_die (child_die
, cu
);
15235 name
= dwarf2_name (child_die
, cu
);
15237 new_symbol (child_die
, this_type
, cu
);
15240 child_die
= child_die
->sibling
;
15244 /* If we are reading an enum from a .debug_types unit, and the enum
15245 is a declaration, and the enum is not the signatured type in the
15246 unit, then we do not want to add a symbol for it. Adding a
15247 symbol would in some cases obscure the true definition of the
15248 enum, giving users an incomplete type when the definition is
15249 actually available. Note that we do not want to do this for all
15250 enums which are just declarations, because C++0x allows forward
15251 enum declarations. */
15252 if (cu
->per_cu
->is_debug_types
15253 && die_is_declaration (die
, cu
))
15255 struct signatured_type
*sig_type
;
15257 sig_type
= (struct signatured_type
*) cu
->per_cu
;
15258 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
15259 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
15263 new_symbol (die
, this_type
, cu
);
15266 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15267 expression for an index type and finds the corresponding field
15268 offset in the hidden "P_BOUNDS" structure. Returns true on success
15269 and updates *FIELD, false if it fails to recognize an
15273 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
15274 int *bounds_offset
, struct field
*field
,
15275 struct dwarf2_cu
*cu
)
15277 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
15278 if (attr
== nullptr || !attr
->form_is_block ())
15281 const struct dwarf_block
*block
= attr
->as_block ();
15282 const gdb_byte
*start
= block
->data
;
15283 const gdb_byte
*end
= block
->data
+ block
->size
;
15285 /* The expression to recognize generally looks like:
15287 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15288 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15290 However, the second "plus_uconst" may be missing:
15292 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15293 DW_OP_deref_size: 4)
15295 This happens when the field is at the start of the structure.
15297 Also, the final deref may not be sized:
15299 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15302 This happens when the size of the index type happens to be the
15303 same as the architecture's word size. This can occur with or
15304 without the second plus_uconst. */
15306 if (end
- start
< 2)
15308 if (*start
++ != DW_OP_push_object_address
)
15310 if (*start
++ != DW_OP_plus_uconst
)
15313 uint64_t this_bound_off
;
15314 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
15315 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
15317 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15318 is consistent among all bounds. */
15319 if (*bounds_offset
== -1)
15320 *bounds_offset
= this_bound_off
;
15321 else if (*bounds_offset
!= this_bound_off
)
15324 if (start
== end
|| *start
++ != DW_OP_deref
)
15330 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
15332 /* This means an offset of 0. */
15334 else if (*start
++ != DW_OP_plus_uconst
)
15338 /* The size is the parameter to DW_OP_plus_uconst. */
15340 start
= gdb_read_uleb128 (start
, end
, &val
);
15341 if (start
== nullptr)
15343 if ((int) val
!= val
)
15352 if (*start
== DW_OP_deref_size
)
15354 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
15355 if (start
== nullptr)
15358 else if (*start
== DW_OP_deref
)
15360 size
= cu
->header
.addr_size
;
15366 field
->set_loc_bitpos (8 * offset
);
15367 if (size
!= TYPE_LENGTH (field
->type ()))
15368 FIELD_BITSIZE (*field
) = 8 * size
;
15373 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15374 some kinds of Ada arrays:
15376 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15377 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15378 <11e0> DW_AT_data_location: 2 byte block: 97 6
15379 (DW_OP_push_object_address; DW_OP_deref)
15380 <11e3> DW_AT_type : <0x1173>
15381 <11e7> DW_AT_sibling : <0x1201>
15382 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15383 <11ec> DW_AT_type : <0x1206>
15384 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15385 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15386 DW_OP_deref_size: 4)
15387 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15388 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15389 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15391 This actually represents a "thick pointer", which is a structure
15392 with two elements: one that is a pointer to the array data, and one
15393 that is a pointer to another structure; this second structure holds
15396 This returns a new type on success, or nullptr if this didn't
15397 recognize the type. */
15399 static struct type
*
15400 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
15403 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
15404 /* So far we've only seen this with block form. */
15405 if (attr
== nullptr || !attr
->form_is_block ())
15408 /* Note that this will fail if the structure layout is changed by
15409 the compiler. However, we have no good way to recognize some
15410 other layout, because we don't know what expression the compiler
15411 might choose to emit should this happen. */
15412 struct dwarf_block
*blk
= attr
->as_block ();
15414 || blk
->data
[0] != DW_OP_push_object_address
15415 || blk
->data
[1] != DW_OP_deref
)
15418 int bounds_offset
= -1;
15419 int max_align
= -1;
15420 std::vector
<struct field
> range_fields
;
15421 for (struct die_info
*child_die
= die
->child
;
15423 child_die
= child_die
->sibling
)
15425 if (child_die
->tag
== DW_TAG_subrange_type
)
15427 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
15429 int this_align
= type_align (underlying
);
15430 if (this_align
> max_align
)
15431 max_align
= this_align
;
15433 range_fields
.emplace_back ();
15434 range_fields
.emplace_back ();
15436 struct field
&lower
= range_fields
[range_fields
.size () - 2];
15437 struct field
&upper
= range_fields
[range_fields
.size () - 1];
15439 lower
.set_type (underlying
);
15440 FIELD_ARTIFICIAL (lower
) = 1;
15442 upper
.set_type (underlying
);
15443 FIELD_ARTIFICIAL (upper
) = 1;
15445 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
15446 &bounds_offset
, &lower
, cu
)
15447 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
15448 &bounds_offset
, &upper
, cu
))
15453 /* This shouldn't really happen, but double-check that we found
15454 where the bounds are stored. */
15455 if (bounds_offset
== -1)
15458 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15459 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15463 /* Set the name of each field in the bounds. */
15464 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
15465 range_fields
[i
].set_name (objfile
->intern (name
));
15466 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
15467 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
15470 struct type
*bounds
= alloc_type (objfile
);
15471 bounds
->set_code (TYPE_CODE_STRUCT
);
15473 bounds
->set_num_fields (range_fields
.size ());
15475 ((struct field
*) TYPE_ALLOC (bounds
, (bounds
->num_fields ()
15476 * sizeof (struct field
))));
15477 memcpy (bounds
->fields (), range_fields
.data (),
15478 bounds
->num_fields () * sizeof (struct field
));
15480 int last_fieldno
= range_fields
.size () - 1;
15481 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
15482 + TYPE_LENGTH (bounds
->field (last_fieldno
).type ()));
15483 TYPE_LENGTH (bounds
) = align_up (bounds_size
, max_align
);
15485 /* Rewrite the existing array type in place. Specifically, we
15486 remove any dynamic properties we might have read, and we replace
15487 the index types. */
15488 struct type
*iter
= type
;
15489 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15491 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
15492 iter
->main_type
->dyn_prop_list
= nullptr;
15493 iter
->set_index_type
15494 (create_static_range_type (NULL
, bounds
->field (i
).type (), 1, 0));
15495 iter
= TYPE_TARGET_TYPE (iter
);
15498 struct type
*result
= alloc_type (objfile
);
15499 result
->set_code (TYPE_CODE_STRUCT
);
15501 result
->set_num_fields (2);
15503 ((struct field
*) TYPE_ZALLOC (result
, (result
->num_fields ()
15504 * sizeof (struct field
))));
15506 /* The names are chosen to coincide with what the compiler does with
15507 -fgnat-encodings=all, which the Ada code in gdb already
15509 result
->field (0).set_name ("P_ARRAY");
15510 result
->field (0).set_type (lookup_pointer_type (type
));
15512 result
->field (1).set_name ("P_BOUNDS");
15513 result
->field (1).set_type (lookup_pointer_type (bounds
));
15514 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
15516 result
->set_name (type
->name ());
15517 TYPE_LENGTH (result
) = (TYPE_LENGTH (result
->field (0).type ())
15518 + TYPE_LENGTH (result
->field (1).type ()));
15523 /* Extract all information from a DW_TAG_array_type DIE and put it in
15524 the DIE's type field. For now, this only handles one dimensional
15527 static struct type
*
15528 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15530 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15531 struct die_info
*child_die
;
15533 struct type
*element_type
, *range_type
, *index_type
;
15534 struct attribute
*attr
;
15536 struct dynamic_prop
*byte_stride_prop
= NULL
;
15537 unsigned int bit_stride
= 0;
15539 element_type
= die_type (die
, cu
);
15541 /* The die_type call above may have already set the type for this DIE. */
15542 type
= get_die_type (die
, cu
);
15546 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15550 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15553 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
15554 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
15558 complaint (_("unable to read array DW_AT_byte_stride "
15559 " - DIE at %s [in module %s]"),
15560 sect_offset_str (die
->sect_off
),
15561 objfile_name (cu
->per_objfile
->objfile
));
15562 /* Ignore this attribute. We will likely not be able to print
15563 arrays of this type correctly, but there is little we can do
15564 to help if we cannot read the attribute's value. */
15565 byte_stride_prop
= NULL
;
15569 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15571 bit_stride
= attr
->constant_value (0);
15573 /* Irix 6.2 native cc creates array types without children for
15574 arrays with unspecified length. */
15575 if (die
->child
== NULL
)
15577 index_type
= objfile_type (objfile
)->builtin_int
;
15578 range_type
= create_static_range_type (NULL
, index_type
, 0, -1);
15579 type
= create_array_type_with_stride (NULL
, element_type
, range_type
,
15580 byte_stride_prop
, bit_stride
);
15581 return set_die_type (die
, type
, cu
);
15584 std::vector
<struct type
*> range_types
;
15585 child_die
= die
->child
;
15586 while (child_die
&& child_die
->tag
)
15588 if (child_die
->tag
== DW_TAG_subrange_type
15589 || child_die
->tag
== DW_TAG_generic_subrange
)
15591 struct type
*child_type
= read_type_die (child_die
, cu
);
15593 if (child_type
!= NULL
)
15595 /* The range type was succesfully read. Save it for the
15596 array type creation. */
15597 range_types
.push_back (child_type
);
15600 child_die
= child_die
->sibling
;
15603 if (range_types
.empty ())
15605 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15606 sect_offset_str (die
->sect_off
),
15607 objfile_name (cu
->per_objfile
->objfile
));
15611 /* Dwarf2 dimensions are output from left to right, create the
15612 necessary array types in backwards order. */
15614 type
= element_type
;
15616 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
15620 while (i
< range_types
.size ())
15622 type
= create_array_type_with_stride (NULL
, type
, range_types
[i
++],
15623 byte_stride_prop
, bit_stride
);
15625 byte_stride_prop
= nullptr;
15630 size_t ndim
= range_types
.size ();
15633 type
= create_array_type_with_stride (NULL
, type
, range_types
[ndim
],
15634 byte_stride_prop
, bit_stride
);
15636 byte_stride_prop
= nullptr;
15640 gdb_assert (type
!= element_type
);
15642 /* Understand Dwarf2 support for vector types (like they occur on
15643 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15644 array type. This is not part of the Dwarf2/3 standard yet, but a
15645 custom vendor extension. The main difference between a regular
15646 array and the vector variant is that vectors are passed by value
15648 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
15649 if (attr
!= nullptr)
15650 make_vector_type (type
);
15652 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15653 implementation may choose to implement triple vectors using this
15655 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15656 if (attr
!= nullptr && attr
->form_is_unsigned ())
15658 if (attr
->as_unsigned () >= TYPE_LENGTH (type
))
15659 TYPE_LENGTH (type
) = attr
->as_unsigned ();
15661 complaint (_("DW_AT_byte_size for array type smaller "
15662 "than the total size of elements"));
15665 name
= dwarf2_name (die
, cu
);
15667 type
->set_name (name
);
15669 maybe_set_alignment (cu
, die
, type
);
15671 struct type
*replacement_type
= nullptr;
15672 if (cu
->lang () == language_ada
)
15674 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
15675 if (replacement_type
!= nullptr)
15676 type
= replacement_type
;
15679 /* Install the type in the die. */
15680 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
15682 /* set_die_type should be already done. */
15683 set_descriptive_type (type
, die
, cu
);
15688 static enum dwarf_array_dim_ordering
15689 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
15691 struct attribute
*attr
;
15693 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
15695 if (attr
!= nullptr)
15697 LONGEST val
= attr
->constant_value (-1);
15698 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
15699 return (enum dwarf_array_dim_ordering
) val
;
15702 /* GNU F77 is a special case, as at 08/2004 array type info is the
15703 opposite order to the dwarf2 specification, but data is still
15704 laid out as per normal fortran.
15706 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15707 version checking. */
15709 if (cu
->lang () == language_fortran
15710 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
15712 return DW_ORD_row_major
;
15715 switch (cu
->language_defn
->array_ordering ())
15717 case array_column_major
:
15718 return DW_ORD_col_major
;
15719 case array_row_major
:
15721 return DW_ORD_row_major
;
15725 /* Extract all information from a DW_TAG_set_type DIE and put it in
15726 the DIE's type field. */
15728 static struct type
*
15729 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15731 struct type
*domain_type
, *set_type
;
15732 struct attribute
*attr
;
15734 domain_type
= die_type (die
, cu
);
15736 /* The die_type call above may have already set the type for this DIE. */
15737 set_type
= get_die_type (die
, cu
);
15741 set_type
= create_set_type (NULL
, domain_type
);
15743 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15744 if (attr
!= nullptr && attr
->form_is_unsigned ())
15745 TYPE_LENGTH (set_type
) = attr
->as_unsigned ();
15747 maybe_set_alignment (cu
, die
, set_type
);
15749 return set_die_type (die
, set_type
, cu
);
15752 /* A helper for read_common_block that creates a locexpr baton.
15753 SYM is the symbol which we are marking as computed.
15754 COMMON_DIE is the DIE for the common block.
15755 COMMON_LOC is the location expression attribute for the common
15757 MEMBER_LOC is the location expression attribute for the particular
15758 member of the common block that we are processing.
15759 CU is the CU from which the above come. */
15762 mark_common_block_symbol_computed (struct symbol
*sym
,
15763 struct die_info
*common_die
,
15764 struct attribute
*common_loc
,
15765 struct attribute
*member_loc
,
15766 struct dwarf2_cu
*cu
)
15768 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15769 struct objfile
*objfile
= per_objfile
->objfile
;
15770 struct dwarf2_locexpr_baton
*baton
;
15772 unsigned int cu_off
;
15773 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
15774 LONGEST offset
= 0;
15776 gdb_assert (common_loc
&& member_loc
);
15777 gdb_assert (common_loc
->form_is_block ());
15778 gdb_assert (member_loc
->form_is_block ()
15779 || member_loc
->form_is_constant ());
15781 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
15782 baton
->per_objfile
= per_objfile
;
15783 baton
->per_cu
= cu
->per_cu
;
15784 gdb_assert (baton
->per_cu
);
15786 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15788 if (member_loc
->form_is_constant ())
15790 offset
= member_loc
->constant_value (0);
15791 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
15794 baton
->size
+= member_loc
->as_block ()->size
;
15796 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
15799 *ptr
++ = DW_OP_call4
;
15800 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
15801 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
15804 if (member_loc
->form_is_constant ())
15806 *ptr
++ = DW_OP_addr
;
15807 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
15808 ptr
+= cu
->header
.addr_size
;
15812 /* We have to copy the data here, because DW_OP_call4 will only
15813 use a DW_AT_location attribute. */
15814 struct dwarf_block
*block
= member_loc
->as_block ();
15815 memcpy (ptr
, block
->data
, block
->size
);
15816 ptr
+= block
->size
;
15819 *ptr
++ = DW_OP_plus
;
15820 gdb_assert (ptr
- baton
->data
== baton
->size
);
15822 SYMBOL_LOCATION_BATON (sym
) = baton
;
15823 sym
->set_aclass_index (dwarf2_locexpr_index
);
15826 /* Create appropriate locally-scoped variables for all the
15827 DW_TAG_common_block entries. Also create a struct common_block
15828 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15829 is used to separate the common blocks name namespace from regular
15833 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
15835 struct attribute
*attr
;
15837 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
15838 if (attr
!= nullptr)
15840 /* Support the .debug_loc offsets. */
15841 if (attr
->form_is_block ())
15845 else if (attr
->form_is_section_offset ())
15847 dwarf2_complex_location_expr_complaint ();
15852 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15853 "common block member");
15858 if (die
->child
!= NULL
)
15860 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15861 struct die_info
*child_die
;
15862 size_t n_entries
= 0, size
;
15863 struct common_block
*common_block
;
15864 struct symbol
*sym
;
15866 for (child_die
= die
->child
;
15867 child_die
&& child_die
->tag
;
15868 child_die
= child_die
->sibling
)
15871 size
= (sizeof (struct common_block
)
15872 + (n_entries
- 1) * sizeof (struct symbol
*));
15874 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
15876 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
15877 common_block
->n_entries
= 0;
15879 for (child_die
= die
->child
;
15880 child_die
&& child_die
->tag
;
15881 child_die
= child_die
->sibling
)
15883 /* Create the symbol in the DW_TAG_common_block block in the current
15885 sym
= new_symbol (child_die
, NULL
, cu
);
15888 struct attribute
*member_loc
;
15890 common_block
->contents
[common_block
->n_entries
++] = sym
;
15892 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
15896 /* GDB has handled this for a long time, but it is
15897 not specified by DWARF. It seems to have been
15898 emitted by gfortran at least as recently as:
15899 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15900 complaint (_("Variable in common block has "
15901 "DW_AT_data_member_location "
15902 "- DIE at %s [in module %s]"),
15903 sect_offset_str (child_die
->sect_off
),
15904 objfile_name (objfile
));
15906 if (member_loc
->form_is_section_offset ())
15907 dwarf2_complex_location_expr_complaint ();
15908 else if (member_loc
->form_is_constant ()
15909 || member_loc
->form_is_block ())
15911 if (attr
!= nullptr)
15912 mark_common_block_symbol_computed (sym
, die
, attr
,
15916 dwarf2_complex_location_expr_complaint ();
15921 sym
= new_symbol (die
, objfile_type (objfile
)->builtin_void
, cu
);
15922 sym
->set_value_common_block (common_block
);
15926 /* Create a type for a C++ namespace. */
15928 static struct type
*
15929 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15931 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15932 const char *previous_prefix
, *name
;
15936 /* For extensions, reuse the type of the original namespace. */
15937 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
15939 struct die_info
*ext_die
;
15940 struct dwarf2_cu
*ext_cu
= cu
;
15942 ext_die
= dwarf2_extension (die
, &ext_cu
);
15943 type
= read_type_die (ext_die
, ext_cu
);
15945 /* EXT_CU may not be the same as CU.
15946 Ensure TYPE is recorded with CU in die_type_hash. */
15947 return set_die_type (die
, type
, cu
);
15950 name
= namespace_name (die
, &is_anonymous
, cu
);
15952 /* Now build the name of the current namespace. */
15954 previous_prefix
= determine_prefix (die
, cu
);
15955 if (previous_prefix
[0] != '\0')
15956 name
= typename_concat (&objfile
->objfile_obstack
,
15957 previous_prefix
, name
, 0, cu
);
15959 /* Create the type. */
15960 type
= init_type (objfile
, TYPE_CODE_NAMESPACE
, 0, name
);
15962 return set_die_type (die
, type
, cu
);
15965 /* Read a namespace scope. */
15968 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
15970 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15973 /* Add a symbol associated to this if we haven't seen the namespace
15974 before. Also, add a using directive if it's an anonymous
15977 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
15981 type
= read_type_die (die
, cu
);
15982 new_symbol (die
, type
, cu
);
15984 namespace_name (die
, &is_anonymous
, cu
);
15987 const char *previous_prefix
= determine_prefix (die
, cu
);
15989 std::vector
<const char *> excludes
;
15990 add_using_directive (using_directives (cu
),
15991 previous_prefix
, type
->name (), NULL
,
15992 NULL
, excludes
, 0, &objfile
->objfile_obstack
);
15996 if (die
->child
!= NULL
)
15998 struct die_info
*child_die
= die
->child
;
16000 while (child_die
&& child_die
->tag
)
16002 process_die (child_die
, cu
);
16003 child_die
= child_die
->sibling
;
16008 /* Read a Fortran module as type. This DIE can be only a declaration used for
16009 imported module. Still we need that type as local Fortran "use ... only"
16010 declaration imports depend on the created type in determine_prefix. */
16012 static struct type
*
16013 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16015 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16016 const char *module_name
;
16019 module_name
= dwarf2_name (die
, cu
);
16020 type
= init_type (objfile
, TYPE_CODE_MODULE
, 0, module_name
);
16022 return set_die_type (die
, type
, cu
);
16025 /* Read a Fortran module. */
16028 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
16030 struct die_info
*child_die
= die
->child
;
16033 type
= read_type_die (die
, cu
);
16034 new_symbol (die
, type
, cu
);
16036 while (child_die
&& child_die
->tag
)
16038 process_die (child_die
, cu
);
16039 child_die
= child_die
->sibling
;
16043 /* Return the name of the namespace represented by DIE. Set
16044 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16047 static const char *
16048 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
16050 struct die_info
*current_die
;
16051 const char *name
= NULL
;
16053 /* Loop through the extensions until we find a name. */
16055 for (current_die
= die
;
16056 current_die
!= NULL
;
16057 current_die
= dwarf2_extension (die
, &cu
))
16059 /* We don't use dwarf2_name here so that we can detect the absence
16060 of a name -> anonymous namespace. */
16061 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
16067 /* Is it an anonymous namespace? */
16069 *is_anonymous
= (name
== NULL
);
16071 name
= CP_ANONYMOUS_NAMESPACE_STR
;
16076 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16077 the user defined type vector. */
16079 static struct type
*
16080 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16082 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
16083 struct comp_unit_head
*cu_header
= &cu
->header
;
16085 struct attribute
*attr_byte_size
;
16086 struct attribute
*attr_address_class
;
16087 int byte_size
, addr_class
;
16088 struct type
*target_type
;
16090 target_type
= die_type (die
, cu
);
16092 /* The die_type call above may have already set the type for this DIE. */
16093 type
= get_die_type (die
, cu
);
16097 type
= lookup_pointer_type (target_type
);
16099 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16100 if (attr_byte_size
)
16101 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
16103 byte_size
= cu_header
->addr_size
;
16105 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
16106 if (attr_address_class
)
16107 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
16109 addr_class
= DW_ADDR_none
;
16111 ULONGEST alignment
= get_alignment (cu
, die
);
16113 /* If the pointer size, alignment, or address class is different
16114 than the default, create a type variant marked as such and set
16115 the length accordingly. */
16116 if (TYPE_LENGTH (type
) != byte_size
16117 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
16118 && alignment
!= TYPE_RAW_ALIGN (type
))
16119 || addr_class
!= DW_ADDR_none
)
16121 if (gdbarch_address_class_type_flags_p (gdbarch
))
16123 type_instance_flags type_flags
16124 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
16126 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
16128 type
= make_type_with_address_space (type
, type_flags
);
16130 else if (TYPE_LENGTH (type
) != byte_size
)
16132 complaint (_("invalid pointer size %d"), byte_size
);
16134 else if (TYPE_RAW_ALIGN (type
) != alignment
)
16136 complaint (_("Invalid DW_AT_alignment"
16137 " - DIE at %s [in module %s]"),
16138 sect_offset_str (die
->sect_off
),
16139 objfile_name (cu
->per_objfile
->objfile
));
16143 /* Should we also complain about unhandled address classes? */
16147 TYPE_LENGTH (type
) = byte_size
;
16148 set_type_align (type
, alignment
);
16149 return set_die_type (die
, type
, cu
);
16152 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16153 the user defined type vector. */
16155 static struct type
*
16156 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16159 struct type
*to_type
;
16160 struct type
*domain
;
16162 to_type
= die_type (die
, cu
);
16163 domain
= die_containing_type (die
, cu
);
16165 /* The calls above may have already set the type for this DIE. */
16166 type
= get_die_type (die
, cu
);
16170 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
16171 type
= lookup_methodptr_type (to_type
);
16172 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
16174 struct type
*new_type
= alloc_type (cu
->per_objfile
->objfile
);
16176 smash_to_method_type (new_type
, domain
, TYPE_TARGET_TYPE (to_type
),
16177 to_type
->fields (), to_type
->num_fields (),
16178 to_type
->has_varargs ());
16179 type
= lookup_methodptr_type (new_type
);
16182 type
= lookup_memberptr_type (to_type
, domain
);
16184 return set_die_type (die
, type
, cu
);
16187 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16188 the user defined type vector. */
16190 static struct type
*
16191 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16192 enum type_code refcode
)
16194 struct comp_unit_head
*cu_header
= &cu
->header
;
16195 struct type
*type
, *target_type
;
16196 struct attribute
*attr
;
16198 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
16200 target_type
= die_type (die
, cu
);
16202 /* The die_type call above may have already set the type for this DIE. */
16203 type
= get_die_type (die
, cu
);
16207 type
= lookup_reference_type (target_type
, refcode
);
16208 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16209 if (attr
!= nullptr)
16211 TYPE_LENGTH (type
) = attr
->constant_value (cu_header
->addr_size
);
16215 TYPE_LENGTH (type
) = cu_header
->addr_size
;
16217 maybe_set_alignment (cu
, die
, type
);
16218 return set_die_type (die
, type
, cu
);
16221 /* Add the given cv-qualifiers to the element type of the array. GCC
16222 outputs DWARF type qualifiers that apply to an array, not the
16223 element type. But GDB relies on the array element type to carry
16224 the cv-qualifiers. This mimics section 6.7.3 of the C99
16227 static struct type
*
16228 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16229 struct type
*base_type
, int cnst
, int voltl
)
16231 struct type
*el_type
, *inner_array
;
16233 base_type
= copy_type (base_type
);
16234 inner_array
= base_type
;
16236 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
16238 TYPE_TARGET_TYPE (inner_array
) =
16239 copy_type (TYPE_TARGET_TYPE (inner_array
));
16240 inner_array
= TYPE_TARGET_TYPE (inner_array
);
16243 el_type
= TYPE_TARGET_TYPE (inner_array
);
16244 cnst
|= TYPE_CONST (el_type
);
16245 voltl
|= TYPE_VOLATILE (el_type
);
16246 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
16248 return set_die_type (die
, base_type
, cu
);
16251 static struct type
*
16252 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16254 struct type
*base_type
, *cv_type
;
16256 base_type
= die_type (die
, cu
);
16258 /* The die_type call above may have already set the type for this DIE. */
16259 cv_type
= get_die_type (die
, cu
);
16263 /* In case the const qualifier is applied to an array type, the element type
16264 is so qualified, not the array type (section 6.7.3 of C99). */
16265 if (base_type
->code () == TYPE_CODE_ARRAY
)
16266 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
16268 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
16269 return set_die_type (die
, cv_type
, cu
);
16272 static struct type
*
16273 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16275 struct type
*base_type
, *cv_type
;
16277 base_type
= die_type (die
, cu
);
16279 /* The die_type call above may have already set the type for this DIE. */
16280 cv_type
= get_die_type (die
, cu
);
16284 /* In case the volatile qualifier is applied to an array type, the
16285 element type is so qualified, not the array type (section 6.7.3
16287 if (base_type
->code () == TYPE_CODE_ARRAY
)
16288 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
16290 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
16291 return set_die_type (die
, cv_type
, cu
);
16294 /* Handle DW_TAG_restrict_type. */
16296 static struct type
*
16297 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16299 struct type
*base_type
, *cv_type
;
16301 base_type
= die_type (die
, cu
);
16303 /* The die_type call above may have already set the type for this DIE. */
16304 cv_type
= get_die_type (die
, cu
);
16308 cv_type
= make_restrict_type (base_type
);
16309 return set_die_type (die
, cv_type
, cu
);
16312 /* Handle DW_TAG_atomic_type. */
16314 static struct type
*
16315 read_tag_atomic_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16317 struct type
*base_type
, *cv_type
;
16319 base_type
= die_type (die
, cu
);
16321 /* The die_type call above may have already set the type for this DIE. */
16322 cv_type
= get_die_type (die
, cu
);
16326 cv_type
= make_atomic_type (base_type
);
16327 return set_die_type (die
, cv_type
, cu
);
16330 /* Extract all information from a DW_TAG_string_type DIE and add to
16331 the user defined type vector. It isn't really a user defined type,
16332 but it behaves like one, with other DIE's using an AT_user_def_type
16333 attribute to reference it. */
16335 static struct type
*
16336 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16338 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16339 struct gdbarch
*gdbarch
= objfile
->arch ();
16340 struct type
*type
, *range_type
, *index_type
, *char_type
;
16341 struct attribute
*attr
;
16342 struct dynamic_prop prop
;
16343 bool length_is_constant
= true;
16346 /* There are a couple of places where bit sizes might be made use of
16347 when parsing a DW_TAG_string_type, however, no producer that we know
16348 of make use of these. Handling bit sizes that are a multiple of the
16349 byte size is easy enough, but what about other bit sizes? Lets deal
16350 with that problem when we have to. Warn about these attributes being
16351 unsupported, then parse the type and ignore them like we always
16353 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
16354 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
16356 static bool warning_printed
= false;
16357 if (!warning_printed
)
16359 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16360 "currently supported on DW_TAG_string_type."));
16361 warning_printed
= true;
16365 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
16366 if (attr
!= nullptr && !attr
->form_is_constant ())
16368 /* The string length describes the location at which the length of
16369 the string can be found. The size of the length field can be
16370 specified with one of the attributes below. */
16371 struct type
*prop_type
;
16372 struct attribute
*len
16373 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
16374 if (len
== nullptr)
16375 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16376 if (len
!= nullptr && len
->form_is_constant ())
16378 /* Pass 0 as the default as we know this attribute is constant
16379 and the default value will not be returned. */
16380 LONGEST sz
= len
->constant_value (0);
16381 prop_type
= objfile_int_type (objfile
, sz
, true);
16385 /* If the size is not specified then we assume it is the size of
16386 an address on this target. */
16387 prop_type
= cu
->addr_sized_int_type (true);
16390 /* Convert the attribute into a dynamic property. */
16391 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
16394 length_is_constant
= false;
16396 else if (attr
!= nullptr)
16398 /* This DW_AT_string_length just contains the length with no
16399 indirection. There's no need to create a dynamic property in this
16400 case. Pass 0 for the default value as we know it will not be
16401 returned in this case. */
16402 length
= attr
->constant_value (0);
16404 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
16406 /* We don't currently support non-constant byte sizes for strings. */
16407 length
= attr
->constant_value (1);
16411 /* Use 1 as a fallback length if we have nothing else. */
16415 index_type
= objfile_type (objfile
)->builtin_int
;
16416 if (length_is_constant
)
16417 range_type
= create_static_range_type (NULL
, index_type
, 1, length
);
16420 struct dynamic_prop low_bound
;
16422 low_bound
.set_const_val (1);
16423 range_type
= create_range_type (NULL
, index_type
, &low_bound
, &prop
, 0);
16425 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
16426 type
= create_string_type (NULL
, char_type
, range_type
);
16428 return set_die_type (die
, type
, cu
);
16431 /* Assuming that DIE corresponds to a function, returns nonzero
16432 if the function is prototyped. */
16435 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
16437 struct attribute
*attr
;
16439 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
16440 if (attr
&& attr
->as_boolean ())
16443 /* The DWARF standard implies that the DW_AT_prototyped attribute
16444 is only meaningful for C, but the concept also extends to other
16445 languages that allow unprototyped functions (Eg: Objective C).
16446 For all other languages, assume that functions are always
16448 if (cu
->lang () != language_c
16449 && cu
->lang () != language_objc
16450 && cu
->lang () != language_opencl
)
16453 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16454 prototyped and unprototyped functions; default to prototyped,
16455 since that is more common in modern code (and RealView warns
16456 about unprototyped functions). */
16457 if (producer_is_realview (cu
->producer
))
16463 /* Handle DIES due to C code like:
16467 int (*funcp)(int a, long l);
16471 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16473 static struct type
*
16474 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16476 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16477 struct type
*type
; /* Type that this function returns. */
16478 struct type
*ftype
; /* Function that returns above type. */
16479 struct attribute
*attr
;
16481 type
= die_type (die
, cu
);
16483 /* The die_type call above may have already set the type for this DIE. */
16484 ftype
= get_die_type (die
, cu
);
16488 ftype
= lookup_function_type (type
);
16490 if (prototyped_function_p (die
, cu
))
16491 ftype
->set_is_prototyped (true);
16493 /* Store the calling convention in the type if it's available in
16494 the subroutine die. Otherwise set the calling convention to
16495 the default value DW_CC_normal. */
16496 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
16497 if (attr
!= nullptr
16498 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
16499 TYPE_CALLING_CONVENTION (ftype
)
16500 = (enum dwarf_calling_convention
) attr
->constant_value (0);
16501 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
16502 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
16504 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
16506 /* Record whether the function returns normally to its caller or not
16507 if the DWARF producer set that information. */
16508 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
16509 if (attr
&& attr
->as_boolean ())
16510 TYPE_NO_RETURN (ftype
) = 1;
16512 /* We need to add the subroutine type to the die immediately so
16513 we don't infinitely recurse when dealing with parameters
16514 declared as the same subroutine type. */
16515 set_die_type (die
, ftype
, cu
);
16517 if (die
->child
!= NULL
)
16519 struct type
*void_type
= objfile_type (objfile
)->builtin_void
;
16520 struct die_info
*child_die
;
16521 int nparams
, iparams
;
16523 /* Count the number of parameters.
16524 FIXME: GDB currently ignores vararg functions, but knows about
16525 vararg member functions. */
16527 child_die
= die
->child
;
16528 while (child_die
&& child_die
->tag
)
16530 if (child_die
->tag
== DW_TAG_formal_parameter
)
16532 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
16533 ftype
->set_has_varargs (true);
16535 child_die
= child_die
->sibling
;
16538 /* Allocate storage for parameters and fill them in. */
16539 ftype
->set_num_fields (nparams
);
16541 ((struct field
*) TYPE_ZALLOC (ftype
, nparams
* sizeof (struct field
)));
16543 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16544 even if we error out during the parameters reading below. */
16545 for (iparams
= 0; iparams
< nparams
; iparams
++)
16546 ftype
->field (iparams
).set_type (void_type
);
16549 child_die
= die
->child
;
16550 while (child_die
&& child_die
->tag
)
16552 if (child_die
->tag
== DW_TAG_formal_parameter
)
16554 struct type
*arg_type
;
16556 /* DWARF version 2 has no clean way to discern C++
16557 static and non-static member functions. G++ helps
16558 GDB by marking the first parameter for non-static
16559 member functions (which is the this pointer) as
16560 artificial. We pass this information to
16561 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16563 DWARF version 3 added DW_AT_object_pointer, which GCC
16564 4.5 does not yet generate. */
16565 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
16566 if (attr
!= nullptr)
16567 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = attr
->as_boolean ();
16569 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
16570 arg_type
= die_type (child_die
, cu
);
16572 /* RealView does not mark THIS as const, which the testsuite
16573 expects. GCC marks THIS as const in method definitions,
16574 but not in the class specifications (GCC PR 43053). */
16575 if (cu
->lang () == language_cplus
16576 && !TYPE_CONST (arg_type
)
16577 && TYPE_FIELD_ARTIFICIAL (ftype
, iparams
))
16580 struct dwarf2_cu
*arg_cu
= cu
;
16581 const char *name
= dwarf2_name (child_die
, cu
);
16583 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
16584 if (attr
!= nullptr)
16586 /* If the compiler emits this, use it. */
16587 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
16590 else if (name
&& strcmp (name
, "this") == 0)
16591 /* Function definitions will have the argument names. */
16593 else if (name
== NULL
&& iparams
== 0)
16594 /* Declarations may not have the names, so like
16595 elsewhere in GDB, assume an artificial first
16596 argument is "this". */
16600 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
16604 ftype
->field (iparams
).set_type (arg_type
);
16607 child_die
= child_die
->sibling
;
16614 static struct type
*
16615 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
16617 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16618 const char *name
= NULL
;
16619 struct type
*this_type
, *target_type
;
16621 name
= dwarf2_full_name (NULL
, die
, cu
);
16622 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, name
);
16623 this_type
->set_target_is_stub (true);
16624 set_die_type (die
, this_type
, cu
);
16625 target_type
= die_type (die
, cu
);
16626 if (target_type
!= this_type
)
16627 TYPE_TARGET_TYPE (this_type
) = target_type
;
16630 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16631 spec and cause infinite loops in GDB. */
16632 complaint (_("Self-referential DW_TAG_typedef "
16633 "- DIE at %s [in module %s]"),
16634 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
16635 TYPE_TARGET_TYPE (this_type
) = NULL
;
16639 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16640 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16641 Handle these by just returning the target type, rather than
16642 constructing an anonymous typedef type and trying to handle this
16644 set_die_type (die
, target_type
, cu
);
16645 return target_type
;
16650 /* Helper for get_dwarf2_rational_constant that computes the value of
16651 a given gmp_mpz given an attribute. */
16654 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
16656 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16657 location expression that pushes an implicit value. */
16658 if (attr
->form
== DW_FORM_exprloc
)
16660 dwarf_block
*blk
= attr
->as_block ();
16661 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
16664 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
16665 blk
->data
+ blk
->size
,
16667 if (ptr
- blk
->data
+ len
<= blk
->size
)
16669 mpz_import (value
->val
, len
,
16670 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16676 /* On failure set it to 1. */
16677 *value
= gdb_mpz (1);
16679 else if (attr
->form_is_block ())
16681 dwarf_block
*blk
= attr
->as_block ();
16682 mpz_import (value
->val
, blk
->size
,
16683 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16684 1, 0, 0, blk
->data
);
16687 *value
= gdb_mpz (attr
->constant_value (1));
16690 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16691 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16693 If the numerator and/or numerator attribute is missing,
16694 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16698 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
16699 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
16701 struct attribute
*num_attr
, *denom_attr
;
16703 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
16704 if (num_attr
== nullptr)
16705 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16706 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16708 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
16709 if (denom_attr
== nullptr)
16710 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16711 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16713 if (num_attr
== nullptr || denom_attr
== nullptr)
16716 get_mpz (cu
, numerator
, num_attr
);
16717 get_mpz (cu
, denominator
, denom_attr
);
16720 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16721 rational constant, rather than a signed one.
16723 If the rational constant has a negative value, a complaint
16724 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16727 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
16728 struct dwarf2_cu
*cu
,
16729 gdb_mpz
*numerator
,
16730 gdb_mpz
*denominator
)
16735 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
16736 if (mpz_sgn (num
.val
) == -1 && mpz_sgn (denom
.val
) == -1)
16738 mpz_neg (num
.val
, num
.val
);
16739 mpz_neg (denom
.val
, denom
.val
);
16741 else if (mpz_sgn (num
.val
) == -1)
16743 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16745 sect_offset_str (die
->sect_off
));
16748 else if (mpz_sgn (denom
.val
) == -1)
16750 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16752 sect_offset_str (die
->sect_off
));
16756 *numerator
= std::move (num
);
16757 *denominator
= std::move (denom
);
16760 /* Assuming that ENCODING is a string whose contents starting at the
16761 K'th character is "_nn" where "nn" is a decimal number, scan that
16762 number and set RESULT to the value. K is updated to point to the
16763 character immediately following the number.
16765 If the string does not conform to the format described above, false
16766 is returned, and K may or may not be changed. */
16769 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
16771 /* The next character should be an underscore ('_') followed
16773 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
16776 /* Skip the underscore. */
16780 /* Determine the number of digits for our number. */
16781 while (isdigit (encoding
[k
]))
16786 std::string
copy (&encoding
[start
], k
- start
);
16787 if (mpz_set_str (result
->val
, copy
.c_str (), 10) == -1)
16793 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16794 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16795 DENOM, update OFFSET, and return true on success. Return false on
16799 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
16800 gdb_mpz
*num
, gdb_mpz
*denom
)
16802 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
16804 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
16807 /* Assuming DIE corresponds to a fixed point type, finish the creation
16808 of the corresponding TYPE by setting its type-specific data. CU is
16809 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16810 encodings. It is nullptr if the GNAT encoding should be
16814 finish_fixed_point_type (struct type
*type
, const char *suffix
,
16815 struct die_info
*die
, struct dwarf2_cu
*cu
)
16817 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
16818 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
16820 /* If GNAT encodings are preferred, don't examine the
16822 struct attribute
*attr
= nullptr;
16823 if (suffix
== nullptr)
16825 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
16826 if (attr
== nullptr)
16827 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
16828 if (attr
== nullptr)
16829 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16832 /* Numerator and denominator of our fixed-point type's scaling factor.
16833 The default is a scaling factor of 1, which we use as a fallback
16834 when we are not able to decode it (problem with the debugging info,
16835 unsupported forms, bug in GDB, etc...). Using that as the default
16836 allows us to at least print the unscaled value, which might still
16837 be useful to a user. */
16838 gdb_mpz
scale_num (1);
16839 gdb_mpz
scale_denom (1);
16841 if (attr
== nullptr)
16844 if (suffix
!= nullptr
16845 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16847 /* The number might be encoded as _nn_dd_nn_dd, where the
16848 second ratio is the 'small value. In this situation, we
16849 want the second value. */
16850 && (suffix
[offset
] != '_'
16851 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16858 /* Scaling factor not found. Assume a scaling factor of 1,
16859 and hope for the best. At least the user will be able to
16860 see the encoded value. */
16863 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16864 sect_offset_str (die
->sect_off
));
16867 else if (attr
->name
== DW_AT_binary_scale
)
16869 LONGEST scale_exp
= attr
->constant_value (0);
16870 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16872 mpz_mul_2exp (num_or_denom
->val
, num_or_denom
->val
, std::abs (scale_exp
));
16874 else if (attr
->name
== DW_AT_decimal_scale
)
16876 LONGEST scale_exp
= attr
->constant_value (0);
16877 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16879 mpz_ui_pow_ui (num_or_denom
->val
, 10, std::abs (scale_exp
));
16881 else if (attr
->name
== DW_AT_small
)
16883 struct die_info
*scale_die
;
16884 struct dwarf2_cu
*scale_cu
= cu
;
16886 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
16887 if (scale_die
->tag
== DW_TAG_constant
)
16888 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
16889 &scale_num
, &scale_denom
);
16891 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16893 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16897 complaint (_("unsupported scale attribute %s for fixed-point type"
16899 dwarf_attr_name (attr
->name
),
16900 sect_offset_str (die
->sect_off
));
16903 gdb_mpq
&scaling_factor
= type
->fixed_point_info ().scaling_factor
;
16904 mpz_set (mpq_numref (scaling_factor
.val
), scale_num
.val
);
16905 mpz_set (mpq_denref (scaling_factor
.val
), scale_denom
.val
);
16906 mpq_canonicalize (scaling_factor
.val
);
16909 /* The gnat-encoding suffix for fixed point. */
16911 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16913 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16914 "XF" suffix of the name. The text after this is what encodes the
16915 'small and 'delta information. Otherwise, return nullptr. */
16917 static const char *
16918 gnat_encoded_fixed_point_type_info (const char *name
)
16920 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
16923 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16924 (which may be different from NAME) to the architecture back-end to allow
16925 it to guess the correct format if necessary. */
16927 static struct type
*
16928 dwarf2_init_float_type (struct objfile
*objfile
, int bits
, const char *name
,
16929 const char *name_hint
, enum bfd_endian byte_order
)
16931 struct gdbarch
*gdbarch
= objfile
->arch ();
16932 const struct floatformat
**format
;
16935 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
16937 type
= init_float_type (objfile
, bits
, name
, format
, byte_order
);
16939 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
16944 /* Allocate an integer type of size BITS and name NAME. */
16946 static struct type
*
16947 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, struct objfile
*objfile
,
16948 int bits
, int unsigned_p
, const char *name
)
16952 /* Versions of Intel's C Compiler generate an integer type called "void"
16953 instead of using DW_TAG_unspecified_type. This has been seen on
16954 at least versions 14, 17, and 18. */
16955 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
16956 && strcmp (name
, "void") == 0)
16957 type
= objfile_type (objfile
)->builtin_void
;
16959 type
= init_integer_type (objfile
, bits
, unsigned_p
, name
);
16964 /* Return true if DIE has a DW_AT_small attribute whose value is
16965 a constant rational, where both the numerator and denominator
16968 CU is the DIE's Compilation Unit. */
16971 has_zero_over_zero_small_attribute (struct die_info
*die
,
16972 struct dwarf2_cu
*cu
)
16974 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16975 if (attr
== nullptr)
16978 struct dwarf2_cu
*scale_cu
= cu
;
16979 struct die_info
*scale_die
16980 = follow_die_ref (die
, attr
, &scale_cu
);
16982 if (scale_die
->tag
!= DW_TAG_constant
)
16985 gdb_mpz
num (1), denom (1);
16986 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
16987 return mpz_sgn (num
.val
) == 0 && mpz_sgn (denom
.val
) == 0;
16990 /* Initialise and return a floating point type of size BITS suitable for
16991 use as a component of a complex number. The NAME_HINT is passed through
16992 when initialising the floating point type and is the name of the complex
16995 As DWARF doesn't currently provide an explicit name for the components
16996 of a complex number, but it can be helpful to have these components
16997 named, we try to select a suitable name based on the size of the
16999 static struct type
*
17000 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
17001 struct objfile
*objfile
,
17002 int bits
, const char *name_hint
,
17003 enum bfd_endian byte_order
)
17005 gdbarch
*gdbarch
= objfile
->arch ();
17006 struct type
*tt
= nullptr;
17008 /* Try to find a suitable floating point builtin type of size BITS.
17009 We're going to use the name of this type as the name for the complex
17010 target type that we are about to create. */
17011 switch (cu
->lang ())
17013 case language_fortran
:
17017 tt
= builtin_f_type (gdbarch
)->builtin_real
;
17020 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
17022 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17024 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
17032 tt
= builtin_type (gdbarch
)->builtin_float
;
17035 tt
= builtin_type (gdbarch
)->builtin_double
;
17037 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17039 tt
= builtin_type (gdbarch
)->builtin_long_double
;
17045 /* If the type we found doesn't match the size we were looking for, then
17046 pretend we didn't find a type at all, the complex target type we
17047 create will then be nameless. */
17048 if (tt
!= nullptr && TYPE_LENGTH (tt
) * TARGET_CHAR_BIT
!= bits
)
17051 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
17052 return dwarf2_init_float_type (objfile
, bits
, name
, name_hint
, byte_order
);
17055 /* Find a representation of a given base type and install
17056 it in the TYPE field of the die. */
17058 static struct type
*
17059 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17061 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17063 struct attribute
*attr
;
17064 int encoding
= 0, bits
= 0;
17068 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
17069 if (attr
!= nullptr && attr
->form_is_constant ())
17070 encoding
= attr
->constant_value (0);
17071 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17072 if (attr
!= nullptr)
17073 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
17074 name
= dwarf2_name (die
, cu
);
17076 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17078 arch
= objfile
->arch ();
17079 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
17081 attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
17082 if (attr
!= nullptr && attr
->form_is_constant ())
17084 int endianity
= attr
->constant_value (0);
17089 byte_order
= BFD_ENDIAN_BIG
;
17091 case DW_END_little
:
17092 byte_order
= BFD_ENDIAN_LITTLE
;
17095 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
17100 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
17101 && cu
->lang () == language_ada
17102 && has_zero_over_zero_small_attribute (die
, cu
))
17104 /* brobecker/2018-02-24: This is a fixed point type for which
17105 the scaling factor is represented as fraction whose value
17106 does not make sense (zero divided by zero), so we should
17107 normally never see these. However, there is a small category
17108 of fixed point types for which GNAT is unable to provide
17109 the scaling factor via the standard DWARF mechanisms, and
17110 for which the info is provided via the GNAT encodings instead.
17111 This is likely what this DIE is about. */
17112 encoding
= (encoding
== DW_ATE_signed_fixed
17114 : DW_ATE_unsigned
);
17117 /* With GNAT encodings, fixed-point information will be encoded in
17118 the type name. Note that this can also occur with the above
17119 zero-over-zero case, which is why this is a separate "if" rather
17120 than an "else if". */
17121 const char *gnat_encoding_suffix
= nullptr;
17122 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
17123 && cu
->lang () == language_ada
17124 && name
!= nullptr)
17126 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
17127 if (gnat_encoding_suffix
!= nullptr)
17129 gdb_assert (startswith (gnat_encoding_suffix
,
17130 GNAT_FIXED_POINT_SUFFIX
));
17131 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
17132 name
, gnat_encoding_suffix
- name
);
17133 /* Use -1 here so that SUFFIX points at the "_" after the
17135 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
17137 encoding
= (encoding
== DW_ATE_signed
17138 ? DW_ATE_signed_fixed
17139 : DW_ATE_unsigned_fixed
);
17145 case DW_ATE_address
:
17146 /* Turn DW_ATE_address into a void * pointer. */
17147 type
= init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
17148 type
= init_pointer_type (objfile
, bits
, name
, type
);
17150 case DW_ATE_boolean
:
17151 type
= init_boolean_type (objfile
, bits
, 1, name
);
17153 case DW_ATE_complex_float
:
17154 type
= dwarf2_init_complex_target_type (cu
, objfile
, bits
/ 2, name
,
17156 if (type
->code () == TYPE_CODE_ERROR
)
17158 if (name
== nullptr)
17160 struct obstack
*obstack
17161 = &cu
->per_objfile
->objfile
->objfile_obstack
;
17162 name
= obconcat (obstack
, "_Complex ", type
->name (),
17165 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17168 type
= init_complex_type (name
, type
);
17170 case DW_ATE_decimal_float
:
17171 type
= init_decfloat_type (objfile
, bits
, name
);
17174 type
= dwarf2_init_float_type (objfile
, bits
, name
, name
, byte_order
);
17176 case DW_ATE_signed
:
17177 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17179 case DW_ATE_unsigned
:
17180 if (cu
->lang () == language_fortran
17182 && startswith (name
, "character("))
17183 type
= init_character_type (objfile
, bits
, 1, name
);
17185 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17187 case DW_ATE_signed_char
:
17188 if (cu
->lang () == language_ada
17189 || cu
->lang () == language_m2
17190 || cu
->lang () == language_pascal
17191 || cu
->lang () == language_fortran
)
17192 type
= init_character_type (objfile
, bits
, 0, name
);
17194 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17196 case DW_ATE_unsigned_char
:
17197 if (cu
->lang () == language_ada
17198 || cu
->lang () == language_m2
17199 || cu
->lang () == language_pascal
17200 || cu
->lang () == language_fortran
17201 || cu
->lang () == language_rust
)
17202 type
= init_character_type (objfile
, bits
, 1, name
);
17204 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17208 type
= init_character_type (objfile
, bits
, 1, name
);
17209 return set_die_type (die
, type
, cu
);
17212 case DW_ATE_signed_fixed
:
17213 type
= init_fixed_point_type (objfile
, bits
, 0, name
);
17214 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17216 case DW_ATE_unsigned_fixed
:
17217 type
= init_fixed_point_type (objfile
, bits
, 1, name
);
17218 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17222 complaint (_("unsupported DW_AT_encoding: '%s'"),
17223 dwarf_type_encoding_name (encoding
));
17224 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17228 if (type
->code () == TYPE_CODE_INT
17230 && strcmp (name
, "char") == 0)
17231 type
->set_has_no_signedness (true);
17233 maybe_set_alignment (cu
, die
, type
);
17235 type
->set_endianity_is_not_default (gdbarch_byte_order (arch
) != byte_order
);
17237 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
17239 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
17240 if (attr
!= nullptr && attr
->as_unsigned () <= 8 * TYPE_LENGTH (type
))
17242 unsigned real_bit_size
= attr
->as_unsigned ();
17243 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
17244 /* Only use the attributes if they make sense together. */
17245 if (attr
== nullptr
17246 || (attr
->as_unsigned () + real_bit_size
17247 <= 8 * TYPE_LENGTH (type
)))
17249 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
17251 if (attr
!= nullptr)
17252 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
17253 = attr
->as_unsigned ();
17258 return set_die_type (die
, type
, cu
);
17261 /* A helper function that returns the name of DIE, if it refers to a
17262 variable declaration. */
17264 static const char *
17265 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17267 if (die
->tag
!= DW_TAG_variable
)
17270 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
17271 if (attr
== nullptr || !attr
->as_boolean ())
17274 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
17275 if (attr
== nullptr)
17277 return attr
->as_string ();
17280 /* Parse dwarf attribute if it's a block, reference or constant and put the
17281 resulting value of the attribute into struct bound_prop.
17282 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17285 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
17286 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
17287 struct type
*default_type
)
17289 struct dwarf2_property_baton
*baton
;
17290 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17291 struct objfile
*objfile
= per_objfile
->objfile
;
17292 struct obstack
*obstack
= &objfile
->objfile_obstack
;
17294 gdb_assert (default_type
!= NULL
);
17296 if (attr
== NULL
|| prop
== NULL
)
17299 if (attr
->form_is_block ())
17301 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17302 baton
->property_type
= default_type
;
17303 baton
->locexpr
.per_cu
= cu
->per_cu
;
17304 baton
->locexpr
.per_objfile
= per_objfile
;
17306 struct dwarf_block
*block
;
17307 if (attr
->form
== DW_FORM_data16
)
17309 size_t data_size
= 16;
17310 block
= XOBNEW (obstack
, struct dwarf_block
);
17311 block
->size
= (data_size
17312 + 2 /* Extra bytes for DW_OP and arg. */);
17313 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
->size
);
17314 data
[0] = DW_OP_implicit_value
;
17315 data
[1] = data_size
;
17316 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
17317 block
->data
= data
;
17320 block
= attr
->as_block ();
17322 baton
->locexpr
.size
= block
->size
;
17323 baton
->locexpr
.data
= block
->data
;
17324 switch (attr
->name
)
17326 case DW_AT_string_length
:
17327 baton
->locexpr
.is_reference
= true;
17330 baton
->locexpr
.is_reference
= false;
17334 prop
->set_locexpr (baton
);
17335 gdb_assert (prop
->baton () != NULL
);
17337 else if (attr
->form_is_ref ())
17339 struct dwarf2_cu
*target_cu
= cu
;
17340 struct die_info
*target_die
;
17341 struct attribute
*target_attr
;
17343 target_die
= follow_die_ref (die
, attr
, &target_cu
);
17344 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
17345 if (target_attr
== NULL
)
17346 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
17348 if (target_attr
== nullptr)
17349 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
17351 if (target_attr
== NULL
)
17353 const char *name
= var_decl_name (target_die
, target_cu
);
17354 if (name
!= nullptr)
17356 prop
->set_variable_name (name
);
17362 switch (target_attr
->name
)
17364 case DW_AT_location
:
17365 if (target_attr
->form_is_section_offset ())
17367 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17368 baton
->property_type
= die_type (target_die
, target_cu
);
17369 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
17370 prop
->set_loclist (baton
);
17371 gdb_assert (prop
->baton () != NULL
);
17373 else if (target_attr
->form_is_block ())
17375 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17376 baton
->property_type
= die_type (target_die
, target_cu
);
17377 baton
->locexpr
.per_cu
= cu
->per_cu
;
17378 baton
->locexpr
.per_objfile
= per_objfile
;
17379 struct dwarf_block
*block
= target_attr
->as_block ();
17380 baton
->locexpr
.size
= block
->size
;
17381 baton
->locexpr
.data
= block
->data
;
17382 baton
->locexpr
.is_reference
= true;
17383 prop
->set_locexpr (baton
);
17384 gdb_assert (prop
->baton () != NULL
);
17388 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17389 "dynamic property");
17393 case DW_AT_data_member_location
:
17394 case DW_AT_data_bit_offset
:
17398 if (!handle_member_location (target_die
, target_cu
, &offset
))
17401 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17402 baton
->property_type
= read_type_die (target_die
->parent
,
17404 baton
->offset_info
.offset
= offset
;
17405 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
17406 prop
->set_addr_offset (baton
);
17411 else if (attr
->form_is_constant ())
17412 prop
->set_const_val (attr
->constant_value (0));
17413 else if (attr
->form_is_section_offset ())
17415 switch (attr
->name
)
17417 case DW_AT_string_length
:
17418 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17419 baton
->property_type
= default_type
;
17420 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
17421 prop
->set_loclist (baton
);
17422 gdb_assert (prop
->baton () != NULL
);
17434 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
17435 dwarf2_name (die
, cu
));
17441 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17442 present (which is valid) then compute the default type based on the
17443 compilation units address size. */
17445 static struct type
*
17446 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17448 struct type
*index_type
= die_type (die
, cu
);
17450 /* Dwarf-2 specifications explicitly allows to create subrange types
17451 without specifying a base type.
17452 In that case, the base type must be set to the type of
17453 the lower bound, upper bound or count, in that order, if any of these
17454 three attributes references an object that has a type.
17455 If no base type is found, the Dwarf-2 specifications say that
17456 a signed integer type of size equal to the size of an address should
17458 For the following C code: `extern char gdb_int [];'
17459 GCC produces an empty range DIE.
17460 FIXME: muller/2010-05-28: Possible references to object for low bound,
17461 high bound or count are not yet handled by this code. */
17462 if (index_type
->code () == TYPE_CODE_VOID
)
17463 index_type
= cu
->addr_sized_int_type (false);
17468 /* Read the given DW_AT_subrange DIE. */
17470 static struct type
*
17471 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17473 struct type
*base_type
, *orig_base_type
;
17474 struct type
*range_type
;
17475 struct attribute
*attr
;
17476 struct dynamic_prop low
, high
;
17477 int low_default_is_valid
;
17478 int high_bound_is_count
= 0;
17480 ULONGEST negative_mask
;
17482 orig_base_type
= read_subrange_index_type (die
, cu
);
17484 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17485 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17486 creating the range type, but we use the result of check_typedef
17487 when examining properties of the type. */
17488 base_type
= check_typedef (orig_base_type
);
17490 /* The die_type call above may have already set the type for this DIE. */
17491 range_type
= get_die_type (die
, cu
);
17495 high
.set_const_val (0);
17497 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17498 omitting DW_AT_lower_bound. */
17499 switch (cu
->lang ())
17502 case language_cplus
:
17503 low
.set_const_val (0);
17504 low_default_is_valid
= 1;
17506 case language_fortran
:
17507 low
.set_const_val (1);
17508 low_default_is_valid
= 1;
17511 case language_objc
:
17512 case language_rust
:
17513 low
.set_const_val (0);
17514 low_default_is_valid
= (cu
->header
.version
>= 4);
17518 case language_pascal
:
17519 low
.set_const_val (1);
17520 low_default_is_valid
= (cu
->header
.version
>= 4);
17523 low
.set_const_val (0);
17524 low_default_is_valid
= 0;
17528 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
17529 if (attr
!= nullptr)
17530 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
17531 else if (!low_default_is_valid
)
17532 complaint (_("Missing DW_AT_lower_bound "
17533 "- DIE at %s [in module %s]"),
17534 sect_offset_str (die
->sect_off
),
17535 objfile_name (cu
->per_objfile
->objfile
));
17537 struct attribute
*attr_ub
, *attr_count
;
17538 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
17539 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17541 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
17542 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17544 /* If bounds are constant do the final calculation here. */
17545 if (low
.kind () == PROP_CONST
&& high
.kind () == PROP_CONST
)
17546 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
17548 high_bound_is_count
= 1;
17552 if (attr_ub
!= NULL
)
17553 complaint (_("Unresolved DW_AT_upper_bound "
17554 "- DIE at %s [in module %s]"),
17555 sect_offset_str (die
->sect_off
),
17556 objfile_name (cu
->per_objfile
->objfile
));
17557 if (attr_count
!= NULL
)
17558 complaint (_("Unresolved DW_AT_count "
17559 "- DIE at %s [in module %s]"),
17560 sect_offset_str (die
->sect_off
),
17561 objfile_name (cu
->per_objfile
->objfile
));
17566 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
17567 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
17568 bias
= bias_attr
->constant_value (0);
17570 /* Normally, the DWARF producers are expected to use a signed
17571 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17572 But this is unfortunately not always the case, as witnessed
17573 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17574 is used instead. To work around that ambiguity, we treat
17575 the bounds as signed, and thus sign-extend their values, when
17576 the base type is signed. */
17578 -((ULONGEST
) 1 << (TYPE_LENGTH (base_type
) * TARGET_CHAR_BIT
- 1));
17579 if (low
.kind () == PROP_CONST
17580 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
17581 low
.set_const_val (low
.const_val () | negative_mask
);
17582 if (high
.kind () == PROP_CONST
17583 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
17584 high
.set_const_val (high
.const_val () | negative_mask
);
17586 /* Check for bit and byte strides. */
17587 struct dynamic_prop byte_stride_prop
;
17588 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
17589 if (attr_byte_stride
!= nullptr)
17591 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17592 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
17596 struct dynamic_prop bit_stride_prop
;
17597 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
17598 if (attr_bit_stride
!= nullptr)
17600 /* It only makes sense to have either a bit or byte stride. */
17601 if (attr_byte_stride
!= nullptr)
17603 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17604 "- DIE at %s [in module %s]"),
17605 sect_offset_str (die
->sect_off
),
17606 objfile_name (cu
->per_objfile
->objfile
));
17607 attr_bit_stride
= nullptr;
17611 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17612 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
17617 if (attr_byte_stride
!= nullptr
17618 || attr_bit_stride
!= nullptr)
17620 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
17621 struct dynamic_prop
*stride
17622 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
17625 = create_range_type_with_stride (NULL
, orig_base_type
, &low
,
17626 &high
, bias
, stride
, byte_stride_p
);
17629 range_type
= create_range_type (NULL
, orig_base_type
, &low
, &high
, bias
);
17631 if (high_bound_is_count
)
17632 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
17634 /* Ada expects an empty array on no boundary attributes. */
17635 if (attr
== NULL
&& cu
->lang () != language_ada
)
17636 range_type
->bounds ()->high
.set_undefined ();
17638 name
= dwarf2_name (die
, cu
);
17640 range_type
->set_name (name
);
17642 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17643 if (attr
!= nullptr)
17644 TYPE_LENGTH (range_type
) = attr
->constant_value (0);
17646 maybe_set_alignment (cu
, die
, range_type
);
17648 set_die_type (die
, range_type
, cu
);
17650 /* set_die_type should be already done. */
17651 set_descriptive_type (range_type
, die
, cu
);
17656 static struct type
*
17657 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17661 type
= init_type (cu
->per_objfile
->objfile
, TYPE_CODE_VOID
, 0, NULL
);
17662 type
->set_name (dwarf2_name (die
, cu
));
17664 /* In Ada, an unspecified type is typically used when the description
17665 of the type is deferred to a different unit. When encountering
17666 such a type, we treat it as a stub, and try to resolve it later on,
17668 if (cu
->lang () == language_ada
)
17669 type
->set_is_stub (true);
17671 return set_die_type (die
, type
, cu
);
17674 /* Read a single die and all its descendents. Set the die's sibling
17675 field to NULL; set other fields in the die correctly, and set all
17676 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17677 location of the info_ptr after reading all of those dies. PARENT
17678 is the parent of the die in question. */
17680 static struct die_info
*
17681 read_die_and_children (const struct die_reader_specs
*reader
,
17682 const gdb_byte
*info_ptr
,
17683 const gdb_byte
**new_info_ptr
,
17684 struct die_info
*parent
)
17686 struct die_info
*die
;
17687 const gdb_byte
*cur_ptr
;
17689 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0);
17692 *new_info_ptr
= cur_ptr
;
17695 store_in_ref_table (die
, reader
->cu
);
17697 if (die
->has_children
)
17698 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
17702 *new_info_ptr
= cur_ptr
;
17705 die
->sibling
= NULL
;
17706 die
->parent
= parent
;
17710 /* Read a die, all of its descendents, and all of its siblings; set
17711 all of the fields of all of the dies correctly. Arguments are as
17712 in read_die_and_children. */
17714 static struct die_info
*
17715 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
17716 const gdb_byte
*info_ptr
,
17717 const gdb_byte
**new_info_ptr
,
17718 struct die_info
*parent
)
17720 struct die_info
*first_die
, *last_sibling
;
17721 const gdb_byte
*cur_ptr
;
17723 cur_ptr
= info_ptr
;
17724 first_die
= last_sibling
= NULL
;
17728 struct die_info
*die
17729 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
17733 *new_info_ptr
= cur_ptr
;
17740 last_sibling
->sibling
= die
;
17742 last_sibling
= die
;
17746 /* Read a die, all of its descendents, and all of its siblings; set
17747 all of the fields of all of the dies correctly. Arguments are as
17748 in read_die_and_children.
17749 This the main entry point for reading a DIE and all its children. */
17751 static struct die_info
*
17752 read_die_and_siblings (const struct die_reader_specs
*reader
,
17753 const gdb_byte
*info_ptr
,
17754 const gdb_byte
**new_info_ptr
,
17755 struct die_info
*parent
)
17757 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
17758 new_info_ptr
, parent
);
17760 if (dwarf_die_debug
)
17762 gdb_printf (gdb_stdlog
,
17763 "Read die from %s@0x%x of %s:\n",
17764 reader
->die_section
->get_name (),
17765 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17766 bfd_get_filename (reader
->abfd
));
17767 dump_die (die
, dwarf_die_debug
);
17773 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17775 The caller is responsible for filling in the extra attributes
17776 and updating (*DIEP)->num_attrs.
17777 Set DIEP to point to a newly allocated die with its information,
17778 except for its child, sibling, and parent fields. */
17780 static const gdb_byte
*
17781 read_full_die_1 (const struct die_reader_specs
*reader
,
17782 struct die_info
**diep
, const gdb_byte
*info_ptr
,
17783 int num_extra_attrs
)
17785 unsigned int abbrev_number
, bytes_read
, i
;
17786 const struct abbrev_info
*abbrev
;
17787 struct die_info
*die
;
17788 struct dwarf2_cu
*cu
= reader
->cu
;
17789 bfd
*abfd
= reader
->abfd
;
17791 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
17792 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17793 info_ptr
+= bytes_read
;
17794 if (!abbrev_number
)
17800 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
17802 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17804 bfd_get_filename (abfd
));
17806 die
= dwarf_alloc_die (cu
, abbrev
->num_attrs
+ num_extra_attrs
);
17807 die
->sect_off
= sect_off
;
17808 die
->tag
= abbrev
->tag
;
17809 die
->abbrev
= abbrev_number
;
17810 die
->has_children
= abbrev
->has_children
;
17812 /* Make the result usable.
17813 The caller needs to update num_attrs after adding the extra
17815 die
->num_attrs
= abbrev
->num_attrs
;
17817 bool any_need_reprocess
= false;
17818 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17820 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
17822 if (die
->attrs
[i
].requires_reprocessing_p ())
17823 any_need_reprocess
= true;
17826 struct attribute
*attr
= die
->attr (DW_AT_str_offsets_base
);
17827 if (attr
!= nullptr && attr
->form_is_unsigned ())
17828 cu
->str_offsets_base
= attr
->as_unsigned ();
17830 attr
= die
->attr (DW_AT_loclists_base
);
17831 if (attr
!= nullptr)
17832 cu
->loclist_base
= attr
->as_unsigned ();
17834 auto maybe_addr_base
= die
->addr_base ();
17835 if (maybe_addr_base
.has_value ())
17836 cu
->addr_base
= *maybe_addr_base
;
17838 attr
= die
->attr (DW_AT_rnglists_base
);
17839 if (attr
!= nullptr)
17840 cu
->rnglists_base
= attr
->as_unsigned ();
17842 if (any_need_reprocess
)
17844 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17846 if (die
->attrs
[i
].requires_reprocessing_p ())
17847 read_attribute_reprocess (reader
, &die
->attrs
[i
], die
->tag
);
17854 /* Read a die and all its attributes.
17855 Set DIEP to point to a newly allocated die with its information,
17856 except for its child, sibling, and parent fields. */
17858 static const gdb_byte
*
17859 read_full_die (const struct die_reader_specs
*reader
,
17860 struct die_info
**diep
, const gdb_byte
*info_ptr
)
17862 const gdb_byte
*result
;
17864 result
= read_full_die_1 (reader
, diep
, info_ptr
, 0);
17866 if (dwarf_die_debug
)
17868 gdb_printf (gdb_stdlog
,
17869 "Read die from %s@0x%x of %s:\n",
17870 reader
->die_section
->get_name (),
17871 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17872 bfd_get_filename (reader
->abfd
));
17873 dump_die (*diep
, dwarf_die_debug
);
17881 cooked_indexer::check_bounds (cutu_reader
*reader
)
17883 if (reader
->cu
->per_cu
->addresses_seen
)
17886 dwarf2_cu
*cu
= reader
->cu
;
17888 CORE_ADDR best_lowpc
= 0, best_highpc
= 0;
17889 /* Possibly set the default values of LOWPC and HIGHPC from
17891 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
17892 enum pc_bounds_kind cu_bounds_kind
17893 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
17894 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
17895 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
17897 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17898 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
17899 struct gdbarch
*gdbarch
= objfile
->arch ();
17901 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_lowpc
+ baseaddr
)
17904 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_highpc
+ baseaddr
)
17906 /* Store the contiguous range if it is not empty; it can be
17907 empty for CUs with no code. */
17908 m_index_storage
->get_addrmap ()->set_empty (low
, high
, cu
->per_cu
);
17910 cu
->per_cu
->addresses_seen
= true;
17914 /* Helper function that returns true if TAG can have a linkage
17918 tag_can_have_linkage_name (enum dwarf_tag tag
)
17922 /* We include types here because an anonymous C++ type might
17923 have a name for linkage purposes. */
17924 case DW_TAG_class_type
:
17925 case DW_TAG_structure_type
:
17926 case DW_TAG_union_type
:
17927 case DW_TAG_variable
:
17928 case DW_TAG_subprogram
:
17937 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
17938 dwarf2_per_objfile
*per_objfile
,
17939 sect_offset sect_off
, bool is_dwz
,
17942 /* Lookups for type unit references are always in the CU, and
17943 cross-CU references will crash. */
17944 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
17945 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
17948 dwarf2_per_cu_data
*per_cu
17949 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
17950 per_objfile
->per_bfd
);
17952 /* When scanning, we only want to visit a given CU a single time.
17953 Doing this check here avoids self-imports as well. */
17957 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
17960 if (per_cu
== m_per_cu
)
17963 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
17964 if (result
== nullptr)
17966 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
17967 m_index_storage
->get_abbrev_cache ());
17969 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
17971 std::unique_ptr
<cutu_reader
> copy
17972 (new cutu_reader (std::move (new_reader
)));
17973 result
= m_index_storage
->preserve (std::move (copy
));
17976 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
17980 check_bounds (result
);
17986 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
17987 cutu_reader
*reader
,
17988 const gdb_byte
*watermark_ptr
,
17989 const gdb_byte
*info_ptr
,
17990 const abbrev_info
*abbrev
,
17992 const char **linkage_name
,
17993 cooked_index_flag
*flags
,
17994 sect_offset
*sibling_offset
,
17995 const cooked_index_entry
**parent_entry
,
17996 CORE_ADDR
*maybe_defer
,
17997 bool for_specification
)
17999 bool origin_is_dwz
= false;
18000 bool is_declaration
= false;
18001 sect_offset origin_offset
{};
18003 gdb::optional
<CORE_ADDR
> low_pc
;
18004 gdb::optional
<CORE_ADDR
> high_pc
;
18005 bool high_pc_relative
= false;
18007 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18010 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18011 if (attr
.requires_reprocessing_p ())
18012 read_attribute_reprocess (reader
, &attr
, abbrev
->tag
);
18014 /* Store the data if it is of an attribute we want to keep in a
18015 partial symbol table. */
18019 switch (abbrev
->tag
)
18021 case DW_TAG_compile_unit
:
18022 case DW_TAG_partial_unit
:
18023 case DW_TAG_type_unit
:
18024 /* Compilation units have a DW_AT_name that is a filename, not
18025 a source language identifier. */
18029 if (*name
== nullptr)
18030 *name
= attr
.as_string ();
18035 case DW_AT_linkage_name
:
18036 case DW_AT_MIPS_linkage_name
:
18037 /* Note that both forms of linkage name might appear. We
18038 assume they will be the same, and we only store the last
18040 if (*linkage_name
== nullptr)
18041 *linkage_name
= attr
.as_string ();
18044 case DW_AT_main_subprogram
:
18045 if (attr
.as_boolean ())
18049 case DW_AT_declaration
:
18050 is_declaration
= attr
.as_boolean ();
18053 case DW_AT_sibling
:
18054 if (sibling_offset
!= nullptr)
18055 *sibling_offset
= attr
.get_ref_die_offset ();
18058 case DW_AT_specification
:
18059 case DW_AT_abstract_origin
:
18060 case DW_AT_extension
:
18061 origin_offset
= attr
.get_ref_die_offset ();
18062 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
18065 case DW_AT_external
:
18066 if (attr
.as_boolean ())
18067 *flags
&= ~IS_STATIC
;
18070 case DW_AT_enum_class
:
18071 if (attr
.as_boolean ())
18072 *flags
|= IS_ENUM_CLASS
;
18076 low_pc
= attr
.as_address ();
18079 case DW_AT_high_pc
:
18080 high_pc
= attr
.as_address ();
18081 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
18082 high_pc_relative
= true;
18085 case DW_AT_location
:
18086 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
18088 struct dwarf_block
*locdesc
= attr
.as_block ();
18089 CORE_ADDR addr
= decode_locdesc (locdesc
, reader
->cu
);
18091 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
)
18094 /* For variables, we don't want to try decoding the
18095 type just to find the size -- for gdb's purposes
18096 we only need the address of a variable. */
18097 high_pc
= addr
+ 1;
18098 high_pc_relative
= false;
18104 if (!scanning_per_cu
->addresses_seen
)
18106 /* Offset in the .debug_ranges or .debug_rnglist section
18107 (depending on DWARF version). */
18108 ULONGEST ranges_offset
= attr
.as_unsigned ();
18110 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18111 want to add this value. */
18112 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
18114 CORE_ADDR lowpc
, highpc
;
18115 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
18116 m_index_storage
->get_addrmap (),
18117 scanning_per_cu
, abbrev
->tag
);
18123 /* We don't want to examine declarations, but if we found a
18124 declaration when handling DW_AT_specification or the like, then
18125 that is ok. Similarly, we allow an external variable without a
18126 location; those are resolved via minimal symbols. */
18127 if (is_declaration
&& !for_specification
18128 && !(abbrev
->tag
== DW_TAG_variable
&& (*flags
& IS_STATIC
) == 0)
18129 && !((abbrev
->tag
== DW_TAG_class_type
18130 || abbrev
->tag
== DW_TAG_structure_type
18131 || abbrev
->tag
== DW_TAG_union_type
)
18132 && abbrev
->has_children
))
18134 *linkage_name
= nullptr;
18137 else if ((*name
== nullptr
18138 || (*linkage_name
== nullptr
18139 && tag_can_have_linkage_name (abbrev
->tag
))
18140 || (*parent_entry
== nullptr && m_language
!= language_c
))
18141 && origin_offset
!= sect_offset (0))
18143 cutu_reader
*new_reader
18144 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
18145 origin_is_dwz
, false);
18146 if (new_reader
!= nullptr)
18148 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
18149 + to_underlying (origin_offset
));
18151 if (new_reader
->cu
== reader
->cu
18152 && new_info_ptr
> watermark_ptr
18153 && maybe_defer
!= nullptr
18154 && *parent_entry
== nullptr)
18155 *maybe_defer
= form_addr (origin_offset
, origin_is_dwz
);
18156 else if (*parent_entry
== nullptr)
18158 CORE_ADDR lookup
= form_addr (origin_offset
, origin_is_dwz
);
18160 = (cooked_index_entry
*) m_die_range_map
.find (lookup
);
18163 unsigned int bytes_read
;
18164 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
18167 new_info_ptr
+= bytes_read
;
18168 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
, new_info_ptr
,
18169 new_abbrev
, name
, linkage_name
, flags
, nullptr,
18170 parent_entry
, maybe_defer
, true);
18174 if (!for_specification
)
18176 if (m_language
== language_ada
18177 && *linkage_name
== nullptr)
18178 *linkage_name
= *name
;
18180 if (!scanning_per_cu
->addresses_seen
18181 && low_pc
.has_value ()
18182 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
18184 && high_pc
.has_value ())
18186 if (high_pc_relative
)
18187 high_pc
= *high_pc
+ *low_pc
;
18189 if (*high_pc
> *low_pc
)
18191 struct objfile
*objfile
= reader
->cu
->per_objfile
->objfile
;
18192 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18193 struct gdbarch
*gdbarch
= objfile
->arch ();
18195 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *low_pc
+ baseaddr
)
18198 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *high_pc
+ baseaddr
)
18200 m_index_storage
->get_addrmap ()->set_empty (lo
, hi
- 1,
18205 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
18206 *flags
&= ~IS_STATIC
;
18208 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
18209 *name
= "(anonymous namespace)";
18211 if (m_language
== language_cplus
18212 && (abbrev
->tag
== DW_TAG_class_type
18213 || abbrev
->tag
== DW_TAG_interface_type
18214 || abbrev
->tag
== DW_TAG_structure_type
18215 || abbrev
->tag
== DW_TAG_union_type
18216 || abbrev
->tag
== DW_TAG_enumeration_type
18217 || abbrev
->tag
== DW_TAG_enumerator
))
18218 *flags
&= ~IS_STATIC
;
18225 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
18226 const gdb_byte
*info_ptr
,
18227 const abbrev_info
*abbrev
)
18229 sect_offset sect_off
{};
18230 bool is_dwz
= false;
18232 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18234 /* Note that we never need to reprocess attributes here. */
18236 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18238 if (attr
.name
== DW_AT_import
)
18240 sect_off
= attr
.get_ref_die_offset ();
18241 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
18242 || reader
->cu
->per_cu
->is_dwz
);
18246 /* Did not find DW_AT_import. */
18247 if (sect_off
== sect_offset (0))
18250 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
18251 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
18253 if (new_reader
!= nullptr)
18255 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
18257 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
18264 cooked_indexer::recurse (cutu_reader
*reader
,
18265 const gdb_byte
*info_ptr
,
18266 const cooked_index_entry
*parent_entry
,
18269 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18271 if (parent_entry
!= nullptr)
18273 CORE_ADDR start
= form_addr (parent_entry
->die_offset
,
18274 reader
->cu
->per_cu
->is_dwz
);
18275 CORE_ADDR end
= form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
18276 reader
->cu
->per_cu
->is_dwz
);
18277 m_die_range_map
.set_empty (start
, end
, (void *) parent_entry
);
18284 cooked_indexer::index_dies (cutu_reader
*reader
,
18285 const gdb_byte
*info_ptr
,
18286 const cooked_index_entry
*parent_entry
,
18289 const gdb_byte
*end_ptr
= (reader
->buffer
18290 + to_underlying (reader
->cu
->header
.sect_off
)
18291 + reader
->cu
->header
.get_length ());
18293 while (info_ptr
< end_ptr
)
18295 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
18296 unsigned int bytes_read
;
18297 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
18299 info_ptr
+= bytes_read
;
18300 if (abbrev
== nullptr)
18303 if (abbrev
->tag
== DW_TAG_imported_unit
)
18305 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
18309 if (!abbrev
->interesting
)
18311 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
18312 if (fully
&& abbrev
->has_children
)
18313 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18317 const char *name
= nullptr;
18318 const char *linkage_name
= nullptr;
18319 CORE_ADDR defer
= 0;
18320 cooked_index_flag flags
= IS_STATIC
;
18321 sect_offset sibling
{};
18322 const cooked_index_entry
*this_parent_entry
= parent_entry
;
18323 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
18324 info_ptr
, abbrev
, &name
, &linkage_name
,
18325 &flags
, &sibling
, &this_parent_entry
,
18328 if (abbrev
->tag
== DW_TAG_namespace
18329 && m_language
== language_cplus
18330 && strcmp (name
, "::") == 0)
18332 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18333 generated bogus DW_TAG_namespace DIEs with a name of "::"
18334 for the global namespace. Work around this problem
18339 const cooked_index_entry
*this_entry
= nullptr;
18340 if (name
!= nullptr)
18343 m_deferred_entries
.push_back ({
18344 this_die
, name
, defer
, abbrev
->tag
, flags
18347 this_entry
= m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18348 name
, this_parent_entry
,
18352 if (linkage_name
!= nullptr)
18354 /* We only want this to be "main" if it has a linkage name
18355 but not an ordinary name. */
18356 if (name
!= nullptr)
18357 flags
= flags
& ~IS_MAIN
;
18358 /* Set the IS_LINKAGE on for everything except when functions
18359 have linkage name present but name is absent. */
18360 if (name
!= nullptr
18361 || (abbrev
->tag
!= DW_TAG_subprogram
18362 && abbrev
->tag
!= DW_TAG_inlined_subroutine
18363 && abbrev
->tag
!= DW_TAG_entry_point
))
18364 flags
= flags
| IS_LINKAGE
;
18365 m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18366 linkage_name
, nullptr, m_per_cu
);
18369 if (abbrev
->has_children
)
18371 switch (abbrev
->tag
)
18373 case DW_TAG_class_type
:
18374 case DW_TAG_interface_type
:
18375 case DW_TAG_structure_type
:
18376 case DW_TAG_union_type
:
18377 if (m_language
!= language_c
&& this_entry
!= nullptr)
18379 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18384 case DW_TAG_enumeration_type
:
18385 /* We need to recurse even for an anonymous enumeration.
18386 Which scope we record as the parent scope depends on
18387 whether we're reading an "enum class". If so, we use
18388 the enum itself as the parent, yielding names like
18389 "enum_class::enumerator"; otherwise we inject the
18390 names into our own parent scope. */
18391 info_ptr
= recurse (reader
, info_ptr
,
18392 ((flags
& IS_ENUM_CLASS
) == 0)
18398 case DW_TAG_module
:
18399 if (this_entry
== nullptr)
18402 case DW_TAG_namespace
:
18403 /* We don't check THIS_ENTRY for a namespace, to handle
18404 the ancient G++ workaround pointed out above. */
18405 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18408 case DW_TAG_subprogram
:
18409 if ((m_language
== language_fortran
18410 || m_language
== language_ada
)
18411 && this_entry
!= nullptr)
18413 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
18419 if (sibling
!= sect_offset (0))
18421 const gdb_byte
*sibling_ptr
18422 = reader
->buffer
+ to_underlying (sibling
);
18424 if (sibling_ptr
< info_ptr
)
18425 complaint (_("DW_AT_sibling points backwards"));
18426 else if (sibling_ptr
> reader
->buffer_end
)
18427 reader
->die_section
->overflow_complaint ();
18429 info_ptr
= sibling_ptr
;
18432 info_ptr
= skip_children (reader
, info_ptr
);
18440 cooked_indexer::make_index (cutu_reader
*reader
)
18442 check_bounds (reader
);
18443 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
18444 if (!reader
->comp_unit_die
->has_children
)
18446 index_dies (reader
, reader
->info_ptr
, nullptr, false);
18448 for (const auto &entry
: m_deferred_entries
)
18450 CORE_ADDR key
= form_addr (entry
.die_offset
, m_per_cu
->is_dwz
);
18451 cooked_index_entry
*parent
18452 = (cooked_index_entry
*) m_die_range_map
.find (key
);
18453 m_index_storage
->add (entry
.die_offset
, entry
.tag
, entry
.flags
,
18454 entry
.name
, parent
, m_per_cu
);
18458 /* An implementation of quick_symbol_functions for the cooked DWARF
18461 struct cooked_index_functions
: public dwarf2_base_index_functions
18463 dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
18464 CORE_ADDR adjusted_pc
) override
;
18466 struct compunit_symtab
*find_compunit_symtab_by_address
18467 (struct objfile
*objfile
, CORE_ADDR address
) override
;
18469 void dump (struct objfile
*objfile
) override
18471 gdb_printf ("Cooked index in use\n");
18474 void expand_matching_symbols
18476 const lookup_name_info
&lookup_name
,
18477 domain_enum domain
,
18479 symbol_compare_ftype
*ordered_compare
) override
;
18481 bool expand_symtabs_matching
18482 (struct objfile
*objfile
,
18483 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18484 const lookup_name_info
*lookup_name
,
18485 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18486 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18487 block_search_flags search_flags
,
18488 domain_enum domain
,
18489 enum search_domain kind
) override
;
18491 bool can_lazily_read_symbols () override
18496 void read_partial_symbols (struct objfile
*objfile
) override
18498 if (dwarf2_has_info (objfile
, nullptr))
18499 dwarf2_build_psymtabs (objfile
);
18503 dwarf2_per_cu_data
*
18504 cooked_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
18505 CORE_ADDR adjusted_pc
)
18507 cooked_index_vector
*table
18508 = (static_cast<cooked_index_vector
*>
18509 (per_bfd
->index_table
.get ()));
18510 if (table
== nullptr)
18512 return table
->lookup (adjusted_pc
);
18515 struct compunit_symtab
*
18516 cooked_index_functions::find_compunit_symtab_by_address
18517 (struct objfile
*objfile
, CORE_ADDR address
)
18519 if (objfile
->sect_index_data
== -1)
18522 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18523 if (per_objfile
->per_bfd
->index_table
== nullptr)
18526 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
18527 cooked_index_vector
*table
18528 = (static_cast<cooked_index_vector
*>
18529 (per_objfile
->per_bfd
->index_table
.get ()));
18530 dwarf2_per_cu_data
*per_cu
= table
->lookup (address
- baseaddr
);
18531 if (per_cu
== nullptr)
18534 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
18538 cooked_index_functions::expand_matching_symbols
18539 (struct objfile
*objfile
,
18540 const lookup_name_info
&lookup_name
,
18541 domain_enum domain
,
18543 symbol_compare_ftype
*ordered_compare
)
18545 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18546 if (per_objfile
->per_bfd
->index_table
== nullptr)
18548 const block_search_flags search_flags
= (global
18549 ? SEARCH_GLOBAL_BLOCK
18550 : SEARCH_STATIC_BLOCK
);
18551 const language_defn
*lang
= language_def (language_ada
);
18552 symbol_name_matcher_ftype
*name_match
18553 = lang
->get_symbol_name_matcher (lookup_name
);
18555 cooked_index_vector
*table
18556 = (static_cast<cooked_index_vector
*>
18557 (per_objfile
->per_bfd
->index_table
.get ()));
18558 for (const cooked_index_entry
*entry
: table
->all_entries ())
18560 if (entry
->parent_entry
!= nullptr)
18563 if (!entry
->matches (search_flags
)
18564 || !entry
->matches (domain
))
18567 if (name_match (entry
->canonical
, lookup_name
, nullptr))
18568 dw2_instantiate_symtab (entry
->per_cu
, per_objfile
, false);
18573 cooked_index_functions::expand_symtabs_matching
18574 (struct objfile
*objfile
,
18575 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18576 const lookup_name_info
*lookup_name
,
18577 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18578 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18579 block_search_flags search_flags
,
18580 domain_enum domain
,
18581 enum search_domain kind
)
18583 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18584 if (per_objfile
->per_bfd
->index_table
== nullptr)
18587 cooked_index_vector
*table
18588 = (static_cast<cooked_index_vector
*>
18589 (per_objfile
->per_bfd
->index_table
.get ()));
18592 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
18594 /* This invariant is documented in quick-functions.h. */
18595 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
18596 if (lookup_name
== nullptr)
18598 for (dwarf2_per_cu_data
*per_cu
18599 : all_comp_units_range (per_objfile
->per_bfd
))
18603 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
18611 lookup_name_info lookup_name_without_params
18612 = lookup_name
->make_ignore_params ();
18613 bool completing
= lookup_name
->completion_mode ();
18615 /* Unique styles of language splitting. */
18616 static const enum language unique_styles
[] =
18618 /* No splitting is also a style. */
18620 /* This includes Rust. */
18622 /* This includes Go. */
18627 for (enum language lang
: unique_styles
)
18629 std::vector
<gdb::string_view
> name_vec
18630 = lookup_name_without_params
.split_name (lang
);
18632 for (const cooked_index_entry
*entry
: table
->find (name_vec
.back (),
18635 /* No need to consider symbols from expanded CUs. */
18636 if (per_objfile
->symtab_set_p (entry
->per_cu
))
18639 /* If file-matching was done, we don't need to consider
18640 symbols from unmarked CUs. */
18641 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
18644 /* See if the symbol matches the type filter. */
18645 if (!entry
->matches (search_flags
)
18646 || !entry
->matches (domain
)
18647 || !entry
->matches (kind
))
18650 /* We've found the base name of the symbol; now walk its
18651 parentage chain, ensuring that each component
18655 const cooked_index_entry
*parent
= entry
->parent_entry
;
18656 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
18658 /* If we ran out of entries, or if this segment doesn't
18659 match, this did not match. */
18660 if (parent
== nullptr
18661 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
18662 name_vec
[i
- 1].length ()) != 0)
18668 parent
= parent
->parent_entry
;
18674 /* Might have been looking for "a::b" and found
18676 if (symbol_matcher
== nullptr)
18678 symbol_name_match_type match_type
18679 = lookup_name_without_params
.match_type ();
18680 if ((match_type
== symbol_name_match_type::FULL
18681 || (lang
!= language_ada
18682 && match_type
== symbol_name_match_type::EXPRESSION
))
18683 && parent
!= nullptr)
18688 auto_obstack temp_storage
;
18689 const char *full_name
= entry
->full_name (&temp_storage
);
18690 if (!symbol_matcher (full_name
))
18694 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
18704 /* Return a new cooked_index_functions object. */
18706 static quick_symbol_functions_up
18707 make_cooked_index_funcs ()
18709 return quick_symbol_functions_up (new cooked_index_functions
);
18712 quick_symbol_functions_up
18713 cooked_index_vector::make_quick_functions () const
18715 return make_cooked_index_funcs ();
18720 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18721 contents from the given SECTION in the HEADER.
18723 HEADER_OFFSET is the offset of the header in the section. */
18725 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
18726 struct dwarf2_section_info
*section
,
18727 sect_offset header_offset
)
18729 unsigned int bytes_read
;
18730 bfd
*abfd
= section
->get_bfd_owner ();
18731 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
18733 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
18734 info_ptr
+= bytes_read
;
18736 header
->version
= read_2_bytes (abfd
, info_ptr
);
18739 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
18742 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
18745 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
18748 /* Return the DW_AT_loclists_base value for the CU. */
18750 lookup_loclist_base (struct dwarf2_cu
*cu
)
18752 /* For the .dwo unit, the loclist_base points to the first offset following
18753 the header. The header consists of the following entities-
18754 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18756 2. version (2 bytes)
18757 3. address size (1 byte)
18758 4. segment selector size (1 byte)
18759 5. offset entry count (4 bytes)
18760 These sizes are derived as per the DWARFv5 standard. */
18761 if (cu
->dwo_unit
!= nullptr)
18763 if (cu
->header
.initial_length_size
== 4)
18764 return LOCLIST_HEADER_SIZE32
;
18765 return LOCLIST_HEADER_SIZE64
;
18767 return cu
->loclist_base
;
18770 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18771 array of offsets in the .debug_loclists section. */
18774 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
18776 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18777 struct objfile
*objfile
= per_objfile
->objfile
;
18778 bfd
*abfd
= objfile
->obfd
;
18779 ULONGEST loclist_header_size
=
18780 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
18781 : LOCLIST_HEADER_SIZE64
);
18782 ULONGEST loclist_base
= lookup_loclist_base (cu
);
18784 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18785 ULONGEST start_offset
=
18786 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
18788 /* Get loclists section. */
18789 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
18791 /* Read the loclists section content. */
18792 section
->read (objfile
);
18793 if (section
->buffer
== NULL
)
18794 error (_("DW_FORM_loclistx used without .debug_loclists "
18795 "section [in module %s]"), objfile_name (objfile
));
18797 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18798 so if loclist_base is smaller than the header size, we have a problem. */
18799 if (loclist_base
< loclist_header_size
)
18800 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18801 objfile_name (objfile
));
18803 /* Read the header of the loclists contribution. */
18804 struct loclists_rnglists_header header
;
18805 read_loclists_rnglists_header (&header
, section
,
18806 (sect_offset
) (loclist_base
- loclist_header_size
));
18808 /* Verify the loclist index is valid. */
18809 if (loclist_index
>= header
.offset_entry_count
)
18810 error (_("DW_FORM_loclistx pointing outside of "
18811 ".debug_loclists offset array [in module %s]"),
18812 objfile_name (objfile
));
18814 /* Validate that reading won't go beyond the end of the section. */
18815 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18816 error (_("Reading DW_FORM_loclistx index beyond end of"
18817 ".debug_loclists section [in module %s]"),
18818 objfile_name (objfile
));
18820 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18822 if (cu
->header
.offset_size
== 4)
18823 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
18825 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
18828 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18829 array of offsets in the .debug_rnglists section. */
18832 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
18835 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
18836 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
18837 bfd
*abfd
= objfile
->obfd
;
18838 ULONGEST rnglist_header_size
=
18839 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
18840 : RNGLIST_HEADER_SIZE64
);
18842 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18843 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18845 ULONGEST rnglist_base
=
18846 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
18848 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18849 ULONGEST start_offset
=
18850 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
18852 /* Get rnglists section. */
18853 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
18855 /* Read the rnglists section content. */
18856 section
->read (objfile
);
18857 if (section
->buffer
== nullptr)
18858 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18860 objfile_name (objfile
));
18862 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18863 so if rnglist_base is smaller than the header size, we have a problem. */
18864 if (rnglist_base
< rnglist_header_size
)
18865 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18866 objfile_name (objfile
));
18868 /* Read the header of the rnglists contribution. */
18869 struct loclists_rnglists_header header
;
18870 read_loclists_rnglists_header (&header
, section
,
18871 (sect_offset
) (rnglist_base
- rnglist_header_size
));
18873 /* Verify the rnglist index is valid. */
18874 if (rnglist_index
>= header
.offset_entry_count
)
18875 error (_("DW_FORM_rnglistx index pointing outside of "
18876 ".debug_rnglists offset array [in module %s]"),
18877 objfile_name (objfile
));
18879 /* Validate that reading won't go beyond the end of the section. */
18880 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18881 error (_("Reading DW_FORM_rnglistx index beyond end of"
18882 ".debug_rnglists section [in module %s]"),
18883 objfile_name (objfile
));
18885 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18887 if (cu
->header
.offset_size
== 4)
18888 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
18890 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
18893 /* Process the attributes that had to be skipped in the first round. These
18894 attributes are the ones that need str_offsets_base or addr_base attributes.
18895 They could not have been processed in the first round, because at the time
18896 the values of str_offsets_base or addr_base may not have been known. */
18898 read_attribute_reprocess (const struct die_reader_specs
*reader
,
18899 struct attribute
*attr
, dwarf_tag tag
)
18901 struct dwarf2_cu
*cu
= reader
->cu
;
18902 switch (attr
->form
)
18904 case DW_FORM_addrx
:
18905 case DW_FORM_GNU_addr_index
:
18906 attr
->set_address (read_addr_index (cu
,
18907 attr
->as_unsigned_reprocess ()));
18909 case DW_FORM_loclistx
:
18911 sect_offset loclists_sect_off
18912 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
18914 attr
->set_unsigned (to_underlying (loclists_sect_off
));
18917 case DW_FORM_rnglistx
:
18919 sect_offset rnglists_sect_off
18920 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
18922 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
18926 case DW_FORM_strx1
:
18927 case DW_FORM_strx2
:
18928 case DW_FORM_strx3
:
18929 case DW_FORM_strx4
:
18930 case DW_FORM_GNU_str_index
:
18932 unsigned int str_index
= attr
->as_unsigned_reprocess ();
18933 gdb_assert (!attr
->canonical_string_p ());
18934 if (reader
->dwo_file
!= NULL
)
18935 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
18938 attr
->set_string_noncanonical (read_stub_str_index (cu
,
18943 gdb_assert_not_reached ("Unexpected DWARF form.");
18947 /* Read an attribute value described by an attribute form. */
18949 static const gdb_byte
*
18950 read_attribute_value (const struct die_reader_specs
*reader
,
18951 struct attribute
*attr
, unsigned form
,
18952 LONGEST implicit_const
, const gdb_byte
*info_ptr
)
18954 struct dwarf2_cu
*cu
= reader
->cu
;
18955 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18956 struct objfile
*objfile
= per_objfile
->objfile
;
18957 bfd
*abfd
= reader
->abfd
;
18958 struct comp_unit_head
*cu_header
= &cu
->header
;
18959 unsigned int bytes_read
;
18960 struct dwarf_block
*blk
;
18962 attr
->form
= (enum dwarf_form
) form
;
18965 case DW_FORM_ref_addr
:
18966 if (cu_header
->version
== 2)
18967 attr
->set_unsigned (cu_header
->read_address (abfd
, info_ptr
,
18970 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18972 info_ptr
+= bytes_read
;
18974 case DW_FORM_GNU_ref_alt
:
18975 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18977 info_ptr
+= bytes_read
;
18981 struct gdbarch
*gdbarch
= objfile
->arch ();
18982 CORE_ADDR addr
= cu_header
->read_address (abfd
, info_ptr
, &bytes_read
);
18983 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
18984 attr
->set_address (addr
);
18985 info_ptr
+= bytes_read
;
18988 case DW_FORM_block2
:
18989 blk
= dwarf_alloc_block (cu
);
18990 blk
->size
= read_2_bytes (abfd
, info_ptr
);
18992 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
18993 info_ptr
+= blk
->size
;
18994 attr
->set_block (blk
);
18996 case DW_FORM_block4
:
18997 blk
= dwarf_alloc_block (cu
);
18998 blk
->size
= read_4_bytes (abfd
, info_ptr
);
19000 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19001 info_ptr
+= blk
->size
;
19002 attr
->set_block (blk
);
19004 case DW_FORM_data2
:
19005 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
19008 case DW_FORM_data4
:
19009 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
19012 case DW_FORM_data8
:
19013 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
19016 case DW_FORM_data16
:
19017 blk
= dwarf_alloc_block (cu
);
19019 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
19021 attr
->set_block (blk
);
19023 case DW_FORM_sec_offset
:
19024 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19026 info_ptr
+= bytes_read
;
19028 case DW_FORM_loclistx
:
19030 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19032 info_ptr
+= bytes_read
;
19035 case DW_FORM_string
:
19036 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
19038 info_ptr
+= bytes_read
;
19041 if (!cu
->per_cu
->is_dwz
)
19043 attr
->set_string_noncanonical
19044 (read_indirect_string (per_objfile
,
19045 abfd
, info_ptr
, cu_header
,
19047 info_ptr
+= bytes_read
;
19051 case DW_FORM_line_strp
:
19052 if (!cu
->per_cu
->is_dwz
)
19054 attr
->set_string_noncanonical
19055 (per_objfile
->read_line_string (info_ptr
, cu_header
,
19057 info_ptr
+= bytes_read
;
19061 case DW_FORM_GNU_strp_alt
:
19063 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19064 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
19067 attr
->set_string_noncanonical
19068 (dwz
->read_string (objfile
, str_offset
));
19069 info_ptr
+= bytes_read
;
19072 case DW_FORM_exprloc
:
19073 case DW_FORM_block
:
19074 blk
= dwarf_alloc_block (cu
);
19075 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19076 info_ptr
+= bytes_read
;
19077 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19078 info_ptr
+= blk
->size
;
19079 attr
->set_block (blk
);
19081 case DW_FORM_block1
:
19082 blk
= dwarf_alloc_block (cu
);
19083 blk
->size
= read_1_byte (abfd
, info_ptr
);
19085 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19086 info_ptr
+= blk
->size
;
19087 attr
->set_block (blk
);
19089 case DW_FORM_data1
:
19091 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
19094 case DW_FORM_flag_present
:
19095 attr
->set_unsigned (1);
19097 case DW_FORM_sdata
:
19098 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
19099 info_ptr
+= bytes_read
;
19101 case DW_FORM_rnglistx
:
19103 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19105 info_ptr
+= bytes_read
;
19108 case DW_FORM_udata
:
19109 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
19110 info_ptr
+= bytes_read
;
19113 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19114 + read_1_byte (abfd
, info_ptr
)));
19118 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19119 + read_2_bytes (abfd
, info_ptr
)));
19123 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19124 + read_4_bytes (abfd
, info_ptr
)));
19128 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19129 + read_8_bytes (abfd
, info_ptr
)));
19132 case DW_FORM_ref_sig8
:
19133 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
19136 case DW_FORM_ref_udata
:
19137 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19138 + read_unsigned_leb128 (abfd
, info_ptr
,
19140 info_ptr
+= bytes_read
;
19142 case DW_FORM_indirect
:
19143 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19144 info_ptr
+= bytes_read
;
19145 if (form
== DW_FORM_implicit_const
)
19147 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
19148 info_ptr
+= bytes_read
;
19150 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
19153 case DW_FORM_implicit_const
:
19154 attr
->set_signed (implicit_const
);
19156 case DW_FORM_addrx
:
19157 case DW_FORM_GNU_addr_index
:
19158 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19160 info_ptr
+= bytes_read
;
19163 case DW_FORM_strx1
:
19164 case DW_FORM_strx2
:
19165 case DW_FORM_strx3
:
19166 case DW_FORM_strx4
:
19167 case DW_FORM_GNU_str_index
:
19169 ULONGEST str_index
;
19170 if (form
== DW_FORM_strx1
)
19172 str_index
= read_1_byte (abfd
, info_ptr
);
19175 else if (form
== DW_FORM_strx2
)
19177 str_index
= read_2_bytes (abfd
, info_ptr
);
19180 else if (form
== DW_FORM_strx3
)
19182 str_index
= read_3_bytes (abfd
, info_ptr
);
19185 else if (form
== DW_FORM_strx4
)
19187 str_index
= read_4_bytes (abfd
, info_ptr
);
19192 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19193 info_ptr
+= bytes_read
;
19195 attr
->set_unsigned_reprocess (str_index
);
19199 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19200 dwarf_form_name (form
),
19201 bfd_get_filename (abfd
));
19205 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
19206 attr
->form
= DW_FORM_GNU_ref_alt
;
19208 /* We have seen instances where the compiler tried to emit a byte
19209 size attribute of -1 which ended up being encoded as an unsigned
19210 0xffffffff. Although 0xffffffff is technically a valid size value,
19211 an object of this size seems pretty unlikely so we can relatively
19212 safely treat these cases as if the size attribute was invalid and
19213 treat them as zero by default. */
19214 if (attr
->name
== DW_AT_byte_size
19215 && form
== DW_FORM_data4
19216 && attr
->as_unsigned () >= 0xffffffff)
19219 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19220 hex_string (attr
->as_unsigned ()));
19221 attr
->set_unsigned (0);
19227 /* Read an attribute described by an abbreviated attribute. */
19229 static const gdb_byte
*
19230 read_attribute (const struct die_reader_specs
*reader
,
19231 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
19232 const gdb_byte
*info_ptr
)
19234 attr
->name
= abbrev
->name
;
19235 attr
->string_is_canonical
= 0;
19236 attr
->requires_reprocessing
= 0;
19237 return read_attribute_value (reader
, attr
, abbrev
->form
,
19238 abbrev
->implicit_const
, info_ptr
);
19241 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19243 static const char *
19244 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
19245 LONGEST str_offset
)
19247 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
19248 str_offset
, "DW_FORM_strp");
19251 /* Return pointer to string at .debug_str offset as read from BUF.
19252 BUF is assumed to be in a compilation unit described by CU_HEADER.
19253 Return *BYTES_READ_PTR count of bytes read from BUF. */
19255 static const char *
19256 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
19257 const gdb_byte
*buf
,
19258 const struct comp_unit_head
*cu_header
,
19259 unsigned int *bytes_read_ptr
)
19261 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19263 return read_indirect_string_at_offset (per_objfile
, str_offset
);
19269 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19270 unsigned int offset_size
)
19272 bfd
*abfd
= objfile
->obfd
;
19273 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
19275 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19281 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19282 const struct comp_unit_head
*cu_header
,
19283 unsigned int *bytes_read_ptr
)
19285 bfd
*abfd
= objfile
->obfd
;
19286 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19288 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19291 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19292 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19293 ADDR_SIZE is the size of addresses from the CU header. */
19296 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
19297 gdb::optional
<ULONGEST
> addr_base
, int addr_size
)
19299 struct objfile
*objfile
= per_objfile
->objfile
;
19300 bfd
*abfd
= objfile
->obfd
;
19301 const gdb_byte
*info_ptr
;
19302 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
19304 per_objfile
->per_bfd
->addr
.read (objfile
);
19305 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
19306 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19307 objfile_name (objfile
));
19308 if (addr_base_or_zero
+ addr_index
* addr_size
19309 >= per_objfile
->per_bfd
->addr
.size
)
19310 error (_("DW_FORM_addr_index pointing outside of "
19311 ".debug_addr section [in module %s]"),
19312 objfile_name (objfile
));
19313 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
19314 + addr_index
* addr_size
);
19315 if (addr_size
== 4)
19316 return bfd_get_32 (abfd
, info_ptr
);
19318 return bfd_get_64 (abfd
, info_ptr
);
19321 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19324 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
19326 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
19327 cu
->addr_base
, cu
->header
.addr_size
);
19330 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19333 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
19334 unsigned int *bytes_read
)
19336 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
;
19337 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
19339 return read_addr_index (cu
, addr_index
);
19345 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
19346 dwarf2_per_objfile
*per_objfile
,
19347 unsigned int addr_index
)
19349 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
19350 gdb::optional
<ULONGEST
> addr_base
;
19353 /* We need addr_base and addr_size.
19354 If we don't have PER_CU->cu, we have to get it.
19355 Nasty, but the alternative is storing the needed info in PER_CU,
19356 which at this point doesn't seem justified: it's not clear how frequently
19357 it would get used and it would increase the size of every PER_CU.
19358 Entry points like dwarf2_per_cu_addr_size do a similar thing
19359 so we're not in uncharted territory here.
19360 Alas we need to be a bit more complicated as addr_base is contained
19363 We don't need to read the entire CU(/TU).
19364 We just need the header and top level die.
19366 IWBN to use the aging mechanism to let us lazily later discard the CU.
19367 For now we skip this optimization. */
19371 addr_base
= cu
->addr_base
;
19372 addr_size
= cu
->header
.addr_size
;
19376 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
19377 addr_base
= reader
.cu
->addr_base
;
19378 addr_size
= reader
.cu
->header
.addr_size
;
19381 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
19384 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19385 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19388 static const char *
19389 read_str_index (struct dwarf2_cu
*cu
,
19390 struct dwarf2_section_info
*str_section
,
19391 struct dwarf2_section_info
*str_offsets_section
,
19392 ULONGEST str_offsets_base
, ULONGEST str_index
,
19393 unsigned offset_size
)
19395 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19396 struct objfile
*objfile
= per_objfile
->objfile
;
19397 const char *objf_name
= objfile_name (objfile
);
19398 bfd
*abfd
= objfile
->obfd
;
19399 const gdb_byte
*info_ptr
;
19400 ULONGEST str_offset
;
19401 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19403 str_section
->read (objfile
);
19404 str_offsets_section
->read (objfile
);
19405 if (str_section
->buffer
== NULL
)
19406 error (_("%s used without %s section"
19407 " in CU at offset %s [in module %s]"),
19408 form_name
, str_section
->get_name (),
19409 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19410 if (str_offsets_section
->buffer
== NULL
)
19411 error (_("%s used without %s section"
19412 " in CU at offset %s [in module %s]"),
19413 form_name
, str_section
->get_name (),
19414 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19415 info_ptr
= (str_offsets_section
->buffer
19417 + str_index
* offset_size
);
19418 if (offset_size
== 4)
19419 str_offset
= bfd_get_32 (abfd
, info_ptr
);
19421 str_offset
= bfd_get_64 (abfd
, info_ptr
);
19422 if (str_offset
>= str_section
->size
)
19423 error (_("Offset from %s pointing outside of"
19424 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19425 form_name
, sect_offset_str (cu
->header
.sect_off
), objf_name
);
19426 return (const char *) (str_section
->buffer
+ str_offset
);
19429 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19431 static const char *
19432 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
19434 unsigned offset_size
;
19435 ULONGEST str_offsets_base
;
19436 if (reader
->cu
->header
.version
>= 5)
19438 /* We have a DWARF5 CU with a reference to a .debug_str_offsets section,
19439 so assume the .debug_str_offsets section is DWARF5 as well, and
19440 parse the header. FIXME: Parse the header only once. */
19441 unsigned int bytes_read
= 0;
19442 bfd
*abfd
= reader
->dwo_file
->sections
.str_offsets
.get_bfd_owner ();
19443 const gdb_byte
*p
= reader
->dwo_file
->sections
.str_offsets
.buffer
;
19445 /* Header: Initial length. */
19446 read_initial_length (abfd
, p
+ bytes_read
, &bytes_read
);
19448 /* Determine offset_size based on the .debug_str_offsets header. */
19449 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
19450 offset_size
= dwarf5_is_dwarf64
? 8 : 4;
19452 /* Header: Version. */
19453 unsigned version
= read_2_bytes (abfd
, p
+ bytes_read
);
19458 /* We'd like one warning here about ignoring the section, but
19459 because we parse the header more than once (see FIXME above)
19460 we'd have many warnings, so use a complaint instead, which at
19461 least has a limit. */
19462 complaint (_("Section .debug_str_offsets in %s has unsupported"
19463 " version %d, use empty string."),
19464 reader
->dwo_file
->dwo_name
, version
);
19468 /* Header: Padding. */
19471 str_offsets_base
= bytes_read
;
19475 /* We have a pre-DWARF5 CU with a reference to a .debug_str_offsets
19476 section, assume the .debug_str_offsets section is pre-DWARF5 as
19477 well, which doesn't have a header. */
19478 str_offsets_base
= 0;
19480 /* Determine offset_size based on the .debug_info header. */
19481 offset_size
= reader
->cu
->header
.offset_size
;
19484 return read_str_index (reader
->cu
,
19485 &reader
->dwo_file
->sections
.str
,
19486 &reader
->dwo_file
->sections
.str_offsets
,
19487 str_offsets_base
, str_index
, offset_size
);
19490 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19492 static const char *
19493 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
19495 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19496 const char *objf_name
= objfile_name (objfile
);
19497 static const char form_name
[] = "DW_FORM_GNU_str_index";
19498 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
19500 if (!cu
->str_offsets_base
.has_value ())
19501 error (_("%s used in Fission stub without %s"
19502 " in CU at offset 0x%lx [in module %s]"),
19503 form_name
, str_offsets_attr_name
,
19504 (long) cu
->header
.offset_size
, objf_name
);
19506 return read_str_index (cu
,
19507 &cu
->per_objfile
->per_bfd
->str
,
19508 &cu
->per_objfile
->per_bfd
->str_offsets
,
19509 *cu
->str_offsets_base
, str_index
,
19510 cu
->header
.offset_size
);
19513 /* Return the length of an LEB128 number in BUF. */
19516 leb128_size (const gdb_byte
*buf
)
19518 const gdb_byte
*begin
= buf
;
19524 if ((byte
& 128) == 0)
19525 return buf
- begin
;
19529 static enum language
19530 dwarf_lang_to_enum_language (unsigned int lang
)
19532 enum language language
;
19541 language
= language_c
;
19544 case DW_LANG_C_plus_plus
:
19545 case DW_LANG_C_plus_plus_11
:
19546 case DW_LANG_C_plus_plus_14
:
19547 language
= language_cplus
;
19550 language
= language_d
;
19552 case DW_LANG_Fortran77
:
19553 case DW_LANG_Fortran90
:
19554 case DW_LANG_Fortran95
:
19555 case DW_LANG_Fortran03
:
19556 case DW_LANG_Fortran08
:
19557 language
= language_fortran
;
19560 language
= language_go
;
19562 case DW_LANG_Mips_Assembler
:
19563 language
= language_asm
;
19565 case DW_LANG_Ada83
:
19566 case DW_LANG_Ada95
:
19567 language
= language_ada
;
19569 case DW_LANG_Modula2
:
19570 language
= language_m2
;
19572 case DW_LANG_Pascal83
:
19573 language
= language_pascal
;
19576 language
= language_objc
;
19579 case DW_LANG_Rust_old
:
19580 language
= language_rust
;
19582 case DW_LANG_OpenCL
:
19583 language
= language_opencl
;
19585 case DW_LANG_Cobol74
:
19586 case DW_LANG_Cobol85
:
19588 language
= language_minimal
;
19595 /* Return the named attribute or NULL if not there. */
19597 static struct attribute
*
19598 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19603 struct attribute
*spec
= NULL
;
19605 for (i
= 0; i
< die
->num_attrs
; ++i
)
19607 if (die
->attrs
[i
].name
== name
)
19608 return &die
->attrs
[i
];
19609 if (die
->attrs
[i
].name
== DW_AT_specification
19610 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
19611 spec
= &die
->attrs
[i
];
19617 die
= follow_die_ref (die
, spec
, &cu
);
19623 /* Return the string associated with a string-typed attribute, or NULL if it
19624 is either not found or is of an incorrect type. */
19626 static const char *
19627 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19629 struct attribute
*attr
;
19630 const char *str
= NULL
;
19632 attr
= dwarf2_attr (die
, name
, cu
);
19636 str
= attr
->as_string ();
19637 if (str
== nullptr)
19638 complaint (_("string type expected for attribute %s for "
19639 "DIE at %s in module %s"),
19640 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
19641 objfile_name (cu
->per_objfile
->objfile
));
19647 /* Return the dwo name or NULL if not present. If present, it is in either
19648 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19649 static const char *
19650 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19652 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
19653 if (dwo_name
== nullptr)
19654 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
19658 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19659 and holds a non-zero value. This function should only be used for
19660 DW_FORM_flag or DW_FORM_flag_present attributes. */
19663 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
19665 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
19667 return attr
!= nullptr && attr
->as_boolean ();
19671 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
19673 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19674 which value is non-zero. However, we have to be careful with
19675 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19676 (via dwarf2_flag_true_p) follows this attribute. So we may
19677 end up accidently finding a declaration attribute that belongs
19678 to a different DIE referenced by the specification attribute,
19679 even though the given DIE does not have a declaration attribute. */
19680 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
19681 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
19684 /* Return the die giving the specification for DIE, if there is
19685 one. *SPEC_CU is the CU containing DIE on input, and the CU
19686 containing the return value on output. If there is no
19687 specification, but there is an abstract origin, that is
19690 static struct die_info
*
19691 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
19693 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
19696 if (spec_attr
== NULL
)
19697 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
19699 if (spec_attr
== NULL
)
19702 return follow_die_ref (die
, spec_attr
, spec_cu
);
19705 /* A convenience function to find the proper .debug_line section for a CU. */
19707 static struct dwarf2_section_info
*
19708 get_debug_line_section (struct dwarf2_cu
*cu
)
19710 struct dwarf2_section_info
*section
;
19711 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19713 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19715 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19716 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
19717 else if (cu
->per_cu
->is_dwz
)
19719 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19721 section
= &dwz
->line
;
19724 section
= &per_objfile
->per_bfd
->line
;
19729 /* Read the statement program header starting at OFFSET in
19730 .debug_line, or .debug_line.dwo. Return a pointer
19731 to a struct line_header, allocated using xmalloc.
19732 Returns NULL if there is a problem reading the header, e.g., if it
19733 has a version we don't understand.
19735 NOTE: the strings in the include directory and file name tables of
19736 the returned object point into the dwarf line section buffer,
19737 and must not be freed. */
19739 static line_header_up
19740 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
)
19742 struct dwarf2_section_info
*section
;
19743 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19745 section
= get_debug_line_section (cu
);
19746 section
->read (per_objfile
->objfile
);
19747 if (section
->buffer
== NULL
)
19749 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19750 complaint (_("missing .debug_line.dwo section"));
19752 complaint (_("missing .debug_line section"));
19756 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
19757 per_objfile
, section
, &cu
->header
);
19760 /* Subroutine of dwarf_decode_lines to simplify it.
19761 Return the file name for the given file_entry.
19762 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19763 If space for the result is malloc'd, *NAME_HOLDER will be set.
19764 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19765 equivalent to CU_INFO. */
19767 static const char *
19768 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
19769 const file_and_directory
&cu_info
,
19770 std::string
&name_holder
)
19772 const char *include_name
= fe
.name
;
19773 const char *include_name_to_compare
= include_name
;
19775 const char *dir_name
= fe
.include_dir (lh
);
19777 std::string hold_compare
;
19778 if (!IS_ABSOLUTE_PATH (include_name
)
19779 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
19781 /* Avoid creating a duplicate name for CU_INFO.
19782 We do this by comparing INCLUDE_NAME and CU_INFO.
19783 Before we do the comparison, however, we need to account
19784 for DIR_NAME and COMP_DIR.
19785 First prepend dir_name (if non-NULL). If we still don't
19786 have an absolute path prepend comp_dir (if non-NULL).
19787 However, the directory we record in the include-file's
19788 psymtab does not contain COMP_DIR (to match the
19789 corresponding symtab(s)).
19794 bash$ gcc -g ./hello.c
19795 include_name = "hello.c"
19797 DW_AT_comp_dir = comp_dir = "/tmp"
19798 DW_AT_name = "./hello.c"
19802 if (dir_name
!= NULL
)
19804 name_holder
= path_join (dir_name
, include_name
);
19805 include_name
= name_holder
.c_str ();
19806 include_name_to_compare
= include_name
;
19808 if (!IS_ABSOLUTE_PATH (include_name
)
19809 && cu_info
.get_comp_dir () != nullptr)
19811 hold_compare
= path_join (cu_info
.get_comp_dir (), include_name
);
19812 include_name_to_compare
= hold_compare
.c_str ();
19816 std::string copied_name
;
19817 const char *cu_filename
= cu_info
.get_name ();
19818 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
19820 copied_name
= path_join (cu_info
.get_comp_dir (), cu_filename
);
19821 cu_filename
= copied_name
.c_str ();
19824 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
19826 return include_name
;
19829 /* State machine to track the state of the line number program. */
19831 class lnp_state_machine
19834 /* Initialize a machine state for the start of a line number
19836 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
19838 file_entry
*current_file ()
19840 /* lh->file_names is 0-based, but the file name numbers in the
19841 statement program are 1-based. */
19842 return m_line_header
->file_name_at (m_file
);
19845 /* Record the line in the state machine. END_SEQUENCE is true if
19846 we're processing the end of a sequence. */
19847 void record_line (bool end_sequence
);
19849 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19850 nop-out rest of the lines in this sequence. */
19851 void check_line_address (struct dwarf2_cu
*cu
,
19852 const gdb_byte
*line_ptr
,
19853 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
);
19855 void handle_set_discriminator (unsigned int discriminator
)
19857 m_discriminator
= discriminator
;
19858 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
19861 /* Handle DW_LNE_set_address. */
19862 void handle_set_address (CORE_ADDR baseaddr
, CORE_ADDR address
)
19865 address
+= baseaddr
;
19866 m_address
= gdbarch_adjust_dwarf2_line (m_gdbarch
, address
, false);
19869 /* Handle DW_LNS_advance_pc. */
19870 void handle_advance_pc (CORE_ADDR adjust
);
19872 /* Handle a special opcode. */
19873 void handle_special_opcode (unsigned char op_code
);
19875 /* Handle DW_LNS_advance_line. */
19876 void handle_advance_line (int line_delta
)
19878 advance_line (line_delta
);
19881 /* Handle DW_LNS_set_file. */
19882 void handle_set_file (file_name_index file
);
19884 /* Handle DW_LNS_negate_stmt. */
19885 void handle_negate_stmt ()
19887 m_flags
^= LEF_IS_STMT
;
19890 /* Handle DW_LNS_const_add_pc. */
19891 void handle_const_add_pc ();
19893 /* Handle DW_LNS_fixed_advance_pc. */
19894 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
19896 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19900 /* Handle DW_LNS_copy. */
19901 void handle_copy ()
19903 record_line (false);
19904 m_discriminator
= 0;
19905 m_flags
&= ~LEF_PROLOGUE_END
;
19908 /* Handle DW_LNE_end_sequence. */
19909 void handle_end_sequence ()
19911 m_currently_recording_lines
= true;
19914 /* Handle DW_LNS_set_prologue_end. */
19915 void handle_set_prologue_end ()
19917 m_flags
|= LEF_PROLOGUE_END
;
19921 /* Advance the line by LINE_DELTA. */
19922 void advance_line (int line_delta
)
19924 m_line
+= line_delta
;
19926 if (line_delta
!= 0)
19927 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
19930 struct dwarf2_cu
*m_cu
;
19932 gdbarch
*m_gdbarch
;
19934 /* The line number header. */
19935 line_header
*m_line_header
;
19937 /* These are part of the standard DWARF line number state machine,
19938 and initialized according to the DWARF spec. */
19940 unsigned char m_op_index
= 0;
19941 /* The line table index of the current file. */
19942 file_name_index m_file
= 1;
19943 unsigned int m_line
= 1;
19945 /* These are initialized in the constructor. */
19947 CORE_ADDR m_address
;
19948 linetable_entry_flags m_flags
;
19949 unsigned int m_discriminator
;
19951 /* Additional bits of state we need to track. */
19953 /* The last file that we called dwarf2_start_subfile for.
19954 This is only used for TLLs. */
19955 unsigned int m_last_file
= 0;
19956 /* The last file a line number was recorded for. */
19957 struct subfile
*m_last_subfile
= NULL
;
19959 /* The address of the last line entry. */
19960 CORE_ADDR m_last_address
;
19962 /* Set to true when a previous line at the same address (using
19963 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19964 when a line entry at a new address (m_address different to
19965 m_last_address) is processed. */
19966 bool m_stmt_at_address
= false;
19968 /* When true, record the lines we decode. */
19969 bool m_currently_recording_lines
= false;
19971 /* The last line number that was recorded, used to coalesce
19972 consecutive entries for the same line. This can happen, for
19973 example, when discriminators are present. PR 17276. */
19974 unsigned int m_last_line
= 0;
19975 bool m_line_has_non_zero_discriminator
= false;
19979 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
19981 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
19982 / m_line_header
->maximum_ops_per_instruction
)
19983 * m_line_header
->minimum_instruction_length
);
19984 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19985 m_op_index
= ((m_op_index
+ adjust
)
19986 % m_line_header
->maximum_ops_per_instruction
);
19990 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
19992 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
19993 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
19994 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
19995 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
19996 / m_line_header
->maximum_ops_per_instruction
)
19997 * m_line_header
->minimum_instruction_length
);
19998 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19999 m_op_index
= ((m_op_index
+ adj_opcode_d
)
20000 % m_line_header
->maximum_ops_per_instruction
);
20002 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
20003 advance_line (line_delta
);
20004 record_line (false);
20005 m_discriminator
= 0;
20006 m_flags
&= ~LEF_PROLOGUE_END
;
20010 lnp_state_machine::handle_set_file (file_name_index file
)
20014 const file_entry
*fe
= current_file ();
20016 dwarf2_debug_line_missing_file_complaint ();
20019 const char *dir
= fe
->include_dir (m_line_header
);
20021 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20022 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
20023 dwarf2_start_subfile (m_cu
, fe
->name
, dir
);
20028 lnp_state_machine::handle_const_add_pc ()
20031 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
20034 = (((m_op_index
+ adjust
)
20035 / m_line_header
->maximum_ops_per_instruction
)
20036 * m_line_header
->minimum_instruction_length
);
20038 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
20039 m_op_index
= ((m_op_index
+ adjust
)
20040 % m_line_header
->maximum_ops_per_instruction
);
20043 /* Return non-zero if we should add LINE to the line number table.
20044 LINE is the line to add, LAST_LINE is the last line that was added,
20045 LAST_SUBFILE is the subfile for LAST_LINE.
20046 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20047 had a non-zero discriminator.
20049 We have to be careful in the presence of discriminators.
20050 E.g., for this line:
20052 for (i = 0; i < 100000; i++);
20054 clang can emit four line number entries for that one line,
20055 each with a different discriminator.
20056 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20058 However, we want gdb to coalesce all four entries into one.
20059 Otherwise the user could stepi into the middle of the line and
20060 gdb would get confused about whether the pc really was in the
20061 middle of the line.
20063 Things are further complicated by the fact that two consecutive
20064 line number entries for the same line is a heuristic used by gcc
20065 to denote the end of the prologue. So we can't just discard duplicate
20066 entries, we have to be selective about it. The heuristic we use is
20067 that we only collapse consecutive entries for the same line if at least
20068 one of those entries has a non-zero discriminator. PR 17276.
20070 Note: Addresses in the line number state machine can never go backwards
20071 within one sequence, thus this coalescing is ok. */
20074 dwarf_record_line_p (struct dwarf2_cu
*cu
,
20075 unsigned int line
, unsigned int last_line
,
20076 int line_has_non_zero_discriminator
,
20077 struct subfile
*last_subfile
)
20079 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
20081 if (line
!= last_line
)
20083 /* Same line for the same file that we've seen already.
20084 As a last check, for pr 17276, only record the line if the line
20085 has never had a non-zero discriminator. */
20086 if (!line_has_non_zero_discriminator
)
20091 /* Use the CU's builder to record line number LINE beginning at
20092 address ADDRESS in the line table of subfile SUBFILE. */
20095 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20096 unsigned int line
, CORE_ADDR address
,
20097 linetable_entry_flags flags
,
20098 struct dwarf2_cu
*cu
)
20100 CORE_ADDR addr
= gdbarch_addr_bits_remove (gdbarch
, address
);
20102 if (dwarf_line_debug
)
20104 gdb_printf (gdb_stdlog
,
20105 "Recording line %u, file %s, address %s\n",
20106 line
, lbasename (subfile
->name
.c_str ()),
20107 paddress (gdbarch
, address
));
20111 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
20114 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20115 Mark the end of a set of line number records.
20116 The arguments are the same as for dwarf_record_line_1.
20117 If SUBFILE is NULL the request is ignored. */
20120 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20121 CORE_ADDR address
, struct dwarf2_cu
*cu
)
20123 if (subfile
== NULL
)
20126 if (dwarf_line_debug
)
20128 gdb_printf (gdb_stdlog
,
20129 "Finishing current line, file %s, address %s\n",
20130 lbasename (subfile
->name
.c_str ()),
20131 paddress (gdbarch
, address
));
20134 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
20138 lnp_state_machine::record_line (bool end_sequence
)
20140 if (dwarf_line_debug
)
20142 gdb_printf (gdb_stdlog
,
20143 "Processing actual line %u: file %u,"
20144 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20146 paddress (m_gdbarch
, m_address
),
20147 (m_flags
& LEF_IS_STMT
) != 0,
20148 (m_flags
& LEF_PROLOGUE_END
) != 0,
20150 (end_sequence
? "\t(end sequence)" : ""));
20153 file_entry
*fe
= current_file ();
20156 dwarf2_debug_line_missing_file_complaint ();
20157 /* For now we ignore lines not starting on an instruction boundary.
20158 But not when processing end_sequence for compatibility with the
20159 previous version of the code. */
20160 else if (m_op_index
== 0 || end_sequence
)
20162 /* When we switch files we insert an end maker in the first file,
20163 switch to the second file and add a new line entry. The
20164 problem is that the end marker inserted in the first file will
20165 discard any previous line entries at the same address. If the
20166 line entries in the first file are marked as is-stmt, while
20167 the new line in the second file is non-stmt, then this means
20168 the end marker will discard is-stmt lines so we can have a
20169 non-stmt line. This means that there are less addresses at
20170 which the user can insert a breakpoint.
20172 To improve this we track the last address in m_last_address,
20173 and whether we have seen an is-stmt at this address. Then
20174 when switching files, if we have seen a stmt at the current
20175 address, and we are switching to create a non-stmt line, then
20176 discard the new line. */
20178 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
20179 bool ignore_this_line
20180 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
20181 && ((m_flags
& LEF_IS_STMT
) == 0)
20182 && m_stmt_at_address
)
20183 || (!end_sequence
&& m_line
== 0));
20185 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
20187 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
20188 m_currently_recording_lines
? m_cu
: nullptr);
20191 if (!end_sequence
&& !ignore_this_line
)
20193 linetable_entry_flags lte_flags
= m_flags
;
20194 if (producer_is_codewarrior (m_cu
))
20195 lte_flags
|= LEF_IS_STMT
;
20197 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
20198 m_line_has_non_zero_discriminator
,
20201 buildsym_compunit
*builder
= m_cu
->get_builder ();
20202 dwarf_record_line_1 (m_gdbarch
,
20203 builder
->get_current_subfile (),
20204 m_line
, m_address
, lte_flags
,
20205 m_currently_recording_lines
? m_cu
: nullptr);
20207 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20208 m_last_line
= m_line
;
20212 /* Track whether we have seen any IS_STMT true at m_address in case we
20213 have multiple line table entries all at m_address. */
20214 if (m_last_address
!= m_address
)
20216 m_stmt_at_address
= false;
20217 m_last_address
= m_address
;
20219 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
20222 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
20227 m_line_header
= lh
;
20229 m_currently_recording_lines
= true;
20231 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20232 was a line entry for it so that the backend has a chance to adjust it
20233 and also record it in case it needs it. This is currently used by MIPS
20234 code, cf. `mips_adjust_dwarf2_line'. */
20235 m_address
= gdbarch_adjust_dwarf2_line (arch
, 0, 0);
20237 if (lh
->default_is_stmt
)
20238 m_flags
|= LEF_IS_STMT
;
20239 m_discriminator
= 0;
20241 m_last_address
= m_address
;
20242 m_stmt_at_address
= false;
20246 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
20247 const gdb_byte
*line_ptr
,
20248 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
)
20250 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20251 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20252 located at 0x0. In this case, additionally check that if
20253 ADDRESS < UNRELOCATED_LOWPC. */
20255 if ((address
== 0 && address
< unrelocated_lowpc
)
20256 || address
== (CORE_ADDR
) -1)
20258 /* This line table is for a function which has been
20259 GCd by the linker. Ignore it. PR gdb/12528 */
20261 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20262 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
20264 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20265 line_offset
, objfile_name (objfile
));
20266 m_currently_recording_lines
= false;
20267 /* Note: m_currently_recording_lines is left as false until we see
20268 DW_LNE_end_sequence. */
20272 /* Subroutine of dwarf_decode_lines to simplify it.
20273 Process the line number information in LH. */
20276 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20279 const gdb_byte
*line_ptr
, *extended_end
;
20280 const gdb_byte
*line_end
;
20281 unsigned int bytes_read
, extended_len
;
20282 unsigned char op_code
, extended_op
;
20283 CORE_ADDR baseaddr
;
20284 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20285 bfd
*abfd
= objfile
->obfd
;
20286 struct gdbarch
*gdbarch
= objfile
->arch ();
20288 baseaddr
= objfile
->text_section_offset ();
20290 line_ptr
= lh
->statement_program_start
;
20291 line_end
= lh
->statement_program_end
;
20293 /* Read the statement sequences until there's nothing left. */
20294 while (line_ptr
< line_end
)
20296 /* The DWARF line number program state machine. Reset the state
20297 machine at the start of each sequence. */
20298 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
20299 bool end_sequence
= false;
20301 /* Start a subfile for the current file of the state
20303 const file_entry
*fe
= state_machine
.current_file ();
20306 dwarf2_start_subfile (cu
, fe
->name
, fe
->include_dir (lh
));
20308 /* Decode the table. */
20309 while (line_ptr
< line_end
&& !end_sequence
)
20311 op_code
= read_1_byte (abfd
, line_ptr
);
20314 if (op_code
>= lh
->opcode_base
)
20316 /* Special opcode. */
20317 state_machine
.handle_special_opcode (op_code
);
20319 else switch (op_code
)
20321 case DW_LNS_extended_op
:
20322 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
20324 line_ptr
+= bytes_read
;
20325 extended_end
= line_ptr
+ extended_len
;
20326 extended_op
= read_1_byte (abfd
, line_ptr
);
20328 if (DW_LNE_lo_user
<= extended_op
20329 && extended_op
<= DW_LNE_hi_user
)
20331 /* Vendor extension, ignore. */
20332 line_ptr
= extended_end
;
20335 switch (extended_op
)
20337 case DW_LNE_end_sequence
:
20338 state_machine
.handle_end_sequence ();
20339 end_sequence
= true;
20341 case DW_LNE_set_address
:
20344 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
20345 line_ptr
+= bytes_read
;
20347 state_machine
.check_line_address (cu
, line_ptr
,
20348 lowpc
- baseaddr
, address
);
20349 state_machine
.handle_set_address (baseaddr
, address
);
20352 case DW_LNE_define_file
:
20354 const char *cur_file
;
20355 unsigned int mod_time
, length
;
20358 cur_file
= read_direct_string (abfd
, line_ptr
,
20360 line_ptr
+= bytes_read
;
20361 dindex
= (dir_index
)
20362 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20363 line_ptr
+= bytes_read
;
20365 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20366 line_ptr
+= bytes_read
;
20368 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20369 line_ptr
+= bytes_read
;
20370 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
20373 case DW_LNE_set_discriminator
:
20375 /* The discriminator is not interesting to the
20376 debugger; just ignore it. We still need to
20377 check its value though:
20378 if there are consecutive entries for the same
20379 (non-prologue) line we want to coalesce them.
20382 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20383 line_ptr
+= bytes_read
;
20385 state_machine
.handle_set_discriminator (discr
);
20389 complaint (_("mangled .debug_line section"));
20392 /* Make sure that we parsed the extended op correctly. If e.g.
20393 we expected a different address size than the producer used,
20394 we may have read the wrong number of bytes. */
20395 if (line_ptr
!= extended_end
)
20397 complaint (_("mangled .debug_line section"));
20402 state_machine
.handle_copy ();
20404 case DW_LNS_advance_pc
:
20407 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20408 line_ptr
+= bytes_read
;
20410 state_machine
.handle_advance_pc (adjust
);
20413 case DW_LNS_advance_line
:
20416 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
20417 line_ptr
+= bytes_read
;
20419 state_machine
.handle_advance_line (line_delta
);
20422 case DW_LNS_set_file
:
20424 file_name_index file
20425 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
20427 line_ptr
+= bytes_read
;
20429 state_machine
.handle_set_file (file
);
20432 case DW_LNS_set_column
:
20433 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20434 line_ptr
+= bytes_read
;
20436 case DW_LNS_negate_stmt
:
20437 state_machine
.handle_negate_stmt ();
20439 case DW_LNS_set_basic_block
:
20441 /* Add to the address register of the state machine the
20442 address increment value corresponding to special opcode
20443 255. I.e., this value is scaled by the minimum
20444 instruction length since special opcode 255 would have
20445 scaled the increment. */
20446 case DW_LNS_const_add_pc
:
20447 state_machine
.handle_const_add_pc ();
20449 case DW_LNS_fixed_advance_pc
:
20451 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
20454 state_machine
.handle_fixed_advance_pc (addr_adj
);
20457 case DW_LNS_set_prologue_end
:
20458 state_machine
.handle_set_prologue_end ();
20462 /* Unknown standard opcode, ignore it. */
20465 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
20467 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20468 line_ptr
+= bytes_read
;
20475 dwarf2_debug_line_missing_end_sequence_complaint ();
20477 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20478 in which case we still finish recording the last line). */
20479 state_machine
.record_line (true);
20483 /* Decode the Line Number Program (LNP) for the given line_header
20484 structure and CU. The actual information extracted and the type
20485 of structures created from the LNP depends on the value of PST.
20487 FND holds the CU file name and directory, if known.
20488 It is used for relative paths in the line table.
20490 NOTE: It is important that psymtabs have the same file name (via
20491 strcmp) as the corresponding symtab. Since the directory is not
20492 used in the name of the symtab we don't use it in the name of the
20493 psymtabs we create. E.g. expand_line_sal requires this when
20494 finding psymtabs to expand. A good testcase for this is
20497 LOWPC is the lowest address in CU (or 0 if not known).
20499 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20500 for its PC<->lines mapping information. Otherwise only the filename
20501 table is read in. */
20504 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20505 CORE_ADDR lowpc
, int decode_mapping
)
20507 if (decode_mapping
)
20508 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
20510 /* Make sure a symtab is created for every file, even files
20511 which contain only variables (i.e. no code with associated
20513 buildsym_compunit
*builder
= cu
->get_builder ();
20514 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
20516 for (auto &fe
: lh
->file_names ())
20518 dwarf2_start_subfile (cu
, fe
.name
, fe
.include_dir (lh
));
20519 if (builder
->get_current_subfile ()->symtab
== NULL
)
20521 builder
->get_current_subfile ()->symtab
20522 = allocate_symtab (cust
,
20523 builder
->get_current_subfile ()->name
.c_str ());
20525 fe
.symtab
= builder
->get_current_subfile ()->symtab
;
20529 /* Start a subfile for DWARF. FILENAME is the name of the file and
20530 DIRNAME the name of the source directory which contains FILENAME
20531 or NULL if not known.
20532 This routine tries to keep line numbers from identical absolute and
20533 relative file names in a common subfile.
20535 Using the `list' example from the GDB testsuite, which resides in
20536 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20537 of /srcdir/list0.c yields the following debugging information for list0.c:
20539 DW_AT_name: /srcdir/list0.c
20540 DW_AT_comp_dir: /compdir
20541 files.files[0].name: list0.h
20542 files.files[0].dir: /srcdir
20543 files.files[1].name: list0.c
20544 files.files[1].dir: /srcdir
20546 The line number information for list0.c has to end up in a single
20547 subfile, so that `break /srcdir/list0.c:1' works as expected.
20548 start_subfile will ensure that this happens provided that we pass the
20549 concatenation of files.files[1].dir and files.files[1].name as the
20553 dwarf2_start_subfile (struct dwarf2_cu
*cu
, const char *filename
,
20554 const char *dirname
)
20558 /* In order not to lose the line information directory,
20559 we concatenate it to the filename when it makes sense.
20560 Note that the Dwarf3 standard says (speaking of filenames in line
20561 information): ``The directory index is ignored for file names
20562 that represent full path names''. Thus ignoring dirname in the
20563 `else' branch below isn't an issue. */
20565 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
20567 copy
= path_join (dirname
, filename
);
20568 filename
= copy
.c_str ();
20571 cu
->get_builder ()->start_subfile (filename
);
20575 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
20576 struct dwarf2_cu
*cu
)
20578 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20579 struct comp_unit_head
*cu_header
= &cu
->header
;
20581 /* NOTE drow/2003-01-30: There used to be a comment and some special
20582 code here to turn a symbol with DW_AT_external and a
20583 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20584 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20585 with some versions of binutils) where shared libraries could have
20586 relocations against symbols in their debug information - the
20587 minimal symbol would have the right address, but the debug info
20588 would not. It's no longer necessary, because we will explicitly
20589 apply relocations when we read in the debug information now. */
20591 /* A DW_AT_location attribute with no contents indicates that a
20592 variable has been optimized away. */
20593 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
20595 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20599 /* Handle one degenerate form of location expression specially, to
20600 preserve GDB's previous behavior when section offsets are
20601 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20602 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20604 if (attr
->form_is_block ())
20606 struct dwarf_block
*block
= attr
->as_block ();
20608 if ((block
->data
[0] == DW_OP_addr
20609 && block
->size
== 1 + cu_header
->addr_size
)
20610 || ((block
->data
[0] == DW_OP_GNU_addr_index
20611 || block
->data
[0] == DW_OP_addrx
)
20613 == 1 + leb128_size (&block
->data
[1]))))
20615 unsigned int dummy
;
20617 if (block
->data
[0] == DW_OP_addr
)
20618 sym
->set_value_address
20619 (cu
->header
.read_address (objfile
->obfd
, block
->data
+ 1,
20622 sym
->set_value_address
20623 (read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
));
20624 sym
->set_aclass_index (LOC_STATIC
);
20625 fixup_symbol_section (sym
, objfile
);
20626 sym
->set_value_address
20627 (sym
->value_address ()
20628 + objfile
->section_offsets
[sym
->section_index ()]);
20633 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20634 expression evaluator, and use LOC_COMPUTED only when necessary
20635 (i.e. when the value of a register or memory location is
20636 referenced, or a thread-local block, etc.). Then again, it might
20637 not be worthwhile. I'm assuming that it isn't unless performance
20638 or memory numbers show me otherwise. */
20640 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
20642 if (SYMBOL_COMPUTED_OPS (sym
)->location_has_loclist
)
20643 cu
->has_loclist
= true;
20646 /* Given a pointer to a DWARF information entry, figure out if we need
20647 to make a symbol table entry for it, and if so, create a new entry
20648 and return a pointer to it.
20649 If TYPE is NULL, determine symbol type from the die, otherwise
20650 used the passed type.
20651 If SPACE is not NULL, use it to hold the new symbol. If it is
20652 NULL, allocate a new symbol on the objfile's obstack. */
20654 static struct symbol
*
20655 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
20656 struct symbol
*space
)
20658 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20659 struct objfile
*objfile
= per_objfile
->objfile
;
20660 struct gdbarch
*gdbarch
= objfile
->arch ();
20661 struct symbol
*sym
= NULL
;
20663 struct attribute
*attr
= NULL
;
20664 struct attribute
*attr2
= NULL
;
20665 CORE_ADDR baseaddr
;
20666 struct pending
**list_to_add
= NULL
;
20668 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
20670 baseaddr
= objfile
->text_section_offset ();
20672 name
= dwarf2_name (die
, cu
);
20673 if (name
== nullptr && (die
->tag
== DW_TAG_subprogram
20674 || die
->tag
== DW_TAG_inlined_subroutine
20675 || die
->tag
== DW_TAG_entry_point
))
20676 name
= dw2_linkage_name (die
, cu
);
20680 int suppress_add
= 0;
20685 sym
= new (&objfile
->objfile_obstack
) symbol
;
20686 OBJSTAT (objfile
, n_syms
++);
20688 /* Cache this symbol's name and the name's demangled form (if any). */
20689 sym
->set_language (cu
->lang (), &objfile
->objfile_obstack
);
20690 /* Fortran does not have mangling standard and the mangling does differ
20691 between gfortran, iFort etc. */
20692 const char *physname
20693 = (cu
->lang () == language_fortran
20694 ? dwarf2_full_name (name
, die
, cu
)
20695 : dwarf2_physname (name
, die
, cu
));
20696 const char *linkagename
= dw2_linkage_name (die
, cu
);
20698 if (linkagename
== nullptr || cu
->lang () == language_ada
)
20699 sym
->set_linkage_name (physname
);
20702 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
20703 sym
->set_linkage_name (linkagename
);
20706 /* Handle DW_AT_artificial. */
20707 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
20708 if (attr
!= nullptr)
20709 sym
->set_is_artificial (attr
->as_boolean ());
20711 /* Default assumptions.
20712 Use the passed type or decode it from the die. */
20713 sym
->set_domain (VAR_DOMAIN
);
20714 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20716 sym
->set_type (type
);
20718 sym
->set_type (die_type (die
, cu
));
20719 attr
= dwarf2_attr (die
,
20720 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
20722 if (attr
!= nullptr)
20723 sym
->set_line (attr
->constant_value (0));
20725 attr
= dwarf2_attr (die
,
20726 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
20728 if (attr
!= nullptr && attr
->is_nonnegative ())
20730 file_name_index file_index
20731 = (file_name_index
) attr
->as_nonnegative ();
20732 struct file_entry
*fe
;
20734 if (cu
->line_header
!= NULL
)
20735 fe
= cu
->line_header
->file_name_at (file_index
);
20740 complaint (_("file index out of range"));
20742 sym
->set_symtab (fe
->symtab
);
20748 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
20749 if (attr
!= nullptr)
20753 addr
= attr
->as_address ();
20754 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ baseaddr
);
20755 sym
->set_value_address (addr
);
20756 sym
->set_aclass_index (LOC_LABEL
);
20759 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20760 sym
->set_type (objfile_type (objfile
)->builtin_core_addr
);
20761 sym
->set_domain (LABEL_DOMAIN
);
20762 add_symbol_to_list (sym
, cu
->list_in_scope
);
20764 case DW_TAG_subprogram
:
20765 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20767 sym
->set_aclass_index (LOC_BLOCK
);
20768 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20769 if ((attr2
!= nullptr && attr2
->as_boolean ())
20770 || cu
->lang () == language_ada
20771 || cu
->lang () == language_fortran
)
20773 /* Subprograms marked external are stored as a global symbol.
20774 Ada and Fortran subprograms, whether marked external or
20775 not, are always stored as a global symbol, because we want
20776 to be able to access them globally. For instance, we want
20777 to be able to break on a nested subprogram without having
20778 to specify the context. */
20779 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20783 list_to_add
= cu
->list_in_scope
;
20786 case DW_TAG_inlined_subroutine
:
20787 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20789 sym
->set_aclass_index (LOC_BLOCK
);
20790 sym
->set_is_inlined (1);
20791 list_to_add
= cu
->list_in_scope
;
20793 case DW_TAG_template_value_param
:
20795 /* Fall through. */
20796 case DW_TAG_constant
:
20797 case DW_TAG_variable
:
20798 case DW_TAG_member
:
20799 /* Compilation with minimal debug info may result in
20800 variables with missing type entries. Change the
20801 misleading `void' type to something sensible. */
20802 if (sym
->type ()->code () == TYPE_CODE_VOID
)
20803 sym
->set_type (objfile_type (objfile
)->builtin_int
);
20805 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20806 /* In the case of DW_TAG_member, we should only be called for
20807 static const members. */
20808 if (die
->tag
== DW_TAG_member
)
20810 /* dwarf2_add_field uses die_is_declaration,
20811 so we do the same. */
20812 gdb_assert (die_is_declaration (die
, cu
));
20815 if (attr
!= nullptr)
20817 dwarf2_const_value (attr
, sym
, cu
);
20818 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20821 if (attr2
!= nullptr && attr2
->as_boolean ())
20822 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20824 list_to_add
= cu
->list_in_scope
;
20828 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20829 if (attr
!= nullptr)
20831 var_decode_location (attr
, sym
, cu
);
20832 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20834 /* Fortran explicitly imports any global symbols to the local
20835 scope by DW_TAG_common_block. */
20836 if (cu
->lang () == language_fortran
&& die
->parent
20837 && die
->parent
->tag
== DW_TAG_common_block
)
20840 if (sym
->aclass () == LOC_STATIC
20841 && sym
->value_address () == 0
20842 && !per_objfile
->per_bfd
->has_section_at_zero
)
20844 /* When a static variable is eliminated by the linker,
20845 the corresponding debug information is not stripped
20846 out, but the variable address is set to null;
20847 do not add such variables into symbol table. */
20849 else if (attr2
!= nullptr && attr2
->as_boolean ())
20851 if (sym
->aclass () == LOC_STATIC
20852 && (objfile
->flags
& OBJF_MAINLINE
) == 0
20853 && per_objfile
->per_bfd
->can_copy
)
20855 /* A global static variable might be subject to
20856 copy relocation. We first check for a local
20857 minsym, though, because maybe the symbol was
20858 marked hidden, in which case this would not
20860 bound_minimal_symbol found
20861 = (lookup_minimal_symbol_linkage
20862 (sym
->linkage_name (), objfile
));
20863 if (found
.minsym
!= nullptr)
20864 sym
->maybe_copied
= 1;
20867 /* A variable with DW_AT_external is never static,
20868 but it may be block-scoped. */
20870 = ((cu
->list_in_scope
20871 == cu
->get_builder ()->get_file_symbols ())
20872 ? cu
->get_builder ()->get_global_symbols ()
20873 : cu
->list_in_scope
);
20876 list_to_add
= cu
->list_in_scope
;
20880 /* We do not know the address of this symbol.
20881 If it is an external symbol and we have type information
20882 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20883 The address of the variable will then be determined from
20884 the minimal symbol table whenever the variable is
20886 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20888 /* Fortran explicitly imports any global symbols to the local
20889 scope by DW_TAG_common_block. */
20890 if (cu
->lang () == language_fortran
&& die
->parent
20891 && die
->parent
->tag
== DW_TAG_common_block
)
20893 /* SYMBOL_CLASS doesn't matter here because
20894 read_common_block is going to reset it. */
20896 list_to_add
= cu
->list_in_scope
;
20898 else if (attr2
!= nullptr && attr2
->as_boolean ()
20899 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
20901 /* A variable with DW_AT_external is never static, but it
20902 may be block-scoped. */
20904 = ((cu
->list_in_scope
20905 == cu
->get_builder ()->get_file_symbols ())
20906 ? cu
->get_builder ()->get_global_symbols ()
20907 : cu
->list_in_scope
);
20909 sym
->set_aclass_index (LOC_UNRESOLVED
);
20911 else if (!die_is_declaration (die
, cu
))
20913 /* Use the default LOC_OPTIMIZED_OUT class. */
20914 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
20916 list_to_add
= cu
->list_in_scope
;
20920 case DW_TAG_formal_parameter
:
20922 /* If we are inside a function, mark this as an argument. If
20923 not, we might be looking at an argument to an inlined function
20924 when we do not have enough information to show inlined frames;
20925 pretend it's a local variable in that case so that the user can
20927 struct context_stack
*curr
20928 = cu
->get_builder ()->get_current_context_stack ();
20929 if (curr
!= nullptr && curr
->name
!= nullptr)
20930 sym
->set_is_argument (1);
20931 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20932 if (attr
!= nullptr)
20934 var_decode_location (attr
, sym
, cu
);
20936 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20937 if (attr
!= nullptr)
20939 dwarf2_const_value (attr
, sym
, cu
);
20942 list_to_add
= cu
->list_in_scope
;
20945 case DW_TAG_unspecified_parameters
:
20946 /* From varargs functions; gdb doesn't seem to have any
20947 interest in this information, so just ignore it for now.
20950 case DW_TAG_template_type_param
:
20952 /* Fall through. */
20953 case DW_TAG_class_type
:
20954 case DW_TAG_interface_type
:
20955 case DW_TAG_structure_type
:
20956 case DW_TAG_union_type
:
20957 case DW_TAG_set_type
:
20958 case DW_TAG_enumeration_type
:
20959 case DW_TAG_namelist
:
20960 if (die
->tag
== DW_TAG_namelist
)
20962 sym
->set_aclass_index (LOC_STATIC
);
20963 sym
->set_domain (VAR_DOMAIN
);
20967 sym
->set_aclass_index (LOC_TYPEDEF
);
20968 sym
->set_domain (STRUCT_DOMAIN
);
20971 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20972 really ever be static objects: otherwise, if you try
20973 to, say, break of a class's method and you're in a file
20974 which doesn't mention that class, it won't work unless
20975 the check for all static symbols in lookup_symbol_aux
20976 saves you. See the OtherFileClass tests in
20977 gdb.c++/namespace.exp. */
20981 buildsym_compunit
*builder
= cu
->get_builder ();
20983 = (cu
->list_in_scope
== builder
->get_file_symbols ()
20984 && cu
->lang () == language_cplus
20985 ? builder
->get_global_symbols ()
20986 : cu
->list_in_scope
);
20988 /* The semantics of C++ state that "struct foo {
20989 ... }" also defines a typedef for "foo". */
20990 if (cu
->lang () == language_cplus
20991 || cu
->lang () == language_ada
20992 || cu
->lang () == language_d
20993 || cu
->lang () == language_rust
)
20995 /* The symbol's name is already allocated along
20996 with this objfile, so we don't need to
20997 duplicate it for the type. */
20998 if (sym
->type ()->name () == 0)
20999 sym
->type ()->set_name (sym
->search_name ());
21004 case DW_TAG_typedef
:
21005 sym
->set_aclass_index (LOC_TYPEDEF
);
21006 sym
->set_domain (VAR_DOMAIN
);
21007 list_to_add
= cu
->list_in_scope
;
21009 case DW_TAG_array_type
:
21010 case DW_TAG_base_type
:
21011 case DW_TAG_subrange_type
:
21012 case DW_TAG_generic_subrange
:
21013 sym
->set_aclass_index (LOC_TYPEDEF
);
21014 sym
->set_domain (VAR_DOMAIN
);
21015 list_to_add
= cu
->list_in_scope
;
21017 case DW_TAG_enumerator
:
21018 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
21019 if (attr
!= nullptr)
21021 dwarf2_const_value (attr
, sym
, cu
);
21024 /* NOTE: carlton/2003-11-10: See comment above in the
21025 DW_TAG_class_type, etc. block. */
21028 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
21029 && cu
->lang () == language_cplus
21030 ? cu
->get_builder ()->get_global_symbols ()
21031 : cu
->list_in_scope
);
21034 case DW_TAG_imported_declaration
:
21035 case DW_TAG_namespace
:
21036 sym
->set_aclass_index (LOC_TYPEDEF
);
21037 list_to_add
= cu
->get_builder ()->get_global_symbols ();
21039 case DW_TAG_module
:
21040 sym
->set_aclass_index (LOC_TYPEDEF
);
21041 sym
->set_domain (MODULE_DOMAIN
);
21042 list_to_add
= cu
->get_builder ()->get_global_symbols ();
21044 case DW_TAG_common_block
:
21045 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
21046 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
21047 add_symbol_to_list (sym
, cu
->list_in_scope
);
21050 /* Not a tag we recognize. Hopefully we aren't processing
21051 trash data, but since we must specifically ignore things
21052 we don't recognize, there is nothing else we should do at
21054 complaint (_("unsupported tag: '%s'"),
21055 dwarf_tag_name (die
->tag
));
21061 sym
->hash_next
= objfile
->template_symbols
;
21062 objfile
->template_symbols
= sym
;
21063 list_to_add
= NULL
;
21066 if (list_to_add
!= NULL
)
21067 add_symbol_to_list (sym
, list_to_add
);
21069 /* For the benefit of old versions of GCC, check for anonymous
21070 namespaces based on the demangled name. */
21071 if (!cu
->processing_has_namespace_info
21072 && cu
->lang () == language_cplus
)
21073 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
21078 /* Given an attr with a DW_FORM_dataN value in host byte order,
21079 zero-extend it as appropriate for the symbol's type. The DWARF
21080 standard (v4) is not entirely clear about the meaning of using
21081 DW_FORM_dataN for a constant with a signed type, where the type is
21082 wider than the data. The conclusion of a discussion on the DWARF
21083 list was that this is unspecified. We choose to always zero-extend
21084 because that is the interpretation long in use by GCC. */
21087 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
21088 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
21090 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21091 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
) ?
21092 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
21093 LONGEST l
= attr
->constant_value (0);
21095 if (bits
< sizeof (*value
) * 8)
21097 l
&= ((LONGEST
) 1 << bits
) - 1;
21100 else if (bits
== sizeof (*value
) * 8)
21104 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
21105 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
21112 /* Read a constant value from an attribute. Either set *VALUE, or if
21113 the value does not fit in *VALUE, set *BYTES - either already
21114 allocated on the objfile obstack, or newly allocated on OBSTACK,
21115 or, set *BATON, if we translated the constant to a location
21119 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
21120 const char *name
, struct obstack
*obstack
,
21121 struct dwarf2_cu
*cu
,
21122 LONGEST
*value
, const gdb_byte
**bytes
,
21123 struct dwarf2_locexpr_baton
**baton
)
21125 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21126 struct objfile
*objfile
= per_objfile
->objfile
;
21127 struct comp_unit_head
*cu_header
= &cu
->header
;
21128 struct dwarf_block
*blk
;
21129 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
) ?
21130 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
21136 switch (attr
->form
)
21139 case DW_FORM_addrx
:
21140 case DW_FORM_GNU_addr_index
:
21144 if (TYPE_LENGTH (type
) != cu_header
->addr_size
)
21145 dwarf2_const_value_length_mismatch_complaint (name
,
21146 cu_header
->addr_size
,
21147 TYPE_LENGTH (type
));
21148 /* Symbols of this form are reasonably rare, so we just
21149 piggyback on the existing location code rather than writing
21150 a new implementation of symbol_computed_ops. */
21151 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
21152 (*baton
)->per_objfile
= per_objfile
;
21153 (*baton
)->per_cu
= cu
->per_cu
;
21154 gdb_assert ((*baton
)->per_cu
);
21156 (*baton
)->size
= 2 + cu_header
->addr_size
;
21157 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
21158 (*baton
)->data
= data
;
21160 data
[0] = DW_OP_addr
;
21161 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
21162 byte_order
, attr
->as_address ());
21163 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
21166 case DW_FORM_string
:
21169 case DW_FORM_GNU_str_index
:
21170 case DW_FORM_GNU_strp_alt
:
21171 /* The string is already allocated on the objfile obstack, point
21173 *bytes
= (const gdb_byte
*) attr
->as_string ();
21175 case DW_FORM_block1
:
21176 case DW_FORM_block2
:
21177 case DW_FORM_block4
:
21178 case DW_FORM_block
:
21179 case DW_FORM_exprloc
:
21180 case DW_FORM_data16
:
21181 blk
= attr
->as_block ();
21182 if (TYPE_LENGTH (type
) != blk
->size
)
21183 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
21184 TYPE_LENGTH (type
));
21185 *bytes
= blk
->data
;
21188 /* The DW_AT_const_value attributes are supposed to carry the
21189 symbol's value "represented as it would be on the target
21190 architecture." By the time we get here, it's already been
21191 converted to host endianness, so we just need to sign- or
21192 zero-extend it as appropriate. */
21193 case DW_FORM_data1
:
21194 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
21196 case DW_FORM_data2
:
21197 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
21199 case DW_FORM_data4
:
21200 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
21202 case DW_FORM_data8
:
21203 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
21206 case DW_FORM_sdata
:
21207 case DW_FORM_implicit_const
:
21208 *value
= attr
->as_signed ();
21211 case DW_FORM_udata
:
21212 *value
= attr
->as_unsigned ();
21216 complaint (_("unsupported const value attribute form: '%s'"),
21217 dwarf_form_name (attr
->form
));
21224 /* Copy constant value from an attribute to a symbol. */
21227 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
21228 struct dwarf2_cu
*cu
)
21230 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21232 const gdb_byte
*bytes
;
21233 struct dwarf2_locexpr_baton
*baton
;
21235 dwarf2_const_value_attr (attr
, sym
->type (),
21236 sym
->print_name (),
21237 &objfile
->objfile_obstack
, cu
,
21238 &value
, &bytes
, &baton
);
21242 SYMBOL_LOCATION_BATON (sym
) = baton
;
21243 sym
->set_aclass_index (dwarf2_locexpr_index
);
21245 else if (bytes
!= NULL
)
21247 sym
->set_value_bytes (bytes
);
21248 sym
->set_aclass_index (LOC_CONST_BYTES
);
21252 sym
->set_value_longest (value
);
21253 sym
->set_aclass_index (LOC_CONST
);
21257 /* Return the type of the die in question using its DW_AT_type attribute. */
21259 static struct type
*
21260 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21262 struct attribute
*type_attr
;
21264 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
21267 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21268 /* A missing DW_AT_type represents a void type. */
21269 return objfile_type (objfile
)->builtin_void
;
21272 return lookup_die_type (die
, type_attr
, cu
);
21275 /* True iff CU's producer generates GNAT Ada auxiliary information
21276 that allows to find parallel types through that information instead
21277 of having to do expensive parallel lookups by type name. */
21280 need_gnat_info (struct dwarf2_cu
*cu
)
21282 /* Assume that the Ada compiler was GNAT, which always produces
21283 the auxiliary information. */
21284 return (cu
->lang () == language_ada
);
21287 /* Return the auxiliary type of the die in question using its
21288 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21289 attribute is not present. */
21291 static struct type
*
21292 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21294 struct attribute
*type_attr
;
21296 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
21300 return lookup_die_type (die
, type_attr
, cu
);
21303 /* If DIE has a descriptive_type attribute, then set the TYPE's
21304 descriptive type accordingly. */
21307 set_descriptive_type (struct type
*type
, struct die_info
*die
,
21308 struct dwarf2_cu
*cu
)
21310 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
21312 if (descriptive_type
)
21314 ALLOCATE_GNAT_AUX_TYPE (type
);
21315 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
21319 /* Return the containing type of the die in question using its
21320 DW_AT_containing_type attribute. */
21322 static struct type
*
21323 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21325 struct attribute
*type_attr
;
21326 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21328 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
21330 error (_("Dwarf Error: Problem turning containing type into gdb type "
21331 "[in module %s]"), objfile_name (objfile
));
21333 return lookup_die_type (die
, type_attr
, cu
);
21336 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21338 static struct type
*
21339 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
21341 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21342 struct objfile
*objfile
= per_objfile
->objfile
;
21345 std::string message
21346 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21347 objfile_name (objfile
),
21348 sect_offset_str (cu
->header
.sect_off
),
21349 sect_offset_str (die
->sect_off
));
21350 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
21352 return init_type (objfile
, TYPE_CODE_ERROR
, 0, saved
);
21355 /* Look up the type of DIE in CU using its type attribute ATTR.
21356 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21357 DW_AT_containing_type.
21358 If there is no type substitute an error marker. */
21360 static struct type
*
21361 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
21362 struct dwarf2_cu
*cu
)
21364 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21365 struct objfile
*objfile
= per_objfile
->objfile
;
21366 struct type
*this_type
;
21368 gdb_assert (attr
->name
== DW_AT_type
21369 || attr
->name
== DW_AT_GNAT_descriptive_type
21370 || attr
->name
== DW_AT_containing_type
);
21372 /* First see if we have it cached. */
21374 if (attr
->form
== DW_FORM_GNU_ref_alt
)
21376 struct dwarf2_per_cu_data
*per_cu
;
21377 sect_offset sect_off
= attr
->get_ref_die_offset ();
21379 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
21380 per_objfile
->per_bfd
);
21381 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
21383 else if (attr
->form_is_ref ())
21385 sect_offset sect_off
= attr
->get_ref_die_offset ();
21387 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
21389 else if (attr
->form
== DW_FORM_ref_sig8
)
21391 ULONGEST signature
= attr
->as_signature ();
21393 return get_signatured_type (die
, signature
, cu
);
21397 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21398 " at %s [in module %s]"),
21399 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
21400 objfile_name (objfile
));
21401 return build_error_marker_type (cu
, die
);
21404 /* If not cached we need to read it in. */
21406 if (this_type
== NULL
)
21408 struct die_info
*type_die
= NULL
;
21409 struct dwarf2_cu
*type_cu
= cu
;
21411 if (attr
->form_is_ref ())
21412 type_die
= follow_die_ref (die
, attr
, &type_cu
);
21413 if (type_die
== NULL
)
21414 return build_error_marker_type (cu
, die
);
21415 /* If we find the type now, it's probably because the type came
21416 from an inter-CU reference and the type's CU got expanded before
21418 this_type
= read_type_die (type_die
, type_cu
);
21421 /* If we still don't have a type use an error marker. */
21423 if (this_type
== NULL
)
21424 return build_error_marker_type (cu
, die
);
21429 /* Return the type in DIE, CU.
21430 Returns NULL for invalid types.
21432 This first does a lookup in die_type_hash,
21433 and only reads the die in if necessary.
21435 NOTE: This can be called when reading in partial or full symbols. */
21437 static struct type
*
21438 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
21440 struct type
*this_type
;
21442 this_type
= get_die_type (die
, cu
);
21446 return read_type_die_1 (die
, cu
);
21449 /* Read the type in DIE, CU.
21450 Returns NULL for invalid types. */
21452 static struct type
*
21453 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
21455 struct type
*this_type
= NULL
;
21459 case DW_TAG_class_type
:
21460 case DW_TAG_interface_type
:
21461 case DW_TAG_structure_type
:
21462 case DW_TAG_union_type
:
21463 this_type
= read_structure_type (die
, cu
);
21465 case DW_TAG_enumeration_type
:
21466 this_type
= read_enumeration_type (die
, cu
);
21468 case DW_TAG_subprogram
:
21469 case DW_TAG_subroutine_type
:
21470 case DW_TAG_inlined_subroutine
:
21471 this_type
= read_subroutine_type (die
, cu
);
21473 case DW_TAG_array_type
:
21474 this_type
= read_array_type (die
, cu
);
21476 case DW_TAG_set_type
:
21477 this_type
= read_set_type (die
, cu
);
21479 case DW_TAG_pointer_type
:
21480 this_type
= read_tag_pointer_type (die
, cu
);
21482 case DW_TAG_ptr_to_member_type
:
21483 this_type
= read_tag_ptr_to_member_type (die
, cu
);
21485 case DW_TAG_reference_type
:
21486 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
21488 case DW_TAG_rvalue_reference_type
:
21489 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
21491 case DW_TAG_const_type
:
21492 this_type
= read_tag_const_type (die
, cu
);
21494 case DW_TAG_volatile_type
:
21495 this_type
= read_tag_volatile_type (die
, cu
);
21497 case DW_TAG_restrict_type
:
21498 this_type
= read_tag_restrict_type (die
, cu
);
21500 case DW_TAG_string_type
:
21501 this_type
= read_tag_string_type (die
, cu
);
21503 case DW_TAG_typedef
:
21504 this_type
= read_typedef (die
, cu
);
21506 case DW_TAG_generic_subrange
:
21507 case DW_TAG_subrange_type
:
21508 this_type
= read_subrange_type (die
, cu
);
21510 case DW_TAG_base_type
:
21511 this_type
= read_base_type (die
, cu
);
21513 case DW_TAG_unspecified_type
:
21514 this_type
= read_unspecified_type (die
, cu
);
21516 case DW_TAG_namespace
:
21517 this_type
= read_namespace_type (die
, cu
);
21519 case DW_TAG_module
:
21520 this_type
= read_module_type (die
, cu
);
21522 case DW_TAG_atomic_type
:
21523 this_type
= read_tag_atomic_type (die
, cu
);
21526 complaint (_("unexpected tag in read_type_die: '%s'"),
21527 dwarf_tag_name (die
->tag
));
21534 /* See if we can figure out if the class lives in a namespace. We do
21535 this by looking for a member function; its demangled name will
21536 contain namespace info, if there is any.
21537 Return the computed name or NULL.
21538 Space for the result is allocated on the objfile's obstack.
21539 This is the full-die version of guess_partial_die_structure_name.
21540 In this case we know DIE has no useful parent. */
21542 static const char *
21543 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21545 struct die_info
*spec_die
;
21546 struct dwarf2_cu
*spec_cu
;
21547 struct die_info
*child
;
21548 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21551 spec_die
= die_specification (die
, &spec_cu
);
21552 if (spec_die
!= NULL
)
21558 for (child
= die
->child
;
21560 child
= child
->sibling
)
21562 if (child
->tag
== DW_TAG_subprogram
)
21564 const char *linkage_name
= dw2_linkage_name (child
, cu
);
21566 if (linkage_name
!= NULL
)
21568 gdb::unique_xmalloc_ptr
<char> actual_name
21569 (cu
->language_defn
->class_name_from_physname (linkage_name
));
21570 const char *name
= NULL
;
21572 if (actual_name
!= NULL
)
21574 const char *die_name
= dwarf2_name (die
, cu
);
21576 if (die_name
!= NULL
21577 && strcmp (die_name
, actual_name
.get ()) != 0)
21579 /* Strip off the class name from the full name.
21580 We want the prefix. */
21581 int die_name_len
= strlen (die_name
);
21582 int actual_name_len
= strlen (actual_name
.get ());
21583 const char *ptr
= actual_name
.get ();
21585 /* Test for '::' as a sanity check. */
21586 if (actual_name_len
> die_name_len
+ 2
21587 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
21588 name
= obstack_strndup (
21589 &objfile
->per_bfd
->storage_obstack
,
21590 ptr
, actual_name_len
- die_name_len
- 2);
21601 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21602 prefix part in such case. See
21603 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21605 static const char *
21606 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21608 struct attribute
*attr
;
21611 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
21612 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
21615 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
21618 attr
= dw2_linkage_name_attr (die
, cu
);
21619 const char *attr_name
= attr
->as_string ();
21620 if (attr
== NULL
|| attr_name
== NULL
)
21623 /* dwarf2_name had to be already called. */
21624 gdb_assert (attr
->canonical_string_p ());
21626 /* Strip the base name, keep any leading namespaces/classes. */
21627 base
= strrchr (attr_name
, ':');
21628 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
21631 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21632 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
21634 &base
[-1] - attr_name
);
21637 /* Return the name of the namespace/class that DIE is defined within,
21638 or "" if we can't tell. The caller should not xfree the result.
21640 For example, if we're within the method foo() in the following
21650 then determine_prefix on foo's die will return "N::C". */
21652 static const char *
21653 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21655 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21656 struct die_info
*parent
, *spec_die
;
21657 struct dwarf2_cu
*spec_cu
;
21658 struct type
*parent_type
;
21659 const char *retval
;
21661 if (cu
->lang () != language_cplus
21662 && cu
->lang () != language_fortran
21663 && cu
->lang () != language_d
21664 && cu
->lang () != language_rust
)
21667 retval
= anonymous_struct_prefix (die
, cu
);
21671 /* We have to be careful in the presence of DW_AT_specification.
21672 For example, with GCC 3.4, given the code
21676 // Definition of N::foo.
21680 then we'll have a tree of DIEs like this:
21682 1: DW_TAG_compile_unit
21683 2: DW_TAG_namespace // N
21684 3: DW_TAG_subprogram // declaration of N::foo
21685 4: DW_TAG_subprogram // definition of N::foo
21686 DW_AT_specification // refers to die #3
21688 Thus, when processing die #4, we have to pretend that we're in
21689 the context of its DW_AT_specification, namely the contex of die
21692 spec_die
= die_specification (die
, &spec_cu
);
21693 if (spec_die
== NULL
)
21694 parent
= die
->parent
;
21697 parent
= spec_die
->parent
;
21701 if (parent
== NULL
)
21703 else if (parent
->building_fullname
)
21706 const char *parent_name
;
21708 /* It has been seen on RealView 2.2 built binaries,
21709 DW_TAG_template_type_param types actually _defined_ as
21710 children of the parent class:
21713 template class <class Enum> Class{};
21714 Class<enum E> class_e;
21716 1: DW_TAG_class_type (Class)
21717 2: DW_TAG_enumeration_type (E)
21718 3: DW_TAG_enumerator (enum1:0)
21719 3: DW_TAG_enumerator (enum2:1)
21721 2: DW_TAG_template_type_param
21722 DW_AT_type DW_FORM_ref_udata (E)
21724 Besides being broken debug info, it can put GDB into an
21725 infinite loop. Consider:
21727 When we're building the full name for Class<E>, we'll start
21728 at Class, and go look over its template type parameters,
21729 finding E. We'll then try to build the full name of E, and
21730 reach here. We're now trying to build the full name of E,
21731 and look over the parent DIE for containing scope. In the
21732 broken case, if we followed the parent DIE of E, we'd again
21733 find Class, and once again go look at its template type
21734 arguments, etc., etc. Simply don't consider such parent die
21735 as source-level parent of this die (it can't be, the language
21736 doesn't allow it), and break the loop here. */
21737 name
= dwarf2_name (die
, cu
);
21738 parent_name
= dwarf2_name (parent
, cu
);
21739 complaint (_("template param type '%s' defined within parent '%s'"),
21740 name
? name
: "<unknown>",
21741 parent_name
? parent_name
: "<unknown>");
21745 switch (parent
->tag
)
21747 case DW_TAG_namespace
:
21748 parent_type
= read_type_die (parent
, cu
);
21749 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21750 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21751 Work around this problem here. */
21752 if (cu
->lang () == language_cplus
21753 && strcmp (parent_type
->name (), "::") == 0)
21755 /* We give a name to even anonymous namespaces. */
21756 return parent_type
->name ();
21757 case DW_TAG_class_type
:
21758 case DW_TAG_interface_type
:
21759 case DW_TAG_structure_type
:
21760 case DW_TAG_union_type
:
21761 case DW_TAG_module
:
21762 parent_type
= read_type_die (parent
, cu
);
21763 if (parent_type
->name () != NULL
)
21764 return parent_type
->name ();
21766 /* An anonymous structure is only allowed non-static data
21767 members; no typedefs, no member functions, et cetera.
21768 So it does not need a prefix. */
21770 case DW_TAG_compile_unit
:
21771 case DW_TAG_partial_unit
:
21772 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21773 if (cu
->lang () == language_cplus
21774 && !per_objfile
->per_bfd
->types
.empty ()
21775 && die
->child
!= NULL
21776 && (die
->tag
== DW_TAG_class_type
21777 || die
->tag
== DW_TAG_structure_type
21778 || die
->tag
== DW_TAG_union_type
))
21780 const char *name
= guess_full_die_structure_name (die
, cu
);
21785 case DW_TAG_subprogram
:
21786 /* Nested subroutines in Fortran get a prefix with the name
21787 of the parent's subroutine. */
21788 if (cu
->lang () == language_fortran
)
21790 if ((die
->tag
== DW_TAG_subprogram
)
21791 && (dwarf2_name (parent
, cu
) != NULL
))
21792 return dwarf2_name (parent
, cu
);
21795 case DW_TAG_enumeration_type
:
21796 parent_type
= read_type_die (parent
, cu
);
21797 if (parent_type
->is_declared_class ())
21799 if (parent_type
->name () != NULL
)
21800 return parent_type
->name ();
21803 /* Fall through. */
21805 return determine_prefix (parent
, cu
);
21809 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21810 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21811 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21812 an obconcat, otherwise allocate storage for the result. The CU argument is
21813 used to determine the language and hence, the appropriate separator. */
21815 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21818 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
21819 int physname
, struct dwarf2_cu
*cu
)
21821 const char *lead
= "";
21824 if (suffix
== NULL
|| suffix
[0] == '\0'
21825 || prefix
== NULL
|| prefix
[0] == '\0')
21827 else if (cu
->lang () == language_d
)
21829 /* For D, the 'main' function could be defined in any module, but it
21830 should never be prefixed. */
21831 if (strcmp (suffix
, "D main") == 0)
21839 else if (cu
->lang () == language_fortran
&& physname
)
21841 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21842 DW_AT_MIPS_linkage_name is preferred and used instead. */
21850 if (prefix
== NULL
)
21852 if (suffix
== NULL
)
21859 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
21861 strcpy (retval
, lead
);
21862 strcat (retval
, prefix
);
21863 strcat (retval
, sep
);
21864 strcat (retval
, suffix
);
21869 /* We have an obstack. */
21870 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
21874 /* Get name of a die, return NULL if not found. */
21876 static const char *
21877 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
21878 struct objfile
*objfile
)
21880 if (name
&& cu
->lang () == language_cplus
)
21882 gdb::unique_xmalloc_ptr
<char> canon_name
21883 = cp_canonicalize_string (name
);
21885 if (canon_name
!= nullptr)
21886 name
= objfile
->intern (canon_name
.get ());
21892 /* Get name of a die, return NULL if not found.
21893 Anonymous namespaces are converted to their magic string. */
21895 static const char *
21896 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21898 struct attribute
*attr
;
21899 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21901 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
21902 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21903 if (attr_name
== nullptr
21904 && die
->tag
!= DW_TAG_namespace
21905 && die
->tag
!= DW_TAG_class_type
21906 && die
->tag
!= DW_TAG_interface_type
21907 && die
->tag
!= DW_TAG_structure_type
21908 && die
->tag
!= DW_TAG_namelist
21909 && die
->tag
!= DW_TAG_union_type
)
21914 case DW_TAG_compile_unit
:
21915 case DW_TAG_partial_unit
:
21916 /* Compilation units have a DW_AT_name that is a filename, not
21917 a source language identifier. */
21918 case DW_TAG_enumeration_type
:
21919 case DW_TAG_enumerator
:
21920 /* These tags always have simple identifiers already; no need
21921 to canonicalize them. */
21924 case DW_TAG_namespace
:
21925 if (attr_name
!= nullptr)
21927 return CP_ANONYMOUS_NAMESPACE_STR
;
21929 case DW_TAG_class_type
:
21930 case DW_TAG_interface_type
:
21931 case DW_TAG_structure_type
:
21932 case DW_TAG_union_type
:
21933 case DW_TAG_namelist
:
21934 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21935 structures or unions. These were of the form "._%d" in GCC 4.1,
21936 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21937 and GCC 4.4. We work around this problem by ignoring these. */
21938 if (attr_name
!= nullptr
21939 && (startswith (attr_name
, "._")
21940 || startswith (attr_name
, "<anonymous")))
21943 /* GCC might emit a nameless typedef that has a linkage name. See
21944 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21945 if (!attr
|| attr_name
== NULL
)
21947 attr
= dw2_linkage_name_attr (die
, cu
);
21948 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21949 if (attr
== NULL
|| attr_name
== NULL
)
21952 /* Avoid demangling attr_name the second time on a second
21953 call for the same DIE. */
21954 if (!attr
->canonical_string_p ())
21956 gdb::unique_xmalloc_ptr
<char> demangled
21957 (gdb_demangle (attr_name
, DMGL_TYPES
));
21958 if (demangled
== nullptr)
21961 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
21962 attr_name
= attr
->as_string ();
21965 /* Strip any leading namespaces/classes, keep only the
21966 base name. DW_AT_name for named DIEs does not
21967 contain the prefixes. */
21968 const char *base
= strrchr (attr_name
, ':');
21969 if (base
&& base
> attr_name
&& base
[-1] == ':')
21980 if (!attr
->canonical_string_p ())
21981 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
21983 return attr
->as_string ();
21986 /* Return the die that this die in an extension of, or NULL if there
21987 is none. *EXT_CU is the CU containing DIE on input, and the CU
21988 containing the return value on output. */
21990 static struct die_info
*
21991 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
21993 struct attribute
*attr
;
21995 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
21999 return follow_die_ref (die
, attr
, ext_cu
);
22003 dump_die_shallow (struct ui_file
*f
, int indent
, struct die_info
*die
)
22007 gdb_printf (f
, "%*sDie: %s (abbrev %d, offset %s)\n",
22009 dwarf_tag_name (die
->tag
), die
->abbrev
,
22010 sect_offset_str (die
->sect_off
));
22012 if (die
->parent
!= NULL
)
22013 gdb_printf (f
, "%*s parent at offset: %s\n",
22015 sect_offset_str (die
->parent
->sect_off
));
22017 gdb_printf (f
, "%*s has children: %s\n",
22019 dwarf_bool_name (die
->child
!= NULL
));
22021 gdb_printf (f
, "%*s attributes:\n", indent
, "");
22023 for (i
= 0; i
< die
->num_attrs
; ++i
)
22025 gdb_printf (f
, "%*s %s (%s) ",
22027 dwarf_attr_name (die
->attrs
[i
].name
),
22028 dwarf_form_name (die
->attrs
[i
].form
));
22030 switch (die
->attrs
[i
].form
)
22033 case DW_FORM_addrx
:
22034 case DW_FORM_GNU_addr_index
:
22035 gdb_printf (f
, "address: ");
22036 gdb_puts (hex_string (die
->attrs
[i
].as_address ()), f
);
22038 case DW_FORM_block2
:
22039 case DW_FORM_block4
:
22040 case DW_FORM_block
:
22041 case DW_FORM_block1
:
22042 gdb_printf (f
, "block: size %s",
22043 pulongest (die
->attrs
[i
].as_block ()->size
));
22045 case DW_FORM_exprloc
:
22046 gdb_printf (f
, "expression: size %s",
22047 pulongest (die
->attrs
[i
].as_block ()->size
));
22049 case DW_FORM_data16
:
22050 gdb_printf (f
, "constant of 16 bytes");
22052 case DW_FORM_ref_addr
:
22053 gdb_printf (f
, "ref address: ");
22054 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
22056 case DW_FORM_GNU_ref_alt
:
22057 gdb_printf (f
, "alt ref address: ");
22058 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
22064 case DW_FORM_ref_udata
:
22065 gdb_printf (f
, "constant ref: 0x%lx (adjusted)",
22066 (long) (die
->attrs
[i
].as_unsigned ()));
22068 case DW_FORM_data1
:
22069 case DW_FORM_data2
:
22070 case DW_FORM_data4
:
22071 case DW_FORM_data8
:
22072 case DW_FORM_udata
:
22073 gdb_printf (f
, "constant: %s",
22074 pulongest (die
->attrs
[i
].as_unsigned ()));
22076 case DW_FORM_sec_offset
:
22077 gdb_printf (f
, "section offset: %s",
22078 pulongest (die
->attrs
[i
].as_unsigned ()));
22080 case DW_FORM_ref_sig8
:
22081 gdb_printf (f
, "signature: %s",
22082 hex_string (die
->attrs
[i
].as_signature ()));
22084 case DW_FORM_string
:
22086 case DW_FORM_line_strp
:
22088 case DW_FORM_GNU_str_index
:
22089 case DW_FORM_GNU_strp_alt
:
22090 gdb_printf (f
, "string: \"%s\" (%s canonicalized)",
22091 die
->attrs
[i
].as_string ()
22092 ? die
->attrs
[i
].as_string () : "",
22093 die
->attrs
[i
].canonical_string_p () ? "is" : "not");
22096 if (die
->attrs
[i
].as_boolean ())
22097 gdb_printf (f
, "flag: TRUE");
22099 gdb_printf (f
, "flag: FALSE");
22101 case DW_FORM_flag_present
:
22102 gdb_printf (f
, "flag: TRUE");
22104 case DW_FORM_indirect
:
22105 /* The reader will have reduced the indirect form to
22106 the "base form" so this form should not occur. */
22108 "unexpected attribute form: DW_FORM_indirect");
22110 case DW_FORM_sdata
:
22111 case DW_FORM_implicit_const
:
22112 gdb_printf (f
, "constant: %s",
22113 plongest (die
->attrs
[i
].as_signed ()));
22116 gdb_printf (f
, "unsupported attribute form: %d.",
22117 die
->attrs
[i
].form
);
22120 gdb_printf (f
, "\n");
22125 dump_die_for_error (struct die_info
*die
)
22127 dump_die_shallow (gdb_stderr
, 0, die
);
22131 dump_die_1 (struct ui_file
*f
, int level
, int max_level
, struct die_info
*die
)
22133 int indent
= level
* 4;
22135 gdb_assert (die
!= NULL
);
22137 if (level
>= max_level
)
22140 dump_die_shallow (f
, indent
, die
);
22142 if (die
->child
!= NULL
)
22144 gdb_printf (f
, "%*s Children:", indent
, "");
22145 if (level
+ 1 < max_level
)
22147 gdb_printf (f
, "\n");
22148 dump_die_1 (f
, level
+ 1, max_level
, die
->child
);
22153 " [not printed, max nesting level reached]\n");
22157 if (die
->sibling
!= NULL
&& level
> 0)
22159 dump_die_1 (f
, level
, max_level
, die
->sibling
);
22163 /* This is called from the pdie macro in gdbinit.in.
22164 It's not static so gcc will keep a copy callable from gdb. */
22167 dump_die (struct die_info
*die
, int max_level
)
22169 dump_die_1 (gdb_stdlog
, 0, max_level
, die
);
22173 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
22177 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
22178 to_underlying (die
->sect_off
),
22184 /* Follow reference or signature attribute ATTR of SRC_DIE.
22185 On entry *REF_CU is the CU of SRC_DIE.
22186 On exit *REF_CU is the CU of the result. */
22188 static struct die_info
*
22189 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22190 struct dwarf2_cu
**ref_cu
)
22192 struct die_info
*die
;
22194 if (attr
->form_is_ref ())
22195 die
= follow_die_ref (src_die
, attr
, ref_cu
);
22196 else if (attr
->form
== DW_FORM_ref_sig8
)
22197 die
= follow_die_sig (src_die
, attr
, ref_cu
);
22200 dump_die_for_error (src_die
);
22201 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22202 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22208 /* Follow reference OFFSET.
22209 On entry *REF_CU is the CU of the source die referencing OFFSET.
22210 On exit *REF_CU is the CU of the result.
22211 Returns NULL if OFFSET is invalid. */
22213 static struct die_info
*
22214 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
22215 struct dwarf2_cu
**ref_cu
)
22217 struct die_info temp_die
;
22218 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
22219 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22221 gdb_assert (cu
->per_cu
!= NULL
);
22225 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22226 "source CU contains target offset: %d",
22227 sect_offset_str (cu
->per_cu
->sect_off
),
22228 sect_offset_str (sect_off
),
22229 cu
->header
.offset_in_cu_p (sect_off
));
22231 if (cu
->per_cu
->is_debug_types
)
22233 /* .debug_types CUs cannot reference anything outside their CU.
22234 If they need to, they have to reference a signatured type via
22235 DW_FORM_ref_sig8. */
22236 if (!cu
->header
.offset_in_cu_p (sect_off
))
22239 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
22240 || !cu
->header
.offset_in_cu_p (sect_off
))
22242 struct dwarf2_per_cu_data
*per_cu
;
22244 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
22245 per_objfile
->per_bfd
);
22247 dwarf_read_debug_printf_v ("target CU offset: %s, "
22248 "target CU DIEs loaded: %d",
22249 sect_offset_str (per_cu
->sect_off
),
22250 per_objfile
->get_cu (per_cu
) != nullptr);
22252 /* If necessary, add it to the queue and load its DIEs.
22254 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22255 it doesn't mean they are currently loaded. Since we require them
22256 to be loaded, we must check for ourselves. */
22257 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->lang ())
22258 || per_objfile
->get_cu (per_cu
) == nullptr)
22259 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
22260 false, cu
->lang ());
22262 target_cu
= per_objfile
->get_cu (per_cu
);
22263 gdb_assert (target_cu
!= nullptr);
22265 else if (cu
->dies
== NULL
)
22267 /* We're loading full DIEs during partial symbol reading. */
22268 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
22272 *ref_cu
= target_cu
;
22273 temp_die
.sect_off
= sect_off
;
22275 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
22277 to_underlying (sect_off
));
22280 /* Follow reference attribute ATTR of SRC_DIE.
22281 On entry *REF_CU is the CU of SRC_DIE.
22282 On exit *REF_CU is the CU of the result. */
22284 static struct die_info
*
22285 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
22286 struct dwarf2_cu
**ref_cu
)
22288 sect_offset sect_off
= attr
->get_ref_die_offset ();
22289 struct dwarf2_cu
*cu
= *ref_cu
;
22290 struct die_info
*die
;
22292 die
= follow_die_offset (sect_off
,
22293 (attr
->form
== DW_FORM_GNU_ref_alt
22294 || cu
->per_cu
->is_dwz
),
22297 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22298 "at %s [in module %s]"),
22299 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
22300 objfile_name (cu
->per_objfile
->objfile
));
22307 struct dwarf2_locexpr_baton
22308 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
22309 dwarf2_per_cu_data
*per_cu
,
22310 dwarf2_per_objfile
*per_objfile
,
22311 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
22312 bool resolve_abstract_p
)
22314 struct die_info
*die
;
22315 struct attribute
*attr
;
22316 struct dwarf2_locexpr_baton retval
;
22317 struct objfile
*objfile
= per_objfile
->objfile
;
22319 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22321 cu
= load_cu (per_cu
, per_objfile
, false);
22325 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22326 Instead just throw an error, not much else we can do. */
22327 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22328 sect_offset_str (sect_off
), objfile_name (objfile
));
22331 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22333 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22334 sect_offset_str (sect_off
), objfile_name (objfile
));
22336 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22337 if (!attr
&& resolve_abstract_p
22338 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
22339 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
22341 CORE_ADDR pc
= get_frame_pc ();
22342 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
22343 struct gdbarch
*gdbarch
= objfile
->arch ();
22345 for (const auto &cand_off
22346 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
22348 struct dwarf2_cu
*cand_cu
= cu
;
22349 struct die_info
*cand
22350 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
22353 || cand
->parent
->tag
!= DW_TAG_subprogram
)
22356 CORE_ADDR pc_low
, pc_high
;
22357 get_scope_pc_bounds (cand
->parent
, &pc_low
, &pc_high
, cu
);
22358 if (pc_low
== ((CORE_ADDR
) -1))
22360 pc_low
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_low
+ baseaddr
);
22361 pc_high
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_high
+ baseaddr
);
22362 if (!(pc_low
<= pc
&& pc
< pc_high
))
22366 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22373 /* DWARF: "If there is no such attribute, then there is no effect.".
22374 DATA is ignored if SIZE is 0. */
22376 retval
.data
= NULL
;
22379 else if (attr
->form_is_section_offset ())
22381 struct dwarf2_loclist_baton loclist_baton
;
22382 CORE_ADDR pc
= get_frame_pc ();
22385 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
22387 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
22389 retval
.size
= size
;
22393 if (!attr
->form_is_block ())
22394 error (_("Dwarf Error: DIE at %s referenced in module %s "
22395 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22396 sect_offset_str (sect_off
), objfile_name (objfile
));
22398 struct dwarf_block
*block
= attr
->as_block ();
22399 retval
.data
= block
->data
;
22400 retval
.size
= block
->size
;
22402 retval
.per_objfile
= per_objfile
;
22403 retval
.per_cu
= cu
->per_cu
;
22405 per_objfile
->age_comp_units ();
22412 struct dwarf2_locexpr_baton
22413 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
22414 dwarf2_per_cu_data
*per_cu
,
22415 dwarf2_per_objfile
*per_objfile
,
22416 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
22418 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
22420 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
22424 /* Write a constant of a given type as target-ordered bytes into
22427 static const gdb_byte
*
22428 write_constant_as_bytes (struct obstack
*obstack
,
22429 enum bfd_endian byte_order
,
22436 *len
= TYPE_LENGTH (type
);
22437 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22438 store_unsigned_integer (result
, *len
, byte_order
, value
);
22446 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
22447 dwarf2_per_cu_data
*per_cu
,
22448 dwarf2_per_objfile
*per_objfile
,
22452 struct die_info
*die
;
22453 struct attribute
*attr
;
22454 const gdb_byte
*result
= NULL
;
22457 enum bfd_endian byte_order
;
22458 struct objfile
*objfile
= per_objfile
->objfile
;
22460 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22462 cu
= load_cu (per_cu
, per_objfile
, false);
22466 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22467 Instead just throw an error, not much else we can do. */
22468 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22469 sect_offset_str (sect_off
), objfile_name (objfile
));
22472 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22474 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22475 sect_offset_str (sect_off
), objfile_name (objfile
));
22477 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
22481 byte_order
= (bfd_big_endian (objfile
->obfd
)
22482 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
22484 switch (attr
->form
)
22487 case DW_FORM_addrx
:
22488 case DW_FORM_GNU_addr_index
:
22492 *len
= cu
->header
.addr_size
;
22493 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22494 store_unsigned_integer (tem
, *len
, byte_order
, attr
->as_address ());
22498 case DW_FORM_string
:
22501 case DW_FORM_GNU_str_index
:
22502 case DW_FORM_GNU_strp_alt
:
22503 /* The string is already allocated on the objfile obstack, point
22506 const char *attr_name
= attr
->as_string ();
22507 result
= (const gdb_byte
*) attr_name
;
22508 *len
= strlen (attr_name
);
22511 case DW_FORM_block1
:
22512 case DW_FORM_block2
:
22513 case DW_FORM_block4
:
22514 case DW_FORM_block
:
22515 case DW_FORM_exprloc
:
22516 case DW_FORM_data16
:
22518 struct dwarf_block
*block
= attr
->as_block ();
22519 result
= block
->data
;
22520 *len
= block
->size
;
22524 /* The DW_AT_const_value attributes are supposed to carry the
22525 symbol's value "represented as it would be on the target
22526 architecture." By the time we get here, it's already been
22527 converted to host endianness, so we just need to sign- or
22528 zero-extend it as appropriate. */
22529 case DW_FORM_data1
:
22530 type
= die_type (die
, cu
);
22531 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
22532 if (result
== NULL
)
22533 result
= write_constant_as_bytes (obstack
, byte_order
,
22536 case DW_FORM_data2
:
22537 type
= die_type (die
, cu
);
22538 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
22539 if (result
== NULL
)
22540 result
= write_constant_as_bytes (obstack
, byte_order
,
22543 case DW_FORM_data4
:
22544 type
= die_type (die
, cu
);
22545 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
22546 if (result
== NULL
)
22547 result
= write_constant_as_bytes (obstack
, byte_order
,
22550 case DW_FORM_data8
:
22551 type
= die_type (die
, cu
);
22552 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
22553 if (result
== NULL
)
22554 result
= write_constant_as_bytes (obstack
, byte_order
,
22558 case DW_FORM_sdata
:
22559 case DW_FORM_implicit_const
:
22560 type
= die_type (die
, cu
);
22561 result
= write_constant_as_bytes (obstack
, byte_order
,
22562 type
, attr
->as_signed (), len
);
22565 case DW_FORM_udata
:
22566 type
= die_type (die
, cu
);
22567 result
= write_constant_as_bytes (obstack
, byte_order
,
22568 type
, attr
->as_unsigned (), len
);
22572 complaint (_("unsupported const value attribute form: '%s'"),
22573 dwarf_form_name (attr
->form
));
22583 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
22584 dwarf2_per_cu_data
*per_cu
,
22585 dwarf2_per_objfile
*per_objfile
,
22586 const char **var_name
)
22588 struct die_info
*die
;
22590 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22592 cu
= load_cu (per_cu
, per_objfile
, false);
22597 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22601 if (var_name
!= nullptr)
22602 *var_name
= var_decl_name (die
, cu
);
22603 return die_type (die
, cu
);
22609 dwarf2_get_die_type (cu_offset die_offset
,
22610 dwarf2_per_cu_data
*per_cu
,
22611 dwarf2_per_objfile
*per_objfile
)
22613 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
22614 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
22617 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22618 On entry *REF_CU is the CU of SRC_DIE.
22619 On exit *REF_CU is the CU of the result.
22620 Returns NULL if the referenced DIE isn't found. */
22622 static struct die_info
*
22623 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
22624 struct dwarf2_cu
**ref_cu
)
22626 struct die_info temp_die
;
22627 struct dwarf2_cu
*sig_cu
;
22628 struct die_info
*die
;
22629 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
22632 /* While it might be nice to assert sig_type->type == NULL here,
22633 we can get here for DW_AT_imported_declaration where we need
22634 the DIE not the type. */
22636 /* If necessary, add it to the queue and load its DIEs.
22638 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22639 it doesn't mean they are currently loaded. Since we require them
22640 to be loaded, we must check for ourselves. */
22641 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
22643 || per_objfile
->get_cu (sig_type
) == nullptr)
22644 read_signatured_type (sig_type
, per_objfile
);
22646 sig_cu
= per_objfile
->get_cu (sig_type
);
22647 gdb_assert (sig_cu
!= NULL
);
22648 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
22649 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
22650 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
22651 to_underlying (temp_die
.sect_off
));
22654 /* For .gdb_index version 7 keep track of included TUs.
22655 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22656 if (per_objfile
->per_bfd
->index_table
!= NULL
22657 && !per_objfile
->per_bfd
->index_table
->version_check ())
22659 (*ref_cu
)->per_cu
->imported_symtabs_push (sig_cu
->per_cu
);
22669 /* Follow signatured type referenced by ATTR in SRC_DIE.
22670 On entry *REF_CU is the CU of SRC_DIE.
22671 On exit *REF_CU is the CU of the result.
22672 The result is the DIE of the type.
22673 If the referenced type cannot be found an error is thrown. */
22675 static struct die_info
*
22676 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22677 struct dwarf2_cu
**ref_cu
)
22679 ULONGEST signature
= attr
->as_signature ();
22680 struct signatured_type
*sig_type
;
22681 struct die_info
*die
;
22683 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
22685 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
22686 /* sig_type will be NULL if the signatured type is missing from
22688 if (sig_type
== NULL
)
22690 error (_("Dwarf Error: Cannot find 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
));
22696 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
22699 dump_die_for_error (src_die
);
22700 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22701 " from DIE at %s [in module %s]"),
22702 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22703 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22709 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22710 reading in and processing the type unit if necessary. */
22712 static struct type
*
22713 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
22714 struct dwarf2_cu
*cu
)
22716 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22717 struct signatured_type
*sig_type
;
22718 struct dwarf2_cu
*type_cu
;
22719 struct die_info
*type_die
;
22722 sig_type
= lookup_signatured_type (cu
, signature
);
22723 /* sig_type will be NULL if the signatured type is missing from
22725 if (sig_type
== NULL
)
22727 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22728 " from DIE at %s [in module %s]"),
22729 hex_string (signature
), sect_offset_str (die
->sect_off
),
22730 objfile_name (per_objfile
->objfile
));
22731 return build_error_marker_type (cu
, die
);
22734 /* If we already know the type we're done. */
22735 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
22736 if (type
!= nullptr)
22740 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
22741 if (type_die
!= NULL
)
22743 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22744 is created. This is important, for example, because for c++ classes
22745 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22746 type
= read_type_die (type_die
, type_cu
);
22749 complaint (_("Dwarf Error: Cannot build signatured type %s"
22750 " referenced from DIE at %s [in module %s]"),
22751 hex_string (signature
), sect_offset_str (die
->sect_off
),
22752 objfile_name (per_objfile
->objfile
));
22753 type
= build_error_marker_type (cu
, die
);
22758 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22759 " from DIE at %s [in module %s]"),
22760 hex_string (signature
), sect_offset_str (die
->sect_off
),
22761 objfile_name (per_objfile
->objfile
));
22762 type
= build_error_marker_type (cu
, die
);
22765 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
22770 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22771 reading in and processing the type unit if necessary. */
22773 static struct type
*
22774 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
22775 struct dwarf2_cu
*cu
) /* ARI: editCase function */
22777 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22778 if (attr
->form_is_ref ())
22780 struct dwarf2_cu
*type_cu
= cu
;
22781 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
22783 return read_type_die (type_die
, type_cu
);
22785 else if (attr
->form
== DW_FORM_ref_sig8
)
22787 return get_signatured_type (die
, attr
->as_signature (), cu
);
22791 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22793 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22794 " at %s [in module %s]"),
22795 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
22796 objfile_name (per_objfile
->objfile
));
22797 return build_error_marker_type (cu
, die
);
22801 /* Load the DIEs associated with type unit PER_CU into memory. */
22804 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
22805 dwarf2_per_objfile
*per_objfile
)
22807 struct signatured_type
*sig_type
;
22809 /* We have the per_cu, but we need the signatured_type.
22810 Fortunately this is an easy translation. */
22811 gdb_assert (per_cu
->is_debug_types
);
22812 sig_type
= (struct signatured_type
*) per_cu
;
22814 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
22816 read_signatured_type (sig_type
, per_objfile
);
22818 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
22821 /* Read in a signatured type and build its CU and DIEs.
22822 If the type is a stub for the real type in a DWO file,
22823 read in the real type from the DWO file as well. */
22826 read_signatured_type (signatured_type
*sig_type
,
22827 dwarf2_per_objfile
*per_objfile
)
22829 gdb_assert (sig_type
->is_debug_types
);
22830 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
22832 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
22834 if (!reader
.dummy_p
)
22836 struct dwarf2_cu
*cu
= reader
.cu
;
22837 const gdb_byte
*info_ptr
= reader
.info_ptr
;
22839 gdb_assert (cu
->die_hash
== NULL
);
22841 htab_create_alloc_ex (cu
->header
.length
/ 12,
22845 &cu
->comp_unit_obstack
,
22846 hashtab_obstack_allocate
,
22847 dummy_obstack_deallocate
);
22849 if (reader
.comp_unit_die
->has_children
)
22850 reader
.comp_unit_die
->child
22851 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
22852 reader
.comp_unit_die
);
22853 cu
->dies
= reader
.comp_unit_die
;
22854 /* comp_unit_die is not stored in die_hash, no need. */
22856 /* We try not to read any attributes in this function, because
22857 not all CUs needed for references have been loaded yet, and
22858 symbol table processing isn't initialized. But we have to
22859 set the CU language, or we won't be able to build types
22860 correctly. Similarly, if we do not read the producer, we can
22861 not apply producer-specific interpretation. */
22862 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
22867 sig_type
->tu_read
= 1;
22870 /* Decode simple location descriptions.
22871 Given a pointer to a dwarf block that defines a location, compute
22872 the location and return the value. If COMPUTED is non-null, it is
22873 set to true to indicate that decoding was successful, and false
22874 otherwise. If COMPUTED is null, then this function may emit a
22878 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
, bool *computed
)
22880 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
22882 size_t size
= blk
->size
;
22883 const gdb_byte
*data
= blk
->data
;
22884 CORE_ADDR stack
[64];
22886 unsigned int bytes_read
, unsnd
;
22889 if (computed
!= nullptr)
22895 stack
[++stacki
] = 0;
22934 stack
[++stacki
] = op
- DW_OP_lit0
;
22969 stack
[++stacki
] = op
- DW_OP_reg0
;
22972 if (computed
== nullptr)
22973 dwarf2_complex_location_expr_complaint ();
22980 unsnd
= read_unsigned_leb128 (NULL
, (data
+ i
), &bytes_read
);
22982 stack
[++stacki
] = unsnd
;
22985 if (computed
== nullptr)
22986 dwarf2_complex_location_expr_complaint ();
22993 stack
[++stacki
] = cu
->header
.read_address (objfile
->obfd
, &data
[i
],
22998 case DW_OP_const1u
:
22999 stack
[++stacki
] = read_1_byte (objfile
->obfd
, &data
[i
]);
23003 case DW_OP_const1s
:
23004 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
, &data
[i
]);
23008 case DW_OP_const2u
:
23009 stack
[++stacki
] = read_2_bytes (objfile
->obfd
, &data
[i
]);
23013 case DW_OP_const2s
:
23014 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
, &data
[i
]);
23018 case DW_OP_const4u
:
23019 stack
[++stacki
] = read_4_bytes (objfile
->obfd
, &data
[i
]);
23023 case DW_OP_const4s
:
23024 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
, &data
[i
]);
23028 case DW_OP_const8u
:
23029 stack
[++stacki
] = read_8_bytes (objfile
->obfd
, &data
[i
]);
23034 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
23040 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
23045 stack
[stacki
+ 1] = stack
[stacki
];
23050 stack
[stacki
- 1] += stack
[stacki
];
23054 case DW_OP_plus_uconst
:
23055 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
23061 stack
[stacki
- 1] -= stack
[stacki
];
23066 /* If we're not the last op, then we definitely can't encode
23067 this using GDB's address_class enum. This is valid for partial
23068 global symbols, although the variable's address will be bogus
23072 if (computed
== nullptr)
23073 dwarf2_complex_location_expr_complaint ();
23079 case DW_OP_GNU_push_tls_address
:
23080 case DW_OP_form_tls_address
:
23081 /* The top of the stack has the offset from the beginning
23082 of the thread control block at which the variable is located. */
23083 /* Nothing should follow this operator, so the top of stack would
23085 /* This is valid for partial global symbols, but the variable's
23086 address will be bogus in the psymtab. Make it always at least
23087 non-zero to not look as a variable garbage collected by linker
23088 which have DW_OP_addr 0. */
23091 if (computed
== nullptr)
23092 dwarf2_complex_location_expr_complaint ();
23099 case DW_OP_GNU_uninit
:
23100 if (computed
!= nullptr)
23105 case DW_OP_GNU_addr_index
:
23106 case DW_OP_GNU_const_index
:
23107 stack
[++stacki
] = read_addr_index_from_leb128 (cu
, &data
[i
],
23113 if (computed
== nullptr)
23115 const char *name
= get_DW_OP_name (op
);
23118 complaint (_("unsupported stack op: '%s'"),
23121 complaint (_("unsupported stack op: '%02x'"),
23125 return (stack
[stacki
]);
23128 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23129 outside of the allocated space. Also enforce minimum>0. */
23130 if (stacki
>= ARRAY_SIZE (stack
) - 1)
23132 if (computed
== nullptr)
23133 complaint (_("location description stack overflow"));
23139 if (computed
== nullptr)
23140 complaint (_("location description stack underflow"));
23145 if (computed
!= nullptr)
23147 return (stack
[stacki
]);
23150 /* memory allocation interface */
23152 static struct dwarf_block
*
23153 dwarf_alloc_block (struct dwarf2_cu
*cu
)
23155 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
23158 static struct die_info
*
23159 dwarf_alloc_die (struct dwarf2_cu
*cu
, int num_attrs
)
23161 struct die_info
*die
;
23162 size_t size
= sizeof (struct die_info
);
23165 size
+= (num_attrs
- 1) * sizeof (struct attribute
);
23167 die
= (struct die_info
*) obstack_alloc (&cu
->comp_unit_obstack
, size
);
23168 memset (die
, 0, sizeof (struct die_info
));
23174 /* Macro support. */
23176 /* An overload of dwarf_decode_macros that finds the correct section
23177 and ensures it is read in before calling the other overload. */
23180 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
23181 int section_is_gnu
)
23183 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23184 struct objfile
*objfile
= per_objfile
->objfile
;
23185 const struct line_header
*lh
= cu
->line_header
;
23186 unsigned int offset_size
= cu
->header
.offset_size
;
23187 struct dwarf2_section_info
*section
;
23188 const char *section_name
;
23190 if (cu
->dwo_unit
!= nullptr)
23192 if (section_is_gnu
)
23194 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
23195 section_name
= ".debug_macro.dwo";
23199 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
23200 section_name
= ".debug_macinfo.dwo";
23205 if (section_is_gnu
)
23207 section
= &per_objfile
->per_bfd
->macro
;
23208 section_name
= ".debug_macro";
23212 section
= &per_objfile
->per_bfd
->macinfo
;
23213 section_name
= ".debug_macinfo";
23217 section
->read (objfile
);
23218 if (section
->buffer
== nullptr)
23220 complaint (_("missing %s section"), section_name
);
23224 buildsym_compunit
*builder
= cu
->get_builder ();
23226 struct dwarf2_section_info
*str_offsets_section
;
23227 struct dwarf2_section_info
*str_section
;
23228 gdb::optional
<ULONGEST
> str_offsets_base
;
23230 if (cu
->dwo_unit
!= nullptr)
23232 str_offsets_section
= &cu
->dwo_unit
->dwo_file
23233 ->sections
.str_offsets
;
23234 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
23235 str_offsets_base
= cu
->header
.addr_size
;
23239 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
23240 str_section
= &per_objfile
->per_bfd
->str
;
23241 str_offsets_base
= cu
->str_offsets_base
;
23244 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
23245 offset_size
, offset
, str_section
, str_offsets_section
,
23246 str_offsets_base
, section_is_gnu
);
23249 /* Return the .debug_loc section to use for CU.
23250 For DWO files use .debug_loc.dwo. */
23252 static struct dwarf2_section_info
*
23253 cu_debug_loc_section (struct dwarf2_cu
*cu
)
23255 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23259 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23261 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
23263 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
23264 : &per_objfile
->per_bfd
->loc
);
23267 /* Return the .debug_rnglists section to use for CU. */
23268 static struct dwarf2_section_info
*
23269 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
23271 if (cu
->header
.version
< 5)
23272 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23273 cu
->header
.version
);
23274 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
23276 /* Make sure we read the .debug_rnglists section from the file that
23277 contains the DW_AT_ranges attribute we are reading. Normally that
23278 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23279 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23281 if (cu
->dwo_unit
!= nullptr
23282 && tag
!= DW_TAG_compile_unit
23283 && tag
!= DW_TAG_skeleton_unit
)
23285 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23287 if (sections
->rnglists
.size
> 0)
23288 return §ions
->rnglists
;
23290 error (_(".debug_rnglists section is missing from .dwo file."));
23292 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
23295 /* A helper function that fills in a dwarf2_loclist_baton. */
23298 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
23299 struct dwarf2_loclist_baton
*baton
,
23300 const struct attribute
*attr
)
23302 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23303 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23305 section
->read (per_objfile
->objfile
);
23307 baton
->per_objfile
= per_objfile
;
23308 baton
->per_cu
= cu
->per_cu
;
23309 gdb_assert (baton
->per_cu
);
23310 /* We don't know how long the location list is, but make sure we
23311 don't run off the edge of the section. */
23312 baton
->size
= section
->size
- attr
->as_unsigned ();
23313 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
23314 if (cu
->base_address
.has_value ())
23315 baton
->base_address
= *cu
->base_address
;
23317 baton
->base_address
= 0;
23318 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
23322 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
23323 struct dwarf2_cu
*cu
, int is_block
)
23325 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23326 struct objfile
*objfile
= per_objfile
->objfile
;
23327 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23329 if (attr
->form_is_section_offset ()
23330 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23331 the section. If so, fall through to the complaint in the
23333 && attr
->as_unsigned () < section
->get_size (objfile
))
23335 struct dwarf2_loclist_baton
*baton
;
23337 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
23339 fill_in_loclist_baton (cu
, baton
, attr
);
23341 if (!cu
->base_address
.has_value ())
23342 complaint (_("Location list used without "
23343 "specifying the CU base address."));
23345 sym
->set_aclass_index ((is_block
23346 ? dwarf2_loclist_block_index
23347 : dwarf2_loclist_index
));
23348 SYMBOL_LOCATION_BATON (sym
) = baton
;
23352 struct dwarf2_locexpr_baton
*baton
;
23354 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
23355 baton
->per_objfile
= per_objfile
;
23356 baton
->per_cu
= cu
->per_cu
;
23357 gdb_assert (baton
->per_cu
);
23359 if (attr
->form_is_block ())
23361 /* Note that we're just copying the block's data pointer
23362 here, not the actual data. We're still pointing into the
23363 info_buffer for SYM's objfile; right now we never release
23364 that buffer, but when we do clean up properly this may
23366 struct dwarf_block
*block
= attr
->as_block ();
23367 baton
->size
= block
->size
;
23368 baton
->data
= block
->data
;
23372 dwarf2_invalid_attrib_class_complaint ("location description",
23373 sym
->natural_name ());
23377 sym
->set_aclass_index ((is_block
23378 ? dwarf2_locexpr_block_index
23379 : dwarf2_locexpr_index
));
23380 SYMBOL_LOCATION_BATON (sym
) = baton
;
23386 const comp_unit_head
*
23387 dwarf2_per_cu_data::get_header () const
23389 if (!m_header_read_in
)
23391 const gdb_byte
*info_ptr
23392 = this->section
->buffer
+ to_underlying (this->sect_off
);
23394 memset (&m_header
, 0, sizeof (m_header
));
23396 read_comp_unit_head (&m_header
, info_ptr
, this->section
,
23397 rcuh_kind::COMPILE
);
23399 m_header_read_in
= true;
23408 dwarf2_per_cu_data::addr_size () const
23410 return this->get_header ()->addr_size
;
23416 dwarf2_per_cu_data::offset_size () const
23418 return this->get_header ()->offset_size
;
23424 dwarf2_per_cu_data::ref_addr_size () const
23426 const comp_unit_head
*header
= this->get_header ();
23428 if (header
->version
== 2)
23429 return header
->addr_size
;
23431 return header
->offset_size
;
23434 /* A helper function for dwarf2_find_containing_comp_unit that returns
23435 the index of the result, and that searches a vector. It will
23436 return a result even if the offset in question does not actually
23437 occur in any CU. This is separate so that it can be unit
23441 dwarf2_find_containing_comp_unit
23442 (sect_offset sect_off
,
23443 unsigned int offset_in_dwz
,
23444 const std::vector
<dwarf2_per_cu_data_up
> &all_comp_units
)
23449 high
= all_comp_units
.size () - 1;
23452 struct dwarf2_per_cu_data
*mid_cu
;
23453 int mid
= low
+ (high
- low
) / 2;
23455 mid_cu
= all_comp_units
[mid
].get ();
23456 if (mid_cu
->is_dwz
> offset_in_dwz
23457 || (mid_cu
->is_dwz
== offset_in_dwz
23458 && mid_cu
->sect_off
+ mid_cu
->length () > sect_off
))
23463 gdb_assert (low
== high
);
23467 /* Locate the .debug_info compilation unit from CU's objfile which contains
23468 the DIE at OFFSET. Raises an error on failure. */
23470 static struct dwarf2_per_cu_data
*
23471 dwarf2_find_containing_comp_unit (sect_offset sect_off
,
23472 unsigned int offset_in_dwz
,
23473 dwarf2_per_bfd
*per_bfd
)
23475 int low
= dwarf2_find_containing_comp_unit
23476 (sect_off
, offset_in_dwz
, per_bfd
->all_comp_units
);
23477 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_comp_units
[low
].get ();
23479 if (this_cu
->is_dwz
!= offset_in_dwz
|| this_cu
->sect_off
> sect_off
)
23481 if (low
== 0 || this_cu
->is_dwz
!= offset_in_dwz
)
23482 error (_("Dwarf Error: could not find partial DIE containing "
23483 "offset %s [in module %s]"),
23484 sect_offset_str (sect_off
),
23485 bfd_get_filename (per_bfd
->obfd
));
23487 gdb_assert (per_bfd
->all_comp_units
[low
-1]->sect_off
23489 return per_bfd
->all_comp_units
[low
- 1].get ();
23493 if (low
== per_bfd
->all_comp_units
.size () - 1
23494 && sect_off
>= this_cu
->sect_off
+ this_cu
->length ())
23495 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off
));
23496 gdb_assert (sect_off
< this_cu
->sect_off
+ this_cu
->length ());
23503 namespace selftests
{
23504 namespace find_containing_comp_unit
{
23509 dwarf2_per_cu_data_up
one (new dwarf2_per_cu_data
);
23510 dwarf2_per_cu_data
*one_ptr
= one
.get ();
23511 dwarf2_per_cu_data_up
two (new dwarf2_per_cu_data
);
23512 dwarf2_per_cu_data
*two_ptr
= two
.get ();
23513 dwarf2_per_cu_data_up
three (new dwarf2_per_cu_data
);
23514 dwarf2_per_cu_data
*three_ptr
= three
.get ();
23515 dwarf2_per_cu_data_up
four (new dwarf2_per_cu_data
);
23516 dwarf2_per_cu_data
*four_ptr
= four
.get ();
23518 one
->set_length (5);
23519 two
->sect_off
= sect_offset (one
->length ());
23520 two
->set_length (7);
23522 three
->set_length (5);
23524 four
->sect_off
= sect_offset (three
->length ());
23525 four
->set_length (7);
23528 std::vector
<dwarf2_per_cu_data_up
> units
;
23529 units
.push_back (std::move (one
));
23530 units
.push_back (std::move (two
));
23531 units
.push_back (std::move (three
));
23532 units
.push_back (std::move (four
));
23536 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 0, units
);
23537 SELF_CHECK (units
[result
].get () == one_ptr
);
23538 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 0, units
);
23539 SELF_CHECK (units
[result
].get () == one_ptr
);
23540 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 0, units
);
23541 SELF_CHECK (units
[result
].get () == two_ptr
);
23543 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 1, units
);
23544 SELF_CHECK (units
[result
].get () == three_ptr
);
23545 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 1, units
);
23546 SELF_CHECK (units
[result
].get () == three_ptr
);
23547 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 1, units
);
23548 SELF_CHECK (units
[result
].get () == four_ptr
);
23554 #endif /* GDB_SELF_TEST */
23556 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23559 prepare_one_comp_unit (struct dwarf2_cu
*cu
, struct die_info
*comp_unit_die
,
23560 enum language pretend_language
)
23562 struct attribute
*attr
;
23564 cu
->producer
= dwarf2_string_attr (comp_unit_die
, DW_AT_producer
, cu
);
23566 /* Set the language we're debugging. */
23567 attr
= dwarf2_attr (comp_unit_die
, DW_AT_language
, cu
);
23568 enum language lang
;
23569 if (cu
->producer
!= nullptr
23570 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
23572 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23573 attribute is not standardised yet. As a workaround for the
23574 language detection we fall back to the DW_AT_producer
23576 lang
= language_opencl
;
23578 else if (cu
->producer
!= nullptr
23579 && strstr (cu
->producer
, "GNU Go ") != NULL
)
23581 /* Similar hack for Go. */
23582 lang
= language_go
;
23584 else if (attr
!= nullptr)
23585 lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
23587 lang
= pretend_language
;
23589 cu
->language_defn
= language_def (lang
);
23591 switch (comp_unit_die
->tag
)
23593 case DW_TAG_compile_unit
:
23594 cu
->per_cu
->set_unit_type (DW_UT_compile
);
23596 case DW_TAG_partial_unit
:
23597 cu
->per_cu
->set_unit_type (DW_UT_partial
);
23599 case DW_TAG_type_unit
:
23600 cu
->per_cu
->set_unit_type (DW_UT_type
);
23603 error (_("Dwarf Error: unexpected tag '%s' at offset %s"),
23604 dwarf_tag_name (comp_unit_die
->tag
),
23605 sect_offset_str (cu
->per_cu
->sect_off
));
23608 cu
->per_cu
->set_lang (lang
);
23614 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
23616 auto it
= m_dwarf2_cus
.find (per_cu
);
23617 if (it
== m_dwarf2_cus
.end ())
23620 return it
->second
.get ();
23626 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
,
23627 std::unique_ptr
<dwarf2_cu
> cu
)
23629 gdb_assert (this->get_cu (per_cu
) == nullptr);
23631 m_dwarf2_cus
[per_cu
] = std::move (cu
);
23637 dwarf2_per_objfile::age_comp_units ()
23639 dwarf_read_debug_printf_v ("running");
23641 /* This is not expected to be called in the middle of CU expansion. There is
23642 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23643 loaded in memory. Calling age_comp_units while the queue is in use could
23644 make us free the DIEs for a CU that is in the queue and therefore break
23646 gdb_assert (!queue
.has_value ());
23648 /* Start by clearing all marks. */
23649 for (const auto &pair
: m_dwarf2_cus
)
23650 pair
.second
->clear_mark ();
23652 /* Traverse all CUs, mark them and their dependencies if used recently
23654 for (const auto &pair
: m_dwarf2_cus
)
23656 dwarf2_cu
*cu
= pair
.second
.get ();
23659 if (cu
->last_used
<= dwarf_max_cache_age
)
23663 /* Delete all CUs still not marked. */
23664 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
23666 dwarf2_cu
*cu
= it
->second
.get ();
23668 if (!cu
->is_marked ())
23670 dwarf_read_debug_printf_v ("deleting old CU %s",
23671 sect_offset_str (cu
->per_cu
->sect_off
));
23672 it
= m_dwarf2_cus
.erase (it
);
23682 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
23684 auto it
= m_dwarf2_cus
.find (per_cu
);
23685 if (it
== m_dwarf2_cus
.end ())
23688 m_dwarf2_cus
.erase (it
);
23691 dwarf2_per_objfile::~dwarf2_per_objfile ()
23696 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23697 We store these in a hash table separate from the DIEs, and preserve them
23698 when the DIEs are flushed out of cache.
23700 The CU "per_cu" pointer is needed because offset alone is not enough to
23701 uniquely identify the type. A file may have multiple .debug_types sections,
23702 or the type may come from a DWO file. Furthermore, while it's more logical
23703 to use per_cu->section+offset, with Fission the section with the data is in
23704 the DWO file but we don't know that section at the point we need it.
23705 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23706 because we can enter the lookup routine, get_die_type_at_offset, from
23707 outside this file, and thus won't necessarily have PER_CU->cu.
23708 Fortunately, PER_CU is stable for the life of the objfile. */
23710 struct dwarf2_per_cu_offset_and_type
23712 const struct dwarf2_per_cu_data
*per_cu
;
23713 sect_offset sect_off
;
23717 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23720 per_cu_offset_and_type_hash (const void *item
)
23722 const struct dwarf2_per_cu_offset_and_type
*ofs
23723 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
23725 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
23728 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23731 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
23733 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
23734 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
23735 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
23736 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
23738 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
23739 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
23742 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23743 table if necessary. For convenience, return TYPE.
23745 The DIEs reading must have careful ordering to:
23746 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23747 reading current DIE.
23748 * Not trying to dereference contents of still incompletely read in types
23749 while reading in other DIEs.
23750 * Enable referencing still incompletely read in types just by a pointer to
23751 the type without accessing its fields.
23753 Therefore caller should follow these rules:
23754 * Try to fetch any prerequisite types we may need to build this DIE type
23755 before building the type and calling set_die_type.
23756 * After building type call set_die_type for current DIE as soon as
23757 possible before fetching more types to complete the current type.
23758 * Make the type as complete as possible before fetching more types. */
23760 static struct type
*
23761 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
23762 bool skip_data_location
)
23764 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23765 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
23766 struct objfile
*objfile
= per_objfile
->objfile
;
23767 struct attribute
*attr
;
23768 struct dynamic_prop prop
;
23770 /* For Ada types, make sure that the gnat-specific data is always
23771 initialized (if not already set). There are a few types where
23772 we should not be doing so, because the type-specific area is
23773 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23774 where the type-specific area is used to store the floatformat).
23775 But this is not a problem, because the gnat-specific information
23776 is actually not needed for these types. */
23777 if (need_gnat_info (cu
)
23778 && type
->code () != TYPE_CODE_FUNC
23779 && type
->code () != TYPE_CODE_FLT
23780 && type
->code () != TYPE_CODE_METHODPTR
23781 && type
->code () != TYPE_CODE_MEMBERPTR
23782 && type
->code () != TYPE_CODE_METHOD
23783 && type
->code () != TYPE_CODE_FIXED_POINT
23784 && !HAVE_GNAT_AUX_INFO (type
))
23785 INIT_GNAT_SPECIFIC (type
);
23787 /* Read DW_AT_allocated and set in type. */
23788 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
23791 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23792 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23793 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
23796 /* Read DW_AT_associated and set in type. */
23797 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
23800 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23801 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23802 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
23805 /* Read DW_AT_rank and set in type. */
23806 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
23809 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23810 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23811 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
23814 /* Read DW_AT_data_location and set in type. */
23815 if (!skip_data_location
)
23817 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
23818 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
23819 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
23822 if (per_objfile
->die_type_hash
== NULL
)
23823 per_objfile
->die_type_hash
23824 = htab_up (htab_create_alloc (127,
23825 per_cu_offset_and_type_hash
,
23826 per_cu_offset_and_type_eq
,
23827 NULL
, xcalloc
, xfree
));
23829 ofs
.per_cu
= cu
->per_cu
;
23830 ofs
.sect_off
= die
->sect_off
;
23832 slot
= (struct dwarf2_per_cu_offset_and_type
**)
23833 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
23835 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23836 sect_offset_str (die
->sect_off
));
23837 *slot
= XOBNEW (&objfile
->objfile_obstack
,
23838 struct dwarf2_per_cu_offset_and_type
);
23843 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23844 or return NULL if the die does not have a saved type. */
23846 static struct type
*
23847 get_die_type_at_offset (sect_offset sect_off
,
23848 dwarf2_per_cu_data
*per_cu
,
23849 dwarf2_per_objfile
*per_objfile
)
23851 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
23853 if (per_objfile
->die_type_hash
== NULL
)
23856 ofs
.per_cu
= per_cu
;
23857 ofs
.sect_off
= sect_off
;
23858 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
23859 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
23866 /* Look up the type for DIE in CU in die_type_hash,
23867 or return NULL if DIE does not have a saved type. */
23869 static struct type
*
23870 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
23872 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
23875 struct cmd_list_element
*set_dwarf_cmdlist
;
23876 struct cmd_list_element
*show_dwarf_cmdlist
;
23879 show_check_physname (struct ui_file
*file
, int from_tty
,
23880 struct cmd_list_element
*c
, const char *value
)
23883 _("Whether to check \"physname\" is %s.\n"),
23887 void _initialize_dwarf2_read ();
23889 _initialize_dwarf2_read ()
23891 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
23893 Set DWARF specific variables.\n\
23894 Configure DWARF variables such as the cache size."),
23896 Show DWARF specific variables.\n\
23897 Show DWARF variables such as the cache size."),
23898 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
23899 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
23901 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
23902 &dwarf_max_cache_age
, _("\
23903 Set the upper bound on the age of cached DWARF compilation units."), _("\
23904 Show the upper bound on the age of cached DWARF compilation units."), _("\
23905 A higher limit means that cached compilation units will be stored\n\
23906 in memory longer, and more total memory will be used. Zero disables\n\
23907 caching, which can slow down startup."),
23909 show_dwarf_max_cache_age
,
23910 &set_dwarf_cmdlist
,
23911 &show_dwarf_cmdlist
);
23913 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
23914 Set debugging of the DWARF reader."), _("\
23915 Show debugging of the DWARF reader."), _("\
23916 When enabled (non-zero), debugging messages are printed during DWARF\n\
23917 reading and symtab expansion. A value of 1 (one) provides basic\n\
23918 information. A value greater than 1 provides more verbose information."),
23921 &setdebuglist
, &showdebuglist
);
23923 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
23924 Set debugging of the DWARF DIE reader."), _("\
23925 Show debugging of the DWARF DIE reader."), _("\
23926 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23927 The value is the maximum depth to print."),
23930 &setdebuglist
, &showdebuglist
);
23932 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
23933 Set debugging of the dwarf line reader."), _("\
23934 Show debugging of the dwarf line reader."), _("\
23935 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23936 A value of 1 (one) provides basic information.\n\
23937 A value greater than 1 provides more verbose information."),
23940 &setdebuglist
, &showdebuglist
);
23942 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
23943 Set cross-checking of \"physname\" code against demangler."), _("\
23944 Show cross-checking of \"physname\" code against demangler."), _("\
23945 When enabled, GDB's internal \"physname\" code is checked against\n\
23947 NULL
, show_check_physname
,
23948 &setdebuglist
, &showdebuglist
);
23950 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23951 no_class
, &use_deprecated_index_sections
, _("\
23952 Set whether to use deprecated gdb_index sections."), _("\
23953 Show whether to use deprecated gdb_index sections."), _("\
23954 When enabled, deprecated .gdb_index sections are used anyway.\n\
23955 Normally they are ignored either because of a missing feature or\n\
23956 performance issue.\n\
23957 Warning: This option must be enabled before gdb reads the file."),
23960 &setlist
, &showlist
);
23962 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23963 &dwarf2_locexpr_funcs
);
23964 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23965 &dwarf2_loclist_funcs
);
23967 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23968 &dwarf2_block_frame_base_locexpr_funcs
);
23969 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23970 &dwarf2_block_frame_base_loclist_funcs
);
23973 selftests::register_test ("dw2_expand_symtabs_matching",
23974 selftests::dw2_expand_symtabs_matching::run_test
);
23975 selftests::register_test ("dwarf2_find_containing_comp_unit",
23976 selftests::find_containing_comp_unit::run_test
);