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 struct compunit_symtab
*find_pc_sect_compunit_symtab
1835 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
1836 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
) override
;
1838 struct compunit_symtab
*find_compunit_symtab_by_address
1839 (struct objfile
*objfile
, CORE_ADDR address
) override
1844 void map_symbol_filenames (struct objfile
*objfile
,
1845 gdb::function_view
<symbol_filename_ftype
> fun
,
1846 bool need_fullname
) override
;
1849 /* With OBJF_READNOW, the DWARF reader expands all CUs immediately.
1850 It's handy in this case to have an empty implementation of the
1851 quick symbol functions, to avoid special cases in the rest of the
1854 struct readnow_functions
: public dwarf2_base_index_functions
1856 void dump (struct objfile
*objfile
) override
1860 void expand_matching_symbols
1862 const lookup_name_info
&lookup_name
,
1865 symbol_compare_ftype
*ordered_compare
) override
1869 bool expand_symtabs_matching
1870 (struct objfile
*objfile
,
1871 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1872 const lookup_name_info
*lookup_name
,
1873 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1874 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1875 block_search_flags search_flags
,
1877 enum search_domain kind
) override
1883 struct dwarf2_gdb_index
: public dwarf2_base_index_functions
1885 void dump (struct objfile
*objfile
) override
;
1887 void expand_matching_symbols
1889 const lookup_name_info
&lookup_name
,
1892 symbol_compare_ftype
*ordered_compare
) override
;
1894 bool expand_symtabs_matching
1895 (struct objfile
*objfile
,
1896 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1897 const lookup_name_info
*lookup_name
,
1898 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1899 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1900 block_search_flags search_flags
,
1902 enum search_domain kind
) override
;
1905 struct dwarf2_debug_names_index
: public dwarf2_base_index_functions
1907 void dump (struct objfile
*objfile
) override
;
1909 void expand_matching_symbols
1911 const lookup_name_info
&lookup_name
,
1914 symbol_compare_ftype
*ordered_compare
) override
;
1916 bool expand_symtabs_matching
1917 (struct objfile
*objfile
,
1918 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
1919 const lookup_name_info
*lookup_name
,
1920 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
1921 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
1922 block_search_flags search_flags
,
1924 enum search_domain kind
) override
;
1927 quick_symbol_functions_up
1928 mapped_index::make_quick_functions () const
1930 return quick_symbol_functions_up (new dwarf2_gdb_index
);
1933 quick_symbol_functions_up
1934 mapped_debug_names::make_quick_functions () const
1936 return quick_symbol_functions_up (new dwarf2_debug_names_index
);
1939 /* Utility hash function for a stmt_list_hash. */
1942 hash_stmt_list_entry (const struct stmt_list_hash
*stmt_list_hash
)
1946 if (stmt_list_hash
->dwo_unit
!= NULL
)
1947 v
+= (uintptr_t) stmt_list_hash
->dwo_unit
->dwo_file
;
1948 v
+= to_underlying (stmt_list_hash
->line_sect_off
);
1952 /* Utility equality function for a stmt_list_hash. */
1955 eq_stmt_list_entry (const struct stmt_list_hash
*lhs
,
1956 const struct stmt_list_hash
*rhs
)
1958 if ((lhs
->dwo_unit
!= NULL
) != (rhs
->dwo_unit
!= NULL
))
1960 if (lhs
->dwo_unit
!= NULL
1961 && lhs
->dwo_unit
->dwo_file
!= rhs
->dwo_unit
->dwo_file
)
1964 return lhs
->line_sect_off
== rhs
->line_sect_off
;
1967 /* Hash function for a quick_file_names. */
1970 hash_file_name_entry (const void *e
)
1972 const struct quick_file_names
*file_data
1973 = (const struct quick_file_names
*) e
;
1975 return hash_stmt_list_entry (&file_data
->hash
);
1978 /* Equality function for a quick_file_names. */
1981 eq_file_name_entry (const void *a
, const void *b
)
1983 const struct quick_file_names
*ea
= (const struct quick_file_names
*) a
;
1984 const struct quick_file_names
*eb
= (const struct quick_file_names
*) b
;
1986 return eq_stmt_list_entry (&ea
->hash
, &eb
->hash
);
1989 /* Create a quick_file_names hash table. */
1992 create_quick_file_names_table (unsigned int nr_initial_entries
)
1994 return htab_up (htab_create_alloc (nr_initial_entries
,
1995 hash_file_name_entry
, eq_file_name_entry
,
1996 nullptr, xcalloc
, xfree
));
1999 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2000 function is unrelated to symtabs, symtab would have to be created afterwards.
2001 You should call age_cached_comp_units after processing the CU. */
2004 load_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_per_objfile
*per_objfile
,
2007 if (per_cu
->is_debug_types
)
2008 load_full_type_unit (per_cu
, per_objfile
);
2010 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
2011 skip_partial
, language_minimal
);
2013 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
2015 return nullptr; /* Dummy CU. */
2017 dwarf2_find_base_address (cu
->dies
, cu
);
2022 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2025 dw2_do_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2026 dwarf2_per_objfile
*per_objfile
, bool skip_partial
)
2029 /* The destructor of dwarf2_queue_guard frees any entries left on
2030 the queue. After this point we're guaranteed to leave this function
2031 with the dwarf queue empty. */
2032 dwarf2_queue_guard
q_guard (per_objfile
);
2034 if (!per_objfile
->symtab_set_p (per_cu
))
2036 queue_comp_unit (per_cu
, per_objfile
, language_minimal
);
2037 dwarf2_cu
*cu
= load_cu (per_cu
, per_objfile
, skip_partial
);
2039 /* If we just loaded a CU from a DWO, and we're working with an index
2040 that may badly handle TUs, load all the TUs in that DWO as well.
2041 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2042 if (!per_cu
->is_debug_types
2044 && cu
->dwo_unit
!= NULL
2045 && per_objfile
->per_bfd
->index_table
!= NULL
2046 && !per_objfile
->per_bfd
->index_table
->version_check ()
2047 /* DWP files aren't supported yet. */
2048 && get_dwp_file (per_objfile
) == NULL
)
2049 queue_and_load_all_dwo_tus (cu
);
2052 process_queue (per_objfile
);
2055 /* Age the cache, releasing compilation units that have not
2056 been used recently. */
2057 per_objfile
->age_comp_units ();
2060 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2061 the per-objfile for which this symtab is instantiated.
2063 Returns the resulting symbol table. */
2065 static struct compunit_symtab
*
2066 dw2_instantiate_symtab (dwarf2_per_cu_data
*per_cu
,
2067 dwarf2_per_objfile
*per_objfile
,
2070 if (!per_objfile
->symtab_set_p (per_cu
))
2072 free_cached_comp_units
freer (per_objfile
);
2073 scoped_restore decrementer
= increment_reading_symtab ();
2074 dw2_do_instantiate_symtab (per_cu
, per_objfile
, skip_partial
);
2075 process_cu_includes (per_objfile
);
2078 return per_objfile
->get_symtab (per_cu
);
2083 dwarf2_per_cu_data_up
2084 dwarf2_per_bfd::allocate_per_cu ()
2086 dwarf2_per_cu_data_up
result (new dwarf2_per_cu_data
);
2087 result
->per_bfd
= this;
2088 result
->index
= all_comp_units
.size ();
2095 dwarf2_per_bfd::allocate_signatured_type (ULONGEST signature
)
2097 signatured_type_up
result (new signatured_type (signature
));
2098 result
->per_bfd
= this;
2099 result
->index
= all_comp_units
.size ();
2100 result
->is_debug_types
= true;
2105 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2106 obstack, and constructed with the specified field values. */
2108 static dwarf2_per_cu_data_up
2109 create_cu_from_index_list (dwarf2_per_bfd
*per_bfd
,
2110 struct dwarf2_section_info
*section
,
2112 sect_offset sect_off
, ULONGEST length
)
2114 dwarf2_per_cu_data_up the_cu
= per_bfd
->allocate_per_cu ();
2115 the_cu
->sect_off
= sect_off
;
2116 the_cu
->length
= length
;
2117 the_cu
->section
= section
;
2118 the_cu
->is_dwz
= is_dwz
;
2122 /* A helper for create_cus_from_index that handles a given list of
2126 create_cus_from_index_list (dwarf2_per_bfd
*per_bfd
,
2127 const gdb_byte
*cu_list
, offset_type n_elements
,
2128 struct dwarf2_section_info
*section
,
2131 for (offset_type i
= 0; i
< n_elements
; i
+= 2)
2133 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2135 sect_offset sect_off
2136 = (sect_offset
) extract_unsigned_integer (cu_list
, 8, BFD_ENDIAN_LITTLE
);
2137 ULONGEST length
= extract_unsigned_integer (cu_list
+ 8, 8, BFD_ENDIAN_LITTLE
);
2140 dwarf2_per_cu_data_up per_cu
2141 = create_cu_from_index_list (per_bfd
, section
, is_dwz
, sect_off
,
2143 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
2147 /* Read the CU list from the mapped index, and use it to create all
2148 the CU objects for PER_BFD. */
2151 create_cus_from_index (dwarf2_per_bfd
*per_bfd
,
2152 const gdb_byte
*cu_list
, offset_type cu_list_elements
,
2153 const gdb_byte
*dwz_list
, offset_type dwz_elements
)
2155 gdb_assert (per_bfd
->all_comp_units
.empty ());
2156 per_bfd
->all_comp_units
.reserve ((cu_list_elements
+ dwz_elements
) / 2);
2158 create_cus_from_index_list (per_bfd
, cu_list
, cu_list_elements
,
2161 if (dwz_elements
== 0)
2164 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
2165 create_cus_from_index_list (per_bfd
, dwz_list
, dwz_elements
,
2169 /* Create the signatured type hash table from the index. */
2172 create_signatured_type_table_from_index
2173 (dwarf2_per_bfd
*per_bfd
, struct dwarf2_section_info
*section
,
2174 const gdb_byte
*bytes
, offset_type elements
)
2176 htab_up sig_types_hash
= allocate_signatured_type_table ();
2178 for (offset_type i
= 0; i
< elements
; i
+= 3)
2180 signatured_type_up sig_type
;
2183 cu_offset type_offset_in_tu
;
2185 gdb_static_assert (sizeof (ULONGEST
) >= 8);
2186 sect_offset sect_off
2187 = (sect_offset
) extract_unsigned_integer (bytes
, 8, BFD_ENDIAN_LITTLE
);
2189 = (cu_offset
) extract_unsigned_integer (bytes
+ 8, 8,
2191 signature
= extract_unsigned_integer (bytes
+ 16, 8, BFD_ENDIAN_LITTLE
);
2194 sig_type
= per_bfd
->allocate_signatured_type (signature
);
2195 sig_type
->type_offset_in_tu
= type_offset_in_tu
;
2196 sig_type
->section
= section
;
2197 sig_type
->sect_off
= sect_off
;
2199 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2200 *slot
= sig_type
.get ();
2202 per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2205 per_bfd
->signatured_types
= std::move (sig_types_hash
);
2208 /* Create the signatured type hash table from .debug_names. */
2211 create_signatured_type_table_from_debug_names
2212 (dwarf2_per_objfile
*per_objfile
,
2213 const mapped_debug_names
&map
,
2214 struct dwarf2_section_info
*section
,
2215 struct dwarf2_section_info
*abbrev_section
)
2217 struct objfile
*objfile
= per_objfile
->objfile
;
2219 section
->read (objfile
);
2220 abbrev_section
->read (objfile
);
2222 htab_up sig_types_hash
= allocate_signatured_type_table ();
2224 for (uint32_t i
= 0; i
< map
.tu_count
; ++i
)
2226 signatured_type_up sig_type
;
2229 sect_offset sect_off
2230 = (sect_offset
) (extract_unsigned_integer
2231 (map
.tu_table_reordered
+ i
* map
.offset_size
,
2233 map
.dwarf5_byte_order
));
2235 comp_unit_head cu_header
;
2236 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
2238 section
->buffer
+ to_underlying (sect_off
),
2241 sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
2242 (cu_header
.signature
);
2243 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
2244 sig_type
->section
= section
;
2245 sig_type
->sect_off
= sect_off
;
2247 slot
= htab_find_slot (sig_types_hash
.get (), sig_type
.get (), INSERT
);
2248 *slot
= sig_type
.get ();
2250 per_objfile
->per_bfd
->all_comp_units
.emplace_back (sig_type
.release ());
2253 per_objfile
->per_bfd
->signatured_types
= std::move (sig_types_hash
);
2256 /* Read the address map data from the mapped index, and use it to
2257 populate the psymtabs_addrmap. */
2260 create_addrmap_from_index (dwarf2_per_objfile
*per_objfile
,
2261 struct mapped_index
*index
)
2263 struct objfile
*objfile
= per_objfile
->objfile
;
2264 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2265 struct gdbarch
*gdbarch
= objfile
->arch ();
2266 const gdb_byte
*iter
, *end
;
2267 struct addrmap
*mutable_map
;
2270 auto_obstack temp_obstack
;
2272 mutable_map
= addrmap_create_mutable (&temp_obstack
);
2274 iter
= index
->address_table
.data ();
2275 end
= iter
+ index
->address_table
.size ();
2277 baseaddr
= objfile
->text_section_offset ();
2281 ULONGEST hi
, lo
, cu_index
;
2282 lo
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2284 hi
= extract_unsigned_integer (iter
, 8, BFD_ENDIAN_LITTLE
);
2286 cu_index
= extract_unsigned_integer (iter
, 4, BFD_ENDIAN_LITTLE
);
2291 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2292 hex_string (lo
), hex_string (hi
));
2296 if (cu_index
>= per_bfd
->all_comp_units
.size ())
2298 complaint (_(".gdb_index address table has invalid CU number %u"),
2299 (unsigned) cu_index
);
2303 lo
= gdbarch_adjust_dwarf2_addr (gdbarch
, lo
+ baseaddr
) - baseaddr
;
2304 hi
= gdbarch_adjust_dwarf2_addr (gdbarch
, hi
+ baseaddr
) - baseaddr
;
2305 addrmap_set_empty (mutable_map
, lo
, hi
- 1,
2306 per_bfd
->get_cu (cu_index
));
2309 per_bfd
->index_addrmap
= addrmap_create_fixed (mutable_map
,
2313 /* Read the address map data from DWARF-5 .debug_aranges, and use it
2314 to populate given addrmap. Returns true on success, false on
2318 read_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2319 struct dwarf2_section_info
*section
,
2320 addrmap
*mutable_map
)
2322 struct objfile
*objfile
= per_objfile
->objfile
;
2323 bfd
*abfd
= objfile
->obfd
;
2324 struct gdbarch
*gdbarch
= objfile
->arch ();
2325 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
2326 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2328 std::unordered_map
<sect_offset
,
2329 dwarf2_per_cu_data
*,
2330 gdb::hash_enum
<sect_offset
>>
2331 debug_info_offset_to_per_cu
;
2332 for (const auto &per_cu
: per_bfd
->all_comp_units
)
2334 /* A TU will not need aranges, and skipping them here is an easy
2335 way of ignoring .debug_types -- and possibly seeing a
2336 duplicate section offset -- entirely. The same applies to
2337 units coming from a dwz file. */
2338 if (per_cu
->is_debug_types
|| per_cu
->is_dwz
)
2341 const auto insertpair
2342 = debug_info_offset_to_per_cu
.emplace (per_cu
->sect_off
,
2344 if (!insertpair
.second
)
2346 warning (_("Section .debug_aranges in %s has duplicate "
2347 "debug_info_offset %s, ignoring .debug_aranges."),
2348 objfile_name (objfile
), sect_offset_str (per_cu
->sect_off
));
2353 section
->read (objfile
);
2355 const bfd_endian dwarf5_byte_order
= gdbarch_byte_order (gdbarch
);
2357 const gdb_byte
*addr
= section
->buffer
;
2359 while (addr
< section
->buffer
+ section
->size
)
2361 const gdb_byte
*const entry_addr
= addr
;
2362 unsigned int bytes_read
;
2364 const LONGEST entry_length
= read_initial_length (abfd
, addr
,
2368 const gdb_byte
*const entry_end
= addr
+ entry_length
;
2369 const bool dwarf5_is_dwarf64
= bytes_read
!= 4;
2370 const uint8_t offset_size
= dwarf5_is_dwarf64
? 8 : 4;
2371 if (addr
+ entry_length
> section
->buffer
+ section
->size
)
2373 warning (_("Section .debug_aranges in %s entry at offset %s "
2374 "length %s exceeds section length %s, "
2375 "ignoring .debug_aranges."),
2376 objfile_name (objfile
),
2377 plongest (entry_addr
- section
->buffer
),
2378 plongest (bytes_read
+ entry_length
),
2379 pulongest (section
->size
));
2383 /* The version number. */
2384 const uint16_t version
= read_2_bytes (abfd
, addr
);
2388 warning (_("Section .debug_aranges in %s entry at offset %s "
2389 "has unsupported version %d, ignoring .debug_aranges."),
2390 objfile_name (objfile
),
2391 plongest (entry_addr
- section
->buffer
), version
);
2395 const uint64_t debug_info_offset
2396 = extract_unsigned_integer (addr
, offset_size
, dwarf5_byte_order
);
2397 addr
+= offset_size
;
2398 const auto per_cu_it
2399 = debug_info_offset_to_per_cu
.find (sect_offset (debug_info_offset
));
2400 if (per_cu_it
== debug_info_offset_to_per_cu
.cend ())
2402 warning (_("Section .debug_aranges in %s entry at offset %s "
2403 "debug_info_offset %s does not exists, "
2404 "ignoring .debug_aranges."),
2405 objfile_name (objfile
),
2406 plongest (entry_addr
- section
->buffer
),
2407 pulongest (debug_info_offset
));
2410 dwarf2_per_cu_data
*const per_cu
= per_cu_it
->second
;
2412 const uint8_t address_size
= *addr
++;
2413 if (address_size
< 1 || address_size
> 8)
2415 warning (_("Section .debug_aranges in %s entry at offset %s "
2416 "address_size %u is invalid, ignoring .debug_aranges."),
2417 objfile_name (objfile
),
2418 plongest (entry_addr
- section
->buffer
), address_size
);
2422 const uint8_t segment_selector_size
= *addr
++;
2423 if (segment_selector_size
!= 0)
2425 warning (_("Section .debug_aranges in %s entry at offset %s "
2426 "segment_selector_size %u is not supported, "
2427 "ignoring .debug_aranges."),
2428 objfile_name (objfile
),
2429 plongest (entry_addr
- section
->buffer
),
2430 segment_selector_size
);
2434 /* Must pad to an alignment boundary that is twice the address
2435 size. It is undocumented by the DWARF standard but GCC does
2436 use it. However, not every compiler does this. We can see
2437 whether it has happened by looking at the total length of the
2438 contents of the aranges for this CU -- it if isn't a multiple
2439 of twice the address size, then we skip any leftover
2441 addr
+= (entry_end
- addr
) % (2 * address_size
);
2443 while (addr
< entry_end
)
2445 if (addr
+ 2 * address_size
> entry_end
)
2447 warning (_("Section .debug_aranges in %s entry at offset %s "
2448 "address list is not properly terminated, "
2449 "ignoring .debug_aranges."),
2450 objfile_name (objfile
),
2451 plongest (entry_addr
- section
->buffer
));
2454 ULONGEST start
= extract_unsigned_integer (addr
, address_size
,
2456 addr
+= address_size
;
2457 ULONGEST length
= extract_unsigned_integer (addr
, address_size
,
2459 addr
+= address_size
;
2460 if (start
== 0 && length
== 0)
2462 /* This can happen on some targets with --gc-sections.
2463 This pair of values is also used to mark the end of
2464 the entries for a given CU, but we ignore it and
2465 instead handle termination using the check at the top
2469 if (start
== 0 && !per_bfd
->has_section_at_zero
)
2471 /* Symbol was eliminated due to a COMDAT group. */
2474 ULONGEST end
= start
+ length
;
2475 start
= (gdbarch_adjust_dwarf2_addr (gdbarch
, start
+ baseaddr
)
2477 end
= (gdbarch_adjust_dwarf2_addr (gdbarch
, end
+ baseaddr
)
2479 addrmap_set_empty (mutable_map
, start
, end
- 1, per_cu
);
2482 per_cu
->addresses_seen
= true;
2488 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2489 populate the psymtabs_addrmap. */
2492 create_addrmap_from_aranges (dwarf2_per_objfile
*per_objfile
,
2493 struct dwarf2_section_info
*section
)
2495 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2497 auto_obstack temp_obstack
;
2498 addrmap
*mutable_map
= addrmap_create_mutable (&temp_obstack
);
2500 if (read_addrmap_from_aranges (per_objfile
, section
, mutable_map
))
2501 per_bfd
->index_addrmap
= addrmap_create_fixed (mutable_map
,
2505 /* A helper function that reads the .gdb_index from BUFFER and fills
2506 in MAP. FILENAME is the name of the file containing the data;
2507 it is used for error reporting. DEPRECATED_OK is true if it is
2508 ok to use deprecated sections.
2510 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2511 out parameters that are filled in with information about the CU and
2512 TU lists in the section.
2514 Returns true if all went well, false otherwise. */
2517 read_gdb_index_from_buffer (const char *filename
,
2519 gdb::array_view
<const gdb_byte
> buffer
,
2520 struct mapped_index
*map
,
2521 const gdb_byte
**cu_list
,
2522 offset_type
*cu_list_elements
,
2523 const gdb_byte
**types_list
,
2524 offset_type
*types_list_elements
)
2526 const gdb_byte
*addr
= &buffer
[0];
2527 offset_view
metadata (buffer
);
2529 /* Version check. */
2530 offset_type version
= metadata
[0];
2531 /* Versions earlier than 3 emitted every copy of a psymbol. This
2532 causes the index to behave very poorly for certain requests. Version 3
2533 contained incomplete addrmap. So, it seems better to just ignore such
2537 static int warning_printed
= 0;
2538 if (!warning_printed
)
2540 warning (_("Skipping obsolete .gdb_index section in %s."),
2542 warning_printed
= 1;
2546 /* Index version 4 uses a different hash function than index version
2549 Versions earlier than 6 did not emit psymbols for inlined
2550 functions. Using these files will cause GDB not to be able to
2551 set breakpoints on inlined functions by name, so we ignore these
2552 indices unless the user has done
2553 "set use-deprecated-index-sections on". */
2554 if (version
< 6 && !deprecated_ok
)
2556 static int warning_printed
= 0;
2557 if (!warning_printed
)
2560 Skipping deprecated .gdb_index section in %s.\n\
2561 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2562 to use the section anyway."),
2564 warning_printed
= 1;
2568 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2569 of the TU (for symbols coming from TUs),
2570 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2571 Plus gold-generated indices can have duplicate entries for global symbols,
2572 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2573 These are just performance bugs, and we can't distinguish gdb-generated
2574 indices from gold-generated ones, so issue no warning here. */
2576 /* Indexes with higher version than the one supported by GDB may be no
2577 longer backward compatible. */
2581 map
->version
= version
;
2584 *cu_list
= addr
+ metadata
[i
];
2585 *cu_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2588 *types_list
= addr
+ metadata
[i
];
2589 *types_list_elements
= (metadata
[i
+ 1] - metadata
[i
]) / 8;
2592 const gdb_byte
*address_table
= addr
+ metadata
[i
];
2593 const gdb_byte
*address_table_end
= addr
+ metadata
[i
+ 1];
2595 = gdb::array_view
<const gdb_byte
> (address_table
, address_table_end
);
2598 const gdb_byte
*symbol_table
= addr
+ metadata
[i
];
2599 const gdb_byte
*symbol_table_end
= addr
+ metadata
[i
+ 1];
2601 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2605 map
->constant_pool
= buffer
.slice (metadata
[i
]);
2607 if (map
->constant_pool
.empty () && !map
->symbol_table
.empty ())
2609 /* An empty constant pool implies that all symbol table entries are
2610 empty. Make map->symbol_table.empty () == true. */
2612 = offset_view (gdb::array_view
<const gdb_byte
> (symbol_table
,
2619 /* Callback types for dwarf2_read_gdb_index. */
2621 typedef gdb::function_view
2622 <gdb::array_view
<const gdb_byte
>(objfile
*, dwarf2_per_bfd
*)>
2623 get_gdb_index_contents_ftype
;
2624 typedef gdb::function_view
2625 <gdb::array_view
<const gdb_byte
>(objfile
*, dwz_file
*)>
2626 get_gdb_index_contents_dwz_ftype
;
2628 /* Read .gdb_index. If everything went ok, initialize the "quick"
2629 elements of all the CUs and return 1. Otherwise, return 0. */
2632 dwarf2_read_gdb_index
2633 (dwarf2_per_objfile
*per_objfile
,
2634 get_gdb_index_contents_ftype get_gdb_index_contents
,
2635 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz
)
2637 const gdb_byte
*cu_list
, *types_list
, *dwz_list
= NULL
;
2638 offset_type cu_list_elements
, types_list_elements
, dwz_list_elements
= 0;
2639 struct dwz_file
*dwz
;
2640 struct objfile
*objfile
= per_objfile
->objfile
;
2641 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
2643 gdb::array_view
<const gdb_byte
> main_index_contents
2644 = get_gdb_index_contents (objfile
, per_bfd
);
2646 if (main_index_contents
.empty ())
2649 std::unique_ptr
<struct mapped_index
> map (new struct mapped_index
);
2650 if (!read_gdb_index_from_buffer (objfile_name (objfile
),
2651 use_deprecated_index_sections
,
2652 main_index_contents
, map
.get (), &cu_list
,
2653 &cu_list_elements
, &types_list
,
2654 &types_list_elements
))
2657 /* Don't use the index if it's empty. */
2658 if (map
->symbol_table
.empty ())
2661 /* If there is a .dwz file, read it so we can get its CU list as
2663 dwz
= dwarf2_get_dwz_file (per_bfd
);
2666 struct mapped_index dwz_map
;
2667 const gdb_byte
*dwz_types_ignore
;
2668 offset_type dwz_types_elements_ignore
;
2670 gdb::array_view
<const gdb_byte
> dwz_index_content
2671 = get_gdb_index_contents_dwz (objfile
, dwz
);
2673 if (dwz_index_content
.empty ())
2676 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz
->dwz_bfd
.get ()),
2677 1, dwz_index_content
, &dwz_map
,
2678 &dwz_list
, &dwz_list_elements
,
2680 &dwz_types_elements_ignore
))
2682 warning (_("could not read '.gdb_index' section from %s; skipping"),
2683 bfd_get_filename (dwz
->dwz_bfd
.get ()));
2688 create_cus_from_index (per_bfd
, cu_list
, cu_list_elements
, dwz_list
,
2691 if (types_list_elements
)
2693 /* We can only handle a single .debug_types when we have an
2695 if (per_bfd
->types
.size () != 1)
2698 dwarf2_section_info
*section
= &per_bfd
->types
[0];
2700 create_signatured_type_table_from_index (per_bfd
, section
, types_list
,
2701 types_list_elements
);
2704 create_addrmap_from_index (per_objfile
, map
.get ());
2706 per_bfd
->index_table
= std::move (map
);
2707 per_bfd
->quick_file_names_table
=
2708 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
2713 /* die_reader_func for dw2_get_file_names. */
2716 dw2_get_file_names_reader (const struct die_reader_specs
*reader
,
2717 struct die_info
*comp_unit_die
)
2719 struct dwarf2_cu
*cu
= reader
->cu
;
2720 struct dwarf2_per_cu_data
*this_cu
= cu
->per_cu
;
2721 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
2722 struct dwarf2_per_cu_data
*lh_cu
;
2723 struct attribute
*attr
;
2725 struct quick_file_names
*qfn
;
2727 gdb_assert (! this_cu
->is_debug_types
);
2729 this_cu
->files_read
= true;
2730 /* Our callers never want to match partial units -- instead they
2731 will match the enclosing full CU. */
2732 if (comp_unit_die
->tag
== DW_TAG_partial_unit
)
2739 sect_offset line_offset
{};
2741 attr
= dwarf2_attr (comp_unit_die
, DW_AT_stmt_list
, cu
);
2742 if (attr
!= nullptr && attr
->form_is_unsigned ())
2744 struct quick_file_names find_entry
;
2746 line_offset
= (sect_offset
) attr
->as_unsigned ();
2748 /* We may have already read in this line header (TU line header sharing).
2749 If we have we're done. */
2750 find_entry
.hash
.dwo_unit
= cu
->dwo_unit
;
2751 find_entry
.hash
.line_sect_off
= line_offset
;
2752 slot
= htab_find_slot (per_objfile
->per_bfd
->quick_file_names_table
.get (),
2753 &find_entry
, INSERT
);
2756 lh_cu
->file_names
= (struct quick_file_names
*) *slot
;
2760 lh
= dwarf_decode_line_header (line_offset
, cu
);
2763 file_and_directory
&fnd
= find_file_and_directory (comp_unit_die
, cu
);
2766 if (!fnd
.is_unknown ())
2768 else if (lh
== nullptr)
2771 qfn
= XOBNEW (&per_objfile
->per_bfd
->obstack
, struct quick_file_names
);
2772 qfn
->hash
.dwo_unit
= cu
->dwo_unit
;
2773 qfn
->hash
.line_sect_off
= line_offset
;
2774 /* There may not be a DW_AT_stmt_list. */
2775 if (slot
!= nullptr)
2778 std::vector
<const char *> include_names
;
2781 for (const auto &entry
: lh
->file_names ())
2783 std::string name_holder
;
2784 const char *include_name
=
2785 compute_include_file_name (lh
.get (), entry
, fnd
, name_holder
);
2786 if (include_name
!= nullptr)
2788 include_name
= per_objfile
->objfile
->intern (include_name
);
2789 include_names
.push_back (include_name
);
2794 qfn
->num_file_names
= offset
+ include_names
.size ();
2795 qfn
->comp_dir
= fnd
.intern_comp_dir (per_objfile
->objfile
);
2797 XOBNEWVEC (&per_objfile
->per_bfd
->obstack
, const char *,
2798 qfn
->num_file_names
);
2800 qfn
->file_names
[0] = xstrdup (fnd
.get_name ());
2802 if (!include_names
.empty ())
2803 memcpy (&qfn
->file_names
[offset
], include_names
.data (),
2804 include_names
.size () * sizeof (const char *));
2806 qfn
->real_names
= NULL
;
2808 lh_cu
->file_names
= qfn
;
2811 /* A helper for the "quick" functions which attempts to read the line
2812 table for THIS_CU. */
2814 static struct quick_file_names
*
2815 dw2_get_file_names (dwarf2_per_cu_data
*this_cu
,
2816 dwarf2_per_objfile
*per_objfile
)
2818 /* This should never be called for TUs. */
2819 gdb_assert (! this_cu
->is_debug_types
);
2821 if (this_cu
->files_read
)
2822 return this_cu
->file_names
;
2824 cutu_reader
reader (this_cu
, per_objfile
);
2825 if (!reader
.dummy_p
)
2826 dw2_get_file_names_reader (&reader
, reader
.comp_unit_die
);
2828 return this_cu
->file_names
;
2831 /* A helper for the "quick" functions which computes and caches the
2832 real path for a given file name from the line table. */
2835 dw2_get_real_path (dwarf2_per_objfile
*per_objfile
,
2836 struct quick_file_names
*qfn
, int index
)
2838 if (qfn
->real_names
== NULL
)
2839 qfn
->real_names
= OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
2840 qfn
->num_file_names
, const char *);
2842 if (qfn
->real_names
[index
] == NULL
)
2844 const char *dirname
= nullptr;
2846 if (!IS_ABSOLUTE_PATH (qfn
->file_names
[index
]))
2847 dirname
= qfn
->comp_dir
;
2849 gdb::unique_xmalloc_ptr
<char> fullname
;
2850 fullname
= find_source_or_rewrite (qfn
->file_names
[index
], dirname
);
2852 qfn
->real_names
[index
] = fullname
.release ();
2855 return qfn
->real_names
[index
];
2859 dwarf2_base_index_functions::find_last_source_symtab (struct objfile
*objfile
)
2861 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2862 dwarf2_per_cu_data
*dwarf_cu
2863 = per_objfile
->per_bfd
->all_comp_units
.back ().get ();
2864 compunit_symtab
*cust
= dw2_instantiate_symtab (dwarf_cu
, per_objfile
, false);
2869 return cust
->primary_filetab ();
2875 dwarf2_per_cu_data::free_cached_file_names ()
2878 fnd
->forget_fullname ();
2880 if (per_bfd
== nullptr)
2883 struct quick_file_names
*file_data
= file_names
;
2884 if (file_data
!= nullptr && file_data
->real_names
!= nullptr)
2886 for (int i
= 0; i
< file_data
->num_file_names
; ++i
)
2888 xfree ((void *) file_data
->real_names
[i
]);
2889 file_data
->real_names
[i
] = nullptr;
2895 dwarf2_base_index_functions::forget_cached_source_info
2896 (struct objfile
*objfile
)
2898 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
2900 for (auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
2901 per_cu
->free_cached_file_names ();
2904 /* Struct used to manage iterating over all CUs looking for a symbol. */
2906 struct dw2_symtab_iterator
2908 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
2909 dwarf2_per_objfile
*per_objfile
;
2910 /* If set, only look for symbols that match that block. Valid values are
2911 GLOBAL_BLOCK and STATIC_BLOCK. */
2912 gdb::optional
<block_enum
> block_index
;
2913 /* The kind of symbol we're looking for. */
2915 /* The list of CUs from the index entry of the symbol,
2916 or NULL if not found. */
2918 /* The next element in VEC to look at. */
2920 /* The number of elements in VEC, or zero if there is no match. */
2922 /* Have we seen a global version of the symbol?
2923 If so we can ignore all further global instances.
2924 This is to work around gold/15646, inefficient gold-generated
2929 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
2932 dw2_symtab_iter_init (struct dw2_symtab_iterator
*iter
,
2933 dwarf2_per_objfile
*per_objfile
,
2934 gdb::optional
<block_enum
> block_index
,
2935 domain_enum domain
, offset_type namei
,
2936 mapped_index
&index
)
2938 iter
->per_objfile
= per_objfile
;
2939 iter
->block_index
= block_index
;
2940 iter
->domain
= domain
;
2942 iter
->global_seen
= 0;
2946 gdb_assert (!index
.symbol_name_slot_invalid (namei
));
2947 offset_type vec_idx
= index
.symbol_vec_index (namei
);
2949 iter
->vec
= offset_view (index
.constant_pool
.slice (vec_idx
));
2950 iter
->length
= iter
->vec
[0];
2953 /* Return the next matching CU or NULL if there are no more. */
2955 static struct dwarf2_per_cu_data
*
2956 dw2_symtab_iter_next (struct dw2_symtab_iterator
*iter
,
2957 mapped_index
&index
)
2959 dwarf2_per_objfile
*per_objfile
= iter
->per_objfile
;
2961 for ( ; iter
->next
< iter
->length
; ++iter
->next
)
2963 offset_type cu_index_and_attrs
= iter
->vec
[iter
->next
+ 1];
2964 offset_type cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
2965 gdb_index_symbol_kind symbol_kind
=
2966 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
2967 /* Only check the symbol attributes if they're present.
2968 Indices prior to version 7 don't record them,
2969 and indices >= 7 may elide them for certain symbols
2970 (gold does this). */
2971 int attrs_valid
= (index
.version
>= 7
2972 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
2974 /* Don't crash on bad data. */
2975 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
2977 complaint (_(".gdb_index entry has bad CU index"
2978 " [in module %s]"), objfile_name (per_objfile
->objfile
));
2982 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
2984 /* Skip if already read in. */
2985 if (per_objfile
->symtab_set_p (per_cu
))
2988 /* Check static vs global. */
2991 bool is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
2993 if (iter
->block_index
.has_value ())
2995 bool want_static
= *iter
->block_index
== STATIC_BLOCK
;
2997 if (is_static
!= want_static
)
3001 /* Work around gold/15646. */
3003 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
3005 if (iter
->global_seen
)
3008 iter
->global_seen
= 1;
3012 /* Only check the symbol's kind if it has one. */
3015 switch (iter
->domain
)
3018 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
3019 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
3020 /* Some types are also in VAR_DOMAIN. */
3021 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3025 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
3029 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3033 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
3049 dwarf2_base_index_functions::print_stats (struct objfile
*objfile
,
3055 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3056 int total
= per_objfile
->per_bfd
->all_comp_units
.size ();
3059 for (int i
= 0; i
< total
; ++i
)
3061 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3063 if (!per_objfile
->symtab_set_p (per_cu
))
3066 gdb_printf (_(" Number of read CUs: %d\n"), total
- count
);
3067 gdb_printf (_(" Number of unread CUs: %d\n"), count
);
3070 /* This dumps minimal information about the index.
3071 It is called via "mt print objfiles".
3072 One use is to verify .gdb_index has been loaded by the
3073 gdb.dwarf2/gdb-index.exp testcase. */
3076 dwarf2_gdb_index::dump (struct objfile
*objfile
)
3078 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3080 mapped_index
*index
= (static_cast<mapped_index
*>
3081 (per_objfile
->per_bfd
->index_table
.get ()));
3082 gdb_printf (".gdb_index: version %d\n", index
->version
);
3087 dwarf2_base_index_functions::expand_all_symtabs (struct objfile
*objfile
)
3089 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3090 int total_units
= per_objfile
->per_bfd
->all_comp_units
.size ();
3092 for (int i
= 0; i
< total_units
; ++i
)
3094 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (i
);
3096 /* We don't want to directly expand a partial CU, because if we
3097 read it with the wrong language, then assertion failures can
3098 be triggered later on. See PR symtab/23010. So, tell
3099 dw2_instantiate_symtab to skip partial CUs -- any important
3100 partial CU will be read via DW_TAG_imported_unit anyway. */
3101 dw2_instantiate_symtab (per_cu
, per_objfile
, true);
3106 dw2_expand_symtabs_matching_symbol
3107 (mapped_index_base
&index
,
3108 const lookup_name_info
&lookup_name_in
,
3109 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3110 gdb::function_view
<bool (offset_type
)> match_callback
,
3111 dwarf2_per_objfile
*per_objfile
);
3114 dw2_expand_symtabs_matching_one
3115 (dwarf2_per_cu_data
*per_cu
,
3116 dwarf2_per_objfile
*per_objfile
,
3117 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3118 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
);
3121 dwarf2_gdb_index::expand_matching_symbols
3122 (struct objfile
*objfile
,
3123 const lookup_name_info
&name
, domain_enum domain
,
3125 symbol_compare_ftype
*ordered_compare
)
3128 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
3130 const block_enum block_kind
= global
? GLOBAL_BLOCK
: STATIC_BLOCK
;
3133 = (static_cast<mapped_index
&>
3134 (*per_objfile
->per_bfd
->index_table
.get ()));
3136 const char *match_name
= name
.ada ().lookup_name ().c_str ();
3137 auto matcher
= [&] (const char *symname
)
3139 if (ordered_compare
== nullptr)
3141 return ordered_compare (symname
, match_name
) == 0;
3144 dw2_expand_symtabs_matching_symbol (index
, name
, matcher
,
3145 [&] (offset_type namei
)
3147 struct dw2_symtab_iterator iter
;
3148 struct dwarf2_per_cu_data
*per_cu
;
3150 dw2_symtab_iter_init (&iter
, per_objfile
, block_kind
, domain
, namei
,
3152 while ((per_cu
= dw2_symtab_iter_next (&iter
, index
)) != NULL
)
3153 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
3159 /* Starting from a search name, return the string that finds the upper
3160 bound of all strings that start with SEARCH_NAME in a sorted name
3161 list. Returns the empty string to indicate that the upper bound is
3162 the end of the list. */
3165 make_sort_after_prefix_name (const char *search_name
)
3167 /* When looking to complete "func", we find the upper bound of all
3168 symbols that start with "func" by looking for where we'd insert
3169 the closest string that would follow "func" in lexicographical
3170 order. Usually, that's "func"-with-last-character-incremented,
3171 i.e. "fund". Mind non-ASCII characters, though. Usually those
3172 will be UTF-8 multi-byte sequences, but we can't be certain.
3173 Especially mind the 0xff character, which is a valid character in
3174 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3175 rule out compilers allowing it in identifiers. Note that
3176 conveniently, strcmp/strcasecmp are specified to compare
3177 characters interpreted as unsigned char. So what we do is treat
3178 the whole string as a base 256 number composed of a sequence of
3179 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3180 to 0, and carries 1 to the following more-significant position.
3181 If the very first character in SEARCH_NAME ends up incremented
3182 and carries/overflows, then the upper bound is the end of the
3183 list. The string after the empty string is also the empty
3186 Some examples of this operation:
3188 SEARCH_NAME => "+1" RESULT
3192 "\xff" "a" "\xff" => "\xff" "b"
3197 Then, with these symbols for example:
3203 completing "func" looks for symbols between "func" and
3204 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3205 which finds "func" and "func1", but not "fund".
3209 funcÿ (Latin1 'ÿ' [0xff])
3213 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3214 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3218 ÿÿ (Latin1 'ÿ' [0xff])
3221 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3222 the end of the list.
3224 std::string after
= search_name
;
3225 while (!after
.empty () && (unsigned char) after
.back () == 0xff)
3227 if (!after
.empty ())
3228 after
.back () = (unsigned char) after
.back () + 1;
3232 /* See declaration. */
3234 std::pair
<std::vector
<name_component
>::const_iterator
,
3235 std::vector
<name_component
>::const_iterator
>
3236 mapped_index_base::find_name_components_bounds
3237 (const lookup_name_info
&lookup_name_without_params
, language lang
,
3238 dwarf2_per_objfile
*per_objfile
) const
3241 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3243 const char *lang_name
3244 = lookup_name_without_params
.language_lookup_name (lang
);
3246 /* Comparison function object for lower_bound that matches against a
3247 given symbol name. */
3248 auto lookup_compare_lower
= [&] (const name_component
&elem
,
3251 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3252 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3253 return name_cmp (elem_name
, name
) < 0;
3256 /* Comparison function object for upper_bound that matches against a
3257 given symbol name. */
3258 auto lookup_compare_upper
= [&] (const char *name
,
3259 const name_component
&elem
)
3261 const char *elem_qualified
= this->symbol_name_at (elem
.idx
, per_objfile
);
3262 const char *elem_name
= elem_qualified
+ elem
.name_offset
;
3263 return name_cmp (name
, elem_name
) < 0;
3266 auto begin
= this->name_components
.begin ();
3267 auto end
= this->name_components
.end ();
3269 /* Find the lower bound. */
3272 if (lookup_name_without_params
.completion_mode () && lang_name
[0] == '\0')
3275 return std::lower_bound (begin
, end
, lang_name
, lookup_compare_lower
);
3278 /* Find the upper bound. */
3281 if (lookup_name_without_params
.completion_mode ())
3283 /* In completion mode, we want UPPER to point past all
3284 symbols names that have the same prefix. I.e., with
3285 these symbols, and completing "func":
3287 function << lower bound
3289 other_function << upper bound
3291 We find the upper bound by looking for the insertion
3292 point of "func"-with-last-character-incremented,
3294 std::string after
= make_sort_after_prefix_name (lang_name
);
3297 return std::lower_bound (lower
, end
, after
.c_str (),
3298 lookup_compare_lower
);
3301 return std::upper_bound (lower
, end
, lang_name
, lookup_compare_upper
);
3304 return {lower
, upper
};
3307 /* See declaration. */
3310 mapped_index_base::build_name_components (dwarf2_per_objfile
*per_objfile
)
3312 if (!this->name_components
.empty ())
3315 this->name_components_casing
= case_sensitivity
;
3317 = this->name_components_casing
== case_sensitive_on
? strcmp
: strcasecmp
;
3319 /* The code below only knows how to break apart components of C++
3320 symbol names (and other languages that use '::' as
3321 namespace/module separator) and Ada symbol names. */
3322 auto count
= this->symbol_name_count ();
3323 for (offset_type idx
= 0; idx
< count
; idx
++)
3325 if (this->symbol_name_slot_invalid (idx
))
3328 const char *name
= this->symbol_name_at (idx
, per_objfile
);
3330 /* Add each name component to the name component table. */
3331 unsigned int previous_len
= 0;
3333 if (strstr (name
, "::") != nullptr)
3335 for (unsigned int current_len
= cp_find_first_component (name
);
3336 name
[current_len
] != '\0';
3337 current_len
+= cp_find_first_component (name
+ current_len
))
3339 gdb_assert (name
[current_len
] == ':');
3340 this->name_components
.push_back ({previous_len
, idx
});
3341 /* Skip the '::'. */
3343 previous_len
= current_len
;
3348 /* Handle the Ada encoded (aka mangled) form here. */
3349 for (const char *iter
= strstr (name
, "__");
3351 iter
= strstr (iter
, "__"))
3353 this->name_components
.push_back ({previous_len
, idx
});
3355 previous_len
= iter
- name
;
3359 this->name_components
.push_back ({previous_len
, idx
});
3362 /* Sort name_components elements by name. */
3363 auto name_comp_compare
= [&] (const name_component
&left
,
3364 const name_component
&right
)
3366 const char *left_qualified
3367 = this->symbol_name_at (left
.idx
, per_objfile
);
3368 const char *right_qualified
3369 = this->symbol_name_at (right
.idx
, per_objfile
);
3371 const char *left_name
= left_qualified
+ left
.name_offset
;
3372 const char *right_name
= right_qualified
+ right
.name_offset
;
3374 return name_cmp (left_name
, right_name
) < 0;
3377 std::sort (this->name_components
.begin (),
3378 this->name_components
.end (),
3382 /* Helper for dw2_expand_symtabs_matching that works with a
3383 mapped_index_base instead of the containing objfile. This is split
3384 to a separate function in order to be able to unit test the
3385 name_components matching using a mock mapped_index_base. For each
3386 symbol name that matches, calls MATCH_CALLBACK, passing it the
3387 symbol's index in the mapped_index_base symbol table. */
3390 dw2_expand_symtabs_matching_symbol
3391 (mapped_index_base
&index
,
3392 const lookup_name_info
&lookup_name_in
,
3393 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
3394 gdb::function_view
<bool (offset_type
)> match_callback
,
3395 dwarf2_per_objfile
*per_objfile
)
3397 lookup_name_info lookup_name_without_params
3398 = lookup_name_in
.make_ignore_params ();
3400 /* Build the symbol name component sorted vector, if we haven't
3402 index
.build_name_components (per_objfile
);
3404 /* The same symbol may appear more than once in the range though.
3405 E.g., if we're looking for symbols that complete "w", and we have
3406 a symbol named "w1::w2", we'll find the two name components for
3407 that same symbol in the range. To be sure we only call the
3408 callback once per symbol, we first collect the symbol name
3409 indexes that matched in a temporary vector and ignore
3411 std::vector
<offset_type
> matches
;
3413 struct name_and_matcher
3415 symbol_name_matcher_ftype
*matcher
;
3418 bool operator== (const name_and_matcher
&other
) const
3420 return matcher
== other
.matcher
&& strcmp (name
, other
.name
) == 0;
3424 /* A vector holding all the different symbol name matchers, for all
3426 std::vector
<name_and_matcher
> matchers
;
3428 for (int i
= 0; i
< nr_languages
; i
++)
3430 enum language lang_e
= (enum language
) i
;
3432 const language_defn
*lang
= language_def (lang_e
);
3433 symbol_name_matcher_ftype
*name_matcher
3434 = lang
->get_symbol_name_matcher (lookup_name_without_params
);
3436 name_and_matcher key
{
3438 lookup_name_without_params
.language_lookup_name (lang_e
)
3441 /* Don't insert the same comparison routine more than once.
3442 Note that we do this linear walk. This is not a problem in
3443 practice because the number of supported languages is
3445 if (std::find (matchers
.begin (), matchers
.end (), key
)
3448 matchers
.push_back (std::move (key
));
3451 = index
.find_name_components_bounds (lookup_name_without_params
,
3452 lang_e
, per_objfile
);
3454 /* Now for each symbol name in range, check to see if we have a name
3455 match, and if so, call the MATCH_CALLBACK callback. */
3457 for (; bounds
.first
!= bounds
.second
; ++bounds
.first
)
3459 const char *qualified
3460 = index
.symbol_name_at (bounds
.first
->idx
, per_objfile
);
3462 if (!name_matcher (qualified
, lookup_name_without_params
, NULL
)
3463 || (symbol_matcher
!= NULL
&& !symbol_matcher (qualified
)))
3466 matches
.push_back (bounds
.first
->idx
);
3470 std::sort (matches
.begin (), matches
.end ());
3472 /* Finally call the callback, once per match. */
3475 for (offset_type idx
: matches
)
3479 if (!match_callback (idx
))
3488 /* Above we use a type wider than idx's for 'prev', since 0 and
3489 (offset_type)-1 are both possible values. */
3490 static_assert (sizeof (prev
) > sizeof (offset_type
), "");
3497 namespace selftests
{ namespace dw2_expand_symtabs_matching
{
3499 /* A mock .gdb_index/.debug_names-like name index table, enough to
3500 exercise dw2_expand_symtabs_matching_symbol, which works with the
3501 mapped_index_base interface. Builds an index from the symbol list
3502 passed as parameter to the constructor. */
3503 class mock_mapped_index
: public mapped_index_base
3506 mock_mapped_index (gdb::array_view
<const char *> symbols
)
3507 : m_symbol_table (symbols
)
3510 DISABLE_COPY_AND_ASSIGN (mock_mapped_index
);
3512 /* Return the number of names in the symbol table. */
3513 size_t symbol_name_count () const override
3515 return m_symbol_table
.size ();
3518 /* Get the name of the symbol at IDX in the symbol table. */
3519 const char *symbol_name_at
3520 (offset_type idx
, dwarf2_per_objfile
*per_objfile
) const override
3522 return m_symbol_table
[idx
];
3525 quick_symbol_functions_up
make_quick_functions () const override
3531 gdb::array_view
<const char *> m_symbol_table
;
3534 /* Convenience function that converts a NULL pointer to a "<null>"
3535 string, to pass to print routines. */
3538 string_or_null (const char *str
)
3540 return str
!= NULL
? str
: "<null>";
3543 /* Check if a lookup_name_info built from
3544 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3545 index. EXPECTED_LIST is the list of expected matches, in expected
3546 matching order. If no match expected, then an empty list is
3547 specified. Returns true on success. On failure prints a warning
3548 indicating the file:line that failed, and returns false. */
3551 check_match (const char *file
, int line
,
3552 mock_mapped_index
&mock_index
,
3553 const char *name
, symbol_name_match_type match_type
,
3554 bool completion_mode
,
3555 std::initializer_list
<const char *> expected_list
,
3556 dwarf2_per_objfile
*per_objfile
)
3558 lookup_name_info
lookup_name (name
, match_type
, completion_mode
);
3560 bool matched
= true;
3562 auto mismatch
= [&] (const char *expected_str
,
3565 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3566 "expected=\"%s\", got=\"%s\"\n"),
3568 (match_type
== symbol_name_match_type::FULL
3570 name
, string_or_null (expected_str
), string_or_null (got
));
3574 auto expected_it
= expected_list
.begin ();
3575 auto expected_end
= expected_list
.end ();
3577 dw2_expand_symtabs_matching_symbol (mock_index
, lookup_name
,
3579 [&] (offset_type idx
)
3581 const char *matched_name
= mock_index
.symbol_name_at (idx
, per_objfile
);
3582 const char *expected_str
3583 = expected_it
== expected_end
? NULL
: *expected_it
++;
3585 if (expected_str
== NULL
|| strcmp (expected_str
, matched_name
) != 0)
3586 mismatch (expected_str
, matched_name
);
3590 const char *expected_str
3591 = expected_it
== expected_end
? NULL
: *expected_it
++;
3592 if (expected_str
!= NULL
)
3593 mismatch (expected_str
, NULL
);
3598 /* The symbols added to the mock mapped_index for testing (in
3600 static const char *test_symbols
[] = {
3609 "ns2::tmpl<int>::foo2",
3610 "(anonymous namespace)::A::B::C",
3612 /* These are used to check that the increment-last-char in the
3613 matching algorithm for completion doesn't match "t1_fund" when
3614 completing "t1_func". */
3620 /* A UTF-8 name with multi-byte sequences to make sure that
3621 cp-name-parser understands this as a single identifier ("função"
3622 is "function" in PT). */
3625 /* \377 (0xff) is Latin1 'ÿ'. */
3628 /* \377 (0xff) is Latin1 'ÿ'. */
3632 /* A name with all sorts of complications. Starts with "z" to make
3633 it easier for the completion tests below. */
3634 #define Z_SYM_NAME \
3635 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
3636 "::tuple<(anonymous namespace)::ui*, " \
3637 "std::default_delete<(anonymous namespace)::ui>, void>"
3642 /* Returns true if the mapped_index_base::find_name_component_bounds
3643 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
3644 in completion mode. */
3647 check_find_bounds_finds (mapped_index_base
&index
,
3648 const char *search_name
,
3649 gdb::array_view
<const char *> expected_syms
,
3650 dwarf2_per_objfile
*per_objfile
)
3652 lookup_name_info
lookup_name (search_name
,
3653 symbol_name_match_type::FULL
, true);
3655 auto bounds
= index
.find_name_components_bounds (lookup_name
,
3659 size_t distance
= std::distance (bounds
.first
, bounds
.second
);
3660 if (distance
!= expected_syms
.size ())
3663 for (size_t exp_elem
= 0; exp_elem
< distance
; exp_elem
++)
3665 auto nc_elem
= bounds
.first
+ exp_elem
;
3666 const char *qualified
= index
.symbol_name_at (nc_elem
->idx
, per_objfile
);
3667 if (strcmp (qualified
, expected_syms
[exp_elem
]) != 0)
3674 /* Test the lower-level mapped_index::find_name_component_bounds
3678 test_mapped_index_find_name_component_bounds ()
3680 mock_mapped_index
mock_index (test_symbols
);
3682 mock_index
.build_name_components (NULL
/* per_objfile */);
3684 /* Test the lower-level mapped_index::find_name_component_bounds
3685 method in completion mode. */
3687 static const char *expected_syms
[] = {
3692 SELF_CHECK (check_find_bounds_finds
3693 (mock_index
, "t1_func", expected_syms
,
3694 NULL
/* per_objfile */));
3697 /* Check that the increment-last-char in the name matching algorithm
3698 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
3700 static const char *expected_syms1
[] = {
3704 SELF_CHECK (check_find_bounds_finds
3705 (mock_index
, "\377", expected_syms1
, NULL
/* per_objfile */));
3707 static const char *expected_syms2
[] = {
3710 SELF_CHECK (check_find_bounds_finds
3711 (mock_index
, "\377\377", expected_syms2
,
3712 NULL
/* per_objfile */));
3716 /* Test dw2_expand_symtabs_matching_symbol. */
3719 test_dw2_expand_symtabs_matching_symbol ()
3721 mock_mapped_index
mock_index (test_symbols
);
3723 /* We let all tests run until the end even if some fails, for debug
3725 bool any_mismatch
= false;
3727 /* Create the expected symbols list (an initializer_list). Needed
3728 because lists have commas, and we need to pass them to CHECK,
3729 which is a macro. */
3730 #define EXPECT(...) { __VA_ARGS__ }
3732 /* Wrapper for check_match that passes down the current
3733 __FILE__/__LINE__. */
3734 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
3735 any_mismatch |= !check_match (__FILE__, __LINE__, \
3737 NAME, MATCH_TYPE, COMPLETION_MODE, \
3738 EXPECTED_LIST, NULL)
3740 /* Identity checks. */
3741 for (const char *sym
: test_symbols
)
3743 /* Should be able to match all existing symbols. */
3744 CHECK_MATCH (sym
, symbol_name_match_type::FULL
, false,
3747 /* Should be able to match all existing symbols with
3749 std::string with_params
= std::string (sym
) + "(int)";
3750 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3753 /* Should be able to match all existing symbols with
3754 parameters and qualifiers. */
3755 with_params
= std::string (sym
) + " ( int ) const";
3756 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3759 /* This should really find sym, but cp-name-parser.y doesn't
3760 know about lvalue/rvalue qualifiers yet. */
3761 with_params
= std::string (sym
) + " ( int ) &&";
3762 CHECK_MATCH (with_params
.c_str (), symbol_name_match_type::FULL
, false,
3766 /* Check that the name matching algorithm for completion doesn't get
3767 confused with Latin1 'ÿ' / 0xff. */
3769 static const char str
[] = "\377";
3770 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3771 EXPECT ("\377", "\377\377123"));
3774 /* Check that the increment-last-char in the matching algorithm for
3775 completion doesn't match "t1_fund" when completing "t1_func". */
3777 static const char str
[] = "t1_func";
3778 CHECK_MATCH (str
, symbol_name_match_type::FULL
, true,
3779 EXPECT ("t1_func", "t1_func1"));
3782 /* Check that completion mode works at each prefix of the expected
3785 static const char str
[] = "function(int)";
3786 size_t len
= strlen (str
);
3789 for (size_t i
= 1; i
< len
; i
++)
3791 lookup
.assign (str
, i
);
3792 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3793 EXPECT ("function"));
3797 /* While "w" is a prefix of both components, the match function
3798 should still only be called once. */
3800 CHECK_MATCH ("w", symbol_name_match_type::FULL
, true,
3802 CHECK_MATCH ("w", symbol_name_match_type::WILD
, true,
3806 /* Same, with a "complicated" symbol. */
3808 static const char str
[] = Z_SYM_NAME
;
3809 size_t len
= strlen (str
);
3812 for (size_t i
= 1; i
< len
; i
++)
3814 lookup
.assign (str
, i
);
3815 CHECK_MATCH (lookup
.c_str (), symbol_name_match_type::FULL
, true,
3816 EXPECT (Z_SYM_NAME
));
3820 /* In FULL mode, an incomplete symbol doesn't match. */
3822 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL
, false,
3826 /* A complete symbol with parameters matches any overload, since the
3827 index has no overload info. */
3829 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL
, true,
3830 EXPECT ("std::zfunction", "std::zfunction2"));
3831 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD
, true,
3832 EXPECT ("std::zfunction", "std::zfunction2"));
3833 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD
, true,
3834 EXPECT ("std::zfunction", "std::zfunction2"));
3837 /* Check that whitespace is ignored appropriately. A symbol with a
3838 template argument list. */
3840 static const char expected
[] = "ns::foo<int>";
3841 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL
, false,
3843 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD
, false,
3847 /* Check that whitespace is ignored appropriately. A symbol with a
3848 template argument list that includes a pointer. */
3850 static const char expected
[] = "ns::foo<char*>";
3851 /* Try both completion and non-completion modes. */
3852 static const bool completion_mode
[2] = {false, true};
3853 for (size_t i
= 0; i
< 2; i
++)
3855 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL
,
3856 completion_mode
[i
], EXPECT (expected
));
3857 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD
,
3858 completion_mode
[i
], EXPECT (expected
));
3860 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL
,
3861 completion_mode
[i
], EXPECT (expected
));
3862 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD
,
3863 completion_mode
[i
], EXPECT (expected
));
3868 /* Check method qualifiers are ignored. */
3869 static const char expected
[] = "ns::foo<char*>";
3870 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
3871 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3872 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
3873 symbol_name_match_type::FULL
, true, EXPECT (expected
));
3874 CHECK_MATCH ("foo < char * > ( int ) const",
3875 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3876 CHECK_MATCH ("foo < char * > ( int ) &&",
3877 symbol_name_match_type::WILD
, true, EXPECT (expected
));
3880 /* Test lookup names that don't match anything. */
3882 CHECK_MATCH ("bar2", symbol_name_match_type::WILD
, false,
3885 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL
, false,
3889 /* Some wild matching tests, exercising "(anonymous namespace)",
3890 which should not be confused with a parameter list. */
3892 static const char *syms
[] = {
3896 "A :: B :: C ( int )",
3901 for (const char *s
: syms
)
3903 CHECK_MATCH (s
, symbol_name_match_type::WILD
, false,
3904 EXPECT ("(anonymous namespace)::A::B::C"));
3909 static const char expected
[] = "ns2::tmpl<int>::foo2";
3910 CHECK_MATCH ("tmp", symbol_name_match_type::WILD
, true,
3912 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD
, true,
3916 SELF_CHECK (!any_mismatch
);
3925 test_mapped_index_find_name_component_bounds ();
3926 test_dw2_expand_symtabs_matching_symbol ();
3929 }} // namespace selftests::dw2_expand_symtabs_matching
3931 #endif /* GDB_SELF_TEST */
3933 /* If FILE_MATCHER is NULL or if PER_CU has
3934 dwarf2_per_cu_quick_data::MARK set (see
3935 dw_expand_symtabs_matching_file_matcher), expand the CU and call
3936 EXPANSION_NOTIFY on it. */
3939 dw2_expand_symtabs_matching_one
3940 (dwarf2_per_cu_data
*per_cu
,
3941 dwarf2_per_objfile
*per_objfile
,
3942 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3943 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
)
3945 if (file_matcher
== NULL
|| per_cu
->mark
)
3947 bool symtab_was_null
= !per_objfile
->symtab_set_p (per_cu
);
3949 compunit_symtab
*symtab
3950 = dw2_instantiate_symtab (per_cu
, per_objfile
, false);
3951 gdb_assert (symtab
!= nullptr);
3953 if (expansion_notify
!= NULL
&& symtab_was_null
)
3954 return expansion_notify (symtab
);
3959 /* Helper for dw2_expand_matching symtabs. Called on each symbol
3960 matched, to expand corresponding CUs that were marked. IDX is the
3961 index of the symbol name that matched. */
3964 dw2_expand_marked_cus
3965 (dwarf2_per_objfile
*per_objfile
, offset_type idx
,
3966 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
3967 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
3968 block_search_flags search_flags
,
3971 offset_type vec_len
, vec_idx
;
3972 bool global_seen
= false;
3974 = (static_cast<mapped_index
&>
3975 (*per_objfile
->per_bfd
->index_table
.get ()));
3977 offset_view
vec (index
.constant_pool
.slice (index
.symbol_vec_index (idx
)));
3979 for (vec_idx
= 0; vec_idx
< vec_len
; ++vec_idx
)
3981 offset_type cu_index_and_attrs
= vec
[vec_idx
+ 1];
3982 /* This value is only valid for index versions >= 7. */
3983 int is_static
= GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs
);
3984 gdb_index_symbol_kind symbol_kind
=
3985 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs
);
3986 int cu_index
= GDB_INDEX_CU_VALUE (cu_index_and_attrs
);
3987 /* Only check the symbol attributes if they're present.
3988 Indices prior to version 7 don't record them,
3989 and indices >= 7 may elide them for certain symbols
3990 (gold does this). */
3993 && symbol_kind
!= GDB_INDEX_SYMBOL_KIND_NONE
);
3995 /* Work around gold/15646. */
3998 && symbol_kind
== GDB_INDEX_SYMBOL_KIND_TYPE
)
4006 /* Only check the symbol's kind if it has one. */
4011 if ((search_flags
& SEARCH_STATIC_BLOCK
) == 0)
4016 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) == 0)
4022 case VARIABLES_DOMAIN
:
4023 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_VARIABLE
)
4026 case FUNCTIONS_DOMAIN
:
4027 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_FUNCTION
)
4031 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_TYPE
)
4034 case MODULES_DOMAIN
:
4035 if (symbol_kind
!= GDB_INDEX_SYMBOL_KIND_OTHER
)
4043 /* Don't crash on bad data. */
4044 if (cu_index
>= per_objfile
->per_bfd
->all_comp_units
.size ())
4046 complaint (_(".gdb_index entry has bad CU index"
4047 " [in module %s]"), objfile_name (per_objfile
->objfile
));
4051 dwarf2_per_cu_data
*per_cu
= per_objfile
->per_bfd
->get_cu (cu_index
);
4052 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, file_matcher
,
4060 /* If FILE_MATCHER is non-NULL, set all the
4061 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4062 that match FILE_MATCHER. */
4065 dw_expand_symtabs_matching_file_matcher
4066 (dwarf2_per_objfile
*per_objfile
,
4067 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
)
4069 if (file_matcher
== NULL
)
4072 htab_up
visited_found (htab_create_alloc (10, htab_hash_pointer
,
4074 NULL
, xcalloc
, xfree
));
4075 htab_up
visited_not_found (htab_create_alloc (10, htab_hash_pointer
,
4077 NULL
, xcalloc
, xfree
));
4079 /* The rule is CUs specify all the files, including those used by
4080 any TU, so there's no need to scan TUs here. */
4082 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4086 if (per_cu
->is_debug_types
)
4090 /* We only need to look at symtabs not already expanded. */
4091 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4094 if (per_cu
->fnd
!= nullptr)
4096 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4098 if (file_matcher (fnd
->get_name (), false))
4104 /* Before we invoke realpath, which can get expensive when many
4105 files are involved, do a quick comparison of the basenames. */
4106 if ((basenames_may_differ
4107 || file_matcher (lbasename (fnd
->get_name ()), true))
4108 && file_matcher (fnd
->get_fullname (), false))
4115 quick_file_names
*file_data
= dw2_get_file_names (per_cu
.get (),
4117 if (file_data
== NULL
)
4120 if (htab_find (visited_not_found
.get (), file_data
) != NULL
)
4122 else if (htab_find (visited_found
.get (), file_data
) != NULL
)
4128 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4130 const char *this_real_name
;
4132 if (file_matcher (file_data
->file_names
[j
], false))
4138 /* Before we invoke realpath, which can get expensive when many
4139 files are involved, do a quick comparison of the basenames. */
4140 if (!basenames_may_differ
4141 && !file_matcher (lbasename (file_data
->file_names
[j
]),
4145 this_real_name
= dw2_get_real_path (per_objfile
, file_data
, j
);
4146 if (file_matcher (this_real_name
, false))
4153 void **slot
= htab_find_slot (per_cu
->mark
4154 ? visited_found
.get ()
4155 : visited_not_found
.get (),
4162 dwarf2_gdb_index::expand_symtabs_matching
4163 (struct objfile
*objfile
,
4164 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
4165 const lookup_name_info
*lookup_name
,
4166 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
4167 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
4168 block_search_flags search_flags
,
4170 enum search_domain kind
)
4172 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4174 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
4176 /* This invariant is documented in quick-functions.h. */
4177 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
4178 if (lookup_name
== nullptr)
4180 for (dwarf2_per_cu_data
*per_cu
4181 : all_comp_units_range (per_objfile
->per_bfd
))
4185 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
4194 = (static_cast<mapped_index
&>
4195 (*per_objfile
->per_bfd
->index_table
.get ()));
4198 = dw2_expand_symtabs_matching_symbol (index
, *lookup_name
,
4200 [&] (offset_type idx
)
4202 if (!dw2_expand_marked_cus (per_objfile
, idx
, file_matcher
,
4203 expansion_notify
, search_flags
, kind
))
4211 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4214 static struct compunit_symtab
*
4215 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab
*cust
,
4220 if (cust
->blockvector () != nullptr
4221 && blockvector_contains_pc (cust
->blockvector (), pc
))
4224 if (cust
->includes
== NULL
)
4227 for (i
= 0; cust
->includes
[i
]; ++i
)
4229 struct compunit_symtab
*s
= cust
->includes
[i
];
4231 s
= recursively_find_pc_sect_compunit_symtab (s
, pc
);
4239 struct compunit_symtab
*
4240 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4241 (struct objfile
*objfile
,
4242 struct bound_minimal_symbol msymbol
,
4244 struct obj_section
*section
,
4247 struct dwarf2_per_cu_data
*data
;
4248 struct compunit_symtab
*result
;
4250 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4251 if (per_objfile
->per_bfd
->index_addrmap
== nullptr)
4254 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
4255 data
= ((struct dwarf2_per_cu_data
*)
4256 addrmap_find (per_objfile
->per_bfd
->index_addrmap
,
4261 if (warn_if_readin
&& per_objfile
->symtab_set_p (data
))
4262 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4263 paddress (objfile
->arch (), pc
));
4265 result
= recursively_find_pc_sect_compunit_symtab
4266 (dw2_instantiate_symtab (data
, per_objfile
, false), pc
);
4268 gdb_assert (result
!= NULL
);
4273 dwarf2_base_index_functions::map_symbol_filenames
4274 (struct objfile
*objfile
,
4275 gdb::function_view
<symbol_filename_ftype
> fun
,
4278 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4280 /* Use caches to ensure we only call FUN once for each filename. */
4281 filename_seen_cache filenames_cache
;
4282 std::unordered_set
<quick_file_names
*> qfn_cache
;
4284 /* The rule is CUs specify all the files, including those used by any TU,
4285 so there's no need to scan TUs here. We can ignore file names coming
4286 from already-expanded CUs. It is possible that an expanded CU might
4287 reuse the file names data from a currently unexpanded CU, in this
4288 case we don't want to report the files from the unexpanded CU. */
4290 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4292 if (!per_cu
->is_debug_types
4293 && per_objfile
->symtab_set_p (per_cu
.get ()))
4295 if (per_cu
->file_names
!= nullptr)
4296 qfn_cache
.insert (per_cu
->file_names
);
4300 for (dwarf2_per_cu_data
*per_cu
4301 : all_comp_units_range (per_objfile
->per_bfd
))
4303 /* We only need to look at symtabs not already expanded. */
4304 if (per_cu
->is_debug_types
|| per_objfile
->symtab_set_p (per_cu
))
4307 if (per_cu
->fnd
!= nullptr)
4309 file_and_directory
*fnd
= per_cu
->fnd
.get ();
4311 const char *filename
= fnd
->get_name ();
4312 const char *key
= filename
;
4313 const char *fullname
= nullptr;
4317 fullname
= fnd
->get_fullname ();
4321 if (!filenames_cache
.seen (key
))
4322 fun (filename
, fullname
);
4325 quick_file_names
*file_data
= dw2_get_file_names (per_cu
, per_objfile
);
4326 if (file_data
== nullptr
4327 || qfn_cache
.find (file_data
) != qfn_cache
.end ())
4330 for (int j
= 0; j
< file_data
->num_file_names
; ++j
)
4332 const char *filename
= file_data
->file_names
[j
];
4333 const char *key
= filename
;
4334 const char *fullname
= nullptr;
4338 fullname
= dw2_get_real_path (per_objfile
, file_data
, j
);
4342 if (!filenames_cache
.seen (key
))
4343 fun (filename
, fullname
);
4349 dwarf2_base_index_functions::has_symbols (struct objfile
*objfile
)
4354 /* See quick_symbol_functions::has_unexpanded_symtabs in quick-symbol.h. */
4357 dwarf2_base_index_functions::has_unexpanded_symtabs (struct objfile
*objfile
)
4359 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
4361 for (const auto &per_cu
: per_objfile
->per_bfd
->all_comp_units
)
4363 /* Is this already expanded? */
4364 if (per_objfile
->symtab_set_p (per_cu
.get ()))
4367 /* It has not yet been expanded. */
4374 /* DWARF-5 debug_names reader. */
4376 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4377 static const gdb_byte dwarf5_augmentation
[] = { 'G', 'D', 'B', 0 };
4379 /* A helper function that reads the .debug_names section in SECTION
4380 and fills in MAP. FILENAME is the name of the file containing the
4381 section; it is used for error reporting.
4383 Returns true if all went well, false otherwise. */
4386 read_debug_names_from_section (struct objfile
*objfile
,
4387 const char *filename
,
4388 struct dwarf2_section_info
*section
,
4389 mapped_debug_names
&map
)
4391 if (section
->empty ())
4394 /* Older elfutils strip versions could keep the section in the main
4395 executable while splitting it for the separate debug info file. */
4396 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
4399 section
->read (objfile
);
4401 map
.dwarf5_byte_order
= gdbarch_byte_order (objfile
->arch ());
4403 const gdb_byte
*addr
= section
->buffer
;
4405 bfd
*const abfd
= section
->get_bfd_owner ();
4407 unsigned int bytes_read
;
4408 LONGEST length
= read_initial_length (abfd
, addr
, &bytes_read
);
4411 map
.dwarf5_is_dwarf64
= bytes_read
!= 4;
4412 map
.offset_size
= map
.dwarf5_is_dwarf64
? 8 : 4;
4413 if (bytes_read
+ length
!= section
->size
)
4415 /* There may be multiple per-CU indices. */
4416 warning (_("Section .debug_names in %s length %s does not match "
4417 "section length %s, ignoring .debug_names."),
4418 filename
, plongest (bytes_read
+ length
),
4419 pulongest (section
->size
));
4423 /* The version number. */
4424 uint16_t version
= read_2_bytes (abfd
, addr
);
4428 warning (_("Section .debug_names in %s has unsupported version %d, "
4429 "ignoring .debug_names."),
4435 uint16_t padding
= read_2_bytes (abfd
, addr
);
4439 warning (_("Section .debug_names in %s has unsupported padding %d, "
4440 "ignoring .debug_names."),
4445 /* comp_unit_count - The number of CUs in the CU list. */
4446 map
.cu_count
= read_4_bytes (abfd
, addr
);
4449 /* local_type_unit_count - The number of TUs in the local TU
4451 map
.tu_count
= read_4_bytes (abfd
, addr
);
4454 /* foreign_type_unit_count - The number of TUs in the foreign TU
4456 uint32_t foreign_tu_count
= read_4_bytes (abfd
, addr
);
4458 if (foreign_tu_count
!= 0)
4460 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4461 "ignoring .debug_names."),
4462 filename
, static_cast<unsigned long> (foreign_tu_count
));
4466 /* bucket_count - The number of hash buckets in the hash lookup
4468 map
.bucket_count
= read_4_bytes (abfd
, addr
);
4471 /* name_count - The number of unique names in the index. */
4472 map
.name_count
= read_4_bytes (abfd
, addr
);
4475 /* abbrev_table_size - The size in bytes of the abbreviations
4477 uint32_t abbrev_table_size
= read_4_bytes (abfd
, addr
);
4480 /* augmentation_string_size - The size in bytes of the augmentation
4481 string. This value is rounded up to a multiple of 4. */
4482 uint32_t augmentation_string_size
= read_4_bytes (abfd
, addr
);
4484 map
.augmentation_is_gdb
= ((augmentation_string_size
4485 == sizeof (dwarf5_augmentation
))
4486 && memcmp (addr
, dwarf5_augmentation
,
4487 sizeof (dwarf5_augmentation
)) == 0);
4488 augmentation_string_size
+= (-augmentation_string_size
) & 3;
4489 addr
+= augmentation_string_size
;
4492 map
.cu_table_reordered
= addr
;
4493 addr
+= map
.cu_count
* map
.offset_size
;
4495 /* List of Local TUs */
4496 map
.tu_table_reordered
= addr
;
4497 addr
+= map
.tu_count
* map
.offset_size
;
4499 /* Hash Lookup Table */
4500 map
.bucket_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4501 addr
+= map
.bucket_count
* 4;
4502 map
.hash_table_reordered
= reinterpret_cast<const uint32_t *> (addr
);
4503 addr
+= map
.name_count
* 4;
4506 map
.name_table_string_offs_reordered
= addr
;
4507 addr
+= map
.name_count
* map
.offset_size
;
4508 map
.name_table_entry_offs_reordered
= addr
;
4509 addr
+= map
.name_count
* map
.offset_size
;
4511 const gdb_byte
*abbrev_table_start
= addr
;
4514 const ULONGEST index_num
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4519 const auto insertpair
4520 = map
.abbrev_map
.emplace (index_num
, mapped_debug_names::index_val ());
4521 if (!insertpair
.second
)
4523 warning (_("Section .debug_names in %s has duplicate index %s, "
4524 "ignoring .debug_names."),
4525 filename
, pulongest (index_num
));
4528 mapped_debug_names::index_val
&indexval
= insertpair
.first
->second
;
4529 indexval
.dwarf_tag
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4534 mapped_debug_names::index_val::attr attr
;
4535 attr
.dw_idx
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4537 attr
.form
= read_unsigned_leb128 (abfd
, addr
, &bytes_read
);
4539 if (attr
.form
== DW_FORM_implicit_const
)
4541 attr
.implicit_const
= read_signed_leb128 (abfd
, addr
,
4545 if (attr
.dw_idx
== 0 && attr
.form
== 0)
4547 indexval
.attr_vec
.push_back (std::move (attr
));
4550 if (addr
!= abbrev_table_start
+ abbrev_table_size
)
4552 warning (_("Section .debug_names in %s has abbreviation_table "
4553 "of size %s vs. written as %u, ignoring .debug_names."),
4554 filename
, plongest (addr
- abbrev_table_start
),
4558 map
.entry_pool
= addr
;
4563 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4567 create_cus_from_debug_names_list (dwarf2_per_bfd
*per_bfd
,
4568 const mapped_debug_names
&map
,
4569 dwarf2_section_info
§ion
,
4572 if (!map
.augmentation_is_gdb
)
4574 for (uint32_t i
= 0; i
< map
.cu_count
; ++i
)
4576 sect_offset sect_off
4577 = (sect_offset
) (extract_unsigned_integer
4578 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4580 map
.dwarf5_byte_order
));
4581 /* We don't know the length of the CU, because the CU list in a
4582 .debug_names index can be incomplete, so we can't use the start
4583 of the next CU as end of this CU. We create the CUs here with
4584 length 0, and in cutu_reader::cutu_reader we'll fill in the
4586 dwarf2_per_cu_data_up per_cu
4587 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4589 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4594 sect_offset sect_off_prev
;
4595 for (uint32_t i
= 0; i
<= map
.cu_count
; ++i
)
4597 sect_offset sect_off_next
;
4598 if (i
< map
.cu_count
)
4601 = (sect_offset
) (extract_unsigned_integer
4602 (map
.cu_table_reordered
+ i
* map
.offset_size
,
4604 map
.dwarf5_byte_order
));
4607 sect_off_next
= (sect_offset
) section
.size
;
4610 const ULONGEST length
= sect_off_next
- sect_off_prev
;
4611 dwarf2_per_cu_data_up per_cu
4612 = create_cu_from_index_list (per_bfd
, §ion
, is_dwz
,
4613 sect_off_prev
, length
);
4614 per_bfd
->all_comp_units
.push_back (std::move (per_cu
));
4616 sect_off_prev
= sect_off_next
;
4620 /* Read the CU list from the mapped index, and use it to create all
4621 the CU objects for this dwarf2_per_objfile. */
4624 create_cus_from_debug_names (dwarf2_per_bfd
*per_bfd
,
4625 const mapped_debug_names
&map
,
4626 const mapped_debug_names
&dwz_map
)
4628 gdb_assert (per_bfd
->all_comp_units
.empty ());
4629 per_bfd
->all_comp_units
.reserve (map
.cu_count
+ dwz_map
.cu_count
);
4631 create_cus_from_debug_names_list (per_bfd
, map
, per_bfd
->info
,
4632 false /* is_dwz */);
4634 if (dwz_map
.cu_count
== 0)
4637 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4638 create_cus_from_debug_names_list (per_bfd
, dwz_map
, dwz
->info
,
4642 /* Read .debug_names. If everything went ok, initialize the "quick"
4643 elements of all the CUs and return true. Otherwise, return false. */
4646 dwarf2_read_debug_names (dwarf2_per_objfile
*per_objfile
)
4648 std::unique_ptr
<mapped_debug_names
> map (new mapped_debug_names
);
4649 mapped_debug_names dwz_map
;
4650 struct objfile
*objfile
= per_objfile
->objfile
;
4651 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
4653 if (!read_debug_names_from_section (objfile
, objfile_name (objfile
),
4654 &per_bfd
->debug_names
, *map
))
4657 /* Don't use the index if it's empty. */
4658 if (map
->name_count
== 0)
4661 /* If there is a .dwz file, read it so we can get its CU list as
4663 dwz_file
*dwz
= dwarf2_get_dwz_file (per_bfd
);
4666 if (!read_debug_names_from_section (objfile
,
4667 bfd_get_filename (dwz
->dwz_bfd
.get ()),
4668 &dwz
->debug_names
, dwz_map
))
4670 warning (_("could not read '.debug_names' section from %s; skipping"),
4671 bfd_get_filename (dwz
->dwz_bfd
.get ()));
4676 create_cus_from_debug_names (per_bfd
, *map
, dwz_map
);
4678 if (map
->tu_count
!= 0)
4680 /* We can only handle a single .debug_types when we have an
4682 if (per_bfd
->types
.size () != 1)
4685 dwarf2_section_info
*section
= &per_bfd
->types
[0];
4687 create_signatured_type_table_from_debug_names
4688 (per_objfile
, *map
, section
, &per_bfd
->abbrev
);
4691 create_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
);
4693 per_bfd
->index_table
= std::move (map
);
4694 per_bfd
->quick_file_names_table
=
4695 create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
4700 /* Type used to manage iterating over all CUs looking for a symbol for
4703 class dw2_debug_names_iterator
4706 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4707 block_search_flags block_index
,
4709 const char *name
, dwarf2_per_objfile
*per_objfile
)
4710 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4711 m_addr (find_vec_in_debug_names (map
, name
, per_objfile
)),
4712 m_per_objfile (per_objfile
)
4715 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4716 search_domain search
, uint32_t namei
,
4717 dwarf2_per_objfile
*per_objfile
,
4718 domain_enum domain
= UNDEF_DOMAIN
)
4722 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4723 m_per_objfile (per_objfile
)
4726 dw2_debug_names_iterator (const mapped_debug_names
&map
,
4727 block_search_flags block_index
, domain_enum domain
,
4728 uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4729 : m_map (map
), m_block_index (block_index
), m_domain (domain
),
4730 m_addr (find_vec_in_debug_names (map
, namei
, per_objfile
)),
4731 m_per_objfile (per_objfile
)
4734 /* Return the next matching CU or NULL if there are no more. */
4735 dwarf2_per_cu_data
*next ();
4738 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4740 dwarf2_per_objfile
*per_objfile
);
4741 static const gdb_byte
*find_vec_in_debug_names (const mapped_debug_names
&map
,
4743 dwarf2_per_objfile
*per_objfile
);
4745 /* The internalized form of .debug_names. */
4746 const mapped_debug_names
&m_map
;
4748 /* Restrict the search to these blocks. */
4749 block_search_flags m_block_index
= (SEARCH_GLOBAL_BLOCK
4750 | SEARCH_STATIC_BLOCK
);
4752 /* The kind of symbol we're looking for. */
4753 const domain_enum m_domain
= UNDEF_DOMAIN
;
4754 const search_domain m_search
= ALL_DOMAIN
;
4756 /* The list of CUs from the index entry of the symbol, or NULL if
4758 const gdb_byte
*m_addr
;
4760 dwarf2_per_objfile
*m_per_objfile
;
4764 mapped_debug_names::namei_to_name
4765 (uint32_t namei
, dwarf2_per_objfile
*per_objfile
) const
4767 const ULONGEST namei_string_offs
4768 = extract_unsigned_integer ((name_table_string_offs_reordered
4769 + namei
* offset_size
),
4772 return read_indirect_string_at_offset (per_objfile
, namei_string_offs
);
4775 /* Find a slot in .debug_names for the object named NAME. If NAME is
4776 found, return pointer to its pool data. If NAME cannot be found,
4780 dw2_debug_names_iterator::find_vec_in_debug_names
4781 (const mapped_debug_names
&map
, const char *name
,
4782 dwarf2_per_objfile
*per_objfile
)
4784 int (*cmp
) (const char *, const char *);
4786 gdb::unique_xmalloc_ptr
<char> without_params
;
4787 if (current_language
->la_language
== language_cplus
4788 || current_language
->la_language
== language_fortran
4789 || current_language
->la_language
== language_d
)
4791 /* NAME is already canonical. Drop any qualifiers as
4792 .debug_names does not contain any. */
4794 if (strchr (name
, '(') != NULL
)
4796 without_params
= cp_remove_params (name
);
4797 if (without_params
!= NULL
)
4798 name
= without_params
.get ();
4802 cmp
= (case_sensitivity
== case_sensitive_on
? strcmp
: strcasecmp
);
4804 const uint32_t full_hash
= dwarf5_djb_hash (name
);
4806 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4807 (map
.bucket_table_reordered
4808 + (full_hash
% map
.bucket_count
)), 4,
4809 map
.dwarf5_byte_order
);
4813 if (namei
>= map
.name_count
)
4815 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4817 namei
, map
.name_count
,
4818 objfile_name (per_objfile
->objfile
));
4824 const uint32_t namei_full_hash
4825 = extract_unsigned_integer (reinterpret_cast<const gdb_byte
*>
4826 (map
.hash_table_reordered
+ namei
), 4,
4827 map
.dwarf5_byte_order
);
4828 if (full_hash
% map
.bucket_count
!= namei_full_hash
% map
.bucket_count
)
4831 if (full_hash
== namei_full_hash
)
4833 const char *const namei_string
= map
.namei_to_name (namei
, per_objfile
);
4835 #if 0 /* An expensive sanity check. */
4836 if (namei_full_hash
!= dwarf5_djb_hash (namei_string
))
4838 complaint (_("Wrong .debug_names hash for string at index %u "
4840 namei
, objfile_name (dwarf2_per_objfile
->objfile
));
4845 if (cmp (namei_string
, name
) == 0)
4847 const ULONGEST namei_entry_offs
4848 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4849 + namei
* map
.offset_size
),
4850 map
.offset_size
, map
.dwarf5_byte_order
);
4851 return map
.entry_pool
+ namei_entry_offs
;
4856 if (namei
>= map
.name_count
)
4862 dw2_debug_names_iterator::find_vec_in_debug_names
4863 (const mapped_debug_names
&map
, uint32_t namei
, dwarf2_per_objfile
*per_objfile
)
4865 if (namei
>= map
.name_count
)
4867 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
4869 namei
, map
.name_count
,
4870 objfile_name (per_objfile
->objfile
));
4874 const ULONGEST namei_entry_offs
4875 = extract_unsigned_integer ((map
.name_table_entry_offs_reordered
4876 + namei
* map
.offset_size
),
4877 map
.offset_size
, map
.dwarf5_byte_order
);
4878 return map
.entry_pool
+ namei_entry_offs
;
4881 /* See dw2_debug_names_iterator. */
4883 dwarf2_per_cu_data
*
4884 dw2_debug_names_iterator::next ()
4889 dwarf2_per_bfd
*per_bfd
= m_per_objfile
->per_bfd
;
4890 struct objfile
*objfile
= m_per_objfile
->objfile
;
4891 bfd
*const abfd
= objfile
->obfd
;
4895 unsigned int bytes_read
;
4896 const ULONGEST abbrev
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4897 m_addr
+= bytes_read
;
4901 const auto indexval_it
= m_map
.abbrev_map
.find (abbrev
);
4902 if (indexval_it
== m_map
.abbrev_map
.cend ())
4904 complaint (_("Wrong .debug_names undefined abbrev code %s "
4906 pulongest (abbrev
), objfile_name (objfile
));
4909 const mapped_debug_names::index_val
&indexval
= indexval_it
->second
;
4910 enum class symbol_linkage
{
4914 } symbol_linkage_
= symbol_linkage::unknown
;
4915 dwarf2_per_cu_data
*per_cu
= NULL
;
4916 for (const mapped_debug_names::index_val::attr
&attr
: indexval
.attr_vec
)
4921 case DW_FORM_implicit_const
:
4922 ull
= attr
.implicit_const
;
4924 case DW_FORM_flag_present
:
4928 ull
= read_unsigned_leb128 (abfd
, m_addr
, &bytes_read
);
4929 m_addr
+= bytes_read
;
4932 ull
= read_4_bytes (abfd
, m_addr
);
4936 ull
= read_8_bytes (abfd
, m_addr
);
4939 case DW_FORM_ref_sig8
:
4940 ull
= read_8_bytes (abfd
, m_addr
);
4944 complaint (_("Unsupported .debug_names form %s [in module %s]"),
4945 dwarf_form_name (attr
.form
),
4946 objfile_name (objfile
));
4949 switch (attr
.dw_idx
)
4951 case DW_IDX_compile_unit
:
4952 /* Don't crash on bad data. */
4953 if (ull
>= per_bfd
->all_comp_units
.size ())
4955 complaint (_(".debug_names entry has bad CU index %s"
4958 objfile_name (objfile
));
4961 per_cu
= per_bfd
->get_cu (ull
);
4963 case DW_IDX_type_unit
:
4964 /* Don't crash on bad data. */
4965 if (ull
>= per_bfd
->tu_stats
.nr_tus
)
4967 complaint (_(".debug_names entry has bad TU index %s"
4970 objfile_name (objfile
));
4973 per_cu
= per_bfd
->get_cu (ull
+ per_bfd
->tu_stats
.nr_tus
);
4975 case DW_IDX_die_offset
:
4976 /* In a per-CU index (as opposed to a per-module index), index
4977 entries without CU attribute implicitly refer to the single CU. */
4979 per_cu
= per_bfd
->get_cu (0);
4981 case DW_IDX_GNU_internal
:
4982 if (!m_map
.augmentation_is_gdb
)
4984 symbol_linkage_
= symbol_linkage::static_
;
4986 case DW_IDX_GNU_external
:
4987 if (!m_map
.augmentation_is_gdb
)
4989 symbol_linkage_
= symbol_linkage::extern_
;
4994 /* Skip if already read in. */
4995 if (m_per_objfile
->symtab_set_p (per_cu
))
4998 /* Check static vs global. */
4999 if (symbol_linkage_
!= symbol_linkage::unknown
)
5001 if (symbol_linkage_
== symbol_linkage::static_
)
5003 if ((m_block_index
& SEARCH_STATIC_BLOCK
) == 0)
5008 if ((m_block_index
& SEARCH_GLOBAL_BLOCK
) == 0)
5013 /* Match dw2_symtab_iter_next, symbol_kind
5014 and debug_names::psymbol_tag. */
5018 switch (indexval
.dwarf_tag
)
5020 case DW_TAG_variable
:
5021 case DW_TAG_subprogram
:
5022 /* Some types are also in VAR_DOMAIN. */
5023 case DW_TAG_typedef
:
5024 case DW_TAG_structure_type
:
5031 switch (indexval
.dwarf_tag
)
5033 case DW_TAG_typedef
:
5034 case DW_TAG_structure_type
:
5041 switch (indexval
.dwarf_tag
)
5044 case DW_TAG_variable
:
5051 switch (indexval
.dwarf_tag
)
5063 /* Match dw2_expand_symtabs_matching, symbol_kind and
5064 debug_names::psymbol_tag. */
5067 case VARIABLES_DOMAIN
:
5068 switch (indexval
.dwarf_tag
)
5070 case DW_TAG_variable
:
5076 case FUNCTIONS_DOMAIN
:
5077 switch (indexval
.dwarf_tag
)
5079 case DW_TAG_subprogram
:
5086 switch (indexval
.dwarf_tag
)
5088 case DW_TAG_typedef
:
5089 case DW_TAG_structure_type
:
5095 case MODULES_DOMAIN
:
5096 switch (indexval
.dwarf_tag
)
5110 /* This dumps minimal information about .debug_names. It is called
5111 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5112 uses this to verify that .debug_names has been loaded. */
5115 dwarf2_debug_names_index::dump (struct objfile
*objfile
)
5117 gdb_printf (".debug_names: exists\n");
5121 dwarf2_debug_names_index::expand_matching_symbols
5122 (struct objfile
*objfile
,
5123 const lookup_name_info
&name
, domain_enum domain
,
5125 symbol_compare_ftype
*ordered_compare
)
5127 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5129 mapped_debug_names
&map
5130 = (static_cast<mapped_debug_names
&>
5131 (*per_objfile
->per_bfd
->index_table
.get ()));
5132 const block_search_flags block_flags
5133 = global
? SEARCH_GLOBAL_BLOCK
: SEARCH_STATIC_BLOCK
;
5135 const char *match_name
= name
.ada ().lookup_name ().c_str ();
5136 auto matcher
= [&] (const char *symname
)
5138 if (ordered_compare
== nullptr)
5140 return ordered_compare (symname
, match_name
) == 0;
5143 dw2_expand_symtabs_matching_symbol (map
, name
, matcher
,
5144 [&] (offset_type namei
)
5146 /* The name was matched, now expand corresponding CUs that were
5148 dw2_debug_names_iterator
iter (map
, block_flags
, domain
, namei
,
5151 struct dwarf2_per_cu_data
*per_cu
;
5152 while ((per_cu
= iter
.next ()) != NULL
)
5153 dw2_expand_symtabs_matching_one (per_cu
, per_objfile
, nullptr,
5160 dwarf2_debug_names_index::expand_symtabs_matching
5161 (struct objfile
*objfile
,
5162 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
5163 const lookup_name_info
*lookup_name
,
5164 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
5165 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
5166 block_search_flags search_flags
,
5168 enum search_domain kind
)
5170 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5172 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
5174 /* This invariant is documented in quick-functions.h. */
5175 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
5176 if (lookup_name
== nullptr)
5178 for (dwarf2_per_cu_data
*per_cu
5179 : all_comp_units_range (per_objfile
->per_bfd
))
5183 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5191 mapped_debug_names
&map
5192 = (static_cast<mapped_debug_names
&>
5193 (*per_objfile
->per_bfd
->index_table
.get ()));
5196 = dw2_expand_symtabs_matching_symbol (map
, *lookup_name
,
5198 [&] (offset_type namei
)
5200 /* The name was matched, now expand corresponding CUs that were
5202 dw2_debug_names_iterator
iter (map
, kind
, namei
, per_objfile
, domain
);
5204 struct dwarf2_per_cu_data
*per_cu
;
5205 while ((per_cu
= iter
.next ()) != NULL
)
5206 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
5216 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5217 to either a dwarf2_per_bfd or dwz_file object. */
5219 template <typename T
>
5220 static gdb::array_view
<const gdb_byte
>
5221 get_gdb_index_contents_from_section (objfile
*obj
, T
*section_owner
)
5223 dwarf2_section_info
*section
= §ion_owner
->gdb_index
;
5225 if (section
->empty ())
5228 /* Older elfutils strip versions could keep the section in the main
5229 executable while splitting it for the separate debug info file. */
5230 if ((section
->get_flags () & SEC_HAS_CONTENTS
) == 0)
5233 section
->read (obj
);
5235 /* dwarf2_section_info::size is a bfd_size_type, while
5236 gdb::array_view works with size_t. On 32-bit hosts, with
5237 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5238 is 32-bit. So we need an explicit narrowing conversion here.
5239 This is fine, because it's impossible to allocate or mmap an
5240 array/buffer larger than what size_t can represent. */
5241 return gdb::make_array_view (section
->buffer
, section
->size
);
5244 /* Lookup the index cache for the contents of the index associated to
5247 static gdb::array_view
<const gdb_byte
>
5248 get_gdb_index_contents_from_cache (objfile
*obj
, dwarf2_per_bfd
*dwarf2_per_bfd
)
5250 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
5251 if (build_id
== nullptr)
5254 return global_index_cache
.lookup_gdb_index (build_id
,
5255 &dwarf2_per_bfd
->index_cache_res
);
5258 /* Same as the above, but for DWZ. */
5260 static gdb::array_view
<const gdb_byte
>
5261 get_gdb_index_contents_from_cache_dwz (objfile
*obj
, dwz_file
*dwz
)
5263 const bfd_build_id
*build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
5264 if (build_id
== nullptr)
5267 return global_index_cache
.lookup_gdb_index (build_id
, &dwz
->index_cache_res
);
5270 static quick_symbol_functions_up
make_cooked_index_funcs ();
5272 /* See dwarf2/public.h. */
5275 dwarf2_initialize_objfile (struct objfile
*objfile
)
5277 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5278 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5280 dwarf_read_debug_printf ("called");
5282 /* If we're about to read full symbols, don't bother with the
5283 indices. In this case we also don't care if some other debug
5284 format is making psymtabs, because they are all about to be
5286 if ((objfile
->flags
& OBJF_READNOW
))
5288 dwarf_read_debug_printf ("readnow requested");
5290 create_all_comp_units (per_objfile
);
5291 per_bfd
->quick_file_names_table
5292 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
5294 objfile
->qf
.emplace_front (new readnow_functions
);
5298 /* Was a GDB index already read when we processed an objfile sharing
5300 if (per_bfd
->index_table
!= nullptr)
5302 dwarf_read_debug_printf ("re-using symbols");
5303 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5307 if (dwarf2_read_debug_names (per_objfile
))
5309 dwarf_read_debug_printf ("found debug names");
5310 objfile
->qf
.push_front
5311 (per_bfd
->index_table
->make_quick_functions ());
5315 if (dwarf2_read_gdb_index (per_objfile
,
5316 get_gdb_index_contents_from_section
<struct dwarf2_per_bfd
>,
5317 get_gdb_index_contents_from_section
<dwz_file
>))
5319 dwarf_read_debug_printf ("found gdb index from file");
5320 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5324 /* ... otherwise, try to find the index in the index cache. */
5325 if (dwarf2_read_gdb_index (per_objfile
,
5326 get_gdb_index_contents_from_cache
,
5327 get_gdb_index_contents_from_cache_dwz
))
5329 dwarf_read_debug_printf ("found gdb index from cache");
5330 global_index_cache
.hit ();
5331 objfile
->qf
.push_front (per_bfd
->index_table
->make_quick_functions ());
5335 global_index_cache
.miss ();
5336 objfile
->qf
.push_front (make_cooked_index_funcs ());
5341 /* Build a partial symbol table. */
5344 dwarf2_build_psymtabs (struct objfile
*objfile
, bool already_attached
)
5346 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
5348 if (already_attached
)
5350 if (per_objfile
->per_bfd
->index_table
!= nullptr)
5354 objfile
->qf
.push_front (make_cooked_index_funcs ());
5358 dwarf2_build_psymtabs_hard (per_objfile
);
5360 /* (maybe) store an index in the cache. */
5361 global_index_cache
.store (per_objfile
);
5363 catch (const gdb_exception_error
&except
)
5365 exception_print (gdb_stderr
, except
);
5369 /* Find the base address of the compilation unit for range lists and
5370 location lists. It will normally be specified by DW_AT_low_pc.
5371 In DWARF-3 draft 4, the base address could be overridden by
5372 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5373 compilation units with discontinuous ranges. */
5376 dwarf2_find_base_address (struct die_info
*die
, struct dwarf2_cu
*cu
)
5378 struct attribute
*attr
;
5380 cu
->base_address
.reset ();
5382 attr
= dwarf2_attr (die
, DW_AT_entry_pc
, cu
);
5383 if (attr
!= nullptr)
5384 cu
->base_address
= attr
->as_address ();
5387 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
5388 if (attr
!= nullptr)
5389 cu
->base_address
= attr
->as_address ();
5393 /* Helper function that returns the proper abbrev section for
5396 static struct dwarf2_section_info
*
5397 get_abbrev_section_for_cu (struct dwarf2_per_cu_data
*this_cu
)
5399 struct dwarf2_section_info
*abbrev
;
5400 dwarf2_per_bfd
*per_bfd
= this_cu
->per_bfd
;
5402 if (this_cu
->is_dwz
)
5403 abbrev
= &dwarf2_get_dwz_file (per_bfd
, true)->abbrev
;
5405 abbrev
= &per_bfd
->abbrev
;
5410 /* Fetch the abbreviation table offset from a comp or type unit header. */
5413 read_abbrev_offset (dwarf2_per_objfile
*per_objfile
,
5414 struct dwarf2_section_info
*section
,
5415 sect_offset sect_off
)
5417 bfd
*abfd
= section
->get_bfd_owner ();
5418 const gdb_byte
*info_ptr
;
5419 unsigned int initial_length_size
, offset_size
;
5422 section
->read (per_objfile
->objfile
);
5423 info_ptr
= section
->buffer
+ to_underlying (sect_off
);
5424 read_initial_length (abfd
, info_ptr
, &initial_length_size
);
5425 offset_size
= initial_length_size
== 4 ? 4 : 8;
5426 info_ptr
+= initial_length_size
;
5428 version
= read_2_bytes (abfd
, info_ptr
);
5432 /* Skip unit type and address size. */
5436 return (sect_offset
) read_offset (abfd
, info_ptr
, offset_size
);
5440 hash_signatured_type (const void *item
)
5442 const struct signatured_type
*sig_type
5443 = (const struct signatured_type
*) item
;
5445 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5446 return sig_type
->signature
;
5450 eq_signatured_type (const void *item_lhs
, const void *item_rhs
)
5452 const struct signatured_type
*lhs
= (const struct signatured_type
*) item_lhs
;
5453 const struct signatured_type
*rhs
= (const struct signatured_type
*) item_rhs
;
5455 return lhs
->signature
== rhs
->signature
;
5458 /* Allocate a hash table for signatured types. */
5461 allocate_signatured_type_table ()
5463 return htab_up (htab_create_alloc (41,
5464 hash_signatured_type
,
5466 NULL
, xcalloc
, xfree
));
5469 /* A helper for create_debug_types_hash_table. Read types from SECTION
5470 and fill them into TYPES_HTAB. It will process only type units,
5471 therefore DW_UT_type. */
5474 create_debug_type_hash_table (dwarf2_per_objfile
*per_objfile
,
5475 struct dwo_file
*dwo_file
,
5476 dwarf2_section_info
*section
, htab_up
&types_htab
,
5477 rcuh_kind section_kind
)
5479 struct objfile
*objfile
= per_objfile
->objfile
;
5480 struct dwarf2_section_info
*abbrev_section
;
5482 const gdb_byte
*info_ptr
, *end_ptr
;
5484 abbrev_section
= &dwo_file
->sections
.abbrev
;
5486 dwarf_read_debug_printf ("Reading %s for %s",
5487 section
->get_name (),
5488 abbrev_section
->get_file_name ());
5490 section
->read (objfile
);
5491 info_ptr
= section
->buffer
;
5493 if (info_ptr
== NULL
)
5496 /* We can't set abfd until now because the section may be empty or
5497 not present, in which case the bfd is unknown. */
5498 abfd
= section
->get_bfd_owner ();
5500 /* We don't use cutu_reader here because we don't need to read
5501 any dies: the signature is in the header. */
5503 end_ptr
= info_ptr
+ section
->size
;
5504 while (info_ptr
< end_ptr
)
5506 signatured_type_up sig_type
;
5507 struct dwo_unit
*dwo_tu
;
5509 const gdb_byte
*ptr
= info_ptr
;
5510 struct comp_unit_head header
;
5511 unsigned int length
;
5513 sect_offset sect_off
= (sect_offset
) (ptr
- section
->buffer
);
5515 /* Initialize it due to a false compiler warning. */
5516 header
.signature
= -1;
5517 header
.type_cu_offset_in_tu
= (cu_offset
) -1;
5519 /* We need to read the type's signature in order to build the hash
5520 table, but we don't need anything else just yet. */
5522 ptr
= read_and_check_comp_unit_head (per_objfile
, &header
, section
,
5523 abbrev_section
, ptr
, section_kind
);
5525 length
= header
.get_length ();
5527 /* Skip dummy type units. */
5528 if (ptr
>= info_ptr
+ length
5529 || peek_abbrev_code (abfd
, ptr
) == 0
5530 || (header
.unit_type
!= DW_UT_type
5531 && header
.unit_type
!= DW_UT_split_type
))
5537 if (types_htab
== NULL
)
5538 types_htab
= allocate_dwo_unit_table ();
5540 dwo_tu
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, dwo_unit
);
5541 dwo_tu
->dwo_file
= dwo_file
;
5542 dwo_tu
->signature
= header
.signature
;
5543 dwo_tu
->type_offset_in_tu
= header
.type_cu_offset_in_tu
;
5544 dwo_tu
->section
= section
;
5545 dwo_tu
->sect_off
= sect_off
;
5546 dwo_tu
->length
= length
;
5548 slot
= htab_find_slot (types_htab
.get (), dwo_tu
, INSERT
);
5549 gdb_assert (slot
!= NULL
);
5551 complaint (_("debug type entry at offset %s is duplicate to"
5552 " the entry at offset %s, signature %s"),
5553 sect_offset_str (sect_off
),
5554 sect_offset_str (dwo_tu
->sect_off
),
5555 hex_string (header
.signature
));
5558 dwarf_read_debug_printf_v (" offset %s, signature %s",
5559 sect_offset_str (sect_off
),
5560 hex_string (header
.signature
));
5566 /* Create the hash table of all entries in the .debug_types
5567 (or .debug_types.dwo) section(s).
5568 DWO_FILE is a pointer to the DWO file object.
5570 The result is a pointer to the hash table or NULL if there are no types.
5572 Note: This function processes DWO files only, not DWP files. */
5575 create_debug_types_hash_table (dwarf2_per_objfile
*per_objfile
,
5576 struct dwo_file
*dwo_file
,
5577 gdb::array_view
<dwarf2_section_info
> type_sections
,
5578 htab_up
&types_htab
)
5580 for (dwarf2_section_info
§ion
: type_sections
)
5581 create_debug_type_hash_table (per_objfile
, dwo_file
, §ion
, types_htab
,
5585 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
5586 If SLOT is non-NULL, it is the entry to use in the hash table.
5587 Otherwise we find one. */
5589 static struct signatured_type
*
5590 add_type_unit (dwarf2_per_objfile
*per_objfile
, ULONGEST sig
, void **slot
)
5592 if (per_objfile
->per_bfd
->all_comp_units
.size ()
5593 == per_objfile
->per_bfd
->all_comp_units
.capacity ())
5594 ++per_objfile
->per_bfd
->tu_stats
.nr_all_type_units_reallocs
;
5596 signatured_type_up sig_type_holder
5597 = per_objfile
->per_bfd
->allocate_signatured_type (sig
);
5598 signatured_type
*sig_type
= sig_type_holder
.get ();
5600 per_objfile
->per_bfd
->all_comp_units
.emplace_back
5601 (sig_type_holder
.release ());
5605 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5608 gdb_assert (*slot
== NULL
);
5610 /* The rest of sig_type must be filled in by the caller. */
5614 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5615 Fill in SIG_ENTRY with DWO_ENTRY. */
5618 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile
*per_objfile
,
5619 struct signatured_type
*sig_entry
,
5620 struct dwo_unit
*dwo_entry
)
5622 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
5624 /* Make sure we're not clobbering something we don't expect to. */
5625 gdb_assert (! sig_entry
->queued
);
5626 gdb_assert (per_objfile
->get_cu (sig_entry
) == NULL
);
5627 gdb_assert (!per_objfile
->symtab_set_p (sig_entry
));
5628 gdb_assert (sig_entry
->signature
== dwo_entry
->signature
);
5629 gdb_assert (to_underlying (sig_entry
->type_offset_in_section
) == 0
5630 || (to_underlying (sig_entry
->type_offset_in_section
)
5631 == to_underlying (dwo_entry
->type_offset_in_tu
)));
5632 gdb_assert (sig_entry
->type_unit_group
== NULL
);
5633 gdb_assert (sig_entry
->dwo_unit
== NULL
5634 || sig_entry
->dwo_unit
== dwo_entry
);
5636 sig_entry
->section
= dwo_entry
->section
;
5637 sig_entry
->sect_off
= dwo_entry
->sect_off
;
5638 sig_entry
->length
= dwo_entry
->length
;
5639 sig_entry
->reading_dwo_directly
= 1;
5640 sig_entry
->per_bfd
= per_bfd
;
5641 sig_entry
->type_offset_in_tu
= dwo_entry
->type_offset_in_tu
;
5642 sig_entry
->dwo_unit
= dwo_entry
;
5645 /* Subroutine of lookup_signatured_type.
5646 If we haven't read the TU yet, create the signatured_type data structure
5647 for a TU to be read in directly from a DWO file, bypassing the stub.
5648 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5649 using .gdb_index, then when reading a CU we want to stay in the DWO file
5650 containing that CU. Otherwise we could end up reading several other DWO
5651 files (due to comdat folding) to process the transitive closure of all the
5652 mentioned TUs, and that can be slow. The current DWO file will have every
5653 type signature that it needs.
5654 We only do this for .gdb_index because in the psymtab case we already have
5655 to read all the DWOs to build the type unit groups. */
5657 static struct signatured_type
*
5658 lookup_dwo_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5660 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5661 struct dwo_file
*dwo_file
;
5662 struct dwo_unit find_dwo_entry
, *dwo_entry
;
5665 gdb_assert (cu
->dwo_unit
);
5667 /* If TU skeletons have been removed then we may not have read in any
5669 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5670 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5672 /* We only ever need to read in one copy of a signatured type.
5673 Use the global signatured_types array to do our own comdat-folding
5674 of types. If this is the first time we're reading this TU, and
5675 the TU has an entry in .gdb_index, replace the recorded data from
5676 .gdb_index with this TU. */
5678 signatured_type
find_sig_entry (sig
);
5679 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5680 &find_sig_entry
, INSERT
);
5681 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5683 /* We can get here with the TU already read, *or* in the process of being
5684 read. Don't reassign the global entry to point to this DWO if that's
5685 the case. Also note that if the TU is already being read, it may not
5686 have come from a DWO, the program may be a mix of Fission-compiled
5687 code and non-Fission-compiled code. */
5689 /* Have we already tried to read this TU?
5690 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5691 needn't exist in the global table yet). */
5692 if (sig_entry
!= NULL
&& sig_entry
->tu_read
)
5695 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5696 dwo_unit of the TU itself. */
5697 dwo_file
= cu
->dwo_unit
->dwo_file
;
5699 /* Ok, this is the first time we're reading this TU. */
5700 if (dwo_file
->tus
== NULL
)
5702 find_dwo_entry
.signature
= sig
;
5703 dwo_entry
= (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
5705 if (dwo_entry
== NULL
)
5708 /* If the global table doesn't have an entry for this TU, add one. */
5709 if (sig_entry
== NULL
)
5710 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5712 if (sig_entry
->dwo_unit
== nullptr)
5713 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5714 sig_entry
->tu_read
= 1;
5718 /* Subroutine of lookup_signatured_type.
5719 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5720 then try the DWP file. If the TU stub (skeleton) has been removed then
5721 it won't be in .gdb_index. */
5723 static struct signatured_type
*
5724 lookup_dwp_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5726 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5727 struct dwp_file
*dwp_file
= get_dwp_file (per_objfile
);
5728 struct dwo_unit
*dwo_entry
;
5731 gdb_assert (cu
->dwo_unit
);
5732 gdb_assert (dwp_file
!= NULL
);
5734 /* If TU skeletons have been removed then we may not have read in any
5736 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5737 per_objfile
->per_bfd
->signatured_types
= allocate_signatured_type_table ();
5739 signatured_type
find_sig_entry (sig
);
5740 slot
= htab_find_slot (per_objfile
->per_bfd
->signatured_types
.get (),
5741 &find_sig_entry
, INSERT
);
5742 signatured_type
*sig_entry
= (struct signatured_type
*) *slot
;
5744 /* Have we already tried to read this TU?
5745 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5746 needn't exist in the global table yet). */
5747 if (sig_entry
!= NULL
)
5750 if (dwp_file
->tus
== NULL
)
5752 dwo_entry
= lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, NULL
, sig
,
5753 1 /* is_debug_types */);
5754 if (dwo_entry
== NULL
)
5757 sig_entry
= add_type_unit (per_objfile
, sig
, slot
);
5758 fill_in_sig_entry_from_dwo_entry (per_objfile
, sig_entry
, dwo_entry
);
5763 /* Lookup a signature based type for DW_FORM_ref_sig8.
5764 Returns NULL if signature SIG is not present in the table.
5765 It is up to the caller to complain about this. */
5767 static struct signatured_type
*
5768 lookup_signatured_type (struct dwarf2_cu
*cu
, ULONGEST sig
)
5770 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5774 /* We're in a DWO/DWP file, and we're using .gdb_index.
5775 These cases require special processing. */
5776 if (get_dwp_file (per_objfile
) == NULL
)
5777 return lookup_dwo_signatured_type (cu
, sig
);
5779 return lookup_dwp_signatured_type (cu
, sig
);
5783 if (per_objfile
->per_bfd
->signatured_types
== NULL
)
5785 signatured_type
find_entry (sig
);
5786 return ((struct signatured_type
*)
5787 htab_find (per_objfile
->per_bfd
->signatured_types
.get (),
5792 /* Low level DIE reading support. */
5794 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5797 init_cu_die_reader (struct die_reader_specs
*reader
,
5798 struct dwarf2_cu
*cu
,
5799 struct dwarf2_section_info
*section
,
5800 struct dwo_file
*dwo_file
,
5801 struct abbrev_table
*abbrev_table
)
5803 gdb_assert (section
->readin
&& section
->buffer
!= NULL
);
5804 reader
->abfd
= section
->get_bfd_owner ();
5806 reader
->dwo_file
= dwo_file
;
5807 reader
->die_section
= section
;
5808 reader
->buffer
= section
->buffer
;
5809 reader
->buffer_end
= section
->buffer
+ section
->size
;
5810 reader
->abbrev_table
= abbrev_table
;
5813 /* Subroutine of cutu_reader to simplify it.
5814 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5815 There's just a lot of work to do, and cutu_reader is big enough
5818 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5819 from it to the DIE in the DWO. If NULL we are skipping the stub.
5820 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5821 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5822 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5823 STUB_COMP_DIR may be non-NULL.
5824 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
5825 are filled in with the info of the DIE from the DWO file.
5826 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
5827 from the dwo. Since *RESULT_READER references this abbrev table, it must be
5828 kept around for at least as long as *RESULT_READER.
5830 The result is non-zero if a valid (non-dummy) DIE was found. */
5833 read_cutu_die_from_dwo (dwarf2_cu
*cu
,
5834 struct dwo_unit
*dwo_unit
,
5835 struct die_info
*stub_comp_unit_die
,
5836 const char *stub_comp_dir
,
5837 struct die_reader_specs
*result_reader
,
5838 const gdb_byte
**result_info_ptr
,
5839 struct die_info
**result_comp_unit_die
,
5840 abbrev_table_up
*result_dwo_abbrev_table
)
5842 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
5843 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
5844 struct objfile
*objfile
= per_objfile
->objfile
;
5846 const gdb_byte
*begin_info_ptr
, *info_ptr
;
5847 struct attribute
*comp_dir
, *stmt_list
, *low_pc
, *high_pc
, *ranges
;
5848 int i
,num_extra_attrs
;
5849 struct dwarf2_section_info
*dwo_abbrev_section
;
5850 struct die_info
*comp_unit_die
;
5852 /* At most one of these may be provided. */
5853 gdb_assert ((stub_comp_unit_die
!= NULL
) + (stub_comp_dir
!= NULL
) <= 1);
5855 /* These attributes aren't processed until later:
5856 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5857 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5858 referenced later. However, these attributes are found in the stub
5859 which we won't have later. In order to not impose this complication
5860 on the rest of the code, we read them here and copy them to the
5869 if (stub_comp_unit_die
!= NULL
)
5871 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5873 if (!per_cu
->is_debug_types
)
5874 stmt_list
= dwarf2_attr (stub_comp_unit_die
, DW_AT_stmt_list
, cu
);
5875 low_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_low_pc
, cu
);
5876 high_pc
= dwarf2_attr (stub_comp_unit_die
, DW_AT_high_pc
, cu
);
5877 ranges
= dwarf2_attr (stub_comp_unit_die
, DW_AT_ranges
, cu
);
5878 comp_dir
= dwarf2_attr (stub_comp_unit_die
, DW_AT_comp_dir
, cu
);
5880 cu
->addr_base
= stub_comp_unit_die
->addr_base ();
5882 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
5883 We need the value before we can process DW_AT_ranges values from the
5885 cu
->gnu_ranges_base
= stub_comp_unit_die
->gnu_ranges_base ();
5887 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
5888 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
5889 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
5890 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
5892 cu
->rnglists_base
= stub_comp_unit_die
->rnglists_base ();
5894 else if (stub_comp_dir
!= NULL
)
5896 /* Reconstruct the comp_dir attribute to simplify the code below. */
5897 comp_dir
= OBSTACK_ZALLOC (&cu
->comp_unit_obstack
, struct attribute
);
5898 comp_dir
->name
= DW_AT_comp_dir
;
5899 comp_dir
->form
= DW_FORM_string
;
5900 comp_dir
->set_string_noncanonical (stub_comp_dir
);
5903 /* Set up for reading the DWO CU/TU. */
5904 cu
->dwo_unit
= dwo_unit
;
5905 dwarf2_section_info
*section
= dwo_unit
->section
;
5906 section
->read (objfile
);
5907 abfd
= section
->get_bfd_owner ();
5908 begin_info_ptr
= info_ptr
= (section
->buffer
5909 + to_underlying (dwo_unit
->sect_off
));
5910 dwo_abbrev_section
= &dwo_unit
->dwo_file
->sections
.abbrev
;
5912 if (per_cu
->is_debug_types
)
5914 signatured_type
*sig_type
= (struct signatured_type
*) per_cu
;
5916 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5917 section
, dwo_abbrev_section
,
5918 info_ptr
, rcuh_kind::TYPE
);
5919 /* This is not an assert because it can be caused by bad debug info. */
5920 if (sig_type
->signature
!= cu
->header
.signature
)
5922 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5923 " TU at offset %s [in module %s]"),
5924 hex_string (sig_type
->signature
),
5925 hex_string (cu
->header
.signature
),
5926 sect_offset_str (dwo_unit
->sect_off
),
5927 bfd_get_filename (abfd
));
5929 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5930 /* For DWOs coming from DWP files, we don't know the CU length
5931 nor the type's offset in the TU until now. */
5932 dwo_unit
->length
= cu
->header
.get_length ();
5933 dwo_unit
->type_offset_in_tu
= cu
->header
.type_cu_offset_in_tu
;
5935 /* Establish the type offset that can be used to lookup the type.
5936 For DWO files, we don't know it until now. */
5937 sig_type
->type_offset_in_section
5938 = dwo_unit
->sect_off
+ to_underlying (dwo_unit
->type_offset_in_tu
);
5942 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
5943 section
, dwo_abbrev_section
,
5944 info_ptr
, rcuh_kind::COMPILE
);
5945 gdb_assert (dwo_unit
->sect_off
== cu
->header
.sect_off
);
5946 /* For DWOs coming from DWP files, we don't know the CU length
5948 dwo_unit
->length
= cu
->header
.get_length ();
5951 dwo_abbrev_section
->read (objfile
);
5952 *result_dwo_abbrev_table
5953 = abbrev_table::read (dwo_abbrev_section
, cu
->header
.abbrev_sect_off
);
5954 init_cu_die_reader (result_reader
, cu
, section
, dwo_unit
->dwo_file
,
5955 result_dwo_abbrev_table
->get ());
5957 /* Read in the die, but leave space to copy over the attributes
5958 from the stub. This has the benefit of simplifying the rest of
5959 the code - all the work to maintain the illusion of a single
5960 DW_TAG_{compile,type}_unit DIE is done here. */
5961 num_extra_attrs
= ((stmt_list
!= NULL
)
5965 + (comp_dir
!= NULL
));
5966 info_ptr
= read_full_die_1 (result_reader
, result_comp_unit_die
, info_ptr
,
5969 /* Copy over the attributes from the stub to the DIE we just read in. */
5970 comp_unit_die
= *result_comp_unit_die
;
5971 i
= comp_unit_die
->num_attrs
;
5972 if (stmt_list
!= NULL
)
5973 comp_unit_die
->attrs
[i
++] = *stmt_list
;
5975 comp_unit_die
->attrs
[i
++] = *low_pc
;
5976 if (high_pc
!= NULL
)
5977 comp_unit_die
->attrs
[i
++] = *high_pc
;
5979 comp_unit_die
->attrs
[i
++] = *ranges
;
5980 if (comp_dir
!= NULL
)
5981 comp_unit_die
->attrs
[i
++] = *comp_dir
;
5982 comp_unit_die
->num_attrs
+= num_extra_attrs
;
5984 if (dwarf_die_debug
)
5986 gdb_printf (gdb_stdlog
,
5987 "Read die from %s@0x%x of %s:\n",
5988 section
->get_name (),
5989 (unsigned) (begin_info_ptr
- section
->buffer
),
5990 bfd_get_filename (abfd
));
5991 dump_die (comp_unit_die
, dwarf_die_debug
);
5994 /* Skip dummy compilation units. */
5995 if (info_ptr
>= begin_info_ptr
+ dwo_unit
->length
5996 || peek_abbrev_code (abfd
, info_ptr
) == 0)
5999 *result_info_ptr
= info_ptr
;
6003 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6004 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6005 signature is part of the header. */
6006 static gdb::optional
<ULONGEST
>
6007 lookup_dwo_id (struct dwarf2_cu
*cu
, struct die_info
* comp_unit_die
)
6009 if (cu
->header
.version
>= 5)
6010 return cu
->header
.signature
;
6011 struct attribute
*attr
;
6012 attr
= dwarf2_attr (comp_unit_die
, DW_AT_GNU_dwo_id
, cu
);
6013 if (attr
== nullptr || !attr
->form_is_unsigned ())
6014 return gdb::optional
<ULONGEST
> ();
6015 return attr
->as_unsigned ();
6018 /* Subroutine of cutu_reader to simplify it.
6019 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6020 Returns NULL if the specified DWO unit cannot be found. */
6022 static struct dwo_unit
*
6023 lookup_dwo_unit (dwarf2_cu
*cu
, die_info
*comp_unit_die
, const char *dwo_name
)
6026 /* We need a lock here both to handle the DWO hash table, and BFD,
6027 which is not thread-safe. */
6028 static std::mutex dwo_lock
;
6030 std::lock_guard
<std::mutex
> guard (dwo_lock
);
6033 dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6034 struct dwo_unit
*dwo_unit
;
6035 const char *comp_dir
;
6037 gdb_assert (cu
!= NULL
);
6039 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6040 dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6041 comp_dir
= dwarf2_string_attr (comp_unit_die
, DW_AT_comp_dir
, cu
);
6043 if (per_cu
->is_debug_types
)
6044 dwo_unit
= lookup_dwo_type_unit (cu
, dwo_name
, comp_dir
);
6047 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
6049 if (!signature
.has_value ())
6050 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6052 dwo_name
, bfd_get_filename (per_cu
->per_bfd
->obfd
));
6054 dwo_unit
= lookup_dwo_comp_unit (cu
, dwo_name
, comp_dir
, *signature
);
6060 /* Subroutine of cutu_reader to simplify it.
6061 See it for a description of the parameters.
6062 Read a TU directly from a DWO file, bypassing the stub. */
6065 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data
*this_cu
,
6066 dwarf2_per_objfile
*per_objfile
,
6067 dwarf2_cu
*existing_cu
)
6069 struct signatured_type
*sig_type
;
6071 /* Verify we can do the following downcast, and that we have the
6073 gdb_assert (this_cu
->is_debug_types
&& this_cu
->reading_dwo_directly
);
6074 sig_type
= (struct signatured_type
*) this_cu
;
6075 gdb_assert (sig_type
->dwo_unit
!= NULL
);
6079 if (existing_cu
!= nullptr)
6082 gdb_assert (cu
->dwo_unit
== sig_type
->dwo_unit
);
6083 /* There's no need to do the rereading_dwo_cu handling that
6084 cutu_reader does since we don't read the stub. */
6088 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6089 in per_objfile yet. */
6090 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6091 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6092 cu
= m_new_cu
.get ();
6095 /* A future optimization, if needed, would be to use an existing
6096 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6097 could share abbrev tables. */
6099 if (read_cutu_die_from_dwo (cu
, sig_type
->dwo_unit
,
6100 NULL
/* stub_comp_unit_die */,
6101 sig_type
->dwo_unit
->dwo_file
->comp_dir
,
6104 &m_dwo_abbrev_table
) == 0)
6111 /* Initialize a CU (or TU) and read its DIEs.
6112 If the CU defers to a DWO file, read the DWO file as well.
6114 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6115 Otherwise the table specified in the comp unit header is read in and used.
6116 This is an optimization for when we already have the abbrev table.
6118 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6121 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6122 dwarf2_per_objfile
*per_objfile
,
6123 struct abbrev_table
*abbrev_table
,
6124 dwarf2_cu
*existing_cu
,
6126 abbrev_cache
*cache
)
6127 : die_reader_specs
{},
6130 struct objfile
*objfile
= per_objfile
->objfile
;
6131 struct dwarf2_section_info
*section
= this_cu
->section
;
6132 bfd
*abfd
= section
->get_bfd_owner ();
6133 const gdb_byte
*begin_info_ptr
;
6134 struct signatured_type
*sig_type
= NULL
;
6135 struct dwarf2_section_info
*abbrev_section
;
6136 /* Non-zero if CU currently points to a DWO file and we need to
6137 reread it. When this happens we need to reread the skeleton die
6138 before we can reread the DWO file (this only applies to CUs, not TUs). */
6139 int rereading_dwo_cu
= 0;
6141 if (dwarf_die_debug
)
6142 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6143 this_cu
->is_debug_types
? "type" : "comp",
6144 sect_offset_str (this_cu
->sect_off
));
6146 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6147 file (instead of going through the stub), short-circuit all of this. */
6148 if (this_cu
->reading_dwo_directly
)
6150 /* Narrow down the scope of possibilities to have to understand. */
6151 gdb_assert (this_cu
->is_debug_types
);
6152 gdb_assert (abbrev_table
== NULL
);
6153 init_tu_and_read_dwo_dies (this_cu
, per_objfile
, existing_cu
);
6157 /* This is cheap if the section is already read in. */
6158 section
->read (objfile
);
6160 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6162 abbrev_section
= get_abbrev_section_for_cu (this_cu
);
6166 if (existing_cu
!= nullptr)
6169 /* If this CU is from a DWO file we need to start over, we need to
6170 refetch the attributes from the skeleton CU.
6171 This could be optimized by retrieving those attributes from when we
6172 were here the first time: the previous comp_unit_die was stored in
6173 comp_unit_obstack. But there's no data yet that we need this
6175 if (cu
->dwo_unit
!= NULL
)
6176 rereading_dwo_cu
= 1;
6180 /* If an existing_cu is provided, a dwarf2_cu must not exist for
6181 this_cu in per_objfile yet. Here, CACHE doubles as a flag to
6182 let us know that the CU is being scanned using the parallel
6183 indexer. This assert is avoided in this case because (1) it
6184 is irrelevant, and (2) the get_cu method is not
6186 gdb_assert (cache
!= nullptr
6187 || per_objfile
->get_cu (this_cu
) == nullptr);
6188 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6189 cu
= m_new_cu
.get ();
6192 /* Get the header. */
6193 if (to_underlying (cu
->header
.first_die_cu_offset
) != 0 && !rereading_dwo_cu
)
6195 /* We already have the header, there's no need to read it in again. */
6196 info_ptr
+= to_underlying (cu
->header
.first_die_cu_offset
);
6200 if (this_cu
->is_debug_types
)
6202 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6203 section
, abbrev_section
,
6204 info_ptr
, rcuh_kind::TYPE
);
6206 /* Since per_cu is the first member of struct signatured_type,
6207 we can go from a pointer to one to a pointer to the other. */
6208 sig_type
= (struct signatured_type
*) this_cu
;
6209 gdb_assert (sig_type
->signature
== cu
->header
.signature
);
6210 gdb_assert (sig_type
->type_offset_in_tu
6211 == cu
->header
.type_cu_offset_in_tu
);
6212 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6214 /* LENGTH has not been set yet for type units if we're
6215 using .gdb_index. */
6216 this_cu
->length
= cu
->header
.get_length ();
6218 /* Establish the type offset that can be used to lookup the type. */
6219 sig_type
->type_offset_in_section
=
6220 this_cu
->sect_off
+ to_underlying (sig_type
->type_offset_in_tu
);
6222 this_cu
->dwarf_version
= cu
->header
.version
;
6226 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &cu
->header
,
6227 section
, abbrev_section
,
6229 rcuh_kind::COMPILE
);
6231 gdb_assert (this_cu
->sect_off
== cu
->header
.sect_off
);
6232 if (this_cu
->length
== 0)
6233 this_cu
->length
= cu
->header
.get_length ();
6235 gdb_assert (this_cu
->length
== cu
->header
.get_length ());
6236 this_cu
->dwarf_version
= cu
->header
.version
;
6240 /* Skip dummy compilation units. */
6241 if (info_ptr
>= begin_info_ptr
+ this_cu
->length
6242 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6248 /* If we don't have them yet, read the abbrevs for this compilation unit.
6249 And if we need to read them now, make sure they're freed when we're
6251 if (abbrev_table
!= NULL
)
6252 gdb_assert (cu
->header
.abbrev_sect_off
== abbrev_table
->sect_off
);
6255 if (cache
!= nullptr)
6256 abbrev_table
= cache
->find (abbrev_section
,
6257 cu
->header
.abbrev_sect_off
);
6258 if (abbrev_table
== nullptr)
6260 abbrev_section
->read (objfile
);
6261 m_abbrev_table_holder
6262 = abbrev_table::read (abbrev_section
, cu
->header
.abbrev_sect_off
);
6263 abbrev_table
= m_abbrev_table_holder
.get ();
6267 /* Read the top level CU/TU die. */
6268 init_cu_die_reader (this, cu
, section
, NULL
, abbrev_table
);
6269 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6271 if (skip_partial
&& comp_unit_die
->tag
== DW_TAG_partial_unit
)
6277 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6278 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6279 table from the DWO file and pass the ownership over to us. It will be
6280 referenced from READER, so we must make sure to free it after we're done
6283 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6284 DWO CU, that this test will fail (the attribute will not be present). */
6285 const char *dwo_name
= dwarf2_dwo_name (comp_unit_die
, cu
);
6286 if (dwo_name
!= nullptr)
6288 struct dwo_unit
*dwo_unit
;
6289 struct die_info
*dwo_comp_unit_die
;
6291 if (comp_unit_die
->has_children
)
6293 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6294 " has children (offset %s) [in module %s]"),
6295 sect_offset_str (this_cu
->sect_off
),
6296 bfd_get_filename (abfd
));
6298 dwo_unit
= lookup_dwo_unit (cu
, comp_unit_die
, dwo_name
);
6299 if (dwo_unit
!= NULL
)
6301 if (read_cutu_die_from_dwo (cu
, dwo_unit
,
6302 comp_unit_die
, NULL
,
6305 &m_dwo_abbrev_table
) == 0)
6311 comp_unit_die
= dwo_comp_unit_die
;
6315 /* Yikes, we couldn't find the rest of the DIE, we only have
6316 the stub. A complaint has already been logged. There's
6317 not much more we can do except pass on the stub DIE to
6318 die_reader_func. We don't want to throw an error on bad
6325 cutu_reader::keep ()
6327 /* Done, clean up. */
6328 gdb_assert (!dummy_p
);
6329 if (m_new_cu
!= NULL
)
6331 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6333 dwarf2_per_objfile
*per_objfile
= m_new_cu
->per_objfile
;
6334 per_objfile
->set_cu (m_this_cu
, m_new_cu
.release ());
6338 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6339 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6340 assumed to have already done the lookup to find the DWO file).
6342 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6343 THIS_CU->is_debug_types, but nothing else.
6345 We fill in THIS_CU->length.
6347 THIS_CU->cu is always freed when done.
6348 This is done in order to not leave THIS_CU->cu in a state where we have
6349 to care whether it refers to the "main" CU or the DWO CU.
6351 When parent_cu is passed, it is used to provide a default value for
6352 str_offsets_base and addr_base from the parent. */
6354 cutu_reader::cutu_reader (dwarf2_per_cu_data
*this_cu
,
6355 dwarf2_per_objfile
*per_objfile
,
6356 struct dwarf2_cu
*parent_cu
,
6357 struct dwo_file
*dwo_file
)
6358 : die_reader_specs
{},
6361 struct objfile
*objfile
= per_objfile
->objfile
;
6362 struct dwarf2_section_info
*section
= this_cu
->section
;
6363 bfd
*abfd
= section
->get_bfd_owner ();
6364 struct dwarf2_section_info
*abbrev_section
;
6365 const gdb_byte
*begin_info_ptr
, *info_ptr
;
6367 if (dwarf_die_debug
)
6368 gdb_printf (gdb_stdlog
, "Reading %s unit at offset %s\n",
6369 this_cu
->is_debug_types
? "type" : "comp",
6370 sect_offset_str (this_cu
->sect_off
));
6372 gdb_assert (per_objfile
->get_cu (this_cu
) == nullptr);
6374 abbrev_section
= (dwo_file
!= NULL
6375 ? &dwo_file
->sections
.abbrev
6376 : get_abbrev_section_for_cu (this_cu
));
6378 /* This is cheap if the section is already read in. */
6379 section
->read (objfile
);
6381 m_new_cu
.reset (new dwarf2_cu (this_cu
, per_objfile
));
6383 begin_info_ptr
= info_ptr
= section
->buffer
+ to_underlying (this_cu
->sect_off
);
6384 info_ptr
= read_and_check_comp_unit_head (per_objfile
, &m_new_cu
->header
,
6385 section
, abbrev_section
, info_ptr
,
6386 (this_cu
->is_debug_types
6388 : rcuh_kind::COMPILE
));
6390 if (parent_cu
!= nullptr)
6392 m_new_cu
->str_offsets_base
= parent_cu
->str_offsets_base
;
6393 m_new_cu
->addr_base
= parent_cu
->addr_base
;
6395 this_cu
->length
= m_new_cu
->header
.get_length ();
6397 /* Skip dummy compilation units. */
6398 if (info_ptr
>= begin_info_ptr
+ this_cu
->length
6399 || peek_abbrev_code (abfd
, info_ptr
) == 0)
6405 abbrev_section
->read (objfile
);
6406 m_abbrev_table_holder
6407 = abbrev_table::read (abbrev_section
, m_new_cu
->header
.abbrev_sect_off
);
6409 init_cu_die_reader (this, m_new_cu
.get (), section
, dwo_file
,
6410 m_abbrev_table_holder
.get ());
6411 info_ptr
= read_full_die (this, &comp_unit_die
, info_ptr
);
6415 /* Type Unit Groups.
6417 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6418 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6419 so that all types coming from the same compilation (.o file) are grouped
6420 together. A future step could be to put the types in the same symtab as
6421 the CU the types ultimately came from. */
6424 hash_type_unit_group (const void *item
)
6426 const struct type_unit_group
*tu_group
6427 = (const struct type_unit_group
*) item
;
6429 return hash_stmt_list_entry (&tu_group
->hash
);
6433 eq_type_unit_group (const void *item_lhs
, const void *item_rhs
)
6435 const struct type_unit_group
*lhs
= (const struct type_unit_group
*) item_lhs
;
6436 const struct type_unit_group
*rhs
= (const struct type_unit_group
*) item_rhs
;
6438 return eq_stmt_list_entry (&lhs
->hash
, &rhs
->hash
);
6441 /* Allocate a hash table for type unit groups. */
6444 allocate_type_unit_groups_table ()
6446 return htab_up (htab_create_alloc (3,
6447 hash_type_unit_group
,
6449 htab_delete_entry
<type_unit_group
>,
6453 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6454 partial symtabs. We combine several TUs per psymtab to not let the size
6455 of any one psymtab grow too big. */
6456 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6457 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6459 /* Helper routine for get_type_unit_group.
6460 Create the type_unit_group object used to hold one or more TUs. */
6462 static std::unique_ptr
<type_unit_group
>
6463 create_type_unit_group (struct dwarf2_cu
*cu
, sect_offset line_offset_struct
)
6465 std::unique_ptr
<type_unit_group
> tu_group (new type_unit_group
);
6467 tu_group
->hash
.dwo_unit
= cu
->dwo_unit
;
6468 tu_group
->hash
.line_sect_off
= line_offset_struct
;
6473 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6474 STMT_LIST is a DW_AT_stmt_list attribute. */
6476 static struct type_unit_group
*
6477 get_type_unit_group (struct dwarf2_cu
*cu
, const struct attribute
*stmt_list
)
6479 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
6480 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6481 struct type_unit_group
*tu_group
;
6483 unsigned int line_offset
;
6484 struct type_unit_group type_unit_group_for_lookup
;
6486 if (per_objfile
->per_bfd
->type_unit_groups
== NULL
)
6487 per_objfile
->per_bfd
->type_unit_groups
= allocate_type_unit_groups_table ();
6489 /* Do we need to create a new group, or can we use an existing one? */
6491 if (stmt_list
!= nullptr && stmt_list
->form_is_unsigned ())
6493 line_offset
= stmt_list
->as_unsigned ();
6494 ++tu_stats
->nr_symtab_sharers
;
6498 /* Ugh, no stmt_list. Rare, but we have to handle it.
6499 We can do various things here like create one group per TU or
6500 spread them over multiple groups to split up the expansion work.
6501 To avoid worst case scenarios (too many groups or too large groups)
6502 we, umm, group them in bunches. */
6503 line_offset
= (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6504 | (tu_stats
->nr_stmt_less_type_units
6505 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE
));
6506 ++tu_stats
->nr_stmt_less_type_units
;
6509 type_unit_group_for_lookup
.hash
.dwo_unit
= cu
->dwo_unit
;
6510 type_unit_group_for_lookup
.hash
.line_sect_off
= (sect_offset
) line_offset
;
6511 slot
= htab_find_slot (per_objfile
->per_bfd
->type_unit_groups
.get (),
6512 &type_unit_group_for_lookup
, INSERT
);
6513 if (*slot
== nullptr)
6515 sect_offset line_offset_struct
= (sect_offset
) line_offset
;
6516 std::unique_ptr
<type_unit_group
> grp
6517 = create_type_unit_group (cu
, line_offset_struct
);
6518 *slot
= grp
.release ();
6519 ++tu_stats
->nr_symtabs
;
6522 tu_group
= (struct type_unit_group
*) *slot
;
6523 gdb_assert (tu_group
!= nullptr);
6528 /* An instance of this is created when scanning DWARF to create a
6531 class cooked_index_storage
6535 cooked_index_storage ()
6536 : m_reader_hash (htab_create_alloc (10, hash_cutu_reader
,
6538 htab_delete_entry
<cutu_reader
>,
6540 m_index (new cooked_index
),
6541 m_addrmap_storage (),
6542 m_addrmap (addrmap_create_mutable (&m_addrmap_storage
))
6546 DISABLE_COPY_AND_ASSIGN (cooked_index_storage
);
6548 /* Return the current abbrev cache. */
6549 abbrev_cache
*get_abbrev_cache ()
6551 return &m_abbrev_cache
;
6554 /* Return the DIE reader corresponding to PER_CU. If no such reader
6555 has been registered, return NULL. */
6556 cutu_reader
*get_reader (dwarf2_per_cu_data
*per_cu
)
6558 int index
= per_cu
->index
;
6559 return (cutu_reader
*) htab_find_with_hash (m_reader_hash
.get (),
6563 /* Preserve READER by storing it in the local hash table. */
6564 cutu_reader
*preserve (std::unique_ptr
<cutu_reader
> reader
)
6566 m_abbrev_cache
.add (reader
->release_abbrev_table ());
6568 int index
= reader
->cu
->per_cu
->index
;
6569 void **slot
= htab_find_slot_with_hash (m_reader_hash
.get (), &index
,
6571 gdb_assert (*slot
== nullptr);
6572 cutu_reader
*result
= reader
.get ();
6573 *slot
= reader
.release ();
6577 /* Add an entry to the index. The arguments describe the entry; see
6578 cooked-index.h. The new entry is returned. */
6579 const cooked_index_entry
*add (sect_offset die_offset
, enum dwarf_tag tag
,
6580 cooked_index_flag flags
,
6582 const cooked_index_entry
*parent_entry
,
6583 dwarf2_per_cu_data
*per_cu
)
6585 return m_index
->add (die_offset
, tag
, flags
, name
, parent_entry
, per_cu
);
6588 /* Install the current addrmap into the index being constructed,
6589 then transfer ownership of the index to the caller. */
6590 std::unique_ptr
<cooked_index
> release ()
6592 m_index
->install_addrmap (m_addrmap
);
6593 return std::move (m_index
);
6596 /* Return the mutable addrmap that is currently being created. */
6597 addrmap
*get_addrmap ()
6604 /* Hash function for a cutu_reader. */
6605 static hashval_t
hash_cutu_reader (const void *a
)
6607 const cutu_reader
*reader
= (const cutu_reader
*) a
;
6608 return reader
->cu
->per_cu
->index
;
6611 /* Equality function for cutu_reader. */
6612 static int eq_cutu_reader (const void *a
, const void *b
)
6614 const cutu_reader
*ra
= (const cutu_reader
*) a
;
6615 const int *rb
= (const int *) b
;
6616 return ra
->cu
->per_cu
->index
== *rb
;
6619 /* The abbrev cache used by this indexer. */
6620 abbrev_cache m_abbrev_cache
;
6621 /* A hash table of cutu_reader objects. */
6622 htab_up m_reader_hash
;
6623 /* The index that is being constructed. */
6624 std::unique_ptr
<cooked_index
> m_index
;
6626 /* Storage for the writeable addrmap. */
6627 auto_obstack m_addrmap_storage
;
6628 /* A writeable addrmap being constructed by this scanner. */
6632 /* An instance of this is created to index a CU. */
6634 class cooked_indexer
6638 cooked_indexer (cooked_index_storage
*storage
,
6639 dwarf2_per_cu_data
*per_cu
,
6640 enum language language
)
6641 : m_index_storage (storage
),
6643 m_language (language
),
6645 m_die_range_map (addrmap_create_mutable (&m_obstack
))
6649 DISABLE_COPY_AND_ASSIGN (cooked_indexer
);
6651 /* Index the given CU. */
6652 void make_index (cutu_reader
*reader
);
6656 /* A helper function to turn a section offset into an address that
6657 can be used in an addrmap. */
6658 CORE_ADDR
form_addr (sect_offset offset
, bool is_dwz
)
6660 CORE_ADDR value
= to_underlying (offset
);
6662 value
|= ((CORE_ADDR
) 1) << (8 * sizeof (CORE_ADDR
) - 1);
6666 /* A helper function to scan the PC bounds of READER and record them
6667 in the storage's addrmap. */
6668 void check_bounds (cutu_reader
*reader
);
6670 /* Ensure that the indicated CU exists. The cutu_reader for it is
6671 returned. FOR_SCANNING is true if the caller intends to scan all
6672 the DIEs in the CU; when false, this use is assumed to be to look
6673 up just a single DIE. */
6674 cutu_reader
*ensure_cu_exists (cutu_reader
*reader
,
6675 dwarf2_per_objfile
*per_objfile
,
6676 sect_offset sect_off
,
6680 /* Index DIEs in the READER starting at INFO_PTR. PARENT_ENTRY is
6681 the entry for the enclosing scope (nullptr at top level). FULLY
6682 is true when a full scan must be done -- in some languages,
6683 function scopes must be fully explored in order to find nested
6684 functions. This returns a pointer to just after the spot where
6686 const gdb_byte
*index_dies (cutu_reader
*reader
,
6687 const gdb_byte
*info_ptr
,
6688 const cooked_index_entry
*parent_entry
,
6691 /* Scan the attributes for a given DIE and update the out
6692 parameters. Returns a pointer to the byte after the DIE. */
6693 const gdb_byte
*scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
6694 cutu_reader
*reader
,
6695 const gdb_byte
*watermark_ptr
,
6696 const gdb_byte
*info_ptr
,
6697 const abbrev_info
*abbrev
,
6699 const char **linkage_name
,
6700 cooked_index_flag
*flags
,
6701 sect_offset
*sibling_offset
,
6702 const cooked_index_entry
**parent_entry
,
6703 CORE_ADDR
*maybe_defer
,
6704 bool for_specification
);
6706 /* Handle DW_TAG_imported_unit, by scanning the DIE to find
6707 DW_AT_import, and then scanning the referenced CU. Returns a
6708 pointer to the byte after the DIE. */
6709 const gdb_byte
*index_imported_unit (cutu_reader
*reader
,
6710 const gdb_byte
*info_ptr
,
6711 const abbrev_info
*abbrev
);
6713 /* Recursively read DIEs, recording the section offsets in
6714 m_die_range_map and then calling index_dies. */
6715 const gdb_byte
*recurse (cutu_reader
*reader
,
6716 const gdb_byte
*info_ptr
,
6717 const cooked_index_entry
*parent_entry
,
6720 /* The storage object, where the results are kept. */
6721 cooked_index_storage
*m_index_storage
;
6722 /* The CU that we are reading on behalf of. This object might be
6723 asked to index one CU but to treat the results as if they come
6724 from some including CU; in this case the including CU would be
6726 dwarf2_per_cu_data
*m_per_cu
;
6727 /* The language that we're assuming when reading. */
6728 enum language m_language
;
6730 /* Temporary storage. */
6731 auto_obstack m_obstack
;
6732 /* An addrmap that maps from section offsets (see the form_addr
6733 method) to newly-created entries. See m_deferred_entries to
6735 addrmap
*m_die_range_map
;
6737 /* A single deferred entry. */
6738 struct deferred_entry
6740 sect_offset die_offset
;
6742 CORE_ADDR spec_offset
;
6744 cooked_index_flag flags
;
6747 /* The generated DWARF can sometimes have the declaration for a
6748 method in a class (or perhaps namespace) scope, with the
6749 definition appearing outside this scope... just one of the many
6750 bad things about DWARF. In order to handle this situation, we
6751 defer certain entries until the end of scanning, at which point
6752 we'll know the containing context of all the DIEs that we might
6753 have scanned. This vector stores these deferred entries. */
6754 std::vector
<deferred_entry
> m_deferred_entries
;
6757 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6758 Process compilation unit THIS_CU for a psymtab. */
6761 process_psymtab_comp_unit (dwarf2_per_cu_data
*this_cu
,
6762 dwarf2_per_objfile
*per_objfile
,
6763 cooked_index_storage
*storage
)
6765 cutu_reader
reader (this_cu
, per_objfile
, nullptr, nullptr, false,
6766 storage
->get_abbrev_cache ());
6768 if (reader
.comp_unit_die
== nullptr)
6771 switch (reader
.comp_unit_die
->tag
)
6773 case DW_TAG_compile_unit
:
6774 this_cu
->unit_type
= DW_UT_compile
;
6776 case DW_TAG_partial_unit
:
6777 this_cu
->unit_type
= DW_UT_partial
;
6779 case DW_TAG_type_unit
:
6780 this_cu
->unit_type
= DW_UT_type
;
6783 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
6784 dwarf_tag_name (reader
.comp_unit_die
->tag
),
6785 sect_offset_str (reader
.cu
->per_cu
->sect_off
),
6786 objfile_name (per_objfile
->objfile
));
6793 else if (this_cu
->is_debug_types
)
6794 build_type_psymtabs_reader (&reader
, storage
);
6795 else if (reader
.comp_unit_die
->tag
!= DW_TAG_partial_unit
)
6798 if (this_cu
->scanned
.compare_exchange_strong (nope
, true))
6800 prepare_one_comp_unit (reader
.cu
, reader
.comp_unit_die
,
6802 gdb_assert (storage
!= nullptr);
6803 cooked_indexer
indexer (storage
, this_cu
, reader
.cu
->per_cu
->lang
);
6804 indexer
.make_index (&reader
);
6809 /* Reader function for build_type_psymtabs. */
6812 build_type_psymtabs_reader (cutu_reader
*reader
,
6813 cooked_index_storage
*storage
)
6815 struct dwarf2_cu
*cu
= reader
->cu
;
6816 struct dwarf2_per_cu_data
*per_cu
= cu
->per_cu
;
6817 struct die_info
*type_unit_die
= reader
->comp_unit_die
;
6819 gdb_assert (per_cu
->is_debug_types
);
6821 if (! type_unit_die
->has_children
)
6824 prepare_one_comp_unit (cu
, type_unit_die
, language_minimal
);
6826 gdb_assert (storage
!= nullptr);
6827 cooked_indexer
indexer (storage
, per_cu
, cu
->per_cu
->lang
);
6828 indexer
.make_index (reader
);
6831 /* Struct used to sort TUs by their abbreviation table offset. */
6833 struct tu_abbrev_offset
6835 tu_abbrev_offset (signatured_type
*sig_type_
, sect_offset abbrev_offset_
)
6836 : sig_type (sig_type_
), abbrev_offset (abbrev_offset_
)
6839 /* This is used when sorting. */
6840 bool operator< (const tu_abbrev_offset
&other
) const
6842 return abbrev_offset
< other
.abbrev_offset
;
6845 signatured_type
*sig_type
;
6846 sect_offset abbrev_offset
;
6849 /* Efficiently read all the type units.
6851 The efficiency is because we sort TUs by the abbrev table they use and
6852 only read each abbrev table once. In one program there are 200K TUs
6853 sharing 8K abbrev tables.
6855 The main purpose of this function is to support building the
6856 dwarf2_per_objfile->per_bfd->type_unit_groups table.
6857 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6858 can collapse the search space by grouping them by stmt_list.
6859 The savings can be significant, in the same program from above the 200K TUs
6860 share 8K stmt_list tables.
6862 FUNC is expected to call get_type_unit_group, which will create the
6863 struct type_unit_group if necessary and add it to
6864 dwarf2_per_objfile->per_bfd->type_unit_groups. */
6867 build_type_psymtabs (dwarf2_per_objfile
*per_objfile
,
6868 cooked_index_storage
*storage
)
6870 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6871 abbrev_table_up abbrev_table
;
6872 sect_offset abbrev_offset
;
6874 /* It's up to the caller to not call us multiple times. */
6875 gdb_assert (per_objfile
->per_bfd
->type_unit_groups
== NULL
);
6877 if (per_objfile
->per_bfd
->tu_stats
.nr_tus
== 0)
6880 /* TUs typically share abbrev tables, and there can be way more TUs than
6881 abbrev tables. Sort by abbrev table to reduce the number of times we
6882 read each abbrev table in.
6883 Alternatives are to punt or to maintain a cache of abbrev tables.
6884 This is simpler and efficient enough for now.
6886 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6887 symtab to use). Typically TUs with the same abbrev offset have the same
6888 stmt_list value too so in practice this should work well.
6890 The basic algorithm here is:
6892 sort TUs by abbrev table
6893 for each TU with same abbrev table:
6894 read abbrev table if first user
6895 read TU top level DIE
6896 [IWBN if DWO skeletons had DW_AT_stmt_list]
6899 dwarf_read_debug_printf ("Building type unit groups ...");
6901 /* Sort in a separate table to maintain the order of all_comp_units
6902 for .gdb_index: TU indices directly index all_type_units. */
6903 std::vector
<tu_abbrev_offset
> sorted_by_abbrev
;
6904 sorted_by_abbrev
.reserve (per_objfile
->per_bfd
->tu_stats
.nr_tus
);
6906 for (const auto &cu
: per_objfile
->per_bfd
->all_comp_units
)
6908 if (cu
->is_debug_types
)
6910 auto sig_type
= static_cast<signatured_type
*> (cu
.get ());
6911 sorted_by_abbrev
.emplace_back
6912 (sig_type
, read_abbrev_offset (per_objfile
, sig_type
->section
,
6913 sig_type
->sect_off
));
6917 std::sort (sorted_by_abbrev
.begin (), sorted_by_abbrev
.end ());
6919 abbrev_offset
= (sect_offset
) ~(unsigned) 0;
6921 for (const tu_abbrev_offset
&tu
: sorted_by_abbrev
)
6923 /* Switch to the next abbrev table if necessary. */
6924 if (abbrev_table
== NULL
6925 || tu
.abbrev_offset
!= abbrev_offset
)
6927 abbrev_offset
= tu
.abbrev_offset
;
6928 per_objfile
->per_bfd
->abbrev
.read (per_objfile
->objfile
);
6930 abbrev_table::read (&per_objfile
->per_bfd
->abbrev
, abbrev_offset
);
6931 ++tu_stats
->nr_uniq_abbrev_tables
;
6934 cutu_reader
reader (tu
.sig_type
, per_objfile
,
6935 abbrev_table
.get (), nullptr, false);
6936 if (!reader
.dummy_p
)
6937 build_type_psymtabs_reader (&reader
, storage
);
6941 /* Print collected type unit statistics. */
6944 print_tu_stats (dwarf2_per_objfile
*per_objfile
)
6946 struct tu_stats
*tu_stats
= &per_objfile
->per_bfd
->tu_stats
;
6948 dwarf_read_debug_printf ("Type unit statistics:");
6949 dwarf_read_debug_printf (" %d TUs", tu_stats
->nr_tus
);
6950 dwarf_read_debug_printf (" %d uniq abbrev tables",
6951 tu_stats
->nr_uniq_abbrev_tables
);
6952 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
6953 tu_stats
->nr_symtabs
);
6954 dwarf_read_debug_printf (" %d symtab sharers",
6955 tu_stats
->nr_symtab_sharers
);
6956 dwarf_read_debug_printf (" %d type units without a stmt_list",
6957 tu_stats
->nr_stmt_less_type_units
);
6958 dwarf_read_debug_printf (" %d all_type_units reallocs",
6959 tu_stats
->nr_all_type_units_reallocs
);
6962 struct skeleton_data
6964 dwarf2_per_objfile
*per_objfile
;
6965 cooked_index_storage
*storage
;
6968 /* Traversal function for process_skeletonless_type_unit.
6969 Read a TU in a DWO file and build partial symbols for it. */
6972 process_skeletonless_type_unit (void **slot
, void *info
)
6974 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
6975 skeleton_data
*data
= (skeleton_data
*) info
;
6977 /* If this TU doesn't exist in the global table, add it and read it in. */
6979 if (data
->per_objfile
->per_bfd
->signatured_types
== NULL
)
6980 data
->per_objfile
->per_bfd
->signatured_types
6981 = allocate_signatured_type_table ();
6983 signatured_type
find_entry (dwo_unit
->signature
);
6984 slot
= htab_find_slot (data
->per_objfile
->per_bfd
->signatured_types
.get (),
6985 &find_entry
, INSERT
);
6986 /* If we've already seen this type there's nothing to do. What's happening
6987 is we're doing our own version of comdat-folding here. */
6991 /* This does the job that create_all_comp_units would have done for
6993 signatured_type
*entry
6994 = add_type_unit (data
->per_objfile
, dwo_unit
->signature
, slot
);
6995 fill_in_sig_entry_from_dwo_entry (data
->per_objfile
, entry
, dwo_unit
);
6998 /* This does the job that build_type_psymtabs would have done. */
6999 cutu_reader
reader (entry
, data
->per_objfile
, nullptr, nullptr, false);
7000 if (!reader
.dummy_p
)
7001 build_type_psymtabs_reader (&reader
, data
->storage
);
7006 /* Traversal function for process_skeletonless_type_units. */
7009 process_dwo_file_for_skeletonless_type_units (void **slot
, void *info
)
7011 struct dwo_file
*dwo_file
= (struct dwo_file
*) *slot
;
7013 if (dwo_file
->tus
!= NULL
)
7014 htab_traverse_noresize (dwo_file
->tus
.get (),
7015 process_skeletonless_type_unit
, info
);
7020 /* Scan all TUs of DWO files, verifying we've processed them.
7021 This is needed in case a TU was emitted without its skeleton.
7022 Note: This can't be done until we know what all the DWO files are. */
7025 process_skeletonless_type_units (dwarf2_per_objfile
*per_objfile
,
7026 cooked_index_storage
*storage
)
7028 skeleton_data data
{ per_objfile
, storage
};
7030 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7031 if (get_dwp_file (per_objfile
) == NULL
7032 && per_objfile
->per_bfd
->dwo_files
!= NULL
)
7034 htab_traverse_noresize (per_objfile
->per_bfd
->dwo_files
.get (),
7035 process_dwo_file_for_skeletonless_type_units
,
7040 /* Build the partial symbol table by doing a quick pass through the
7041 .debug_info and .debug_abbrev sections. */
7044 dwarf2_build_psymtabs_hard (dwarf2_per_objfile
*per_objfile
)
7046 struct objfile
*objfile
= per_objfile
->objfile
;
7047 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
7049 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7050 objfile_name (objfile
));
7052 per_bfd
->map_info_sections (objfile
);
7054 cooked_index_storage index_storage
;
7055 create_all_comp_units (per_objfile
);
7056 build_type_psymtabs (per_objfile
, &index_storage
);
7057 std::vector
<std::unique_ptr
<cooked_index
>> indexes
;
7059 per_bfd
->quick_file_names_table
7060 = create_quick_file_names_table (per_bfd
->all_comp_units
.size ());
7061 if (!per_bfd
->debug_aranges
.empty ())
7062 read_addrmap_from_aranges (per_objfile
, &per_bfd
->debug_aranges
,
7063 index_storage
.get_addrmap ());
7066 /* Ensure that complaints are handled correctly. */
7067 complaint_interceptor complaint_handler
;
7069 using iter_type
= decltype (per_bfd
->all_comp_units
.begin ());
7071 /* Each thread returns a pair holding a cooked index, and a vector
7072 of errors that should be printed. The latter is done because
7073 GDB's I/O system is not thread-safe. run_on_main_thread could be
7074 used, but that would mean the messages are printed after the
7075 prompt, which looks weird. */
7076 using result_type
= std::pair
<std::unique_ptr
<cooked_index
>,
7077 std::vector
<gdb_exception
>>;
7078 std::vector
<result_type
> results
7079 = gdb::parallel_for_each (1, per_bfd
->all_comp_units
.begin (),
7080 per_bfd
->all_comp_units
.end (),
7081 [=] (iter_type iter
, iter_type end
)
7083 std::vector
<gdb_exception
> errors
;
7084 cooked_index_storage thread_storage
;
7085 for (; iter
!= end
; ++iter
)
7087 dwarf2_per_cu_data
*per_cu
= iter
->get ();
7090 process_psymtab_comp_unit (per_cu
, per_objfile
,
7093 catch (gdb_exception
&except
)
7095 errors
.push_back (std::move (except
));
7098 return result_type (thread_storage
.release (), std::move (errors
));
7101 /* Only show a given exception a single time. */
7102 std::unordered_set
<gdb_exception
> seen_exceptions
;
7103 for (auto &one_result
: results
)
7105 indexes
.push_back (std::move (one_result
.first
));
7106 for (auto &one_exc
: one_result
.second
)
7107 if (seen_exceptions
.insert (one_exc
).second
)
7108 exception_print (gdb_stderr
, one_exc
);
7112 /* This has to wait until we read the CUs, we need the list of DWOs. */
7113 process_skeletonless_type_units (per_objfile
, &index_storage
);
7115 if (dwarf_read_debug
> 0)
7116 print_tu_stats (per_objfile
);
7118 indexes
.push_back (index_storage
.release ());
7119 /* Remove any NULL entries. This might happen if parallel-for
7120 decides to throttle the number of threads that were used. */
7122 (std::remove_if (indexes
.begin (),
7124 [] (const std::unique_ptr
<cooked_index
> &entry
)
7126 return entry
== nullptr;
7129 indexes
.shrink_to_fit ();
7131 cooked_index_vector
*vec
= new cooked_index_vector (std::move (indexes
));
7132 per_bfd
->index_table
.reset (vec
);
7134 const cooked_index_entry
*main_entry
= vec
->get_main ();
7135 if (main_entry
!= nullptr)
7136 set_objfile_main_name (objfile
, main_entry
->name
,
7137 main_entry
->per_cu
->lang
);
7139 dwarf_read_debug_printf ("Done building psymtabs of %s",
7140 objfile_name (objfile
));
7144 read_comp_units_from_section (dwarf2_per_objfile
*per_objfile
,
7145 struct dwarf2_section_info
*section
,
7146 struct dwarf2_section_info
*abbrev_section
,
7147 unsigned int is_dwz
,
7148 htab_up
&types_htab
,
7149 rcuh_kind section_kind
)
7151 const gdb_byte
*info_ptr
;
7152 struct objfile
*objfile
= per_objfile
->objfile
;
7154 dwarf_read_debug_printf ("Reading %s for %s",
7155 section
->get_name (),
7156 section
->get_file_name ());
7158 section
->read (objfile
);
7160 info_ptr
= section
->buffer
;
7162 while (info_ptr
< section
->buffer
+ section
->size
)
7164 dwarf2_per_cu_data_up this_cu
;
7166 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
->buffer
);
7168 comp_unit_head cu_header
;
7169 read_and_check_comp_unit_head (per_objfile
, &cu_header
, section
,
7170 abbrev_section
, info_ptr
,
7173 /* Save the compilation unit for later lookup. */
7174 if (cu_header
.unit_type
!= DW_UT_type
)
7175 this_cu
= per_objfile
->per_bfd
->allocate_per_cu ();
7178 if (types_htab
== nullptr)
7179 types_htab
= allocate_signatured_type_table ();
7181 auto sig_type
= per_objfile
->per_bfd
->allocate_signatured_type
7182 (cu_header
.signature
);
7183 signatured_type
*sig_ptr
= sig_type
.get ();
7184 sig_type
->type_offset_in_tu
= cu_header
.type_cu_offset_in_tu
;
7185 this_cu
.reset (sig_type
.release ());
7187 void **slot
= htab_find_slot (types_htab
.get (), sig_ptr
, INSERT
);
7188 gdb_assert (slot
!= nullptr);
7189 if (*slot
!= nullptr)
7190 complaint (_("debug type entry at offset %s is duplicate to"
7191 " the entry at offset %s, signature %s"),
7192 sect_offset_str (sect_off
),
7193 sect_offset_str (sig_ptr
->sect_off
),
7194 hex_string (sig_ptr
->signature
));
7197 this_cu
->sect_off
= sect_off
;
7198 this_cu
->length
= cu_header
.length
+ cu_header
.initial_length_size
;
7199 this_cu
->is_dwz
= is_dwz
;
7200 this_cu
->section
= section
;
7202 info_ptr
= info_ptr
+ this_cu
->length
;
7203 per_objfile
->per_bfd
->all_comp_units
.push_back (std::move (this_cu
));
7207 /* Create a list of all compilation units in OBJFILE.
7208 This is only done for -readnow and building partial symtabs. */
7211 create_all_comp_units (dwarf2_per_objfile
*per_objfile
)
7215 read_comp_units_from_section (per_objfile
, &per_objfile
->per_bfd
->info
,
7216 &per_objfile
->per_bfd
->abbrev
, 0,
7217 types_htab
, rcuh_kind::COMPILE
);
7218 for (dwarf2_section_info
§ion
: per_objfile
->per_bfd
->types
)
7219 read_comp_units_from_section (per_objfile
, §ion
,
7220 &per_objfile
->per_bfd
->abbrev
, 0,
7221 types_htab
, rcuh_kind::TYPE
);
7223 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
);
7226 /* Pre-read the sections we'll need to construct an index. */
7227 struct objfile
*objfile
= per_objfile
->objfile
;
7228 dwz
->abbrev
.read (objfile
);
7229 dwz
->info
.read (objfile
);
7230 dwz
->str
.read (objfile
);
7231 dwz
->line
.read (objfile
);
7232 read_comp_units_from_section (per_objfile
, &dwz
->info
, &dwz
->abbrev
, 1,
7233 types_htab
, rcuh_kind::COMPILE
);
7236 per_objfile
->per_bfd
->signatured_types
= std::move (types_htab
);
7239 /* Return the initial uleb128 in the die at INFO_PTR. */
7242 peek_abbrev_code (bfd
*abfd
, const gdb_byte
*info_ptr
)
7244 unsigned int bytes_read
;
7246 return read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7249 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
7250 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
7252 Return the corresponding abbrev, or NULL if the number is zero (indicating
7253 an empty DIE). In either case *BYTES_READ will be set to the length of
7254 the initial number. */
7256 static const struct abbrev_info
*
7257 peek_die_abbrev (const die_reader_specs
&reader
,
7258 const gdb_byte
*info_ptr
, unsigned int *bytes_read
)
7260 dwarf2_cu
*cu
= reader
.cu
;
7261 bfd
*abfd
= reader
.abfd
;
7262 unsigned int abbrev_number
7263 = read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
7265 if (abbrev_number
== 0)
7268 const abbrev_info
*abbrev
7269 = reader
.abbrev_table
->lookup_abbrev (abbrev_number
);
7272 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7273 " at offset %s [in module %s]"),
7274 abbrev_number
, cu
->per_cu
->is_debug_types
? "TU" : "CU",
7275 sect_offset_str (cu
->header
.sect_off
), bfd_get_filename (abfd
));
7281 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7282 Returns a pointer to the end of a series of DIEs, terminated by an empty
7283 DIE. Any children of the skipped DIEs will also be skipped. */
7285 static const gdb_byte
*
7286 skip_children (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
)
7290 unsigned int bytes_read
;
7291 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
7295 return info_ptr
+ bytes_read
;
7297 info_ptr
= skip_one_die (reader
, info_ptr
+ bytes_read
, abbrev
);
7301 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7302 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7303 abbrev corresponding to that skipped uleb128 should be passed in
7306 If DO_SKIP_CHILDREN is true, or if the DIE has no children, this
7307 returns a pointer to this DIE's sibling, skipping any children.
7308 Otherwise, returns a pointer to the DIE's first child. */
7310 static const gdb_byte
*
7311 skip_one_die (const struct die_reader_specs
*reader
, const gdb_byte
*info_ptr
,
7312 const struct abbrev_info
*abbrev
, bool do_skip_children
)
7314 unsigned int bytes_read
;
7315 struct attribute attr
;
7316 bfd
*abfd
= reader
->abfd
;
7317 struct dwarf2_cu
*cu
= reader
->cu
;
7318 const gdb_byte
*buffer
= reader
->buffer
;
7319 const gdb_byte
*buffer_end
= reader
->buffer_end
;
7320 unsigned int form
, i
;
7322 if (do_skip_children
&& abbrev
->sibling_offset
!= (unsigned short) -1)
7324 /* We only handle DW_FORM_ref4 here. */
7325 const gdb_byte
*sibling_data
= info_ptr
+ abbrev
->sibling_offset
;
7326 unsigned int offset
= read_4_bytes (abfd
, sibling_data
);
7327 const gdb_byte
*sibling_ptr
7328 = buffer
+ to_underlying (cu
->header
.sect_off
) + offset
;
7329 if (sibling_ptr
>= info_ptr
&& sibling_ptr
< reader
->buffer_end
)
7331 /* Fall through to the slow way. */
7333 else if (abbrev
->size_if_constant
!= 0)
7335 info_ptr
+= abbrev
->size_if_constant
;
7336 if (do_skip_children
&& abbrev
->has_children
)
7337 return skip_children (reader
, info_ptr
);
7341 for (i
= 0; i
< abbrev
->num_attrs
; i
++)
7343 /* The only abbrev we care about is DW_AT_sibling. */
7344 if (do_skip_children
&& abbrev
->attrs
[i
].name
== DW_AT_sibling
)
7346 read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
7347 if (attr
.form
== DW_FORM_ref_addr
)
7348 complaint (_("ignoring absolute DW_AT_sibling"));
7351 sect_offset off
= attr
.get_ref_die_offset ();
7352 const gdb_byte
*sibling_ptr
= buffer
+ to_underlying (off
);
7354 if (sibling_ptr
< info_ptr
)
7355 complaint (_("DW_AT_sibling points backwards"));
7356 else if (sibling_ptr
> reader
->buffer_end
)
7357 reader
->die_section
->overflow_complaint ();
7363 /* If it isn't DW_AT_sibling, skip this attribute. */
7364 form
= abbrev
->attrs
[i
].form
;
7368 case DW_FORM_ref_addr
:
7369 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7370 and later it is offset sized. */
7371 if (cu
->header
.version
== 2)
7372 info_ptr
+= cu
->header
.addr_size
;
7374 info_ptr
+= cu
->header
.offset_size
;
7376 case DW_FORM_GNU_ref_alt
:
7377 info_ptr
+= cu
->header
.offset_size
;
7380 info_ptr
+= cu
->header
.addr_size
;
7388 case DW_FORM_flag_present
:
7389 case DW_FORM_implicit_const
:
7406 case DW_FORM_ref_sig8
:
7409 case DW_FORM_data16
:
7412 case DW_FORM_string
:
7413 read_direct_string (abfd
, info_ptr
, &bytes_read
);
7414 info_ptr
+= bytes_read
;
7416 case DW_FORM_sec_offset
:
7418 case DW_FORM_GNU_strp_alt
:
7419 info_ptr
+= cu
->header
.offset_size
;
7421 case DW_FORM_exprloc
:
7423 info_ptr
+= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7424 info_ptr
+= bytes_read
;
7426 case DW_FORM_block1
:
7427 info_ptr
+= 1 + read_1_byte (abfd
, info_ptr
);
7429 case DW_FORM_block2
:
7430 info_ptr
+= 2 + read_2_bytes (abfd
, info_ptr
);
7432 case DW_FORM_block4
:
7433 info_ptr
+= 4 + read_4_bytes (abfd
, info_ptr
);
7439 case DW_FORM_ref_udata
:
7440 case DW_FORM_GNU_addr_index
:
7441 case DW_FORM_GNU_str_index
:
7442 case DW_FORM_rnglistx
:
7443 case DW_FORM_loclistx
:
7444 info_ptr
= safe_skip_leb128 (info_ptr
, buffer_end
);
7446 case DW_FORM_indirect
:
7447 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
7448 info_ptr
+= bytes_read
;
7449 /* We need to continue parsing from here, so just go back to
7451 goto skip_attribute
;
7454 error (_("Dwarf Error: Cannot handle %s "
7455 "in DWARF reader [in module %s]"),
7456 dwarf_form_name (form
),
7457 bfd_get_filename (abfd
));
7461 if (do_skip_children
&& abbrev
->has_children
)
7462 return skip_children (reader
, info_ptr
);
7467 /* Reading in full CUs. */
7469 /* Add PER_CU to the queue. */
7472 queue_comp_unit (dwarf2_per_cu_data
*per_cu
,
7473 dwarf2_per_objfile
*per_objfile
,
7474 enum language pretend_language
)
7478 gdb_assert (per_objfile
->per_bfd
->queue
.has_value ());
7479 per_cu
->per_bfd
->queue
->emplace (per_cu
, per_objfile
, pretend_language
);
7482 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
7484 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7487 Return true if maybe_queue_comp_unit requires the caller to load the CU's
7488 DIEs, false otherwise.
7490 Explanation: there is an invariant that if a CU is queued for expansion
7491 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
7492 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
7493 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
7494 are not yet loaded, the the caller must load the CU's DIEs to ensure the
7495 invariant is respected.
7497 The caller is therefore not required to load the CU's DIEs (we return false)
7500 - the CU is already expanded, and therefore does not get enqueued
7501 - the CU gets enqueued for expansion, but its DIEs are already loaded
7503 Note that the caller should not use this function's return value as an
7504 indicator of whether the CU's DIEs are loaded right now, it should check
7505 that by calling `dwarf2_per_objfile::get_cu` instead. */
7508 maybe_queue_comp_unit (struct dwarf2_cu
*dependent_cu
,
7509 dwarf2_per_cu_data
*per_cu
,
7510 dwarf2_per_objfile
*per_objfile
,
7511 enum language pretend_language
)
7513 /* Mark the dependence relation so that we don't flush PER_CU
7515 if (dependent_cu
!= NULL
)
7516 dependent_cu
->add_dependence (per_cu
);
7518 /* If it's already on the queue, we have nothing to do. */
7521 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
7523 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
7525 /* If the CU is queued for expansion, it should not already be
7527 gdb_assert (!per_objfile
->symtab_set_p (per_cu
));
7529 /* The DIEs are already loaded, the caller doesn't need to do it. */
7533 bool queued
= false;
7534 if (!per_objfile
->symtab_set_p (per_cu
))
7536 /* Add it to the queue. */
7537 queue_comp_unit (per_cu
, per_objfile
, pretend_language
);
7541 /* If the compilation unit is already loaded, just mark it as
7543 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7547 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
7548 and the DIEs are not already loaded. */
7549 return queued
&& cu
== nullptr;
7552 /* Process the queue. */
7555 process_queue (dwarf2_per_objfile
*per_objfile
)
7557 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
7558 objfile_name (per_objfile
->objfile
));
7560 /* The queue starts out with one item, but following a DIE reference
7561 may load a new CU, adding it to the end of the queue. */
7562 while (!per_objfile
->per_bfd
->queue
->empty ())
7564 dwarf2_queue_item
&item
= per_objfile
->per_bfd
->queue
->front ();
7565 dwarf2_per_cu_data
*per_cu
= item
.per_cu
;
7567 if (!per_objfile
->symtab_set_p (per_cu
))
7569 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
7571 /* Skip dummy CUs. */
7574 unsigned int debug_print_threshold
;
7577 if (per_cu
->is_debug_types
)
7579 struct signatured_type
*sig_type
=
7580 (struct signatured_type
*) per_cu
;
7582 sprintf (buf
, "TU %s at offset %s",
7583 hex_string (sig_type
->signature
),
7584 sect_offset_str (per_cu
->sect_off
));
7585 /* There can be 100s of TUs.
7586 Only print them in verbose mode. */
7587 debug_print_threshold
= 2;
7591 sprintf (buf
, "CU at offset %s",
7592 sect_offset_str (per_cu
->sect_off
));
7593 debug_print_threshold
= 1;
7596 if (dwarf_read_debug
>= debug_print_threshold
)
7597 dwarf_read_debug_printf ("Expanding symtab of %s", buf
);
7599 if (per_cu
->is_debug_types
)
7600 process_full_type_unit (cu
, item
.pretend_language
);
7602 process_full_comp_unit (cu
, item
.pretend_language
);
7604 if (dwarf_read_debug
>= debug_print_threshold
)
7605 dwarf_read_debug_printf ("Done expanding %s", buf
);
7610 per_objfile
->per_bfd
->queue
->pop ();
7613 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
7614 objfile_name (per_objfile
->objfile
));
7617 /* Trivial hash function for die_info: the hash value of a DIE
7618 is its offset in .debug_info for this objfile. */
7621 die_hash (const void *item
)
7623 const struct die_info
*die
= (const struct die_info
*) item
;
7625 return to_underlying (die
->sect_off
);
7628 /* Trivial comparison function for die_info structures: two DIEs
7629 are equal if they have the same offset. */
7632 die_eq (const void *item_lhs
, const void *item_rhs
)
7634 const struct die_info
*die_lhs
= (const struct die_info
*) item_lhs
;
7635 const struct die_info
*die_rhs
= (const struct die_info
*) item_rhs
;
7637 return die_lhs
->sect_off
== die_rhs
->sect_off
;
7640 /* Load the DIEs associated with PER_CU into memory.
7642 In some cases, the caller, while reading partial symbols, will need to load
7643 the full symbols for the CU for some reason. It will already have a
7644 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
7645 rather than creating a new one. */
7648 load_full_comp_unit (dwarf2_per_cu_data
*this_cu
,
7649 dwarf2_per_objfile
*per_objfile
,
7650 dwarf2_cu
*existing_cu
,
7652 enum language pretend_language
)
7654 gdb_assert (! this_cu
->is_debug_types
);
7656 cutu_reader
reader (this_cu
, per_objfile
, NULL
, existing_cu
, skip_partial
);
7660 struct dwarf2_cu
*cu
= reader
.cu
;
7661 const gdb_byte
*info_ptr
= reader
.info_ptr
;
7663 gdb_assert (cu
->die_hash
== NULL
);
7665 htab_create_alloc_ex (cu
->header
.length
/ 12,
7669 &cu
->comp_unit_obstack
,
7670 hashtab_obstack_allocate
,
7671 dummy_obstack_deallocate
);
7673 if (reader
.comp_unit_die
->has_children
)
7674 reader
.comp_unit_die
->child
7675 = read_die_and_siblings (&reader
, reader
.info_ptr
,
7676 &info_ptr
, reader
.comp_unit_die
);
7677 cu
->dies
= reader
.comp_unit_die
;
7678 /* comp_unit_die is not stored in die_hash, no need. */
7680 /* We try not to read any attributes in this function, because not
7681 all CUs needed for references have been loaded yet, and symbol
7682 table processing isn't initialized. But we have to set the CU language,
7683 or we won't be able to build types correctly.
7684 Similarly, if we do not read the producer, we can not apply
7685 producer-specific interpretation. */
7686 prepare_one_comp_unit (cu
, cu
->dies
, pretend_language
);
7691 /* Add a DIE to the delayed physname list. */
7694 add_to_method_list (struct type
*type
, int fnfield_index
, int index
,
7695 const char *name
, struct die_info
*die
,
7696 struct dwarf2_cu
*cu
)
7698 struct delayed_method_info mi
;
7700 mi
.fnfield_index
= fnfield_index
;
7704 cu
->method_list
.push_back (mi
);
7707 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
7708 "const" / "volatile". If so, decrements LEN by the length of the
7709 modifier and return true. Otherwise return false. */
7713 check_modifier (const char *physname
, size_t &len
, const char (&mod
)[N
])
7715 size_t mod_len
= sizeof (mod
) - 1;
7716 if (len
> mod_len
&& startswith (physname
+ (len
- mod_len
), mod
))
7724 /* Compute the physnames of any methods on the CU's method list.
7726 The computation of method physnames is delayed in order to avoid the
7727 (bad) condition that one of the method's formal parameters is of an as yet
7731 compute_delayed_physnames (struct dwarf2_cu
*cu
)
7733 /* Only C++ delays computing physnames. */
7734 if (cu
->method_list
.empty ())
7736 gdb_assert (cu
->per_cu
->lang
== language_cplus
);
7738 for (const delayed_method_info
&mi
: cu
->method_list
)
7740 const char *physname
;
7741 struct fn_fieldlist
*fn_flp
7742 = &TYPE_FN_FIELDLIST (mi
.type
, mi
.fnfield_index
);
7743 physname
= dwarf2_physname (mi
.name
, mi
.die
, cu
);
7744 TYPE_FN_FIELD_PHYSNAME (fn_flp
->fn_fields
, mi
.index
)
7745 = physname
? physname
: "";
7747 /* Since there's no tag to indicate whether a method is a
7748 const/volatile overload, extract that information out of the
7750 if (physname
!= NULL
)
7752 size_t len
= strlen (physname
);
7756 if (physname
[len
] == ')') /* shortcut */
7758 else if (check_modifier (physname
, len
, " const"))
7759 TYPE_FN_FIELD_CONST (fn_flp
->fn_fields
, mi
.index
) = 1;
7760 else if (check_modifier (physname
, len
, " volatile"))
7761 TYPE_FN_FIELD_VOLATILE (fn_flp
->fn_fields
, mi
.index
) = 1;
7768 /* The list is no longer needed. */
7769 cu
->method_list
.clear ();
7772 /* Go objects should be embedded in a DW_TAG_module DIE,
7773 and it's not clear if/how imported objects will appear.
7774 To keep Go support simple until that's worked out,
7775 go back through what we've read and create something usable.
7776 We could do this while processing each DIE, and feels kinda cleaner,
7777 but that way is more invasive.
7778 This is to, for example, allow the user to type "p var" or "b main"
7779 without having to specify the package name, and allow lookups
7780 of module.object to work in contexts that use the expression
7784 fixup_go_packaging (struct dwarf2_cu
*cu
)
7786 gdb::unique_xmalloc_ptr
<char> package_name
;
7787 struct pending
*list
;
7790 for (list
= *cu
->get_builder ()->get_global_symbols ();
7794 for (i
= 0; i
< list
->nsyms
; ++i
)
7796 struct symbol
*sym
= list
->symbol
[i
];
7798 if (sym
->language () == language_go
7799 && sym
->aclass () == LOC_BLOCK
)
7801 gdb::unique_xmalloc_ptr
<char> this_package_name
7802 (go_symbol_package_name (sym
));
7804 if (this_package_name
== NULL
)
7806 if (package_name
== NULL
)
7807 package_name
= std::move (this_package_name
);
7810 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7811 if (strcmp (package_name
.get (), this_package_name
.get ()) != 0)
7812 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
7813 (sym
->symtab () != NULL
7814 ? symtab_to_filename_for_display
7816 : objfile_name (objfile
)),
7817 this_package_name
.get (), package_name
.get ());
7823 if (package_name
!= NULL
)
7825 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
7826 const char *saved_package_name
= objfile
->intern (package_name
.get ());
7827 struct type
*type
= init_type (objfile
, TYPE_CODE_MODULE
, 0,
7828 saved_package_name
);
7831 sym
= new (&objfile
->objfile_obstack
) symbol
;
7832 sym
->set_language (language_go
, &objfile
->objfile_obstack
);
7833 sym
->compute_and_set_names (saved_package_name
, false, objfile
->per_bfd
);
7834 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7835 e.g., "main" finds the "main" module and not C's main(). */
7836 sym
->set_domain (STRUCT_DOMAIN
);
7837 sym
->set_aclass_index (LOC_TYPEDEF
);
7838 sym
->set_type (type
);
7840 add_symbol_to_list (sym
, cu
->get_builder ()->get_global_symbols ());
7844 /* Allocate a fully-qualified name consisting of the two parts on the
7848 rust_fully_qualify (struct obstack
*obstack
, const char *p1
, const char *p2
)
7850 return obconcat (obstack
, p1
, "::", p2
, (char *) NULL
);
7853 /* A helper that allocates a variant part to attach to a Rust enum
7854 type. OBSTACK is where the results should be allocated. TYPE is
7855 the type we're processing. DISCRIMINANT_INDEX is the index of the
7856 discriminant. It must be the index of one of the fields of TYPE,
7857 or -1 to mean there is no discriminant (univariant enum).
7858 DEFAULT_INDEX is the index of the default field; or -1 if there is
7859 no default. RANGES is indexed by "effective" field number (the
7860 field index, but omitting the discriminant and default fields) and
7861 must hold the discriminant values used by the variants. Note that
7862 RANGES must have a lifetime at least as long as OBSTACK -- either
7863 already allocated on it, or static. */
7866 alloc_rust_variant (struct obstack
*obstack
, struct type
*type
,
7867 int discriminant_index
, int default_index
,
7868 gdb::array_view
<discriminant_range
> ranges
)
7870 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
7871 gdb_assert (discriminant_index
== -1
7872 || (discriminant_index
>= 0
7873 && discriminant_index
< type
->num_fields ()));
7874 gdb_assert (default_index
== -1
7875 || (default_index
>= 0 && default_index
< type
->num_fields ()));
7877 /* We have one variant for each non-discriminant field. */
7878 int n_variants
= type
->num_fields ();
7879 if (discriminant_index
!= -1)
7882 variant
*variants
= new (obstack
) variant
[n_variants
];
7885 for (int i
= 0; i
< type
->num_fields (); ++i
)
7887 if (i
== discriminant_index
)
7890 variants
[var_idx
].first_field
= i
;
7891 variants
[var_idx
].last_field
= i
+ 1;
7893 /* The default field does not need a range, but other fields do.
7894 We skipped the discriminant above. */
7895 if (i
!= default_index
)
7897 variants
[var_idx
].discriminants
= ranges
.slice (range_idx
, 1);
7904 gdb_assert (range_idx
== ranges
.size ());
7905 gdb_assert (var_idx
== n_variants
);
7907 variant_part
*part
= new (obstack
) variant_part
;
7908 part
->discriminant_index
= discriminant_index
;
7909 /* If there is no discriminant, then whether it is signed is of no
7912 = (discriminant_index
== -1
7914 : type
->field (discriminant_index
).type ()->is_unsigned ());
7915 part
->variants
= gdb::array_view
<variant
> (variants
, n_variants
);
7917 void *storage
= obstack_alloc (obstack
, sizeof (gdb::array_view
<variant_part
>));
7918 gdb::array_view
<variant_part
> *prop_value
7919 = new (storage
) gdb::array_view
<variant_part
> (part
, 1);
7921 struct dynamic_prop prop
;
7922 prop
.set_variant_parts (prop_value
);
7924 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
7927 /* Some versions of rustc emitted enums in an unusual way.
7929 Ordinary enums were emitted as unions. The first element of each
7930 structure in the union was named "RUST$ENUM$DISR". This element
7931 held the discriminant.
7933 These versions of Rust also implemented the "non-zero"
7934 optimization. When the enum had two values, and one is empty and
7935 the other holds a pointer that cannot be zero, the pointer is used
7936 as the discriminant, with a zero value meaning the empty variant.
7937 Here, the union's first member is of the form
7938 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
7939 where the fieldnos are the indices of the fields that should be
7940 traversed in order to find the field (which may be several fields deep)
7941 and the variantname is the name of the variant of the case when the
7944 This function recognizes whether TYPE is of one of these forms,
7945 and, if so, smashes it to be a variant type. */
7948 quirk_rust_enum (struct type
*type
, struct objfile
*objfile
)
7950 gdb_assert (type
->code () == TYPE_CODE_UNION
);
7952 /* We don't need to deal with empty enums. */
7953 if (type
->num_fields () == 0)
7956 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
7957 if (type
->num_fields () == 1
7958 && startswith (type
->field (0).name (), RUST_ENUM_PREFIX
))
7960 const char *name
= type
->field (0).name () + strlen (RUST_ENUM_PREFIX
);
7962 /* Decode the field name to find the offset of the
7964 ULONGEST bit_offset
= 0;
7965 struct type
*field_type
= type
->field (0).type ();
7966 while (name
[0] >= '0' && name
[0] <= '9')
7969 unsigned long index
= strtoul (name
, &tail
, 10);
7972 || index
>= field_type
->num_fields ()
7973 || (field_type
->field (index
).loc_kind ()
7974 != FIELD_LOC_KIND_BITPOS
))
7976 complaint (_("Could not parse Rust enum encoding string \"%s\""
7978 type
->field (0).name (),
7979 objfile_name (objfile
));
7984 bit_offset
+= field_type
->field (index
).loc_bitpos ();
7985 field_type
= field_type
->field (index
).type ();
7988 /* Smash this type to be a structure type. We have to do this
7989 because the type has already been recorded. */
7990 type
->set_code (TYPE_CODE_STRUCT
);
7991 type
->set_num_fields (3);
7992 /* Save the field we care about. */
7993 struct field saved_field
= type
->field (0);
7995 ((struct field
*) TYPE_ZALLOC (type
, 3 * sizeof (struct field
)));
7997 /* Put the discriminant at index 0. */
7998 type
->field (0).set_type (field_type
);
7999 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8000 type
->field (0).set_name ("<<discriminant>>");
8001 type
->field (0).set_loc_bitpos (bit_offset
);
8003 /* The order of fields doesn't really matter, so put the real
8004 field at index 1 and the data-less field at index 2. */
8005 type
->field (1) = saved_field
;
8006 type
->field (1).set_name
8007 (rust_last_path_segment (type
->field (1).type ()->name ()));
8008 type
->field (1).type ()->set_name
8009 (rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8010 type
->field (1).name ()));
8012 const char *dataless_name
8013 = rust_fully_qualify (&objfile
->objfile_obstack
, type
->name (),
8015 struct type
*dataless_type
= init_type (objfile
, TYPE_CODE_VOID
, 0,
8017 type
->field (2).set_type (dataless_type
);
8018 /* NAME points into the original discriminant name, which
8019 already has the correct lifetime. */
8020 type
->field (2).set_name (name
);
8021 type
->field (2).set_loc_bitpos (0);
8023 /* Indicate that this is a variant type. */
8024 static discriminant_range ranges
[1] = { { 0, 0 } };
8025 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, 1, ranges
);
8027 /* A union with a single anonymous field is probably an old-style
8029 else if (type
->num_fields () == 1 && streq (type
->field (0).name (), ""))
8031 /* Smash this type to be a structure type. We have to do this
8032 because the type has already been recorded. */
8033 type
->set_code (TYPE_CODE_STRUCT
);
8035 struct type
*field_type
= type
->field (0).type ();
8036 const char *variant_name
8037 = rust_last_path_segment (field_type
->name ());
8038 type
->field (0).set_name (variant_name
);
8039 field_type
->set_name
8040 (rust_fully_qualify (&objfile
->objfile_obstack
,
8041 type
->name (), variant_name
));
8043 alloc_rust_variant (&objfile
->objfile_obstack
, type
, -1, 0, {});
8047 struct type
*disr_type
= nullptr;
8048 for (int i
= 0; i
< type
->num_fields (); ++i
)
8050 disr_type
= type
->field (i
).type ();
8052 if (disr_type
->code () != TYPE_CODE_STRUCT
)
8054 /* All fields of a true enum will be structs. */
8057 else if (disr_type
->num_fields () == 0)
8059 /* Could be data-less variant, so keep going. */
8060 disr_type
= nullptr;
8062 else if (strcmp (disr_type
->field (0).name (),
8063 "RUST$ENUM$DISR") != 0)
8065 /* Not a Rust enum. */
8075 /* If we got here without a discriminant, then it's probably
8077 if (disr_type
== nullptr)
8080 /* Smash this type to be a structure type. We have to do this
8081 because the type has already been recorded. */
8082 type
->set_code (TYPE_CODE_STRUCT
);
8084 /* Make space for the discriminant field. */
8085 struct field
*disr_field
= &disr_type
->field (0);
8087 = (struct field
*) TYPE_ZALLOC (type
, ((type
->num_fields () + 1)
8088 * sizeof (struct field
)));
8089 memcpy (new_fields
+ 1, type
->fields (),
8090 type
->num_fields () * sizeof (struct field
));
8091 type
->set_fields (new_fields
);
8092 type
->set_num_fields (type
->num_fields () + 1);
8094 /* Install the discriminant at index 0 in the union. */
8095 type
->field (0) = *disr_field
;
8096 TYPE_FIELD_ARTIFICIAL (type
, 0) = 1;
8097 type
->field (0).set_name ("<<discriminant>>");
8099 /* We need a way to find the correct discriminant given a
8100 variant name. For convenience we build a map here. */
8101 struct type
*enum_type
= disr_field
->type ();
8102 std::unordered_map
<std::string
, ULONGEST
> discriminant_map
;
8103 for (int i
= 0; i
< enum_type
->num_fields (); ++i
)
8105 if (enum_type
->field (i
).loc_kind () == FIELD_LOC_KIND_ENUMVAL
)
8108 = rust_last_path_segment (enum_type
->field (i
).name ());
8109 discriminant_map
[name
] = enum_type
->field (i
).loc_enumval ();
8113 int n_fields
= type
->num_fields ();
8114 /* We don't need a range entry for the discriminant, but we do
8115 need one for every other field, as there is no default
8117 discriminant_range
*ranges
= XOBNEWVEC (&objfile
->objfile_obstack
,
8120 /* Skip the discriminant here. */
8121 for (int i
= 1; i
< n_fields
; ++i
)
8123 /* Find the final word in the name of this variant's type.
8124 That name can be used to look up the correct
8126 const char *variant_name
8127 = rust_last_path_segment (type
->field (i
).type ()->name ());
8129 auto iter
= discriminant_map
.find (variant_name
);
8130 if (iter
!= discriminant_map
.end ())
8132 ranges
[i
- 1].low
= iter
->second
;
8133 ranges
[i
- 1].high
= iter
->second
;
8136 /* In Rust, each element should have the size of the
8138 TYPE_LENGTH (type
->field (i
).type ()) = TYPE_LENGTH (type
);
8140 /* Remove the discriminant field, if it exists. */
8141 struct type
*sub_type
= type
->field (i
).type ();
8142 if (sub_type
->num_fields () > 0)
8144 sub_type
->set_num_fields (sub_type
->num_fields () - 1);
8145 sub_type
->set_fields (sub_type
->fields () + 1);
8147 type
->field (i
).set_name (variant_name
);
8149 (rust_fully_qualify (&objfile
->objfile_obstack
,
8150 type
->name (), variant_name
));
8153 /* Indicate that this is a variant type. */
8154 alloc_rust_variant (&objfile
->objfile_obstack
, type
, 0, -1,
8155 gdb::array_view
<discriminant_range
> (ranges
,
8160 /* Rewrite some Rust unions to be structures with variants parts. */
8163 rust_union_quirks (struct dwarf2_cu
*cu
)
8165 gdb_assert (cu
->per_cu
->lang
== language_rust
);
8166 for (type
*type_
: cu
->rust_unions
)
8167 quirk_rust_enum (type_
, cu
->per_objfile
->objfile
);
8168 /* We don't need this any more. */
8169 cu
->rust_unions
.clear ();
8174 type_unit_group_unshareable
*
8175 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group
*tu_group
)
8177 auto iter
= this->m_type_units
.find (tu_group
);
8178 if (iter
!= this->m_type_units
.end ())
8179 return iter
->second
.get ();
8181 type_unit_group_unshareable_up
uniq (new type_unit_group_unshareable
);
8182 type_unit_group_unshareable
*result
= uniq
.get ();
8183 this->m_type_units
[tu_group
] = std::move (uniq
);
8188 dwarf2_per_objfile::get_type_for_signatured_type
8189 (signatured_type
*sig_type
) const
8191 auto iter
= this->m_type_map
.find (sig_type
);
8192 if (iter
== this->m_type_map
.end ())
8195 return iter
->second
;
8198 void dwarf2_per_objfile::set_type_for_signatured_type
8199 (signatured_type
*sig_type
, struct type
*type
)
8201 gdb_assert (this->m_type_map
.find (sig_type
) == this->m_type_map
.end ());
8203 this->m_type_map
[sig_type
] = type
;
8206 /* A helper function for computing the list of all symbol tables
8207 included by PER_CU. */
8210 recursively_compute_inclusions (std::vector
<compunit_symtab
*> *result
,
8211 htab_t all_children
, htab_t all_type_symtabs
,
8212 dwarf2_per_cu_data
*per_cu
,
8213 dwarf2_per_objfile
*per_objfile
,
8214 struct compunit_symtab
*immediate_parent
)
8216 void **slot
= htab_find_slot (all_children
, per_cu
, INSERT
);
8219 /* This inclusion and its children have been processed. */
8225 /* Only add a CU if it has a symbol table. */
8226 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8229 /* If this is a type unit only add its symbol table if we haven't
8230 seen it yet (type unit per_cu's can share symtabs). */
8231 if (per_cu
->is_debug_types
)
8233 slot
= htab_find_slot (all_type_symtabs
, cust
, INSERT
);
8237 result
->push_back (cust
);
8238 if (cust
->user
== NULL
)
8239 cust
->user
= immediate_parent
;
8244 result
->push_back (cust
);
8245 if (cust
->user
== NULL
)
8246 cust
->user
= immediate_parent
;
8250 if (!per_cu
->imported_symtabs_empty ())
8251 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8253 recursively_compute_inclusions (result
, all_children
,
8254 all_type_symtabs
, ptr
, per_objfile
,
8259 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8263 compute_compunit_symtab_includes (dwarf2_per_cu_data
*per_cu
,
8264 dwarf2_per_objfile
*per_objfile
)
8266 gdb_assert (! per_cu
->is_debug_types
);
8268 if (!per_cu
->imported_symtabs_empty ())
8271 std::vector
<compunit_symtab
*> result_symtabs
;
8272 compunit_symtab
*cust
= per_objfile
->get_symtab (per_cu
);
8274 /* If we don't have a symtab, we can just skip this case. */
8278 htab_up
all_children (htab_create_alloc (1, htab_hash_pointer
,
8280 NULL
, xcalloc
, xfree
));
8281 htab_up
all_type_symtabs (htab_create_alloc (1, htab_hash_pointer
,
8283 NULL
, xcalloc
, xfree
));
8285 for (dwarf2_per_cu_data
*ptr
: *per_cu
->imported_symtabs
)
8287 recursively_compute_inclusions (&result_symtabs
, all_children
.get (),
8288 all_type_symtabs
.get (), ptr
,
8292 /* Now we have a transitive closure of all the included symtabs. */
8293 len
= result_symtabs
.size ();
8295 = XOBNEWVEC (&per_objfile
->objfile
->objfile_obstack
,
8296 struct compunit_symtab
*, len
+ 1);
8297 memcpy (cust
->includes
, result_symtabs
.data (),
8298 len
* sizeof (compunit_symtab
*));
8299 cust
->includes
[len
] = NULL
;
8303 /* Compute the 'includes' field for the symtabs of all the CUs we just
8307 process_cu_includes (dwarf2_per_objfile
*per_objfile
)
8309 for (dwarf2_per_cu_data
*iter
: per_objfile
->per_bfd
->just_read_cus
)
8311 if (! iter
->is_debug_types
)
8312 compute_compunit_symtab_includes (iter
, per_objfile
);
8315 per_objfile
->per_bfd
->just_read_cus
.clear ();
8318 /* Generate full symbol information for CU, whose DIEs have
8319 already been loaded into memory. */
8322 process_full_comp_unit (dwarf2_cu
*cu
, enum language pretend_language
)
8324 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8325 struct objfile
*objfile
= per_objfile
->objfile
;
8326 struct gdbarch
*gdbarch
= objfile
->arch ();
8327 CORE_ADDR lowpc
, highpc
;
8328 struct compunit_symtab
*cust
;
8330 struct block
*static_block
;
8333 baseaddr
= objfile
->text_section_offset ();
8335 /* Clear the list here in case something was left over. */
8336 cu
->method_list
.clear ();
8338 dwarf2_find_base_address (cu
->dies
, cu
);
8340 /* Before we start reading the top-level DIE, ensure it has a valid tag
8342 switch (cu
->dies
->tag
)
8344 case DW_TAG_compile_unit
:
8345 case DW_TAG_partial_unit
:
8346 case DW_TAG_type_unit
:
8349 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
8350 dwarf_tag_name (cu
->dies
->tag
),
8351 sect_offset_str (cu
->per_cu
->sect_off
),
8352 objfile_name (per_objfile
->objfile
));
8355 /* Do line number decoding in read_file_scope () */
8356 process_die (cu
->dies
, cu
);
8358 /* For now fudge the Go package. */
8359 if (cu
->per_cu
->lang
== language_go
)
8360 fixup_go_packaging (cu
);
8362 /* Now that we have processed all the DIEs in the CU, all the types
8363 should be complete, and it should now be safe to compute all of the
8365 compute_delayed_physnames (cu
);
8367 if (cu
->per_cu
->lang
== language_rust
)
8368 rust_union_quirks (cu
);
8370 /* Some compilers don't define a DW_AT_high_pc attribute for the
8371 compilation unit. If the DW_AT_high_pc is missing, synthesize
8372 it, by scanning the DIE's below the compilation unit. */
8373 get_scope_pc_bounds (cu
->dies
, &lowpc
, &highpc
, cu
);
8375 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
8377 = cu
->get_builder ()->end_compunit_symtab_get_static_block (addr
, 0, 1);
8379 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8380 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8381 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8382 addrmap to help ensure it has an accurate map of pc values belonging to
8384 dwarf2_record_block_ranges (cu
->dies
, static_block
, baseaddr
, cu
);
8386 cust
= cu
->get_builder ()->end_compunit_symtab_from_static_block
8387 (static_block
, SECT_OFF_TEXT (objfile
), 0);
8391 int gcc_4_minor
= producer_is_gcc_ge_4 (cu
->producer
);
8393 /* Set symtab language to language from DW_AT_language. If the
8394 compilation is from a C file generated by language preprocessors, do
8395 not set the language if it was already deduced by start_subfile. */
8396 if (!(cu
->per_cu
->lang
== language_c
8397 && cust
->primary_filetab ()->language () != language_unknown
))
8398 cust
->primary_filetab ()->set_language (cu
->per_cu
->lang
);
8400 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8401 produce DW_AT_location with location lists but it can be possibly
8402 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8403 there were bugs in prologue debug info, fixed later in GCC-4.5
8404 by "unwind info for epilogues" patch (which is not directly related).
8406 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8407 needed, it would be wrong due to missing DW_AT_producer there.
8409 Still one can confuse GDB by using non-standard GCC compilation
8410 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8412 if (cu
->has_loclist
&& gcc_4_minor
>= 5)
8413 cust
->set_locations_valid (true);
8415 if (gcc_4_minor
>= 5)
8416 cust
->set_epilogue_unwind_valid (true);
8418 cust
->set_call_site_htab (cu
->call_site_htab
);
8421 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8423 /* Push it for inclusion processing later. */
8424 per_objfile
->per_bfd
->just_read_cus
.push_back (cu
->per_cu
);
8426 /* Not needed any more. */
8427 cu
->reset_builder ();
8430 /* Generate full symbol information for type unit CU, whose DIEs have
8431 already been loaded into memory. */
8434 process_full_type_unit (dwarf2_cu
*cu
,
8435 enum language pretend_language
)
8437 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8438 struct objfile
*objfile
= per_objfile
->objfile
;
8439 struct compunit_symtab
*cust
;
8440 struct signatured_type
*sig_type
;
8442 gdb_assert (cu
->per_cu
->is_debug_types
);
8443 sig_type
= (struct signatured_type
*) cu
->per_cu
;
8445 /* Clear the list here in case something was left over. */
8446 cu
->method_list
.clear ();
8448 /* The symbol tables are set up in read_type_unit_scope. */
8449 process_die (cu
->dies
, cu
);
8451 /* For now fudge the Go package. */
8452 if (cu
->per_cu
->lang
== language_go
)
8453 fixup_go_packaging (cu
);
8455 /* Now that we have processed all the DIEs in the CU, all the types
8456 should be complete, and it should now be safe to compute all of the
8458 compute_delayed_physnames (cu
);
8460 if (cu
->per_cu
->lang
== language_rust
)
8461 rust_union_quirks (cu
);
8463 /* TUs share symbol tables.
8464 If this is the first TU to use this symtab, complete the construction
8465 of it with end_expandable_symtab. Otherwise, complete the addition of
8466 this TU's symbols to the existing symtab. */
8467 type_unit_group_unshareable
*tug_unshare
=
8468 per_objfile
->get_type_unit_group_unshareable (sig_type
->type_unit_group
);
8469 if (tug_unshare
->compunit_symtab
== NULL
)
8471 buildsym_compunit
*builder
= cu
->get_builder ();
8472 cust
= builder
->end_expandable_symtab (0, SECT_OFF_TEXT (objfile
));
8473 tug_unshare
->compunit_symtab
= cust
;
8477 /* Set symtab language to language from DW_AT_language. If the
8478 compilation is from a C file generated by language preprocessors,
8479 do not set the language if it was already deduced by
8481 if (!(cu
->per_cu
->lang
== language_c
8482 && cust
->primary_filetab ()->language () != language_c
))
8483 cust
->primary_filetab ()->set_language (cu
->per_cu
->lang
);
8488 cu
->get_builder ()->augment_type_symtab ();
8489 cust
= tug_unshare
->compunit_symtab
;
8492 per_objfile
->set_symtab (cu
->per_cu
, cust
);
8494 /* Not needed any more. */
8495 cu
->reset_builder ();
8498 /* Process an imported unit DIE. */
8501 process_imported_unit_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8503 struct attribute
*attr
;
8505 /* For now we don't handle imported units in type units. */
8506 if (cu
->per_cu
->is_debug_types
)
8508 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8509 " supported in type units [in module %s]"),
8510 objfile_name (cu
->per_objfile
->objfile
));
8513 attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
8516 sect_offset sect_off
= attr
->get_ref_die_offset ();
8517 bool is_dwz
= (attr
->form
== DW_FORM_GNU_ref_alt
|| cu
->per_cu
->is_dwz
);
8518 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
8519 dwarf2_per_cu_data
*per_cu
8520 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
8521 per_objfile
->per_bfd
);
8523 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
8524 into another compilation unit, at root level. Regard this as a hint,
8526 if (die
->parent
&& die
->parent
->parent
== NULL
8527 && per_cu
->unit_type
== DW_UT_compile
8528 && per_cu
->lang
== language_cplus
)
8531 /* If necessary, add it to the queue and load its DIEs. */
8532 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
,
8534 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
8535 false, cu
->per_cu
->lang
);
8537 cu
->per_cu
->imported_symtabs_push (per_cu
);
8541 /* RAII object that represents a process_die scope: i.e.,
8542 starts/finishes processing a DIE. */
8543 class process_die_scope
8546 process_die_scope (die_info
*die
, dwarf2_cu
*cu
)
8547 : m_die (die
), m_cu (cu
)
8549 /* We should only be processing DIEs not already in process. */
8550 gdb_assert (!m_die
->in_process
);
8551 m_die
->in_process
= true;
8554 ~process_die_scope ()
8556 m_die
->in_process
= false;
8558 /* If we're done processing the DIE for the CU that owns the line
8559 header, we don't need the line header anymore. */
8560 if (m_cu
->line_header_die_owner
== m_die
)
8562 delete m_cu
->line_header
;
8563 m_cu
->line_header
= NULL
;
8564 m_cu
->line_header_die_owner
= NULL
;
8573 /* Process a die and its children. */
8576 process_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
8578 process_die_scope
scope (die
, cu
);
8582 case DW_TAG_padding
:
8584 case DW_TAG_compile_unit
:
8585 case DW_TAG_partial_unit
:
8586 read_file_scope (die
, cu
);
8588 case DW_TAG_type_unit
:
8589 read_type_unit_scope (die
, cu
);
8591 case DW_TAG_subprogram
:
8592 /* Nested subprograms in Fortran get a prefix. */
8593 if (cu
->per_cu
->lang
== language_fortran
8594 && die
->parent
!= NULL
8595 && die
->parent
->tag
== DW_TAG_subprogram
)
8596 cu
->processing_has_namespace_info
= true;
8598 case DW_TAG_inlined_subroutine
:
8599 read_func_scope (die
, cu
);
8601 case DW_TAG_lexical_block
:
8602 case DW_TAG_try_block
:
8603 case DW_TAG_catch_block
:
8604 read_lexical_block_scope (die
, cu
);
8606 case DW_TAG_call_site
:
8607 case DW_TAG_GNU_call_site
:
8608 read_call_site_scope (die
, cu
);
8610 case DW_TAG_class_type
:
8611 case DW_TAG_interface_type
:
8612 case DW_TAG_structure_type
:
8613 case DW_TAG_union_type
:
8614 case DW_TAG_namelist
:
8615 process_structure_scope (die
, cu
);
8617 case DW_TAG_enumeration_type
:
8618 process_enumeration_scope (die
, cu
);
8621 /* These dies have a type, but processing them does not create
8622 a symbol or recurse to process the children. Therefore we can
8623 read them on-demand through read_type_die. */
8624 case DW_TAG_subroutine_type
:
8625 case DW_TAG_set_type
:
8626 case DW_TAG_pointer_type
:
8627 case DW_TAG_ptr_to_member_type
:
8628 case DW_TAG_reference_type
:
8629 case DW_TAG_rvalue_reference_type
:
8630 case DW_TAG_string_type
:
8633 case DW_TAG_array_type
:
8634 /* We only need to handle this case for Ada -- in other
8635 languages, it's normal for the compiler to emit a typedef
8637 if (cu
->per_cu
->lang
!= language_ada
)
8640 case DW_TAG_base_type
:
8641 case DW_TAG_subrange_type
:
8642 case DW_TAG_generic_subrange
:
8643 case DW_TAG_typedef
:
8644 /* Add a typedef symbol for the type definition, if it has a
8646 new_symbol (die
, read_type_die (die
, cu
), cu
);
8648 case DW_TAG_common_block
:
8649 read_common_block (die
, cu
);
8651 case DW_TAG_common_inclusion
:
8653 case DW_TAG_namespace
:
8654 cu
->processing_has_namespace_info
= true;
8655 read_namespace (die
, cu
);
8658 cu
->processing_has_namespace_info
= true;
8659 read_module (die
, cu
);
8661 case DW_TAG_imported_declaration
:
8662 cu
->processing_has_namespace_info
= true;
8663 if (read_namespace_alias (die
, cu
))
8665 /* The declaration is not a global namespace alias. */
8667 case DW_TAG_imported_module
:
8668 cu
->processing_has_namespace_info
= true;
8669 if (die
->child
!= NULL
&& (die
->tag
== DW_TAG_imported_declaration
8670 || cu
->per_cu
->lang
!= language_fortran
))
8671 complaint (_("Tag '%s' has unexpected children"),
8672 dwarf_tag_name (die
->tag
));
8673 read_import_statement (die
, cu
);
8676 case DW_TAG_imported_unit
:
8677 process_imported_unit_die (die
, cu
);
8680 case DW_TAG_variable
:
8681 read_variable (die
, cu
);
8685 new_symbol (die
, NULL
, cu
);
8690 /* DWARF name computation. */
8692 /* A helper function for dwarf2_compute_name which determines whether DIE
8693 needs to have the name of the scope prepended to the name listed in the
8697 die_needs_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
8699 struct attribute
*attr
;
8703 case DW_TAG_namespace
:
8704 case DW_TAG_typedef
:
8705 case DW_TAG_class_type
:
8706 case DW_TAG_interface_type
:
8707 case DW_TAG_structure_type
:
8708 case DW_TAG_union_type
:
8709 case DW_TAG_enumeration_type
:
8710 case DW_TAG_enumerator
:
8711 case DW_TAG_subprogram
:
8712 case DW_TAG_inlined_subroutine
:
8714 case DW_TAG_imported_declaration
:
8717 case DW_TAG_variable
:
8718 case DW_TAG_constant
:
8719 /* We only need to prefix "globally" visible variables. These include
8720 any variable marked with DW_AT_external or any variable that
8721 lives in a namespace. [Variables in anonymous namespaces
8722 require prefixing, but they are not DW_AT_external.] */
8724 if (dwarf2_attr (die
, DW_AT_specification
, cu
))
8726 struct dwarf2_cu
*spec_cu
= cu
;
8728 return die_needs_namespace (die_specification (die
, &spec_cu
),
8732 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
8733 if (attr
== NULL
&& die
->parent
->tag
!= DW_TAG_namespace
8734 && die
->parent
->tag
!= DW_TAG_module
)
8736 /* A variable in a lexical block of some kind does not need a
8737 namespace, even though in C++ such variables may be external
8738 and have a mangled name. */
8739 if (die
->parent
->tag
== DW_TAG_lexical_block
8740 || die
->parent
->tag
== DW_TAG_try_block
8741 || die
->parent
->tag
== DW_TAG_catch_block
8742 || die
->parent
->tag
== DW_TAG_subprogram
)
8751 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
8752 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8753 defined for the given DIE. */
8755 static struct attribute
*
8756 dw2_linkage_name_attr (struct die_info
*die
, struct dwarf2_cu
*cu
)
8758 struct attribute
*attr
;
8760 attr
= dwarf2_attr (die
, DW_AT_linkage_name
, cu
);
8762 attr
= dwarf2_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8767 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
8768 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
8769 defined for the given DIE. */
8772 dw2_linkage_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
8774 const char *linkage_name
;
8776 linkage_name
= dwarf2_string_attr (die
, DW_AT_linkage_name
, cu
);
8777 if (linkage_name
== NULL
)
8778 linkage_name
= dwarf2_string_attr (die
, DW_AT_MIPS_linkage_name
, cu
);
8780 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8781 See https://github.com/rust-lang/rust/issues/32925. */
8782 if (cu
->per_cu
->lang
== language_rust
&& linkage_name
!= NULL
8783 && strchr (linkage_name
, '{') != NULL
)
8784 linkage_name
= NULL
;
8786 return linkage_name
;
8789 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8790 compute the physname for the object, which include a method's:
8791 - formal parameters (C++),
8792 - receiver type (Go),
8794 The term "physname" is a bit confusing.
8795 For C++, for example, it is the demangled name.
8796 For Go, for example, it's the mangled name.
8798 For Ada, return the DIE's linkage name rather than the fully qualified
8799 name. PHYSNAME is ignored..
8801 The result is allocated on the objfile->per_bfd's obstack and
8805 dwarf2_compute_name (const char *name
,
8806 struct die_info
*die
, struct dwarf2_cu
*cu
,
8809 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
8812 name
= dwarf2_name (die
, cu
);
8814 enum language lang
= cu
->per_cu
->lang
;
8816 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8817 but otherwise compute it by typename_concat inside GDB.
8818 FIXME: Actually this is not really true, or at least not always true.
8819 It's all very confusing. compute_and_set_names doesn't try to demangle
8820 Fortran names because there is no mangling standard. So new_symbol
8821 will set the demangled name to the result of dwarf2_full_name, and it is
8822 the demangled name that GDB uses if it exists. */
8823 if (lang
== language_ada
8824 || (lang
== language_fortran
&& physname
))
8826 /* For Ada unit, we prefer the linkage name over the name, as
8827 the former contains the exported name, which the user expects
8828 to be able to reference. Ideally, we want the user to be able
8829 to reference this entity using either natural or linkage name,
8830 but we haven't started looking at this enhancement yet. */
8831 const char *linkage_name
= dw2_linkage_name (die
, cu
);
8833 if (linkage_name
!= NULL
)
8834 return linkage_name
;
8837 /* These are the only languages we know how to qualify names in. */
8839 && (lang
== language_cplus
8840 || lang
== language_fortran
|| lang
== language_d
8841 || lang
== language_rust
))
8843 if (die_needs_namespace (die
, cu
))
8846 const char *canonical_name
= NULL
;
8850 prefix
= determine_prefix (die
, cu
);
8851 if (*prefix
!= '\0')
8853 gdb::unique_xmalloc_ptr
<char> prefixed_name
8854 (typename_concat (NULL
, prefix
, name
, physname
, cu
));
8856 buf
.puts (prefixed_name
.get ());
8861 /* Template parameters may be specified in the DIE's DW_AT_name, or
8862 as children with DW_TAG_template_type_param or
8863 DW_TAG_value_type_param. If the latter, add them to the name
8864 here. If the name already has template parameters, then
8865 skip this step; some versions of GCC emit both, and
8866 it is more efficient to use the pre-computed name.
8868 Something to keep in mind about this process: it is very
8869 unlikely, or in some cases downright impossible, to produce
8870 something that will match the mangled name of a function.
8871 If the definition of the function has the same debug info,
8872 we should be able to match up with it anyway. But fallbacks
8873 using the minimal symbol, for instance to find a method
8874 implemented in a stripped copy of libstdc++, will not work.
8875 If we do not have debug info for the definition, we will have to
8876 match them up some other way.
8878 When we do name matching there is a related problem with function
8879 templates; two instantiated function templates are allowed to
8880 differ only by their return types, which we do not add here. */
8882 if (lang
== language_cplus
&& strchr (name
, '<') == NULL
)
8884 struct attribute
*attr
;
8885 struct die_info
*child
;
8888 die
->building_fullname
= 1;
8890 for (child
= die
->child
; child
!= NULL
; child
= child
->sibling
)
8894 const gdb_byte
*bytes
;
8895 struct dwarf2_locexpr_baton
*baton
;
8898 if (child
->tag
!= DW_TAG_template_type_param
8899 && child
->tag
!= DW_TAG_template_value_param
)
8910 attr
= dwarf2_attr (child
, DW_AT_type
, cu
);
8913 complaint (_("template parameter missing DW_AT_type"));
8914 buf
.puts ("UNKNOWN_TYPE");
8917 type
= die_type (child
, cu
);
8919 if (child
->tag
== DW_TAG_template_type_param
)
8921 cu
->language_defn
->print_type (type
, "", &buf
, -1, 0,
8922 &type_print_raw_options
);
8926 attr
= dwarf2_attr (child
, DW_AT_const_value
, cu
);
8929 complaint (_("template parameter missing "
8930 "DW_AT_const_value"));
8931 buf
.puts ("UNKNOWN_VALUE");
8935 dwarf2_const_value_attr (attr
, type
, name
,
8936 &cu
->comp_unit_obstack
, cu
,
8937 &value
, &bytes
, &baton
);
8939 if (type
->has_no_signedness ())
8940 /* GDB prints characters as NUMBER 'CHAR'. If that's
8941 changed, this can use value_print instead. */
8942 cu
->language_defn
->printchar (value
, type
, &buf
);
8945 struct value_print_options opts
;
8948 v
= dwarf2_evaluate_loc_desc (type
, NULL
,
8952 baton
->per_objfile
);
8953 else if (bytes
!= NULL
)
8955 v
= allocate_value (type
);
8956 memcpy (value_contents_writeable (v
).data (), bytes
,
8957 TYPE_LENGTH (type
));
8960 v
= value_from_longest (type
, value
);
8962 /* Specify decimal so that we do not depend on
8964 get_formatted_print_options (&opts
, 'd');
8966 value_print (v
, &buf
, &opts
);
8971 die
->building_fullname
= 0;
8975 /* Close the argument list, with a space if necessary
8976 (nested templates). */
8977 if (!buf
.empty () && buf
.string ().back () == '>')
8984 /* For C++ methods, append formal parameter type
8985 information, if PHYSNAME. */
8987 if (physname
&& die
->tag
== DW_TAG_subprogram
8988 && lang
== language_cplus
)
8990 struct type
*type
= read_type_die (die
, cu
);
8992 c_type_print_args (type
, &buf
, 1, lang
,
8993 &type_print_raw_options
);
8995 if (lang
== language_cplus
)
8997 /* Assume that an artificial first parameter is
8998 "this", but do not crash if it is not. RealView
8999 marks unnamed (and thus unused) parameters as
9000 artificial; there is no way to differentiate
9002 if (type
->num_fields () > 0
9003 && TYPE_FIELD_ARTIFICIAL (type
, 0)
9004 && type
->field (0).type ()->code () == TYPE_CODE_PTR
9005 && TYPE_CONST (TYPE_TARGET_TYPE (type
->field (0).type ())))
9006 buf
.puts (" const");
9010 const std::string
&intermediate_name
= buf
.string ();
9012 if (lang
== language_cplus
)
9014 = dwarf2_canonicalize_name (intermediate_name
.c_str (), cu
,
9017 /* If we only computed INTERMEDIATE_NAME, or if
9018 INTERMEDIATE_NAME is already canonical, then we need to
9020 if (canonical_name
== NULL
|| canonical_name
== intermediate_name
.c_str ())
9021 name
= objfile
->intern (intermediate_name
);
9023 name
= canonical_name
;
9030 /* Return the fully qualified name of DIE, based on its DW_AT_name.
9031 If scope qualifiers are appropriate they will be added. The result
9032 will be allocated on the storage_obstack, or NULL if the DIE does
9033 not have a name. NAME may either be from a previous call to
9034 dwarf2_name or NULL.
9036 The output string will be canonicalized (if C++). */
9039 dwarf2_full_name (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9041 return dwarf2_compute_name (name
, die
, cu
, 0);
9044 /* Construct a physname for the given DIE in CU. NAME may either be
9045 from a previous call to dwarf2_name or NULL. The result will be
9046 allocated on the objfile_objstack or NULL if the DIE does not have a
9049 The output string will be canonicalized (if C++). */
9052 dwarf2_physname (const char *name
, struct die_info
*die
, struct dwarf2_cu
*cu
)
9054 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9055 const char *retval
, *mangled
= NULL
, *canon
= NULL
;
9058 /* In this case dwarf2_compute_name is just a shortcut not building anything
9060 if (!die_needs_namespace (die
, cu
))
9061 return dwarf2_compute_name (name
, die
, cu
, 1);
9063 if (cu
->per_cu
->lang
!= language_rust
)
9064 mangled
= dw2_linkage_name (die
, cu
);
9066 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
9068 gdb::unique_xmalloc_ptr
<char> demangled
;
9069 if (mangled
!= NULL
)
9071 if (cu
->language_defn
->store_sym_names_in_linkage_form_p ())
9073 /* Do nothing (do not demangle the symbol name). */
9077 /* Use DMGL_RET_DROP for C++ template functions to suppress
9078 their return type. It is easier for GDB users to search
9079 for such functions as `name(params)' than `long name(params)'.
9080 In such case the minimal symbol names do not match the full
9081 symbol names but for template functions there is never a need
9082 to look up their definition from their declaration so
9083 the only disadvantage remains the minimal symbol variant
9084 `long name(params)' does not have the proper inferior type. */
9085 demangled
= gdb_demangle (mangled
, (DMGL_PARAMS
| DMGL_ANSI
9089 canon
= demangled
.get ();
9097 if (canon
== NULL
|| check_physname
)
9099 const char *physname
= dwarf2_compute_name (name
, die
, cu
, 1);
9101 if (canon
!= NULL
&& strcmp (physname
, canon
) != 0)
9103 /* It may not mean a bug in GDB. The compiler could also
9104 compute DW_AT_linkage_name incorrectly. But in such case
9105 GDB would need to be bug-to-bug compatible. */
9107 complaint (_("Computed physname <%s> does not match demangled <%s> "
9108 "(from linkage <%s>) - DIE at %s [in module %s]"),
9109 physname
, canon
, mangled
, sect_offset_str (die
->sect_off
),
9110 objfile_name (objfile
));
9112 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
9113 is available here - over computed PHYSNAME. It is safer
9114 against both buggy GDB and buggy compilers. */
9128 retval
= objfile
->intern (retval
);
9133 /* Inspect DIE in CU for a namespace alias. If one exists, record
9134 a new symbol for it.
9136 Returns 1 if a namespace alias was recorded, 0 otherwise. */
9139 read_namespace_alias (struct die_info
*die
, struct dwarf2_cu
*cu
)
9141 struct attribute
*attr
;
9143 /* If the die does not have a name, this is not a namespace
9145 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
9149 struct die_info
*d
= die
;
9150 struct dwarf2_cu
*imported_cu
= cu
;
9152 /* If the compiler has nested DW_AT_imported_declaration DIEs,
9153 keep inspecting DIEs until we hit the underlying import. */
9154 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
9155 for (num
= 0; num
< MAX_NESTED_IMPORTED_DECLARATIONS
; ++num
)
9157 attr
= dwarf2_attr (d
, DW_AT_import
, cu
);
9161 d
= follow_die_ref (d
, attr
, &imported_cu
);
9162 if (d
->tag
!= DW_TAG_imported_declaration
)
9166 if (num
== MAX_NESTED_IMPORTED_DECLARATIONS
)
9168 complaint (_("DIE at %s has too many recursively imported "
9169 "declarations"), sect_offset_str (d
->sect_off
));
9176 sect_offset sect_off
= attr
->get_ref_die_offset ();
9178 type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, cu
->per_objfile
);
9179 if (type
!= NULL
&& type
->code () == TYPE_CODE_NAMESPACE
)
9181 /* This declaration is a global namespace alias. Add
9182 a symbol for it whose type is the aliased namespace. */
9183 new_symbol (die
, type
, cu
);
9192 /* Return the using directives repository (global or local?) to use in the
9193 current context for CU.
9195 For Ada, imported declarations can materialize renamings, which *may* be
9196 global. However it is impossible (for now?) in DWARF to distinguish
9197 "external" imported declarations and "static" ones. As all imported
9198 declarations seem to be static in all other languages, make them all CU-wide
9199 global only in Ada. */
9201 static struct using_direct
**
9202 using_directives (struct dwarf2_cu
*cu
)
9204 if (cu
->per_cu
->lang
== language_ada
9205 && cu
->get_builder ()->outermost_context_p ())
9206 return cu
->get_builder ()->get_global_using_directives ();
9208 return cu
->get_builder ()->get_local_using_directives ();
9211 /* Read the import statement specified by the given die and record it. */
9214 read_import_statement (struct die_info
*die
, struct dwarf2_cu
*cu
)
9216 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
9217 struct attribute
*import_attr
;
9218 struct die_info
*imported_die
, *child_die
;
9219 struct dwarf2_cu
*imported_cu
;
9220 const char *imported_name
;
9221 const char *imported_name_prefix
;
9222 const char *canonical_name
;
9223 const char *import_alias
;
9224 const char *imported_declaration
= NULL
;
9225 const char *import_prefix
;
9226 std::vector
<const char *> excludes
;
9228 import_attr
= dwarf2_attr (die
, DW_AT_import
, cu
);
9229 if (import_attr
== NULL
)
9231 complaint (_("Tag '%s' has no DW_AT_import"),
9232 dwarf_tag_name (die
->tag
));
9237 imported_die
= follow_die_ref_or_sig (die
, import_attr
, &imported_cu
);
9238 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9239 if (imported_name
== NULL
)
9241 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9243 The import in the following code:
9257 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9258 <52> DW_AT_decl_file : 1
9259 <53> DW_AT_decl_line : 6
9260 <54> DW_AT_import : <0x75>
9261 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9263 <5b> DW_AT_decl_file : 1
9264 <5c> DW_AT_decl_line : 2
9265 <5d> DW_AT_type : <0x6e>
9267 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9268 <76> DW_AT_byte_size : 4
9269 <77> DW_AT_encoding : 5 (signed)
9271 imports the wrong die ( 0x75 instead of 0x58 ).
9272 This case will be ignored until the gcc bug is fixed. */
9276 /* Figure out the local name after import. */
9277 import_alias
= dwarf2_name (die
, cu
);
9279 /* Figure out where the statement is being imported to. */
9280 import_prefix
= determine_prefix (die
, cu
);
9282 /* Figure out what the scope of the imported die is and prepend it
9283 to the name of the imported die. */
9284 imported_name_prefix
= determine_prefix (imported_die
, imported_cu
);
9286 if (imported_die
->tag
!= DW_TAG_namespace
9287 && imported_die
->tag
!= DW_TAG_module
)
9289 imported_declaration
= imported_name
;
9290 canonical_name
= imported_name_prefix
;
9292 else if (strlen (imported_name_prefix
) > 0)
9293 canonical_name
= obconcat (&objfile
->objfile_obstack
,
9294 imported_name_prefix
,
9295 (cu
->per_cu
->lang
== language_d
9298 imported_name
, (char *) NULL
);
9300 canonical_name
= imported_name
;
9302 if (die
->tag
== DW_TAG_imported_module
9303 && cu
->per_cu
->lang
== language_fortran
)
9304 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
9305 child_die
= child_die
->sibling
)
9307 /* DWARF-4: A Fortran use statement with a “rename list” may be
9308 represented by an imported module entry with an import attribute
9309 referring to the module and owned entries corresponding to those
9310 entities that are renamed as part of being imported. */
9312 if (child_die
->tag
!= DW_TAG_imported_declaration
)
9314 complaint (_("child DW_TAG_imported_declaration expected "
9315 "- DIE at %s [in module %s]"),
9316 sect_offset_str (child_die
->sect_off
),
9317 objfile_name (objfile
));
9321 import_attr
= dwarf2_attr (child_die
, DW_AT_import
, cu
);
9322 if (import_attr
== NULL
)
9324 complaint (_("Tag '%s' has no DW_AT_import"),
9325 dwarf_tag_name (child_die
->tag
));
9330 imported_die
= follow_die_ref_or_sig (child_die
, import_attr
,
9332 imported_name
= dwarf2_name (imported_die
, imported_cu
);
9333 if (imported_name
== NULL
)
9335 complaint (_("child DW_TAG_imported_declaration has unknown "
9336 "imported name - DIE at %s [in module %s]"),
9337 sect_offset_str (child_die
->sect_off
),
9338 objfile_name (objfile
));
9342 excludes
.push_back (imported_name
);
9344 process_die (child_die
, cu
);
9347 add_using_directive (using_directives (cu
),
9351 imported_declaration
,
9354 &objfile
->objfile_obstack
);
9357 /* ICC<14 does not output the required DW_AT_declaration on incomplete
9358 types, but gives them a size of zero. Starting with version 14,
9359 ICC is compatible with GCC. */
9362 producer_is_icc_lt_14 (struct dwarf2_cu
*cu
)
9364 if (!cu
->checked_producer
)
9365 check_producer (cu
);
9367 return cu
->producer_is_icc_lt_14
;
9370 /* ICC generates a DW_AT_type for C void functions. This was observed on
9371 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
9372 which says that void functions should not have a DW_AT_type. */
9375 producer_is_icc (struct dwarf2_cu
*cu
)
9377 if (!cu
->checked_producer
)
9378 check_producer (cu
);
9380 return cu
->producer_is_icc
;
9383 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9384 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9385 this, it was first present in GCC release 4.3.0. */
9388 producer_is_gcc_lt_4_3 (struct dwarf2_cu
*cu
)
9390 if (!cu
->checked_producer
)
9391 check_producer (cu
);
9393 return cu
->producer_is_gcc_lt_4_3
;
9396 static file_and_directory
&
9397 find_file_and_directory (struct die_info
*die
, struct dwarf2_cu
*cu
)
9399 if (cu
->per_cu
->fnd
!= nullptr)
9400 return *cu
->per_cu
->fnd
;
9402 /* Find the filename. Do not use dwarf2_name here, since the filename
9403 is not a source language identifier. */
9404 file_and_directory
res (dwarf2_string_attr (die
, DW_AT_name
, cu
),
9405 dwarf2_string_attr (die
, DW_AT_comp_dir
, cu
));
9407 if (res
.get_comp_dir () == nullptr
9408 && producer_is_gcc_lt_4_3 (cu
)
9409 && res
.get_name () != nullptr
9410 && IS_ABSOLUTE_PATH (res
.get_name ()))
9411 res
.set_comp_dir (ldirname (res
.get_name ()));
9413 cu
->per_cu
->fnd
.reset (new file_and_directory (std::move (res
)));
9414 return *cu
->per_cu
->fnd
;
9417 /* Handle DW_AT_stmt_list for a compilation unit.
9418 DIE is the DW_TAG_compile_unit die for CU.
9419 COMP_DIR is the compilation directory. LOWPC is passed to
9420 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9423 handle_DW_AT_stmt_list (struct die_info
*die
, struct dwarf2_cu
*cu
,
9424 CORE_ADDR lowpc
) /* ARI: editCase function */
9426 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9427 struct attribute
*attr
;
9428 struct line_header line_header_local
;
9429 hashval_t line_header_local_hash
;
9433 gdb_assert (! cu
->per_cu
->is_debug_types
);
9435 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, cu
);
9436 if (attr
== NULL
|| !attr
->form_is_unsigned ())
9439 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9441 /* The line header hash table is only created if needed (it exists to
9442 prevent redundant reading of the line table for partial_units).
9443 If we're given a partial_unit, we'll need it. If we're given a
9444 compile_unit, then use the line header hash table if it's already
9445 created, but don't create one just yet. */
9447 if (per_objfile
->line_header_hash
== NULL
9448 && die
->tag
== DW_TAG_partial_unit
)
9450 per_objfile
->line_header_hash
9451 .reset (htab_create_alloc (127, line_header_hash_voidp
,
9452 line_header_eq_voidp
,
9453 htab_delete_entry
<line_header
>,
9457 line_header_local
.sect_off
= line_offset
;
9458 line_header_local
.offset_in_dwz
= cu
->per_cu
->is_dwz
;
9459 line_header_local_hash
= line_header_hash (&line_header_local
);
9460 if (per_objfile
->line_header_hash
!= NULL
)
9462 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9464 line_header_local_hash
, NO_INSERT
);
9466 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9467 is not present in *SLOT (since if there is something in *SLOT then
9468 it will be for a partial_unit). */
9469 if (die
->tag
== DW_TAG_partial_unit
&& slot
!= NULL
)
9471 gdb_assert (*slot
!= NULL
);
9472 cu
->line_header
= (struct line_header
*) *slot
;
9477 /* dwarf_decode_line_header does not yet provide sufficient information.
9478 We always have to call also dwarf_decode_lines for it. */
9479 line_header_up lh
= dwarf_decode_line_header (line_offset
, cu
);
9483 cu
->line_header
= lh
.release ();
9484 cu
->line_header_die_owner
= die
;
9486 if (per_objfile
->line_header_hash
== NULL
)
9490 slot
= htab_find_slot_with_hash (per_objfile
->line_header_hash
.get (),
9492 line_header_local_hash
, INSERT
);
9493 gdb_assert (slot
!= NULL
);
9495 if (slot
!= NULL
&& *slot
== NULL
)
9497 /* This newly decoded line number information unit will be owned
9498 by line_header_hash hash table. */
9499 *slot
= cu
->line_header
;
9500 cu
->line_header_die_owner
= NULL
;
9504 /* We cannot free any current entry in (*slot) as that struct line_header
9505 may be already used by multiple CUs. Create only temporary decoded
9506 line_header for this CU - it may happen at most once for each line
9507 number information unit. And if we're not using line_header_hash
9508 then this is what we want as well. */
9509 gdb_assert (die
->tag
!= DW_TAG_partial_unit
);
9511 decode_mapping
= (die
->tag
!= DW_TAG_partial_unit
);
9512 dwarf_decode_lines (cu
->line_header
, cu
, lowpc
, decode_mapping
);
9515 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9518 read_file_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9520 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
9521 struct objfile
*objfile
= per_objfile
->objfile
;
9522 struct gdbarch
*gdbarch
= objfile
->arch ();
9523 CORE_ADDR lowpc
= ((CORE_ADDR
) -1);
9524 CORE_ADDR highpc
= ((CORE_ADDR
) 0);
9525 struct attribute
*attr
;
9526 struct die_info
*child_die
;
9529 prepare_one_comp_unit (cu
, die
, cu
->per_cu
->lang
);
9530 baseaddr
= objfile
->text_section_offset ();
9532 get_scope_pc_bounds (die
, &lowpc
, &highpc
, cu
);
9534 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9535 from finish_block. */
9536 if (lowpc
== ((CORE_ADDR
) -1))
9538 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
9540 file_and_directory
&fnd
= find_file_and_directory (die
, cu
);
9542 cu
->start_compunit_symtab (fnd
.get_name (), fnd
.intern_comp_dir (objfile
),
9545 gdb_assert (per_objfile
->sym_cu
== nullptr);
9546 scoped_restore restore_sym_cu
9547 = make_scoped_restore (&per_objfile
->sym_cu
, cu
);
9549 /* Decode line number information if present. We do this before
9550 processing child DIEs, so that the line header table is available
9551 for DW_AT_decl_file. The PC check is here because, if LOWPC and
9552 HIGHPC are both 0x0, then there won't be any interesting code in
9553 the CU, but a check later on (in
9554 lnp_state_machine::check_line_address) will fail to properly
9555 exclude an entry that was removed via --gc-sections. */
9556 if (lowpc
!= highpc
)
9557 handle_DW_AT_stmt_list (die
, cu
, lowpc
);
9559 /* Process all dies in compilation unit. */
9560 if (die
->child
!= NULL
)
9562 child_die
= die
->child
;
9563 while (child_die
&& child_die
->tag
)
9565 process_die (child_die
, cu
);
9566 child_die
= child_die
->sibling
;
9569 per_objfile
->sym_cu
= nullptr;
9571 /* Decode macro information, if present. Dwarf 2 macro information
9572 refers to information in the line number info statement program
9573 header, so we can only read it if we've read the header
9575 attr
= dwarf2_attr (die
, DW_AT_macros
, cu
);
9577 attr
= dwarf2_attr (die
, DW_AT_GNU_macros
, cu
);
9578 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9580 if (dwarf2_attr (die
, DW_AT_macro_info
, cu
))
9581 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9583 dwarf_decode_macros (cu
, attr
->as_unsigned (), 1);
9587 attr
= dwarf2_attr (die
, DW_AT_macro_info
, cu
);
9588 if (attr
!= nullptr && attr
->form_is_unsigned () && cu
->line_header
)
9590 unsigned int macro_offset
= attr
->as_unsigned ();
9592 dwarf_decode_macros (cu
, macro_offset
, 0);
9598 dwarf2_cu::setup_type_unit_groups (struct die_info
*die
)
9600 struct type_unit_group
*tu_group
;
9602 struct attribute
*attr
;
9604 struct signatured_type
*sig_type
;
9606 gdb_assert (per_cu
->is_debug_types
);
9607 sig_type
= (struct signatured_type
*) per_cu
;
9609 attr
= dwarf2_attr (die
, DW_AT_stmt_list
, this);
9611 /* If we're using .gdb_index (includes -readnow) then
9612 per_cu->type_unit_group may not have been set up yet. */
9613 if (sig_type
->type_unit_group
== NULL
)
9614 sig_type
->type_unit_group
= get_type_unit_group (this, attr
);
9615 tu_group
= sig_type
->type_unit_group
;
9617 /* If we've already processed this stmt_list there's no real need to
9618 do it again, we could fake it and just recreate the part we need
9619 (file name,index -> symtab mapping). If data shows this optimization
9620 is useful we can do it then. */
9621 type_unit_group_unshareable
*tug_unshare
9622 = per_objfile
->get_type_unit_group_unshareable (tu_group
);
9623 first_time
= tug_unshare
->compunit_symtab
== NULL
;
9625 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9628 if (attr
!= NULL
&& attr
->form_is_unsigned ())
9630 sect_offset line_offset
= (sect_offset
) attr
->as_unsigned ();
9631 lh
= dwarf_decode_line_header (line_offset
, this);
9636 start_compunit_symtab ("", NULL
, 0);
9639 gdb_assert (tug_unshare
->symtabs
== NULL
);
9640 gdb_assert (m_builder
== nullptr);
9641 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9642 m_builder
.reset (new struct buildsym_compunit
9643 (cust
->objfile (), "",
9645 compunit_language (cust
),
9647 list_in_scope
= get_builder ()->get_file_symbols ();
9652 line_header
= lh
.release ();
9653 line_header_die_owner
= die
;
9657 struct compunit_symtab
*cust
= start_compunit_symtab ("", NULL
, 0);
9659 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9660 still initializing it, and our caller (a few levels up)
9661 process_full_type_unit still needs to know if this is the first
9664 tug_unshare
->symtabs
9665 = XOBNEWVEC (&cust
->objfile ()->objfile_obstack
,
9666 struct symtab
*, line_header
->file_names_size ());
9668 auto &file_names
= line_header
->file_names ();
9669 for (i
= 0; i
< file_names
.size (); ++i
)
9671 file_entry
&fe
= file_names
[i
];
9672 dwarf2_start_subfile (this, fe
.name
,
9673 fe
.include_dir (line_header
));
9674 buildsym_compunit
*b
= get_builder ();
9675 if (b
->get_current_subfile ()->symtab
== NULL
)
9677 /* NOTE: start_subfile will recognize when it's been
9678 passed a file it has already seen. So we can't
9679 assume there's a simple mapping from
9680 cu->line_header->file_names to subfiles, plus
9681 cu->line_header->file_names may contain dups. */
9682 const char *name
= b
->get_current_subfile ()->name
.c_str ();
9683 b
->get_current_subfile ()->symtab
= allocate_symtab (cust
, name
);
9686 fe
.symtab
= b
->get_current_subfile ()->symtab
;
9687 tug_unshare
->symtabs
[i
] = fe
.symtab
;
9692 gdb_assert (m_builder
== nullptr);
9693 struct compunit_symtab
*cust
= tug_unshare
->compunit_symtab
;
9694 m_builder
.reset (new struct buildsym_compunit
9695 (cust
->objfile (), "",
9697 compunit_language (cust
),
9699 list_in_scope
= get_builder ()->get_file_symbols ();
9701 auto &file_names
= line_header
->file_names ();
9702 for (i
= 0; i
< file_names
.size (); ++i
)
9704 file_entry
&fe
= file_names
[i
];
9705 fe
.symtab
= tug_unshare
->symtabs
[i
];
9709 /* The main symtab is allocated last. Type units don't have DW_AT_name
9710 so they don't have a "real" (so to speak) symtab anyway.
9711 There is later code that will assign the main symtab to all symbols
9712 that don't have one. We need to handle the case of a symbol with a
9713 missing symtab (DW_AT_decl_file) anyway. */
9716 /* Process DW_TAG_type_unit.
9717 For TUs we want to skip the first top level sibling if it's not the
9718 actual type being defined by this TU. In this case the first top
9719 level sibling is there to provide context only. */
9722 read_type_unit_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
9724 struct die_info
*child_die
;
9726 prepare_one_comp_unit (cu
, die
, language_minimal
);
9728 /* Initialize (or reinitialize) the machinery for building symtabs.
9729 We do this before processing child DIEs, so that the line header table
9730 is available for DW_AT_decl_file. */
9731 cu
->setup_type_unit_groups (die
);
9733 if (die
->child
!= NULL
)
9735 child_die
= die
->child
;
9736 while (child_die
&& child_die
->tag
)
9738 process_die (child_die
, cu
);
9739 child_die
= child_die
->sibling
;
9746 http://gcc.gnu.org/wiki/DebugFission
9747 http://gcc.gnu.org/wiki/DebugFissionDWP
9749 To simplify handling of both DWO files ("object" files with the DWARF info)
9750 and DWP files (a file with the DWOs packaged up into one file), we treat
9751 DWP files as having a collection of virtual DWO files. */
9754 hash_dwo_file (const void *item
)
9756 const struct dwo_file
*dwo_file
= (const struct dwo_file
*) item
;
9759 hash
= htab_hash_string (dwo_file
->dwo_name
);
9760 if (dwo_file
->comp_dir
!= NULL
)
9761 hash
+= htab_hash_string (dwo_file
->comp_dir
);
9766 eq_dwo_file (const void *item_lhs
, const void *item_rhs
)
9768 const struct dwo_file
*lhs
= (const struct dwo_file
*) item_lhs
;
9769 const struct dwo_file
*rhs
= (const struct dwo_file
*) item_rhs
;
9771 if (strcmp (lhs
->dwo_name
, rhs
->dwo_name
) != 0)
9773 if (lhs
->comp_dir
== NULL
|| rhs
->comp_dir
== NULL
)
9774 return lhs
->comp_dir
== rhs
->comp_dir
;
9775 return strcmp (lhs
->comp_dir
, rhs
->comp_dir
) == 0;
9778 /* Allocate a hash table for DWO files. */
9781 allocate_dwo_file_hash_table ()
9783 return htab_up (htab_create_alloc (41,
9786 htab_delete_entry
<dwo_file
>,
9790 /* Lookup DWO file DWO_NAME. */
9793 lookup_dwo_file_slot (dwarf2_per_objfile
*per_objfile
,
9794 const char *dwo_name
,
9795 const char *comp_dir
)
9797 struct dwo_file find_entry
;
9800 if (per_objfile
->per_bfd
->dwo_files
== NULL
)
9801 per_objfile
->per_bfd
->dwo_files
= allocate_dwo_file_hash_table ();
9803 find_entry
.dwo_name
= dwo_name
;
9804 find_entry
.comp_dir
= comp_dir
;
9805 slot
= htab_find_slot (per_objfile
->per_bfd
->dwo_files
.get (), &find_entry
,
9812 hash_dwo_unit (const void *item
)
9814 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
9816 /* This drops the top 32 bits of the id, but is ok for a hash. */
9817 return dwo_unit
->signature
;
9821 eq_dwo_unit (const void *item_lhs
, const void *item_rhs
)
9823 const struct dwo_unit
*lhs
= (const struct dwo_unit
*) item_lhs
;
9824 const struct dwo_unit
*rhs
= (const struct dwo_unit
*) item_rhs
;
9826 /* The signature is assumed to be unique within the DWO file.
9827 So while object file CU dwo_id's always have the value zero,
9828 that's OK, assuming each object file DWO file has only one CU,
9829 and that's the rule for now. */
9830 return lhs
->signature
== rhs
->signature
;
9833 /* Allocate a hash table for DWO CUs,TUs.
9834 There is one of these tables for each of CUs,TUs for each DWO file. */
9837 allocate_dwo_unit_table ()
9839 /* Start out with a pretty small number.
9840 Generally DWO files contain only one CU and maybe some TUs. */
9841 return htab_up (htab_create_alloc (3,
9844 NULL
, xcalloc
, xfree
));
9847 /* die_reader_func for create_dwo_cu. */
9850 create_dwo_cu_reader (const struct die_reader_specs
*reader
,
9851 const gdb_byte
*info_ptr
,
9852 struct die_info
*comp_unit_die
,
9853 struct dwo_file
*dwo_file
,
9854 struct dwo_unit
*dwo_unit
)
9856 struct dwarf2_cu
*cu
= reader
->cu
;
9857 sect_offset sect_off
= cu
->per_cu
->sect_off
;
9858 struct dwarf2_section_info
*section
= cu
->per_cu
->section
;
9860 gdb::optional
<ULONGEST
> signature
= lookup_dwo_id (cu
, comp_unit_die
);
9861 if (!signature
.has_value ())
9863 complaint (_("Dwarf Error: debug entry at offset %s is missing"
9864 " its dwo_id [in module %s]"),
9865 sect_offset_str (sect_off
), dwo_file
->dwo_name
);
9869 dwo_unit
->dwo_file
= dwo_file
;
9870 dwo_unit
->signature
= *signature
;
9871 dwo_unit
->section
= section
;
9872 dwo_unit
->sect_off
= sect_off
;
9873 dwo_unit
->length
= cu
->per_cu
->length
;
9875 dwarf_read_debug_printf (" offset %s, dwo_id %s",
9876 sect_offset_str (sect_off
),
9877 hex_string (dwo_unit
->signature
));
9880 /* Create the dwo_units for the CUs in a DWO_FILE.
9881 Note: This function processes DWO files only, not DWP files. */
9884 create_cus_hash_table (dwarf2_per_objfile
*per_objfile
,
9885 dwarf2_cu
*cu
, struct dwo_file
&dwo_file
,
9886 dwarf2_section_info
§ion
, htab_up
&cus_htab
)
9888 struct objfile
*objfile
= per_objfile
->objfile
;
9889 dwarf2_per_bfd
*per_bfd
= per_objfile
->per_bfd
;
9890 const gdb_byte
*info_ptr
, *end_ptr
;
9892 section
.read (objfile
);
9893 info_ptr
= section
.buffer
;
9895 if (info_ptr
== NULL
)
9898 dwarf_read_debug_printf ("Reading %s for %s:",
9899 section
.get_name (),
9900 section
.get_file_name ());
9902 end_ptr
= info_ptr
+ section
.size
;
9903 while (info_ptr
< end_ptr
)
9905 struct dwarf2_per_cu_data per_cu
;
9906 struct dwo_unit read_unit
{};
9907 struct dwo_unit
*dwo_unit
;
9909 sect_offset sect_off
= (sect_offset
) (info_ptr
- section
.buffer
);
9911 per_cu
.per_bfd
= per_bfd
;
9912 per_cu
.is_debug_types
= 0;
9913 per_cu
.sect_off
= sect_offset (info_ptr
- section
.buffer
);
9914 per_cu
.section
= §ion
;
9916 cutu_reader
reader (&per_cu
, per_objfile
, cu
, &dwo_file
);
9917 if (!reader
.dummy_p
)
9918 create_dwo_cu_reader (&reader
, reader
.info_ptr
, reader
.comp_unit_die
,
9919 &dwo_file
, &read_unit
);
9920 info_ptr
+= per_cu
.length
;
9922 // If the unit could not be parsed, skip it.
9923 if (read_unit
.dwo_file
== NULL
)
9926 if (cus_htab
== NULL
)
9927 cus_htab
= allocate_dwo_unit_table ();
9929 dwo_unit
= OBSTACK_ZALLOC (&per_bfd
->obstack
,
9931 *dwo_unit
= read_unit
;
9932 slot
= htab_find_slot (cus_htab
.get (), dwo_unit
, INSERT
);
9933 gdb_assert (slot
!= NULL
);
9936 const struct dwo_unit
*dup_cu
= (const struct dwo_unit
*)*slot
;
9937 sect_offset dup_sect_off
= dup_cu
->sect_off
;
9939 complaint (_("debug cu entry at offset %s is duplicate to"
9940 " the entry at offset %s, signature %s"),
9941 sect_offset_str (sect_off
), sect_offset_str (dup_sect_off
),
9942 hex_string (dwo_unit
->signature
));
9944 *slot
= (void *)dwo_unit
;
9948 /* DWP file .debug_{cu,tu}_index section format:
9949 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9950 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
9952 DWP Versions 1 & 2 are older, pre-standard format versions. The first
9953 officially standard DWP format was published with DWARF v5 and is called
9954 Version 5. There are no versions 3 or 4.
9958 Both index sections have the same format, and serve to map a 64-bit
9959 signature to a set of section numbers. Each section begins with a header,
9960 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9961 indexes, and a pool of 32-bit section numbers. The index sections will be
9962 aligned at 8-byte boundaries in the file.
9964 The index section header consists of:
9966 V, 32 bit version number
9968 N, 32 bit number of compilation units or type units in the index
9969 M, 32 bit number of slots in the hash table
9971 Numbers are recorded using the byte order of the application binary.
9973 The hash table begins at offset 16 in the section, and consists of an array
9974 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9975 order of the application binary). Unused slots in the hash table are 0.
9976 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9978 The parallel table begins immediately after the hash table
9979 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9980 array of 32-bit indexes (using the byte order of the application binary),
9981 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9982 table contains a 32-bit index into the pool of section numbers. For unused
9983 hash table slots, the corresponding entry in the parallel table will be 0.
9985 The pool of section numbers begins immediately following the hash table
9986 (at offset 16 + 12 * M from the beginning of the section). The pool of
9987 section numbers consists of an array of 32-bit words (using the byte order
9988 of the application binary). Each item in the array is indexed starting
9989 from 0. The hash table entry provides the index of the first section
9990 number in the set. Additional section numbers in the set follow, and the
9991 set is terminated by a 0 entry (section number 0 is not used in ELF).
9993 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9994 section must be the first entry in the set, and the .debug_abbrev.dwo must
9995 be the second entry. Other members of the set may follow in any order.
9999 DWP Versions 2 and 5:
10001 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
10002 and the entries in the index tables are now offsets into these sections.
10003 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
10006 Index Section Contents:
10008 Hash Table of Signatures dwp_hash_table.hash_table
10009 Parallel Table of Indices dwp_hash_table.unit_table
10010 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
10011 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
10013 The index section header consists of:
10015 V, 32 bit version number
10016 L, 32 bit number of columns in the table of section offsets
10017 N, 32 bit number of compilation units or type units in the index
10018 M, 32 bit number of slots in the hash table
10020 Numbers are recorded using the byte order of the application binary.
10022 The hash table has the same format as version 1.
10023 The parallel table of indices has the same format as version 1,
10024 except that the entries are origin-1 indices into the table of sections
10025 offsets and the table of section sizes.
10027 The table of offsets begins immediately following the parallel table
10028 (at offset 16 + 12 * M from the beginning of the section). The table is
10029 a two-dimensional array of 32-bit words (using the byte order of the
10030 application binary), with L columns and N+1 rows, in row-major order.
10031 Each row in the array is indexed starting from 0. The first row provides
10032 a key to the remaining rows: each column in this row provides an identifier
10033 for a debug section, and the offsets in the same column of subsequent rows
10034 refer to that section. The section identifiers for Version 2 are:
10036 DW_SECT_INFO 1 .debug_info.dwo
10037 DW_SECT_TYPES 2 .debug_types.dwo
10038 DW_SECT_ABBREV 3 .debug_abbrev.dwo
10039 DW_SECT_LINE 4 .debug_line.dwo
10040 DW_SECT_LOC 5 .debug_loc.dwo
10041 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
10042 DW_SECT_MACINFO 7 .debug_macinfo.dwo
10043 DW_SECT_MACRO 8 .debug_macro.dwo
10045 The section identifiers for Version 5 are:
10047 DW_SECT_INFO_V5 1 .debug_info.dwo
10048 DW_SECT_RESERVED_V5 2 --
10049 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
10050 DW_SECT_LINE_V5 4 .debug_line.dwo
10051 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
10052 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
10053 DW_SECT_MACRO_V5 7 .debug_macro.dwo
10054 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
10056 The offsets provided by the CU and TU index sections are the base offsets
10057 for the contributions made by each CU or TU to the corresponding section
10058 in the package file. Each CU and TU header contains an abbrev_offset
10059 field, used to find the abbreviations table for that CU or TU within the
10060 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
10061 be interpreted as relative to the base offset given in the index section.
10062 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
10063 should be interpreted as relative to the base offset for .debug_line.dwo,
10064 and offsets into other debug sections obtained from DWARF attributes should
10065 also be interpreted as relative to the corresponding base offset.
10067 The table of sizes begins immediately following the table of offsets.
10068 Like the table of offsets, it is a two-dimensional array of 32-bit words,
10069 with L columns and N rows, in row-major order. Each row in the array is
10070 indexed starting from 1 (row 0 is shared by the two tables).
10074 Hash table lookup is handled the same in version 1 and 2:
10076 We assume that N and M will not exceed 2^32 - 1.
10077 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
10079 Given a 64-bit compilation unit signature or a type signature S, an entry
10080 in the hash table is located as follows:
10082 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
10083 the low-order k bits all set to 1.
10085 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
10087 3) If the hash table entry at index H matches the signature, use that
10088 entry. If the hash table entry at index H is unused (all zeroes),
10089 terminate the search: the signature is not present in the table.
10091 4) Let H = (H + H') modulo M. Repeat at Step 3.
10093 Because M > N and H' and M are relatively prime, the search is guaranteed
10094 to stop at an unused slot or find the match. */
10096 /* Create a hash table to map DWO IDs to their CU/TU entry in
10097 .debug_{info,types}.dwo in DWP_FILE.
10098 Returns NULL if there isn't one.
10099 Note: This function processes DWP files only, not DWO files. */
10101 static struct dwp_hash_table
*
10102 create_dwp_hash_table (dwarf2_per_objfile
*per_objfile
,
10103 struct dwp_file
*dwp_file
, int is_debug_types
)
10105 struct objfile
*objfile
= per_objfile
->objfile
;
10106 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10107 const gdb_byte
*index_ptr
, *index_end
;
10108 struct dwarf2_section_info
*index
;
10109 uint32_t version
, nr_columns
, nr_units
, nr_slots
;
10110 struct dwp_hash_table
*htab
;
10112 if (is_debug_types
)
10113 index
= &dwp_file
->sections
.tu_index
;
10115 index
= &dwp_file
->sections
.cu_index
;
10117 if (index
->empty ())
10119 index
->read (objfile
);
10121 index_ptr
= index
->buffer
;
10122 index_end
= index_ptr
+ index
->size
;
10124 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
10125 For now it's safe to just read 4 bytes (particularly as it's difficult to
10126 tell if you're dealing with Version 5 before you've read the version). */
10127 version
= read_4_bytes (dbfd
, index_ptr
);
10129 if (version
== 2 || version
== 5)
10130 nr_columns
= read_4_bytes (dbfd
, index_ptr
);
10134 nr_units
= read_4_bytes (dbfd
, index_ptr
);
10136 nr_slots
= read_4_bytes (dbfd
, index_ptr
);
10139 if (version
!= 1 && version
!= 2 && version
!= 5)
10141 error (_("Dwarf Error: unsupported DWP file version (%s)"
10142 " [in module %s]"),
10143 pulongest (version
), dwp_file
->name
);
10145 if (nr_slots
!= (nr_slots
& -nr_slots
))
10147 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
10148 " is not power of 2 [in module %s]"),
10149 pulongest (nr_slots
), dwp_file
->name
);
10152 htab
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwp_hash_table
);
10153 htab
->version
= version
;
10154 htab
->nr_columns
= nr_columns
;
10155 htab
->nr_units
= nr_units
;
10156 htab
->nr_slots
= nr_slots
;
10157 htab
->hash_table
= index_ptr
;
10158 htab
->unit_table
= htab
->hash_table
+ sizeof (uint64_t) * nr_slots
;
10160 /* Exit early if the table is empty. */
10161 if (nr_slots
== 0 || nr_units
== 0
10162 || (version
== 2 && nr_columns
== 0)
10163 || (version
== 5 && nr_columns
== 0))
10165 /* All must be zero. */
10166 if (nr_slots
!= 0 || nr_units
!= 0
10167 || (version
== 2 && nr_columns
!= 0)
10168 || (version
== 5 && nr_columns
!= 0))
10170 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
10171 " all zero [in modules %s]"),
10179 htab
->section_pool
.v1
.indices
=
10180 htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10181 /* It's harder to decide whether the section is too small in v1.
10182 V1 is deprecated anyway so we punt. */
10184 else if (version
== 2)
10186 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10187 int *ids
= htab
->section_pool
.v2
.section_ids
;
10188 size_t sizeof_ids
= sizeof (htab
->section_pool
.v2
.section_ids
);
10189 /* Reverse map for error checking. */
10190 int ids_seen
[DW_SECT_MAX
+ 1];
10193 if (nr_columns
< 2)
10195 error (_("Dwarf Error: bad DWP hash table, too few columns"
10196 " in section table [in module %s]"),
10199 if (nr_columns
> MAX_NR_V2_DWO_SECTIONS
)
10201 error (_("Dwarf Error: bad DWP hash table, too many columns"
10202 " in section table [in module %s]"),
10205 memset (ids
, 255, sizeof_ids
);
10206 memset (ids_seen
, 255, sizeof (ids_seen
));
10207 for (i
= 0; i
< nr_columns
; ++i
)
10209 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10211 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX
)
10213 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10214 " in section table [in module %s]"),
10215 id
, dwp_file
->name
);
10217 if (ids_seen
[id
] != -1)
10219 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10220 " id %d in section table [in module %s]"),
10221 id
, dwp_file
->name
);
10226 /* Must have exactly one info or types section. */
10227 if (((ids_seen
[DW_SECT_INFO
] != -1)
10228 + (ids_seen
[DW_SECT_TYPES
] != -1))
10231 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10232 " DWO info/types section [in module %s]"),
10235 /* Must have an abbrev section. */
10236 if (ids_seen
[DW_SECT_ABBREV
] == -1)
10238 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10239 " section [in module %s]"),
10242 htab
->section_pool
.v2
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10243 htab
->section_pool
.v2
.sizes
=
10244 htab
->section_pool
.v2
.offsets
+ (sizeof (uint32_t)
10245 * nr_units
* nr_columns
);
10246 if ((htab
->section_pool
.v2
.sizes
+ (sizeof (uint32_t)
10247 * nr_units
* nr_columns
))
10250 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10251 " [in module %s]"),
10255 else /* version == 5 */
10257 const gdb_byte
*ids_ptr
= htab
->unit_table
+ sizeof (uint32_t) * nr_slots
;
10258 int *ids
= htab
->section_pool
.v5
.section_ids
;
10259 size_t sizeof_ids
= sizeof (htab
->section_pool
.v5
.section_ids
);
10260 /* Reverse map for error checking. */
10261 int ids_seen
[DW_SECT_MAX_V5
+ 1];
10263 if (nr_columns
< 2)
10265 error (_("Dwarf Error: bad DWP hash table, too few columns"
10266 " in section table [in module %s]"),
10269 if (nr_columns
> MAX_NR_V5_DWO_SECTIONS
)
10271 error (_("Dwarf Error: bad DWP hash table, too many columns"
10272 " in section table [in module %s]"),
10275 memset (ids
, 255, sizeof_ids
);
10276 memset (ids_seen
, 255, sizeof (ids_seen
));
10277 for (int i
= 0; i
< nr_columns
; ++i
)
10279 int id
= read_4_bytes (dbfd
, ids_ptr
+ i
* sizeof (uint32_t));
10281 if (id
< DW_SECT_MIN
|| id
> DW_SECT_MAX_V5
)
10283 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10284 " in section table [in module %s]"),
10285 id
, dwp_file
->name
);
10287 if (ids_seen
[id
] != -1)
10289 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10290 " id %d in section table [in module %s]"),
10291 id
, dwp_file
->name
);
10296 /* Must have seen an info section. */
10297 if (ids_seen
[DW_SECT_INFO_V5
] == -1)
10299 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10300 " DWO info/types section [in module %s]"),
10303 /* Must have an abbrev section. */
10304 if (ids_seen
[DW_SECT_ABBREV_V5
] == -1)
10306 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10307 " section [in module %s]"),
10310 htab
->section_pool
.v5
.offsets
= ids_ptr
+ sizeof (uint32_t) * nr_columns
;
10311 htab
->section_pool
.v5
.sizes
10312 = htab
->section_pool
.v5
.offsets
+ (sizeof (uint32_t)
10313 * nr_units
* nr_columns
);
10314 if ((htab
->section_pool
.v5
.sizes
+ (sizeof (uint32_t)
10315 * nr_units
* nr_columns
))
10318 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10319 " [in module %s]"),
10327 /* Update SECTIONS with the data from SECTP.
10329 This function is like the other "locate" section routines, but in
10330 this context the sections to read comes from the DWP V1 hash table,
10331 not the full ELF section table.
10333 The result is non-zero for success, or zero if an error was found. */
10336 locate_v1_virtual_dwo_sections (asection
*sectp
,
10337 struct virtual_v1_dwo_sections
*sections
)
10339 const struct dwop_section_names
*names
= &dwop_section_names
;
10341 if (names
->abbrev_dwo
.matches (sectp
->name
))
10343 /* There can be only one. */
10344 if (sections
->abbrev
.s
.section
!= NULL
)
10346 sections
->abbrev
.s
.section
= sectp
;
10347 sections
->abbrev
.size
= bfd_section_size (sectp
);
10349 else if (names
->info_dwo
.matches (sectp
->name
)
10350 || names
->types_dwo
.matches (sectp
->name
))
10352 /* There can be only one. */
10353 if (sections
->info_or_types
.s
.section
!= NULL
)
10355 sections
->info_or_types
.s
.section
= sectp
;
10356 sections
->info_or_types
.size
= bfd_section_size (sectp
);
10358 else if (names
->line_dwo
.matches (sectp
->name
))
10360 /* There can be only one. */
10361 if (sections
->line
.s
.section
!= NULL
)
10363 sections
->line
.s
.section
= sectp
;
10364 sections
->line
.size
= bfd_section_size (sectp
);
10366 else if (names
->loc_dwo
.matches (sectp
->name
))
10368 /* There can be only one. */
10369 if (sections
->loc
.s
.section
!= NULL
)
10371 sections
->loc
.s
.section
= sectp
;
10372 sections
->loc
.size
= bfd_section_size (sectp
);
10374 else if (names
->macinfo_dwo
.matches (sectp
->name
))
10376 /* There can be only one. */
10377 if (sections
->macinfo
.s
.section
!= NULL
)
10379 sections
->macinfo
.s
.section
= sectp
;
10380 sections
->macinfo
.size
= bfd_section_size (sectp
);
10382 else if (names
->macro_dwo
.matches (sectp
->name
))
10384 /* There can be only one. */
10385 if (sections
->macro
.s
.section
!= NULL
)
10387 sections
->macro
.s
.section
= sectp
;
10388 sections
->macro
.size
= bfd_section_size (sectp
);
10390 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
10392 /* There can be only one. */
10393 if (sections
->str_offsets
.s
.section
!= NULL
)
10395 sections
->str_offsets
.s
.section
= sectp
;
10396 sections
->str_offsets
.size
= bfd_section_size (sectp
);
10400 /* No other kind of section is valid. */
10407 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10408 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10409 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10410 This is for DWP version 1 files. */
10412 static struct dwo_unit
*
10413 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile
*per_objfile
,
10414 struct dwp_file
*dwp_file
,
10415 uint32_t unit_index
,
10416 const char *comp_dir
,
10417 ULONGEST signature
, int is_debug_types
)
10419 const struct dwp_hash_table
*dwp_htab
=
10420 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10421 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10422 const char *kind
= is_debug_types
? "TU" : "CU";
10423 struct dwo_file
*dwo_file
;
10424 struct dwo_unit
*dwo_unit
;
10425 struct virtual_v1_dwo_sections sections
;
10426 void **dwo_file_slot
;
10429 gdb_assert (dwp_file
->version
== 1);
10431 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
10432 kind
, pulongest (unit_index
), hex_string (signature
),
10435 /* Fetch the sections of this DWO unit.
10436 Put a limit on the number of sections we look for so that bad data
10437 doesn't cause us to loop forever. */
10439 #define MAX_NR_V1_DWO_SECTIONS \
10440 (1 /* .debug_info or .debug_types */ \
10441 + 1 /* .debug_abbrev */ \
10442 + 1 /* .debug_line */ \
10443 + 1 /* .debug_loc */ \
10444 + 1 /* .debug_str_offsets */ \
10445 + 1 /* .debug_macro or .debug_macinfo */ \
10446 + 1 /* trailing zero */)
10448 memset (§ions
, 0, sizeof (sections
));
10450 for (i
= 0; i
< MAX_NR_V1_DWO_SECTIONS
; ++i
)
10453 uint32_t section_nr
=
10454 read_4_bytes (dbfd
,
10455 dwp_htab
->section_pool
.v1
.indices
10456 + (unit_index
+ i
) * sizeof (uint32_t));
10458 if (section_nr
== 0)
10460 if (section_nr
>= dwp_file
->num_sections
)
10462 error (_("Dwarf Error: bad DWP hash table, section number too large"
10463 " [in module %s]"),
10467 sectp
= dwp_file
->elf_sections
[section_nr
];
10468 if (! locate_v1_virtual_dwo_sections (sectp
, §ions
))
10470 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10471 " [in module %s]"),
10477 || sections
.info_or_types
.empty ()
10478 || sections
.abbrev
.empty ())
10480 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10481 " [in module %s]"),
10484 if (i
== MAX_NR_V1_DWO_SECTIONS
)
10486 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10487 " [in module %s]"),
10491 /* It's easier for the rest of the code if we fake a struct dwo_file and
10492 have dwo_unit "live" in that. At least for now.
10494 The DWP file can be made up of a random collection of CUs and TUs.
10495 However, for each CU + set of TUs that came from the same original DWO
10496 file, we can combine them back into a virtual DWO file to save space
10497 (fewer struct dwo_file objects to allocate). Remember that for really
10498 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10500 std::string virtual_dwo_name
=
10501 string_printf ("virtual-dwo/%d-%d-%d-%d",
10502 sections
.abbrev
.get_id (),
10503 sections
.line
.get_id (),
10504 sections
.loc
.get_id (),
10505 sections
.str_offsets
.get_id ());
10506 /* Can we use an existing virtual DWO file? */
10507 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10509 /* Create one if necessary. */
10510 if (*dwo_file_slot
== NULL
)
10512 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10513 virtual_dwo_name
.c_str ());
10515 dwo_file
= new struct dwo_file
;
10516 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10517 dwo_file
->comp_dir
= comp_dir
;
10518 dwo_file
->sections
.abbrev
= sections
.abbrev
;
10519 dwo_file
->sections
.line
= sections
.line
;
10520 dwo_file
->sections
.loc
= sections
.loc
;
10521 dwo_file
->sections
.macinfo
= sections
.macinfo
;
10522 dwo_file
->sections
.macro
= sections
.macro
;
10523 dwo_file
->sections
.str_offsets
= sections
.str_offsets
;
10524 /* The "str" section is global to the entire DWP file. */
10525 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10526 /* The info or types section is assigned below to dwo_unit,
10527 there's no need to record it in dwo_file.
10528 Also, we can't simply record type sections in dwo_file because
10529 we record a pointer into the vector in dwo_unit. As we collect more
10530 types we'll grow the vector and eventually have to reallocate space
10531 for it, invalidating all copies of pointers into the previous
10533 *dwo_file_slot
= dwo_file
;
10537 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10538 virtual_dwo_name
.c_str ());
10540 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10543 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10544 dwo_unit
->dwo_file
= dwo_file
;
10545 dwo_unit
->signature
= signature
;
10546 dwo_unit
->section
=
10547 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10548 *dwo_unit
->section
= sections
.info_or_types
;
10549 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10554 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
10555 simplify them. Given a pointer to the containing section SECTION, and
10556 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
10557 virtual section of just that piece. */
10559 static struct dwarf2_section_info
10560 create_dwp_v2_or_v5_section (dwarf2_per_objfile
*per_objfile
,
10561 struct dwarf2_section_info
*section
,
10562 bfd_size_type offset
, bfd_size_type size
)
10564 struct dwarf2_section_info result
;
10567 gdb_assert (section
!= NULL
);
10568 gdb_assert (!section
->is_virtual
);
10570 memset (&result
, 0, sizeof (result
));
10571 result
.s
.containing_section
= section
;
10572 result
.is_virtual
= true;
10577 sectp
= section
->get_bfd_section ();
10579 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10580 bounds of the real section. This is a pretty-rare event, so just
10581 flag an error (easier) instead of a warning and trying to cope. */
10583 || offset
+ size
> bfd_section_size (sectp
))
10585 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
10586 " in section %s [in module %s]"),
10587 sectp
? bfd_section_name (sectp
) : "<unknown>",
10588 objfile_name (per_objfile
->objfile
));
10591 result
.virtual_offset
= offset
;
10592 result
.size
= size
;
10596 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10597 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10598 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10599 This is for DWP version 2 files. */
10601 static struct dwo_unit
*
10602 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile
*per_objfile
,
10603 struct dwp_file
*dwp_file
,
10604 uint32_t unit_index
,
10605 const char *comp_dir
,
10606 ULONGEST signature
, int is_debug_types
)
10608 const struct dwp_hash_table
*dwp_htab
=
10609 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10610 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10611 const char *kind
= is_debug_types
? "TU" : "CU";
10612 struct dwo_file
*dwo_file
;
10613 struct dwo_unit
*dwo_unit
;
10614 struct virtual_v2_or_v5_dwo_sections sections
;
10615 void **dwo_file_slot
;
10618 gdb_assert (dwp_file
->version
== 2);
10620 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
10621 kind
, pulongest (unit_index
), hex_string (signature
),
10624 /* Fetch the section offsets of this DWO unit. */
10626 memset (§ions
, 0, sizeof (sections
));
10628 for (i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10630 uint32_t offset
= read_4_bytes (dbfd
,
10631 dwp_htab
->section_pool
.v2
.offsets
10632 + (((unit_index
- 1) * dwp_htab
->nr_columns
10634 * sizeof (uint32_t)));
10635 uint32_t size
= read_4_bytes (dbfd
,
10636 dwp_htab
->section_pool
.v2
.sizes
10637 + (((unit_index
- 1) * dwp_htab
->nr_columns
10639 * sizeof (uint32_t)));
10641 switch (dwp_htab
->section_pool
.v2
.section_ids
[i
])
10644 case DW_SECT_TYPES
:
10645 sections
.info_or_types_offset
= offset
;
10646 sections
.info_or_types_size
= size
;
10648 case DW_SECT_ABBREV
:
10649 sections
.abbrev_offset
= offset
;
10650 sections
.abbrev_size
= size
;
10653 sections
.line_offset
= offset
;
10654 sections
.line_size
= size
;
10657 sections
.loc_offset
= offset
;
10658 sections
.loc_size
= size
;
10660 case DW_SECT_STR_OFFSETS
:
10661 sections
.str_offsets_offset
= offset
;
10662 sections
.str_offsets_size
= size
;
10664 case DW_SECT_MACINFO
:
10665 sections
.macinfo_offset
= offset
;
10666 sections
.macinfo_size
= size
;
10668 case DW_SECT_MACRO
:
10669 sections
.macro_offset
= offset
;
10670 sections
.macro_size
= size
;
10675 /* It's easier for the rest of the code if we fake a struct dwo_file and
10676 have dwo_unit "live" in that. At least for now.
10678 The DWP file can be made up of a random collection of CUs and TUs.
10679 However, for each CU + set of TUs that came from the same original DWO
10680 file, we can combine them back into a virtual DWO file to save space
10681 (fewer struct dwo_file objects to allocate). Remember that for really
10682 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10684 std::string virtual_dwo_name
=
10685 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
10686 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10687 (long) (sections
.line_size
? sections
.line_offset
: 0),
10688 (long) (sections
.loc_size
? sections
.loc_offset
: 0),
10689 (long) (sections
.str_offsets_size
10690 ? sections
.str_offsets_offset
: 0));
10691 /* Can we use an existing virtual DWO file? */
10692 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, virtual_dwo_name
.c_str (),
10694 /* Create one if necessary. */
10695 if (*dwo_file_slot
== NULL
)
10697 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10698 virtual_dwo_name
.c_str ());
10700 dwo_file
= new struct dwo_file
;
10701 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10702 dwo_file
->comp_dir
= comp_dir
;
10703 dwo_file
->sections
.abbrev
=
10704 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.abbrev
,
10705 sections
.abbrev_offset
,
10706 sections
.abbrev_size
);
10707 dwo_file
->sections
.line
=
10708 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.line
,
10709 sections
.line_offset
,
10710 sections
.line_size
);
10711 dwo_file
->sections
.loc
=
10712 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.loc
,
10713 sections
.loc_offset
, sections
.loc_size
);
10714 dwo_file
->sections
.macinfo
=
10715 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macinfo
,
10716 sections
.macinfo_offset
,
10717 sections
.macinfo_size
);
10718 dwo_file
->sections
.macro
=
10719 create_dwp_v2_or_v5_section (per_objfile
, &dwp_file
->sections
.macro
,
10720 sections
.macro_offset
,
10721 sections
.macro_size
);
10722 dwo_file
->sections
.str_offsets
=
10723 create_dwp_v2_or_v5_section (per_objfile
,
10724 &dwp_file
->sections
.str_offsets
,
10725 sections
.str_offsets_offset
,
10726 sections
.str_offsets_size
);
10727 /* The "str" section is global to the entire DWP file. */
10728 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10729 /* The info or types section is assigned below to dwo_unit,
10730 there's no need to record it in dwo_file.
10731 Also, we can't simply record type sections in dwo_file because
10732 we record a pointer into the vector in dwo_unit. As we collect more
10733 types we'll grow the vector and eventually have to reallocate space
10734 for it, invalidating all copies of pointers into the previous
10736 *dwo_file_slot
= dwo_file
;
10740 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10741 virtual_dwo_name
.c_str ());
10743 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10746 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10747 dwo_unit
->dwo_file
= dwo_file
;
10748 dwo_unit
->signature
= signature
;
10749 dwo_unit
->section
=
10750 XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10751 *dwo_unit
->section
= create_dwp_v2_or_v5_section
10754 ? &dwp_file
->sections
.types
10755 : &dwp_file
->sections
.info
,
10756 sections
.info_or_types_offset
,
10757 sections
.info_or_types_size
);
10758 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10763 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10764 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10765 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10766 This is for DWP version 5 files. */
10768 static struct dwo_unit
*
10769 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile
*per_objfile
,
10770 struct dwp_file
*dwp_file
,
10771 uint32_t unit_index
,
10772 const char *comp_dir
,
10773 ULONGEST signature
, int is_debug_types
)
10775 const struct dwp_hash_table
*dwp_htab
10776 = is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10777 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10778 const char *kind
= is_debug_types
? "TU" : "CU";
10779 struct dwo_file
*dwo_file
;
10780 struct dwo_unit
*dwo_unit
;
10781 struct virtual_v2_or_v5_dwo_sections sections
{};
10782 void **dwo_file_slot
;
10784 gdb_assert (dwp_file
->version
== 5);
10786 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
10787 kind
, pulongest (unit_index
), hex_string (signature
),
10790 /* Fetch the section offsets of this DWO unit. */
10792 /* memset (§ions, 0, sizeof (sections)); */
10794 for (int i
= 0; i
< dwp_htab
->nr_columns
; ++i
)
10796 uint32_t offset
= read_4_bytes (dbfd
,
10797 dwp_htab
->section_pool
.v5
.offsets
10798 + (((unit_index
- 1)
10799 * dwp_htab
->nr_columns
10801 * sizeof (uint32_t)));
10802 uint32_t size
= read_4_bytes (dbfd
,
10803 dwp_htab
->section_pool
.v5
.sizes
10804 + (((unit_index
- 1) * dwp_htab
->nr_columns
10806 * sizeof (uint32_t)));
10808 switch (dwp_htab
->section_pool
.v5
.section_ids
[i
])
10810 case DW_SECT_ABBREV_V5
:
10811 sections
.abbrev_offset
= offset
;
10812 sections
.abbrev_size
= size
;
10814 case DW_SECT_INFO_V5
:
10815 sections
.info_or_types_offset
= offset
;
10816 sections
.info_or_types_size
= size
;
10818 case DW_SECT_LINE_V5
:
10819 sections
.line_offset
= offset
;
10820 sections
.line_size
= size
;
10822 case DW_SECT_LOCLISTS_V5
:
10823 sections
.loclists_offset
= offset
;
10824 sections
.loclists_size
= size
;
10826 case DW_SECT_MACRO_V5
:
10827 sections
.macro_offset
= offset
;
10828 sections
.macro_size
= size
;
10830 case DW_SECT_RNGLISTS_V5
:
10831 sections
.rnglists_offset
= offset
;
10832 sections
.rnglists_size
= size
;
10834 case DW_SECT_STR_OFFSETS_V5
:
10835 sections
.str_offsets_offset
= offset
;
10836 sections
.str_offsets_size
= size
;
10838 case DW_SECT_RESERVED_V5
:
10844 /* It's easier for the rest of the code if we fake a struct dwo_file and
10845 have dwo_unit "live" in that. At least for now.
10847 The DWP file can be made up of a random collection of CUs and TUs.
10848 However, for each CU + set of TUs that came from the same original DWO
10849 file, we can combine them back into a virtual DWO file to save space
10850 (fewer struct dwo_file objects to allocate). Remember that for really
10851 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10853 std::string virtual_dwo_name
=
10854 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
10855 (long) (sections
.abbrev_size
? sections
.abbrev_offset
: 0),
10856 (long) (sections
.line_size
? sections
.line_offset
: 0),
10857 (long) (sections
.loclists_size
? sections
.loclists_offset
: 0),
10858 (long) (sections
.str_offsets_size
10859 ? sections
.str_offsets_offset
: 0),
10860 (long) (sections
.macro_size
? sections
.macro_offset
: 0),
10861 (long) (sections
.rnglists_size
? sections
.rnglists_offset
: 0));
10862 /* Can we use an existing virtual DWO file? */
10863 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
,
10864 virtual_dwo_name
.c_str (),
10866 /* Create one if necessary. */
10867 if (*dwo_file_slot
== NULL
)
10869 dwarf_read_debug_printf ("Creating virtual DWO: %s",
10870 virtual_dwo_name
.c_str ());
10872 dwo_file
= new struct dwo_file
;
10873 dwo_file
->dwo_name
= per_objfile
->objfile
->intern (virtual_dwo_name
);
10874 dwo_file
->comp_dir
= comp_dir
;
10875 dwo_file
->sections
.abbrev
=
10876 create_dwp_v2_or_v5_section (per_objfile
,
10877 &dwp_file
->sections
.abbrev
,
10878 sections
.abbrev_offset
,
10879 sections
.abbrev_size
);
10880 dwo_file
->sections
.line
=
10881 create_dwp_v2_or_v5_section (per_objfile
,
10882 &dwp_file
->sections
.line
,
10883 sections
.line_offset
, sections
.line_size
);
10884 dwo_file
->sections
.macro
=
10885 create_dwp_v2_or_v5_section (per_objfile
,
10886 &dwp_file
->sections
.macro
,
10887 sections
.macro_offset
,
10888 sections
.macro_size
);
10889 dwo_file
->sections
.loclists
=
10890 create_dwp_v2_or_v5_section (per_objfile
,
10891 &dwp_file
->sections
.loclists
,
10892 sections
.loclists_offset
,
10893 sections
.loclists_size
);
10894 dwo_file
->sections
.rnglists
=
10895 create_dwp_v2_or_v5_section (per_objfile
,
10896 &dwp_file
->sections
.rnglists
,
10897 sections
.rnglists_offset
,
10898 sections
.rnglists_size
);
10899 dwo_file
->sections
.str_offsets
=
10900 create_dwp_v2_or_v5_section (per_objfile
,
10901 &dwp_file
->sections
.str_offsets
,
10902 sections
.str_offsets_offset
,
10903 sections
.str_offsets_size
);
10904 /* The "str" section is global to the entire DWP file. */
10905 dwo_file
->sections
.str
= dwp_file
->sections
.str
;
10906 /* The info or types section is assigned below to dwo_unit,
10907 there's no need to record it in dwo_file.
10908 Also, we can't simply record type sections in dwo_file because
10909 we record a pointer into the vector in dwo_unit. As we collect more
10910 types we'll grow the vector and eventually have to reallocate space
10911 for it, invalidating all copies of pointers into the previous
10913 *dwo_file_slot
= dwo_file
;
10917 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
10918 virtual_dwo_name
.c_str ());
10920 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
10923 dwo_unit
= OBSTACK_ZALLOC (&per_objfile
->per_bfd
->obstack
, struct dwo_unit
);
10924 dwo_unit
->dwo_file
= dwo_file
;
10925 dwo_unit
->signature
= signature
;
10927 = XOBNEW (&per_objfile
->per_bfd
->obstack
, struct dwarf2_section_info
);
10928 *dwo_unit
->section
= create_dwp_v2_or_v5_section (per_objfile
,
10929 &dwp_file
->sections
.info
,
10930 sections
.info_or_types_offset
,
10931 sections
.info_or_types_size
);
10932 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10937 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10938 Returns NULL if the signature isn't found. */
10940 static struct dwo_unit
*
10941 lookup_dwo_unit_in_dwp (dwarf2_per_objfile
*per_objfile
,
10942 struct dwp_file
*dwp_file
, const char *comp_dir
,
10943 ULONGEST signature
, int is_debug_types
)
10945 const struct dwp_hash_table
*dwp_htab
=
10946 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
10947 bfd
*dbfd
= dwp_file
->dbfd
.get ();
10948 uint32_t mask
= dwp_htab
->nr_slots
- 1;
10949 uint32_t hash
= signature
& mask
;
10950 uint32_t hash2
= ((signature
>> 32) & mask
) | 1;
10953 struct dwo_unit find_dwo_cu
;
10955 memset (&find_dwo_cu
, 0, sizeof (find_dwo_cu
));
10956 find_dwo_cu
.signature
= signature
;
10957 slot
= htab_find_slot (is_debug_types
10958 ? dwp_file
->loaded_tus
.get ()
10959 : dwp_file
->loaded_cus
.get (),
10960 &find_dwo_cu
, INSERT
);
10963 return (struct dwo_unit
*) *slot
;
10965 /* Use a for loop so that we don't loop forever on bad debug info. */
10966 for (i
= 0; i
< dwp_htab
->nr_slots
; ++i
)
10968 ULONGEST signature_in_table
;
10970 signature_in_table
=
10971 read_8_bytes (dbfd
, dwp_htab
->hash_table
+ hash
* sizeof (uint64_t));
10972 if (signature_in_table
== signature
)
10974 uint32_t unit_index
=
10975 read_4_bytes (dbfd
,
10976 dwp_htab
->unit_table
+ hash
* sizeof (uint32_t));
10978 if (dwp_file
->version
== 1)
10980 *slot
= create_dwo_unit_in_dwp_v1 (per_objfile
, dwp_file
,
10981 unit_index
, comp_dir
,
10982 signature
, is_debug_types
);
10984 else if (dwp_file
->version
== 2)
10986 *slot
= create_dwo_unit_in_dwp_v2 (per_objfile
, dwp_file
,
10987 unit_index
, comp_dir
,
10988 signature
, is_debug_types
);
10990 else /* version == 5 */
10992 *slot
= create_dwo_unit_in_dwp_v5 (per_objfile
, dwp_file
,
10993 unit_index
, comp_dir
,
10994 signature
, is_debug_types
);
10996 return (struct dwo_unit
*) *slot
;
10998 if (signature_in_table
== 0)
11000 hash
= (hash
+ hash2
) & mask
;
11003 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11004 " [in module %s]"),
11008 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11009 Open the file specified by FILE_NAME and hand it off to BFD for
11010 preliminary analysis. Return a newly initialized bfd *, which
11011 includes a canonicalized copy of FILE_NAME.
11012 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11013 SEARCH_CWD is true if the current directory is to be searched.
11014 It will be searched before debug-file-directory.
11015 If successful, the file is added to the bfd include table of the
11016 objfile's bfd (see gdb_bfd_record_inclusion).
11017 If unable to find/open the file, return NULL.
11018 NOTE: This function is derived from symfile_bfd_open. */
11020 static gdb_bfd_ref_ptr
11021 try_open_dwop_file (dwarf2_per_objfile
*per_objfile
,
11022 const char *file_name
, int is_dwp
, int search_cwd
)
11025 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11026 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11027 to debug_file_directory. */
11028 const char *search_path
;
11029 static const char dirname_separator_string
[] = { DIRNAME_SEPARATOR
, '\0' };
11031 gdb::unique_xmalloc_ptr
<char> search_path_holder
;
11034 if (!debug_file_directory
.empty ())
11036 search_path_holder
.reset (concat (".", dirname_separator_string
,
11037 debug_file_directory
.c_str (),
11039 search_path
= search_path_holder
.get ();
11045 search_path
= debug_file_directory
.c_str ();
11047 /* Add the path for the executable binary to the list of search paths. */
11048 std::string objfile_dir
= ldirname (objfile_name (per_objfile
->objfile
));
11049 search_path_holder
.reset (concat (objfile_dir
.c_str (),
11050 dirname_separator_string
,
11051 search_path
, nullptr));
11052 search_path
= search_path_holder
.get ();
11054 openp_flags flags
= OPF_RETURN_REALPATH
;
11056 flags
|= OPF_SEARCH_IN_PATH
;
11058 gdb::unique_xmalloc_ptr
<char> absolute_name
;
11059 desc
= openp (search_path
, flags
, file_name
,
11060 O_RDONLY
| O_BINARY
, &absolute_name
);
11064 gdb_bfd_ref_ptr
sym_bfd (gdb_bfd_open (absolute_name
.get (),
11066 if (sym_bfd
== NULL
)
11068 bfd_set_cacheable (sym_bfd
.get (), 1);
11070 if (!bfd_check_format (sym_bfd
.get (), bfd_object
))
11073 /* Success. Record the bfd as having been included by the objfile's bfd.
11074 This is important because things like demangled_names_hash lives in the
11075 objfile's per_bfd space and may have references to things like symbol
11076 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11077 gdb_bfd_record_inclusion (per_objfile
->objfile
->obfd
, sym_bfd
.get ());
11082 /* Try to open DWO file FILE_NAME.
11083 COMP_DIR is the DW_AT_comp_dir attribute.
11084 The result is the bfd handle of the file.
11085 If there is a problem finding or opening the file, return NULL.
11086 Upon success, the canonicalized path of the file is stored in the bfd,
11087 same as symfile_bfd_open. */
11089 static gdb_bfd_ref_ptr
11090 open_dwo_file (dwarf2_per_objfile
*per_objfile
,
11091 const char *file_name
, const char *comp_dir
)
11093 if (IS_ABSOLUTE_PATH (file_name
))
11094 return try_open_dwop_file (per_objfile
, file_name
,
11095 0 /*is_dwp*/, 0 /*search_cwd*/);
11097 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11099 if (comp_dir
!= NULL
)
11101 std::string path_to_try
= path_join (comp_dir
, file_name
);
11103 /* NOTE: If comp_dir is a relative path, this will also try the
11104 search path, which seems useful. */
11105 gdb_bfd_ref_ptr
abfd (try_open_dwop_file
11106 (per_objfile
, path_to_try
.c_str (), 0 /*is_dwp*/, 1 /*search_cwd*/));
11112 /* That didn't work, try debug-file-directory, which, despite its name,
11113 is a list of paths. */
11115 if (debug_file_directory
.empty ())
11118 return try_open_dwop_file (per_objfile
, file_name
,
11119 0 /*is_dwp*/, 1 /*search_cwd*/);
11122 /* This function is mapped across the sections and remembers the offset and
11123 size of each of the DWO debugging sections we are interested in. */
11126 dwarf2_locate_dwo_sections (struct objfile
*objfile
, bfd
*abfd
,
11127 asection
*sectp
, dwo_sections
*dwo_sections
)
11129 const struct dwop_section_names
*names
= &dwop_section_names
;
11131 struct dwarf2_section_info
*dw_sect
= nullptr;
11133 if (names
->abbrev_dwo
.matches (sectp
->name
))
11134 dw_sect
= &dwo_sections
->abbrev
;
11135 else if (names
->info_dwo
.matches (sectp
->name
))
11136 dw_sect
= &dwo_sections
->info
;
11137 else if (names
->line_dwo
.matches (sectp
->name
))
11138 dw_sect
= &dwo_sections
->line
;
11139 else if (names
->loc_dwo
.matches (sectp
->name
))
11140 dw_sect
= &dwo_sections
->loc
;
11141 else if (names
->loclists_dwo
.matches (sectp
->name
))
11142 dw_sect
= &dwo_sections
->loclists
;
11143 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11144 dw_sect
= &dwo_sections
->macinfo
;
11145 else if (names
->macro_dwo
.matches (sectp
->name
))
11146 dw_sect
= &dwo_sections
->macro
;
11147 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11148 dw_sect
= &dwo_sections
->rnglists
;
11149 else if (names
->str_dwo
.matches (sectp
->name
))
11150 dw_sect
= &dwo_sections
->str
;
11151 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11152 dw_sect
= &dwo_sections
->str_offsets
;
11153 else if (names
->types_dwo
.matches (sectp
->name
))
11155 struct dwarf2_section_info type_section
;
11157 memset (&type_section
, 0, sizeof (type_section
));
11158 dwo_sections
->types
.push_back (type_section
);
11159 dw_sect
= &dwo_sections
->types
.back ();
11162 if (dw_sect
!= nullptr)
11164 dw_sect
->s
.section
= sectp
;
11165 dw_sect
->size
= bfd_section_size (sectp
);
11166 dw_sect
->read (objfile
);
11170 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
11171 by PER_CU. This is for the non-DWP case.
11172 The result is NULL if DWO_NAME can't be found. */
11174 static struct dwo_file
*
11175 open_and_init_dwo_file (dwarf2_cu
*cu
, const char *dwo_name
,
11176 const char *comp_dir
)
11178 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11180 gdb_bfd_ref_ptr dbfd
= open_dwo_file (per_objfile
, dwo_name
, comp_dir
);
11183 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name
);
11188 dwo_file_up
dwo_file (new struct dwo_file
);
11189 dwo_file
->dwo_name
= dwo_name
;
11190 dwo_file
->comp_dir
= comp_dir
;
11191 dwo_file
->dbfd
= std::move (dbfd
);
11193 for (asection
*sec
: gdb_bfd_sections (dwo_file
->dbfd
))
11194 dwarf2_locate_dwo_sections (per_objfile
->objfile
, dwo_file
->dbfd
.get (),
11195 sec
, &dwo_file
->sections
);
11197 create_cus_hash_table (per_objfile
, cu
, *dwo_file
, dwo_file
->sections
.info
,
11200 if (cu
->per_cu
->dwarf_version
< 5)
11202 create_debug_types_hash_table (per_objfile
, dwo_file
.get (),
11203 dwo_file
->sections
.types
, dwo_file
->tus
);
11207 create_debug_type_hash_table (per_objfile
, dwo_file
.get (),
11208 &dwo_file
->sections
.info
, dwo_file
->tus
,
11209 rcuh_kind::COMPILE
);
11212 dwarf_read_debug_printf ("DWO file found: %s", dwo_name
);
11214 return dwo_file
.release ();
11217 /* This function is mapped across the sections and remembers the offset and
11218 size of each of the DWP debugging sections common to version 1 and 2 that
11219 we are interested in. */
11222 dwarf2_locate_common_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11223 asection
*sectp
, dwp_file
*dwp_file
)
11225 const struct dwop_section_names
*names
= &dwop_section_names
;
11226 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11228 /* Record the ELF section number for later lookup: this is what the
11229 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11230 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11231 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11233 /* Look for specific sections that we need. */
11234 struct dwarf2_section_info
*dw_sect
= nullptr;
11235 if (names
->str_dwo
.matches (sectp
->name
))
11236 dw_sect
= &dwp_file
->sections
.str
;
11237 else if (names
->cu_index
.matches (sectp
->name
))
11238 dw_sect
= &dwp_file
->sections
.cu_index
;
11239 else if (names
->tu_index
.matches (sectp
->name
))
11240 dw_sect
= &dwp_file
->sections
.tu_index
;
11242 if (dw_sect
!= nullptr)
11244 dw_sect
->s
.section
= sectp
;
11245 dw_sect
->size
= bfd_section_size (sectp
);
11246 dw_sect
->read (objfile
);
11250 /* This function is mapped across the sections and remembers the offset and
11251 size of each of the DWP version 2 debugging sections that we are interested
11252 in. This is split into a separate function because we don't know if we
11253 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11256 dwarf2_locate_v2_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11257 asection
*sectp
, void *dwp_file_ptr
)
11259 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11260 const struct dwop_section_names
*names
= &dwop_section_names
;
11261 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11263 /* Record the ELF section number for later lookup: this is what the
11264 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11265 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11266 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11268 /* Look for specific sections that we need. */
11269 struct dwarf2_section_info
*dw_sect
= nullptr;
11270 if (names
->abbrev_dwo
.matches (sectp
->name
))
11271 dw_sect
= &dwp_file
->sections
.abbrev
;
11272 else if (names
->info_dwo
.matches (sectp
->name
))
11273 dw_sect
= &dwp_file
->sections
.info
;
11274 else if (names
->line_dwo
.matches (sectp
->name
))
11275 dw_sect
= &dwp_file
->sections
.line
;
11276 else if (names
->loc_dwo
.matches (sectp
->name
))
11277 dw_sect
= &dwp_file
->sections
.loc
;
11278 else if (names
->macinfo_dwo
.matches (sectp
->name
))
11279 dw_sect
= &dwp_file
->sections
.macinfo
;
11280 else if (names
->macro_dwo
.matches (sectp
->name
))
11281 dw_sect
= &dwp_file
->sections
.macro
;
11282 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11283 dw_sect
= &dwp_file
->sections
.str_offsets
;
11284 else if (names
->types_dwo
.matches (sectp
->name
))
11285 dw_sect
= &dwp_file
->sections
.types
;
11287 if (dw_sect
!= nullptr)
11289 dw_sect
->s
.section
= sectp
;
11290 dw_sect
->size
= bfd_section_size (sectp
);
11291 dw_sect
->read (objfile
);
11295 /* This function is mapped across the sections and remembers the offset and
11296 size of each of the DWP version 5 debugging sections that we are interested
11297 in. This is split into a separate function because we don't know if we
11298 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
11301 dwarf2_locate_v5_dwp_sections (struct objfile
*objfile
, bfd
*abfd
,
11302 asection
*sectp
, void *dwp_file_ptr
)
11304 struct dwp_file
*dwp_file
= (struct dwp_file
*) dwp_file_ptr
;
11305 const struct dwop_section_names
*names
= &dwop_section_names
;
11306 unsigned int elf_section_nr
= elf_section_data (sectp
)->this_idx
;
11308 /* Record the ELF section number for later lookup: this is what the
11309 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
11310 gdb_assert (elf_section_nr
< dwp_file
->num_sections
);
11311 dwp_file
->elf_sections
[elf_section_nr
] = sectp
;
11313 /* Look for specific sections that we need. */
11314 struct dwarf2_section_info
*dw_sect
= nullptr;
11315 if (names
->abbrev_dwo
.matches (sectp
->name
))
11316 dw_sect
= &dwp_file
->sections
.abbrev
;
11317 else if (names
->info_dwo
.matches (sectp
->name
))
11318 dw_sect
= &dwp_file
->sections
.info
;
11319 else if (names
->line_dwo
.matches (sectp
->name
))
11320 dw_sect
= &dwp_file
->sections
.line
;
11321 else if (names
->loclists_dwo
.matches (sectp
->name
))
11322 dw_sect
= &dwp_file
->sections
.loclists
;
11323 else if (names
->macro_dwo
.matches (sectp
->name
))
11324 dw_sect
= &dwp_file
->sections
.macro
;
11325 else if (names
->rnglists_dwo
.matches (sectp
->name
))
11326 dw_sect
= &dwp_file
->sections
.rnglists
;
11327 else if (names
->str_offsets_dwo
.matches (sectp
->name
))
11328 dw_sect
= &dwp_file
->sections
.str_offsets
;
11330 if (dw_sect
!= nullptr)
11332 dw_sect
->s
.section
= sectp
;
11333 dw_sect
->size
= bfd_section_size (sectp
);
11334 dw_sect
->read (objfile
);
11338 /* Hash function for dwp_file loaded CUs/TUs. */
11341 hash_dwp_loaded_cutus (const void *item
)
11343 const struct dwo_unit
*dwo_unit
= (const struct dwo_unit
*) item
;
11345 /* This drops the top 32 bits of the signature, but is ok for a hash. */
11346 return dwo_unit
->signature
;
11349 /* Equality function for dwp_file loaded CUs/TUs. */
11352 eq_dwp_loaded_cutus (const void *a
, const void *b
)
11354 const struct dwo_unit
*dua
= (const struct dwo_unit
*) a
;
11355 const struct dwo_unit
*dub
= (const struct dwo_unit
*) b
;
11357 return dua
->signature
== dub
->signature
;
11360 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
11363 allocate_dwp_loaded_cutus_table ()
11365 return htab_up (htab_create_alloc (3,
11366 hash_dwp_loaded_cutus
,
11367 eq_dwp_loaded_cutus
,
11368 NULL
, xcalloc
, xfree
));
11371 /* Try to open DWP file FILE_NAME.
11372 The result is the bfd handle of the file.
11373 If there is a problem finding or opening the file, return NULL.
11374 Upon success, the canonicalized path of the file is stored in the bfd,
11375 same as symfile_bfd_open. */
11377 static gdb_bfd_ref_ptr
11378 open_dwp_file (dwarf2_per_objfile
*per_objfile
, const char *file_name
)
11380 gdb_bfd_ref_ptr
abfd (try_open_dwop_file (per_objfile
, file_name
,
11382 1 /*search_cwd*/));
11386 /* Work around upstream bug 15652.
11387 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
11388 [Whether that's a "bug" is debatable, but it is getting in our way.]
11389 We have no real idea where the dwp file is, because gdb's realpath-ing
11390 of the executable's path may have discarded the needed info.
11391 [IWBN if the dwp file name was recorded in the executable, akin to
11392 .gnu_debuglink, but that doesn't exist yet.]
11393 Strip the directory from FILE_NAME and search again. */
11394 if (!debug_file_directory
.empty ())
11396 /* Don't implicitly search the current directory here.
11397 If the user wants to search "." to handle this case,
11398 it must be added to debug-file-directory. */
11399 return try_open_dwop_file (per_objfile
, lbasename (file_name
),
11407 /* Initialize the use of the DWP file for the current objfile.
11408 By convention the name of the DWP file is ${objfile}.dwp.
11409 The result is NULL if it can't be found. */
11411 static std::unique_ptr
<struct dwp_file
>
11412 open_and_init_dwp_file (dwarf2_per_objfile
*per_objfile
)
11414 struct objfile
*objfile
= per_objfile
->objfile
;
11416 /* Try to find first .dwp for the binary file before any symbolic links
11419 /* If the objfile is a debug file, find the name of the real binary
11420 file and get the name of dwp file from there. */
11421 std::string dwp_name
;
11422 if (objfile
->separate_debug_objfile_backlink
!= NULL
)
11424 struct objfile
*backlink
= objfile
->separate_debug_objfile_backlink
;
11425 const char *backlink_basename
= lbasename (backlink
->original_name
);
11427 dwp_name
= ldirname (objfile
->original_name
) + SLASH_STRING
+ backlink_basename
;
11430 dwp_name
= objfile
->original_name
;
11432 dwp_name
+= ".dwp";
11434 gdb_bfd_ref_ptr
dbfd (open_dwp_file (per_objfile
, dwp_name
.c_str ()));
11436 && strcmp (objfile
->original_name
, objfile_name (objfile
)) != 0)
11438 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
11439 dwp_name
= objfile_name (objfile
);
11440 dwp_name
+= ".dwp";
11441 dbfd
= open_dwp_file (per_objfile
, dwp_name
.c_str ());
11446 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name
.c_str ());
11448 return std::unique_ptr
<dwp_file
> ();
11451 const char *name
= bfd_get_filename (dbfd
.get ());
11452 std::unique_ptr
<struct dwp_file
> dwp_file
11453 (new struct dwp_file (name
, std::move (dbfd
)));
11455 dwp_file
->num_sections
= elf_numsections (dwp_file
->dbfd
);
11456 dwp_file
->elf_sections
=
11457 OBSTACK_CALLOC (&per_objfile
->per_bfd
->obstack
,
11458 dwp_file
->num_sections
, asection
*);
11460 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11461 dwarf2_locate_common_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11464 dwp_file
->cus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 0);
11466 dwp_file
->tus
= create_dwp_hash_table (per_objfile
, dwp_file
.get (), 1);
11468 /* The DWP file version is stored in the hash table. Oh well. */
11469 if (dwp_file
->cus
&& dwp_file
->tus
11470 && dwp_file
->cus
->version
!= dwp_file
->tus
->version
)
11472 /* Technically speaking, we should try to limp along, but this is
11473 pretty bizarre. We use pulongest here because that's the established
11474 portability solution (e.g, we cannot use %u for uint32_t). */
11475 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11476 " TU version %s [in DWP file %s]"),
11477 pulongest (dwp_file
->cus
->version
),
11478 pulongest (dwp_file
->tus
->version
), dwp_name
.c_str ());
11482 dwp_file
->version
= dwp_file
->cus
->version
;
11483 else if (dwp_file
->tus
)
11484 dwp_file
->version
= dwp_file
->tus
->version
;
11486 dwp_file
->version
= 2;
11488 for (asection
*sec
: gdb_bfd_sections (dwp_file
->dbfd
))
11490 if (dwp_file
->version
== 2)
11491 dwarf2_locate_v2_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11494 dwarf2_locate_v5_dwp_sections (objfile
, dwp_file
->dbfd
.get (), sec
,
11498 dwp_file
->loaded_cus
= allocate_dwp_loaded_cutus_table ();
11499 dwp_file
->loaded_tus
= allocate_dwp_loaded_cutus_table ();
11501 dwarf_read_debug_printf ("DWP file found: %s", dwp_file
->name
);
11502 dwarf_read_debug_printf (" %s CUs, %s TUs",
11503 pulongest (dwp_file
->cus
? dwp_file
->cus
->nr_units
: 0),
11504 pulongest (dwp_file
->tus
? dwp_file
->tus
->nr_units
: 0));
11509 /* Wrapper around open_and_init_dwp_file, only open it once. */
11511 static struct dwp_file
*
11512 get_dwp_file (dwarf2_per_objfile
*per_objfile
)
11514 if (!per_objfile
->per_bfd
->dwp_checked
)
11516 per_objfile
->per_bfd
->dwp_file
= open_and_init_dwp_file (per_objfile
);
11517 per_objfile
->per_bfd
->dwp_checked
= 1;
11519 return per_objfile
->per_bfd
->dwp_file
.get ();
11522 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11523 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11524 or in the DWP file for the objfile, referenced by THIS_UNIT.
11525 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11526 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11528 This is called, for example, when wanting to read a variable with a
11529 complex location. Therefore we don't want to do file i/o for every call.
11530 Therefore we don't want to look for a DWO file on every call.
11531 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11532 then we check if we've already seen DWO_NAME, and only THEN do we check
11535 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11536 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11538 static struct dwo_unit
*
11539 lookup_dwo_cutu (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11540 ULONGEST signature
, int is_debug_types
)
11542 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
11543 struct objfile
*objfile
= per_objfile
->objfile
;
11544 const char *kind
= is_debug_types
? "TU" : "CU";
11545 void **dwo_file_slot
;
11546 struct dwo_file
*dwo_file
;
11547 struct dwp_file
*dwp_file
;
11549 /* First see if there's a DWP file.
11550 If we have a DWP file but didn't find the DWO inside it, don't
11551 look for the original DWO file. It makes gdb behave differently
11552 depending on whether one is debugging in the build tree. */
11554 dwp_file
= get_dwp_file (per_objfile
);
11555 if (dwp_file
!= NULL
)
11557 const struct dwp_hash_table
*dwp_htab
=
11558 is_debug_types
? dwp_file
->tus
: dwp_file
->cus
;
11560 if (dwp_htab
!= NULL
)
11562 struct dwo_unit
*dwo_cutu
=
11563 lookup_dwo_unit_in_dwp (per_objfile
, dwp_file
, comp_dir
, signature
,
11566 if (dwo_cutu
!= NULL
)
11568 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
11569 kind
, hex_string (signature
),
11570 host_address_to_string (dwo_cutu
));
11578 /* No DWP file, look for the DWO file. */
11580 dwo_file_slot
= lookup_dwo_file_slot (per_objfile
, dwo_name
, comp_dir
);
11581 if (*dwo_file_slot
== NULL
)
11583 /* Read in the file and build a table of the CUs/TUs it contains. */
11584 *dwo_file_slot
= open_and_init_dwo_file (cu
, dwo_name
, comp_dir
);
11586 /* NOTE: This will be NULL if unable to open the file. */
11587 dwo_file
= (struct dwo_file
*) *dwo_file_slot
;
11589 if (dwo_file
!= NULL
)
11591 struct dwo_unit
*dwo_cutu
= NULL
;
11593 if (is_debug_types
&& dwo_file
->tus
)
11595 struct dwo_unit find_dwo_cutu
;
11597 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11598 find_dwo_cutu
.signature
= signature
;
11600 = (struct dwo_unit
*) htab_find (dwo_file
->tus
.get (),
11603 else if (!is_debug_types
&& dwo_file
->cus
)
11605 struct dwo_unit find_dwo_cutu
;
11607 memset (&find_dwo_cutu
, 0, sizeof (find_dwo_cutu
));
11608 find_dwo_cutu
.signature
= signature
;
11609 dwo_cutu
= (struct dwo_unit
*)htab_find (dwo_file
->cus
.get (),
11613 if (dwo_cutu
!= NULL
)
11615 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
11616 kind
, dwo_name
, hex_string (signature
),
11617 host_address_to_string (dwo_cutu
));
11624 /* We didn't find it. This could mean a dwo_id mismatch, or
11625 someone deleted the DWO/DWP file, or the search path isn't set up
11626 correctly to find the file. */
11628 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
11629 kind
, dwo_name
, hex_string (signature
));
11631 /* This is a warning and not a complaint because it can be caused by
11632 pilot error (e.g., user accidentally deleting the DWO). */
11634 /* Print the name of the DWP file if we looked there, helps the user
11635 better diagnose the problem. */
11636 std::string dwp_text
;
11638 if (dwp_file
!= NULL
)
11639 dwp_text
= string_printf (" [in DWP file %s]",
11640 lbasename (dwp_file
->name
));
11642 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
11643 " [in module %s]"),
11644 kind
, dwo_name
, hex_string (signature
), dwp_text
.c_str (), kind
,
11645 sect_offset_str (cu
->per_cu
->sect_off
), objfile_name (objfile
));
11650 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11651 See lookup_dwo_cutu_unit for details. */
11653 static struct dwo_unit
*
11654 lookup_dwo_comp_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
,
11655 ULONGEST signature
)
11657 gdb_assert (!cu
->per_cu
->is_debug_types
);
11659 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, signature
, 0);
11662 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11663 See lookup_dwo_cutu_unit for details. */
11665 static struct dwo_unit
*
11666 lookup_dwo_type_unit (dwarf2_cu
*cu
, const char *dwo_name
, const char *comp_dir
)
11668 gdb_assert (cu
->per_cu
->is_debug_types
);
11670 signatured_type
*sig_type
= (signatured_type
*) cu
->per_cu
;
11672 return lookup_dwo_cutu (cu
, dwo_name
, comp_dir
, sig_type
->signature
, 1);
11675 /* Traversal function for queue_and_load_all_dwo_tus. */
11678 queue_and_load_dwo_tu (void **slot
, void *info
)
11680 struct dwo_unit
*dwo_unit
= (struct dwo_unit
*) *slot
;
11681 dwarf2_cu
*cu
= (dwarf2_cu
*) info
;
11682 ULONGEST signature
= dwo_unit
->signature
;
11683 signatured_type
*sig_type
= lookup_dwo_signatured_type (cu
, signature
);
11685 if (sig_type
!= NULL
)
11687 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11688 a real dependency of PER_CU on SIG_TYPE. That is detected later
11689 while processing PER_CU. */
11690 if (maybe_queue_comp_unit (NULL
, sig_type
, cu
->per_objfile
,
11692 load_full_type_unit (sig_type
, cu
->per_objfile
);
11693 cu
->per_cu
->imported_symtabs_push (sig_type
);
11699 /* Queue all TUs contained in the DWO of CU to be read in.
11700 The DWO may have the only definition of the type, though it may not be
11701 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11702 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11705 queue_and_load_all_dwo_tus (dwarf2_cu
*cu
)
11707 struct dwo_unit
*dwo_unit
;
11708 struct dwo_file
*dwo_file
;
11710 gdb_assert (cu
!= nullptr);
11711 gdb_assert (!cu
->per_cu
->is_debug_types
);
11712 gdb_assert (get_dwp_file (cu
->per_objfile
) == nullptr);
11714 dwo_unit
= cu
->dwo_unit
;
11715 gdb_assert (dwo_unit
!= NULL
);
11717 dwo_file
= dwo_unit
->dwo_file
;
11718 if (dwo_file
->tus
!= NULL
)
11719 htab_traverse_noresize (dwo_file
->tus
.get (), queue_and_load_dwo_tu
, cu
);
11722 /* Read in various DIEs. */
11724 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11725 Inherit only the children of the DW_AT_abstract_origin DIE not being
11726 already referenced by DW_AT_abstract_origin from the children of the
11730 inherit_abstract_dies (struct die_info
*die
, struct dwarf2_cu
*cu
)
11732 struct die_info
*child_die
;
11733 sect_offset
*offsetp
;
11734 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11735 struct die_info
*origin_die
;
11736 /* Iterator of the ORIGIN_DIE children. */
11737 struct die_info
*origin_child_die
;
11738 struct attribute
*attr
;
11739 struct dwarf2_cu
*origin_cu
;
11740 struct pending
**origin_previous_list_in_scope
;
11742 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
11746 /* Note that following die references may follow to a die in a
11750 origin_die
= follow_die_ref (die
, attr
, &origin_cu
);
11752 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11754 origin_previous_list_in_scope
= origin_cu
->list_in_scope
;
11755 origin_cu
->list_in_scope
= cu
->list_in_scope
;
11757 if (die
->tag
!= origin_die
->tag
11758 && !(die
->tag
== DW_TAG_inlined_subroutine
11759 && origin_die
->tag
== DW_TAG_subprogram
))
11760 complaint (_("DIE %s and its abstract origin %s have different tags"),
11761 sect_offset_str (die
->sect_off
),
11762 sect_offset_str (origin_die
->sect_off
));
11764 /* Find if the concrete and abstract trees are structurally the
11765 same. This is a shallow traversal and it is not bullet-proof;
11766 the compiler can trick the debugger into believing that the trees
11767 are isomorphic, whereas they actually are not. However, the
11768 likelyhood of this happening is pretty low, and a full-fledged
11769 check would be an overkill. */
11770 bool are_isomorphic
= true;
11771 die_info
*concrete_child
= die
->child
;
11772 die_info
*abstract_child
= origin_die
->child
;
11773 while (concrete_child
!= nullptr || abstract_child
!= nullptr)
11775 if (concrete_child
== nullptr
11776 || abstract_child
== nullptr
11777 || concrete_child
->tag
!= abstract_child
->tag
)
11779 are_isomorphic
= false;
11783 concrete_child
= concrete_child
->sibling
;
11784 abstract_child
= abstract_child
->sibling
;
11787 /* Walk the origin's children in parallel to the concrete children.
11788 This helps match an origin child in case the debug info misses
11789 DW_AT_abstract_origin attributes. Keep in mind that the abstract
11790 origin tree may not have the same tree structure as the concrete
11792 die_info
*corresponding_abstract_child
11793 = are_isomorphic
? origin_die
->child
: nullptr;
11795 std::vector
<sect_offset
> offsets
;
11797 for (child_die
= die
->child
;
11798 child_die
&& child_die
->tag
;
11799 child_die
= child_die
->sibling
)
11801 struct die_info
*child_origin_die
;
11802 struct dwarf2_cu
*child_origin_cu
;
11804 /* We are trying to process concrete instance entries:
11805 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11806 it's not relevant to our analysis here. i.e. detecting DIEs that are
11807 present in the abstract instance but not referenced in the concrete
11809 if (child_die
->tag
== DW_TAG_call_site
11810 || child_die
->tag
== DW_TAG_GNU_call_site
)
11812 if (are_isomorphic
)
11813 corresponding_abstract_child
11814 = corresponding_abstract_child
->sibling
;
11818 /* For each CHILD_DIE, find the corresponding child of
11819 ORIGIN_DIE. If there is more than one layer of
11820 DW_AT_abstract_origin, follow them all; there shouldn't be,
11821 but GCC versions at least through 4.4 generate this (GCC PR
11823 child_origin_die
= child_die
;
11824 child_origin_cu
= cu
;
11827 attr
= dwarf2_attr (child_origin_die
, DW_AT_abstract_origin
,
11831 child_origin_die
= follow_die_ref (child_origin_die
, attr
,
11835 /* If missing DW_AT_abstract_origin, try the corresponding child
11836 of the origin. Clang emits such lexical scopes. */
11837 if (child_origin_die
== child_die
11838 && dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
) == nullptr
11840 && child_die
->tag
== DW_TAG_lexical_block
)
11841 child_origin_die
= corresponding_abstract_child
;
11843 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11844 counterpart may exist. */
11845 if (child_origin_die
!= child_die
)
11847 if (child_die
->tag
!= child_origin_die
->tag
11848 && !(child_die
->tag
== DW_TAG_inlined_subroutine
11849 && child_origin_die
->tag
== DW_TAG_subprogram
))
11850 complaint (_("Child DIE %s and its abstract origin %s have "
11852 sect_offset_str (child_die
->sect_off
),
11853 sect_offset_str (child_origin_die
->sect_off
));
11854 if (child_origin_die
->parent
!= origin_die
)
11855 complaint (_("Child DIE %s and its abstract origin %s have "
11856 "different parents"),
11857 sect_offset_str (child_die
->sect_off
),
11858 sect_offset_str (child_origin_die
->sect_off
));
11860 offsets
.push_back (child_origin_die
->sect_off
);
11863 if (are_isomorphic
)
11864 corresponding_abstract_child
= corresponding_abstract_child
->sibling
;
11866 std::sort (offsets
.begin (), offsets
.end ());
11867 sect_offset
*offsets_end
= offsets
.data () + offsets
.size ();
11868 for (offsetp
= offsets
.data () + 1; offsetp
< offsets_end
; offsetp
++)
11869 if (offsetp
[-1] == *offsetp
)
11870 complaint (_("Multiple children of DIE %s refer "
11871 "to DIE %s as their abstract origin"),
11872 sect_offset_str (die
->sect_off
), sect_offset_str (*offsetp
));
11874 offsetp
= offsets
.data ();
11875 origin_child_die
= origin_die
->child
;
11876 while (origin_child_die
&& origin_child_die
->tag
)
11878 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11879 while (offsetp
< offsets_end
11880 && *offsetp
< origin_child_die
->sect_off
)
11882 if (offsetp
>= offsets_end
11883 || *offsetp
> origin_child_die
->sect_off
)
11885 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11886 Check whether we're already processing ORIGIN_CHILD_DIE.
11887 This can happen with mutually referenced abstract_origins.
11889 if (!origin_child_die
->in_process
)
11890 process_die (origin_child_die
, origin_cu
);
11892 origin_child_die
= origin_child_die
->sibling
;
11894 origin_cu
->list_in_scope
= origin_previous_list_in_scope
;
11896 if (cu
!= origin_cu
)
11897 compute_delayed_physnames (origin_cu
);
11901 read_func_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
11903 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
11904 struct gdbarch
*gdbarch
= objfile
->arch ();
11905 struct context_stack
*newobj
;
11908 struct die_info
*child_die
;
11909 struct attribute
*attr
, *call_line
, *call_file
;
11911 CORE_ADDR baseaddr
;
11912 struct block
*block
;
11913 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
11914 std::vector
<struct symbol
*> template_args
;
11915 struct template_symbol
*templ_func
= NULL
;
11919 /* If we do not have call site information, we can't show the
11920 caller of this inlined function. That's too confusing, so
11921 only use the scope for local variables. */
11922 call_line
= dwarf2_attr (die
, DW_AT_call_line
, cu
);
11923 call_file
= dwarf2_attr (die
, DW_AT_call_file
, cu
);
11924 if (call_line
== NULL
|| call_file
== NULL
)
11926 read_lexical_block_scope (die
, cu
);
11931 baseaddr
= objfile
->text_section_offset ();
11933 name
= dwarf2_name (die
, cu
);
11935 /* Ignore functions with missing or empty names. These are actually
11936 illegal according to the DWARF standard. */
11939 complaint (_("missing name for subprogram DIE at %s"),
11940 sect_offset_str (die
->sect_off
));
11944 /* Ignore functions with missing or invalid low and high pc attributes. */
11945 if (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr)
11946 <= PC_BOUNDS_INVALID
)
11948 attr
= dwarf2_attr (die
, DW_AT_external
, cu
);
11949 if (attr
== nullptr || !attr
->as_boolean ())
11950 complaint (_("cannot get low and high bounds "
11951 "for subprogram DIE at %s"),
11952 sect_offset_str (die
->sect_off
));
11956 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
11957 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
11959 /* If we have any template arguments, then we must allocate a
11960 different sort of symbol. */
11961 for (child_die
= die
->child
; child_die
; child_die
= child_die
->sibling
)
11963 if (child_die
->tag
== DW_TAG_template_type_param
11964 || child_die
->tag
== DW_TAG_template_value_param
)
11966 templ_func
= new (&objfile
->objfile_obstack
) template_symbol
;
11967 templ_func
->subclass
= SYMBOL_TEMPLATE
;
11972 gdb_assert (cu
->get_builder () != nullptr);
11973 newobj
= cu
->get_builder ()->push_context (0, lowpc
);
11974 newobj
->name
= new_symbol (die
, read_type_die (die
, cu
), cu
,
11975 (struct symbol
*) templ_func
);
11977 if (dwarf2_flag_true_p (die
, DW_AT_main_subprogram
, cu
))
11978 set_objfile_main_name (objfile
, newobj
->name
->linkage_name (),
11981 /* If there is a location expression for DW_AT_frame_base, record
11983 attr
= dwarf2_attr (die
, DW_AT_frame_base
, cu
);
11984 if (attr
!= nullptr)
11985 dwarf2_symbol_mark_computed (attr
, newobj
->name
, cu
, 1);
11987 /* If there is a location for the static link, record it. */
11988 newobj
->static_link
= NULL
;
11989 attr
= dwarf2_attr (die
, DW_AT_static_link
, cu
);
11990 if (attr
!= nullptr)
11992 newobj
->static_link
11993 = XOBNEW (&objfile
->objfile_obstack
, struct dynamic_prop
);
11994 attr_to_dynamic_prop (attr
, die
, cu
, newobj
->static_link
,
11998 cu
->list_in_scope
= cu
->get_builder ()->get_local_symbols ();
12000 if (die
->child
!= NULL
)
12002 child_die
= die
->child
;
12003 while (child_die
&& child_die
->tag
)
12005 if (child_die
->tag
== DW_TAG_template_type_param
12006 || child_die
->tag
== DW_TAG_template_value_param
)
12008 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
12011 template_args
.push_back (arg
);
12014 process_die (child_die
, cu
);
12015 child_die
= child_die
->sibling
;
12019 inherit_abstract_dies (die
, cu
);
12021 /* If we have a DW_AT_specification, we might need to import using
12022 directives from the context of the specification DIE. See the
12023 comment in determine_prefix. */
12024 if (cu
->per_cu
->lang
== language_cplus
12025 && dwarf2_attr (die
, DW_AT_specification
, cu
))
12027 struct dwarf2_cu
*spec_cu
= cu
;
12028 struct die_info
*spec_die
= die_specification (die
, &spec_cu
);
12032 child_die
= spec_die
->child
;
12033 while (child_die
&& child_die
->tag
)
12035 if (child_die
->tag
== DW_TAG_imported_module
)
12036 process_die (child_die
, spec_cu
);
12037 child_die
= child_die
->sibling
;
12040 /* In some cases, GCC generates specification DIEs that
12041 themselves contain DW_AT_specification attributes. */
12042 spec_die
= die_specification (spec_die
, &spec_cu
);
12046 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12047 /* Make a block for the local symbols within. */
12048 block
= cu
->get_builder ()->finish_block (cstk
.name
, cstk
.old_blocks
,
12049 cstk
.static_link
, lowpc
, highpc
);
12051 /* For C++, set the block's scope. */
12052 if ((cu
->per_cu
->lang
== language_cplus
12053 || cu
->per_cu
->lang
== language_fortran
12054 || cu
->per_cu
->lang
== language_d
12055 || cu
->per_cu
->lang
== language_rust
)
12056 && cu
->processing_has_namespace_info
)
12057 block_set_scope (block
, determine_prefix (die
, cu
),
12058 &objfile
->objfile_obstack
);
12060 /* If we have address ranges, record them. */
12061 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12063 gdbarch_make_symbol_special (gdbarch
, cstk
.name
, objfile
);
12065 /* Attach template arguments to function. */
12066 if (!template_args
.empty ())
12068 gdb_assert (templ_func
!= NULL
);
12070 templ_func
->n_template_arguments
= template_args
.size ();
12071 templ_func
->template_arguments
12072 = XOBNEWVEC (&objfile
->objfile_obstack
, struct symbol
*,
12073 templ_func
->n_template_arguments
);
12074 memcpy (templ_func
->template_arguments
,
12075 template_args
.data (),
12076 (templ_func
->n_template_arguments
* sizeof (struct symbol
*)));
12078 /* Make sure that the symtab is set on the new symbols. Even
12079 though they don't appear in this symtab directly, other parts
12080 of gdb assume that symbols do, and this is reasonably
12082 for (symbol
*sym
: template_args
)
12083 sym
->set_symtab (templ_func
->symtab ());
12086 /* In C++, we can have functions nested inside functions (e.g., when
12087 a function declares a class that has methods). This means that
12088 when we finish processing a function scope, we may need to go
12089 back to building a containing block's symbol lists. */
12090 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12091 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12093 /* If we've finished processing a top-level function, subsequent
12094 symbols go in the file symbol list. */
12095 if (cu
->get_builder ()->outermost_context_p ())
12096 cu
->list_in_scope
= cu
->get_builder ()->get_file_symbols ();
12099 /* Process all the DIES contained within a lexical block scope. Start
12100 a new scope, process the dies, and then close the scope. */
12103 read_lexical_block_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12105 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12106 struct gdbarch
*gdbarch
= objfile
->arch ();
12107 CORE_ADDR lowpc
, highpc
;
12108 struct die_info
*child_die
;
12109 CORE_ADDR baseaddr
;
12111 baseaddr
= objfile
->text_section_offset ();
12113 /* Ignore blocks with missing or invalid low and high pc attributes. */
12114 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12115 as multiple lexical blocks? Handling children in a sane way would
12116 be nasty. Might be easier to properly extend generic blocks to
12117 describe ranges. */
12118 switch (dwarf2_get_pc_bounds (die
, &lowpc
, &highpc
, cu
, nullptr, nullptr))
12120 case PC_BOUNDS_NOT_PRESENT
:
12121 /* DW_TAG_lexical_block has no attributes, process its children as if
12122 there was no wrapping by that DW_TAG_lexical_block.
12123 GCC does no longer produces such DWARF since GCC r224161. */
12124 for (child_die
= die
->child
;
12125 child_die
!= NULL
&& child_die
->tag
;
12126 child_die
= child_die
->sibling
)
12128 /* We might already be processing this DIE. This can happen
12129 in an unusual circumstance -- where a subroutine A
12130 appears lexically in another subroutine B, but A actually
12131 inlines B. The recursion is broken here, rather than in
12132 inherit_abstract_dies, because it seems better to simply
12133 drop concrete children here. */
12134 if (!child_die
->in_process
)
12135 process_die (child_die
, cu
);
12138 case PC_BOUNDS_INVALID
:
12141 lowpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
);
12142 highpc
= gdbarch_adjust_dwarf2_addr (gdbarch
, highpc
+ baseaddr
);
12144 cu
->get_builder ()->push_context (0, lowpc
);
12145 if (die
->child
!= NULL
)
12147 child_die
= die
->child
;
12148 while (child_die
&& child_die
->tag
)
12150 process_die (child_die
, cu
);
12151 child_die
= child_die
->sibling
;
12154 inherit_abstract_dies (die
, cu
);
12155 struct context_stack cstk
= cu
->get_builder ()->pop_context ();
12157 if (*cu
->get_builder ()->get_local_symbols () != NULL
12158 || (*cu
->get_builder ()->get_local_using_directives ()) != NULL
)
12160 struct block
*block
12161 = cu
->get_builder ()->finish_block (0, cstk
.old_blocks
, NULL
,
12162 cstk
.start_addr
, highpc
);
12164 /* Note that recording ranges after traversing children, as we
12165 do here, means that recording a parent's ranges entails
12166 walking across all its children's ranges as they appear in
12167 the address map, which is quadratic behavior.
12169 It would be nicer to record the parent's ranges before
12170 traversing its children, simply overriding whatever you find
12171 there. But since we don't even decide whether to create a
12172 block until after we've traversed its children, that's hard
12174 dwarf2_record_block_ranges (die
, block
, baseaddr
, cu
);
12176 *cu
->get_builder ()->get_local_symbols () = cstk
.locals
;
12177 cu
->get_builder ()->set_local_using_directives (cstk
.local_using_directives
);
12180 static void dwarf2_ranges_read_low_addrs (unsigned offset
,
12181 struct dwarf2_cu
*cu
,
12183 std::vector
<CORE_ADDR
> &result
);
12185 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12188 read_call_site_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
12190 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12191 struct objfile
*objfile
= per_objfile
->objfile
;
12192 struct gdbarch
*gdbarch
= objfile
->arch ();
12193 CORE_ADDR pc
, baseaddr
;
12194 struct attribute
*attr
;
12197 struct die_info
*child_die
;
12199 baseaddr
= objfile
->text_section_offset ();
12201 attr
= dwarf2_attr (die
, DW_AT_call_return_pc
, cu
);
12204 /* This was a pre-DWARF-5 GNU extension alias
12205 for DW_AT_call_return_pc. */
12206 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12210 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
12211 "DIE %s [in module %s]"),
12212 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12215 pc
= attr
->as_address () + baseaddr
;
12216 pc
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc
);
12219 if (cu
->call_site_htab
== NULL
)
12220 cu
->call_site_htab
= htab_create_alloc_ex (16, call_site::hash
,
12221 call_site::eq
, NULL
,
12222 &objfile
->objfile_obstack
,
12223 hashtab_obstack_allocate
, NULL
);
12224 struct call_site
call_site_local (pc
, nullptr, nullptr);
12225 slot
= htab_find_slot (cu
->call_site_htab
, &call_site_local
, INSERT
);
12228 complaint (_("Duplicate PC %s for DW_TAG_call_site "
12229 "DIE %s [in module %s]"),
12230 paddress (gdbarch
, pc
), sect_offset_str (die
->sect_off
),
12231 objfile_name (objfile
));
12235 /* Count parameters at the caller. */
12238 for (child_die
= die
->child
; child_die
&& child_die
->tag
;
12239 child_die
= child_die
->sibling
)
12241 if (child_die
->tag
!= DW_TAG_call_site_parameter
12242 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12244 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
12245 "DW_TAG_call_site child DIE %s [in module %s]"),
12246 child_die
->tag
, sect_offset_str (child_die
->sect_off
),
12247 objfile_name (objfile
));
12254 struct call_site
*call_site
12255 = new (XOBNEWVAR (&objfile
->objfile_obstack
,
12257 sizeof (*call_site
) + sizeof (call_site
->parameter
[0]) * nparams
))
12258 struct call_site (pc
, cu
->per_cu
, per_objfile
);
12261 /* We never call the destructor of call_site, so we must ensure it is
12262 trivially destructible. */
12263 gdb_static_assert(std::is_trivially_destructible
<struct call_site
>::value
);
12265 if (dwarf2_flag_true_p (die
, DW_AT_call_tail_call
, cu
)
12266 || dwarf2_flag_true_p (die
, DW_AT_GNU_tail_call
, cu
))
12268 struct die_info
*func_die
;
12270 /* Skip also over DW_TAG_inlined_subroutine. */
12271 for (func_die
= die
->parent
;
12272 func_die
&& func_die
->tag
!= DW_TAG_subprogram
12273 && func_die
->tag
!= DW_TAG_subroutine_type
;
12274 func_die
= func_die
->parent
);
12276 /* DW_AT_call_all_calls is a superset
12277 of DW_AT_call_all_tail_calls. */
12279 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_calls
, cu
)
12280 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_call_sites
, cu
)
12281 && !dwarf2_flag_true_p (func_die
, DW_AT_call_all_tail_calls
, cu
)
12282 && !dwarf2_flag_true_p (func_die
, DW_AT_GNU_all_tail_call_sites
, cu
))
12284 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
12285 not complete. But keep CALL_SITE for look ups via call_site_htab,
12286 both the initial caller containing the real return address PC and
12287 the final callee containing the current PC of a chain of tail
12288 calls do not need to have the tail call list complete. But any
12289 function candidate for a virtual tail call frame searched via
12290 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
12291 determined unambiguously. */
12295 struct type
*func_type
= NULL
;
12298 func_type
= get_die_type (func_die
, cu
);
12299 if (func_type
!= NULL
)
12301 gdb_assert (func_type
->code () == TYPE_CODE_FUNC
);
12303 /* Enlist this call site to the function. */
12304 call_site
->tail_call_next
= TYPE_TAIL_CALL_LIST (func_type
);
12305 TYPE_TAIL_CALL_LIST (func_type
) = call_site
;
12308 complaint (_("Cannot find function owning DW_TAG_call_site "
12309 "DIE %s [in module %s]"),
12310 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12314 attr
= dwarf2_attr (die
, DW_AT_call_target
, cu
);
12316 attr
= dwarf2_attr (die
, DW_AT_GNU_call_site_target
, cu
);
12318 attr
= dwarf2_attr (die
, DW_AT_call_origin
, cu
);
12321 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
12322 attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12325 call_site
->target
.set_loc_dwarf_block (nullptr);
12326 if (!attr
|| (attr
->form_is_block () && attr
->as_block ()->size
== 0))
12327 /* Keep NULL DWARF_BLOCK. */;
12328 else if (attr
->form_is_block ())
12330 struct dwarf2_locexpr_baton
*dlbaton
;
12331 struct dwarf_block
*block
= attr
->as_block ();
12333 dlbaton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
12334 dlbaton
->data
= block
->data
;
12335 dlbaton
->size
= block
->size
;
12336 dlbaton
->per_objfile
= per_objfile
;
12337 dlbaton
->per_cu
= cu
->per_cu
;
12339 call_site
->target
.set_loc_dwarf_block (dlbaton
);
12341 else if (attr
->form_is_ref ())
12343 struct dwarf2_cu
*target_cu
= cu
;
12344 struct die_info
*target_die
;
12346 target_die
= follow_die_ref (die
, attr
, &target_cu
);
12347 gdb_assert (target_cu
->per_objfile
->objfile
== objfile
);
12349 struct attribute
*ranges_attr
12350 = dwarf2_attr (target_die
, DW_AT_ranges
, target_cu
);
12352 if (die_is_declaration (target_die
, target_cu
))
12354 const char *target_physname
;
12356 /* Prefer the mangled name; otherwise compute the demangled one. */
12357 target_physname
= dw2_linkage_name (target_die
, target_cu
);
12358 if (target_physname
== NULL
)
12359 target_physname
= dwarf2_physname (NULL
, target_die
, target_cu
);
12360 if (target_physname
== NULL
)
12361 complaint (_("DW_AT_call_target target DIE has invalid "
12362 "physname, for referencing DIE %s [in module %s]"),
12363 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12365 call_site
->target
.set_loc_physname (target_physname
);
12367 else if (ranges_attr
!= nullptr && ranges_attr
->form_is_unsigned ())
12369 ULONGEST ranges_offset
= (ranges_attr
->as_unsigned ()
12370 + target_cu
->gnu_ranges_base
);
12371 std::vector
<CORE_ADDR
> addresses
;
12372 dwarf2_ranges_read_low_addrs (ranges_offset
, target_cu
,
12373 target_die
->tag
, addresses
);
12374 CORE_ADDR
*saved
= XOBNEWVAR (&objfile
->objfile_obstack
, CORE_ADDR
,
12375 addresses
.size ());
12376 std::copy (addresses
.begin (), addresses
.end (), saved
);
12377 call_site
->target
.set_loc_array (addresses
.size (), saved
);
12383 /* DW_AT_entry_pc should be preferred. */
12384 if (dwarf2_get_pc_bounds (target_die
, &lowpc
, NULL
, target_cu
,
12386 <= PC_BOUNDS_INVALID
)
12387 complaint (_("DW_AT_call_target target DIE has invalid "
12388 "low pc, for referencing DIE %s [in module %s]"),
12389 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12392 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
, lowpc
+ baseaddr
)
12394 call_site
->target
.set_loc_physaddr (lowpc
);
12399 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
12400 "block nor reference, for DIE %s [in module %s]"),
12401 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
12403 for (child_die
= die
->child
;
12404 child_die
&& child_die
->tag
;
12405 child_die
= child_die
->sibling
)
12407 struct call_site_parameter
*parameter
;
12408 struct attribute
*loc
, *origin
;
12410 if (child_die
->tag
!= DW_TAG_call_site_parameter
12411 && child_die
->tag
!= DW_TAG_GNU_call_site_parameter
)
12413 /* Already printed the complaint above. */
12417 gdb_assert (call_site
->parameter_count
< nparams
);
12418 parameter
= &call_site
->parameter
[call_site
->parameter_count
];
12420 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
12421 specifies DW_TAG_formal_parameter. Value of the data assumed for the
12422 register is contained in DW_AT_call_value. */
12424 loc
= dwarf2_attr (child_die
, DW_AT_location
, cu
);
12425 origin
= dwarf2_attr (child_die
, DW_AT_call_parameter
, cu
);
12426 if (origin
== NULL
)
12428 /* This was a pre-DWARF-5 GNU extension alias
12429 for DW_AT_call_parameter. */
12430 origin
= dwarf2_attr (child_die
, DW_AT_abstract_origin
, cu
);
12432 if (loc
== NULL
&& origin
!= NULL
&& origin
->form_is_ref ())
12434 parameter
->kind
= CALL_SITE_PARAMETER_PARAM_OFFSET
;
12436 sect_offset sect_off
= origin
->get_ref_die_offset ();
12437 if (!cu
->header
.offset_in_cu_p (sect_off
))
12439 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
12440 binding can be done only inside one CU. Such referenced DIE
12441 therefore cannot be even moved to DW_TAG_partial_unit. */
12442 complaint (_("DW_AT_call_parameter offset is not in CU for "
12443 "DW_TAG_call_site child DIE %s [in module %s]"),
12444 sect_offset_str (child_die
->sect_off
),
12445 objfile_name (objfile
));
12448 parameter
->u
.param_cu_off
12449 = (cu_offset
) (sect_off
- cu
->header
.sect_off
);
12451 else if (loc
== NULL
|| origin
!= NULL
|| !loc
->form_is_block ())
12453 complaint (_("No DW_FORM_block* DW_AT_location for "
12454 "DW_TAG_call_site child DIE %s [in module %s]"),
12455 sect_offset_str (child_die
->sect_off
), objfile_name (objfile
));
12460 struct dwarf_block
*block
= loc
->as_block ();
12462 parameter
->u
.dwarf_reg
= dwarf_block_to_dwarf_reg
12463 (block
->data
, &block
->data
[block
->size
]);
12464 if (parameter
->u
.dwarf_reg
!= -1)
12465 parameter
->kind
= CALL_SITE_PARAMETER_DWARF_REG
;
12466 else if (dwarf_block_to_sp_offset (gdbarch
, block
->data
,
12467 &block
->data
[block
->size
],
12468 ¶meter
->u
.fb_offset
))
12469 parameter
->kind
= CALL_SITE_PARAMETER_FB_OFFSET
;
12472 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
12473 "for DW_FORM_block* DW_AT_location is supported for "
12474 "DW_TAG_call_site child DIE %s "
12476 sect_offset_str (child_die
->sect_off
),
12477 objfile_name (objfile
));
12482 attr
= dwarf2_attr (child_die
, DW_AT_call_value
, cu
);
12484 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_value
, cu
);
12485 if (attr
== NULL
|| !attr
->form_is_block ())
12487 complaint (_("No DW_FORM_block* DW_AT_call_value for "
12488 "DW_TAG_call_site child DIE %s [in module %s]"),
12489 sect_offset_str (child_die
->sect_off
),
12490 objfile_name (objfile
));
12494 struct dwarf_block
*block
= attr
->as_block ();
12495 parameter
->value
= block
->data
;
12496 parameter
->value_size
= block
->size
;
12498 /* Parameters are not pre-cleared by memset above. */
12499 parameter
->data_value
= NULL
;
12500 parameter
->data_value_size
= 0;
12501 call_site
->parameter_count
++;
12503 attr
= dwarf2_attr (child_die
, DW_AT_call_data_value
, cu
);
12505 attr
= dwarf2_attr (child_die
, DW_AT_GNU_call_site_data_value
, cu
);
12506 if (attr
!= nullptr)
12508 if (!attr
->form_is_block ())
12509 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
12510 "DW_TAG_call_site child DIE %s [in module %s]"),
12511 sect_offset_str (child_die
->sect_off
),
12512 objfile_name (objfile
));
12515 block
= attr
->as_block ();
12516 parameter
->data_value
= block
->data
;
12517 parameter
->data_value_size
= block
->size
;
12523 /* Helper function for read_variable. If DIE represents a virtual
12524 table, then return the type of the concrete object that is
12525 associated with the virtual table. Otherwise, return NULL. */
12527 static struct type
*
12528 rust_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
12530 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
12534 /* Find the type DIE. */
12535 struct die_info
*type_die
= NULL
;
12536 struct dwarf2_cu
*type_cu
= cu
;
12538 if (attr
->form_is_ref ())
12539 type_die
= follow_die_ref (die
, attr
, &type_cu
);
12540 if (type_die
== NULL
)
12543 if (dwarf2_attr (type_die
, DW_AT_containing_type
, type_cu
) == NULL
)
12545 return die_containing_type (type_die
, type_cu
);
12548 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
12551 read_variable (struct die_info
*die
, struct dwarf2_cu
*cu
)
12553 struct rust_vtable_symbol
*storage
= NULL
;
12555 if (cu
->per_cu
->lang
== language_rust
)
12557 struct type
*containing_type
= rust_containing_type (die
, cu
);
12559 if (containing_type
!= NULL
)
12561 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12563 storage
= new (&objfile
->objfile_obstack
) rust_vtable_symbol
;
12564 storage
->concrete_type
= containing_type
;
12565 storage
->subclass
= SYMBOL_RUST_VTABLE
;
12569 struct symbol
*res
= new_symbol (die
, NULL
, cu
, storage
);
12570 struct attribute
*abstract_origin
12571 = dwarf2_attr (die
, DW_AT_abstract_origin
, cu
);
12572 struct attribute
*loc
= dwarf2_attr (die
, DW_AT_location
, cu
);
12573 if (res
== NULL
&& loc
&& abstract_origin
)
12575 /* We have a variable without a name, but with a location and an abstract
12576 origin. This may be a concrete instance of an abstract variable
12577 referenced from an DW_OP_GNU_variable_value, so save it to find it back
12579 struct dwarf2_cu
*origin_cu
= cu
;
12580 struct die_info
*origin_die
12581 = follow_die_ref (die
, abstract_origin
, &origin_cu
);
12582 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12583 per_objfile
->per_bfd
->abstract_to_concrete
12584 [origin_die
->sect_off
].push_back (die
->sect_off
);
12588 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12589 reading .debug_rnglists.
12590 Callback's type should be:
12591 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12592 Return true if the attributes are present and valid, otherwise,
12595 template <typename Callback
>
12597 dwarf2_rnglists_process (unsigned offset
, struct dwarf2_cu
*cu
,
12598 dwarf_tag tag
, Callback
&&callback
)
12600 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12601 struct objfile
*objfile
= per_objfile
->objfile
;
12602 bfd
*obfd
= objfile
->obfd
;
12603 /* Base address selection entry. */
12604 gdb::optional
<CORE_ADDR
> base
;
12605 const gdb_byte
*buffer
;
12606 bool overflow
= false;
12607 ULONGEST addr_index
;
12608 struct dwarf2_section_info
*rnglists_section
;
12610 base
= cu
->base_address
;
12611 rnglists_section
= cu_debug_rnglists_section (cu
, tag
);
12612 rnglists_section
->read (objfile
);
12614 if (offset
>= rnglists_section
->size
)
12616 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12620 buffer
= rnglists_section
->buffer
+ offset
;
12624 /* Initialize it due to a false compiler warning. */
12625 CORE_ADDR range_beginning
= 0, range_end
= 0;
12626 const gdb_byte
*buf_end
= (rnglists_section
->buffer
12627 + rnglists_section
->size
);
12628 unsigned int bytes_read
;
12630 if (buffer
== buf_end
)
12635 const auto rlet
= static_cast<enum dwarf_range_list_entry
>(*buffer
++);
12638 case DW_RLE_end_of_list
:
12640 case DW_RLE_base_address
:
12641 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12646 base
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12647 buffer
+= bytes_read
;
12649 case DW_RLE_base_addressx
:
12650 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12651 buffer
+= bytes_read
;
12652 base
= read_addr_index (cu
, addr_index
);
12654 case DW_RLE_start_length
:
12655 if (buffer
+ cu
->header
.addr_size
> buf_end
)
12660 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12662 buffer
+= bytes_read
;
12663 range_end
= (range_beginning
12664 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12665 buffer
+= bytes_read
;
12666 if (buffer
> buf_end
)
12672 case DW_RLE_startx_length
:
12673 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12674 buffer
+= bytes_read
;
12675 range_beginning
= read_addr_index (cu
, addr_index
);
12676 if (buffer
> buf_end
)
12681 range_end
= (range_beginning
12682 + read_unsigned_leb128 (obfd
, buffer
, &bytes_read
));
12683 buffer
+= bytes_read
;
12685 case DW_RLE_offset_pair
:
12686 range_beginning
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12687 buffer
+= bytes_read
;
12688 if (buffer
> buf_end
)
12693 range_end
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12694 buffer
+= bytes_read
;
12695 if (buffer
> buf_end
)
12701 case DW_RLE_start_end
:
12702 if (buffer
+ 2 * cu
->header
.addr_size
> buf_end
)
12707 range_beginning
= cu
->header
.read_address (obfd
, buffer
,
12709 buffer
+= bytes_read
;
12710 range_end
= cu
->header
.read_address (obfd
, buffer
, &bytes_read
);
12711 buffer
+= bytes_read
;
12713 case DW_RLE_startx_endx
:
12714 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12715 buffer
+= bytes_read
;
12716 range_beginning
= read_addr_index (cu
, addr_index
);
12717 if (buffer
> buf_end
)
12722 addr_index
= read_unsigned_leb128 (obfd
, buffer
, &bytes_read
);
12723 buffer
+= bytes_read
;
12724 range_end
= read_addr_index (cu
, addr_index
);
12727 complaint (_("Invalid .debug_rnglists data (no base address)"));
12730 if (rlet
== DW_RLE_end_of_list
|| overflow
)
12732 if (rlet
== DW_RLE_base_address
)
12735 if (range_beginning
> range_end
)
12737 /* Inverted range entries are invalid. */
12738 complaint (_("Invalid .debug_rnglists data (inverted range)"));
12742 /* Empty range entries have no effect. */
12743 if (range_beginning
== range_end
)
12746 /* Only DW_RLE_offset_pair needs the base address added. */
12747 if (rlet
== DW_RLE_offset_pair
)
12749 if (!base
.has_value ())
12751 /* We have no valid base address for the DW_RLE_offset_pair. */
12752 complaint (_("Invalid .debug_rnglists data (no base address for "
12753 "DW_RLE_offset_pair)"));
12757 range_beginning
+= *base
;
12758 range_end
+= *base
;
12761 /* A not-uncommon case of bad debug info.
12762 Don't pollute the addrmap with bad data. */
12763 if (range_beginning
== 0
12764 && !per_objfile
->per_bfd
->has_section_at_zero
)
12766 complaint (_(".debug_rnglists entry has start address of zero"
12767 " [in module %s]"), objfile_name (objfile
));
12771 callback (range_beginning
, range_end
);
12776 complaint (_("Offset %d is not terminated "
12777 "for DW_AT_ranges attribute"),
12785 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12786 Callback's type should be:
12787 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12788 Return 1 if the attributes are present and valid, otherwise, return 0. */
12790 template <typename Callback
>
12792 dwarf2_ranges_process (unsigned offset
, struct dwarf2_cu
*cu
, dwarf_tag tag
,
12793 Callback
&&callback
)
12795 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12796 struct objfile
*objfile
= per_objfile
->objfile
;
12797 struct comp_unit_head
*cu_header
= &cu
->header
;
12798 bfd
*obfd
= objfile
->obfd
;
12799 unsigned int addr_size
= cu_header
->addr_size
;
12800 CORE_ADDR mask
= ~(~(CORE_ADDR
)1 << (addr_size
* 8 - 1));
12801 /* Base address selection entry. */
12802 gdb::optional
<CORE_ADDR
> base
;
12803 unsigned int dummy
;
12804 const gdb_byte
*buffer
;
12806 if (cu_header
->version
>= 5)
12807 return dwarf2_rnglists_process (offset
, cu
, tag
, callback
);
12809 base
= cu
->base_address
;
12811 per_objfile
->per_bfd
->ranges
.read (objfile
);
12812 if (offset
>= per_objfile
->per_bfd
->ranges
.size
)
12814 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
12818 buffer
= per_objfile
->per_bfd
->ranges
.buffer
+ offset
;
12822 CORE_ADDR range_beginning
, range_end
;
12824 range_beginning
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12825 buffer
+= addr_size
;
12826 range_end
= cu
->header
.read_address (obfd
, buffer
, &dummy
);
12827 buffer
+= addr_size
;
12828 offset
+= 2 * addr_size
;
12830 /* An end of list marker is a pair of zero addresses. */
12831 if (range_beginning
== 0 && range_end
== 0)
12832 /* Found the end of list entry. */
12835 /* Each base address selection entry is a pair of 2 values.
12836 The first is the largest possible address, the second is
12837 the base address. Check for a base address here. */
12838 if ((range_beginning
& mask
) == mask
)
12840 /* If we found the largest possible address, then we already
12841 have the base address in range_end. */
12846 if (!base
.has_value ())
12848 /* We have no valid base address for the ranges
12850 complaint (_("Invalid .debug_ranges data (no base address)"));
12854 if (range_beginning
> range_end
)
12856 /* Inverted range entries are invalid. */
12857 complaint (_("Invalid .debug_ranges data (inverted range)"));
12861 /* Empty range entries have no effect. */
12862 if (range_beginning
== range_end
)
12865 range_beginning
+= *base
;
12866 range_end
+= *base
;
12868 /* A not-uncommon case of bad debug info.
12869 Don't pollute the addrmap with bad data. */
12870 if (range_beginning
== 0
12871 && !per_objfile
->per_bfd
->has_section_at_zero
)
12873 complaint (_(".debug_ranges entry has start address of zero"
12874 " [in module %s]"), objfile_name (objfile
));
12878 callback (range_beginning
, range_end
);
12884 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12885 Return 1 if the attributes are present and valid, otherwise, return 0.
12886 TAG is passed to dwarf2_ranges_process. If MAP is not NULL, then
12887 ranges in MAP are set, using DATUM as the value. */
12890 dwarf2_ranges_read (unsigned offset
, CORE_ADDR
*low_return
,
12891 CORE_ADDR
*high_return
, struct dwarf2_cu
*cu
,
12892 addrmap
*map
, void *datum
, dwarf_tag tag
)
12894 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
12895 struct gdbarch
*gdbarch
= objfile
->arch ();
12896 const CORE_ADDR baseaddr
= objfile
->text_section_offset ();
12899 CORE_ADDR high
= 0;
12902 retval
= dwarf2_ranges_process (offset
, cu
, tag
,
12903 [&] (CORE_ADDR range_beginning
, CORE_ADDR range_end
)
12905 if (map
!= nullptr)
12910 lowpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12911 range_beginning
+ baseaddr
)
12913 highpc
= (gdbarch_adjust_dwarf2_addr (gdbarch
,
12914 range_end
+ baseaddr
)
12916 addrmap_set_empty (map
, lowpc
, highpc
- 1, datum
);
12919 /* FIXME: This is recording everything as a low-high
12920 segment of consecutive addresses. We should have a
12921 data structure for discontiguous block ranges
12925 low
= range_beginning
;
12931 if (range_beginning
< low
)
12932 low
= range_beginning
;
12933 if (range_end
> high
)
12941 /* If the first entry is an end-of-list marker, the range
12942 describes an empty scope, i.e. no instructions. */
12948 *high_return
= high
;
12952 /* Process ranges and fill in a vector of the low PC values only. */
12955 dwarf2_ranges_read_low_addrs (unsigned offset
, struct dwarf2_cu
*cu
,
12957 std::vector
<CORE_ADDR
> &result
)
12959 dwarf2_ranges_process (offset
, cu
, tag
,
12960 [&] (CORE_ADDR start
, CORE_ADDR end
)
12962 result
.push_back (start
);
12966 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12967 definition for the return value. *LOWPC and *HIGHPC are set iff
12968 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12970 static enum pc_bounds_kind
12971 dwarf2_get_pc_bounds (struct die_info
*die
, CORE_ADDR
*lowpc
,
12972 CORE_ADDR
*highpc
, struct dwarf2_cu
*cu
,
12973 addrmap
*map
, void *datum
)
12975 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
12976 struct attribute
*attr
;
12977 struct attribute
*attr_high
;
12979 CORE_ADDR high
= 0;
12980 enum pc_bounds_kind ret
;
12982 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
12985 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
12986 if (attr
!= nullptr)
12988 low
= attr
->as_address ();
12989 high
= attr_high
->as_address ();
12990 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
12994 /* Found high w/o low attribute. */
12995 return PC_BOUNDS_INVALID
;
12997 /* Found consecutive range of addresses. */
12998 ret
= PC_BOUNDS_HIGH_LOW
;
13002 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13003 if (attr
!= nullptr && attr
->form_is_unsigned ())
13005 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13006 on DWARF version). */
13007 ULONGEST ranges_offset
= attr
->as_unsigned ();
13009 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13011 if (die
->tag
!= DW_TAG_compile_unit
)
13012 ranges_offset
+= cu
->gnu_ranges_base
;
13014 /* Value of the DW_AT_ranges attribute is the offset in the
13015 .debug_ranges section. */
13016 if (!dwarf2_ranges_read (ranges_offset
, &low
, &high
, cu
,
13017 map
, datum
, die
->tag
))
13018 return PC_BOUNDS_INVALID
;
13019 /* Found discontinuous range of addresses. */
13020 ret
= PC_BOUNDS_RANGES
;
13023 return PC_BOUNDS_NOT_PRESENT
;
13026 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13028 return PC_BOUNDS_INVALID
;
13030 /* When using the GNU linker, .gnu.linkonce. sections are used to
13031 eliminate duplicate copies of functions and vtables and such.
13032 The linker will arbitrarily choose one and discard the others.
13033 The AT_*_pc values for such functions refer to local labels in
13034 these sections. If the section from that file was discarded, the
13035 labels are not in the output, so the relocs get a value of 0.
13036 If this is a discarded function, mark the pc bounds as invalid,
13037 so that GDB will ignore it. */
13038 if (low
== 0 && !per_objfile
->per_bfd
->has_section_at_zero
)
13039 return PC_BOUNDS_INVALID
;
13047 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13048 its low and high PC addresses. Do nothing if these addresses could not
13049 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13050 and HIGHPC to the high address if greater than HIGHPC. */
13053 dwarf2_get_subprogram_pc_bounds (struct die_info
*die
,
13054 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13055 struct dwarf2_cu
*cu
)
13057 CORE_ADDR low
, high
;
13058 struct die_info
*child
= die
->child
;
13060 if (dwarf2_get_pc_bounds (die
, &low
, &high
, cu
, nullptr, nullptr)
13061 >= PC_BOUNDS_RANGES
)
13063 *lowpc
= std::min (*lowpc
, low
);
13064 *highpc
= std::max (*highpc
, high
);
13067 /* If the language does not allow nested subprograms (either inside
13068 subprograms or lexical blocks), we're done. */
13069 if (cu
->per_cu
->lang
!= language_ada
)
13072 /* Check all the children of the given DIE. If it contains nested
13073 subprograms, then check their pc bounds. Likewise, we need to
13074 check lexical blocks as well, as they may also contain subprogram
13076 while (child
&& child
->tag
)
13078 if (child
->tag
== DW_TAG_subprogram
13079 || child
->tag
== DW_TAG_lexical_block
)
13080 dwarf2_get_subprogram_pc_bounds (child
, lowpc
, highpc
, cu
);
13081 child
= child
->sibling
;
13085 /* Get the low and high pc's represented by the scope DIE, and store
13086 them in *LOWPC and *HIGHPC. If the correct values can't be
13087 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13090 get_scope_pc_bounds (struct die_info
*die
,
13091 CORE_ADDR
*lowpc
, CORE_ADDR
*highpc
,
13092 struct dwarf2_cu
*cu
)
13094 CORE_ADDR best_low
= (CORE_ADDR
) -1;
13095 CORE_ADDR best_high
= (CORE_ADDR
) 0;
13096 CORE_ADDR current_low
, current_high
;
13098 if (dwarf2_get_pc_bounds (die
, ¤t_low
, ¤t_high
, cu
,
13100 >= PC_BOUNDS_RANGES
)
13102 best_low
= current_low
;
13103 best_high
= current_high
;
13107 struct die_info
*child
= die
->child
;
13109 while (child
&& child
->tag
)
13111 switch (child
->tag
) {
13112 case DW_TAG_subprogram
:
13113 dwarf2_get_subprogram_pc_bounds (child
, &best_low
, &best_high
, cu
);
13115 case DW_TAG_namespace
:
13116 case DW_TAG_module
:
13117 /* FIXME: carlton/2004-01-16: Should we do this for
13118 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13119 that current GCC's always emit the DIEs corresponding
13120 to definitions of methods of classes as children of a
13121 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13122 the DIEs giving the declarations, which could be
13123 anywhere). But I don't see any reason why the
13124 standards says that they have to be there. */
13125 get_scope_pc_bounds (child
, ¤t_low
, ¤t_high
, cu
);
13127 if (current_low
!= ((CORE_ADDR
) -1))
13129 best_low
= std::min (best_low
, current_low
);
13130 best_high
= std::max (best_high
, current_high
);
13138 child
= child
->sibling
;
13143 *highpc
= best_high
;
13146 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13150 dwarf2_record_block_ranges (struct die_info
*die
, struct block
*block
,
13151 CORE_ADDR baseaddr
, struct dwarf2_cu
*cu
)
13153 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13154 struct gdbarch
*gdbarch
= objfile
->arch ();
13155 struct attribute
*attr
;
13156 struct attribute
*attr_high
;
13158 attr_high
= dwarf2_attr (die
, DW_AT_high_pc
, cu
);
13161 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
13162 if (attr
!= nullptr)
13164 CORE_ADDR low
= attr
->as_address ();
13165 CORE_ADDR high
= attr_high
->as_address ();
13167 if (cu
->header
.version
>= 4 && attr_high
->form_is_constant ())
13170 low
= gdbarch_adjust_dwarf2_addr (gdbarch
, low
+ baseaddr
);
13171 high
= gdbarch_adjust_dwarf2_addr (gdbarch
, high
+ baseaddr
);
13172 cu
->get_builder ()->record_block_range (block
, low
, high
- 1);
13176 attr
= dwarf2_attr (die
, DW_AT_ranges
, cu
);
13177 if (attr
!= nullptr && attr
->form_is_unsigned ())
13179 /* Offset in the .debug_ranges or .debug_rnglist section (depending
13180 on DWARF version). */
13181 ULONGEST ranges_offset
= attr
->as_unsigned ();
13183 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
13185 if (die
->tag
!= DW_TAG_compile_unit
)
13186 ranges_offset
+= cu
->gnu_ranges_base
;
13188 std::vector
<blockrange
> blockvec
;
13189 dwarf2_ranges_process (ranges_offset
, cu
, die
->tag
,
13190 [&] (CORE_ADDR start
, CORE_ADDR end
)
13194 start
= gdbarch_adjust_dwarf2_addr (gdbarch
, start
);
13195 end
= gdbarch_adjust_dwarf2_addr (gdbarch
, end
);
13196 cu
->get_builder ()->record_block_range (block
, start
, end
- 1);
13197 blockvec
.emplace_back (start
, end
);
13200 BLOCK_RANGES(block
) = make_blockranges (objfile
, blockvec
);
13204 /* Check whether the producer field indicates either of GCC < 4.6, or the
13205 Intel C/C++ compiler, and cache the result in CU. */
13208 check_producer (struct dwarf2_cu
*cu
)
13212 if (cu
->producer
== NULL
)
13214 /* For unknown compilers expect their behavior is DWARF version
13217 GCC started to support .debug_types sections by -gdwarf-4 since
13218 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13219 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13220 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13221 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13223 else if (producer_is_gcc (cu
->producer
, &major
, &minor
))
13225 cu
->producer_is_gxx_lt_4_6
= major
< 4 || (major
== 4 && minor
< 6);
13226 cu
->producer_is_gcc_lt_4_3
= major
< 4 || (major
== 4 && minor
< 3);
13227 cu
->producer_is_gcc_11
= major
== 11;
13229 else if (producer_is_icc (cu
->producer
, &major
, &minor
))
13231 cu
->producer_is_icc
= true;
13232 cu
->producer_is_icc_lt_14
= major
< 14;
13234 else if (startswith (cu
->producer
, "CodeWarrior S12/L-ISA"))
13235 cu
->producer_is_codewarrior
= true;
13238 /* For other non-GCC compilers, expect their behavior is DWARF version
13242 cu
->checked_producer
= true;
13245 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13246 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13247 during 4.6.0 experimental. */
13250 producer_is_gxx_lt_4_6 (struct dwarf2_cu
*cu
)
13252 if (!cu
->checked_producer
)
13253 check_producer (cu
);
13255 return cu
->producer_is_gxx_lt_4_6
;
13259 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13260 with incorrect is_stmt attributes. */
13263 producer_is_codewarrior (struct dwarf2_cu
*cu
)
13265 if (!cu
->checked_producer
)
13266 check_producer (cu
);
13268 return cu
->producer_is_codewarrior
;
13271 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
13272 If that attribute is not available, return the appropriate
13275 static enum dwarf_access_attribute
13276 dwarf2_access_attribute (struct die_info
*die
, struct dwarf2_cu
*cu
)
13278 attribute
*attr
= dwarf2_attr (die
, DW_AT_accessibility
, cu
);
13279 if (attr
!= nullptr)
13281 LONGEST value
= attr
->constant_value (-1);
13282 if (value
== DW_ACCESS_public
13283 || value
== DW_ACCESS_protected
13284 || value
== DW_ACCESS_private
)
13285 return (dwarf_access_attribute
) value
;
13286 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
13290 if (cu
->header
.version
< 3 || producer_is_gxx_lt_4_6 (cu
))
13292 /* The default DWARF 2 accessibility for members is public, the default
13293 accessibility for inheritance is private. */
13295 if (die
->tag
!= DW_TAG_inheritance
)
13296 return DW_ACCESS_public
;
13298 return DW_ACCESS_private
;
13302 /* DWARF 3+ defines the default accessibility a different way. The same
13303 rules apply now for DW_TAG_inheritance as for the members and it only
13304 depends on the container kind. */
13306 if (die
->parent
->tag
== DW_TAG_class_type
)
13307 return DW_ACCESS_private
;
13309 return DW_ACCESS_public
;
13313 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset. Set
13314 *OFFSET to the byte offset. If the attribute was not found return
13315 0, otherwise return 1. If it was found but could not properly be
13316 handled, set *OFFSET to 0. */
13319 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13322 struct attribute
*attr
;
13324 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13329 /* Note that we do not check for a section offset first here.
13330 This is because DW_AT_data_member_location is new in DWARF 4,
13331 so if we see it, we can assume that a constant form is really
13332 a constant and not a section offset. */
13333 if (attr
->form_is_constant ())
13334 *offset
= attr
->constant_value (0);
13335 else if (attr
->form_is_section_offset ())
13336 dwarf2_complex_location_expr_complaint ();
13337 else if (attr
->form_is_block ())
13338 *offset
= decode_locdesc (attr
->as_block (), cu
);
13340 dwarf2_complex_location_expr_complaint ();
13346 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13347 if (attr
!= nullptr)
13349 *offset
= attr
->constant_value (0);
13357 /* Look for DW_AT_data_member_location or DW_AT_data_bit_offset and
13358 store the results in FIELD. */
13361 handle_member_location (struct die_info
*die
, struct dwarf2_cu
*cu
,
13362 struct field
*field
)
13364 struct attribute
*attr
;
13366 attr
= dwarf2_attr (die
, DW_AT_data_member_location
, cu
);
13369 if (attr
->form_is_constant ())
13371 LONGEST offset
= attr
->constant_value (0);
13373 /* Work around this GCC 11 bug, where it would erroneously use -1
13374 data member locations, instead of 0:
13376 Negative DW_AT_data_member_location
13377 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101378
13379 if (offset
== -1 && cu
->producer_is_gcc_11
)
13381 complaint (_("DW_AT_data_member_location value of -1, assuming 0"));
13385 field
->set_loc_bitpos (offset
* bits_per_byte
);
13387 else if (attr
->form_is_section_offset ())
13388 dwarf2_complex_location_expr_complaint ();
13389 else if (attr
->form_is_block ())
13392 CORE_ADDR offset
= decode_locdesc (attr
->as_block (), cu
, &handled
);
13394 field
->set_loc_bitpos (offset
* bits_per_byte
);
13397 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
13398 struct objfile
*objfile
= per_objfile
->objfile
;
13399 struct dwarf2_locexpr_baton
*dlbaton
13400 = XOBNEW (&objfile
->objfile_obstack
,
13401 struct dwarf2_locexpr_baton
);
13402 dlbaton
->data
= attr
->as_block ()->data
;
13403 dlbaton
->size
= attr
->as_block ()->size
;
13404 /* When using this baton, we want to compute the address
13405 of the field, not the value. This is why
13406 is_reference is set to false here. */
13407 dlbaton
->is_reference
= false;
13408 dlbaton
->per_objfile
= per_objfile
;
13409 dlbaton
->per_cu
= cu
->per_cu
;
13411 field
->set_loc_dwarf_block (dlbaton
);
13415 dwarf2_complex_location_expr_complaint ();
13419 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
13420 if (attr
!= nullptr)
13421 field
->set_loc_bitpos (attr
->constant_value (0));
13425 /* Add an aggregate field to the field list. */
13428 dwarf2_add_field (struct field_info
*fip
, struct die_info
*die
,
13429 struct dwarf2_cu
*cu
)
13431 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13432 struct gdbarch
*gdbarch
= objfile
->arch ();
13433 struct nextfield
*new_field
;
13434 struct attribute
*attr
;
13436 const char *fieldname
= "";
13438 if (die
->tag
== DW_TAG_inheritance
)
13440 fip
->baseclasses
.emplace_back ();
13441 new_field
= &fip
->baseclasses
.back ();
13445 fip
->fields
.emplace_back ();
13446 new_field
= &fip
->fields
.back ();
13449 new_field
->offset
= die
->sect_off
;
13451 new_field
->accessibility
= dwarf2_access_attribute (die
, cu
);
13452 if (new_field
->accessibility
!= DW_ACCESS_public
)
13453 fip
->non_public_fields
= true;
13455 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
13456 if (attr
!= nullptr)
13457 new_field
->virtuality
= attr
->as_virtuality ();
13459 new_field
->virtuality
= DW_VIRTUALITY_none
;
13461 fp
= &new_field
->field
;
13463 if ((die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_namelist_item
)
13464 && !die_is_declaration (die
, cu
))
13466 if (die
->tag
== DW_TAG_namelist_item
)
13468 /* Typically, DW_TAG_namelist_item are references to namelist items.
13469 If so, follow that reference. */
13470 struct attribute
*attr1
= dwarf2_attr (die
, DW_AT_namelist_item
, cu
);
13471 struct die_info
*item_die
= nullptr;
13472 struct dwarf2_cu
*item_cu
= cu
;
13473 if (attr1
->form_is_ref ())
13474 item_die
= follow_die_ref (die
, attr1
, &item_cu
);
13475 if (item_die
!= nullptr)
13478 /* Data member other than a C++ static data member. */
13480 /* Get type of field. */
13481 fp
->set_type (die_type (die
, cu
));
13483 fp
->set_loc_bitpos (0);
13485 /* Get bit size of field (zero if none). */
13486 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
13487 if (attr
!= nullptr)
13489 FIELD_BITSIZE (*fp
) = attr
->constant_value (0);
13493 FIELD_BITSIZE (*fp
) = 0;
13496 /* Get bit offset of field. */
13497 handle_member_location (die
, cu
, fp
);
13498 attr
= dwarf2_attr (die
, DW_AT_bit_offset
, cu
);
13499 if (attr
!= nullptr && attr
->form_is_constant ())
13501 if (gdbarch_byte_order (gdbarch
) == BFD_ENDIAN_BIG
)
13503 /* For big endian bits, the DW_AT_bit_offset gives the
13504 additional bit offset from the MSB of the containing
13505 anonymous object to the MSB of the field. We don't
13506 have to do anything special since we don't need to
13507 know the size of the anonymous object. */
13508 fp
->set_loc_bitpos (fp
->loc_bitpos () + attr
->constant_value (0));
13512 /* For little endian bits, compute the bit offset to the
13513 MSB of the anonymous object, subtract off the number of
13514 bits from the MSB of the field to the MSB of the
13515 object, and then subtract off the number of bits of
13516 the field itself. The result is the bit offset of
13517 the LSB of the field. */
13518 int anonymous_size
;
13519 int bit_offset
= attr
->constant_value (0);
13521 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
13522 if (attr
!= nullptr && attr
->form_is_constant ())
13524 /* The size of the anonymous object containing
13525 the bit field is explicit, so use the
13526 indicated size (in bytes). */
13527 anonymous_size
= attr
->constant_value (0);
13531 /* The size of the anonymous object containing
13532 the bit field must be inferred from the type
13533 attribute of the data member containing the
13535 anonymous_size
= TYPE_LENGTH (fp
->type ());
13537 fp
->set_loc_bitpos (fp
->loc_bitpos ()
13538 + anonymous_size
* bits_per_byte
13539 - bit_offset
- FIELD_BITSIZE (*fp
));
13543 /* Get name of field. */
13544 fieldname
= dwarf2_name (die
, cu
);
13545 if (fieldname
== NULL
)
13548 /* The name is already allocated along with this objfile, so we don't
13549 need to duplicate it for the type. */
13550 fp
->set_name (fieldname
);
13552 /* Change accessibility for artificial fields (e.g. virtual table
13553 pointer or virtual base class pointer) to private. */
13554 if (dwarf2_attr (die
, DW_AT_artificial
, cu
))
13556 FIELD_ARTIFICIAL (*fp
) = 1;
13557 new_field
->accessibility
= DW_ACCESS_private
;
13558 fip
->non_public_fields
= true;
13561 else if (die
->tag
== DW_TAG_member
|| die
->tag
== DW_TAG_variable
)
13563 /* C++ static member. */
13565 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
13566 is a declaration, but all versions of G++ as of this writing
13567 (so through at least 3.2.1) incorrectly generate
13568 DW_TAG_variable tags. */
13570 const char *physname
;
13572 /* Get name of field. */
13573 fieldname
= dwarf2_name (die
, cu
);
13574 if (fieldname
== NULL
)
13577 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
13579 /* Only create a symbol if this is an external value.
13580 new_symbol checks this and puts the value in the global symbol
13581 table, which we want. If it is not external, new_symbol
13582 will try to put the value in cu->list_in_scope which is wrong. */
13583 && dwarf2_flag_true_p (die
, DW_AT_external
, cu
))
13585 /* A static const member, not much different than an enum as far as
13586 we're concerned, except that we can support more types. */
13587 new_symbol (die
, NULL
, cu
);
13590 /* Get physical name. */
13591 physname
= dwarf2_physname (fieldname
, die
, cu
);
13593 /* The name is already allocated along with this objfile, so we don't
13594 need to duplicate it for the type. */
13595 fp
->set_loc_physname (physname
? physname
: "");
13596 fp
->set_type (die_type (die
, cu
));
13597 fp
->set_name (fieldname
);
13599 else if (die
->tag
== DW_TAG_inheritance
)
13601 /* C++ base class field. */
13602 handle_member_location (die
, cu
, fp
);
13603 FIELD_BITSIZE (*fp
) = 0;
13604 fp
->set_type (die_type (die
, cu
));
13605 fp
->set_name (fp
->type ()->name ());
13608 gdb_assert_not_reached ("missing case in dwarf2_add_field");
13611 /* Can the type given by DIE define another type? */
13614 type_can_define_types (const struct die_info
*die
)
13618 case DW_TAG_typedef
:
13619 case DW_TAG_class_type
:
13620 case DW_TAG_structure_type
:
13621 case DW_TAG_union_type
:
13622 case DW_TAG_enumeration_type
:
13630 /* Add a type definition defined in the scope of the FIP's class. */
13633 dwarf2_add_type_defn (struct field_info
*fip
, struct die_info
*die
,
13634 struct dwarf2_cu
*cu
)
13636 struct decl_field fp
;
13637 memset (&fp
, 0, sizeof (fp
));
13639 gdb_assert (type_can_define_types (die
));
13641 /* Get name of field. NULL is okay here, meaning an anonymous type. */
13642 fp
.name
= dwarf2_name (die
, cu
);
13643 fp
.type
= read_type_die (die
, cu
);
13645 /* Save accessibility. */
13646 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
13647 switch (accessibility
)
13649 case DW_ACCESS_public
:
13650 /* The assumed value if neither private nor protected. */
13652 case DW_ACCESS_private
:
13655 case DW_ACCESS_protected
:
13656 fp
.is_protected
= 1;
13660 if (die
->tag
== DW_TAG_typedef
)
13661 fip
->typedef_field_list
.push_back (fp
);
13663 fip
->nested_types_list
.push_back (fp
);
13666 /* A convenience typedef that's used when finding the discriminant
13667 field for a variant part. */
13668 typedef std::unordered_map
<sect_offset
, int, gdb::hash_enum
<sect_offset
>>
13671 /* Compute the discriminant range for a given variant. OBSTACK is
13672 where the results will be stored. VARIANT is the variant to
13673 process. IS_UNSIGNED indicates whether the discriminant is signed
13676 static const gdb::array_view
<discriminant_range
>
13677 convert_variant_range (struct obstack
*obstack
, const variant_field
&variant
,
13680 std::vector
<discriminant_range
> ranges
;
13682 if (variant
.default_branch
)
13685 if (variant
.discr_list_data
== nullptr)
13687 discriminant_range r
13688 = {variant
.discriminant_value
, variant
.discriminant_value
};
13689 ranges
.push_back (r
);
13693 gdb::array_view
<const gdb_byte
> data (variant
.discr_list_data
->data
,
13694 variant
.discr_list_data
->size
);
13695 while (!data
.empty ())
13697 if (data
[0] != DW_DSC_range
&& data
[0] != DW_DSC_label
)
13699 complaint (_("invalid discriminant marker: %d"), data
[0]);
13702 bool is_range
= data
[0] == DW_DSC_range
;
13703 data
= data
.slice (1);
13705 ULONGEST low
, high
;
13706 unsigned int bytes_read
;
13710 complaint (_("DW_AT_discr_list missing low value"));
13714 low
= read_unsigned_leb128 (nullptr, data
.data (), &bytes_read
);
13716 low
= (ULONGEST
) read_signed_leb128 (nullptr, data
.data (),
13718 data
= data
.slice (bytes_read
);
13724 complaint (_("DW_AT_discr_list missing high value"));
13728 high
= read_unsigned_leb128 (nullptr, data
.data (),
13731 high
= (LONGEST
) read_signed_leb128 (nullptr, data
.data (),
13733 data
= data
.slice (bytes_read
);
13738 ranges
.push_back ({ low
, high
});
13742 discriminant_range
*result
= XOBNEWVEC (obstack
, discriminant_range
,
13744 std::copy (ranges
.begin (), ranges
.end (), result
);
13745 return gdb::array_view
<discriminant_range
> (result
, ranges
.size ());
13748 static const gdb::array_view
<variant_part
> create_variant_parts
13749 (struct obstack
*obstack
,
13750 const offset_map_type
&offset_map
,
13751 struct field_info
*fi
,
13752 const std::vector
<variant_part_builder
> &variant_parts
);
13754 /* Fill in a "struct variant" for a given variant field. RESULT is
13755 the variant to fill in. OBSTACK is where any needed allocations
13756 will be done. OFFSET_MAP holds the mapping from section offsets to
13757 fields for the type. FI describes the fields of the type we're
13758 processing. FIELD is the variant field we're converting. */
13761 create_one_variant (variant
&result
, struct obstack
*obstack
,
13762 const offset_map_type
&offset_map
,
13763 struct field_info
*fi
, const variant_field
&field
)
13765 result
.discriminants
= convert_variant_range (obstack
, field
, false);
13766 result
.first_field
= field
.first_field
+ fi
->baseclasses
.size ();
13767 result
.last_field
= field
.last_field
+ fi
->baseclasses
.size ();
13768 result
.parts
= create_variant_parts (obstack
, offset_map
, fi
,
13769 field
.variant_parts
);
13772 /* Fill in a "struct variant_part" for a given variant part. RESULT
13773 is the variant part to fill in. OBSTACK is where any needed
13774 allocations will be done. OFFSET_MAP holds the mapping from
13775 section offsets to fields for the type. FI describes the fields of
13776 the type we're processing. BUILDER is the variant part to be
13780 create_one_variant_part (variant_part
&result
,
13781 struct obstack
*obstack
,
13782 const offset_map_type
&offset_map
,
13783 struct field_info
*fi
,
13784 const variant_part_builder
&builder
)
13786 auto iter
= offset_map
.find (builder
.discriminant_offset
);
13787 if (iter
== offset_map
.end ())
13789 result
.discriminant_index
= -1;
13790 /* Doesn't matter. */
13791 result
.is_unsigned
= false;
13795 result
.discriminant_index
= iter
->second
;
13797 = fi
->fields
[result
.discriminant_index
].field
.type ()->is_unsigned ();
13800 size_t n
= builder
.variants
.size ();
13801 variant
*output
= new (obstack
) variant
[n
];
13802 for (size_t i
= 0; i
< n
; ++i
)
13803 create_one_variant (output
[i
], obstack
, offset_map
, fi
,
13804 builder
.variants
[i
]);
13806 result
.variants
= gdb::array_view
<variant
> (output
, n
);
13809 /* Create a vector of variant parts that can be attached to a type.
13810 OBSTACK is where any needed allocations will be done. OFFSET_MAP
13811 holds the mapping from section offsets to fields for the type. FI
13812 describes the fields of the type we're processing. VARIANT_PARTS
13813 is the vector to convert. */
13815 static const gdb::array_view
<variant_part
>
13816 create_variant_parts (struct obstack
*obstack
,
13817 const offset_map_type
&offset_map
,
13818 struct field_info
*fi
,
13819 const std::vector
<variant_part_builder
> &variant_parts
)
13821 if (variant_parts
.empty ())
13824 size_t n
= variant_parts
.size ();
13825 variant_part
*result
= new (obstack
) variant_part
[n
];
13826 for (size_t i
= 0; i
< n
; ++i
)
13827 create_one_variant_part (result
[i
], obstack
, offset_map
, fi
,
13830 return gdb::array_view
<variant_part
> (result
, n
);
13833 /* Compute the variant part vector for FIP, attaching it to TYPE when
13837 add_variant_property (struct field_info
*fip
, struct type
*type
,
13838 struct dwarf2_cu
*cu
)
13840 /* Map section offsets of fields to their field index. Note the
13841 field index here does not take the number of baseclasses into
13843 offset_map_type offset_map
;
13844 for (int i
= 0; i
< fip
->fields
.size (); ++i
)
13845 offset_map
[fip
->fields
[i
].offset
] = i
;
13847 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13848 gdb::array_view
<const variant_part
> parts
13849 = create_variant_parts (&objfile
->objfile_obstack
, offset_map
, fip
,
13850 fip
->variant_parts
);
13852 struct dynamic_prop prop
;
13853 prop
.set_variant_parts ((gdb::array_view
<variant_part
> *)
13854 obstack_copy (&objfile
->objfile_obstack
, &parts
,
13857 type
->add_dyn_prop (DYN_PROP_VARIANT_PARTS
, prop
);
13860 /* Create the vector of fields, and attach it to the type. */
13863 dwarf2_attach_fields_to_type (struct field_info
*fip
, struct type
*type
,
13864 struct dwarf2_cu
*cu
)
13866 int nfields
= fip
->nfields ();
13868 /* Record the field count, allocate space for the array of fields,
13869 and create blank accessibility bitfields if necessary. */
13870 type
->set_num_fields (nfields
);
13872 ((struct field
*) TYPE_ZALLOC (type
, sizeof (struct field
) * nfields
));
13874 if (fip
->non_public_fields
&& cu
->per_cu
->lang
!= language_ada
)
13876 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13878 TYPE_FIELD_PRIVATE_BITS (type
) =
13879 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13880 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type
), nfields
);
13882 TYPE_FIELD_PROTECTED_BITS (type
) =
13883 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13884 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type
), nfields
);
13886 TYPE_FIELD_IGNORE_BITS (type
) =
13887 (B_TYPE
*) TYPE_ALLOC (type
, B_BYTES (nfields
));
13888 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type
), nfields
);
13891 /* If the type has baseclasses, allocate and clear a bit vector for
13892 TYPE_FIELD_VIRTUAL_BITS. */
13893 if (!fip
->baseclasses
.empty () && cu
->per_cu
->lang
!= language_ada
)
13895 int num_bytes
= B_BYTES (fip
->baseclasses
.size ());
13896 unsigned char *pointer
;
13898 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
13899 pointer
= (unsigned char *) TYPE_ALLOC (type
, num_bytes
);
13900 TYPE_FIELD_VIRTUAL_BITS (type
) = pointer
;
13901 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type
), fip
->baseclasses
.size ());
13902 TYPE_N_BASECLASSES (type
) = fip
->baseclasses
.size ();
13905 if (!fip
->variant_parts
.empty ())
13906 add_variant_property (fip
, type
, cu
);
13908 /* Copy the saved-up fields into the field vector. */
13909 for (int i
= 0; i
< nfields
; ++i
)
13911 struct nextfield
&field
13912 = ((i
< fip
->baseclasses
.size ()) ? fip
->baseclasses
[i
]
13913 : fip
->fields
[i
- fip
->baseclasses
.size ()]);
13915 type
->field (i
) = field
.field
;
13916 switch (field
.accessibility
)
13918 case DW_ACCESS_private
:
13919 if (cu
->per_cu
->lang
!= language_ada
)
13920 SET_TYPE_FIELD_PRIVATE (type
, i
);
13923 case DW_ACCESS_protected
:
13924 if (cu
->per_cu
->lang
!= language_ada
)
13925 SET_TYPE_FIELD_PROTECTED (type
, i
);
13928 case DW_ACCESS_public
:
13932 /* Unknown accessibility. Complain and treat it as public. */
13934 complaint (_("unsupported accessibility %d"),
13935 field
.accessibility
);
13939 if (i
< fip
->baseclasses
.size ())
13941 switch (field
.virtuality
)
13943 case DW_VIRTUALITY_virtual
:
13944 case DW_VIRTUALITY_pure_virtual
:
13945 if (cu
->per_cu
->lang
== language_ada
)
13946 error (_("unexpected virtuality in component of Ada type"));
13947 SET_TYPE_FIELD_VIRTUAL (type
, i
);
13954 /* Return true if this member function is a constructor, false
13958 dwarf2_is_constructor (struct die_info
*die
, struct dwarf2_cu
*cu
)
13960 const char *fieldname
;
13961 const char *type_name
;
13964 if (die
->parent
== NULL
)
13967 if (die
->parent
->tag
!= DW_TAG_structure_type
13968 && die
->parent
->tag
!= DW_TAG_union_type
13969 && die
->parent
->tag
!= DW_TAG_class_type
)
13972 fieldname
= dwarf2_name (die
, cu
);
13973 type_name
= dwarf2_name (die
->parent
, cu
);
13974 if (fieldname
== NULL
|| type_name
== NULL
)
13977 len
= strlen (fieldname
);
13978 return (strncmp (fieldname
, type_name
, len
) == 0
13979 && (type_name
[len
] == '\0' || type_name
[len
] == '<'));
13982 /* Add a member function to the proper fieldlist. */
13985 dwarf2_add_member_fn (struct field_info
*fip
, struct die_info
*die
,
13986 struct type
*type
, struct dwarf2_cu
*cu
)
13988 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
13989 struct attribute
*attr
;
13991 struct fnfieldlist
*flp
= nullptr;
13992 struct fn_field
*fnp
;
13993 const char *fieldname
;
13994 struct type
*this_type
;
13996 if (cu
->per_cu
->lang
== language_ada
)
13997 error (_("unexpected member function in Ada type"));
13999 /* Get name of member function. */
14000 fieldname
= dwarf2_name (die
, cu
);
14001 if (fieldname
== NULL
)
14004 /* Look up member function name in fieldlist. */
14005 for (i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14007 if (strcmp (fip
->fnfieldlists
[i
].name
, fieldname
) == 0)
14009 flp
= &fip
->fnfieldlists
[i
];
14014 /* Create a new fnfieldlist if necessary. */
14015 if (flp
== nullptr)
14017 fip
->fnfieldlists
.emplace_back ();
14018 flp
= &fip
->fnfieldlists
.back ();
14019 flp
->name
= fieldname
;
14020 i
= fip
->fnfieldlists
.size () - 1;
14023 /* Create a new member function field and add it to the vector of
14025 flp
->fnfields
.emplace_back ();
14026 fnp
= &flp
->fnfields
.back ();
14028 /* Delay processing of the physname until later. */
14029 if (cu
->per_cu
->lang
== language_cplus
)
14030 add_to_method_list (type
, i
, flp
->fnfields
.size () - 1, fieldname
,
14034 const char *physname
= dwarf2_physname (fieldname
, die
, cu
);
14035 fnp
->physname
= physname
? physname
: "";
14038 fnp
->type
= alloc_type (objfile
);
14039 this_type
= read_type_die (die
, cu
);
14040 if (this_type
&& this_type
->code () == TYPE_CODE_FUNC
)
14042 int nparams
= this_type
->num_fields ();
14044 /* TYPE is the domain of this method, and THIS_TYPE is the type
14045 of the method itself (TYPE_CODE_METHOD). */
14046 smash_to_method_type (fnp
->type
, type
,
14047 TYPE_TARGET_TYPE (this_type
),
14048 this_type
->fields (),
14049 this_type
->num_fields (),
14050 this_type
->has_varargs ());
14052 /* Handle static member functions.
14053 Dwarf2 has no clean way to discern C++ static and non-static
14054 member functions. G++ helps GDB by marking the first
14055 parameter for non-static member functions (which is the this
14056 pointer) as artificial. We obtain this information from
14057 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14058 if (nparams
== 0 || TYPE_FIELD_ARTIFICIAL (this_type
, 0) == 0)
14059 fnp
->voffset
= VOFFSET_STATIC
;
14062 complaint (_("member function type missing for '%s'"),
14063 dwarf2_full_name (fieldname
, die
, cu
));
14065 /* Get fcontext from DW_AT_containing_type if present. */
14066 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14067 fnp
->fcontext
= die_containing_type (die
, cu
);
14069 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14070 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14072 /* Get accessibility. */
14073 dwarf_access_attribute accessibility
= dwarf2_access_attribute (die
, cu
);
14074 switch (accessibility
)
14076 case DW_ACCESS_private
:
14077 fnp
->is_private
= 1;
14079 case DW_ACCESS_protected
:
14080 fnp
->is_protected
= 1;
14084 /* Check for artificial methods. */
14085 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
14086 if (attr
&& attr
->as_boolean ())
14087 fnp
->is_artificial
= 1;
14089 /* Check for defaulted methods. */
14090 attr
= dwarf2_attr (die
, DW_AT_defaulted
, cu
);
14091 if (attr
!= nullptr)
14092 fnp
->defaulted
= attr
->defaulted ();
14094 /* Check for deleted methods. */
14095 attr
= dwarf2_attr (die
, DW_AT_deleted
, cu
);
14096 if (attr
!= nullptr && attr
->as_boolean ())
14097 fnp
->is_deleted
= 1;
14099 fnp
->is_constructor
= dwarf2_is_constructor (die
, cu
);
14101 /* Get index in virtual function table if it is a virtual member
14102 function. For older versions of GCC, this is an offset in the
14103 appropriate virtual table, as specified by DW_AT_containing_type.
14104 For everyone else, it is an expression to be evaluated relative
14105 to the object address. */
14107 attr
= dwarf2_attr (die
, DW_AT_vtable_elem_location
, cu
);
14108 if (attr
!= nullptr)
14110 if (attr
->form_is_block () && attr
->as_block ()->size
> 0)
14112 struct dwarf_block
*block
= attr
->as_block ();
14114 if (block
->data
[0] == DW_OP_constu
)
14116 /* Old-style GCC. */
14117 fnp
->voffset
= decode_locdesc (block
, cu
) + 2;
14119 else if (block
->data
[0] == DW_OP_deref
14120 || (block
->size
> 1
14121 && block
->data
[0] == DW_OP_deref_size
14122 && block
->data
[1] == cu
->header
.addr_size
))
14124 fnp
->voffset
= decode_locdesc (block
, cu
);
14125 if ((fnp
->voffset
% cu
->header
.addr_size
) != 0)
14126 dwarf2_complex_location_expr_complaint ();
14128 fnp
->voffset
/= cu
->header
.addr_size
;
14132 dwarf2_complex_location_expr_complaint ();
14134 if (!fnp
->fcontext
)
14136 /* If there is no `this' field and no DW_AT_containing_type,
14137 we cannot actually find a base class context for the
14139 if (this_type
->num_fields () == 0
14140 || !TYPE_FIELD_ARTIFICIAL (this_type
, 0))
14142 complaint (_("cannot determine context for virtual member "
14143 "function \"%s\" (offset %s)"),
14144 fieldname
, sect_offset_str (die
->sect_off
));
14149 = TYPE_TARGET_TYPE (this_type
->field (0).type ());
14153 else if (attr
->form_is_section_offset ())
14155 dwarf2_complex_location_expr_complaint ();
14159 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14165 attr
= dwarf2_attr (die
, DW_AT_virtuality
, cu
);
14166 if (attr
!= nullptr && attr
->as_virtuality () != DW_VIRTUALITY_none
)
14168 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14169 complaint (_("Member function \"%s\" (offset %s) is virtual "
14170 "but the vtable offset is not specified"),
14171 fieldname
, sect_offset_str (die
->sect_off
));
14172 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14173 TYPE_CPLUS_DYNAMIC (type
) = 1;
14178 /* Create the vector of member function fields, and attach it to the type. */
14181 dwarf2_attach_fn_fields_to_type (struct field_info
*fip
, struct type
*type
,
14182 struct dwarf2_cu
*cu
)
14184 if (cu
->per_cu
->lang
== language_ada
)
14185 error (_("unexpected member functions in Ada type"));
14187 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14188 TYPE_FN_FIELDLISTS (type
) = (struct fn_fieldlist
*)
14190 sizeof (struct fn_fieldlist
) * fip
->fnfieldlists
.size ());
14192 for (int i
= 0; i
< fip
->fnfieldlists
.size (); i
++)
14194 struct fnfieldlist
&nf
= fip
->fnfieldlists
[i
];
14195 struct fn_fieldlist
*fn_flp
= &TYPE_FN_FIELDLIST (type
, i
);
14197 TYPE_FN_FIELDLIST_NAME (type
, i
) = nf
.name
;
14198 TYPE_FN_FIELDLIST_LENGTH (type
, i
) = nf
.fnfields
.size ();
14199 fn_flp
->fn_fields
= (struct fn_field
*)
14200 TYPE_ALLOC (type
, sizeof (struct fn_field
) * nf
.fnfields
.size ());
14202 for (int k
= 0; k
< nf
.fnfields
.size (); ++k
)
14203 fn_flp
->fn_fields
[k
] = nf
.fnfields
[k
];
14206 TYPE_NFN_FIELDS (type
) = fip
->fnfieldlists
.size ();
14209 /* Returns non-zero if NAME is the name of a vtable member in CU's
14210 language, zero otherwise. */
14212 is_vtable_name (const char *name
, struct dwarf2_cu
*cu
)
14214 static const char vptr
[] = "_vptr";
14216 /* Look for the C++ form of the vtable. */
14217 if (startswith (name
, vptr
) && is_cplus_marker (name
[sizeof (vptr
) - 1]))
14223 /* GCC outputs unnamed structures that are really pointers to member
14224 functions, with the ABI-specified layout. If TYPE describes
14225 such a structure, smash it into a member function type.
14227 GCC shouldn't do this; it should just output pointer to member DIEs.
14228 This is GCC PR debug/28767. */
14231 quirk_gcc_member_function_pointer (struct type
*type
, struct objfile
*objfile
)
14233 struct type
*pfn_type
, *self_type
, *new_type
;
14235 /* Check for a structure with no name and two children. */
14236 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14239 /* Check for __pfn and __delta members. */
14240 if (type
->field (0).name () == NULL
14241 || strcmp (type
->field (0).name (), "__pfn") != 0
14242 || type
->field (1).name () == NULL
14243 || strcmp (type
->field (1).name (), "__delta") != 0)
14246 /* Find the type of the method. */
14247 pfn_type
= type
->field (0).type ();
14248 if (pfn_type
== NULL
14249 || pfn_type
->code () != TYPE_CODE_PTR
14250 || TYPE_TARGET_TYPE (pfn_type
)->code () != TYPE_CODE_FUNC
)
14253 /* Look for the "this" argument. */
14254 pfn_type
= TYPE_TARGET_TYPE (pfn_type
);
14255 if (pfn_type
->num_fields () == 0
14256 /* || pfn_type->field (0).type () == NULL */
14257 || pfn_type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14260 self_type
= TYPE_TARGET_TYPE (pfn_type
->field (0).type ());
14261 new_type
= alloc_type (objfile
);
14262 smash_to_method_type (new_type
, self_type
, TYPE_TARGET_TYPE (pfn_type
),
14263 pfn_type
->fields (), pfn_type
->num_fields (),
14264 pfn_type
->has_varargs ());
14265 smash_to_methodptr_type (type
, new_type
);
14268 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
14269 requires rewriting, then copy it and return the updated copy.
14270 Otherwise return nullptr. */
14272 static struct type
*
14273 rewrite_array_type (struct type
*type
)
14275 if (type
->code () != TYPE_CODE_ARRAY
)
14278 struct type
*index_type
= type
->index_type ();
14279 range_bounds
*current_bounds
= index_type
->bounds ();
14281 /* Handle multi-dimensional arrays. */
14282 struct type
*new_target
= rewrite_array_type (TYPE_TARGET_TYPE (type
));
14283 if (new_target
== nullptr)
14285 /* Maybe we don't need to rewrite this array. */
14286 if (current_bounds
->low
.kind () == PROP_CONST
14287 && current_bounds
->high
.kind () == PROP_CONST
)
14291 /* Either the target type was rewritten, or the bounds have to be
14292 updated. Either way we want to copy the type and update
14294 struct type
*copy
= copy_type (type
);
14295 int nfields
= copy
->num_fields ();
14297 = ((struct field
*) TYPE_ZALLOC (copy
,
14298 nfields
* sizeof (struct field
)));
14299 memcpy (new_fields
, copy
->fields (), nfields
* sizeof (struct field
));
14300 copy
->set_fields (new_fields
);
14301 if (new_target
!= nullptr)
14302 TYPE_TARGET_TYPE (copy
) = new_target
;
14304 struct type
*index_copy
= copy_type (index_type
);
14305 range_bounds
*bounds
14306 = (struct range_bounds
*) TYPE_ZALLOC (index_copy
,
14307 sizeof (range_bounds
));
14308 *bounds
= *current_bounds
;
14309 bounds
->low
.set_const_val (1);
14310 bounds
->high
.set_const_val (0);
14311 index_copy
->set_bounds (bounds
);
14312 copy
->set_index_type (index_copy
);
14317 /* While some versions of GCC will generate complicated DWARF for an
14318 array (see quirk_ada_thick_pointer), more recent versions were
14319 modified to emit an explicit thick pointer structure. However, in
14320 this case, the array still has DWARF expressions for its ranges,
14321 and these must be ignored. */
14324 quirk_ada_thick_pointer_struct (struct die_info
*die
, struct dwarf2_cu
*cu
,
14327 gdb_assert (cu
->per_cu
->lang
== language_ada
);
14329 /* Check for a structure with two children. */
14330 if (type
->code () != TYPE_CODE_STRUCT
|| type
->num_fields () != 2)
14333 /* Check for P_ARRAY and P_BOUNDS members. */
14334 if (type
->field (0).name () == NULL
14335 || strcmp (type
->field (0).name (), "P_ARRAY") != 0
14336 || type
->field (1).name () == NULL
14337 || strcmp (type
->field (1).name (), "P_BOUNDS") != 0)
14340 /* Make sure we're looking at a pointer to an array. */
14341 if (type
->field (0).type ()->code () != TYPE_CODE_PTR
)
14344 /* The Ada code already knows how to handle these types, so all that
14345 we need to do is turn the bounds into static bounds. However, we
14346 don't want to rewrite existing array or index types in-place,
14347 because those may be referenced in other contexts where this
14348 rewriting is undesirable. */
14349 struct type
*new_ary_type
14350 = rewrite_array_type (TYPE_TARGET_TYPE (type
->field (0).type ()));
14351 if (new_ary_type
!= nullptr)
14352 type
->field (0).set_type (lookup_pointer_type (new_ary_type
));
14355 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14356 appropriate error checking and issuing complaints if there is a
14360 get_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
)
14362 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_alignment
, cu
);
14364 if (attr
== nullptr)
14367 if (!attr
->form_is_constant ())
14369 complaint (_("DW_AT_alignment must have constant form"
14370 " - DIE at %s [in module %s]"),
14371 sect_offset_str (die
->sect_off
),
14372 objfile_name (cu
->per_objfile
->objfile
));
14376 LONGEST val
= attr
->constant_value (0);
14379 complaint (_("DW_AT_alignment value must not be negative"
14380 " - DIE at %s [in module %s]"),
14381 sect_offset_str (die
->sect_off
),
14382 objfile_name (cu
->per_objfile
->objfile
));
14385 ULONGEST align
= val
;
14389 complaint (_("DW_AT_alignment value must not be zero"
14390 " - DIE at %s [in module %s]"),
14391 sect_offset_str (die
->sect_off
),
14392 objfile_name (cu
->per_objfile
->objfile
));
14395 if ((align
& (align
- 1)) != 0)
14397 complaint (_("DW_AT_alignment value must be a power of 2"
14398 " - DIE at %s [in module %s]"),
14399 sect_offset_str (die
->sect_off
),
14400 objfile_name (cu
->per_objfile
->objfile
));
14407 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14408 the alignment for TYPE. */
14411 maybe_set_alignment (struct dwarf2_cu
*cu
, struct die_info
*die
,
14414 if (!set_type_align (type
, get_alignment (cu
, die
)))
14415 complaint (_("DW_AT_alignment value too large"
14416 " - DIE at %s [in module %s]"),
14417 sect_offset_str (die
->sect_off
),
14418 objfile_name (cu
->per_objfile
->objfile
));
14421 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14422 constant for a type, according to DWARF5 spec, Table 5.5. */
14425 is_valid_DW_AT_calling_convention_for_type (ULONGEST value
)
14430 case DW_CC_pass_by_reference
:
14431 case DW_CC_pass_by_value
:
14435 complaint (_("unrecognized DW_AT_calling_convention value "
14436 "(%s) for a type"), pulongest (value
));
14441 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14442 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14443 also according to GNU-specific values (see include/dwarf2.h). */
14446 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value
)
14451 case DW_CC_program
:
14455 case DW_CC_GNU_renesas_sh
:
14456 case DW_CC_GNU_borland_fastcall_i386
:
14457 case DW_CC_GDB_IBM_OpenCL
:
14461 complaint (_("unrecognized DW_AT_calling_convention value "
14462 "(%s) for a subroutine"), pulongest (value
));
14467 /* Called when we find the DIE that starts a structure or union scope
14468 (definition) to create a type for the structure or union. Fill in
14469 the type's name and general properties; the members will not be
14470 processed until process_structure_scope. A symbol table entry for
14471 the type will also not be done until process_structure_scope (assuming
14472 the type has a name).
14474 NOTE: we need to call these functions regardless of whether or not the
14475 DIE has a DW_AT_name attribute, since it might be an anonymous
14476 structure or union. This gets the type entered into our set of
14477 user defined types. */
14479 static struct type
*
14480 read_structure_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
14482 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14484 struct attribute
*attr
;
14487 /* If the definition of this type lives in .debug_types, read that type.
14488 Don't follow DW_AT_specification though, that will take us back up
14489 the chain and we want to go down. */
14490 attr
= die
->attr (DW_AT_signature
);
14491 if (attr
!= nullptr)
14493 type
= get_DW_AT_signature_type (die
, attr
, cu
);
14495 /* The type's CU may not be the same as CU.
14496 Ensure TYPE is recorded with CU in die_type_hash. */
14497 return set_die_type (die
, type
, cu
);
14500 type
= alloc_type (objfile
);
14501 INIT_CPLUS_SPECIFIC (type
);
14503 name
= dwarf2_name (die
, cu
);
14506 if (cu
->per_cu
->lang
== language_cplus
14507 || cu
->per_cu
->lang
== language_d
14508 || cu
->per_cu
->lang
== language_rust
)
14510 const char *full_name
= dwarf2_full_name (name
, die
, cu
);
14512 /* dwarf2_full_name might have already finished building the DIE's
14513 type. If so, there is no need to continue. */
14514 if (get_die_type (die
, cu
) != NULL
)
14515 return get_die_type (die
, cu
);
14517 type
->set_name (full_name
);
14521 /* The name is already allocated along with this objfile, so
14522 we don't need to duplicate it for the type. */
14523 type
->set_name (name
);
14527 if (die
->tag
== DW_TAG_structure_type
)
14529 type
->set_code (TYPE_CODE_STRUCT
);
14531 else if (die
->tag
== DW_TAG_union_type
)
14533 type
->set_code (TYPE_CODE_UNION
);
14535 else if (die
->tag
== DW_TAG_namelist
)
14537 type
->set_code (TYPE_CODE_NAMELIST
);
14541 type
->set_code (TYPE_CODE_STRUCT
);
14544 if (cu
->per_cu
->lang
== language_cplus
&& die
->tag
== DW_TAG_class_type
)
14545 type
->set_is_declared_class (true);
14547 /* Store the calling convention in the type if it's available in
14548 the die. Otherwise the calling convention remains set to
14549 the default value DW_CC_normal. */
14550 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
14551 if (attr
!= nullptr
14552 && is_valid_DW_AT_calling_convention_for_type (attr
->constant_value (0)))
14554 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14555 TYPE_CPLUS_CALLING_CONVENTION (type
)
14556 = (enum dwarf_calling_convention
) (attr
->constant_value (0));
14559 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
14560 if (attr
!= nullptr)
14562 if (attr
->form_is_constant ())
14563 TYPE_LENGTH (type
) = attr
->constant_value (0);
14566 struct dynamic_prop prop
;
14567 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
14568 type
->add_dyn_prop (DYN_PROP_BYTE_SIZE
, prop
);
14569 TYPE_LENGTH (type
) = 0;
14574 TYPE_LENGTH (type
) = 0;
14577 maybe_set_alignment (cu
, die
, type
);
14579 if (producer_is_icc_lt_14 (cu
) && (TYPE_LENGTH (type
) == 0))
14581 /* ICC<14 does not output the required DW_AT_declaration on
14582 incomplete types, but gives them a size of zero. */
14583 type
->set_is_stub (true);
14586 type
->set_stub_is_supported (true);
14588 if (die_is_declaration (die
, cu
))
14589 type
->set_is_stub (true);
14590 else if (attr
== NULL
&& die
->child
== NULL
14591 && producer_is_realview (cu
->producer
))
14592 /* RealView does not output the required DW_AT_declaration
14593 on incomplete types. */
14594 type
->set_is_stub (true);
14596 /* We need to add the type field to the die immediately so we don't
14597 infinitely recurse when dealing with pointers to the structure
14598 type within the structure itself. */
14599 set_die_type (die
, type
, cu
);
14601 /* set_die_type should be already done. */
14602 set_descriptive_type (type
, die
, cu
);
14607 static void handle_struct_member_die
14608 (struct die_info
*child_die
,
14610 struct field_info
*fi
,
14611 std::vector
<struct symbol
*> *template_args
,
14612 struct dwarf2_cu
*cu
);
14614 /* A helper for handle_struct_member_die that handles
14615 DW_TAG_variant_part. */
14618 handle_variant_part (struct die_info
*die
, struct type
*type
,
14619 struct field_info
*fi
,
14620 std::vector
<struct symbol
*> *template_args
,
14621 struct dwarf2_cu
*cu
)
14623 variant_part_builder
*new_part
;
14624 if (fi
->current_variant_part
== nullptr)
14626 fi
->variant_parts
.emplace_back ();
14627 new_part
= &fi
->variant_parts
.back ();
14629 else if (!fi
->current_variant_part
->processing_variant
)
14631 complaint (_("nested DW_TAG_variant_part seen "
14632 "- DIE at %s [in module %s]"),
14633 sect_offset_str (die
->sect_off
),
14634 objfile_name (cu
->per_objfile
->objfile
));
14639 variant_field
¤t
= fi
->current_variant_part
->variants
.back ();
14640 current
.variant_parts
.emplace_back ();
14641 new_part
= ¤t
.variant_parts
.back ();
14644 /* When we recurse, we want callees to add to this new variant
14646 scoped_restore save_current_variant_part
14647 = make_scoped_restore (&fi
->current_variant_part
, new_part
);
14649 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr
, cu
);
14652 /* It's a univariant form, an extension we support. */
14654 else if (discr
->form_is_ref ())
14656 struct dwarf2_cu
*target_cu
= cu
;
14657 struct die_info
*target_die
= follow_die_ref (die
, discr
, &target_cu
);
14659 new_part
->discriminant_offset
= target_die
->sect_off
;
14663 complaint (_("DW_AT_discr does not have DIE reference form"
14664 " - DIE at %s [in module %s]"),
14665 sect_offset_str (die
->sect_off
),
14666 objfile_name (cu
->per_objfile
->objfile
));
14669 for (die_info
*child_die
= die
->child
;
14671 child_die
= child_die
->sibling
)
14672 handle_struct_member_die (child_die
, type
, fi
, template_args
, cu
);
14675 /* A helper for handle_struct_member_die that handles
14679 handle_variant (struct die_info
*die
, struct type
*type
,
14680 struct field_info
*fi
,
14681 std::vector
<struct symbol
*> *template_args
,
14682 struct dwarf2_cu
*cu
)
14684 if (fi
->current_variant_part
== nullptr)
14686 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
14687 "- DIE at %s [in module %s]"),
14688 sect_offset_str (die
->sect_off
),
14689 objfile_name (cu
->per_objfile
->objfile
));
14692 if (fi
->current_variant_part
->processing_variant
)
14694 complaint (_("nested DW_TAG_variant seen "
14695 "- DIE at %s [in module %s]"),
14696 sect_offset_str (die
->sect_off
),
14697 objfile_name (cu
->per_objfile
->objfile
));
14701 scoped_restore save_processing_variant
14702 = make_scoped_restore (&fi
->current_variant_part
->processing_variant
,
14705 fi
->current_variant_part
->variants
.emplace_back ();
14706 variant_field
&variant
= fi
->current_variant_part
->variants
.back ();
14707 variant
.first_field
= fi
->fields
.size ();
14709 /* In a variant we want to get the discriminant and also add a
14710 field for our sole member child. */
14711 struct attribute
*discr
= dwarf2_attr (die
, DW_AT_discr_value
, cu
);
14712 if (discr
== nullptr || !discr
->form_is_constant ())
14714 discr
= dwarf2_attr (die
, DW_AT_discr_list
, cu
);
14715 if (discr
== nullptr || discr
->as_block ()->size
== 0)
14716 variant
.default_branch
= true;
14718 variant
.discr_list_data
= discr
->as_block ();
14721 variant
.discriminant_value
= discr
->constant_value (0);
14723 for (die_info
*variant_child
= die
->child
;
14724 variant_child
!= NULL
;
14725 variant_child
= variant_child
->sibling
)
14726 handle_struct_member_die (variant_child
, type
, fi
, template_args
, cu
);
14728 variant
.last_field
= fi
->fields
.size ();
14731 /* A helper for process_structure_scope that handles a single member
14735 handle_struct_member_die (struct die_info
*child_die
, struct type
*type
,
14736 struct field_info
*fi
,
14737 std::vector
<struct symbol
*> *template_args
,
14738 struct dwarf2_cu
*cu
)
14740 if (child_die
->tag
== DW_TAG_member
14741 || child_die
->tag
== DW_TAG_variable
14742 || child_die
->tag
== DW_TAG_namelist_item
)
14744 /* NOTE: carlton/2002-11-05: A C++ static data member
14745 should be a DW_TAG_member that is a declaration, but
14746 all versions of G++ as of this writing (so through at
14747 least 3.2.1) incorrectly generate DW_TAG_variable
14748 tags for them instead. */
14749 dwarf2_add_field (fi
, child_die
, cu
);
14751 else if (child_die
->tag
== DW_TAG_subprogram
)
14753 /* Rust doesn't have member functions in the C++ sense.
14754 However, it does emit ordinary functions as children
14755 of a struct DIE. */
14756 if (cu
->per_cu
->lang
== language_rust
)
14757 read_func_scope (child_die
, cu
);
14760 /* C++ member function. */
14761 dwarf2_add_member_fn (fi
, child_die
, type
, cu
);
14764 else if (child_die
->tag
== DW_TAG_inheritance
)
14766 /* C++ base class field. */
14767 dwarf2_add_field (fi
, child_die
, cu
);
14769 else if (type_can_define_types (child_die
))
14770 dwarf2_add_type_defn (fi
, child_die
, cu
);
14771 else if (child_die
->tag
== DW_TAG_template_type_param
14772 || child_die
->tag
== DW_TAG_template_value_param
)
14774 struct symbol
*arg
= new_symbol (child_die
, NULL
, cu
);
14777 template_args
->push_back (arg
);
14779 else if (child_die
->tag
== DW_TAG_variant_part
)
14780 handle_variant_part (child_die
, type
, fi
, template_args
, cu
);
14781 else if (child_die
->tag
== DW_TAG_variant
)
14782 handle_variant (child_die
, type
, fi
, template_args
, cu
);
14785 /* Finish creating a structure or union type, including filling in its
14786 members and creating a symbol for it. This function also handles Fortran
14787 namelist variables, their items or members and creating a symbol for
14791 process_structure_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
14793 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
14794 struct die_info
*child_die
;
14797 type
= get_die_type (die
, cu
);
14799 type
= read_structure_type (die
, cu
);
14801 bool has_template_parameters
= false;
14802 if (die
->child
!= NULL
&& ! die_is_declaration (die
, cu
))
14804 struct field_info fi
;
14805 std::vector
<struct symbol
*> template_args
;
14807 child_die
= die
->child
;
14809 while (child_die
&& child_die
->tag
)
14811 handle_struct_member_die (child_die
, type
, &fi
, &template_args
, cu
);
14812 child_die
= child_die
->sibling
;
14815 /* Attach template arguments to type. */
14816 if (!template_args
.empty ())
14818 has_template_parameters
= true;
14819 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14820 TYPE_N_TEMPLATE_ARGUMENTS (type
) = template_args
.size ();
14821 TYPE_TEMPLATE_ARGUMENTS (type
)
14822 = XOBNEWVEC (&objfile
->objfile_obstack
,
14824 TYPE_N_TEMPLATE_ARGUMENTS (type
));
14825 memcpy (TYPE_TEMPLATE_ARGUMENTS (type
),
14826 template_args
.data (),
14827 (TYPE_N_TEMPLATE_ARGUMENTS (type
)
14828 * sizeof (struct symbol
*)));
14831 /* Attach fields and member functions to the type. */
14832 if (fi
.nfields () > 0)
14833 dwarf2_attach_fields_to_type (&fi
, type
, cu
);
14834 if (!fi
.fnfieldlists
.empty ())
14836 dwarf2_attach_fn_fields_to_type (&fi
, type
, cu
);
14838 /* Get the type which refers to the base class (possibly this
14839 class itself) which contains the vtable pointer for the current
14840 class from the DW_AT_containing_type attribute. This use of
14841 DW_AT_containing_type is a GNU extension. */
14843 if (dwarf2_attr (die
, DW_AT_containing_type
, cu
) != NULL
)
14845 struct type
*t
= die_containing_type (die
, cu
);
14847 set_type_vptr_basetype (type
, t
);
14852 /* Our own class provides vtbl ptr. */
14853 for (i
= t
->num_fields () - 1;
14854 i
>= TYPE_N_BASECLASSES (t
);
14857 const char *fieldname
= t
->field (i
).name ();
14859 if (is_vtable_name (fieldname
, cu
))
14861 set_type_vptr_fieldno (type
, i
);
14866 /* Complain if virtual function table field not found. */
14867 if (i
< TYPE_N_BASECLASSES (t
))
14868 complaint (_("virtual function table pointer "
14869 "not found when defining class '%s'"),
14870 type
->name () ? type
->name () : "");
14874 set_type_vptr_fieldno (type
, TYPE_VPTR_FIELDNO (t
));
14877 else if (cu
->producer
14878 && startswith (cu
->producer
, "IBM(R) XL C/C++ Advanced Edition"))
14880 /* The IBM XLC compiler does not provide direct indication
14881 of the containing type, but the vtable pointer is
14882 always named __vfp. */
14886 for (i
= type
->num_fields () - 1;
14887 i
>= TYPE_N_BASECLASSES (type
);
14890 if (strcmp (type
->field (i
).name (), "__vfp") == 0)
14892 set_type_vptr_fieldno (type
, i
);
14893 set_type_vptr_basetype (type
, type
);
14900 /* Copy fi.typedef_field_list linked list elements content into the
14901 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
14902 if (!fi
.typedef_field_list
.empty ())
14904 int count
= fi
.typedef_field_list
.size ();
14906 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14907 TYPE_TYPEDEF_FIELD_ARRAY (type
)
14908 = ((struct decl_field
*)
14910 sizeof (TYPE_TYPEDEF_FIELD (type
, 0)) * count
));
14911 TYPE_TYPEDEF_FIELD_COUNT (type
) = count
;
14913 for (int i
= 0; i
< fi
.typedef_field_list
.size (); ++i
)
14914 TYPE_TYPEDEF_FIELD (type
, i
) = fi
.typedef_field_list
[i
];
14917 /* Copy fi.nested_types_list linked list elements content into the
14918 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
14919 if (!fi
.nested_types_list
.empty ()
14920 && cu
->per_cu
->lang
!= language_ada
)
14922 int count
= fi
.nested_types_list
.size ();
14924 ALLOCATE_CPLUS_STRUCT_TYPE (type
);
14925 TYPE_NESTED_TYPES_ARRAY (type
)
14926 = ((struct decl_field
*)
14927 TYPE_ALLOC (type
, sizeof (struct decl_field
) * count
));
14928 TYPE_NESTED_TYPES_COUNT (type
) = count
;
14930 for (int i
= 0; i
< fi
.nested_types_list
.size (); ++i
)
14931 TYPE_NESTED_TYPES_FIELD (type
, i
) = fi
.nested_types_list
[i
];
14935 quirk_gcc_member_function_pointer (type
, objfile
);
14936 if (cu
->per_cu
->lang
== language_rust
&& die
->tag
== DW_TAG_union_type
)
14937 cu
->rust_unions
.push_back (type
);
14938 else if (cu
->per_cu
->lang
== language_ada
)
14939 quirk_ada_thick_pointer_struct (die
, cu
, type
);
14941 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
14942 snapshots) has been known to create a die giving a declaration
14943 for a class that has, as a child, a die giving a definition for a
14944 nested class. So we have to process our children even if the
14945 current die is a declaration. Normally, of course, a declaration
14946 won't have any children at all. */
14948 child_die
= die
->child
;
14950 while (child_die
!= NULL
&& child_die
->tag
)
14952 if (child_die
->tag
== DW_TAG_member
14953 || child_die
->tag
== DW_TAG_variable
14954 || child_die
->tag
== DW_TAG_inheritance
14955 || child_die
->tag
== DW_TAG_template_value_param
14956 || child_die
->tag
== DW_TAG_template_type_param
)
14961 process_die (child_die
, cu
);
14963 child_die
= child_die
->sibling
;
14966 /* Do not consider external references. According to the DWARF standard,
14967 these DIEs are identified by the fact that they have no byte_size
14968 attribute, and a declaration attribute. */
14969 if (dwarf2_attr (die
, DW_AT_byte_size
, cu
) != NULL
14970 || !die_is_declaration (die
, cu
)
14971 || dwarf2_attr (die
, DW_AT_signature
, cu
) != NULL
)
14973 struct symbol
*sym
= new_symbol (die
, type
, cu
);
14975 if (has_template_parameters
)
14977 struct symtab
*symtab
;
14978 if (sym
!= nullptr)
14979 symtab
= sym
->symtab ();
14980 else if (cu
->line_header
!= nullptr)
14982 /* Any related symtab will do. */
14984 = cu
->line_header
->file_names ()[0].symtab
;
14989 complaint (_("could not find suitable "
14990 "symtab for template parameter"
14991 " - DIE at %s [in module %s]"),
14992 sect_offset_str (die
->sect_off
),
14993 objfile_name (objfile
));
14996 if (symtab
!= nullptr)
14998 /* Make sure that the symtab is set on the new symbols.
14999 Even though they don't appear in this symtab directly,
15000 other parts of gdb assume that symbols do, and this is
15001 reasonably true. */
15002 for (int i
= 0; i
< TYPE_N_TEMPLATE_ARGUMENTS (type
); ++i
)
15003 TYPE_TEMPLATE_ARGUMENT (type
, i
)->set_symtab (symtab
);
15009 /* Assuming DIE is an enumeration type, and TYPE is its associated
15010 type, update TYPE using some information only available in DIE's
15011 children. In particular, the fields are computed. */
15014 update_enumeration_type_from_children (struct die_info
*die
,
15016 struct dwarf2_cu
*cu
)
15018 struct die_info
*child_die
;
15019 int unsigned_enum
= 1;
15022 auto_obstack obstack
;
15023 std::vector
<struct field
> fields
;
15025 for (child_die
= die
->child
;
15026 child_die
!= NULL
&& child_die
->tag
;
15027 child_die
= child_die
->sibling
)
15029 struct attribute
*attr
;
15031 const gdb_byte
*bytes
;
15032 struct dwarf2_locexpr_baton
*baton
;
15035 if (child_die
->tag
!= DW_TAG_enumerator
)
15038 attr
= dwarf2_attr (child_die
, DW_AT_const_value
, cu
);
15042 name
= dwarf2_name (child_die
, cu
);
15044 name
= "<anonymous enumerator>";
15046 dwarf2_const_value_attr (attr
, type
, name
, &obstack
, cu
,
15047 &value
, &bytes
, &baton
);
15055 if (count_one_bits_ll (value
) >= 2)
15059 fields
.emplace_back ();
15060 struct field
&field
= fields
.back ();
15061 field
.set_name (dwarf2_physname (name
, child_die
, cu
));
15062 field
.set_loc_enumval (value
);
15065 if (!fields
.empty ())
15067 type
->set_num_fields (fields
.size ());
15070 TYPE_ALLOC (type
, sizeof (struct field
) * fields
.size ()));
15071 memcpy (type
->fields (), fields
.data (),
15072 sizeof (struct field
) * fields
.size ());
15076 type
->set_is_unsigned (true);
15079 type
->set_is_flag_enum (true);
15082 /* Given a DW_AT_enumeration_type die, set its type. We do not
15083 complete the type's fields yet, or create any symbols. */
15085 static struct type
*
15086 read_enumeration_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15088 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15090 struct attribute
*attr
;
15093 /* If the definition of this type lives in .debug_types, read that type.
15094 Don't follow DW_AT_specification though, that will take us back up
15095 the chain and we want to go down. */
15096 attr
= die
->attr (DW_AT_signature
);
15097 if (attr
!= nullptr)
15099 type
= get_DW_AT_signature_type (die
, attr
, cu
);
15101 /* The type's CU may not be the same as CU.
15102 Ensure TYPE is recorded with CU in die_type_hash. */
15103 return set_die_type (die
, type
, cu
);
15106 type
= alloc_type (objfile
);
15108 type
->set_code (TYPE_CODE_ENUM
);
15109 name
= dwarf2_full_name (NULL
, die
, cu
);
15111 type
->set_name (name
);
15113 attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
15116 struct type
*underlying_type
= die_type (die
, cu
);
15118 TYPE_TARGET_TYPE (type
) = underlying_type
;
15121 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15122 if (attr
!= nullptr)
15124 TYPE_LENGTH (type
) = attr
->constant_value (0);
15128 TYPE_LENGTH (type
) = 0;
15131 maybe_set_alignment (cu
, die
, type
);
15133 /* The enumeration DIE can be incomplete. In Ada, any type can be
15134 declared as private in the package spec, and then defined only
15135 inside the package body. Such types are known as Taft Amendment
15136 Types. When another package uses such a type, an incomplete DIE
15137 may be generated by the compiler. */
15138 if (die_is_declaration (die
, cu
))
15139 type
->set_is_stub (true);
15141 /* If this type has an underlying type that is not a stub, then we
15142 may use its attributes. We always use the "unsigned" attribute
15143 in this situation, because ordinarily we guess whether the type
15144 is unsigned -- but the guess can be wrong and the underlying type
15145 can tell us the reality. However, we defer to a local size
15146 attribute if one exists, because this lets the compiler override
15147 the underlying type if needed. */
15148 if (TYPE_TARGET_TYPE (type
) != NULL
&& !TYPE_TARGET_TYPE (type
)->is_stub ())
15150 struct type
*underlying_type
= TYPE_TARGET_TYPE (type
);
15151 underlying_type
= check_typedef (underlying_type
);
15153 type
->set_is_unsigned (underlying_type
->is_unsigned ());
15155 if (TYPE_LENGTH (type
) == 0)
15156 TYPE_LENGTH (type
) = TYPE_LENGTH (underlying_type
);
15158 if (TYPE_RAW_ALIGN (type
) == 0
15159 && TYPE_RAW_ALIGN (underlying_type
) != 0)
15160 set_type_align (type
, TYPE_RAW_ALIGN (underlying_type
));
15163 type
->set_is_declared_class (dwarf2_flag_true_p (die
, DW_AT_enum_class
, cu
));
15165 set_die_type (die
, type
, cu
);
15167 /* Finish the creation of this type by using the enum's children.
15168 Note that, as usual, this must come after set_die_type to avoid
15169 infinite recursion when trying to compute the names of the
15171 update_enumeration_type_from_children (die
, type
, cu
);
15176 /* Given a pointer to a die which begins an enumeration, process all
15177 the dies that define the members of the enumeration, and create the
15178 symbol for the enumeration type.
15180 NOTE: We reverse the order of the element list. */
15183 process_enumeration_scope (struct die_info
*die
, struct dwarf2_cu
*cu
)
15185 struct type
*this_type
;
15187 this_type
= get_die_type (die
, cu
);
15188 if (this_type
== NULL
)
15189 this_type
= read_enumeration_type (die
, cu
);
15191 if (die
->child
!= NULL
)
15193 struct die_info
*child_die
;
15196 child_die
= die
->child
;
15197 while (child_die
&& child_die
->tag
)
15199 if (child_die
->tag
!= DW_TAG_enumerator
)
15201 process_die (child_die
, cu
);
15205 name
= dwarf2_name (child_die
, cu
);
15207 new_symbol (child_die
, this_type
, cu
);
15210 child_die
= child_die
->sibling
;
15214 /* If we are reading an enum from a .debug_types unit, and the enum
15215 is a declaration, and the enum is not the signatured type in the
15216 unit, then we do not want to add a symbol for it. Adding a
15217 symbol would in some cases obscure the true definition of the
15218 enum, giving users an incomplete type when the definition is
15219 actually available. Note that we do not want to do this for all
15220 enums which are just declarations, because C++0x allows forward
15221 enum declarations. */
15222 if (cu
->per_cu
->is_debug_types
15223 && die_is_declaration (die
, cu
))
15225 struct signatured_type
*sig_type
;
15227 sig_type
= (struct signatured_type
*) cu
->per_cu
;
15228 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
15229 if (sig_type
->type_offset_in_section
!= die
->sect_off
)
15233 new_symbol (die
, this_type
, cu
);
15236 /* Helper function for quirk_ada_thick_pointer that examines a bounds
15237 expression for an index type and finds the corresponding field
15238 offset in the hidden "P_BOUNDS" structure. Returns true on success
15239 and updates *FIELD, false if it fails to recognize an
15243 recognize_bound_expression (struct die_info
*die
, enum dwarf_attribute name
,
15244 int *bounds_offset
, struct field
*field
,
15245 struct dwarf2_cu
*cu
)
15247 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
15248 if (attr
== nullptr || !attr
->form_is_block ())
15251 const struct dwarf_block
*block
= attr
->as_block ();
15252 const gdb_byte
*start
= block
->data
;
15253 const gdb_byte
*end
= block
->data
+ block
->size
;
15255 /* The expression to recognize generally looks like:
15257 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15258 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15260 However, the second "plus_uconst" may be missing:
15262 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15263 DW_OP_deref_size: 4)
15265 This happens when the field is at the start of the structure.
15267 Also, the final deref may not be sized:
15269 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
15272 This happens when the size of the index type happens to be the
15273 same as the architecture's word size. This can occur with or
15274 without the second plus_uconst. */
15276 if (end
- start
< 2)
15278 if (*start
++ != DW_OP_push_object_address
)
15280 if (*start
++ != DW_OP_plus_uconst
)
15283 uint64_t this_bound_off
;
15284 start
= gdb_read_uleb128 (start
, end
, &this_bound_off
);
15285 if (start
== nullptr || (int) this_bound_off
!= this_bound_off
)
15287 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
15288 is consistent among all bounds. */
15289 if (*bounds_offset
== -1)
15290 *bounds_offset
= this_bound_off
;
15291 else if (*bounds_offset
!= this_bound_off
)
15294 if (start
== end
|| *start
++ != DW_OP_deref
)
15300 else if (*start
== DW_OP_deref_size
|| *start
== DW_OP_deref
)
15302 /* This means an offset of 0. */
15304 else if (*start
++ != DW_OP_plus_uconst
)
15308 /* The size is the parameter to DW_OP_plus_uconst. */
15310 start
= gdb_read_uleb128 (start
, end
, &val
);
15311 if (start
== nullptr)
15313 if ((int) val
!= val
)
15322 if (*start
== DW_OP_deref_size
)
15324 start
= gdb_read_uleb128 (start
+ 1, end
, &size
);
15325 if (start
== nullptr)
15328 else if (*start
== DW_OP_deref
)
15330 size
= cu
->header
.addr_size
;
15336 field
->set_loc_bitpos (8 * offset
);
15337 if (size
!= TYPE_LENGTH (field
->type ()))
15338 FIELD_BITSIZE (*field
) = 8 * size
;
15343 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
15344 some kinds of Ada arrays:
15346 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
15347 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
15348 <11e0> DW_AT_data_location: 2 byte block: 97 6
15349 (DW_OP_push_object_address; DW_OP_deref)
15350 <11e3> DW_AT_type : <0x1173>
15351 <11e7> DW_AT_sibling : <0x1201>
15352 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
15353 <11ec> DW_AT_type : <0x1206>
15354 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
15355 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15356 DW_OP_deref_size: 4)
15357 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
15358 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
15359 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
15361 This actually represents a "thick pointer", which is a structure
15362 with two elements: one that is a pointer to the array data, and one
15363 that is a pointer to another structure; this second structure holds
15366 This returns a new type on success, or nullptr if this didn't
15367 recognize the type. */
15369 static struct type
*
15370 quirk_ada_thick_pointer (struct die_info
*die
, struct dwarf2_cu
*cu
,
15373 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
15374 /* So far we've only seen this with block form. */
15375 if (attr
== nullptr || !attr
->form_is_block ())
15378 /* Note that this will fail if the structure layout is changed by
15379 the compiler. However, we have no good way to recognize some
15380 other layout, because we don't know what expression the compiler
15381 might choose to emit should this happen. */
15382 struct dwarf_block
*blk
= attr
->as_block ();
15384 || blk
->data
[0] != DW_OP_push_object_address
15385 || blk
->data
[1] != DW_OP_deref
)
15388 int bounds_offset
= -1;
15389 int max_align
= -1;
15390 std::vector
<struct field
> range_fields
;
15391 for (struct die_info
*child_die
= die
->child
;
15393 child_die
= child_die
->sibling
)
15395 if (child_die
->tag
== DW_TAG_subrange_type
)
15397 struct type
*underlying
= read_subrange_index_type (child_die
, cu
);
15399 int this_align
= type_align (underlying
);
15400 if (this_align
> max_align
)
15401 max_align
= this_align
;
15403 range_fields
.emplace_back ();
15404 range_fields
.emplace_back ();
15406 struct field
&lower
= range_fields
[range_fields
.size () - 2];
15407 struct field
&upper
= range_fields
[range_fields
.size () - 1];
15409 lower
.set_type (underlying
);
15410 FIELD_ARTIFICIAL (lower
) = 1;
15412 upper
.set_type (underlying
);
15413 FIELD_ARTIFICIAL (upper
) = 1;
15415 if (!recognize_bound_expression (child_die
, DW_AT_lower_bound
,
15416 &bounds_offset
, &lower
, cu
)
15417 || !recognize_bound_expression (child_die
, DW_AT_upper_bound
,
15418 &bounds_offset
, &upper
, cu
))
15423 /* This shouldn't really happen, but double-check that we found
15424 where the bounds are stored. */
15425 if (bounds_offset
== -1)
15428 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15429 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15433 /* Set the name of each field in the bounds. */
15434 xsnprintf (name
, sizeof (name
), "LB%d", i
/ 2);
15435 range_fields
[i
].set_name (objfile
->intern (name
));
15436 xsnprintf (name
, sizeof (name
), "UB%d", i
/ 2);
15437 range_fields
[i
+ 1].set_name (objfile
->intern (name
));
15440 struct type
*bounds
= alloc_type (objfile
);
15441 bounds
->set_code (TYPE_CODE_STRUCT
);
15443 bounds
->set_num_fields (range_fields
.size ());
15445 ((struct field
*) TYPE_ALLOC (bounds
, (bounds
->num_fields ()
15446 * sizeof (struct field
))));
15447 memcpy (bounds
->fields (), range_fields
.data (),
15448 bounds
->num_fields () * sizeof (struct field
));
15450 int last_fieldno
= range_fields
.size () - 1;
15451 int bounds_size
= (bounds
->field (last_fieldno
).loc_bitpos () / 8
15452 + TYPE_LENGTH (bounds
->field (last_fieldno
).type ()));
15453 TYPE_LENGTH (bounds
) = align_up (bounds_size
, max_align
);
15455 /* Rewrite the existing array type in place. Specifically, we
15456 remove any dynamic properties we might have read, and we replace
15457 the index types. */
15458 struct type
*iter
= type
;
15459 for (int i
= 0; i
< range_fields
.size (); i
+= 2)
15461 gdb_assert (iter
->code () == TYPE_CODE_ARRAY
);
15462 iter
->main_type
->dyn_prop_list
= nullptr;
15463 iter
->set_index_type
15464 (create_static_range_type (NULL
, bounds
->field (i
).type (), 1, 0));
15465 iter
= TYPE_TARGET_TYPE (iter
);
15468 struct type
*result
= alloc_type (objfile
);
15469 result
->set_code (TYPE_CODE_STRUCT
);
15471 result
->set_num_fields (2);
15473 ((struct field
*) TYPE_ZALLOC (result
, (result
->num_fields ()
15474 * sizeof (struct field
))));
15476 /* The names are chosen to coincide with what the compiler does with
15477 -fgnat-encodings=all, which the Ada code in gdb already
15479 result
->field (0).set_name ("P_ARRAY");
15480 result
->field (0).set_type (lookup_pointer_type (type
));
15482 result
->field (1).set_name ("P_BOUNDS");
15483 result
->field (1).set_type (lookup_pointer_type (bounds
));
15484 result
->field (1).set_loc_bitpos (8 * bounds_offset
);
15486 result
->set_name (type
->name ());
15487 TYPE_LENGTH (result
) = (TYPE_LENGTH (result
->field (0).type ())
15488 + TYPE_LENGTH (result
->field (1).type ()));
15493 /* Extract all information from a DW_TAG_array_type DIE and put it in
15494 the DIE's type field. For now, this only handles one dimensional
15497 static struct type
*
15498 read_array_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15500 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15501 struct die_info
*child_die
;
15503 struct type
*element_type
, *range_type
, *index_type
;
15504 struct attribute
*attr
;
15506 struct dynamic_prop
*byte_stride_prop
= NULL
;
15507 unsigned int bit_stride
= 0;
15509 element_type
= die_type (die
, cu
);
15511 /* The die_type call above may have already set the type for this DIE. */
15512 type
= get_die_type (die
, cu
);
15516 attr
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
15520 struct type
*prop_type
= cu
->addr_sized_int_type (false);
15523 = (struct dynamic_prop
*) alloca (sizeof (struct dynamic_prop
));
15524 stride_ok
= attr_to_dynamic_prop (attr
, die
, cu
, byte_stride_prop
,
15528 complaint (_("unable to read array DW_AT_byte_stride "
15529 " - DIE at %s [in module %s]"),
15530 sect_offset_str (die
->sect_off
),
15531 objfile_name (cu
->per_objfile
->objfile
));
15532 /* Ignore this attribute. We will likely not be able to print
15533 arrays of this type correctly, but there is little we can do
15534 to help if we cannot read the attribute's value. */
15535 byte_stride_prop
= NULL
;
15539 attr
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
15541 bit_stride
= attr
->constant_value (0);
15543 /* Irix 6.2 native cc creates array types without children for
15544 arrays with unspecified length. */
15545 if (die
->child
== NULL
)
15547 index_type
= objfile_type (objfile
)->builtin_int
;
15548 range_type
= create_static_range_type (NULL
, index_type
, 0, -1);
15549 type
= create_array_type_with_stride (NULL
, element_type
, range_type
,
15550 byte_stride_prop
, bit_stride
);
15551 return set_die_type (die
, type
, cu
);
15554 std::vector
<struct type
*> range_types
;
15555 child_die
= die
->child
;
15556 while (child_die
&& child_die
->tag
)
15558 if (child_die
->tag
== DW_TAG_subrange_type
15559 || child_die
->tag
== DW_TAG_generic_subrange
)
15561 struct type
*child_type
= read_type_die (child_die
, cu
);
15563 if (child_type
!= NULL
)
15565 /* The range type was succesfully read. Save it for the
15566 array type creation. */
15567 range_types
.push_back (child_type
);
15570 child_die
= child_die
->sibling
;
15573 if (range_types
.empty ())
15575 complaint (_("unable to find array range - DIE at %s [in module %s]"),
15576 sect_offset_str (die
->sect_off
),
15577 objfile_name (cu
->per_objfile
->objfile
));
15581 /* Dwarf2 dimensions are output from left to right, create the
15582 necessary array types in backwards order. */
15584 type
= element_type
;
15586 if (read_array_order (die
, cu
) == DW_ORD_col_major
)
15590 while (i
< range_types
.size ())
15592 type
= create_array_type_with_stride (NULL
, type
, range_types
[i
++],
15593 byte_stride_prop
, bit_stride
);
15595 byte_stride_prop
= nullptr;
15600 size_t ndim
= range_types
.size ();
15603 type
= create_array_type_with_stride (NULL
, type
, range_types
[ndim
],
15604 byte_stride_prop
, bit_stride
);
15606 byte_stride_prop
= nullptr;
15610 gdb_assert (type
!= element_type
);
15612 /* Understand Dwarf2 support for vector types (like they occur on
15613 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15614 array type. This is not part of the Dwarf2/3 standard yet, but a
15615 custom vendor extension. The main difference between a regular
15616 array and the vector variant is that vectors are passed by value
15618 attr
= dwarf2_attr (die
, DW_AT_GNU_vector
, cu
);
15619 if (attr
!= nullptr)
15620 make_vector_type (type
);
15622 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15623 implementation may choose to implement triple vectors using this
15625 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15626 if (attr
!= nullptr && attr
->form_is_unsigned ())
15628 if (attr
->as_unsigned () >= TYPE_LENGTH (type
))
15629 TYPE_LENGTH (type
) = attr
->as_unsigned ();
15631 complaint (_("DW_AT_byte_size for array type smaller "
15632 "than the total size of elements"));
15635 name
= dwarf2_name (die
, cu
);
15637 type
->set_name (name
);
15639 maybe_set_alignment (cu
, die
, type
);
15641 struct type
*replacement_type
= nullptr;
15642 if (cu
->per_cu
->lang
== language_ada
)
15644 replacement_type
= quirk_ada_thick_pointer (die
, cu
, type
);
15645 if (replacement_type
!= nullptr)
15646 type
= replacement_type
;
15649 /* Install the type in the die. */
15650 set_die_type (die
, type
, cu
, replacement_type
!= nullptr);
15652 /* set_die_type should be already done. */
15653 set_descriptive_type (type
, die
, cu
);
15658 static enum dwarf_array_dim_ordering
15659 read_array_order (struct die_info
*die
, struct dwarf2_cu
*cu
)
15661 struct attribute
*attr
;
15663 attr
= dwarf2_attr (die
, DW_AT_ordering
, cu
);
15665 if (attr
!= nullptr)
15667 LONGEST val
= attr
->constant_value (-1);
15668 if (val
== DW_ORD_row_major
|| val
== DW_ORD_col_major
)
15669 return (enum dwarf_array_dim_ordering
) val
;
15672 /* GNU F77 is a special case, as at 08/2004 array type info is the
15673 opposite order to the dwarf2 specification, but data is still
15674 laid out as per normal fortran.
15676 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15677 version checking. */
15679 if (cu
->per_cu
->lang
== language_fortran
15680 && cu
->producer
&& strstr (cu
->producer
, "GNU F77"))
15682 return DW_ORD_row_major
;
15685 switch (cu
->language_defn
->array_ordering ())
15687 case array_column_major
:
15688 return DW_ORD_col_major
;
15689 case array_row_major
:
15691 return DW_ORD_row_major
;
15695 /* Extract all information from a DW_TAG_set_type DIE and put it in
15696 the DIE's type field. */
15698 static struct type
*
15699 read_set_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15701 struct type
*domain_type
, *set_type
;
15702 struct attribute
*attr
;
15704 domain_type
= die_type (die
, cu
);
15706 /* The die_type call above may have already set the type for this DIE. */
15707 set_type
= get_die_type (die
, cu
);
15711 set_type
= create_set_type (NULL
, domain_type
);
15713 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
15714 if (attr
!= nullptr && attr
->form_is_unsigned ())
15715 TYPE_LENGTH (set_type
) = attr
->as_unsigned ();
15717 maybe_set_alignment (cu
, die
, set_type
);
15719 return set_die_type (die
, set_type
, cu
);
15722 /* A helper for read_common_block that creates a locexpr baton.
15723 SYM is the symbol which we are marking as computed.
15724 COMMON_DIE is the DIE for the common block.
15725 COMMON_LOC is the location expression attribute for the common
15727 MEMBER_LOC is the location expression attribute for the particular
15728 member of the common block that we are processing.
15729 CU is the CU from which the above come. */
15732 mark_common_block_symbol_computed (struct symbol
*sym
,
15733 struct die_info
*common_die
,
15734 struct attribute
*common_loc
,
15735 struct attribute
*member_loc
,
15736 struct dwarf2_cu
*cu
)
15738 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
15739 struct objfile
*objfile
= per_objfile
->objfile
;
15740 struct dwarf2_locexpr_baton
*baton
;
15742 unsigned int cu_off
;
15743 enum bfd_endian byte_order
= gdbarch_byte_order (objfile
->arch ());
15744 LONGEST offset
= 0;
15746 gdb_assert (common_loc
&& member_loc
);
15747 gdb_assert (common_loc
->form_is_block ());
15748 gdb_assert (member_loc
->form_is_block ()
15749 || member_loc
->form_is_constant ());
15751 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
15752 baton
->per_objfile
= per_objfile
;
15753 baton
->per_cu
= cu
->per_cu
;
15754 gdb_assert (baton
->per_cu
);
15756 baton
->size
= 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15758 if (member_loc
->form_is_constant ())
15760 offset
= member_loc
->constant_value (0);
15761 baton
->size
+= 1 /* DW_OP_addr */ + cu
->header
.addr_size
;
15764 baton
->size
+= member_loc
->as_block ()->size
;
15766 ptr
= (gdb_byte
*) obstack_alloc (&objfile
->objfile_obstack
, baton
->size
);
15769 *ptr
++ = DW_OP_call4
;
15770 cu_off
= common_die
->sect_off
- cu
->per_cu
->sect_off
;
15771 store_unsigned_integer (ptr
, 4, byte_order
, cu_off
);
15774 if (member_loc
->form_is_constant ())
15776 *ptr
++ = DW_OP_addr
;
15777 store_unsigned_integer (ptr
, cu
->header
.addr_size
, byte_order
, offset
);
15778 ptr
+= cu
->header
.addr_size
;
15782 /* We have to copy the data here, because DW_OP_call4 will only
15783 use a DW_AT_location attribute. */
15784 struct dwarf_block
*block
= member_loc
->as_block ();
15785 memcpy (ptr
, block
->data
, block
->size
);
15786 ptr
+= block
->size
;
15789 *ptr
++ = DW_OP_plus
;
15790 gdb_assert (ptr
- baton
->data
== baton
->size
);
15792 SYMBOL_LOCATION_BATON (sym
) = baton
;
15793 sym
->set_aclass_index (dwarf2_locexpr_index
);
15796 /* Create appropriate locally-scoped variables for all the
15797 DW_TAG_common_block entries. Also create a struct common_block
15798 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15799 is used to separate the common blocks name namespace from regular
15803 read_common_block (struct die_info
*die
, struct dwarf2_cu
*cu
)
15805 struct attribute
*attr
;
15807 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
15808 if (attr
!= nullptr)
15810 /* Support the .debug_loc offsets. */
15811 if (attr
->form_is_block ())
15815 else if (attr
->form_is_section_offset ())
15817 dwarf2_complex_location_expr_complaint ();
15822 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15823 "common block member");
15828 if (die
->child
!= NULL
)
15830 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15831 struct die_info
*child_die
;
15832 size_t n_entries
= 0, size
;
15833 struct common_block
*common_block
;
15834 struct symbol
*sym
;
15836 for (child_die
= die
->child
;
15837 child_die
&& child_die
->tag
;
15838 child_die
= child_die
->sibling
)
15841 size
= (sizeof (struct common_block
)
15842 + (n_entries
- 1) * sizeof (struct symbol
*));
15844 = (struct common_block
*) obstack_alloc (&objfile
->objfile_obstack
,
15846 memset (common_block
->contents
, 0, n_entries
* sizeof (struct symbol
*));
15847 common_block
->n_entries
= 0;
15849 for (child_die
= die
->child
;
15850 child_die
&& child_die
->tag
;
15851 child_die
= child_die
->sibling
)
15853 /* Create the symbol in the DW_TAG_common_block block in the current
15855 sym
= new_symbol (child_die
, NULL
, cu
);
15858 struct attribute
*member_loc
;
15860 common_block
->contents
[common_block
->n_entries
++] = sym
;
15862 member_loc
= dwarf2_attr (child_die
, DW_AT_data_member_location
,
15866 /* GDB has handled this for a long time, but it is
15867 not specified by DWARF. It seems to have been
15868 emitted by gfortran at least as recently as:
15869 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15870 complaint (_("Variable in common block has "
15871 "DW_AT_data_member_location "
15872 "- DIE at %s [in module %s]"),
15873 sect_offset_str (child_die
->sect_off
),
15874 objfile_name (objfile
));
15876 if (member_loc
->form_is_section_offset ())
15877 dwarf2_complex_location_expr_complaint ();
15878 else if (member_loc
->form_is_constant ()
15879 || member_loc
->form_is_block ())
15881 if (attr
!= nullptr)
15882 mark_common_block_symbol_computed (sym
, die
, attr
,
15886 dwarf2_complex_location_expr_complaint ();
15891 sym
= new_symbol (die
, objfile_type (objfile
)->builtin_void
, cu
);
15892 sym
->set_value_common_block (common_block
);
15896 /* Create a type for a C++ namespace. */
15898 static struct type
*
15899 read_namespace_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15901 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15902 const char *previous_prefix
, *name
;
15906 /* For extensions, reuse the type of the original namespace. */
15907 if (dwarf2_attr (die
, DW_AT_extension
, cu
) != NULL
)
15909 struct die_info
*ext_die
;
15910 struct dwarf2_cu
*ext_cu
= cu
;
15912 ext_die
= dwarf2_extension (die
, &ext_cu
);
15913 type
= read_type_die (ext_die
, ext_cu
);
15915 /* EXT_CU may not be the same as CU.
15916 Ensure TYPE is recorded with CU in die_type_hash. */
15917 return set_die_type (die
, type
, cu
);
15920 name
= namespace_name (die
, &is_anonymous
, cu
);
15922 /* Now build the name of the current namespace. */
15924 previous_prefix
= determine_prefix (die
, cu
);
15925 if (previous_prefix
[0] != '\0')
15926 name
= typename_concat (&objfile
->objfile_obstack
,
15927 previous_prefix
, name
, 0, cu
);
15929 /* Create the type. */
15930 type
= init_type (objfile
, TYPE_CODE_NAMESPACE
, 0, name
);
15932 return set_die_type (die
, type
, cu
);
15935 /* Read a namespace scope. */
15938 read_namespace (struct die_info
*die
, struct dwarf2_cu
*cu
)
15940 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15943 /* Add a symbol associated to this if we haven't seen the namespace
15944 before. Also, add a using directive if it's an anonymous
15947 if (dwarf2_attr (die
, DW_AT_extension
, cu
) == NULL
)
15951 type
= read_type_die (die
, cu
);
15952 new_symbol (die
, type
, cu
);
15954 namespace_name (die
, &is_anonymous
, cu
);
15957 const char *previous_prefix
= determine_prefix (die
, cu
);
15959 std::vector
<const char *> excludes
;
15960 add_using_directive (using_directives (cu
),
15961 previous_prefix
, type
->name (), NULL
,
15962 NULL
, excludes
, 0, &objfile
->objfile_obstack
);
15966 if (die
->child
!= NULL
)
15968 struct die_info
*child_die
= die
->child
;
15970 while (child_die
&& child_die
->tag
)
15972 process_die (child_die
, cu
);
15973 child_die
= child_die
->sibling
;
15978 /* Read a Fortran module as type. This DIE can be only a declaration used for
15979 imported module. Still we need that type as local Fortran "use ... only"
15980 declaration imports depend on the created type in determine_prefix. */
15982 static struct type
*
15983 read_module_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
15985 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
15986 const char *module_name
;
15989 module_name
= dwarf2_name (die
, cu
);
15990 type
= init_type (objfile
, TYPE_CODE_MODULE
, 0, module_name
);
15992 return set_die_type (die
, type
, cu
);
15995 /* Read a Fortran module. */
15998 read_module (struct die_info
*die
, struct dwarf2_cu
*cu
)
16000 struct die_info
*child_die
= die
->child
;
16003 type
= read_type_die (die
, cu
);
16004 new_symbol (die
, type
, cu
);
16006 while (child_die
&& child_die
->tag
)
16008 process_die (child_die
, cu
);
16009 child_die
= child_die
->sibling
;
16013 /* Return the name of the namespace represented by DIE. Set
16014 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16017 static const char *
16018 namespace_name (struct die_info
*die
, int *is_anonymous
, struct dwarf2_cu
*cu
)
16020 struct die_info
*current_die
;
16021 const char *name
= NULL
;
16023 /* Loop through the extensions until we find a name. */
16025 for (current_die
= die
;
16026 current_die
!= NULL
;
16027 current_die
= dwarf2_extension (die
, &cu
))
16029 /* We don't use dwarf2_name here so that we can detect the absence
16030 of a name -> anonymous namespace. */
16031 name
= dwarf2_string_attr (die
, DW_AT_name
, cu
);
16037 /* Is it an anonymous namespace? */
16039 *is_anonymous
= (name
== NULL
);
16041 name
= CP_ANONYMOUS_NAMESPACE_STR
;
16046 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16047 the user defined type vector. */
16049 static struct type
*
16050 read_tag_pointer_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16052 struct gdbarch
*gdbarch
= cu
->per_objfile
->objfile
->arch ();
16053 struct comp_unit_head
*cu_header
= &cu
->header
;
16055 struct attribute
*attr_byte_size
;
16056 struct attribute
*attr_address_class
;
16057 int byte_size
, addr_class
;
16058 struct type
*target_type
;
16060 target_type
= die_type (die
, cu
);
16062 /* The die_type call above may have already set the type for this DIE. */
16063 type
= get_die_type (die
, cu
);
16067 type
= lookup_pointer_type (target_type
);
16069 attr_byte_size
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16070 if (attr_byte_size
)
16071 byte_size
= attr_byte_size
->constant_value (cu_header
->addr_size
);
16073 byte_size
= cu_header
->addr_size
;
16075 attr_address_class
= dwarf2_attr (die
, DW_AT_address_class
, cu
);
16076 if (attr_address_class
)
16077 addr_class
= attr_address_class
->constant_value (DW_ADDR_none
);
16079 addr_class
= DW_ADDR_none
;
16081 ULONGEST alignment
= get_alignment (cu
, die
);
16083 /* If the pointer size, alignment, or address class is different
16084 than the default, create a type variant marked as such and set
16085 the length accordingly. */
16086 if (TYPE_LENGTH (type
) != byte_size
16087 || (alignment
!= 0 && TYPE_RAW_ALIGN (type
) != 0
16088 && alignment
!= TYPE_RAW_ALIGN (type
))
16089 || addr_class
!= DW_ADDR_none
)
16091 if (gdbarch_address_class_type_flags_p (gdbarch
))
16093 type_instance_flags type_flags
16094 = gdbarch_address_class_type_flags (gdbarch
, byte_size
,
16096 gdb_assert ((type_flags
& ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL
)
16098 type
= make_type_with_address_space (type
, type_flags
);
16100 else if (TYPE_LENGTH (type
) != byte_size
)
16102 complaint (_("invalid pointer size %d"), byte_size
);
16104 else if (TYPE_RAW_ALIGN (type
) != alignment
)
16106 complaint (_("Invalid DW_AT_alignment"
16107 " - DIE at %s [in module %s]"),
16108 sect_offset_str (die
->sect_off
),
16109 objfile_name (cu
->per_objfile
->objfile
));
16113 /* Should we also complain about unhandled address classes? */
16117 TYPE_LENGTH (type
) = byte_size
;
16118 set_type_align (type
, alignment
);
16119 return set_die_type (die
, type
, cu
);
16122 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16123 the user defined type vector. */
16125 static struct type
*
16126 read_tag_ptr_to_member_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16129 struct type
*to_type
;
16130 struct type
*domain
;
16132 to_type
= die_type (die
, cu
);
16133 domain
= die_containing_type (die
, cu
);
16135 /* The calls above may have already set the type for this DIE. */
16136 type
= get_die_type (die
, cu
);
16140 if (check_typedef (to_type
)->code () == TYPE_CODE_METHOD
)
16141 type
= lookup_methodptr_type (to_type
);
16142 else if (check_typedef (to_type
)->code () == TYPE_CODE_FUNC
)
16144 struct type
*new_type
= alloc_type (cu
->per_objfile
->objfile
);
16146 smash_to_method_type (new_type
, domain
, TYPE_TARGET_TYPE (to_type
),
16147 to_type
->fields (), to_type
->num_fields (),
16148 to_type
->has_varargs ());
16149 type
= lookup_methodptr_type (new_type
);
16152 type
= lookup_memberptr_type (to_type
, domain
);
16154 return set_die_type (die
, type
, cu
);
16157 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16158 the user defined type vector. */
16160 static struct type
*
16161 read_tag_reference_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16162 enum type_code refcode
)
16164 struct comp_unit_head
*cu_header
= &cu
->header
;
16165 struct type
*type
, *target_type
;
16166 struct attribute
*attr
;
16168 gdb_assert (refcode
== TYPE_CODE_REF
|| refcode
== TYPE_CODE_RVALUE_REF
);
16170 target_type
= die_type (die
, cu
);
16172 /* The die_type call above may have already set the type for this DIE. */
16173 type
= get_die_type (die
, cu
);
16177 type
= lookup_reference_type (target_type
, refcode
);
16178 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16179 if (attr
!= nullptr)
16181 TYPE_LENGTH (type
) = attr
->constant_value (cu_header
->addr_size
);
16185 TYPE_LENGTH (type
) = cu_header
->addr_size
;
16187 maybe_set_alignment (cu
, die
, type
);
16188 return set_die_type (die
, type
, cu
);
16191 /* Add the given cv-qualifiers to the element type of the array. GCC
16192 outputs DWARF type qualifiers that apply to an array, not the
16193 element type. But GDB relies on the array element type to carry
16194 the cv-qualifiers. This mimics section 6.7.3 of the C99
16197 static struct type
*
16198 add_array_cv_type (struct die_info
*die
, struct dwarf2_cu
*cu
,
16199 struct type
*base_type
, int cnst
, int voltl
)
16201 struct type
*el_type
, *inner_array
;
16203 base_type
= copy_type (base_type
);
16204 inner_array
= base_type
;
16206 while (TYPE_TARGET_TYPE (inner_array
)->code () == TYPE_CODE_ARRAY
)
16208 TYPE_TARGET_TYPE (inner_array
) =
16209 copy_type (TYPE_TARGET_TYPE (inner_array
));
16210 inner_array
= TYPE_TARGET_TYPE (inner_array
);
16213 el_type
= TYPE_TARGET_TYPE (inner_array
);
16214 cnst
|= TYPE_CONST (el_type
);
16215 voltl
|= TYPE_VOLATILE (el_type
);
16216 TYPE_TARGET_TYPE (inner_array
) = make_cv_type (cnst
, voltl
, el_type
, NULL
);
16218 return set_die_type (die
, base_type
, cu
);
16221 static struct type
*
16222 read_tag_const_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16224 struct type
*base_type
, *cv_type
;
16226 base_type
= die_type (die
, cu
);
16228 /* The die_type call above may have already set the type for this DIE. */
16229 cv_type
= get_die_type (die
, cu
);
16233 /* In case the const qualifier is applied to an array type, the element type
16234 is so qualified, not the array type (section 6.7.3 of C99). */
16235 if (base_type
->code () == TYPE_CODE_ARRAY
)
16236 return add_array_cv_type (die
, cu
, base_type
, 1, 0);
16238 cv_type
= make_cv_type (1, TYPE_VOLATILE (base_type
), base_type
, 0);
16239 return set_die_type (die
, cv_type
, cu
);
16242 static struct type
*
16243 read_tag_volatile_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16245 struct type
*base_type
, *cv_type
;
16247 base_type
= die_type (die
, cu
);
16249 /* The die_type call above may have already set the type for this DIE. */
16250 cv_type
= get_die_type (die
, cu
);
16254 /* In case the volatile qualifier is applied to an array type, the
16255 element type is so qualified, not the array type (section 6.7.3
16257 if (base_type
->code () == TYPE_CODE_ARRAY
)
16258 return add_array_cv_type (die
, cu
, base_type
, 0, 1);
16260 cv_type
= make_cv_type (TYPE_CONST (base_type
), 1, base_type
, 0);
16261 return set_die_type (die
, cv_type
, cu
);
16264 /* Handle DW_TAG_restrict_type. */
16266 static struct type
*
16267 read_tag_restrict_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16269 struct type
*base_type
, *cv_type
;
16271 base_type
= die_type (die
, cu
);
16273 /* The die_type call above may have already set the type for this DIE. */
16274 cv_type
= get_die_type (die
, cu
);
16278 cv_type
= make_restrict_type (base_type
);
16279 return set_die_type (die
, cv_type
, cu
);
16282 /* Handle DW_TAG_atomic_type. */
16284 static struct type
*
16285 read_tag_atomic_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16287 struct type
*base_type
, *cv_type
;
16289 base_type
= die_type (die
, cu
);
16291 /* The die_type call above may have already set the type for this DIE. */
16292 cv_type
= get_die_type (die
, cu
);
16296 cv_type
= make_atomic_type (base_type
);
16297 return set_die_type (die
, cv_type
, cu
);
16300 /* Extract all information from a DW_TAG_string_type DIE and add to
16301 the user defined type vector. It isn't really a user defined type,
16302 but it behaves like one, with other DIE's using an AT_user_def_type
16303 attribute to reference it. */
16305 static struct type
*
16306 read_tag_string_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16308 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16309 struct gdbarch
*gdbarch
= objfile
->arch ();
16310 struct type
*type
, *range_type
, *index_type
, *char_type
;
16311 struct attribute
*attr
;
16312 struct dynamic_prop prop
;
16313 bool length_is_constant
= true;
16316 /* There are a couple of places where bit sizes might be made use of
16317 when parsing a DW_TAG_string_type, however, no producer that we know
16318 of make use of these. Handling bit sizes that are a multiple of the
16319 byte size is easy enough, but what about other bit sizes? Lets deal
16320 with that problem when we have to. Warn about these attributes being
16321 unsupported, then parse the type and ignore them like we always
16323 if (dwarf2_attr (die
, DW_AT_bit_size
, cu
) != nullptr
16324 || dwarf2_attr (die
, DW_AT_string_length_bit_size
, cu
) != nullptr)
16326 static bool warning_printed
= false;
16327 if (!warning_printed
)
16329 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16330 "currently supported on DW_TAG_string_type."));
16331 warning_printed
= true;
16335 attr
= dwarf2_attr (die
, DW_AT_string_length
, cu
);
16336 if (attr
!= nullptr && !attr
->form_is_constant ())
16338 /* The string length describes the location at which the length of
16339 the string can be found. The size of the length field can be
16340 specified with one of the attributes below. */
16341 struct type
*prop_type
;
16342 struct attribute
*len
16343 = dwarf2_attr (die
, DW_AT_string_length_byte_size
, cu
);
16344 if (len
== nullptr)
16345 len
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
16346 if (len
!= nullptr && len
->form_is_constant ())
16348 /* Pass 0 as the default as we know this attribute is constant
16349 and the default value will not be returned. */
16350 LONGEST sz
= len
->constant_value (0);
16351 prop_type
= objfile_int_type (objfile
, sz
, true);
16355 /* If the size is not specified then we assume it is the size of
16356 an address on this target. */
16357 prop_type
= cu
->addr_sized_int_type (true);
16360 /* Convert the attribute into a dynamic property. */
16361 if (!attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
16364 length_is_constant
= false;
16366 else if (attr
!= nullptr)
16368 /* This DW_AT_string_length just contains the length with no
16369 indirection. There's no need to create a dynamic property in this
16370 case. Pass 0 for the default value as we know it will not be
16371 returned in this case. */
16372 length
= attr
->constant_value (0);
16374 else if ((attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
)) != nullptr)
16376 /* We don't currently support non-constant byte sizes for strings. */
16377 length
= attr
->constant_value (1);
16381 /* Use 1 as a fallback length if we have nothing else. */
16385 index_type
= objfile_type (objfile
)->builtin_int
;
16386 if (length_is_constant
)
16387 range_type
= create_static_range_type (NULL
, index_type
, 1, length
);
16390 struct dynamic_prop low_bound
;
16392 low_bound
.set_const_val (1);
16393 range_type
= create_range_type (NULL
, index_type
, &low_bound
, &prop
, 0);
16395 char_type
= language_string_char_type (cu
->language_defn
, gdbarch
);
16396 type
= create_string_type (NULL
, char_type
, range_type
);
16398 return set_die_type (die
, type
, cu
);
16401 /* Assuming that DIE corresponds to a function, returns nonzero
16402 if the function is prototyped. */
16405 prototyped_function_p (struct die_info
*die
, struct dwarf2_cu
*cu
)
16407 struct attribute
*attr
;
16409 attr
= dwarf2_attr (die
, DW_AT_prototyped
, cu
);
16410 if (attr
&& attr
->as_boolean ())
16413 /* The DWARF standard implies that the DW_AT_prototyped attribute
16414 is only meaningful for C, but the concept also extends to other
16415 languages that allow unprototyped functions (Eg: Objective C).
16416 For all other languages, assume that functions are always
16418 if (cu
->per_cu
->lang
!= language_c
16419 && cu
->per_cu
->lang
!= language_objc
16420 && cu
->per_cu
->lang
!= language_opencl
)
16423 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16424 prototyped and unprototyped functions; default to prototyped,
16425 since that is more common in modern code (and RealView warns
16426 about unprototyped functions). */
16427 if (producer_is_realview (cu
->producer
))
16433 /* Handle DIES due to C code like:
16437 int (*funcp)(int a, long l);
16441 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16443 static struct type
*
16444 read_subroutine_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
16446 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16447 struct type
*type
; /* Type that this function returns. */
16448 struct type
*ftype
; /* Function that returns above type. */
16449 struct attribute
*attr
;
16451 type
= die_type (die
, cu
);
16453 /* The die_type call above may have already set the type for this DIE. */
16454 ftype
= get_die_type (die
, cu
);
16458 ftype
= lookup_function_type (type
);
16460 if (prototyped_function_p (die
, cu
))
16461 ftype
->set_is_prototyped (true);
16463 /* Store the calling convention in the type if it's available in
16464 the subroutine die. Otherwise set the calling convention to
16465 the default value DW_CC_normal. */
16466 attr
= dwarf2_attr (die
, DW_AT_calling_convention
, cu
);
16467 if (attr
!= nullptr
16468 && is_valid_DW_AT_calling_convention_for_subroutine (attr
->constant_value (0)))
16469 TYPE_CALLING_CONVENTION (ftype
)
16470 = (enum dwarf_calling_convention
) attr
->constant_value (0);
16471 else if (cu
->producer
&& strstr (cu
->producer
, "IBM XL C for OpenCL"))
16472 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_GDB_IBM_OpenCL
;
16474 TYPE_CALLING_CONVENTION (ftype
) = DW_CC_normal
;
16476 /* Record whether the function returns normally to its caller or not
16477 if the DWARF producer set that information. */
16478 attr
= dwarf2_attr (die
, DW_AT_noreturn
, cu
);
16479 if (attr
&& attr
->as_boolean ())
16480 TYPE_NO_RETURN (ftype
) = 1;
16482 /* We need to add the subroutine type to the die immediately so
16483 we don't infinitely recurse when dealing with parameters
16484 declared as the same subroutine type. */
16485 set_die_type (die
, ftype
, cu
);
16487 if (die
->child
!= NULL
)
16489 struct type
*void_type
= objfile_type (objfile
)->builtin_void
;
16490 struct die_info
*child_die
;
16491 int nparams
, iparams
;
16493 /* Count the number of parameters.
16494 FIXME: GDB currently ignores vararg functions, but knows about
16495 vararg member functions. */
16497 child_die
= die
->child
;
16498 while (child_die
&& child_die
->tag
)
16500 if (child_die
->tag
== DW_TAG_formal_parameter
)
16502 else if (child_die
->tag
== DW_TAG_unspecified_parameters
)
16503 ftype
->set_has_varargs (true);
16505 child_die
= child_die
->sibling
;
16508 /* Allocate storage for parameters and fill them in. */
16509 ftype
->set_num_fields (nparams
);
16511 ((struct field
*) TYPE_ZALLOC (ftype
, nparams
* sizeof (struct field
)));
16513 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16514 even if we error out during the parameters reading below. */
16515 for (iparams
= 0; iparams
< nparams
; iparams
++)
16516 ftype
->field (iparams
).set_type (void_type
);
16519 child_die
= die
->child
;
16520 while (child_die
&& child_die
->tag
)
16522 if (child_die
->tag
== DW_TAG_formal_parameter
)
16524 struct type
*arg_type
;
16526 /* DWARF version 2 has no clean way to discern C++
16527 static and non-static member functions. G++ helps
16528 GDB by marking the first parameter for non-static
16529 member functions (which is the this pointer) as
16530 artificial. We pass this information to
16531 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16533 DWARF version 3 added DW_AT_object_pointer, which GCC
16534 4.5 does not yet generate. */
16535 attr
= dwarf2_attr (child_die
, DW_AT_artificial
, cu
);
16536 if (attr
!= nullptr)
16537 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = attr
->as_boolean ();
16539 TYPE_FIELD_ARTIFICIAL (ftype
, iparams
) = 0;
16540 arg_type
= die_type (child_die
, cu
);
16542 /* RealView does not mark THIS as const, which the testsuite
16543 expects. GCC marks THIS as const in method definitions,
16544 but not in the class specifications (GCC PR 43053). */
16545 if (cu
->per_cu
->lang
== language_cplus
16546 && !TYPE_CONST (arg_type
)
16547 && TYPE_FIELD_ARTIFICIAL (ftype
, iparams
))
16550 struct dwarf2_cu
*arg_cu
= cu
;
16551 const char *name
= dwarf2_name (child_die
, cu
);
16553 attr
= dwarf2_attr (die
, DW_AT_object_pointer
, cu
);
16554 if (attr
!= nullptr)
16556 /* If the compiler emits this, use it. */
16557 if (follow_die_ref (die
, attr
, &arg_cu
) == child_die
)
16560 else if (name
&& strcmp (name
, "this") == 0)
16561 /* Function definitions will have the argument names. */
16563 else if (name
== NULL
&& iparams
== 0)
16564 /* Declarations may not have the names, so like
16565 elsewhere in GDB, assume an artificial first
16566 argument is "this". */
16570 arg_type
= make_cv_type (1, TYPE_VOLATILE (arg_type
),
16574 ftype
->field (iparams
).set_type (arg_type
);
16577 child_die
= child_die
->sibling
;
16584 static struct type
*
16585 read_typedef (struct die_info
*die
, struct dwarf2_cu
*cu
)
16587 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
16588 const char *name
= NULL
;
16589 struct type
*this_type
, *target_type
;
16591 name
= dwarf2_full_name (NULL
, die
, cu
);
16592 this_type
= init_type (objfile
, TYPE_CODE_TYPEDEF
, 0, name
);
16593 this_type
->set_target_is_stub (true);
16594 set_die_type (die
, this_type
, cu
);
16595 target_type
= die_type (die
, cu
);
16596 if (target_type
!= this_type
)
16597 TYPE_TARGET_TYPE (this_type
) = target_type
;
16600 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16601 spec and cause infinite loops in GDB. */
16602 complaint (_("Self-referential DW_TAG_typedef "
16603 "- DIE at %s [in module %s]"),
16604 sect_offset_str (die
->sect_off
), objfile_name (objfile
));
16605 TYPE_TARGET_TYPE (this_type
) = NULL
;
16609 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16610 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16611 Handle these by just returning the target type, rather than
16612 constructing an anonymous typedef type and trying to handle this
16614 set_die_type (die
, target_type
, cu
);
16615 return target_type
;
16620 /* Helper for get_dwarf2_rational_constant that computes the value of
16621 a given gmp_mpz given an attribute. */
16624 get_mpz (struct dwarf2_cu
*cu
, gdb_mpz
*value
, struct attribute
*attr
)
16626 /* GCC will sometimes emit a 16-byte constant value as a DWARF
16627 location expression that pushes an implicit value. */
16628 if (attr
->form
== DW_FORM_exprloc
)
16630 dwarf_block
*blk
= attr
->as_block ();
16631 if (blk
->size
> 0 && blk
->data
[0] == DW_OP_implicit_value
)
16634 const gdb_byte
*ptr
= safe_read_uleb128 (blk
->data
+ 1,
16635 blk
->data
+ blk
->size
,
16637 if (ptr
- blk
->data
+ len
<= blk
->size
)
16639 mpz_import (value
->val
, len
,
16640 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16646 /* On failure set it to 1. */
16647 *value
= gdb_mpz (1);
16649 else if (attr
->form_is_block ())
16651 dwarf_block
*blk
= attr
->as_block ();
16652 mpz_import (value
->val
, blk
->size
,
16653 bfd_big_endian (cu
->per_objfile
->objfile
->obfd
) ? 1 : -1,
16654 1, 0, 0, blk
->data
);
16657 *value
= gdb_mpz (attr
->constant_value (1));
16660 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
16661 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
16663 If the numerator and/or numerator attribute is missing,
16664 a complaint is filed, and NUMERATOR and DENOMINATOR are left
16668 get_dwarf2_rational_constant (struct die_info
*die
, struct dwarf2_cu
*cu
,
16669 gdb_mpz
*numerator
, gdb_mpz
*denominator
)
16671 struct attribute
*num_attr
, *denom_attr
;
16673 num_attr
= dwarf2_attr (die
, DW_AT_GNU_numerator
, cu
);
16674 if (num_attr
== nullptr)
16675 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
16676 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16678 denom_attr
= dwarf2_attr (die
, DW_AT_GNU_denominator
, cu
);
16679 if (denom_attr
== nullptr)
16680 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
16681 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16683 if (num_attr
== nullptr || denom_attr
== nullptr)
16686 get_mpz (cu
, numerator
, num_attr
);
16687 get_mpz (cu
, denominator
, denom_attr
);
16690 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
16691 rational constant, rather than a signed one.
16693 If the rational constant has a negative value, a complaint
16694 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
16697 get_dwarf2_unsigned_rational_constant (struct die_info
*die
,
16698 struct dwarf2_cu
*cu
,
16699 gdb_mpz
*numerator
,
16700 gdb_mpz
*denominator
)
16705 get_dwarf2_rational_constant (die
, cu
, &num
, &denom
);
16706 if (mpz_sgn (num
.val
) == -1 && mpz_sgn (denom
.val
) == -1)
16708 mpz_neg (num
.val
, num
.val
);
16709 mpz_neg (denom
.val
, denom
.val
);
16711 else if (mpz_sgn (num
.val
) == -1)
16713 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
16715 sect_offset_str (die
->sect_off
));
16718 else if (mpz_sgn (denom
.val
) == -1)
16720 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
16722 sect_offset_str (die
->sect_off
));
16726 *numerator
= std::move (num
);
16727 *denominator
= std::move (denom
);
16730 /* Assuming that ENCODING is a string whose contents starting at the
16731 K'th character is "_nn" where "nn" is a decimal number, scan that
16732 number and set RESULT to the value. K is updated to point to the
16733 character immediately following the number.
16735 If the string does not conform to the format described above, false
16736 is returned, and K may or may not be changed. */
16739 ada_get_gnat_encoded_number (const char *encoding
, int &k
, gdb_mpz
*result
)
16741 /* The next character should be an underscore ('_') followed
16743 if (encoding
[k
] != '_' || !isdigit (encoding
[k
+ 1]))
16746 /* Skip the underscore. */
16750 /* Determine the number of digits for our number. */
16751 while (isdigit (encoding
[k
]))
16756 std::string
copy (&encoding
[start
], k
- start
);
16757 if (mpz_set_str (result
->val
, copy
.c_str (), 10) == -1)
16763 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
16764 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
16765 DENOM, update OFFSET, and return true on success. Return false on
16769 ada_get_gnat_encoded_ratio (const char *encoding
, int &offset
,
16770 gdb_mpz
*num
, gdb_mpz
*denom
)
16772 if (!ada_get_gnat_encoded_number (encoding
, offset
, num
))
16774 return ada_get_gnat_encoded_number (encoding
, offset
, denom
);
16777 /* Assuming DIE corresponds to a fixed point type, finish the creation
16778 of the corresponding TYPE by setting its type-specific data. CU is
16779 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
16780 encodings. It is nullptr if the GNAT encoding should be
16784 finish_fixed_point_type (struct type
*type
, const char *suffix
,
16785 struct die_info
*die
, struct dwarf2_cu
*cu
)
16787 gdb_assert (type
->code () == TYPE_CODE_FIXED_POINT
16788 && TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_FIXED_POINT
);
16790 /* If GNAT encodings are preferred, don't examine the
16792 struct attribute
*attr
= nullptr;
16793 if (suffix
== nullptr)
16795 attr
= dwarf2_attr (die
, DW_AT_binary_scale
, cu
);
16796 if (attr
== nullptr)
16797 attr
= dwarf2_attr (die
, DW_AT_decimal_scale
, cu
);
16798 if (attr
== nullptr)
16799 attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16802 /* Numerator and denominator of our fixed-point type's scaling factor.
16803 The default is a scaling factor of 1, which we use as a fallback
16804 when we are not able to decode it (problem with the debugging info,
16805 unsupported forms, bug in GDB, etc...). Using that as the default
16806 allows us to at least print the unscaled value, which might still
16807 be useful to a user. */
16808 gdb_mpz
scale_num (1);
16809 gdb_mpz
scale_denom (1);
16811 if (attr
== nullptr)
16814 if (suffix
!= nullptr
16815 && ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16817 /* The number might be encoded as _nn_dd_nn_dd, where the
16818 second ratio is the 'small value. In this situation, we
16819 want the second value. */
16820 && (suffix
[offset
] != '_'
16821 || ada_get_gnat_encoded_ratio (suffix
, offset
, &scale_num
,
16828 /* Scaling factor not found. Assume a scaling factor of 1,
16829 and hope for the best. At least the user will be able to
16830 see the encoded value. */
16833 complaint (_("no scale found for fixed-point type (DIE at %s)"),
16834 sect_offset_str (die
->sect_off
));
16837 else if (attr
->name
== DW_AT_binary_scale
)
16839 LONGEST scale_exp
= attr
->constant_value (0);
16840 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16842 mpz_mul_2exp (num_or_denom
->val
, num_or_denom
->val
, std::abs (scale_exp
));
16844 else if (attr
->name
== DW_AT_decimal_scale
)
16846 LONGEST scale_exp
= attr
->constant_value (0);
16847 gdb_mpz
*num_or_denom
= scale_exp
> 0 ? &scale_num
: &scale_denom
;
16849 mpz_ui_pow_ui (num_or_denom
->val
, 10, std::abs (scale_exp
));
16851 else if (attr
->name
== DW_AT_small
)
16853 struct die_info
*scale_die
;
16854 struct dwarf2_cu
*scale_cu
= cu
;
16856 scale_die
= follow_die_ref (die
, attr
, &scale_cu
);
16857 if (scale_die
->tag
== DW_TAG_constant
)
16858 get_dwarf2_unsigned_rational_constant (scale_die
, scale_cu
,
16859 &scale_num
, &scale_denom
);
16861 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
16863 dwarf_tag_name (die
->tag
), sect_offset_str (die
->sect_off
));
16867 complaint (_("unsupported scale attribute %s for fixed-point type"
16869 dwarf_attr_name (attr
->name
),
16870 sect_offset_str (die
->sect_off
));
16873 gdb_mpq
&scaling_factor
= type
->fixed_point_info ().scaling_factor
;
16874 mpz_set (mpq_numref (scaling_factor
.val
), scale_num
.val
);
16875 mpz_set (mpq_denref (scaling_factor
.val
), scale_denom
.val
);
16876 mpq_canonicalize (scaling_factor
.val
);
16879 /* The gnat-encoding suffix for fixed point. */
16881 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
16883 /* If NAME encodes an Ada fixed-point type, return a pointer to the
16884 "XF" suffix of the name. The text after this is what encodes the
16885 'small and 'delta information. Otherwise, return nullptr. */
16887 static const char *
16888 gnat_encoded_fixed_point_type_info (const char *name
)
16890 return strstr (name
, GNAT_FIXED_POINT_SUFFIX
);
16893 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16894 (which may be different from NAME) to the architecture back-end to allow
16895 it to guess the correct format if necessary. */
16897 static struct type
*
16898 dwarf2_init_float_type (struct objfile
*objfile
, int bits
, const char *name
,
16899 const char *name_hint
, enum bfd_endian byte_order
)
16901 struct gdbarch
*gdbarch
= objfile
->arch ();
16902 const struct floatformat
**format
;
16905 format
= gdbarch_floatformat_for_type (gdbarch
, name_hint
, bits
);
16907 type
= init_float_type (objfile
, bits
, name
, format
, byte_order
);
16909 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
16914 /* Allocate an integer type of size BITS and name NAME. */
16916 static struct type
*
16917 dwarf2_init_integer_type (struct dwarf2_cu
*cu
, struct objfile
*objfile
,
16918 int bits
, int unsigned_p
, const char *name
)
16922 /* Versions of Intel's C Compiler generate an integer type called "void"
16923 instead of using DW_TAG_unspecified_type. This has been seen on
16924 at least versions 14, 17, and 18. */
16925 if (bits
== 0 && producer_is_icc (cu
) && name
!= nullptr
16926 && strcmp (name
, "void") == 0)
16927 type
= objfile_type (objfile
)->builtin_void
;
16929 type
= init_integer_type (objfile
, bits
, unsigned_p
, name
);
16934 /* Return true if DIE has a DW_AT_small attribute whose value is
16935 a constant rational, where both the numerator and denominator
16938 CU is the DIE's Compilation Unit. */
16941 has_zero_over_zero_small_attribute (struct die_info
*die
,
16942 struct dwarf2_cu
*cu
)
16944 struct attribute
*attr
= dwarf2_attr (die
, DW_AT_small
, cu
);
16945 if (attr
== nullptr)
16948 struct dwarf2_cu
*scale_cu
= cu
;
16949 struct die_info
*scale_die
16950 = follow_die_ref (die
, attr
, &scale_cu
);
16952 if (scale_die
->tag
!= DW_TAG_constant
)
16955 gdb_mpz
num (1), denom (1);
16956 get_dwarf2_rational_constant (scale_die
, cu
, &num
, &denom
);
16957 return mpz_sgn (num
.val
) == 0 && mpz_sgn (denom
.val
) == 0;
16960 /* Initialise and return a floating point type of size BITS suitable for
16961 use as a component of a complex number. The NAME_HINT is passed through
16962 when initialising the floating point type and is the name of the complex
16965 As DWARF doesn't currently provide an explicit name for the components
16966 of a complex number, but it can be helpful to have these components
16967 named, we try to select a suitable name based on the size of the
16969 static struct type
*
16970 dwarf2_init_complex_target_type (struct dwarf2_cu
*cu
,
16971 struct objfile
*objfile
,
16972 int bits
, const char *name_hint
,
16973 enum bfd_endian byte_order
)
16975 gdbarch
*gdbarch
= objfile
->arch ();
16976 struct type
*tt
= nullptr;
16978 /* Try to find a suitable floating point builtin type of size BITS.
16979 We're going to use the name of this type as the name for the complex
16980 target type that we are about to create. */
16981 switch (cu
->per_cu
->lang
)
16983 case language_fortran
:
16987 tt
= builtin_f_type (gdbarch
)->builtin_real
;
16990 tt
= builtin_f_type (gdbarch
)->builtin_real_s8
;
16992 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16994 tt
= builtin_f_type (gdbarch
)->builtin_real_s16
;
17002 tt
= builtin_type (gdbarch
)->builtin_float
;
17005 tt
= builtin_type (gdbarch
)->builtin_double
;
17007 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17009 tt
= builtin_type (gdbarch
)->builtin_long_double
;
17015 /* If the type we found doesn't match the size we were looking for, then
17016 pretend we didn't find a type at all, the complex target type we
17017 create will then be nameless. */
17018 if (tt
!= nullptr && TYPE_LENGTH (tt
) * TARGET_CHAR_BIT
!= bits
)
17021 const char *name
= (tt
== nullptr) ? nullptr : tt
->name ();
17022 return dwarf2_init_float_type (objfile
, bits
, name
, name_hint
, byte_order
);
17025 /* Find a representation of a given base type and install
17026 it in the TYPE field of the die. */
17028 static struct type
*
17029 read_base_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17031 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17033 struct attribute
*attr
;
17034 int encoding
= 0, bits
= 0;
17038 attr
= dwarf2_attr (die
, DW_AT_encoding
, cu
);
17039 if (attr
!= nullptr && attr
->form_is_constant ())
17040 encoding
= attr
->constant_value (0);
17041 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17042 if (attr
!= nullptr)
17043 bits
= attr
->constant_value (0) * TARGET_CHAR_BIT
;
17044 name
= dwarf2_name (die
, cu
);
17046 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17048 arch
= objfile
->arch ();
17049 enum bfd_endian byte_order
= gdbarch_byte_order (arch
);
17051 attr
= dwarf2_attr (die
, DW_AT_endianity
, cu
);
17052 if (attr
!= nullptr && attr
->form_is_constant ())
17054 int endianity
= attr
->constant_value (0);
17059 byte_order
= BFD_ENDIAN_BIG
;
17061 case DW_END_little
:
17062 byte_order
= BFD_ENDIAN_LITTLE
;
17065 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity
);
17070 if ((encoding
== DW_ATE_signed_fixed
|| encoding
== DW_ATE_unsigned_fixed
)
17071 && cu
->per_cu
->lang
== language_ada
17072 && has_zero_over_zero_small_attribute (die
, cu
))
17074 /* brobecker/2018-02-24: This is a fixed point type for which
17075 the scaling factor is represented as fraction whose value
17076 does not make sense (zero divided by zero), so we should
17077 normally never see these. However, there is a small category
17078 of fixed point types for which GNAT is unable to provide
17079 the scaling factor via the standard DWARF mechanisms, and
17080 for which the info is provided via the GNAT encodings instead.
17081 This is likely what this DIE is about. */
17082 encoding
= (encoding
== DW_ATE_signed_fixed
17084 : DW_ATE_unsigned
);
17087 /* With GNAT encodings, fixed-point information will be encoded in
17088 the type name. Note that this can also occur with the above
17089 zero-over-zero case, which is why this is a separate "if" rather
17090 than an "else if". */
17091 const char *gnat_encoding_suffix
= nullptr;
17092 if ((encoding
== DW_ATE_signed
|| encoding
== DW_ATE_unsigned
)
17093 && cu
->per_cu
->lang
== language_ada
17094 && name
!= nullptr)
17096 gnat_encoding_suffix
= gnat_encoded_fixed_point_type_info (name
);
17097 if (gnat_encoding_suffix
!= nullptr)
17099 gdb_assert (startswith (gnat_encoding_suffix
,
17100 GNAT_FIXED_POINT_SUFFIX
));
17101 name
= obstack_strndup (&cu
->per_objfile
->objfile
->objfile_obstack
,
17102 name
, gnat_encoding_suffix
- name
);
17103 /* Use -1 here so that SUFFIX points at the "_" after the
17105 gnat_encoding_suffix
+= strlen (GNAT_FIXED_POINT_SUFFIX
) - 1;
17107 encoding
= (encoding
== DW_ATE_signed
17108 ? DW_ATE_signed_fixed
17109 : DW_ATE_unsigned_fixed
);
17115 case DW_ATE_address
:
17116 /* Turn DW_ATE_address into a void * pointer. */
17117 type
= init_type (objfile
, TYPE_CODE_VOID
, TARGET_CHAR_BIT
, NULL
);
17118 type
= init_pointer_type (objfile
, bits
, name
, type
);
17120 case DW_ATE_boolean
:
17121 type
= init_boolean_type (objfile
, bits
, 1, name
);
17123 case DW_ATE_complex_float
:
17124 type
= dwarf2_init_complex_target_type (cu
, objfile
, bits
/ 2, name
,
17126 if (type
->code () == TYPE_CODE_ERROR
)
17128 if (name
== nullptr)
17130 struct obstack
*obstack
17131 = &cu
->per_objfile
->objfile
->objfile_obstack
;
17132 name
= obconcat (obstack
, "_Complex ", type
->name (),
17135 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17138 type
= init_complex_type (name
, type
);
17140 case DW_ATE_decimal_float
:
17141 type
= init_decfloat_type (objfile
, bits
, name
);
17144 type
= dwarf2_init_float_type (objfile
, bits
, name
, name
, byte_order
);
17146 case DW_ATE_signed
:
17147 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17149 case DW_ATE_unsigned
:
17150 if (cu
->per_cu
->lang
== language_fortran
17152 && startswith (name
, "character("))
17153 type
= init_character_type (objfile
, bits
, 1, name
);
17155 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17157 case DW_ATE_signed_char
:
17158 if (cu
->per_cu
->lang
== language_ada
17159 || cu
->per_cu
->lang
== language_m2
17160 || cu
->per_cu
->lang
== language_pascal
17161 || cu
->per_cu
->lang
== language_fortran
)
17162 type
= init_character_type (objfile
, bits
, 0, name
);
17164 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 0, name
);
17166 case DW_ATE_unsigned_char
:
17167 if (cu
->per_cu
->lang
== language_ada
17168 || cu
->per_cu
->lang
== language_m2
17169 || cu
->per_cu
->lang
== language_pascal
17170 || cu
->per_cu
->lang
== language_fortran
17171 || cu
->per_cu
->lang
== language_rust
)
17172 type
= init_character_type (objfile
, bits
, 1, name
);
17174 type
= dwarf2_init_integer_type (cu
, objfile
, bits
, 1, name
);
17178 type
= init_character_type (objfile
, bits
, 1, name
);
17179 return set_die_type (die
, type
, cu
);
17182 case DW_ATE_signed_fixed
:
17183 type
= init_fixed_point_type (objfile
, bits
, 0, name
);
17184 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17186 case DW_ATE_unsigned_fixed
:
17187 type
= init_fixed_point_type (objfile
, bits
, 1, name
);
17188 finish_fixed_point_type (type
, gnat_encoding_suffix
, die
, cu
);
17192 complaint (_("unsupported DW_AT_encoding: '%s'"),
17193 dwarf_type_encoding_name (encoding
));
17194 type
= init_type (objfile
, TYPE_CODE_ERROR
, bits
, name
);
17198 if (type
->code () == TYPE_CODE_INT
17200 && strcmp (name
, "char") == 0)
17201 type
->set_has_no_signedness (true);
17203 maybe_set_alignment (cu
, die
, type
);
17205 type
->set_endianity_is_not_default (gdbarch_byte_order (arch
) != byte_order
);
17207 if (TYPE_SPECIFIC_FIELD (type
) == TYPE_SPECIFIC_INT
)
17209 attr
= dwarf2_attr (die
, DW_AT_bit_size
, cu
);
17210 if (attr
!= nullptr && attr
->as_unsigned () <= 8 * TYPE_LENGTH (type
))
17212 unsigned real_bit_size
= attr
->as_unsigned ();
17213 attr
= dwarf2_attr (die
, DW_AT_data_bit_offset
, cu
);
17214 /* Only use the attributes if they make sense together. */
17215 if (attr
== nullptr
17216 || (attr
->as_unsigned () + real_bit_size
17217 <= 8 * TYPE_LENGTH (type
)))
17219 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_size
17221 if (attr
!= nullptr)
17222 TYPE_MAIN_TYPE (type
)->type_specific
.int_stuff
.bit_offset
17223 = attr
->as_unsigned ();
17228 return set_die_type (die
, type
, cu
);
17231 /* A helper function that returns the name of DIE, if it refers to a
17232 variable declaration. */
17234 static const char *
17235 var_decl_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
17237 if (die
->tag
!= DW_TAG_variable
)
17240 attribute
*attr
= dwarf2_attr (die
, DW_AT_declaration
, cu
);
17241 if (attr
== nullptr || !attr
->as_boolean ())
17244 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
17245 if (attr
== nullptr)
17247 return attr
->as_string ();
17250 /* Parse dwarf attribute if it's a block, reference or constant and put the
17251 resulting value of the attribute into struct bound_prop.
17252 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17255 attr_to_dynamic_prop (const struct attribute
*attr
, struct die_info
*die
,
17256 struct dwarf2_cu
*cu
, struct dynamic_prop
*prop
,
17257 struct type
*default_type
)
17259 struct dwarf2_property_baton
*baton
;
17260 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
17261 struct objfile
*objfile
= per_objfile
->objfile
;
17262 struct obstack
*obstack
= &objfile
->objfile_obstack
;
17264 gdb_assert (default_type
!= NULL
);
17266 if (attr
== NULL
|| prop
== NULL
)
17269 if (attr
->form_is_block ())
17271 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17272 baton
->property_type
= default_type
;
17273 baton
->locexpr
.per_cu
= cu
->per_cu
;
17274 baton
->locexpr
.per_objfile
= per_objfile
;
17276 struct dwarf_block
*block
;
17277 if (attr
->form
== DW_FORM_data16
)
17279 size_t data_size
= 16;
17280 block
= XOBNEW (obstack
, struct dwarf_block
);
17281 block
->size
= (data_size
17282 + 2 /* Extra bytes for DW_OP and arg. */);
17283 gdb_byte
*data
= XOBNEWVEC (obstack
, gdb_byte
, block
->size
);
17284 data
[0] = DW_OP_implicit_value
;
17285 data
[1] = data_size
;
17286 memcpy (&data
[2], attr
->as_block ()->data
, data_size
);
17287 block
->data
= data
;
17290 block
= attr
->as_block ();
17292 baton
->locexpr
.size
= block
->size
;
17293 baton
->locexpr
.data
= block
->data
;
17294 switch (attr
->name
)
17296 case DW_AT_string_length
:
17297 baton
->locexpr
.is_reference
= true;
17300 baton
->locexpr
.is_reference
= false;
17304 prop
->set_locexpr (baton
);
17305 gdb_assert (prop
->baton () != NULL
);
17307 else if (attr
->form_is_ref ())
17309 struct dwarf2_cu
*target_cu
= cu
;
17310 struct die_info
*target_die
;
17311 struct attribute
*target_attr
;
17313 target_die
= follow_die_ref (die
, attr
, &target_cu
);
17314 target_attr
= dwarf2_attr (target_die
, DW_AT_location
, target_cu
);
17315 if (target_attr
== NULL
)
17316 target_attr
= dwarf2_attr (target_die
, DW_AT_data_member_location
,
17318 if (target_attr
== nullptr)
17319 target_attr
= dwarf2_attr (target_die
, DW_AT_data_bit_offset
,
17321 if (target_attr
== NULL
)
17323 const char *name
= var_decl_name (target_die
, target_cu
);
17324 if (name
!= nullptr)
17326 prop
->set_variable_name (name
);
17332 switch (target_attr
->name
)
17334 case DW_AT_location
:
17335 if (target_attr
->form_is_section_offset ())
17337 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17338 baton
->property_type
= die_type (target_die
, target_cu
);
17339 fill_in_loclist_baton (cu
, &baton
->loclist
, target_attr
);
17340 prop
->set_loclist (baton
);
17341 gdb_assert (prop
->baton () != NULL
);
17343 else if (target_attr
->form_is_block ())
17345 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17346 baton
->property_type
= die_type (target_die
, target_cu
);
17347 baton
->locexpr
.per_cu
= cu
->per_cu
;
17348 baton
->locexpr
.per_objfile
= per_objfile
;
17349 struct dwarf_block
*block
= target_attr
->as_block ();
17350 baton
->locexpr
.size
= block
->size
;
17351 baton
->locexpr
.data
= block
->data
;
17352 baton
->locexpr
.is_reference
= true;
17353 prop
->set_locexpr (baton
);
17354 gdb_assert (prop
->baton () != NULL
);
17358 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17359 "dynamic property");
17363 case DW_AT_data_member_location
:
17364 case DW_AT_data_bit_offset
:
17368 if (!handle_member_location (target_die
, target_cu
, &offset
))
17371 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17372 baton
->property_type
= read_type_die (target_die
->parent
,
17374 baton
->offset_info
.offset
= offset
;
17375 baton
->offset_info
.type
= die_type (target_die
, target_cu
);
17376 prop
->set_addr_offset (baton
);
17381 else if (attr
->form_is_constant ())
17382 prop
->set_const_val (attr
->constant_value (0));
17383 else if (attr
->form_is_section_offset ())
17385 switch (attr
->name
)
17387 case DW_AT_string_length
:
17388 baton
= XOBNEW (obstack
, struct dwarf2_property_baton
);
17389 baton
->property_type
= default_type
;
17390 fill_in_loclist_baton (cu
, &baton
->loclist
, attr
);
17391 prop
->set_loclist (baton
);
17392 gdb_assert (prop
->baton () != NULL
);
17404 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr
->form
),
17405 dwarf2_name (die
, cu
));
17411 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17412 present (which is valid) then compute the default type based on the
17413 compilation units address size. */
17415 static struct type
*
17416 read_subrange_index_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17418 struct type
*index_type
= die_type (die
, cu
);
17420 /* Dwarf-2 specifications explicitly allows to create subrange types
17421 without specifying a base type.
17422 In that case, the base type must be set to the type of
17423 the lower bound, upper bound or count, in that order, if any of these
17424 three attributes references an object that has a type.
17425 If no base type is found, the Dwarf-2 specifications say that
17426 a signed integer type of size equal to the size of an address should
17428 For the following C code: `extern char gdb_int [];'
17429 GCC produces an empty range DIE.
17430 FIXME: muller/2010-05-28: Possible references to object for low bound,
17431 high bound or count are not yet handled by this code. */
17432 if (index_type
->code () == TYPE_CODE_VOID
)
17433 index_type
= cu
->addr_sized_int_type (false);
17438 /* Read the given DW_AT_subrange DIE. */
17440 static struct type
*
17441 read_subrange_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17443 struct type
*base_type
, *orig_base_type
;
17444 struct type
*range_type
;
17445 struct attribute
*attr
;
17446 struct dynamic_prop low
, high
;
17447 int low_default_is_valid
;
17448 int high_bound_is_count
= 0;
17450 ULONGEST negative_mask
;
17452 orig_base_type
= read_subrange_index_type (die
, cu
);
17454 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17455 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17456 creating the range type, but we use the result of check_typedef
17457 when examining properties of the type. */
17458 base_type
= check_typedef (orig_base_type
);
17460 /* The die_type call above may have already set the type for this DIE. */
17461 range_type
= get_die_type (die
, cu
);
17465 high
.set_const_val (0);
17467 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17468 omitting DW_AT_lower_bound. */
17469 switch (cu
->per_cu
->lang
)
17472 case language_cplus
:
17473 low
.set_const_val (0);
17474 low_default_is_valid
= 1;
17476 case language_fortran
:
17477 low
.set_const_val (1);
17478 low_default_is_valid
= 1;
17481 case language_objc
:
17482 case language_rust
:
17483 low
.set_const_val (0);
17484 low_default_is_valid
= (cu
->header
.version
>= 4);
17488 case language_pascal
:
17489 low
.set_const_val (1);
17490 low_default_is_valid
= (cu
->header
.version
>= 4);
17493 low
.set_const_val (0);
17494 low_default_is_valid
= 0;
17498 attr
= dwarf2_attr (die
, DW_AT_lower_bound
, cu
);
17499 if (attr
!= nullptr)
17500 attr_to_dynamic_prop (attr
, die
, cu
, &low
, base_type
);
17501 else if (!low_default_is_valid
)
17502 complaint (_("Missing DW_AT_lower_bound "
17503 "- DIE at %s [in module %s]"),
17504 sect_offset_str (die
->sect_off
),
17505 objfile_name (cu
->per_objfile
->objfile
));
17507 struct attribute
*attr_ub
, *attr_count
;
17508 attr
= attr_ub
= dwarf2_attr (die
, DW_AT_upper_bound
, cu
);
17509 if (!attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17511 attr
= attr_count
= dwarf2_attr (die
, DW_AT_count
, cu
);
17512 if (attr_to_dynamic_prop (attr
, die
, cu
, &high
, base_type
))
17514 /* If bounds are constant do the final calculation here. */
17515 if (low
.kind () == PROP_CONST
&& high
.kind () == PROP_CONST
)
17516 high
.set_const_val (low
.const_val () + high
.const_val () - 1);
17518 high_bound_is_count
= 1;
17522 if (attr_ub
!= NULL
)
17523 complaint (_("Unresolved DW_AT_upper_bound "
17524 "- DIE at %s [in module %s]"),
17525 sect_offset_str (die
->sect_off
),
17526 objfile_name (cu
->per_objfile
->objfile
));
17527 if (attr_count
!= NULL
)
17528 complaint (_("Unresolved DW_AT_count "
17529 "- DIE at %s [in module %s]"),
17530 sect_offset_str (die
->sect_off
),
17531 objfile_name (cu
->per_objfile
->objfile
));
17536 struct attribute
*bias_attr
= dwarf2_attr (die
, DW_AT_GNU_bias
, cu
);
17537 if (bias_attr
!= nullptr && bias_attr
->form_is_constant ())
17538 bias
= bias_attr
->constant_value (0);
17540 /* Normally, the DWARF producers are expected to use a signed
17541 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17542 But this is unfortunately not always the case, as witnessed
17543 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17544 is used instead. To work around that ambiguity, we treat
17545 the bounds as signed, and thus sign-extend their values, when
17546 the base type is signed. */
17548 -((ULONGEST
) 1 << (TYPE_LENGTH (base_type
) * TARGET_CHAR_BIT
- 1));
17549 if (low
.kind () == PROP_CONST
17550 && !base_type
->is_unsigned () && (low
.const_val () & negative_mask
))
17551 low
.set_const_val (low
.const_val () | negative_mask
);
17552 if (high
.kind () == PROP_CONST
17553 && !base_type
->is_unsigned () && (high
.const_val () & negative_mask
))
17554 high
.set_const_val (high
.const_val () | negative_mask
);
17556 /* Check for bit and byte strides. */
17557 struct dynamic_prop byte_stride_prop
;
17558 attribute
*attr_byte_stride
= dwarf2_attr (die
, DW_AT_byte_stride
, cu
);
17559 if (attr_byte_stride
!= nullptr)
17561 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17562 attr_to_dynamic_prop (attr_byte_stride
, die
, cu
, &byte_stride_prop
,
17566 struct dynamic_prop bit_stride_prop
;
17567 attribute
*attr_bit_stride
= dwarf2_attr (die
, DW_AT_bit_stride
, cu
);
17568 if (attr_bit_stride
!= nullptr)
17570 /* It only makes sense to have either a bit or byte stride. */
17571 if (attr_byte_stride
!= nullptr)
17573 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17574 "- DIE at %s [in module %s]"),
17575 sect_offset_str (die
->sect_off
),
17576 objfile_name (cu
->per_objfile
->objfile
));
17577 attr_bit_stride
= nullptr;
17581 struct type
*prop_type
= cu
->addr_sized_int_type (false);
17582 attr_to_dynamic_prop (attr_bit_stride
, die
, cu
, &bit_stride_prop
,
17587 if (attr_byte_stride
!= nullptr
17588 || attr_bit_stride
!= nullptr)
17590 bool byte_stride_p
= (attr_byte_stride
!= nullptr);
17591 struct dynamic_prop
*stride
17592 = byte_stride_p
? &byte_stride_prop
: &bit_stride_prop
;
17595 = create_range_type_with_stride (NULL
, orig_base_type
, &low
,
17596 &high
, bias
, stride
, byte_stride_p
);
17599 range_type
= create_range_type (NULL
, orig_base_type
, &low
, &high
, bias
);
17601 if (high_bound_is_count
)
17602 range_type
->bounds ()->flag_upper_bound_is_count
= 1;
17604 /* Ada expects an empty array on no boundary attributes. */
17605 if (attr
== NULL
&& cu
->per_cu
->lang
!= language_ada
)
17606 range_type
->bounds ()->high
.set_undefined ();
17608 name
= dwarf2_name (die
, cu
);
17610 range_type
->set_name (name
);
17612 attr
= dwarf2_attr (die
, DW_AT_byte_size
, cu
);
17613 if (attr
!= nullptr)
17614 TYPE_LENGTH (range_type
) = attr
->constant_value (0);
17616 maybe_set_alignment (cu
, die
, range_type
);
17618 set_die_type (die
, range_type
, cu
);
17620 /* set_die_type should be already done. */
17621 set_descriptive_type (range_type
, die
, cu
);
17626 static struct type
*
17627 read_unspecified_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
17631 type
= init_type (cu
->per_objfile
->objfile
, TYPE_CODE_VOID
, 0, NULL
);
17632 type
->set_name (dwarf2_name (die
, cu
));
17634 /* In Ada, an unspecified type is typically used when the description
17635 of the type is deferred to a different unit. When encountering
17636 such a type, we treat it as a stub, and try to resolve it later on,
17638 if (cu
->per_cu
->lang
== language_ada
)
17639 type
->set_is_stub (true);
17641 return set_die_type (die
, type
, cu
);
17644 /* Read a single die and all its descendents. Set the die's sibling
17645 field to NULL; set other fields in the die correctly, and set all
17646 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17647 location of the info_ptr after reading all of those dies. PARENT
17648 is the parent of the die in question. */
17650 static struct die_info
*
17651 read_die_and_children (const struct die_reader_specs
*reader
,
17652 const gdb_byte
*info_ptr
,
17653 const gdb_byte
**new_info_ptr
,
17654 struct die_info
*parent
)
17656 struct die_info
*die
;
17657 const gdb_byte
*cur_ptr
;
17659 cur_ptr
= read_full_die_1 (reader
, &die
, info_ptr
, 0);
17662 *new_info_ptr
= cur_ptr
;
17665 store_in_ref_table (die
, reader
->cu
);
17667 if (die
->has_children
)
17668 die
->child
= read_die_and_siblings_1 (reader
, cur_ptr
, new_info_ptr
, die
);
17672 *new_info_ptr
= cur_ptr
;
17675 die
->sibling
= NULL
;
17676 die
->parent
= parent
;
17680 /* Read a die, all of its descendents, and all of its siblings; set
17681 all of the fields of all of the dies correctly. Arguments are as
17682 in read_die_and_children. */
17684 static struct die_info
*
17685 read_die_and_siblings_1 (const struct die_reader_specs
*reader
,
17686 const gdb_byte
*info_ptr
,
17687 const gdb_byte
**new_info_ptr
,
17688 struct die_info
*parent
)
17690 struct die_info
*first_die
, *last_sibling
;
17691 const gdb_byte
*cur_ptr
;
17693 cur_ptr
= info_ptr
;
17694 first_die
= last_sibling
= NULL
;
17698 struct die_info
*die
17699 = read_die_and_children (reader
, cur_ptr
, &cur_ptr
, parent
);
17703 *new_info_ptr
= cur_ptr
;
17710 last_sibling
->sibling
= die
;
17712 last_sibling
= die
;
17716 /* Read a die, all of its descendents, and all of its siblings; set
17717 all of the fields of all of the dies correctly. Arguments are as
17718 in read_die_and_children.
17719 This the main entry point for reading a DIE and all its children. */
17721 static struct die_info
*
17722 read_die_and_siblings (const struct die_reader_specs
*reader
,
17723 const gdb_byte
*info_ptr
,
17724 const gdb_byte
**new_info_ptr
,
17725 struct die_info
*parent
)
17727 struct die_info
*die
= read_die_and_siblings_1 (reader
, info_ptr
,
17728 new_info_ptr
, parent
);
17730 if (dwarf_die_debug
)
17732 gdb_printf (gdb_stdlog
,
17733 "Read die from %s@0x%x of %s:\n",
17734 reader
->die_section
->get_name (),
17735 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17736 bfd_get_filename (reader
->abfd
));
17737 dump_die (die
, dwarf_die_debug
);
17743 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17745 The caller is responsible for filling in the extra attributes
17746 and updating (*DIEP)->num_attrs.
17747 Set DIEP to point to a newly allocated die with its information,
17748 except for its child, sibling, and parent fields. */
17750 static const gdb_byte
*
17751 read_full_die_1 (const struct die_reader_specs
*reader
,
17752 struct die_info
**diep
, const gdb_byte
*info_ptr
,
17753 int num_extra_attrs
)
17755 unsigned int abbrev_number
, bytes_read
, i
;
17756 const struct abbrev_info
*abbrev
;
17757 struct die_info
*die
;
17758 struct dwarf2_cu
*cu
= reader
->cu
;
17759 bfd
*abfd
= reader
->abfd
;
17761 sect_offset sect_off
= (sect_offset
) (info_ptr
- reader
->buffer
);
17762 abbrev_number
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
17763 info_ptr
+= bytes_read
;
17764 if (!abbrev_number
)
17770 abbrev
= reader
->abbrev_table
->lookup_abbrev (abbrev_number
);
17772 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17774 bfd_get_filename (abfd
));
17776 die
= dwarf_alloc_die (cu
, abbrev
->num_attrs
+ num_extra_attrs
);
17777 die
->sect_off
= sect_off
;
17778 die
->tag
= abbrev
->tag
;
17779 die
->abbrev
= abbrev_number
;
17780 die
->has_children
= abbrev
->has_children
;
17782 /* Make the result usable.
17783 The caller needs to update num_attrs after adding the extra
17785 die
->num_attrs
= abbrev
->num_attrs
;
17787 bool any_need_reprocess
= false;
17788 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17790 info_ptr
= read_attribute (reader
, &die
->attrs
[i
], &abbrev
->attrs
[i
],
17792 if (die
->attrs
[i
].requires_reprocessing_p ())
17793 any_need_reprocess
= true;
17796 struct attribute
*attr
= die
->attr (DW_AT_str_offsets_base
);
17797 if (attr
!= nullptr && attr
->form_is_unsigned ())
17798 cu
->str_offsets_base
= attr
->as_unsigned ();
17800 attr
= die
->attr (DW_AT_loclists_base
);
17801 if (attr
!= nullptr)
17802 cu
->loclist_base
= attr
->as_unsigned ();
17804 auto maybe_addr_base
= die
->addr_base ();
17805 if (maybe_addr_base
.has_value ())
17806 cu
->addr_base
= *maybe_addr_base
;
17808 attr
= die
->attr (DW_AT_rnglists_base
);
17809 if (attr
!= nullptr)
17810 cu
->rnglists_base
= attr
->as_unsigned ();
17812 if (any_need_reprocess
)
17814 for (i
= 0; i
< abbrev
->num_attrs
; ++i
)
17816 if (die
->attrs
[i
].requires_reprocessing_p ())
17817 read_attribute_reprocess (reader
, &die
->attrs
[i
], die
->tag
);
17824 /* Read a die and all its attributes.
17825 Set DIEP to point to a newly allocated die with its information,
17826 except for its child, sibling, and parent fields. */
17828 static const gdb_byte
*
17829 read_full_die (const struct die_reader_specs
*reader
,
17830 struct die_info
**diep
, const gdb_byte
*info_ptr
)
17832 const gdb_byte
*result
;
17834 result
= read_full_die_1 (reader
, diep
, info_ptr
, 0);
17836 if (dwarf_die_debug
)
17838 gdb_printf (gdb_stdlog
,
17839 "Read die from %s@0x%x of %s:\n",
17840 reader
->die_section
->get_name (),
17841 (unsigned) (info_ptr
- reader
->die_section
->buffer
),
17842 bfd_get_filename (reader
->abfd
));
17843 dump_die (*diep
, dwarf_die_debug
);
17851 cooked_indexer::check_bounds (cutu_reader
*reader
)
17853 if (reader
->cu
->per_cu
->addresses_seen
)
17856 dwarf2_cu
*cu
= reader
->cu
;
17858 CORE_ADDR best_lowpc
= 0, best_highpc
= 0;
17859 /* Possibly set the default values of LOWPC and HIGHPC from
17861 dwarf2_find_base_address (reader
->comp_unit_die
, cu
);
17862 enum pc_bounds_kind cu_bounds_kind
17863 = dwarf2_get_pc_bounds (reader
->comp_unit_die
, &best_lowpc
, &best_highpc
,
17864 cu
, m_index_storage
->get_addrmap (), cu
->per_cu
);
17865 if (cu_bounds_kind
== PC_BOUNDS_HIGH_LOW
&& best_lowpc
< best_highpc
)
17867 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
17868 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
17869 struct gdbarch
*gdbarch
= objfile
->arch ();
17871 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_lowpc
+ baseaddr
)
17874 = (gdbarch_adjust_dwarf2_addr (gdbarch
, best_highpc
+ baseaddr
)
17876 /* Store the contiguous range if it is not empty; it can be
17877 empty for CUs with no code. */
17878 addrmap_set_empty (m_index_storage
->get_addrmap (), low
, high
,
17881 cu
->per_cu
->addresses_seen
= true;
17885 /* Helper function that returns true if TAG can have a linkage
17889 tag_can_have_linkage_name (enum dwarf_tag tag
)
17893 /* We include types here because an anonymous C++ type might
17894 have a name for linkage purposes. */
17895 case DW_TAG_class_type
:
17896 case DW_TAG_structure_type
:
17897 case DW_TAG_union_type
:
17898 case DW_TAG_variable
:
17899 case DW_TAG_subprogram
:
17908 cooked_indexer::ensure_cu_exists (cutu_reader
*reader
,
17909 dwarf2_per_objfile
*per_objfile
,
17910 sect_offset sect_off
, bool is_dwz
,
17913 /* Lookups for type unit references are always in the CU, and
17914 cross-CU references will crash. */
17915 if (reader
->cu
->per_cu
->is_dwz
== is_dwz
17916 && reader
->cu
->header
.offset_in_cu_p (sect_off
))
17919 dwarf2_per_cu_data
*per_cu
17920 = dwarf2_find_containing_comp_unit (sect_off
, is_dwz
,
17921 per_objfile
->per_bfd
);
17923 /* When scanning, we only want to visit a given CU a single time.
17924 Doing this check here avoids self-imports as well. */
17928 if (!per_cu
->scanned
.compare_exchange_strong (nope
, true))
17931 if (per_cu
== m_per_cu
)
17934 cutu_reader
*result
= m_index_storage
->get_reader (per_cu
);
17935 if (result
== nullptr)
17937 cutu_reader
new_reader (per_cu
, per_objfile
, nullptr, nullptr, false,
17938 m_index_storage
->get_abbrev_cache ());
17940 prepare_one_comp_unit (new_reader
.cu
, new_reader
.comp_unit_die
,
17942 std::unique_ptr
<cutu_reader
> copy
17943 (new cutu_reader (std::move (new_reader
)));
17944 result
= m_index_storage
->preserve (std::move (copy
));
17947 if (result
->dummy_p
|| !result
->comp_unit_die
->has_children
)
17951 check_bounds (result
);
17957 cooked_indexer::scan_attributes (dwarf2_per_cu_data
*scanning_per_cu
,
17958 cutu_reader
*reader
,
17959 const gdb_byte
*watermark_ptr
,
17960 const gdb_byte
*info_ptr
,
17961 const abbrev_info
*abbrev
,
17963 const char **linkage_name
,
17964 cooked_index_flag
*flags
,
17965 sect_offset
*sibling_offset
,
17966 const cooked_index_entry
**parent_entry
,
17967 CORE_ADDR
*maybe_defer
,
17968 bool for_specification
)
17970 bool origin_is_dwz
= false;
17971 bool is_declaration
= false;
17972 sect_offset origin_offset
{};
17974 gdb::optional
<CORE_ADDR
> low_pc
;
17975 gdb::optional
<CORE_ADDR
> high_pc
;
17976 bool high_pc_relative
= false;
17978 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
17981 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
17982 if (attr
.requires_reprocessing_p ())
17983 read_attribute_reprocess (reader
, &attr
, abbrev
->tag
);
17985 /* Store the data if it is of an attribute we want to keep in a
17986 partial symbol table. */
17990 switch (abbrev
->tag
)
17992 case DW_TAG_compile_unit
:
17993 case DW_TAG_partial_unit
:
17994 case DW_TAG_type_unit
:
17995 /* Compilation units have a DW_AT_name that is a filename, not
17996 a source language identifier. */
18000 if (*name
== nullptr)
18001 *name
= attr
.as_string ();
18006 case DW_AT_linkage_name
:
18007 case DW_AT_MIPS_linkage_name
:
18008 /* Note that both forms of linkage name might appear. We
18009 assume they will be the same, and we only store the last
18011 if (*linkage_name
== nullptr)
18012 *linkage_name
= attr
.as_string ();
18015 case DW_AT_main_subprogram
:
18016 if (attr
.as_boolean ())
18020 case DW_AT_declaration
:
18021 is_declaration
= attr
.as_boolean ();
18024 case DW_AT_sibling
:
18025 if (sibling_offset
!= nullptr)
18026 *sibling_offset
= attr
.get_ref_die_offset ();
18029 case DW_AT_specification
:
18030 case DW_AT_abstract_origin
:
18031 case DW_AT_extension
:
18032 origin_offset
= attr
.get_ref_die_offset ();
18033 origin_is_dwz
= attr
.form
== DW_FORM_GNU_ref_alt
;
18036 case DW_AT_external
:
18037 if (attr
.as_boolean ())
18038 *flags
&= ~IS_STATIC
;
18041 case DW_AT_enum_class
:
18042 if (attr
.as_boolean ())
18043 *flags
|= IS_ENUM_CLASS
;
18047 low_pc
= attr
.as_address ();
18050 case DW_AT_high_pc
:
18051 high_pc
= attr
.as_address ();
18052 if (reader
->cu
->header
.version
>= 4 && attr
.form_is_constant ())
18053 high_pc_relative
= true;
18056 case DW_AT_location
:
18057 if (!scanning_per_cu
->addresses_seen
&& attr
.form_is_block ())
18059 struct dwarf_block
*locdesc
= attr
.as_block ();
18060 CORE_ADDR addr
= decode_locdesc (locdesc
, reader
->cu
);
18062 || reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
)
18065 /* For variables, we don't want to try decoding the
18066 type just to find the size -- for gdb's purposes
18067 we only need the address of a variable. */
18068 high_pc
= addr
+ 1;
18069 high_pc_relative
= false;
18075 if (!scanning_per_cu
->addresses_seen
)
18077 /* Offset in the .debug_ranges or .debug_rnglist section
18078 (depending on DWARF version). */
18079 ULONGEST ranges_offset
= attr
.as_unsigned ();
18081 /* See dwarf2_cu::gnu_ranges_base's doc for why we might
18082 want to add this value. */
18083 ranges_offset
+= reader
->cu
->gnu_ranges_base
;
18085 CORE_ADDR lowpc
, highpc
;
18086 dwarf2_ranges_read (ranges_offset
, &lowpc
, &highpc
, reader
->cu
,
18087 m_index_storage
->get_addrmap (),
18088 scanning_per_cu
, abbrev
->tag
);
18094 /* We don't want to examine declarations, but if we found a
18095 declaration when handling DW_AT_specification or the like, then
18096 that is ok. Similarly, we allow an external variable without a
18097 location; those are resolved via minimal symbols. */
18098 if (is_declaration
&& !for_specification
18099 && (abbrev
->tag
!= DW_TAG_variable
18100 || (*flags
& IS_STATIC
) != 0))
18102 *linkage_name
= nullptr;
18105 else if ((*name
== nullptr
18106 || (*linkage_name
== nullptr
18107 && tag_can_have_linkage_name (abbrev
->tag
))
18108 || (*parent_entry
== nullptr && m_language
!= language_c
))
18109 && origin_offset
!= sect_offset (0))
18111 cutu_reader
*new_reader
18112 = ensure_cu_exists (reader
, reader
->cu
->per_objfile
, origin_offset
,
18113 origin_is_dwz
, false);
18114 if (new_reader
!= nullptr)
18116 const gdb_byte
*new_info_ptr
= (new_reader
->buffer
18117 + to_underlying (origin_offset
));
18119 if (new_reader
->cu
== reader
->cu
18120 && new_info_ptr
> watermark_ptr
18121 && maybe_defer
!= nullptr
18122 && *parent_entry
== nullptr)
18123 *maybe_defer
= form_addr (origin_offset
, origin_is_dwz
);
18124 else if (*parent_entry
== nullptr)
18126 CORE_ADDR lookup
= form_addr (origin_offset
, origin_is_dwz
);
18128 = (cooked_index_entry
*) addrmap_find (m_die_range_map
,
18132 unsigned int bytes_read
;
18133 const abbrev_info
*new_abbrev
= peek_die_abbrev (*new_reader
,
18136 new_info_ptr
+= bytes_read
;
18137 scan_attributes (scanning_per_cu
, new_reader
, new_info_ptr
, new_info_ptr
,
18138 new_abbrev
, name
, linkage_name
, flags
, nullptr,
18139 parent_entry
, maybe_defer
, true);
18143 if (!for_specification
)
18145 if (m_language
== language_ada
18146 && *linkage_name
== nullptr)
18147 *linkage_name
= *name
;
18149 if (!scanning_per_cu
->addresses_seen
18150 && low_pc
.has_value ()
18151 && (reader
->cu
->per_objfile
->per_bfd
->has_section_at_zero
18153 && high_pc
.has_value ())
18155 if (high_pc_relative
)
18156 high_pc
= *high_pc
+ *low_pc
;
18158 if (*high_pc
> *low_pc
)
18160 struct objfile
*objfile
= reader
->cu
->per_objfile
->objfile
;
18161 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18162 struct gdbarch
*gdbarch
= objfile
->arch ();
18164 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *low_pc
+ baseaddr
)
18167 = (gdbarch_adjust_dwarf2_addr (gdbarch
, *high_pc
+ baseaddr
)
18169 addrmap_set_empty (m_index_storage
->get_addrmap (), lo
, hi
- 1,
18174 if (abbrev
->tag
== DW_TAG_module
|| abbrev
->tag
== DW_TAG_namespace
)
18175 *flags
&= ~IS_STATIC
;
18177 if (abbrev
->tag
== DW_TAG_namespace
&& *name
== nullptr)
18178 *name
= "(anonymous namespace)";
18180 if (m_language
== language_cplus
18181 && (abbrev
->tag
== DW_TAG_class_type
18182 || abbrev
->tag
== DW_TAG_interface_type
18183 || abbrev
->tag
== DW_TAG_structure_type
18184 || abbrev
->tag
== DW_TAG_union_type
18185 || abbrev
->tag
== DW_TAG_enumeration_type
18186 || abbrev
->tag
== DW_TAG_enumerator
))
18187 *flags
&= ~IS_STATIC
;
18194 cooked_indexer::index_imported_unit (cutu_reader
*reader
,
18195 const gdb_byte
*info_ptr
,
18196 const abbrev_info
*abbrev
)
18198 sect_offset sect_off
{};
18199 bool is_dwz
= false;
18201 for (int i
= 0; i
< abbrev
->num_attrs
; ++i
)
18203 /* Note that we never need to reprocess attributes here. */
18205 info_ptr
= read_attribute (reader
, &attr
, &abbrev
->attrs
[i
], info_ptr
);
18207 if (attr
.name
== DW_AT_import
)
18209 sect_off
= attr
.get_ref_die_offset ();
18210 is_dwz
= (attr
.form
== DW_FORM_GNU_ref_alt
18211 || reader
->cu
->per_cu
->is_dwz
);
18215 /* Did not find DW_AT_import. */
18216 if (sect_off
== sect_offset (0))
18219 dwarf2_per_objfile
*per_objfile
= reader
->cu
->per_objfile
;
18220 cutu_reader
*new_reader
= ensure_cu_exists (reader
, per_objfile
, sect_off
,
18222 if (new_reader
!= nullptr)
18224 index_dies (new_reader
, new_reader
->info_ptr
, nullptr, false);
18226 reader
->cu
->add_dependence (new_reader
->cu
->per_cu
);
18233 cooked_indexer::recurse (cutu_reader
*reader
,
18234 const gdb_byte
*info_ptr
,
18235 const cooked_index_entry
*parent_entry
,
18238 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18240 if (parent_entry
!= nullptr)
18242 CORE_ADDR start
= form_addr (parent_entry
->die_offset
,
18243 reader
->cu
->per_cu
->is_dwz
);
18244 CORE_ADDR end
= form_addr (sect_offset (info_ptr
- 1 - reader
->buffer
),
18245 reader
->cu
->per_cu
->is_dwz
);
18246 addrmap_set_empty (m_die_range_map
, start
, end
, (void *) parent_entry
);
18253 cooked_indexer::index_dies (cutu_reader
*reader
,
18254 const gdb_byte
*info_ptr
,
18255 const cooked_index_entry
*parent_entry
,
18258 const gdb_byte
*end_ptr
= info_ptr
+ reader
->cu
->header
.get_length ();
18260 while (info_ptr
< end_ptr
)
18262 sect_offset this_die
= (sect_offset
) (info_ptr
- reader
->buffer
);
18263 unsigned int bytes_read
;
18264 const abbrev_info
*abbrev
= peek_die_abbrev (*reader
, info_ptr
,
18266 info_ptr
+= bytes_read
;
18267 if (abbrev
== nullptr)
18270 if (abbrev
->tag
== DW_TAG_imported_unit
)
18272 info_ptr
= index_imported_unit (reader
, info_ptr
, abbrev
);
18276 if (!abbrev
->interesting
)
18278 info_ptr
= skip_one_die (reader
, info_ptr
, abbrev
, !fully
);
18279 if (fully
&& abbrev
->has_children
)
18280 info_ptr
= index_dies (reader
, info_ptr
, parent_entry
, fully
);
18284 const char *name
= nullptr;
18285 const char *linkage_name
= nullptr;
18286 CORE_ADDR defer
= 0;
18287 cooked_index_flag flags
= IS_STATIC
;
18288 sect_offset sibling
{};
18289 const cooked_index_entry
*this_parent_entry
= parent_entry
;
18290 info_ptr
= scan_attributes (reader
->cu
->per_cu
, reader
, info_ptr
,
18291 info_ptr
, abbrev
, &name
, &linkage_name
,
18292 &flags
, &sibling
, &this_parent_entry
,
18295 if (abbrev
->tag
== DW_TAG_namespace
18296 && m_language
== language_cplus
18297 && strcmp (name
, "::") == 0)
18299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they
18300 generated bogus DW_TAG_namespace DIEs with a name of "::"
18301 for the global namespace. Work around this problem
18306 const cooked_index_entry
*this_entry
= nullptr;
18307 if (name
!= nullptr)
18310 m_deferred_entries
.push_back ({
18311 this_die
, name
, defer
, abbrev
->tag
, flags
18314 this_entry
= m_index_storage
->add (this_die
, abbrev
->tag
, flags
,
18315 name
, this_parent_entry
,
18319 if (linkage_name
!= nullptr)
18321 /* We only want this to be "main" if it has a linkage name
18322 but not an ordinary name. */
18323 if (name
!= nullptr)
18324 flags
= flags
& ~IS_MAIN
;
18325 m_index_storage
->add (this_die
, abbrev
->tag
, flags
| IS_LINKAGE
,
18326 linkage_name
, nullptr, m_per_cu
);
18329 if (abbrev
->has_children
)
18331 switch (abbrev
->tag
)
18333 case DW_TAG_class_type
:
18334 case DW_TAG_interface_type
:
18335 case DW_TAG_structure_type
:
18336 case DW_TAG_union_type
:
18337 if (m_language
!= language_c
&& this_entry
!= nullptr)
18339 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18344 case DW_TAG_enumeration_type
:
18345 /* We need to recurse even for an anonymous enumeration.
18346 Which scope we record as the parent scope depends on
18347 whether we're reading an "enum class". If so, we use
18348 the enum itself as the parent, yielding names like
18349 "enum_class::enumerator"; otherwise we inject the
18350 names into our own parent scope. */
18351 info_ptr
= recurse (reader
, info_ptr
,
18352 ((flags
& IS_ENUM_CLASS
) == 0)
18358 case DW_TAG_module
:
18359 if (this_entry
== nullptr)
18362 case DW_TAG_namespace
:
18363 /* We don't check THIS_ENTRY for a namespace, to handle
18364 the ancient G++ workaround pointed out above. */
18365 info_ptr
= recurse (reader
, info_ptr
, this_entry
, fully
);
18368 case DW_TAG_subprogram
:
18369 if ((m_language
== language_fortran
18370 || m_language
== language_ada
)
18371 && this_entry
!= nullptr)
18373 info_ptr
= recurse (reader
, info_ptr
, this_entry
, true);
18379 if (sibling
!= sect_offset (0))
18381 const gdb_byte
*sibling_ptr
18382 = reader
->buffer
+ to_underlying (sibling
);
18384 if (sibling_ptr
< info_ptr
)
18385 complaint (_("DW_AT_sibling points backwards"));
18386 else if (sibling_ptr
> reader
->buffer_end
)
18387 reader
->die_section
->overflow_complaint ();
18389 info_ptr
= sibling_ptr
;
18392 info_ptr
= skip_children (reader
, info_ptr
);
18400 cooked_indexer::make_index (cutu_reader
*reader
)
18402 check_bounds (reader
);
18403 find_file_and_directory (reader
->comp_unit_die
, reader
->cu
);
18404 if (!reader
->comp_unit_die
->has_children
)
18406 index_dies (reader
, reader
->info_ptr
, nullptr, false);
18408 for (const auto &entry
: m_deferred_entries
)
18410 CORE_ADDR key
= form_addr (entry
.die_offset
, m_per_cu
->is_dwz
);
18411 cooked_index_entry
*parent
18412 = (cooked_index_entry
*) addrmap_find (m_die_range_map
, key
);
18413 m_index_storage
->add (entry
.die_offset
, entry
.tag
, entry
.flags
,
18414 entry
.name
, parent
, m_per_cu
);
18418 /* An implementation of quick_symbol_functions for the cooked DWARF
18421 struct cooked_index_functions
: public dwarf2_base_index_functions
18423 struct compunit_symtab
*find_pc_sect_compunit_symtab
18424 (struct objfile
*objfile
, struct bound_minimal_symbol msymbol
,
18425 CORE_ADDR pc
, struct obj_section
*section
, int warn_if_readin
) override
;
18427 struct compunit_symtab
*find_compunit_symtab_by_address
18428 (struct objfile
*objfile
, CORE_ADDR address
) override
;
18430 void dump (struct objfile
*objfile
) override
18432 gdb_printf ("Cooked index in use\n");
18435 void expand_matching_symbols
18437 const lookup_name_info
&lookup_name
,
18438 domain_enum domain
,
18440 symbol_compare_ftype
*ordered_compare
) override
;
18442 bool expand_symtabs_matching
18443 (struct objfile
*objfile
,
18444 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18445 const lookup_name_info
*lookup_name
,
18446 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18447 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18448 block_search_flags search_flags
,
18449 domain_enum domain
,
18450 enum search_domain kind
) override
;
18452 bool can_lazily_read_symbols () override
18457 void read_partial_symbols (struct objfile
*objfile
) override
18459 if (dwarf2_has_info (objfile
, nullptr))
18460 dwarf2_build_psymtabs (objfile
, true);
18464 struct compunit_symtab
*
18465 cooked_index_functions::find_pc_sect_compunit_symtab
18466 (struct objfile
*objfile
,
18467 struct bound_minimal_symbol msymbol
,
18469 struct obj_section
*section
,
18470 int warn_if_readin
)
18472 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18473 if (per_objfile
->per_bfd
->index_table
== nullptr)
18476 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
18477 cooked_index_vector
*table
18478 = (static_cast<cooked_index_vector
*>
18479 (per_objfile
->per_bfd
->index_table
.get ()));
18480 dwarf2_per_cu_data
*per_cu
= table
->lookup (pc
- baseaddr
);
18481 if (per_cu
== nullptr)
18484 if (warn_if_readin
&& per_objfile
->symtab_set_p (per_cu
))
18485 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
18486 paddress (objfile
->arch (), pc
));
18488 compunit_symtab
*result
= (recursively_find_pc_sect_compunit_symtab
18489 (dw2_instantiate_symtab (per_cu
, per_objfile
,
18492 gdb_assert (result
!= nullptr);
18496 struct compunit_symtab
*
18497 cooked_index_functions::find_compunit_symtab_by_address
18498 (struct objfile
*objfile
, CORE_ADDR address
)
18500 if (objfile
->sect_index_data
== -1)
18503 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18504 if (per_objfile
->per_bfd
->index_table
== nullptr)
18507 CORE_ADDR baseaddr
= objfile
->data_section_offset ();
18508 cooked_index_vector
*table
18509 = (static_cast<cooked_index_vector
*>
18510 (per_objfile
->per_bfd
->index_table
.get ()));
18511 dwarf2_per_cu_data
*per_cu
= table
->lookup (address
- baseaddr
);
18512 if (per_cu
== nullptr)
18515 return dw2_instantiate_symtab (per_cu
, per_objfile
, false);
18519 cooked_index_functions::expand_matching_symbols
18520 (struct objfile
*objfile
,
18521 const lookup_name_info
&lookup_name
,
18522 domain_enum domain
,
18524 symbol_compare_ftype
*ordered_compare
)
18526 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18527 if (per_objfile
->per_bfd
->index_table
== nullptr)
18529 const block_search_flags search_flags
= (global
18530 ? SEARCH_GLOBAL_BLOCK
18531 : SEARCH_STATIC_BLOCK
);
18532 const language_defn
*lang
= language_def (language_ada
);
18533 symbol_name_matcher_ftype
*name_match
18534 = lang
->get_symbol_name_matcher (lookup_name
);
18536 cooked_index_vector
*table
18537 = (static_cast<cooked_index_vector
*>
18538 (per_objfile
->per_bfd
->index_table
.get ()));
18539 for (const cooked_index_entry
*entry
: table
->all_entries ())
18541 if (entry
->parent_entry
!= nullptr)
18544 if (!entry
->matches (search_flags
)
18545 || !entry
->matches (domain
))
18548 if (name_match (entry
->canonical
, lookup_name
, nullptr))
18549 dw2_instantiate_symtab (entry
->per_cu
, per_objfile
, false);
18554 cooked_index_functions::expand_symtabs_matching
18555 (struct objfile
*objfile
,
18556 gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
18557 const lookup_name_info
*lookup_name
,
18558 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
18559 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
18560 block_search_flags search_flags
,
18561 domain_enum domain
,
18562 enum search_domain kind
)
18564 dwarf2_per_objfile
*per_objfile
= get_dwarf2_per_objfile (objfile
);
18565 if (per_objfile
->per_bfd
->index_table
== nullptr)
18568 dw_expand_symtabs_matching_file_matcher (per_objfile
, file_matcher
);
18570 /* This invariant is documented in quick-functions.h. */
18571 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
18572 if (lookup_name
== nullptr)
18574 for (dwarf2_per_cu_data
*per_cu
18575 : all_comp_units_range (per_objfile
->per_bfd
))
18579 if (!dw2_expand_symtabs_matching_one (per_cu
, per_objfile
,
18587 lookup_name_info lookup_name_without_params
18588 = lookup_name
->make_ignore_params ();
18589 bool completing
= lookup_name
->completion_mode ();
18591 /* Unique styles of language splitting. */
18592 static const enum language unique_styles
[] =
18594 /* No splitting is also a style. */
18596 /* This includes Rust. */
18598 /* This includes Go. */
18603 cooked_index_vector
*table
18604 = (static_cast<cooked_index_vector
*>
18605 (per_objfile
->per_bfd
->index_table
.get ()));
18606 for (enum language lang
: unique_styles
)
18608 std::vector
<gdb::string_view
> name_vec
18609 = lookup_name_without_params
.split_name (lang
);
18611 for (const cooked_index_entry
*entry
: table
->find (name_vec
.back (),
18614 /* No need to consider symbols from expanded CUs. */
18615 if (per_objfile
->symtab_set_p (entry
->per_cu
))
18618 /* If file-matching was done, we don't need to consider
18619 symbols from unmarked CUs. */
18620 if (file_matcher
!= nullptr && !entry
->per_cu
->mark
)
18623 /* See if the symbol matches the type filter. */
18624 if (!entry
->matches (search_flags
)
18625 || !entry
->matches (domain
)
18626 || !entry
->matches (kind
))
18629 /* We've found the base name of the symbol; now walk its
18630 parentage chain, ensuring that each component
18634 const cooked_index_entry
*parent
= entry
->parent_entry
;
18635 for (int i
= name_vec
.size () - 1; i
> 0; --i
)
18637 /* If we ran out of entries, or if this segment doesn't
18638 match, this did not match. */
18639 if (parent
== nullptr
18640 || strncmp (parent
->name
, name_vec
[i
- 1].data (),
18641 name_vec
[i
- 1].length ()) != 0)
18647 parent
= parent
->parent_entry
;
18653 /* Might have been looking for "a::b" and found
18655 if (symbol_matcher
== nullptr)
18657 symbol_name_match_type match_type
18658 = lookup_name_without_params
.match_type ();
18659 if ((match_type
== symbol_name_match_type::FULL
18660 || (lang
!= language_ada
18661 && match_type
== symbol_name_match_type::EXPRESSION
))
18662 && parent
!= nullptr)
18667 auto_obstack temp_storage
;
18668 const char *full_name
= entry
->full_name (&temp_storage
);
18669 if (!symbol_matcher (full_name
))
18673 if (!dw2_expand_symtabs_matching_one (entry
->per_cu
, per_objfile
,
18683 /* Return a new cooked_index_functions object. */
18685 static quick_symbol_functions_up
18686 make_cooked_index_funcs ()
18688 return quick_symbol_functions_up (new cooked_index_functions
);
18691 quick_symbol_functions_up
18692 cooked_index_vector::make_quick_functions () const
18694 return make_cooked_index_funcs ();
18699 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
18700 contents from the given SECTION in the HEADER.
18702 HEADER_OFFSET is the offset of the header in the section. */
18704 read_loclists_rnglists_header (struct loclists_rnglists_header
*header
,
18705 struct dwarf2_section_info
*section
,
18706 sect_offset header_offset
)
18708 unsigned int bytes_read
;
18709 bfd
*abfd
= section
->get_bfd_owner ();
18710 const gdb_byte
*info_ptr
= section
->buffer
+ to_underlying (header_offset
);
18712 header
->length
= read_initial_length (abfd
, info_ptr
, &bytes_read
);
18713 info_ptr
+= bytes_read
;
18715 header
->version
= read_2_bytes (abfd
, info_ptr
);
18718 header
->addr_size
= read_1_byte (abfd
, info_ptr
);
18721 header
->segment_collector_size
= read_1_byte (abfd
, info_ptr
);
18724 header
->offset_entry_count
= read_4_bytes (abfd
, info_ptr
);
18727 /* Return the DW_AT_loclists_base value for the CU. */
18729 lookup_loclist_base (struct dwarf2_cu
*cu
)
18731 /* For the .dwo unit, the loclist_base points to the first offset following
18732 the header. The header consists of the following entities-
18733 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18735 2. version (2 bytes)
18736 3. address size (1 byte)
18737 4. segment selector size (1 byte)
18738 5. offset entry count (4 bytes)
18739 These sizes are derived as per the DWARFv5 standard. */
18740 if (cu
->dwo_unit
!= nullptr)
18742 if (cu
->header
.initial_length_size
== 4)
18743 return LOCLIST_HEADER_SIZE32
;
18744 return LOCLIST_HEADER_SIZE64
;
18746 return cu
->loclist_base
;
18749 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18750 array of offsets in the .debug_loclists section. */
18753 read_loclist_index (struct dwarf2_cu
*cu
, ULONGEST loclist_index
)
18755 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18756 struct objfile
*objfile
= per_objfile
->objfile
;
18757 bfd
*abfd
= objfile
->obfd
;
18758 ULONGEST loclist_header_size
=
18759 (cu
->header
.initial_length_size
== 4 ? LOCLIST_HEADER_SIZE32
18760 : LOCLIST_HEADER_SIZE64
);
18761 ULONGEST loclist_base
= lookup_loclist_base (cu
);
18763 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
18764 ULONGEST start_offset
=
18765 loclist_base
+ loclist_index
* cu
->header
.offset_size
;
18767 /* Get loclists section. */
18768 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
18770 /* Read the loclists section content. */
18771 section
->read (objfile
);
18772 if (section
->buffer
== NULL
)
18773 error (_("DW_FORM_loclistx used without .debug_loclists "
18774 "section [in module %s]"), objfile_name (objfile
));
18776 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
18777 so if loclist_base is smaller than the header size, we have a problem. */
18778 if (loclist_base
< loclist_header_size
)
18779 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
18780 objfile_name (objfile
));
18782 /* Read the header of the loclists contribution. */
18783 struct loclists_rnglists_header header
;
18784 read_loclists_rnglists_header (&header
, section
,
18785 (sect_offset
) (loclist_base
- loclist_header_size
));
18787 /* Verify the loclist index is valid. */
18788 if (loclist_index
>= header
.offset_entry_count
)
18789 error (_("DW_FORM_loclistx pointing outside of "
18790 ".debug_loclists offset array [in module %s]"),
18791 objfile_name (objfile
));
18793 /* Validate that reading won't go beyond the end of the section. */
18794 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18795 error (_("Reading DW_FORM_loclistx index beyond end of"
18796 ".debug_loclists section [in module %s]"),
18797 objfile_name (objfile
));
18799 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18801 if (cu
->header
.offset_size
== 4)
18802 return (sect_offset
) (bfd_get_32 (abfd
, info_ptr
) + loclist_base
);
18804 return (sect_offset
) (bfd_get_64 (abfd
, info_ptr
) + loclist_base
);
18807 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
18808 array of offsets in the .debug_rnglists section. */
18811 read_rnglist_index (struct dwarf2_cu
*cu
, ULONGEST rnglist_index
,
18814 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
18815 struct objfile
*objfile
= dwarf2_per_objfile
->objfile
;
18816 bfd
*abfd
= objfile
->obfd
;
18817 ULONGEST rnglist_header_size
=
18818 (cu
->header
.initial_length_size
== 4 ? RNGLIST_HEADER_SIZE32
18819 : RNGLIST_HEADER_SIZE64
);
18821 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
18822 .debug_rnglists.dwo section. The rnglists base given in the skeleton
18824 ULONGEST rnglist_base
=
18825 (cu
->dwo_unit
!= nullptr) ? rnglist_header_size
: cu
->rnglists_base
;
18827 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
18828 ULONGEST start_offset
=
18829 rnglist_base
+ rnglist_index
* cu
->header
.offset_size
;
18831 /* Get rnglists section. */
18832 struct dwarf2_section_info
*section
= cu_debug_rnglists_section (cu
, tag
);
18834 /* Read the rnglists section content. */
18835 section
->read (objfile
);
18836 if (section
->buffer
== nullptr)
18837 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
18839 objfile_name (objfile
));
18841 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
18842 so if rnglist_base is smaller than the header size, we have a problem. */
18843 if (rnglist_base
< rnglist_header_size
)
18844 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
18845 objfile_name (objfile
));
18847 /* Read the header of the rnglists contribution. */
18848 struct loclists_rnglists_header header
;
18849 read_loclists_rnglists_header (&header
, section
,
18850 (sect_offset
) (rnglist_base
- rnglist_header_size
));
18852 /* Verify the rnglist index is valid. */
18853 if (rnglist_index
>= header
.offset_entry_count
)
18854 error (_("DW_FORM_rnglistx index pointing outside of "
18855 ".debug_rnglists offset array [in module %s]"),
18856 objfile_name (objfile
));
18858 /* Validate that reading won't go beyond the end of the section. */
18859 if (start_offset
+ cu
->header
.offset_size
> section
->size
)
18860 error (_("Reading DW_FORM_rnglistx index beyond end of"
18861 ".debug_rnglists section [in module %s]"),
18862 objfile_name (objfile
));
18864 const gdb_byte
*info_ptr
= section
->buffer
+ start_offset
;
18866 if (cu
->header
.offset_size
== 4)
18867 return (sect_offset
) (read_4_bytes (abfd
, info_ptr
) + rnglist_base
);
18869 return (sect_offset
) (read_8_bytes (abfd
, info_ptr
) + rnglist_base
);
18872 /* Process the attributes that had to be skipped in the first round. These
18873 attributes are the ones that need str_offsets_base or addr_base attributes.
18874 They could not have been processed in the first round, because at the time
18875 the values of str_offsets_base or addr_base may not have been known. */
18877 read_attribute_reprocess (const struct die_reader_specs
*reader
,
18878 struct attribute
*attr
, dwarf_tag tag
)
18880 struct dwarf2_cu
*cu
= reader
->cu
;
18881 switch (attr
->form
)
18883 case DW_FORM_addrx
:
18884 case DW_FORM_GNU_addr_index
:
18885 attr
->set_address (read_addr_index (cu
,
18886 attr
->as_unsigned_reprocess ()));
18888 case DW_FORM_loclistx
:
18890 sect_offset loclists_sect_off
18891 = read_loclist_index (cu
, attr
->as_unsigned_reprocess ());
18893 attr
->set_unsigned (to_underlying (loclists_sect_off
));
18896 case DW_FORM_rnglistx
:
18898 sect_offset rnglists_sect_off
18899 = read_rnglist_index (cu
, attr
->as_unsigned_reprocess (), tag
);
18901 attr
->set_unsigned (to_underlying (rnglists_sect_off
));
18905 case DW_FORM_strx1
:
18906 case DW_FORM_strx2
:
18907 case DW_FORM_strx3
:
18908 case DW_FORM_strx4
:
18909 case DW_FORM_GNU_str_index
:
18911 unsigned int str_index
= attr
->as_unsigned_reprocess ();
18912 gdb_assert (!attr
->canonical_string_p ());
18913 if (reader
->dwo_file
!= NULL
)
18914 attr
->set_string_noncanonical (read_dwo_str_index (reader
,
18917 attr
->set_string_noncanonical (read_stub_str_index (cu
,
18922 gdb_assert_not_reached ("Unexpected DWARF form.");
18926 /* Read an attribute value described by an attribute form. */
18928 static const gdb_byte
*
18929 read_attribute_value (const struct die_reader_specs
*reader
,
18930 struct attribute
*attr
, unsigned form
,
18931 LONGEST implicit_const
, const gdb_byte
*info_ptr
)
18933 struct dwarf2_cu
*cu
= reader
->cu
;
18934 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
18935 struct objfile
*objfile
= per_objfile
->objfile
;
18936 bfd
*abfd
= reader
->abfd
;
18937 struct comp_unit_head
*cu_header
= &cu
->header
;
18938 unsigned int bytes_read
;
18939 struct dwarf_block
*blk
;
18941 attr
->form
= (enum dwarf_form
) form
;
18944 case DW_FORM_ref_addr
:
18945 if (cu_header
->version
== 2)
18946 attr
->set_unsigned (cu_header
->read_address (abfd
, info_ptr
,
18949 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18951 info_ptr
+= bytes_read
;
18953 case DW_FORM_GNU_ref_alt
:
18954 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
18956 info_ptr
+= bytes_read
;
18960 struct gdbarch
*gdbarch
= objfile
->arch ();
18961 CORE_ADDR addr
= cu_header
->read_address (abfd
, info_ptr
, &bytes_read
);
18962 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
18963 attr
->set_address (addr
);
18964 info_ptr
+= bytes_read
;
18967 case DW_FORM_block2
:
18968 blk
= dwarf_alloc_block (cu
);
18969 blk
->size
= read_2_bytes (abfd
, info_ptr
);
18971 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
18972 info_ptr
+= blk
->size
;
18973 attr
->set_block (blk
);
18975 case DW_FORM_block4
:
18976 blk
= dwarf_alloc_block (cu
);
18977 blk
->size
= read_4_bytes (abfd
, info_ptr
);
18979 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
18980 info_ptr
+= blk
->size
;
18981 attr
->set_block (blk
);
18983 case DW_FORM_data2
:
18984 attr
->set_unsigned (read_2_bytes (abfd
, info_ptr
));
18987 case DW_FORM_data4
:
18988 attr
->set_unsigned (read_4_bytes (abfd
, info_ptr
));
18991 case DW_FORM_data8
:
18992 attr
->set_unsigned (read_8_bytes (abfd
, info_ptr
));
18995 case DW_FORM_data16
:
18996 blk
= dwarf_alloc_block (cu
);
18998 blk
->data
= read_n_bytes (abfd
, info_ptr
, 16);
19000 attr
->set_block (blk
);
19002 case DW_FORM_sec_offset
:
19003 attr
->set_unsigned (cu_header
->read_offset (abfd
, info_ptr
,
19005 info_ptr
+= bytes_read
;
19007 case DW_FORM_loclistx
:
19009 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19011 info_ptr
+= bytes_read
;
19014 case DW_FORM_string
:
19015 attr
->set_string_noncanonical (read_direct_string (abfd
, info_ptr
,
19017 info_ptr
+= bytes_read
;
19020 if (!cu
->per_cu
->is_dwz
)
19022 attr
->set_string_noncanonical
19023 (read_indirect_string (per_objfile
,
19024 abfd
, info_ptr
, cu_header
,
19026 info_ptr
+= bytes_read
;
19030 case DW_FORM_line_strp
:
19031 if (!cu
->per_cu
->is_dwz
)
19033 attr
->set_string_noncanonical
19034 (per_objfile
->read_line_string (info_ptr
, cu_header
,
19036 info_ptr
+= bytes_read
;
19040 case DW_FORM_GNU_strp_alt
:
19042 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19043 LONGEST str_offset
= cu_header
->read_offset (abfd
, info_ptr
,
19046 attr
->set_string_noncanonical
19047 (dwz
->read_string (objfile
, str_offset
));
19048 info_ptr
+= bytes_read
;
19051 case DW_FORM_exprloc
:
19052 case DW_FORM_block
:
19053 blk
= dwarf_alloc_block (cu
);
19054 blk
->size
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19055 info_ptr
+= bytes_read
;
19056 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19057 info_ptr
+= blk
->size
;
19058 attr
->set_block (blk
);
19060 case DW_FORM_block1
:
19061 blk
= dwarf_alloc_block (cu
);
19062 blk
->size
= read_1_byte (abfd
, info_ptr
);
19064 blk
->data
= read_n_bytes (abfd
, info_ptr
, blk
->size
);
19065 info_ptr
+= blk
->size
;
19066 attr
->set_block (blk
);
19068 case DW_FORM_data1
:
19070 attr
->set_unsigned (read_1_byte (abfd
, info_ptr
));
19073 case DW_FORM_flag_present
:
19074 attr
->set_unsigned (1);
19076 case DW_FORM_sdata
:
19077 attr
->set_signed (read_signed_leb128 (abfd
, info_ptr
, &bytes_read
));
19078 info_ptr
+= bytes_read
;
19080 case DW_FORM_rnglistx
:
19082 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19084 info_ptr
+= bytes_read
;
19087 case DW_FORM_udata
:
19088 attr
->set_unsigned (read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
));
19089 info_ptr
+= bytes_read
;
19092 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19093 + read_1_byte (abfd
, info_ptr
)));
19097 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19098 + read_2_bytes (abfd
, info_ptr
)));
19102 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19103 + read_4_bytes (abfd
, info_ptr
)));
19107 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19108 + read_8_bytes (abfd
, info_ptr
)));
19111 case DW_FORM_ref_sig8
:
19112 attr
->set_signature (read_8_bytes (abfd
, info_ptr
));
19115 case DW_FORM_ref_udata
:
19116 attr
->set_unsigned ((to_underlying (cu_header
->sect_off
)
19117 + read_unsigned_leb128 (abfd
, info_ptr
,
19119 info_ptr
+= bytes_read
;
19121 case DW_FORM_indirect
:
19122 form
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19123 info_ptr
+= bytes_read
;
19124 if (form
== DW_FORM_implicit_const
)
19126 implicit_const
= read_signed_leb128 (abfd
, info_ptr
, &bytes_read
);
19127 info_ptr
+= bytes_read
;
19129 info_ptr
= read_attribute_value (reader
, attr
, form
, implicit_const
,
19132 case DW_FORM_implicit_const
:
19133 attr
->set_signed (implicit_const
);
19135 case DW_FORM_addrx
:
19136 case DW_FORM_GNU_addr_index
:
19137 attr
->set_unsigned_reprocess (read_unsigned_leb128 (abfd
, info_ptr
,
19139 info_ptr
+= bytes_read
;
19142 case DW_FORM_strx1
:
19143 case DW_FORM_strx2
:
19144 case DW_FORM_strx3
:
19145 case DW_FORM_strx4
:
19146 case DW_FORM_GNU_str_index
:
19148 ULONGEST str_index
;
19149 if (form
== DW_FORM_strx1
)
19151 str_index
= read_1_byte (abfd
, info_ptr
);
19154 else if (form
== DW_FORM_strx2
)
19156 str_index
= read_2_bytes (abfd
, info_ptr
);
19159 else if (form
== DW_FORM_strx3
)
19161 str_index
= read_3_bytes (abfd
, info_ptr
);
19164 else if (form
== DW_FORM_strx4
)
19166 str_index
= read_4_bytes (abfd
, info_ptr
);
19171 str_index
= read_unsigned_leb128 (abfd
, info_ptr
, &bytes_read
);
19172 info_ptr
+= bytes_read
;
19174 attr
->set_unsigned_reprocess (str_index
);
19178 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19179 dwarf_form_name (form
),
19180 bfd_get_filename (abfd
));
19184 if (cu
->per_cu
->is_dwz
&& attr
->form_is_ref ())
19185 attr
->form
= DW_FORM_GNU_ref_alt
;
19187 /* We have seen instances where the compiler tried to emit a byte
19188 size attribute of -1 which ended up being encoded as an unsigned
19189 0xffffffff. Although 0xffffffff is technically a valid size value,
19190 an object of this size seems pretty unlikely so we can relatively
19191 safely treat these cases as if the size attribute was invalid and
19192 treat them as zero by default. */
19193 if (attr
->name
== DW_AT_byte_size
19194 && form
== DW_FORM_data4
19195 && attr
->as_unsigned () >= 0xffffffff)
19198 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19199 hex_string (attr
->as_unsigned ()));
19200 attr
->set_unsigned (0);
19206 /* Read an attribute described by an abbreviated attribute. */
19208 static const gdb_byte
*
19209 read_attribute (const struct die_reader_specs
*reader
,
19210 struct attribute
*attr
, const struct attr_abbrev
*abbrev
,
19211 const gdb_byte
*info_ptr
)
19213 attr
->name
= abbrev
->name
;
19214 attr
->string_is_canonical
= 0;
19215 attr
->requires_reprocessing
= 0;
19216 return read_attribute_value (reader
, attr
, abbrev
->form
,
19217 abbrev
->implicit_const
, info_ptr
);
19220 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19222 static const char *
19223 read_indirect_string_at_offset (dwarf2_per_objfile
*per_objfile
,
19224 LONGEST str_offset
)
19226 return per_objfile
->per_bfd
->str
.read_string (per_objfile
->objfile
,
19227 str_offset
, "DW_FORM_strp");
19230 /* Return pointer to string at .debug_str offset as read from BUF.
19231 BUF is assumed to be in a compilation unit described by CU_HEADER.
19232 Return *BYTES_READ_PTR count of bytes read from BUF. */
19234 static const char *
19235 read_indirect_string (dwarf2_per_objfile
*per_objfile
, bfd
*abfd
,
19236 const gdb_byte
*buf
,
19237 const struct comp_unit_head
*cu_header
,
19238 unsigned int *bytes_read_ptr
)
19240 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19242 return read_indirect_string_at_offset (per_objfile
, str_offset
);
19248 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19249 unsigned int offset_size
)
19251 bfd
*abfd
= objfile
->obfd
;
19252 ULONGEST str_offset
= read_offset (abfd
, buf
, offset_size
);
19254 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19260 dwarf2_per_objfile::read_line_string (const gdb_byte
*buf
,
19261 const struct comp_unit_head
*cu_header
,
19262 unsigned int *bytes_read_ptr
)
19264 bfd
*abfd
= objfile
->obfd
;
19265 LONGEST str_offset
= cu_header
->read_offset (abfd
, buf
, bytes_read_ptr
);
19267 return per_bfd
->line_str
.read_string (objfile
, str_offset
, "DW_FORM_line_strp");
19270 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19271 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19272 ADDR_SIZE is the size of addresses from the CU header. */
19275 read_addr_index_1 (dwarf2_per_objfile
*per_objfile
, unsigned int addr_index
,
19276 gdb::optional
<ULONGEST
> addr_base
, int addr_size
)
19278 struct objfile
*objfile
= per_objfile
->objfile
;
19279 bfd
*abfd
= objfile
->obfd
;
19280 const gdb_byte
*info_ptr
;
19281 ULONGEST addr_base_or_zero
= addr_base
.has_value () ? *addr_base
: 0;
19283 per_objfile
->per_bfd
->addr
.read (objfile
);
19284 if (per_objfile
->per_bfd
->addr
.buffer
== NULL
)
19285 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19286 objfile_name (objfile
));
19287 if (addr_base_or_zero
+ addr_index
* addr_size
19288 >= per_objfile
->per_bfd
->addr
.size
)
19289 error (_("DW_FORM_addr_index pointing outside of "
19290 ".debug_addr section [in module %s]"),
19291 objfile_name (objfile
));
19292 info_ptr
= (per_objfile
->per_bfd
->addr
.buffer
+ addr_base_or_zero
19293 + addr_index
* addr_size
);
19294 if (addr_size
== 4)
19295 return bfd_get_32 (abfd
, info_ptr
);
19297 return bfd_get_64 (abfd
, info_ptr
);
19300 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19303 read_addr_index (struct dwarf2_cu
*cu
, unsigned int addr_index
)
19305 return read_addr_index_1 (cu
->per_objfile
, addr_index
,
19306 cu
->addr_base
, cu
->header
.addr_size
);
19309 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19312 read_addr_index_from_leb128 (struct dwarf2_cu
*cu
, const gdb_byte
*info_ptr
,
19313 unsigned int *bytes_read
)
19315 bfd
*abfd
= cu
->per_objfile
->objfile
->obfd
;
19316 unsigned int addr_index
= read_unsigned_leb128 (abfd
, info_ptr
, bytes_read
);
19318 return read_addr_index (cu
, addr_index
);
19324 dwarf2_read_addr_index (dwarf2_per_cu_data
*per_cu
,
19325 dwarf2_per_objfile
*per_objfile
,
19326 unsigned int addr_index
)
19328 struct dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
19329 gdb::optional
<ULONGEST
> addr_base
;
19332 /* We need addr_base and addr_size.
19333 If we don't have PER_CU->cu, we have to get it.
19334 Nasty, but the alternative is storing the needed info in PER_CU,
19335 which at this point doesn't seem justified: it's not clear how frequently
19336 it would get used and it would increase the size of every PER_CU.
19337 Entry points like dwarf2_per_cu_addr_size do a similar thing
19338 so we're not in uncharted territory here.
19339 Alas we need to be a bit more complicated as addr_base is contained
19342 We don't need to read the entire CU(/TU).
19343 We just need the header and top level die.
19345 IWBN to use the aging mechanism to let us lazily later discard the CU.
19346 For now we skip this optimization. */
19350 addr_base
= cu
->addr_base
;
19351 addr_size
= cu
->header
.addr_size
;
19355 cutu_reader
reader (per_cu
, per_objfile
, nullptr, nullptr, false);
19356 addr_base
= reader
.cu
->addr_base
;
19357 addr_size
= reader
.cu
->header
.addr_size
;
19360 return read_addr_index_1 (per_objfile
, addr_index
, addr_base
, addr_size
);
19363 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19364 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19367 static const char *
19368 read_str_index (struct dwarf2_cu
*cu
,
19369 struct dwarf2_section_info
*str_section
,
19370 struct dwarf2_section_info
*str_offsets_section
,
19371 ULONGEST str_offsets_base
, ULONGEST str_index
)
19373 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19374 struct objfile
*objfile
= per_objfile
->objfile
;
19375 const char *objf_name
= objfile_name (objfile
);
19376 bfd
*abfd
= objfile
->obfd
;
19377 const gdb_byte
*info_ptr
;
19378 ULONGEST str_offset
;
19379 static const char form_name
[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19381 str_section
->read (objfile
);
19382 str_offsets_section
->read (objfile
);
19383 if (str_section
->buffer
== NULL
)
19384 error (_("%s used without %s section"
19385 " in CU at offset %s [in module %s]"),
19386 form_name
, str_section
->get_name (),
19387 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19388 if (str_offsets_section
->buffer
== NULL
)
19389 error (_("%s used without %s section"
19390 " in CU at offset %s [in module %s]"),
19391 form_name
, str_section
->get_name (),
19392 sect_offset_str (cu
->header
.sect_off
), objf_name
);
19393 info_ptr
= (str_offsets_section
->buffer
19395 + str_index
* cu
->header
.offset_size
);
19396 if (cu
->header
.offset_size
== 4)
19397 str_offset
= bfd_get_32 (abfd
, info_ptr
);
19399 str_offset
= bfd_get_64 (abfd
, info_ptr
);
19400 if (str_offset
>= str_section
->size
)
19401 error (_("Offset from %s pointing outside of"
19402 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19403 form_name
, sect_offset_str (cu
->header
.sect_off
), objf_name
);
19404 return (const char *) (str_section
->buffer
+ str_offset
);
19407 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19409 static const char *
19410 read_dwo_str_index (const struct die_reader_specs
*reader
, ULONGEST str_index
)
19412 ULONGEST str_offsets_base
= reader
->cu
->header
.version
>= 5
19413 ? reader
->cu
->header
.addr_size
: 0;
19414 return read_str_index (reader
->cu
,
19415 &reader
->dwo_file
->sections
.str
,
19416 &reader
->dwo_file
->sections
.str_offsets
,
19417 str_offsets_base
, str_index
);
19420 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19422 static const char *
19423 read_stub_str_index (struct dwarf2_cu
*cu
, ULONGEST str_index
)
19425 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
19426 const char *objf_name
= objfile_name (objfile
);
19427 static const char form_name
[] = "DW_FORM_GNU_str_index";
19428 static const char str_offsets_attr_name
[] = "DW_AT_str_offsets";
19430 if (!cu
->str_offsets_base
.has_value ())
19431 error (_("%s used in Fission stub without %s"
19432 " in CU at offset 0x%lx [in module %s]"),
19433 form_name
, str_offsets_attr_name
,
19434 (long) cu
->header
.offset_size
, objf_name
);
19436 return read_str_index (cu
,
19437 &cu
->per_objfile
->per_bfd
->str
,
19438 &cu
->per_objfile
->per_bfd
->str_offsets
,
19439 *cu
->str_offsets_base
, str_index
);
19442 /* Return the length of an LEB128 number in BUF. */
19445 leb128_size (const gdb_byte
*buf
)
19447 const gdb_byte
*begin
= buf
;
19453 if ((byte
& 128) == 0)
19454 return buf
- begin
;
19458 static enum language
19459 dwarf_lang_to_enum_language (unsigned int lang
)
19461 enum language language
;
19470 language
= language_c
;
19473 case DW_LANG_C_plus_plus
:
19474 case DW_LANG_C_plus_plus_11
:
19475 case DW_LANG_C_plus_plus_14
:
19476 language
= language_cplus
;
19479 language
= language_d
;
19481 case DW_LANG_Fortran77
:
19482 case DW_LANG_Fortran90
:
19483 case DW_LANG_Fortran95
:
19484 case DW_LANG_Fortran03
:
19485 case DW_LANG_Fortran08
:
19486 language
= language_fortran
;
19489 language
= language_go
;
19491 case DW_LANG_Mips_Assembler
:
19492 language
= language_asm
;
19494 case DW_LANG_Ada83
:
19495 case DW_LANG_Ada95
:
19496 language
= language_ada
;
19498 case DW_LANG_Modula2
:
19499 language
= language_m2
;
19501 case DW_LANG_Pascal83
:
19502 language
= language_pascal
;
19505 language
= language_objc
;
19508 case DW_LANG_Rust_old
:
19509 language
= language_rust
;
19511 case DW_LANG_OpenCL
:
19512 language
= language_opencl
;
19514 case DW_LANG_Cobol74
:
19515 case DW_LANG_Cobol85
:
19517 language
= language_minimal
;
19524 /* Return the named attribute or NULL if not there. */
19526 static struct attribute
*
19527 dwarf2_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19532 struct attribute
*spec
= NULL
;
19534 for (i
= 0; i
< die
->num_attrs
; ++i
)
19536 if (die
->attrs
[i
].name
== name
)
19537 return &die
->attrs
[i
];
19538 if (die
->attrs
[i
].name
== DW_AT_specification
19539 || die
->attrs
[i
].name
== DW_AT_abstract_origin
)
19540 spec
= &die
->attrs
[i
];
19546 die
= follow_die_ref (die
, spec
, &cu
);
19552 /* Return the string associated with a string-typed attribute, or NULL if it
19553 is either not found or is of an incorrect type. */
19555 static const char *
19556 dwarf2_string_attr (struct die_info
*die
, unsigned int name
, struct dwarf2_cu
*cu
)
19558 struct attribute
*attr
;
19559 const char *str
= NULL
;
19561 attr
= dwarf2_attr (die
, name
, cu
);
19565 str
= attr
->as_string ();
19566 if (str
== nullptr)
19567 complaint (_("string type expected for attribute %s for "
19568 "DIE at %s in module %s"),
19569 dwarf_attr_name (name
), sect_offset_str (die
->sect_off
),
19570 objfile_name (cu
->per_objfile
->objfile
));
19576 /* Return the dwo name or NULL if not present. If present, it is in either
19577 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19578 static const char *
19579 dwarf2_dwo_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
19581 const char *dwo_name
= dwarf2_string_attr (die
, DW_AT_GNU_dwo_name
, cu
);
19582 if (dwo_name
== nullptr)
19583 dwo_name
= dwarf2_string_attr (die
, DW_AT_dwo_name
, cu
);
19587 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19588 and holds a non-zero value. This function should only be used for
19589 DW_FORM_flag or DW_FORM_flag_present attributes. */
19592 dwarf2_flag_true_p (struct die_info
*die
, unsigned name
, struct dwarf2_cu
*cu
)
19594 struct attribute
*attr
= dwarf2_attr (die
, name
, cu
);
19596 return attr
!= nullptr && attr
->as_boolean ();
19600 die_is_declaration (struct die_info
*die
, struct dwarf2_cu
*cu
)
19602 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19603 which value is non-zero. However, we have to be careful with
19604 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19605 (via dwarf2_flag_true_p) follows this attribute. So we may
19606 end up accidently finding a declaration attribute that belongs
19607 to a different DIE referenced by the specification attribute,
19608 even though the given DIE does not have a declaration attribute. */
19609 return (dwarf2_flag_true_p (die
, DW_AT_declaration
, cu
)
19610 && dwarf2_attr (die
, DW_AT_specification
, cu
) == NULL
);
19613 /* Return the die giving the specification for DIE, if there is
19614 one. *SPEC_CU is the CU containing DIE on input, and the CU
19615 containing the return value on output. If there is no
19616 specification, but there is an abstract origin, that is
19619 static struct die_info
*
19620 die_specification (struct die_info
*die
, struct dwarf2_cu
**spec_cu
)
19622 struct attribute
*spec_attr
= dwarf2_attr (die
, DW_AT_specification
,
19625 if (spec_attr
== NULL
)
19626 spec_attr
= dwarf2_attr (die
, DW_AT_abstract_origin
, *spec_cu
);
19628 if (spec_attr
== NULL
)
19631 return follow_die_ref (die
, spec_attr
, spec_cu
);
19634 /* A convenience function to find the proper .debug_line section for a CU. */
19636 static struct dwarf2_section_info
*
19637 get_debug_line_section (struct dwarf2_cu
*cu
)
19639 struct dwarf2_section_info
*section
;
19640 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19642 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19644 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19645 section
= &cu
->dwo_unit
->dwo_file
->sections
.line
;
19646 else if (cu
->per_cu
->is_dwz
)
19648 dwz_file
*dwz
= dwarf2_get_dwz_file (per_objfile
->per_bfd
, true);
19650 section
= &dwz
->line
;
19653 section
= &per_objfile
->per_bfd
->line
;
19658 /* Read the statement program header starting at OFFSET in
19659 .debug_line, or .debug_line.dwo. Return a pointer
19660 to a struct line_header, allocated using xmalloc.
19661 Returns NULL if there is a problem reading the header, e.g., if it
19662 has a version we don't understand.
19664 NOTE: the strings in the include directory and file name tables of
19665 the returned object point into the dwarf line section buffer,
19666 and must not be freed. */
19668 static line_header_up
19669 dwarf_decode_line_header (sect_offset sect_off
, struct dwarf2_cu
*cu
)
19671 struct dwarf2_section_info
*section
;
19672 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
19674 section
= get_debug_line_section (cu
);
19675 section
->read (per_objfile
->objfile
);
19676 if (section
->buffer
== NULL
)
19678 if (cu
->dwo_unit
&& cu
->per_cu
->is_debug_types
)
19679 complaint (_("missing .debug_line.dwo section"));
19681 complaint (_("missing .debug_line section"));
19685 return dwarf_decode_line_header (sect_off
, cu
->per_cu
->is_dwz
,
19686 per_objfile
, section
, &cu
->header
);
19689 /* Subroutine of dwarf_decode_lines to simplify it.
19690 Return the file name for the given file_entry.
19691 CU_INFO describes the CU's DW_AT_name and DW_AT_comp_dir.
19692 If space for the result is malloc'd, *NAME_HOLDER will be set.
19693 Returns NULL if FILE_INDEX should be ignored, i.e., it is
19694 equivalent to CU_INFO. */
19696 static const char *
19697 compute_include_file_name (const struct line_header
*lh
, const file_entry
&fe
,
19698 const file_and_directory
&cu_info
,
19699 std::string
&name_holder
)
19701 const char *include_name
= fe
.name
;
19702 const char *include_name_to_compare
= include_name
;
19704 const char *dir_name
= fe
.include_dir (lh
);
19706 std::string hold_compare
;
19707 if (!IS_ABSOLUTE_PATH (include_name
)
19708 && (dir_name
!= nullptr || cu_info
.get_comp_dir () != nullptr))
19710 /* Avoid creating a duplicate name for CU_INFO.
19711 We do this by comparing INCLUDE_NAME and CU_INFO.
19712 Before we do the comparison, however, we need to account
19713 for DIR_NAME and COMP_DIR.
19714 First prepend dir_name (if non-NULL). If we still don't
19715 have an absolute path prepend comp_dir (if non-NULL).
19716 However, the directory we record in the include-file's
19717 psymtab does not contain COMP_DIR (to match the
19718 corresponding symtab(s)).
19723 bash$ gcc -g ./hello.c
19724 include_name = "hello.c"
19726 DW_AT_comp_dir = comp_dir = "/tmp"
19727 DW_AT_name = "./hello.c"
19731 if (dir_name
!= NULL
)
19733 name_holder
= path_join (dir_name
, include_name
);
19734 include_name
= name_holder
.c_str ();
19735 include_name_to_compare
= include_name
;
19737 if (!IS_ABSOLUTE_PATH (include_name
)
19738 && cu_info
.get_comp_dir () != nullptr)
19740 hold_compare
= path_join (cu_info
.get_comp_dir (), include_name
);
19741 include_name_to_compare
= hold_compare
.c_str ();
19745 std::string copied_name
;
19746 const char *cu_filename
= cu_info
.get_name ();
19747 if (!IS_ABSOLUTE_PATH (cu_filename
) && cu_info
.get_comp_dir () != nullptr)
19749 copied_name
= path_join (cu_info
.get_comp_dir (), cu_filename
);
19750 cu_filename
= copied_name
.c_str ();
19753 if (FILENAME_CMP (include_name_to_compare
, cu_filename
) == 0)
19755 return include_name
;
19758 /* State machine to track the state of the line number program. */
19760 class lnp_state_machine
19763 /* Initialize a machine state for the start of a line number
19765 lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
, line_header
*lh
);
19767 file_entry
*current_file ()
19769 /* lh->file_names is 0-based, but the file name numbers in the
19770 statement program are 1-based. */
19771 return m_line_header
->file_name_at (m_file
);
19774 /* Record the line in the state machine. END_SEQUENCE is true if
19775 we're processing the end of a sequence. */
19776 void record_line (bool end_sequence
);
19778 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
19779 nop-out rest of the lines in this sequence. */
19780 void check_line_address (struct dwarf2_cu
*cu
,
19781 const gdb_byte
*line_ptr
,
19782 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
);
19784 void handle_set_discriminator (unsigned int discriminator
)
19786 m_discriminator
= discriminator
;
19787 m_line_has_non_zero_discriminator
|= discriminator
!= 0;
19790 /* Handle DW_LNE_set_address. */
19791 void handle_set_address (CORE_ADDR baseaddr
, CORE_ADDR address
)
19794 address
+= baseaddr
;
19795 m_address
= gdbarch_adjust_dwarf2_line (m_gdbarch
, address
, false);
19798 /* Handle DW_LNS_advance_pc. */
19799 void handle_advance_pc (CORE_ADDR adjust
);
19801 /* Handle a special opcode. */
19802 void handle_special_opcode (unsigned char op_code
);
19804 /* Handle DW_LNS_advance_line. */
19805 void handle_advance_line (int line_delta
)
19807 advance_line (line_delta
);
19810 /* Handle DW_LNS_set_file. */
19811 void handle_set_file (file_name_index file
);
19813 /* Handle DW_LNS_negate_stmt. */
19814 void handle_negate_stmt ()
19816 m_flags
^= LEF_IS_STMT
;
19819 /* Handle DW_LNS_const_add_pc. */
19820 void handle_const_add_pc ();
19822 /* Handle DW_LNS_fixed_advance_pc. */
19823 void handle_fixed_advance_pc (CORE_ADDR addr_adj
)
19825 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19829 /* Handle DW_LNS_copy. */
19830 void handle_copy ()
19832 record_line (false);
19833 m_discriminator
= 0;
19834 m_flags
&= ~LEF_PROLOGUE_END
;
19837 /* Handle DW_LNE_end_sequence. */
19838 void handle_end_sequence ()
19840 m_currently_recording_lines
= true;
19843 /* Handle DW_LNS_set_prologue_end. */
19844 void handle_set_prologue_end ()
19846 m_flags
|= LEF_PROLOGUE_END
;
19850 /* Advance the line by LINE_DELTA. */
19851 void advance_line (int line_delta
)
19853 m_line
+= line_delta
;
19855 if (line_delta
!= 0)
19856 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
19859 struct dwarf2_cu
*m_cu
;
19861 gdbarch
*m_gdbarch
;
19863 /* The line number header. */
19864 line_header
*m_line_header
;
19866 /* These are part of the standard DWARF line number state machine,
19867 and initialized according to the DWARF spec. */
19869 unsigned char m_op_index
= 0;
19870 /* The line table index of the current file. */
19871 file_name_index m_file
= 1;
19872 unsigned int m_line
= 1;
19874 /* These are initialized in the constructor. */
19876 CORE_ADDR m_address
;
19877 linetable_entry_flags m_flags
;
19878 unsigned int m_discriminator
;
19880 /* Additional bits of state we need to track. */
19882 /* The last file that we called dwarf2_start_subfile for.
19883 This is only used for TLLs. */
19884 unsigned int m_last_file
= 0;
19885 /* The last file a line number was recorded for. */
19886 struct subfile
*m_last_subfile
= NULL
;
19888 /* The address of the last line entry. */
19889 CORE_ADDR m_last_address
;
19891 /* Set to true when a previous line at the same address (using
19892 m_last_address) had LEF_IS_STMT set in m_flags. This is reset to false
19893 when a line entry at a new address (m_address different to
19894 m_last_address) is processed. */
19895 bool m_stmt_at_address
= false;
19897 /* When true, record the lines we decode. */
19898 bool m_currently_recording_lines
= false;
19900 /* The last line number that was recorded, used to coalesce
19901 consecutive entries for the same line. This can happen, for
19902 example, when discriminators are present. PR 17276. */
19903 unsigned int m_last_line
= 0;
19904 bool m_line_has_non_zero_discriminator
= false;
19908 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust
)
19910 CORE_ADDR addr_adj
= (((m_op_index
+ adjust
)
19911 / m_line_header
->maximum_ops_per_instruction
)
19912 * m_line_header
->minimum_instruction_length
);
19913 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19914 m_op_index
= ((m_op_index
+ adjust
)
19915 % m_line_header
->maximum_ops_per_instruction
);
19919 lnp_state_machine::handle_special_opcode (unsigned char op_code
)
19921 unsigned char adj_opcode
= op_code
- m_line_header
->opcode_base
;
19922 unsigned char adj_opcode_d
= adj_opcode
/ m_line_header
->line_range
;
19923 unsigned char adj_opcode_r
= adj_opcode
% m_line_header
->line_range
;
19924 CORE_ADDR addr_adj
= (((m_op_index
+ adj_opcode_d
)
19925 / m_line_header
->maximum_ops_per_instruction
)
19926 * m_line_header
->minimum_instruction_length
);
19927 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19928 m_op_index
= ((m_op_index
+ adj_opcode_d
)
19929 % m_line_header
->maximum_ops_per_instruction
);
19931 int line_delta
= m_line_header
->line_base
+ adj_opcode_r
;
19932 advance_line (line_delta
);
19933 record_line (false);
19934 m_discriminator
= 0;
19935 m_flags
&= ~LEF_PROLOGUE_END
;
19939 lnp_state_machine::handle_set_file (file_name_index file
)
19943 const file_entry
*fe
= current_file ();
19945 dwarf2_debug_line_missing_file_complaint ();
19948 const char *dir
= fe
->include_dir (m_line_header
);
19950 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
19951 m_line_has_non_zero_discriminator
= m_discriminator
!= 0;
19952 dwarf2_start_subfile (m_cu
, fe
->name
, dir
);
19957 lnp_state_machine::handle_const_add_pc ()
19960 = (255 - m_line_header
->opcode_base
) / m_line_header
->line_range
;
19963 = (((m_op_index
+ adjust
)
19964 / m_line_header
->maximum_ops_per_instruction
)
19965 * m_line_header
->minimum_instruction_length
);
19967 m_address
+= gdbarch_adjust_dwarf2_line (m_gdbarch
, addr_adj
, true);
19968 m_op_index
= ((m_op_index
+ adjust
)
19969 % m_line_header
->maximum_ops_per_instruction
);
19972 /* Return non-zero if we should add LINE to the line number table.
19973 LINE is the line to add, LAST_LINE is the last line that was added,
19974 LAST_SUBFILE is the subfile for LAST_LINE.
19975 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19976 had a non-zero discriminator.
19978 We have to be careful in the presence of discriminators.
19979 E.g., for this line:
19981 for (i = 0; i < 100000; i++);
19983 clang can emit four line number entries for that one line,
19984 each with a different discriminator.
19985 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19987 However, we want gdb to coalesce all four entries into one.
19988 Otherwise the user could stepi into the middle of the line and
19989 gdb would get confused about whether the pc really was in the
19990 middle of the line.
19992 Things are further complicated by the fact that two consecutive
19993 line number entries for the same line is a heuristic used by gcc
19994 to denote the end of the prologue. So we can't just discard duplicate
19995 entries, we have to be selective about it. The heuristic we use is
19996 that we only collapse consecutive entries for the same line if at least
19997 one of those entries has a non-zero discriminator. PR 17276.
19999 Note: Addresses in the line number state machine can never go backwards
20000 within one sequence, thus this coalescing is ok. */
20003 dwarf_record_line_p (struct dwarf2_cu
*cu
,
20004 unsigned int line
, unsigned int last_line
,
20005 int line_has_non_zero_discriminator
,
20006 struct subfile
*last_subfile
)
20008 if (cu
->get_builder ()->get_current_subfile () != last_subfile
)
20010 if (line
!= last_line
)
20012 /* Same line for the same file that we've seen already.
20013 As a last check, for pr 17276, only record the line if the line
20014 has never had a non-zero discriminator. */
20015 if (!line_has_non_zero_discriminator
)
20020 /* Use the CU's builder to record line number LINE beginning at
20021 address ADDRESS in the line table of subfile SUBFILE. */
20024 dwarf_record_line_1 (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20025 unsigned int line
, CORE_ADDR address
,
20026 linetable_entry_flags flags
,
20027 struct dwarf2_cu
*cu
)
20029 CORE_ADDR addr
= gdbarch_addr_bits_remove (gdbarch
, address
);
20031 if (dwarf_line_debug
)
20033 gdb_printf (gdb_stdlog
,
20034 "Recording line %u, file %s, address %s\n",
20035 line
, lbasename (subfile
->name
.c_str ()),
20036 paddress (gdbarch
, address
));
20040 cu
->get_builder ()->record_line (subfile
, line
, addr
, flags
);
20043 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20044 Mark the end of a set of line number records.
20045 The arguments are the same as for dwarf_record_line_1.
20046 If SUBFILE is NULL the request is ignored. */
20049 dwarf_finish_line (struct gdbarch
*gdbarch
, struct subfile
*subfile
,
20050 CORE_ADDR address
, struct dwarf2_cu
*cu
)
20052 if (subfile
== NULL
)
20055 if (dwarf_line_debug
)
20057 gdb_printf (gdb_stdlog
,
20058 "Finishing current line, file %s, address %s\n",
20059 lbasename (subfile
->name
.c_str ()),
20060 paddress (gdbarch
, address
));
20063 dwarf_record_line_1 (gdbarch
, subfile
, 0, address
, LEF_IS_STMT
, cu
);
20067 lnp_state_machine::record_line (bool end_sequence
)
20069 if (dwarf_line_debug
)
20071 gdb_printf (gdb_stdlog
,
20072 "Processing actual line %u: file %u,"
20073 " address %s, is_stmt %u, prologue_end %u, discrim %u%s\n",
20075 paddress (m_gdbarch
, m_address
),
20076 (m_flags
& LEF_IS_STMT
) != 0,
20077 (m_flags
& LEF_PROLOGUE_END
) != 0,
20079 (end_sequence
? "\t(end sequence)" : ""));
20082 file_entry
*fe
= current_file ();
20085 dwarf2_debug_line_missing_file_complaint ();
20086 /* For now we ignore lines not starting on an instruction boundary.
20087 But not when processing end_sequence for compatibility with the
20088 previous version of the code. */
20089 else if (m_op_index
== 0 || end_sequence
)
20091 /* When we switch files we insert an end maker in the first file,
20092 switch to the second file and add a new line entry. The
20093 problem is that the end marker inserted in the first file will
20094 discard any previous line entries at the same address. If the
20095 line entries in the first file are marked as is-stmt, while
20096 the new line in the second file is non-stmt, then this means
20097 the end marker will discard is-stmt lines so we can have a
20098 non-stmt line. This means that there are less addresses at
20099 which the user can insert a breakpoint.
20101 To improve this we track the last address in m_last_address,
20102 and whether we have seen an is-stmt at this address. Then
20103 when switching files, if we have seen a stmt at the current
20104 address, and we are switching to create a non-stmt line, then
20105 discard the new line. */
20107 = m_last_subfile
!= m_cu
->get_builder ()->get_current_subfile ();
20108 bool ignore_this_line
20109 = ((file_changed
&& !end_sequence
&& m_last_address
== m_address
20110 && ((m_flags
& LEF_IS_STMT
) == 0)
20111 && m_stmt_at_address
)
20112 || (!end_sequence
&& m_line
== 0));
20114 if ((file_changed
&& !ignore_this_line
) || end_sequence
)
20116 dwarf_finish_line (m_gdbarch
, m_last_subfile
, m_address
,
20117 m_currently_recording_lines
? m_cu
: nullptr);
20120 if (!end_sequence
&& !ignore_this_line
)
20122 linetable_entry_flags lte_flags
= m_flags
;
20123 if (producer_is_codewarrior (m_cu
))
20124 lte_flags
|= LEF_IS_STMT
;
20126 if (dwarf_record_line_p (m_cu
, m_line
, m_last_line
,
20127 m_line_has_non_zero_discriminator
,
20130 buildsym_compunit
*builder
= m_cu
->get_builder ();
20131 dwarf_record_line_1 (m_gdbarch
,
20132 builder
->get_current_subfile (),
20133 m_line
, m_address
, lte_flags
,
20134 m_currently_recording_lines
? m_cu
: nullptr);
20136 m_last_subfile
= m_cu
->get_builder ()->get_current_subfile ();
20137 m_last_line
= m_line
;
20141 /* Track whether we have seen any IS_STMT true at m_address in case we
20142 have multiple line table entries all at m_address. */
20143 if (m_last_address
!= m_address
)
20145 m_stmt_at_address
= false;
20146 m_last_address
= m_address
;
20148 m_stmt_at_address
|= (m_flags
& LEF_IS_STMT
) != 0;
20151 lnp_state_machine::lnp_state_machine (struct dwarf2_cu
*cu
, gdbarch
*arch
,
20156 m_line_header
= lh
;
20158 m_currently_recording_lines
= true;
20160 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20161 was a line entry for it so that the backend has a chance to adjust it
20162 and also record it in case it needs it. This is currently used by MIPS
20163 code, cf. `mips_adjust_dwarf2_line'. */
20164 m_address
= gdbarch_adjust_dwarf2_line (arch
, 0, 0);
20166 if (lh
->default_is_stmt
)
20167 m_flags
|= LEF_IS_STMT
;
20168 m_discriminator
= 0;
20170 m_last_address
= m_address
;
20171 m_stmt_at_address
= false;
20175 lnp_state_machine::check_line_address (struct dwarf2_cu
*cu
,
20176 const gdb_byte
*line_ptr
,
20177 CORE_ADDR unrelocated_lowpc
, CORE_ADDR address
)
20179 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20180 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20181 located at 0x0. In this case, additionally check that if
20182 ADDRESS < UNRELOCATED_LOWPC. */
20184 if ((address
== 0 && address
< unrelocated_lowpc
)
20185 || address
== (CORE_ADDR
) -1)
20187 /* This line table is for a function which has been
20188 GCd by the linker. Ignore it. PR gdb/12528 */
20190 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20191 long line_offset
= line_ptr
- get_debug_line_section (cu
)->buffer
;
20193 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20194 line_offset
, objfile_name (objfile
));
20195 m_currently_recording_lines
= false;
20196 /* Note: m_currently_recording_lines is left as false until we see
20197 DW_LNE_end_sequence. */
20201 /* Subroutine of dwarf_decode_lines to simplify it.
20202 Process the line number information in LH. */
20205 dwarf_decode_lines_1 (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20208 const gdb_byte
*line_ptr
, *extended_end
;
20209 const gdb_byte
*line_end
;
20210 unsigned int bytes_read
, extended_len
;
20211 unsigned char op_code
, extended_op
;
20212 CORE_ADDR baseaddr
;
20213 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20214 bfd
*abfd
= objfile
->obfd
;
20215 struct gdbarch
*gdbarch
= objfile
->arch ();
20217 baseaddr
= objfile
->text_section_offset ();
20219 line_ptr
= lh
->statement_program_start
;
20220 line_end
= lh
->statement_program_end
;
20222 /* Read the statement sequences until there's nothing left. */
20223 while (line_ptr
< line_end
)
20225 /* The DWARF line number program state machine. Reset the state
20226 machine at the start of each sequence. */
20227 lnp_state_machine
state_machine (cu
, gdbarch
, lh
);
20228 bool end_sequence
= false;
20230 /* Start a subfile for the current file of the state
20232 const file_entry
*fe
= state_machine
.current_file ();
20235 dwarf2_start_subfile (cu
, fe
->name
, fe
->include_dir (lh
));
20237 /* Decode the table. */
20238 while (line_ptr
< line_end
&& !end_sequence
)
20240 op_code
= read_1_byte (abfd
, line_ptr
);
20243 if (op_code
>= lh
->opcode_base
)
20245 /* Special opcode. */
20246 state_machine
.handle_special_opcode (op_code
);
20248 else switch (op_code
)
20250 case DW_LNS_extended_op
:
20251 extended_len
= read_unsigned_leb128 (abfd
, line_ptr
,
20253 line_ptr
+= bytes_read
;
20254 extended_end
= line_ptr
+ extended_len
;
20255 extended_op
= read_1_byte (abfd
, line_ptr
);
20257 if (DW_LNE_lo_user
<= extended_op
20258 && extended_op
<= DW_LNE_hi_user
)
20260 /* Vendor extension, ignore. */
20261 line_ptr
= extended_end
;
20264 switch (extended_op
)
20266 case DW_LNE_end_sequence
:
20267 state_machine
.handle_end_sequence ();
20268 end_sequence
= true;
20270 case DW_LNE_set_address
:
20273 = cu
->header
.read_address (abfd
, line_ptr
, &bytes_read
);
20274 line_ptr
+= bytes_read
;
20276 state_machine
.check_line_address (cu
, line_ptr
,
20277 lowpc
- baseaddr
, address
);
20278 state_machine
.handle_set_address (baseaddr
, address
);
20281 case DW_LNE_define_file
:
20283 const char *cur_file
;
20284 unsigned int mod_time
, length
;
20287 cur_file
= read_direct_string (abfd
, line_ptr
,
20289 line_ptr
+= bytes_read
;
20290 dindex
= (dir_index
)
20291 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20292 line_ptr
+= bytes_read
;
20294 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20295 line_ptr
+= bytes_read
;
20297 read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20298 line_ptr
+= bytes_read
;
20299 lh
->add_file_name (cur_file
, dindex
, mod_time
, length
);
20302 case DW_LNE_set_discriminator
:
20304 /* The discriminator is not interesting to the
20305 debugger; just ignore it. We still need to
20306 check its value though:
20307 if there are consecutive entries for the same
20308 (non-prologue) line we want to coalesce them.
20311 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20312 line_ptr
+= bytes_read
;
20314 state_machine
.handle_set_discriminator (discr
);
20318 complaint (_("mangled .debug_line section"));
20321 /* Make sure that we parsed the extended op correctly. If e.g.
20322 we expected a different address size than the producer used,
20323 we may have read the wrong number of bytes. */
20324 if (line_ptr
!= extended_end
)
20326 complaint (_("mangled .debug_line section"));
20331 state_machine
.handle_copy ();
20333 case DW_LNS_advance_pc
:
20336 = read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20337 line_ptr
+= bytes_read
;
20339 state_machine
.handle_advance_pc (adjust
);
20342 case DW_LNS_advance_line
:
20345 = read_signed_leb128 (abfd
, line_ptr
, &bytes_read
);
20346 line_ptr
+= bytes_read
;
20348 state_machine
.handle_advance_line (line_delta
);
20351 case DW_LNS_set_file
:
20353 file_name_index file
20354 = (file_name_index
) read_unsigned_leb128 (abfd
, line_ptr
,
20356 line_ptr
+= bytes_read
;
20358 state_machine
.handle_set_file (file
);
20361 case DW_LNS_set_column
:
20362 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20363 line_ptr
+= bytes_read
;
20365 case DW_LNS_negate_stmt
:
20366 state_machine
.handle_negate_stmt ();
20368 case DW_LNS_set_basic_block
:
20370 /* Add to the address register of the state machine the
20371 address increment value corresponding to special opcode
20372 255. I.e., this value is scaled by the minimum
20373 instruction length since special opcode 255 would have
20374 scaled the increment. */
20375 case DW_LNS_const_add_pc
:
20376 state_machine
.handle_const_add_pc ();
20378 case DW_LNS_fixed_advance_pc
:
20380 CORE_ADDR addr_adj
= read_2_bytes (abfd
, line_ptr
);
20383 state_machine
.handle_fixed_advance_pc (addr_adj
);
20386 case DW_LNS_set_prologue_end
:
20387 state_machine
.handle_set_prologue_end ();
20391 /* Unknown standard opcode, ignore it. */
20394 for (i
= 0; i
< lh
->standard_opcode_lengths
[op_code
]; i
++)
20396 (void) read_unsigned_leb128 (abfd
, line_ptr
, &bytes_read
);
20397 line_ptr
+= bytes_read
;
20404 dwarf2_debug_line_missing_end_sequence_complaint ();
20406 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20407 in which case we still finish recording the last line). */
20408 state_machine
.record_line (true);
20412 /* Decode the Line Number Program (LNP) for the given line_header
20413 structure and CU. The actual information extracted and the type
20414 of structures created from the LNP depends on the value of PST.
20416 FND holds the CU file name and directory, if known.
20417 It is used for relative paths in the line table.
20419 NOTE: It is important that psymtabs have the same file name (via
20420 strcmp) as the corresponding symtab. Since the directory is not
20421 used in the name of the symtab we don't use it in the name of the
20422 psymtabs we create. E.g. expand_line_sal requires this when
20423 finding psymtabs to expand. A good testcase for this is
20426 LOWPC is the lowest address in CU (or 0 if not known).
20428 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20429 for its PC<->lines mapping information. Otherwise only the filename
20430 table is read in. */
20433 dwarf_decode_lines (struct line_header
*lh
, struct dwarf2_cu
*cu
,
20434 CORE_ADDR lowpc
, int decode_mapping
)
20436 if (decode_mapping
)
20437 dwarf_decode_lines_1 (lh
, cu
, lowpc
);
20439 /* Make sure a symtab is created for every file, even files
20440 which contain only variables (i.e. no code with associated
20442 buildsym_compunit
*builder
= cu
->get_builder ();
20443 struct compunit_symtab
*cust
= builder
->get_compunit_symtab ();
20445 for (auto &fe
: lh
->file_names ())
20447 dwarf2_start_subfile (cu
, fe
.name
, fe
.include_dir (lh
));
20448 if (builder
->get_current_subfile ()->symtab
== NULL
)
20450 builder
->get_current_subfile ()->symtab
20451 = allocate_symtab (cust
,
20452 builder
->get_current_subfile ()->name
.c_str ());
20454 fe
.symtab
= builder
->get_current_subfile ()->symtab
;
20458 /* Start a subfile for DWARF. FILENAME is the name of the file and
20459 DIRNAME the name of the source directory which contains FILENAME
20460 or NULL if not known.
20461 This routine tries to keep line numbers from identical absolute and
20462 relative file names in a common subfile.
20464 Using the `list' example from the GDB testsuite, which resides in
20465 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20466 of /srcdir/list0.c yields the following debugging information for list0.c:
20468 DW_AT_name: /srcdir/list0.c
20469 DW_AT_comp_dir: /compdir
20470 files.files[0].name: list0.h
20471 files.files[0].dir: /srcdir
20472 files.files[1].name: list0.c
20473 files.files[1].dir: /srcdir
20475 The line number information for list0.c has to end up in a single
20476 subfile, so that `break /srcdir/list0.c:1' works as expected.
20477 start_subfile will ensure that this happens provided that we pass the
20478 concatenation of files.files[1].dir and files.files[1].name as the
20482 dwarf2_start_subfile (struct dwarf2_cu
*cu
, const char *filename
,
20483 const char *dirname
)
20487 /* In order not to lose the line information directory,
20488 we concatenate it to the filename when it makes sense.
20489 Note that the Dwarf3 standard says (speaking of filenames in line
20490 information): ``The directory index is ignored for file names
20491 that represent full path names''. Thus ignoring dirname in the
20492 `else' branch below isn't an issue. */
20494 if (!IS_ABSOLUTE_PATH (filename
) && dirname
!= NULL
)
20496 copy
= path_join (dirname
, filename
);
20497 filename
= copy
.c_str ();
20500 cu
->get_builder ()->start_subfile (filename
);
20504 var_decode_location (struct attribute
*attr
, struct symbol
*sym
,
20505 struct dwarf2_cu
*cu
)
20507 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
20508 struct comp_unit_head
*cu_header
= &cu
->header
;
20510 /* NOTE drow/2003-01-30: There used to be a comment and some special
20511 code here to turn a symbol with DW_AT_external and a
20512 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20513 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20514 with some versions of binutils) where shared libraries could have
20515 relocations against symbols in their debug information - the
20516 minimal symbol would have the right address, but the debug info
20517 would not. It's no longer necessary, because we will explicitly
20518 apply relocations when we read in the debug information now. */
20520 /* A DW_AT_location attribute with no contents indicates that a
20521 variable has been optimized away. */
20522 if (attr
->form_is_block () && attr
->as_block ()->size
== 0)
20524 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20528 /* Handle one degenerate form of location expression specially, to
20529 preserve GDB's previous behavior when section offsets are
20530 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20531 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20533 if (attr
->form_is_block ())
20535 struct dwarf_block
*block
= attr
->as_block ();
20537 if ((block
->data
[0] == DW_OP_addr
20538 && block
->size
== 1 + cu_header
->addr_size
)
20539 || ((block
->data
[0] == DW_OP_GNU_addr_index
20540 || block
->data
[0] == DW_OP_addrx
)
20542 == 1 + leb128_size (&block
->data
[1]))))
20544 unsigned int dummy
;
20546 if (block
->data
[0] == DW_OP_addr
)
20547 sym
->set_value_address
20548 (cu
->header
.read_address (objfile
->obfd
, block
->data
+ 1,
20551 sym
->set_value_address
20552 (read_addr_index_from_leb128 (cu
, block
->data
+ 1, &dummy
));
20553 sym
->set_aclass_index (LOC_STATIC
);
20554 fixup_symbol_section (sym
, objfile
);
20555 sym
->set_value_address
20556 (sym
->value_address ()
20557 + objfile
->section_offsets
[sym
->section_index ()]);
20562 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20563 expression evaluator, and use LOC_COMPUTED only when necessary
20564 (i.e. when the value of a register or memory location is
20565 referenced, or a thread-local block, etc.). Then again, it might
20566 not be worthwhile. I'm assuming that it isn't unless performance
20567 or memory numbers show me otherwise. */
20569 dwarf2_symbol_mark_computed (attr
, sym
, cu
, 0);
20571 if (SYMBOL_COMPUTED_OPS (sym
)->location_has_loclist
)
20572 cu
->has_loclist
= true;
20575 /* Given a pointer to a DWARF information entry, figure out if we need
20576 to make a symbol table entry for it, and if so, create a new entry
20577 and return a pointer to it.
20578 If TYPE is NULL, determine symbol type from the die, otherwise
20579 used the passed type.
20580 If SPACE is not NULL, use it to hold the new symbol. If it is
20581 NULL, allocate a new symbol on the objfile's obstack. */
20583 static struct symbol
*
20584 new_symbol (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
20585 struct symbol
*space
)
20587 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
20588 struct objfile
*objfile
= per_objfile
->objfile
;
20589 struct gdbarch
*gdbarch
= objfile
->arch ();
20590 struct symbol
*sym
= NULL
;
20592 struct attribute
*attr
= NULL
;
20593 struct attribute
*attr2
= NULL
;
20594 CORE_ADDR baseaddr
;
20595 struct pending
**list_to_add
= NULL
;
20597 int inlined_func
= (die
->tag
== DW_TAG_inlined_subroutine
);
20599 baseaddr
= objfile
->text_section_offset ();
20601 name
= dwarf2_name (die
, cu
);
20604 int suppress_add
= 0;
20609 sym
= new (&objfile
->objfile_obstack
) symbol
;
20610 OBJSTAT (objfile
, n_syms
++);
20612 /* Cache this symbol's name and the name's demangled form (if any). */
20613 sym
->set_language (cu
->per_cu
->lang
, &objfile
->objfile_obstack
);
20614 /* Fortran does not have mangling standard and the mangling does differ
20615 between gfortran, iFort etc. */
20616 const char *physname
20617 = (cu
->per_cu
->lang
== language_fortran
20618 ? dwarf2_full_name (name
, die
, cu
)
20619 : dwarf2_physname (name
, die
, cu
));
20620 const char *linkagename
= dw2_linkage_name (die
, cu
);
20622 if (linkagename
== nullptr || cu
->per_cu
->lang
== language_ada
)
20623 sym
->set_linkage_name (physname
);
20626 sym
->set_demangled_name (physname
, &objfile
->objfile_obstack
);
20627 sym
->set_linkage_name (linkagename
);
20630 /* Handle DW_AT_artificial. */
20631 attr
= dwarf2_attr (die
, DW_AT_artificial
, cu
);
20632 if (attr
!= nullptr)
20633 sym
->set_is_artificial (attr
->as_boolean ());
20635 /* Default assumptions.
20636 Use the passed type or decode it from the die. */
20637 sym
->set_domain (VAR_DOMAIN
);
20638 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20640 sym
->set_type (type
);
20642 sym
->set_type (die_type (die
, cu
));
20643 attr
= dwarf2_attr (die
,
20644 inlined_func
? DW_AT_call_line
: DW_AT_decl_line
,
20646 if (attr
!= nullptr)
20647 sym
->set_line (attr
->constant_value (0));
20649 attr
= dwarf2_attr (die
,
20650 inlined_func
? DW_AT_call_file
: DW_AT_decl_file
,
20652 if (attr
!= nullptr && attr
->is_nonnegative ())
20654 file_name_index file_index
20655 = (file_name_index
) attr
->as_nonnegative ();
20656 struct file_entry
*fe
;
20658 if (cu
->line_header
!= NULL
)
20659 fe
= cu
->line_header
->file_name_at (file_index
);
20664 complaint (_("file index out of range"));
20666 sym
->set_symtab (fe
->symtab
);
20672 attr
= dwarf2_attr (die
, DW_AT_low_pc
, cu
);
20673 if (attr
!= nullptr)
20677 addr
= attr
->as_address ();
20678 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ baseaddr
);
20679 sym
->set_value_address (addr
);
20680 sym
->set_aclass_index (LOC_LABEL
);
20683 sym
->set_aclass_index (LOC_OPTIMIZED_OUT
);
20684 sym
->set_type (objfile_type (objfile
)->builtin_core_addr
);
20685 sym
->set_domain (LABEL_DOMAIN
);
20686 add_symbol_to_list (sym
, cu
->list_in_scope
);
20688 case DW_TAG_subprogram
:
20689 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20691 sym
->set_aclass_index (LOC_BLOCK
);
20692 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20693 if ((attr2
!= nullptr && attr2
->as_boolean ())
20694 || cu
->per_cu
->lang
== language_ada
20695 || cu
->per_cu
->lang
== language_fortran
)
20697 /* Subprograms marked external are stored as a global symbol.
20698 Ada and Fortran subprograms, whether marked external or
20699 not, are always stored as a global symbol, because we want
20700 to be able to access them globally. For instance, we want
20701 to be able to break on a nested subprogram without having
20702 to specify the context. */
20703 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20707 list_to_add
= cu
->list_in_scope
;
20710 case DW_TAG_inlined_subroutine
:
20711 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20713 sym
->set_aclass_index (LOC_BLOCK
);
20714 sym
->set_is_inlined (1);
20715 list_to_add
= cu
->list_in_scope
;
20717 case DW_TAG_template_value_param
:
20719 /* Fall through. */
20720 case DW_TAG_constant
:
20721 case DW_TAG_variable
:
20722 case DW_TAG_member
:
20723 /* Compilation with minimal debug info may result in
20724 variables with missing type entries. Change the
20725 misleading `void' type to something sensible. */
20726 if (sym
->type ()->code () == TYPE_CODE_VOID
)
20727 sym
->set_type (objfile_type (objfile
)->builtin_int
);
20729 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20730 /* In the case of DW_TAG_member, we should only be called for
20731 static const members. */
20732 if (die
->tag
== DW_TAG_member
)
20734 /* dwarf2_add_field uses die_is_declaration,
20735 so we do the same. */
20736 gdb_assert (die_is_declaration (die
, cu
));
20739 if (attr
!= nullptr)
20741 dwarf2_const_value (attr
, sym
, cu
);
20742 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20745 if (attr2
!= nullptr && attr2
->as_boolean ())
20746 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20748 list_to_add
= cu
->list_in_scope
;
20752 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20753 if (attr
!= nullptr)
20755 var_decode_location (attr
, sym
, cu
);
20756 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20758 /* Fortran explicitly imports any global symbols to the local
20759 scope by DW_TAG_common_block. */
20760 if (cu
->per_cu
->lang
== language_fortran
&& die
->parent
20761 && die
->parent
->tag
== DW_TAG_common_block
)
20764 if (sym
->aclass () == LOC_STATIC
20765 && sym
->value_address () == 0
20766 && !per_objfile
->per_bfd
->has_section_at_zero
)
20768 /* When a static variable is eliminated by the linker,
20769 the corresponding debug information is not stripped
20770 out, but the variable address is set to null;
20771 do not add such variables into symbol table. */
20773 else if (attr2
!= nullptr && attr2
->as_boolean ())
20775 if (sym
->aclass () == LOC_STATIC
20776 && (objfile
->flags
& OBJF_MAINLINE
) == 0
20777 && per_objfile
->per_bfd
->can_copy
)
20779 /* A global static variable might be subject to
20780 copy relocation. We first check for a local
20781 minsym, though, because maybe the symbol was
20782 marked hidden, in which case this would not
20784 bound_minimal_symbol found
20785 = (lookup_minimal_symbol_linkage
20786 (sym
->linkage_name (), objfile
));
20787 if (found
.minsym
!= nullptr)
20788 sym
->maybe_copied
= 1;
20791 /* A variable with DW_AT_external is never static,
20792 but it may be block-scoped. */
20794 = ((cu
->list_in_scope
20795 == cu
->get_builder ()->get_file_symbols ())
20796 ? cu
->get_builder ()->get_global_symbols ()
20797 : cu
->list_in_scope
);
20800 list_to_add
= cu
->list_in_scope
;
20804 /* We do not know the address of this symbol.
20805 If it is an external symbol and we have type information
20806 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20807 The address of the variable will then be determined from
20808 the minimal symbol table whenever the variable is
20810 attr2
= dwarf2_attr (die
, DW_AT_external
, cu
);
20812 /* Fortran explicitly imports any global symbols to the local
20813 scope by DW_TAG_common_block. */
20814 if (cu
->per_cu
->lang
== language_fortran
&& die
->parent
20815 && die
->parent
->tag
== DW_TAG_common_block
)
20817 /* SYMBOL_CLASS doesn't matter here because
20818 read_common_block is going to reset it. */
20820 list_to_add
= cu
->list_in_scope
;
20822 else if (attr2
!= nullptr && attr2
->as_boolean ()
20823 && dwarf2_attr (die
, DW_AT_type
, cu
) != NULL
)
20825 /* A variable with DW_AT_external is never static, but it
20826 may be block-scoped. */
20828 = ((cu
->list_in_scope
20829 == cu
->get_builder ()->get_file_symbols ())
20830 ? cu
->get_builder ()->get_global_symbols ()
20831 : cu
->list_in_scope
);
20833 sym
->set_aclass_index (LOC_UNRESOLVED
);
20835 else if (!die_is_declaration (die
, cu
))
20837 /* Use the default LOC_OPTIMIZED_OUT class. */
20838 gdb_assert (sym
->aclass () == LOC_OPTIMIZED_OUT
);
20840 list_to_add
= cu
->list_in_scope
;
20844 case DW_TAG_formal_parameter
:
20846 /* If we are inside a function, mark this as an argument. If
20847 not, we might be looking at an argument to an inlined function
20848 when we do not have enough information to show inlined frames;
20849 pretend it's a local variable in that case so that the user can
20851 struct context_stack
*curr
20852 = cu
->get_builder ()->get_current_context_stack ();
20853 if (curr
!= nullptr && curr
->name
!= nullptr)
20854 sym
->set_is_argument (1);
20855 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
20856 if (attr
!= nullptr)
20858 var_decode_location (attr
, sym
, cu
);
20860 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20861 if (attr
!= nullptr)
20863 dwarf2_const_value (attr
, sym
, cu
);
20866 list_to_add
= cu
->list_in_scope
;
20869 case DW_TAG_unspecified_parameters
:
20870 /* From varargs functions; gdb doesn't seem to have any
20871 interest in this information, so just ignore it for now.
20874 case DW_TAG_template_type_param
:
20876 /* Fall through. */
20877 case DW_TAG_class_type
:
20878 case DW_TAG_interface_type
:
20879 case DW_TAG_structure_type
:
20880 case DW_TAG_union_type
:
20881 case DW_TAG_set_type
:
20882 case DW_TAG_enumeration_type
:
20883 case DW_TAG_namelist
:
20884 if (die
->tag
== DW_TAG_namelist
)
20886 sym
->set_aclass_index (LOC_STATIC
);
20887 sym
->set_domain (VAR_DOMAIN
);
20891 sym
->set_aclass_index (LOC_TYPEDEF
);
20892 sym
->set_domain (STRUCT_DOMAIN
);
20895 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20896 really ever be static objects: otherwise, if you try
20897 to, say, break of a class's method and you're in a file
20898 which doesn't mention that class, it won't work unless
20899 the check for all static symbols in lookup_symbol_aux
20900 saves you. See the OtherFileClass tests in
20901 gdb.c++/namespace.exp. */
20905 buildsym_compunit
*builder
= cu
->get_builder ();
20907 = (cu
->list_in_scope
== builder
->get_file_symbols ()
20908 && cu
->per_cu
->lang
== language_cplus
20909 ? builder
->get_global_symbols ()
20910 : cu
->list_in_scope
);
20912 /* The semantics of C++ state that "struct foo {
20913 ... }" also defines a typedef for "foo". */
20914 if (cu
->per_cu
->lang
== language_cplus
20915 || cu
->per_cu
->lang
== language_ada
20916 || cu
->per_cu
->lang
== language_d
20917 || cu
->per_cu
->lang
== language_rust
)
20919 /* The symbol's name is already allocated along
20920 with this objfile, so we don't need to
20921 duplicate it for the type. */
20922 if (sym
->type ()->name () == 0)
20923 sym
->type ()->set_name (sym
->search_name ());
20928 case DW_TAG_typedef
:
20929 sym
->set_aclass_index (LOC_TYPEDEF
);
20930 sym
->set_domain (VAR_DOMAIN
);
20931 list_to_add
= cu
->list_in_scope
;
20933 case DW_TAG_array_type
:
20934 case DW_TAG_base_type
:
20935 case DW_TAG_subrange_type
:
20936 case DW_TAG_generic_subrange
:
20937 sym
->set_aclass_index (LOC_TYPEDEF
);
20938 sym
->set_domain (VAR_DOMAIN
);
20939 list_to_add
= cu
->list_in_scope
;
20941 case DW_TAG_enumerator
:
20942 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
20943 if (attr
!= nullptr)
20945 dwarf2_const_value (attr
, sym
, cu
);
20948 /* NOTE: carlton/2003-11-10: See comment above in the
20949 DW_TAG_class_type, etc. block. */
20952 = (cu
->list_in_scope
== cu
->get_builder ()->get_file_symbols ()
20953 && cu
->per_cu
->lang
== language_cplus
20954 ? cu
->get_builder ()->get_global_symbols ()
20955 : cu
->list_in_scope
);
20958 case DW_TAG_imported_declaration
:
20959 case DW_TAG_namespace
:
20960 sym
->set_aclass_index (LOC_TYPEDEF
);
20961 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20963 case DW_TAG_module
:
20964 sym
->set_aclass_index (LOC_TYPEDEF
);
20965 sym
->set_domain (MODULE_DOMAIN
);
20966 list_to_add
= cu
->get_builder ()->get_global_symbols ();
20968 case DW_TAG_common_block
:
20969 sym
->set_aclass_index (LOC_COMMON_BLOCK
);
20970 sym
->set_domain (COMMON_BLOCK_DOMAIN
);
20971 add_symbol_to_list (sym
, cu
->list_in_scope
);
20974 /* Not a tag we recognize. Hopefully we aren't processing
20975 trash data, but since we must specifically ignore things
20976 we don't recognize, there is nothing else we should do at
20978 complaint (_("unsupported tag: '%s'"),
20979 dwarf_tag_name (die
->tag
));
20985 sym
->hash_next
= objfile
->template_symbols
;
20986 objfile
->template_symbols
= sym
;
20987 list_to_add
= NULL
;
20990 if (list_to_add
!= NULL
)
20991 add_symbol_to_list (sym
, list_to_add
);
20993 /* For the benefit of old versions of GCC, check for anonymous
20994 namespaces based on the demangled name. */
20995 if (!cu
->processing_has_namespace_info
20996 && cu
->per_cu
->lang
== language_cplus
)
20997 cp_scan_for_anonymous_namespaces (cu
->get_builder (), sym
, objfile
);
21002 /* Given an attr with a DW_FORM_dataN value in host byte order,
21003 zero-extend it as appropriate for the symbol's type. The DWARF
21004 standard (v4) is not entirely clear about the meaning of using
21005 DW_FORM_dataN for a constant with a signed type, where the type is
21006 wider than the data. The conclusion of a discussion on the DWARF
21007 list was that this is unspecified. We choose to always zero-extend
21008 because that is the interpretation long in use by GCC. */
21011 dwarf2_const_value_data (const struct attribute
*attr
, struct obstack
*obstack
,
21012 struct dwarf2_cu
*cu
, LONGEST
*value
, int bits
)
21014 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21015 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
) ?
21016 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
21017 LONGEST l
= attr
->constant_value (0);
21019 if (bits
< sizeof (*value
) * 8)
21021 l
&= ((LONGEST
) 1 << bits
) - 1;
21024 else if (bits
== sizeof (*value
) * 8)
21028 gdb_byte
*bytes
= (gdb_byte
*) obstack_alloc (obstack
, bits
/ 8);
21029 store_unsigned_integer (bytes
, bits
/ 8, byte_order
, l
);
21036 /* Read a constant value from an attribute. Either set *VALUE, or if
21037 the value does not fit in *VALUE, set *BYTES - either already
21038 allocated on the objfile obstack, or newly allocated on OBSTACK,
21039 or, set *BATON, if we translated the constant to a location
21043 dwarf2_const_value_attr (const struct attribute
*attr
, struct type
*type
,
21044 const char *name
, struct obstack
*obstack
,
21045 struct dwarf2_cu
*cu
,
21046 LONGEST
*value
, const gdb_byte
**bytes
,
21047 struct dwarf2_locexpr_baton
**baton
)
21049 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21050 struct objfile
*objfile
= per_objfile
->objfile
;
21051 struct comp_unit_head
*cu_header
= &cu
->header
;
21052 struct dwarf_block
*blk
;
21053 enum bfd_endian byte_order
= (bfd_big_endian (objfile
->obfd
) ?
21054 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
21060 switch (attr
->form
)
21063 case DW_FORM_addrx
:
21064 case DW_FORM_GNU_addr_index
:
21068 if (TYPE_LENGTH (type
) != cu_header
->addr_size
)
21069 dwarf2_const_value_length_mismatch_complaint (name
,
21070 cu_header
->addr_size
,
21071 TYPE_LENGTH (type
));
21072 /* Symbols of this form are reasonably rare, so we just
21073 piggyback on the existing location code rather than writing
21074 a new implementation of symbol_computed_ops. */
21075 *baton
= XOBNEW (obstack
, struct dwarf2_locexpr_baton
);
21076 (*baton
)->per_objfile
= per_objfile
;
21077 (*baton
)->per_cu
= cu
->per_cu
;
21078 gdb_assert ((*baton
)->per_cu
);
21080 (*baton
)->size
= 2 + cu_header
->addr_size
;
21081 data
= (gdb_byte
*) obstack_alloc (obstack
, (*baton
)->size
);
21082 (*baton
)->data
= data
;
21084 data
[0] = DW_OP_addr
;
21085 store_unsigned_integer (&data
[1], cu_header
->addr_size
,
21086 byte_order
, attr
->as_address ());
21087 data
[cu_header
->addr_size
+ 1] = DW_OP_stack_value
;
21090 case DW_FORM_string
:
21093 case DW_FORM_GNU_str_index
:
21094 case DW_FORM_GNU_strp_alt
:
21095 /* The string is already allocated on the objfile obstack, point
21097 *bytes
= (const gdb_byte
*) attr
->as_string ();
21099 case DW_FORM_block1
:
21100 case DW_FORM_block2
:
21101 case DW_FORM_block4
:
21102 case DW_FORM_block
:
21103 case DW_FORM_exprloc
:
21104 case DW_FORM_data16
:
21105 blk
= attr
->as_block ();
21106 if (TYPE_LENGTH (type
) != blk
->size
)
21107 dwarf2_const_value_length_mismatch_complaint (name
, blk
->size
,
21108 TYPE_LENGTH (type
));
21109 *bytes
= blk
->data
;
21112 /* The DW_AT_const_value attributes are supposed to carry the
21113 symbol's value "represented as it would be on the target
21114 architecture." By the time we get here, it's already been
21115 converted to host endianness, so we just need to sign- or
21116 zero-extend it as appropriate. */
21117 case DW_FORM_data1
:
21118 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 8);
21120 case DW_FORM_data2
:
21121 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 16);
21123 case DW_FORM_data4
:
21124 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 32);
21126 case DW_FORM_data8
:
21127 *bytes
= dwarf2_const_value_data (attr
, obstack
, cu
, value
, 64);
21130 case DW_FORM_sdata
:
21131 case DW_FORM_implicit_const
:
21132 *value
= attr
->as_signed ();
21135 case DW_FORM_udata
:
21136 *value
= attr
->as_unsigned ();
21140 complaint (_("unsupported const value attribute form: '%s'"),
21141 dwarf_form_name (attr
->form
));
21148 /* Copy constant value from an attribute to a symbol. */
21151 dwarf2_const_value (const struct attribute
*attr
, struct symbol
*sym
,
21152 struct dwarf2_cu
*cu
)
21154 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21156 const gdb_byte
*bytes
;
21157 struct dwarf2_locexpr_baton
*baton
;
21159 dwarf2_const_value_attr (attr
, sym
->type (),
21160 sym
->print_name (),
21161 &objfile
->objfile_obstack
, cu
,
21162 &value
, &bytes
, &baton
);
21166 SYMBOL_LOCATION_BATON (sym
) = baton
;
21167 sym
->set_aclass_index (dwarf2_locexpr_index
);
21169 else if (bytes
!= NULL
)
21171 sym
->set_value_bytes (bytes
);
21172 sym
->set_aclass_index (LOC_CONST_BYTES
);
21176 sym
->set_value_longest (value
);
21177 sym
->set_aclass_index (LOC_CONST
);
21181 /* Return the type of the die in question using its DW_AT_type attribute. */
21183 static struct type
*
21184 die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21186 struct attribute
*type_attr
;
21188 type_attr
= dwarf2_attr (die
, DW_AT_type
, cu
);
21191 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21192 /* A missing DW_AT_type represents a void type. */
21193 return objfile_type (objfile
)->builtin_void
;
21196 return lookup_die_type (die
, type_attr
, cu
);
21199 /* True iff CU's producer generates GNAT Ada auxiliary information
21200 that allows to find parallel types through that information instead
21201 of having to do expensive parallel lookups by type name. */
21204 need_gnat_info (struct dwarf2_cu
*cu
)
21206 /* Assume that the Ada compiler was GNAT, which always produces
21207 the auxiliary information. */
21208 return (cu
->per_cu
->lang
== language_ada
);
21211 /* Return the auxiliary type of the die in question using its
21212 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21213 attribute is not present. */
21215 static struct type
*
21216 die_descriptive_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21218 struct attribute
*type_attr
;
21220 type_attr
= dwarf2_attr (die
, DW_AT_GNAT_descriptive_type
, cu
);
21224 return lookup_die_type (die
, type_attr
, cu
);
21227 /* If DIE has a descriptive_type attribute, then set the TYPE's
21228 descriptive type accordingly. */
21231 set_descriptive_type (struct type
*type
, struct die_info
*die
,
21232 struct dwarf2_cu
*cu
)
21234 struct type
*descriptive_type
= die_descriptive_type (die
, cu
);
21236 if (descriptive_type
)
21238 ALLOCATE_GNAT_AUX_TYPE (type
);
21239 TYPE_DESCRIPTIVE_TYPE (type
) = descriptive_type
;
21243 /* Return the containing type of the die in question using its
21244 DW_AT_containing_type attribute. */
21246 static struct type
*
21247 die_containing_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
21249 struct attribute
*type_attr
;
21250 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21252 type_attr
= dwarf2_attr (die
, DW_AT_containing_type
, cu
);
21254 error (_("Dwarf Error: Problem turning containing type into gdb type "
21255 "[in module %s]"), objfile_name (objfile
));
21257 return lookup_die_type (die
, type_attr
, cu
);
21260 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21262 static struct type
*
21263 build_error_marker_type (struct dwarf2_cu
*cu
, struct die_info
*die
)
21265 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21266 struct objfile
*objfile
= per_objfile
->objfile
;
21269 std::string message
21270 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21271 objfile_name (objfile
),
21272 sect_offset_str (cu
->header
.sect_off
),
21273 sect_offset_str (die
->sect_off
));
21274 saved
= obstack_strdup (&objfile
->objfile_obstack
, message
);
21276 return init_type (objfile
, TYPE_CODE_ERROR
, 0, saved
);
21279 /* Look up the type of DIE in CU using its type attribute ATTR.
21280 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21281 DW_AT_containing_type.
21282 If there is no type substitute an error marker. */
21284 static struct type
*
21285 lookup_die_type (struct die_info
*die
, const struct attribute
*attr
,
21286 struct dwarf2_cu
*cu
)
21288 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21289 struct objfile
*objfile
= per_objfile
->objfile
;
21290 struct type
*this_type
;
21292 gdb_assert (attr
->name
== DW_AT_type
21293 || attr
->name
== DW_AT_GNAT_descriptive_type
21294 || attr
->name
== DW_AT_containing_type
);
21296 /* First see if we have it cached. */
21298 if (attr
->form
== DW_FORM_GNU_ref_alt
)
21300 struct dwarf2_per_cu_data
*per_cu
;
21301 sect_offset sect_off
= attr
->get_ref_die_offset ();
21303 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, 1,
21304 per_objfile
->per_bfd
);
21305 this_type
= get_die_type_at_offset (sect_off
, per_cu
, per_objfile
);
21307 else if (attr
->form_is_ref ())
21309 sect_offset sect_off
= attr
->get_ref_die_offset ();
21311 this_type
= get_die_type_at_offset (sect_off
, cu
->per_cu
, per_objfile
);
21313 else if (attr
->form
== DW_FORM_ref_sig8
)
21315 ULONGEST signature
= attr
->as_signature ();
21317 return get_signatured_type (die
, signature
, cu
);
21321 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21322 " at %s [in module %s]"),
21323 dwarf_attr_name (attr
->name
), sect_offset_str (die
->sect_off
),
21324 objfile_name (objfile
));
21325 return build_error_marker_type (cu
, die
);
21328 /* If not cached we need to read it in. */
21330 if (this_type
== NULL
)
21332 struct die_info
*type_die
= NULL
;
21333 struct dwarf2_cu
*type_cu
= cu
;
21335 if (attr
->form_is_ref ())
21336 type_die
= follow_die_ref (die
, attr
, &type_cu
);
21337 if (type_die
== NULL
)
21338 return build_error_marker_type (cu
, die
);
21339 /* If we find the type now, it's probably because the type came
21340 from an inter-CU reference and the type's CU got expanded before
21342 this_type
= read_type_die (type_die
, type_cu
);
21345 /* If we still don't have a type use an error marker. */
21347 if (this_type
== NULL
)
21348 return build_error_marker_type (cu
, die
);
21353 /* Return the type in DIE, CU.
21354 Returns NULL for invalid types.
21356 This first does a lookup in die_type_hash,
21357 and only reads the die in if necessary.
21359 NOTE: This can be called when reading in partial or full symbols. */
21361 static struct type
*
21362 read_type_die (struct die_info
*die
, struct dwarf2_cu
*cu
)
21364 struct type
*this_type
;
21366 this_type
= get_die_type (die
, cu
);
21370 return read_type_die_1 (die
, cu
);
21373 /* Read the type in DIE, CU.
21374 Returns NULL for invalid types. */
21376 static struct type
*
21377 read_type_die_1 (struct die_info
*die
, struct dwarf2_cu
*cu
)
21379 struct type
*this_type
= NULL
;
21383 case DW_TAG_class_type
:
21384 case DW_TAG_interface_type
:
21385 case DW_TAG_structure_type
:
21386 case DW_TAG_union_type
:
21387 this_type
= read_structure_type (die
, cu
);
21389 case DW_TAG_enumeration_type
:
21390 this_type
= read_enumeration_type (die
, cu
);
21392 case DW_TAG_subprogram
:
21393 case DW_TAG_subroutine_type
:
21394 case DW_TAG_inlined_subroutine
:
21395 this_type
= read_subroutine_type (die
, cu
);
21397 case DW_TAG_array_type
:
21398 this_type
= read_array_type (die
, cu
);
21400 case DW_TAG_set_type
:
21401 this_type
= read_set_type (die
, cu
);
21403 case DW_TAG_pointer_type
:
21404 this_type
= read_tag_pointer_type (die
, cu
);
21406 case DW_TAG_ptr_to_member_type
:
21407 this_type
= read_tag_ptr_to_member_type (die
, cu
);
21409 case DW_TAG_reference_type
:
21410 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_REF
);
21412 case DW_TAG_rvalue_reference_type
:
21413 this_type
= read_tag_reference_type (die
, cu
, TYPE_CODE_RVALUE_REF
);
21415 case DW_TAG_const_type
:
21416 this_type
= read_tag_const_type (die
, cu
);
21418 case DW_TAG_volatile_type
:
21419 this_type
= read_tag_volatile_type (die
, cu
);
21421 case DW_TAG_restrict_type
:
21422 this_type
= read_tag_restrict_type (die
, cu
);
21424 case DW_TAG_string_type
:
21425 this_type
= read_tag_string_type (die
, cu
);
21427 case DW_TAG_typedef
:
21428 this_type
= read_typedef (die
, cu
);
21430 case DW_TAG_generic_subrange
:
21431 case DW_TAG_subrange_type
:
21432 this_type
= read_subrange_type (die
, cu
);
21434 case DW_TAG_base_type
:
21435 this_type
= read_base_type (die
, cu
);
21437 case DW_TAG_unspecified_type
:
21438 this_type
= read_unspecified_type (die
, cu
);
21440 case DW_TAG_namespace
:
21441 this_type
= read_namespace_type (die
, cu
);
21443 case DW_TAG_module
:
21444 this_type
= read_module_type (die
, cu
);
21446 case DW_TAG_atomic_type
:
21447 this_type
= read_tag_atomic_type (die
, cu
);
21450 complaint (_("unexpected tag in read_type_die: '%s'"),
21451 dwarf_tag_name (die
->tag
));
21458 /* See if we can figure out if the class lives in a namespace. We do
21459 this by looking for a member function; its demangled name will
21460 contain namespace info, if there is any.
21461 Return the computed name or NULL.
21462 Space for the result is allocated on the objfile's obstack.
21463 This is the full-die version of guess_partial_die_structure_name.
21464 In this case we know DIE has no useful parent. */
21466 static const char *
21467 guess_full_die_structure_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21469 struct die_info
*spec_die
;
21470 struct dwarf2_cu
*spec_cu
;
21471 struct die_info
*child
;
21472 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21475 spec_die
= die_specification (die
, &spec_cu
);
21476 if (spec_die
!= NULL
)
21482 for (child
= die
->child
;
21484 child
= child
->sibling
)
21486 if (child
->tag
== DW_TAG_subprogram
)
21488 const char *linkage_name
= dw2_linkage_name (child
, cu
);
21490 if (linkage_name
!= NULL
)
21492 gdb::unique_xmalloc_ptr
<char> actual_name
21493 (cu
->language_defn
->class_name_from_physname (linkage_name
));
21494 const char *name
= NULL
;
21496 if (actual_name
!= NULL
)
21498 const char *die_name
= dwarf2_name (die
, cu
);
21500 if (die_name
!= NULL
21501 && strcmp (die_name
, actual_name
.get ()) != 0)
21503 /* Strip off the class name from the full name.
21504 We want the prefix. */
21505 int die_name_len
= strlen (die_name
);
21506 int actual_name_len
= strlen (actual_name
.get ());
21507 const char *ptr
= actual_name
.get ();
21509 /* Test for '::' as a sanity check. */
21510 if (actual_name_len
> die_name_len
+ 2
21511 && ptr
[actual_name_len
- die_name_len
- 1] == ':')
21512 name
= obstack_strndup (
21513 &objfile
->per_bfd
->storage_obstack
,
21514 ptr
, actual_name_len
- die_name_len
- 2);
21525 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21526 prefix part in such case. See
21527 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21529 static const char *
21530 anonymous_struct_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21532 struct attribute
*attr
;
21535 if (die
->tag
!= DW_TAG_class_type
&& die
->tag
!= DW_TAG_interface_type
21536 && die
->tag
!= DW_TAG_structure_type
&& die
->tag
!= DW_TAG_union_type
)
21539 if (dwarf2_string_attr (die
, DW_AT_name
, cu
) != NULL
)
21542 attr
= dw2_linkage_name_attr (die
, cu
);
21543 const char *attr_name
= attr
->as_string ();
21544 if (attr
== NULL
|| attr_name
== NULL
)
21547 /* dwarf2_name had to be already called. */
21548 gdb_assert (attr
->canonical_string_p ());
21550 /* Strip the base name, keep any leading namespaces/classes. */
21551 base
= strrchr (attr_name
, ':');
21552 if (base
== NULL
|| base
== attr_name
|| base
[-1] != ':')
21555 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21556 return obstack_strndup (&objfile
->per_bfd
->storage_obstack
,
21558 &base
[-1] - attr_name
);
21561 /* Return the name of the namespace/class that DIE is defined within,
21562 or "" if we can't tell. The caller should not xfree the result.
21564 For example, if we're within the method foo() in the following
21574 then determine_prefix on foo's die will return "N::C". */
21576 static const char *
21577 determine_prefix (struct die_info
*die
, struct dwarf2_cu
*cu
)
21579 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
21580 struct die_info
*parent
, *spec_die
;
21581 struct dwarf2_cu
*spec_cu
;
21582 struct type
*parent_type
;
21583 const char *retval
;
21585 if (cu
->per_cu
->lang
!= language_cplus
21586 && cu
->per_cu
->lang
!= language_fortran
21587 && cu
->per_cu
->lang
!= language_d
21588 && cu
->per_cu
->lang
!= language_rust
)
21591 retval
= anonymous_struct_prefix (die
, cu
);
21595 /* We have to be careful in the presence of DW_AT_specification.
21596 For example, with GCC 3.4, given the code
21600 // Definition of N::foo.
21604 then we'll have a tree of DIEs like this:
21606 1: DW_TAG_compile_unit
21607 2: DW_TAG_namespace // N
21608 3: DW_TAG_subprogram // declaration of N::foo
21609 4: DW_TAG_subprogram // definition of N::foo
21610 DW_AT_specification // refers to die #3
21612 Thus, when processing die #4, we have to pretend that we're in
21613 the context of its DW_AT_specification, namely the contex of die
21616 spec_die
= die_specification (die
, &spec_cu
);
21617 if (spec_die
== NULL
)
21618 parent
= die
->parent
;
21621 parent
= spec_die
->parent
;
21625 if (parent
== NULL
)
21627 else if (parent
->building_fullname
)
21630 const char *parent_name
;
21632 /* It has been seen on RealView 2.2 built binaries,
21633 DW_TAG_template_type_param types actually _defined_ as
21634 children of the parent class:
21637 template class <class Enum> Class{};
21638 Class<enum E> class_e;
21640 1: DW_TAG_class_type (Class)
21641 2: DW_TAG_enumeration_type (E)
21642 3: DW_TAG_enumerator (enum1:0)
21643 3: DW_TAG_enumerator (enum2:1)
21645 2: DW_TAG_template_type_param
21646 DW_AT_type DW_FORM_ref_udata (E)
21648 Besides being broken debug info, it can put GDB into an
21649 infinite loop. Consider:
21651 When we're building the full name for Class<E>, we'll start
21652 at Class, and go look over its template type parameters,
21653 finding E. We'll then try to build the full name of E, and
21654 reach here. We're now trying to build the full name of E,
21655 and look over the parent DIE for containing scope. In the
21656 broken case, if we followed the parent DIE of E, we'd again
21657 find Class, and once again go look at its template type
21658 arguments, etc., etc. Simply don't consider such parent die
21659 as source-level parent of this die (it can't be, the language
21660 doesn't allow it), and break the loop here. */
21661 name
= dwarf2_name (die
, cu
);
21662 parent_name
= dwarf2_name (parent
, cu
);
21663 complaint (_("template param type '%s' defined within parent '%s'"),
21664 name
? name
: "<unknown>",
21665 parent_name
? parent_name
: "<unknown>");
21669 switch (parent
->tag
)
21671 case DW_TAG_namespace
:
21672 parent_type
= read_type_die (parent
, cu
);
21673 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21674 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21675 Work around this problem here. */
21676 if (cu
->per_cu
->lang
== language_cplus
21677 && strcmp (parent_type
->name (), "::") == 0)
21679 /* We give a name to even anonymous namespaces. */
21680 return parent_type
->name ();
21681 case DW_TAG_class_type
:
21682 case DW_TAG_interface_type
:
21683 case DW_TAG_structure_type
:
21684 case DW_TAG_union_type
:
21685 case DW_TAG_module
:
21686 parent_type
= read_type_die (parent
, cu
);
21687 if (parent_type
->name () != NULL
)
21688 return parent_type
->name ();
21690 /* An anonymous structure is only allowed non-static data
21691 members; no typedefs, no member functions, et cetera.
21692 So it does not need a prefix. */
21694 case DW_TAG_compile_unit
:
21695 case DW_TAG_partial_unit
:
21696 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21697 if (cu
->per_cu
->lang
== language_cplus
21698 && !per_objfile
->per_bfd
->types
.empty ()
21699 && die
->child
!= NULL
21700 && (die
->tag
== DW_TAG_class_type
21701 || die
->tag
== DW_TAG_structure_type
21702 || die
->tag
== DW_TAG_union_type
))
21704 const char *name
= guess_full_die_structure_name (die
, cu
);
21709 case DW_TAG_subprogram
:
21710 /* Nested subroutines in Fortran get a prefix with the name
21711 of the parent's subroutine. */
21712 if (cu
->per_cu
->lang
== language_fortran
)
21714 if ((die
->tag
== DW_TAG_subprogram
)
21715 && (dwarf2_name (parent
, cu
) != NULL
))
21716 return dwarf2_name (parent
, cu
);
21719 case DW_TAG_enumeration_type
:
21720 parent_type
= read_type_die (parent
, cu
);
21721 if (parent_type
->is_declared_class ())
21723 if (parent_type
->name () != NULL
)
21724 return parent_type
->name ();
21727 /* Fall through. */
21729 return determine_prefix (parent
, cu
);
21733 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21734 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21735 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21736 an obconcat, otherwise allocate storage for the result. The CU argument is
21737 used to determine the language and hence, the appropriate separator. */
21739 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21742 typename_concat (struct obstack
*obs
, const char *prefix
, const char *suffix
,
21743 int physname
, struct dwarf2_cu
*cu
)
21745 const char *lead
= "";
21748 if (suffix
== NULL
|| suffix
[0] == '\0'
21749 || prefix
== NULL
|| prefix
[0] == '\0')
21751 else if (cu
->per_cu
->lang
== language_d
)
21753 /* For D, the 'main' function could be defined in any module, but it
21754 should never be prefixed. */
21755 if (strcmp (suffix
, "D main") == 0)
21763 else if (cu
->per_cu
->lang
== language_fortran
&& physname
)
21765 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21766 DW_AT_MIPS_linkage_name is preferred and used instead. */
21774 if (prefix
== NULL
)
21776 if (suffix
== NULL
)
21783 xmalloc (strlen (prefix
) + MAX_SEP_LEN
+ strlen (suffix
) + 1));
21785 strcpy (retval
, lead
);
21786 strcat (retval
, prefix
);
21787 strcat (retval
, sep
);
21788 strcat (retval
, suffix
);
21793 /* We have an obstack. */
21794 return obconcat (obs
, lead
, prefix
, sep
, suffix
, (char *) NULL
);
21798 /* Get name of a die, return NULL if not found. */
21800 static const char *
21801 dwarf2_canonicalize_name (const char *name
, struct dwarf2_cu
*cu
,
21802 struct objfile
*objfile
)
21804 if (name
&& cu
->per_cu
->lang
== language_cplus
)
21806 gdb::unique_xmalloc_ptr
<char> canon_name
21807 = cp_canonicalize_string (name
);
21809 if (canon_name
!= nullptr)
21810 name
= objfile
->intern (canon_name
.get ());
21816 /* Get name of a die, return NULL if not found.
21817 Anonymous namespaces are converted to their magic string. */
21819 static const char *
21820 dwarf2_name (struct die_info
*die
, struct dwarf2_cu
*cu
)
21822 struct attribute
*attr
;
21823 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
21825 attr
= dwarf2_attr (die
, DW_AT_name
, cu
);
21826 const char *attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21827 if (attr_name
== nullptr
21828 && die
->tag
!= DW_TAG_namespace
21829 && die
->tag
!= DW_TAG_class_type
21830 && die
->tag
!= DW_TAG_interface_type
21831 && die
->tag
!= DW_TAG_structure_type
21832 && die
->tag
!= DW_TAG_namelist
21833 && die
->tag
!= DW_TAG_union_type
)
21838 case DW_TAG_compile_unit
:
21839 case DW_TAG_partial_unit
:
21840 /* Compilation units have a DW_AT_name that is a filename, not
21841 a source language identifier. */
21842 case DW_TAG_enumeration_type
:
21843 case DW_TAG_enumerator
:
21844 /* These tags always have simple identifiers already; no need
21845 to canonicalize them. */
21848 case DW_TAG_namespace
:
21849 if (attr_name
!= nullptr)
21851 return CP_ANONYMOUS_NAMESPACE_STR
;
21853 case DW_TAG_class_type
:
21854 case DW_TAG_interface_type
:
21855 case DW_TAG_structure_type
:
21856 case DW_TAG_union_type
:
21857 case DW_TAG_namelist
:
21858 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21859 structures or unions. These were of the form "._%d" in GCC 4.1,
21860 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21861 and GCC 4.4. We work around this problem by ignoring these. */
21862 if (attr_name
!= nullptr
21863 && (startswith (attr_name
, "._")
21864 || startswith (attr_name
, "<anonymous")))
21867 /* GCC might emit a nameless typedef that has a linkage name. See
21868 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21869 if (!attr
|| attr_name
== NULL
)
21871 attr
= dw2_linkage_name_attr (die
, cu
);
21872 attr_name
= attr
== nullptr ? nullptr : attr
->as_string ();
21873 if (attr
== NULL
|| attr_name
== NULL
)
21876 /* Avoid demangling attr_name the second time on a second
21877 call for the same DIE. */
21878 if (!attr
->canonical_string_p ())
21880 gdb::unique_xmalloc_ptr
<char> demangled
21881 (gdb_demangle (attr_name
, DMGL_TYPES
));
21882 if (demangled
== nullptr)
21885 attr
->set_string_canonical (objfile
->intern (demangled
.get ()));
21886 attr_name
= attr
->as_string ();
21889 /* Strip any leading namespaces/classes, keep only the
21890 base name. DW_AT_name for named DIEs does not
21891 contain the prefixes. */
21892 const char *base
= strrchr (attr_name
, ':');
21893 if (base
&& base
> attr_name
&& base
[-1] == ':')
21904 if (!attr
->canonical_string_p ())
21905 attr
->set_string_canonical (dwarf2_canonicalize_name (attr_name
, cu
,
21907 return attr
->as_string ();
21910 /* Return the die that this die in an extension of, or NULL if there
21911 is none. *EXT_CU is the CU containing DIE on input, and the CU
21912 containing the return value on output. */
21914 static struct die_info
*
21915 dwarf2_extension (struct die_info
*die
, struct dwarf2_cu
**ext_cu
)
21917 struct attribute
*attr
;
21919 attr
= dwarf2_attr (die
, DW_AT_extension
, *ext_cu
);
21923 return follow_die_ref (die
, attr
, ext_cu
);
21927 dump_die_shallow (struct ui_file
*f
, int indent
, struct die_info
*die
)
21931 gdb_printf (f
, "%*sDie: %s (abbrev %d, offset %s)\n",
21933 dwarf_tag_name (die
->tag
), die
->abbrev
,
21934 sect_offset_str (die
->sect_off
));
21936 if (die
->parent
!= NULL
)
21937 gdb_printf (f
, "%*s parent at offset: %s\n",
21939 sect_offset_str (die
->parent
->sect_off
));
21941 gdb_printf (f
, "%*s has children: %s\n",
21943 dwarf_bool_name (die
->child
!= NULL
));
21945 gdb_printf (f
, "%*s attributes:\n", indent
, "");
21947 for (i
= 0; i
< die
->num_attrs
; ++i
)
21949 gdb_printf (f
, "%*s %s (%s) ",
21951 dwarf_attr_name (die
->attrs
[i
].name
),
21952 dwarf_form_name (die
->attrs
[i
].form
));
21954 switch (die
->attrs
[i
].form
)
21957 case DW_FORM_addrx
:
21958 case DW_FORM_GNU_addr_index
:
21959 gdb_printf (f
, "address: ");
21960 gdb_puts (hex_string (die
->attrs
[i
].as_address ()), f
);
21962 case DW_FORM_block2
:
21963 case DW_FORM_block4
:
21964 case DW_FORM_block
:
21965 case DW_FORM_block1
:
21966 gdb_printf (f
, "block: size %s",
21967 pulongest (die
->attrs
[i
].as_block ()->size
));
21969 case DW_FORM_exprloc
:
21970 gdb_printf (f
, "expression: size %s",
21971 pulongest (die
->attrs
[i
].as_block ()->size
));
21973 case DW_FORM_data16
:
21974 gdb_printf (f
, "constant of 16 bytes");
21976 case DW_FORM_ref_addr
:
21977 gdb_printf (f
, "ref address: ");
21978 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
21980 case DW_FORM_GNU_ref_alt
:
21981 gdb_printf (f
, "alt ref address: ");
21982 gdb_puts (hex_string (die
->attrs
[i
].as_unsigned ()), f
);
21988 case DW_FORM_ref_udata
:
21989 gdb_printf (f
, "constant ref: 0x%lx (adjusted)",
21990 (long) (die
->attrs
[i
].as_unsigned ()));
21992 case DW_FORM_data1
:
21993 case DW_FORM_data2
:
21994 case DW_FORM_data4
:
21995 case DW_FORM_data8
:
21996 case DW_FORM_udata
:
21997 gdb_printf (f
, "constant: %s",
21998 pulongest (die
->attrs
[i
].as_unsigned ()));
22000 case DW_FORM_sec_offset
:
22001 gdb_printf (f
, "section offset: %s",
22002 pulongest (die
->attrs
[i
].as_unsigned ()));
22004 case DW_FORM_ref_sig8
:
22005 gdb_printf (f
, "signature: %s",
22006 hex_string (die
->attrs
[i
].as_signature ()));
22008 case DW_FORM_string
:
22010 case DW_FORM_line_strp
:
22012 case DW_FORM_GNU_str_index
:
22013 case DW_FORM_GNU_strp_alt
:
22014 gdb_printf (f
, "string: \"%s\" (%s canonicalized)",
22015 die
->attrs
[i
].as_string ()
22016 ? die
->attrs
[i
].as_string () : "",
22017 die
->attrs
[i
].canonical_string_p () ? "is" : "not");
22020 if (die
->attrs
[i
].as_boolean ())
22021 gdb_printf (f
, "flag: TRUE");
22023 gdb_printf (f
, "flag: FALSE");
22025 case DW_FORM_flag_present
:
22026 gdb_printf (f
, "flag: TRUE");
22028 case DW_FORM_indirect
:
22029 /* The reader will have reduced the indirect form to
22030 the "base form" so this form should not occur. */
22032 "unexpected attribute form: DW_FORM_indirect");
22034 case DW_FORM_sdata
:
22035 case DW_FORM_implicit_const
:
22036 gdb_printf (f
, "constant: %s",
22037 plongest (die
->attrs
[i
].as_signed ()));
22040 gdb_printf (f
, "unsupported attribute form: %d.",
22041 die
->attrs
[i
].form
);
22044 gdb_printf (f
, "\n");
22049 dump_die_for_error (struct die_info
*die
)
22051 dump_die_shallow (gdb_stderr
, 0, die
);
22055 dump_die_1 (struct ui_file
*f
, int level
, int max_level
, struct die_info
*die
)
22057 int indent
= level
* 4;
22059 gdb_assert (die
!= NULL
);
22061 if (level
>= max_level
)
22064 dump_die_shallow (f
, indent
, die
);
22066 if (die
->child
!= NULL
)
22068 gdb_printf (f
, "%*s Children:", indent
, "");
22069 if (level
+ 1 < max_level
)
22071 gdb_printf (f
, "\n");
22072 dump_die_1 (f
, level
+ 1, max_level
, die
->child
);
22077 " [not printed, max nesting level reached]\n");
22081 if (die
->sibling
!= NULL
&& level
> 0)
22083 dump_die_1 (f
, level
, max_level
, die
->sibling
);
22087 /* This is called from the pdie macro in gdbinit.in.
22088 It's not static so gcc will keep a copy callable from gdb. */
22091 dump_die (struct die_info
*die
, int max_level
)
22093 dump_die_1 (gdb_stdlog
, 0, max_level
, die
);
22097 store_in_ref_table (struct die_info
*die
, struct dwarf2_cu
*cu
)
22101 slot
= htab_find_slot_with_hash (cu
->die_hash
, die
,
22102 to_underlying (die
->sect_off
),
22108 /* Follow reference or signature attribute ATTR of SRC_DIE.
22109 On entry *REF_CU is the CU of SRC_DIE.
22110 On exit *REF_CU is the CU of the result. */
22112 static struct die_info
*
22113 follow_die_ref_or_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22114 struct dwarf2_cu
**ref_cu
)
22116 struct die_info
*die
;
22118 if (attr
->form_is_ref ())
22119 die
= follow_die_ref (src_die
, attr
, ref_cu
);
22120 else if (attr
->form
== DW_FORM_ref_sig8
)
22121 die
= follow_die_sig (src_die
, attr
, ref_cu
);
22124 dump_die_for_error (src_die
);
22125 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22126 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22132 /* Follow reference OFFSET.
22133 On entry *REF_CU is the CU of the source die referencing OFFSET.
22134 On exit *REF_CU is the CU of the result.
22135 Returns NULL if OFFSET is invalid. */
22137 static struct die_info
*
22138 follow_die_offset (sect_offset sect_off
, int offset_in_dwz
,
22139 struct dwarf2_cu
**ref_cu
)
22141 struct die_info temp_die
;
22142 struct dwarf2_cu
*target_cu
, *cu
= *ref_cu
;
22143 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22145 gdb_assert (cu
->per_cu
!= NULL
);
22149 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
22150 "source CU contains target offset: %d",
22151 sect_offset_str (cu
->per_cu
->sect_off
),
22152 sect_offset_str (sect_off
),
22153 cu
->header
.offset_in_cu_p (sect_off
));
22155 if (cu
->per_cu
->is_debug_types
)
22157 /* .debug_types CUs cannot reference anything outside their CU.
22158 If they need to, they have to reference a signatured type via
22159 DW_FORM_ref_sig8. */
22160 if (!cu
->header
.offset_in_cu_p (sect_off
))
22163 else if (offset_in_dwz
!= cu
->per_cu
->is_dwz
22164 || !cu
->header
.offset_in_cu_p (sect_off
))
22166 struct dwarf2_per_cu_data
*per_cu
;
22168 per_cu
= dwarf2_find_containing_comp_unit (sect_off
, offset_in_dwz
,
22169 per_objfile
->per_bfd
);
22171 dwarf_read_debug_printf_v ("target CU offset: %s, "
22172 "target CU DIEs loaded: %d",
22173 sect_offset_str (per_cu
->sect_off
),
22174 per_objfile
->get_cu (per_cu
) != nullptr);
22176 /* If necessary, add it to the queue and load its DIEs.
22178 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22179 it doesn't mean they are currently loaded. Since we require them
22180 to be loaded, we must check for ourselves. */
22181 if (maybe_queue_comp_unit (cu
, per_cu
, per_objfile
, cu
->per_cu
->lang
)
22182 || per_objfile
->get_cu (per_cu
) == nullptr)
22183 load_full_comp_unit (per_cu
, per_objfile
, per_objfile
->get_cu (per_cu
),
22184 false, cu
->per_cu
->lang
);
22186 target_cu
= per_objfile
->get_cu (per_cu
);
22187 gdb_assert (target_cu
!= nullptr);
22189 else if (cu
->dies
== NULL
)
22191 /* We're loading full DIEs during partial symbol reading. */
22192 load_full_comp_unit (cu
->per_cu
, per_objfile
, cu
, false,
22196 *ref_cu
= target_cu
;
22197 temp_die
.sect_off
= sect_off
;
22199 return (struct die_info
*) htab_find_with_hash (target_cu
->die_hash
,
22201 to_underlying (sect_off
));
22204 /* Follow reference attribute ATTR of SRC_DIE.
22205 On entry *REF_CU is the CU of SRC_DIE.
22206 On exit *REF_CU is the CU of the result. */
22208 static struct die_info
*
22209 follow_die_ref (struct die_info
*src_die
, const struct attribute
*attr
,
22210 struct dwarf2_cu
**ref_cu
)
22212 sect_offset sect_off
= attr
->get_ref_die_offset ();
22213 struct dwarf2_cu
*cu
= *ref_cu
;
22214 struct die_info
*die
;
22216 die
= follow_die_offset (sect_off
,
22217 (attr
->form
== DW_FORM_GNU_ref_alt
22218 || cu
->per_cu
->is_dwz
),
22221 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22222 "at %s [in module %s]"),
22223 sect_offset_str (sect_off
), sect_offset_str (src_die
->sect_off
),
22224 objfile_name (cu
->per_objfile
->objfile
));
22231 struct dwarf2_locexpr_baton
22232 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off
,
22233 dwarf2_per_cu_data
*per_cu
,
22234 dwarf2_per_objfile
*per_objfile
,
22235 gdb::function_view
<CORE_ADDR ()> get_frame_pc
,
22236 bool resolve_abstract_p
)
22238 struct die_info
*die
;
22239 struct attribute
*attr
;
22240 struct dwarf2_locexpr_baton retval
;
22241 struct objfile
*objfile
= per_objfile
->objfile
;
22243 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22245 cu
= load_cu (per_cu
, per_objfile
, false);
22249 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22250 Instead just throw an error, not much else we can do. */
22251 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22252 sect_offset_str (sect_off
), objfile_name (objfile
));
22255 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22257 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22258 sect_offset_str (sect_off
), objfile_name (objfile
));
22260 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22261 if (!attr
&& resolve_abstract_p
22262 && (per_objfile
->per_bfd
->abstract_to_concrete
.find (die
->sect_off
)
22263 != per_objfile
->per_bfd
->abstract_to_concrete
.end ()))
22265 CORE_ADDR pc
= get_frame_pc ();
22266 CORE_ADDR baseaddr
= objfile
->text_section_offset ();
22267 struct gdbarch
*gdbarch
= objfile
->arch ();
22269 for (const auto &cand_off
22270 : per_objfile
->per_bfd
->abstract_to_concrete
[die
->sect_off
])
22272 struct dwarf2_cu
*cand_cu
= cu
;
22273 struct die_info
*cand
22274 = follow_die_offset (cand_off
, per_cu
->is_dwz
, &cand_cu
);
22277 || cand
->parent
->tag
!= DW_TAG_subprogram
)
22280 CORE_ADDR pc_low
, pc_high
;
22281 get_scope_pc_bounds (cand
->parent
, &pc_low
, &pc_high
, cu
);
22282 if (pc_low
== ((CORE_ADDR
) -1))
22284 pc_low
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_low
+ baseaddr
);
22285 pc_high
= gdbarch_adjust_dwarf2_addr (gdbarch
, pc_high
+ baseaddr
);
22286 if (!(pc_low
<= pc
&& pc
< pc_high
))
22290 attr
= dwarf2_attr (die
, DW_AT_location
, cu
);
22297 /* DWARF: "If there is no such attribute, then there is no effect.".
22298 DATA is ignored if SIZE is 0. */
22300 retval
.data
= NULL
;
22303 else if (attr
->form_is_section_offset ())
22305 struct dwarf2_loclist_baton loclist_baton
;
22306 CORE_ADDR pc
= get_frame_pc ();
22309 fill_in_loclist_baton (cu
, &loclist_baton
, attr
);
22311 retval
.data
= dwarf2_find_location_expression (&loclist_baton
,
22313 retval
.size
= size
;
22317 if (!attr
->form_is_block ())
22318 error (_("Dwarf Error: DIE at %s referenced in module %s "
22319 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22320 sect_offset_str (sect_off
), objfile_name (objfile
));
22322 struct dwarf_block
*block
= attr
->as_block ();
22323 retval
.data
= block
->data
;
22324 retval
.size
= block
->size
;
22326 retval
.per_objfile
= per_objfile
;
22327 retval
.per_cu
= cu
->per_cu
;
22329 per_objfile
->age_comp_units ();
22336 struct dwarf2_locexpr_baton
22337 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu
,
22338 dwarf2_per_cu_data
*per_cu
,
22339 dwarf2_per_objfile
*per_objfile
,
22340 gdb::function_view
<CORE_ADDR ()> get_frame_pc
)
22342 sect_offset sect_off
= per_cu
->sect_off
+ to_underlying (offset_in_cu
);
22344 return dwarf2_fetch_die_loc_sect_off (sect_off
, per_cu
, per_objfile
,
22348 /* Write a constant of a given type as target-ordered bytes into
22351 static const gdb_byte
*
22352 write_constant_as_bytes (struct obstack
*obstack
,
22353 enum bfd_endian byte_order
,
22360 *len
= TYPE_LENGTH (type
);
22361 result
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22362 store_unsigned_integer (result
, *len
, byte_order
, value
);
22370 dwarf2_fetch_constant_bytes (sect_offset sect_off
,
22371 dwarf2_per_cu_data
*per_cu
,
22372 dwarf2_per_objfile
*per_objfile
,
22376 struct die_info
*die
;
22377 struct attribute
*attr
;
22378 const gdb_byte
*result
= NULL
;
22381 enum bfd_endian byte_order
;
22382 struct objfile
*objfile
= per_objfile
->objfile
;
22384 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22386 cu
= load_cu (per_cu
, per_objfile
, false);
22390 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22391 Instead just throw an error, not much else we can do. */
22392 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22393 sect_offset_str (sect_off
), objfile_name (objfile
));
22396 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22398 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22399 sect_offset_str (sect_off
), objfile_name (objfile
));
22401 attr
= dwarf2_attr (die
, DW_AT_const_value
, cu
);
22405 byte_order
= (bfd_big_endian (objfile
->obfd
)
22406 ? BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
);
22408 switch (attr
->form
)
22411 case DW_FORM_addrx
:
22412 case DW_FORM_GNU_addr_index
:
22416 *len
= cu
->header
.addr_size
;
22417 tem
= (gdb_byte
*) obstack_alloc (obstack
, *len
);
22418 store_unsigned_integer (tem
, *len
, byte_order
, attr
->as_address ());
22422 case DW_FORM_string
:
22425 case DW_FORM_GNU_str_index
:
22426 case DW_FORM_GNU_strp_alt
:
22427 /* The string is already allocated on the objfile obstack, point
22430 const char *attr_name
= attr
->as_string ();
22431 result
= (const gdb_byte
*) attr_name
;
22432 *len
= strlen (attr_name
);
22435 case DW_FORM_block1
:
22436 case DW_FORM_block2
:
22437 case DW_FORM_block4
:
22438 case DW_FORM_block
:
22439 case DW_FORM_exprloc
:
22440 case DW_FORM_data16
:
22442 struct dwarf_block
*block
= attr
->as_block ();
22443 result
= block
->data
;
22444 *len
= block
->size
;
22448 /* The DW_AT_const_value attributes are supposed to carry the
22449 symbol's value "represented as it would be on the target
22450 architecture." By the time we get here, it's already been
22451 converted to host endianness, so we just need to sign- or
22452 zero-extend it as appropriate. */
22453 case DW_FORM_data1
:
22454 type
= die_type (die
, cu
);
22455 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 8);
22456 if (result
== NULL
)
22457 result
= write_constant_as_bytes (obstack
, byte_order
,
22460 case DW_FORM_data2
:
22461 type
= die_type (die
, cu
);
22462 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 16);
22463 if (result
== NULL
)
22464 result
= write_constant_as_bytes (obstack
, byte_order
,
22467 case DW_FORM_data4
:
22468 type
= die_type (die
, cu
);
22469 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 32);
22470 if (result
== NULL
)
22471 result
= write_constant_as_bytes (obstack
, byte_order
,
22474 case DW_FORM_data8
:
22475 type
= die_type (die
, cu
);
22476 result
= dwarf2_const_value_data (attr
, obstack
, cu
, &value
, 64);
22477 if (result
== NULL
)
22478 result
= write_constant_as_bytes (obstack
, byte_order
,
22482 case DW_FORM_sdata
:
22483 case DW_FORM_implicit_const
:
22484 type
= die_type (die
, cu
);
22485 result
= write_constant_as_bytes (obstack
, byte_order
,
22486 type
, attr
->as_signed (), len
);
22489 case DW_FORM_udata
:
22490 type
= die_type (die
, cu
);
22491 result
= write_constant_as_bytes (obstack
, byte_order
,
22492 type
, attr
->as_unsigned (), len
);
22496 complaint (_("unsupported const value attribute form: '%s'"),
22497 dwarf_form_name (attr
->form
));
22507 dwarf2_fetch_die_type_sect_off (sect_offset sect_off
,
22508 dwarf2_per_cu_data
*per_cu
,
22509 dwarf2_per_objfile
*per_objfile
,
22510 const char **var_name
)
22512 struct die_info
*die
;
22514 dwarf2_cu
*cu
= per_objfile
->get_cu (per_cu
);
22516 cu
= load_cu (per_cu
, per_objfile
, false);
22521 die
= follow_die_offset (sect_off
, per_cu
->is_dwz
, &cu
);
22525 if (var_name
!= nullptr)
22526 *var_name
= var_decl_name (die
, cu
);
22527 return die_type (die
, cu
);
22533 dwarf2_get_die_type (cu_offset die_offset
,
22534 dwarf2_per_cu_data
*per_cu
,
22535 dwarf2_per_objfile
*per_objfile
)
22537 sect_offset die_offset_sect
= per_cu
->sect_off
+ to_underlying (die_offset
);
22538 return get_die_type_at_offset (die_offset_sect
, per_cu
, per_objfile
);
22541 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22542 On entry *REF_CU is the CU of SRC_DIE.
22543 On exit *REF_CU is the CU of the result.
22544 Returns NULL if the referenced DIE isn't found. */
22546 static struct die_info
*
22547 follow_die_sig_1 (struct die_info
*src_die
, struct signatured_type
*sig_type
,
22548 struct dwarf2_cu
**ref_cu
)
22550 struct die_info temp_die
;
22551 struct dwarf2_cu
*sig_cu
;
22552 struct die_info
*die
;
22553 dwarf2_per_objfile
*per_objfile
= (*ref_cu
)->per_objfile
;
22556 /* While it might be nice to assert sig_type->type == NULL here,
22557 we can get here for DW_AT_imported_declaration where we need
22558 the DIE not the type. */
22560 /* If necessary, add it to the queue and load its DIEs.
22562 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
22563 it doesn't mean they are currently loaded. Since we require them
22564 to be loaded, we must check for ourselves. */
22565 if (maybe_queue_comp_unit (*ref_cu
, sig_type
, per_objfile
,
22567 || per_objfile
->get_cu (sig_type
) == nullptr)
22568 read_signatured_type (sig_type
, per_objfile
);
22570 sig_cu
= per_objfile
->get_cu (sig_type
);
22571 gdb_assert (sig_cu
!= NULL
);
22572 gdb_assert (to_underlying (sig_type
->type_offset_in_section
) != 0);
22573 temp_die
.sect_off
= sig_type
->type_offset_in_section
;
22574 die
= (struct die_info
*) htab_find_with_hash (sig_cu
->die_hash
, &temp_die
,
22575 to_underlying (temp_die
.sect_off
));
22578 /* For .gdb_index version 7 keep track of included TUs.
22579 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22580 if (per_objfile
->per_bfd
->index_table
!= NULL
22581 && !per_objfile
->per_bfd
->index_table
->version_check ())
22583 (*ref_cu
)->per_cu
->imported_symtabs_push (sig_cu
->per_cu
);
22593 /* Follow signatured type referenced by ATTR in SRC_DIE.
22594 On entry *REF_CU is the CU of SRC_DIE.
22595 On exit *REF_CU is the CU of the result.
22596 The result is the DIE of the type.
22597 If the referenced type cannot be found an error is thrown. */
22599 static struct die_info
*
22600 follow_die_sig (struct die_info
*src_die
, const struct attribute
*attr
,
22601 struct dwarf2_cu
**ref_cu
)
22603 ULONGEST signature
= attr
->as_signature ();
22604 struct signatured_type
*sig_type
;
22605 struct die_info
*die
;
22607 gdb_assert (attr
->form
== DW_FORM_ref_sig8
);
22609 sig_type
= lookup_signatured_type (*ref_cu
, signature
);
22610 /* sig_type will be NULL if the signatured type is missing from
22612 if (sig_type
== NULL
)
22614 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22615 " from DIE at %s [in module %s]"),
22616 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22617 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22620 die
= follow_die_sig_1 (src_die
, sig_type
, ref_cu
);
22623 dump_die_for_error (src_die
);
22624 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22625 " from DIE at %s [in module %s]"),
22626 hex_string (signature
), sect_offset_str (src_die
->sect_off
),
22627 objfile_name ((*ref_cu
)->per_objfile
->objfile
));
22633 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22634 reading in and processing the type unit if necessary. */
22636 static struct type
*
22637 get_signatured_type (struct die_info
*die
, ULONGEST signature
,
22638 struct dwarf2_cu
*cu
)
22640 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22641 struct signatured_type
*sig_type
;
22642 struct dwarf2_cu
*type_cu
;
22643 struct die_info
*type_die
;
22646 sig_type
= lookup_signatured_type (cu
, signature
);
22647 /* sig_type will be NULL if the signatured type is missing from
22649 if (sig_type
== NULL
)
22651 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22652 " from DIE at %s [in module %s]"),
22653 hex_string (signature
), sect_offset_str (die
->sect_off
),
22654 objfile_name (per_objfile
->objfile
));
22655 return build_error_marker_type (cu
, die
);
22658 /* If we already know the type we're done. */
22659 type
= per_objfile
->get_type_for_signatured_type (sig_type
);
22660 if (type
!= nullptr)
22664 type_die
= follow_die_sig_1 (die
, sig_type
, &type_cu
);
22665 if (type_die
!= NULL
)
22667 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22668 is created. This is important, for example, because for c++ classes
22669 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22670 type
= read_type_die (type_die
, type_cu
);
22673 complaint (_("Dwarf Error: Cannot build signatured type %s"
22674 " referenced from DIE at %s [in module %s]"),
22675 hex_string (signature
), sect_offset_str (die
->sect_off
),
22676 objfile_name (per_objfile
->objfile
));
22677 type
= build_error_marker_type (cu
, die
);
22682 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22683 " from DIE at %s [in module %s]"),
22684 hex_string (signature
), sect_offset_str (die
->sect_off
),
22685 objfile_name (per_objfile
->objfile
));
22686 type
= build_error_marker_type (cu
, die
);
22689 per_objfile
->set_type_for_signatured_type (sig_type
, type
);
22694 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22695 reading in and processing the type unit if necessary. */
22697 static struct type
*
22698 get_DW_AT_signature_type (struct die_info
*die
, const struct attribute
*attr
,
22699 struct dwarf2_cu
*cu
) /* ARI: editCase function */
22701 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22702 if (attr
->form_is_ref ())
22704 struct dwarf2_cu
*type_cu
= cu
;
22705 struct die_info
*type_die
= follow_die_ref (die
, attr
, &type_cu
);
22707 return read_type_die (type_die
, type_cu
);
22709 else if (attr
->form
== DW_FORM_ref_sig8
)
22711 return get_signatured_type (die
, attr
->as_signature (), cu
);
22715 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
22717 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22718 " at %s [in module %s]"),
22719 dwarf_form_name (attr
->form
), sect_offset_str (die
->sect_off
),
22720 objfile_name (per_objfile
->objfile
));
22721 return build_error_marker_type (cu
, die
);
22725 /* Load the DIEs associated with type unit PER_CU into memory. */
22728 load_full_type_unit (dwarf2_per_cu_data
*per_cu
,
22729 dwarf2_per_objfile
*per_objfile
)
22731 struct signatured_type
*sig_type
;
22733 /* We have the per_cu, but we need the signatured_type.
22734 Fortunately this is an easy translation. */
22735 gdb_assert (per_cu
->is_debug_types
);
22736 sig_type
= (struct signatured_type
*) per_cu
;
22738 gdb_assert (per_objfile
->get_cu (per_cu
) == nullptr);
22740 read_signatured_type (sig_type
, per_objfile
);
22742 gdb_assert (per_objfile
->get_cu (per_cu
) != nullptr);
22745 /* Read in a signatured type and build its CU and DIEs.
22746 If the type is a stub for the real type in a DWO file,
22747 read in the real type from the DWO file as well. */
22750 read_signatured_type (signatured_type
*sig_type
,
22751 dwarf2_per_objfile
*per_objfile
)
22753 gdb_assert (sig_type
->is_debug_types
);
22754 gdb_assert (per_objfile
->get_cu (sig_type
) == nullptr);
22756 cutu_reader
reader (sig_type
, per_objfile
, nullptr, nullptr, false);
22758 if (!reader
.dummy_p
)
22760 struct dwarf2_cu
*cu
= reader
.cu
;
22761 const gdb_byte
*info_ptr
= reader
.info_ptr
;
22763 gdb_assert (cu
->die_hash
== NULL
);
22765 htab_create_alloc_ex (cu
->header
.length
/ 12,
22769 &cu
->comp_unit_obstack
,
22770 hashtab_obstack_allocate
,
22771 dummy_obstack_deallocate
);
22773 if (reader
.comp_unit_die
->has_children
)
22774 reader
.comp_unit_die
->child
22775 = read_die_and_siblings (&reader
, info_ptr
, &info_ptr
,
22776 reader
.comp_unit_die
);
22777 cu
->dies
= reader
.comp_unit_die
;
22778 /* comp_unit_die is not stored in die_hash, no need. */
22780 /* We try not to read any attributes in this function, because
22781 not all CUs needed for references have been loaded yet, and
22782 symbol table processing isn't initialized. But we have to
22783 set the CU language, or we won't be able to build types
22784 correctly. Similarly, if we do not read the producer, we can
22785 not apply producer-specific interpretation. */
22786 prepare_one_comp_unit (cu
, cu
->dies
, language_minimal
);
22791 sig_type
->tu_read
= 1;
22794 /* Decode simple location descriptions.
22795 Given a pointer to a dwarf block that defines a location, compute
22796 the location and return the value. If COMPUTED is non-null, it is
22797 set to true to indicate that decoding was successful, and false
22798 otherwise. If COMPUTED is null, then this function may emit a
22802 decode_locdesc (struct dwarf_block
*blk
, struct dwarf2_cu
*cu
, bool *computed
)
22804 struct objfile
*objfile
= cu
->per_objfile
->objfile
;
22806 size_t size
= blk
->size
;
22807 const gdb_byte
*data
= blk
->data
;
22808 CORE_ADDR stack
[64];
22810 unsigned int bytes_read
, unsnd
;
22813 if (computed
!= nullptr)
22819 stack
[++stacki
] = 0;
22858 stack
[++stacki
] = op
- DW_OP_lit0
;
22893 stack
[++stacki
] = op
- DW_OP_reg0
;
22896 if (computed
== nullptr)
22897 dwarf2_complex_location_expr_complaint ();
22904 unsnd
= read_unsigned_leb128 (NULL
, (data
+ i
), &bytes_read
);
22906 stack
[++stacki
] = unsnd
;
22909 if (computed
== nullptr)
22910 dwarf2_complex_location_expr_complaint ();
22917 stack
[++stacki
] = cu
->header
.read_address (objfile
->obfd
, &data
[i
],
22922 case DW_OP_const1u
:
22923 stack
[++stacki
] = read_1_byte (objfile
->obfd
, &data
[i
]);
22927 case DW_OP_const1s
:
22928 stack
[++stacki
] = read_1_signed_byte (objfile
->obfd
, &data
[i
]);
22932 case DW_OP_const2u
:
22933 stack
[++stacki
] = read_2_bytes (objfile
->obfd
, &data
[i
]);
22937 case DW_OP_const2s
:
22938 stack
[++stacki
] = read_2_signed_bytes (objfile
->obfd
, &data
[i
]);
22942 case DW_OP_const4u
:
22943 stack
[++stacki
] = read_4_bytes (objfile
->obfd
, &data
[i
]);
22947 case DW_OP_const4s
:
22948 stack
[++stacki
] = read_4_signed_bytes (objfile
->obfd
, &data
[i
]);
22952 case DW_OP_const8u
:
22953 stack
[++stacki
] = read_8_bytes (objfile
->obfd
, &data
[i
]);
22958 stack
[++stacki
] = read_unsigned_leb128 (NULL
, (data
+ i
),
22964 stack
[++stacki
] = read_signed_leb128 (NULL
, (data
+ i
), &bytes_read
);
22969 stack
[stacki
+ 1] = stack
[stacki
];
22974 stack
[stacki
- 1] += stack
[stacki
];
22978 case DW_OP_plus_uconst
:
22979 stack
[stacki
] += read_unsigned_leb128 (NULL
, (data
+ i
),
22985 stack
[stacki
- 1] -= stack
[stacki
];
22990 /* If we're not the last op, then we definitely can't encode
22991 this using GDB's address_class enum. This is valid for partial
22992 global symbols, although the variable's address will be bogus
22996 if (computed
== nullptr)
22997 dwarf2_complex_location_expr_complaint ();
23003 case DW_OP_GNU_push_tls_address
:
23004 case DW_OP_form_tls_address
:
23005 /* The top of the stack has the offset from the beginning
23006 of the thread control block at which the variable is located. */
23007 /* Nothing should follow this operator, so the top of stack would
23009 /* This is valid for partial global symbols, but the variable's
23010 address will be bogus in the psymtab. Make it always at least
23011 non-zero to not look as a variable garbage collected by linker
23012 which have DW_OP_addr 0. */
23015 if (computed
== nullptr)
23016 dwarf2_complex_location_expr_complaint ();
23023 case DW_OP_GNU_uninit
:
23024 if (computed
!= nullptr)
23029 case DW_OP_GNU_addr_index
:
23030 case DW_OP_GNU_const_index
:
23031 stack
[++stacki
] = read_addr_index_from_leb128 (cu
, &data
[i
],
23037 if (computed
== nullptr)
23039 const char *name
= get_DW_OP_name (op
);
23042 complaint (_("unsupported stack op: '%s'"),
23045 complaint (_("unsupported stack op: '%02x'"),
23049 return (stack
[stacki
]);
23052 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23053 outside of the allocated space. Also enforce minimum>0. */
23054 if (stacki
>= ARRAY_SIZE (stack
) - 1)
23056 if (computed
== nullptr)
23057 complaint (_("location description stack overflow"));
23063 if (computed
== nullptr)
23064 complaint (_("location description stack underflow"));
23069 if (computed
!= nullptr)
23071 return (stack
[stacki
]);
23074 /* memory allocation interface */
23076 static struct dwarf_block
*
23077 dwarf_alloc_block (struct dwarf2_cu
*cu
)
23079 return XOBNEW (&cu
->comp_unit_obstack
, struct dwarf_block
);
23082 static struct die_info
*
23083 dwarf_alloc_die (struct dwarf2_cu
*cu
, int num_attrs
)
23085 struct die_info
*die
;
23086 size_t size
= sizeof (struct die_info
);
23089 size
+= (num_attrs
- 1) * sizeof (struct attribute
);
23091 die
= (struct die_info
*) obstack_alloc (&cu
->comp_unit_obstack
, size
);
23092 memset (die
, 0, sizeof (struct die_info
));
23098 /* Macro support. */
23100 /* An overload of dwarf_decode_macros that finds the correct section
23101 and ensures it is read in before calling the other overload. */
23104 dwarf_decode_macros (struct dwarf2_cu
*cu
, unsigned int offset
,
23105 int section_is_gnu
)
23107 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23108 struct objfile
*objfile
= per_objfile
->objfile
;
23109 const struct line_header
*lh
= cu
->line_header
;
23110 unsigned int offset_size
= cu
->header
.offset_size
;
23111 struct dwarf2_section_info
*section
;
23112 const char *section_name
;
23114 if (cu
->dwo_unit
!= nullptr)
23116 if (section_is_gnu
)
23118 section
= &cu
->dwo_unit
->dwo_file
->sections
.macro
;
23119 section_name
= ".debug_macro.dwo";
23123 section
= &cu
->dwo_unit
->dwo_file
->sections
.macinfo
;
23124 section_name
= ".debug_macinfo.dwo";
23129 if (section_is_gnu
)
23131 section
= &per_objfile
->per_bfd
->macro
;
23132 section_name
= ".debug_macro";
23136 section
= &per_objfile
->per_bfd
->macinfo
;
23137 section_name
= ".debug_macinfo";
23141 section
->read (objfile
);
23142 if (section
->buffer
== nullptr)
23144 complaint (_("missing %s section"), section_name
);
23148 buildsym_compunit
*builder
= cu
->get_builder ();
23150 struct dwarf2_section_info
*str_offsets_section
;
23151 struct dwarf2_section_info
*str_section
;
23152 gdb::optional
<ULONGEST
> str_offsets_base
;
23154 if (cu
->dwo_unit
!= nullptr)
23156 str_offsets_section
= &cu
->dwo_unit
->dwo_file
23157 ->sections
.str_offsets
;
23158 str_section
= &cu
->dwo_unit
->dwo_file
->sections
.str
;
23159 str_offsets_base
= cu
->header
.addr_size
;
23163 str_offsets_section
= &per_objfile
->per_bfd
->str_offsets
;
23164 str_section
= &per_objfile
->per_bfd
->str
;
23165 str_offsets_base
= cu
->str_offsets_base
;
23168 dwarf_decode_macros (per_objfile
, builder
, section
, lh
,
23169 offset_size
, offset
, str_section
, str_offsets_section
,
23170 str_offsets_base
, section_is_gnu
);
23173 /* Return the .debug_loc section to use for CU.
23174 For DWO files use .debug_loc.dwo. */
23176 static struct dwarf2_section_info
*
23177 cu_debug_loc_section (struct dwarf2_cu
*cu
)
23179 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23183 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23185 return cu
->header
.version
>= 5 ? §ions
->loclists
: §ions
->loc
;
23187 return (cu
->header
.version
>= 5 ? &per_objfile
->per_bfd
->loclists
23188 : &per_objfile
->per_bfd
->loc
);
23191 /* Return the .debug_rnglists section to use for CU. */
23192 static struct dwarf2_section_info
*
23193 cu_debug_rnglists_section (struct dwarf2_cu
*cu
, dwarf_tag tag
)
23195 if (cu
->header
.version
< 5)
23196 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23197 cu
->header
.version
);
23198 struct dwarf2_per_objfile
*dwarf2_per_objfile
= cu
->per_objfile
;
23200 /* Make sure we read the .debug_rnglists section from the file that
23201 contains the DW_AT_ranges attribute we are reading. Normally that
23202 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23203 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23205 if (cu
->dwo_unit
!= nullptr
23206 && tag
!= DW_TAG_compile_unit
23207 && tag
!= DW_TAG_skeleton_unit
)
23209 struct dwo_sections
*sections
= &cu
->dwo_unit
->dwo_file
->sections
;
23211 if (sections
->rnglists
.size
> 0)
23212 return §ions
->rnglists
;
23214 error (_(".debug_rnglists section is missing from .dwo file."));
23216 return &dwarf2_per_objfile
->per_bfd
->rnglists
;
23219 /* A helper function that fills in a dwarf2_loclist_baton. */
23222 fill_in_loclist_baton (struct dwarf2_cu
*cu
,
23223 struct dwarf2_loclist_baton
*baton
,
23224 const struct attribute
*attr
)
23226 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23227 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23229 section
->read (per_objfile
->objfile
);
23231 baton
->per_objfile
= per_objfile
;
23232 baton
->per_cu
= cu
->per_cu
;
23233 gdb_assert (baton
->per_cu
);
23234 /* We don't know how long the location list is, but make sure we
23235 don't run off the edge of the section. */
23236 baton
->size
= section
->size
- attr
->as_unsigned ();
23237 baton
->data
= section
->buffer
+ attr
->as_unsigned ();
23238 if (cu
->base_address
.has_value ())
23239 baton
->base_address
= *cu
->base_address
;
23241 baton
->base_address
= 0;
23242 baton
->from_dwo
= cu
->dwo_unit
!= NULL
;
23246 dwarf2_symbol_mark_computed (const struct attribute
*attr
, struct symbol
*sym
,
23247 struct dwarf2_cu
*cu
, int is_block
)
23249 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23250 struct objfile
*objfile
= per_objfile
->objfile
;
23251 struct dwarf2_section_info
*section
= cu_debug_loc_section (cu
);
23253 if (attr
->form_is_section_offset ()
23254 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23255 the section. If so, fall through to the complaint in the
23257 && attr
->as_unsigned () < section
->get_size (objfile
))
23259 struct dwarf2_loclist_baton
*baton
;
23261 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_loclist_baton
);
23263 fill_in_loclist_baton (cu
, baton
, attr
);
23265 if (!cu
->base_address
.has_value ())
23266 complaint (_("Location list used without "
23267 "specifying the CU base address."));
23269 sym
->set_aclass_index ((is_block
23270 ? dwarf2_loclist_block_index
23271 : dwarf2_loclist_index
));
23272 SYMBOL_LOCATION_BATON (sym
) = baton
;
23276 struct dwarf2_locexpr_baton
*baton
;
23278 baton
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_locexpr_baton
);
23279 baton
->per_objfile
= per_objfile
;
23280 baton
->per_cu
= cu
->per_cu
;
23281 gdb_assert (baton
->per_cu
);
23283 if (attr
->form_is_block ())
23285 /* Note that we're just copying the block's data pointer
23286 here, not the actual data. We're still pointing into the
23287 info_buffer for SYM's objfile; right now we never release
23288 that buffer, but when we do clean up properly this may
23290 struct dwarf_block
*block
= attr
->as_block ();
23291 baton
->size
= block
->size
;
23292 baton
->data
= block
->data
;
23296 dwarf2_invalid_attrib_class_complaint ("location description",
23297 sym
->natural_name ());
23301 sym
->set_aclass_index ((is_block
23302 ? dwarf2_locexpr_block_index
23303 : dwarf2_locexpr_index
));
23304 SYMBOL_LOCATION_BATON (sym
) = baton
;
23310 const comp_unit_head
*
23311 dwarf2_per_cu_data::get_header () const
23313 if (!m_header_read_in
)
23315 const gdb_byte
*info_ptr
23316 = this->section
->buffer
+ to_underlying (this->sect_off
);
23318 memset (&m_header
, 0, sizeof (m_header
));
23320 read_comp_unit_head (&m_header
, info_ptr
, this->section
,
23321 rcuh_kind::COMPILE
);
23323 m_header_read_in
= true;
23332 dwarf2_per_cu_data::addr_size () const
23334 return this->get_header ()->addr_size
;
23340 dwarf2_per_cu_data::offset_size () const
23342 return this->get_header ()->offset_size
;
23348 dwarf2_per_cu_data::ref_addr_size () const
23350 const comp_unit_head
*header
= this->get_header ();
23352 if (header
->version
== 2)
23353 return header
->addr_size
;
23355 return header
->offset_size
;
23358 /* A helper function for dwarf2_find_containing_comp_unit that returns
23359 the index of the result, and that searches a vector. It will
23360 return a result even if the offset in question does not actually
23361 occur in any CU. This is separate so that it can be unit
23365 dwarf2_find_containing_comp_unit
23366 (sect_offset sect_off
,
23367 unsigned int offset_in_dwz
,
23368 const std::vector
<dwarf2_per_cu_data_up
> &all_comp_units
)
23373 high
= all_comp_units
.size () - 1;
23376 struct dwarf2_per_cu_data
*mid_cu
;
23377 int mid
= low
+ (high
- low
) / 2;
23379 mid_cu
= all_comp_units
[mid
].get ();
23380 if (mid_cu
->is_dwz
> offset_in_dwz
23381 || (mid_cu
->is_dwz
== offset_in_dwz
23382 && mid_cu
->sect_off
+ mid_cu
->length
> sect_off
))
23387 gdb_assert (low
== high
);
23391 /* Locate the .debug_info compilation unit from CU's objfile which contains
23392 the DIE at OFFSET. Raises an error on failure. */
23394 static struct dwarf2_per_cu_data
*
23395 dwarf2_find_containing_comp_unit (sect_offset sect_off
,
23396 unsigned int offset_in_dwz
,
23397 dwarf2_per_bfd
*per_bfd
)
23399 int low
= dwarf2_find_containing_comp_unit
23400 (sect_off
, offset_in_dwz
, per_bfd
->all_comp_units
);
23401 dwarf2_per_cu_data
*this_cu
= per_bfd
->all_comp_units
[low
].get ();
23403 if (this_cu
->is_dwz
!= offset_in_dwz
|| this_cu
->sect_off
> sect_off
)
23405 if (low
== 0 || this_cu
->is_dwz
!= offset_in_dwz
)
23406 error (_("Dwarf Error: could not find partial DIE containing "
23407 "offset %s [in module %s]"),
23408 sect_offset_str (sect_off
),
23409 bfd_get_filename (per_bfd
->obfd
));
23411 gdb_assert (per_bfd
->all_comp_units
[low
-1]->sect_off
23413 return per_bfd
->all_comp_units
[low
- 1].get ();
23417 if (low
== per_bfd
->all_comp_units
.size () - 1
23418 && sect_off
>= this_cu
->sect_off
+ this_cu
->length
)
23419 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off
));
23420 gdb_assert (sect_off
< this_cu
->sect_off
+ this_cu
->length
);
23427 namespace selftests
{
23428 namespace find_containing_comp_unit
{
23433 dwarf2_per_cu_data_up
one (new dwarf2_per_cu_data
);
23434 dwarf2_per_cu_data
*one_ptr
= one
.get ();
23435 dwarf2_per_cu_data_up
two (new dwarf2_per_cu_data
);
23436 dwarf2_per_cu_data
*two_ptr
= two
.get ();
23437 dwarf2_per_cu_data_up
three (new dwarf2_per_cu_data
);
23438 dwarf2_per_cu_data
*three_ptr
= three
.get ();
23439 dwarf2_per_cu_data_up
four (new dwarf2_per_cu_data
);
23440 dwarf2_per_cu_data
*four_ptr
= four
.get ();
23443 two
->sect_off
= sect_offset (one
->length
);
23448 four
->sect_off
= sect_offset (three
->length
);
23452 std::vector
<dwarf2_per_cu_data_up
> units
;
23453 units
.push_back (std::move (one
));
23454 units
.push_back (std::move (two
));
23455 units
.push_back (std::move (three
));
23456 units
.push_back (std::move (four
));
23460 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 0, units
);
23461 SELF_CHECK (units
[result
].get () == one_ptr
);
23462 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 0, units
);
23463 SELF_CHECK (units
[result
].get () == one_ptr
);
23464 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 0, units
);
23465 SELF_CHECK (units
[result
].get () == two_ptr
);
23467 result
= dwarf2_find_containing_comp_unit (sect_offset (0), 1, units
);
23468 SELF_CHECK (units
[result
].get () == three_ptr
);
23469 result
= dwarf2_find_containing_comp_unit (sect_offset (3), 1, units
);
23470 SELF_CHECK (units
[result
].get () == three_ptr
);
23471 result
= dwarf2_find_containing_comp_unit (sect_offset (5), 1, units
);
23472 SELF_CHECK (units
[result
].get () == four_ptr
);
23478 #endif /* GDB_SELF_TEST */
23480 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23483 prepare_one_comp_unit (struct dwarf2_cu
*cu
, struct die_info
*comp_unit_die
,
23484 enum language pretend_language
)
23486 struct attribute
*attr
;
23488 cu
->producer
= dwarf2_string_attr (comp_unit_die
, DW_AT_producer
, cu
);
23490 /* Set the language we're debugging. */
23491 attr
= dwarf2_attr (comp_unit_die
, DW_AT_language
, cu
);
23492 if (cu
->producer
!= nullptr
23493 && strstr (cu
->producer
, "IBM XL C for OpenCL") != NULL
)
23495 /* The XLCL doesn't generate DW_LANG_OpenCL because this
23496 attribute is not standardised yet. As a workaround for the
23497 language detection we fall back to the DW_AT_producer
23499 cu
->per_cu
->lang
= language_opencl
;
23501 else if (cu
->producer
!= nullptr
23502 && strstr (cu
->producer
, "GNU Go ") != NULL
)
23504 /* Similar hack for Go. */
23505 cu
->per_cu
->lang
= language_go
;
23507 else if (attr
!= nullptr)
23508 cu
->per_cu
->lang
= dwarf_lang_to_enum_language (attr
->constant_value (0));
23510 cu
->per_cu
->lang
= pretend_language
;
23511 cu
->language_defn
= language_def (cu
->per_cu
->lang
);
23517 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data
*per_cu
)
23519 auto it
= m_dwarf2_cus
.find (per_cu
);
23520 if (it
== m_dwarf2_cus
.end ())
23529 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data
*per_cu
, dwarf2_cu
*cu
)
23531 gdb_assert (this->get_cu (per_cu
) == nullptr);
23533 m_dwarf2_cus
[per_cu
] = cu
;
23539 dwarf2_per_objfile::age_comp_units ()
23541 dwarf_read_debug_printf_v ("running");
23543 /* This is not expected to be called in the middle of CU expansion. There is
23544 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
23545 loaded in memory. Calling age_comp_units while the queue is in use could
23546 make us free the DIEs for a CU that is in the queue and therefore break
23548 gdb_assert (!this->per_bfd
->queue
.has_value ());
23550 /* Start by clearing all marks. */
23551 for (auto pair
: m_dwarf2_cus
)
23552 pair
.second
->clear_mark ();
23554 /* Traverse all CUs, mark them and their dependencies if used recently
23556 for (auto pair
: m_dwarf2_cus
)
23558 dwarf2_cu
*cu
= pair
.second
;
23561 if (cu
->last_used
<= dwarf_max_cache_age
)
23565 /* Delete all CUs still not marked. */
23566 for (auto it
= m_dwarf2_cus
.begin (); it
!= m_dwarf2_cus
.end ();)
23568 dwarf2_cu
*cu
= it
->second
;
23570 if (!cu
->is_marked ())
23572 dwarf_read_debug_printf_v ("deleting old CU %s",
23573 sect_offset_str (cu
->per_cu
->sect_off
));
23575 it
= m_dwarf2_cus
.erase (it
);
23585 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data
*per_cu
)
23587 auto it
= m_dwarf2_cus
.find (per_cu
);
23588 if (it
== m_dwarf2_cus
.end ())
23593 m_dwarf2_cus
.erase (it
);
23596 dwarf2_per_objfile::~dwarf2_per_objfile ()
23601 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23602 We store these in a hash table separate from the DIEs, and preserve them
23603 when the DIEs are flushed out of cache.
23605 The CU "per_cu" pointer is needed because offset alone is not enough to
23606 uniquely identify the type. A file may have multiple .debug_types sections,
23607 or the type may come from a DWO file. Furthermore, while it's more logical
23608 to use per_cu->section+offset, with Fission the section with the data is in
23609 the DWO file but we don't know that section at the point we need it.
23610 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23611 because we can enter the lookup routine, get_die_type_at_offset, from
23612 outside this file, and thus won't necessarily have PER_CU->cu.
23613 Fortunately, PER_CU is stable for the life of the objfile. */
23615 struct dwarf2_per_cu_offset_and_type
23617 const struct dwarf2_per_cu_data
*per_cu
;
23618 sect_offset sect_off
;
23622 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23625 per_cu_offset_and_type_hash (const void *item
)
23627 const struct dwarf2_per_cu_offset_and_type
*ofs
23628 = (const struct dwarf2_per_cu_offset_and_type
*) item
;
23630 return (uintptr_t) ofs
->per_cu
+ to_underlying (ofs
->sect_off
);
23633 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23636 per_cu_offset_and_type_eq (const void *item_lhs
, const void *item_rhs
)
23638 const struct dwarf2_per_cu_offset_and_type
*ofs_lhs
23639 = (const struct dwarf2_per_cu_offset_and_type
*) item_lhs
;
23640 const struct dwarf2_per_cu_offset_and_type
*ofs_rhs
23641 = (const struct dwarf2_per_cu_offset_and_type
*) item_rhs
;
23643 return (ofs_lhs
->per_cu
== ofs_rhs
->per_cu
23644 && ofs_lhs
->sect_off
== ofs_rhs
->sect_off
);
23647 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23648 table if necessary. For convenience, return TYPE.
23650 The DIEs reading must have careful ordering to:
23651 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23652 reading current DIE.
23653 * Not trying to dereference contents of still incompletely read in types
23654 while reading in other DIEs.
23655 * Enable referencing still incompletely read in types just by a pointer to
23656 the type without accessing its fields.
23658 Therefore caller should follow these rules:
23659 * Try to fetch any prerequisite types we may need to build this DIE type
23660 before building the type and calling set_die_type.
23661 * After building type call set_die_type for current DIE as soon as
23662 possible before fetching more types to complete the current type.
23663 * Make the type as complete as possible before fetching more types. */
23665 static struct type
*
23666 set_die_type (struct die_info
*die
, struct type
*type
, struct dwarf2_cu
*cu
,
23667 bool skip_data_location
)
23669 dwarf2_per_objfile
*per_objfile
= cu
->per_objfile
;
23670 struct dwarf2_per_cu_offset_and_type
**slot
, ofs
;
23671 struct objfile
*objfile
= per_objfile
->objfile
;
23672 struct attribute
*attr
;
23673 struct dynamic_prop prop
;
23675 /* For Ada types, make sure that the gnat-specific data is always
23676 initialized (if not already set). There are a few types where
23677 we should not be doing so, because the type-specific area is
23678 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23679 where the type-specific area is used to store the floatformat).
23680 But this is not a problem, because the gnat-specific information
23681 is actually not needed for these types. */
23682 if (need_gnat_info (cu
)
23683 && type
->code () != TYPE_CODE_FUNC
23684 && type
->code () != TYPE_CODE_FLT
23685 && type
->code () != TYPE_CODE_METHODPTR
23686 && type
->code () != TYPE_CODE_MEMBERPTR
23687 && type
->code () != TYPE_CODE_METHOD
23688 && type
->code () != TYPE_CODE_FIXED_POINT
23689 && !HAVE_GNAT_AUX_INFO (type
))
23690 INIT_GNAT_SPECIFIC (type
);
23692 /* Read DW_AT_allocated and set in type. */
23693 attr
= dwarf2_attr (die
, DW_AT_allocated
, cu
);
23696 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23697 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23698 type
->add_dyn_prop (DYN_PROP_ALLOCATED
, prop
);
23701 /* Read DW_AT_associated and set in type. */
23702 attr
= dwarf2_attr (die
, DW_AT_associated
, cu
);
23705 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23706 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23707 type
->add_dyn_prop (DYN_PROP_ASSOCIATED
, prop
);
23710 /* Read DW_AT_rank and set in type. */
23711 attr
= dwarf2_attr (die
, DW_AT_rank
, cu
);
23714 struct type
*prop_type
= cu
->addr_sized_int_type (false);
23715 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, prop_type
))
23716 type
->add_dyn_prop (DYN_PROP_RANK
, prop
);
23719 /* Read DW_AT_data_location and set in type. */
23720 if (!skip_data_location
)
23722 attr
= dwarf2_attr (die
, DW_AT_data_location
, cu
);
23723 if (attr_to_dynamic_prop (attr
, die
, cu
, &prop
, cu
->addr_type ()))
23724 type
->add_dyn_prop (DYN_PROP_DATA_LOCATION
, prop
);
23727 if (per_objfile
->die_type_hash
== NULL
)
23728 per_objfile
->die_type_hash
23729 = htab_up (htab_create_alloc (127,
23730 per_cu_offset_and_type_hash
,
23731 per_cu_offset_and_type_eq
,
23732 NULL
, xcalloc
, xfree
));
23734 ofs
.per_cu
= cu
->per_cu
;
23735 ofs
.sect_off
= die
->sect_off
;
23737 slot
= (struct dwarf2_per_cu_offset_and_type
**)
23738 htab_find_slot (per_objfile
->die_type_hash
.get (), &ofs
, INSERT
);
23740 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23741 sect_offset_str (die
->sect_off
));
23742 *slot
= XOBNEW (&objfile
->objfile_obstack
,
23743 struct dwarf2_per_cu_offset_and_type
);
23748 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23749 or return NULL if the die does not have a saved type. */
23751 static struct type
*
23752 get_die_type_at_offset (sect_offset sect_off
,
23753 dwarf2_per_cu_data
*per_cu
,
23754 dwarf2_per_objfile
*per_objfile
)
23756 struct dwarf2_per_cu_offset_and_type
*slot
, ofs
;
23758 if (per_objfile
->die_type_hash
== NULL
)
23761 ofs
.per_cu
= per_cu
;
23762 ofs
.sect_off
= sect_off
;
23763 slot
= ((struct dwarf2_per_cu_offset_and_type
*)
23764 htab_find (per_objfile
->die_type_hash
.get (), &ofs
));
23771 /* Look up the type for DIE in CU in die_type_hash,
23772 or return NULL if DIE does not have a saved type. */
23774 static struct type
*
23775 get_die_type (struct die_info
*die
, struct dwarf2_cu
*cu
)
23777 return get_die_type_at_offset (die
->sect_off
, cu
->per_cu
, cu
->per_objfile
);
23780 struct cmd_list_element
*set_dwarf_cmdlist
;
23781 struct cmd_list_element
*show_dwarf_cmdlist
;
23784 show_check_physname (struct ui_file
*file
, int from_tty
,
23785 struct cmd_list_element
*c
, const char *value
)
23788 _("Whether to check \"physname\" is %s.\n"),
23792 void _initialize_dwarf2_read ();
23794 _initialize_dwarf2_read ()
23796 add_setshow_prefix_cmd ("dwarf", class_maintenance
,
23798 Set DWARF specific variables.\n\
23799 Configure DWARF variables such as the cache size."),
23801 Show DWARF specific variables.\n\
23802 Show DWARF variables such as the cache size."),
23803 &set_dwarf_cmdlist
, &show_dwarf_cmdlist
,
23804 &maintenance_set_cmdlist
, &maintenance_show_cmdlist
);
23806 add_setshow_zinteger_cmd ("max-cache-age", class_obscure
,
23807 &dwarf_max_cache_age
, _("\
23808 Set the upper bound on the age of cached DWARF compilation units."), _("\
23809 Show the upper bound on the age of cached DWARF compilation units."), _("\
23810 A higher limit means that cached compilation units will be stored\n\
23811 in memory longer, and more total memory will be used. Zero disables\n\
23812 caching, which can slow down startup."),
23814 show_dwarf_max_cache_age
,
23815 &set_dwarf_cmdlist
,
23816 &show_dwarf_cmdlist
);
23818 add_setshow_zuinteger_cmd ("dwarf-read", no_class
, &dwarf_read_debug
, _("\
23819 Set debugging of the DWARF reader."), _("\
23820 Show debugging of the DWARF reader."), _("\
23821 When enabled (non-zero), debugging messages are printed during DWARF\n\
23822 reading and symtab expansion. A value of 1 (one) provides basic\n\
23823 information. A value greater than 1 provides more verbose information."),
23826 &setdebuglist
, &showdebuglist
);
23828 add_setshow_zuinteger_cmd ("dwarf-die", no_class
, &dwarf_die_debug
, _("\
23829 Set debugging of the DWARF DIE reader."), _("\
23830 Show debugging of the DWARF DIE reader."), _("\
23831 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23832 The value is the maximum depth to print."),
23835 &setdebuglist
, &showdebuglist
);
23837 add_setshow_zuinteger_cmd ("dwarf-line", no_class
, &dwarf_line_debug
, _("\
23838 Set debugging of the dwarf line reader."), _("\
23839 Show debugging of the dwarf line reader."), _("\
23840 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23841 A value of 1 (one) provides basic information.\n\
23842 A value greater than 1 provides more verbose information."),
23845 &setdebuglist
, &showdebuglist
);
23847 add_setshow_boolean_cmd ("check-physname", no_class
, &check_physname
, _("\
23848 Set cross-checking of \"physname\" code against demangler."), _("\
23849 Show cross-checking of \"physname\" code against demangler."), _("\
23850 When enabled, GDB's internal \"physname\" code is checked against\n\
23852 NULL
, show_check_physname
,
23853 &setdebuglist
, &showdebuglist
);
23855 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23856 no_class
, &use_deprecated_index_sections
, _("\
23857 Set whether to use deprecated gdb_index sections."), _("\
23858 Show whether to use deprecated gdb_index sections."), _("\
23859 When enabled, deprecated .gdb_index sections are used anyway.\n\
23860 Normally they are ignored either because of a missing feature or\n\
23861 performance issue.\n\
23862 Warning: This option must be enabled before gdb reads the file."),
23865 &setlist
, &showlist
);
23867 dwarf2_locexpr_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23868 &dwarf2_locexpr_funcs
);
23869 dwarf2_loclist_index
= register_symbol_computed_impl (LOC_COMPUTED
,
23870 &dwarf2_loclist_funcs
);
23872 dwarf2_locexpr_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23873 &dwarf2_block_frame_base_locexpr_funcs
);
23874 dwarf2_loclist_block_index
= register_symbol_block_impl (LOC_BLOCK
,
23875 &dwarf2_block_frame_base_loclist_funcs
);
23878 selftests::register_test ("dw2_expand_symtabs_matching",
23879 selftests::dw2_expand_symtabs_matching::run_test
);
23880 selftests::register_test ("dwarf2_find_containing_comp_unit",
23881 selftests::find_containing_comp_unit::run_test
);