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 unsigned int peek_abbrev_code (bfd
*, const gdb_byte
*);
899 static const gdb_byte
*read_attribute (const struct die_reader_specs
*,
901 const struct attr_abbrev
*,
904 static void read_attribute_reprocess (const struct die_reader_specs
*reader
,
905 struct attribute
*attr
, dwarf_tag tag
);
907 static CORE_ADDR
read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
);
909 static sect_offset
read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
910 dwarf2_section_info
*, sect_offset
);
912 static const char *read_indirect_string
913 (dwarf2_per_objfile
*per_objfile
, bfd
*, const gdb_byte
*,
914 const struct comp_unit_head
*, unsigned int *);
916 static const char *read_indirect_string_at_offset
917 (dwarf2_per_objfile
*per_objfile
, LONGEST str_offset
);
919 static CORE_ADDR
read_addr_index_from_leb128 (struct dwarf2_cu
*,
923 static const char *read_dwo_str_index (const struct die_reader_specs
*reader
,
926 static const char *read_stub_str_index (struct dwarf2_cu
*cu
,
929 static struct attribute
*dwarf2_attr (struct die_info
*, unsigned int,
932 static const char *dwarf2_string_attr (struct die_info
*die
, unsigned int name
,
933 struct dwarf2_cu
*cu
);
935 static const char *dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
);
937 static int dwarf2_flag_true_p (struct die_info
*die
, unsigned name
,
938 struct dwarf2_cu
*cu
);
940 static int die_is_declaration (struct die_info
*, struct dwarf2_cu
*cu
);
942 static struct die_info
*die_specification (struct die_info
*die
,
943 struct dwarf2_cu
**);
945 static line_header_up
dwarf_decode_line_header (sect_offset sect_off
,
946 struct dwarf2_cu
*cu
);
948 static void dwarf_decode_lines (struct line_header
*,
950 CORE_ADDR
, int decode_mapping
);
952 static void dwarf2_start_subfile (struct dwarf2_cu
*, const char *,
955 static struct symbol
*new_symbol (struct die_info
*, struct type
*,
956 struct dwarf2_cu
*, struct symbol
* = NULL
);
958 static void dwarf2_const_value (const struct attribute
*, struct symbol
*,
961 static void dwarf2_const_value_attr (const struct attribute
*attr
,
964 struct obstack
*obstack
,
965 struct dwarf2_cu
*cu
, LONGEST
*value
,
966 const gdb_byte
**bytes
,
967 struct dwarf2_locexpr_baton
**baton
);
969 static struct type
*read_subrange_index_type (struct die_info
*die
,
970 struct dwarf2_cu
*cu
);
972 static struct type
*die_type (struct die_info
*, struct dwarf2_cu
*);
974 static int need_gnat_info (struct dwarf2_cu
*);
976 static struct type
*die_descriptive_type (struct die_info
*,
979 static void set_descriptive_type (struct type
*, struct die_info
*,
982 static struct type
*die_containing_type (struct die_info
*,
985 static struct type
*lookup_die_type (struct die_info
*, const struct attribute
*,
988 static struct type
*read_type_die (struct die_info
*, struct dwarf2_cu
*);
990 static struct type
*read_type_die_1 (struct die_info
*, struct dwarf2_cu
*);
992 static const char *determine_prefix (struct die_info
*die
, struct dwarf2_cu
*);
994 static char *typename_concat (struct obstack
*obs
, const char *prefix
,
995 const char *suffix
, int physname
,
996 struct dwarf2_cu
*cu
);
998 static void read_file_scope (struct die_info
*, struct dwarf2_cu
*);
1000 static void read_type_unit_scope (struct die_info
*, struct dwarf2_cu
*);
1002 static void read_func_scope (struct die_info
*, struct dwarf2_cu
*);
1004 static void read_lexical_block_scope (struct die_info
*, struct dwarf2_cu
*);
1006 static void read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
);
1008 static void read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
);
1010 /* Return the .debug_loclists section to use for cu. */
1011 static struct dwarf2_section_info
*cu_debug_loc_section (struct dwarf2_cu
*cu
);
1013 /* Return the .debug_rnglists section to use for cu. */
1014 static struct dwarf2_section_info
*cu_debug_rnglists_section
1015 (struct dwarf2_cu
*cu
, dwarf_tag tag
);
1017 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1018 values. Keep the items ordered with increasing constraints compliance. */
1021 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1022 PC_BOUNDS_NOT_PRESENT
,
1024 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1025 were present but they do not form a valid range of PC addresses. */
1028 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1031 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1035 static enum pc_bounds_kind
dwarf2_get_pc_bounds (struct die_info
*,
1036 CORE_ADDR
*, CORE_ADDR
*,
1041 static void get_scope_pc_bounds (struct die_info
*,
1042 CORE_ADDR
*, CORE_ADDR
*,
1043 struct dwarf2_cu
*);
1045 static void dwarf2_record_block_ranges (struct die_info
*, struct block
*,
1046 CORE_ADDR
, struct dwarf2_cu
*);
1048 static void dwarf2_add_field (struct field_info
*, struct die_info
*,
1049 struct dwarf2_cu
*);
1051 static void dwarf2_attach_fields_to_type (struct field_info
*,
1052 struct type
*, struct dwarf2_cu
*);
1054 static void dwarf2_add_member_fn (struct field_info
*,
1055 struct die_info
*, struct type
*,
1056 struct dwarf2_cu
*);
1058 static void dwarf2_attach_fn_fields_to_type (struct field_info
*,
1060 struct dwarf2_cu
*);
1062 static void process_structure_scope (struct die_info
*, struct dwarf2_cu
*);
1064 static void read_common_block (struct die_info
*, struct dwarf2_cu
*);
1066 static void read_namespace (struct die_info
*die
, struct dwarf2_cu
*);
1068 static void read_module (struct die_info
*die
, struct dwarf2_cu
*cu
);
1070 static struct using_direct
**using_directives (struct dwarf2_cu
*cu
);
1072 static void read_import_statement (struct die_info
*die
, struct dwarf2_cu
*);
1074 static int read_namespace_alias (struct die_info
*die
, struct dwarf2_cu
*cu
);
1076 static struct type
*read_module_type (struct die_info
*die
,
1077 struct dwarf2_cu
*cu
);
1079 static const char *namespace_name (struct die_info
*die
,
1080 int *is_anonymous
, struct dwarf2_cu
*);
1082 static void process_enumeration_scope (struct die_info
*, struct dwarf2_cu
*);
1084 static CORE_ADDR
decode_locdesc (struct dwarf_block
*, struct dwarf2_cu
*,
1087 static enum dwarf_array_dim_ordering
read_array_order (struct die_info
*,
1088 struct dwarf2_cu
*);
1090 static struct die_info
*read_die_and_siblings_1
1091 (const struct die_reader_specs
*, const gdb_byte
*, const gdb_byte
**,
1094 static struct die_info
*read_die_and_siblings (const struct die_reader_specs
*,
1095 const gdb_byte
*info_ptr
,
1096 const gdb_byte
**new_info_ptr
,
1097 struct die_info
*parent
);
1099 static const gdb_byte
*read_full_die_1 (const struct die_reader_specs
*,
1100 struct die_info
**, const gdb_byte
*,
1103 static const gdb_byte
*read_full_die (const struct die_reader_specs
*,
1104 struct die_info
**, const gdb_byte
*);
1106 static void process_die (struct die_info
*, struct dwarf2_cu
*);
1108 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu
*,
1111 static const char *dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*);
1113 static const char *dwarf2_full_name (const char *name
,
1114 struct die_info
*die
,
1115 struct dwarf2_cu
*cu
);
1117 static const char *dwarf2_physname (const char *name
, struct die_info
*die
,
1118 struct dwarf2_cu
*cu
);
1120 static struct die_info
*dwarf2_extension (struct die_info
*die
,
1121 struct dwarf2_cu
**);
1123 static void dump_die_shallow (struct ui_file
*, int indent
, struct die_info
*);
1125 static void dump_die_for_error (struct die_info
*);
1127 static void dump_die_1 (struct ui_file
*, int level
, int max_level
,
1130 /*static*/ void dump_die (struct die_info
*, int max_level
);
1132 static void store_in_ref_table (struct die_info
*,
1133 struct dwarf2_cu
*);
1135 static struct die_info
*follow_die_ref_or_sig (struct die_info
*,
1136 const struct attribute
*,
1137 struct dwarf2_cu
**);
1139 static struct die_info
*follow_die_ref (struct die_info
*,
1140 const struct attribute
*,
1141 struct dwarf2_cu
**);
1143 static struct die_info
*follow_die_sig (struct die_info
*,
1144 const struct attribute
*,
1145 struct dwarf2_cu
**);
1147 static struct type
*get_signatured_type (struct die_info
*, ULONGEST
,
1148 struct dwarf2_cu
*);
1150 static struct type
*get_DW_AT_signature_type (struct die_info
*,
1151 const struct attribute
*,
1152 struct dwarf2_cu
*);
1154 static void load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
1155 dwarf2_per_objfile
*per_objfile
);
1157 static void read_signatured_type (signatured_type
*sig_type
,
1158 dwarf2_per_objfile
*per_objfile
);
1160 static int attr_to_dynamic_prop (const struct attribute
*attr
,
1161 struct die_info
*die
, struct dwarf2_cu
*cu
,
1162 struct dynamic_prop
*prop
, struct type
*type
);
1164 /* memory allocation interface */
1166 static struct dwarf_block
*dwarf_alloc_block (struct dwarf2_cu
*);
1168 static struct die_info
*dwarf_alloc_die (struct dwarf2_cu
*, int);
1170 static void dwarf_decode_macros (struct dwarf2_cu
*, unsigned int, int);
1172 static void fill_in_loclist_baton (struct dwarf2_cu
*cu
,
1173 struct dwarf2_loclist_baton
*baton
,
1174 const struct attribute
*attr
);
1176 static void dwarf2_symbol_mark_computed (const struct attribute
*attr
,
1178 struct dwarf2_cu
*cu
,
1181 static const gdb_byte
*skip_one_die (const struct die_reader_specs
*reader
,
1182 const gdb_byte
*info_ptr
,
1183 const struct abbrev_info
*abbrev
,
1184 bool do_skip_children
= true);
1186 static struct dwarf2_per_cu_data
*dwarf2_find_containing_comp_unit
1187 (sect_offset sect_off
, unsigned int offset_in_dwz
,
1188 dwarf2_per_bfd
*per_bfd
);
1190 static void prepare_one_comp_unit (struct dwarf2_cu
*cu
,
1191 struct die_info
*comp_unit_die
,
1192 enum language pretend_language
);
1194 static struct type
*set_die_type (struct die_info
*, struct type
*,
1195 struct dwarf2_cu
*, bool = false);
1197 static void create_all_comp_units (dwarf2_per_objfile
*per_objfile
);
1199 static void load_full_comp_unit (dwarf2_per_cu_data
*per_cu
,
1200 dwarf2_per_objfile
*per_objfile
,
1201 dwarf2_cu
*existing_cu
,
1203 enum language pretend_language
);
1205 static void process_full_comp_unit (dwarf2_cu
*cu
,
1206 enum language pretend_language
);
1208 static void process_full_type_unit (dwarf2_cu
*cu
,
1209 enum language pretend_language
);
1211 static struct type
*get_die_type_at_offset (sect_offset
,
1212 dwarf2_per_cu_data
*per_cu
,
1213 dwarf2_per_objfile
*per_objfile
);
1215 static struct type
*get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
);
1217 static void queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
1218 dwarf2_per_objfile
*per_objfile
,
1219 enum language pretend_language
);
1221 static void process_queue (dwarf2_per_objfile
*per_objfile
);
1223 /* Class, the destructor of which frees all allocated queue entries. This
1224 will only have work to do if an error was thrown while processing the
1225 dwarf. If no error was thrown then the queue entries should have all
1226 been processed, and freed, as we went along. */
1228 class dwarf2_queue_guard
1231 explicit dwarf2_queue_guard (dwarf2_per_objfile
*per_objfile
)
1232 : m_per_objfile (per_objfile
)
1234 gdb_assert (!m_per_objfile
->per_bfd
->queue
.has_value ());
1236 m_per_objfile
->per_bfd
->queue
.emplace ();
1239 /* Free any entries remaining on the queue. There should only be
1240 entries left if we hit an error while processing the dwarf. */
1241 ~dwarf2_queue_guard ()
1243 gdb_assert (m_per_objfile
->per_bfd
->queue
.has_value ());
1245 m_per_objfile
->per_bfd
->queue
.reset ();
1248 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard
);
1251 dwarf2_per_objfile
*m_per_objfile
;
1254 dwarf2_queue_item::~dwarf2_queue_item ()
1256 /* Anything still marked queued is likely to be in an
1257 inconsistent state, so discard it. */
1260 per_objfile
->remove_cu (per_cu
);
1265 /* See dwarf2/read.h. */
1268 dwarf2_per_cu_data_deleter::operator() (dwarf2_per_cu_data
*data
)
1270 if (data
->is_debug_types
)
1271 delete static_cast<signatured_type
*> (data
);
1276 static file_and_directory
&find_file_and_directory
1277 (struct die_info
*die
, struct dwarf2_cu
*cu
);
1279 static const char *compute_include_file_name
1280 (const struct line_header
*lh
,
1281 const file_entry
&fe
,
1282 const file_and_directory
&cu_info
,
1283 std::string
&name_holder
);
1285 static htab_up
allocate_signatured_type_table ();
1287 static htab_up
allocate_dwo_unit_table ();
1289 static struct dwo_unit
*lookup_dwo_unit_in_dwp
1290 (dwarf2_per_objfile
*per_objfile
, struct dwp_file
*dwp_file
,
1291 const char *comp_dir
, ULONGEST signature
, int is_debug_types
);
1293 static struct dwp_file
*get_dwp_file (dwarf2_per_objfile
*per_objfile
);
1295 static struct dwo_unit
*lookup_dwo_comp_unit
1296 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
1297 ULONGEST signature
);
1299 static struct dwo_unit
*lookup_dwo_type_unit
1300 (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
);
1302 static void queue_and_load_all_dwo_tus (dwarf2_cu
*cu
);
1304 /* A unique pointer to a dwo_file. */
1306 typedef std::unique_ptr
<struct dwo_file
> dwo_file_up
;
1308 static void process_cu_includes (dwarf2_per_objfile
*per_objfile
);
1310 static void check_producer (struct dwarf2_cu
*cu
);
1312 /* Various complaints about symbol reading that don't abort the process. */
1315 dwarf2_debug_line_missing_file_complaint (void)
1317 complaint (_(".debug_line section has line data without a file"));
1321 dwarf2_debug_line_missing_end_sequence_complaint (void)
1323 complaint (_(".debug_line section has line "
1324 "program sequence without an end"));
1328 dwarf2_complex_location_expr_complaint (void)
1330 complaint (_("location expression too complex"));
1334 dwarf2_const_value_length_mismatch_complaint (const char *arg1
, int arg2
,
1337 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1342 dwarf2_invalid_attrib_class_complaint (const char *arg1
, const char *arg2
)
1344 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1348 /* Hash function for line_header_hash. */
1351 line_header_hash (const struct line_header
*ofs
)
1353 return to_underlying (ofs
->sect_off
) ^ ofs
->offset_in_dwz
;
1356 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1359 line_header_hash_voidp (const void *item
)
1361 const struct line_header
*ofs
= (const struct line_header
*) item
;
1363 return line_header_hash (ofs
);
1366 /* Equality function for line_header_hash. */
1369 line_header_eq_voidp (const void *item_lhs
, const void *item_rhs
)
1371 const struct line_header
*ofs_lhs
= (const struct line_header
*) item_lhs
;
1372 const struct line_header
*ofs_rhs
= (const struct line_header
*) item_rhs
;
1374 return (ofs_lhs
->sect_off
== ofs_rhs
->sect_off
1375 && ofs_lhs
->offset_in_dwz
== ofs_rhs
->offset_in_dwz
);
1380 /* An iterator for all_comp_units that is based on index. This
1381 approach makes it possible to iterate over all_comp_units safely,
1382 when some caller in the loop may add new units. */
1384 class all_comp_units_iterator
1388 all_comp_units_iterator (dwarf2_per_bfd
*per_bfd
, bool start
)
1389 : m_per_bfd (per_bfd
),
1390 m_index (start
? 0 : per_bfd
->all_comp_units
.size ())
1394 all_comp_units_iterator
&operator++ ()
1400 dwarf2_per_cu_data
*operator* () const
1402 return m_per_bfd
->get_cu (m_index
);
1405 bool operator== (const all_comp_units_iterator
&other
) const
1407 return m_index
== other
.m_index
;
1411 bool operator!= (const all_comp_units_iterator
&other
) const
1413 return m_index
!= other
.m_index
;
1418 dwarf2_per_bfd
*m_per_bfd
;
1422 /* A range adapter for the all_comp_units_iterator. */
1423 class all_comp_units_range
1427 all_comp_units_range (dwarf2_per_bfd
*per_bfd
)
1428 : m_per_bfd (per_bfd
)
1432 all_comp_units_iterator
begin ()
1434 return all_comp_units_iterator (m_per_bfd
, true);
1437 all_comp_units_iterator
end ()
1439 return all_comp_units_iterator (m_per_bfd
, false);
1444 dwarf2_per_bfd
*m_per_bfd
;
1447 /* See declaration. */
1449 dwarf2_per_bfd::dwarf2_per_bfd (bfd
*obfd
, const dwarf2_debug_sections
*names
,
1452 can_copy (can_copy_
)
1455 names
= &dwarf2_elf_names
;
1457 for (asection
*sec
= obfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
1458 locate_sections (obfd
, sec
, *names
);
1461 dwarf2_per_bfd::~dwarf2_per_bfd ()
1463 for (auto &per_cu
: all_comp_units
)
1465 per_cu
->imported_symtabs_free ();
1466 per_cu
->free_cached_file_names ();
1469 /* Everything else should be on this->obstack. */
1475 dwarf2_per_objfile::remove_all_cus ()
1477 gdb_assert (!this->per_bfd
->queue
.has_value ());
1479 for (auto pair
: m_dwarf2_cus
)
1482 m_dwarf2_cus
.clear ();
1485 /* A helper class that calls free_cached_comp_units on
1488 class free_cached_comp_units
1492 explicit free_cached_comp_units (dwarf2_per_objfile
*per_objfile
)
1493 : m_per_objfile (per_objfile
)
1497 ~free_cached_comp_units ()
1499 m_per_objfile
->remove_all_cus ();
1502 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units
);
1506 dwarf2_per_objfile
*m_per_objfile
;
1512 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data
*per_cu
) const
1514 if (per_cu
->index
< this->m_symtabs
.size ())
1515 return this->m_symtabs
[per_cu
->index
] != nullptr;
1522 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data
*per_cu
) const
1524 if (per_cu
->index
< this->m_symtabs
.size ())
1525 return this->m_symtabs
[per_cu
->index
];
1532 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data
*per_cu
,
1533 compunit_symtab
*symtab
)
1535 if (per_cu
->index
>= this->m_symtabs
.size ())
1536 this->m_symtabs
.resize (per_cu
->index
+ 1);
1537 gdb_assert (this->m_symtabs
[per_cu
->index
] == nullptr);
1538 this->m_symtabs
[per_cu
->index
] = symtab
;
1541 /* Try to locate the sections we need for DWARF 2 debugging
1542 information and return true if we have enough to do something.
1543 NAMES points to the dwarf2 section names, or is NULL if the standard
1544 ELF names are used. CAN_COPY is true for formats where symbol
1545 interposition is possible and so symbol values must follow copy
1546 relocation rules. */
1549 dwarf2_has_info (struct objfile
*objfile
,
1550 const struct dwarf2_debug_sections
*names
,
1553 if (objfile
->flags
& OBJF_READNEVER
)
1556 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1558 if (per_objfile
== NULL
)
1560 dwarf2_per_bfd
*per_bfd
;
1562 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1563 BFD doesn't require relocations.
1565 We don't share with objfiles for which -readnow was requested,
1566 because it would complicate things when loading the same BFD with
1567 -readnow and then without -readnow. */
1568 if (!gdb_bfd_requires_relocations (objfile
->obfd
)
1569 && (objfile
->flags
& OBJF_READNOW
) == 0)
1571 /* See if one has been created for this BFD yet. */
1572 per_bfd
= dwarf2_per_bfd_bfd_data_key
.get (objfile
->obfd
);
1574 if (per_bfd
== nullptr)
1576 /* No, create it now. */
1577 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
, names
, can_copy
);
1578 dwarf2_per_bfd_bfd_data_key
.set (objfile
->obfd
, per_bfd
);
1583 /* No sharing possible, create one specifically for this objfile. */
1584 per_bfd
= new dwarf2_per_bfd (objfile
->obfd
, names
, can_copy
);
1585 dwarf2_per_bfd_objfile_data_key
.set (objfile
, per_bfd
);
1588 per_objfile
= dwarf2_objfile_data_key
.emplace (objfile
, objfile
, per_bfd
);
1591 return (!per_objfile
->per_bfd
->info
.is_virtual
1592 && per_objfile
->per_bfd
->info
.s
.section
!= NULL
1593 && !per_objfile
->per_bfd
->abbrev
.is_virtual
1594 && per_objfile
->per_bfd
->abbrev
.s
.section
!= NULL
);
1597 /* See declaration. */
1600 dwarf2_per_bfd::locate_sections (bfd
*abfd
, asection
*sectp
,
1601 const dwarf2_debug_sections
&names
)
1603 flagword aflag
= bfd_section_flags (sectp
);
1605 if ((aflag
& SEC_HAS_CONTENTS
) == 0)
1608 else if (elf_section_data (sectp
)->this_hdr
.sh_size
1609 > bfd_get_file_size (abfd
))
1611 bfd_size_type size
= elf_section_data (sectp
)->this_hdr
.sh_size
;
1612 warning (_("Discarding section %s which has a section size (%s"
1613 ") larger than the file size [in module %s]"),
1614 bfd_section_name (sectp
), phex_nz (size
, sizeof (size
)),
1615 bfd_get_filename (abfd
));
1617 else if (names
.info
.matches (sectp
->name
))
1619 this->info
.s
.section
= sectp
;
1620 this->info
.size
= bfd_section_size (sectp
);
1622 else if (names
.abbrev
.matches (sectp
->name
))
1624 this->abbrev
.s
.section
= sectp
;
1625 this->abbrev
.size
= bfd_section_size (sectp
);
1627 else if (names
.line
.matches (sectp
->name
))
1629 this->line
.s
.section
= sectp
;
1630 this->line
.size
= bfd_section_size (sectp
);
1632 else if (names
.loc
.matches (sectp
->name
))
1634 this->loc
.s
.section
= sectp
;
1635 this->loc
.size
= bfd_section_size (sectp
);
1637 else if (names
.loclists
.matches (sectp
->name
))
1639 this->loclists
.s
.section
= sectp
;
1640 this->loclists
.size
= bfd_section_size (sectp
);
1642 else if (names
.macinfo
.matches (sectp
->name
))
1644 this->macinfo
.s
.section
= sectp
;
1645 this->macinfo
.size
= bfd_section_size (sectp
);
1647 else if (names
.macro
.matches (sectp
->name
))
1649 this->macro
.s
.section
= sectp
;
1650 this->macro
.size
= bfd_section_size (sectp
);
1652 else if (names
.str
.matches (sectp
->name
))
1654 this->str
.s
.section
= sectp
;
1655 this->str
.size
= bfd_section_size (sectp
);
1657 else if (names
.str_offsets
.matches (sectp
->name
))
1659 this->str_offsets
.s
.section
= sectp
;
1660 this->str_offsets
.size
= bfd_section_size (sectp
);
1662 else if (names
.line_str
.matches (sectp
->name
))
1664 this->line_str
.s
.section
= sectp
;
1665 this->line_str
.size
= bfd_section_size (sectp
);
1667 else if (names
.addr
.matches (sectp
->name
))
1669 this->addr
.s
.section
= sectp
;
1670 this->addr
.size
= bfd_section_size (sectp
);
1672 else if (names
.frame
.matches (sectp
->name
))
1674 this->frame
.s
.section
= sectp
;
1675 this->frame
.size
= bfd_section_size (sectp
);
1677 else if (names
.eh_frame
.matches (sectp
->name
))
1679 this->eh_frame
.s
.section
= sectp
;
1680 this->eh_frame
.size
= bfd_section_size (sectp
);
1682 else if (names
.ranges
.matches (sectp
->name
))
1684 this->ranges
.s
.section
= sectp
;
1685 this->ranges
.size
= bfd_section_size (sectp
);
1687 else if (names
.rnglists
.matches (sectp
->name
))
1689 this->rnglists
.s
.section
= sectp
;
1690 this->rnglists
.size
= bfd_section_size (sectp
);
1692 else if (names
.types
.matches (sectp
->name
))
1694 struct dwarf2_section_info type_section
;
1696 memset (&type_section
, 0, sizeof (type_section
));
1697 type_section
.s
.section
= sectp
;
1698 type_section
.size
= bfd_section_size (sectp
);
1700 this->types
.push_back (type_section
);
1702 else if (names
.gdb_index
.matches (sectp
->name
))
1704 this->gdb_index
.s
.section
= sectp
;
1705 this->gdb_index
.size
= bfd_section_size (sectp
);
1707 else if (names
.debug_names
.matches (sectp
->name
))
1709 this->debug_names
.s
.section
= sectp
;
1710 this->debug_names
.size
= bfd_section_size (sectp
);
1712 else if (names
.debug_aranges
.matches (sectp
->name
))
1714 this->debug_aranges
.s
.section
= sectp
;
1715 this->debug_aranges
.size
= bfd_section_size (sectp
);
1718 if ((bfd_section_flags (sectp
) & (SEC_LOAD
| SEC_ALLOC
))
1719 && bfd_section_vma (sectp
) == 0)
1720 this->has_section_at_zero
= true;
1723 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1727 dwarf2_get_section_info (struct objfile
*objfile
,
1728 enum dwarf2_section_enum sect
,
1729 asection
**sectp
, const gdb_byte
**bufp
,
1730 bfd_size_type
*sizep
)
1732 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
1733 struct dwarf2_section_info
*info
;
1735 /* We may see an objfile without any DWARF, in which case we just
1737 if (per_objfile
== NULL
)
1746 case DWARF2_DEBUG_FRAME
:
1747 info
= &per_objfile
->per_bfd
->frame
;
1749 case DWARF2_EH_FRAME
:
1750 info
= &per_objfile
->per_bfd
->eh_frame
;
1753 gdb_assert_not_reached ("unexpected section");
1756 info
->read (objfile
);
1758 *sectp
= info
->get_bfd_section ();
1759 *bufp
= info
->buffer
;
1760 *sizep
= info
->size
;
1763 /* See dwarf2/read.h. */
1766 dwarf2_per_bfd::map_info_sections (struct objfile
*objfile
)
1768 info
.read (objfile
);
1769 abbrev
.read (objfile
);
1770 line
.read (objfile
);
1772 str_offsets
.read (objfile
);
1773 line_str
.read (objfile
);
1774 ranges
.read (objfile
);
1775 rnglists
.read (objfile
);
1776 addr
.read (objfile
);
1778 for (auto §ion
: types
)
1779 section
.read (objfile
);
1783 /* DWARF quick_symbol_functions support. */
1785 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1786 unique line tables, so we maintain a separate table of all .debug_line
1787 derived entries to support the sharing.
1788 All the quick functions need is the list of file names. We discard the
1789 line_header when we're done and don't need to record it here. */
1790 struct quick_file_names
1792 /* The data used to construct the hash key. */
1793 struct stmt_list_hash hash
;
1795 /* The number of entries in file_names, real_names. */
1796 unsigned int num_file_names
;
1798 /* The CU directory, as given by DW_AT_comp_dir. May be
1800 const char *comp_dir
;
1802 /* The file names from the line table, after being run through
1804 const char **file_names
;
1806 /* The file names from the line table after being run through
1807 gdb_realpath. These are computed lazily. */
1808 const char **real_names
;
1811 struct dwarf2_base_index_functions
: public quick_symbol_functions
1813 bool has_symbols (struct objfile
*objfile
) override
;
1815 bool has_unexpanded_symtabs (struct objfile
*objfile
) override
;
1817 struct symtab
*find_last_source_symtab (struct objfile
*objfile
) override
;
1819 void forget_cached_source_info (struct objfile
*objfile
) override
;
1821 enum language
lookup_global_symbol_language (struct objfile
*objfile
,
1824 bool *symbol_found_p
) override
1826 *symbol_found_p
= false;
1827 return language_unknown
;
1830 void print_stats (struct objfile
*objfile
, bool print_bcache
) override
;
1832 void expand_all_symtabs (struct objfile
*objfile
) override
;
1834 /* A helper function that finds the per-cu object from an "adjusted"
1835 PC -- a PC with the base text offset removed. */
1836 virtual dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
1837 CORE_ADDR adjusted_pc
);
1839 struct compunit_symtab
*find_pc_sect_compunit_symtab
1840 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
1841 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
)
1844 struct compunit_symtab
*find_compunit_symtab_by_address
1845 (struct objfile
*objfile
, CORE_ADDR address
) override
1850 void map_symbol_filenames (struct objfile
*objfile
,
1851 gdb::function_view
<symbol_filename_ftype
> fun
,
1852 bool need_fullname
) override
;
1855 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1856 It's handy in this case to have an empty implementation of the
1857 quick symbol functions, to avoid special cases in the rest of the
1860 struct readnow_functions
: public dwarf2_base_index_functions
1862 void dump (struct objfile
*objfile
) override
1866 void expand_matching_symbols
1868 const lookup_name_info
&lookup_name
,
1871 symbol_compare_ftype
*ordered_compare
) override
1875 bool expand_symtabs_matching
1876 (struct objfile
*objfile
,
1877 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1878 const lookup_name_info
*lookup_name
,
1879 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1880 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1881 block_search_flags search_flags
,
1883 enum search_domain kind
) override
1889 struct dwarf2_gdb_index
: public dwarf2_base_index_functions
1891 void dump (struct objfile
*objfile
) override
;
1893 void expand_matching_symbols
1895 const lookup_name_info
&lookup_name
,
1898 symbol_compare_ftype
*ordered_compare
) override
;
1900 bool expand_symtabs_matching
1901 (struct objfile
*objfile
,
1902 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1903 const lookup_name_info
*lookup_name
,
1904 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1905 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1906 block_search_flags search_flags
,
1908 enum search_domain kind
) override
;
1911 struct dwarf2_debug_names_index
: public dwarf2_base_index_functions
1913 void dump (struct objfile
*objfile
) override
;
1915 void expand_matching_symbols
1917 const lookup_name_info
&lookup_name
,
1920 symbol_compare_ftype
*ordered_compare
) override
;
1922 bool expand_symtabs_matching
1923 (struct objfile
*objfile
,
1924 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1925 const lookup_name_info
*lookup_name
,
1926 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1927 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1928 block_search_flags search_flags
,
1930 enum search_domain kind
) override
;
1933 quick_symbol_functions_up
1934 mapped_index::make_quick_functions () const
1936 return quick_symbol_functions_up (new dwarf2_gdb_index
);
1939 quick_symbol_functions_up
1940 mapped_debug_names::make_quick_functions () const
1942 return quick_symbol_functions_up (new dwarf2_debug_names_index
);
1945 /* Utility hash function for a stmt_list_hash. */
1948 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1952 if (stmt_list_hash
->dwo_unit
!= NULL
)
1953 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1954 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1958 /* Utility equality function for a stmt_list_hash. */
1961 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1962 const struct stmt_list_hash
*rhs
)
1964 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1966 if (lhs
->dwo_unit
!= NULL
1967 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1970 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1973 /* Hash function for a quick_file_names. */
1976 hash_file_name_entry (const void *e
)
1978 const struct quick_file_names
*file_data
1979 = (const struct quick_file_names
*) e
;
1981 return hash_stmt_list_entry (&file_data
->hash
);
1984 /* Equality function for a quick_file_names. */
1987 eq_file_name_entry (const void *a
, const void *b
)
1989 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
1990 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
1992 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
1995 /* Create a quick_file_names hash table. */
1998 create_quick_file_names_table (unsigned int nr_initial_entries
)
2000 return htab_up (htab_create_alloc (nr_initial_entries
,
2001 hash_file_name_entry
, eq_file_name_entry
,
2002 nullptr, xcalloc
, xfree
));
2005 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2006 function is unrelated to symtabs, symtab would have to be created afterwards.
2007 You should call age_cached_comp_units after processing the CU. */
2010 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
2013 if (per_cu
->is_debug_types
)
2014 load_full_type_unit (per_cu
, per_objfile
);
2016 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
2017 skip_partial
, language_minimal
);
2019 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
2021 return nullptr; /* Dummy CU. */
2023 dwarf2_find_base_address (cu
->dies
, cu
);
2028 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2031 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2032 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
2035 /* The destructor of dwarf2_queue_guard frees any entries left on
2036 the queue. After this point we're guaranteed to leave this function
2037 with the dwarf queue empty. */
2038 dwarf2_queue_guard
q_guard (per_objfile
);
2040 if (!per_objfile
->symtab_set_p (per_cu
))
2042 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
2043 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
2045 /* If we just loaded a CU from a DWO, and we're working with an index
2046 that may badly handle TUs, load all the TUs in that DWO as well.
2047 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2048 if (!per_cu
->is_debug_types
2050 && cu
->dwo_unit
!= NULL
2051 && per_objfile
->per_bfd
->index_table
!= NULL
2052 && !per_objfile
->per_bfd
->index_table
->version_check ()
2053 /* DWP files aren't supported yet. */
2054 && get_dwp_file (per_objfile
) == NULL
)
2055 queue_and_load_all_dwo_tus (cu
);
2058 process_queue (per_objfile
);
2061 /* Age the cache, releasing compilation units that have not
2062 been used recently. */
2063 per_objfile
->age_comp_units ();
2066 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2067 the per-objfile for which this symtab is instantiated.
2069 Returns the resulting symbol table. */
2071 static struct compunit_symtab
*
2072 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2073 dwarf2_per_objfile
*per_objfile
,
2076 if (!per_objfile
->symtab_set_p (per_cu
))
2078 free_cached_comp_units
freer (per_objfile
);
2079 scoped_restore decrementer
= increment_reading_symtab ();
2080 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
2081 process_cu_includes (per_objfile
);
2084 return per_objfile
->get_symtab (per_cu
);
2089 dwarf2_per_cu_data_up
2090 dwarf2_per_bfd::allocate_per_cu ()
2092 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
2093 result
->per_bfd
= this;
2094 result
->index
= all_comp_units
.size ();
2101 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
2103 signatured_type_up
result (new signatured_type (signature
));
2104 result
->per_bfd
= this;
2105 result
->index
= all_comp_units
.size ();
2106 result
->is_debug_types
= true;
2111 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2112 obstack, and constructed with the specified field values. */
2114 static dwarf2_per_cu_data_up
2115 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
2116 struct dwarf2_section_info
*section
,
2118 sect_offset sect_off
, ULONGEST length
)
2120 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
2121 the_cu
->sect_off
= sect_off
;
2122 the_cu
->length
= length
;
2123 the_cu
->section
= section
;
2124 the_cu
->is_dwz
= is_dwz
;
2128 /* A helper for create_cus_from_index that handles a given list of
2132 create_cus_from_index_list (dwarf2_per_bfd
*per_bfd
,
2133 const gdb_byte
*cu_list
, offset_type n_elements
,
2134 struct dwarf2_section_info
*section
,
2137 for (offset_type i
= 0; i
< n_elements
; i
+= 2)
2139 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2141 sect_offset sect_off
2142 = (sect_offset
) extract_unsigned_integer (cu_list
, 8, BFD_ENDIAN_LITTLE
);
2143 ULONGEST length
= extract_unsigned_integer (cu_list
+ 8, 8, BFD_ENDIAN_LITTLE
);
2146 dwarf2_per_cu_data_up per_cu
2147 = create_cu_from_index_list (per_bfd
, section
, is_dwz
, sect_off
,
2149 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
2153 /* Read the CU list from the mapped index, and use it to create all
2154 the CU objects for PER_BFD. */
2157 create_cus_from_index (dwarf2_per_bfd
*per_bfd
,
2158 const gdb_byte
*cu_list
, offset_type cu_list_elements
,
2159 const gdb_byte
*dwz_list
, offset_type dwz_elements
)
2161 gdb_assert (per_bfd
->all_comp_units
.empty ());
2162 per_bfd
->all_comp_units
.reserve ((cu_list_elements
+ dwz_elements
) / 2);
2164 create_cus_from_index_list (per_bfd
, cu_list
, cu_list_elements
,
2167 if (dwz_elements
== 0)
2170 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
2171 create_cus_from_index_list (per_bfd
, dwz_list
, dwz_elements
,
2175 /* Create the signatured type hash table from the index. */
2178 create_signatured_type_table_from_index
2179 (dwarf2_per_bfd
*per_bfd
, struct dwarf2_section_info
*section
,
2180 const gdb_byte
*bytes
, offset_type elements
)
2182 htab_up sig_types_hash
= allocate_signatured_type_table ();
2184 for (offset_type i
= 0; i
< elements
; i
+= 3)
2186 signatured_type_up sig_type
;
2189 cu_offset type_offset_in_tu
;
2191 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2192 sect_offset sect_off
2193 = (sect_offset
) extract_unsigned_integer (bytes
, 8, BFD_ENDIAN_LITTLE
);
2195 = (cu_offset
) extract_unsigned_integer (bytes
+ 8, 8,
2197 signature
= extract_unsigned_integer (bytes
+ 16, 8, BFD_ENDIAN_LITTLE
);
2200 sig_type
= per_bfd
->allocate_signatured_type (signature
);
2201 sig_type
->type_offset_in_tu
= type_offset_in_tu
;
2202 sig_type
->section
= section
;
2203 sig_type
->sect_off
= sect_off
;
2205 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2206 *slot
= sig_type
.get ();
2208 per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2211 per_bfd
->signatured_types
= std::move (sig_types_hash
);
2214 /* Create the signatured type hash table from .debug_names. */
2217 create_signatured_type_table_from_debug_names
2218 (dwarf2_per_objfile
*per_objfile
,
2219 const mapped_debug_names
&map
,
2220 struct dwarf2_section_info
*section
,
2221 struct dwarf2_section_info
*abbrev_section
)
2223 struct objfile
*objfile
= per_objfile
->objfile
;
2225 section
->read (objfile
);
2226 abbrev_section
->read (objfile
);
2228 htab_up sig_types_hash
= allocate_signatured_type_table ();
2230 for (uint32_t i
= 0; i
< map
.tu_count
; ++i
)
2232 signatured_type_up sig_type
;
2235 sect_offset sect_off
2236 = (sect_offset
) (extract_unsigned_integer
2237 (map
.tu_table_reordered
+ i
* map
.offset_size
,
2239 map
.dwarf5_byte_order
));
2241 comp_unit_head cu_header
;
2242 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
2244 section
->buffer
+ to_underlying (sect_off
),
2247 sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
2248 (cu_header
.signature
);
2249 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
2250 sig_type
->section
= section
;
2251 sig_type
->sect_off
= sect_off
;
2253 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2254 *slot
= sig_type
.get ();
2256 per_objfile
->per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2259 per_objfile
->per_bfd
->signatured_types
= std::move (sig_types_hash
);
2262 /* Read the address map data from the mapped index, and use it to
2263 populate the psymtabs_addrmap. */
2266 create_addrmap_from_index (dwarf2_per_objfile
*per_objfile
,
2267 struct mapped_index
*index
)
2269 struct objfile
*objfile
= per_objfile
->objfile
;
2270 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2271 struct gdbarch
*gdbarch
= objfile
->arch ();
2272 const gdb_byte
*iter
, *end
;
2273 struct addrmap
*mutable_map
;
2276 auto_obstack temp_obstack
;
2278 mutable_map
= addrmap_create_mutable (&temp_obstack
);
2280 iter
= index
->address_table
.data ();
2281 end
= iter
+ index
->address_table
.size ();
2283 baseaddr
= objfile
->text_section_offset ();
2287 ULONGEST hi
, lo
, cu_index
;
2288 lo
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2290 hi
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2292 cu_index
= extract_unsigned_integer (iter
, 4, BFD_ENDIAN_LITTLE
);
2297 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2298 hex_string (lo
), hex_string (hi
));
2302 if (cu_index
>= per_bfd
->all_comp_units
.size ())
2304 complaint (_(".gdb_index address table has invalid CU number %u"),
2305 (unsigned) cu_index
);
2309 lo
= gdbarch_adjust_dwarf2_addr (gdbarch
, lo
+ baseaddr
) - baseaddr
;
2310 hi
= gdbarch_adjust_dwarf2_addr (gdbarch
, hi
+ baseaddr
) - baseaddr
;
2311 addrmap_set_empty (mutable_map
, lo
, hi
- 1,
2312 per_bfd
->get_cu (cu_index
));
2315 per_bfd
->index_addrmap
= addrmap_create_fixed (mutable_map
,
2319 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2320 to populate given addrmap. Returns true on success, false on
2324 read_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2325 struct dwarf2_section_info
*section
,
2326 addrmap
*mutable_map
)
2328 struct objfile
*objfile
= per_objfile
->objfile
;
2329 bfd
*abfd
= objfile
->obfd
;
2330 struct gdbarch
*gdbarch
= objfile
->arch ();
2331 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
2332 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2334 std::unordered_map
<sect_offset
,
2335 dwarf2_per_cu_data
*,
2336 gdb::hash_enum
<sect_offset
>>
2337 debug_info_offset_to_per_cu
;
2338 for (const auto &per_cu
: per_bfd
->all_comp_units
)
2340 /* A TU will not need aranges, and skipping them here is an easy
2341 way of ignoring .debug_types -- and possibly seeing a
2342 duplicate section offset -- entirely. The same applies to
2343 units coming from a dwz file. */
2344 if (per_cu
->is_debug_types
|| per_cu
->is_dwz
)
2347 const auto insertpair
2348 = debug_info_offset_to_per_cu
.emplace (per_cu
->sect_off
,
2350 if (!insertpair
.second
)
2352 warning (_("Section .debug_aranges in %s has duplicate "
2353 "debug_info_offset %s, ignoring .debug_aranges."),
2354 objfile_name (objfile
), sect_offset_str (per_cu
->sect_off
));
2359 section
->read (objfile
);
2361 const bfd_endian dwarf5_byte_order
= gdbarch_byte_order (gdbarch
);
2363 const gdb_byte
*addr
= section
->buffer
;
2365 while (addr
< section
->buffer
+ section
->size
)
2367 const gdb_byte
*const entry_addr
= addr
;
2368 unsigned int bytes_read
;
2370 const LONGEST entry_length
= read_initial_length (abfd
, addr
,
2374 const gdb_byte
*const entry_end
= addr
+ entry_length
;
2375 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
2376 const uint8_t offset_size
= dwarf5_is_dwarf64
? 8 : 4;
2377 if (addr
+ entry_length
> section
->buffer
+ section
->size
)
2379 warning (_("Section .debug_aranges in %s entry at offset %s "
2380 "length %s exceeds section length %s, "
2381 "ignoring .debug_aranges."),
2382 objfile_name (objfile
),
2383 plongest (entry_addr
- section
->buffer
),
2384 plongest (bytes_read
+ entry_length
),
2385 pulongest (section
->size
));
2389 /* The version number. */
2390 const uint16_t version
= read_2_bytes (abfd
, addr
);
2394 warning (_("Section .debug_aranges in %s entry at offset %s "
2395 "has unsupported version %d, ignoring .debug_aranges."),
2396 objfile_name (objfile
),
2397 plongest (entry_addr
- section
->buffer
), version
);
2401 const uint64_t debug_info_offset
2402 = extract_unsigned_integer (addr
, offset_size
, dwarf5_byte_order
);
2403 addr
+= offset_size
;
2404 const auto per_cu_it
2405 = debug_info_offset_to_per_cu
.find (sect_offset (debug_info_offset
));
2406 if (per_cu_it
== debug_info_offset_to_per_cu
.cend ())
2408 warning (_("Section .debug_aranges in %s entry at offset %s "
2409 "debug_info_offset %s does not exists, "
2410 "ignoring .debug_aranges."),
2411 objfile_name (objfile
),
2412 plongest (entry_addr
- section
->buffer
),
2413 pulongest (debug_info_offset
));
2416 dwarf2_per_cu_data
*const per_cu
= per_cu_it
->second
;
2418 const uint8_t address_size
= *addr
++;
2419 if (address_size
< 1 || address_size
> 8)
2421 warning (_("Section .debug_aranges in %s entry at offset %s "
2422 "address_size %u is invalid, ignoring .debug_aranges."),
2423 objfile_name (objfile
),
2424 plongest (entry_addr
- section
->buffer
), address_size
);
2428 const uint8_t segment_selector_size
= *addr
++;
2429 if (segment_selector_size
!= 0)
2431 warning (_("Section .debug_aranges in %s entry at offset %s "
2432 "segment_selector_size %u is not supported, "
2433 "ignoring .debug_aranges."),
2434 objfile_name (objfile
),
2435 plongest (entry_addr
- section
->buffer
),
2436 segment_selector_size
);
2440 /* Must pad to an alignment boundary that is twice the address
2441 size. It is undocumented by the DWARF standard but GCC does
2442 use it. However, not every compiler does this. We can see
2443 whether it has happened by looking at the total length of the
2444 contents of the aranges for this CU -- it if isn't a multiple
2445 of twice the address size, then we skip any leftover
2447 addr
+= (entry_end
- addr
) % (2 * address_size
);
2449 while (addr
< entry_end
)
2451 if (addr
+ 2 * address_size
> entry_end
)
2453 warning (_("Section .debug_aranges in %s entry at offset %s "
2454 "address list is not properly terminated, "
2455 "ignoring .debug_aranges."),
2456 objfile_name (objfile
),
2457 plongest (entry_addr
- section
->buffer
));
2460 ULONGEST start
= extract_unsigned_integer (addr
, address_size
,
2462 addr
+= address_size
;
2463 ULONGEST length
= extract_unsigned_integer (addr
, address_size
,
2465 addr
+= address_size
;
2466 if (start
== 0 && length
== 0)
2468 /* This can happen on some targets with --gc-sections.
2469 This pair of values is also used to mark the end of
2470 the entries for a given CU, but we ignore it and
2471 instead handle termination using the check at the top
2475 if (start
== 0 && !per_bfd
->has_section_at_zero
)
2477 /* Symbol was eliminated due to a COMDAT group. */
2480 ULONGEST end
= start
+ length
;
2481 start
= (gdbarch_adjust_dwarf2_addr (gdbarch
, start
+ baseaddr
)
2483 end
= (gdbarch_adjust_dwarf2_addr (gdbarch
, end
+ baseaddr
)
2485 addrmap_set_empty (mutable_map
, start
, end
- 1, per_cu
);
2488 per_cu
->addresses_seen
= true;
2494 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2495 populate the psymtabs_addrmap. */
2498 create_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2499 struct dwarf2_section_info
*section
)
2501 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2503 auto_obstack temp_obstack
;
2504 addrmap
*mutable_map
= addrmap_create_mutable (&temp_obstack
);
2506 if (read_addrmap_from_aranges (per_objfile
, section
, mutable_map
))
2507 per_bfd
->index_addrmap
= addrmap_create_fixed (mutable_map
,
2511 /* A helper function that reads the .gdb_index from BUFFER and fills
2512 in MAP. FILENAME is the name of the file containing the data;
2513 it is used for error reporting. DEPRECATED_OK is true if it is
2514 ok to use deprecated sections.
2516 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2517 out parameters that are filled in with information about the CU and
2518 TU lists in the section.
2520 Returns true if all went well, false otherwise. */
2523 read_gdb_index_from_buffer (const char *filename
,
2525 gdb::array_view
<const gdb_byte
> buffer
,
2526 struct mapped_index
*map
,
2527 const gdb_byte
**cu_list
,
2528 offset_type
*cu_list_elements
,
2529 const gdb_byte
**types_list
,
2530 offset_type
*types_list_elements
)
2532 const gdb_byte
*addr
= &buffer
[0];
2533 offset_view
metadata (buffer
);
2535 /* Version check. */
2536 offset_type version
= metadata
[0];
2537 /* Versions earlier than 3 emitted every copy of a psymbol. This
2538 causes the index to behave very poorly for certain requests. Version 3
2539 contained incomplete addrmap. So, it seems better to just ignore such
2543 static int warning_printed
= 0;
2544 if (!warning_printed
)
2546 warning (_("Skipping obsolete .gdb_index section in %s."),
2548 warning_printed
= 1;
2552 /* Index version 4 uses a different hash function than index version
2555 Versions earlier than 6 did not emit psymbols for inlined
2556 functions. Using these files will cause GDB not to be able to
2557 set breakpoints on inlined functions by name, so we ignore these
2558 indices unless the user has done
2559 "set use-deprecated-index-sections on". */
2560 if (version
< 6 && !deprecated_ok
)
2562 static int warning_printed
= 0;
2563 if (!warning_printed
)
2566 Skipping deprecated .gdb_index section in %s.\n\
2567 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2568 to use the section anyway."),
2570 warning_printed
= 1;
2574 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2575 of the TU (for symbols coming from TUs),
2576 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2577 Plus gold-generated indices can have duplicate entries for global symbols,
2578 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2579 These are just performance bugs, and we can't distinguish gdb-generated
2580 indices from gold-generated ones, so issue no warning here. */
2582 /* Indexes with higher version than the one supported by GDB may be no
2583 longer backward compatible. */
2587 map
->version
= version
;
2590 *cu_list
= addr
+ metadata
[i
];
2591 *cu_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2594 *types_list
= addr
+ metadata
[i
];
2595 *types_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2598 const gdb_byte
*address_table
= addr
+ metadata
[i
];
2599 const gdb_byte
*address_table_end
= addr
+ metadata
[i
+ 1];
2601 = gdb::array_view
<const gdb_byte
> (address_table
, address_table_end
);
2604 const gdb_byte
*symbol_table
= addr
+ metadata
[i
];
2605 const gdb_byte
*symbol_table_end
= addr
+ metadata
[i
+ 1];
2607 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2611 map
->constant_pool
= buffer
.slice (metadata
[i
]);
2613 if (map
->constant_pool
.empty () && !map
->symbol_table
.empty ())
2615 /* An empty constant pool implies that all symbol table entries are
2616 empty. Make map->symbol_table.empty () == true. */
2618 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2625 /* Callback types for dwarf2_read_gdb_index. */
2627 typedef gdb::function_view
2628 <gdb::array_view
<const gdb_byte
>(objfile
*, dwarf2_per_bfd
*)>
2629 get_gdb_index_contents_ftype
;
2630 typedef gdb::function_view
2631 <gdb::array_view
<const gdb_byte
>(objfile
*, dwz_file
*)>
2632 get_gdb_index_contents_dwz_ftype
;
2634 /* Read .gdb_index. If everything went ok, initialize the "quick"
2635 elements of all the CUs and return 1. Otherwise, return 0. */
2638 dwarf2_read_gdb_index
2639 (dwarf2_per_objfile
*per_objfile
,
2640 get_gdb_index_contents_ftype get_gdb_index_contents
,
2641 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz
)
2643 const gdb_byte
*cu_list
, *types_list
, *dwz_list
= NULL
;
2644 offset_type cu_list_elements
, types_list_elements
, dwz_list_elements
= 0;
2645 struct dwz_file
*dwz
;
2646 struct objfile
*objfile
= per_objfile
->objfile
;
2647 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2649 gdb::array_view
<const gdb_byte
> main_index_contents
2650 = get_gdb_index_contents (objfile
, per_bfd
);
2652 if (main_index_contents
.empty ())
2655 std::unique_ptr
<struct mapped_index
> map (new struct mapped_index
);
2656 if (!read_gdb_index_from_buffer (objfile_name (objfile
),
2657 use_deprecated_index_sections
,
2658 main_index_contents
, map
.get (), &cu_list
,
2659 &cu_list_elements
, &types_list
,
2660 &types_list_elements
))
2663 /* Don't use the index if it's empty. */
2664 if (map
->symbol_table
.empty ())
2667 /* If there is a .dwz file, read it so we can get its CU list as
2669 dwz
= dwarf2_get_dwz_file (per_bfd
);
2672 struct mapped_index dwz_map
;
2673 const gdb_byte
*dwz_types_ignore
;
2674 offset_type dwz_types_elements_ignore
;
2676 gdb::array_view
<const gdb_byte
> dwz_index_content
2677 = get_gdb_index_contents_dwz (objfile
, dwz
);
2679 if (dwz_index_content
.empty ())
2682 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz
->dwz_bfd
.get ()),
2683 1, dwz_index_content
, &dwz_map
,
2684 &dwz_list
, &dwz_list_elements
,
2686 &dwz_types_elements_ignore
))
2688 warning (_("could not read '.gdb_index' section from %s; skipping"),
2689 bfd_get_filename (dwz
->dwz_bfd
.get ()));
2694 create_cus_from_index (per_bfd
, cu_list
, cu_list_elements
, dwz_list
,
2697 if (types_list_elements
)
2699 /* We can only handle a single .debug_types when we have an
2701 if (per_bfd
->types
.size () != 1)
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 /* \377 (0xff) is Latin1 'ÿ'. */
3634 /* \377 (0xff) is Latin1 'ÿ'. */
3638 /* A name with all sorts of complications. Starts with "z" to make
3639 it easier for the completion tests below. */
3640 #define Z_SYM_NAME \
3641 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3642 "::tuple<(anonymous namespace)::ui*, " \
3643 "std::default_delete<(anonymous namespace)::ui>, void>"
3648 /* Returns true if the mapped_index_base::find_name_component_bounds
3649 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3650 in completion mode. */
3653 check_find_bounds_finds (mapped_index_base
&index
,
3654 const char *search_name
,
3655 gdb::array_view
<const char *> expected_syms
,
3656 dwarf2_per_objfile
*per_objfile
)
3658 lookup_name_info
lookup_name (search_name
,
3659 symbol_name_match_type::FULL
, true);
3661 auto bounds
= index
.find_name_components_bounds (lookup_name
,
3665 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
3666 if (distance
!= expected_syms
.size ())
3669 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
3671 auto nc_elem
= bounds
.first
+ exp_elem
;
3672 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
3673 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
3680 /* Test the lower-level mapped_index::find_name_component_bounds
3684 test_mapped_index_find_name_component_bounds ()
3686 mock_mapped_index
mock_index (test_symbols
);
3688 mock_index
.build_name_components (NULL
/* per_objfile */);
3690 /* Test the lower-level mapped_index::find_name_component_bounds
3691 method in completion mode. */
3693 static const char *expected_syms
[] = {
3698 SELF_CHECK (check_find_bounds_finds
3699 (mock_index
, "t1_func", expected_syms
,
3700 NULL
/* per_objfile */));
3703 /* Check that the increment-last-char in the name matching algorithm
3704 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3706 static const char *expected_syms1
[] = {
3710 SELF_CHECK (check_find_bounds_finds
3711 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
3713 static const char *expected_syms2
[] = {
3716 SELF_CHECK (check_find_bounds_finds
3717 (mock_index
, "\377\377", expected_syms2
,
3718 NULL
/* per_objfile */));
3722 /* Test dw2_expand_symtabs_matching_symbol. */
3725 test_dw2_expand_symtabs_matching_symbol ()
3727 mock_mapped_index
mock_index (test_symbols
);
3729 /* We let all tests run until the end even if some fails, for debug
3731 bool any_mismatch
= false;
3733 /* Create the expected symbols list (an initializer_list). Needed
3734 because lists have commas, and we need to pass them to CHECK,
3735 which is a macro. */
3736 #define EXPECT(...) { __VA_ARGS__ }
3738 /* Wrapper for check_match that passes down the current
3739 __FILE__/__LINE__. */
3740 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3741 any_mismatch |= !check_match (__FILE__, __LINE__, \
3743 NAME, MATCH_TYPE, COMPLETION_MODE, \
3744 EXPECTED_LIST, NULL)
3746 /* Identity checks. */
3747 for (const char *sym
: test_symbols
)
3749 /* Should be able to match all existing symbols. */
3750 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
3753 /* Should be able to match all existing symbols with
3755 std::string with_params
= std::string (sym
) + "(int)";
3756 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3759 /* Should be able to match all existing symbols with
3760 parameters and qualifiers. */
3761 with_params
= std::string (sym
) + " ( int ) const";
3762 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3765 /* This should really find sym, but cp-name-parser.y doesn't
3766 know about lvalue/rvalue qualifiers yet. */
3767 with_params
= std::string (sym
) + " ( int ) &&";
3768 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3772 /* Check that the name matching algorithm for completion doesn't get
3773 confused with Latin1 'ÿ' / 0xff. */
3775 static const char str
[] = "\377";
3776 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3777 EXPECT ("\377", "\377\377123"));
3780 /* Check that the increment-last-char in the matching algorithm for
3781 completion doesn't match "t1_fund" when completing "t1_func". */
3783 static const char str
[] = "t1_func";
3784 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3785 EXPECT ("t1_func", "t1_func1"));
3788 /* Check that completion mode works at each prefix of the expected
3791 static const char str
[] = "function(int)";
3792 size_t len
= strlen (str
);
3795 for (size_t i
= 1; i
< len
; i
++)
3797 lookup
.assign (str
, i
);
3798 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3799 EXPECT ("function"));
3803 /* While "w" is a prefix of both components, the match function
3804 should still only be called once. */
3806 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
3808 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
3812 /* Same, with a "complicated" symbol. */
3814 static const char str
[] = Z_SYM_NAME
;
3815 size_t len
= strlen (str
);
3818 for (size_t i
= 1; i
< len
; i
++)
3820 lookup
.assign (str
, i
);
3821 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3822 EXPECT (Z_SYM_NAME
));
3826 /* In FULL mode, an incomplete symbol doesn't match. */
3828 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
3832 /* A complete symbol with parameters matches any overload, since the
3833 index has no overload info. */
3835 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
3836 EXPECT ("std::zfunction", "std::zfunction2"));
3837 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
3838 EXPECT ("std::zfunction", "std::zfunction2"));
3839 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
3840 EXPECT ("std::zfunction", "std::zfunction2"));
3843 /* Check that whitespace is ignored appropriately. A symbol with a
3844 template argument list. */
3846 static const char expected
[] = "ns::foo<int>";
3847 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
3849 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
3853 /* Check that whitespace is ignored appropriately. A symbol with a
3854 template argument list that includes a pointer. */
3856 static const char expected
[] = "ns::foo<char*>";
3857 /* Try both completion and non-completion modes. */
3858 static const bool completion_mode
[2] = {false, true};
3859 for (size_t i
= 0; i
< 2; i
++)
3861 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
3862 completion_mode
[i
], EXPECT (expected
));
3863 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
3864 completion_mode
[i
], EXPECT (expected
));
3866 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
3867 completion_mode
[i
], EXPECT (expected
));
3868 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
3869 completion_mode
[i
], EXPECT (expected
));
3874 /* Check method qualifiers are ignored. */
3875 static const char expected
[] = "ns::foo<char*>";
3876 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3877 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3878 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3879 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3880 CHECK_MATCH ("foo < char * > ( int ) const",
3881 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3882 CHECK_MATCH ("foo < char * > ( int ) &&",
3883 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3886 /* Test lookup names that don't match anything. */
3888 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
3891 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
3895 /* Some wild matching tests, exercising "(anonymous namespace)",
3896 which should not be confused with a parameter list. */
3898 static const char *syms
[] = {
3902 "A :: B :: C ( int )",
3907 for (const char *s
: syms
)
3909 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
3910 EXPECT ("(anonymous namespace)::A::B::C"));
3915 static const char expected
[] = "ns2::tmpl<int>::foo2";
3916 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
3918 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
3922 SELF_CHECK (!any_mismatch
);
3931 test_mapped_index_find_name_component_bounds ();
3932 test_dw2_expand_symtabs_matching_symbol ();
3935 }} // namespace selftests::dw2_expand_symtabs_matching
3937 #endif /* GDB_SELF_TEST */
3939 /* If FILE_MATCHER is NULL or if PER_CU has
3940 dwarf2_per_cu_quick_data::MARK set (see
3941 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3942 EXPANSION_NOTIFY on it. */
3945 dw2_expand_symtabs_matching_one
3946 (dwarf2_per_cu_data
*per_cu
,
3947 dwarf2_per_objfile
*per_objfile
,
3948 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3949 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
3951 if (file_matcher
== NULL
|| per_cu
->mark
)
3953 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
3955 compunit_symtab
*symtab
3956 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
3957 gdb_assert (symtab
!= nullptr);
3959 if (expansion_notify
!= NULL
&& symtab_was_null
)
3960 return expansion_notify (symtab
);
3965 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3966 matched, to expand corresponding CUs that were marked. IDX is the
3967 index of the symbol name that matched. */
3970 dw2_expand_marked_cus
3971 (dwarf2_per_objfile
*per_objfile
, offset_type idx
,
3972 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3973 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
3974 block_search_flags search_flags
,
3977 offset_type vec_len
, vec_idx
;
3978 bool global_seen
= false;
3980 = (static_cast<mapped_index
&>
3981 (*per_objfile
->per_bfd
->index_table
.get ()));
3983 offset_view
vec (index
.constant_pool
.slice (index
.symbol_vec_index (idx
)));
3985 for (vec_idx
= 0; vec_idx
< vec_len
; ++vec_idx
)
3987 offset_type cu_index_and_attrs
= vec
[vec_idx
+ 1];
3988 /* This value is only valid for index versions >= 7. */
3989 int is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
3990 gdb_index_symbol_kind symbol_kind
=
3991 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
3992 int cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
3993 /* Only check the symbol attributes if they're present.
3994 Indices prior to version 7 don't record them,
3995 and indices >= 7 may elide them for certain symbols
3996 (gold does this). */
3999 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
4001 /* Work around gold/15646. */
4004 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
4012 /* Only check the symbol's kind if it has one. */
4017 if ((search_flags
& SEARCH_STATIC_BLOCK
) == 0)
4022 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
4028 case VARIABLES_DOMAIN
:
4029 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
)
4032 case FUNCTIONS_DOMAIN
:
4033 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
)
4037 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
4040 case MODULES_DOMAIN
:
4041 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
4049 /* Don't crash on bad data. */
4050 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
4052 complaint (_(".gdb_index entry has bad CU index"
4053 " [in module %s]"), objfile_name (per_objfile
->objfile
));
4057 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
4058 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, file_matcher
,
4066 /* If FILE_MATCHER is non-NULL, set all the
4067 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4068 that match FILE_MATCHER. */
4071 dw_expand_symtabs_matching_file_matcher
4072 (dwarf2_per_objfile
*per_objfile
,
4073 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
4075 if (file_matcher
== NULL
)
4078 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
4080 NULL
, xcalloc
, xfree
));
4081 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
4083 NULL
, xcalloc
, xfree
));
4085 /* The rule is CUs specify all the files, including those used by
4086 any TU, so there's no need to scan TUs here. */
4088 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4092 if (per_cu
->is_debug_types
)
4096 /* We only need to look at symtabs not already expanded. */
4097 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4100 if (per_cu
->fnd
!= nullptr)
4102 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4104 if (file_matcher (fnd
->get_name (), false))
4110 /* Before we invoke realpath, which can get expensive when many
4111 files are involved, do a quick comparison of the basenames. */
4112 if ((basenames_may_differ
4113 || file_matcher (lbasename (fnd
->get_name ()), true))
4114 && file_matcher (fnd
->get_fullname (), false))
4121 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
4123 if (file_data
== NULL
)
4126 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
4128 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
4134 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4136 const char *this_real_name
;
4138 if (file_matcher (file_data
->file_names
[j
], false))
4144 /* Before we invoke realpath, which can get expensive when many
4145 files are involved, do a quick comparison of the basenames. */
4146 if (!basenames_may_differ
4147 && !file_matcher (lbasename (file_data
->file_names
[j
]),
4151 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
4152 if (file_matcher (this_real_name
, false))
4159 void **slot
= htab_find_slot (per_cu
->mark
4160 ? visited_found
.get ()
4161 : visited_not_found
.get (),
4168 dwarf2_gdb_index::expand_symtabs_matching
4169 (struct objfile
*objfile
,
4170 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
4171 const lookup_name_info
*lookup_name
,
4172 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
4173 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
4174 block_search_flags search_flags
,
4176 enum search_domain kind
)
4178 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4180 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
4182 /* This invariant is documented in quick-functions.h. */
4183 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
4184 if (lookup_name
== nullptr)
4186 for (dwarf2_per_cu_data
*per_cu
4187 : all_comp_units_range (per_objfile
->per_bfd
))
4191 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
4200 = (static_cast<mapped_index
&>
4201 (*per_objfile
->per_bfd
->index_table
.get ()));
4204 = dw2_expand_symtabs_matching_symbol (index
, *lookup_name
,
4206 [&] (offset_type idx
)
4208 if (!dw2_expand_marked_cus (per_objfile
, idx
, file_matcher
,
4209 expansion_notify
, search_flags
, kind
))
4217 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4220 static struct compunit_symtab
*
4221 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
4226 if (cust
->blockvector () != nullptr
4227 && blockvector_contains_pc (cust
->blockvector (), pc
))
4230 if (cust
->includes
== NULL
)
4233 for (i
= 0; cust
->includes
[i
]; ++i
)
4235 struct compunit_symtab
*s
= cust
->includes
[i
];
4237 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
4245 dwarf2_per_cu_data
*
4246 dwarf2_base_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
4247 CORE_ADDR adjusted_pc
)
4249 if (per_bfd
->index_addrmap
== nullptr)
4251 return (struct dwarf2_per_cu_data
*) addrmap_find (per_bfd
->index_addrmap
,
4255 struct compunit_symtab
*
4256 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4257 (struct objfile
*objfile
,
4258 struct bound_minimal_symbol msymbol
,
4260 struct obj_section
*section
,
4263 struct compunit_symtab
*result
;
4265 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4267 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
4268 struct dwarf2_per_cu_data
*data
= find_per_cu (per_objfile
->per_bfd
,
4270 if (data
== nullptr)
4273 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
4274 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4275 paddress (objfile
->arch (), pc
));
4277 result
= recursively_find_pc_sect_compunit_symtab
4278 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
4280 if (warn_if_readin
&& result
== nullptr)
4281 warning (_("(Error: pc %s in address map, but not in symtab.)"),
4282 paddress (objfile
->arch (), pc
));
4288 dwarf2_base_index_functions::map_symbol_filenames
4289 (struct objfile
*objfile
,
4290 gdb::function_view
<symbol_filename_ftype
> fun
,
4293 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4295 /* Use caches to ensure we only call FUN once for each filename. */
4296 filename_seen_cache filenames_cache
;
4297 std::unordered_set
<quick_file_names
*> qfn_cache
;
4299 /* The rule is CUs specify all the files, including those used by any TU,
4300 so there's no need to scan TUs here. We can ignore file names coming
4301 from already-expanded CUs. It is possible that an expanded CU might
4302 reuse the file names data from a currently unexpanded CU, in this
4303 case we don't want to report the files from the unexpanded CU. */
4305 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4307 if (!per_cu
->is_debug_types
4308 && per_objfile
->symtab_set_p (per_cu
.get ()))
4310 if (per_cu
->file_names
!= nullptr)
4311 qfn_cache
.insert (per_cu
->file_names
);
4315 for (dwarf2_per_cu_data
*per_cu
4316 : all_comp_units_range (per_objfile
->per_bfd
))
4318 /* We only need to look at symtabs not already expanded. */
4319 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
4322 if (per_cu
->fnd
!= nullptr)
4324 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4326 const char *filename
= fnd
->get_name ();
4327 const char *key
= filename
;
4328 const char *fullname
= nullptr;
4332 fullname
= fnd
->get_fullname ();
4336 if (!filenames_cache
.seen (key
))
4337 fun (filename
, fullname
);
4340 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
4341 if (file_data
== nullptr
4342 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
4345 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4347 const char *filename
= file_data
->file_names
[j
];
4348 const char *key
= filename
;
4349 const char *fullname
= nullptr;
4353 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
4357 if (!filenames_cache
.seen (key
))
4358 fun (filename
, fullname
);
4364 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
4369 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4372 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
4374 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4376 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4378 /* Is this already expanded? */
4379 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4382 /* It has not yet been expanded. */
4389 /* DWARF-5 debug_names reader. */
4391 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4392 static const gdb_byte dwarf5_augmentation
[] = { 'G', 'D', 'B', 0 };
4394 /* A helper function that reads the .debug_names section in SECTION
4395 and fills in MAP. FILENAME is the name of the file containing the
4396 section; it is used for error reporting.
4398 Returns true if all went well, false otherwise. */
4401 read_debug_names_from_section (struct objfile
*objfile
,
4402 const char *filename
,
4403 struct dwarf2_section_info
*section
,
4404 mapped_debug_names
&map
)
4406 if (section
->empty ())
4409 /* Older elfutils strip versions could keep the section in the main
4410 executable while splitting it for the separate debug info file. */
4411 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
4414 section
->read (objfile
);
4416 map
.dwarf5_byte_order
= gdbarch_byte_order (objfile
->arch ());
4418 const gdb_byte
*addr
= section
->buffer
;
4420 bfd
*const abfd
= section
->get_bfd_owner ();
4422 unsigned int bytes_read
;
4423 LONGEST length
= read_initial_length (abfd
, addr
, &bytes_read
);
4426 map
.dwarf5_is_dwarf64
= bytes_read
!= 4;
4427 map
.offset_size
= map
.dwarf5_is_dwarf64
? 8 : 4;
4428 if (bytes_read
+ length
!= section
->size
)
4430 /* There may be multiple per-CU indices. */
4431 warning (_("Section .debug_names in %s length %s does not match "
4432 "section length %s, ignoring .debug_names."),
4433 filename
, plongest (bytes_read
+ length
),
4434 pulongest (section
->size
));
4438 /* The version number. */
4439 uint16_t version
= read_2_bytes (abfd
, addr
);
4443 warning (_("Section .debug_names in %s has unsupported version %d, "
4444 "ignoring .debug_names."),
4450 uint16_t padding
= read_2_bytes (abfd
, addr
);
4454 warning (_("Section .debug_names in %s has unsupported padding %d, "
4455 "ignoring .debug_names."),
4460 /* comp_unit_count - The number of CUs in the CU list. */
4461 map
.cu_count
= read_4_bytes (abfd
, addr
);
4464 /* local_type_unit_count - The number of TUs in the local TU
4466 map
.tu_count
= read_4_bytes (abfd
, addr
);
4469 /* foreign_type_unit_count - The number of TUs in the foreign TU
4471 uint32_t foreign_tu_count
= read_4_bytes (abfd
, addr
);
4473 if (foreign_tu_count
!= 0)
4475 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4476 "ignoring .debug_names."),
4477 filename
, static_cast<unsigned long> (foreign_tu_count
));
4481 /* bucket_count - The number of hash buckets in the hash lookup
4483 map
.bucket_count
= read_4_bytes (abfd
, addr
);
4486 /* name_count - The number of unique names in the index. */
4487 map
.name_count
= read_4_bytes (abfd
, addr
);
4490 /* abbrev_table_size - The size in bytes of the abbreviations
4492 uint32_t abbrev_table_size
= read_4_bytes (abfd
, addr
);
4495 /* augmentation_string_size - The size in bytes of the augmentation
4496 string. This value is rounded up to a multiple of 4. */
4497 uint32_t augmentation_string_size
= read_4_bytes (abfd
, addr
);
4499 map
.augmentation_is_gdb
= ((augmentation_string_size
4500 == sizeof (dwarf5_augmentation
))
4501 && memcmp (addr
, dwarf5_augmentation
,
4502 sizeof (dwarf5_augmentation
)) == 0);
4503 augmentation_string_size
+= (-augmentation_string_size
) & 3;
4504 addr
+= augmentation_string_size
;
4507 map
.cu_table_reordered
= addr
;
4508 addr
+= map
.cu_count
* map
.offset_size
;
4510 /* List of Local TUs */
4511 map
.tu_table_reordered
= addr
;
4512 addr
+= map
.tu_count
* map
.offset_size
;
4514 /* Hash Lookup Table */
4515 map
.bucket_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4516 addr
+= map
.bucket_count
* 4;
4517 map
.hash_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4518 addr
+= map
.name_count
* 4;
4521 map
.name_table_string_offs_reordered
= addr
;
4522 addr
+= map
.name_count
* map
.offset_size
;
4523 map
.name_table_entry_offs_reordered
= addr
;
4524 addr
+= map
.name_count
* map
.offset_size
;
4526 const gdb_byte
*abbrev_table_start
= addr
;
4529 const ULONGEST index_num
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4534 const auto insertpair
4535 = map
.abbrev_map
.emplace (index_num
, mapped_debug_names::index_val ());
4536 if (!insertpair
.second
)
4538 warning (_("Section .debug_names in %s has duplicate index %s, "
4539 "ignoring .debug_names."),
4540 filename
, pulongest (index_num
));
4543 mapped_debug_names::index_val
&indexval
= insertpair
.first
->second
;
4544 indexval
.dwarf_tag
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4549 mapped_debug_names::index_val::attr attr
;
4550 attr
.dw_idx
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4552 attr
.form
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4554 if (attr
.form
== DW_FORM_implicit_const
)
4556 attr
.implicit_const
= read_signed_leb128 (abfd
, addr
,
4560 if (attr
.dw_idx
== 0 && attr
.form
== 0)
4562 indexval
.attr_vec
.push_back (std::move (attr
));
4565 if (addr
!= abbrev_table_start
+ abbrev_table_size
)
4567 warning (_("Section .debug_names in %s has abbreviation_table "
4568 "of size %s vs. written as %u, ignoring .debug_names."),
4569 filename
, plongest (addr
- abbrev_table_start
),
4573 map
.entry_pool
= addr
;
4578 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4582 create_cus_from_debug_names_list (dwarf2_per_bfd
*per_bfd
,
4583 const mapped_debug_names
&map
,
4584 dwarf2_section_info
§ion
,
4587 if (!map
.augmentation_is_gdb
)
4589 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
4591 sect_offset sect_off
4592 = (sect_offset
) (extract_unsigned_integer
4593 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4595 map
.dwarf5_byte_order
));
4596 /* We don't know the length of the CU, because the CU list in a
4597 .debug_names index can be incomplete, so we can't use the start
4598 of the next CU as end of this CU. We create the CUs here with
4599 length 0, and in cutu_reader::cutu_reader we'll fill in the
4601 dwarf2_per_cu_data_up per_cu
4602 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4604 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4609 sect_offset sect_off_prev
;
4610 for (uint32_t i
= 0; i
<= map
.cu_count
; ++i
)
4612 sect_offset sect_off_next
;
4613 if (i
< map
.cu_count
)
4616 = (sect_offset
) (extract_unsigned_integer
4617 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4619 map
.dwarf5_byte_order
));
4622 sect_off_next
= (sect_offset
) section
.size
;
4625 const ULONGEST length
= sect_off_next
- sect_off_prev
;
4626 dwarf2_per_cu_data_up per_cu
4627 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4628 sect_off_prev
, length
);
4629 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4631 sect_off_prev
= sect_off_next
;
4635 /* Read the CU list from the mapped index, and use it to create all
4636 the CU objects for this dwarf2_per_objfile. */
4639 create_cus_from_debug_names (dwarf2_per_bfd
*per_bfd
,
4640 const mapped_debug_names
&map
,
4641 const mapped_debug_names
&dwz_map
)
4643 gdb_assert (per_bfd
->all_comp_units
.empty ());
4644 per_bfd
->all_comp_units
.reserve (map
.cu_count
+ dwz_map
.cu_count
);
4646 create_cus_from_debug_names_list (per_bfd
, map
, per_bfd
->info
,
4647 false /* is_dwz */);
4649 if (dwz_map
.cu_count
== 0)
4652 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4653 create_cus_from_debug_names_list (per_bfd
, dwz_map
, dwz
->info
,
4657 /* Read .debug_names. If everything went ok, initialize the "quick"
4658 elements of all the CUs and return true. Otherwise, return false. */
4661 dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
4663 std::unique_ptr
<mapped_debug_names
> map (new mapped_debug_names
);
4664 mapped_debug_names dwz_map
;
4665 struct objfile
*objfile
= per_objfile
->objfile
;
4666 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4668 if (!read_debug_names_from_section (objfile
, objfile_name (objfile
),
4669 &per_bfd
->debug_names
, *map
))
4672 /* Don't use the index if it's empty. */
4673 if (map
->name_count
== 0)
4676 /* If there is a .dwz file, read it so we can get its CU list as
4678 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4681 if (!read_debug_names_from_section (objfile
,
4682 bfd_get_filename (dwz
->dwz_bfd
.get ()),
4683 &dwz
->debug_names
, dwz_map
))
4685 warning (_("could not read '.debug_names' section from %s; skipping"),
4686 bfd_get_filename (dwz
->dwz_bfd
.get ()));
4691 create_cus_from_debug_names (per_bfd
, *map
, dwz_map
);
4693 if (map
->tu_count
!= 0)
4695 /* We can only handle a single .debug_types when we have an
4697 if (per_bfd
->types
.size () != 1)
4700 dwarf2_section_info
*section
= &per_bfd
->types
[0];
4702 create_signatured_type_table_from_debug_names
4703 (per_objfile
, *map
, section
, &per_bfd
->abbrev
);
4706 create_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
);
4708 per_bfd
->index_table
= std::move (map
);
4709 per_bfd
->quick_file_names_table
=
4710 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
4715 /* Type used to manage iterating over all CUs looking for a symbol for
4718 class dw2_debug_names_iterator
4721 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4722 block_search_flags block_index
,
4724 const char *name
, dwarf2_per_objfile
*per_objfile
)
4725 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4726 m_addr (find_vec_in_debug_names (map
, name
, per_objfile
)),
4727 m_per_objfile (per_objfile
)
4730 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4731 search_domain search
, uint32_t namei
,
4732 dwarf2_per_objfile
*per_objfile
,
4733 domain_enum domain
= UNDEF_DOMAIN
)
4737 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4738 m_per_objfile (per_objfile
)
4741 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4742 block_search_flags block_index
, domain_enum domain
,
4743 uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4744 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4745 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4746 m_per_objfile (per_objfile
)
4749 /* Return the next matching CU or NULL if there are no more. */
4750 dwarf2_per_cu_data
*next ();
4753 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4755 dwarf2_per_objfile
*per_objfile
);
4756 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4758 dwarf2_per_objfile
*per_objfile
);
4760 /* The internalized form of .debug_names. */
4761 const mapped_debug_names
&m_map
;
4763 /* Restrict the search to these blocks. */
4764 block_search_flags m_block_index
= (SEARCH_GLOBAL_BLOCK
4765 | SEARCH_STATIC_BLOCK
);
4767 /* The kind of symbol we're looking for. */
4768 const domain_enum m_domain
= UNDEF_DOMAIN
;
4769 const search_domain m_search
= ALL_DOMAIN
;
4771 /* The list of CUs from the index entry of the symbol, or NULL if
4773 const gdb_byte
*m_addr
;
4775 dwarf2_per_objfile
*m_per_objfile
;
4779 mapped_debug_names::namei_to_name
4780 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const
4782 const ULONGEST namei_string_offs
4783 = extract_unsigned_integer ((name_table_string_offs_reordered
4784 + namei
* offset_size
),
4787 return read_indirect_string_at_offset (per_objfile
, namei_string_offs
);
4790 /* Find a slot in .debug_names for the object named NAME. If NAME is
4791 found, return pointer to its pool data. If NAME cannot be found,
4795 dw2_debug_names_iterator::find_vec_in_debug_names
4796 (const mapped_debug_names
&map
, const char *name
,
4797 dwarf2_per_objfile
*per_objfile
)
4799 int (*cmp
) (const char *, const char *);
4801 gdb::unique_xmalloc_ptr
<char> without_params
;
4802 if (current_language
->la_language
== language_cplus
4803 || current_language
->la_language
== language_fortran
4804 || current_language
->la_language
== language_d
)
4806 /* NAME is already canonical. Drop any qualifiers as
4807 .debug_names does not contain any. */
4809 if (strchr (name
, '(') != NULL
)
4811 without_params
= cp_remove_params (name
);
4812 if (without_params
!= NULL
)
4813 name
= without_params
.get ();
4817 cmp
= (case_sensitivity
== case_sensitive_on
? strcmp
: strcasecmp
);
4819 const uint32_t full_hash
= dwarf5_djb_hash (name
);
4821 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4822 (map
.bucket_table_reordered
4823 + (full_hash
% map
.bucket_count
)), 4,
4824 map
.dwarf5_byte_order
);
4828 if (namei
>= map
.name_count
)
4830 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4832 namei
, map
.name_count
,
4833 objfile_name (per_objfile
->objfile
));
4839 const uint32_t namei_full_hash
4840 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4841 (map
.hash_table_reordered
+ namei
), 4,
4842 map
.dwarf5_byte_order
);
4843 if (full_hash
% map
.bucket_count
!= namei_full_hash
% map
.bucket_count
)
4846 if (full_hash
== namei_full_hash
)
4848 const char *const namei_string
= map
.namei_to_name (namei
, per_objfile
);
4850 #if 0 /* An expensive sanity check. */
4851 if (namei_full_hash
!= dwarf5_djb_hash (namei_string
))
4853 complaint (_("Wrong .debug_names hash for string at index %u "
4855 namei
, objfile_name (dwarf2_per_objfile
->objfile
));
4860 if (cmp (namei_string
, name
) == 0)
4862 const ULONGEST namei_entry_offs
4863 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4864 + namei
* map
.offset_size
),
4865 map
.offset_size
, map
.dwarf5_byte_order
);
4866 return map
.entry_pool
+ namei_entry_offs
;
4871 if (namei
>= map
.name_count
)
4877 dw2_debug_names_iterator::find_vec_in_debug_names
4878 (const mapped_debug_names
&map
, uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4880 if (namei
>= map
.name_count
)
4882 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4884 namei
, map
.name_count
,
4885 objfile_name (per_objfile
->objfile
));
4889 const ULONGEST namei_entry_offs
4890 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4891 + namei
* map
.offset_size
),
4892 map
.offset_size
, map
.dwarf5_byte_order
);
4893 return map
.entry_pool
+ namei_entry_offs
;
4896 /* See dw2_debug_names_iterator. */
4898 dwarf2_per_cu_data
*
4899 dw2_debug_names_iterator::next ()
4904 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4905 struct objfile
*objfile
= m_per_objfile
->objfile
;
4906 bfd
*const abfd
= objfile
->obfd
;
4910 unsigned int bytes_read
;
4911 const ULONGEST abbrev
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4912 m_addr
+= bytes_read
;
4916 const auto indexval_it
= m_map
.abbrev_map
.find (abbrev
);
4917 if (indexval_it
== m_map
.abbrev_map
.cend ())
4919 complaint (_("Wrong .debug_names undefined abbrev code %s "
4921 pulongest (abbrev
), objfile_name (objfile
));
4924 const mapped_debug_names::index_val
&indexval
= indexval_it
->second
;
4925 enum class symbol_linkage
{
4929 } symbol_linkage_
= symbol_linkage::unknown
;
4930 dwarf2_per_cu_data
*per_cu
= NULL
;
4931 for (const mapped_debug_names::index_val::attr
&attr
: indexval
.attr_vec
)
4936 case DW_FORM_implicit_const
:
4937 ull
= attr
.implicit_const
;
4939 case DW_FORM_flag_present
:
4943 ull
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4944 m_addr
+= bytes_read
;
4947 ull
= read_4_bytes (abfd
, m_addr
);
4951 ull
= read_8_bytes (abfd
, m_addr
);
4954 case DW_FORM_ref_sig8
:
4955 ull
= read_8_bytes (abfd
, m_addr
);
4959 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4960 dwarf_form_name (attr
.form
),
4961 objfile_name (objfile
));
4964 switch (attr
.dw_idx
)
4966 case DW_IDX_compile_unit
:
4967 /* Don't crash on bad data. */
4968 if (ull
>= per_bfd
->all_comp_units
.size ())
4970 complaint (_(".debug_names entry has bad CU index %s"
4973 objfile_name (objfile
));
4976 per_cu
= per_bfd
->get_cu (ull
);
4978 case DW_IDX_type_unit
:
4979 /* Don't crash on bad data. */
4980 if (ull
>= per_bfd
->tu_stats
.nr_tus
)
4982 complaint (_(".debug_names entry has bad TU index %s"
4985 objfile_name (objfile
));
4988 per_cu
= per_bfd
->get_cu (ull
+ per_bfd
->tu_stats
.nr_tus
);
4990 case DW_IDX_die_offset
:
4991 /* In a per-CU index (as opposed to a per-module index), index
4992 entries without CU attribute implicitly refer to the single CU. */
4994 per_cu
= per_bfd
->get_cu (0);
4996 case DW_IDX_GNU_internal
:
4997 if (!m_map
.augmentation_is_gdb
)
4999 symbol_linkage_
= symbol_linkage::static_
;
5001 case DW_IDX_GNU_external
:
5002 if (!m_map
.augmentation_is_gdb
)
5004 symbol_linkage_
= symbol_linkage::extern_
;
5009 /* Skip if already read in. */
5010 if (m_per_objfile
->symtab_set_p (per_cu
))
5013 /* Check static vs global. */
5014 if (symbol_linkage_
!= symbol_linkage::unknown
)
5016 if (symbol_linkage_
== symbol_linkage::static_
)
5018 if ((m_block_index
& SEARCH_STATIC_BLOCK
) == 0)
5023 if ((m_block_index
& SEARCH_GLOBAL_BLOCK
) == 0)
5028 /* Match dw2_symtab_iter_next, symbol_kind
5029 and debug_names::psymbol_tag. */
5033 switch (indexval
.dwarf_tag
)
5035 case DW_TAG_variable
:
5036 case DW_TAG_subprogram
:
5037 /* Some types are also in VAR_DOMAIN. */
5038 case DW_TAG_typedef
:
5039 case DW_TAG_structure_type
:
5046 switch (indexval
.dwarf_tag
)
5048 case DW_TAG_typedef
:
5049 case DW_TAG_structure_type
:
5056 switch (indexval
.dwarf_tag
)
5059 case DW_TAG_variable
:
5066 switch (indexval
.dwarf_tag
)
5078 /* Match dw2_expand_symtabs_matching, symbol_kind and
5079 debug_names::psymbol_tag. */
5082 case VARIABLES_DOMAIN
:
5083 switch (indexval
.dwarf_tag
)
5085 case DW_TAG_variable
:
5091 case FUNCTIONS_DOMAIN
:
5092 switch (indexval
.dwarf_tag
)
5094 case DW_TAG_subprogram
:
5101 switch (indexval
.dwarf_tag
)
5103 case DW_TAG_typedef
:
5104 case DW_TAG_structure_type
:
5110 case MODULES_DOMAIN
:
5111 switch (indexval
.dwarf_tag
)
5125 /* This dumps minimal information about .debug_names. It is called
5126 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5127 uses this to verify that .debug_names has been loaded. */
5130 dwarf2_debug_names_index::dump (struct objfile
*objfile
)
5132 gdb_printf (".debug_names: exists\n");
5136 dwarf2_debug_names_index::expand_matching_symbols
5137 (struct objfile
*objfile
,
5138 const lookup_name_info
&name
, domain_enum domain
,
5140 symbol_compare_ftype
*ordered_compare
)
5142 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5144 mapped_debug_names
&map
5145 = (static_cast<mapped_debug_names
&>
5146 (*per_objfile
->per_bfd
->index_table
.get ()));
5147 const block_search_flags block_flags
5148 = global
? SEARCH_GLOBAL_BLOCK
: SEARCH_STATIC_BLOCK
;
5150 const char *match_name
= name
.ada ().lookup_name ().c_str ();
5151 auto matcher
= [&] (const char *symname
)
5153 if (ordered_compare
== nullptr)
5155 return ordered_compare (symname
, match_name
) == 0;
5158 dw2_expand_symtabs_matching_symbol (map
, name
, matcher
,
5159 [&] (offset_type namei
)
5161 /* The name was matched, now expand corresponding CUs that were
5163 dw2_debug_names_iterator
iter (map
, block_flags
, domain
, namei
,
5166 struct dwarf2_per_cu_data
*per_cu
;
5167 while ((per_cu
= iter
.next ()) != NULL
)
5168 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
5175 dwarf2_debug_names_index::expand_symtabs_matching
5176 (struct objfile
*objfile
,
5177 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
5178 const lookup_name_info
*lookup_name
,
5179 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
5180 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
5181 block_search_flags search_flags
,
5183 enum search_domain kind
)
5185 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5187 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
5189 /* This invariant is documented in quick-functions.h. */
5190 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
5191 if (lookup_name
== nullptr)
5193 for (dwarf2_per_cu_data
*per_cu
5194 : all_comp_units_range (per_objfile
->per_bfd
))
5198 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5206 mapped_debug_names
&map
5207 = (static_cast<mapped_debug_names
&>
5208 (*per_objfile
->per_bfd
->index_table
.get ()));
5211 = dw2_expand_symtabs_matching_symbol (map
, *lookup_name
,
5213 [&] (offset_type namei
)
5215 /* The name was matched, now expand corresponding CUs that were
5217 dw2_debug_names_iterator
iter (map
, kind
, namei
, per_objfile
, domain
);
5219 struct dwarf2_per_cu_data
*per_cu
;
5220 while ((per_cu
= iter
.next ()) != NULL
)
5221 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5231 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5232 to either a dwarf2_per_bfd or dwz_file object. */
5234 template <typename T
>
5235 static gdb::array_view
<const gdb_byte
>
5236 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
5238 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
5240 if (section
->empty ())
5243 /* Older elfutils strip versions could keep the section in the main
5244 executable while splitting it for the separate debug info file. */
5245 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
5248 section
->read (obj
);
5250 /* dwarf2_section_info::size is a bfd_size_type, while
5251 gdb::array_view works with size_t. On 32-bit hosts, with
5252 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5253 is 32-bit. So we need an explicit narrowing conversion here.
5254 This is fine, because it's impossible to allocate or mmap an
5255 array/buffer larger than what size_t can represent. */
5256 return gdb::make_array_view (section
->buffer
, section
->size
);
5259 /* Lookup the index cache for the contents of the index associated to
5262 static gdb::array_view
<const gdb_byte
>
5263 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
5265 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
5266 if (build_id
== nullptr)
5269 return global_index_cache
.lookup_gdb_index (build_id
,
5270 &dwarf2_per_bfd
->index_cache_res
);
5273 /* Same as the above, but for DWZ. */
5275 static gdb::array_view
<const gdb_byte
>
5276 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
5278 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
5279 if (build_id
== nullptr)
5282 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
5285 static quick_symbol_functions_up
make_cooked_index_funcs ();
5287 /* See dwarf2/public.h. */
5290 dwarf2_initialize_objfile (struct objfile
*objfile
)
5292 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5293 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5295 dwarf_read_debug_printf ("called");
5297 /* If we're about to read full symbols, don't bother with the
5298 indices. In this case we also don't care if some other debug
5299 format is making psymtabs, because they are all about to be
5301 if ((objfile
->flags
& OBJF_READNOW
))
5303 dwarf_read_debug_printf ("readnow requested");
5305 create_all_comp_units (per_objfile
);
5306 per_bfd
->quick_file_names_table
5307 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
5309 objfile
->qf
.emplace_front (new readnow_functions
);
5313 /* Was a GDB index already read when we processed an objfile sharing
5315 if (per_bfd
->index_table
!= nullptr)
5317 dwarf_read_debug_printf ("re-using symbols");
5318 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5322 if (dwarf2_read_debug_names (per_objfile
))
5324 dwarf_read_debug_printf ("found debug names");
5325 objfile
->qf
.push_front
5326 (per_bfd
->index_table
->make_quick_functions ());
5330 if (dwarf2_read_gdb_index (per_objfile
,
5331 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
5332 get_gdb_index_contents_from_section
<dwz_file
>))
5334 dwarf_read_debug_printf ("found gdb index from file");
5335 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5339 /* ... otherwise, try to find the index in the index cache. */
5340 if (dwarf2_read_gdb_index (per_objfile
,
5341 get_gdb_index_contents_from_cache
,
5342 get_gdb_index_contents_from_cache_dwz
))
5344 dwarf_read_debug_printf ("found gdb index from cache");
5345 global_index_cache
.hit ();
5346 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5350 global_index_cache
.miss ();
5351 objfile
->qf
.push_front (make_cooked_index_funcs ());
5356 /* Build a partial symbol table. */
5359 dwarf2_build_psymtabs (struct objfile
*objfile
)
5361 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5363 if (per_objfile
->per_bfd
->index_table
!= nullptr)
5368 dwarf2_build_psymtabs_hard (per_objfile
);
5370 /* (maybe) store an index in the cache. */
5371 global_index_cache
.store (per_objfile
);
5373 catch (const gdb_exception_error
&except
)
5375 exception_print (gdb_stderr
, except
);
5379 /* Find the base address of the compilation unit for range lists and
5380 location lists. It will normally be specified by DW_AT_low_pc.
5381 In DWARF-3 draft 4, the base address could be overridden by
5382 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5383 compilation units with discontinuous ranges. */
5386 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
5388 struct attribute
*attr
;
5390 cu
->base_address
.reset ();
5392 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
5393 if (attr
!= nullptr)
5394 cu
->base_address
= attr
->as_address ();
5397 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
5398 if (attr
!= nullptr)
5399 cu
->base_address
= attr
->as_address ();
5403 /* Helper function that returns the proper abbrev section for
5406 static struct dwarf2_section_info
*
5407 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
5409 struct dwarf2_section_info
*abbrev
;
5410 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
5412 if (this_cu
->is_dwz
)
5413 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
5415 abbrev
= &per_bfd
->abbrev
;
5420 /* Fetch the abbreviation table offset from a comp or type unit header. */
5423 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
5424 struct dwarf2_section_info
*section
,
5425 sect_offset sect_off
)
5427 bfd
*abfd
= section
->get_bfd_owner ();
5428 const gdb_byte
*info_ptr
;
5429 unsigned int initial_length_size
, offset_size
;
5432 section
->read (per_objfile
->objfile
);
5433 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
5434 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
5435 offset_size
= initial_length_size
== 4 ? 4 : 8;
5436 info_ptr
+= initial_length_size
;
5438 version
= read_2_bytes (abfd
, info_ptr
);
5442 /* Skip unit type and address size. */
5446 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
5450 hash_signatured_type (const void *item
)
5452 const struct signatured_type
*sig_type
5453 = (const struct signatured_type
*) item
;
5455 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5456 return sig_type
->signature
;
5460 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
5462 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
5463 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
5465 return lhs
->signature
== rhs
->signature
;
5468 /* Allocate a hash table for signatured types. */
5471 allocate_signatured_type_table ()
5473 return htab_up (htab_create_alloc (41,
5474 hash_signatured_type
,
5476 NULL
, xcalloc
, xfree
));
5479 /* A helper for create_debug_types_hash_table. Read types from SECTION
5480 and fill them into TYPES_HTAB. It will process only type units,
5481 therefore DW_UT_type. */
5484 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
5485 struct dwo_file
*dwo_file
,
5486 dwarf2_section_info
*section
, htab_up
&types_htab
,
5487 rcuh_kind section_kind
)
5489 struct objfile
*objfile
= per_objfile
->objfile
;
5490 struct dwarf2_section_info
*abbrev_section
;
5492 const gdb_byte
*info_ptr
, *end_ptr
;
5494 abbrev_section
= &dwo_file
->sections
.abbrev
;
5496 dwarf_read_debug_printf ("Reading %s for %s",
5497 section
->get_name (),
5498 abbrev_section
->get_file_name ());
5500 section
->read (objfile
);
5501 info_ptr
= section
->buffer
;
5503 if (info_ptr
== NULL
)
5506 /* We can't set abfd until now because the section may be empty or
5507 not present, in which case the bfd is unknown. */
5508 abfd
= section
->get_bfd_owner ();
5510 /* We don't use cutu_reader here because we don't need to read
5511 any dies: the signature is in the header. */
5513 end_ptr
= info_ptr
+ section
->size
;
5514 while (info_ptr
< end_ptr
)
5516 signatured_type_up sig_type
;
5517 struct dwo_unit
*dwo_tu
;
5519 const gdb_byte
*ptr
= info_ptr
;
5520 struct comp_unit_head header
;
5521 unsigned int length
;
5523 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
5525 /* Initialize it due to a false compiler warning. */
5526 header
.signature
= -1;
5527 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
5529 /* We need to read the type's signature in order to build the hash
5530 table, but we don't need anything else just yet. */
5532 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
5533 abbrev_section
, ptr
, section_kind
);
5535 length
= header
.get_length ();
5537 /* Skip dummy type units. */
5538 if (ptr
>= info_ptr
+ length
5539 || peek_abbrev_code (abfd
, ptr
) == 0
5540 || (header
.unit_type
!= DW_UT_type
5541 && header
.unit_type
!= DW_UT_split_type
))
5547 if (types_htab
== NULL
)
5548 types_htab
= allocate_dwo_unit_table ();
5550 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
5551 dwo_tu
->dwo_file
= dwo_file
;
5552 dwo_tu
->signature
= header
.signature
;
5553 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
5554 dwo_tu
->section
= section
;
5555 dwo_tu
->sect_off
= sect_off
;
5556 dwo_tu
->length
= length
;
5558 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
5559 gdb_assert (slot
!= NULL
);
5561 complaint (_("debug type entry at offset %s is duplicate to"
5562 " the entry at offset %s, signature %s"),
5563 sect_offset_str (sect_off
),
5564 sect_offset_str (dwo_tu
->sect_off
),
5565 hex_string (header
.signature
));
5568 dwarf_read_debug_printf_v (" offset %s, signature %s",
5569 sect_offset_str (sect_off
),
5570 hex_string (header
.signature
));
5576 /* Create the hash table of all entries in the .debug_types
5577 (or .debug_types.dwo) section(s).
5578 DWO_FILE is a pointer to the DWO file object.
5580 The result is a pointer to the hash table or NULL if there are no types.
5582 Note: This function processes DWO files only, not DWP files. */
5585 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
5586 struct dwo_file
*dwo_file
,
5587 gdb::array_view
<dwarf2_section_info
> type_sections
,
5588 htab_up
&types_htab
)
5590 for (dwarf2_section_info
§ion
: type_sections
)
5591 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
5595 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5596 If SLOT is non-NULL, it is the entry to use in the hash table.
5597 Otherwise we find one. */
5599 static struct signatured_type
*
5600 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
5602 if (per_objfile
->per_bfd
->all_comp_units
.size ()
5603 == per_objfile
->per_bfd
->all_comp_units
.capacity ())
5604 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
5606 signatured_type_up sig_type_holder
5607 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
5608 signatured_type
*sig_type
= sig_type_holder
.get ();
5610 per_objfile
->per_bfd
->all_comp_units
.emplace_back
5611 (sig_type_holder
.release ());
5615 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5618 gdb_assert (*slot
== NULL
);
5620 /* The rest of sig_type must be filled in by the caller. */
5624 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5625 Fill in SIG_ENTRY with DWO_ENTRY. */
5628 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
5629 struct signatured_type
*sig_entry
,
5630 struct dwo_unit
*dwo_entry
)
5632 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5634 /* Make sure we're not clobbering something we don't expect to. */
5635 gdb_assert (! sig_entry
->queued
);
5636 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
5637 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
5638 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
5639 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
5640 || (to_underlying (sig_entry
->type_offset_in_section
)
5641 == to_underlying (dwo_entry
->type_offset_in_tu
)));
5642 gdb_assert (sig_entry
->type_unit_group
== NULL
);
5643 gdb_assert (sig_entry
->dwo_unit
== NULL
5644 || sig_entry
->dwo_unit
== dwo_entry
);
5646 sig_entry
->section
= dwo_entry
->section
;
5647 sig_entry
->sect_off
= dwo_entry
->sect_off
;
5648 sig_entry
->length
= dwo_entry
->length
;
5649 sig_entry
->reading_dwo_directly
= 1;
5650 sig_entry
->per_bfd
= per_bfd
;
5651 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
5652 sig_entry
->dwo_unit
= dwo_entry
;
5655 /* Subroutine of lookup_signatured_type.
5656 If we haven't read the TU yet, create the signatured_type data structure
5657 for a TU to be read in directly from a DWO file, bypassing the stub.
5658 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5659 using .gdb_index, then when reading a CU we want to stay in the DWO file
5660 containing that CU. Otherwise we could end up reading several other DWO
5661 files (due to comdat folding) to process the transitive closure of all the
5662 mentioned TUs, and that can be slow. The current DWO file will have every
5663 type signature that it needs.
5664 We only do this for .gdb_index because in the psymtab case we already have
5665 to read all the DWOs to build the type unit groups. */
5667 static struct signatured_type
*
5668 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5670 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5671 struct dwo_file
*dwo_file
;
5672 struct dwo_unit find_dwo_entry
, *dwo_entry
;
5675 gdb_assert (cu
->dwo_unit
);
5677 /* If TU skeletons have been removed then we may not have read in any
5679 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5680 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5682 /* We only ever need to read in one copy of a signatured type.
5683 Use the global signatured_types array to do our own comdat-folding
5684 of types. If this is the first time we're reading this TU, and
5685 the TU has an entry in .gdb_index, replace the recorded data from
5686 .gdb_index with this TU. */
5688 signatured_type
find_sig_entry (sig
);
5689 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5690 &find_sig_entry
, INSERT
);
5691 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5693 /* We can get here with the TU already read, *or* in the process of being
5694 read. Don't reassign the global entry to point to this DWO if that's
5695 the case. Also note that if the TU is already being read, it may not
5696 have come from a DWO, the program may be a mix of Fission-compiled
5697 code and non-Fission-compiled code. */
5699 /* Have we already tried to read this TU?
5700 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5701 needn't exist in the global table yet). */
5702 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
5705 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5706 dwo_unit of the TU itself. */
5707 dwo_file
= cu
->dwo_unit
->dwo_file
;
5709 /* Ok, this is the first time we're reading this TU. */
5710 if (dwo_file
->tus
== NULL
)
5712 find_dwo_entry
.signature
= sig
;
5713 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
5715 if (dwo_entry
== NULL
)
5718 /* If the global table doesn't have an entry for this TU, add one. */
5719 if (sig_entry
== NULL
)
5720 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5722 if (sig_entry
->dwo_unit
== nullptr)
5723 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5724 sig_entry
->tu_read
= 1;
5728 /* Subroutine of lookup_signatured_type.
5729 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5730 then try the DWP file. If the TU stub (skeleton) has been removed then
5731 it won't be in .gdb_index. */
5733 static struct signatured_type
*
5734 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5736 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5737 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
5738 struct dwo_unit
*dwo_entry
;
5741 gdb_assert (cu
->dwo_unit
);
5742 gdb_assert (dwp_file
!= NULL
);
5744 /* If TU skeletons have been removed then we may not have read in any
5746 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5747 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5749 signatured_type
find_sig_entry (sig
);
5750 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5751 &find_sig_entry
, INSERT
);
5752 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5754 /* Have we already tried to read this TU?
5755 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5756 needn't exist in the global table yet). */
5757 if (sig_entry
!= NULL
)
5760 if (dwp_file
->tus
== NULL
)
5762 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
5763 1 /* is_debug_types */);
5764 if (dwo_entry
== NULL
)
5767 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5768 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5773 /* Lookup a signature based type for DW_FORM_ref_sig8.
5774 Returns NULL if signature SIG is not present in the table.
5775 It is up to the caller to complain about this. */
5777 static struct signatured_type
*
5778 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5780 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5784 /* We're in a DWO/DWP file, and we're using .gdb_index.
5785 These cases require special processing. */
5786 if (get_dwp_file (per_objfile
) == NULL
)
5787 return lookup_dwo_signatured_type (cu
, sig
);
5789 return lookup_dwp_signatured_type (cu
, sig
);
5793 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5795 signatured_type
find_entry (sig
);
5796 return ((struct signatured_type
*)
5797 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
5802 /* Low level DIE reading support. */
5804 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5807 init_cu_die_reader (struct die_reader_specs
*reader
,
5808 struct dwarf2_cu
*cu
,
5809 struct dwarf2_section_info
*section
,
5810 struct dwo_file
*dwo_file
,
5811 struct abbrev_table
*abbrev_table
)
5813 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
5814 reader
->abfd
= section
->get_bfd_owner ();
5816 reader
->dwo_file
= dwo_file
;
5817 reader
->die_section
= section
;
5818 reader
->buffer
= section
->buffer
;
5819 reader
->buffer_end
= section
->buffer
+ section
->size
;
5820 reader
->abbrev_table
= abbrev_table
;
5823 /* Subroutine of cutu_reader to simplify it.
5824 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5825 There's just a lot of work to do, and cutu_reader is big enough
5828 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5829 from it to the DIE in the DWO. If NULL we are skipping the stub.
5830 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5831 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5832 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5833 STUB_COMP_DIR may be non-NULL.
5834 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5835 are filled in with the info of the DIE from the DWO file.
5836 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5837 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5838 kept around for at least as long as *RESULT_READER.
5840 The result is non-zero if a valid (non-dummy) DIE was found. */
5843 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
5844 struct dwo_unit
*dwo_unit
,
5845 struct die_info
*stub_comp_unit_die
,
5846 const char *stub_comp_dir
,
5847 struct die_reader_specs
*result_reader
,
5848 const gdb_byte
**result_info_ptr
,
5849 struct die_info
**result_comp_unit_die
,
5850 abbrev_table_up
*result_dwo_abbrev_table
)
5852 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5853 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
5854 struct objfile
*objfile
= per_objfile
->objfile
;
5856 const gdb_byte
*begin_info_ptr
, *info_ptr
;
5857 struct attribute
*comp_dir
, *stmt_list
, *low_pc
, *high_pc
, *ranges
;
5858 int i
,num_extra_attrs
;
5859 struct dwarf2_section_info
*dwo_abbrev_section
;
5860 struct die_info
*comp_unit_die
;
5862 /* At most one of these may be provided. */
5863 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
5865 /* These attributes aren't processed until later:
5866 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5867 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5868 referenced later. However, these attributes are found in the stub
5869 which we won't have later. In order to not impose this complication
5870 on the rest of the code, we read them here and copy them to the
5879 if (stub_comp_unit_die
!= NULL
)
5881 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5883 if (!per_cu
->is_debug_types
)
5884 stmt_list
= dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
);
5885 low_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
);
5886 high_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
);
5887 ranges
= dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
);
5888 comp_dir
= dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
);
5890 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
5892 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5893 We need the value before we can process DW_AT_ranges values from the
5895 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
5897 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5898 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5899 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5900 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5902 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
5904 else if (stub_comp_dir
!= NULL
)
5906 /* Reconstruct the comp_dir attribute to simplify the code below. */
5907 comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
, struct attribute
);
5908 comp_dir
->name
= DW_AT_comp_dir
;
5909 comp_dir
->form
= DW_FORM_string
;
5910 comp_dir
->set_string_noncanonical (stub_comp_dir
);
5913 /* Set up for reading the DWO CU/TU. */
5914 cu
->dwo_unit
= dwo_unit
;
5915 dwarf2_section_info
*section
= dwo_unit
->section
;
5916 section
->read (objfile
);
5917 abfd
= section
->get_bfd_owner ();
5918 begin_info_ptr
= info_ptr
= (section
->buffer
5919 + to_underlying (dwo_unit
->sect_off
));
5920 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
5922 if (per_cu
->is_debug_types
)
5924 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
5926 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5927 section
, dwo_abbrev_section
,
5928 info_ptr
, rcuh_kind::TYPE
);
5929 /* This is not an assert because it can be caused by bad debug info. */
5930 if (sig_type
->signature
!= cu
->header
.signature
)
5932 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5933 " TU at offset %s [in module %s]"),
5934 hex_string (sig_type
->signature
),
5935 hex_string (cu
->header
.signature
),
5936 sect_offset_str (dwo_unit
->sect_off
),
5937 bfd_get_filename (abfd
));
5939 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5940 /* For DWOs coming from DWP files, we don't know the CU length
5941 nor the type's offset in the TU until now. */
5942 dwo_unit
->length
= cu
->header
.get_length ();
5943 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
5945 /* Establish the type offset that can be used to lookup the type.
5946 For DWO files, we don't know it until now. */
5947 sig_type
->type_offset_in_section
5948 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
5952 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5953 section
, dwo_abbrev_section
,
5954 info_ptr
, rcuh_kind::COMPILE
);
5955 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5956 /* For DWOs coming from DWP files, we don't know the CU length
5958 dwo_unit
->length
= cu
->header
.get_length ();
5961 dwo_abbrev_section
->read (objfile
);
5962 *result_dwo_abbrev_table
5963 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
5964 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
5965 result_dwo_abbrev_table
->get ());
5967 /* Read in the die, but leave space to copy over the attributes
5968 from the stub. This has the benefit of simplifying the rest of
5969 the code - all the work to maintain the illusion of a single
5970 DW_TAG_{compile,type}_unit DIE is done here. */
5971 num_extra_attrs
= ((stmt_list
!= NULL
)
5975 + (comp_dir
!= NULL
));
5976 info_ptr
= read_full_die_1 (result_reader
, result_comp_unit_die
, info_ptr
,
5979 /* Copy over the attributes from the stub to the DIE we just read in. */
5980 comp_unit_die
= *result_comp_unit_die
;
5981 i
= comp_unit_die
->num_attrs
;
5982 if (stmt_list
!= NULL
)
5983 comp_unit_die
->attrs
[i
++] = *stmt_list
;
5985 comp_unit_die
->attrs
[i
++] = *low_pc
;
5986 if (high_pc
!= NULL
)
5987 comp_unit_die
->attrs
[i
++] = *high_pc
;
5989 comp_unit_die
->attrs
[i
++] = *ranges
;
5990 if (comp_dir
!= NULL
)
5991 comp_unit_die
->attrs
[i
++] = *comp_dir
;
5992 comp_unit_die
->num_attrs
+= num_extra_attrs
;
5994 if (dwarf_die_debug
)
5996 gdb_printf (gdb_stdlog
,
5997 "Read die from %s@0x%x of %s:\n",
5998 section
->get_name (),
5999 (unsigned) (begin_info_ptr
- section
->buffer
),
6000 bfd_get_filename (abfd
));
6001 dump_die (comp_unit_die
, dwarf_die_debug
);
6004 /* Skip dummy compilation units. */
6005 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
6006 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6009 *result_info_ptr
= info_ptr
;
6013 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6014 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6015 signature is part of the header. */
6016 static gdb::optional
<ULONGEST
>
6017 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
6019 if (cu
->header
.version
>= 5)
6020 return cu
->header
.signature
;
6021 struct attribute
*attr
;
6022 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
6023 if (attr
== nullptr || !attr
->form_is_unsigned ())
6024 return gdb::optional
<ULONGEST
> ();
6025 return attr
->as_unsigned ();
6028 /* Subroutine of cutu_reader to simplify it.
6029 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6030 Returns NULL if the specified DWO unit cannot be found. */
6032 static struct dwo_unit
*
6033 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
6036 /* We need a lock here both to handle the DWO hash table, and BFD,
6037 which is not thread-safe. */
6038 static std::mutex dwo_lock
;
6040 std::lock_guard
<std::mutex
> guard (dwo_lock
);
6043 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6044 struct dwo_unit
*dwo_unit
;
6045 const char *comp_dir
;
6047 gdb_assert (cu
!= NULL
);
6049 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6050 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6051 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
6053 if (per_cu
->is_debug_types
)
6054 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
6057 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
6059 if (!signature
.has_value ())
6060 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6062 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
6064 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
6070 /* Subroutine of cutu_reader to simplify it.
6071 See it for a description of the parameters.
6072 Read a TU directly from a DWO file, bypassing the stub. */
6075 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
6076 dwarf2_per_objfile
*per_objfile
,
6077 dwarf2_cu
*existing_cu
)
6079 struct signatured_type
*sig_type
;
6081 /* Verify we can do the following downcast, and that we have the
6083 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
6084 sig_type
= (struct signatured_type
*) this_cu
;
6085 gdb_assert (sig_type
->dwo_unit
!= NULL
);
6089 if (existing_cu
!= nullptr)
6092 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
6093 /* There's no need to do the rereading_dwo_cu handling that
6094 cutu_reader does since we don't read the stub. */
6098 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6099 in per_objfile yet. */
6100 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6101 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6102 cu
= m_new_cu
.get ();
6105 /* A future optimization, if needed, would be to use an existing
6106 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6107 could share abbrev tables. */
6109 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
6110 NULL
/* stub_comp_unit_die */,
6111 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
6114 &m_dwo_abbrev_table
) == 0)
6121 /* Initialize a CU (or TU) and read its DIEs.
6122 If the CU defers to a DWO file, read the DWO file as well.
6124 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6125 Otherwise the table specified in the comp unit header is read in and used.
6126 This is an optimization for when we already have the abbrev table.
6128 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6131 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6132 dwarf2_per_objfile
*per_objfile
,
6133 struct abbrev_table
*abbrev_table
,
6134 dwarf2_cu
*existing_cu
,
6136 abbrev_cache
*cache
)
6137 : die_reader_specs
{},
6140 struct objfile
*objfile
= per_objfile
->objfile
;
6141 struct dwarf2_section_info
*section
= this_cu
->section
;
6142 bfd
*abfd
= section
->get_bfd_owner ();
6143 const gdb_byte
*begin_info_ptr
;
6144 struct signatured_type
*sig_type
= NULL
;
6145 struct dwarf2_section_info
*abbrev_section
;
6146 /* Non-zero if CU currently points to a DWO file and we need to
6147 reread it. When this happens we need to reread the skeleton die
6148 before we can reread the DWO file (this only applies to CUs, not TUs). */
6149 int rereading_dwo_cu
= 0;
6151 if (dwarf_die_debug
)
6152 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6153 this_cu
->is_debug_types
? "type" : "comp",
6154 sect_offset_str (this_cu
->sect_off
));
6156 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6157 file (instead of going through the stub), short-circuit all of this. */
6158 if (this_cu
->reading_dwo_directly
)
6160 /* Narrow down the scope of possibilities to have to understand. */
6161 gdb_assert (this_cu
->is_debug_types
);
6162 gdb_assert (abbrev_table
== NULL
);
6163 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
6167 /* This is cheap if the section is already read in. */
6168 section
->read (objfile
);
6170 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6172 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
6176 if (existing_cu
!= nullptr)
6179 /* If this CU is from a DWO file we need to start over, we need to
6180 refetch the attributes from the skeleton CU.
6181 This could be optimized by retrieving those attributes from when we
6182 were here the first time: the previous comp_unit_die was stored in
6183 comp_unit_obstack. But there's no data yet that we need this
6185 if (cu
->dwo_unit
!= NULL
)
6186 rereading_dwo_cu
= 1;
6190 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6191 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6192 let us know that the CU is being scanned using the parallel
6193 indexer. This assert is avoided in this case because (1) it
6194 is irrelevant, and (2) the get_cu method is not
6196 gdb_assert (cache
!= nullptr
6197 || per_objfile
->get_cu (this_cu
) == nullptr);
6198 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6199 cu
= m_new_cu
.get ();
6202 /* Get the header. */
6203 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
6205 /* We already have the header, there's no need to read it in again. */
6206 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
6210 if (this_cu
->is_debug_types
)
6212 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6213 section
, abbrev_section
,
6214 info_ptr
, rcuh_kind::TYPE
);
6216 /* Since per_cu is the first member of struct signatured_type,
6217 we can go from a pointer to one to a pointer to the other. */
6218 sig_type
= (struct signatured_type
*) this_cu
;
6219 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
6220 gdb_assert (sig_type
->type_offset_in_tu
6221 == cu
->header
.type_cu_offset_in_tu
);
6222 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6224 /* LENGTH has not been set yet for type units if we're
6225 using .gdb_index. */
6226 this_cu
->length
= cu
->header
.get_length ();
6228 /* Establish the type offset that can be used to lookup the type. */
6229 sig_type
->type_offset_in_section
=
6230 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
6232 this_cu
->dwarf_version
= cu
->header
.version
;
6236 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6237 section
, abbrev_section
,
6239 rcuh_kind::COMPILE
);
6241 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6242 if (this_cu
->length
== 0)
6243 this_cu
->length
= cu
->header
.get_length ();
6245 gdb_assert (this_cu
->length
== cu
->header
.get_length ());
6246 this_cu
->dwarf_version
= cu
->header
.version
;
6250 /* Skip dummy compilation units. */
6251 if (info_ptr
>= begin_info_ptr
+ this_cu
->length
6252 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6258 /* If we don't have them yet, read the abbrevs for this compilation unit.
6259 And if we need to read them now, make sure they're freed when we're
6261 if (abbrev_table
!= NULL
)
6262 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
6265 if (cache
!= nullptr)
6266 abbrev_table
= cache
->find (abbrev_section
,
6267 cu
->header
.abbrev_sect_off
);
6268 if (abbrev_table
== nullptr)
6270 abbrev_section
->read (objfile
);
6271 m_abbrev_table_holder
6272 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
6273 abbrev_table
= m_abbrev_table_holder
.get ();
6277 /* Read the top level CU/TU die. */
6278 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
6279 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6281 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
6287 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6288 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6289 table from the DWO file and pass the ownership over to us. It will be
6290 referenced from READER, so we must make sure to free it after we're done
6293 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6294 DWO CU, that this test will fail (the attribute will not be present). */
6295 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6296 if (dwo_name
!= nullptr)
6298 struct dwo_unit
*dwo_unit
;
6299 struct die_info
*dwo_comp_unit_die
;
6301 if (comp_unit_die
->has_children
)
6303 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6304 " has children (offset %s) [in module %s]"),
6305 sect_offset_str (this_cu
->sect_off
),
6306 bfd_get_filename (abfd
));
6308 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
6309 if (dwo_unit
!= NULL
)
6311 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
6312 comp_unit_die
, NULL
,
6315 &m_dwo_abbrev_table
) == 0)
6321 comp_unit_die
= dwo_comp_unit_die
;
6325 /* Yikes, we couldn't find the rest of the DIE, we only have
6326 the stub. A complaint has already been logged. There's
6327 not much more we can do except pass on the stub DIE to
6328 die_reader_func. We don't want to throw an error on bad
6335 cutu_reader::keep ()
6337 /* Done, clean up. */
6338 gdb_assert (!dummy_p
);
6339 if (m_new_cu
!= NULL
)
6341 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6343 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
6344 per_objfile
->set_cu (m_this_cu
, m_new_cu
.release ());
6348 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6349 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6350 assumed to have already done the lookup to find the DWO file).
6352 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6353 THIS_CU->is_debug_types, but nothing else.
6355 We fill in THIS_CU->length.
6357 THIS_CU->cu is always freed when done.
6358 This is done in order to not leave THIS_CU->cu in a state where we have
6359 to care whether it refers to the "main" CU or the DWO CU.
6361 When parent_cu is passed, it is used to provide a default value for
6362 str_offsets_base and addr_base from the parent. */
6364 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6365 dwarf2_per_objfile
*per_objfile
,
6366 struct dwarf2_cu
*parent_cu
,
6367 struct dwo_file
*dwo_file
)
6368 : die_reader_specs
{},
6371 struct objfile
*objfile
= per_objfile
->objfile
;
6372 struct dwarf2_section_info
*section
= this_cu
->section
;
6373 bfd
*abfd
= section
->get_bfd_owner ();
6374 struct dwarf2_section_info
*abbrev_section
;
6375 const gdb_byte
*begin_info_ptr
, *info_ptr
;
6377 if (dwarf_die_debug
)
6378 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6379 this_cu
->is_debug_types
? "type" : "comp",
6380 sect_offset_str (this_cu
->sect_off
));
6382 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6384 abbrev_section
= (dwo_file
!= NULL
6385 ? &dwo_file
->sections
.abbrev
6386 : get_abbrev_section_for_cu (this_cu
));
6388 /* This is cheap if the section is already read in. */
6389 section
->read (objfile
);
6391 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6393 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6394 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
6395 section
, abbrev_section
, info_ptr
,
6396 (this_cu
->is_debug_types
6398 : rcuh_kind::COMPILE
));
6400 if (parent_cu
!= nullptr)
6402 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
6403 m_new_cu
->addr_base
= parent_cu
->addr_base
;
6405 this_cu
->length
= m_new_cu
->header
.get_length ();
6407 /* Skip dummy compilation units. */
6408 if (info_ptr
>= begin_info_ptr
+ this_cu
->length
6409 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6415 abbrev_section
->read (objfile
);
6416 m_abbrev_table_holder
6417 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
6419 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
6420 m_abbrev_table_holder
.get ());
6421 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6425 /* Type Unit Groups.
6427 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6428 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6429 so that all types coming from the same compilation (.o file) are grouped
6430 together. A future step could be to put the types in the same symtab as
6431 the CU the types ultimately came from. */
6434 hash_type_unit_group (const void *item
)
6436 const struct type_unit_group
*tu_group
6437 = (const struct type_unit_group
*) item
;
6439 return hash_stmt_list_entry (&tu_group
->hash
);
6443 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
6445 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
6446 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
6448 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
6451 /* Allocate a hash table for type unit groups. */
6454 allocate_type_unit_groups_table ()
6456 return htab_up (htab_create_alloc (3,
6457 hash_type_unit_group
,
6459 htab_delete_entry
<type_unit_group
>,
6463 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6464 partial symtabs. We combine several TUs per psymtab to not let the size
6465 of any one psymtab grow too big. */
6466 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6467 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6469 /* Helper routine for get_type_unit_group.
6470 Create the type_unit_group object used to hold one or more TUs. */
6472 static std::unique_ptr
<type_unit_group
>
6473 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
6475 std::unique_ptr
<type_unit_group
> tu_group (new type_unit_group
);
6477 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
6478 tu_group
->hash
.line_sect_off
= line_offset_struct
;
6483 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6484 STMT_LIST is a DW_AT_stmt_list attribute. */
6486 static struct type_unit_group
*
6487 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
6489 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6490 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6491 struct type_unit_group
*tu_group
;
6493 unsigned int line_offset
;
6494 struct type_unit_group type_unit_group_for_lookup
;
6496 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
6497 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
6499 /* Do we need to create a new group, or can we use an existing one? */
6501 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
6503 line_offset
= stmt_list
->as_unsigned ();
6504 ++tu_stats
->nr_symtab_sharers
;
6508 /* Ugh, no stmt_list. Rare, but we have to handle it.
6509 We can do various things here like create one group per TU or
6510 spread them over multiple groups to split up the expansion work.
6511 To avoid worst case scenarios (too many groups or too large groups)
6512 we, umm, group them in bunches. */
6513 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6514 | (tu_stats
->nr_stmt_less_type_units
6515 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
6516 ++tu_stats
->nr_stmt_less_type_units
;
6519 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
6520 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
6521 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
6522 &type_unit_group_for_lookup
, INSERT
);
6523 if (*slot
== nullptr)
6525 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
6526 std::unique_ptr
<type_unit_group
> grp
6527 = create_type_unit_group (cu
, line_offset_struct
);
6528 *slot
= grp
.release ();
6529 ++tu_stats
->nr_symtabs
;
6532 tu_group
= (struct type_unit_group
*) *slot
;
6533 gdb_assert (tu_group
!= nullptr);
6538 /* An instance of this is created when scanning DWARF to create a
6541 class cooked_index_storage
6545 cooked_index_storage ()
6546 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
6548 htab_delete_entry
<cutu_reader
>,
6550 m_index (new cooked_index
),
6551 m_addrmap_storage (),
6552 m_addrmap (addrmap_create_mutable (&m_addrmap_storage
))
6556 DISABLE_COPY_AND_ASSIGN (cooked_index_storage
);
6558 /* Return the current abbrev cache. */
6559 abbrev_cache
*get_abbrev_cache ()
6561 return &m_abbrev_cache
;
6564 /* Return the DIE reader corresponding to PER_CU. If no such reader
6565 has been registered, return NULL. */
6566 cutu_reader
*get_reader (dwarf2_per_cu_data
*per_cu
)
6568 int index
= per_cu
->index
;
6569 return (cutu_reader
*) htab_find_with_hash (m_reader_hash
.get (),
6573 /* Preserve READER by storing it in the local hash table. */
6574 cutu_reader
*preserve (std::unique_ptr
<cutu_reader
> reader
)
6576 m_abbrev_cache
.add (reader
->release_abbrev_table ());
6578 int index
= reader
->cu
->per_cu
->index
;
6579 void **slot
= htab_find_slot_with_hash (m_reader_hash
.get (), &index
,
6581 gdb_assert (*slot
== nullptr);
6582 cutu_reader
*result
= reader
.get ();
6583 *slot
= reader
.release ();
6587 /* Add an entry to the index. The arguments describe the entry; see
6588 cooked-index.h. The new entry is returned. */
6589 const cooked_index_entry
*add (sect_offset die_offset
, enum dwarf_tag tag
,
6590 cooked_index_flag flags
,
6592 const cooked_index_entry
*parent_entry
,
6593 dwarf2_per_cu_data
*per_cu
)
6595 return m_index
->add (die_offset
, tag
, flags
, name
, parent_entry
, per_cu
);
6598 /* Install the current addrmap into the index being constructed,
6599 then transfer ownership of the index to the caller. */
6600 std::unique_ptr
<cooked_index
> release ()
6602 m_index
->install_addrmap (m_addrmap
);
6603 return std::move (m_index
);
6606 /* Return the mutable addrmap that is currently being created. */
6607 addrmap
*get_addrmap ()
6614 /* Hash function for a cutu_reader. */
6615 static hashval_t
hash_cutu_reader (const void *a
)
6617 const cutu_reader
*reader
= (const cutu_reader
*) a
;
6618 return reader
->cu
->per_cu
->index
;
6621 /* Equality function for cutu_reader. */
6622 static int eq_cutu_reader (const void *a
, const void *b
)
6624 const cutu_reader
*ra
= (const cutu_reader
*) a
;
6625 const int *rb
= (const int *) b
;
6626 return ra
->cu
->per_cu
->index
== *rb
;
6629 /* The abbrev cache used by this indexer. */
6630 abbrev_cache m_abbrev_cache
;
6631 /* A hash table of cutu_reader objects. */
6632 htab_up m_reader_hash
;
6633 /* The index that is being constructed. */
6634 std::unique_ptr
<cooked_index
> m_index
;
6636 /* Storage for the writeable addrmap. */
6637 auto_obstack m_addrmap_storage
;
6638 /* A writeable addrmap being constructed by this scanner. */
6642 /* An instance of this is created to index a CU. */
6644 class cooked_indexer
6648 cooked_indexer (cooked_index_storage
*storage
,
6649 dwarf2_per_cu_data
*per_cu
,
6650 enum language language
)
6651 : m_index_storage (storage
),
6653 m_language (language
),
6655 m_die_range_map (addrmap_create_mutable (&m_obstack
))
6659 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
6661 /* Index the given CU. */
6662 void make_index (cutu_reader
*reader
);
6666 /* A helper function to turn a section offset into an address that
6667 can be used in an addrmap. */
6668 CORE_ADDR
form_addr (sect_offset offset
, bool is_dwz
)
6670 CORE_ADDR value
= to_underlying (offset
);
6672 value
|= ((CORE_ADDR
) 1) << (8 * sizeof (CORE_ADDR
) - 1);
6676 /* A helper function to scan the PC bounds of READER and record them
6677 in the storage's addrmap. */
6678 void check_bounds (cutu_reader
*reader
);
6680 /* Ensure that the indicated CU exists. The cutu_reader for it is
6681 returned. FOR_SCANNING is true if the caller intends to scan all
6682 the DIEs in the CU; when false, this use is assumed to be to look
6683 up just a single DIE. */
6684 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
6685 dwarf2_per_objfile
*per_objfile
,
6686 sect_offset sect_off
,
6690 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6691 the entry for the enclosing scope (nullptr at top level). FULLY
6692 is true when a full scan must be done -- in some languages,
6693 function scopes must be fully explored in order to find nested
6694 functions. This returns a pointer to just after the spot where
6696 const gdb_byte
*index_dies (cutu_reader
*reader
,
6697 const gdb_byte
*info_ptr
,
6698 const cooked_index_entry
*parent_entry
,
6701 /* Scan the attributes for a given DIE and update the out
6702 parameters. Returns a pointer to the byte after the DIE. */
6703 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
6704 cutu_reader
*reader
,
6705 const gdb_byte
*watermark_ptr
,
6706 const gdb_byte
*info_ptr
,
6707 const abbrev_info
*abbrev
,
6709 const char **linkage_name
,
6710 cooked_index_flag
*flags
,
6711 sect_offset
*sibling_offset
,
6712 const cooked_index_entry
**parent_entry
,
6713 CORE_ADDR
*maybe_defer
,
6714 bool for_specification
);
6716 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6717 DW_AT_import, and then scanning the referenced CU. Returns a
6718 pointer to the byte after the DIE. */
6719 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
6720 const gdb_byte
*info_ptr
,
6721 const abbrev_info
*abbrev
);
6723 /* Recursively read DIEs, recording the section offsets in
6724 m_die_range_map and then calling index_dies. */
6725 const gdb_byte
*recurse (cutu_reader
*reader
,
6726 const gdb_byte
*info_ptr
,
6727 const cooked_index_entry
*parent_entry
,
6730 /* The storage object, where the results are kept. */
6731 cooked_index_storage
*m_index_storage
;
6732 /* The CU that we are reading on behalf of. This object might be
6733 asked to index one CU but to treat the results as if they come
6734 from some including CU; in this case the including CU would be
6736 dwarf2_per_cu_data
*m_per_cu
;
6737 /* The language that we're assuming when reading. */
6738 enum language m_language
;
6740 /* Temporary storage. */
6741 auto_obstack m_obstack
;
6742 /* An addrmap that maps from section offsets (see the form_addr
6743 method) to newly-created entries. See m_deferred_entries to
6745 addrmap
*m_die_range_map
;
6747 /* A single deferred entry. */
6748 struct deferred_entry
6750 sect_offset die_offset
;
6752 CORE_ADDR spec_offset
;
6754 cooked_index_flag flags
;
6757 /* The generated DWARF can sometimes have the declaration for a
6758 method in a class (or perhaps namespace) scope, with the
6759 definition appearing outside this scope... just one of the many
6760 bad things about DWARF. In order to handle this situation, we
6761 defer certain entries until the end of scanning, at which point
6762 we'll know the containing context of all the DIEs that we might
6763 have scanned. This vector stores these deferred entries. */
6764 std::vector
<deferred_entry
> m_deferred_entries
;
6767 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6768 Process compilation unit THIS_CU for a psymtab. */
6771 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
6772 dwarf2_per_objfile
*per_objfile
,
6773 cooked_index_storage
*storage
)
6775 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
6776 storage
->get_abbrev_cache ());
6778 if (reader
.comp_unit_die
== nullptr)
6781 switch (reader
.comp_unit_die
->tag
)
6783 case DW_TAG_compile_unit
:
6784 this_cu
->unit_type
= DW_UT_compile
;
6786 case DW_TAG_partial_unit
:
6787 this_cu
->unit_type
= DW_UT_partial
;
6789 case DW_TAG_type_unit
:
6790 this_cu
->unit_type
= DW_UT_type
;
6793 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6794 dwarf_tag_name (reader
.comp_unit_die
->tag
),
6795 sect_offset_str (reader
.cu
->per_cu
->sect_off
),
6796 objfile_name (per_objfile
->objfile
));
6803 else if (this_cu
->is_debug_types
)
6804 build_type_psymtabs_reader (&reader
, storage
);
6805 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
6808 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
6810 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
6812 gdb_assert (storage
!= nullptr);
6813 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->per_cu
->lang
);
6814 indexer
.make_index (&reader
);
6819 /* Reader function for build_type_psymtabs. */
6822 build_type_psymtabs_reader (cutu_reader
*reader
,
6823 cooked_index_storage
*storage
)
6825 struct dwarf2_cu
*cu
= reader
->cu
;
6826 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6827 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
6829 gdb_assert (per_cu
->is_debug_types
);
6831 if (! type_unit_die
->has_children
)
6834 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
6836 gdb_assert (storage
!= nullptr);
6837 cooked_indexer
indexer (storage
, per_cu
, cu
->per_cu
->lang
);
6838 indexer
.make_index (reader
);
6841 /* Struct used to sort TUs by their abbreviation table offset. */
6843 struct tu_abbrev_offset
6845 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
6846 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
6849 /* This is used when sorting. */
6850 bool operator< (const tu_abbrev_offset
&other
) const
6852 return abbrev_offset
< other
.abbrev_offset
;
6855 signatured_type
*sig_type
;
6856 sect_offset abbrev_offset
;
6859 /* Efficiently read all the type units.
6861 The efficiency is because we sort TUs by the abbrev table they use and
6862 only read each abbrev table once. In one program there are 200K TUs
6863 sharing 8K abbrev tables.
6865 The main purpose of this function is to support building the
6866 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6867 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6868 can collapse the search space by grouping them by stmt_list.
6869 The savings can be significant, in the same program from above the 200K TUs
6870 share 8K stmt_list tables.
6872 FUNC is expected to call get_type_unit_group, which will create the
6873 struct type_unit_group if necessary and add it to
6874 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6877 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
6878 cooked_index_storage
*storage
)
6880 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6881 abbrev_table_up abbrev_table
;
6882 sect_offset abbrev_offset
;
6884 /* It's up to the caller to not call us multiple times. */
6885 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
6887 if (per_objfile
->per_bfd
->tu_stats
.nr_tus
== 0)
6890 /* TUs typically share abbrev tables, and there can be way more TUs than
6891 abbrev tables. Sort by abbrev table to reduce the number of times we
6892 read each abbrev table in.
6893 Alternatives are to punt or to maintain a cache of abbrev tables.
6894 This is simpler and efficient enough for now.
6896 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6897 symtab to use). Typically TUs with the same abbrev offset have the same
6898 stmt_list value too so in practice this should work well.
6900 The basic algorithm here is:
6902 sort TUs by abbrev table
6903 for each TU with same abbrev table:
6904 read abbrev table if first user
6905 read TU top level DIE
6906 [IWBN if DWO skeletons had DW_AT_stmt_list]
6909 dwarf_read_debug_printf ("Building type unit groups ...");
6911 /* Sort in a separate table to maintain the order of all_comp_units
6912 for .gdb_index: TU indices directly index all_type_units. */
6913 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
6914 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->tu_stats
.nr_tus
);
6916 for (const auto &cu
: per_objfile
->per_bfd
->all_comp_units
)
6918 if (cu
->is_debug_types
)
6920 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
6921 sorted_by_abbrev
.emplace_back
6922 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
6923 sig_type
->sect_off
));
6927 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
6929 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
6931 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
6933 /* Switch to the next abbrev table if necessary. */
6934 if (abbrev_table
== NULL
6935 || tu
.abbrev_offset
!= abbrev_offset
)
6937 abbrev_offset
= tu
.abbrev_offset
;
6938 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
6940 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
6941 ++tu_stats
->nr_uniq_abbrev_tables
;
6944 cutu_reader
reader (tu
.sig_type
, per_objfile
,
6945 abbrev_table
.get (), nullptr, false);
6946 if (!reader
.dummy_p
)
6947 build_type_psymtabs_reader (&reader
, storage
);
6951 /* Print collected type unit statistics. */
6954 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
6956 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6958 dwarf_read_debug_printf ("Type unit statistics:");
6959 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
6960 dwarf_read_debug_printf (" %d uniq abbrev tables",
6961 tu_stats
->nr_uniq_abbrev_tables
);
6962 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6963 tu_stats
->nr_symtabs
);
6964 dwarf_read_debug_printf (" %d symtab sharers",
6965 tu_stats
->nr_symtab_sharers
);
6966 dwarf_read_debug_printf (" %d type units without a stmt_list",
6967 tu_stats
->nr_stmt_less_type_units
);
6968 dwarf_read_debug_printf (" %d all_type_units reallocs",
6969 tu_stats
->nr_all_type_units_reallocs
);
6972 struct skeleton_data
6974 dwarf2_per_objfile
*per_objfile
;
6975 cooked_index_storage
*storage
;
6978 /* Traversal function for process_skeletonless_type_unit.
6979 Read a TU in a DWO file and build partial symbols for it. */
6982 process_skeletonless_type_unit (void **slot
, void *info
)
6984 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
6985 skeleton_data
*data
= (skeleton_data
*) info
;
6987 /* If this TU doesn't exist in the global table, add it and read it in. */
6989 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
6990 data
->per_objfile
->per_bfd
->signatured_types
6991 = allocate_signatured_type_table ();
6993 signatured_type
find_entry (dwo_unit
->signature
);
6994 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
6995 &find_entry
, INSERT
);
6996 /* If we've already seen this type there's nothing to do. What's happening
6997 is we're doing our own version of comdat-folding here. */
7001 /* This does the job that create_all_comp_units would have done for
7003 signatured_type
*entry
7004 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
7005 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
7008 /* This does the job that build_type_psymtabs would have done. */
7009 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
7010 if (!reader
.dummy_p
)
7011 build_type_psymtabs_reader (&reader
, data
->storage
);
7016 /* Traversal function for process_skeletonless_type_units. */
7019 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
7021 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
7023 if (dwo_file
->tus
!= NULL
)
7024 htab_traverse_noresize (dwo_file
->tus
.get (),
7025 process_skeletonless_type_unit
, info
);
7030 /* Scan all TUs of DWO files, verifying we've processed them.
7031 This is needed in case a TU was emitted without its skeleton.
7032 Note: This can't be done until we know what all the DWO files are. */
7035 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
7036 cooked_index_storage
*storage
)
7038 skeleton_data data
{ per_objfile
, storage
};
7040 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7041 if (get_dwp_file (per_objfile
) == NULL
7042 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
7044 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
7045 process_dwo_file_for_skeletonless_type_units
,
7050 /* Build the partial symbol table by doing a quick pass through the
7051 .debug_info and .debug_abbrev sections. */
7054 dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
)
7056 struct objfile
*objfile
= per_objfile
->objfile
;
7057 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7059 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7060 objfile_name (objfile
));
7062 per_bfd
->map_info_sections (objfile
);
7064 cooked_index_storage index_storage
;
7065 create_all_comp_units (per_objfile
);
7066 build_type_psymtabs (per_objfile
, &index_storage
);
7067 std::vector
<std::unique_ptr
<cooked_index
>> indexes
;
7069 per_bfd
->quick_file_names_table
7070 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
7071 if (!per_bfd
->debug_aranges
.empty ())
7072 read_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
,
7073 index_storage
.get_addrmap ());
7076 /* Ensure that complaints are handled correctly. */
7077 complaint_interceptor complaint_handler
;
7079 using iter_type
= decltype (per_bfd
->all_comp_units
.begin ());
7081 /* Each thread returns a pair holding a cooked index, and a vector
7082 of errors that should be printed. The latter is done because
7083 GDB's I/O system is not thread-safe. run_on_main_thread could be
7084 used, but that would mean the messages are printed after the
7085 prompt, which looks weird. */
7086 using result_type
= std::pair
<std::unique_ptr
<cooked_index
>,
7087 std::vector
<gdb_exception
>>;
7088 std::vector
<result_type
> results
7089 = gdb::parallel_for_each (1, per_bfd
->all_comp_units
.begin (),
7090 per_bfd
->all_comp_units
.end (),
7091 [=] (iter_type iter
, iter_type end
)
7093 std::vector
<gdb_exception
> errors
;
7094 cooked_index_storage thread_storage
;
7095 for (; iter
!= end
; ++iter
)
7097 dwarf2_per_cu_data
*per_cu
= iter
->get ();
7100 process_psymtab_comp_unit (per_cu
, per_objfile
,
7103 catch (gdb_exception
&except
)
7105 errors
.push_back (std::move (except
));
7108 return result_type (thread_storage
.release (), std::move (errors
));
7111 /* Only show a given exception a single time. */
7112 std::unordered_set
<gdb_exception
> seen_exceptions
;
7113 for (auto &one_result
: results
)
7115 indexes
.push_back (std::move (one_result
.first
));
7116 for (auto &one_exc
: one_result
.second
)
7117 if (seen_exceptions
.insert (one_exc
).second
)
7118 exception_print (gdb_stderr
, one_exc
);
7122 /* This has to wait until we read the CUs, we need the list of DWOs. */
7123 process_skeletonless_type_units (per_objfile
, &index_storage
);
7125 if (dwarf_read_debug
> 0)
7126 print_tu_stats (per_objfile
);
7128 indexes
.push_back (index_storage
.release ());
7129 /* Remove any NULL entries. This might happen if parallel-for
7130 decides to throttle the number of threads that were used. */
7132 (std::remove_if (indexes
.begin (),
7134 [] (const std::unique_ptr
<cooked_index
> &entry
)
7136 return entry
== nullptr;
7139 indexes
.shrink_to_fit ();
7141 cooked_index_vector
*vec
= new cooked_index_vector (std::move (indexes
));
7142 per_bfd
->index_table
.reset (vec
);
7144 const cooked_index_entry
*main_entry
= vec
->get_main ();
7145 if (main_entry
!= nullptr)
7146 set_objfile_main_name (objfile
, main_entry
->name
,
7147 main_entry
->per_cu
->lang
);
7149 dwarf_read_debug_printf ("Done building psymtabs of %s",
7150 objfile_name (objfile
));
7154 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
7155 struct dwarf2_section_info
*section
,
7156 struct dwarf2_section_info
*abbrev_section
,
7157 unsigned int is_dwz
,
7158 htab_up
&types_htab
,
7159 rcuh_kind section_kind
)
7161 const gdb_byte
*info_ptr
;
7162 struct objfile
*objfile
= per_objfile
->objfile
;
7164 dwarf_read_debug_printf ("Reading %s for %s",
7165 section
->get_name (),
7166 section
->get_file_name ());
7168 section
->read (objfile
);
7170 info_ptr
= section
->buffer
;
7172 while (info_ptr
< section
->buffer
+ section
->size
)
7174 dwarf2_per_cu_data_up this_cu
;
7176 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
7178 comp_unit_head cu_header
;
7179 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
7180 abbrev_section
, info_ptr
,
7183 /* Save the compilation unit for later lookup. */
7184 if (cu_header
.unit_type
!= DW_UT_type
)
7185 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
7188 if (types_htab
== nullptr)
7189 types_htab
= allocate_signatured_type_table ();
7191 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
7192 (cu_header
.signature
);
7193 signatured_type
*sig_ptr
= sig_type
.get ();
7194 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
7195 this_cu
.reset (sig_type
.release ());
7197 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
7198 gdb_assert (slot
!= nullptr);
7199 if (*slot
!= nullptr)
7200 complaint (_("debug type entry at offset %s is duplicate to"
7201 " the entry at offset %s, signature %s"),
7202 sect_offset_str (sect_off
),
7203 sect_offset_str (sig_ptr
->sect_off
),
7204 hex_string (sig_ptr
->signature
));
7207 this_cu
->sect_off
= sect_off
;
7208 this_cu
->length
= cu_header
.length
+ cu_header
.initial_length_size
;
7209 this_cu
->is_dwz
= is_dwz
;
7210 this_cu
->section
= section
;
7212 info_ptr
= info_ptr
+ this_cu
->length
;
7213 per_objfile
->per_bfd
->all_comp_units
.push_back (std::move (this_cu
));
7217 /* Create a list of all compilation units in OBJFILE.
7218 This is only done for -readnow and building partial symtabs. */
7221 create_all_comp_units (dwarf2_per_objfile
*per_objfile
)
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
->per_bfd
->queue
.has_value ());
7489 per_cu
->per_bfd
->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
->per_bfd
->queue
->empty ())
7574 dwarf2_queue_item
&item
= per_objfile
->per_bfd
->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
->per_bfd
->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
->per_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
->per_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
->per_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
->per_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
->per_cu
->lang
== language_c
8407 && cust
->primary_filetab ()->language () != language_unknown
))
8408 cust
->primary_filetab ()->set_language (cu
->per_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
->per_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
->per_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
->per_cu
->lang
== language_c
8492 && cust
->primary_filetab ()->language () != language_c
))
8493 cust
->primary_filetab ()->set_language (cu
->per_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,
8536 if (die
->parent
&& die
->parent
->parent
== NULL
8537 && per_cu
->unit_type
== DW_UT_compile
8538 && per_cu
->lang
== language_cplus
)
8541 /* If necessary, add it to the queue and load its DIEs. */
8542 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
8544 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
8545 false, cu
->per_cu
->lang
);
8547 cu
->per_cu
->imported_symtabs_push (per_cu
);
8551 /* RAII object that represents a process_die scope: i.e.,
8552 starts/finishes processing a DIE. */
8553 class process_die_scope
8556 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
8557 : m_die (die
), m_cu (cu
)
8559 /* We should only be processing DIEs not already in process. */
8560 gdb_assert (!m_die
->in_process
);
8561 m_die
->in_process
= true;
8564 ~process_die_scope ()
8566 m_die
->in_process
= false;
8568 /* If we're done processing the DIE for the CU that owns the line
8569 header, we don't need the line header anymore. */
8570 if (m_cu
->line_header_die_owner
== m_die
)
8572 delete m_cu
->line_header
;
8573 m_cu
->line_header
= NULL
;
8574 m_cu
->line_header_die_owner
= NULL
;
8583 /* Process a die and its children. */
8586 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8588 process_die_scope
scope (die
, cu
);
8592 case DW_TAG_padding
:
8594 case DW_TAG_compile_unit
:
8595 case DW_TAG_partial_unit
:
8596 read_file_scope (die
, cu
);
8598 case DW_TAG_type_unit
:
8599 read_type_unit_scope (die
, cu
);
8601 case DW_TAG_subprogram
:
8602 /* Nested subprograms in Fortran get a prefix. */
8603 if (cu
->per_cu
->lang
== language_fortran
8604 && die
->parent
!= NULL
8605 && die
->parent
->tag
== DW_TAG_subprogram
)
8606 cu
->processing_has_namespace_info
= true;
8608 case DW_TAG_inlined_subroutine
:
8609 read_func_scope (die
, cu
);
8611 case DW_TAG_lexical_block
:
8612 case DW_TAG_try_block
:
8613 case DW_TAG_catch_block
:
8614 read_lexical_block_scope (die
, cu
);
8616 case DW_TAG_call_site
:
8617 case DW_TAG_GNU_call_site
:
8618 read_call_site_scope (die
, cu
);
8620 case DW_TAG_class_type
:
8621 case DW_TAG_interface_type
:
8622 case DW_TAG_structure_type
:
8623 case DW_TAG_union_type
:
8624 case DW_TAG_namelist
:
8625 process_structure_scope (die
, cu
);
8627 case DW_TAG_enumeration_type
:
8628 process_enumeration_scope (die
, cu
);
8631 /* These dies have a type, but processing them does not create
8632 a symbol or recurse to process the children. Therefore we can
8633 read them on-demand through read_type_die. */
8634 case DW_TAG_subroutine_type
:
8635 case DW_TAG_set_type
:
8636 case DW_TAG_pointer_type
:
8637 case DW_TAG_ptr_to_member_type
:
8638 case DW_TAG_reference_type
:
8639 case DW_TAG_rvalue_reference_type
:
8640 case DW_TAG_string_type
:
8643 case DW_TAG_array_type
:
8644 /* We only need to handle this case for Ada -- in other
8645 languages, it's normal for the compiler to emit a typedef
8647 if (cu
->per_cu
->lang
!= language_ada
)
8650 case DW_TAG_base_type
:
8651 case DW_TAG_subrange_type
:
8652 case DW_TAG_generic_subrange
:
8653 case DW_TAG_typedef
:
8654 /* Add a typedef symbol for the type definition, if it has a
8656 new_symbol (die
, read_type_die (die
, cu
), cu
);
8658 case DW_TAG_common_block
:
8659 read_common_block (die
, cu
);
8661 case DW_TAG_common_inclusion
:
8663 case DW_TAG_namespace
:
8664 cu
->processing_has_namespace_info
= true;
8665 read_namespace (die
, cu
);
8668 cu
->processing_has_namespace_info
= true;
8669 read_module (die
, cu
);
8671 case DW_TAG_imported_declaration
:
8672 cu
->processing_has_namespace_info
= true;
8673 if (read_namespace_alias (die
, cu
))
8675 /* The declaration is not a global namespace alias. */
8677 case DW_TAG_imported_module
:
8678 cu
->processing_has_namespace_info
= true;
8679 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
8680 || cu
->per_cu
->lang
!= language_fortran
))
8681 complaint (_("Tag '%s' has unexpected children"),
8682 dwarf_tag_name (die
->tag
));
8683 read_import_statement (die
, cu
);
8686 case DW_TAG_imported_unit
:
8687 process_imported_unit_die (die
, cu
);
8690 case DW_TAG_variable
:
8691 read_variable (die
, cu
);
8695 new_symbol (die
, NULL
, cu
);
8700 /* DWARF name computation. */
8702 /* A helper function for dwarf2_compute_name which determines whether DIE
8703 needs to have the name of the scope prepended to the name listed in the
8707 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
8709 struct attribute
*attr
;
8713 case DW_TAG_namespace
:
8714 case DW_TAG_typedef
:
8715 case DW_TAG_class_type
:
8716 case DW_TAG_interface_type
:
8717 case DW_TAG_structure_type
:
8718 case DW_TAG_union_type
:
8719 case DW_TAG_enumeration_type
:
8720 case DW_TAG_enumerator
:
8721 case DW_TAG_subprogram
:
8722 case DW_TAG_inlined_subroutine
:
8724 case DW_TAG_imported_declaration
:
8727 case DW_TAG_variable
:
8728 case DW_TAG_constant
:
8729 /* We only need to prefix "globally" visible variables. These include
8730 any variable marked with DW_AT_external or any variable that
8731 lives in a namespace. [Variables in anonymous namespaces
8732 require prefixing, but they are not DW_AT_external.] */
8734 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
8736 struct dwarf2_cu
*spec_cu
= cu
;
8738 return die_needs_namespace (die_specification (die
, &spec_cu
),
8742 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
8743 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
8744 && die
->parent
->tag
!= DW_TAG_module
)
8746 /* A variable in a lexical block of some kind does not need a
8747 namespace, even though in C++ such variables may be external
8748 and have a mangled name. */
8749 if (die
->parent
->tag
== DW_TAG_lexical_block
8750 || die
->parent
->tag
== DW_TAG_try_block
8751 || die
->parent
->tag
== DW_TAG_catch_block
8752 || die
->parent
->tag
== DW_TAG_subprogram
)
8761 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8762 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8763 defined for the given DIE. */
8765 static struct attribute
*
8766 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
8768 struct attribute
*attr
;
8770 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
8772 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8777 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8778 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8779 defined for the given DIE. */
8782 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
8784 const char *linkage_name
;
8786 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
8787 if (linkage_name
== NULL
)
8788 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8790 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8791 See https://github.com/rust-lang/rust/issues/32925. */
8792 if (cu
->per_cu
->lang
== language_rust
&& linkage_name
!= NULL
8793 && strchr (linkage_name
, '{') != NULL
)
8794 linkage_name
= NULL
;
8796 return linkage_name
;
8799 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8800 compute the physname for the object, which include a method's:
8801 - formal parameters (C++),
8802 - receiver type (Go),
8804 The term "physname" is a bit confusing.
8805 For C++, for example, it is the demangled name.
8806 For Go, for example, it's the mangled name.
8808 For Ada, return the DIE's linkage name rather than the fully qualified
8809 name. PHYSNAME is ignored..
8811 The result is allocated on the objfile->per_bfd's obstack and
8815 dwarf2_compute_name (const char *name
,
8816 struct die_info
*die
, struct dwarf2_cu
*cu
,
8819 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
8822 name
= dwarf2_name (die
, cu
);
8824 enum language lang
= cu
->per_cu
->lang
;
8826 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8827 but otherwise compute it by typename_concat inside GDB.
8828 FIXME: Actually this is not really true, or at least not always true.
8829 It's all very confusing. compute_and_set_names doesn't try to demangle
8830 Fortran names because there is no mangling standard. So new_symbol
8831 will set the demangled name to the result of dwarf2_full_name, and it is
8832 the demangled name that GDB uses if it exists. */
8833 if (lang
== language_ada
8834 || (lang
== language_fortran
&& physname
))
8836 /* For Ada unit, we prefer the linkage name over the name, as
8837 the former contains the exported name, which the user expects
8838 to be able to reference. Ideally, we want the user to be able
8839 to reference this entity using either natural or linkage name,
8840 but we haven't started looking at this enhancement yet. */
8841 const char *linkage_name
= dw2_linkage_name (die
, cu
);
8843 if (linkage_name
!= NULL
)
8844 return linkage_name
;
8847 /* These are the only languages we know how to qualify names in. */
8849 && (lang
== language_cplus
8850 || lang
== language_fortran
|| lang
== language_d
8851 || lang
== language_rust
))
8853 if (die_needs_namespace (die
, cu
))
8856 const char *canonical_name
= NULL
;
8860 prefix
= determine_prefix (die
, cu
);
8861 if (*prefix
!= '\0')
8863 gdb::unique_xmalloc_ptr
<char> prefixed_name
8864 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
8866 buf
.puts (prefixed_name
.get ());
8871 /* Template parameters may be specified in the DIE's DW_AT_name, or
8872 as children with DW_TAG_template_type_param or
8873 DW_TAG_value_type_param. If the latter, add them to the name
8874 here. If the name already has template parameters, then
8875 skip this step; some versions of GCC emit both, and
8876 it is more efficient to use the pre-computed name.
8878 Something to keep in mind about this process: it is very
8879 unlikely, or in some cases downright impossible, to produce
8880 something that will match the mangled name of a function.
8881 If the definition of the function has the same debug info,
8882 we should be able to match up with it anyway. But fallbacks
8883 using the minimal symbol, for instance to find a method
8884 implemented in a stripped copy of libstdc++, will not work.
8885 If we do not have debug info for the definition, we will have to
8886 match them up some other way.
8888 When we do name matching there is a related problem with function
8889 templates; two instantiated function templates are allowed to
8890 differ only by their return types, which we do not add here. */
8892 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
8894 struct attribute
*attr
;
8895 struct die_info
*child
;
8898 die
->building_fullname
= 1;
8900 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
8904 const gdb_byte
*bytes
;
8905 struct dwarf2_locexpr_baton
*baton
;
8908 if (child
->tag
!= DW_TAG_template_type_param
8909 && child
->tag
!= DW_TAG_template_value_param
)
8920 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
8923 complaint (_("template parameter missing DW_AT_type"));
8924 buf
.puts ("UNKNOWN_TYPE");
8927 type
= die_type (child
, cu
);
8929 if (child
->tag
== DW_TAG_template_type_param
)
8931 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
8932 &type_print_raw_options
);
8936 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
8939 complaint (_("template parameter missing "
8940 "DW_AT_const_value"));
8941 buf
.puts ("UNKNOWN_VALUE");
8945 dwarf2_const_value_attr (attr
, type
, name
,
8946 &cu
->comp_unit_obstack
, cu
,
8947 &value
, &bytes
, &baton
);
8949 if (type
->has_no_signedness ())
8950 /* GDB prints characters as NUMBER 'CHAR'. If that's
8951 changed, this can use value_print instead. */
8952 cu
->language_defn
->printchar (value
, type
, &buf
);
8955 struct value_print_options opts
;
8958 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
8962 baton
->per_objfile
);
8963 else if (bytes
!= NULL
)
8965 v
= allocate_value (type
);
8966 memcpy (value_contents_writeable (v
).data (), bytes
,
8967 TYPE_LENGTH (type
));
8970 v
= value_from_longest (type
, value
);
8972 /* Specify decimal so that we do not depend on
8974 get_formatted_print_options (&opts
, 'd');
8976 value_print (v
, &buf
, &opts
);
8981 die
->building_fullname
= 0;
8985 /* Close the argument list, with a space if necessary
8986 (nested templates). */
8987 if (!buf
.empty () && buf
.string ().back () == '>')
8994 /* For C++ methods, append formal parameter type
8995 information, if PHYSNAME. */
8997 if (physname
&& die
->tag
== DW_TAG_subprogram
8998 && lang
== language_cplus
)
9000 struct type
*type
= read_type_die (die
, cu
);
9002 c_type_print_args (type
, &buf
, 1, lang
,
9003 &type_print_raw_options
);
9005 if (lang
== language_cplus
)
9007 /* Assume that an artificial first parameter is
9008 "this", but do not crash if it is not. RealView
9009 marks unnamed (and thus unused) parameters as
9010 artificial; there is no way to differentiate
9012 if (type
->num_fields () > 0
9013 && TYPE_FIELD_ARTIFICIAL (type
, 0)
9014 && type
->field (0).type ()->code () == TYPE_CODE_PTR
9015 && TYPE_CONST (TYPE_TARGET_TYPE (type
->field (0).type ())))
9016 buf
.puts (" const");
9020 const std::string
&intermediate_name
= buf
.string ();
9022 if (lang
== language_cplus
)
9024 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
9027 /* If we only computed INTERMEDIATE_NAME, or if
9028 INTERMEDIATE_NAME is already canonical, then we need to
9030 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
9031 name
= objfile
->intern (intermediate_name
);
9033 name
= canonical_name
;
9040 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9041 If scope qualifiers are appropriate they will be added. The result
9042 will be allocated on the storage_obstack, or NULL if the DIE does
9043 not have a name. NAME may either be from a previous call to
9044 dwarf2_name or NULL.
9046 The output string will be canonicalized (if C++). */
9049 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9051 return dwarf2_compute_name (name
, die
, cu
, 0);
9054 /* Construct a physname for the given DIE in CU. NAME may either be
9055 from a previous call to dwarf2_name or NULL. The result will be
9056 allocated on the objfile_objstack or NULL if the DIE does not have a
9059 The output string will be canonicalized (if C++). */
9062 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9064 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9065 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
9068 /* In this case dwarf2_compute_name is just a shortcut not building anything
9070 if (!die_needs_namespace (die
, cu
))
9071 return dwarf2_compute_name (name
, die
, cu
, 1);
9073 if (cu
->per_cu
->lang
!= language_rust
)
9074 mangled
= dw2_linkage_name (die
, cu
);
9076 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9078 gdb::unique_xmalloc_ptr
<char> demangled
;
9079 if (mangled
!= NULL
)
9081 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
9083 /* Do nothing (do not demangle the symbol name). */
9087 /* Use DMGL_RET_DROP for C++ template functions to suppress
9088 their return type. It is easier for GDB users to search
9089 for such functions as `name(params)' than `long name(params)'.
9090 In such case the minimal symbol names do not match the full
9091 symbol names but for template functions there is never a need
9092 to look up their definition from their declaration so
9093 the only disadvantage remains the minimal symbol variant
9094 `long name(params)' does not have the proper inferior type. */
9095 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
9099 canon
= demangled
.get ();
9107 if (canon
== NULL
|| check_physname
)
9109 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
9111 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
9113 /* It may not mean a bug in GDB. The compiler could also
9114 compute DW_AT_linkage_name incorrectly. But in such case
9115 GDB would need to be bug-to-bug compatible. */
9117 complaint (_("Computed physname <%s> does not match demangled <%s> "
9118 "(from linkage <%s>) - DIE at %s [in module %s]"),
9119 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
9120 objfile_name (objfile
));
9122 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9123 is available here - over computed PHYSNAME. It is safer
9124 against both buggy GDB and buggy compilers. */
9138 retval
= objfile
->intern (retval
);
9143 /* Inspect DIE in CU for a namespace alias. If one exists, record
9144 a new symbol for it.
9146 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9149 read_namespace_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
9151 struct attribute
*attr
;
9153 /* If the die does not have a name, this is not a namespace
9155 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
9159 struct die_info
*d
= die
;
9160 struct dwarf2_cu
*imported_cu
= cu
;
9162 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9163 keep inspecting DIEs until we hit the underlying import. */
9164 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9165 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
9167 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
9171 d
= follow_die_ref (d
, attr
, &imported_cu
);
9172 if (d
->tag
!= DW_TAG_imported_declaration
)
9176 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
9178 complaint (_("DIE at %s has too many recursively imported "
9179 "declarations"), sect_offset_str (d
->sect_off
));
9186 sect_offset sect_off
= attr
->get_ref_die_offset ();
9188 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, cu
->per_objfile
);
9189 if (type
!= NULL
&& type
->code () == TYPE_CODE_NAMESPACE
)
9191 /* This declaration is a global namespace alias. Add
9192 a symbol for it whose type is the aliased namespace. */
9193 new_symbol (die
, type
, cu
);
9202 /* Return the using directives repository (global or local?) to use in the
9203 current context for CU.
9205 For Ada, imported declarations can materialize renamings, which *may* be
9206 global. However it is impossible (for now?) in DWARF to distinguish
9207 "external" imported declarations and "static" ones. As all imported
9208 declarations seem to be static in all other languages, make them all CU-wide
9209 global only in Ada. */
9211 static struct using_direct
**
9212 using_directives (struct dwarf2_cu
*cu
)
9214 if (cu
->per_cu
->lang
== language_ada
9215 && cu
->get_builder ()->outermost_context_p ())
9216 return cu
->get_builder ()->get_global_using_directives ();
9218 return cu
->get_builder ()->get_local_using_directives ();
9221 /* Read the import statement specified by the given die and record it. */
9224 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
9226 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9227 struct attribute
*import_attr
;
9228 struct die_info
*imported_die
, *child_die
;
9229 struct dwarf2_cu
*imported_cu
;
9230 const char *imported_name
;
9231 const char *imported_name_prefix
;
9232 const char *canonical_name
;
9233 const char *import_alias
;
9234 const char *imported_declaration
= NULL
;
9235 const char *import_prefix
;
9236 std::vector
<const char *> excludes
;
9238 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
9239 if (import_attr
== NULL
)
9241 complaint (_("Tag '%s' has no DW_AT_import"),
9242 dwarf_tag_name (die
->tag
));
9247 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
9248 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9249 if (imported_name
== NULL
)
9251 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9253 The import in the following code:
9267 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9268 <52> DW_AT_decl_file : 1
9269 <53> DW_AT_decl_line : 6
9270 <54> DW_AT_import : <0x75>
9271 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9273 <5b> DW_AT_decl_file : 1
9274 <5c> DW_AT_decl_line : 2
9275 <5d> DW_AT_type : <0x6e>
9277 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9278 <76> DW_AT_byte_size : 4
9279 <77> DW_AT_encoding : 5 (signed)
9281 imports the wrong die ( 0x75 instead of 0x58 ).
9282 This case will be ignored until the gcc bug is fixed. */
9286 /* Figure out the local name after import. */
9287 import_alias
= dwarf2_name (die
, cu
);
9289 /* Figure out where the statement is being imported to. */
9290 import_prefix
= determine_prefix (die
, cu
);
9292 /* Figure out what the scope of the imported die is and prepend it
9293 to the name of the imported die. */
9294 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
9296 if (imported_die
->tag
!= DW_TAG_namespace
9297 && imported_die
->tag
!= DW_TAG_module
)
9299 imported_declaration
= imported_name
;
9300 canonical_name
= imported_name_prefix
;
9302 else if (strlen (imported_name_prefix
) > 0)
9303 canonical_name
= obconcat (&objfile
->objfile_obstack
,
9304 imported_name_prefix
,
9305 (cu
->per_cu
->lang
== language_d
9308 imported_name
, (char *) NULL
);
9310 canonical_name
= imported_name
;
9312 if (die
->tag
== DW_TAG_imported_module
9313 && cu
->per_cu
->lang
== language_fortran
)
9314 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
9315 child_die
= child_die
->sibling
)
9317 /* DWARF-4: A Fortran use statement with a “rename list” may be
9318 represented by an imported module entry with an import attribute
9319 referring to the module and owned entries corresponding to those
9320 entities that are renamed as part of being imported. */
9322 if (child_die
->tag
!= DW_TAG_imported_declaration
)
9324 complaint (_("child DW_TAG_imported_declaration expected "
9325 "- DIE at %s [in module %s]"),
9326 sect_offset_str (child_die
->sect_off
),
9327 objfile_name (objfile
));
9331 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
9332 if (import_attr
== NULL
)
9334 complaint (_("Tag '%s' has no DW_AT_import"),
9335 dwarf_tag_name (child_die
->tag
));
9340 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
9342 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9343 if (imported_name
== NULL
)
9345 complaint (_("child DW_TAG_imported_declaration has unknown "
9346 "imported name - DIE at %s [in module %s]"),
9347 sect_offset_str (child_die
->sect_off
),
9348 objfile_name (objfile
));
9352 excludes
.push_back (imported_name
);
9354 process_die (child_die
, cu
);
9357 add_using_directive (using_directives (cu
),
9361 imported_declaration
,
9364 &objfile
->objfile_obstack
);
9367 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9368 types, but gives them a size of zero. Starting with version 14,
9369 ICC is compatible with GCC. */
9372 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
9374 if (!cu
->checked_producer
)
9375 check_producer (cu
);
9377 return cu
->producer_is_icc_lt_14
;
9380 /* ICC generates a DW_AT_type for C void functions. This was observed on
9381 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9382 which says that void functions should not have a DW_AT_type. */
9385 producer_is_icc (struct dwarf2_cu
*cu
)
9387 if (!cu
->checked_producer
)
9388 check_producer (cu
);
9390 return cu
->producer_is_icc
;
9393 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9394 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9395 this, it was first present in GCC release 4.3.0. */
9398 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
9400 if (!cu
->checked_producer
)
9401 check_producer (cu
);
9403 return cu
->producer_is_gcc_lt_4_3
;
9406 static file_and_directory
&
9407 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
9409 if (cu
->per_cu
->fnd
!= nullptr)
9410 return *cu
->per_cu
->fnd
;
9412 /* Find the filename. Do not use dwarf2_name here, since the filename
9413 is not a source language identifier. */
9414 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
9415 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
9417 if (res
.get_comp_dir () == nullptr
9418 && producer_is_gcc_lt_4_3 (cu
)
9419 && res
.get_name () != nullptr
9420 && IS_ABSOLUTE_PATH (res
.get_name ()))
9421 res
.set_comp_dir (ldirname (res
.get_name ()));
9423 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
9424 return *cu
->per_cu
->fnd
;
9427 /* Handle DW_AT_stmt_list for a compilation unit.
9428 DIE is the DW_TAG_compile_unit die for CU.
9429 COMP_DIR is the compilation directory. LOWPC is passed to
9430 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9433 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
9434 CORE_ADDR lowpc
) /* ARI: editCase function */
9436 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9437 struct attribute
*attr
;
9438 struct line_header line_header_local
;
9439 hashval_t line_header_local_hash
;
9443 gdb_assert (! cu
->per_cu
->is_debug_types
);
9445 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
9446 if (attr
== NULL
|| !attr
->form_is_unsigned ())
9449 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9451 /* The line header hash table is only created if needed (it exists to
9452 prevent redundant reading of the line table for partial_units).
9453 If we're given a partial_unit, we'll need it. If we're given a
9454 compile_unit, then use the line header hash table if it's already
9455 created, but don't create one just yet. */
9457 if (per_objfile
->line_header_hash
== NULL
9458 && die
->tag
== DW_TAG_partial_unit
)
9460 per_objfile
->line_header_hash
9461 .reset (htab_create_alloc (127, line_header_hash_voidp
,
9462 line_header_eq_voidp
,
9463 htab_delete_entry
<line_header
>,
9467 line_header_local
.sect_off
= line_offset
;
9468 line_header_local
.offset_in_dwz
= cu
->per_cu
->is_dwz
;
9469 line_header_local_hash
= line_header_hash (&line_header_local
);
9470 if (per_objfile
->line_header_hash
!= NULL
)
9472 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9474 line_header_local_hash
, NO_INSERT
);
9476 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9477 is not present in *SLOT (since if there is something in *SLOT then
9478 it will be for a partial_unit). */
9479 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
9481 gdb_assert (*slot
!= NULL
);
9482 cu
->line_header
= (struct line_header
*) *slot
;
9487 /* dwarf_decode_line_header does not yet provide sufficient information.
9488 We always have to call also dwarf_decode_lines for it. */
9489 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
);
9493 cu
->line_header
= lh
.release ();
9494 cu
->line_header_die_owner
= die
;
9496 if (per_objfile
->line_header_hash
== NULL
)
9500 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9502 line_header_local_hash
, INSERT
);
9503 gdb_assert (slot
!= NULL
);
9505 if (slot
!= NULL
&& *slot
== NULL
)
9507 /* This newly decoded line number information unit will be owned
9508 by line_header_hash hash table. */
9509 *slot
= cu
->line_header
;
9510 cu
->line_header_die_owner
= NULL
;
9514 /* We cannot free any current entry in (*slot) as that struct line_header
9515 may be already used by multiple CUs. Create only temporary decoded
9516 line_header for this CU - it may happen at most once for each line
9517 number information unit. And if we're not using line_header_hash
9518 then this is what we want as well. */
9519 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
9521 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
9522 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
);
9525 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9528 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9530 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9531 struct objfile
*objfile
= per_objfile
->objfile
;
9532 struct gdbarch
*gdbarch
= objfile
->arch ();
9533 CORE_ADDR lowpc
= ((CORE_ADDR
) -1);
9534 CORE_ADDR highpc
= ((CORE_ADDR
) 0);
9535 struct attribute
*attr
;
9536 struct die_info
*child_die
;
9539 prepare_one_comp_unit (cu
, die
, cu
->per_cu
->lang
);
9540 baseaddr
= objfile
->text_section_offset ();
9542 get_scope_pc_bounds (die
, &lowpc
, &highpc
, cu
);
9544 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9545 from finish_block. */
9546 if (lowpc
== ((CORE_ADDR
) -1))
9548 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
9550 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
9552 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
9555 gdb_assert (per_objfile
->sym_cu
== nullptr);
9556 scoped_restore restore_sym_cu
9557 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
9559 /* Decode line number information if present. We do this before
9560 processing child DIEs, so that the line header table is available
9561 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9562 HIGHPC are both 0x0, then there won't be any interesting code in
9563 the CU, but a check later on (in
9564 lnp_state_machine::check_line_address) will fail to properly
9565 exclude an entry that was removed via --gc-sections. */
9566 if (lowpc
!= highpc
)
9567 handle_DW_AT_stmt_list (die
, cu
, lowpc
);
9569 /* Process all dies in compilation unit. */
9570 if (die
->child
!= NULL
)
9572 child_die
= die
->child
;
9573 while (child_die
&& child_die
->tag
)
9575 process_die (child_die
, cu
);
9576 child_die
= child_die
->sibling
;
9579 per_objfile
->sym_cu
= nullptr;
9581 /* Decode macro information, if present. Dwarf 2 macro information
9582 refers to information in the line number info statement program
9583 header, so we can only read it if we've read the header
9585 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
9587 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
9588 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9590 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
9591 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9593 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
9597 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
9598 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9600 unsigned int macro_offset
= attr
->as_unsigned ();
9602 dwarf_decode_macros (cu
, macro_offset
, 0);
9608 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
9610 struct type_unit_group
*tu_group
;
9612 struct attribute
*attr
;
9614 struct signatured_type
*sig_type
;
9616 gdb_assert (per_cu
->is_debug_types
);
9617 sig_type
= (struct signatured_type
*) per_cu
;
9619 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
9621 /* If we're using .gdb_index (includes -readnow) then
9622 per_cu->type_unit_group may not have been set up yet. */
9623 if (sig_type
->type_unit_group
== NULL
)
9624 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
9625 tu_group
= sig_type
->type_unit_group
;
9627 /* If we've already processed this stmt_list there's no real need to
9628 do it again, we could fake it and just recreate the part we need
9629 (file name,index -> symtab mapping). If data shows this optimization
9630 is useful we can do it then. */
9631 type_unit_group_unshareable
*tug_unshare
9632 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
9633 first_time
= tug_unshare
->compunit_symtab
== NULL
;
9635 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9638 if (attr
!= NULL
&& attr
->form_is_unsigned ())
9640 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9641 lh
= dwarf_decode_line_header (line_offset
, this);
9646 start_compunit_symtab ("", NULL
, 0);
9649 gdb_assert (tug_unshare
->symtabs
== NULL
);
9650 gdb_assert (m_builder
== nullptr);
9651 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9652 m_builder
.reset (new struct buildsym_compunit
9653 (cust
->objfile (), "",
9655 compunit_language (cust
),
9657 list_in_scope
= get_builder ()->get_file_symbols ();
9662 line_header
= lh
.release ();
9663 line_header_die_owner
= die
;
9667 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
9669 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9670 still initializing it, and our caller (a few levels up)
9671 process_full_type_unit still needs to know if this is the first
9674 tug_unshare
->symtabs
9675 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
9676 struct symtab
*, line_header
->file_names_size ());
9678 auto &file_names
= line_header
->file_names ();
9679 for (i
= 0; i
< file_names
.size (); ++i
)
9681 file_entry
&fe
= file_names
[i
];
9682 dwarf2_start_subfile (this, fe
.name
,
9683 fe
.include_dir (line_header
));
9684 buildsym_compunit
*b
= get_builder ();
9685 if (b
->get_current_subfile ()->symtab
== NULL
)
9687 /* NOTE: start_subfile will recognize when it's been
9688 passed a file it has already seen. So we can't
9689 assume there's a simple mapping from
9690 cu->line_header->file_names to subfiles, plus
9691 cu->line_header->file_names may contain dups. */
9692 const char *name
= b
->get_current_subfile ()->name
.c_str ();
9693 b
->get_current_subfile ()->symtab
= allocate_symtab (cust
, name
);
9696 fe
.symtab
= b
->get_current_subfile ()->symtab
;
9697 tug_unshare
->symtabs
[i
] = fe
.symtab
;
9702 gdb_assert (m_builder
== nullptr);
9703 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9704 m_builder
.reset (new struct buildsym_compunit
9705 (cust
->objfile (), "",
9707 compunit_language (cust
),
9709 list_in_scope
= get_builder ()->get_file_symbols ();
9711 auto &file_names
= line_header
->file_names ();
9712 for (i
= 0; i
< file_names
.size (); ++i
)
9714 file_entry
&fe
= file_names
[i
];
9715 fe
.symtab
= tug_unshare
->symtabs
[i
];
9719 /* The main symtab is allocated last. Type units don't have DW_AT_name
9720 so they don't have a "real" (so to speak) symtab anyway.
9721 There is later code that will assign the main symtab to all symbols
9722 that don't have one. We need to handle the case of a symbol with a
9723 missing symtab (DW_AT_decl_file) anyway. */
9726 /* Process DW_TAG_type_unit.
9727 For TUs we want to skip the first top level sibling if it's not the
9728 actual type being defined by this TU. In this case the first top
9729 level sibling is there to provide context only. */
9732 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9734 struct die_info
*child_die
;
9736 prepare_one_comp_unit (cu
, die
, language_minimal
);
9738 /* Initialize (or reinitialize) the machinery for building symtabs.
9739 We do this before processing child DIEs, so that the line header table
9740 is available for DW_AT_decl_file. */
9741 cu
->setup_type_unit_groups (die
);
9743 if (die
->child
!= NULL
)
9745 child_die
= die
->child
;
9746 while (child_die
&& child_die
->tag
)
9748 process_die (child_die
, cu
);
9749 child_die
= child_die
->sibling
;
9756 http://gcc.gnu.org/wiki/DebugFission
9757 http://gcc.gnu.org/wiki/DebugFissionDWP
9759 To simplify handling of both DWO files ("object" files with the DWARF info)
9760 and DWP files (a file with the DWOs packaged up into one file), we treat
9761 DWP files as having a collection of virtual DWO files. */
9764 hash_dwo_file (const void *item
)
9766 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
9769 hash
= htab_hash_string (dwo_file
->dwo_name
);
9770 if (dwo_file
->comp_dir
!= NULL
)
9771 hash
+= htab_hash_string (dwo_file
->comp_dir
);
9776 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
9778 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
9779 const struct dwo_file
*rhs
= (const struct dwo_file
*) item_rhs
;
9781 if (strcmp (lhs
->dwo_name
, rhs
->dwo_name
) != 0)
9783 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
9784 return lhs
->comp_dir
== rhs
->comp_dir
;
9785 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
9788 /* Allocate a hash table for DWO files. */
9791 allocate_dwo_file_hash_table ()
9793 return htab_up (htab_create_alloc (41,
9796 htab_delete_entry
<dwo_file
>,
9800 /* Lookup DWO file DWO_NAME. */
9803 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
9804 const char *dwo_name
,
9805 const char *comp_dir
)
9807 struct dwo_file find_entry
;
9810 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
9811 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
9813 find_entry
.dwo_name
= dwo_name
;
9814 find_entry
.comp_dir
= comp_dir
;
9815 slot
= htab_find_slot (per_objfile
->per_bfd
->dwo_files
.get (), &find_entry
,
9822 hash_dwo_unit (const void *item
)
9824 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9826 /* This drops the top 32 bits of the id, but is ok for a hash. */
9827 return dwo_unit
->signature
;
9831 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
9833 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
9834 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
9836 /* The signature is assumed to be unique within the DWO file.
9837 So while object file CU dwo_id's always have the value zero,
9838 that's OK, assuming each object file DWO file has only one CU,
9839 and that's the rule for now. */
9840 return lhs
->signature
== rhs
->signature
;
9843 /* Allocate a hash table for DWO CUs,TUs.
9844 There is one of these tables for each of CUs,TUs for each DWO file. */
9847 allocate_dwo_unit_table ()
9849 /* Start out with a pretty small number.
9850 Generally DWO files contain only one CU and maybe some TUs. */
9851 return htab_up (htab_create_alloc (3,
9854 NULL
, xcalloc
, xfree
));
9857 /* die_reader_func for create_dwo_cu. */
9860 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
9861 const gdb_byte
*info_ptr
,
9862 struct die_info
*comp_unit_die
,
9863 struct dwo_file
*dwo_file
,
9864 struct dwo_unit
*dwo_unit
)
9866 struct dwarf2_cu
*cu
= reader
->cu
;
9867 sect_offset sect_off
= cu
->per_cu
->sect_off
;
9868 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
9870 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
9871 if (!signature
.has_value ())
9873 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9874 " its dwo_id [in module %s]"),
9875 sect_offset_str (sect_off
), dwo_file
->dwo_name
);
9879 dwo_unit
->dwo_file
= dwo_file
;
9880 dwo_unit
->signature
= *signature
;
9881 dwo_unit
->section
= section
;
9882 dwo_unit
->sect_off
= sect_off
;
9883 dwo_unit
->length
= cu
->per_cu
->length
;
9885 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9886 sect_offset_str (sect_off
),
9887 hex_string (dwo_unit
->signature
));
9890 /* Create the dwo_units for the CUs in a DWO_FILE.
9891 Note: This function processes DWO files only, not DWP files. */
9894 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
9895 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
9896 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
9898 struct objfile
*objfile
= per_objfile
->objfile
;
9899 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
9900 const gdb_byte
*info_ptr
, *end_ptr
;
9902 section
.read (objfile
);
9903 info_ptr
= section
.buffer
;
9905 if (info_ptr
== NULL
)
9908 dwarf_read_debug_printf ("Reading %s for %s:",
9909 section
.get_name (),
9910 section
.get_file_name ());
9912 end_ptr
= info_ptr
+ section
.size
;
9913 while (info_ptr
< end_ptr
)
9915 struct dwarf2_per_cu_data per_cu
;
9916 struct dwo_unit read_unit
{};
9917 struct dwo_unit
*dwo_unit
;
9919 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
9921 per_cu
.per_bfd
= per_bfd
;
9922 per_cu
.is_debug_types
= 0;
9923 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
9924 per_cu
.section
= §ion
;
9926 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
9927 if (!reader
.dummy_p
)
9928 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
9929 &dwo_file
, &read_unit
);
9930 info_ptr
+= per_cu
.length
;
9932 // If the unit could not be parsed, skip it.
9933 if (read_unit
.dwo_file
== NULL
)
9936 if (cus_htab
== NULL
)
9937 cus_htab
= allocate_dwo_unit_table ();
9939 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
9941 *dwo_unit
= read_unit
;
9942 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
9943 gdb_assert (slot
!= NULL
);
9946 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
9947 sect_offset dup_sect_off
= dup_cu
->sect_off
;
9949 complaint (_("debug cu entry at offset %s is duplicate to"
9950 " the entry at offset %s, signature %s"),
9951 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
9952 hex_string (dwo_unit
->signature
));
9954 *slot
= (void *)dwo_unit
;
9958 /* DWP file .debug_{cu,tu}_index section format:
9959 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9960 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
9962 DWP Versions 1 & 2 are older, pre-standard format versions. The first
9963 officially standard DWP format was published with DWARF v5 and is called
9964 Version 5. There are no versions 3 or 4.
9968 Both index sections have the same format, and serve to map a 64-bit
9969 signature to a set of section numbers. Each section begins with a header,
9970 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9971 indexes, and a pool of 32-bit section numbers. The index sections will be
9972 aligned at 8-byte boundaries in the file.
9974 The index section header consists of:
9976 V, 32 bit version number
9978 N, 32 bit number of compilation units or type units in the index
9979 M, 32 bit number of slots in the hash table
9981 Numbers are recorded using the byte order of the application binary.
9983 The hash table begins at offset 16 in the section, and consists of an array
9984 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9985 order of the application binary). Unused slots in the hash table are 0.
9986 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9988 The parallel table begins immediately after the hash table
9989 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9990 array of 32-bit indexes (using the byte order of the application binary),
9991 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9992 table contains a 32-bit index into the pool of section numbers. For unused
9993 hash table slots, the corresponding entry in the parallel table will be 0.
9995 The pool of section numbers begins immediately following the hash table
9996 (at offset 16 + 12 * M from the beginning of the section). The pool of
9997 section numbers consists of an array of 32-bit words (using the byte order
9998 of the application binary). Each item in the array is indexed starting
9999 from 0. The hash table entry provides the index of the first section
10000 number in the set. Additional section numbers in the set follow, and the
10001 set is terminated by a 0 entry (section number 0 is not used in ELF).
10003 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
10004 section must be the first entry in the set, and the .debug_abbrev.dwo must
10005 be the second entry. Other members of the set may follow in any order.
10009 DWP Versions 2 and 5:
10011 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10012 and the entries in the index tables are now offsets into these sections.
10013 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10016 Index Section Contents:
10018 Hash Table of Signatures dwp_hash_table.hash_table
10019 Parallel Table of Indices dwp_hash_table.unit_table
10020 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10021 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10023 The index section header consists of:
10025 V, 32 bit version number
10026 L, 32 bit number of columns in the table of section offsets
10027 N, 32 bit number of compilation units or type units in the index
10028 M, 32 bit number of slots in the hash table
10030 Numbers are recorded using the byte order of the application binary.
10032 The hash table has the same format as version 1.
10033 The parallel table of indices has the same format as version 1,
10034 except that the entries are origin-1 indices into the table of sections
10035 offsets and the table of section sizes.
10037 The table of offsets begins immediately following the parallel table
10038 (at offset 16 + 12 * M from the beginning of the section). The table is
10039 a two-dimensional array of 32-bit words (using the byte order of the
10040 application binary), with L columns and N+1 rows, in row-major order.
10041 Each row in the array is indexed starting from 0. The first row provides
10042 a key to the remaining rows: each column in this row provides an identifier
10043 for a debug section, and the offsets in the same column of subsequent rows
10044 refer to that section. The section identifiers for Version 2 are:
10046 DW_SECT_INFO 1 .debug_info.dwo
10047 DW_SECT_TYPES 2 .debug_types.dwo
10048 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10049 DW_SECT_LINE 4 .debug_line.dwo
10050 DW_SECT_LOC 5 .debug_loc.dwo
10051 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10052 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10053 DW_SECT_MACRO 8 .debug_macro.dwo
10055 The section identifiers for Version 5 are:
10057 DW_SECT_INFO_V5 1 .debug_info.dwo
10058 DW_SECT_RESERVED_V5 2 --
10059 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10060 DW_SECT_LINE_V5 4 .debug_line.dwo
10061 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10062 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10063 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10064 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10066 The offsets provided by the CU and TU index sections are the base offsets
10067 for the contributions made by each CU or TU to the corresponding section
10068 in the package file. Each CU and TU header contains an abbrev_offset
10069 field, used to find the abbreviations table for that CU or TU within the
10070 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10071 be interpreted as relative to the base offset given in the index section.
10072 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10073 should be interpreted as relative to the base offset for .debug_line.dwo,
10074 and offsets into other debug sections obtained from DWARF attributes should
10075 also be interpreted as relative to the corresponding base offset.
10077 The table of sizes begins immediately following the table of offsets.
10078 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10079 with L columns and N rows, in row-major order. Each row in the array is
10080 indexed starting from 1 (row 0 is shared by the two tables).
10084 Hash table lookup is handled the same in version 1 and 2:
10086 We assume that N and M will not exceed 2^32 - 1.
10087 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10089 Given a 64-bit compilation unit signature or a type signature S, an entry
10090 in the hash table is located as follows:
10092 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10093 the low-order k bits all set to 1.
10095 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10097 3) If the hash table entry at index H matches the signature, use that
10098 entry. If the hash table entry at index H is unused (all zeroes),
10099 terminate the search: the signature is not present in the table.
10101 4) Let H = (H + H') modulo M. Repeat at Step 3.
10103 Because M > N and H' and M are relatively prime, the search is guaranteed
10104 to stop at an unused slot or find the match. */
10106 /* Create a hash table to map DWO IDs to their CU/TU entry in
10107 .debug_{info,types}.dwo in DWP_FILE.
10108 Returns NULL if there isn't one.
10109 Note: This function processes DWP files only, not DWO files. */
10111 static struct dwp_hash_table
*
10112 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
10113 struct dwp_file
*dwp_file
, int is_debug_types
)
10115 struct objfile
*objfile
= per_objfile
->objfile
;
10116 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10117 const gdb_byte
*index_ptr
, *index_end
;
10118 struct dwarf2_section_info
*index
;
10119 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
10120 struct dwp_hash_table
*htab
;
10122 if (is_debug_types
)
10123 index
= &dwp_file
->sections
.tu_index
;
10125 index
= &dwp_file
->sections
.cu_index
;
10127 if (index
->empty ())
10129 index
->read (objfile
);
10131 index_ptr
= index
->buffer
;
10132 index_end
= index_ptr
+ index
->size
;
10134 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10135 For now it's safe to just read 4 bytes (particularly as it's difficult to
10136 tell if you're dealing with Version 5 before you've read the version). */
10137 version
= read_4_bytes (dbfd
, index_ptr
);
10139 if (version
== 2 || version
== 5)
10140 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
10144 nr_units
= read_4_bytes (dbfd
, index_ptr
);
10146 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
10149 if (version
!= 1 && version
!= 2 && version
!= 5)
10151 error (_("Dwarf Error: unsupported DWP file version (%s)"
10152 " [in module %s]"),
10153 pulongest (version
), dwp_file
->name
);
10155 if (nr_slots
!= (nr_slots
& -nr_slots
))
10157 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10158 " is not power of 2 [in module %s]"),
10159 pulongest (nr_slots
), dwp_file
->name
);
10162 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
10163 htab
->version
= version
;
10164 htab
->nr_columns
= nr_columns
;
10165 htab
->nr_units
= nr_units
;
10166 htab
->nr_slots
= nr_slots
;
10167 htab
->hash_table
= index_ptr
;
10168 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
10170 /* Exit early if the table is empty. */
10171 if (nr_slots
== 0 || nr_units
== 0
10172 || (version
== 2 && nr_columns
== 0)
10173 || (version
== 5 && nr_columns
== 0))
10175 /* All must be zero. */
10176 if (nr_slots
!= 0 || nr_units
!= 0
10177 || (version
== 2 && nr_columns
!= 0)
10178 || (version
== 5 && nr_columns
!= 0))
10180 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10181 " all zero [in modules %s]"),
10189 htab
->section_pool
.v1
.indices
=
10190 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10191 /* It's harder to decide whether the section is too small in v1.
10192 V1 is deprecated anyway so we punt. */
10194 else if (version
== 2)
10196 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10197 int *ids
= htab
->section_pool
.v2
.section_ids
;
10198 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
10199 /* Reverse map for error checking. */
10200 int ids_seen
[DW_SECT_MAX
+ 1];
10203 if (nr_columns
< 2)
10205 error (_("Dwarf Error: bad DWP hash table, too few columns"
10206 " in section table [in module %s]"),
10209 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
10211 error (_("Dwarf Error: bad DWP hash table, too many columns"
10212 " in section table [in module %s]"),
10215 memset (ids
, 255, sizeof_ids
);
10216 memset (ids_seen
, 255, sizeof (ids_seen
));
10217 for (i
= 0; i
< nr_columns
; ++i
)
10219 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10221 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
10223 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10224 " in section table [in module %s]"),
10225 id
, dwp_file
->name
);
10227 if (ids_seen
[id
] != -1)
10229 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10230 " id %d in section table [in module %s]"),
10231 id
, dwp_file
->name
);
10236 /* Must have exactly one info or types section. */
10237 if (((ids_seen
[DW_SECT_INFO
] != -1)
10238 + (ids_seen
[DW_SECT_TYPES
] != -1))
10241 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10242 " DWO info/types section [in module %s]"),
10245 /* Must have an abbrev section. */
10246 if (ids_seen
[DW_SECT_ABBREV
] == -1)
10248 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10249 " section [in module %s]"),
10252 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10253 htab
->section_pool
.v2
.sizes
=
10254 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
10255 * nr_units
* nr_columns
);
10256 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
10257 * nr_units
* nr_columns
))
10260 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10261 " [in module %s]"),
10265 else /* version == 5 */
10267 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10268 int *ids
= htab
->section_pool
.v5
.section_ids
;
10269 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
10270 /* Reverse map for error checking. */
10271 int ids_seen
[DW_SECT_MAX_V5
+ 1];
10273 if (nr_columns
< 2)
10275 error (_("Dwarf Error: bad DWP hash table, too few columns"
10276 " in section table [in module %s]"),
10279 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
10281 error (_("Dwarf Error: bad DWP hash table, too many columns"
10282 " in section table [in module %s]"),
10285 memset (ids
, 255, sizeof_ids
);
10286 memset (ids_seen
, 255, sizeof (ids_seen
));
10287 for (int i
= 0; i
< nr_columns
; ++i
)
10289 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10291 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
10293 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10294 " in section table [in module %s]"),
10295 id
, dwp_file
->name
);
10297 if (ids_seen
[id
] != -1)
10299 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10300 " id %d in section table [in module %s]"),
10301 id
, dwp_file
->name
);
10306 /* Must have seen an info section. */
10307 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
10309 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10310 " DWO info/types section [in module %s]"),
10313 /* Must have an abbrev section. */
10314 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
10316 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10317 " section [in module %s]"),
10320 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10321 htab
->section_pool
.v5
.sizes
10322 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
10323 * nr_units
* nr_columns
);
10324 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
10325 * nr_units
* nr_columns
))
10328 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10329 " [in module %s]"),
10337 /* Update SECTIONS with the data from SECTP.
10339 This function is like the other "locate" section routines, but in
10340 this context the sections to read comes from the DWP V1 hash table,
10341 not the full ELF section table.
10343 The result is non-zero for success, or zero if an error was found. */
10346 locate_v1_virtual_dwo_sections (asection
*sectp
,
10347 struct virtual_v1_dwo_sections
*sections
)
10349 const struct dwop_section_names
*names
= &dwop_section_names
;
10351 if (names
->abbrev_dwo
.matches (sectp
->name
))
10353 /* There can be only one. */
10354 if (sections
->abbrev
.s
.section
!= NULL
)
10356 sections
->abbrev
.s
.section
= sectp
;
10357 sections
->abbrev
.size
= bfd_section_size (sectp
);
10359 else if (names
->info_dwo
.matches (sectp
->name
)
10360 || names
->types_dwo
.matches (sectp
->name
))
10362 /* There can be only one. */
10363 if (sections
->info_or_types
.s
.section
!= NULL
)
10365 sections
->info_or_types
.s
.section
= sectp
;
10366 sections
->info_or_types
.size
= bfd_section_size (sectp
);
10368 else if (names
->line_dwo
.matches (sectp
->name
))
10370 /* There can be only one. */
10371 if (sections
->line
.s
.section
!= NULL
)
10373 sections
->line
.s
.section
= sectp
;
10374 sections
->line
.size
= bfd_section_size (sectp
);
10376 else if (names
->loc_dwo
.matches (sectp
->name
))
10378 /* There can be only one. */
10379 if (sections
->loc
.s
.section
!= NULL
)
10381 sections
->loc
.s
.section
= sectp
;
10382 sections
->loc
.size
= bfd_section_size (sectp
);
10384 else if (names
->macinfo_dwo
.matches (sectp
->name
))
10386 /* There can be only one. */
10387 if (sections
->macinfo
.s
.section
!= NULL
)
10389 sections
->macinfo
.s
.section
= sectp
;
10390 sections
->macinfo
.size
= bfd_section_size (sectp
);
10392 else if (names
->macro_dwo
.matches (sectp
->name
))
10394 /* There can be only one. */
10395 if (sections
->macro
.s
.section
!= NULL
)
10397 sections
->macro
.s
.section
= sectp
;
10398 sections
->macro
.size
= bfd_section_size (sectp
);
10400 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
10402 /* There can be only one. */
10403 if (sections
->str_offsets
.s
.section
!= NULL
)
10405 sections
->str_offsets
.s
.section
= sectp
;
10406 sections
->str_offsets
.size
= bfd_section_size (sectp
);
10410 /* No other kind of section is valid. */
10417 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10418 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10419 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10420 This is for DWP version 1 files. */
10422 static struct dwo_unit
*
10423 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
10424 struct dwp_file
*dwp_file
,
10425 uint32_t unit_index
,
10426 const char *comp_dir
,
10427 ULONGEST signature
, int is_debug_types
)
10429 const struct dwp_hash_table
*dwp_htab
=
10430 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10431 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10432 const char *kind
= is_debug_types
? "TU" : "CU";
10433 struct dwo_file
*dwo_file
;
10434 struct dwo_unit
*dwo_unit
;
10435 struct virtual_v1_dwo_sections sections
;
10436 void **dwo_file_slot
;
10439 gdb_assert (dwp_file
->version
== 1);
10441 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10442 kind
, pulongest (unit_index
), hex_string (signature
),
10445 /* Fetch the sections of this DWO unit.
10446 Put a limit on the number of sections we look for so that bad data
10447 doesn't cause us to loop forever. */
10449 #define MAX_NR_V1_DWO_SECTIONS \
10450 (1 /* .debug_info or .debug_types */ \
10451 + 1 /* .debug_abbrev */ \
10452 + 1 /* .debug_line */ \
10453 + 1 /* .debug_loc */ \
10454 + 1 /* .debug_str_offsets */ \
10455 + 1 /* .debug_macro or .debug_macinfo */ \
10456 + 1 /* trailing zero */)
10458 memset (§ions
, 0, sizeof (sections
));
10460 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
10463 uint32_t section_nr
=
10464 read_4_bytes (dbfd
,
10465 dwp_htab
->section_pool
.v1
.indices
10466 + (unit_index
+ i
) * sizeof (uint32_t));
10468 if (section_nr
== 0)
10470 if (section_nr
>= dwp_file
->num_sections
)
10472 error (_("Dwarf Error: bad DWP hash table, section number too large"
10473 " [in module %s]"),
10477 sectp
= dwp_file
->elf_sections
[section_nr
];
10478 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
10480 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10481 " [in module %s]"),
10487 || sections
.info_or_types
.empty ()
10488 || sections
.abbrev
.empty ())
10490 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10491 " [in module %s]"),
10494 if (i
== MAX_NR_V1_DWO_SECTIONS
)
10496 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10497 " [in module %s]"),
10501 /* It's easier for the rest of the code if we fake a struct dwo_file and
10502 have dwo_unit "live" in that. At least for now.
10504 The DWP file can be made up of a random collection of CUs and TUs.
10505 However, for each CU + set of TUs that came from the same original DWO
10506 file, we can combine them back into a virtual DWO file to save space
10507 (fewer struct dwo_file objects to allocate). Remember that for really
10508 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10510 std::string virtual_dwo_name
=
10511 string_printf ("virtual-dwo/%d-%d-%d-%d",
10512 sections
.abbrev
.get_id (),
10513 sections
.line
.get_id (),
10514 sections
.loc
.get_id (),
10515 sections
.str_offsets
.get_id ());
10516 /* Can we use an existing virtual DWO file? */
10517 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10519 /* Create one if necessary. */
10520 if (*dwo_file_slot
== NULL
)
10522 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10523 virtual_dwo_name
.c_str ());
10525 dwo_file
= new struct dwo_file
;
10526 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10527 dwo_file
->comp_dir
= comp_dir
;
10528 dwo_file
->sections
.abbrev
= sections
.abbrev
;
10529 dwo_file
->sections
.line
= sections
.line
;
10530 dwo_file
->sections
.loc
= sections
.loc
;
10531 dwo_file
->sections
.macinfo
= sections
.macinfo
;
10532 dwo_file
->sections
.macro
= sections
.macro
;
10533 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
10534 /* The "str" section is global to the entire DWP file. */
10535 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10536 /* The info or types section is assigned below to dwo_unit,
10537 there's no need to record it in dwo_file.
10538 Also, we can't simply record type sections in dwo_file because
10539 we record a pointer into the vector in dwo_unit. As we collect more
10540 types we'll grow the vector and eventually have to reallocate space
10541 for it, invalidating all copies of pointers into the previous
10543 *dwo_file_slot
= dwo_file
;
10547 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10548 virtual_dwo_name
.c_str ());
10550 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10553 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10554 dwo_unit
->dwo_file
= dwo_file
;
10555 dwo_unit
->signature
= signature
;
10556 dwo_unit
->section
=
10557 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10558 *dwo_unit
->section
= sections
.info_or_types
;
10559 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10564 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10565 simplify them. Given a pointer to the containing section SECTION, and
10566 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10567 virtual section of just that piece. */
10569 static struct dwarf2_section_info
10570 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
10571 struct dwarf2_section_info
*section
,
10572 bfd_size_type offset
, bfd_size_type size
)
10574 struct dwarf2_section_info result
;
10577 gdb_assert (section
!= NULL
);
10578 gdb_assert (!section
->is_virtual
);
10580 memset (&result
, 0, sizeof (result
));
10581 result
.s
.containing_section
= section
;
10582 result
.is_virtual
= true;
10587 sectp
= section
->get_bfd_section ();
10589 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10590 bounds of the real section. This is a pretty-rare event, so just
10591 flag an error (easier) instead of a warning and trying to cope. */
10593 || offset
+ size
> bfd_section_size (sectp
))
10595 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10596 " in section %s [in module %s]"),
10597 sectp
? bfd_section_name (sectp
) : "<unknown>",
10598 objfile_name (per_objfile
->objfile
));
10601 result
.virtual_offset
= offset
;
10602 result
.size
= size
;
10606 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10607 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10608 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10609 This is for DWP version 2 files. */
10611 static struct dwo_unit
*
10612 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
10613 struct dwp_file
*dwp_file
,
10614 uint32_t unit_index
,
10615 const char *comp_dir
,
10616 ULONGEST signature
, int is_debug_types
)
10618 const struct dwp_hash_table
*dwp_htab
=
10619 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10620 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10621 const char *kind
= is_debug_types
? "TU" : "CU";
10622 struct dwo_file
*dwo_file
;
10623 struct dwo_unit
*dwo_unit
;
10624 struct virtual_v2_or_v5_dwo_sections sections
;
10625 void **dwo_file_slot
;
10628 gdb_assert (dwp_file
->version
== 2);
10630 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10631 kind
, pulongest (unit_index
), hex_string (signature
),
10634 /* Fetch the section offsets of this DWO unit. */
10636 memset (§ions
, 0, sizeof (sections
));
10638 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10640 uint32_t offset
= read_4_bytes (dbfd
,
10641 dwp_htab
->section_pool
.v2
.offsets
10642 + (((unit_index
- 1) * dwp_htab
->nr_columns
10644 * sizeof (uint32_t)));
10645 uint32_t size
= read_4_bytes (dbfd
,
10646 dwp_htab
->section_pool
.v2
.sizes
10647 + (((unit_index
- 1) * dwp_htab
->nr_columns
10649 * sizeof (uint32_t)));
10651 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
10654 case DW_SECT_TYPES
:
10655 sections
.info_or_types_offset
= offset
;
10656 sections
.info_or_types_size
= size
;
10658 case DW_SECT_ABBREV
:
10659 sections
.abbrev_offset
= offset
;
10660 sections
.abbrev_size
= size
;
10663 sections
.line_offset
= offset
;
10664 sections
.line_size
= size
;
10667 sections
.loc_offset
= offset
;
10668 sections
.loc_size
= size
;
10670 case DW_SECT_STR_OFFSETS
:
10671 sections
.str_offsets_offset
= offset
;
10672 sections
.str_offsets_size
= size
;
10674 case DW_SECT_MACINFO
:
10675 sections
.macinfo_offset
= offset
;
10676 sections
.macinfo_size
= size
;
10678 case DW_SECT_MACRO
:
10679 sections
.macro_offset
= offset
;
10680 sections
.macro_size
= size
;
10685 /* It's easier for the rest of the code if we fake a struct dwo_file and
10686 have dwo_unit "live" in that. At least for now.
10688 The DWP file can be made up of a random collection of CUs and TUs.
10689 However, for each CU + set of TUs that came from the same original DWO
10690 file, we can combine them back into a virtual DWO file to save space
10691 (fewer struct dwo_file objects to allocate). Remember that for really
10692 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10694 std::string virtual_dwo_name
=
10695 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10696 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10697 (long) (sections
.line_size
? sections
.line_offset
: 0),
10698 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
10699 (long) (sections
.str_offsets_size
10700 ? sections
.str_offsets_offset
: 0));
10701 /* Can we use an existing virtual DWO file? */
10702 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10704 /* Create one if necessary. */
10705 if (*dwo_file_slot
== NULL
)
10707 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10708 virtual_dwo_name
.c_str ());
10710 dwo_file
= new struct dwo_file
;
10711 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10712 dwo_file
->comp_dir
= comp_dir
;
10713 dwo_file
->sections
.abbrev
=
10714 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
10715 sections
.abbrev_offset
,
10716 sections
.abbrev_size
);
10717 dwo_file
->sections
.line
=
10718 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
10719 sections
.line_offset
,
10720 sections
.line_size
);
10721 dwo_file
->sections
.loc
=
10722 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
10723 sections
.loc_offset
, sections
.loc_size
);
10724 dwo_file
->sections
.macinfo
=
10725 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
10726 sections
.macinfo_offset
,
10727 sections
.macinfo_size
);
10728 dwo_file
->sections
.macro
=
10729 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
10730 sections
.macro_offset
,
10731 sections
.macro_size
);
10732 dwo_file
->sections
.str_offsets
=
10733 create_dwp_v2_or_v5_section (per_objfile
,
10734 &dwp_file
->sections
.str_offsets
,
10735 sections
.str_offsets_offset
,
10736 sections
.str_offsets_size
);
10737 /* The "str" section is global to the entire DWP file. */
10738 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10739 /* The info or types section is assigned below to dwo_unit,
10740 there's no need to record it in dwo_file.
10741 Also, we can't simply record type sections in dwo_file because
10742 we record a pointer into the vector in dwo_unit. As we collect more
10743 types we'll grow the vector and eventually have to reallocate space
10744 for it, invalidating all copies of pointers into the previous
10746 *dwo_file_slot
= dwo_file
;
10750 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10751 virtual_dwo_name
.c_str ());
10753 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10756 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10757 dwo_unit
->dwo_file
= dwo_file
;
10758 dwo_unit
->signature
= signature
;
10759 dwo_unit
->section
=
10760 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10761 *dwo_unit
->section
= create_dwp_v2_or_v5_section
10764 ? &dwp_file
->sections
.types
10765 : &dwp_file
->sections
.info
,
10766 sections
.info_or_types_offset
,
10767 sections
.info_or_types_size
);
10768 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10773 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10774 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10775 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10776 This is for DWP version 5 files. */
10778 static struct dwo_unit
*
10779 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
10780 struct dwp_file
*dwp_file
,
10781 uint32_t unit_index
,
10782 const char *comp_dir
,
10783 ULONGEST signature
, int is_debug_types
)
10785 const struct dwp_hash_table
*dwp_htab
10786 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10787 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10788 const char *kind
= is_debug_types
? "TU" : "CU";
10789 struct dwo_file
*dwo_file
;
10790 struct dwo_unit
*dwo_unit
;
10791 struct virtual_v2_or_v5_dwo_sections sections
{};
10792 void **dwo_file_slot
;
10794 gdb_assert (dwp_file
->version
== 5);
10796 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10797 kind
, pulongest (unit_index
), hex_string (signature
),
10800 /* Fetch the section offsets of this DWO unit. */
10802 /* memset (§ions, 0, sizeof (sections)); */
10804 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10806 uint32_t offset
= read_4_bytes (dbfd
,
10807 dwp_htab
->section_pool
.v5
.offsets
10808 + (((unit_index
- 1)
10809 * dwp_htab
->nr_columns
10811 * sizeof (uint32_t)));
10812 uint32_t size
= read_4_bytes (dbfd
,
10813 dwp_htab
->section_pool
.v5
.sizes
10814 + (((unit_index
- 1) * dwp_htab
->nr_columns
10816 * sizeof (uint32_t)));
10818 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
10820 case DW_SECT_ABBREV_V5
:
10821 sections
.abbrev_offset
= offset
;
10822 sections
.abbrev_size
= size
;
10824 case DW_SECT_INFO_V5
:
10825 sections
.info_or_types_offset
= offset
;
10826 sections
.info_or_types_size
= size
;
10828 case DW_SECT_LINE_V5
:
10829 sections
.line_offset
= offset
;
10830 sections
.line_size
= size
;
10832 case DW_SECT_LOCLISTS_V5
:
10833 sections
.loclists_offset
= offset
;
10834 sections
.loclists_size
= size
;
10836 case DW_SECT_MACRO_V5
:
10837 sections
.macro_offset
= offset
;
10838 sections
.macro_size
= size
;
10840 case DW_SECT_RNGLISTS_V5
:
10841 sections
.rnglists_offset
= offset
;
10842 sections
.rnglists_size
= size
;
10844 case DW_SECT_STR_OFFSETS_V5
:
10845 sections
.str_offsets_offset
= offset
;
10846 sections
.str_offsets_size
= size
;
10848 case DW_SECT_RESERVED_V5
:
10854 /* It's easier for the rest of the code if we fake a struct dwo_file and
10855 have dwo_unit "live" in that. At least for now.
10857 The DWP file can be made up of a random collection of CUs and TUs.
10858 However, for each CU + set of TUs that came from the same original DWO
10859 file, we can combine them back into a virtual DWO file to save space
10860 (fewer struct dwo_file objects to allocate). Remember that for really
10861 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10863 std::string virtual_dwo_name
=
10864 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10865 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10866 (long) (sections
.line_size
? sections
.line_offset
: 0),
10867 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
10868 (long) (sections
.str_offsets_size
10869 ? sections
.str_offsets_offset
: 0),
10870 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
10871 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
10872 /* Can we use an existing virtual DWO file? */
10873 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
10874 virtual_dwo_name
.c_str (),
10876 /* Create one if necessary. */
10877 if (*dwo_file_slot
== NULL
)
10879 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10880 virtual_dwo_name
.c_str ());
10882 dwo_file
= new struct dwo_file
;
10883 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10884 dwo_file
->comp_dir
= comp_dir
;
10885 dwo_file
->sections
.abbrev
=
10886 create_dwp_v2_or_v5_section (per_objfile
,
10887 &dwp_file
->sections
.abbrev
,
10888 sections
.abbrev_offset
,
10889 sections
.abbrev_size
);
10890 dwo_file
->sections
.line
=
10891 create_dwp_v2_or_v5_section (per_objfile
,
10892 &dwp_file
->sections
.line
,
10893 sections
.line_offset
, sections
.line_size
);
10894 dwo_file
->sections
.macro
=
10895 create_dwp_v2_or_v5_section (per_objfile
,
10896 &dwp_file
->sections
.macro
,
10897 sections
.macro_offset
,
10898 sections
.macro_size
);
10899 dwo_file
->sections
.loclists
=
10900 create_dwp_v2_or_v5_section (per_objfile
,
10901 &dwp_file
->sections
.loclists
,
10902 sections
.loclists_offset
,
10903 sections
.loclists_size
);
10904 dwo_file
->sections
.rnglists
=
10905 create_dwp_v2_or_v5_section (per_objfile
,
10906 &dwp_file
->sections
.rnglists
,
10907 sections
.rnglists_offset
,
10908 sections
.rnglists_size
);
10909 dwo_file
->sections
.str_offsets
=
10910 create_dwp_v2_or_v5_section (per_objfile
,
10911 &dwp_file
->sections
.str_offsets
,
10912 sections
.str_offsets_offset
,
10913 sections
.str_offsets_size
);
10914 /* The "str" section is global to the entire DWP file. */
10915 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10916 /* The info or types section is assigned below to dwo_unit,
10917 there's no need to record it in dwo_file.
10918 Also, we can't simply record type sections in dwo_file because
10919 we record a pointer into the vector in dwo_unit. As we collect more
10920 types we'll grow the vector and eventually have to reallocate space
10921 for it, invalidating all copies of pointers into the previous
10923 *dwo_file_slot
= dwo_file
;
10927 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10928 virtual_dwo_name
.c_str ());
10930 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10933 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10934 dwo_unit
->dwo_file
= dwo_file
;
10935 dwo_unit
->signature
= signature
;
10937 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10938 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
10939 &dwp_file
->sections
.info
,
10940 sections
.info_or_types_offset
,
10941 sections
.info_or_types_size
);
10942 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10947 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10948 Returns NULL if the signature isn't found. */
10950 static struct dwo_unit
*
10951 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
10952 struct dwp_file
*dwp_file
, const char *comp_dir
,
10953 ULONGEST signature
, int is_debug_types
)
10955 const struct dwp_hash_table
*dwp_htab
=
10956 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10957 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10958 uint32_t mask
= dwp_htab
->nr_slots
- 1;
10959 uint32_t hash
= signature
& mask
;
10960 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
10963 struct dwo_unit find_dwo_cu
;
10965 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
10966 find_dwo_cu
.signature
= signature
;
10967 slot
= htab_find_slot (is_debug_types
10968 ? dwp_file
->loaded_tus
.get ()
10969 : dwp_file
->loaded_cus
.get (),
10970 &find_dwo_cu
, INSERT
);
10973 return (struct dwo_unit
*) *slot
;
10975 /* Use a for loop so that we don't loop forever on bad debug info. */
10976 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
10978 ULONGEST signature_in_table
;
10980 signature_in_table
=
10981 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
10982 if (signature_in_table
== signature
)
10984 uint32_t unit_index
=
10985 read_4_bytes (dbfd
,
10986 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
10988 if (dwp_file
->version
== 1)
10990 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
10991 unit_index
, comp_dir
,
10992 signature
, is_debug_types
);
10994 else if (dwp_file
->version
== 2)
10996 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
10997 unit_index
, comp_dir
,
10998 signature
, is_debug_types
);
11000 else /* version == 5 */
11002 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
11003 unit_index
, comp_dir
,
11004 signature
, is_debug_types
);
11006 return (struct dwo_unit
*) *slot
;
11008 if (signature_in_table
== 0)
11010 hash
= (hash
+ hash2
) & mask
;
11013 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11014 " [in module %s]"),
11018 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11019 Open the file specified by FILE_NAME and hand it off to BFD for
11020 preliminary analysis. Return a newly initialized bfd *, which
11021 includes a canonicalized copy of FILE_NAME.
11022 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11023 SEARCH_CWD is true if the current directory is to be searched.
11024 It will be searched before debug-file-directory.
11025 If successful, the file is added to the bfd include table of the
11026 objfile's bfd (see gdb_bfd_record_inclusion).
11027 If unable to find/open the file, return NULL.
11028 NOTE: This function is derived from symfile_bfd_open. */
11030 static gdb_bfd_ref_ptr
11031 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
11032 const char *file_name
, int is_dwp
, int search_cwd
)
11035 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11036 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11037 to debug_file_directory. */
11038 const char *search_path
;
11039 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
11041 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
11044 if (!debug_file_directory
.empty ())
11046 search_path_holder
.reset (concat (".", dirname_separator_string
,
11047 debug_file_directory
.c_str (),
11049 search_path
= search_path_holder
.get ();
11055 search_path
= debug_file_directory
.c_str ();
11057 /* Add the path for the executable binary to the list of search paths. */
11058 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
11059 search_path_holder
.reset (concat (objfile_dir
.c_str (),
11060 dirname_separator_string
,
11061 search_path
, nullptr));
11062 search_path
= search_path_holder
.get ();
11064 openp_flags flags
= OPF_RETURN_REALPATH
;
11066 flags
|= OPF_SEARCH_IN_PATH
;
11068 gdb::unique_xmalloc_ptr
<char> absolute_name
;
11069 desc
= openp (search_path
, flags
, file_name
,
11070 O_RDONLY
| O_BINARY
, &absolute_name
);
11074 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
11076 if (sym_bfd
== NULL
)
11078 bfd_set_cacheable (sym_bfd
.get (), 1);
11080 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
11083 /* Success. Record the bfd as having been included by the objfile's bfd.
11084 This is important because things like demangled_names_hash lives in the
11085 objfile's per_bfd space and may have references to things like symbol
11086 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11087 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
, sym_bfd
.get ());
11092 /* Try to open DWO file FILE_NAME.
11093 COMP_DIR is the DW_AT_comp_dir attribute.
11094 The result is the bfd handle of the file.
11095 If there is a problem finding or opening the file, return NULL.
11096 Upon success, the canonicalized path of the file is stored in the bfd,
11097 same as symfile_bfd_open. */
11099 static gdb_bfd_ref_ptr
11100 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
11101 const char *file_name
, const char *comp_dir
)
11103 if (IS_ABSOLUTE_PATH (file_name
))
11104 return try_open_dwop_file (per_objfile
, file_name
,
11105 0 /*is_dwp*/, 0 /*search_cwd*/);
11107 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11109 if (comp_dir
!= NULL
)
11111 std::string path_to_try
= path_join (comp_dir
, file_name
);
11113 /* NOTE: If comp_dir is a relative path, this will also try the
11114 search path, which seems useful. */
11115 gdb_bfd_ref_ptr
abfd (try_open_dwop_file
11116 (per_objfile
, path_to_try
.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11122 /* That didn't work, try debug-file-directory, which, despite its name,
11123 is a list of paths. */
11125 if (debug_file_directory
.empty ())
11128 return try_open_dwop_file (per_objfile
, file_name
,
11129 0 /*is_dwp*/, 1 /*search_cwd*/);
11132 /* This function is mapped across the sections and remembers the offset and
11133 size of each of the DWO debugging sections we are interested in. */
11136 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
11137 asection
*sectp
, dwo_sections
*dwo_sections
)
11139 const struct dwop_section_names
*names
= &dwop_section_names
;
11141 struct dwarf2_section_info
*dw_sect
= nullptr;
11143 if (names
->abbrev_dwo
.matches (sectp
->name
))
11144 dw_sect
= &dwo_sections
->abbrev
;
11145 else if (names
->info_dwo
.matches (sectp
->name
))
11146 dw_sect
= &dwo_sections
->info
;
11147 else if (names
->line_dwo
.matches (sectp
->name
))
11148 dw_sect
= &dwo_sections
->line
;
11149 else if (names
->loc_dwo
.matches (sectp
->name
))
11150 dw_sect
= &dwo_sections
->loc
;
11151 else if (names
->loclists_dwo
.matches (sectp
->name
))
11152 dw_sect
= &dwo_sections
->loclists
;
11153 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11154 dw_sect
= &dwo_sections
->macinfo
;
11155 else if (names
->macro_dwo
.matches (sectp
->name
))
11156 dw_sect
= &dwo_sections
->macro
;
11157 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11158 dw_sect
= &dwo_sections
->rnglists
;
11159 else if (names
->str_dwo
.matches (sectp
->name
))
11160 dw_sect
= &dwo_sections
->str
;
11161 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11162 dw_sect
= &dwo_sections
->str_offsets
;
11163 else if (names
->types_dwo
.matches (sectp
->name
))
11165 struct dwarf2_section_info type_section
;
11167 memset (&type_section
, 0, sizeof (type_section
));
11168 dwo_sections
->types
.push_back (type_section
);
11169 dw_sect
= &dwo_sections
->types
.back ();
11172 if (dw_sect
!= nullptr)
11174 dw_sect
->s
.section
= sectp
;
11175 dw_sect
->size
= bfd_section_size (sectp
);
11176 dw_sect
->read (objfile
);
11180 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11181 by PER_CU. This is for the non-DWP case.
11182 The result is NULL if DWO_NAME can't be found. */
11184 static struct dwo_file
*
11185 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
11186 const char *comp_dir
)
11188 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11190 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
11193 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
11198 dwo_file_up
dwo_file (new struct dwo_file
);
11199 dwo_file
->dwo_name
= dwo_name
;
11200 dwo_file
->comp_dir
= comp_dir
;
11201 dwo_file
->dbfd
= std::move (dbfd
);
11203 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
11204 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
11205 sec
, &dwo_file
->sections
);
11207 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
11210 if (cu
->per_cu
->dwarf_version
< 5)
11212 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
11213 dwo_file
->sections
.types
, dwo_file
->tus
);
11217 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
11218 &dwo_file
->sections
.info
, dwo_file
->tus
,
11219 rcuh_kind::COMPILE
);
11222 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
11224 return dwo_file
.release ();
11227 /* This function is mapped across the sections and remembers the offset and
11228 size of each of the DWP debugging sections common to version 1 and 2 that
11229 we are interested in. */
11232 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11233 asection
*sectp
, dwp_file
*dwp_file
)
11235 const struct dwop_section_names
*names
= &dwop_section_names
;
11236 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11238 /* Record the ELF section number for later lookup: this is what the
11239 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11240 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11241 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11243 /* Look for specific sections that we need. */
11244 struct dwarf2_section_info
*dw_sect
= nullptr;
11245 if (names
->str_dwo
.matches (sectp
->name
))
11246 dw_sect
= &dwp_file
->sections
.str
;
11247 else if (names
->cu_index
.matches (sectp
->name
))
11248 dw_sect
= &dwp_file
->sections
.cu_index
;
11249 else if (names
->tu_index
.matches (sectp
->name
))
11250 dw_sect
= &dwp_file
->sections
.tu_index
;
11252 if (dw_sect
!= nullptr)
11254 dw_sect
->s
.section
= sectp
;
11255 dw_sect
->size
= bfd_section_size (sectp
);
11256 dw_sect
->read (objfile
);
11260 /* This function is mapped across the sections and remembers the offset and
11261 size of each of the DWP version 2 debugging sections that we are interested
11262 in. This is split into a separate function because we don't know if we
11263 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11266 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11267 asection
*sectp
, void *dwp_file_ptr
)
11269 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11270 const struct dwop_section_names
*names
= &dwop_section_names
;
11271 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11273 /* Record the ELF section number for later lookup: this is what the
11274 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11275 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11276 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11278 /* Look for specific sections that we need. */
11279 struct dwarf2_section_info
*dw_sect
= nullptr;
11280 if (names
->abbrev_dwo
.matches (sectp
->name
))
11281 dw_sect
= &dwp_file
->sections
.abbrev
;
11282 else if (names
->info_dwo
.matches (sectp
->name
))
11283 dw_sect
= &dwp_file
->sections
.info
;
11284 else if (names
->line_dwo
.matches (sectp
->name
))
11285 dw_sect
= &dwp_file
->sections
.line
;
11286 else if (names
->loc_dwo
.matches (sectp
->name
))
11287 dw_sect
= &dwp_file
->sections
.loc
;
11288 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11289 dw_sect
= &dwp_file
->sections
.macinfo
;
11290 else if (names
->macro_dwo
.matches (sectp
->name
))
11291 dw_sect
= &dwp_file
->sections
.macro
;
11292 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11293 dw_sect
= &dwp_file
->sections
.str_offsets
;
11294 else if (names
->types_dwo
.matches (sectp
->name
))
11295 dw_sect
= &dwp_file
->sections
.types
;
11297 if (dw_sect
!= nullptr)
11299 dw_sect
->s
.section
= sectp
;
11300 dw_sect
->size
= bfd_section_size (sectp
);
11301 dw_sect
->read (objfile
);
11305 /* This function is mapped across the sections and remembers the offset and
11306 size of each of the DWP version 5 debugging sections that we are interested
11307 in. This is split into a separate function because we don't know if we
11308 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11311 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11312 asection
*sectp
, void *dwp_file_ptr
)
11314 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11315 const struct dwop_section_names
*names
= &dwop_section_names
;
11316 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11318 /* Record the ELF section number for later lookup: this is what the
11319 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11320 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11321 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11323 /* Look for specific sections that we need. */
11324 struct dwarf2_section_info
*dw_sect
= nullptr;
11325 if (names
->abbrev_dwo
.matches (sectp
->name
))
11326 dw_sect
= &dwp_file
->sections
.abbrev
;
11327 else if (names
->info_dwo
.matches (sectp
->name
))
11328 dw_sect
= &dwp_file
->sections
.info
;
11329 else if (names
->line_dwo
.matches (sectp
->name
))
11330 dw_sect
= &dwp_file
->sections
.line
;
11331 else if (names
->loclists_dwo
.matches (sectp
->name
))
11332 dw_sect
= &dwp_file
->sections
.loclists
;
11333 else if (names
->macro_dwo
.matches (sectp
->name
))
11334 dw_sect
= &dwp_file
->sections
.macro
;
11335 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11336 dw_sect
= &dwp_file
->sections
.rnglists
;
11337 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11338 dw_sect
= &dwp_file
->sections
.str_offsets
;
11340 if (dw_sect
!= nullptr)
11342 dw_sect
->s
.section
= sectp
;
11343 dw_sect
->size
= bfd_section_size (sectp
);
11344 dw_sect
->read (objfile
);
11348 /* Hash function for dwp_file loaded CUs/TUs. */
11351 hash_dwp_loaded_cutus (const void *item
)
11353 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
11355 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11356 return dwo_unit
->signature
;
11359 /* Equality function for dwp_file loaded CUs/TUs. */
11362 eq_dwp_loaded_cutus (const void *a
, const void *b
)
11364 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
11365 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
11367 return dua
->signature
== dub
->signature
;
11370 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11373 allocate_dwp_loaded_cutus_table ()
11375 return htab_up (htab_create_alloc (3,
11376 hash_dwp_loaded_cutus
,
11377 eq_dwp_loaded_cutus
,
11378 NULL
, xcalloc
, xfree
));
11381 /* Try to open DWP file FILE_NAME.
11382 The result is the bfd handle of the file.
11383 If there is a problem finding or opening the file, return NULL.
11384 Upon success, the canonicalized path of the file is stored in the bfd,
11385 same as symfile_bfd_open. */
11387 static gdb_bfd_ref_ptr
11388 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
11390 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
11392 1 /*search_cwd*/));
11396 /* Work around upstream bug 15652.
11397 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11398 [Whether that's a "bug" is debatable, but it is getting in our way.]
11399 We have no real idea where the dwp file is, because gdb's realpath-ing
11400 of the executable's path may have discarded the needed info.
11401 [IWBN if the dwp file name was recorded in the executable, akin to
11402 .gnu_debuglink, but that doesn't exist yet.]
11403 Strip the directory from FILE_NAME and search again. */
11404 if (!debug_file_directory
.empty ())
11406 /* Don't implicitly search the current directory here.
11407 If the user wants to search "." to handle this case,
11408 it must be added to debug-file-directory. */
11409 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
11417 /* Initialize the use of the DWP file for the current objfile.
11418 By convention the name of the DWP file is ${objfile}.dwp.
11419 The result is NULL if it can't be found. */
11421 static std::unique_ptr
<struct dwp_file
>
11422 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
11424 struct objfile
*objfile
= per_objfile
->objfile
;
11426 /* Try to find first .dwp for the binary file before any symbolic links
11429 /* If the objfile is a debug file, find the name of the real binary
11430 file and get the name of dwp file from there. */
11431 std::string dwp_name
;
11432 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
11434 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
11435 const char *backlink_basename
= lbasename (backlink
->original_name
);
11437 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
11440 dwp_name
= objfile
->original_name
;
11442 dwp_name
+= ".dwp";
11444 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
11446 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
11448 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11449 dwp_name
= objfile_name (objfile
);
11450 dwp_name
+= ".dwp";
11451 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
11456 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
11458 return std::unique_ptr
<dwp_file
> ();
11461 const char *name
= bfd_get_filename (dbfd
.get ());
11462 std::unique_ptr
<struct dwp_file
> dwp_file
11463 (new struct dwp_file (name
, std::move (dbfd
)));
11465 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
11466 dwp_file
->elf_sections
=
11467 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
11468 dwp_file
->num_sections
, asection
*);
11470 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11471 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11474 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
11476 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
11478 /* The DWP file version is stored in the hash table. Oh well. */
11479 if (dwp_file
->cus
&& dwp_file
->tus
11480 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
11482 /* Technically speaking, we should try to limp along, but this is
11483 pretty bizarre. We use pulongest here because that's the established
11484 portability solution (e.g, we cannot use %u for uint32_t). */
11485 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11486 " TU version %s [in DWP file %s]"),
11487 pulongest (dwp_file
->cus
->version
),
11488 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
11492 dwp_file
->version
= dwp_file
->cus
->version
;
11493 else if (dwp_file
->tus
)
11494 dwp_file
->version
= dwp_file
->tus
->version
;
11496 dwp_file
->version
= 2;
11498 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11500 if (dwp_file
->version
== 2)
11501 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11504 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11508 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
11509 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
11511 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
11512 dwarf_read_debug_printf (" %s CUs, %s TUs",
11513 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
11514 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
11519 /* Wrapper around open_and_init_dwp_file, only open it once. */
11521 static struct dwp_file
*
11522 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
11524 if (!per_objfile
->per_bfd
->dwp_checked
)
11526 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
11527 per_objfile
->per_bfd
->dwp_checked
= 1;
11529 return per_objfile
->per_bfd
->dwp_file
.get ();
11532 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11533 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11534 or in the DWP file for the objfile, referenced by THIS_UNIT.
11535 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11536 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11538 This is called, for example, when wanting to read a variable with a
11539 complex location. Therefore we don't want to do file i/o for every call.
11540 Therefore we don't want to look for a DWO file on every call.
11541 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11542 then we check if we've already seen DWO_NAME, and only THEN do we check
11545 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11546 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11548 static struct dwo_unit
*
11549 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11550 ULONGEST signature
, int is_debug_types
)
11552 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11553 struct objfile
*objfile
= per_objfile
->objfile
;
11554 const char *kind
= is_debug_types
? "TU" : "CU";
11555 void **dwo_file_slot
;
11556 struct dwo_file
*dwo_file
;
11557 struct dwp_file
*dwp_file
;
11559 /* First see if there's a DWP file.
11560 If we have a DWP file but didn't find the DWO inside it, don't
11561 look for the original DWO file. It makes gdb behave differently
11562 depending on whether one is debugging in the build tree. */
11564 dwp_file
= get_dwp_file (per_objfile
);
11565 if (dwp_file
!= NULL
)
11567 const struct dwp_hash_table
*dwp_htab
=
11568 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11570 if (dwp_htab
!= NULL
)
11572 struct dwo_unit
*dwo_cutu
=
11573 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
11576 if (dwo_cutu
!= NULL
)
11578 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11579 kind
, hex_string (signature
),
11580 host_address_to_string (dwo_cutu
));
11588 /* No DWP file, look for the DWO file. */
11590 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
11591 if (*dwo_file_slot
== NULL
)
11593 /* Read in the file and build a table of the CUs/TUs it contains. */
11594 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
11596 /* NOTE: This will be NULL if unable to open the file. */
11597 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
11599 if (dwo_file
!= NULL
)
11601 struct dwo_unit
*dwo_cutu
= NULL
;
11603 if (is_debug_types
&& dwo_file
->tus
)
11605 struct dwo_unit find_dwo_cutu
;
11607 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11608 find_dwo_cutu
.signature
= signature
;
11610 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
11613 else if (!is_debug_types
&& dwo_file
->cus
)
11615 struct dwo_unit find_dwo_cutu
;
11617 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11618 find_dwo_cutu
.signature
= signature
;
11619 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
11623 if (dwo_cutu
!= NULL
)
11625 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11626 kind
, dwo_name
, hex_string (signature
),
11627 host_address_to_string (dwo_cutu
));
11634 /* We didn't find it. This could mean a dwo_id mismatch, or
11635 someone deleted the DWO/DWP file, or the search path isn't set up
11636 correctly to find the file. */
11638 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11639 kind
, dwo_name
, hex_string (signature
));
11641 /* This is a warning and not a complaint because it can be caused by
11642 pilot error (e.g., user accidentally deleting the DWO). */
11644 /* Print the name of the DWP file if we looked there, helps the user
11645 better diagnose the problem. */
11646 std::string dwp_text
;
11648 if (dwp_file
!= NULL
)
11649 dwp_text
= string_printf (" [in DWP file %s]",
11650 lbasename (dwp_file
->name
));
11652 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11653 " [in module %s]"),
11654 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
11655 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
11660 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11661 See lookup_dwo_cutu_unit for details. */
11663 static struct dwo_unit
*
11664 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11665 ULONGEST signature
)
11667 gdb_assert (!cu
->per_cu
->is_debug_types
);
11669 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
11672 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11673 See lookup_dwo_cutu_unit for details. */
11675 static struct dwo_unit
*
11676 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
11678 gdb_assert (cu
->per_cu
->is_debug_types
);
11680 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
11682 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
11685 /* Traversal function for queue_and_load_all_dwo_tus. */
11688 queue_and_load_dwo_tu (void **slot
, void *info
)
11690 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
11691 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
11692 ULONGEST signature
= dwo_unit
->signature
;
11693 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
11695 if (sig_type
!= NULL
)
11697 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11698 a real dependency of PER_CU on SIG_TYPE. That is detected later
11699 while processing PER_CU. */
11700 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
11702 load_full_type_unit (sig_type
, cu
->per_objfile
);
11703 cu
->per_cu
->imported_symtabs_push (sig_type
);
11709 /* Queue all TUs contained in the DWO of CU to be read in.
11710 The DWO may have the only definition of the type, though it may not be
11711 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11712 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11715 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
11717 struct dwo_unit
*dwo_unit
;
11718 struct dwo_file
*dwo_file
;
11720 gdb_assert (cu
!= nullptr);
11721 gdb_assert (!cu
->per_cu
->is_debug_types
);
11722 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
11724 dwo_unit
= cu
->dwo_unit
;
11725 gdb_assert (dwo_unit
!= NULL
);
11727 dwo_file
= dwo_unit
->dwo_file
;
11728 if (dwo_file
->tus
!= NULL
)
11729 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
11732 /* Read in various DIEs. */
11734 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11735 Inherit only the children of the DW_AT_abstract_origin DIE not being
11736 already referenced by DW_AT_abstract_origin from the children of the
11740 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
11742 struct die_info
*child_die
;
11743 sect_offset
*offsetp
;
11744 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11745 struct die_info
*origin_die
;
11746 /* Iterator of the ORIGIN_DIE children. */
11747 struct die_info
*origin_child_die
;
11748 struct attribute
*attr
;
11749 struct dwarf2_cu
*origin_cu
;
11750 struct pending
**origin_previous_list_in_scope
;
11752 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
11756 /* Note that following die references may follow to a die in a
11760 origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
11762 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11764 origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
11765 origin_cu
->list_in_scope
= cu
->list_in_scope
;
11767 if (die
->tag
!= origin_die
->tag
11768 && !(die
->tag
== DW_TAG_inlined_subroutine
11769 && origin_die
->tag
== DW_TAG_subprogram
))
11770 complaint (_("DIE %s and its abstract origin %s have different tags"),
11771 sect_offset_str (die
->sect_off
),
11772 sect_offset_str (origin_die
->sect_off
));
11774 /* Find if the concrete and abstract trees are structurally the
11775 same. This is a shallow traversal and it is not bullet-proof;
11776 the compiler can trick the debugger into believing that the trees
11777 are isomorphic, whereas they actually are not. However, the
11778 likelyhood of this happening is pretty low, and a full-fledged
11779 check would be an overkill. */
11780 bool are_isomorphic
= true;
11781 die_info
*concrete_child
= die
->child
;
11782 die_info
*abstract_child
= origin_die
->child
;
11783 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
11785 if (concrete_child
== nullptr
11786 || abstract_child
== nullptr
11787 || concrete_child
->tag
!= abstract_child
->tag
)
11789 are_isomorphic
= false;
11793 concrete_child
= concrete_child
->sibling
;
11794 abstract_child
= abstract_child
->sibling
;
11797 /* Walk the origin's children in parallel to the concrete children.
11798 This helps match an origin child in case the debug info misses
11799 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11800 origin tree may not have the same tree structure as the concrete
11802 die_info
*corresponding_abstract_child
11803 = are_isomorphic
? origin_die
->child
: nullptr;
11805 std::vector
<sect_offset
> offsets
;
11807 for (child_die
= die
->child
;
11808 child_die
&& child_die
->tag
;
11809 child_die
= child_die
->sibling
)
11811 struct die_info
*child_origin_die
;
11812 struct dwarf2_cu
*child_origin_cu
;
11814 /* We are trying to process concrete instance entries:
11815 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11816 it's not relevant to our analysis here. i.e. detecting DIEs that are
11817 present in the abstract instance but not referenced in the concrete
11819 if (child_die
->tag
== DW_TAG_call_site
11820 || child_die
->tag
== DW_TAG_GNU_call_site
)
11822 if (are_isomorphic
)
11823 corresponding_abstract_child
11824 = corresponding_abstract_child
->sibling
;
11828 /* For each CHILD_DIE, find the corresponding child of
11829 ORIGIN_DIE. If there is more than one layer of
11830 DW_AT_abstract_origin, follow them all; there shouldn't be,
11831 but GCC versions at least through 4.4 generate this (GCC PR
11833 child_origin_die
= child_die
;
11834 child_origin_cu
= cu
;
11837 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
11841 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
11845 /* If missing DW_AT_abstract_origin, try the corresponding child
11846 of the origin. Clang emits such lexical scopes. */
11847 if (child_origin_die
== child_die
11848 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
11850 && child_die
->tag
== DW_TAG_lexical_block
)
11851 child_origin_die
= corresponding_abstract_child
;
11853 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11854 counterpart may exist. */
11855 if (child_origin_die
!= child_die
)
11857 if (child_die
->tag
!= child_origin_die
->tag
11858 && !(child_die
->tag
== DW_TAG_inlined_subroutine
11859 && child_origin_die
->tag
== DW_TAG_subprogram
))
11860 complaint (_("Child DIE %s and its abstract origin %s have "
11862 sect_offset_str (child_die
->sect_off
),
11863 sect_offset_str (child_origin_die
->sect_off
));
11864 if (child_origin_die
->parent
!= origin_die
)
11865 complaint (_("Child DIE %s and its abstract origin %s have "
11866 "different parents"),
11867 sect_offset_str (child_die
->sect_off
),
11868 sect_offset_str (child_origin_die
->sect_off
));
11870 offsets
.push_back (child_origin_die
->sect_off
);
11873 if (are_isomorphic
)
11874 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
11876 std::sort (offsets
.begin (), offsets
.end ());
11877 sect_offset
*offsets_end
= offsets
.data () + offsets
.size ();
11878 for (offsetp
= offsets
.data () + 1; offsetp
< offsets_end
; offsetp
++)
11879 if (offsetp
[-1] == *offsetp
)
11880 complaint (_("Multiple children of DIE %s refer "
11881 "to DIE %s as their abstract origin"),
11882 sect_offset_str (die
->sect_off
), sect_offset_str (*offsetp
));
11884 offsetp
= offsets
.data ();
11885 origin_child_die
= origin_die
->child
;
11886 while (origin_child_die
&& origin_child_die
->tag
)
11888 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11889 while (offsetp
< offsets_end
11890 && *offsetp
< origin_child_die
->sect_off
)
11892 if (offsetp
>= offsets_end
11893 || *offsetp
> origin_child_die
->sect_off
)
11895 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11896 Check whether we're already processing ORIGIN_CHILD_DIE.
11897 This can happen with mutually referenced abstract_origins.
11899 if (!origin_child_die
->in_process
)
11900 process_die (origin_child_die
, origin_cu
);
11902 origin_child_die
= origin_child_die
->sibling
;
11904 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
11906 if (cu
!= origin_cu
)
11907 compute_delayed_physnames (origin_cu
);
11911 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
11913 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11914 struct gdbarch
*gdbarch
= objfile
->arch ();
11915 struct context_stack
*newobj
;
11918 struct die_info
*child_die
;
11919 struct attribute
*attr
, *call_line
, *call_file
;
11921 CORE_ADDR baseaddr
;
11922 struct block
*block
;
11923 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
11924 std::vector
<struct symbol
*> template_args
;
11925 struct template_symbol
*templ_func
= NULL
;
11929 /* If we do not have call site information, we can't show the
11930 caller of this inlined function. That's too confusing, so
11931 only use the scope for local variables. */
11932 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
11933 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
11934 if (call_line
== NULL
|| call_file
== NULL
)
11936 read_lexical_block_scope (die
, cu
);
11941 baseaddr
= objfile
->text_section_offset ();
11943 name
= dwarf2_name (die
, cu
);
11944 if (name
== nullptr)
11945 name
= dw2_linkage_name (die
, cu
);
11947 /* Ignore functions with missing or empty names. These are actually
11948 illegal according to the DWARF standard. */
11951 complaint (_("missing name for subprogram DIE at %s"),
11952 sect_offset_str (die
->sect_off
));
11956 /* Ignore functions with missing or invalid low and high pc attributes. */
11957 if (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr)
11958 <= PC_BOUNDS_INVALID
)
11960 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
11961 if (attr
== nullptr || !attr
->as_boolean ())
11962 complaint (_("cannot get low and high bounds "
11963 "for subprogram DIE at %s"),
11964 sect_offset_str (die
->sect_off
));
11968 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
11969 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
11971 /* If we have any template arguments, then we must allocate a
11972 different sort of symbol. */
11973 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
11975 if (child_die
->tag
== DW_TAG_template_type_param
11976 || child_die
->tag
== DW_TAG_template_value_param
)
11978 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
11979 templ_func
->subclass
= SYMBOL_TEMPLATE
;
11984 gdb_assert (cu
->get_builder () != nullptr);
11985 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
11986 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
,
11987 (struct symbol
*) templ_func
);
11989 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
11990 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
11993 /* If there is a location expression for DW_AT_frame_base, record
11995 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
11996 if (attr
!= nullptr)
11997 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
11999 /* If there is a location for the static link, record it. */
12000 newobj
->static_link
= NULL
;
12001 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
12002 if (attr
!= nullptr)
12004 newobj
->static_link
12005 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
12006 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
12010 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
12012 if (die
->child
!= NULL
)
12014 child_die
= die
->child
;
12015 while (child_die
&& child_die
->tag
)
12017 if (child_die
->tag
== DW_TAG_template_type_param
12018 || child_die
->tag
== DW_TAG_template_value_param
)
12020 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12023 template_args
.push_back (arg
);
12026 process_die (child_die
, cu
);
12027 child_die
= child_die
->sibling
;
12031 inherit_abstract_dies (die
, cu
);
12033 /* If we have a DW_AT_specification, we might need to import using
12034 directives from the context of the specification DIE. See the
12035 comment in determine_prefix. */
12036 if (cu
->per_cu
->lang
== language_cplus
12037 && dwarf2_attr (die
, DW_AT_specification
, cu
))
12039 struct dwarf2_cu
*spec_cu
= cu
;
12040 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
12044 child_die
= spec_die
->child
;
12045 while (child_die
&& child_die
->tag
)
12047 if (child_die
->tag
== DW_TAG_imported_module
)
12048 process_die (child_die
, spec_cu
);
12049 child_die
= child_die
->sibling
;
12052 /* In some cases, GCC generates specification DIEs that
12053 themselves contain DW_AT_specification attributes. */
12054 spec_die
= die_specification (spec_die
, &spec_cu
);
12058 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12059 /* Make a block for the local symbols within. */
12060 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
12061 cstk
.static_link
, lowpc
, highpc
);
12063 /* For C++, set the block's scope. */
12064 if ((cu
->per_cu
->lang
== language_cplus
12065 || cu
->per_cu
->lang
== language_fortran
12066 || cu
->per_cu
->lang
== language_d
12067 || cu
->per_cu
->lang
== language_rust
)
12068 && cu
->processing_has_namespace_info
)
12069 block_set_scope (block
, determine_prefix (die
, cu
),
12070 &objfile
->objfile_obstack
);
12072 /* If we have address ranges, record them. */
12073 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12075 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
12077 /* Attach template arguments to function. */
12078 if (!template_args
.empty ())
12080 gdb_assert (templ_func
!= NULL
);
12082 templ_func
->n_template_arguments
= template_args
.size ();
12083 templ_func
->template_arguments
12084 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
12085 templ_func
->n_template_arguments
);
12086 memcpy (templ_func
->template_arguments
,
12087 template_args
.data (),
12088 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
12090 /* Make sure that the symtab is set on the new symbols. Even
12091 though they don't appear in this symtab directly, other parts
12092 of gdb assume that symbols do, and this is reasonably
12094 for (symbol
*sym
: template_args
)
12095 sym
->set_symtab (templ_func
->symtab ());
12098 /* In C++, we can have functions nested inside functions (e.g., when
12099 a function declares a class that has methods). This means that
12100 when we finish processing a function scope, we may need to go
12101 back to building a containing block's symbol lists. */
12102 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12103 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12105 /* If we've finished processing a top-level function, subsequent
12106 symbols go in the file symbol list. */
12107 if (cu
->get_builder ()->outermost_context_p ())
12108 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
12111 /* Process all the DIES contained within a lexical block scope. Start
12112 a new scope, process the dies, and then close the scope. */
12115 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12117 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12118 struct gdbarch
*gdbarch
= objfile
->arch ();
12119 CORE_ADDR lowpc
, highpc
;
12120 struct die_info
*child_die
;
12121 CORE_ADDR baseaddr
;
12123 baseaddr
= objfile
->text_section_offset ();
12125 /* Ignore blocks with missing or invalid low and high pc attributes. */
12126 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12127 as multiple lexical blocks? Handling children in a sane way would
12128 be nasty. Might be easier to properly extend generic blocks to
12129 describe ranges. */
12130 switch (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr))
12132 case PC_BOUNDS_NOT_PRESENT
:
12133 /* DW_TAG_lexical_block has no attributes, process its children as if
12134 there was no wrapping by that DW_TAG_lexical_block.
12135 GCC does no longer produces such DWARF since GCC r224161. */
12136 for (child_die
= die
->child
;
12137 child_die
!= NULL
&& child_die
->tag
;
12138 child_die
= child_die
->sibling
)
12140 /* We might already be processing this DIE. This can happen
12141 in an unusual circumstance -- where a subroutine A
12142 appears lexically in another subroutine B, but A actually
12143 inlines B. The recursion is broken here, rather than in
12144 inherit_abstract_dies, because it seems better to simply
12145 drop concrete children here. */
12146 if (!child_die
->in_process
)
12147 process_die (child_die
, cu
);
12150 case PC_BOUNDS_INVALID
:
12153 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12154 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12156 cu
->get_builder ()->push_context (0, lowpc
);
12157 if (die
->child
!= NULL
)
12159 child_die
= die
->child
;
12160 while (child_die
&& child_die
->tag
)
12162 process_die (child_die
, cu
);
12163 child_die
= child_die
->sibling
;
12166 inherit_abstract_dies (die
, cu
);
12167 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12169 if (*cu
->get_builder ()->get_local_symbols () != NULL
12170 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
12172 struct block
*block
12173 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
12174 cstk
.start_addr
, highpc
);
12176 /* Note that recording ranges after traversing children, as we
12177 do here, means that recording a parent's ranges entails
12178 walking across all its children's ranges as they appear in
12179 the address map, which is quadratic behavior.
12181 It would be nicer to record the parent's ranges before
12182 traversing its children, simply overriding whatever you find
12183 there. But since we don't even decide whether to create a
12184 block until after we've traversed its children, that's hard
12186 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12188 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12189 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12192 static void dwarf2_ranges_read_low_addrs (unsigned offset
,
12193 struct dwarf2_cu
*cu
,
12195 std::vector
<CORE_ADDR
> &result
);
12197 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12200 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12202 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12203 struct objfile
*objfile
= per_objfile
->objfile
;
12204 struct gdbarch
*gdbarch
= objfile
->arch ();
12205 CORE_ADDR pc
, baseaddr
;
12206 struct attribute
*attr
;
12209 struct die_info
*child_die
;
12211 baseaddr
= objfile
->text_section_offset ();
12213 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
12216 /* This was a pre-DWARF-5 GNU extension alias
12217 for DW_AT_call_return_pc. */
12218 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12222 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12223 "DIE %s [in module %s]"),
12224 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12227 pc
= attr
->as_address () + baseaddr
;
12228 pc
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc
);
12231 if (cu
->call_site_htab
== NULL
)
12232 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
12233 call_site::eq
, NULL
,
12234 &objfile
->objfile_obstack
,
12235 hashtab_obstack_allocate
, NULL
);
12236 struct call_site
call_site_local (pc
, nullptr, nullptr);
12237 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
12240 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12241 "DIE %s [in module %s]"),
12242 paddress (gdbarch
, pc
), sect_offset_str (die
->sect_off
),
12243 objfile_name (objfile
));
12247 /* Count parameters at the caller. */
12250 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
12251 child_die
= child_die
->sibling
)
12253 if (child_die
->tag
!= DW_TAG_call_site_parameter
12254 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12256 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12257 "DW_TAG_call_site child DIE %s [in module %s]"),
12258 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
12259 objfile_name (objfile
));
12266 struct call_site
*call_site
12267 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
12269 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
12270 struct call_site (pc
, cu
->per_cu
, per_objfile
);
12273 /* We never call the destructor of call_site, so we must ensure it is
12274 trivially destructible. */
12275 gdb_static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
12277 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
12278 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
12280 struct die_info
*func_die
;
12282 /* Skip also over DW_TAG_inlined_subroutine. */
12283 for (func_die
= die
->parent
;
12284 func_die
&& func_die
->tag
!= DW_TAG_subprogram
12285 && func_die
->tag
!= DW_TAG_subroutine_type
;
12286 func_die
= func_die
->parent
);
12288 /* DW_AT_call_all_calls is a superset
12289 of DW_AT_call_all_tail_calls. */
12291 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
12292 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
12293 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
12294 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
12296 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12297 not complete. But keep CALL_SITE for look ups via call_site_htab,
12298 both the initial caller containing the real return address PC and
12299 the final callee containing the current PC of a chain of tail
12300 calls do not need to have the tail call list complete. But any
12301 function candidate for a virtual tail call frame searched via
12302 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12303 determined unambiguously. */
12307 struct type
*func_type
= NULL
;
12310 func_type
= get_die_type (func_die
, cu
);
12311 if (func_type
!= NULL
)
12313 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
12315 /* Enlist this call site to the function. */
12316 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
12317 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
12320 complaint (_("Cannot find function owning DW_TAG_call_site "
12321 "DIE %s [in module %s]"),
12322 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12326 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
12328 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
12330 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
12333 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12334 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12337 call_site
->target
.set_loc_dwarf_block (nullptr);
12338 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
12339 /* Keep NULL DWARF_BLOCK. */;
12340 else if (attr
->form_is_block ())
12342 struct dwarf2_locexpr_baton
*dlbaton
;
12343 struct dwarf_block
*block
= attr
->as_block ();
12345 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
12346 dlbaton
->data
= block
->data
;
12347 dlbaton
->size
= block
->size
;
12348 dlbaton
->per_objfile
= per_objfile
;
12349 dlbaton
->per_cu
= cu
->per_cu
;
12351 call_site
->target
.set_loc_dwarf_block (dlbaton
);
12353 else if (attr
->form_is_ref ())
12355 struct dwarf2_cu
*target_cu
= cu
;
12356 struct die_info
*target_die
;
12358 target_die
= follow_die_ref (die
, attr
, &target_cu
);
12359 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
12361 struct attribute
*ranges_attr
12362 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
12364 if (die_is_declaration (target_die
, target_cu
))
12366 const char *target_physname
;
12368 /* Prefer the mangled name; otherwise compute the demangled one. */
12369 target_physname
= dw2_linkage_name (target_die
, target_cu
);
12370 if (target_physname
== NULL
)
12371 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
12372 if (target_physname
== NULL
)
12373 complaint (_("DW_AT_call_target target DIE has invalid "
12374 "physname, for referencing DIE %s [in module %s]"),
12375 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12377 call_site
->target
.set_loc_physname (target_physname
);
12379 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
12381 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
12382 + target_cu
->gnu_ranges_base
);
12383 std::vector
<CORE_ADDR
> addresses
;
12384 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
12385 target_die
->tag
, addresses
);
12386 CORE_ADDR
*saved
= XOBNEWVAR (&objfile
->objfile_obstack
, CORE_ADDR
,
12387 addresses
.size ());
12388 std::copy (addresses
.begin (), addresses
.end (), saved
);
12389 call_site
->target
.set_loc_array (addresses
.size (), saved
);
12395 /* DW_AT_entry_pc should be preferred. */
12396 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
12398 <= PC_BOUNDS_INVALID
)
12399 complaint (_("DW_AT_call_target target DIE has invalid "
12400 "low pc, for referencing DIE %s [in module %s]"),
12401 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12404 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
)
12406 call_site
->target
.set_loc_physaddr (lowpc
);
12411 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12412 "block nor reference, for DIE %s [in module %s]"),
12413 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12415 for (child_die
= die
->child
;
12416 child_die
&& child_die
->tag
;
12417 child_die
= child_die
->sibling
)
12419 struct call_site_parameter
*parameter
;
12420 struct attribute
*loc
, *origin
;
12422 if (child_die
->tag
!= DW_TAG_call_site_parameter
12423 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12425 /* Already printed the complaint above. */
12429 gdb_assert (call_site
->parameter_count
< nparams
);
12430 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
12432 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12433 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12434 register is contained in DW_AT_call_value. */
12436 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
12437 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
12438 if (origin
== NULL
)
12440 /* This was a pre-DWARF-5 GNU extension alias
12441 for DW_AT_call_parameter. */
12442 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
12444 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
12446 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
12448 sect_offset sect_off
= origin
->get_ref_die_offset ();
12449 if (!cu
->header
.offset_in_cu_p (sect_off
))
12451 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12452 binding can be done only inside one CU. Such referenced DIE
12453 therefore cannot be even moved to DW_TAG_partial_unit. */
12454 complaint (_("DW_AT_call_parameter offset is not in CU for "
12455 "DW_TAG_call_site child DIE %s [in module %s]"),
12456 sect_offset_str (child_die
->sect_off
),
12457 objfile_name (objfile
));
12460 parameter
->u
.param_cu_off
12461 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
12463 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
12465 complaint (_("No DW_FORM_block* DW_AT_location for "
12466 "DW_TAG_call_site child DIE %s [in module %s]"),
12467 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
12472 struct dwarf_block
*block
= loc
->as_block ();
12474 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
12475 (block
->data
, &block
->data
[block
->size
]);
12476 if (parameter
->u
.dwarf_reg
!= -1)
12477 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
12478 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
12479 &block
->data
[block
->size
],
12480 ¶meter
->u
.fb_offset
))
12481 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
12484 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12485 "for DW_FORM_block* DW_AT_location is supported for "
12486 "DW_TAG_call_site child DIE %s "
12488 sect_offset_str (child_die
->sect_off
),
12489 objfile_name (objfile
));
12494 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
12496 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
12497 if (attr
== NULL
|| !attr
->form_is_block ())
12499 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12500 "DW_TAG_call_site child DIE %s [in module %s]"),
12501 sect_offset_str (child_die
->sect_off
),
12502 objfile_name (objfile
));
12506 struct dwarf_block
*block
= attr
->as_block ();
12507 parameter
->value
= block
->data
;
12508 parameter
->value_size
= block
->size
;
12510 /* Parameters are not pre-cleared by memset above. */
12511 parameter
->data_value
= NULL
;
12512 parameter
->data_value_size
= 0;
12513 call_site
->parameter_count
++;
12515 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
12517 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
12518 if (attr
!= nullptr)
12520 if (!attr
->form_is_block ())
12521 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12522 "DW_TAG_call_site child DIE %s [in module %s]"),
12523 sect_offset_str (child_die
->sect_off
),
12524 objfile_name (objfile
));
12527 block
= attr
->as_block ();
12528 parameter
->data_value
= block
->data
;
12529 parameter
->data_value_size
= block
->size
;
12535 /* Helper function for read_variable. If DIE represents a virtual
12536 table, then return the type of the concrete object that is
12537 associated with the virtual table. Otherwise, return NULL. */
12539 static struct type
*
12540 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12542 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
12546 /* Find the type DIE. */
12547 struct die_info
*type_die
= NULL
;
12548 struct dwarf2_cu
*type_cu
= cu
;
12550 if (attr
->form_is_ref ())
12551 type_die
= follow_die_ref (die
, attr
, &type_cu
);
12552 if (type_die
== NULL
)
12555 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
12557 return die_containing_type (type_die
, type_cu
);
12560 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12563 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
12565 struct rust_vtable_symbol
*storage
= NULL
;
12567 if (cu
->per_cu
->lang
== language_rust
)
12569 struct type
*containing_type
= rust_containing_type (die
, cu
);
12571 if (containing_type
!= NULL
)
12573 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12575 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
12576 storage
->concrete_type
= containing_type
;
12577 storage
->subclass
= SYMBOL_RUST_VTABLE
;
12581 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
12582 struct attribute
*abstract_origin
12583 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12584 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
12585 if (res
== NULL
&& loc
&& abstract_origin
)
12587 /* We have a variable without a name, but with a location and an abstract
12588 origin. This may be a concrete instance of an abstract variable
12589 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12591 struct dwarf2_cu
*origin_cu
= cu
;
12592 struct die_info
*origin_die
12593 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
12594 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12595 per_objfile
->per_bfd
->abstract_to_concrete
12596 [origin_die
->sect_off
].push_back (die
->sect_off
);
12600 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12601 reading .debug_rnglists.
12602 Callback's type should be:
12603 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12604 Return true if the attributes are present and valid, otherwise,
12607 template <typename Callback
>
12609 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
12610 dwarf_tag tag
, Callback
&&callback
)
12612 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12613 struct objfile
*objfile
= per_objfile
->objfile
;
12614 bfd
*obfd
= objfile
->obfd
;
12615 /* Base address selection entry. */
12616 gdb::optional
<CORE_ADDR
> base
;
12617 const gdb_byte
*buffer
;
12618 bool overflow
= false;
12619 ULONGEST addr_index
;
12620 struct dwarf2_section_info
*rnglists_section
;
12622 base
= cu
->base_address
;
12623 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
12624 rnglists_section
->read (objfile
);
12626 if (offset
>= rnglists_section
->size
)
12628 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12632 buffer
= rnglists_section
->buffer
+ offset
;
12636 /* Initialize it due to a false compiler warning. */
12637 CORE_ADDR range_beginning
= 0, range_end
= 0;
12638 const gdb_byte
*buf_end
= (rnglists_section
->buffer
12639 + rnglists_section
->size
);
12640 unsigned int bytes_read
;
12642 if (buffer
== buf_end
)
12647 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
12650 case DW_RLE_end_of_list
:
12652 case DW_RLE_base_address
:
12653 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12658 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12659 buffer
+= bytes_read
;
12661 case DW_RLE_base_addressx
:
12662 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12663 buffer
+= bytes_read
;
12664 base
= read_addr_index (cu
, addr_index
);
12666 case DW_RLE_start_length
:
12667 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12672 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12674 buffer
+= bytes_read
;
12675 range_end
= (range_beginning
12676 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12677 buffer
+= bytes_read
;
12678 if (buffer
> buf_end
)
12684 case DW_RLE_startx_length
:
12685 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12686 buffer
+= bytes_read
;
12687 range_beginning
= read_addr_index (cu
, addr_index
);
12688 if (buffer
> buf_end
)
12693 range_end
= (range_beginning
12694 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12695 buffer
+= bytes_read
;
12697 case DW_RLE_offset_pair
:
12698 range_beginning
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12699 buffer
+= bytes_read
;
12700 if (buffer
> buf_end
)
12705 range_end
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12706 buffer
+= bytes_read
;
12707 if (buffer
> buf_end
)
12713 case DW_RLE_start_end
:
12714 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
12719 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12721 buffer
+= bytes_read
;
12722 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12723 buffer
+= bytes_read
;
12725 case DW_RLE_startx_endx
:
12726 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12727 buffer
+= bytes_read
;
12728 range_beginning
= read_addr_index (cu
, addr_index
);
12729 if (buffer
> buf_end
)
12734 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12735 buffer
+= bytes_read
;
12736 range_end
= read_addr_index (cu
, addr_index
);
12739 complaint (_("Invalid .debug_rnglists data (no base address)"));
12742 if (rlet
== DW_RLE_end_of_list
|| overflow
)
12744 if (rlet
== DW_RLE_base_address
)
12747 if (range_beginning
> range_end
)
12749 /* Inverted range entries are invalid. */
12750 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12754 /* Empty range entries have no effect. */
12755 if (range_beginning
== range_end
)
12758 /* Only DW_RLE_offset_pair needs the base address added. */
12759 if (rlet
== DW_RLE_offset_pair
)
12761 if (!base
.has_value ())
12763 /* We have no valid base address for the DW_RLE_offset_pair. */
12764 complaint (_("Invalid .debug_rnglists data (no base address for "
12765 "DW_RLE_offset_pair)"));
12769 range_beginning
+= *base
;
12770 range_end
+= *base
;
12773 /* A not-uncommon case of bad debug info.
12774 Don't pollute the addrmap with bad data. */
12775 if (range_beginning
== 0
12776 && !per_objfile
->per_bfd
->has_section_at_zero
)
12778 complaint (_(".debug_rnglists entry has start address of zero"
12779 " [in module %s]"), objfile_name (objfile
));
12783 callback (range_beginning
, range_end
);
12788 complaint (_("Offset %d is not terminated "
12789 "for DW_AT_ranges attribute"),
12797 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12798 Callback's type should be:
12799 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12800 Return 1 if the attributes are present and valid, otherwise, return 0. */
12802 template <typename Callback
>
12804 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
12805 Callback
&&callback
)
12807 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12808 struct objfile
*objfile
= per_objfile
->objfile
;
12809 struct comp_unit_head
*cu_header
= &cu
->header
;
12810 bfd
*obfd
= objfile
->obfd
;
12811 unsigned int addr_size
= cu_header
->addr_size
;
12812 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
12813 /* Base address selection entry. */
12814 gdb::optional
<CORE_ADDR
> base
;
12815 unsigned int dummy
;
12816 const gdb_byte
*buffer
;
12818 if (cu_header
->version
>= 5)
12819 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
12821 base
= cu
->base_address
;
12823 per_objfile
->per_bfd
->ranges
.read (objfile
);
12824 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
12826 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12830 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
12834 CORE_ADDR range_beginning
, range_end
;
12836 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12837 buffer
+= addr_size
;
12838 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12839 buffer
+= addr_size
;
12840 offset
+= 2 * addr_size
;
12842 /* An end of list marker is a pair of zero addresses. */
12843 if (range_beginning
== 0 && range_end
== 0)
12844 /* Found the end of list entry. */
12847 /* Each base address selection entry is a pair of 2 values.
12848 The first is the largest possible address, the second is
12849 the base address. Check for a base address here. */
12850 if ((range_beginning
& mask
) == mask
)
12852 /* If we found the largest possible address, then we already
12853 have the base address in range_end. */
12858 if (!base
.has_value ())
12860 /* We have no valid base address for the ranges
12862 complaint (_("Invalid .debug_ranges data (no base address)"));
12866 if (range_beginning
> range_end
)
12868 /* Inverted range entries are invalid. */
12869 complaint (_("Invalid .debug_ranges data (inverted range)"));
12873 /* Empty range entries have no effect. */
12874 if (range_beginning
== range_end
)
12877 range_beginning
+= *base
;
12878 range_end
+= *base
;
12880 /* A not-uncommon case of bad debug info.
12881 Don't pollute the addrmap with bad data. */
12882 if (range_beginning
== 0
12883 && !per_objfile
->per_bfd
->has_section_at_zero
)
12885 complaint (_(".debug_ranges entry has start address of zero"
12886 " [in module %s]"), objfile_name (objfile
));
12890 callback (range_beginning
, range_end
);
12896 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12897 Return 1 if the attributes are present and valid, otherwise, return 0.
12898 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12899 ranges in MAP are set, using DATUM as the value. */
12902 dwarf2_ranges_read (unsigned offset
, CORE_ADDR
*low_return
,
12903 CORE_ADDR
*high_return
, struct dwarf2_cu
*cu
,
12904 addrmap
*map
, void *datum
, dwarf_tag tag
)
12906 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12907 struct gdbarch
*gdbarch
= objfile
->arch ();
12908 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
12911 CORE_ADDR high
= 0;
12914 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
12915 [&] (CORE_ADDR range_beginning
, CORE_ADDR range_end
)
12917 if (map
!= nullptr)
12922 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12923 range_beginning
+ baseaddr
)
12925 highpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12926 range_end
+ baseaddr
)
12928 addrmap_set_empty (map
, lowpc
, highpc
- 1, datum
);
12931 /* FIXME: This is recording everything as a low-high
12932 segment of consecutive addresses. We should have a
12933 data structure for discontiguous block ranges
12937 low
= range_beginning
;
12943 if (range_beginning
< low
)
12944 low
= range_beginning
;
12945 if (range_end
> high
)
12953 /* If the first entry is an end-of-list marker, the range
12954 describes an empty scope, i.e. no instructions. */
12960 *high_return
= high
;
12964 /* Process ranges and fill in a vector of the low PC values only. */
12967 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
12969 std::vector
<CORE_ADDR
> &result
)
12971 dwarf2_ranges_process (offset
, cu
, tag
,
12972 [&] (CORE_ADDR start
, CORE_ADDR end
)
12974 result
.push_back (start
);
12978 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12979 definition for the return value. *LOWPC and *HIGHPC are set iff
12980 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12982 static enum pc_bounds_kind
12983 dwarf2_get_pc_bounds (struct die_info
*die
, CORE_ADDR
*lowpc
,
12984 CORE_ADDR
*highpc
, struct dwarf2_cu
*cu
,
12985 addrmap
*map
, void *datum
)
12987 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12988 struct attribute
*attr
;
12989 struct attribute
*attr_high
;
12991 CORE_ADDR high
= 0;
12992 enum pc_bounds_kind ret
;
12994 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
12997 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12998 if (attr
!= nullptr)
13000 low
= attr
->as_address ();
13001 high
= attr_high
->as_address ();
13002 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13006 /* Found high w/o low attribute. */
13007 return PC_BOUNDS_INVALID
;
13009 /* Found consecutive range of addresses. */
13010 ret
= PC_BOUNDS_HIGH_LOW
;
13014 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13015 if (attr
!= nullptr && attr
->form_is_unsigned ())
13017 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13018 on DWARF version). */
13019 ULONGEST ranges_offset
= attr
->as_unsigned ();
13021 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13023 if (die
->tag
!= DW_TAG_compile_unit
)
13024 ranges_offset
+= cu
->gnu_ranges_base
;
13026 /* Value of the DW_AT_ranges attribute is the offset in the
13027 .debug_ranges section. */
13028 if (!dwarf2_ranges_read (ranges_offset
, &low
, &high
, cu
,
13029 map
, datum
, die
->tag
))
13030 return PC_BOUNDS_INVALID
;
13031 /* Found discontinuous range of addresses. */
13032 ret
= PC_BOUNDS_RANGES
;
13035 return PC_BOUNDS_NOT_PRESENT
;
13038 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13040 return PC_BOUNDS_INVALID
;
13042 /* When using the GNU linker, .gnu.linkonce. sections are used to
13043 eliminate duplicate copies of functions and vtables and such.
13044 The linker will arbitrarily choose one and discard the others.
13045 The AT_*_pc values for such functions refer to local labels in
13046 these sections. If the section from that file was discarded, the
13047 labels are not in the output, so the relocs get a value of 0.
13048 If this is a discarded function, mark the pc bounds as invalid,
13049 so that GDB will ignore it. */
13050 if (low
== 0 && !per_objfile
->per_bfd
->has_section_at_zero
)
13051 return PC_BOUNDS_INVALID
;
13059 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13060 its low and high PC addresses. Do nothing if these addresses could not
13061 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13062 and HIGHPC to the high address if greater than HIGHPC. */
13065 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
13066 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13067 struct dwarf2_cu
*cu
)
13069 CORE_ADDR low
, high
;
13070 struct die_info
*child
= die
->child
;
13072 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
13073 >= PC_BOUNDS_RANGES
)
13075 *lowpc
= std::min (*lowpc
, low
);
13076 *highpc
= std::max (*highpc
, high
);
13079 /* If the language does not allow nested subprograms (either inside
13080 subprograms or lexical blocks), we're done. */
13081 if (cu
->per_cu
->lang
!= language_ada
)
13084 /* Check all the children of the given DIE. If it contains nested
13085 subprograms, then check their pc bounds. Likewise, we need to
13086 check lexical blocks as well, as they may also contain subprogram
13088 while (child
&& child
->tag
)
13090 if (child
->tag
== DW_TAG_subprogram
13091 || child
->tag
== DW_TAG_lexical_block
)
13092 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
13093 child
= child
->sibling
;
13097 /* Get the low and high pc's represented by the scope DIE, and store
13098 them in *LOWPC and *HIGHPC. If the correct values can't be
13099 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13102 get_scope_pc_bounds (struct die_info
*die
,
13103 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13104 struct dwarf2_cu
*cu
)
13106 CORE_ADDR best_low
= (CORE_ADDR
) -1;
13107 CORE_ADDR best_high
= (CORE_ADDR
) 0;
13108 CORE_ADDR current_low
, current_high
;
13110 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
13112 >= PC_BOUNDS_RANGES
)
13114 best_low
= current_low
;
13115 best_high
= current_high
;
13119 struct die_info
*child
= die
->child
;
13121 while (child
&& child
->tag
)
13123 switch (child
->tag
) {
13124 case DW_TAG_subprogram
:
13125 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
13127 case DW_TAG_namespace
:
13128 case DW_TAG_module
:
13129 /* FIXME: carlton/2004-01-16: Should we do this for
13130 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13131 that current GCC's always emit the DIEs corresponding
13132 to definitions of methods of classes as children of a
13133 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13134 the DIEs giving the declarations, which could be
13135 anywhere). But I don't see any reason why the
13136 standards says that they have to be there. */
13137 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
13139 if (current_low
!= ((CORE_ADDR
) -1))
13141 best_low
= std::min (best_low
, current_low
);
13142 best_high
= std::max (best_high
, current_high
);
13150 child
= child
->sibling
;
13155 *highpc
= best_high
;
13158 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13162 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
13163 CORE_ADDR baseaddr
, struct dwarf2_cu
*cu
)
13165 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13166 struct gdbarch
*gdbarch
= objfile
->arch ();
13167 struct attribute
*attr
;
13168 struct attribute
*attr_high
;
13170 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13173 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13174 if (attr
!= nullptr)
13176 CORE_ADDR low
= attr
->as_address ();
13177 CORE_ADDR high
= attr_high
->as_address ();
13179 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13182 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
+ baseaddr
);
13183 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
+ baseaddr
);
13184 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
13188 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13189 if (attr
!= nullptr && attr
->form_is_unsigned ())
13191 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13192 on DWARF version). */
13193 ULONGEST ranges_offset
= attr
->as_unsigned ();
13195 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13197 if (die
->tag
!= DW_TAG_compile_unit
)
13198 ranges_offset
+= cu
->gnu_ranges_base
;
13200 std::vector
<blockrange
> blockvec
;
13201 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
13202 [&] (CORE_ADDR start
, CORE_ADDR end
)
13206 start
= gdbarch_adjust_dwarf2_addr (gdbarch
, start
);
13207 end
= gdbarch_adjust_dwarf2_addr (gdbarch
, end
);
13208 cu
->get_builder ()->record_block_range (block
, start
, end
- 1);
13209 blockvec
.emplace_back (start
, end
);
13212 block
->set_ranges (make_blockranges (objfile
, blockvec
));
13216 /* Check whether the producer field indicates either of GCC < 4.6, or the
13217 Intel C/C++ compiler, and cache the result in CU. */
13220 check_producer (struct dwarf2_cu
*cu
)
13224 if (cu
->producer
== NULL
)
13226 /* For unknown compilers expect their behavior is DWARF version
13229 GCC started to support .debug_types sections by -gdwarf-4 since
13230 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13231 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13232 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13233 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13235 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
13237 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
13238 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
13239 cu
->producer_is_gcc_11
= major
== 11;
13241 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
13243 cu
->producer_is_icc
= true;
13244 cu
->producer_is_icc_lt_14
= major
< 14;
13246 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
13247 cu
->producer_is_codewarrior
= true;
13250 /* For other non-GCC compilers, expect their behavior is DWARF version
13254 cu
->checked_producer
= true;
13257 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13258 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13259 during 4.6.0 experimental. */
13262 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
13264 if (!cu
->checked_producer
)
13265 check_producer (cu
);
13267 return cu
->producer_is_gxx_lt_4_6
;
13271 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13272 with incorrect is_stmt attributes. */
13275 producer_is_codewarrior (struct dwarf2_cu
*cu
)
13277 if (!cu
->checked_producer
)
13278 check_producer (cu
);
13280 return cu
->producer_is_codewarrior
;
13283 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13284 If that attribute is not available, return the appropriate
13287 static enum dwarf_access_attribute
13288 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
13290 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
13291 if (attr
!= nullptr)
13293 LONGEST value
= attr
->constant_value (-1);
13294 if (value
== DW_ACCESS_public
13295 || value
== DW_ACCESS_protected
13296 || value
== DW_ACCESS_private
)
13297 return (dwarf_access_attribute
) value
;
13298 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13302 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
13304 /* The default DWARF 2 accessibility for members is public, the default
13305 accessibility for inheritance is private. */
13307 if (die
->tag
!= DW_TAG_inheritance
)
13308 return DW_ACCESS_public
;
13310 return DW_ACCESS_private
;
13314 /* DWARF 3+ defines the default accessibility a different way. The same
13315 rules apply now for DW_TAG_inheritance as for the members and it only
13316 depends on the container kind. */
13318 if (die
->parent
->tag
== DW_TAG_class_type
)
13319 return DW_ACCESS_private
;
13321 return DW_ACCESS_public
;
13325 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13326 *OFFSET to the byte offset. If the attribute was not found return
13327 0, otherwise return 1. If it was found but could not properly be
13328 handled, set *OFFSET to 0. */
13331 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13334 struct attribute
*attr
;
13336 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13341 /* Note that we do not check for a section offset first here.
13342 This is because DW_AT_data_member_location is new in DWARF 4,
13343 so if we see it, we can assume that a constant form is really
13344 a constant and not a section offset. */
13345 if (attr
->form_is_constant ())
13346 *offset
= attr
->constant_value (0);
13347 else if (attr
->form_is_section_offset ())
13348 dwarf2_complex_location_expr_complaint ();
13349 else if (attr
->form_is_block ())
13350 *offset
= decode_locdesc (attr
->as_block (), cu
);
13352 dwarf2_complex_location_expr_complaint ();
13358 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13359 if (attr
!= nullptr)
13361 *offset
= attr
->constant_value (0);
13369 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13370 store the results in FIELD. */
13373 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13374 struct field
*field
)
13376 struct attribute
*attr
;
13378 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13381 if (attr
->form_is_constant ())
13383 LONGEST offset
= attr
->constant_value (0);
13385 /* Work around this GCC 11 bug, where it would erroneously use -1
13386 data member locations, instead of 0:
13388 Negative DW_AT_data_member_location
13389 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13391 if (offset
== -1 && cu
->producer_is_gcc_11
)
13393 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13397 field
->set_loc_bitpos (offset
* bits_per_byte
);
13399 else if (attr
->form_is_section_offset ())
13400 dwarf2_complex_location_expr_complaint ();
13401 else if (attr
->form_is_block ())
13404 CORE_ADDR offset
= decode_locdesc (attr
->as_block (), cu
, &handled
);
13406 field
->set_loc_bitpos (offset
* bits_per_byte
);
13409 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13410 struct objfile
*objfile
= per_objfile
->objfile
;
13411 struct dwarf2_locexpr_baton
*dlbaton
13412 = XOBNEW (&objfile
->objfile_obstack
,
13413 struct dwarf2_locexpr_baton
);
13414 dlbaton
->data
= attr
->as_block ()->data
;
13415 dlbaton
->size
= attr
->as_block ()->size
;
13416 /* When using this baton, we want to compute the address
13417 of the field, not the value. This is why
13418 is_reference is set to false here. */
13419 dlbaton
->is_reference
= false;
13420 dlbaton
->per_objfile
= per_objfile
;
13421 dlbaton
->per_cu
= cu
->per_cu
;
13423 field
->set_loc_dwarf_block (dlbaton
);
13427 dwarf2_complex_location_expr_complaint ();
13431 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13432 if (attr
!= nullptr)
13433 field
->set_loc_bitpos (attr
->constant_value (0));
13437 /* Add an aggregate field to the field list. */
13440 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
13441 struct dwarf2_cu
*cu
)
13443 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13444 struct gdbarch
*gdbarch
= objfile
->arch ();
13445 struct nextfield
*new_field
;
13446 struct attribute
*attr
;
13448 const char *fieldname
= "";
13450 if (die
->tag
== DW_TAG_inheritance
)
13452 fip
->baseclasses
.emplace_back ();
13453 new_field
= &fip
->baseclasses
.back ();
13457 fip
->fields
.emplace_back ();
13458 new_field
= &fip
->fields
.back ();
13461 new_field
->offset
= die
->sect_off
;
13463 new_field
->accessibility
= dwarf2_access_attribute (die
, cu
);
13464 if (new_field
->accessibility
!= DW_ACCESS_public
)
13465 fip
->non_public_fields
= true;
13467 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
13468 if (attr
!= nullptr)
13469 new_field
->virtuality
= attr
->as_virtuality ();
13471 new_field
->virtuality
= DW_VIRTUALITY_none
;
13473 fp
= &new_field
->field
;
13475 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
13476 && !die_is_declaration (die
, cu
))
13478 if (die
->tag
== DW_TAG_namelist_item
)
13480 /* Typically, DW_TAG_namelist_item are references to namelist items.
13481 If so, follow that reference. */
13482 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
13483 struct die_info
*item_die
= nullptr;
13484 struct dwarf2_cu
*item_cu
= cu
;
13485 if (attr1
->form_is_ref ())
13486 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
13487 if (item_die
!= nullptr)
13490 /* Data member other than a C++ static data member. */
13492 /* Get type of field. */
13493 fp
->set_type (die_type (die
, cu
));
13495 fp
->set_loc_bitpos (0);
13497 /* Get bit size of field (zero if none). */
13498 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
13499 if (attr
!= nullptr)
13501 FIELD_BITSIZE (*fp
) = attr
->constant_value (0);
13505 FIELD_BITSIZE (*fp
) = 0;
13508 /* Get bit offset of field. */
13509 handle_member_location (die
, cu
, fp
);
13510 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
13511 if (attr
!= nullptr && attr
->form_is_constant ())
13513 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
13515 /* For big endian bits, the DW_AT_bit_offset gives the
13516 additional bit offset from the MSB of the containing
13517 anonymous object to the MSB of the field. We don't
13518 have to do anything special since we don't need to
13519 know the size of the anonymous object. */
13520 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
13524 /* For little endian bits, compute the bit offset to the
13525 MSB of the anonymous object, subtract off the number of
13526 bits from the MSB of the field to the MSB of the
13527 object, and then subtract off the number of bits of
13528 the field itself. The result is the bit offset of
13529 the LSB of the field. */
13530 int anonymous_size
;
13531 int bit_offset
= attr
->constant_value (0);
13533 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13534 if (attr
!= nullptr && attr
->form_is_constant ())
13536 /* The size of the anonymous object containing
13537 the bit field is explicit, so use the
13538 indicated size (in bytes). */
13539 anonymous_size
= attr
->constant_value (0);
13543 /* The size of the anonymous object containing
13544 the bit field must be inferred from the type
13545 attribute of the data member containing the
13547 anonymous_size
= TYPE_LENGTH (fp
->type ());
13549 fp
->set_loc_bitpos (fp
->loc_bitpos ()
13550 + anonymous_size
* bits_per_byte
13551 - bit_offset
- FIELD_BITSIZE (*fp
));
13555 /* Get name of field. */
13556 fieldname
= dwarf2_name (die
, cu
);
13557 if (fieldname
== NULL
)
13560 /* The name is already allocated along with this objfile, so we don't
13561 need to duplicate it for the type. */
13562 fp
->set_name (fieldname
);
13564 /* Change accessibility for artificial fields (e.g. virtual table
13565 pointer or virtual base class pointer) to private. */
13566 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
13568 FIELD_ARTIFICIAL (*fp
) = 1;
13569 new_field
->accessibility
= DW_ACCESS_private
;
13570 fip
->non_public_fields
= true;
13573 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
13575 /* C++ static member. */
13577 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13578 is a declaration, but all versions of G++ as of this writing
13579 (so through at least 3.2.1) incorrectly generate
13580 DW_TAG_variable tags. */
13582 const char *physname
;
13584 /* Get name of field. */
13585 fieldname
= dwarf2_name (die
, cu
);
13586 if (fieldname
== NULL
)
13589 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
13591 /* Only create a symbol if this is an external value.
13592 new_symbol checks this and puts the value in the global symbol
13593 table, which we want. If it is not external, new_symbol
13594 will try to put the value in cu->list_in_scope which is wrong. */
13595 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
13597 /* A static const member, not much different than an enum as far as
13598 we're concerned, except that we can support more types. */
13599 new_symbol (die
, NULL
, cu
);
13602 /* Get physical name. */
13603 physname
= dwarf2_physname (fieldname
, die
, cu
);
13605 /* The name is already allocated along with this objfile, so we don't
13606 need to duplicate it for the type. */
13607 fp
->set_loc_physname (physname
? physname
: "");
13608 fp
->set_type (die_type (die
, cu
));
13609 fp
->set_name (fieldname
);
13611 else if (die
->tag
== DW_TAG_inheritance
)
13613 /* C++ base class field. */
13614 handle_member_location (die
, cu
, fp
);
13615 FIELD_BITSIZE (*fp
) = 0;
13616 fp
->set_type (die_type (die
, cu
));
13617 fp
->set_name (fp
->type ()->name ());
13620 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13623 /* Can the type given by DIE define another type? */
13626 type_can_define_types (const struct die_info
*die
)
13630 case DW_TAG_typedef
:
13631 case DW_TAG_class_type
:
13632 case DW_TAG_structure_type
:
13633 case DW_TAG_union_type
:
13634 case DW_TAG_enumeration_type
:
13642 /* Add a type definition defined in the scope of the FIP's class. */
13645 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
13646 struct dwarf2_cu
*cu
)
13648 struct decl_field fp
;
13649 memset (&fp
, 0, sizeof (fp
));
13651 gdb_assert (type_can_define_types (die
));
13653 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13654 fp
.name
= dwarf2_name (die
, cu
);
13655 fp
.type
= read_type_die (die
, cu
);
13657 /* Save accessibility. */
13658 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
13659 switch (accessibility
)
13661 case DW_ACCESS_public
:
13662 /* The assumed value if neither private nor protected. */
13664 case DW_ACCESS_private
:
13667 case DW_ACCESS_protected
:
13668 fp
.is_protected
= 1;
13672 if (die
->tag
== DW_TAG_typedef
)
13673 fip
->typedef_field_list
.push_back (fp
);
13675 fip
->nested_types_list
.push_back (fp
);
13678 /* A convenience typedef that's used when finding the discriminant
13679 field for a variant part. */
13680 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
13683 /* Compute the discriminant range for a given variant. OBSTACK is
13684 where the results will be stored. VARIANT is the variant to
13685 process. IS_UNSIGNED indicates whether the discriminant is signed
13688 static const gdb::array_view
<discriminant_range
>
13689 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
13692 std::vector
<discriminant_range
> ranges
;
13694 if (variant
.default_branch
)
13697 if (variant
.discr_list_data
== nullptr)
13699 discriminant_range r
13700 = {variant
.discriminant_value
, variant
.discriminant_value
};
13701 ranges
.push_back (r
);
13705 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
13706 variant
.discr_list_data
->size
);
13707 while (!data
.empty ())
13709 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
13711 complaint (_("invalid discriminant marker: %d"), data
[0]);
13714 bool is_range
= data
[0] == DW_DSC_range
;
13715 data
= data
.slice (1);
13717 ULONGEST low
, high
;
13718 unsigned int bytes_read
;
13722 complaint (_("DW_AT_discr_list missing low value"));
13726 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
13728 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
13730 data
= data
.slice (bytes_read
);
13736 complaint (_("DW_AT_discr_list missing high value"));
13740 high
= read_unsigned_leb128 (nullptr, data
.data (),
13743 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
13745 data
= data
.slice (bytes_read
);
13750 ranges
.push_back ({ low
, high
});
13754 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
13756 std::copy (ranges
.begin (), ranges
.end (), result
);
13757 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
13760 static const gdb::array_view
<variant_part
> create_variant_parts
13761 (struct obstack
*obstack
,
13762 const offset_map_type
&offset_map
,
13763 struct field_info
*fi
,
13764 const std::vector
<variant_part_builder
> &variant_parts
);
13766 /* Fill in a "struct variant" for a given variant field. RESULT is
13767 the variant to fill in. OBSTACK is where any needed allocations
13768 will be done. OFFSET_MAP holds the mapping from section offsets to
13769 fields for the type. FI describes the fields of the type we're
13770 processing. FIELD is the variant field we're converting. */
13773 create_one_variant (variant
&result
, struct obstack
*obstack
,
13774 const offset_map_type
&offset_map
,
13775 struct field_info
*fi
, const variant_field
&field
)
13777 result
.discriminants
= convert_variant_range (obstack
, field
, false);
13778 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
13779 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
13780 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
13781 field
.variant_parts
);
13784 /* Fill in a "struct variant_part" for a given variant part. RESULT
13785 is the variant part to fill in. OBSTACK is where any needed
13786 allocations will be done. OFFSET_MAP holds the mapping from
13787 section offsets to fields for the type. FI describes the fields of
13788 the type we're processing. BUILDER is the variant part to be
13792 create_one_variant_part (variant_part
&result
,
13793 struct obstack
*obstack
,
13794 const offset_map_type
&offset_map
,
13795 struct field_info
*fi
,
13796 const variant_part_builder
&builder
)
13798 auto iter
= offset_map
.find (builder
.discriminant_offset
);
13799 if (iter
== offset_map
.end ())
13801 result
.discriminant_index
= -1;
13802 /* Doesn't matter. */
13803 result
.is_unsigned
= false;
13807 result
.discriminant_index
= iter
->second
;
13809 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
13812 size_t n
= builder
.variants
.size ();
13813 variant
*output
= new (obstack
) variant
[n
];
13814 for (size_t i
= 0; i
< n
; ++i
)
13815 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
13816 builder
.variants
[i
]);
13818 result
.variants
= gdb::array_view
<variant
> (output
, n
);
13821 /* Create a vector of variant parts that can be attached to a type.
13822 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13823 holds the mapping from section offsets to fields for the type. FI
13824 describes the fields of the type we're processing. VARIANT_PARTS
13825 is the vector to convert. */
13827 static const gdb::array_view
<variant_part
>
13828 create_variant_parts (struct obstack
*obstack
,
13829 const offset_map_type
&offset_map
,
13830 struct field_info
*fi
,
13831 const std::vector
<variant_part_builder
> &variant_parts
)
13833 if (variant_parts
.empty ())
13836 size_t n
= variant_parts
.size ();
13837 variant_part
*result
= new (obstack
) variant_part
[n
];
13838 for (size_t i
= 0; i
< n
; ++i
)
13839 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
13842 return gdb::array_view
<variant_part
> (result
, n
);
13845 /* Compute the variant part vector for FIP, attaching it to TYPE when
13849 add_variant_property (struct field_info
*fip
, struct type
*type
,
13850 struct dwarf2_cu
*cu
)
13852 /* Map section offsets of fields to their field index. Note the
13853 field index here does not take the number of baseclasses into
13855 offset_map_type offset_map
;
13856 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
13857 offset_map
[fip
->fields
[i
].offset
] = i
;
13859 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13860 gdb::array_view
<const variant_part
> parts
13861 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
13862 fip
->variant_parts
);
13864 struct dynamic_prop prop
;
13865 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
13866 obstack_copy (&objfile
->objfile_obstack
, &parts
,
13869 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
13872 /* Create the vector of fields, and attach it to the type. */
13875 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
13876 struct dwarf2_cu
*cu
)
13878 int nfields
= fip
->nfields ();
13880 /* Record the field count, allocate space for the array of fields,
13881 and create blank accessibility bitfields if necessary. */
13882 type
->set_num_fields (nfields
);
13884 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
13886 if (fip
->non_public_fields
&& cu
->per_cu
->lang
!= language_ada
)
13888 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13890 TYPE_FIELD_PRIVATE_BITS (type
) =
13891 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13892 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type
), nfields
);
13894 TYPE_FIELD_PROTECTED_BITS (type
) =
13895 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13896 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type
), nfields
);
13898 TYPE_FIELD_IGNORE_BITS (type
) =
13899 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13900 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type
), nfields
);
13903 /* If the type has baseclasses, allocate and clear a bit vector for
13904 TYPE_FIELD_VIRTUAL_BITS. */
13905 if (!fip
->baseclasses
.empty () && cu
->per_cu
->lang
!= language_ada
)
13907 int num_bytes
= B_BYTES (fip
->baseclasses
.size ());
13908 unsigned char *pointer
;
13910 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13911 pointer
= (unsigned char *) TYPE_ALLOC (type
, num_bytes
);
13912 TYPE_FIELD_VIRTUAL_BITS (type
) = pointer
;
13913 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type
), fip
->baseclasses
.size ());
13914 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
13917 if (!fip
->variant_parts
.empty ())
13918 add_variant_property (fip
, type
, cu
);
13920 /* Copy the saved-up fields into the field vector. */
13921 for (int i
= 0; i
< nfields
; ++i
)
13923 struct nextfield
&field
13924 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
13925 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
13927 type
->field (i
) = field
.field
;
13928 switch (field
.accessibility
)
13930 case DW_ACCESS_private
:
13931 if (cu
->per_cu
->lang
!= language_ada
)
13932 SET_TYPE_FIELD_PRIVATE (type
, i
);
13935 case DW_ACCESS_protected
:
13936 if (cu
->per_cu
->lang
!= language_ada
)
13937 SET_TYPE_FIELD_PROTECTED (type
, i
);
13940 case DW_ACCESS_public
:
13944 /* Unknown accessibility. Complain and treat it as public. */
13946 complaint (_("unsupported accessibility %d"),
13947 field
.accessibility
);
13951 if (i
< fip
->baseclasses
.size ())
13953 switch (field
.virtuality
)
13955 case DW_VIRTUALITY_virtual
:
13956 case DW_VIRTUALITY_pure_virtual
:
13957 if (cu
->per_cu
->lang
== language_ada
)
13958 error (_("unexpected virtuality in component of Ada type"));
13959 SET_TYPE_FIELD_VIRTUAL (type
, i
);
13966 /* Return true if this member function is a constructor, false
13970 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
13972 const char *fieldname
;
13973 const char *type_name
;
13976 if (die
->parent
== NULL
)
13979 if (die
->parent
->tag
!= DW_TAG_structure_type
13980 && die
->parent
->tag
!= DW_TAG_union_type
13981 && die
->parent
->tag
!= DW_TAG_class_type
)
13984 fieldname
= dwarf2_name (die
, cu
);
13985 type_name
= dwarf2_name (die
->parent
, cu
);
13986 if (fieldname
== NULL
|| type_name
== NULL
)
13989 len
= strlen (fieldname
);
13990 return (strncmp (fieldname
, type_name
, len
) == 0
13991 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
13994 /* Add a member function to the proper fieldlist. */
13997 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
13998 struct type
*type
, struct dwarf2_cu
*cu
)
14000 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14001 struct attribute
*attr
;
14003 struct fnfieldlist
*flp
= nullptr;
14004 struct fn_field
*fnp
;
14005 const char *fieldname
;
14006 struct type
*this_type
;
14008 if (cu
->per_cu
->lang
== language_ada
)
14009 error (_("unexpected member function in Ada type"));
14011 /* Get name of member function. */
14012 fieldname
= dwarf2_name (die
, cu
);
14013 if (fieldname
== NULL
)
14016 /* Look up member function name in fieldlist. */
14017 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14019 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
14021 flp
= &fip
->fnfieldlists
[i
];
14026 /* Create a new fnfieldlist if necessary. */
14027 if (flp
== nullptr)
14029 fip
->fnfieldlists
.emplace_back ();
14030 flp
= &fip
->fnfieldlists
.back ();
14031 flp
->name
= fieldname
;
14032 i
= fip
->fnfieldlists
.size () - 1;
14035 /* Create a new member function field and add it to the vector of
14037 flp
->fnfields
.emplace_back ();
14038 fnp
= &flp
->fnfields
.back ();
14040 /* Delay processing of the physname until later. */
14041 if (cu
->per_cu
->lang
== language_cplus
)
14042 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
14046 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
14047 fnp
->physname
= physname
? physname
: "";
14050 fnp
->type
= alloc_type (objfile
);
14051 this_type
= read_type_die (die
, cu
);
14052 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
14054 int nparams
= this_type
->num_fields ();
14056 /* TYPE is the domain of this method, and THIS_TYPE is the type
14057 of the method itself (TYPE_CODE_METHOD). */
14058 smash_to_method_type (fnp
->type
, type
,
14059 TYPE_TARGET_TYPE (this_type
),
14060 this_type
->fields (),
14061 this_type
->num_fields (),
14062 this_type
->has_varargs ());
14064 /* Handle static member functions.
14065 Dwarf2 has no clean way to discern C++ static and non-static
14066 member functions. G++ helps GDB by marking the first
14067 parameter for non-static member functions (which is the this
14068 pointer) as artificial. We obtain this information from
14069 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14070 if (nparams
== 0 || TYPE_FIELD_ARTIFICIAL (this_type
, 0) == 0)
14071 fnp
->voffset
= VOFFSET_STATIC
;
14074 complaint (_("member function type missing for '%s'"),
14075 dwarf2_full_name (fieldname
, die
, cu
));
14077 /* Get fcontext from DW_AT_containing_type if present. */
14078 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14079 fnp
->fcontext
= die_containing_type (die
, cu
);
14081 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14082 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14084 /* Get accessibility. */
14085 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
14086 switch (accessibility
)
14088 case DW_ACCESS_private
:
14089 fnp
->is_private
= 1;
14091 case DW_ACCESS_protected
:
14092 fnp
->is_protected
= 1;
14096 /* Check for artificial methods. */
14097 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
14098 if (attr
&& attr
->as_boolean ())
14099 fnp
->is_artificial
= 1;
14101 /* Check for defaulted methods. */
14102 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
14103 if (attr
!= nullptr)
14104 fnp
->defaulted
= attr
->defaulted ();
14106 /* Check for deleted methods. */
14107 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
14108 if (attr
!= nullptr && attr
->as_boolean ())
14109 fnp
->is_deleted
= 1;
14111 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
14113 /* Get index in virtual function table if it is a virtual member
14114 function. For older versions of GCC, this is an offset in the
14115 appropriate virtual table, as specified by DW_AT_containing_type.
14116 For everyone else, it is an expression to be evaluated relative
14117 to the object address. */
14119 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
14120 if (attr
!= nullptr)
14122 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
14124 struct dwarf_block
*block
= attr
->as_block ();
14126 if (block
->data
[0] == DW_OP_constu
)
14128 /* Old-style GCC. */
14129 fnp
->voffset
= decode_locdesc (block
, cu
) + 2;
14131 else if (block
->data
[0] == DW_OP_deref
14132 || (block
->size
> 1
14133 && block
->data
[0] == DW_OP_deref_size
14134 && block
->data
[1] == cu
->header
.addr_size
))
14136 fnp
->voffset
= decode_locdesc (block
, cu
);
14137 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
14138 dwarf2_complex_location_expr_complaint ();
14140 fnp
->voffset
/= cu
->header
.addr_size
;
14144 dwarf2_complex_location_expr_complaint ();
14146 if (!fnp
->fcontext
)
14148 /* If there is no `this' field and no DW_AT_containing_type,
14149 we cannot actually find a base class context for the
14151 if (this_type
->num_fields () == 0
14152 || !TYPE_FIELD_ARTIFICIAL (this_type
, 0))
14154 complaint (_("cannot determine context for virtual member "
14155 "function \"%s\" (offset %s)"),
14156 fieldname
, sect_offset_str (die
->sect_off
));
14161 = TYPE_TARGET_TYPE (this_type
->field (0).type ());
14165 else if (attr
->form_is_section_offset ())
14167 dwarf2_complex_location_expr_complaint ();
14171 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14177 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
14178 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
14180 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14181 complaint (_("Member function \"%s\" (offset %s) is virtual "
14182 "but the vtable offset is not specified"),
14183 fieldname
, sect_offset_str (die
->sect_off
));
14184 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14185 TYPE_CPLUS_DYNAMIC (type
) = 1;
14190 /* Create the vector of member function fields, and attach it to the type. */
14193 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
14194 struct dwarf2_cu
*cu
)
14196 if (cu
->per_cu
->lang
== language_ada
)
14197 error (_("unexpected member functions in Ada type"));
14199 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14200 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
14202 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
14204 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14206 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
14207 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
14209 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
14210 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
14211 fn_flp
->fn_fields
= (struct fn_field
*)
14212 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
14214 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
14215 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
14218 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
14221 /* Returns non-zero if NAME is the name of a vtable member in CU's
14222 language, zero otherwise. */
14224 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
14226 static const char vptr
[] = "_vptr";
14228 /* Look for the C++ form of the vtable. */
14229 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
14235 /* GCC outputs unnamed structures that are really pointers to member
14236 functions, with the ABI-specified layout. If TYPE describes
14237 such a structure, smash it into a member function type.
14239 GCC shouldn't do this; it should just output pointer to member DIEs.
14240 This is GCC PR debug/28767. */
14243 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
14245 struct type
*pfn_type
, *self_type
, *new_type
;
14247 /* Check for a structure with no name and two children. */
14248 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14251 /* Check for __pfn and __delta members. */
14252 if (type
->field (0).name () == NULL
14253 || strcmp (type
->field (0).name (), "__pfn") != 0
14254 || type
->field (1).name () == NULL
14255 || strcmp (type
->field (1).name (), "__delta") != 0)
14258 /* Find the type of the method. */
14259 pfn_type
= type
->field (0).type ();
14260 if (pfn_type
== NULL
14261 || pfn_type
->code () != TYPE_CODE_PTR
14262 || TYPE_TARGET_TYPE (pfn_type
)->code () != TYPE_CODE_FUNC
)
14265 /* Look for the "this" argument. */
14266 pfn_type
= TYPE_TARGET_TYPE (pfn_type
);
14267 if (pfn_type
->num_fields () == 0
14268 /* || pfn_type->field (0).type () == NULL */
14269 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14272 self_type
= TYPE_TARGET_TYPE (pfn_type
->field (0).type ());
14273 new_type
= alloc_type (objfile
);
14274 smash_to_method_type (new_type
, self_type
, TYPE_TARGET_TYPE (pfn_type
),
14275 pfn_type
->fields (), pfn_type
->num_fields (),
14276 pfn_type
->has_varargs ());
14277 smash_to_methodptr_type (type
, new_type
);
14280 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14281 requires rewriting, then copy it and return the updated copy.
14282 Otherwise return nullptr. */
14284 static struct type
*
14285 rewrite_array_type (struct type
*type
)
14287 if (type
->code () != TYPE_CODE_ARRAY
)
14290 struct type
*index_type
= type
->index_type ();
14291 range_bounds
*current_bounds
= index_type
->bounds ();
14293 /* Handle multi-dimensional arrays. */
14294 struct type
*new_target
= rewrite_array_type (TYPE_TARGET_TYPE (type
));
14295 if (new_target
== nullptr)
14297 /* Maybe we don't need to rewrite this array. */
14298 if (current_bounds
->low
.kind () == PROP_CONST
14299 && current_bounds
->high
.kind () == PROP_CONST
)
14303 /* Either the target type was rewritten, or the bounds have to be
14304 updated. Either way we want to copy the type and update
14306 struct type
*copy
= copy_type (type
);
14307 int nfields
= copy
->num_fields ();
14309 = ((struct field
*) TYPE_ZALLOC (copy
,
14310 nfields
* sizeof (struct field
)));
14311 memcpy (new_fields
, copy
->fields (), nfields
* sizeof (struct field
));
14312 copy
->set_fields (new_fields
);
14313 if (new_target
!= nullptr)
14314 TYPE_TARGET_TYPE (copy
) = new_target
;
14316 struct type
*index_copy
= copy_type (index_type
);
14317 range_bounds
*bounds
14318 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
14319 sizeof (range_bounds
));
14320 *bounds
= *current_bounds
;
14321 bounds
->low
.set_const_val (1);
14322 bounds
->high
.set_const_val (0);
14323 index_copy
->set_bounds (bounds
);
14324 copy
->set_index_type (index_copy
);
14329 /* While some versions of GCC will generate complicated DWARF for an
14330 array (see quirk_ada_thick_pointer), more recent versions were
14331 modified to emit an explicit thick pointer structure. However, in
14332 this case, the array still has DWARF expressions for its ranges,
14333 and these must be ignored. */
14336 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
14339 gdb_assert (cu
->per_cu
->lang
== language_ada
);
14341 /* Check for a structure with two children. */
14342 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14345 /* Check for P_ARRAY and P_BOUNDS members. */
14346 if (type
->field (0).name () == NULL
14347 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
14348 || type
->field (1).name () == NULL
14349 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
14352 /* Make sure we're looking at a pointer to an array. */
14353 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14356 /* The Ada code already knows how to handle these types, so all that
14357 we need to do is turn the bounds into static bounds. However, we
14358 don't want to rewrite existing array or index types in-place,
14359 because those may be referenced in other contexts where this
14360 rewriting is undesirable. */
14361 struct type
*new_ary_type
14362 = rewrite_array_type (TYPE_TARGET_TYPE (type
->field (0).type ()));
14363 if (new_ary_type
!= nullptr)
14364 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
14367 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14368 appropriate error checking and issuing complaints if there is a
14372 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
14374 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
14376 if (attr
== nullptr)
14379 if (!attr
->form_is_constant ())
14381 complaint (_("DW_AT_alignment must have constant form"
14382 " - DIE at %s [in module %s]"),
14383 sect_offset_str (die
->sect_off
),
14384 objfile_name (cu
->per_objfile
->objfile
));
14388 LONGEST val
= attr
->constant_value (0);
14391 complaint (_("DW_AT_alignment value must not be negative"
14392 " - DIE at %s [in module %s]"),
14393 sect_offset_str (die
->sect_off
),
14394 objfile_name (cu
->per_objfile
->objfile
));
14397 ULONGEST align
= val
;
14401 complaint (_("DW_AT_alignment value must not be zero"
14402 " - DIE at %s [in module %s]"),
14403 sect_offset_str (die
->sect_off
),
14404 objfile_name (cu
->per_objfile
->objfile
));
14407 if ((align
& (align
- 1)) != 0)
14409 complaint (_("DW_AT_alignment value must be a power of 2"
14410 " - DIE at %s [in module %s]"),
14411 sect_offset_str (die
->sect_off
),
14412 objfile_name (cu
->per_objfile
->objfile
));
14419 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14420 the alignment for TYPE. */
14423 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
14426 if (!set_type_align (type
, get_alignment (cu
, die
)))
14427 complaint (_("DW_AT_alignment value too large"
14428 " - DIE at %s [in module %s]"),
14429 sect_offset_str (die
->sect_off
),
14430 objfile_name (cu
->per_objfile
->objfile
));
14433 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14434 constant for a type, according to DWARF5 spec, Table 5.5. */
14437 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
14442 case DW_CC_pass_by_reference
:
14443 case DW_CC_pass_by_value
:
14447 complaint (_("unrecognized DW_AT_calling_convention value "
14448 "(%s) for a type"), pulongest (value
));
14453 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14454 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14455 also according to GNU-specific values (see include/dwarf2.h). */
14458 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
14463 case DW_CC_program
:
14467 case DW_CC_GNU_renesas_sh
:
14468 case DW_CC_GNU_borland_fastcall_i386
:
14469 case DW_CC_GDB_IBM_OpenCL
:
14473 complaint (_("unrecognized DW_AT_calling_convention value "
14474 "(%s) for a subroutine"), pulongest (value
));
14479 /* Called when we find the DIE that starts a structure or union scope
14480 (definition) to create a type for the structure or union. Fill in
14481 the type's name and general properties; the members will not be
14482 processed until process_structure_scope. A symbol table entry for
14483 the type will also not be done until process_structure_scope (assuming
14484 the type has a name).
14486 NOTE: we need to call these functions regardless of whether or not the
14487 DIE has a DW_AT_name attribute, since it might be an anonymous
14488 structure or union. This gets the type entered into our set of
14489 user defined types. */
14491 static struct type
*
14492 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14494 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14496 struct attribute
*attr
;
14499 /* If the definition of this type lives in .debug_types, read that type.
14500 Don't follow DW_AT_specification though, that will take us back up
14501 the chain and we want to go down. */
14502 attr
= die
->attr (DW_AT_signature
);
14503 if (attr
!= nullptr)
14505 type
= get_DW_AT_signature_type (die
, attr
, cu
);
14507 /* The type's CU may not be the same as CU.
14508 Ensure TYPE is recorded with CU in die_type_hash. */
14509 return set_die_type (die
, type
, cu
);
14512 type
= alloc_type (objfile
);
14513 INIT_CPLUS_SPECIFIC (type
);
14515 name
= dwarf2_name (die
, cu
);
14518 if (cu
->per_cu
->lang
== language_cplus
14519 || cu
->per_cu
->lang
== language_d
14520 || cu
->per_cu
->lang
== language_rust
)
14522 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
14524 /* dwarf2_full_name might have already finished building the DIE's
14525 type. If so, there is no need to continue. */
14526 if (get_die_type (die
, cu
) != NULL
)
14527 return get_die_type (die
, cu
);
14529 type
->set_name (full_name
);
14533 /* The name is already allocated along with this objfile, so
14534 we don't need to duplicate it for the type. */
14535 type
->set_name (name
);
14539 if (die
->tag
== DW_TAG_structure_type
)
14541 type
->set_code (TYPE_CODE_STRUCT
);
14543 else if (die
->tag
== DW_TAG_union_type
)
14545 type
->set_code (TYPE_CODE_UNION
);
14547 else if (die
->tag
== DW_TAG_namelist
)
14549 type
->set_code (TYPE_CODE_NAMELIST
);
14553 type
->set_code (TYPE_CODE_STRUCT
);
14556 if (cu
->per_cu
->lang
== language_cplus
&& die
->tag
== DW_TAG_class_type
)
14557 type
->set_is_declared_class (true);
14559 /* Store the calling convention in the type if it's available in
14560 the die. Otherwise the calling convention remains set to
14561 the default value DW_CC_normal. */
14562 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14563 if (attr
!= nullptr
14564 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
14566 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14567 TYPE_CPLUS_CALLING_CONVENTION (type
)
14568 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
14571 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14572 if (attr
!= nullptr)
14574 if (attr
->form_is_constant ())
14575 TYPE_LENGTH (type
) = attr
->constant_value (0);
14578 struct dynamic_prop prop
;
14579 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
14580 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
14581 TYPE_LENGTH (type
) = 0;
14586 TYPE_LENGTH (type
) = 0;
14589 maybe_set_alignment (cu
, die
, type
);
14591 if (producer_is_icc_lt_14 (cu
) && (TYPE_LENGTH (type
) == 0))
14593 /* ICC<14 does not output the required DW_AT_declaration on
14594 incomplete types, but gives them a size of zero. */
14595 type
->set_is_stub (true);
14598 type
->set_stub_is_supported (true);
14600 if (die_is_declaration (die
, cu
))
14601 type
->set_is_stub (true);
14602 else if (attr
== NULL
&& die
->child
== NULL
14603 && producer_is_realview (cu
->producer
))
14604 /* RealView does not output the required DW_AT_declaration
14605 on incomplete types. */
14606 type
->set_is_stub (true);
14608 /* We need to add the type field to the die immediately so we don't
14609 infinitely recurse when dealing with pointers to the structure
14610 type within the structure itself. */
14611 set_die_type (die
, type
, cu
);
14613 /* set_die_type should be already done. */
14614 set_descriptive_type (type
, die
, cu
);
14619 static void handle_struct_member_die
14620 (struct die_info
*child_die
,
14622 struct field_info
*fi
,
14623 std::vector
<struct symbol
*> *template_args
,
14624 struct dwarf2_cu
*cu
);
14626 /* A helper for handle_struct_member_die that handles
14627 DW_TAG_variant_part. */
14630 handle_variant_part (struct die_info
*die
, struct type
*type
,
14631 struct field_info
*fi
,
14632 std::vector
<struct symbol
*> *template_args
,
14633 struct dwarf2_cu
*cu
)
14635 variant_part_builder
*new_part
;
14636 if (fi
->current_variant_part
== nullptr)
14638 fi
->variant_parts
.emplace_back ();
14639 new_part
= &fi
->variant_parts
.back ();
14641 else if (!fi
->current_variant_part
->processing_variant
)
14643 complaint (_("nested DW_TAG_variant_part seen "
14644 "- DIE at %s [in module %s]"),
14645 sect_offset_str (die
->sect_off
),
14646 objfile_name (cu
->per_objfile
->objfile
));
14651 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
14652 current
.variant_parts
.emplace_back ();
14653 new_part
= ¤t
.variant_parts
.back ();
14656 /* When we recurse, we want callees to add to this new variant
14658 scoped_restore save_current_variant_part
14659 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
14661 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
14664 /* It's a univariant form, an extension we support. */
14666 else if (discr
->form_is_ref ())
14668 struct dwarf2_cu
*target_cu
= cu
;
14669 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
14671 new_part
->discriminant_offset
= target_die
->sect_off
;
14675 complaint (_("DW_AT_discr does not have DIE reference form"
14676 " - DIE at %s [in module %s]"),
14677 sect_offset_str (die
->sect_off
),
14678 objfile_name (cu
->per_objfile
->objfile
));
14681 for (die_info
*child_die
= die
->child
;
14683 child_die
= child_die
->sibling
)
14684 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
14687 /* A helper for handle_struct_member_die that handles
14691 handle_variant (struct die_info
*die
, struct type
*type
,
14692 struct field_info
*fi
,
14693 std::vector
<struct symbol
*> *template_args
,
14694 struct dwarf2_cu
*cu
)
14696 if (fi
->current_variant_part
== nullptr)
14698 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14699 "- DIE at %s [in module %s]"),
14700 sect_offset_str (die
->sect_off
),
14701 objfile_name (cu
->per_objfile
->objfile
));
14704 if (fi
->current_variant_part
->processing_variant
)
14706 complaint (_("nested DW_TAG_variant seen "
14707 "- DIE at %s [in module %s]"),
14708 sect_offset_str (die
->sect_off
),
14709 objfile_name (cu
->per_objfile
->objfile
));
14713 scoped_restore save_processing_variant
14714 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
14717 fi
->current_variant_part
->variants
.emplace_back ();
14718 variant_field
&variant
= fi
->current_variant_part
->variants
.back ();
14719 variant
.first_field
= fi
->fields
.size ();
14721 /* In a variant we want to get the discriminant and also add a
14722 field for our sole member child. */
14723 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
14724 if (discr
== nullptr || !discr
->form_is_constant ())
14726 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
14727 if (discr
== nullptr || discr
->as_block ()->size
== 0)
14728 variant
.default_branch
= true;
14730 variant
.discr_list_data
= discr
->as_block ();
14733 variant
.discriminant_value
= discr
->constant_value (0);
14735 for (die_info
*variant_child
= die
->child
;
14736 variant_child
!= NULL
;
14737 variant_child
= variant_child
->sibling
)
14738 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
14740 variant
.last_field
= fi
->fields
.size ();
14743 /* A helper for process_structure_scope that handles a single member
14747 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
14748 struct field_info
*fi
,
14749 std::vector
<struct symbol
*> *template_args
,
14750 struct dwarf2_cu
*cu
)
14752 if (child_die
->tag
== DW_TAG_member
14753 || child_die
->tag
== DW_TAG_variable
14754 || child_die
->tag
== DW_TAG_namelist_item
)
14756 /* NOTE: carlton/2002-11-05: A C++ static data member
14757 should be a DW_TAG_member that is a declaration, but
14758 all versions of G++ as of this writing (so through at
14759 least 3.2.1) incorrectly generate DW_TAG_variable
14760 tags for them instead. */
14761 dwarf2_add_field (fi
, child_die
, cu
);
14763 else if (child_die
->tag
== DW_TAG_subprogram
)
14765 /* Rust doesn't have member functions in the C++ sense.
14766 However, it does emit ordinary functions as children
14767 of a struct DIE. */
14768 if (cu
->per_cu
->lang
== language_rust
)
14769 read_func_scope (child_die
, cu
);
14772 /* C++ member function. */
14773 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
14776 else if (child_die
->tag
== DW_TAG_inheritance
)
14778 /* C++ base class field. */
14779 dwarf2_add_field (fi
, child_die
, cu
);
14781 else if (type_can_define_types (child_die
))
14782 dwarf2_add_type_defn (fi
, child_die
, cu
);
14783 else if (child_die
->tag
== DW_TAG_template_type_param
14784 || child_die
->tag
== DW_TAG_template_value_param
)
14786 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
14789 template_args
->push_back (arg
);
14791 else if (child_die
->tag
== DW_TAG_variant_part
)
14792 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
14793 else if (child_die
->tag
== DW_TAG_variant
)
14794 handle_variant (child_die
, type
, fi
, template_args
, cu
);
14797 /* Finish creating a structure or union type, including filling in its
14798 members and creating a symbol for it. This function also handles Fortran
14799 namelist variables, their items or members and creating a symbol for
14803 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
14805 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14806 struct die_info
*child_die
;
14809 type
= get_die_type (die
, cu
);
14811 type
= read_structure_type (die
, cu
);
14813 bool has_template_parameters
= false;
14814 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
14816 struct field_info fi
;
14817 std::vector
<struct symbol
*> template_args
;
14819 child_die
= die
->child
;
14821 while (child_die
&& child_die
->tag
)
14823 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
14824 child_die
= child_die
->sibling
;
14827 /* Attach template arguments to type. */
14828 if (!template_args
.empty ())
14830 has_template_parameters
= true;
14831 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14832 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
14833 TYPE_TEMPLATE_ARGUMENTS (type
)
14834 = XOBNEWVEC (&objfile
->objfile_obstack
,
14836 TYPE_N_TEMPLATE_ARGUMENTS (type
));
14837 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
14838 template_args
.data (),
14839 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
14840 * sizeof (struct symbol
*)));
14843 /* Attach fields and member functions to the type. */
14844 if (fi
.nfields () > 0)
14845 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
14846 if (!fi
.fnfieldlists
.empty ())
14848 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
14850 /* Get the type which refers to the base class (possibly this
14851 class itself) which contains the vtable pointer for the current
14852 class from the DW_AT_containing_type attribute. This use of
14853 DW_AT_containing_type is a GNU extension. */
14855 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14857 struct type
*t
= die_containing_type (die
, cu
);
14859 set_type_vptr_basetype (type
, t
);
14864 /* Our own class provides vtbl ptr. */
14865 for (i
= t
->num_fields () - 1;
14866 i
>= TYPE_N_BASECLASSES (t
);
14869 const char *fieldname
= t
->field (i
).name ();
14871 if (is_vtable_name (fieldname
, cu
))
14873 set_type_vptr_fieldno (type
, i
);
14878 /* Complain if virtual function table field not found. */
14879 if (i
< TYPE_N_BASECLASSES (t
))
14880 complaint (_("virtual function table pointer "
14881 "not found when defining class '%s'"),
14882 type
->name () ? type
->name () : "");
14886 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
14889 else if (cu
->producer
14890 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
14892 /* The IBM XLC compiler does not provide direct indication
14893 of the containing type, but the vtable pointer is
14894 always named __vfp. */
14898 for (i
= type
->num_fields () - 1;
14899 i
>= TYPE_N_BASECLASSES (type
);
14902 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
14904 set_type_vptr_fieldno (type
, i
);
14905 set_type_vptr_basetype (type
, type
);
14912 /* Copy fi.typedef_field_list linked list elements content into the
14913 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14914 if (!fi
.typedef_field_list
.empty ())
14916 int count
= fi
.typedef_field_list
.size ();
14918 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14919 TYPE_TYPEDEF_FIELD_ARRAY (type
)
14920 = ((struct decl_field
*)
14922 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
14923 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
14925 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
14926 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
14929 /* Copy fi.nested_types_list linked list elements content into the
14930 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14931 if (!fi
.nested_types_list
.empty ()
14932 && cu
->per_cu
->lang
!= language_ada
)
14934 int count
= fi
.nested_types_list
.size ();
14936 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14937 TYPE_NESTED_TYPES_ARRAY (type
)
14938 = ((struct decl_field
*)
14939 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
14940 TYPE_NESTED_TYPES_COUNT (type
) = count
;
14942 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
14943 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
14947 quirk_gcc_member_function_pointer (type
, objfile
);
14948 if (cu
->per_cu
->lang
== language_rust
&& die
->tag
== DW_TAG_union_type
)
14949 cu
->rust_unions
.push_back (type
);
14950 else if (cu
->per_cu
->lang
== language_ada
)
14951 quirk_ada_thick_pointer_struct (die
, cu
, type
);
14953 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14954 snapshots) has been known to create a die giving a declaration
14955 for a class that has, as a child, a die giving a definition for a
14956 nested class. So we have to process our children even if the
14957 current die is a declaration. Normally, of course, a declaration
14958 won't have any children at all. */
14960 child_die
= die
->child
;
14962 while (child_die
!= NULL
&& child_die
->tag
)
14964 if (child_die
->tag
== DW_TAG_member
14965 || child_die
->tag
== DW_TAG_variable
14966 || child_die
->tag
== DW_TAG_inheritance
14967 || child_die
->tag
== DW_TAG_template_value_param
14968 || child_die
->tag
== DW_TAG_template_type_param
)
14973 process_die (child_die
, cu
);
14975 child_die
= child_die
->sibling
;
14978 /* Do not consider external references. According to the DWARF standard,
14979 these DIEs are identified by the fact that they have no byte_size
14980 attribute, and a declaration attribute. */
14981 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
14982 || !die_is_declaration (die
, cu
)
14983 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
14985 struct symbol
*sym
= new_symbol (die
, type
, cu
);
14987 if (has_template_parameters
)
14989 struct symtab
*symtab
;
14990 if (sym
!= nullptr)
14991 symtab
= sym
->symtab ();
14992 else if (cu
->line_header
!= nullptr)
14994 /* Any related symtab will do. */
14996 = cu
->line_header
->file_names ()[0].symtab
;
15001 complaint (_("could not find suitable "
15002 "symtab for template parameter"
15003 " - DIE at %s [in module %s]"),
15004 sect_offset_str (die
->sect_off
),
15005 objfile_name (objfile
));
15008 if (symtab
!= nullptr)
15010 /* Make sure that the symtab is set on the new symbols.
15011 Even though they don't appear in this symtab directly,
15012 other parts of gdb assume that symbols do, and this is
15013 reasonably true. */
15014 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
15015 TYPE_TEMPLATE_ARGUMENT (type
, i
)->set_symtab (symtab
);
15021 /* Assuming DIE is an enumeration type, and TYPE is its associated
15022 type, update TYPE using some information only available in DIE's
15023 children. In particular, the fields are computed. */
15026 update_enumeration_type_from_children (struct die_info
*die
,
15028 struct dwarf2_cu
*cu
)
15030 struct die_info
*child_die
;
15031 int unsigned_enum
= 1;
15034 auto_obstack obstack
;
15035 std::vector
<struct field
> fields
;
15037 for (child_die
= die
->child
;
15038 child_die
!= NULL
&& child_die
->tag
;
15039 child_die
= child_die
->sibling
)
15041 struct attribute
*attr
;
15043 const gdb_byte
*bytes
;
15044 struct dwarf2_locexpr_baton
*baton
;
15047 if (child_die
->tag
!= DW_TAG_enumerator
)
15050 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
15054 name
= dwarf2_name (child_die
, cu
);
15056 name
= "<anonymous enumerator>";
15058 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
15059 &value
, &bytes
, &baton
);
15067 if (count_one_bits_ll (value
) >= 2)
15071 fields
.emplace_back ();
15072 struct field
&field
= fields
.back ();
15073 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
15074 field
.set_loc_enumval (value
);
15077 if (!fields
.empty ())
15079 type
->set_num_fields (fields
.size ());
15082 TYPE_ALLOC (type
, sizeof (struct field
) * fields
.size ()));
15083 memcpy (type
->fields (), fields
.data (),
15084 sizeof (struct field
) * fields
.size ());
15088 type
->set_is_unsigned (true);
15091 type
->set_is_flag_enum (true);
15094 /* Given a DW_AT_enumeration_type die, set its type. We do not
15095 complete the type's fields yet, or create any symbols. */
15097 static struct type
*
15098 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15100 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15102 struct attribute
*attr
;
15105 /* If the definition of this type lives in .debug_types, read that type.
15106 Don't follow DW_AT_specification though, that will take us back up
15107 the chain and we want to go down. */
15108 attr
= die
->attr (DW_AT_signature
);
15109 if (attr
!= nullptr)
15111 type
= get_DW_AT_signature_type (die
, attr
, cu
);
15113 /* The type's CU may not be the same as CU.
15114 Ensure TYPE is recorded with CU in die_type_hash. */
15115 return set_die_type (die
, type
, cu
);
15118 type
= alloc_type (objfile
);
15120 type
->set_code (TYPE_CODE_ENUM
);
15121 name
= dwarf2_full_name (NULL
, die
, cu
);
15123 type
->set_name (name
);
15125 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
15128 struct type
*underlying_type
= die_type (die
, cu
);
15130 TYPE_TARGET_TYPE (type
) = underlying_type
;
15133 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15134 if (attr
!= nullptr)
15136 TYPE_LENGTH (type
) = attr
->constant_value (0);
15140 TYPE_LENGTH (type
) = 0;
15143 maybe_set_alignment (cu
, die
, type
);
15145 /* The enumeration DIE can be incomplete. In Ada, any type can be
15146 declared as private in the package spec, and then defined only
15147 inside the package body. Such types are known as Taft Amendment
15148 Types. When another package uses such a type, an incomplete DIE
15149 may be generated by the compiler. */
15150 if (die_is_declaration (die
, cu
))
15151 type
->set_is_stub (true);
15153 /* If this type has an underlying type that is not a stub, then we
15154 may use its attributes. We always use the "unsigned" attribute
15155 in this situation, because ordinarily we guess whether the type
15156 is unsigned -- but the guess can be wrong and the underlying type
15157 can tell us the reality. However, we defer to a local size
15158 attribute if one exists, because this lets the compiler override
15159 the underlying type if needed. */
15160 if (TYPE_TARGET_TYPE (type
) != NULL
&& !TYPE_TARGET_TYPE (type
)->is_stub ())
15162 struct type
*underlying_type
= TYPE_TARGET_TYPE (type
);
15163 underlying_type
= check_typedef (underlying_type
);
15165 type
->set_is_unsigned (underlying_type
->is_unsigned ());
15167 if (TYPE_LENGTH (type
) == 0)
15168 TYPE_LENGTH (type
) = TYPE_LENGTH (underlying_type
);
15170 if (TYPE_RAW_ALIGN (type
) == 0
15171 && TYPE_RAW_ALIGN (underlying_type
) != 0)
15172 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
15175 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
15177 set_die_type (die
, type
, cu
);
15179 /* Finish the creation of this type by using the enum's children.
15180 Note that, as usual, this must come after set_die_type to avoid
15181 infinite recursion when trying to compute the names of the
15183 update_enumeration_type_from_children (die
, type
, cu
);
15188 /* Given a pointer to a die which begins an enumeration, process all
15189 the dies that define the members of the enumeration, and create the
15190 symbol for the enumeration type.
15192 NOTE: We reverse the order of the element list. */
15195 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
15197 struct type
*this_type
;
15199 this_type
= get_die_type (die
, cu
);
15200 if (this_type
== NULL
)
15201 this_type
= read_enumeration_type (die
, cu
);
15203 if (die
->child
!= NULL
)
15205 struct die_info
*child_die
;
15208 child_die
= die
->child
;
15209 while (child_die
&& child_die
->tag
)
15211 if (child_die
->tag
!= DW_TAG_enumerator
)
15213 process_die (child_die
, cu
);
15217 name
= dwarf2_name (child_die
, cu
);
15219 new_symbol (child_die
, this_type
, cu
);
15222 child_die
= child_die
->sibling
;
15226 /* If we are reading an enum from a .debug_types unit, and the enum
15227 is a declaration, and the enum is not the signatured type in the
15228 unit, then we do not want to add a symbol for it. Adding a
15229 symbol would in some cases obscure the true definition of the
15230 enum, giving users an incomplete type when the definition is
15231 actually available. Note that we do not want to do this for all
15232 enums which are just declarations, because C++0x allows forward
15233 enum declarations. */
15234 if (cu
->per_cu
->is_debug_types
15235 && die_is_declaration (die
, cu
))
15237 struct signatured_type
*sig_type
;
15239 sig_type
= (struct signatured_type
*) cu
->per_cu
;
15240 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
15241 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
15245 new_symbol (die
, this_type
, cu
);
15248 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15249 expression for an index type and finds the corresponding field
15250 offset in the hidden "P_BOUNDS" structure. Returns true on success
15251 and updates *FIELD, false if it fails to recognize an
15255 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
15256 int *bounds_offset
, struct field
*field
,
15257 struct dwarf2_cu
*cu
)
15259 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
15260 if (attr
== nullptr || !attr
->form_is_block ())
15263 const struct dwarf_block
*block
= attr
->as_block ();
15264 const gdb_byte
*start
= block
->data
;
15265 const gdb_byte
*end
= block
->data
+ block
->size
;
15267 /* The expression to recognize generally looks like:
15269 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15270 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15272 However, the second "plus_uconst" may be missing:
15274 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15275 DW_OP_deref_size: 4)
15277 This happens when the field is at the start of the structure.
15279 Also, the final deref may not be sized:
15281 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15284 This happens when the size of the index type happens to be the
15285 same as the architecture's word size. This can occur with or
15286 without the second plus_uconst. */
15288 if (end
- start
< 2)
15290 if (*start
++ != DW_OP_push_object_address
)
15292 if (*start
++ != DW_OP_plus_uconst
)
15295 uint64_t this_bound_off
;
15296 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
15297 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
15299 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15300 is consistent among all bounds. */
15301 if (*bounds_offset
== -1)
15302 *bounds_offset
= this_bound_off
;
15303 else if (*bounds_offset
!= this_bound_off
)
15306 if (start
== end
|| *start
++ != DW_OP_deref
)
15312 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
15314 /* This means an offset of 0. */
15316 else if (*start
++ != DW_OP_plus_uconst
)
15320 /* The size is the parameter to DW_OP_plus_uconst. */
15322 start
= gdb_read_uleb128 (start
, end
, &val
);
15323 if (start
== nullptr)
15325 if ((int) val
!= val
)
15334 if (*start
== DW_OP_deref_size
)
15336 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
15337 if (start
== nullptr)
15340 else if (*start
== DW_OP_deref
)
15342 size
= cu
->header
.addr_size
;
15348 field
->set_loc_bitpos (8 * offset
);
15349 if (size
!= TYPE_LENGTH (field
->type ()))
15350 FIELD_BITSIZE (*field
) = 8 * size
;
15355 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15356 some kinds of Ada arrays:
15358 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15359 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15360 <11e0> DW_AT_data_location: 2 byte block: 97 6
15361 (DW_OP_push_object_address; DW_OP_deref)
15362 <11e3> DW_AT_type : <0x1173>
15363 <11e7> DW_AT_sibling : <0x1201>
15364 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15365 <11ec> DW_AT_type : <0x1206>
15366 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15367 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15368 DW_OP_deref_size: 4)
15369 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15370 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15371 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15373 This actually represents a "thick pointer", which is a structure
15374 with two elements: one that is a pointer to the array data, and one
15375 that is a pointer to another structure; this second structure holds
15378 This returns a new type on success, or nullptr if this didn't
15379 recognize the type. */
15381 static struct type
*
15382 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
15385 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
15386 /* So far we've only seen this with block form. */
15387 if (attr
== nullptr || !attr
->form_is_block ())
15390 /* Note that this will fail if the structure layout is changed by
15391 the compiler. However, we have no good way to recognize some
15392 other layout, because we don't know what expression the compiler
15393 might choose to emit should this happen. */
15394 struct dwarf_block
*blk
= attr
->as_block ();
15396 || blk
->data
[0] != DW_OP_push_object_address
15397 || blk
->data
[1] != DW_OP_deref
)
15400 int bounds_offset
= -1;
15401 int max_align
= -1;
15402 std::vector
<struct field
> range_fields
;
15403 for (struct die_info
*child_die
= die
->child
;
15405 child_die
= child_die
->sibling
)
15407 if (child_die
->tag
== DW_TAG_subrange_type
)
15409 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
15411 int this_align
= type_align (underlying
);
15412 if (this_align
> max_align
)
15413 max_align
= this_align
;
15415 range_fields
.emplace_back ();
15416 range_fields
.emplace_back ();
15418 struct field
&lower
= range_fields
[range_fields
.size () - 2];
15419 struct field
&upper
= range_fields
[range_fields
.size () - 1];
15421 lower
.set_type (underlying
);
15422 FIELD_ARTIFICIAL (lower
) = 1;
15424 upper
.set_type (underlying
);
15425 FIELD_ARTIFICIAL (upper
) = 1;
15427 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
15428 &bounds_offset
, &lower
, cu
)
15429 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
15430 &bounds_offset
, &upper
, cu
))
15435 /* This shouldn't really happen, but double-check that we found
15436 where the bounds are stored. */
15437 if (bounds_offset
== -1)
15440 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15441 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15445 /* Set the name of each field in the bounds. */
15446 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
15447 range_fields
[i
].set_name (objfile
->intern (name
));
15448 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
15449 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
15452 struct type
*bounds
= alloc_type (objfile
);
15453 bounds
->set_code (TYPE_CODE_STRUCT
);
15455 bounds
->set_num_fields (range_fields
.size ());
15457 ((struct field
*) TYPE_ALLOC (bounds
, (bounds
->num_fields ()
15458 * sizeof (struct field
))));
15459 memcpy (bounds
->fields (), range_fields
.data (),
15460 bounds
->num_fields () * sizeof (struct field
));
15462 int last_fieldno
= range_fields
.size () - 1;
15463 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
15464 + TYPE_LENGTH (bounds
->field (last_fieldno
).type ()));
15465 TYPE_LENGTH (bounds
) = align_up (bounds_size
, max_align
);
15467 /* Rewrite the existing array type in place. Specifically, we
15468 remove any dynamic properties we might have read, and we replace
15469 the index types. */
15470 struct type
*iter
= type
;
15471 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15473 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
15474 iter
->main_type
->dyn_prop_list
= nullptr;
15475 iter
->set_index_type
15476 (create_static_range_type (NULL
, bounds
->field (i
).type (), 1, 0));
15477 iter
= TYPE_TARGET_TYPE (iter
);
15480 struct type
*result
= alloc_type (objfile
);
15481 result
->set_code (TYPE_CODE_STRUCT
);
15483 result
->set_num_fields (2);
15485 ((struct field
*) TYPE_ZALLOC (result
, (result
->num_fields ()
15486 * sizeof (struct field
))));
15488 /* The names are chosen to coincide with what the compiler does with
15489 -fgnat-encodings=all, which the Ada code in gdb already
15491 result
->field (0).set_name ("P_ARRAY");
15492 result
->field (0).set_type (lookup_pointer_type (type
));
15494 result
->field (1).set_name ("P_BOUNDS");
15495 result
->field (1).set_type (lookup_pointer_type (bounds
));
15496 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
15498 result
->set_name (type
->name ());
15499 TYPE_LENGTH (result
) = (TYPE_LENGTH (result
->field (0).type ())
15500 + TYPE_LENGTH (result
->field (1).type ()));
15505 /* Extract all information from a DW_TAG_array_type DIE and put it in
15506 the DIE's type field. For now, this only handles one dimensional
15509 static struct type
*
15510 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15512 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15513 struct die_info
*child_die
;
15515 struct type
*element_type
, *range_type
, *index_type
;
15516 struct attribute
*attr
;
15518 struct dynamic_prop
*byte_stride_prop
= NULL
;
15519 unsigned int bit_stride
= 0;
15521 element_type
= die_type (die
, cu
);
15523 /* The die_type call above may have already set the type for this DIE. */
15524 type
= get_die_type (die
, cu
);
15528 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15532 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15535 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
15536 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
15540 complaint (_("unable to read array DW_AT_byte_stride "
15541 " - DIE at %s [in module %s]"),
15542 sect_offset_str (die
->sect_off
),
15543 objfile_name (cu
->per_objfile
->objfile
));
15544 /* Ignore this attribute. We will likely not be able to print
15545 arrays of this type correctly, but there is little we can do
15546 to help if we cannot read the attribute's value. */
15547 byte_stride_prop
= NULL
;
15551 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15553 bit_stride
= attr
->constant_value (0);
15555 /* Irix 6.2 native cc creates array types without children for
15556 arrays with unspecified length. */
15557 if (die
->child
== NULL
)
15559 index_type
= objfile_type (objfile
)->builtin_int
;
15560 range_type
= create_static_range_type (NULL
, index_type
, 0, -1);
15561 type
= create_array_type_with_stride (NULL
, element_type
, range_type
,
15562 byte_stride_prop
, bit_stride
);
15563 return set_die_type (die
, type
, cu
);
15566 std::vector
<struct type
*> range_types
;
15567 child_die
= die
->child
;
15568 while (child_die
&& child_die
->tag
)
15570 if (child_die
->tag
== DW_TAG_subrange_type
15571 || child_die
->tag
== DW_TAG_generic_subrange
)
15573 struct type
*child_type
= read_type_die (child_die
, cu
);
15575 if (child_type
!= NULL
)
15577 /* The range type was succesfully read. Save it for the
15578 array type creation. */
15579 range_types
.push_back (child_type
);
15582 child_die
= child_die
->sibling
;
15585 if (range_types
.empty ())
15587 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15588 sect_offset_str (die
->sect_off
),
15589 objfile_name (cu
->per_objfile
->objfile
));
15593 /* Dwarf2 dimensions are output from left to right, create the
15594 necessary array types in backwards order. */
15596 type
= element_type
;
15598 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
15602 while (i
< range_types
.size ())
15604 type
= create_array_type_with_stride (NULL
, type
, range_types
[i
++],
15605 byte_stride_prop
, bit_stride
);
15607 byte_stride_prop
= nullptr;
15612 size_t ndim
= range_types
.size ();
15615 type
= create_array_type_with_stride (NULL
, type
, range_types
[ndim
],
15616 byte_stride_prop
, bit_stride
);
15618 byte_stride_prop
= nullptr;
15622 gdb_assert (type
!= element_type
);
15624 /* Understand Dwarf2 support for vector types (like they occur on
15625 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15626 array type. This is not part of the Dwarf2/3 standard yet, but a
15627 custom vendor extension. The main difference between a regular
15628 array and the vector variant is that vectors are passed by value
15630 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
15631 if (attr
!= nullptr)
15632 make_vector_type (type
);
15634 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15635 implementation may choose to implement triple vectors using this
15637 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15638 if (attr
!= nullptr && attr
->form_is_unsigned ())
15640 if (attr
->as_unsigned () >= TYPE_LENGTH (type
))
15641 TYPE_LENGTH (type
) = attr
->as_unsigned ();
15643 complaint (_("DW_AT_byte_size for array type smaller "
15644 "than the total size of elements"));
15647 name
= dwarf2_name (die
, cu
);
15649 type
->set_name (name
);
15651 maybe_set_alignment (cu
, die
, type
);
15653 struct type
*replacement_type
= nullptr;
15654 if (cu
->per_cu
->lang
== language_ada
)
15656 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
15657 if (replacement_type
!= nullptr)
15658 type
= replacement_type
;
15661 /* Install the type in the die. */
15662 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
15664 /* set_die_type should be already done. */
15665 set_descriptive_type (type
, die
, cu
);
15670 static enum dwarf_array_dim_ordering
15671 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
15673 struct attribute
*attr
;
15675 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
15677 if (attr
!= nullptr)
15679 LONGEST val
= attr
->constant_value (-1);
15680 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
15681 return (enum dwarf_array_dim_ordering
) val
;
15684 /* GNU F77 is a special case, as at 08/2004 array type info is the
15685 opposite order to the dwarf2 specification, but data is still
15686 laid out as per normal fortran.
15688 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15689 version checking. */
15691 if (cu
->per_cu
->lang
== language_fortran
15692 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
15694 return DW_ORD_row_major
;
15697 switch (cu
->language_defn
->array_ordering ())
15699 case array_column_major
:
15700 return DW_ORD_col_major
;
15701 case array_row_major
:
15703 return DW_ORD_row_major
;
15707 /* Extract all information from a DW_TAG_set_type DIE and put it in
15708 the DIE's type field. */
15710 static struct type
*
15711 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15713 struct type
*domain_type
, *set_type
;
15714 struct attribute
*attr
;
15716 domain_type
= die_type (die
, cu
);
15718 /* The die_type call above may have already set the type for this DIE. */
15719 set_type
= get_die_type (die
, cu
);
15723 set_type
= create_set_type (NULL
, domain_type
);
15725 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15726 if (attr
!= nullptr && attr
->form_is_unsigned ())
15727 TYPE_LENGTH (set_type
) = attr
->as_unsigned ();
15729 maybe_set_alignment (cu
, die
, set_type
);
15731 return set_die_type (die
, set_type
, cu
);
15734 /* A helper for read_common_block that creates a locexpr baton.
15735 SYM is the symbol which we are marking as computed.
15736 COMMON_DIE is the DIE for the common block.
15737 COMMON_LOC is the location expression attribute for the common
15739 MEMBER_LOC is the location expression attribute for the particular
15740 member of the common block that we are processing.
15741 CU is the CU from which the above come. */
15744 mark_common_block_symbol_computed (struct symbol
*sym
,
15745 struct die_info
*common_die
,
15746 struct attribute
*common_loc
,
15747 struct attribute
*member_loc
,
15748 struct dwarf2_cu
*cu
)
15750 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15751 struct objfile
*objfile
= per_objfile
->objfile
;
15752 struct dwarf2_locexpr_baton
*baton
;
15754 unsigned int cu_off
;
15755 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
15756 LONGEST offset
= 0;
15758 gdb_assert (common_loc
&& member_loc
);
15759 gdb_assert (common_loc
->form_is_block ());
15760 gdb_assert (member_loc
->form_is_block ()
15761 || member_loc
->form_is_constant ());
15763 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
15764 baton
->per_objfile
= per_objfile
;
15765 baton
->per_cu
= cu
->per_cu
;
15766 gdb_assert (baton
->per_cu
);
15768 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15770 if (member_loc
->form_is_constant ())
15772 offset
= member_loc
->constant_value (0);
15773 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
15776 baton
->size
+= member_loc
->as_block ()->size
;
15778 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
15781 *ptr
++ = DW_OP_call4
;
15782 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
15783 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
15786 if (member_loc
->form_is_constant ())
15788 *ptr
++ = DW_OP_addr
;
15789 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
15790 ptr
+= cu
->header
.addr_size
;
15794 /* We have to copy the data here, because DW_OP_call4 will only
15795 use a DW_AT_location attribute. */
15796 struct dwarf_block
*block
= member_loc
->as_block ();
15797 memcpy (ptr
, block
->data
, block
->size
);
15798 ptr
+= block
->size
;
15801 *ptr
++ = DW_OP_plus
;
15802 gdb_assert (ptr
- baton
->data
== baton
->size
);
15804 SYMBOL_LOCATION_BATON (sym
) = baton
;
15805 sym
->set_aclass_index (dwarf2_locexpr_index
);
15808 /* Create appropriate locally-scoped variables for all the
15809 DW_TAG_common_block entries. Also create a struct common_block
15810 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15811 is used to separate the common blocks name namespace from regular
15815 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
15817 struct attribute
*attr
;
15819 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
15820 if (attr
!= nullptr)
15822 /* Support the .debug_loc offsets. */
15823 if (attr
->form_is_block ())
15827 else if (attr
->form_is_section_offset ())
15829 dwarf2_complex_location_expr_complaint ();
15834 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15835 "common block member");
15840 if (die
->child
!= NULL
)
15842 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15843 struct die_info
*child_die
;
15844 size_t n_entries
= 0, size
;
15845 struct common_block
*common_block
;
15846 struct symbol
*sym
;
15848 for (child_die
= die
->child
;
15849 child_die
&& child_die
->tag
;
15850 child_die
= child_die
->sibling
)
15853 size
= (sizeof (struct common_block
)
15854 + (n_entries
- 1) * sizeof (struct symbol
*));
15856 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
15858 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
15859 common_block
->n_entries
= 0;
15861 for (child_die
= die
->child
;
15862 child_die
&& child_die
->tag
;
15863 child_die
= child_die
->sibling
)
15865 /* Create the symbol in the DW_TAG_common_block block in the current
15867 sym
= new_symbol (child_die
, NULL
, cu
);
15870 struct attribute
*member_loc
;
15872 common_block
->contents
[common_block
->n_entries
++] = sym
;
15874 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
15878 /* GDB has handled this for a long time, but it is
15879 not specified by DWARF. It seems to have been
15880 emitted by gfortran at least as recently as:
15881 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15882 complaint (_("Variable in common block has "
15883 "DW_AT_data_member_location "
15884 "- DIE at %s [in module %s]"),
15885 sect_offset_str (child_die
->sect_off
),
15886 objfile_name (objfile
));
15888 if (member_loc
->form_is_section_offset ())
15889 dwarf2_complex_location_expr_complaint ();
15890 else if (member_loc
->form_is_constant ()
15891 || member_loc
->form_is_block ())
15893 if (attr
!= nullptr)
15894 mark_common_block_symbol_computed (sym
, die
, attr
,
15898 dwarf2_complex_location_expr_complaint ();
15903 sym
= new_symbol (die
, objfile_type (objfile
)->builtin_void
, cu
);
15904 sym
->set_value_common_block (common_block
);
15908 /* Create a type for a C++ namespace. */
15910 static struct type
*
15911 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15913 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15914 const char *previous_prefix
, *name
;
15918 /* For extensions, reuse the type of the original namespace. */
15919 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
15921 struct die_info
*ext_die
;
15922 struct dwarf2_cu
*ext_cu
= cu
;
15924 ext_die
= dwarf2_extension (die
, &ext_cu
);
15925 type
= read_type_die (ext_die
, ext_cu
);
15927 /* EXT_CU may not be the same as CU.
15928 Ensure TYPE is recorded with CU in die_type_hash. */
15929 return set_die_type (die
, type
, cu
);
15932 name
= namespace_name (die
, &is_anonymous
, cu
);
15934 /* Now build the name of the current namespace. */
15936 previous_prefix
= determine_prefix (die
, cu
);
15937 if (previous_prefix
[0] != '\0')
15938 name
= typename_concat (&objfile
->objfile_obstack
,
15939 previous_prefix
, name
, 0, cu
);
15941 /* Create the type. */
15942 type
= init_type (objfile
, TYPE_CODE_NAMESPACE
, 0, name
);
15944 return set_die_type (die
, type
, cu
);
15947 /* Read a namespace scope. */
15950 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
15952 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15955 /* Add a symbol associated to this if we haven't seen the namespace
15956 before. Also, add a using directive if it's an anonymous
15959 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
15963 type
= read_type_die (die
, cu
);
15964 new_symbol (die
, type
, cu
);
15966 namespace_name (die
, &is_anonymous
, cu
);
15969 const char *previous_prefix
= determine_prefix (die
, cu
);
15971 std::vector
<const char *> excludes
;
15972 add_using_directive (using_directives (cu
),
15973 previous_prefix
, type
->name (), NULL
,
15974 NULL
, excludes
, 0, &objfile
->objfile_obstack
);
15978 if (die
->child
!= NULL
)
15980 struct die_info
*child_die
= die
->child
;
15982 while (child_die
&& child_die
->tag
)
15984 process_die (child_die
, cu
);
15985 child_die
= child_die
->sibling
;
15990 /* Read a Fortran module as type. This DIE can be only a declaration used for
15991 imported module. Still we need that type as local Fortran "use ... only"
15992 declaration imports depend on the created type in determine_prefix. */
15994 static struct type
*
15995 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15997 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15998 const char *module_name
;
16001 module_name
= dwarf2_name (die
, cu
);
16002 type
= init_type (objfile
, TYPE_CODE_MODULE
, 0, module_name
);
16004 return set_die_type (die
, type
, cu
);
16007 /* Read a Fortran module. */
16010 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
16012 struct die_info
*child_die
= die
->child
;
16015 type
= read_type_die (die
, cu
);
16016 new_symbol (die
, type
, cu
);
16018 while (child_die
&& child_die
->tag
)
16020 process_die (child_die
, cu
);
16021 child_die
= child_die
->sibling
;
16025 /* Return the name of the namespace represented by DIE. Set
16026 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16029 static const char *
16030 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
16032 struct die_info
*current_die
;
16033 const char *name
= NULL
;
16035 /* Loop through the extensions until we find a name. */
16037 for (current_die
= die
;
16038 current_die
!= NULL
;
16039 current_die
= dwarf2_extension (die
, &cu
))
16041 /* We don't use dwarf2_name here so that we can detect the absence
16042 of a name -> anonymous namespace. */
16043 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
16049 /* Is it an anonymous namespace? */
16051 *is_anonymous
= (name
== NULL
);
16053 name
= CP_ANONYMOUS_NAMESPACE_STR
;
16058 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16059 the user defined type vector. */
16061 static struct type
*
16062 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16064 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
16065 struct comp_unit_head
*cu_header
= &cu
->header
;
16067 struct attribute
*attr_byte_size
;
16068 struct attribute
*attr_address_class
;
16069 int byte_size
, addr_class
;
16070 struct type
*target_type
;
16072 target_type
= die_type (die
, cu
);
16074 /* The die_type call above may have already set the type for this DIE. */
16075 type
= get_die_type (die
, cu
);
16079 type
= lookup_pointer_type (target_type
);
16081 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16082 if (attr_byte_size
)
16083 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
16085 byte_size
= cu_header
->addr_size
;
16087 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
16088 if (attr_address_class
)
16089 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
16091 addr_class
= DW_ADDR_none
;
16093 ULONGEST alignment
= get_alignment (cu
, die
);
16095 /* If the pointer size, alignment, or address class is different
16096 than the default, create a type variant marked as such and set
16097 the length accordingly. */
16098 if (TYPE_LENGTH (type
) != byte_size
16099 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
16100 && alignment
!= TYPE_RAW_ALIGN (type
))
16101 || addr_class
!= DW_ADDR_none
)
16103 if (gdbarch_address_class_type_flags_p (gdbarch
))
16105 type_instance_flags type_flags
16106 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
16108 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
16110 type
= make_type_with_address_space (type
, type_flags
);
16112 else if (TYPE_LENGTH (type
) != byte_size
)
16114 complaint (_("invalid pointer size %d"), byte_size
);
16116 else if (TYPE_RAW_ALIGN (type
) != alignment
)
16118 complaint (_("Invalid DW_AT_alignment"
16119 " - DIE at %s [in module %s]"),
16120 sect_offset_str (die
->sect_off
),
16121 objfile_name (cu
->per_objfile
->objfile
));
16125 /* Should we also complain about unhandled address classes? */
16129 TYPE_LENGTH (type
) = byte_size
;
16130 set_type_align (type
, alignment
);
16131 return set_die_type (die
, type
, cu
);
16134 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16135 the user defined type vector. */
16137 static struct type
*
16138 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16141 struct type
*to_type
;
16142 struct type
*domain
;
16144 to_type
= die_type (die
, cu
);
16145 domain
= die_containing_type (die
, cu
);
16147 /* The calls above may have already set the type for this DIE. */
16148 type
= get_die_type (die
, cu
);
16152 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
16153 type
= lookup_methodptr_type (to_type
);
16154 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
16156 struct type
*new_type
= alloc_type (cu
->per_objfile
->objfile
);
16158 smash_to_method_type (new_type
, domain
, TYPE_TARGET_TYPE (to_type
),
16159 to_type
->fields (), to_type
->num_fields (),
16160 to_type
->has_varargs ());
16161 type
= lookup_methodptr_type (new_type
);
16164 type
= lookup_memberptr_type (to_type
, domain
);
16166 return set_die_type (die
, type
, cu
);
16169 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16170 the user defined type vector. */
16172 static struct type
*
16173 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16174 enum type_code refcode
)
16176 struct comp_unit_head
*cu_header
= &cu
->header
;
16177 struct type
*type
, *target_type
;
16178 struct attribute
*attr
;
16180 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
16182 target_type
= die_type (die
, cu
);
16184 /* The die_type call above may have already set the type for this DIE. */
16185 type
= get_die_type (die
, cu
);
16189 type
= lookup_reference_type (target_type
, refcode
);
16190 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16191 if (attr
!= nullptr)
16193 TYPE_LENGTH (type
) = attr
->constant_value (cu_header
->addr_size
);
16197 TYPE_LENGTH (type
) = cu_header
->addr_size
;
16199 maybe_set_alignment (cu
, die
, type
);
16200 return set_die_type (die
, type
, cu
);
16203 /* Add the given cv-qualifiers to the element type of the array. GCC
16204 outputs DWARF type qualifiers that apply to an array, not the
16205 element type. But GDB relies on the array element type to carry
16206 the cv-qualifiers. This mimics section 6.7.3 of the C99
16209 static struct type
*
16210 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16211 struct type
*base_type
, int cnst
, int voltl
)
16213 struct type
*el_type
, *inner_array
;
16215 base_type
= copy_type (base_type
);
16216 inner_array
= base_type
;
16218 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
16220 TYPE_TARGET_TYPE (inner_array
) =
16221 copy_type (TYPE_TARGET_TYPE (inner_array
));
16222 inner_array
= TYPE_TARGET_TYPE (inner_array
);
16225 el_type
= TYPE_TARGET_TYPE (inner_array
);
16226 cnst
|= TYPE_CONST (el_type
);
16227 voltl
|= TYPE_VOLATILE (el_type
);
16228 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
16230 return set_die_type (die
, base_type
, cu
);
16233 static struct type
*
16234 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16236 struct type
*base_type
, *cv_type
;
16238 base_type
= die_type (die
, cu
);
16240 /* The die_type call above may have already set the type for this DIE. */
16241 cv_type
= get_die_type (die
, cu
);
16245 /* In case the const qualifier is applied to an array type, the element type
16246 is so qualified, not the array type (section 6.7.3 of C99). */
16247 if (base_type
->code () == TYPE_CODE_ARRAY
)
16248 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
16250 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
16251 return set_die_type (die
, cv_type
, cu
);
16254 static struct type
*
16255 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16257 struct type
*base_type
, *cv_type
;
16259 base_type
= die_type (die
, cu
);
16261 /* The die_type call above may have already set the type for this DIE. */
16262 cv_type
= get_die_type (die
, cu
);
16266 /* In case the volatile qualifier is applied to an array type, the
16267 element type is so qualified, not the array type (section 6.7.3
16269 if (base_type
->code () == TYPE_CODE_ARRAY
)
16270 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
16272 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
16273 return set_die_type (die
, cv_type
, cu
);
16276 /* Handle DW_TAG_restrict_type. */
16278 static struct type
*
16279 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16281 struct type
*base_type
, *cv_type
;
16283 base_type
= die_type (die
, cu
);
16285 /* The die_type call above may have already set the type for this DIE. */
16286 cv_type
= get_die_type (die
, cu
);
16290 cv_type
= make_restrict_type (base_type
);
16291 return set_die_type (die
, cv_type
, cu
);
16294 /* Handle DW_TAG_atomic_type. */
16296 static struct type
*
16297 read_tag_atomic_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_atomic_type (base_type
);
16309 return set_die_type (die
, cv_type
, cu
);
16312 /* Extract all information from a DW_TAG_string_type DIE and add to
16313 the user defined type vector. It isn't really a user defined type,
16314 but it behaves like one, with other DIE's using an AT_user_def_type
16315 attribute to reference it. */
16317 static struct type
*
16318 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16320 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16321 struct gdbarch
*gdbarch
= objfile
->arch ();
16322 struct type
*type
, *range_type
, *index_type
, *char_type
;
16323 struct attribute
*attr
;
16324 struct dynamic_prop prop
;
16325 bool length_is_constant
= true;
16328 /* There are a couple of places where bit sizes might be made use of
16329 when parsing a DW_TAG_string_type, however, no producer that we know
16330 of make use of these. Handling bit sizes that are a multiple of the
16331 byte size is easy enough, but what about other bit sizes? Lets deal
16332 with that problem when we have to. Warn about these attributes being
16333 unsupported, then parse the type and ignore them like we always
16335 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
16336 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
16338 static bool warning_printed
= false;
16339 if (!warning_printed
)
16341 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16342 "currently supported on DW_TAG_string_type."));
16343 warning_printed
= true;
16347 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
16348 if (attr
!= nullptr && !attr
->form_is_constant ())
16350 /* The string length describes the location at which the length of
16351 the string can be found. The size of the length field can be
16352 specified with one of the attributes below. */
16353 struct type
*prop_type
;
16354 struct attribute
*len
16355 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
16356 if (len
== nullptr)
16357 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16358 if (len
!= nullptr && len
->form_is_constant ())
16360 /* Pass 0 as the default as we know this attribute is constant
16361 and the default value will not be returned. */
16362 LONGEST sz
= len
->constant_value (0);
16363 prop_type
= objfile_int_type (objfile
, sz
, true);
16367 /* If the size is not specified then we assume it is the size of
16368 an address on this target. */
16369 prop_type
= cu
->addr_sized_int_type (true);
16372 /* Convert the attribute into a dynamic property. */
16373 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
16376 length_is_constant
= false;
16378 else if (attr
!= nullptr)
16380 /* This DW_AT_string_length just contains the length with no
16381 indirection. There's no need to create a dynamic property in this
16382 case. Pass 0 for the default value as we know it will not be
16383 returned in this case. */
16384 length
= attr
->constant_value (0);
16386 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
16388 /* We don't currently support non-constant byte sizes for strings. */
16389 length
= attr
->constant_value (1);
16393 /* Use 1 as a fallback length if we have nothing else. */
16397 index_type
= objfile_type (objfile
)->builtin_int
;
16398 if (length_is_constant
)
16399 range_type
= create_static_range_type (NULL
, index_type
, 1, length
);
16402 struct dynamic_prop low_bound
;
16404 low_bound
.set_const_val (1);
16405 range_type
= create_range_type (NULL
, index_type
, &low_bound
, &prop
, 0);
16407 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
16408 type
= create_string_type (NULL
, char_type
, range_type
);
16410 return set_die_type (die
, type
, cu
);
16413 /* Assuming that DIE corresponds to a function, returns nonzero
16414 if the function is prototyped. */
16417 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
16419 struct attribute
*attr
;
16421 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
16422 if (attr
&& attr
->as_boolean ())
16425 /* The DWARF standard implies that the DW_AT_prototyped attribute
16426 is only meaningful for C, but the concept also extends to other
16427 languages that allow unprototyped functions (Eg: Objective C).
16428 For all other languages, assume that functions are always
16430 if (cu
->per_cu
->lang
!= language_c
16431 && cu
->per_cu
->lang
!= language_objc
16432 && cu
->per_cu
->lang
!= language_opencl
)
16435 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16436 prototyped and unprototyped functions; default to prototyped,
16437 since that is more common in modern code (and RealView warns
16438 about unprototyped functions). */
16439 if (producer_is_realview (cu
->producer
))
16445 /* Handle DIES due to C code like:
16449 int (*funcp)(int a, long l);
16453 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16455 static struct type
*
16456 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16458 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16459 struct type
*type
; /* Type that this function returns. */
16460 struct type
*ftype
; /* Function that returns above type. */
16461 struct attribute
*attr
;
16463 type
= die_type (die
, cu
);
16465 /* The die_type call above may have already set the type for this DIE. */
16466 ftype
= get_die_type (die
, cu
);
16470 ftype
= lookup_function_type (type
);
16472 if (prototyped_function_p (die
, cu
))
16473 ftype
->set_is_prototyped (true);
16475 /* Store the calling convention in the type if it's available in
16476 the subroutine die. Otherwise set the calling convention to
16477 the default value DW_CC_normal. */
16478 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
16479 if (attr
!= nullptr
16480 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
16481 TYPE_CALLING_CONVENTION (ftype
)
16482 = (enum dwarf_calling_convention
) attr
->constant_value (0);
16483 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
16484 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
16486 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
16488 /* Record whether the function returns normally to its caller or not
16489 if the DWARF producer set that information. */
16490 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
16491 if (attr
&& attr
->as_boolean ())
16492 TYPE_NO_RETURN (ftype
) = 1;
16494 /* We need to add the subroutine type to the die immediately so
16495 we don't infinitely recurse when dealing with parameters
16496 declared as the same subroutine type. */
16497 set_die_type (die
, ftype
, cu
);
16499 if (die
->child
!= NULL
)
16501 struct type
*void_type
= objfile_type (objfile
)->builtin_void
;
16502 struct die_info
*child_die
;
16503 int nparams
, iparams
;
16505 /* Count the number of parameters.
16506 FIXME: GDB currently ignores vararg functions, but knows about
16507 vararg member functions. */
16509 child_die
= die
->child
;
16510 while (child_die
&& child_die
->tag
)
16512 if (child_die
->tag
== DW_TAG_formal_parameter
)
16514 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
16515 ftype
->set_has_varargs (true);
16517 child_die
= child_die
->sibling
;
16520 /* Allocate storage for parameters and fill them in. */
16521 ftype
->set_num_fields (nparams
);
16523 ((struct field
*) TYPE_ZALLOC (ftype
, nparams
* sizeof (struct field
)));
16525 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16526 even if we error out during the parameters reading below. */
16527 for (iparams
= 0; iparams
< nparams
; iparams
++)
16528 ftype
->field (iparams
).set_type (void_type
);
16531 child_die
= die
->child
;
16532 while (child_die
&& child_die
->tag
)
16534 if (child_die
->tag
== DW_TAG_formal_parameter
)
16536 struct type
*arg_type
;
16538 /* DWARF version 2 has no clean way to discern C++
16539 static and non-static member functions. G++ helps
16540 GDB by marking the first parameter for non-static
16541 member functions (which is the this pointer) as
16542 artificial. We pass this information to
16543 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16545 DWARF version 3 added DW_AT_object_pointer, which GCC
16546 4.5 does not yet generate. */
16547 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
16548 if (attr
!= nullptr)
16549 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = attr
->as_boolean ();
16551 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
16552 arg_type
= die_type (child_die
, cu
);
16554 /* RealView does not mark THIS as const, which the testsuite
16555 expects. GCC marks THIS as const in method definitions,
16556 but not in the class specifications (GCC PR 43053). */
16557 if (cu
->per_cu
->lang
== language_cplus
16558 && !TYPE_CONST (arg_type
)
16559 && TYPE_FIELD_ARTIFICIAL (ftype
, iparams
))
16562 struct dwarf2_cu
*arg_cu
= cu
;
16563 const char *name
= dwarf2_name (child_die
, cu
);
16565 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
16566 if (attr
!= nullptr)
16568 /* If the compiler emits this, use it. */
16569 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
16572 else if (name
&& strcmp (name
, "this") == 0)
16573 /* Function definitions will have the argument names. */
16575 else if (name
== NULL
&& iparams
== 0)
16576 /* Declarations may not have the names, so like
16577 elsewhere in GDB, assume an artificial first
16578 argument is "this". */
16582 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
16586 ftype
->field (iparams
).set_type (arg_type
);
16589 child_die
= child_die
->sibling
;
16596 static struct type
*
16597 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
16599 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16600 const char *name
= NULL
;
16601 struct type
*this_type
, *target_type
;
16603 name
= dwarf2_full_name (NULL
, die
, cu
);
16604 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, name
);
16605 this_type
->set_target_is_stub (true);
16606 set_die_type (die
, this_type
, cu
);
16607 target_type
= die_type (die
, cu
);
16608 if (target_type
!= this_type
)
16609 TYPE_TARGET_TYPE (this_type
) = target_type
;
16612 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16613 spec and cause infinite loops in GDB. */
16614 complaint (_("Self-referential DW_TAG_typedef "
16615 "- DIE at %s [in module %s]"),
16616 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
16617 TYPE_TARGET_TYPE (this_type
) = NULL
;
16621 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16622 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16623 Handle these by just returning the target type, rather than
16624 constructing an anonymous typedef type and trying to handle this
16626 set_die_type (die
, target_type
, cu
);
16627 return target_type
;
16632 /* Helper for get_dwarf2_rational_constant that computes the value of
16633 a given gmp_mpz given an attribute. */
16636 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
16638 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16639 location expression that pushes an implicit value. */
16640 if (attr
->form
== DW_FORM_exprloc
)
16642 dwarf_block
*blk
= attr
->as_block ();
16643 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
16646 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
16647 blk
->data
+ blk
->size
,
16649 if (ptr
- blk
->data
+ len
<= blk
->size
)
16651 mpz_import (value
->val
, len
,
16652 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16658 /* On failure set it to 1. */
16659 *value
= gdb_mpz (1);
16661 else if (attr
->form_is_block ())
16663 dwarf_block
*blk
= attr
->as_block ();
16664 mpz_import (value
->val
, blk
->size
,
16665 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16666 1, 0, 0, blk
->data
);
16669 *value
= gdb_mpz (attr
->constant_value (1));
16672 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16673 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16675 If the numerator and/or numerator attribute is missing,
16676 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16680 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
16681 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
16683 struct attribute
*num_attr
, *denom_attr
;
16685 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
16686 if (num_attr
== nullptr)
16687 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16688 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16690 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
16691 if (denom_attr
== nullptr)
16692 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16693 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16695 if (num_attr
== nullptr || denom_attr
== nullptr)
16698 get_mpz (cu
, numerator
, num_attr
);
16699 get_mpz (cu
, denominator
, denom_attr
);
16702 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16703 rational constant, rather than a signed one.
16705 If the rational constant has a negative value, a complaint
16706 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16709 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
16710 struct dwarf2_cu
*cu
,
16711 gdb_mpz
*numerator
,
16712 gdb_mpz
*denominator
)
16717 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
16718 if (mpz_sgn (num
.val
) == -1 && mpz_sgn (denom
.val
) == -1)
16720 mpz_neg (num
.val
, num
.val
);
16721 mpz_neg (denom
.val
, denom
.val
);
16723 else if (mpz_sgn (num
.val
) == -1)
16725 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16727 sect_offset_str (die
->sect_off
));
16730 else if (mpz_sgn (denom
.val
) == -1)
16732 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16734 sect_offset_str (die
->sect_off
));
16738 *numerator
= std::move (num
);
16739 *denominator
= std::move (denom
);
16742 /* Assuming that ENCODING is a string whose contents starting at the
16743 K'th character is "_nn" where "nn" is a decimal number, scan that
16744 number and set RESULT to the value. K is updated to point to the
16745 character immediately following the number.
16747 If the string does not conform to the format described above, false
16748 is returned, and K may or may not be changed. */
16751 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
16753 /* The next character should be an underscore ('_') followed
16755 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
16758 /* Skip the underscore. */
16762 /* Determine the number of digits for our number. */
16763 while (isdigit (encoding
[k
]))
16768 std::string
copy (&encoding
[start
], k
- start
);
16769 if (mpz_set_str (result
->val
, copy
.c_str (), 10) == -1)
16775 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16776 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16777 DENOM, update OFFSET, and return true on success. Return false on
16781 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
16782 gdb_mpz
*num
, gdb_mpz
*denom
)
16784 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
16786 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
16789 /* Assuming DIE corresponds to a fixed point type, finish the creation
16790 of the corresponding TYPE by setting its type-specific data. CU is
16791 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16792 encodings. It is nullptr if the GNAT encoding should be
16796 finish_fixed_point_type (struct type
*type
, const char *suffix
,
16797 struct die_info
*die
, struct dwarf2_cu
*cu
)
16799 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
16800 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
16802 /* If GNAT encodings are preferred, don't examine the
16804 struct attribute
*attr
= nullptr;
16805 if (suffix
== nullptr)
16807 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
16808 if (attr
== nullptr)
16809 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
16810 if (attr
== nullptr)
16811 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16814 /* Numerator and denominator of our fixed-point type's scaling factor.
16815 The default is a scaling factor of 1, which we use as a fallback
16816 when we are not able to decode it (problem with the debugging info,
16817 unsupported forms, bug in GDB, etc...). Using that as the default
16818 allows us to at least print the unscaled value, which might still
16819 be useful to a user. */
16820 gdb_mpz
scale_num (1);
16821 gdb_mpz
scale_denom (1);
16823 if (attr
== nullptr)
16826 if (suffix
!= nullptr
16827 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16829 /* The number might be encoded as _nn_dd_nn_dd, where the
16830 second ratio is the 'small value. In this situation, we
16831 want the second value. */
16832 && (suffix
[offset
] != '_'
16833 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16840 /* Scaling factor not found. Assume a scaling factor of 1,
16841 and hope for the best. At least the user will be able to
16842 see the encoded value. */
16845 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16846 sect_offset_str (die
->sect_off
));
16849 else if (attr
->name
== DW_AT_binary_scale
)
16851 LONGEST scale_exp
= attr
->constant_value (0);
16852 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16854 mpz_mul_2exp (num_or_denom
->val
, num_or_denom
->val
, std::abs (scale_exp
));
16856 else if (attr
->name
== DW_AT_decimal_scale
)
16858 LONGEST scale_exp
= attr
->constant_value (0);
16859 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16861 mpz_ui_pow_ui (num_or_denom
->val
, 10, std::abs (scale_exp
));
16863 else if (attr
->name
== DW_AT_small
)
16865 struct die_info
*scale_die
;
16866 struct dwarf2_cu
*scale_cu
= cu
;
16868 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
16869 if (scale_die
->tag
== DW_TAG_constant
)
16870 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
16871 &scale_num
, &scale_denom
);
16873 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16875 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16879 complaint (_("unsupported scale attribute %s for fixed-point type"
16881 dwarf_attr_name (attr
->name
),
16882 sect_offset_str (die
->sect_off
));
16885 gdb_mpq
&scaling_factor
= type
->fixed_point_info ().scaling_factor
;
16886 mpz_set (mpq_numref (scaling_factor
.val
), scale_num
.val
);
16887 mpz_set (mpq_denref (scaling_factor
.val
), scale_denom
.val
);
16888 mpq_canonicalize (scaling_factor
.val
);
16891 /* The gnat-encoding suffix for fixed point. */
16893 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16895 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16896 "XF" suffix of the name. The text after this is what encodes the
16897 'small and 'delta information. Otherwise, return nullptr. */
16899 static const char *
16900 gnat_encoded_fixed_point_type_info (const char *name
)
16902 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
16905 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16906 (which may be different from NAME) to the architecture back-end to allow
16907 it to guess the correct format if necessary. */
16909 static struct type
*
16910 dwarf2_init_float_type (struct objfile
*objfile
, int bits
, const char *name
,
16911 const char *name_hint
, enum bfd_endian byte_order
)
16913 struct gdbarch
*gdbarch
= objfile
->arch ();
16914 const struct floatformat
**format
;
16917 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
16919 type
= init_float_type (objfile
, bits
, name
, format
, byte_order
);
16921 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
16926 /* Allocate an integer type of size BITS and name NAME. */
16928 static struct type
*
16929 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, struct objfile
*objfile
,
16930 int bits
, int unsigned_p
, const char *name
)
16934 /* Versions of Intel's C Compiler generate an integer type called "void"
16935 instead of using DW_TAG_unspecified_type. This has been seen on
16936 at least versions 14, 17, and 18. */
16937 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
16938 && strcmp (name
, "void") == 0)
16939 type
= objfile_type (objfile
)->builtin_void
;
16941 type
= init_integer_type (objfile
, bits
, unsigned_p
, name
);
16946 /* Return true if DIE has a DW_AT_small attribute whose value is
16947 a constant rational, where both the numerator and denominator
16950 CU is the DIE's Compilation Unit. */
16953 has_zero_over_zero_small_attribute (struct die_info
*die
,
16954 struct dwarf2_cu
*cu
)
16956 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16957 if (attr
== nullptr)
16960 struct dwarf2_cu
*scale_cu
= cu
;
16961 struct die_info
*scale_die
16962 = follow_die_ref (die
, attr
, &scale_cu
);
16964 if (scale_die
->tag
!= DW_TAG_constant
)
16967 gdb_mpz
num (1), denom (1);
16968 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
16969 return mpz_sgn (num
.val
) == 0 && mpz_sgn (denom
.val
) == 0;
16972 /* Initialise and return a floating point type of size BITS suitable for
16973 use as a component of a complex number. The NAME_HINT is passed through
16974 when initialising the floating point type and is the name of the complex
16977 As DWARF doesn't currently provide an explicit name for the components
16978 of a complex number, but it can be helpful to have these components
16979 named, we try to select a suitable name based on the size of the
16981 static struct type
*
16982 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
16983 struct objfile
*objfile
,
16984 int bits
, const char *name_hint
,
16985 enum bfd_endian byte_order
)
16987 gdbarch
*gdbarch
= objfile
->arch ();
16988 struct type
*tt
= nullptr;
16990 /* Try to find a suitable floating point builtin type of size BITS.
16991 We're going to use the name of this type as the name for the complex
16992 target type that we are about to create. */
16993 switch (cu
->per_cu
->lang
)
16995 case language_fortran
:
16999 tt
= builtin_f_type (gdbarch
)->builtin_real
;
17002 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
17004 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17006 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
17014 tt
= builtin_type (gdbarch
)->builtin_float
;
17017 tt
= builtin_type (gdbarch
)->builtin_double
;
17019 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17021 tt
= builtin_type (gdbarch
)->builtin_long_double
;
17027 /* If the type we found doesn't match the size we were looking for, then
17028 pretend we didn't find a type at all, the complex target type we
17029 create will then be nameless. */
17030 if (tt
!= nullptr && TYPE_LENGTH (tt
) * TARGET_CHAR_BIT
!= bits
)
17033 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
17034 return dwarf2_init_float_type (objfile
, bits
, name
, name_hint
, byte_order
);
17037 /* Find a representation of a given base type and install
17038 it in the TYPE field of the die. */
17040 static struct type
*
17041 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17043 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17045 struct attribute
*attr
;
17046 int encoding
= 0, bits
= 0;
17050 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
17051 if (attr
!= nullptr && attr
->form_is_constant ())
17052 encoding
= attr
->constant_value (0);
17053 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17054 if (attr
!= nullptr)
17055 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
17056 name
= dwarf2_name (die
, cu
);
17058 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17060 arch
= objfile
->arch ();
17061 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
17063 attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
17064 if (attr
!= nullptr && attr
->form_is_constant ())
17066 int endianity
= attr
->constant_value (0);
17071 byte_order
= BFD_ENDIAN_BIG
;
17073 case DW_END_little
:
17074 byte_order
= BFD_ENDIAN_LITTLE
;
17077 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
17082 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
17083 && cu
->per_cu
->lang
== language_ada
17084 && has_zero_over_zero_small_attribute (die
, cu
))
17086 /* brobecker/2018-02-24: This is a fixed point type for which
17087 the scaling factor is represented as fraction whose value
17088 does not make sense (zero divided by zero), so we should
17089 normally never see these. However, there is a small category
17090 of fixed point types for which GNAT is unable to provide
17091 the scaling factor via the standard DWARF mechanisms, and
17092 for which the info is provided via the GNAT encodings instead.
17093 This is likely what this DIE is about. */
17094 encoding
= (encoding
== DW_ATE_signed_fixed
17096 : DW_ATE_unsigned
);
17099 /* With GNAT encodings, fixed-point information will be encoded in
17100 the type name. Note that this can also occur with the above
17101 zero-over-zero case, which is why this is a separate "if" rather
17102 than an "else if". */
17103 const char *gnat_encoding_suffix
= nullptr;
17104 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
17105 && cu
->per_cu
->lang
== language_ada
17106 && name
!= nullptr)
17108 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
17109 if (gnat_encoding_suffix
!= nullptr)
17111 gdb_assert (startswith (gnat_encoding_suffix
,
17112 GNAT_FIXED_POINT_SUFFIX
));
17113 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
17114 name
, gnat_encoding_suffix
- name
);
17115 /* Use -1 here so that SUFFIX points at the "_" after the
17117 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
17119 encoding
= (encoding
== DW_ATE_signed
17120 ? DW_ATE_signed_fixed
17121 : DW_ATE_unsigned_fixed
);
17127 case DW_ATE_address
:
17128 /* Turn DW_ATE_address into a void * pointer. */
17129 type
= init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
17130 type
= init_pointer_type (objfile
, bits
, name
, type
);
17132 case DW_ATE_boolean
:
17133 type
= init_boolean_type (objfile
, bits
, 1, name
);
17135 case DW_ATE_complex_float
:
17136 type
= dwarf2_init_complex_target_type (cu
, objfile
, bits
/ 2, name
,
17138 if (type
->code () == TYPE_CODE_ERROR
)
17140 if (name
== nullptr)
17142 struct obstack
*obstack
17143 = &cu
->per_objfile
->objfile
->objfile_obstack
;
17144 name
= obconcat (obstack
, "_Complex ", type
->name (),
17147 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17150 type
= init_complex_type (name
, type
);
17152 case DW_ATE_decimal_float
:
17153 type
= init_decfloat_type (objfile
, bits
, name
);
17156 type
= dwarf2_init_float_type (objfile
, bits
, name
, name
, byte_order
);
17158 case DW_ATE_signed
:
17159 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17161 case DW_ATE_unsigned
:
17162 if (cu
->per_cu
->lang
== language_fortran
17164 && startswith (name
, "character("))
17165 type
= init_character_type (objfile
, bits
, 1, name
);
17167 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17169 case DW_ATE_signed_char
:
17170 if (cu
->per_cu
->lang
== language_ada
17171 || cu
->per_cu
->lang
== language_m2
17172 || cu
->per_cu
->lang
== language_pascal
17173 || cu
->per_cu
->lang
== language_fortran
)
17174 type
= init_character_type (objfile
, bits
, 0, name
);
17176 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17178 case DW_ATE_unsigned_char
:
17179 if (cu
->per_cu
->lang
== language_ada
17180 || cu
->per_cu
->lang
== language_m2
17181 || cu
->per_cu
->lang
== language_pascal
17182 || cu
->per_cu
->lang
== language_fortran
17183 || cu
->per_cu
->lang
== language_rust
)
17184 type
= init_character_type (objfile
, bits
, 1, name
);
17186 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17190 type
= init_character_type (objfile
, bits
, 1, name
);
17191 return set_die_type (die
, type
, cu
);
17194 case DW_ATE_signed_fixed
:
17195 type
= init_fixed_point_type (objfile
, bits
, 0, name
);
17196 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17198 case DW_ATE_unsigned_fixed
:
17199 type
= init_fixed_point_type (objfile
, bits
, 1, name
);
17200 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17204 complaint (_("unsupported DW_AT_encoding: '%s'"),
17205 dwarf_type_encoding_name (encoding
));
17206 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17210 if (type
->code () == TYPE_CODE_INT
17212 && strcmp (name
, "char") == 0)
17213 type
->set_has_no_signedness (true);
17215 maybe_set_alignment (cu
, die
, type
);
17217 type
->set_endianity_is_not_default (gdbarch_byte_order (arch
) != byte_order
);
17219 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
17221 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
17222 if (attr
!= nullptr && attr
->as_unsigned () <= 8 * TYPE_LENGTH (type
))
17224 unsigned real_bit_size
= attr
->as_unsigned ();
17225 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
17226 /* Only use the attributes if they make sense together. */
17227 if (attr
== nullptr
17228 || (attr
->as_unsigned () + real_bit_size
17229 <= 8 * TYPE_LENGTH (type
)))
17231 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
17233 if (attr
!= nullptr)
17234 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
17235 = attr
->as_unsigned ();
17240 return set_die_type (die
, type
, cu
);
17243 /* A helper function that returns the name of DIE, if it refers to a
17244 variable declaration. */
17246 static const char *
17247 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17249 if (die
->tag
!= DW_TAG_variable
)
17252 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
17253 if (attr
== nullptr || !attr
->as_boolean ())
17256 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
17257 if (attr
== nullptr)
17259 return attr
->as_string ();
17262 /* Parse dwarf attribute if it's a block, reference or constant and put the
17263 resulting value of the attribute into struct bound_prop.
17264 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17267 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
17268 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
17269 struct type
*default_type
)
17271 struct dwarf2_property_baton
*baton
;
17272 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17273 struct objfile
*objfile
= per_objfile
->objfile
;
17274 struct obstack
*obstack
= &objfile
->objfile_obstack
;
17276 gdb_assert (default_type
!= NULL
);
17278 if (attr
== NULL
|| prop
== NULL
)
17281 if (attr
->form_is_block ())
17283 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17284 baton
->property_type
= default_type
;
17285 baton
->locexpr
.per_cu
= cu
->per_cu
;
17286 baton
->locexpr
.per_objfile
= per_objfile
;
17288 struct dwarf_block
*block
;
17289 if (attr
->form
== DW_FORM_data16
)
17291 size_t data_size
= 16;
17292 block
= XOBNEW (obstack
, struct dwarf_block
);
17293 block
->size
= (data_size
17294 + 2 /* Extra bytes for DW_OP and arg. */);
17295 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
->size
);
17296 data
[0] = DW_OP_implicit_value
;
17297 data
[1] = data_size
;
17298 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
17299 block
->data
= data
;
17302 block
= attr
->as_block ();
17304 baton
->locexpr
.size
= block
->size
;
17305 baton
->locexpr
.data
= block
->data
;
17306 switch (attr
->name
)
17308 case DW_AT_string_length
:
17309 baton
->locexpr
.is_reference
= true;
17312 baton
->locexpr
.is_reference
= false;
17316 prop
->set_locexpr (baton
);
17317 gdb_assert (prop
->baton () != NULL
);
17319 else if (attr
->form_is_ref ())
17321 struct dwarf2_cu
*target_cu
= cu
;
17322 struct die_info
*target_die
;
17323 struct attribute
*target_attr
;
17325 target_die
= follow_die_ref (die
, attr
, &target_cu
);
17326 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
17327 if (target_attr
== NULL
)
17328 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
17330 if (target_attr
== nullptr)
17331 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
17333 if (target_attr
== NULL
)
17335 const char *name
= var_decl_name (target_die
, target_cu
);
17336 if (name
!= nullptr)
17338 prop
->set_variable_name (name
);
17344 switch (target_attr
->name
)
17346 case DW_AT_location
:
17347 if (target_attr
->form_is_section_offset ())
17349 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17350 baton
->property_type
= die_type (target_die
, target_cu
);
17351 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
17352 prop
->set_loclist (baton
);
17353 gdb_assert (prop
->baton () != NULL
);
17355 else if (target_attr
->form_is_block ())
17357 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17358 baton
->property_type
= die_type (target_die
, target_cu
);
17359 baton
->locexpr
.per_cu
= cu
->per_cu
;
17360 baton
->locexpr
.per_objfile
= per_objfile
;
17361 struct dwarf_block
*block
= target_attr
->as_block ();
17362 baton
->locexpr
.size
= block
->size
;
17363 baton
->locexpr
.data
= block
->data
;
17364 baton
->locexpr
.is_reference
= true;
17365 prop
->set_locexpr (baton
);
17366 gdb_assert (prop
->baton () != NULL
);
17370 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17371 "dynamic property");
17375 case DW_AT_data_member_location
:
17376 case DW_AT_data_bit_offset
:
17380 if (!handle_member_location (target_die
, target_cu
, &offset
))
17383 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17384 baton
->property_type
= read_type_die (target_die
->parent
,
17386 baton
->offset_info
.offset
= offset
;
17387 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
17388 prop
->set_addr_offset (baton
);
17393 else if (attr
->form_is_constant ())
17394 prop
->set_const_val (attr
->constant_value (0));
17395 else if (attr
->form_is_section_offset ())
17397 switch (attr
->name
)
17399 case DW_AT_string_length
:
17400 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17401 baton
->property_type
= default_type
;
17402 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
17403 prop
->set_loclist (baton
);
17404 gdb_assert (prop
->baton () != NULL
);
17416 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
17417 dwarf2_name (die
, cu
));
17423 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17424 present (which is valid) then compute the default type based on the
17425 compilation units address size. */
17427 static struct type
*
17428 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17430 struct type
*index_type
= die_type (die
, cu
);
17432 /* Dwarf-2 specifications explicitly allows to create subrange types
17433 without specifying a base type.
17434 In that case, the base type must be set to the type of
17435 the lower bound, upper bound or count, in that order, if any of these
17436 three attributes references an object that has a type.
17437 If no base type is found, the Dwarf-2 specifications say that
17438 a signed integer type of size equal to the size of an address should
17440 For the following C code: `extern char gdb_int [];'
17441 GCC produces an empty range DIE.
17442 FIXME: muller/2010-05-28: Possible references to object for low bound,
17443 high bound or count are not yet handled by this code. */
17444 if (index_type
->code () == TYPE_CODE_VOID
)
17445 index_type
= cu
->addr_sized_int_type (false);
17450 /* Read the given DW_AT_subrange DIE. */
17452 static struct type
*
17453 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17455 struct type
*base_type
, *orig_base_type
;
17456 struct type
*range_type
;
17457 struct attribute
*attr
;
17458 struct dynamic_prop low
, high
;
17459 int low_default_is_valid
;
17460 int high_bound_is_count
= 0;
17462 ULONGEST negative_mask
;
17464 orig_base_type
= read_subrange_index_type (die
, cu
);
17466 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17467 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17468 creating the range type, but we use the result of check_typedef
17469 when examining properties of the type. */
17470 base_type
= check_typedef (orig_base_type
);
17472 /* The die_type call above may have already set the type for this DIE. */
17473 range_type
= get_die_type (die
, cu
);
17477 high
.set_const_val (0);
17479 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17480 omitting DW_AT_lower_bound. */
17481 switch (cu
->per_cu
->lang
)
17484 case language_cplus
:
17485 low
.set_const_val (0);
17486 low_default_is_valid
= 1;
17488 case language_fortran
:
17489 low
.set_const_val (1);
17490 low_default_is_valid
= 1;
17493 case language_objc
:
17494 case language_rust
:
17495 low
.set_const_val (0);
17496 low_default_is_valid
= (cu
->header
.version
>= 4);
17500 case language_pascal
:
17501 low
.set_const_val (1);
17502 low_default_is_valid
= (cu
->header
.version
>= 4);
17505 low
.set_const_val (0);
17506 low_default_is_valid
= 0;
17510 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
17511 if (attr
!= nullptr)
17512 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
17513 else if (!low_default_is_valid
)
17514 complaint (_("Missing DW_AT_lower_bound "
17515 "- DIE at %s [in module %s]"),
17516 sect_offset_str (die
->sect_off
),
17517 objfile_name (cu
->per_objfile
->objfile
));
17519 struct attribute
*attr_ub
, *attr_count
;
17520 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
17521 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17523 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
17524 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17526 /* If bounds are constant do the final calculation here. */
17527 if (low
.kind () == PROP_CONST
&& high
.kind () == PROP_CONST
)
17528 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
17530 high_bound_is_count
= 1;
17534 if (attr_ub
!= NULL
)
17535 complaint (_("Unresolved DW_AT_upper_bound "
17536 "- DIE at %s [in module %s]"),
17537 sect_offset_str (die
->sect_off
),
17538 objfile_name (cu
->per_objfile
->objfile
));
17539 if (attr_count
!= NULL
)
17540 complaint (_("Unresolved DW_AT_count "
17541 "- DIE at %s [in module %s]"),
17542 sect_offset_str (die
->sect_off
),
17543 objfile_name (cu
->per_objfile
->objfile
));
17548 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
17549 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
17550 bias
= bias_attr
->constant_value (0);
17552 /* Normally, the DWARF producers are expected to use a signed
17553 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17554 But this is unfortunately not always the case, as witnessed
17555 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17556 is used instead. To work around that ambiguity, we treat
17557 the bounds as signed, and thus sign-extend their values, when
17558 the base type is signed. */
17560 -((ULONGEST
) 1 << (TYPE_LENGTH (base_type
) * TARGET_CHAR_BIT
- 1));
17561 if (low
.kind () == PROP_CONST
17562 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
17563 low
.set_const_val (low
.const_val () | negative_mask
);
17564 if (high
.kind () == PROP_CONST
17565 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
17566 high
.set_const_val (high
.const_val () | negative_mask
);
17568 /* Check for bit and byte strides. */
17569 struct dynamic_prop byte_stride_prop
;
17570 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
17571 if (attr_byte_stride
!= nullptr)
17573 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17574 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
17578 struct dynamic_prop bit_stride_prop
;
17579 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
17580 if (attr_bit_stride
!= nullptr)
17582 /* It only makes sense to have either a bit or byte stride. */
17583 if (attr_byte_stride
!= nullptr)
17585 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17586 "- DIE at %s [in module %s]"),
17587 sect_offset_str (die
->sect_off
),
17588 objfile_name (cu
->per_objfile
->objfile
));
17589 attr_bit_stride
= nullptr;
17593 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17594 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
17599 if (attr_byte_stride
!= nullptr
17600 || attr_bit_stride
!= nullptr)
17602 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
17603 struct dynamic_prop
*stride
17604 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
17607 = create_range_type_with_stride (NULL
, orig_base_type
, &low
,
17608 &high
, bias
, stride
, byte_stride_p
);
17611 range_type
= create_range_type (NULL
, orig_base_type
, &low
, &high
, bias
);
17613 if (high_bound_is_count
)
17614 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
17616 /* Ada expects an empty array on no boundary attributes. */
17617 if (attr
== NULL
&& cu
->per_cu
->lang
!= language_ada
)
17618 range_type
->bounds ()->high
.set_undefined ();
17620 name
= dwarf2_name (die
, cu
);
17622 range_type
->set_name (name
);
17624 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17625 if (attr
!= nullptr)
17626 TYPE_LENGTH (range_type
) = attr
->constant_value (0);
17628 maybe_set_alignment (cu
, die
, range_type
);
17630 set_die_type (die
, range_type
, cu
);
17632 /* set_die_type should be already done. */
17633 set_descriptive_type (range_type
, die
, cu
);
17638 static struct type
*
17639 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17643 type
= init_type (cu
->per_objfile
->objfile
, TYPE_CODE_VOID
, 0, NULL
);
17644 type
->set_name (dwarf2_name (die
, cu
));
17646 /* In Ada, an unspecified type is typically used when the description
17647 of the type is deferred to a different unit. When encountering
17648 such a type, we treat it as a stub, and try to resolve it later on,
17650 if (cu
->per_cu
->lang
== language_ada
)
17651 type
->set_is_stub (true);
17653 return set_die_type (die
, type
, cu
);
17656 /* Read a single die and all its descendents. Set the die's sibling
17657 field to NULL; set other fields in the die correctly, and set all
17658 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17659 location of the info_ptr after reading all of those dies. PARENT
17660 is the parent of the die in question. */
17662 static struct die_info
*
17663 read_die_and_children (const struct die_reader_specs
*reader
,
17664 const gdb_byte
*info_ptr
,
17665 const gdb_byte
**new_info_ptr
,
17666 struct die_info
*parent
)
17668 struct die_info
*die
;
17669 const gdb_byte
*cur_ptr
;
17671 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0);
17674 *new_info_ptr
= cur_ptr
;
17677 store_in_ref_table (die
, reader
->cu
);
17679 if (die
->has_children
)
17680 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
17684 *new_info_ptr
= cur_ptr
;
17687 die
->sibling
= NULL
;
17688 die
->parent
= parent
;
17692 /* Read a die, all of its descendents, and all of its siblings; set
17693 all of the fields of all of the dies correctly. Arguments are as
17694 in read_die_and_children. */
17696 static struct die_info
*
17697 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
17698 const gdb_byte
*info_ptr
,
17699 const gdb_byte
**new_info_ptr
,
17700 struct die_info
*parent
)
17702 struct die_info
*first_die
, *last_sibling
;
17703 const gdb_byte
*cur_ptr
;
17705 cur_ptr
= info_ptr
;
17706 first_die
= last_sibling
= NULL
;
17710 struct die_info
*die
17711 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
17715 *new_info_ptr
= cur_ptr
;
17722 last_sibling
->sibling
= die
;
17724 last_sibling
= die
;
17728 /* Read a die, all of its descendents, and all of its siblings; set
17729 all of the fields of all of the dies correctly. Arguments are as
17730 in read_die_and_children.
17731 This the main entry point for reading a DIE and all its children. */
17733 static struct die_info
*
17734 read_die_and_siblings (const struct die_reader_specs
*reader
,
17735 const gdb_byte
*info_ptr
,
17736 const gdb_byte
**new_info_ptr
,
17737 struct die_info
*parent
)
17739 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
17740 new_info_ptr
, parent
);
17742 if (dwarf_die_debug
)
17744 gdb_printf (gdb_stdlog
,
17745 "Read die from %s@0x%x of %s:\n",
17746 reader
->die_section
->get_name (),
17747 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17748 bfd_get_filename (reader
->abfd
));
17749 dump_die (die
, dwarf_die_debug
);
17755 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17757 The caller is responsible for filling in the extra attributes
17758 and updating (*DIEP)->num_attrs.
17759 Set DIEP to point to a newly allocated die with its information,
17760 except for its child, sibling, and parent fields. */
17762 static const gdb_byte
*
17763 read_full_die_1 (const struct die_reader_specs
*reader
,
17764 struct die_info
**diep
, const gdb_byte
*info_ptr
,
17765 int num_extra_attrs
)
17767 unsigned int abbrev_number
, bytes_read
, i
;
17768 const struct abbrev_info
*abbrev
;
17769 struct die_info
*die
;
17770 struct dwarf2_cu
*cu
= reader
->cu
;
17771 bfd
*abfd
= reader
->abfd
;
17773 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
17774 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17775 info_ptr
+= bytes_read
;
17776 if (!abbrev_number
)
17782 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
17784 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17786 bfd_get_filename (abfd
));
17788 die
= dwarf_alloc_die (cu
, abbrev
->num_attrs
+ num_extra_attrs
);
17789 die
->sect_off
= sect_off
;
17790 die
->tag
= abbrev
->tag
;
17791 die
->abbrev
= abbrev_number
;
17792 die
->has_children
= abbrev
->has_children
;
17794 /* Make the result usable.
17795 The caller needs to update num_attrs after adding the extra
17797 die
->num_attrs
= abbrev
->num_attrs
;
17799 bool any_need_reprocess
= false;
17800 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17802 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
17804 if (die
->attrs
[i
].requires_reprocessing_p ())
17805 any_need_reprocess
= true;
17808 struct attribute
*attr
= die
->attr (DW_AT_str_offsets_base
);
17809 if (attr
!= nullptr && attr
->form_is_unsigned ())
17810 cu
->str_offsets_base
= attr
->as_unsigned ();
17812 attr
= die
->attr (DW_AT_loclists_base
);
17813 if (attr
!= nullptr)
17814 cu
->loclist_base
= attr
->as_unsigned ();
17816 auto maybe_addr_base
= die
->addr_base ();
17817 if (maybe_addr_base
.has_value ())
17818 cu
->addr_base
= *maybe_addr_base
;
17820 attr
= die
->attr (DW_AT_rnglists_base
);
17821 if (attr
!= nullptr)
17822 cu
->rnglists_base
= attr
->as_unsigned ();
17824 if (any_need_reprocess
)
17826 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17828 if (die
->attrs
[i
].requires_reprocessing_p ())
17829 read_attribute_reprocess (reader
, &die
->attrs
[i
], die
->tag
);
17836 /* Read a die and all its attributes.
17837 Set DIEP to point to a newly allocated die with its information,
17838 except for its child, sibling, and parent fields. */
17840 static const gdb_byte
*
17841 read_full_die (const struct die_reader_specs
*reader
,
17842 struct die_info
**diep
, const gdb_byte
*info_ptr
)
17844 const gdb_byte
*result
;
17846 result
= read_full_die_1 (reader
, diep
, info_ptr
, 0);
17848 if (dwarf_die_debug
)
17850 gdb_printf (gdb_stdlog
,
17851 "Read die from %s@0x%x of %s:\n",
17852 reader
->die_section
->get_name (),
17853 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17854 bfd_get_filename (reader
->abfd
));
17855 dump_die (*diep
, dwarf_die_debug
);
17863 cooked_indexer::check_bounds (cutu_reader
*reader
)
17865 if (reader
->cu
->per_cu
->addresses_seen
)
17868 dwarf2_cu
*cu
= reader
->cu
;
17870 CORE_ADDR best_lowpc
= 0, best_highpc
= 0;
17871 /* Possibly set the default values of LOWPC and HIGHPC from
17873 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
17874 enum pc_bounds_kind cu_bounds_kind
17875 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
17876 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
17877 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
17879 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17880 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
17881 struct gdbarch
*gdbarch
= objfile
->arch ();
17883 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_lowpc
+ baseaddr
)
17886 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_highpc
+ baseaddr
)
17888 /* Store the contiguous range if it is not empty; it can be
17889 empty for CUs with no code. */
17890 addrmap_set_empty (m_index_storage
->get_addrmap (), low
, high
,
17893 cu
->per_cu
->addresses_seen
= true;
17897 /* Helper function that returns true if TAG can have a linkage
17901 tag_can_have_linkage_name (enum dwarf_tag tag
)
17905 /* We include types here because an anonymous C++ type might
17906 have a name for linkage purposes. */
17907 case DW_TAG_class_type
:
17908 case DW_TAG_structure_type
:
17909 case DW_TAG_union_type
:
17910 case DW_TAG_variable
:
17911 case DW_TAG_subprogram
:
17920 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
17921 dwarf2_per_objfile
*per_objfile
,
17922 sect_offset sect_off
, bool is_dwz
,
17925 /* Lookups for type unit references are always in the CU, and
17926 cross-CU references will crash. */
17927 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
17928 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
17931 dwarf2_per_cu_data
*per_cu
17932 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
17933 per_objfile
->per_bfd
);
17935 /* When scanning, we only want to visit a given CU a single time.
17936 Doing this check here avoids self-imports as well. */
17940 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
17943 if (per_cu
== m_per_cu
)
17946 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
17947 if (result
== nullptr)
17949 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
17950 m_index_storage
->get_abbrev_cache ());
17952 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
17954 std::unique_ptr
<cutu_reader
> copy
17955 (new cutu_reader (std::move (new_reader
)));
17956 result
= m_index_storage
->preserve (std::move (copy
));
17959 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
17963 check_bounds (result
);
17969 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
17970 cutu_reader
*reader
,
17971 const gdb_byte
*watermark_ptr
,
17972 const gdb_byte
*info_ptr
,
17973 const abbrev_info
*abbrev
,
17975 const char **linkage_name
,
17976 cooked_index_flag
*flags
,
17977 sect_offset
*sibling_offset
,
17978 const cooked_index_entry
**parent_entry
,
17979 CORE_ADDR
*maybe_defer
,
17980 bool for_specification
)
17982 bool origin_is_dwz
= false;
17983 bool is_declaration
= false;
17984 sect_offset origin_offset
{};
17986 gdb::optional
<CORE_ADDR
> low_pc
;
17987 gdb::optional
<CORE_ADDR
> high_pc
;
17988 bool high_pc_relative
= false;
17990 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
17993 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
17994 if (attr
.requires_reprocessing_p ())
17995 read_attribute_reprocess (reader
, &attr
, abbrev
->tag
);
17997 /* Store the data if it is of an attribute we want to keep in a
17998 partial symbol table. */
18002 switch (abbrev
->tag
)
18004 case DW_TAG_compile_unit
:
18005 case DW_TAG_partial_unit
:
18006 case DW_TAG_type_unit
:
18007 /* Compilation units have a DW_AT_name that is a filename, not
18008 a source language identifier. */
18012 if (*name
== nullptr)
18013 *name
= attr
.as_string ();
18018 case DW_AT_linkage_name
:
18019 case DW_AT_MIPS_linkage_name
:
18020 /* Note that both forms of linkage name might appear. We
18021 assume they will be the same, and we only store the last
18023 if (*linkage_name
== nullptr)
18024 *linkage_name
= attr
.as_string ();
18027 case DW_AT_main_subprogram
:
18028 if (attr
.as_boolean ())
18032 case DW_AT_declaration
:
18033 is_declaration
= attr
.as_boolean ();
18036 case DW_AT_sibling
:
18037 if (sibling_offset
!= nullptr)
18038 *sibling_offset
= attr
.get_ref_die_offset ();
18041 case DW_AT_specification
:
18042 case DW_AT_abstract_origin
:
18043 case DW_AT_extension
:
18044 origin_offset
= attr
.get_ref_die_offset ();
18045 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
18048 case DW_AT_external
:
18049 if (attr
.as_boolean ())
18050 *flags
&= ~IS_STATIC
;
18053 case DW_AT_enum_class
:
18054 if (attr
.as_boolean ())
18055 *flags
|= IS_ENUM_CLASS
;
18059 low_pc
= attr
.as_address ();
18062 case DW_AT_high_pc
:
18063 high_pc
= attr
.as_address ();
18064 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
18065 high_pc_relative
= true;
18068 case DW_AT_location
:
18069 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
18071 struct dwarf_block
*locdesc
= attr
.as_block ();
18072 CORE_ADDR addr
= decode_locdesc (locdesc
, reader
->cu
);
18074 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
)
18077 /* For variables, we don't want to try decoding the
18078 type just to find the size -- for gdb's purposes
18079 we only need the address of a variable. */
18080 high_pc
= addr
+ 1;
18081 high_pc_relative
= false;
18087 if (!scanning_per_cu
->addresses_seen
)
18089 /* Offset in the .debug_ranges or .debug_rnglist section
18090 (depending on DWARF version). */
18091 ULONGEST ranges_offset
= attr
.as_unsigned ();
18093 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18094 want to add this value. */
18095 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
18097 CORE_ADDR lowpc
, highpc
;
18098 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
18099 m_index_storage
->get_addrmap (),
18100 scanning_per_cu
, abbrev
->tag
);
18106 /* We don't want to examine declarations, but if we found a
18107 declaration when handling DW_AT_specification or the like, then
18108 that is ok. Similarly, we allow an external variable without a
18109 location; those are resolved via minimal symbols. */
18110 if (is_declaration
&& !for_specification
18111 && !(abbrev
->tag
== DW_TAG_variable
&& (*flags
& IS_STATIC
) == 0)
18112 && !((abbrev
->tag
== DW_TAG_class_type
18113 || abbrev
->tag
== DW_TAG_structure_type
18114 || abbrev
->tag
== DW_TAG_union_type
)
18115 && abbrev
->has_children
))
18117 *linkage_name
= nullptr;
18120 else if ((*name
== nullptr
18121 || (*linkage_name
== nullptr
18122 && tag_can_have_linkage_name (abbrev
->tag
))
18123 || (*parent_entry
== nullptr && m_language
!= language_c
))
18124 && origin_offset
!= sect_offset (0))
18126 cutu_reader
*new_reader
18127 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
18128 origin_is_dwz
, false);
18129 if (new_reader
!= nullptr)
18131 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
18132 + to_underlying (origin_offset
));
18134 if (new_reader
->cu
== reader
->cu
18135 && new_info_ptr
> watermark_ptr
18136 && maybe_defer
!= nullptr
18137 && *parent_entry
== nullptr)
18138 *maybe_defer
= form_addr (origin_offset
, origin_is_dwz
);
18139 else if (*parent_entry
== nullptr)
18141 CORE_ADDR lookup
= form_addr (origin_offset
, origin_is_dwz
);
18143 = (cooked_index_entry
*) addrmap_find (m_die_range_map
,
18147 unsigned int bytes_read
;
18148 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
18151 new_info_ptr
+= bytes_read
;
18152 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
, new_info_ptr
,
18153 new_abbrev
, name
, linkage_name
, flags
, nullptr,
18154 parent_entry
, maybe_defer
, true);
18158 if (!for_specification
)
18160 if (m_language
== language_ada
18161 && *linkage_name
== nullptr)
18162 *linkage_name
= *name
;
18164 if (!scanning_per_cu
->addresses_seen
18165 && low_pc
.has_value ()
18166 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
18168 && high_pc
.has_value ())
18170 if (high_pc_relative
)
18171 high_pc
= *high_pc
+ *low_pc
;
18173 if (*high_pc
> *low_pc
)
18175 struct objfile
*objfile
= reader
->cu
->per_objfile
->objfile
;
18176 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18177 struct gdbarch
*gdbarch
= objfile
->arch ();
18179 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *low_pc
+ baseaddr
)
18182 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *high_pc
+ baseaddr
)
18184 addrmap_set_empty (m_index_storage
->get_addrmap (), lo
, hi
- 1,
18189 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
18190 *flags
&= ~IS_STATIC
;
18192 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
18193 *name
= "(anonymous namespace)";
18195 if (m_language
== language_cplus
18196 && (abbrev
->tag
== DW_TAG_class_type
18197 || abbrev
->tag
== DW_TAG_interface_type
18198 || abbrev
->tag
== DW_TAG_structure_type
18199 || abbrev
->tag
== DW_TAG_union_type
18200 || abbrev
->tag
== DW_TAG_enumeration_type
18201 || abbrev
->tag
== DW_TAG_enumerator
))
18202 *flags
&= ~IS_STATIC
;
18209 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
18210 const gdb_byte
*info_ptr
,
18211 const abbrev_info
*abbrev
)
18213 sect_offset sect_off
{};
18214 bool is_dwz
= false;
18216 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18218 /* Note that we never need to reprocess attributes here. */
18220 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18222 if (attr
.name
== DW_AT_import
)
18224 sect_off
= attr
.get_ref_die_offset ();
18225 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
18226 || reader
->cu
->per_cu
->is_dwz
);
18230 /* Did not find DW_AT_import. */
18231 if (sect_off
== sect_offset (0))
18234 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
18235 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
18237 if (new_reader
!= nullptr)
18239 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
18241 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
18248 cooked_indexer::recurse (cutu_reader
*reader
,
18249 const gdb_byte
*info_ptr
,
18250 const cooked_index_entry
*parent_entry
,
18253 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18255 if (parent_entry
!= nullptr)
18257 CORE_ADDR start
= form_addr (parent_entry
->die_offset
,
18258 reader
->cu
->per_cu
->is_dwz
);
18259 CORE_ADDR end
= form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
18260 reader
->cu
->per_cu
->is_dwz
);
18261 addrmap_set_empty (m_die_range_map
, start
, end
, (void *) parent_entry
);
18268 cooked_indexer::index_dies (cutu_reader
*reader
,
18269 const gdb_byte
*info_ptr
,
18270 const cooked_index_entry
*parent_entry
,
18273 const gdb_byte
*end_ptr
= info_ptr
+ reader
->cu
->header
.get_length ();
18275 while (info_ptr
< end_ptr
)
18277 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
18278 unsigned int bytes_read
;
18279 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
18281 info_ptr
+= bytes_read
;
18282 if (abbrev
== nullptr)
18285 if (abbrev
->tag
== DW_TAG_imported_unit
)
18287 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
18291 if (!abbrev
->interesting
)
18293 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
18294 if (fully
&& abbrev
->has_children
)
18295 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18299 const char *name
= nullptr;
18300 const char *linkage_name
= nullptr;
18301 CORE_ADDR defer
= 0;
18302 cooked_index_flag flags
= IS_STATIC
;
18303 sect_offset sibling
{};
18304 const cooked_index_entry
*this_parent_entry
= parent_entry
;
18305 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
18306 info_ptr
, abbrev
, &name
, &linkage_name
,
18307 &flags
, &sibling
, &this_parent_entry
,
18310 if (abbrev
->tag
== DW_TAG_namespace
18311 && m_language
== language_cplus
18312 && strcmp (name
, "::") == 0)
18314 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18315 generated bogus DW_TAG_namespace DIEs with a name of "::"
18316 for the global namespace. Work around this problem
18321 const cooked_index_entry
*this_entry
= nullptr;
18322 if (name
!= nullptr)
18325 m_deferred_entries
.push_back ({
18326 this_die
, name
, defer
, abbrev
->tag
, flags
18329 this_entry
= m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18330 name
, this_parent_entry
,
18334 if (linkage_name
!= nullptr)
18336 /* We only want this to be "main" if it has a linkage name
18337 but not an ordinary name. */
18338 if (name
!= nullptr)
18339 flags
= flags
& ~IS_MAIN
;
18340 /* Set the IS_LINKAGE on for everything except when functions
18341 have linkage name present but name is absent. */
18342 if (name
!= nullptr
18343 || (abbrev
->tag
!= DW_TAG_subprogram
18344 && abbrev
->tag
!= DW_TAG_inlined_subroutine
18345 && abbrev
->tag
!= DW_TAG_entry_point
))
18346 flags
= flags
| IS_LINKAGE
;
18347 m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18348 linkage_name
, nullptr, m_per_cu
);
18351 if (abbrev
->has_children
)
18353 switch (abbrev
->tag
)
18355 case DW_TAG_class_type
:
18356 case DW_TAG_interface_type
:
18357 case DW_TAG_structure_type
:
18358 case DW_TAG_union_type
:
18359 if (m_language
!= language_c
&& this_entry
!= nullptr)
18361 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18366 case DW_TAG_enumeration_type
:
18367 /* We need to recurse even for an anonymous enumeration.
18368 Which scope we record as the parent scope depends on
18369 whether we're reading an "enum class". If so, we use
18370 the enum itself as the parent, yielding names like
18371 "enum_class::enumerator"; otherwise we inject the
18372 names into our own parent scope. */
18373 info_ptr
= recurse (reader
, info_ptr
,
18374 ((flags
& IS_ENUM_CLASS
) == 0)
18380 case DW_TAG_module
:
18381 if (this_entry
== nullptr)
18384 case DW_TAG_namespace
:
18385 /* We don't check THIS_ENTRY for a namespace, to handle
18386 the ancient G++ workaround pointed out above. */
18387 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18390 case DW_TAG_subprogram
:
18391 if ((m_language
== language_fortran
18392 || m_language
== language_ada
)
18393 && this_entry
!= nullptr)
18395 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
18401 if (sibling
!= sect_offset (0))
18403 const gdb_byte
*sibling_ptr
18404 = reader
->buffer
+ to_underlying (sibling
);
18406 if (sibling_ptr
< info_ptr
)
18407 complaint (_("DW_AT_sibling points backwards"));
18408 else if (sibling_ptr
> reader
->buffer_end
)
18409 reader
->die_section
->overflow_complaint ();
18411 info_ptr
= sibling_ptr
;
18414 info_ptr
= skip_children (reader
, info_ptr
);
18422 cooked_indexer::make_index (cutu_reader
*reader
)
18424 check_bounds (reader
);
18425 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
18426 if (!reader
->comp_unit_die
->has_children
)
18428 index_dies (reader
, reader
->info_ptr
, nullptr, false);
18430 for (const auto &entry
: m_deferred_entries
)
18432 CORE_ADDR key
= form_addr (entry
.die_offset
, m_per_cu
->is_dwz
);
18433 cooked_index_entry
*parent
18434 = (cooked_index_entry
*) addrmap_find (m_die_range_map
, key
);
18435 m_index_storage
->add (entry
.die_offset
, entry
.tag
, entry
.flags
,
18436 entry
.name
, parent
, m_per_cu
);
18440 /* An implementation of quick_symbol_functions for the cooked DWARF
18443 struct cooked_index_functions
: public dwarf2_base_index_functions
18445 dwarf2_per_cu_data
*find_per_cu (dwarf2_per_bfd
*per_bfd
,
18446 CORE_ADDR adjusted_pc
) override
;
18448 struct compunit_symtab
*find_compunit_symtab_by_address
18449 (struct objfile
*objfile
, CORE_ADDR address
) override
;
18451 void dump (struct objfile
*objfile
) override
18453 gdb_printf ("Cooked index in use\n");
18456 void expand_matching_symbols
18458 const lookup_name_info
&lookup_name
,
18459 domain_enum domain
,
18461 symbol_compare_ftype
*ordered_compare
) override
;
18463 bool expand_symtabs_matching
18464 (struct objfile
*objfile
,
18465 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18466 const lookup_name_info
*lookup_name
,
18467 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18468 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18469 block_search_flags search_flags
,
18470 domain_enum domain
,
18471 enum search_domain kind
) override
;
18473 bool can_lazily_read_symbols () override
18478 void read_partial_symbols (struct objfile
*objfile
) override
18480 if (dwarf2_has_info (objfile
, nullptr))
18481 dwarf2_build_psymtabs (objfile
);
18485 dwarf2_per_cu_data
*
18486 cooked_index_functions::find_per_cu (dwarf2_per_bfd
*per_bfd
,
18487 CORE_ADDR adjusted_pc
)
18489 cooked_index_vector
*table
18490 = (static_cast<cooked_index_vector
*>
18491 (per_bfd
->index_table
.get ()));
18492 if (table
== nullptr)
18494 return table
->lookup (adjusted_pc
);
18497 struct compunit_symtab
*
18498 cooked_index_functions::find_compunit_symtab_by_address
18499 (struct objfile
*objfile
, CORE_ADDR address
)
18501 if (objfile
->sect_index_data
== -1)
18504 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18505 if (per_objfile
->per_bfd
->index_table
== nullptr)
18508 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
18509 cooked_index_vector
*table
18510 = (static_cast<cooked_index_vector
*>
18511 (per_objfile
->per_bfd
->index_table
.get ()));
18512 dwarf2_per_cu_data
*per_cu
= table
->lookup (address
- baseaddr
);
18513 if (per_cu
== nullptr)
18516 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
18520 cooked_index_functions::expand_matching_symbols
18521 (struct objfile
*objfile
,
18522 const lookup_name_info
&lookup_name
,
18523 domain_enum domain
,
18525 symbol_compare_ftype
*ordered_compare
)
18527 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18528 if (per_objfile
->per_bfd
->index_table
== nullptr)
18530 const block_search_flags search_flags
= (global
18531 ? SEARCH_GLOBAL_BLOCK
18532 : SEARCH_STATIC_BLOCK
);
18533 const language_defn
*lang
= language_def (language_ada
);
18534 symbol_name_matcher_ftype
*name_match
18535 = lang
->get_symbol_name_matcher (lookup_name
);
18537 cooked_index_vector
*table
18538 = (static_cast<cooked_index_vector
*>
18539 (per_objfile
->per_bfd
->index_table
.get ()));
18540 for (const cooked_index_entry
*entry
: table
->all_entries ())
18542 if (entry
->parent_entry
!= nullptr)
18545 if (!entry
->matches (search_flags
)
18546 || !entry
->matches (domain
))
18549 if (name_match (entry
->canonical
, lookup_name
, nullptr))
18550 dw2_instantiate_symtab (entry
->per_cu
, per_objfile
, false);
18555 cooked_index_functions::expand_symtabs_matching
18556 (struct objfile
*objfile
,
18557 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18558 const lookup_name_info
*lookup_name
,
18559 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18560 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18561 block_search_flags search_flags
,
18562 domain_enum domain
,
18563 enum search_domain kind
)
18565 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18566 if (per_objfile
->per_bfd
->index_table
== nullptr)
18569 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
18571 /* This invariant is documented in quick-functions.h. */
18572 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
18573 if (lookup_name
== nullptr)
18575 for (dwarf2_per_cu_data
*per_cu
18576 : all_comp_units_range (per_objfile
->per_bfd
))
18580 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
18588 lookup_name_info lookup_name_without_params
18589 = lookup_name
->make_ignore_params ();
18590 bool completing
= lookup_name
->completion_mode ();
18592 /* Unique styles of language splitting. */
18593 static const enum language unique_styles
[] =
18595 /* No splitting is also a style. */
18597 /* This includes Rust. */
18599 /* This includes Go. */
18604 cooked_index_vector
*table
18605 = (static_cast<cooked_index_vector
*>
18606 (per_objfile
->per_bfd
->index_table
.get ()));
18607 for (enum language lang
: unique_styles
)
18609 std::vector
<gdb::string_view
> name_vec
18610 = lookup_name_without_params
.split_name (lang
);
18612 for (const cooked_index_entry
*entry
: table
->find (name_vec
.back (),
18615 /* No need to consider symbols from expanded CUs. */
18616 if (per_objfile
->symtab_set_p (entry
->per_cu
))
18619 /* If file-matching was done, we don't need to consider
18620 symbols from unmarked CUs. */
18621 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
18624 /* See if the symbol matches the type filter. */
18625 if (!entry
->matches (search_flags
)
18626 || !entry
->matches (domain
)
18627 || !entry
->matches (kind
))
18630 /* We've found the base name of the symbol; now walk its
18631 parentage chain, ensuring that each component
18635 const cooked_index_entry
*parent
= entry
->parent_entry
;
18636 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
18638 /* If we ran out of entries, or if this segment doesn't
18639 match, this did not match. */
18640 if (parent
== nullptr
18641 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
18642 name_vec
[i
- 1].length ()) != 0)
18648 parent
= parent
->parent_entry
;
18654 /* Might have been looking for "a::b" and found
18656 if (symbol_matcher
== nullptr)
18658 symbol_name_match_type match_type
18659 = lookup_name_without_params
.match_type ();
18660 if ((match_type
== symbol_name_match_type::FULL
18661 || (lang
!= language_ada
18662 && match_type
== symbol_name_match_type::EXPRESSION
))
18663 && parent
!= nullptr)
18668 auto_obstack temp_storage
;
18669 const char *full_name
= entry
->full_name (&temp_storage
);
18670 if (!symbol_matcher (full_name
))
18674 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
18684 /* Return a new cooked_index_functions object. */
18686 static quick_symbol_functions_up
18687 make_cooked_index_funcs ()
18689 return quick_symbol_functions_up (new cooked_index_functions
);
18692 quick_symbol_functions_up
18693 cooked_index_vector::make_quick_functions () const
18695 return make_cooked_index_funcs ();
18700 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18701 contents from the given SECTION in the HEADER.
18703 HEADER_OFFSET is the offset of the header in the section. */
18705 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
18706 struct dwarf2_section_info
*section
,
18707 sect_offset header_offset
)
18709 unsigned int bytes_read
;
18710 bfd
*abfd
= section
->get_bfd_owner ();
18711 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
18713 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
18714 info_ptr
+= bytes_read
;
18716 header
->version
= read_2_bytes (abfd
, info_ptr
);
18719 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
18722 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
18725 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
18728 /* Return the DW_AT_loclists_base value for the CU. */
18730 lookup_loclist_base (struct dwarf2_cu
*cu
)
18732 /* For the .dwo unit, the loclist_base points to the first offset following
18733 the header. The header consists of the following entities-
18734 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18736 2. version (2 bytes)
18737 3. address size (1 byte)
18738 4. segment selector size (1 byte)
18739 5. offset entry count (4 bytes)
18740 These sizes are derived as per the DWARFv5 standard. */
18741 if (cu
->dwo_unit
!= nullptr)
18743 if (cu
->header
.initial_length_size
== 4)
18744 return LOCLIST_HEADER_SIZE32
;
18745 return LOCLIST_HEADER_SIZE64
;
18747 return cu
->loclist_base
;
18750 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18751 array of offsets in the .debug_loclists section. */
18754 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
18756 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18757 struct objfile
*objfile
= per_objfile
->objfile
;
18758 bfd
*abfd
= objfile
->obfd
;
18759 ULONGEST loclist_header_size
=
18760 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
18761 : LOCLIST_HEADER_SIZE64
);
18762 ULONGEST loclist_base
= lookup_loclist_base (cu
);
18764 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18765 ULONGEST start_offset
=
18766 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
18768 /* Get loclists section. */
18769 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
18771 /* Read the loclists section content. */
18772 section
->read (objfile
);
18773 if (section
->buffer
== NULL
)
18774 error (_("DW_FORM_loclistx used without .debug_loclists "
18775 "section [in module %s]"), objfile_name (objfile
));
18777 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18778 so if loclist_base is smaller than the header size, we have a problem. */
18779 if (loclist_base
< loclist_header_size
)
18780 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18781 objfile_name (objfile
));
18783 /* Read the header of the loclists contribution. */
18784 struct loclists_rnglists_header header
;
18785 read_loclists_rnglists_header (&header
, section
,
18786 (sect_offset
) (loclist_base
- loclist_header_size
));
18788 /* Verify the loclist index is valid. */
18789 if (loclist_index
>= header
.offset_entry_count
)
18790 error (_("DW_FORM_loclistx pointing outside of "
18791 ".debug_loclists offset array [in module %s]"),
18792 objfile_name (objfile
));
18794 /* Validate that reading won't go beyond the end of the section. */
18795 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18796 error (_("Reading DW_FORM_loclistx index beyond end of"
18797 ".debug_loclists section [in module %s]"),
18798 objfile_name (objfile
));
18800 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18802 if (cu
->header
.offset_size
== 4)
18803 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
18805 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
18808 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18809 array of offsets in the .debug_rnglists section. */
18812 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
18815 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
18816 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
18817 bfd
*abfd
= objfile
->obfd
;
18818 ULONGEST rnglist_header_size
=
18819 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
18820 : RNGLIST_HEADER_SIZE64
);
18822 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18823 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18825 ULONGEST rnglist_base
=
18826 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
18828 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18829 ULONGEST start_offset
=
18830 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
18832 /* Get rnglists section. */
18833 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
18835 /* Read the rnglists section content. */
18836 section
->read (objfile
);
18837 if (section
->buffer
== nullptr)
18838 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18840 objfile_name (objfile
));
18842 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18843 so if rnglist_base is smaller than the header size, we have a problem. */
18844 if (rnglist_base
< rnglist_header_size
)
18845 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18846 objfile_name (objfile
));
18848 /* Read the header of the rnglists contribution. */
18849 struct loclists_rnglists_header header
;
18850 read_loclists_rnglists_header (&header
, section
,
18851 (sect_offset
) (rnglist_base
- rnglist_header_size
));
18853 /* Verify the rnglist index is valid. */
18854 if (rnglist_index
>= header
.offset_entry_count
)
18855 error (_("DW_FORM_rnglistx index pointing outside of "
18856 ".debug_rnglists offset array [in module %s]"),
18857 objfile_name (objfile
));
18859 /* Validate that reading won't go beyond the end of the section. */
18860 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18861 error (_("Reading DW_FORM_rnglistx index beyond end of"
18862 ".debug_rnglists section [in module %s]"),
18863 objfile_name (objfile
));
18865 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18867 if (cu
->header
.offset_size
== 4)
18868 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
18870 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
18873 /* Process the attributes that had to be skipped in the first round. These
18874 attributes are the ones that need str_offsets_base or addr_base attributes.
18875 They could not have been processed in the first round, because at the time
18876 the values of str_offsets_base or addr_base may not have been known. */
18878 read_attribute_reprocess (const struct die_reader_specs
*reader
,
18879 struct attribute
*attr
, dwarf_tag tag
)
18881 struct dwarf2_cu
*cu
= reader
->cu
;
18882 switch (attr
->form
)
18884 case DW_FORM_addrx
:
18885 case DW_FORM_GNU_addr_index
:
18886 attr
->set_address (read_addr_index (cu
,
18887 attr
->as_unsigned_reprocess ()));
18889 case DW_FORM_loclistx
:
18891 sect_offset loclists_sect_off
18892 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
18894 attr
->set_unsigned (to_underlying (loclists_sect_off
));
18897 case DW_FORM_rnglistx
:
18899 sect_offset rnglists_sect_off
18900 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
18902 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
18906 case DW_FORM_strx1
:
18907 case DW_FORM_strx2
:
18908 case DW_FORM_strx3
:
18909 case DW_FORM_strx4
:
18910 case DW_FORM_GNU_str_index
:
18912 unsigned int str_index
= attr
->as_unsigned_reprocess ();
18913 gdb_assert (!attr
->canonical_string_p ());
18914 if (reader
->dwo_file
!= NULL
)
18915 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
18918 attr
->set_string_noncanonical (read_stub_str_index (cu
,
18923 gdb_assert_not_reached ("Unexpected DWARF form.");
18927 /* Read an attribute value described by an attribute form. */
18929 static const gdb_byte
*
18930 read_attribute_value (const struct die_reader_specs
*reader
,
18931 struct attribute
*attr
, unsigned form
,
18932 LONGEST implicit_const
, const gdb_byte
*info_ptr
)
18934 struct dwarf2_cu
*cu
= reader
->cu
;
18935 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18936 struct objfile
*objfile
= per_objfile
->objfile
;
18937 bfd
*abfd
= reader
->abfd
;
18938 struct comp_unit_head
*cu_header
= &cu
->header
;
18939 unsigned int bytes_read
;
18940 struct dwarf_block
*blk
;
18942 attr
->form
= (enum dwarf_form
) form
;
18945 case DW_FORM_ref_addr
:
18946 if (cu_header
->version
== 2)
18947 attr
->set_unsigned (cu_header
->read_address (abfd
, info_ptr
,
18950 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18952 info_ptr
+= bytes_read
;
18954 case DW_FORM_GNU_ref_alt
:
18955 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18957 info_ptr
+= bytes_read
;
18961 struct gdbarch
*gdbarch
= objfile
->arch ();
18962 CORE_ADDR addr
= cu_header
->read_address (abfd
, info_ptr
, &bytes_read
);
18963 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
18964 attr
->set_address (addr
);
18965 info_ptr
+= bytes_read
;
18968 case DW_FORM_block2
:
18969 blk
= dwarf_alloc_block (cu
);
18970 blk
->size
= read_2_bytes (abfd
, info_ptr
);
18972 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
18973 info_ptr
+= blk
->size
;
18974 attr
->set_block (blk
);
18976 case DW_FORM_block4
:
18977 blk
= dwarf_alloc_block (cu
);
18978 blk
->size
= read_4_bytes (abfd
, info_ptr
);
18980 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
18981 info_ptr
+= blk
->size
;
18982 attr
->set_block (blk
);
18984 case DW_FORM_data2
:
18985 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
18988 case DW_FORM_data4
:
18989 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
18992 case DW_FORM_data8
:
18993 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
18996 case DW_FORM_data16
:
18997 blk
= dwarf_alloc_block (cu
);
18999 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
19001 attr
->set_block (blk
);
19003 case DW_FORM_sec_offset
:
19004 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19006 info_ptr
+= bytes_read
;
19008 case DW_FORM_loclistx
:
19010 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19012 info_ptr
+= bytes_read
;
19015 case DW_FORM_string
:
19016 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
19018 info_ptr
+= bytes_read
;
19021 if (!cu
->per_cu
->is_dwz
)
19023 attr
->set_string_noncanonical
19024 (read_indirect_string (per_objfile
,
19025 abfd
, info_ptr
, cu_header
,
19027 info_ptr
+= bytes_read
;
19031 case DW_FORM_line_strp
:
19032 if (!cu
->per_cu
->is_dwz
)
19034 attr
->set_string_noncanonical
19035 (per_objfile
->read_line_string (info_ptr
, cu_header
,
19037 info_ptr
+= bytes_read
;
19041 case DW_FORM_GNU_strp_alt
:
19043 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19044 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
19047 attr
->set_string_noncanonical
19048 (dwz
->read_string (objfile
, str_offset
));
19049 info_ptr
+= bytes_read
;
19052 case DW_FORM_exprloc
:
19053 case DW_FORM_block
:
19054 blk
= dwarf_alloc_block (cu
);
19055 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19056 info_ptr
+= bytes_read
;
19057 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19058 info_ptr
+= blk
->size
;
19059 attr
->set_block (blk
);
19061 case DW_FORM_block1
:
19062 blk
= dwarf_alloc_block (cu
);
19063 blk
->size
= read_1_byte (abfd
, info_ptr
);
19065 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19066 info_ptr
+= blk
->size
;
19067 attr
->set_block (blk
);
19069 case DW_FORM_data1
:
19071 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
19074 case DW_FORM_flag_present
:
19075 attr
->set_unsigned (1);
19077 case DW_FORM_sdata
:
19078 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
19079 info_ptr
+= bytes_read
;
19081 case DW_FORM_rnglistx
:
19083 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19085 info_ptr
+= bytes_read
;
19088 case DW_FORM_udata
:
19089 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
19090 info_ptr
+= bytes_read
;
19093 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19094 + read_1_byte (abfd
, info_ptr
)));
19098 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19099 + read_2_bytes (abfd
, info_ptr
)));
19103 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19104 + read_4_bytes (abfd
, info_ptr
)));
19108 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19109 + read_8_bytes (abfd
, info_ptr
)));
19112 case DW_FORM_ref_sig8
:
19113 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
19116 case DW_FORM_ref_udata
:
19117 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19118 + read_unsigned_leb128 (abfd
, info_ptr
,
19120 info_ptr
+= bytes_read
;
19122 case DW_FORM_indirect
:
19123 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19124 info_ptr
+= bytes_read
;
19125 if (form
== DW_FORM_implicit_const
)
19127 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
19128 info_ptr
+= bytes_read
;
19130 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
19133 case DW_FORM_implicit_const
:
19134 attr
->set_signed (implicit_const
);
19136 case DW_FORM_addrx
:
19137 case DW_FORM_GNU_addr_index
:
19138 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19140 info_ptr
+= bytes_read
;
19143 case DW_FORM_strx1
:
19144 case DW_FORM_strx2
:
19145 case DW_FORM_strx3
:
19146 case DW_FORM_strx4
:
19147 case DW_FORM_GNU_str_index
:
19149 ULONGEST str_index
;
19150 if (form
== DW_FORM_strx1
)
19152 str_index
= read_1_byte (abfd
, info_ptr
);
19155 else if (form
== DW_FORM_strx2
)
19157 str_index
= read_2_bytes (abfd
, info_ptr
);
19160 else if (form
== DW_FORM_strx3
)
19162 str_index
= read_3_bytes (abfd
, info_ptr
);
19165 else if (form
== DW_FORM_strx4
)
19167 str_index
= read_4_bytes (abfd
, info_ptr
);
19172 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19173 info_ptr
+= bytes_read
;
19175 attr
->set_unsigned_reprocess (str_index
);
19179 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19180 dwarf_form_name (form
),
19181 bfd_get_filename (abfd
));
19185 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
19186 attr
->form
= DW_FORM_GNU_ref_alt
;
19188 /* We have seen instances where the compiler tried to emit a byte
19189 size attribute of -1 which ended up being encoded as an unsigned
19190 0xffffffff. Although 0xffffffff is technically a valid size value,
19191 an object of this size seems pretty unlikely so we can relatively
19192 safely treat these cases as if the size attribute was invalid and
19193 treat them as zero by default. */
19194 if (attr
->name
== DW_AT_byte_size
19195 && form
== DW_FORM_data4
19196 && attr
->as_unsigned () >= 0xffffffff)
19199 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19200 hex_string (attr
->as_unsigned ()));
19201 attr
->set_unsigned (0);
19207 /* Read an attribute described by an abbreviated attribute. */
19209 static const gdb_byte
*
19210 read_attribute (const struct die_reader_specs
*reader
,
19211 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
19212 const gdb_byte
*info_ptr
)
19214 attr
->name
= abbrev
->name
;
19215 attr
->string_is_canonical
= 0;
19216 attr
->requires_reprocessing
= 0;
19217 return read_attribute_value (reader
, attr
, abbrev
->form
,
19218 abbrev
->implicit_const
, info_ptr
);
19221 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19223 static const char *
19224 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
19225 LONGEST str_offset
)
19227 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
19228 str_offset
, "DW_FORM_strp");
19231 /* Return pointer to string at .debug_str offset as read from BUF.
19232 BUF is assumed to be in a compilation unit described by CU_HEADER.
19233 Return *BYTES_READ_PTR count of bytes read from BUF. */
19235 static const char *
19236 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
19237 const gdb_byte
*buf
,
19238 const struct comp_unit_head
*cu_header
,
19239 unsigned int *bytes_read_ptr
)
19241 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19243 return read_indirect_string_at_offset (per_objfile
, str_offset
);
19249 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19250 unsigned int offset_size
)
19252 bfd
*abfd
= objfile
->obfd
;
19253 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
19255 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19261 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19262 const struct comp_unit_head
*cu_header
,
19263 unsigned int *bytes_read_ptr
)
19265 bfd
*abfd
= objfile
->obfd
;
19266 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19268 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19271 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19272 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19273 ADDR_SIZE is the size of addresses from the CU header. */
19276 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
19277 gdb::optional
<ULONGEST
> addr_base
, int addr_size
)
19279 struct objfile
*objfile
= per_objfile
->objfile
;
19280 bfd
*abfd
= objfile
->obfd
;
19281 const gdb_byte
*info_ptr
;
19282 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
19284 per_objfile
->per_bfd
->addr
.read (objfile
);
19285 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
19286 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19287 objfile_name (objfile
));
19288 if (addr_base_or_zero
+ addr_index
* addr_size
19289 >= per_objfile
->per_bfd
->addr
.size
)
19290 error (_("DW_FORM_addr_index pointing outside of "
19291 ".debug_addr section [in module %s]"),
19292 objfile_name (objfile
));
19293 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
19294 + addr_index
* addr_size
);
19295 if (addr_size
== 4)
19296 return bfd_get_32 (abfd
, info_ptr
);
19298 return bfd_get_64 (abfd
, info_ptr
);
19301 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19304 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
19306 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
19307 cu
->addr_base
, cu
->header
.addr_size
);
19310 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19313 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
19314 unsigned int *bytes_read
)
19316 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
;
19317 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
19319 return read_addr_index (cu
, addr_index
);
19325 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
19326 dwarf2_per_objfile
*per_objfile
,
19327 unsigned int addr_index
)
19329 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
19330 gdb::optional
<ULONGEST
> addr_base
;
19333 /* We need addr_base and addr_size.
19334 If we don't have PER_CU->cu, we have to get it.
19335 Nasty, but the alternative is storing the needed info in PER_CU,
19336 which at this point doesn't seem justified: it's not clear how frequently
19337 it would get used and it would increase the size of every PER_CU.
19338 Entry points like dwarf2_per_cu_addr_size do a similar thing
19339 so we're not in uncharted territory here.
19340 Alas we need to be a bit more complicated as addr_base is contained
19343 We don't need to read the entire CU(/TU).
19344 We just need the header and top level die.
19346 IWBN to use the aging mechanism to let us lazily later discard the CU.
19347 For now we skip this optimization. */
19351 addr_base
= cu
->addr_base
;
19352 addr_size
= cu
->header
.addr_size
;
19356 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
19357 addr_base
= reader
.cu
->addr_base
;
19358 addr_size
= reader
.cu
->header
.addr_size
;
19361 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
19364 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19365 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19368 static const char *
19369 read_str_index (struct dwarf2_cu
*cu
,
19370 struct dwarf2_section_info
*str_section
,
19371 struct dwarf2_section_info
*str_offsets_section
,
19372 ULONGEST str_offsets_base
, ULONGEST str_index
)
19374 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19375 struct objfile
*objfile
= per_objfile
->objfile
;
19376 const char *objf_name
= objfile_name (objfile
);
19377 bfd
*abfd
= objfile
->obfd
;
19378 const gdb_byte
*info_ptr
;
19379 ULONGEST str_offset
;
19380 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19382 str_section
->read (objfile
);
19383 str_offsets_section
->read (objfile
);
19384 if (str_section
->buffer
== NULL
)
19385 error (_("%s used without %s section"
19386 " in CU at offset %s [in module %s]"),
19387 form_name
, str_section
->get_name (),
19388 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19389 if (str_offsets_section
->buffer
== NULL
)
19390 error (_("%s used without %s section"
19391 " in CU at offset %s [in module %s]"),
19392 form_name
, str_section
->get_name (),
19393 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19394 info_ptr
= (str_offsets_section
->buffer
19396 + str_index
* cu
->header
.offset_size
);
19397 if (cu
->header
.offset_size
== 4)
19398 str_offset
= bfd_get_32 (abfd
, info_ptr
);
19400 str_offset
= bfd_get_64 (abfd
, info_ptr
);
19401 if (str_offset
>= str_section
->size
)
19402 error (_("Offset from %s pointing outside of"
19403 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19404 form_name
, sect_offset_str (cu
->header
.sect_off
), objf_name
);
19405 return (const char *) (str_section
->buffer
+ str_offset
);
19408 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19410 static const char *
19411 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
19413 ULONGEST str_offsets_base
= reader
->cu
->header
.version
>= 5
19414 ? reader
->cu
->header
.addr_size
: 0;
19415 return read_str_index (reader
->cu
,
19416 &reader
->dwo_file
->sections
.str
,
19417 &reader
->dwo_file
->sections
.str_offsets
,
19418 str_offsets_base
, str_index
);
19421 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19423 static const char *
19424 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
19426 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19427 const char *objf_name
= objfile_name (objfile
);
19428 static const char form_name
[] = "DW_FORM_GNU_str_index";
19429 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
19431 if (!cu
->str_offsets_base
.has_value ())
19432 error (_("%s used in Fission stub without %s"
19433 " in CU at offset 0x%lx [in module %s]"),
19434 form_name
, str_offsets_attr_name
,
19435 (long) cu
->header
.offset_size
, objf_name
);
19437 return read_str_index (cu
,
19438 &cu
->per_objfile
->per_bfd
->str
,
19439 &cu
->per_objfile
->per_bfd
->str_offsets
,
19440 *cu
->str_offsets_base
, str_index
);
19443 /* Return the length of an LEB128 number in BUF. */
19446 leb128_size (const gdb_byte
*buf
)
19448 const gdb_byte
*begin
= buf
;
19454 if ((byte
& 128) == 0)
19455 return buf
- begin
;
19459 static enum language
19460 dwarf_lang_to_enum_language (unsigned int lang
)
19462 enum language language
;
19471 language
= language_c
;
19474 case DW_LANG_C_plus_plus
:
19475 case DW_LANG_C_plus_plus_11
:
19476 case DW_LANG_C_plus_plus_14
:
19477 language
= language_cplus
;
19480 language
= language_d
;
19482 case DW_LANG_Fortran77
:
19483 case DW_LANG_Fortran90
:
19484 case DW_LANG_Fortran95
:
19485 case DW_LANG_Fortran03
:
19486 case DW_LANG_Fortran08
:
19487 language
= language_fortran
;
19490 language
= language_go
;
19492 case DW_LANG_Mips_Assembler
:
19493 language
= language_asm
;
19495 case DW_LANG_Ada83
:
19496 case DW_LANG_Ada95
:
19497 language
= language_ada
;
19499 case DW_LANG_Modula2
:
19500 language
= language_m2
;
19502 case DW_LANG_Pascal83
:
19503 language
= language_pascal
;
19506 language
= language_objc
;
19509 case DW_LANG_Rust_old
:
19510 language
= language_rust
;
19512 case DW_LANG_OpenCL
:
19513 language
= language_opencl
;
19515 case DW_LANG_Cobol74
:
19516 case DW_LANG_Cobol85
:
19518 language
= language_minimal
;
19525 /* Return the named attribute or NULL if not there. */
19527 static struct attribute
*
19528 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19533 struct attribute
*spec
= NULL
;
19535 for (i
= 0; i
< die
->num_attrs
; ++i
)
19537 if (die
->attrs
[i
].name
== name
)
19538 return &die
->attrs
[i
];
19539 if (die
->attrs
[i
].name
== DW_AT_specification
19540 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
19541 spec
= &die
->attrs
[i
];
19547 die
= follow_die_ref (die
, spec
, &cu
);
19553 /* Return the string associated with a string-typed attribute, or NULL if it
19554 is either not found or is of an incorrect type. */
19556 static const char *
19557 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19559 struct attribute
*attr
;
19560 const char *str
= NULL
;
19562 attr
= dwarf2_attr (die
, name
, cu
);
19566 str
= attr
->as_string ();
19567 if (str
== nullptr)
19568 complaint (_("string type expected for attribute %s for "
19569 "DIE at %s in module %s"),
19570 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
19571 objfile_name (cu
->per_objfile
->objfile
));
19577 /* Return the dwo name or NULL if not present. If present, it is in either
19578 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19579 static const char *
19580 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19582 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
19583 if (dwo_name
== nullptr)
19584 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
19588 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19589 and holds a non-zero value. This function should only be used for
19590 DW_FORM_flag or DW_FORM_flag_present attributes. */
19593 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
19595 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
19597 return attr
!= nullptr && attr
->as_boolean ();
19601 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
19603 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19604 which value is non-zero. However, we have to be careful with
19605 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19606 (via dwarf2_flag_true_p) follows this attribute. So we may
19607 end up accidently finding a declaration attribute that belongs
19608 to a different DIE referenced by the specification attribute,
19609 even though the given DIE does not have a declaration attribute. */
19610 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
19611 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
19614 /* Return the die giving the specification for DIE, if there is
19615 one. *SPEC_CU is the CU containing DIE on input, and the CU
19616 containing the return value on output. If there is no
19617 specification, but there is an abstract origin, that is
19620 static struct die_info
*
19621 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
19623 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
19626 if (spec_attr
== NULL
)
19627 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
19629 if (spec_attr
== NULL
)
19632 return follow_die_ref (die
, spec_attr
, spec_cu
);
19635 /* A convenience function to find the proper .debug_line section for a CU. */
19637 static struct dwarf2_section_info
*
19638 get_debug_line_section (struct dwarf2_cu
*cu
)
19640 struct dwarf2_section_info
*section
;
19641 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19643 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19645 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19646 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
19647 else if (cu
->per_cu
->is_dwz
)
19649 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19651 section
= &dwz
->line
;
19654 section
= &per_objfile
->per_bfd
->line
;
19659 /* Read the statement program header starting at OFFSET in
19660 .debug_line, or .debug_line.dwo. Return a pointer
19661 to a struct line_header, allocated using xmalloc.
19662 Returns NULL if there is a problem reading the header, e.g., if it
19663 has a version we don't understand.
19665 NOTE: the strings in the include directory and file name tables of
19666 the returned object point into the dwarf line section buffer,
19667 and must not be freed. */
19669 static line_header_up
19670 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
)
19672 struct dwarf2_section_info
*section
;
19673 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19675 section
= get_debug_line_section (cu
);
19676 section
->read (per_objfile
->objfile
);
19677 if (section
->buffer
== NULL
)
19679 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19680 complaint (_("missing .debug_line.dwo section"));
19682 complaint (_("missing .debug_line section"));
19686 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
19687 per_objfile
, section
, &cu
->header
);
19690 /* Subroutine of dwarf_decode_lines to simplify it.
19691 Return the file name for the given file_entry.
19692 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19693 If space for the result is malloc'd, *NAME_HOLDER will be set.
19694 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19695 equivalent to CU_INFO. */
19697 static const char *
19698 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
19699 const file_and_directory
&cu_info
,
19700 std::string
&name_holder
)
19702 const char *include_name
= fe
.name
;
19703 const char *include_name_to_compare
= include_name
;
19705 const char *dir_name
= fe
.include_dir (lh
);
19707 std::string hold_compare
;
19708 if (!IS_ABSOLUTE_PATH (include_name
)
19709 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
19711 /* Avoid creating a duplicate name for CU_INFO.
19712 We do this by comparing INCLUDE_NAME and CU_INFO.
19713 Before we do the comparison, however, we need to account
19714 for DIR_NAME and COMP_DIR.
19715 First prepend dir_name (if non-NULL). If we still don't
19716 have an absolute path prepend comp_dir (if non-NULL).
19717 However, the directory we record in the include-file's
19718 psymtab does not contain COMP_DIR (to match the
19719 corresponding symtab(s)).
19724 bash$ gcc -g ./hello.c
19725 include_name = "hello.c"
19727 DW_AT_comp_dir = comp_dir = "/tmp"
19728 DW_AT_name = "./hello.c"
19732 if (dir_name
!= NULL
)
19734 name_holder
= path_join (dir_name
, include_name
);
19735 include_name
= name_holder
.c_str ();
19736 include_name_to_compare
= include_name
;
19738 if (!IS_ABSOLUTE_PATH (include_name
)
19739 && cu_info
.get_comp_dir () != nullptr)
19741 hold_compare
= path_join (cu_info
.get_comp_dir (), include_name
);
19742 include_name_to_compare
= hold_compare
.c_str ();
19746 std::string copied_name
;
19747 const char *cu_filename
= cu_info
.get_name ();
19748 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
19750 copied_name
= path_join (cu_info
.get_comp_dir (), cu_filename
);
19751 cu_filename
= copied_name
.c_str ();
19754 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
19756 return include_name
;
19759 /* State machine to track the state of the line number program. */
19761 class lnp_state_machine
19764 /* Initialize a machine state for the start of a line number
19766 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
19768 file_entry
*current_file ()
19770 /* lh->file_names is 0-based, but the file name numbers in the
19771 statement program are 1-based. */
19772 return m_line_header
->file_name_at (m_file
);
19775 /* Record the line in the state machine. END_SEQUENCE is true if
19776 we're processing the end of a sequence. */
19777 void record_line (bool end_sequence
);
19779 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19780 nop-out rest of the lines in this sequence. */
19781 void check_line_address (struct dwarf2_cu
*cu
,
19782 const gdb_byte
*line_ptr
,
19783 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
);
19785 void handle_set_discriminator (unsigned int discriminator
)
19787 m_discriminator
= discriminator
;
19788 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
19791 /* Handle DW_LNE_set_address. */
19792 void handle_set_address (CORE_ADDR baseaddr
, CORE_ADDR address
)
19795 address
+= baseaddr
;
19796 m_address
= gdbarch_adjust_dwarf2_line (m_gdbarch
, address
, false);
19799 /* Handle DW_LNS_advance_pc. */
19800 void handle_advance_pc (CORE_ADDR adjust
);
19802 /* Handle a special opcode. */
19803 void handle_special_opcode (unsigned char op_code
);
19805 /* Handle DW_LNS_advance_line. */
19806 void handle_advance_line (int line_delta
)
19808 advance_line (line_delta
);
19811 /* Handle DW_LNS_set_file. */
19812 void handle_set_file (file_name_index file
);
19814 /* Handle DW_LNS_negate_stmt. */
19815 void handle_negate_stmt ()
19817 m_flags
^= LEF_IS_STMT
;
19820 /* Handle DW_LNS_const_add_pc. */
19821 void handle_const_add_pc ();
19823 /* Handle DW_LNS_fixed_advance_pc. */
19824 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
19826 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19830 /* Handle DW_LNS_copy. */
19831 void handle_copy ()
19833 record_line (false);
19834 m_discriminator
= 0;
19835 m_flags
&= ~LEF_PROLOGUE_END
;
19838 /* Handle DW_LNE_end_sequence. */
19839 void handle_end_sequence ()
19841 m_currently_recording_lines
= true;
19844 /* Handle DW_LNS_set_prologue_end. */
19845 void handle_set_prologue_end ()
19847 m_flags
|= LEF_PROLOGUE_END
;
19851 /* Advance the line by LINE_DELTA. */
19852 void advance_line (int line_delta
)
19854 m_line
+= line_delta
;
19856 if (line_delta
!= 0)
19857 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
19860 struct dwarf2_cu
*m_cu
;
19862 gdbarch
*m_gdbarch
;
19864 /* The line number header. */
19865 line_header
*m_line_header
;
19867 /* These are part of the standard DWARF line number state machine,
19868 and initialized according to the DWARF spec. */
19870 unsigned char m_op_index
= 0;
19871 /* The line table index of the current file. */
19872 file_name_index m_file
= 1;
19873 unsigned int m_line
= 1;
19875 /* These are initialized in the constructor. */
19877 CORE_ADDR m_address
;
19878 linetable_entry_flags m_flags
;
19879 unsigned int m_discriminator
;
19881 /* Additional bits of state we need to track. */
19883 /* The last file that we called dwarf2_start_subfile for.
19884 This is only used for TLLs. */
19885 unsigned int m_last_file
= 0;
19886 /* The last file a line number was recorded for. */
19887 struct subfile
*m_last_subfile
= NULL
;
19889 /* The address of the last line entry. */
19890 CORE_ADDR m_last_address
;
19892 /* Set to true when a previous line at the same address (using
19893 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19894 when a line entry at a new address (m_address different to
19895 m_last_address) is processed. */
19896 bool m_stmt_at_address
= false;
19898 /* When true, record the lines we decode. */
19899 bool m_currently_recording_lines
= false;
19901 /* The last line number that was recorded, used to coalesce
19902 consecutive entries for the same line. This can happen, for
19903 example, when discriminators are present. PR 17276. */
19904 unsigned int m_last_line
= 0;
19905 bool m_line_has_non_zero_discriminator
= false;
19909 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
19911 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
19912 / m_line_header
->maximum_ops_per_instruction
)
19913 * m_line_header
->minimum_instruction_length
);
19914 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19915 m_op_index
= ((m_op_index
+ adjust
)
19916 % m_line_header
->maximum_ops_per_instruction
);
19920 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
19922 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
19923 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
19924 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
19925 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
19926 / m_line_header
->maximum_ops_per_instruction
)
19927 * m_line_header
->minimum_instruction_length
);
19928 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19929 m_op_index
= ((m_op_index
+ adj_opcode_d
)
19930 % m_line_header
->maximum_ops_per_instruction
);
19932 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
19933 advance_line (line_delta
);
19934 record_line (false);
19935 m_discriminator
= 0;
19936 m_flags
&= ~LEF_PROLOGUE_END
;
19940 lnp_state_machine::handle_set_file (file_name_index file
)
19944 const file_entry
*fe
= current_file ();
19946 dwarf2_debug_line_missing_file_complaint ();
19949 const char *dir
= fe
->include_dir (m_line_header
);
19951 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
19952 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
19953 dwarf2_start_subfile (m_cu
, fe
->name
, dir
);
19958 lnp_state_machine::handle_const_add_pc ()
19961 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
19964 = (((m_op_index
+ adjust
)
19965 / m_line_header
->maximum_ops_per_instruction
)
19966 * m_line_header
->minimum_instruction_length
);
19968 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19969 m_op_index
= ((m_op_index
+ adjust
)
19970 % m_line_header
->maximum_ops_per_instruction
);
19973 /* Return non-zero if we should add LINE to the line number table.
19974 LINE is the line to add, LAST_LINE is the last line that was added,
19975 LAST_SUBFILE is the subfile for LAST_LINE.
19976 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19977 had a non-zero discriminator.
19979 We have to be careful in the presence of discriminators.
19980 E.g., for this line:
19982 for (i = 0; i < 100000; i++);
19984 clang can emit four line number entries for that one line,
19985 each with a different discriminator.
19986 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19988 However, we want gdb to coalesce all four entries into one.
19989 Otherwise the user could stepi into the middle of the line and
19990 gdb would get confused about whether the pc really was in the
19991 middle of the line.
19993 Things are further complicated by the fact that two consecutive
19994 line number entries for the same line is a heuristic used by gcc
19995 to denote the end of the prologue. So we can't just discard duplicate
19996 entries, we have to be selective about it. The heuristic we use is
19997 that we only collapse consecutive entries for the same line if at least
19998 one of those entries has a non-zero discriminator. PR 17276.
20000 Note: Addresses in the line number state machine can never go backwards
20001 within one sequence, thus this coalescing is ok. */
20004 dwarf_record_line_p (struct dwarf2_cu
*cu
,
20005 unsigned int line
, unsigned int last_line
,
20006 int line_has_non_zero_discriminator
,
20007 struct subfile
*last_subfile
)
20009 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
20011 if (line
!= last_line
)
20013 /* Same line for the same file that we've seen already.
20014 As a last check, for pr 17276, only record the line if the line
20015 has never had a non-zero discriminator. */
20016 if (!line_has_non_zero_discriminator
)
20021 /* Use the CU's builder to record line number LINE beginning at
20022 address ADDRESS in the line table of subfile SUBFILE. */
20025 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20026 unsigned int line
, CORE_ADDR address
,
20027 linetable_entry_flags flags
,
20028 struct dwarf2_cu
*cu
)
20030 CORE_ADDR addr
= gdbarch_addr_bits_remove (gdbarch
, address
);
20032 if (dwarf_line_debug
)
20034 gdb_printf (gdb_stdlog
,
20035 "Recording line %u, file %s, address %s\n",
20036 line
, lbasename (subfile
->name
.c_str ()),
20037 paddress (gdbarch
, address
));
20041 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
20044 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20045 Mark the end of a set of line number records.
20046 The arguments are the same as for dwarf_record_line_1.
20047 If SUBFILE is NULL the request is ignored. */
20050 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20051 CORE_ADDR address
, struct dwarf2_cu
*cu
)
20053 if (subfile
== NULL
)
20056 if (dwarf_line_debug
)
20058 gdb_printf (gdb_stdlog
,
20059 "Finishing current line, file %s, address %s\n",
20060 lbasename (subfile
->name
.c_str ()),
20061 paddress (gdbarch
, address
));
20064 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
20068 lnp_state_machine::record_line (bool end_sequence
)
20070 if (dwarf_line_debug
)
20072 gdb_printf (gdb_stdlog
,
20073 "Processing actual line %u: file %u,"
20074 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20076 paddress (m_gdbarch
, m_address
),
20077 (m_flags
& LEF_IS_STMT
) != 0,
20078 (m_flags
& LEF_PROLOGUE_END
) != 0,
20080 (end_sequence
? "\t(end sequence)" : ""));
20083 file_entry
*fe
= current_file ();
20086 dwarf2_debug_line_missing_file_complaint ();
20087 /* For now we ignore lines not starting on an instruction boundary.
20088 But not when processing end_sequence for compatibility with the
20089 previous version of the code. */
20090 else if (m_op_index
== 0 || end_sequence
)
20092 /* When we switch files we insert an end maker in the first file,
20093 switch to the second file and add a new line entry. The
20094 problem is that the end marker inserted in the first file will
20095 discard any previous line entries at the same address. If the
20096 line entries in the first file are marked as is-stmt, while
20097 the new line in the second file is non-stmt, then this means
20098 the end marker will discard is-stmt lines so we can have a
20099 non-stmt line. This means that there are less addresses at
20100 which the user can insert a breakpoint.
20102 To improve this we track the last address in m_last_address,
20103 and whether we have seen an is-stmt at this address. Then
20104 when switching files, if we have seen a stmt at the current
20105 address, and we are switching to create a non-stmt line, then
20106 discard the new line. */
20108 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
20109 bool ignore_this_line
20110 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
20111 && ((m_flags
& LEF_IS_STMT
) == 0)
20112 && m_stmt_at_address
)
20113 || (!end_sequence
&& m_line
== 0));
20115 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
20117 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
20118 m_currently_recording_lines
? m_cu
: nullptr);
20121 if (!end_sequence
&& !ignore_this_line
)
20123 linetable_entry_flags lte_flags
= m_flags
;
20124 if (producer_is_codewarrior (m_cu
))
20125 lte_flags
|= LEF_IS_STMT
;
20127 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
20128 m_line_has_non_zero_discriminator
,
20131 buildsym_compunit
*builder
= m_cu
->get_builder ();
20132 dwarf_record_line_1 (m_gdbarch
,
20133 builder
->get_current_subfile (),
20134 m_line
, m_address
, lte_flags
,
20135 m_currently_recording_lines
? m_cu
: nullptr);
20137 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20138 m_last_line
= m_line
;
20142 /* Track whether we have seen any IS_STMT true at m_address in case we
20143 have multiple line table entries all at m_address. */
20144 if (m_last_address
!= m_address
)
20146 m_stmt_at_address
= false;
20147 m_last_address
= m_address
;
20149 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
20152 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
20157 m_line_header
= lh
;
20159 m_currently_recording_lines
= true;
20161 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20162 was a line entry for it so that the backend has a chance to adjust it
20163 and also record it in case it needs it. This is currently used by MIPS
20164 code, cf. `mips_adjust_dwarf2_line'. */
20165 m_address
= gdbarch_adjust_dwarf2_line (arch
, 0, 0);
20167 if (lh
->default_is_stmt
)
20168 m_flags
|= LEF_IS_STMT
;
20169 m_discriminator
= 0;
20171 m_last_address
= m_address
;
20172 m_stmt_at_address
= false;
20176 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
20177 const gdb_byte
*line_ptr
,
20178 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
)
20180 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20181 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20182 located at 0x0. In this case, additionally check that if
20183 ADDRESS < UNRELOCATED_LOWPC. */
20185 if ((address
== 0 && address
< unrelocated_lowpc
)
20186 || address
== (CORE_ADDR
) -1)
20188 /* This line table is for a function which has been
20189 GCd by the linker. Ignore it. PR gdb/12528 */
20191 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20192 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
20194 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20195 line_offset
, objfile_name (objfile
));
20196 m_currently_recording_lines
= false;
20197 /* Note: m_currently_recording_lines is left as false until we see
20198 DW_LNE_end_sequence. */
20202 /* Subroutine of dwarf_decode_lines to simplify it.
20203 Process the line number information in LH. */
20206 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20209 const gdb_byte
*line_ptr
, *extended_end
;
20210 const gdb_byte
*line_end
;
20211 unsigned int bytes_read
, extended_len
;
20212 unsigned char op_code
, extended_op
;
20213 CORE_ADDR baseaddr
;
20214 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20215 bfd
*abfd
= objfile
->obfd
;
20216 struct gdbarch
*gdbarch
= objfile
->arch ();
20218 baseaddr
= objfile
->text_section_offset ();
20220 line_ptr
= lh
->statement_program_start
;
20221 line_end
= lh
->statement_program_end
;
20223 /* Read the statement sequences until there's nothing left. */
20224 while (line_ptr
< line_end
)
20226 /* The DWARF line number program state machine. Reset the state
20227 machine at the start of each sequence. */
20228 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
20229 bool end_sequence
= false;
20231 /* Start a subfile for the current file of the state
20233 const file_entry
*fe
= state_machine
.current_file ();
20236 dwarf2_start_subfile (cu
, fe
->name
, fe
->include_dir (lh
));
20238 /* Decode the table. */
20239 while (line_ptr
< line_end
&& !end_sequence
)
20241 op_code
= read_1_byte (abfd
, line_ptr
);
20244 if (op_code
>= lh
->opcode_base
)
20246 /* Special opcode. */
20247 state_machine
.handle_special_opcode (op_code
);
20249 else switch (op_code
)
20251 case DW_LNS_extended_op
:
20252 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
20254 line_ptr
+= bytes_read
;
20255 extended_end
= line_ptr
+ extended_len
;
20256 extended_op
= read_1_byte (abfd
, line_ptr
);
20258 if (DW_LNE_lo_user
<= extended_op
20259 && extended_op
<= DW_LNE_hi_user
)
20261 /* Vendor extension, ignore. */
20262 line_ptr
= extended_end
;
20265 switch (extended_op
)
20267 case DW_LNE_end_sequence
:
20268 state_machine
.handle_end_sequence ();
20269 end_sequence
= true;
20271 case DW_LNE_set_address
:
20274 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
20275 line_ptr
+= bytes_read
;
20277 state_machine
.check_line_address (cu
, line_ptr
,
20278 lowpc
- baseaddr
, address
);
20279 state_machine
.handle_set_address (baseaddr
, address
);
20282 case DW_LNE_define_file
:
20284 const char *cur_file
;
20285 unsigned int mod_time
, length
;
20288 cur_file
= read_direct_string (abfd
, line_ptr
,
20290 line_ptr
+= bytes_read
;
20291 dindex
= (dir_index
)
20292 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20293 line_ptr
+= bytes_read
;
20295 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20296 line_ptr
+= bytes_read
;
20298 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20299 line_ptr
+= bytes_read
;
20300 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
20303 case DW_LNE_set_discriminator
:
20305 /* The discriminator is not interesting to the
20306 debugger; just ignore it. We still need to
20307 check its value though:
20308 if there are consecutive entries for the same
20309 (non-prologue) line we want to coalesce them.
20312 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20313 line_ptr
+= bytes_read
;
20315 state_machine
.handle_set_discriminator (discr
);
20319 complaint (_("mangled .debug_line section"));
20322 /* Make sure that we parsed the extended op correctly. If e.g.
20323 we expected a different address size than the producer used,
20324 we may have read the wrong number of bytes. */
20325 if (line_ptr
!= extended_end
)
20327 complaint (_("mangled .debug_line section"));
20332 state_machine
.handle_copy ();
20334 case DW_LNS_advance_pc
:
20337 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20338 line_ptr
+= bytes_read
;
20340 state_machine
.handle_advance_pc (adjust
);
20343 case DW_LNS_advance_line
:
20346 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
20347 line_ptr
+= bytes_read
;
20349 state_machine
.handle_advance_line (line_delta
);
20352 case DW_LNS_set_file
:
20354 file_name_index file
20355 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
20357 line_ptr
+= bytes_read
;
20359 state_machine
.handle_set_file (file
);
20362 case DW_LNS_set_column
:
20363 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20364 line_ptr
+= bytes_read
;
20366 case DW_LNS_negate_stmt
:
20367 state_machine
.handle_negate_stmt ();
20369 case DW_LNS_set_basic_block
:
20371 /* Add to the address register of the state machine the
20372 address increment value corresponding to special opcode
20373 255. I.e., this value is scaled by the minimum
20374 instruction length since special opcode 255 would have
20375 scaled the increment. */
20376 case DW_LNS_const_add_pc
:
20377 state_machine
.handle_const_add_pc ();
20379 case DW_LNS_fixed_advance_pc
:
20381 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
20384 state_machine
.handle_fixed_advance_pc (addr_adj
);
20387 case DW_LNS_set_prologue_end
:
20388 state_machine
.handle_set_prologue_end ();
20392 /* Unknown standard opcode, ignore it. */
20395 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
20397 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20398 line_ptr
+= bytes_read
;
20405 dwarf2_debug_line_missing_end_sequence_complaint ();
20407 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20408 in which case we still finish recording the last line). */
20409 state_machine
.record_line (true);
20413 /* Decode the Line Number Program (LNP) for the given line_header
20414 structure and CU. The actual information extracted and the type
20415 of structures created from the LNP depends on the value of PST.
20417 FND holds the CU file name and directory, if known.
20418 It is used for relative paths in the line table.
20420 NOTE: It is important that psymtabs have the same file name (via
20421 strcmp) as the corresponding symtab. Since the directory is not
20422 used in the name of the symtab we don't use it in the name of the
20423 psymtabs we create. E.g. expand_line_sal requires this when
20424 finding psymtabs to expand. A good testcase for this is
20427 LOWPC is the lowest address in CU (or 0 if not known).
20429 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20430 for its PC<->lines mapping information. Otherwise only the filename
20431 table is read in. */
20434 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20435 CORE_ADDR lowpc
, int decode_mapping
)
20437 if (decode_mapping
)
20438 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
20440 /* Make sure a symtab is created for every file, even files
20441 which contain only variables (i.e. no code with associated
20443 buildsym_compunit
*builder
= cu
->get_builder ();
20444 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
20446 for (auto &fe
: lh
->file_names ())
20448 dwarf2_start_subfile (cu
, fe
.name
, fe
.include_dir (lh
));
20449 if (builder
->get_current_subfile ()->symtab
== NULL
)
20451 builder
->get_current_subfile ()->symtab
20452 = allocate_symtab (cust
,
20453 builder
->get_current_subfile ()->name
.c_str ());
20455 fe
.symtab
= builder
->get_current_subfile ()->symtab
;
20459 /* Start a subfile for DWARF. FILENAME is the name of the file and
20460 DIRNAME the name of the source directory which contains FILENAME
20461 or NULL if not known.
20462 This routine tries to keep line numbers from identical absolute and
20463 relative file names in a common subfile.
20465 Using the `list' example from the GDB testsuite, which resides in
20466 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20467 of /srcdir/list0.c yields the following debugging information for list0.c:
20469 DW_AT_name: /srcdir/list0.c
20470 DW_AT_comp_dir: /compdir
20471 files.files[0].name: list0.h
20472 files.files[0].dir: /srcdir
20473 files.files[1].name: list0.c
20474 files.files[1].dir: /srcdir
20476 The line number information for list0.c has to end up in a single
20477 subfile, so that `break /srcdir/list0.c:1' works as expected.
20478 start_subfile will ensure that this happens provided that we pass the
20479 concatenation of files.files[1].dir and files.files[1].name as the
20483 dwarf2_start_subfile (struct dwarf2_cu
*cu
, const char *filename
,
20484 const char *dirname
)
20488 /* In order not to lose the line information directory,
20489 we concatenate it to the filename when it makes sense.
20490 Note that the Dwarf3 standard says (speaking of filenames in line
20491 information): ``The directory index is ignored for file names
20492 that represent full path names''. Thus ignoring dirname in the
20493 `else' branch below isn't an issue. */
20495 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
20497 copy
= path_join (dirname
, filename
);
20498 filename
= copy
.c_str ();
20501 cu
->get_builder ()->start_subfile (filename
);
20505 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
20506 struct dwarf2_cu
*cu
)
20508 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20509 struct comp_unit_head
*cu_header
= &cu
->header
;
20511 /* NOTE drow/2003-01-30: There used to be a comment and some special
20512 code here to turn a symbol with DW_AT_external and a
20513 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20514 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20515 with some versions of binutils) where shared libraries could have
20516 relocations against symbols in their debug information - the
20517 minimal symbol would have the right address, but the debug info
20518 would not. It's no longer necessary, because we will explicitly
20519 apply relocations when we read in the debug information now. */
20521 /* A DW_AT_location attribute with no contents indicates that a
20522 variable has been optimized away. */
20523 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
20525 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20529 /* Handle one degenerate form of location expression specially, to
20530 preserve GDB's previous behavior when section offsets are
20531 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20532 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20534 if (attr
->form_is_block ())
20536 struct dwarf_block
*block
= attr
->as_block ();
20538 if ((block
->data
[0] == DW_OP_addr
20539 && block
->size
== 1 + cu_header
->addr_size
)
20540 || ((block
->data
[0] == DW_OP_GNU_addr_index
20541 || block
->data
[0] == DW_OP_addrx
)
20543 == 1 + leb128_size (&block
->data
[1]))))
20545 unsigned int dummy
;
20547 if (block
->data
[0] == DW_OP_addr
)
20548 sym
->set_value_address
20549 (cu
->header
.read_address (objfile
->obfd
, block
->data
+ 1,
20552 sym
->set_value_address
20553 (read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
));
20554 sym
->set_aclass_index (LOC_STATIC
);
20555 fixup_symbol_section (sym
, objfile
);
20556 sym
->set_value_address
20557 (sym
->value_address ()
20558 + objfile
->section_offsets
[sym
->section_index ()]);
20563 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20564 expression evaluator, and use LOC_COMPUTED only when necessary
20565 (i.e. when the value of a register or memory location is
20566 referenced, or a thread-local block, etc.). Then again, it might
20567 not be worthwhile. I'm assuming that it isn't unless performance
20568 or memory numbers show me otherwise. */
20570 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
20572 if (SYMBOL_COMPUTED_OPS (sym
)->location_has_loclist
)
20573 cu
->has_loclist
= true;
20576 /* Given a pointer to a DWARF information entry, figure out if we need
20577 to make a symbol table entry for it, and if so, create a new entry
20578 and return a pointer to it.
20579 If TYPE is NULL, determine symbol type from the die, otherwise
20580 used the passed type.
20581 If SPACE is not NULL, use it to hold the new symbol. If it is
20582 NULL, allocate a new symbol on the objfile's obstack. */
20584 static struct symbol
*
20585 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
20586 struct symbol
*space
)
20588 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20589 struct objfile
*objfile
= per_objfile
->objfile
;
20590 struct gdbarch
*gdbarch
= objfile
->arch ();
20591 struct symbol
*sym
= NULL
;
20593 struct attribute
*attr
= NULL
;
20594 struct attribute
*attr2
= NULL
;
20595 CORE_ADDR baseaddr
;
20596 struct pending
**list_to_add
= NULL
;
20598 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
20600 baseaddr
= objfile
->text_section_offset ();
20602 name
= dwarf2_name (die
, cu
);
20603 if (name
== nullptr && (die
->tag
== DW_TAG_subprogram
20604 || die
->tag
== DW_TAG_inlined_subroutine
20605 || die
->tag
== DW_TAG_entry_point
))
20606 name
= dw2_linkage_name (die
, cu
);
20610 int suppress_add
= 0;
20615 sym
= new (&objfile
->objfile_obstack
) symbol
;
20616 OBJSTAT (objfile
, n_syms
++);
20618 /* Cache this symbol's name and the name's demangled form (if any). */
20619 sym
->set_language (cu
->per_cu
->lang
, &objfile
->objfile_obstack
);
20620 /* Fortran does not have mangling standard and the mangling does differ
20621 between gfortran, iFort etc. */
20622 const char *physname
20623 = (cu
->per_cu
->lang
== language_fortran
20624 ? dwarf2_full_name (name
, die
, cu
)
20625 : dwarf2_physname (name
, die
, cu
));
20626 const char *linkagename
= dw2_linkage_name (die
, cu
);
20628 if (linkagename
== nullptr || cu
->per_cu
->lang
== language_ada
)
20629 sym
->set_linkage_name (physname
);
20632 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
20633 sym
->set_linkage_name (linkagename
);
20636 /* Handle DW_AT_artificial. */
20637 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
20638 if (attr
!= nullptr)
20639 sym
->set_is_artificial (attr
->as_boolean ());
20641 /* Default assumptions.
20642 Use the passed type or decode it from the die. */
20643 sym
->set_domain (VAR_DOMAIN
);
20644 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20646 sym
->set_type (type
);
20648 sym
->set_type (die_type (die
, cu
));
20649 attr
= dwarf2_attr (die
,
20650 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
20652 if (attr
!= nullptr)
20653 sym
->set_line (attr
->constant_value (0));
20655 attr
= dwarf2_attr (die
,
20656 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
20658 if (attr
!= nullptr && attr
->is_nonnegative ())
20660 file_name_index file_index
20661 = (file_name_index
) attr
->as_nonnegative ();
20662 struct file_entry
*fe
;
20664 if (cu
->line_header
!= NULL
)
20665 fe
= cu
->line_header
->file_name_at (file_index
);
20670 complaint (_("file index out of range"));
20672 sym
->set_symtab (fe
->symtab
);
20678 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
20679 if (attr
!= nullptr)
20683 addr
= attr
->as_address ();
20684 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ baseaddr
);
20685 sym
->set_value_address (addr
);
20686 sym
->set_aclass_index (LOC_LABEL
);
20689 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20690 sym
->set_type (objfile_type (objfile
)->builtin_core_addr
);
20691 sym
->set_domain (LABEL_DOMAIN
);
20692 add_symbol_to_list (sym
, cu
->list_in_scope
);
20694 case DW_TAG_subprogram
:
20695 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20697 sym
->set_aclass_index (LOC_BLOCK
);
20698 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20699 if ((attr2
!= nullptr && attr2
->as_boolean ())
20700 || cu
->per_cu
->lang
== language_ada
20701 || cu
->per_cu
->lang
== language_fortran
)
20703 /* Subprograms marked external are stored as a global symbol.
20704 Ada and Fortran subprograms, whether marked external or
20705 not, are always stored as a global symbol, because we want
20706 to be able to access them globally. For instance, we want
20707 to be able to break on a nested subprogram without having
20708 to specify the context. */
20709 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20713 list_to_add
= cu
->list_in_scope
;
20716 case DW_TAG_inlined_subroutine
:
20717 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20719 sym
->set_aclass_index (LOC_BLOCK
);
20720 sym
->set_is_inlined (1);
20721 list_to_add
= cu
->list_in_scope
;
20723 case DW_TAG_template_value_param
:
20725 /* Fall through. */
20726 case DW_TAG_constant
:
20727 case DW_TAG_variable
:
20728 case DW_TAG_member
:
20729 /* Compilation with minimal debug info may result in
20730 variables with missing type entries. Change the
20731 misleading `void' type to something sensible. */
20732 if (sym
->type ()->code () == TYPE_CODE_VOID
)
20733 sym
->set_type (objfile_type (objfile
)->builtin_int
);
20735 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20736 /* In the case of DW_TAG_member, we should only be called for
20737 static const members. */
20738 if (die
->tag
== DW_TAG_member
)
20740 /* dwarf2_add_field uses die_is_declaration,
20741 so we do the same. */
20742 gdb_assert (die_is_declaration (die
, cu
));
20745 if (attr
!= nullptr)
20747 dwarf2_const_value (attr
, sym
, cu
);
20748 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20751 if (attr2
!= nullptr && attr2
->as_boolean ())
20752 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20754 list_to_add
= cu
->list_in_scope
;
20758 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20759 if (attr
!= nullptr)
20761 var_decode_location (attr
, sym
, cu
);
20762 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20764 /* Fortran explicitly imports any global symbols to the local
20765 scope by DW_TAG_common_block. */
20766 if (cu
->per_cu
->lang
== language_fortran
&& die
->parent
20767 && die
->parent
->tag
== DW_TAG_common_block
)
20770 if (sym
->aclass () == LOC_STATIC
20771 && sym
->value_address () == 0
20772 && !per_objfile
->per_bfd
->has_section_at_zero
)
20774 /* When a static variable is eliminated by the linker,
20775 the corresponding debug information is not stripped
20776 out, but the variable address is set to null;
20777 do not add such variables into symbol table. */
20779 else if (attr2
!= nullptr && attr2
->as_boolean ())
20781 if (sym
->aclass () == LOC_STATIC
20782 && (objfile
->flags
& OBJF_MAINLINE
) == 0
20783 && per_objfile
->per_bfd
->can_copy
)
20785 /* A global static variable might be subject to
20786 copy relocation. We first check for a local
20787 minsym, though, because maybe the symbol was
20788 marked hidden, in which case this would not
20790 bound_minimal_symbol found
20791 = (lookup_minimal_symbol_linkage
20792 (sym
->linkage_name (), objfile
));
20793 if (found
.minsym
!= nullptr)
20794 sym
->maybe_copied
= 1;
20797 /* A variable with DW_AT_external is never static,
20798 but it may be block-scoped. */
20800 = ((cu
->list_in_scope
20801 == cu
->get_builder ()->get_file_symbols ())
20802 ? cu
->get_builder ()->get_global_symbols ()
20803 : cu
->list_in_scope
);
20806 list_to_add
= cu
->list_in_scope
;
20810 /* We do not know the address of this symbol.
20811 If it is an external symbol and we have type information
20812 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20813 The address of the variable will then be determined from
20814 the minimal symbol table whenever the variable is
20816 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20818 /* Fortran explicitly imports any global symbols to the local
20819 scope by DW_TAG_common_block. */
20820 if (cu
->per_cu
->lang
== language_fortran
&& die
->parent
20821 && die
->parent
->tag
== DW_TAG_common_block
)
20823 /* SYMBOL_CLASS doesn't matter here because
20824 read_common_block is going to reset it. */
20826 list_to_add
= cu
->list_in_scope
;
20828 else if (attr2
!= nullptr && attr2
->as_boolean ()
20829 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
20831 /* A variable with DW_AT_external is never static, but it
20832 may be block-scoped. */
20834 = ((cu
->list_in_scope
20835 == cu
->get_builder ()->get_file_symbols ())
20836 ? cu
->get_builder ()->get_global_symbols ()
20837 : cu
->list_in_scope
);
20839 sym
->set_aclass_index (LOC_UNRESOLVED
);
20841 else if (!die_is_declaration (die
, cu
))
20843 /* Use the default LOC_OPTIMIZED_OUT class. */
20844 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
20846 list_to_add
= cu
->list_in_scope
;
20850 case DW_TAG_formal_parameter
:
20852 /* If we are inside a function, mark this as an argument. If
20853 not, we might be looking at an argument to an inlined function
20854 when we do not have enough information to show inlined frames;
20855 pretend it's a local variable in that case so that the user can
20857 struct context_stack
*curr
20858 = cu
->get_builder ()->get_current_context_stack ();
20859 if (curr
!= nullptr && curr
->name
!= nullptr)
20860 sym
->set_is_argument (1);
20861 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20862 if (attr
!= nullptr)
20864 var_decode_location (attr
, sym
, cu
);
20866 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20867 if (attr
!= nullptr)
20869 dwarf2_const_value (attr
, sym
, cu
);
20872 list_to_add
= cu
->list_in_scope
;
20875 case DW_TAG_unspecified_parameters
:
20876 /* From varargs functions; gdb doesn't seem to have any
20877 interest in this information, so just ignore it for now.
20880 case DW_TAG_template_type_param
:
20882 /* Fall through. */
20883 case DW_TAG_class_type
:
20884 case DW_TAG_interface_type
:
20885 case DW_TAG_structure_type
:
20886 case DW_TAG_union_type
:
20887 case DW_TAG_set_type
:
20888 case DW_TAG_enumeration_type
:
20889 case DW_TAG_namelist
:
20890 if (die
->tag
== DW_TAG_namelist
)
20892 sym
->set_aclass_index (LOC_STATIC
);
20893 sym
->set_domain (VAR_DOMAIN
);
20897 sym
->set_aclass_index (LOC_TYPEDEF
);
20898 sym
->set_domain (STRUCT_DOMAIN
);
20901 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20902 really ever be static objects: otherwise, if you try
20903 to, say, break of a class's method and you're in a file
20904 which doesn't mention that class, it won't work unless
20905 the check for all static symbols in lookup_symbol_aux
20906 saves you. See the OtherFileClass tests in
20907 gdb.c++/namespace.exp. */
20911 buildsym_compunit
*builder
= cu
->get_builder ();
20913 = (cu
->list_in_scope
== builder
->get_file_symbols ()
20914 && cu
->per_cu
->lang
== language_cplus
20915 ? builder
->get_global_symbols ()
20916 : cu
->list_in_scope
);
20918 /* The semantics of C++ state that "struct foo {
20919 ... }" also defines a typedef for "foo". */
20920 if (cu
->per_cu
->lang
== language_cplus
20921 || cu
->per_cu
->lang
== language_ada
20922 || cu
->per_cu
->lang
== language_d
20923 || cu
->per_cu
->lang
== language_rust
)
20925 /* The symbol's name is already allocated along
20926 with this objfile, so we don't need to
20927 duplicate it for the type. */
20928 if (sym
->type ()->name () == 0)
20929 sym
->type ()->set_name (sym
->search_name ());
20934 case DW_TAG_typedef
:
20935 sym
->set_aclass_index (LOC_TYPEDEF
);
20936 sym
->set_domain (VAR_DOMAIN
);
20937 list_to_add
= cu
->list_in_scope
;
20939 case DW_TAG_array_type
:
20940 case DW_TAG_base_type
:
20941 case DW_TAG_subrange_type
:
20942 case DW_TAG_generic_subrange
:
20943 sym
->set_aclass_index (LOC_TYPEDEF
);
20944 sym
->set_domain (VAR_DOMAIN
);
20945 list_to_add
= cu
->list_in_scope
;
20947 case DW_TAG_enumerator
:
20948 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20949 if (attr
!= nullptr)
20951 dwarf2_const_value (attr
, sym
, cu
);
20954 /* NOTE: carlton/2003-11-10: See comment above in the
20955 DW_TAG_class_type, etc. block. */
20958 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
20959 && cu
->per_cu
->lang
== language_cplus
20960 ? cu
->get_builder ()->get_global_symbols ()
20961 : cu
->list_in_scope
);
20964 case DW_TAG_imported_declaration
:
20965 case DW_TAG_namespace
:
20966 sym
->set_aclass_index (LOC_TYPEDEF
);
20967 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20969 case DW_TAG_module
:
20970 sym
->set_aclass_index (LOC_TYPEDEF
);
20971 sym
->set_domain (MODULE_DOMAIN
);
20972 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20974 case DW_TAG_common_block
:
20975 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
20976 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
20977 add_symbol_to_list (sym
, cu
->list_in_scope
);
20980 /* Not a tag we recognize. Hopefully we aren't processing
20981 trash data, but since we must specifically ignore things
20982 we don't recognize, there is nothing else we should do at
20984 complaint (_("unsupported tag: '%s'"),
20985 dwarf_tag_name (die
->tag
));
20991 sym
->hash_next
= objfile
->template_symbols
;
20992 objfile
->template_symbols
= sym
;
20993 list_to_add
= NULL
;
20996 if (list_to_add
!= NULL
)
20997 add_symbol_to_list (sym
, list_to_add
);
20999 /* For the benefit of old versions of GCC, check for anonymous
21000 namespaces based on the demangled name. */
21001 if (!cu
->processing_has_namespace_info
21002 && cu
->per_cu
->lang
== language_cplus
)
21003 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
21008 /* Given an attr with a DW_FORM_dataN value in host byte order,
21009 zero-extend it as appropriate for the symbol's type. The DWARF
21010 standard (v4) is not entirely clear about the meaning of using
21011 DW_FORM_dataN for a constant with a signed type, where the type is
21012 wider than the data. The conclusion of a discussion on the DWARF
21013 list was that this is unspecified. We choose to always zero-extend
21014 because that is the interpretation long in use by GCC. */
21017 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
21018 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
21020 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21021 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
) ?
21022 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
21023 LONGEST l
= attr
->constant_value (0);
21025 if (bits
< sizeof (*value
) * 8)
21027 l
&= ((LONGEST
) 1 << bits
) - 1;
21030 else if (bits
== sizeof (*value
) * 8)
21034 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
21035 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
21042 /* Read a constant value from an attribute. Either set *VALUE, or if
21043 the value does not fit in *VALUE, set *BYTES - either already
21044 allocated on the objfile obstack, or newly allocated on OBSTACK,
21045 or, set *BATON, if we translated the constant to a location
21049 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
21050 const char *name
, struct obstack
*obstack
,
21051 struct dwarf2_cu
*cu
,
21052 LONGEST
*value
, const gdb_byte
**bytes
,
21053 struct dwarf2_locexpr_baton
**baton
)
21055 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21056 struct objfile
*objfile
= per_objfile
->objfile
;
21057 struct comp_unit_head
*cu_header
= &cu
->header
;
21058 struct dwarf_block
*blk
;
21059 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
) ?
21060 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
21066 switch (attr
->form
)
21069 case DW_FORM_addrx
:
21070 case DW_FORM_GNU_addr_index
:
21074 if (TYPE_LENGTH (type
) != cu_header
->addr_size
)
21075 dwarf2_const_value_length_mismatch_complaint (name
,
21076 cu_header
->addr_size
,
21077 TYPE_LENGTH (type
));
21078 /* Symbols of this form are reasonably rare, so we just
21079 piggyback on the existing location code rather than writing
21080 a new implementation of symbol_computed_ops. */
21081 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
21082 (*baton
)->per_objfile
= per_objfile
;
21083 (*baton
)->per_cu
= cu
->per_cu
;
21084 gdb_assert ((*baton
)->per_cu
);
21086 (*baton
)->size
= 2 + cu_header
->addr_size
;
21087 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
21088 (*baton
)->data
= data
;
21090 data
[0] = DW_OP_addr
;
21091 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
21092 byte_order
, attr
->as_address ());
21093 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
21096 case DW_FORM_string
:
21099 case DW_FORM_GNU_str_index
:
21100 case DW_FORM_GNU_strp_alt
:
21101 /* The string is already allocated on the objfile obstack, point
21103 *bytes
= (const gdb_byte
*) attr
->as_string ();
21105 case DW_FORM_block1
:
21106 case DW_FORM_block2
:
21107 case DW_FORM_block4
:
21108 case DW_FORM_block
:
21109 case DW_FORM_exprloc
:
21110 case DW_FORM_data16
:
21111 blk
= attr
->as_block ();
21112 if (TYPE_LENGTH (type
) != blk
->size
)
21113 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
21114 TYPE_LENGTH (type
));
21115 *bytes
= blk
->data
;
21118 /* The DW_AT_const_value attributes are supposed to carry the
21119 symbol's value "represented as it would be on the target
21120 architecture." By the time we get here, it's already been
21121 converted to host endianness, so we just need to sign- or
21122 zero-extend it as appropriate. */
21123 case DW_FORM_data1
:
21124 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
21126 case DW_FORM_data2
:
21127 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
21129 case DW_FORM_data4
:
21130 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
21132 case DW_FORM_data8
:
21133 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
21136 case DW_FORM_sdata
:
21137 case DW_FORM_implicit_const
:
21138 *value
= attr
->as_signed ();
21141 case DW_FORM_udata
:
21142 *value
= attr
->as_unsigned ();
21146 complaint (_("unsupported const value attribute form: '%s'"),
21147 dwarf_form_name (attr
->form
));
21154 /* Copy constant value from an attribute to a symbol. */
21157 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
21158 struct dwarf2_cu
*cu
)
21160 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21162 const gdb_byte
*bytes
;
21163 struct dwarf2_locexpr_baton
*baton
;
21165 dwarf2_const_value_attr (attr
, sym
->type (),
21166 sym
->print_name (),
21167 &objfile
->objfile_obstack
, cu
,
21168 &value
, &bytes
, &baton
);
21172 SYMBOL_LOCATION_BATON (sym
) = baton
;
21173 sym
->set_aclass_index (dwarf2_locexpr_index
);
21175 else if (bytes
!= NULL
)
21177 sym
->set_value_bytes (bytes
);
21178 sym
->set_aclass_index (LOC_CONST_BYTES
);
21182 sym
->set_value_longest (value
);
21183 sym
->set_aclass_index (LOC_CONST
);
21187 /* Return the type of the die in question using its DW_AT_type attribute. */
21189 static struct type
*
21190 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21192 struct attribute
*type_attr
;
21194 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
21197 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21198 /* A missing DW_AT_type represents a void type. */
21199 return objfile_type (objfile
)->builtin_void
;
21202 return lookup_die_type (die
, type_attr
, cu
);
21205 /* True iff CU's producer generates GNAT Ada auxiliary information
21206 that allows to find parallel types through that information instead
21207 of having to do expensive parallel lookups by type name. */
21210 need_gnat_info (struct dwarf2_cu
*cu
)
21212 /* Assume that the Ada compiler was GNAT, which always produces
21213 the auxiliary information. */
21214 return (cu
->per_cu
->lang
== language_ada
);
21217 /* Return the auxiliary type of the die in question using its
21218 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21219 attribute is not present. */
21221 static struct type
*
21222 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21224 struct attribute
*type_attr
;
21226 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
21230 return lookup_die_type (die
, type_attr
, cu
);
21233 /* If DIE has a descriptive_type attribute, then set the TYPE's
21234 descriptive type accordingly. */
21237 set_descriptive_type (struct type
*type
, struct die_info
*die
,
21238 struct dwarf2_cu
*cu
)
21240 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
21242 if (descriptive_type
)
21244 ALLOCATE_GNAT_AUX_TYPE (type
);
21245 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
21249 /* Return the containing type of the die in question using its
21250 DW_AT_containing_type attribute. */
21252 static struct type
*
21253 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21255 struct attribute
*type_attr
;
21256 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21258 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
21260 error (_("Dwarf Error: Problem turning containing type into gdb type "
21261 "[in module %s]"), objfile_name (objfile
));
21263 return lookup_die_type (die
, type_attr
, cu
);
21266 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21268 static struct type
*
21269 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
21271 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21272 struct objfile
*objfile
= per_objfile
->objfile
;
21275 std::string message
21276 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21277 objfile_name (objfile
),
21278 sect_offset_str (cu
->header
.sect_off
),
21279 sect_offset_str (die
->sect_off
));
21280 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
21282 return init_type (objfile
, TYPE_CODE_ERROR
, 0, saved
);
21285 /* Look up the type of DIE in CU using its type attribute ATTR.
21286 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21287 DW_AT_containing_type.
21288 If there is no type substitute an error marker. */
21290 static struct type
*
21291 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
21292 struct dwarf2_cu
*cu
)
21294 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21295 struct objfile
*objfile
= per_objfile
->objfile
;
21296 struct type
*this_type
;
21298 gdb_assert (attr
->name
== DW_AT_type
21299 || attr
->name
== DW_AT_GNAT_descriptive_type
21300 || attr
->name
== DW_AT_containing_type
);
21302 /* First see if we have it cached. */
21304 if (attr
->form
== DW_FORM_GNU_ref_alt
)
21306 struct dwarf2_per_cu_data
*per_cu
;
21307 sect_offset sect_off
= attr
->get_ref_die_offset ();
21309 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
21310 per_objfile
->per_bfd
);
21311 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
21313 else if (attr
->form_is_ref ())
21315 sect_offset sect_off
= attr
->get_ref_die_offset ();
21317 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
21319 else if (attr
->form
== DW_FORM_ref_sig8
)
21321 ULONGEST signature
= attr
->as_signature ();
21323 return get_signatured_type (die
, signature
, cu
);
21327 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21328 " at %s [in module %s]"),
21329 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
21330 objfile_name (objfile
));
21331 return build_error_marker_type (cu
, die
);
21334 /* If not cached we need to read it in. */
21336 if (this_type
== NULL
)
21338 struct die_info
*type_die
= NULL
;
21339 struct dwarf2_cu
*type_cu
= cu
;
21341 if (attr
->form_is_ref ())
21342 type_die
= follow_die_ref (die
, attr
, &type_cu
);
21343 if (type_die
== NULL
)
21344 return build_error_marker_type (cu
, die
);
21345 /* If we find the type now, it's probably because the type came
21346 from an inter-CU reference and the type's CU got expanded before
21348 this_type
= read_type_die (type_die
, type_cu
);
21351 /* If we still don't have a type use an error marker. */
21353 if (this_type
== NULL
)
21354 return build_error_marker_type (cu
, die
);
21359 /* Return the type in DIE, CU.
21360 Returns NULL for invalid types.
21362 This first does a lookup in die_type_hash,
21363 and only reads the die in if necessary.
21365 NOTE: This can be called when reading in partial or full symbols. */
21367 static struct type
*
21368 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
21370 struct type
*this_type
;
21372 this_type
= get_die_type (die
, cu
);
21376 return read_type_die_1 (die
, cu
);
21379 /* Read the type in DIE, CU.
21380 Returns NULL for invalid types. */
21382 static struct type
*
21383 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
21385 struct type
*this_type
= NULL
;
21389 case DW_TAG_class_type
:
21390 case DW_TAG_interface_type
:
21391 case DW_TAG_structure_type
:
21392 case DW_TAG_union_type
:
21393 this_type
= read_structure_type (die
, cu
);
21395 case DW_TAG_enumeration_type
:
21396 this_type
= read_enumeration_type (die
, cu
);
21398 case DW_TAG_subprogram
:
21399 case DW_TAG_subroutine_type
:
21400 case DW_TAG_inlined_subroutine
:
21401 this_type
= read_subroutine_type (die
, cu
);
21403 case DW_TAG_array_type
:
21404 this_type
= read_array_type (die
, cu
);
21406 case DW_TAG_set_type
:
21407 this_type
= read_set_type (die
, cu
);
21409 case DW_TAG_pointer_type
:
21410 this_type
= read_tag_pointer_type (die
, cu
);
21412 case DW_TAG_ptr_to_member_type
:
21413 this_type
= read_tag_ptr_to_member_type (die
, cu
);
21415 case DW_TAG_reference_type
:
21416 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
21418 case DW_TAG_rvalue_reference_type
:
21419 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
21421 case DW_TAG_const_type
:
21422 this_type
= read_tag_const_type (die
, cu
);
21424 case DW_TAG_volatile_type
:
21425 this_type
= read_tag_volatile_type (die
, cu
);
21427 case DW_TAG_restrict_type
:
21428 this_type
= read_tag_restrict_type (die
, cu
);
21430 case DW_TAG_string_type
:
21431 this_type
= read_tag_string_type (die
, cu
);
21433 case DW_TAG_typedef
:
21434 this_type
= read_typedef (die
, cu
);
21436 case DW_TAG_generic_subrange
:
21437 case DW_TAG_subrange_type
:
21438 this_type
= read_subrange_type (die
, cu
);
21440 case DW_TAG_base_type
:
21441 this_type
= read_base_type (die
, cu
);
21443 case DW_TAG_unspecified_type
:
21444 this_type
= read_unspecified_type (die
, cu
);
21446 case DW_TAG_namespace
:
21447 this_type
= read_namespace_type (die
, cu
);
21449 case DW_TAG_module
:
21450 this_type
= read_module_type (die
, cu
);
21452 case DW_TAG_atomic_type
:
21453 this_type
= read_tag_atomic_type (die
, cu
);
21456 complaint (_("unexpected tag in read_type_die: '%s'"),
21457 dwarf_tag_name (die
->tag
));
21464 /* See if we can figure out if the class lives in a namespace. We do
21465 this by looking for a member function; its demangled name will
21466 contain namespace info, if there is any.
21467 Return the computed name or NULL.
21468 Space for the result is allocated on the objfile's obstack.
21469 This is the full-die version of guess_partial_die_structure_name.
21470 In this case we know DIE has no useful parent. */
21472 static const char *
21473 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21475 struct die_info
*spec_die
;
21476 struct dwarf2_cu
*spec_cu
;
21477 struct die_info
*child
;
21478 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21481 spec_die
= die_specification (die
, &spec_cu
);
21482 if (spec_die
!= NULL
)
21488 for (child
= die
->child
;
21490 child
= child
->sibling
)
21492 if (child
->tag
== DW_TAG_subprogram
)
21494 const char *linkage_name
= dw2_linkage_name (child
, cu
);
21496 if (linkage_name
!= NULL
)
21498 gdb::unique_xmalloc_ptr
<char> actual_name
21499 (cu
->language_defn
->class_name_from_physname (linkage_name
));
21500 const char *name
= NULL
;
21502 if (actual_name
!= NULL
)
21504 const char *die_name
= dwarf2_name (die
, cu
);
21506 if (die_name
!= NULL
21507 && strcmp (die_name
, actual_name
.get ()) != 0)
21509 /* Strip off the class name from the full name.
21510 We want the prefix. */
21511 int die_name_len
= strlen (die_name
);
21512 int actual_name_len
= strlen (actual_name
.get ());
21513 const char *ptr
= actual_name
.get ();
21515 /* Test for '::' as a sanity check. */
21516 if (actual_name_len
> die_name_len
+ 2
21517 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
21518 name
= obstack_strndup (
21519 &objfile
->per_bfd
->storage_obstack
,
21520 ptr
, actual_name_len
- die_name_len
- 2);
21531 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21532 prefix part in such case. See
21533 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21535 static const char *
21536 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21538 struct attribute
*attr
;
21541 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
21542 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
21545 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
21548 attr
= dw2_linkage_name_attr (die
, cu
);
21549 const char *attr_name
= attr
->as_string ();
21550 if (attr
== NULL
|| attr_name
== NULL
)
21553 /* dwarf2_name had to be already called. */
21554 gdb_assert (attr
->canonical_string_p ());
21556 /* Strip the base name, keep any leading namespaces/classes. */
21557 base
= strrchr (attr_name
, ':');
21558 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
21561 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21562 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
21564 &base
[-1] - attr_name
);
21567 /* Return the name of the namespace/class that DIE is defined within,
21568 or "" if we can't tell. The caller should not xfree the result.
21570 For example, if we're within the method foo() in the following
21580 then determine_prefix on foo's die will return "N::C". */
21582 static const char *
21583 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21585 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21586 struct die_info
*parent
, *spec_die
;
21587 struct dwarf2_cu
*spec_cu
;
21588 struct type
*parent_type
;
21589 const char *retval
;
21591 if (cu
->per_cu
->lang
!= language_cplus
21592 && cu
->per_cu
->lang
!= language_fortran
21593 && cu
->per_cu
->lang
!= language_d
21594 && cu
->per_cu
->lang
!= language_rust
)
21597 retval
= anonymous_struct_prefix (die
, cu
);
21601 /* We have to be careful in the presence of DW_AT_specification.
21602 For example, with GCC 3.4, given the code
21606 // Definition of N::foo.
21610 then we'll have a tree of DIEs like this:
21612 1: DW_TAG_compile_unit
21613 2: DW_TAG_namespace // N
21614 3: DW_TAG_subprogram // declaration of N::foo
21615 4: DW_TAG_subprogram // definition of N::foo
21616 DW_AT_specification // refers to die #3
21618 Thus, when processing die #4, we have to pretend that we're in
21619 the context of its DW_AT_specification, namely the contex of die
21622 spec_die
= die_specification (die
, &spec_cu
);
21623 if (spec_die
== NULL
)
21624 parent
= die
->parent
;
21627 parent
= spec_die
->parent
;
21631 if (parent
== NULL
)
21633 else if (parent
->building_fullname
)
21636 const char *parent_name
;
21638 /* It has been seen on RealView 2.2 built binaries,
21639 DW_TAG_template_type_param types actually _defined_ as
21640 children of the parent class:
21643 template class <class Enum> Class{};
21644 Class<enum E> class_e;
21646 1: DW_TAG_class_type (Class)
21647 2: DW_TAG_enumeration_type (E)
21648 3: DW_TAG_enumerator (enum1:0)
21649 3: DW_TAG_enumerator (enum2:1)
21651 2: DW_TAG_template_type_param
21652 DW_AT_type DW_FORM_ref_udata (E)
21654 Besides being broken debug info, it can put GDB into an
21655 infinite loop. Consider:
21657 When we're building the full name for Class<E>, we'll start
21658 at Class, and go look over its template type parameters,
21659 finding E. We'll then try to build the full name of E, and
21660 reach here. We're now trying to build the full name of E,
21661 and look over the parent DIE for containing scope. In the
21662 broken case, if we followed the parent DIE of E, we'd again
21663 find Class, and once again go look at its template type
21664 arguments, etc., etc. Simply don't consider such parent die
21665 as source-level parent of this die (it can't be, the language
21666 doesn't allow it), and break the loop here. */
21667 name
= dwarf2_name (die
, cu
);
21668 parent_name
= dwarf2_name (parent
, cu
);
21669 complaint (_("template param type '%s' defined within parent '%s'"),
21670 name
? name
: "<unknown>",
21671 parent_name
? parent_name
: "<unknown>");
21675 switch (parent
->tag
)
21677 case DW_TAG_namespace
:
21678 parent_type
= read_type_die (parent
, cu
);
21679 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21680 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21681 Work around this problem here. */
21682 if (cu
->per_cu
->lang
== language_cplus
21683 && strcmp (parent_type
->name (), "::") == 0)
21685 /* We give a name to even anonymous namespaces. */
21686 return parent_type
->name ();
21687 case DW_TAG_class_type
:
21688 case DW_TAG_interface_type
:
21689 case DW_TAG_structure_type
:
21690 case DW_TAG_union_type
:
21691 case DW_TAG_module
:
21692 parent_type
= read_type_die (parent
, cu
);
21693 if (parent_type
->name () != NULL
)
21694 return parent_type
->name ();
21696 /* An anonymous structure is only allowed non-static data
21697 members; no typedefs, no member functions, et cetera.
21698 So it does not need a prefix. */
21700 case DW_TAG_compile_unit
:
21701 case DW_TAG_partial_unit
:
21702 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21703 if (cu
->per_cu
->lang
== language_cplus
21704 && !per_objfile
->per_bfd
->types
.empty ()
21705 && die
->child
!= NULL
21706 && (die
->tag
== DW_TAG_class_type
21707 || die
->tag
== DW_TAG_structure_type
21708 || die
->tag
== DW_TAG_union_type
))
21710 const char *name
= guess_full_die_structure_name (die
, cu
);
21715 case DW_TAG_subprogram
:
21716 /* Nested subroutines in Fortran get a prefix with the name
21717 of the parent's subroutine. */
21718 if (cu
->per_cu
->lang
== language_fortran
)
21720 if ((die
->tag
== DW_TAG_subprogram
)
21721 && (dwarf2_name (parent
, cu
) != NULL
))
21722 return dwarf2_name (parent
, cu
);
21725 case DW_TAG_enumeration_type
:
21726 parent_type
= read_type_die (parent
, cu
);
21727 if (parent_type
->is_declared_class ())
21729 if (parent_type
->name () != NULL
)
21730 return parent_type
->name ();
21733 /* Fall through. */
21735 return determine_prefix (parent
, cu
);
21739 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21740 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21741 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21742 an obconcat, otherwise allocate storage for the result. The CU argument is
21743 used to determine the language and hence, the appropriate separator. */
21745 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21748 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
21749 int physname
, struct dwarf2_cu
*cu
)
21751 const char *lead
= "";
21754 if (suffix
== NULL
|| suffix
[0] == '\0'
21755 || prefix
== NULL
|| prefix
[0] == '\0')
21757 else if (cu
->per_cu
->lang
== language_d
)
21759 /* For D, the 'main' function could be defined in any module, but it
21760 should never be prefixed. */
21761 if (strcmp (suffix
, "D main") == 0)
21769 else if (cu
->per_cu
->lang
== language_fortran
&& physname
)
21771 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21772 DW_AT_MIPS_linkage_name is preferred and used instead. */
21780 if (prefix
== NULL
)
21782 if (suffix
== NULL
)
21789 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
21791 strcpy (retval
, lead
);
21792 strcat (retval
, prefix
);
21793 strcat (retval
, sep
);
21794 strcat (retval
, suffix
);
21799 /* We have an obstack. */
21800 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
21804 /* Get name of a die, return NULL if not found. */
21806 static const char *
21807 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
21808 struct objfile
*objfile
)
21810 if (name
&& cu
->per_cu
->lang
== language_cplus
)
21812 gdb::unique_xmalloc_ptr
<char> canon_name
21813 = cp_canonicalize_string (name
);
21815 if (canon_name
!= nullptr)
21816 name
= objfile
->intern (canon_name
.get ());
21822 /* Get name of a die, return NULL if not found.
21823 Anonymous namespaces are converted to their magic string. */
21825 static const char *
21826 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21828 struct attribute
*attr
;
21829 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21831 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
21832 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21833 if (attr_name
== nullptr
21834 && die
->tag
!= DW_TAG_namespace
21835 && die
->tag
!= DW_TAG_class_type
21836 && die
->tag
!= DW_TAG_interface_type
21837 && die
->tag
!= DW_TAG_structure_type
21838 && die
->tag
!= DW_TAG_namelist
21839 && die
->tag
!= DW_TAG_union_type
)
21844 case DW_TAG_compile_unit
:
21845 case DW_TAG_partial_unit
:
21846 /* Compilation units have a DW_AT_name that is a filename, not
21847 a source language identifier. */
21848 case DW_TAG_enumeration_type
:
21849 case DW_TAG_enumerator
:
21850 /* These tags always have simple identifiers already; no need
21851 to canonicalize them. */
21854 case DW_TAG_namespace
:
21855 if (attr_name
!= nullptr)
21857 return CP_ANONYMOUS_NAMESPACE_STR
;
21859 case DW_TAG_class_type
:
21860 case DW_TAG_interface_type
:
21861 case DW_TAG_structure_type
:
21862 case DW_TAG_union_type
:
21863 case DW_TAG_namelist
:
21864 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21865 structures or unions. These were of the form "._%d" in GCC 4.1,
21866 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21867 and GCC 4.4. We work around this problem by ignoring these. */
21868 if (attr_name
!= nullptr
21869 && (startswith (attr_name
, "._")
21870 || startswith (attr_name
, "<anonymous")))
21873 /* GCC might emit a nameless typedef that has a linkage name. See
21874 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21875 if (!attr
|| attr_name
== NULL
)
21877 attr
= dw2_linkage_name_attr (die
, cu
);
21878 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21879 if (attr
== NULL
|| attr_name
== NULL
)
21882 /* Avoid demangling attr_name the second time on a second
21883 call for the same DIE. */
21884 if (!attr
->canonical_string_p ())
21886 gdb::unique_xmalloc_ptr
<char> demangled
21887 (gdb_demangle (attr_name
, DMGL_TYPES
));
21888 if (demangled
== nullptr)
21891 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
21892 attr_name
= attr
->as_string ();
21895 /* Strip any leading namespaces/classes, keep only the
21896 base name. DW_AT_name for named DIEs does not
21897 contain the prefixes. */
21898 const char *base
= strrchr (attr_name
, ':');
21899 if (base
&& base
> attr_name
&& base
[-1] == ':')
21910 if (!attr
->canonical_string_p ())
21911 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
21913 return attr
->as_string ();
21916 /* Return the die that this die in an extension of, or NULL if there
21917 is none. *EXT_CU is the CU containing DIE on input, and the CU
21918 containing the return value on output. */
21920 static struct die_info
*
21921 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
21923 struct attribute
*attr
;
21925 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
21929 return follow_die_ref (die
, attr
, ext_cu
);
21933 dump_die_shallow (struct ui_file
*f
, int indent
, struct die_info
*die
)
21937 gdb_printf (f
, "%*sDie: %s (abbrev %d, offset %s)\n",
21939 dwarf_tag_name (die
->tag
), die
->abbrev
,
21940 sect_offset_str (die
->sect_off
));
21942 if (die
->parent
!= NULL
)
21943 gdb_printf (f
, "%*s parent at offset: %s\n",
21945 sect_offset_str (die
->parent
->sect_off
));
21947 gdb_printf (f
, "%*s has children: %s\n",
21949 dwarf_bool_name (die
->child
!= NULL
));
21951 gdb_printf (f
, "%*s attributes:\n", indent
, "");
21953 for (i
= 0; i
< die
->num_attrs
; ++i
)
21955 gdb_printf (f
, "%*s %s (%s) ",
21957 dwarf_attr_name (die
->attrs
[i
].name
),
21958 dwarf_form_name (die
->attrs
[i
].form
));
21960 switch (die
->attrs
[i
].form
)
21963 case DW_FORM_addrx
:
21964 case DW_FORM_GNU_addr_index
:
21965 gdb_printf (f
, "address: ");
21966 gdb_puts (hex_string (die
->attrs
[i
].as_address ()), f
);
21968 case DW_FORM_block2
:
21969 case DW_FORM_block4
:
21970 case DW_FORM_block
:
21971 case DW_FORM_block1
:
21972 gdb_printf (f
, "block: size %s",
21973 pulongest (die
->attrs
[i
].as_block ()->size
));
21975 case DW_FORM_exprloc
:
21976 gdb_printf (f
, "expression: size %s",
21977 pulongest (die
->attrs
[i
].as_block ()->size
));
21979 case DW_FORM_data16
:
21980 gdb_printf (f
, "constant of 16 bytes");
21982 case DW_FORM_ref_addr
:
21983 gdb_printf (f
, "ref address: ");
21984 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
21986 case DW_FORM_GNU_ref_alt
:
21987 gdb_printf (f
, "alt ref address: ");
21988 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
21994 case DW_FORM_ref_udata
:
21995 gdb_printf (f
, "constant ref: 0x%lx (adjusted)",
21996 (long) (die
->attrs
[i
].as_unsigned ()));
21998 case DW_FORM_data1
:
21999 case DW_FORM_data2
:
22000 case DW_FORM_data4
:
22001 case DW_FORM_data8
:
22002 case DW_FORM_udata
:
22003 gdb_printf (f
, "constant: %s",
22004 pulongest (die
->attrs
[i
].as_unsigned ()));
22006 case DW_FORM_sec_offset
:
22007 gdb_printf (f
, "section offset: %s",
22008 pulongest (die
->attrs
[i
].as_unsigned ()));
22010 case DW_FORM_ref_sig8
:
22011 gdb_printf (f
, "signature: %s",
22012 hex_string (die
->attrs
[i
].as_signature ()));
22014 case DW_FORM_string
:
22016 case DW_FORM_line_strp
:
22018 case DW_FORM_GNU_str_index
:
22019 case DW_FORM_GNU_strp_alt
:
22020 gdb_printf (f
, "string: \"%s\" (%s canonicalized)",
22021 die
->attrs
[i
].as_string ()
22022 ? die
->attrs
[i
].as_string () : "",
22023 die
->attrs
[i
].canonical_string_p () ? "is" : "not");
22026 if (die
->attrs
[i
].as_boolean ())
22027 gdb_printf (f
, "flag: TRUE");
22029 gdb_printf (f
, "flag: FALSE");
22031 case DW_FORM_flag_present
:
22032 gdb_printf (f
, "flag: TRUE");
22034 case DW_FORM_indirect
:
22035 /* The reader will have reduced the indirect form to
22036 the "base form" so this form should not occur. */
22038 "unexpected attribute form: DW_FORM_indirect");
22040 case DW_FORM_sdata
:
22041 case DW_FORM_implicit_const
:
22042 gdb_printf (f
, "constant: %s",
22043 plongest (die
->attrs
[i
].as_signed ()));
22046 gdb_printf (f
, "unsupported attribute form: %d.",
22047 die
->attrs
[i
].form
);
22050 gdb_printf (f
, "\n");
22055 dump_die_for_error (struct die_info
*die
)
22057 dump_die_shallow (gdb_stderr
, 0, die
);
22061 dump_die_1 (struct ui_file
*f
, int level
, int max_level
, struct die_info
*die
)
22063 int indent
= level
* 4;
22065 gdb_assert (die
!= NULL
);
22067 if (level
>= max_level
)
22070 dump_die_shallow (f
, indent
, die
);
22072 if (die
->child
!= NULL
)
22074 gdb_printf (f
, "%*s Children:", indent
, "");
22075 if (level
+ 1 < max_level
)
22077 gdb_printf (f
, "\n");
22078 dump_die_1 (f
, level
+ 1, max_level
, die
->child
);
22083 " [not printed, max nesting level reached]\n");
22087 if (die
->sibling
!= NULL
&& level
> 0)
22089 dump_die_1 (f
, level
, max_level
, die
->sibling
);
22093 /* This is called from the pdie macro in gdbinit.in.
22094 It's not static so gcc will keep a copy callable from gdb. */
22097 dump_die (struct die_info
*die
, int max_level
)
22099 dump_die_1 (gdb_stdlog
, 0, max_level
, die
);
22103 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
22107 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
22108 to_underlying (die
->sect_off
),
22114 /* Follow reference or signature attribute ATTR of SRC_DIE.
22115 On entry *REF_CU is the CU of SRC_DIE.
22116 On exit *REF_CU is the CU of the result. */
22118 static struct die_info
*
22119 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22120 struct dwarf2_cu
**ref_cu
)
22122 struct die_info
*die
;
22124 if (attr
->form_is_ref ())
22125 die
= follow_die_ref (src_die
, attr
, ref_cu
);
22126 else if (attr
->form
== DW_FORM_ref_sig8
)
22127 die
= follow_die_sig (src_die
, attr
, ref_cu
);
22130 dump_die_for_error (src_die
);
22131 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22132 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22138 /* Follow reference OFFSET.
22139 On entry *REF_CU is the CU of the source die referencing OFFSET.
22140 On exit *REF_CU is the CU of the result.
22141 Returns NULL if OFFSET is invalid. */
22143 static struct die_info
*
22144 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
22145 struct dwarf2_cu
**ref_cu
)
22147 struct die_info temp_die
;
22148 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
22149 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22151 gdb_assert (cu
->per_cu
!= NULL
);
22155 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22156 "source CU contains target offset: %d",
22157 sect_offset_str (cu
->per_cu
->sect_off
),
22158 sect_offset_str (sect_off
),
22159 cu
->header
.offset_in_cu_p (sect_off
));
22161 if (cu
->per_cu
->is_debug_types
)
22163 /* .debug_types CUs cannot reference anything outside their CU.
22164 If they need to, they have to reference a signatured type via
22165 DW_FORM_ref_sig8. */
22166 if (!cu
->header
.offset_in_cu_p (sect_off
))
22169 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
22170 || !cu
->header
.offset_in_cu_p (sect_off
))
22172 struct dwarf2_per_cu_data
*per_cu
;
22174 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
22175 per_objfile
->per_bfd
);
22177 dwarf_read_debug_printf_v ("target CU offset: %s, "
22178 "target CU DIEs loaded: %d",
22179 sect_offset_str (per_cu
->sect_off
),
22180 per_objfile
->get_cu (per_cu
) != nullptr);
22182 /* If necessary, add it to the queue and load its DIEs.
22184 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22185 it doesn't mean they are currently loaded. Since we require them
22186 to be loaded, we must check for ourselves. */
22187 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->per_cu
->lang
)
22188 || per_objfile
->get_cu (per_cu
) == nullptr)
22189 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
22190 false, cu
->per_cu
->lang
);
22192 target_cu
= per_objfile
->get_cu (per_cu
);
22193 gdb_assert (target_cu
!= nullptr);
22195 else if (cu
->dies
== NULL
)
22197 /* We're loading full DIEs during partial symbol reading. */
22198 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
22202 *ref_cu
= target_cu
;
22203 temp_die
.sect_off
= sect_off
;
22205 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
22207 to_underlying (sect_off
));
22210 /* Follow reference attribute ATTR of SRC_DIE.
22211 On entry *REF_CU is the CU of SRC_DIE.
22212 On exit *REF_CU is the CU of the result. */
22214 static struct die_info
*
22215 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
22216 struct dwarf2_cu
**ref_cu
)
22218 sect_offset sect_off
= attr
->get_ref_die_offset ();
22219 struct dwarf2_cu
*cu
= *ref_cu
;
22220 struct die_info
*die
;
22222 die
= follow_die_offset (sect_off
,
22223 (attr
->form
== DW_FORM_GNU_ref_alt
22224 || cu
->per_cu
->is_dwz
),
22227 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22228 "at %s [in module %s]"),
22229 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
22230 objfile_name (cu
->per_objfile
->objfile
));
22237 struct dwarf2_locexpr_baton
22238 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
22239 dwarf2_per_cu_data
*per_cu
,
22240 dwarf2_per_objfile
*per_objfile
,
22241 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
22242 bool resolve_abstract_p
)
22244 struct die_info
*die
;
22245 struct attribute
*attr
;
22246 struct dwarf2_locexpr_baton retval
;
22247 struct objfile
*objfile
= per_objfile
->objfile
;
22249 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22251 cu
= load_cu (per_cu
, per_objfile
, false);
22255 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22256 Instead just throw an error, not much else we can do. */
22257 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22258 sect_offset_str (sect_off
), objfile_name (objfile
));
22261 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22263 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22264 sect_offset_str (sect_off
), objfile_name (objfile
));
22266 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22267 if (!attr
&& resolve_abstract_p
22268 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
22269 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
22271 CORE_ADDR pc
= get_frame_pc ();
22272 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
22273 struct gdbarch
*gdbarch
= objfile
->arch ();
22275 for (const auto &cand_off
22276 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
22278 struct dwarf2_cu
*cand_cu
= cu
;
22279 struct die_info
*cand
22280 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
22283 || cand
->parent
->tag
!= DW_TAG_subprogram
)
22286 CORE_ADDR pc_low
, pc_high
;
22287 get_scope_pc_bounds (cand
->parent
, &pc_low
, &pc_high
, cu
);
22288 if (pc_low
== ((CORE_ADDR
) -1))
22290 pc_low
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_low
+ baseaddr
);
22291 pc_high
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_high
+ baseaddr
);
22292 if (!(pc_low
<= pc
&& pc
< pc_high
))
22296 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22303 /* DWARF: "If there is no such attribute, then there is no effect.".
22304 DATA is ignored if SIZE is 0. */
22306 retval
.data
= NULL
;
22309 else if (attr
->form_is_section_offset ())
22311 struct dwarf2_loclist_baton loclist_baton
;
22312 CORE_ADDR pc
= get_frame_pc ();
22315 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
22317 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
22319 retval
.size
= size
;
22323 if (!attr
->form_is_block ())
22324 error (_("Dwarf Error: DIE at %s referenced in module %s "
22325 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22326 sect_offset_str (sect_off
), objfile_name (objfile
));
22328 struct dwarf_block
*block
= attr
->as_block ();
22329 retval
.data
= block
->data
;
22330 retval
.size
= block
->size
;
22332 retval
.per_objfile
= per_objfile
;
22333 retval
.per_cu
= cu
->per_cu
;
22335 per_objfile
->age_comp_units ();
22342 struct dwarf2_locexpr_baton
22343 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
22344 dwarf2_per_cu_data
*per_cu
,
22345 dwarf2_per_objfile
*per_objfile
,
22346 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
22348 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
22350 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
22354 /* Write a constant of a given type as target-ordered bytes into
22357 static const gdb_byte
*
22358 write_constant_as_bytes (struct obstack
*obstack
,
22359 enum bfd_endian byte_order
,
22366 *len
= TYPE_LENGTH (type
);
22367 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22368 store_unsigned_integer (result
, *len
, byte_order
, value
);
22376 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
22377 dwarf2_per_cu_data
*per_cu
,
22378 dwarf2_per_objfile
*per_objfile
,
22382 struct die_info
*die
;
22383 struct attribute
*attr
;
22384 const gdb_byte
*result
= NULL
;
22387 enum bfd_endian byte_order
;
22388 struct objfile
*objfile
= per_objfile
->objfile
;
22390 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22392 cu
= load_cu (per_cu
, per_objfile
, false);
22396 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22397 Instead just throw an error, not much else we can do. */
22398 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22399 sect_offset_str (sect_off
), objfile_name (objfile
));
22402 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22404 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22405 sect_offset_str (sect_off
), objfile_name (objfile
));
22407 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
22411 byte_order
= (bfd_big_endian (objfile
->obfd
)
22412 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
22414 switch (attr
->form
)
22417 case DW_FORM_addrx
:
22418 case DW_FORM_GNU_addr_index
:
22422 *len
= cu
->header
.addr_size
;
22423 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22424 store_unsigned_integer (tem
, *len
, byte_order
, attr
->as_address ());
22428 case DW_FORM_string
:
22431 case DW_FORM_GNU_str_index
:
22432 case DW_FORM_GNU_strp_alt
:
22433 /* The string is already allocated on the objfile obstack, point
22436 const char *attr_name
= attr
->as_string ();
22437 result
= (const gdb_byte
*) attr_name
;
22438 *len
= strlen (attr_name
);
22441 case DW_FORM_block1
:
22442 case DW_FORM_block2
:
22443 case DW_FORM_block4
:
22444 case DW_FORM_block
:
22445 case DW_FORM_exprloc
:
22446 case DW_FORM_data16
:
22448 struct dwarf_block
*block
= attr
->as_block ();
22449 result
= block
->data
;
22450 *len
= block
->size
;
22454 /* The DW_AT_const_value attributes are supposed to carry the
22455 symbol's value "represented as it would be on the target
22456 architecture." By the time we get here, it's already been
22457 converted to host endianness, so we just need to sign- or
22458 zero-extend it as appropriate. */
22459 case DW_FORM_data1
:
22460 type
= die_type (die
, cu
);
22461 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
22462 if (result
== NULL
)
22463 result
= write_constant_as_bytes (obstack
, byte_order
,
22466 case DW_FORM_data2
:
22467 type
= die_type (die
, cu
);
22468 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
22469 if (result
== NULL
)
22470 result
= write_constant_as_bytes (obstack
, byte_order
,
22473 case DW_FORM_data4
:
22474 type
= die_type (die
, cu
);
22475 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
22476 if (result
== NULL
)
22477 result
= write_constant_as_bytes (obstack
, byte_order
,
22480 case DW_FORM_data8
:
22481 type
= die_type (die
, cu
);
22482 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
22483 if (result
== NULL
)
22484 result
= write_constant_as_bytes (obstack
, byte_order
,
22488 case DW_FORM_sdata
:
22489 case DW_FORM_implicit_const
:
22490 type
= die_type (die
, cu
);
22491 result
= write_constant_as_bytes (obstack
, byte_order
,
22492 type
, attr
->as_signed (), len
);
22495 case DW_FORM_udata
:
22496 type
= die_type (die
, cu
);
22497 result
= write_constant_as_bytes (obstack
, byte_order
,
22498 type
, attr
->as_unsigned (), len
);
22502 complaint (_("unsupported const value attribute form: '%s'"),
22503 dwarf_form_name (attr
->form
));
22513 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
22514 dwarf2_per_cu_data
*per_cu
,
22515 dwarf2_per_objfile
*per_objfile
,
22516 const char **var_name
)
22518 struct die_info
*die
;
22520 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22522 cu
= load_cu (per_cu
, per_objfile
, false);
22527 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22531 if (var_name
!= nullptr)
22532 *var_name
= var_decl_name (die
, cu
);
22533 return die_type (die
, cu
);
22539 dwarf2_get_die_type (cu_offset die_offset
,
22540 dwarf2_per_cu_data
*per_cu
,
22541 dwarf2_per_objfile
*per_objfile
)
22543 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
22544 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
22547 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22548 On entry *REF_CU is the CU of SRC_DIE.
22549 On exit *REF_CU is the CU of the result.
22550 Returns NULL if the referenced DIE isn't found. */
22552 static struct die_info
*
22553 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
22554 struct dwarf2_cu
**ref_cu
)
22556 struct die_info temp_die
;
22557 struct dwarf2_cu
*sig_cu
;
22558 struct die_info
*die
;
22559 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
22562 /* While it might be nice to assert sig_type->type == NULL here,
22563 we can get here for DW_AT_imported_declaration where we need
22564 the DIE not the type. */
22566 /* If necessary, add it to the queue and load its DIEs.
22568 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22569 it doesn't mean they are currently loaded. Since we require them
22570 to be loaded, we must check for ourselves. */
22571 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
22573 || per_objfile
->get_cu (sig_type
) == nullptr)
22574 read_signatured_type (sig_type
, per_objfile
);
22576 sig_cu
= per_objfile
->get_cu (sig_type
);
22577 gdb_assert (sig_cu
!= NULL
);
22578 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
22579 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
22580 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
22581 to_underlying (temp_die
.sect_off
));
22584 /* For .gdb_index version 7 keep track of included TUs.
22585 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22586 if (per_objfile
->per_bfd
->index_table
!= NULL
22587 && !per_objfile
->per_bfd
->index_table
->version_check ())
22589 (*ref_cu
)->per_cu
->imported_symtabs_push (sig_cu
->per_cu
);
22599 /* Follow signatured type referenced by ATTR in SRC_DIE.
22600 On entry *REF_CU is the CU of SRC_DIE.
22601 On exit *REF_CU is the CU of the result.
22602 The result is the DIE of the type.
22603 If the referenced type cannot be found an error is thrown. */
22605 static struct die_info
*
22606 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22607 struct dwarf2_cu
**ref_cu
)
22609 ULONGEST signature
= attr
->as_signature ();
22610 struct signatured_type
*sig_type
;
22611 struct die_info
*die
;
22613 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
22615 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
22616 /* sig_type will be NULL if the signatured type is missing from
22618 if (sig_type
== NULL
)
22620 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22621 " from DIE at %s [in module %s]"),
22622 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22623 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22626 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
22629 dump_die_for_error (src_die
);
22630 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22631 " from DIE at %s [in module %s]"),
22632 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22633 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22639 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22640 reading in and processing the type unit if necessary. */
22642 static struct type
*
22643 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
22644 struct dwarf2_cu
*cu
)
22646 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22647 struct signatured_type
*sig_type
;
22648 struct dwarf2_cu
*type_cu
;
22649 struct die_info
*type_die
;
22652 sig_type
= lookup_signatured_type (cu
, signature
);
22653 /* sig_type will be NULL if the signatured type is missing from
22655 if (sig_type
== NULL
)
22657 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22658 " from DIE at %s [in module %s]"),
22659 hex_string (signature
), sect_offset_str (die
->sect_off
),
22660 objfile_name (per_objfile
->objfile
));
22661 return build_error_marker_type (cu
, die
);
22664 /* If we already know the type we're done. */
22665 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
22666 if (type
!= nullptr)
22670 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
22671 if (type_die
!= NULL
)
22673 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22674 is created. This is important, for example, because for c++ classes
22675 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22676 type
= read_type_die (type_die
, type_cu
);
22679 complaint (_("Dwarf Error: Cannot build signatured type %s"
22680 " referenced from DIE at %s [in module %s]"),
22681 hex_string (signature
), sect_offset_str (die
->sect_off
),
22682 objfile_name (per_objfile
->objfile
));
22683 type
= build_error_marker_type (cu
, die
);
22688 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22689 " from DIE at %s [in module %s]"),
22690 hex_string (signature
), sect_offset_str (die
->sect_off
),
22691 objfile_name (per_objfile
->objfile
));
22692 type
= build_error_marker_type (cu
, die
);
22695 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
22700 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22701 reading in and processing the type unit if necessary. */
22703 static struct type
*
22704 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
22705 struct dwarf2_cu
*cu
) /* ARI: editCase function */
22707 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22708 if (attr
->form_is_ref ())
22710 struct dwarf2_cu
*type_cu
= cu
;
22711 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
22713 return read_type_die (type_die
, type_cu
);
22715 else if (attr
->form
== DW_FORM_ref_sig8
)
22717 return get_signatured_type (die
, attr
->as_signature (), cu
);
22721 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22723 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22724 " at %s [in module %s]"),
22725 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
22726 objfile_name (per_objfile
->objfile
));
22727 return build_error_marker_type (cu
, die
);
22731 /* Load the DIEs associated with type unit PER_CU into memory. */
22734 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
22735 dwarf2_per_objfile
*per_objfile
)
22737 struct signatured_type
*sig_type
;
22739 /* We have the per_cu, but we need the signatured_type.
22740 Fortunately this is an easy translation. */
22741 gdb_assert (per_cu
->is_debug_types
);
22742 sig_type
= (struct signatured_type
*) per_cu
;
22744 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
22746 read_signatured_type (sig_type
, per_objfile
);
22748 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
22751 /* Read in a signatured type and build its CU and DIEs.
22752 If the type is a stub for the real type in a DWO file,
22753 read in the real type from the DWO file as well. */
22756 read_signatured_type (signatured_type
*sig_type
,
22757 dwarf2_per_objfile
*per_objfile
)
22759 gdb_assert (sig_type
->is_debug_types
);
22760 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
22762 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
22764 if (!reader
.dummy_p
)
22766 struct dwarf2_cu
*cu
= reader
.cu
;
22767 const gdb_byte
*info_ptr
= reader
.info_ptr
;
22769 gdb_assert (cu
->die_hash
== NULL
);
22771 htab_create_alloc_ex (cu
->header
.length
/ 12,
22775 &cu
->comp_unit_obstack
,
22776 hashtab_obstack_allocate
,
22777 dummy_obstack_deallocate
);
22779 if (reader
.comp_unit_die
->has_children
)
22780 reader
.comp_unit_die
->child
22781 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
22782 reader
.comp_unit_die
);
22783 cu
->dies
= reader
.comp_unit_die
;
22784 /* comp_unit_die is not stored in die_hash, no need. */
22786 /* We try not to read any attributes in this function, because
22787 not all CUs needed for references have been loaded yet, and
22788 symbol table processing isn't initialized. But we have to
22789 set the CU language, or we won't be able to build types
22790 correctly. Similarly, if we do not read the producer, we can
22791 not apply producer-specific interpretation. */
22792 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
22797 sig_type
->tu_read
= 1;
22800 /* Decode simple location descriptions.
22801 Given a pointer to a dwarf block that defines a location, compute
22802 the location and return the value. If COMPUTED is non-null, it is
22803 set to true to indicate that decoding was successful, and false
22804 otherwise. If COMPUTED is null, then this function may emit a
22808 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
, bool *computed
)
22810 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
22812 size_t size
= blk
->size
;
22813 const gdb_byte
*data
= blk
->data
;
22814 CORE_ADDR stack
[64];
22816 unsigned int bytes_read
, unsnd
;
22819 if (computed
!= nullptr)
22825 stack
[++stacki
] = 0;
22864 stack
[++stacki
] = op
- DW_OP_lit0
;
22899 stack
[++stacki
] = op
- DW_OP_reg0
;
22902 if (computed
== nullptr)
22903 dwarf2_complex_location_expr_complaint ();
22910 unsnd
= read_unsigned_leb128 (NULL
, (data
+ i
), &bytes_read
);
22912 stack
[++stacki
] = unsnd
;
22915 if (computed
== nullptr)
22916 dwarf2_complex_location_expr_complaint ();
22923 stack
[++stacki
] = cu
->header
.read_address (objfile
->obfd
, &data
[i
],
22928 case DW_OP_const1u
:
22929 stack
[++stacki
] = read_1_byte (objfile
->obfd
, &data
[i
]);
22933 case DW_OP_const1s
:
22934 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
, &data
[i
]);
22938 case DW_OP_const2u
:
22939 stack
[++stacki
] = read_2_bytes (objfile
->obfd
, &data
[i
]);
22943 case DW_OP_const2s
:
22944 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
, &data
[i
]);
22948 case DW_OP_const4u
:
22949 stack
[++stacki
] = read_4_bytes (objfile
->obfd
, &data
[i
]);
22953 case DW_OP_const4s
:
22954 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
, &data
[i
]);
22958 case DW_OP_const8u
:
22959 stack
[++stacki
] = read_8_bytes (objfile
->obfd
, &data
[i
]);
22964 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
22970 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
22975 stack
[stacki
+ 1] = stack
[stacki
];
22980 stack
[stacki
- 1] += stack
[stacki
];
22984 case DW_OP_plus_uconst
:
22985 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
22991 stack
[stacki
- 1] -= stack
[stacki
];
22996 /* If we're not the last op, then we definitely can't encode
22997 this using GDB's address_class enum. This is valid for partial
22998 global symbols, although the variable's address will be bogus
23002 if (computed
== nullptr)
23003 dwarf2_complex_location_expr_complaint ();
23009 case DW_OP_GNU_push_tls_address
:
23010 case DW_OP_form_tls_address
:
23011 /* The top of the stack has the offset from the beginning
23012 of the thread control block at which the variable is located. */
23013 /* Nothing should follow this operator, so the top of stack would
23015 /* This is valid for partial global symbols, but the variable's
23016 address will be bogus in the psymtab. Make it always at least
23017 non-zero to not look as a variable garbage collected by linker
23018 which have DW_OP_addr 0. */
23021 if (computed
== nullptr)
23022 dwarf2_complex_location_expr_complaint ();
23029 case DW_OP_GNU_uninit
:
23030 if (computed
!= nullptr)
23035 case DW_OP_GNU_addr_index
:
23036 case DW_OP_GNU_const_index
:
23037 stack
[++stacki
] = read_addr_index_from_leb128 (cu
, &data
[i
],
23043 if (computed
== nullptr)
23045 const char *name
= get_DW_OP_name (op
);
23048 complaint (_("unsupported stack op: '%s'"),
23051 complaint (_("unsupported stack op: '%02x'"),
23055 return (stack
[stacki
]);
23058 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23059 outside of the allocated space. Also enforce minimum>0. */
23060 if (stacki
>= ARRAY_SIZE (stack
) - 1)
23062 if (computed
== nullptr)
23063 complaint (_("location description stack overflow"));
23069 if (computed
== nullptr)
23070 complaint (_("location description stack underflow"));
23075 if (computed
!= nullptr)
23077 return (stack
[stacki
]);
23080 /* memory allocation interface */
23082 static struct dwarf_block
*
23083 dwarf_alloc_block (struct dwarf2_cu
*cu
)
23085 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
23088 static struct die_info
*
23089 dwarf_alloc_die (struct dwarf2_cu
*cu
, int num_attrs
)
23091 struct die_info
*die
;
23092 size_t size
= sizeof (struct die_info
);
23095 size
+= (num_attrs
- 1) * sizeof (struct attribute
);
23097 die
= (struct die_info
*) obstack_alloc (&cu
->comp_unit_obstack
, size
);
23098 memset (die
, 0, sizeof (struct die_info
));
23104 /* Macro support. */
23106 /* An overload of dwarf_decode_macros that finds the correct section
23107 and ensures it is read in before calling the other overload. */
23110 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
23111 int section_is_gnu
)
23113 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23114 struct objfile
*objfile
= per_objfile
->objfile
;
23115 const struct line_header
*lh
= cu
->line_header
;
23116 unsigned int offset_size
= cu
->header
.offset_size
;
23117 struct dwarf2_section_info
*section
;
23118 const char *section_name
;
23120 if (cu
->dwo_unit
!= nullptr)
23122 if (section_is_gnu
)
23124 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
23125 section_name
= ".debug_macro.dwo";
23129 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
23130 section_name
= ".debug_macinfo.dwo";
23135 if (section_is_gnu
)
23137 section
= &per_objfile
->per_bfd
->macro
;
23138 section_name
= ".debug_macro";
23142 section
= &per_objfile
->per_bfd
->macinfo
;
23143 section_name
= ".debug_macinfo";
23147 section
->read (objfile
);
23148 if (section
->buffer
== nullptr)
23150 complaint (_("missing %s section"), section_name
);
23154 buildsym_compunit
*builder
= cu
->get_builder ();
23156 struct dwarf2_section_info
*str_offsets_section
;
23157 struct dwarf2_section_info
*str_section
;
23158 gdb::optional
<ULONGEST
> str_offsets_base
;
23160 if (cu
->dwo_unit
!= nullptr)
23162 str_offsets_section
= &cu
->dwo_unit
->dwo_file
23163 ->sections
.str_offsets
;
23164 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
23165 str_offsets_base
= cu
->header
.addr_size
;
23169 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
23170 str_section
= &per_objfile
->per_bfd
->str
;
23171 str_offsets_base
= cu
->str_offsets_base
;
23174 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
23175 offset_size
, offset
, str_section
, str_offsets_section
,
23176 str_offsets_base
, section_is_gnu
);
23179 /* Return the .debug_loc section to use for CU.
23180 For DWO files use .debug_loc.dwo. */
23182 static struct dwarf2_section_info
*
23183 cu_debug_loc_section (struct dwarf2_cu
*cu
)
23185 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23189 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23191 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
23193 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
23194 : &per_objfile
->per_bfd
->loc
);
23197 /* Return the .debug_rnglists section to use for CU. */
23198 static struct dwarf2_section_info
*
23199 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
23201 if (cu
->header
.version
< 5)
23202 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23203 cu
->header
.version
);
23204 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
23206 /* Make sure we read the .debug_rnglists section from the file that
23207 contains the DW_AT_ranges attribute we are reading. Normally that
23208 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23209 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23211 if (cu
->dwo_unit
!= nullptr
23212 && tag
!= DW_TAG_compile_unit
23213 && tag
!= DW_TAG_skeleton_unit
)
23215 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23217 if (sections
->rnglists
.size
> 0)
23218 return §ions
->rnglists
;
23220 error (_(".debug_rnglists section is missing from .dwo file."));
23222 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
23225 /* A helper function that fills in a dwarf2_loclist_baton. */
23228 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
23229 struct dwarf2_loclist_baton
*baton
,
23230 const struct attribute
*attr
)
23232 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23233 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23235 section
->read (per_objfile
->objfile
);
23237 baton
->per_objfile
= per_objfile
;
23238 baton
->per_cu
= cu
->per_cu
;
23239 gdb_assert (baton
->per_cu
);
23240 /* We don't know how long the location list is, but make sure we
23241 don't run off the edge of the section. */
23242 baton
->size
= section
->size
- attr
->as_unsigned ();
23243 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
23244 if (cu
->base_address
.has_value ())
23245 baton
->base_address
= *cu
->base_address
;
23247 baton
->base_address
= 0;
23248 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
23252 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
23253 struct dwarf2_cu
*cu
, int is_block
)
23255 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23256 struct objfile
*objfile
= per_objfile
->objfile
;
23257 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23259 if (attr
->form_is_section_offset ()
23260 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23261 the section. If so, fall through to the complaint in the
23263 && attr
->as_unsigned () < section
->get_size (objfile
))
23265 struct dwarf2_loclist_baton
*baton
;
23267 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
23269 fill_in_loclist_baton (cu
, baton
, attr
);
23271 if (!cu
->base_address
.has_value ())
23272 complaint (_("Location list used without "
23273 "specifying the CU base address."));
23275 sym
->set_aclass_index ((is_block
23276 ? dwarf2_loclist_block_index
23277 : dwarf2_loclist_index
));
23278 SYMBOL_LOCATION_BATON (sym
) = baton
;
23282 struct dwarf2_locexpr_baton
*baton
;
23284 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
23285 baton
->per_objfile
= per_objfile
;
23286 baton
->per_cu
= cu
->per_cu
;
23287 gdb_assert (baton
->per_cu
);
23289 if (attr
->form_is_block ())
23291 /* Note that we're just copying the block's data pointer
23292 here, not the actual data. We're still pointing into the
23293 info_buffer for SYM's objfile; right now we never release
23294 that buffer, but when we do clean up properly this may
23296 struct dwarf_block
*block
= attr
->as_block ();
23297 baton
->size
= block
->size
;
23298 baton
->data
= block
->data
;
23302 dwarf2_invalid_attrib_class_complaint ("location description",
23303 sym
->natural_name ());
23307 sym
->set_aclass_index ((is_block
23308 ? dwarf2_locexpr_block_index
23309 : dwarf2_locexpr_index
));
23310 SYMBOL_LOCATION_BATON (sym
) = baton
;
23316 const comp_unit_head
*
23317 dwarf2_per_cu_data::get_header () const
23319 if (!m_header_read_in
)
23321 const gdb_byte
*info_ptr
23322 = this->section
->buffer
+ to_underlying (this->sect_off
);
23324 memset (&m_header
, 0, sizeof (m_header
));
23326 read_comp_unit_head (&m_header
, info_ptr
, this->section
,
23327 rcuh_kind::COMPILE
);
23329 m_header_read_in
= true;
23338 dwarf2_per_cu_data::addr_size () const
23340 return this->get_header ()->addr_size
;
23346 dwarf2_per_cu_data::offset_size () const
23348 return this->get_header ()->offset_size
;
23354 dwarf2_per_cu_data::ref_addr_size () const
23356 const comp_unit_head
*header
= this->get_header ();
23358 if (header
->version
== 2)
23359 return header
->addr_size
;
23361 return header
->offset_size
;
23364 /* A helper function for dwarf2_find_containing_comp_unit that returns
23365 the index of the result, and that searches a vector. It will
23366 return a result even if the offset in question does not actually
23367 occur in any CU. This is separate so that it can be unit
23371 dwarf2_find_containing_comp_unit
23372 (sect_offset sect_off
,
23373 unsigned int offset_in_dwz
,
23374 const std::vector
<dwarf2_per_cu_data_up
> &all_comp_units
)
23379 high
= all_comp_units
.size () - 1;
23382 struct dwarf2_per_cu_data
*mid_cu
;
23383 int mid
= low
+ (high
- low
) / 2;
23385 mid_cu
= all_comp_units
[mid
].get ();
23386 if (mid_cu
->is_dwz
> offset_in_dwz
23387 || (mid_cu
->is_dwz
== offset_in_dwz
23388 && mid_cu
->sect_off
+ mid_cu
->length
> sect_off
))
23393 gdb_assert (low
== high
);
23397 /* Locate the .debug_info compilation unit from CU's objfile which contains
23398 the DIE at OFFSET. Raises an error on failure. */
23400 static struct dwarf2_per_cu_data
*
23401 dwarf2_find_containing_comp_unit (sect_offset sect_off
,
23402 unsigned int offset_in_dwz
,
23403 dwarf2_per_bfd
*per_bfd
)
23405 int low
= dwarf2_find_containing_comp_unit
23406 (sect_off
, offset_in_dwz
, per_bfd
->all_comp_units
);
23407 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_comp_units
[low
].get ();
23409 if (this_cu
->is_dwz
!= offset_in_dwz
|| this_cu
->sect_off
> sect_off
)
23411 if (low
== 0 || this_cu
->is_dwz
!= offset_in_dwz
)
23412 error (_("Dwarf Error: could not find partial DIE containing "
23413 "offset %s [in module %s]"),
23414 sect_offset_str (sect_off
),
23415 bfd_get_filename (per_bfd
->obfd
));
23417 gdb_assert (per_bfd
->all_comp_units
[low
-1]->sect_off
23419 return per_bfd
->all_comp_units
[low
- 1].get ();
23423 if (low
== per_bfd
->all_comp_units
.size () - 1
23424 && sect_off
>= this_cu
->sect_off
+ this_cu
->length
)
23425 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off
));
23426 gdb_assert (sect_off
< this_cu
->sect_off
+ this_cu
->length
);
23433 namespace selftests
{
23434 namespace find_containing_comp_unit
{
23439 dwarf2_per_cu_data_up
one (new dwarf2_per_cu_data
);
23440 dwarf2_per_cu_data
*one_ptr
= one
.get ();
23441 dwarf2_per_cu_data_up
two (new dwarf2_per_cu_data
);
23442 dwarf2_per_cu_data
*two_ptr
= two
.get ();
23443 dwarf2_per_cu_data_up
three (new dwarf2_per_cu_data
);
23444 dwarf2_per_cu_data
*three_ptr
= three
.get ();
23445 dwarf2_per_cu_data_up
four (new dwarf2_per_cu_data
);
23446 dwarf2_per_cu_data
*four_ptr
= four
.get ();
23449 two
->sect_off
= sect_offset (one
->length
);
23454 four
->sect_off
= sect_offset (three
->length
);
23458 std::vector
<dwarf2_per_cu_data_up
> units
;
23459 units
.push_back (std::move (one
));
23460 units
.push_back (std::move (two
));
23461 units
.push_back (std::move (three
));
23462 units
.push_back (std::move (four
));
23466 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 0, units
);
23467 SELF_CHECK (units
[result
].get () == one_ptr
);
23468 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 0, units
);
23469 SELF_CHECK (units
[result
].get () == one_ptr
);
23470 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 0, units
);
23471 SELF_CHECK (units
[result
].get () == two_ptr
);
23473 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 1, units
);
23474 SELF_CHECK (units
[result
].get () == three_ptr
);
23475 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 1, units
);
23476 SELF_CHECK (units
[result
].get () == three_ptr
);
23477 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 1, units
);
23478 SELF_CHECK (units
[result
].get () == four_ptr
);
23484 #endif /* GDB_SELF_TEST */
23486 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23489 prepare_one_comp_unit (struct dwarf2_cu
*cu
, struct die_info
*comp_unit_die
,
23490 enum language pretend_language
)
23492 struct attribute
*attr
;
23494 cu
->producer
= dwarf2_string_attr (comp_unit_die
, DW_AT_producer
, cu
);
23496 /* Set the language we're debugging. */
23497 attr
= dwarf2_attr (comp_unit_die
, DW_AT_language
, cu
);
23498 if (cu
->producer
!= nullptr
23499 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
23501 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23502 attribute is not standardised yet. As a workaround for the
23503 language detection we fall back to the DW_AT_producer
23505 cu
->per_cu
->lang
= language_opencl
;
23507 else if (cu
->producer
!= nullptr
23508 && strstr (cu
->producer
, "GNU Go ") != NULL
)
23510 /* Similar hack for Go. */
23511 cu
->per_cu
->lang
= language_go
;
23513 else if (attr
!= nullptr)
23514 cu
->per_cu
->lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
23516 cu
->per_cu
->lang
= pretend_language
;
23517 cu
->language_defn
= language_def (cu
->per_cu
->lang
);
23523 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
23525 auto it
= m_dwarf2_cus
.find (per_cu
);
23526 if (it
== m_dwarf2_cus
.end ())
23535 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_cu
*cu
)
23537 gdb_assert (this->get_cu (per_cu
) == nullptr);
23539 m_dwarf2_cus
[per_cu
] = cu
;
23545 dwarf2_per_objfile::age_comp_units ()
23547 dwarf_read_debug_printf_v ("running");
23549 /* This is not expected to be called in the middle of CU expansion. There is
23550 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23551 loaded in memory. Calling age_comp_units while the queue is in use could
23552 make us free the DIEs for a CU that is in the queue and therefore break
23554 gdb_assert (!this->per_bfd
->queue
.has_value ());
23556 /* Start by clearing all marks. */
23557 for (auto pair
: m_dwarf2_cus
)
23558 pair
.second
->clear_mark ();
23560 /* Traverse all CUs, mark them and their dependencies if used recently
23562 for (auto pair
: m_dwarf2_cus
)
23564 dwarf2_cu
*cu
= pair
.second
;
23567 if (cu
->last_used
<= dwarf_max_cache_age
)
23571 /* Delete all CUs still not marked. */
23572 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
23574 dwarf2_cu
*cu
= it
->second
;
23576 if (!cu
->is_marked ())
23578 dwarf_read_debug_printf_v ("deleting old CU %s",
23579 sect_offset_str (cu
->per_cu
->sect_off
));
23581 it
= m_dwarf2_cus
.erase (it
);
23591 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
23593 auto it
= m_dwarf2_cus
.find (per_cu
);
23594 if (it
== m_dwarf2_cus
.end ())
23599 m_dwarf2_cus
.erase (it
);
23602 dwarf2_per_objfile::~dwarf2_per_objfile ()
23607 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23608 We store these in a hash table separate from the DIEs, and preserve them
23609 when the DIEs are flushed out of cache.
23611 The CU "per_cu" pointer is needed because offset alone is not enough to
23612 uniquely identify the type. A file may have multiple .debug_types sections,
23613 or the type may come from a DWO file. Furthermore, while it's more logical
23614 to use per_cu->section+offset, with Fission the section with the data is in
23615 the DWO file but we don't know that section at the point we need it.
23616 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23617 because we can enter the lookup routine, get_die_type_at_offset, from
23618 outside this file, and thus won't necessarily have PER_CU->cu.
23619 Fortunately, PER_CU is stable for the life of the objfile. */
23621 struct dwarf2_per_cu_offset_and_type
23623 const struct dwarf2_per_cu_data
*per_cu
;
23624 sect_offset sect_off
;
23628 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23631 per_cu_offset_and_type_hash (const void *item
)
23633 const struct dwarf2_per_cu_offset_and_type
*ofs
23634 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
23636 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
23639 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23642 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
23644 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
23645 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
23646 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
23647 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
23649 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
23650 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
23653 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23654 table if necessary. For convenience, return TYPE.
23656 The DIEs reading must have careful ordering to:
23657 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23658 reading current DIE.
23659 * Not trying to dereference contents of still incompletely read in types
23660 while reading in other DIEs.
23661 * Enable referencing still incompletely read in types just by a pointer to
23662 the type without accessing its fields.
23664 Therefore caller should follow these rules:
23665 * Try to fetch any prerequisite types we may need to build this DIE type
23666 before building the type and calling set_die_type.
23667 * After building type call set_die_type for current DIE as soon as
23668 possible before fetching more types to complete the current type.
23669 * Make the type as complete as possible before fetching more types. */
23671 static struct type
*
23672 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
23673 bool skip_data_location
)
23675 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23676 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
23677 struct objfile
*objfile
= per_objfile
->objfile
;
23678 struct attribute
*attr
;
23679 struct dynamic_prop prop
;
23681 /* For Ada types, make sure that the gnat-specific data is always
23682 initialized (if not already set). There are a few types where
23683 we should not be doing so, because the type-specific area is
23684 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23685 where the type-specific area is used to store the floatformat).
23686 But this is not a problem, because the gnat-specific information
23687 is actually not needed for these types. */
23688 if (need_gnat_info (cu
)
23689 && type
->code () != TYPE_CODE_FUNC
23690 && type
->code () != TYPE_CODE_FLT
23691 && type
->code () != TYPE_CODE_METHODPTR
23692 && type
->code () != TYPE_CODE_MEMBERPTR
23693 && type
->code () != TYPE_CODE_METHOD
23694 && type
->code () != TYPE_CODE_FIXED_POINT
23695 && !HAVE_GNAT_AUX_INFO (type
))
23696 INIT_GNAT_SPECIFIC (type
);
23698 /* Read DW_AT_allocated and set in type. */
23699 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
23702 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23703 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23704 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
23707 /* Read DW_AT_associated and set in type. */
23708 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
23711 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23712 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23713 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
23716 /* Read DW_AT_rank and set in type. */
23717 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
23720 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23721 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23722 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
23725 /* Read DW_AT_data_location and set in type. */
23726 if (!skip_data_location
)
23728 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
23729 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
23730 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
23733 if (per_objfile
->die_type_hash
== NULL
)
23734 per_objfile
->die_type_hash
23735 = htab_up (htab_create_alloc (127,
23736 per_cu_offset_and_type_hash
,
23737 per_cu_offset_and_type_eq
,
23738 NULL
, xcalloc
, xfree
));
23740 ofs
.per_cu
= cu
->per_cu
;
23741 ofs
.sect_off
= die
->sect_off
;
23743 slot
= (struct dwarf2_per_cu_offset_and_type
**)
23744 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
23746 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23747 sect_offset_str (die
->sect_off
));
23748 *slot
= XOBNEW (&objfile
->objfile_obstack
,
23749 struct dwarf2_per_cu_offset_and_type
);
23754 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23755 or return NULL if the die does not have a saved type. */
23757 static struct type
*
23758 get_die_type_at_offset (sect_offset sect_off
,
23759 dwarf2_per_cu_data
*per_cu
,
23760 dwarf2_per_objfile
*per_objfile
)
23762 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
23764 if (per_objfile
->die_type_hash
== NULL
)
23767 ofs
.per_cu
= per_cu
;
23768 ofs
.sect_off
= sect_off
;
23769 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
23770 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
23777 /* Look up the type for DIE in CU in die_type_hash,
23778 or return NULL if DIE does not have a saved type. */
23780 static struct type
*
23781 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
23783 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
23786 struct cmd_list_element
*set_dwarf_cmdlist
;
23787 struct cmd_list_element
*show_dwarf_cmdlist
;
23790 show_check_physname (struct ui_file
*file
, int from_tty
,
23791 struct cmd_list_element
*c
, const char *value
)
23794 _("Whether to check \"physname\" is %s.\n"),
23798 void _initialize_dwarf2_read ();
23800 _initialize_dwarf2_read ()
23802 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
23804 Set DWARF specific variables.\n\
23805 Configure DWARF variables such as the cache size."),
23807 Show DWARF specific variables.\n\
23808 Show DWARF variables such as the cache size."),
23809 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
23810 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
23812 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
23813 &dwarf_max_cache_age
, _("\
23814 Set the upper bound on the age of cached DWARF compilation units."), _("\
23815 Show the upper bound on the age of cached DWARF compilation units."), _("\
23816 A higher limit means that cached compilation units will be stored\n\
23817 in memory longer, and more total memory will be used. Zero disables\n\
23818 caching, which can slow down startup."),
23820 show_dwarf_max_cache_age
,
23821 &set_dwarf_cmdlist
,
23822 &show_dwarf_cmdlist
);
23824 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
23825 Set debugging of the DWARF reader."), _("\
23826 Show debugging of the DWARF reader."), _("\
23827 When enabled (non-zero), debugging messages are printed during DWARF\n\
23828 reading and symtab expansion. A value of 1 (one) provides basic\n\
23829 information. A value greater than 1 provides more verbose information."),
23832 &setdebuglist
, &showdebuglist
);
23834 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
23835 Set debugging of the DWARF DIE reader."), _("\
23836 Show debugging of the DWARF DIE reader."), _("\
23837 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23838 The value is the maximum depth to print."),
23841 &setdebuglist
, &showdebuglist
);
23843 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
23844 Set debugging of the dwarf line reader."), _("\
23845 Show debugging of the dwarf line reader."), _("\
23846 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23847 A value of 1 (one) provides basic information.\n\
23848 A value greater than 1 provides more verbose information."),
23851 &setdebuglist
, &showdebuglist
);
23853 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
23854 Set cross-checking of \"physname\" code against demangler."), _("\
23855 Show cross-checking of \"physname\" code against demangler."), _("\
23856 When enabled, GDB's internal \"physname\" code is checked against\n\
23858 NULL
, show_check_physname
,
23859 &setdebuglist
, &showdebuglist
);
23861 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23862 no_class
, &use_deprecated_index_sections
, _("\
23863 Set whether to use deprecated gdb_index sections."), _("\
23864 Show whether to use deprecated gdb_index sections."), _("\
23865 When enabled, deprecated .gdb_index sections are used anyway.\n\
23866 Normally they are ignored either because of a missing feature or\n\
23867 performance issue.\n\
23868 Warning: This option must be enabled before gdb reads the file."),
23871 &setlist
, &showlist
);
23873 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23874 &dwarf2_locexpr_funcs
);
23875 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23876 &dwarf2_loclist_funcs
);
23878 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23879 &dwarf2_block_frame_base_locexpr_funcs
);
23880 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23881 &dwarf2_block_frame_base_loclist_funcs
);
23884 selftests::register_test ("dw2_expand_symtabs_matching",
23885 selftests::dw2_expand_symtabs_matching::run_test
);
23886 selftests::register_test ("dwarf2_find_containing_comp_unit",
23887 selftests::find_containing_comp_unit::run_test
);