1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2019 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 "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
43 #include "gdb-demangle.h"
44 #include "expression.h"
45 #include "filenames.h" /* for DOSish file names */
48 #include "complaints.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
57 #include "typeprint.h"
60 #include "completer.h"
61 #include "common/vec.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
71 #include "common/filestuff.h"
73 #include "namespace.h"
74 #include "common/gdb_unlinker.h"
75 #include "common/function-view.h"
76 #include "common/gdb_optional.h"
77 #include "common/underlying.h"
78 #include "common/byte-vector.h"
79 #include "common/hash_enum.h"
80 #include "filename-seen-cache.h"
83 #include <sys/types.h>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "common/selftest.h"
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "common/pathstuff.h"
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
105 /* When non-zero, cross-check physname against demangler. */
106 static int check_physname = 0;
108 /* When non-zero, do not reject deprecated .gdb_index sections. */
109 static int use_deprecated_index_sections = 0;
111 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
113 /* The "aclass" indices for various kinds of computed DWARF symbols. */
115 static int dwarf2_locexpr_index;
116 static int dwarf2_loclist_index;
117 static int dwarf2_locexpr_block_index;
118 static int dwarf2_loclist_block_index;
120 /* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
133 struct name_component
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
140 /* The symbol's index in the symbol and constant pool tables of a
145 /* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
148 struct mapped_index_base
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
166 /* Return whether the name at IDX in the symbol table should be
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
173 /* Build the symbol name component sorted vector, if we haven't
175 void build_name_components ();
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
184 /* Prevent deleting/destroying via a base class pointer. */
186 ~mapped_index_base() = default;
189 /* A description of the mapped index. The file format is described in
190 a comment by the code that writes the index. */
191 struct mapped_index final : public mapped_index_base
193 /* A slot/bucket in the symbol table hash. */
194 struct symbol_table_slot
196 const offset_type name;
197 const offset_type vec;
200 /* Index data format version. */
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
206 /* The symbol table, implemented as a hash table. */
207 gdb::array_view<symbol_table_slot> symbol_table;
209 /* A pointer to the constant pool. */
210 const char *constant_pool = nullptr;
212 bool symbol_name_slot_invalid (offset_type idx) const override
214 const auto &bucket = this->symbol_table[idx];
215 return bucket.name == 0 && bucket.vec == 0;
218 /* Convenience method to get at the name of the symbol at IDX in the
220 const char *symbol_name_at (offset_type idx) const override
221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
227 /* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
229 struct mapped_debug_names final : public mapped_index_base
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
253 /* Attribute name DW_IDX_*. */
256 /* Attribute form DW_FORM_*. */
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
262 std::vector<attr> attr_vec;
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
267 const char *namei_to_name (uint32_t namei) const;
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
275 size_t symbol_name_count () const override
276 { return this->name_count; }
279 /* See dwarf2read.h. */
282 get_dwarf2_per_objfile (struct objfile *objfile)
284 return dwarf2_objfile_data_key.get (objfile);
287 /* Default names of the debugging sections. */
289 /* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
292 static const struct dwarf2_debug_sections dwarf2_elf_names =
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_loclists", ".zdebug_loclists" },
299 { ".debug_macinfo", ".zdebug_macinfo" },
300 { ".debug_macro", ".zdebug_macro" },
301 { ".debug_str", ".zdebug_str" },
302 { ".debug_line_str", ".zdebug_line_str" },
303 { ".debug_ranges", ".zdebug_ranges" },
304 { ".debug_rnglists", ".zdebug_rnglists" },
305 { ".debug_types", ".zdebug_types" },
306 { ".debug_addr", ".zdebug_addr" },
307 { ".debug_frame", ".zdebug_frame" },
308 { ".eh_frame", NULL },
309 { ".gdb_index", ".zgdb_index" },
310 { ".debug_names", ".zdebug_names" },
311 { ".debug_aranges", ".zdebug_aranges" },
315 /* List of DWO/DWP sections. */
317 static const struct dwop_section_names
319 struct dwarf2_section_names abbrev_dwo;
320 struct dwarf2_section_names info_dwo;
321 struct dwarf2_section_names line_dwo;
322 struct dwarf2_section_names loc_dwo;
323 struct dwarf2_section_names loclists_dwo;
324 struct dwarf2_section_names macinfo_dwo;
325 struct dwarf2_section_names macro_dwo;
326 struct dwarf2_section_names str_dwo;
327 struct dwarf2_section_names str_offsets_dwo;
328 struct dwarf2_section_names types_dwo;
329 struct dwarf2_section_names cu_index;
330 struct dwarf2_section_names tu_index;
334 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
335 { ".debug_info.dwo", ".zdebug_info.dwo" },
336 { ".debug_line.dwo", ".zdebug_line.dwo" },
337 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
338 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
339 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
340 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
341 { ".debug_str.dwo", ".zdebug_str.dwo" },
342 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
343 { ".debug_types.dwo", ".zdebug_types.dwo" },
344 { ".debug_cu_index", ".zdebug_cu_index" },
345 { ".debug_tu_index", ".zdebug_tu_index" },
348 /* local data types */
350 /* The data in a compilation unit header, after target2host
351 translation, looks like this. */
352 struct comp_unit_head
356 unsigned char addr_size;
357 unsigned char signed_addr_p;
358 sect_offset abbrev_sect_off;
360 /* Size of file offsets; either 4 or 8. */
361 unsigned int offset_size;
363 /* Size of the length field; either 4 or 12. */
364 unsigned int initial_length_size;
366 enum dwarf_unit_type unit_type;
368 /* Offset to the first byte of this compilation unit header in the
369 .debug_info section, for resolving relative reference dies. */
370 sect_offset sect_off;
372 /* Offset to first die in this cu from the start of the cu.
373 This will be the first byte following the compilation unit header. */
374 cu_offset first_die_cu_offset;
376 /* 64-bit signature of this type unit - it is valid only for
377 UNIT_TYPE DW_UT_type. */
380 /* For types, offset in the type's DIE of the type defined by this TU. */
381 cu_offset type_cu_offset_in_tu;
384 /* Type used for delaying computation of method physnames.
385 See comments for compute_delayed_physnames. */
386 struct delayed_method_info
388 /* The type to which the method is attached, i.e., its parent class. */
391 /* The index of the method in the type's function fieldlists. */
394 /* The index of the method in the fieldlist. */
397 /* The name of the DIE. */
400 /* The DIE associated with this method. */
401 struct die_info *die;
404 /* Internal state when decoding a particular compilation unit. */
407 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
410 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
412 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
413 Create the set of symtabs used by this TU, or if this TU is sharing
414 symtabs with another TU and the symtabs have already been created
415 then restore those symtabs in the line header.
416 We don't need the pc/line-number mapping for type units. */
417 void setup_type_unit_groups (struct die_info *die);
419 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
420 buildsym_compunit constructor. */
421 struct compunit_symtab *start_symtab (const char *name,
422 const char *comp_dir,
425 /* Reset the builder. */
426 void reset_builder () { m_builder.reset (); }
428 /* The header of the compilation unit. */
429 struct comp_unit_head header {};
431 /* Base address of this compilation unit. */
432 CORE_ADDR base_address = 0;
434 /* Non-zero if base_address has been set. */
437 /* The language we are debugging. */
438 enum language language = language_unknown;
439 const struct language_defn *language_defn = nullptr;
441 const char *producer = nullptr;
444 /* The symtab builder for this CU. This is only non-NULL when full
445 symbols are being read. */
446 std::unique_ptr<buildsym_compunit> m_builder;
449 /* The generic symbol table building routines have separate lists for
450 file scope symbols and all all other scopes (local scopes). So
451 we need to select the right one to pass to add_symbol_to_list().
452 We do it by keeping a pointer to the correct list in list_in_scope.
454 FIXME: The original dwarf code just treated the file scope as the
455 first local scope, and all other local scopes as nested local
456 scopes, and worked fine. Check to see if we really need to
457 distinguish these in buildsym.c. */
458 struct pending **list_in_scope = nullptr;
460 /* Hash table holding all the loaded partial DIEs
461 with partial_die->offset.SECT_OFF as hash. */
462 htab_t partial_dies = nullptr;
464 /* Storage for things with the same lifetime as this read-in compilation
465 unit, including partial DIEs. */
466 auto_obstack comp_unit_obstack;
468 /* When multiple dwarf2_cu structures are living in memory, this field
469 chains them all together, so that they can be released efficiently.
470 We will probably also want a generation counter so that most-recently-used
471 compilation units are cached... */
472 struct dwarf2_per_cu_data *read_in_chain = nullptr;
474 /* Backlink to our per_cu entry. */
475 struct dwarf2_per_cu_data *per_cu;
477 /* How many compilation units ago was this CU last referenced? */
480 /* A hash table of DIE cu_offset for following references with
481 die_info->offset.sect_off as hash. */
482 htab_t die_hash = nullptr;
484 /* Full DIEs if read in. */
485 struct die_info *dies = nullptr;
487 /* A set of pointers to dwarf2_per_cu_data objects for compilation
488 units referenced by this one. Only set during full symbol processing;
489 partial symbol tables do not have dependencies. */
490 htab_t dependencies = nullptr;
492 /* Header data from the line table, during full symbol processing. */
493 struct line_header *line_header = nullptr;
494 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
495 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
496 this is the DW_TAG_compile_unit die for this CU. We'll hold on
497 to the line header as long as this DIE is being processed. See
498 process_die_scope. */
499 die_info *line_header_die_owner = nullptr;
501 /* A list of methods which need to have physnames computed
502 after all type information has been read. */
503 std::vector<delayed_method_info> method_list;
505 /* To be copied to symtab->call_site_htab. */
506 htab_t call_site_htab = nullptr;
508 /* Non-NULL if this CU came from a DWO file.
509 There is an invariant here that is important to remember:
510 Except for attributes copied from the top level DIE in the "main"
511 (or "stub") file in preparation for reading the DWO file
512 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
513 Either there isn't a DWO file (in which case this is NULL and the point
514 is moot), or there is and either we're not going to read it (in which
515 case this is NULL) or there is and we are reading it (in which case this
517 struct dwo_unit *dwo_unit = nullptr;
519 /* The DW_AT_addr_base attribute if present, zero otherwise
520 (zero is a valid value though).
521 Note this value comes from the Fission stub CU/TU's DIE. */
522 ULONGEST addr_base = 0;
524 /* The DW_AT_ranges_base attribute if present, zero otherwise
525 (zero is a valid value though).
526 Note this value comes from the Fission stub CU/TU's DIE.
527 Also note that the value is zero in the non-DWO case so this value can
528 be used without needing to know whether DWO files are in use or not.
529 N.B. This does not apply to DW_AT_ranges appearing in
530 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
531 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
532 DW_AT_ranges_base *would* have to be applied, and we'd have to care
533 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
534 ULONGEST ranges_base = 0;
536 /* When reading debug info generated by older versions of rustc, we
537 have to rewrite some union types to be struct types with a
538 variant part. This rewriting must be done after the CU is fully
539 read in, because otherwise at the point of rewriting some struct
540 type might not have been fully processed. So, we keep a list of
541 all such types here and process them after expansion. */
542 std::vector<struct type *> rust_unions;
544 /* Mark used when releasing cached dies. */
547 /* This CU references .debug_loc. See the symtab->locations_valid field.
548 This test is imperfect as there may exist optimized debug code not using
549 any location list and still facing inlining issues if handled as
550 unoptimized code. For a future better test see GCC PR other/32998. */
551 bool has_loclist : 1;
553 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
554 if all the producer_is_* fields are valid. This information is cached
555 because profiling CU expansion showed excessive time spent in
556 producer_is_gxx_lt_4_6. */
557 bool checked_producer : 1;
558 bool producer_is_gxx_lt_4_6 : 1;
559 bool producer_is_gcc_lt_4_3 : 1;
560 bool producer_is_icc : 1;
561 bool producer_is_icc_lt_14 : 1;
562 bool producer_is_codewarrior : 1;
564 /* When true, the file that we're processing is known to have
565 debugging info for C++ namespaces. GCC 3.3.x did not produce
566 this information, but later versions do. */
568 bool processing_has_namespace_info : 1;
570 struct partial_die_info *find_partial_die (sect_offset sect_off);
572 /* If this CU was inherited by another CU (via specification,
573 abstract_origin, etc), this is the ancestor CU. */
576 /* Get the buildsym_compunit for this CU. */
577 buildsym_compunit *get_builder ()
579 /* If this CU has a builder associated with it, use that. */
580 if (m_builder != nullptr)
581 return m_builder.get ();
583 /* Otherwise, search ancestors for a valid builder. */
584 if (ancestor != nullptr)
585 return ancestor->get_builder ();
591 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
592 This includes type_unit_group and quick_file_names. */
594 struct stmt_list_hash
596 /* The DWO unit this table is from or NULL if there is none. */
597 struct dwo_unit *dwo_unit;
599 /* Offset in .debug_line or .debug_line.dwo. */
600 sect_offset line_sect_off;
603 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
604 an object of this type. */
606 struct type_unit_group
608 /* dwarf2read.c's main "handle" on a TU symtab.
609 To simplify things we create an artificial CU that "includes" all the
610 type units using this stmt_list so that the rest of the code still has
611 a "per_cu" handle on the symtab.
612 This PER_CU is recognized by having no section. */
613 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
614 struct dwarf2_per_cu_data per_cu;
616 /* The TUs that share this DW_AT_stmt_list entry.
617 This is added to while parsing type units to build partial symtabs,
618 and is deleted afterwards and not used again. */
619 VEC (sig_type_ptr) *tus;
621 /* The compunit symtab.
622 Type units in a group needn't all be defined in the same source file,
623 so we create an essentially anonymous symtab as the compunit symtab. */
624 struct compunit_symtab *compunit_symtab;
626 /* The data used to construct the hash key. */
627 struct stmt_list_hash hash;
629 /* The number of symtabs from the line header.
630 The value here must match line_header.num_file_names. */
631 unsigned int num_symtabs;
633 /* The symbol tables for this TU (obtained from the files listed in
635 WARNING: The order of entries here must match the order of entries
636 in the line header. After the first TU using this type_unit_group, the
637 line header for the subsequent TUs is recreated from this. This is done
638 because we need to use the same symtabs for each TU using the same
639 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
640 there's no guarantee the line header doesn't have duplicate entries. */
641 struct symtab **symtabs;
644 /* These sections are what may appear in a (real or virtual) DWO file. */
648 struct dwarf2_section_info abbrev;
649 struct dwarf2_section_info line;
650 struct dwarf2_section_info loc;
651 struct dwarf2_section_info loclists;
652 struct dwarf2_section_info macinfo;
653 struct dwarf2_section_info macro;
654 struct dwarf2_section_info str;
655 struct dwarf2_section_info str_offsets;
656 /* In the case of a virtual DWO file, these two are unused. */
657 struct dwarf2_section_info info;
658 VEC (dwarf2_section_info_def) *types;
661 /* CUs/TUs in DWP/DWO files. */
665 /* Backlink to the containing struct dwo_file. */
666 struct dwo_file *dwo_file;
668 /* The "id" that distinguishes this CU/TU.
669 .debug_info calls this "dwo_id", .debug_types calls this "signature".
670 Since signatures came first, we stick with it for consistency. */
673 /* The section this CU/TU lives in, in the DWO file. */
674 struct dwarf2_section_info *section;
676 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
677 sect_offset sect_off;
680 /* For types, offset in the type's DIE of the type defined by this TU. */
681 cu_offset type_offset_in_tu;
684 /* include/dwarf2.h defines the DWP section codes.
685 It defines a max value but it doesn't define a min value, which we
686 use for error checking, so provide one. */
688 enum dwp_v2_section_ids
693 /* Data for one DWO file.
695 This includes virtual DWO files (a virtual DWO file is a DWO file as it
696 appears in a DWP file). DWP files don't really have DWO files per se -
697 comdat folding of types "loses" the DWO file they came from, and from
698 a high level view DWP files appear to contain a mass of random types.
699 However, to maintain consistency with the non-DWP case we pretend DWP
700 files contain virtual DWO files, and we assign each TU with one virtual
701 DWO file (generally based on the line and abbrev section offsets -
702 a heuristic that seems to work in practice). */
706 dwo_file () = default;
707 DISABLE_COPY_AND_ASSIGN (dwo_file);
711 VEC_free (dwarf2_section_info_def, sections.types);
714 /* The DW_AT_GNU_dwo_name attribute.
715 For virtual DWO files the name is constructed from the section offsets
716 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
717 from related CU+TUs. */
718 const char *dwo_name = nullptr;
720 /* The DW_AT_comp_dir attribute. */
721 const char *comp_dir = nullptr;
723 /* The bfd, when the file is open. Otherwise this is NULL.
724 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
725 gdb_bfd_ref_ptr dbfd;
727 /* The sections that make up this DWO file.
728 Remember that for virtual DWO files in DWP V2, these are virtual
729 sections (for lack of a better name). */
730 struct dwo_sections sections {};
732 /* The CUs in the file.
733 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
734 an extension to handle LLVM's Link Time Optimization output (where
735 multiple source files may be compiled into a single object/dwo pair). */
738 /* Table of TUs in the file.
739 Each element is a struct dwo_unit. */
743 /* These sections are what may appear in a DWP file. */
747 /* These are used by both DWP version 1 and 2. */
748 struct dwarf2_section_info str;
749 struct dwarf2_section_info cu_index;
750 struct dwarf2_section_info tu_index;
752 /* These are only used by DWP version 2 files.
753 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
754 sections are referenced by section number, and are not recorded here.
755 In DWP version 2 there is at most one copy of all these sections, each
756 section being (effectively) comprised of the concatenation of all of the
757 individual sections that exist in the version 1 format.
758 To keep the code simple we treat each of these concatenated pieces as a
759 section itself (a virtual section?). */
760 struct dwarf2_section_info abbrev;
761 struct dwarf2_section_info info;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 struct dwarf2_section_info types;
770 /* These sections are what may appear in a virtual DWO file in DWP version 1.
771 A virtual DWO file is a DWO file as it appears in a DWP file. */
773 struct virtual_v1_dwo_sections
775 struct dwarf2_section_info abbrev;
776 struct dwarf2_section_info line;
777 struct dwarf2_section_info loc;
778 struct dwarf2_section_info macinfo;
779 struct dwarf2_section_info macro;
780 struct dwarf2_section_info str_offsets;
781 /* Each DWP hash table entry records one CU or one TU.
782 That is recorded here, and copied to dwo_unit.section. */
783 struct dwarf2_section_info info_or_types;
786 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
787 In version 2, the sections of the DWO files are concatenated together
788 and stored in one section of that name. Thus each ELF section contains
789 several "virtual" sections. */
791 struct virtual_v2_dwo_sections
793 bfd_size_type abbrev_offset;
794 bfd_size_type abbrev_size;
796 bfd_size_type line_offset;
797 bfd_size_type line_size;
799 bfd_size_type loc_offset;
800 bfd_size_type loc_size;
802 bfd_size_type macinfo_offset;
803 bfd_size_type macinfo_size;
805 bfd_size_type macro_offset;
806 bfd_size_type macro_size;
808 bfd_size_type str_offsets_offset;
809 bfd_size_type str_offsets_size;
811 /* Each DWP hash table entry records one CU or one TU.
812 That is recorded here, and copied to dwo_unit.section. */
813 bfd_size_type info_or_types_offset;
814 bfd_size_type info_or_types_size;
817 /* Contents of DWP hash tables. */
819 struct dwp_hash_table
821 uint32_t version, nr_columns;
822 uint32_t nr_units, nr_slots;
823 const gdb_byte *hash_table, *unit_table;
828 const gdb_byte *indices;
832 /* This is indexed by column number and gives the id of the section
834 #define MAX_NR_V2_DWO_SECTIONS \
835 (1 /* .debug_info or .debug_types */ \
836 + 1 /* .debug_abbrev */ \
837 + 1 /* .debug_line */ \
838 + 1 /* .debug_loc */ \
839 + 1 /* .debug_str_offsets */ \
840 + 1 /* .debug_macro or .debug_macinfo */)
841 int section_ids[MAX_NR_V2_DWO_SECTIONS];
842 const gdb_byte *offsets;
843 const gdb_byte *sizes;
848 /* Data for one DWP file. */
852 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
854 dbfd (std::move (abfd))
858 /* Name of the file. */
861 /* File format version. */
865 gdb_bfd_ref_ptr dbfd;
867 /* Section info for this file. */
868 struct dwp_sections sections {};
870 /* Table of CUs in the file. */
871 const struct dwp_hash_table *cus = nullptr;
873 /* Table of TUs in the file. */
874 const struct dwp_hash_table *tus = nullptr;
876 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
877 htab_t loaded_cus {};
878 htab_t loaded_tus {};
880 /* Table to map ELF section numbers to their sections.
881 This is only needed for the DWP V1 file format. */
882 unsigned int num_sections = 0;
883 asection **elf_sections = nullptr;
886 /* Struct used to pass misc. parameters to read_die_and_children, et
887 al. which are used for both .debug_info and .debug_types dies.
888 All parameters here are unchanging for the life of the call. This
889 struct exists to abstract away the constant parameters of die reading. */
891 struct die_reader_specs
893 /* The bfd of die_section. */
896 /* The CU of the DIE we are parsing. */
897 struct dwarf2_cu *cu;
899 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
900 struct dwo_file *dwo_file;
902 /* The section the die comes from.
903 This is either .debug_info or .debug_types, or the .dwo variants. */
904 struct dwarf2_section_info *die_section;
906 /* die_section->buffer. */
907 const gdb_byte *buffer;
909 /* The end of the buffer. */
910 const gdb_byte *buffer_end;
912 /* The value of the DW_AT_comp_dir attribute. */
913 const char *comp_dir;
915 /* The abbreviation table to use when reading the DIEs. */
916 struct abbrev_table *abbrev_table;
919 /* Type of function passed to init_cutu_and_read_dies, et.al. */
920 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
921 const gdb_byte *info_ptr,
922 struct die_info *comp_unit_die,
926 /* A 1-based directory index. This is a strong typedef to prevent
927 accidentally using a directory index as a 0-based index into an
929 enum class dir_index : unsigned int {};
931 /* Likewise, a 1-based file name index. */
932 enum class file_name_index : unsigned int {};
936 file_entry () = default;
938 file_entry (const char *name_, dir_index d_index_,
939 unsigned int mod_time_, unsigned int length_)
942 mod_time (mod_time_),
946 /* Return the include directory at D_INDEX stored in LH. Returns
947 NULL if D_INDEX is out of bounds. */
948 const char *include_dir (const line_header *lh) const;
950 /* The file name. Note this is an observing pointer. The memory is
951 owned by debug_line_buffer. */
954 /* The directory index (1-based). */
955 dir_index d_index {};
957 unsigned int mod_time {};
959 unsigned int length {};
961 /* True if referenced by the Line Number Program. */
964 /* The associated symbol table, if any. */
965 struct symtab *symtab {};
968 /* The line number information for a compilation unit (found in the
969 .debug_line section) begins with a "statement program header",
970 which contains the following information. */
977 /* Add an entry to the include directory table. */
978 void add_include_dir (const char *include_dir);
980 /* Add an entry to the file name table. */
981 void add_file_name (const char *name, dir_index d_index,
982 unsigned int mod_time, unsigned int length);
984 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
986 const char *include_dir_at (dir_index index) const
988 /* Convert directory index number (1-based) to vector index
990 size_t vec_index = to_underlying (index) - 1;
992 if (vec_index >= include_dirs.size ())
994 return include_dirs[vec_index];
997 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
999 file_entry *file_name_at (file_name_index index)
1001 /* Convert file name index number (1-based) to vector index
1003 size_t vec_index = to_underlying (index) - 1;
1005 if (vec_index >= file_names.size ())
1007 return &file_names[vec_index];
1010 /* Offset of line number information in .debug_line section. */
1011 sect_offset sect_off {};
1013 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1014 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1016 unsigned int total_length {};
1017 unsigned short version {};
1018 unsigned int header_length {};
1019 unsigned char minimum_instruction_length {};
1020 unsigned char maximum_ops_per_instruction {};
1021 unsigned char default_is_stmt {};
1023 unsigned char line_range {};
1024 unsigned char opcode_base {};
1026 /* standard_opcode_lengths[i] is the number of operands for the
1027 standard opcode whose value is i. This means that
1028 standard_opcode_lengths[0] is unused, and the last meaningful
1029 element is standard_opcode_lengths[opcode_base - 1]. */
1030 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1032 /* The include_directories table. Note these are observing
1033 pointers. The memory is owned by debug_line_buffer. */
1034 std::vector<const char *> include_dirs;
1036 /* The file_names table. */
1037 std::vector<file_entry> file_names;
1039 /* The start and end of the statement program following this
1040 header. These point into dwarf2_per_objfile->line_buffer. */
1041 const gdb_byte *statement_program_start {}, *statement_program_end {};
1044 typedef std::unique_ptr<line_header> line_header_up;
1047 file_entry::include_dir (const line_header *lh) const
1049 return lh->include_dir_at (d_index);
1052 /* When we construct a partial symbol table entry we only
1053 need this much information. */
1054 struct partial_die_info : public allocate_on_obstack
1056 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1058 /* Disable assign but still keep copy ctor, which is needed
1059 load_partial_dies. */
1060 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1062 /* Adjust the partial die before generating a symbol for it. This
1063 function may set the is_external flag or change the DIE's
1065 void fixup (struct dwarf2_cu *cu);
1067 /* Read a minimal amount of information into the minimal die
1069 const gdb_byte *read (const struct die_reader_specs *reader,
1070 const struct abbrev_info &abbrev,
1071 const gdb_byte *info_ptr);
1073 /* Offset of this DIE. */
1074 const sect_offset sect_off;
1076 /* DWARF-2 tag for this DIE. */
1077 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1079 /* Assorted flags describing the data found in this DIE. */
1080 const unsigned int has_children : 1;
1082 unsigned int is_external : 1;
1083 unsigned int is_declaration : 1;
1084 unsigned int has_type : 1;
1085 unsigned int has_specification : 1;
1086 unsigned int has_pc_info : 1;
1087 unsigned int may_be_inlined : 1;
1089 /* This DIE has been marked DW_AT_main_subprogram. */
1090 unsigned int main_subprogram : 1;
1092 /* Flag set if the SCOPE field of this structure has been
1094 unsigned int scope_set : 1;
1096 /* Flag set if the DIE has a byte_size attribute. */
1097 unsigned int has_byte_size : 1;
1099 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1100 unsigned int has_const_value : 1;
1102 /* Flag set if any of the DIE's children are template arguments. */
1103 unsigned int has_template_arguments : 1;
1105 /* Flag set if fixup has been called on this die. */
1106 unsigned int fixup_called : 1;
1108 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1109 unsigned int is_dwz : 1;
1111 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1112 unsigned int spec_is_dwz : 1;
1114 /* The name of this DIE. Normally the value of DW_AT_name, but
1115 sometimes a default name for unnamed DIEs. */
1116 const char *name = nullptr;
1118 /* The linkage name, if present. */
1119 const char *linkage_name = nullptr;
1121 /* The scope to prepend to our children. This is generally
1122 allocated on the comp_unit_obstack, so will disappear
1123 when this compilation unit leaves the cache. */
1124 const char *scope = nullptr;
1126 /* Some data associated with the partial DIE. The tag determines
1127 which field is live. */
1130 /* The location description associated with this DIE, if any. */
1131 struct dwarf_block *locdesc;
1132 /* The offset of an import, for DW_TAG_imported_unit. */
1133 sect_offset sect_off;
1136 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1137 CORE_ADDR lowpc = 0;
1138 CORE_ADDR highpc = 0;
1140 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1141 DW_AT_sibling, if any. */
1142 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1143 could return DW_AT_sibling values to its caller load_partial_dies. */
1144 const gdb_byte *sibling = nullptr;
1146 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1147 DW_AT_specification (or DW_AT_abstract_origin or
1148 DW_AT_extension). */
1149 sect_offset spec_offset {};
1151 /* Pointers to this DIE's parent, first child, and next sibling,
1153 struct partial_die_info *die_parent = nullptr;
1154 struct partial_die_info *die_child = nullptr;
1155 struct partial_die_info *die_sibling = nullptr;
1157 friend struct partial_die_info *
1158 dwarf2_cu::find_partial_die (sect_offset sect_off);
1161 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1162 partial_die_info (sect_offset sect_off)
1163 : partial_die_info (sect_off, DW_TAG_padding, 0)
1167 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1169 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1174 has_specification = 0;
1177 main_subprogram = 0;
1180 has_const_value = 0;
1181 has_template_arguments = 0;
1188 /* This data structure holds the information of an abbrev. */
1191 unsigned int number; /* number identifying abbrev */
1192 enum dwarf_tag tag; /* dwarf tag */
1193 unsigned short has_children; /* boolean */
1194 unsigned short num_attrs; /* number of attributes */
1195 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1196 struct abbrev_info *next; /* next in chain */
1201 ENUM_BITFIELD(dwarf_attribute) name : 16;
1202 ENUM_BITFIELD(dwarf_form) form : 16;
1204 /* It is valid only if FORM is DW_FORM_implicit_const. */
1205 LONGEST implicit_const;
1208 /* Size of abbrev_table.abbrev_hash_table. */
1209 #define ABBREV_HASH_SIZE 121
1211 /* Top level data structure to contain an abbreviation table. */
1215 explicit abbrev_table (sect_offset off)
1219 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1220 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1223 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1225 /* Allocate space for a struct abbrev_info object in
1227 struct abbrev_info *alloc_abbrev ();
1229 /* Add an abbreviation to the table. */
1230 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1232 /* Look up an abbrev in the table.
1233 Returns NULL if the abbrev is not found. */
1235 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1238 /* Where the abbrev table came from.
1239 This is used as a sanity check when the table is used. */
1240 const sect_offset sect_off;
1242 /* Storage for the abbrev table. */
1243 auto_obstack abbrev_obstack;
1247 /* Hash table of abbrevs.
1248 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1249 It could be statically allocated, but the previous code didn't so we
1251 struct abbrev_info **m_abbrevs;
1254 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1256 /* Attributes have a name and a value. */
1259 ENUM_BITFIELD(dwarf_attribute) name : 16;
1260 ENUM_BITFIELD(dwarf_form) form : 15;
1262 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1263 field should be in u.str (existing only for DW_STRING) but it is kept
1264 here for better struct attribute alignment. */
1265 unsigned int string_is_canonical : 1;
1270 struct dwarf_block *blk;
1279 /* This data structure holds a complete die structure. */
1282 /* DWARF-2 tag for this DIE. */
1283 ENUM_BITFIELD(dwarf_tag) tag : 16;
1285 /* Number of attributes */
1286 unsigned char num_attrs;
1288 /* True if we're presently building the full type name for the
1289 type derived from this DIE. */
1290 unsigned char building_fullname : 1;
1292 /* True if this die is in process. PR 16581. */
1293 unsigned char in_process : 1;
1296 unsigned int abbrev;
1298 /* Offset in .debug_info or .debug_types section. */
1299 sect_offset sect_off;
1301 /* The dies in a compilation unit form an n-ary tree. PARENT
1302 points to this die's parent; CHILD points to the first child of
1303 this node; and all the children of a given node are chained
1304 together via their SIBLING fields. */
1305 struct die_info *child; /* Its first child, if any. */
1306 struct die_info *sibling; /* Its next sibling, if any. */
1307 struct die_info *parent; /* Its parent, if any. */
1309 /* An array of attributes, with NUM_ATTRS elements. There may be
1310 zero, but it's not common and zero-sized arrays are not
1311 sufficiently portable C. */
1312 struct attribute attrs[1];
1315 /* Get at parts of an attribute structure. */
1317 #define DW_STRING(attr) ((attr)->u.str)
1318 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1319 #define DW_UNSND(attr) ((attr)->u.unsnd)
1320 #define DW_BLOCK(attr) ((attr)->u.blk)
1321 #define DW_SND(attr) ((attr)->u.snd)
1322 #define DW_ADDR(attr) ((attr)->u.addr)
1323 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1325 /* Blocks are a bunch of untyped bytes. */
1330 /* Valid only if SIZE is not zero. */
1331 const gdb_byte *data;
1334 #ifndef ATTR_ALLOC_CHUNK
1335 #define ATTR_ALLOC_CHUNK 4
1338 /* Allocate fields for structs, unions and enums in this size. */
1339 #ifndef DW_FIELD_ALLOC_CHUNK
1340 #define DW_FIELD_ALLOC_CHUNK 4
1343 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1344 but this would require a corresponding change in unpack_field_as_long
1346 static int bits_per_byte = 8;
1348 /* When reading a variant or variant part, we track a bit more
1349 information about the field, and store it in an object of this
1352 struct variant_field
1354 /* If we see a DW_TAG_variant, then this will be the discriminant
1356 ULONGEST discriminant_value;
1357 /* If we see a DW_TAG_variant, then this will be set if this is the
1359 bool default_branch;
1360 /* While reading a DW_TAG_variant_part, this will be set if this
1361 field is the discriminant. */
1362 bool is_discriminant;
1367 int accessibility = 0;
1369 /* Extra information to describe a variant or variant part. */
1370 struct variant_field variant {};
1371 struct field field {};
1376 const char *name = nullptr;
1377 std::vector<struct fn_field> fnfields;
1380 /* The routines that read and process dies for a C struct or C++ class
1381 pass lists of data member fields and lists of member function fields
1382 in an instance of a field_info structure, as defined below. */
1385 /* List of data member and baseclasses fields. */
1386 std::vector<struct nextfield> fields;
1387 std::vector<struct nextfield> baseclasses;
1389 /* Number of fields (including baseclasses). */
1392 /* Set if the accesibility of one of the fields is not public. */
1393 int non_public_fields = 0;
1395 /* Member function fieldlist array, contains name of possibly overloaded
1396 member function, number of overloaded member functions and a pointer
1397 to the head of the member function field chain. */
1398 std::vector<struct fnfieldlist> fnfieldlists;
1400 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1401 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1402 std::vector<struct decl_field> typedef_field_list;
1404 /* Nested types defined by this class and the number of elements in this
1406 std::vector<struct decl_field> nested_types_list;
1409 /* One item on the queue of compilation units to read in full symbols
1411 struct dwarf2_queue_item
1413 struct dwarf2_per_cu_data *per_cu;
1414 enum language pretend_language;
1415 struct dwarf2_queue_item *next;
1418 /* The current queue. */
1419 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1421 /* Loaded secondary compilation units are kept in memory until they
1422 have not been referenced for the processing of this many
1423 compilation units. Set this to zero to disable caching. Cache
1424 sizes of up to at least twenty will improve startup time for
1425 typical inter-CU-reference binaries, at an obvious memory cost. */
1426 static int dwarf_max_cache_age = 5;
1428 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1429 struct cmd_list_element *c, const char *value)
1431 fprintf_filtered (file, _("The upper bound on the age of cached "
1432 "DWARF compilation units is %s.\n"),
1436 /* local function prototypes */
1438 static const char *get_section_name (const struct dwarf2_section_info *);
1440 static const char *get_section_file_name (const struct dwarf2_section_info *);
1442 static void dwarf2_find_base_address (struct die_info *die,
1443 struct dwarf2_cu *cu);
1445 static struct partial_symtab *create_partial_symtab
1446 (struct dwarf2_per_cu_data *per_cu, const char *name);
1448 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1449 const gdb_byte *info_ptr,
1450 struct die_info *type_unit_die,
1451 int has_children, void *data);
1453 static void dwarf2_build_psymtabs_hard
1454 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1456 static void scan_partial_symbols (struct partial_die_info *,
1457 CORE_ADDR *, CORE_ADDR *,
1458 int, struct dwarf2_cu *);
1460 static void add_partial_symbol (struct partial_die_info *,
1461 struct dwarf2_cu *);
1463 static void add_partial_namespace (struct partial_die_info *pdi,
1464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1465 int set_addrmap, struct dwarf2_cu *cu);
1467 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1468 CORE_ADDR *highpc, int set_addrmap,
1469 struct dwarf2_cu *cu);
1471 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1472 struct dwarf2_cu *cu);
1474 static void add_partial_subprogram (struct partial_die_info *pdi,
1475 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1476 int need_pc, struct dwarf2_cu *cu);
1478 static void dwarf2_read_symtab (struct partial_symtab *,
1481 static void psymtab_to_symtab_1 (struct partial_symtab *);
1483 static abbrev_table_up abbrev_table_read_table
1484 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1487 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1489 static struct partial_die_info *load_partial_dies
1490 (const struct die_reader_specs *, const gdb_byte *, int);
1492 /* A pair of partial_die_info and compilation unit. */
1493 struct cu_partial_die_info
1495 /* The compilation unit of the partial_die_info. */
1496 struct dwarf2_cu *cu;
1497 /* A partial_die_info. */
1498 struct partial_die_info *pdi;
1500 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1506 cu_partial_die_info () = delete;
1509 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1510 struct dwarf2_cu *);
1512 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1513 struct attribute *, struct attr_abbrev *,
1516 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1518 static int read_1_signed_byte (bfd *, const gdb_byte *);
1520 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1522 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1523 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1525 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1527 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1529 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1532 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1534 static LONGEST read_checked_initial_length_and_offset
1535 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1536 unsigned int *, unsigned int *);
1538 static LONGEST read_offset (bfd *, const gdb_byte *,
1539 const struct comp_unit_head *,
1542 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1544 static sect_offset read_abbrev_offset
1545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1546 struct dwarf2_section_info *, sect_offset);
1548 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1550 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1552 static const char *read_indirect_string
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1554 const struct comp_unit_head *, unsigned int *);
1556 static const char *read_indirect_line_string
1557 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1558 const struct comp_unit_head *, unsigned int *);
1560 static const char *read_indirect_string_at_offset
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1562 LONGEST str_offset);
1564 static const char *read_indirect_string_from_dwz
1565 (struct objfile *objfile, struct dwz_file *, LONGEST);
1567 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1569 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1573 static const char *read_str_index (const struct die_reader_specs *reader,
1574 ULONGEST str_index);
1576 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1578 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1579 struct dwarf2_cu *);
1581 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1584 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1585 struct dwarf2_cu *cu);
1587 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1588 struct dwarf2_cu *cu);
1590 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1592 static struct die_info *die_specification (struct die_info *die,
1593 struct dwarf2_cu **);
1595 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1596 struct dwarf2_cu *cu);
1598 static void dwarf_decode_lines (struct line_header *, const char *,
1599 struct dwarf2_cu *, struct partial_symtab *,
1600 CORE_ADDR, int decode_mapping);
1602 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1605 static struct symbol *new_symbol (struct die_info *, struct type *,
1606 struct dwarf2_cu *, struct symbol * = NULL);
1608 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1609 struct dwarf2_cu *);
1611 static void dwarf2_const_value_attr (const struct attribute *attr,
1614 struct obstack *obstack,
1615 struct dwarf2_cu *cu, LONGEST *value,
1616 const gdb_byte **bytes,
1617 struct dwarf2_locexpr_baton **baton);
1619 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1621 static int need_gnat_info (struct dwarf2_cu *);
1623 static struct type *die_descriptive_type (struct die_info *,
1624 struct dwarf2_cu *);
1626 static void set_descriptive_type (struct type *, struct die_info *,
1627 struct dwarf2_cu *);
1629 static struct type *die_containing_type (struct die_info *,
1630 struct dwarf2_cu *);
1632 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1633 struct dwarf2_cu *);
1635 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1637 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1639 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1641 static char *typename_concat (struct obstack *obs, const char *prefix,
1642 const char *suffix, int physname,
1643 struct dwarf2_cu *cu);
1645 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1647 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1649 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1651 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1653 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1655 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1657 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1658 struct dwarf2_cu *, struct partial_symtab *);
1660 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1661 values. Keep the items ordered with increasing constraints compliance. */
1664 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1665 PC_BOUNDS_NOT_PRESENT,
1667 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1668 were present but they do not form a valid range of PC addresses. */
1671 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1674 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1678 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1679 CORE_ADDR *, CORE_ADDR *,
1681 struct partial_symtab *);
1683 static void get_scope_pc_bounds (struct die_info *,
1684 CORE_ADDR *, CORE_ADDR *,
1685 struct dwarf2_cu *);
1687 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1688 CORE_ADDR, struct dwarf2_cu *);
1690 static void dwarf2_add_field (struct field_info *, struct die_info *,
1691 struct dwarf2_cu *);
1693 static void dwarf2_attach_fields_to_type (struct field_info *,
1694 struct type *, struct dwarf2_cu *);
1696 static void dwarf2_add_member_fn (struct field_info *,
1697 struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1700 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1702 struct dwarf2_cu *);
1704 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1706 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1708 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1710 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1712 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1714 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1716 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1718 static struct type *read_module_type (struct die_info *die,
1719 struct dwarf2_cu *cu);
1721 static const char *namespace_name (struct die_info *die,
1722 int *is_anonymous, struct dwarf2_cu *);
1724 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1726 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1728 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1729 struct dwarf2_cu *);
1731 static struct die_info *read_die_and_siblings_1
1732 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1735 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1736 const gdb_byte *info_ptr,
1737 const gdb_byte **new_info_ptr,
1738 struct die_info *parent);
1740 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1741 struct die_info **, const gdb_byte *,
1744 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1745 struct die_info **, const gdb_byte *,
1748 static void process_die (struct die_info *, struct dwarf2_cu *);
1750 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1753 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1755 static const char *dwarf2_full_name (const char *name,
1756 struct die_info *die,
1757 struct dwarf2_cu *cu);
1759 static const char *dwarf2_physname (const char *name, struct die_info *die,
1760 struct dwarf2_cu *cu);
1762 static struct die_info *dwarf2_extension (struct die_info *die,
1763 struct dwarf2_cu **);
1765 static const char *dwarf_tag_name (unsigned int);
1767 static const char *dwarf_attr_name (unsigned int);
1769 static const char *dwarf_form_name (unsigned int);
1771 static const char *dwarf_bool_name (unsigned int);
1773 static const char *dwarf_type_encoding_name (unsigned int);
1775 static struct die_info *sibling_die (struct die_info *);
1777 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1779 static void dump_die_for_error (struct die_info *);
1781 static void dump_die_1 (struct ui_file *, int level, int max_level,
1784 /*static*/ void dump_die (struct die_info *, int max_level);
1786 static void store_in_ref_table (struct die_info *,
1787 struct dwarf2_cu *);
1789 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1791 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1793 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1794 const struct attribute *,
1795 struct dwarf2_cu **);
1797 static struct die_info *follow_die_ref (struct die_info *,
1798 const struct attribute *,
1799 struct dwarf2_cu **);
1801 static struct die_info *follow_die_sig (struct die_info *,
1802 const struct attribute *,
1803 struct dwarf2_cu **);
1805 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1806 struct dwarf2_cu *);
1808 static struct type *get_DW_AT_signature_type (struct die_info *,
1809 const struct attribute *,
1810 struct dwarf2_cu *);
1812 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1814 static void read_signatured_type (struct signatured_type *);
1816 static int attr_to_dynamic_prop (const struct attribute *attr,
1817 struct die_info *die, struct dwarf2_cu *cu,
1818 struct dynamic_prop *prop);
1820 /* memory allocation interface */
1822 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1824 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1826 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1828 static int attr_form_is_block (const struct attribute *);
1830 static int attr_form_is_section_offset (const struct attribute *);
1832 static int attr_form_is_constant (const struct attribute *);
1834 static int attr_form_is_ref (const struct attribute *);
1836 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1837 struct dwarf2_loclist_baton *baton,
1838 const struct attribute *attr);
1840 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1842 struct dwarf2_cu *cu,
1845 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1846 const gdb_byte *info_ptr,
1847 struct abbrev_info *abbrev);
1849 static hashval_t partial_die_hash (const void *item);
1851 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1853 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1854 (sect_offset sect_off, unsigned int offset_in_dwz,
1855 struct dwarf2_per_objfile *dwarf2_per_objfile);
1857 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1858 struct die_info *comp_unit_die,
1859 enum language pretend_language);
1861 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1863 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1865 static struct type *set_die_type (struct die_info *, struct type *,
1866 struct dwarf2_cu *);
1868 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1870 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1872 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1875 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1878 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1881 static void dwarf2_add_dependence (struct dwarf2_cu *,
1882 struct dwarf2_per_cu_data *);
1884 static void dwarf2_mark (struct dwarf2_cu *);
1886 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1888 static struct type *get_die_type_at_offset (sect_offset,
1889 struct dwarf2_per_cu_data *);
1891 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1893 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1894 enum language pretend_language);
1896 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1898 /* Class, the destructor of which frees all allocated queue entries. This
1899 will only have work to do if an error was thrown while processing the
1900 dwarf. If no error was thrown then the queue entries should have all
1901 been processed, and freed, as we went along. */
1903 class dwarf2_queue_guard
1906 dwarf2_queue_guard () = default;
1908 /* Free any entries remaining on the queue. There should only be
1909 entries left if we hit an error while processing the dwarf. */
1910 ~dwarf2_queue_guard ()
1912 struct dwarf2_queue_item *item, *last;
1914 item = dwarf2_queue;
1917 /* Anything still marked queued is likely to be in an
1918 inconsistent state, so discard it. */
1919 if (item->per_cu->queued)
1921 if (item->per_cu->cu != NULL)
1922 free_one_cached_comp_unit (item->per_cu);
1923 item->per_cu->queued = 0;
1931 dwarf2_queue = dwarf2_queue_tail = NULL;
1935 /* The return type of find_file_and_directory. Note, the enclosed
1936 string pointers are only valid while this object is valid. */
1938 struct file_and_directory
1940 /* The filename. This is never NULL. */
1943 /* The compilation directory. NULL if not known. If we needed to
1944 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1945 points directly to the DW_AT_comp_dir string attribute owned by
1946 the obstack that owns the DIE. */
1947 const char *comp_dir;
1949 /* If we needed to build a new string for comp_dir, this is what
1950 owns the storage. */
1951 std::string comp_dir_storage;
1954 static file_and_directory find_file_and_directory (struct die_info *die,
1955 struct dwarf2_cu *cu);
1957 static char *file_full_name (int file, struct line_header *lh,
1958 const char *comp_dir);
1960 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1961 enum class rcuh_kind { COMPILE, TYPE };
1963 static const gdb_byte *read_and_check_comp_unit_head
1964 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1965 struct comp_unit_head *header,
1966 struct dwarf2_section_info *section,
1967 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1968 rcuh_kind section_kind);
1970 static void init_cutu_and_read_dies
1971 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1972 int use_existing_cu, int keep, bool skip_partial,
1973 die_reader_func_ftype *die_reader_func, void *data);
1975 static void init_cutu_and_read_dies_simple
1976 (struct dwarf2_per_cu_data *this_cu,
1977 die_reader_func_ftype *die_reader_func, void *data);
1979 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1981 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1983 static struct dwo_unit *lookup_dwo_unit_in_dwp
1984 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1985 struct dwp_file *dwp_file, const char *comp_dir,
1986 ULONGEST signature, int is_debug_types);
1988 static struct dwp_file *get_dwp_file
1989 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1991 static struct dwo_unit *lookup_dwo_comp_unit
1992 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1994 static struct dwo_unit *lookup_dwo_type_unit
1995 (struct signatured_type *, const char *, const char *);
1997 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1999 /* A unique pointer to a dwo_file. */
2001 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2003 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2005 static void check_producer (struct dwarf2_cu *cu);
2007 static void free_line_header_voidp (void *arg);
2009 /* Various complaints about symbol reading that don't abort the process. */
2012 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2014 complaint (_("statement list doesn't fit in .debug_line section"));
2018 dwarf2_debug_line_missing_file_complaint (void)
2020 complaint (_(".debug_line section has line data without a file"));
2024 dwarf2_debug_line_missing_end_sequence_complaint (void)
2026 complaint (_(".debug_line section has line "
2027 "program sequence without an end"));
2031 dwarf2_complex_location_expr_complaint (void)
2033 complaint (_("location expression too complex"));
2037 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2040 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2045 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2047 complaint (_("debug info runs off end of %s section"
2049 get_section_name (section),
2050 get_section_file_name (section));
2054 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2056 complaint (_("macro debug info contains a "
2057 "malformed macro definition:\n`%s'"),
2062 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2064 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2068 /* Hash function for line_header_hash. */
2071 line_header_hash (const struct line_header *ofs)
2073 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2076 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2079 line_header_hash_voidp (const void *item)
2081 const struct line_header *ofs = (const struct line_header *) item;
2083 return line_header_hash (ofs);
2086 /* Equality function for line_header_hash. */
2089 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2091 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2092 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2094 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2095 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2100 /* Read the given attribute value as an address, taking the attribute's
2101 form into account. */
2104 attr_value_as_address (struct attribute *attr)
2108 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2109 && attr->form != DW_FORM_GNU_addr_index)
2111 /* Aside from a few clearly defined exceptions, attributes that
2112 contain an address must always be in DW_FORM_addr form.
2113 Unfortunately, some compilers happen to be violating this
2114 requirement by encoding addresses using other forms, such
2115 as DW_FORM_data4 for example. For those broken compilers,
2116 we try to do our best, without any guarantee of success,
2117 to interpret the address correctly. It would also be nice
2118 to generate a complaint, but that would require us to maintain
2119 a list of legitimate cases where a non-address form is allowed,
2120 as well as update callers to pass in at least the CU's DWARF
2121 version. This is more overhead than what we're willing to
2122 expand for a pretty rare case. */
2123 addr = DW_UNSND (attr);
2126 addr = DW_ADDR (attr);
2131 /* See declaration. */
2133 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2134 const dwarf2_debug_sections *names)
2135 : objfile (objfile_)
2138 names = &dwarf2_elf_names;
2140 bfd *obfd = objfile->obfd;
2142 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2143 locate_sections (obfd, sec, *names);
2146 dwarf2_per_objfile::~dwarf2_per_objfile ()
2148 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2149 free_cached_comp_units ();
2151 if (quick_file_names_table)
2152 htab_delete (quick_file_names_table);
2154 if (line_header_hash)
2155 htab_delete (line_header_hash);
2157 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2158 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2160 for (signatured_type *sig_type : all_type_units)
2161 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2163 VEC_free (dwarf2_section_info_def, types);
2165 /* Everything else should be on the objfile obstack. */
2168 /* See declaration. */
2171 dwarf2_per_objfile::free_cached_comp_units ()
2173 dwarf2_per_cu_data *per_cu = read_in_chain;
2174 dwarf2_per_cu_data **last_chain = &read_in_chain;
2175 while (per_cu != NULL)
2177 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2180 *last_chain = next_cu;
2185 /* A helper class that calls free_cached_comp_units on
2188 class free_cached_comp_units
2192 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2193 : m_per_objfile (per_objfile)
2197 ~free_cached_comp_units ()
2199 m_per_objfile->free_cached_comp_units ();
2202 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2206 dwarf2_per_objfile *m_per_objfile;
2209 /* Try to locate the sections we need for DWARF 2 debugging
2210 information and return true if we have enough to do something.
2211 NAMES points to the dwarf2 section names, or is NULL if the standard
2212 ELF names are used. */
2215 dwarf2_has_info (struct objfile *objfile,
2216 const struct dwarf2_debug_sections *names)
2218 if (objfile->flags & OBJF_READNEVER)
2221 struct dwarf2_per_objfile *dwarf2_per_objfile
2222 = get_dwarf2_per_objfile (objfile);
2224 if (dwarf2_per_objfile == NULL)
2225 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2228 return (!dwarf2_per_objfile->info.is_virtual
2229 && dwarf2_per_objfile->info.s.section != NULL
2230 && !dwarf2_per_objfile->abbrev.is_virtual
2231 && dwarf2_per_objfile->abbrev.s.section != NULL);
2234 /* Return the containing section of virtual section SECTION. */
2236 static struct dwarf2_section_info *
2237 get_containing_section (const struct dwarf2_section_info *section)
2239 gdb_assert (section->is_virtual);
2240 return section->s.containing_section;
2243 /* Return the bfd owner of SECTION. */
2246 get_section_bfd_owner (const struct dwarf2_section_info *section)
2248 if (section->is_virtual)
2250 section = get_containing_section (section);
2251 gdb_assert (!section->is_virtual);
2253 return section->s.section->owner;
2256 /* Return the bfd section of SECTION.
2257 Returns NULL if the section is not present. */
2260 get_section_bfd_section (const struct dwarf2_section_info *section)
2262 if (section->is_virtual)
2264 section = get_containing_section (section);
2265 gdb_assert (!section->is_virtual);
2267 return section->s.section;
2270 /* Return the name of SECTION. */
2273 get_section_name (const struct dwarf2_section_info *section)
2275 asection *sectp = get_section_bfd_section (section);
2277 gdb_assert (sectp != NULL);
2278 return bfd_section_name (get_section_bfd_owner (section), sectp);
2281 /* Return the name of the file SECTION is in. */
2284 get_section_file_name (const struct dwarf2_section_info *section)
2286 bfd *abfd = get_section_bfd_owner (section);
2288 return bfd_get_filename (abfd);
2291 /* Return the id of SECTION.
2292 Returns 0 if SECTION doesn't exist. */
2295 get_section_id (const struct dwarf2_section_info *section)
2297 asection *sectp = get_section_bfd_section (section);
2304 /* Return the flags of SECTION.
2305 SECTION (or containing section if this is a virtual section) must exist. */
2308 get_section_flags (const struct dwarf2_section_info *section)
2310 asection *sectp = get_section_bfd_section (section);
2312 gdb_assert (sectp != NULL);
2313 return bfd_get_section_flags (sectp->owner, sectp);
2316 /* When loading sections, we look either for uncompressed section or for
2317 compressed section names. */
2320 section_is_p (const char *section_name,
2321 const struct dwarf2_section_names *names)
2323 if (names->normal != NULL
2324 && strcmp (section_name, names->normal) == 0)
2326 if (names->compressed != NULL
2327 && strcmp (section_name, names->compressed) == 0)
2332 /* See declaration. */
2335 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2336 const dwarf2_debug_sections &names)
2338 flagword aflag = bfd_get_section_flags (abfd, sectp);
2340 if ((aflag & SEC_HAS_CONTENTS) == 0)
2343 else if (section_is_p (sectp->name, &names.info))
2345 this->info.s.section = sectp;
2346 this->info.size = bfd_get_section_size (sectp);
2348 else if (section_is_p (sectp->name, &names.abbrev))
2350 this->abbrev.s.section = sectp;
2351 this->abbrev.size = bfd_get_section_size (sectp);
2353 else if (section_is_p (sectp->name, &names.line))
2355 this->line.s.section = sectp;
2356 this->line.size = bfd_get_section_size (sectp);
2358 else if (section_is_p (sectp->name, &names.loc))
2360 this->loc.s.section = sectp;
2361 this->loc.size = bfd_get_section_size (sectp);
2363 else if (section_is_p (sectp->name, &names.loclists))
2365 this->loclists.s.section = sectp;
2366 this->loclists.size = bfd_get_section_size (sectp);
2368 else if (section_is_p (sectp->name, &names.macinfo))
2370 this->macinfo.s.section = sectp;
2371 this->macinfo.size = bfd_get_section_size (sectp);
2373 else if (section_is_p (sectp->name, &names.macro))
2375 this->macro.s.section = sectp;
2376 this->macro.size = bfd_get_section_size (sectp);
2378 else if (section_is_p (sectp->name, &names.str))
2380 this->str.s.section = sectp;
2381 this->str.size = bfd_get_section_size (sectp);
2383 else if (section_is_p (sectp->name, &names.line_str))
2385 this->line_str.s.section = sectp;
2386 this->line_str.size = bfd_get_section_size (sectp);
2388 else if (section_is_p (sectp->name, &names.addr))
2390 this->addr.s.section = sectp;
2391 this->addr.size = bfd_get_section_size (sectp);
2393 else if (section_is_p (sectp->name, &names.frame))
2395 this->frame.s.section = sectp;
2396 this->frame.size = bfd_get_section_size (sectp);
2398 else if (section_is_p (sectp->name, &names.eh_frame))
2400 this->eh_frame.s.section = sectp;
2401 this->eh_frame.size = bfd_get_section_size (sectp);
2403 else if (section_is_p (sectp->name, &names.ranges))
2405 this->ranges.s.section = sectp;
2406 this->ranges.size = bfd_get_section_size (sectp);
2408 else if (section_is_p (sectp->name, &names.rnglists))
2410 this->rnglists.s.section = sectp;
2411 this->rnglists.size = bfd_get_section_size (sectp);
2413 else if (section_is_p (sectp->name, &names.types))
2415 struct dwarf2_section_info type_section;
2417 memset (&type_section, 0, sizeof (type_section));
2418 type_section.s.section = sectp;
2419 type_section.size = bfd_get_section_size (sectp);
2421 VEC_safe_push (dwarf2_section_info_def, this->types,
2424 else if (section_is_p (sectp->name, &names.gdb_index))
2426 this->gdb_index.s.section = sectp;
2427 this->gdb_index.size = bfd_get_section_size (sectp);
2429 else if (section_is_p (sectp->name, &names.debug_names))
2431 this->debug_names.s.section = sectp;
2432 this->debug_names.size = bfd_get_section_size (sectp);
2434 else if (section_is_p (sectp->name, &names.debug_aranges))
2436 this->debug_aranges.s.section = sectp;
2437 this->debug_aranges.size = bfd_get_section_size (sectp);
2440 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2441 && bfd_section_vma (abfd, sectp) == 0)
2442 this->has_section_at_zero = true;
2445 /* A helper function that decides whether a section is empty,
2449 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2451 if (section->is_virtual)
2452 return section->size == 0;
2453 return section->s.section == NULL || section->size == 0;
2456 /* See dwarf2read.h. */
2459 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2463 gdb_byte *buf, *retbuf;
2467 info->buffer = NULL;
2468 info->readin = true;
2470 if (dwarf2_section_empty_p (info))
2473 sectp = get_section_bfd_section (info);
2475 /* If this is a virtual section we need to read in the real one first. */
2476 if (info->is_virtual)
2478 struct dwarf2_section_info *containing_section =
2479 get_containing_section (info);
2481 gdb_assert (sectp != NULL);
2482 if ((sectp->flags & SEC_RELOC) != 0)
2484 error (_("Dwarf Error: DWP format V2 with relocations is not"
2485 " supported in section %s [in module %s]"),
2486 get_section_name (info), get_section_file_name (info));
2488 dwarf2_read_section (objfile, containing_section);
2489 /* Other code should have already caught virtual sections that don't
2491 gdb_assert (info->virtual_offset + info->size
2492 <= containing_section->size);
2493 /* If the real section is empty or there was a problem reading the
2494 section we shouldn't get here. */
2495 gdb_assert (containing_section->buffer != NULL);
2496 info->buffer = containing_section->buffer + info->virtual_offset;
2500 /* If the section has relocations, we must read it ourselves.
2501 Otherwise we attach it to the BFD. */
2502 if ((sectp->flags & SEC_RELOC) == 0)
2504 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2508 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2511 /* When debugging .o files, we may need to apply relocations; see
2512 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2513 We never compress sections in .o files, so we only need to
2514 try this when the section is not compressed. */
2515 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2518 info->buffer = retbuf;
2522 abfd = get_section_bfd_owner (info);
2523 gdb_assert (abfd != NULL);
2525 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2526 || bfd_bread (buf, info->size, abfd) != info->size)
2528 error (_("Dwarf Error: Can't read DWARF data"
2529 " in section %s [in module %s]"),
2530 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2534 /* A helper function that returns the size of a section in a safe way.
2535 If you are positive that the section has been read before using the
2536 size, then it is safe to refer to the dwarf2_section_info object's
2537 "size" field directly. In other cases, you must call this
2538 function, because for compressed sections the size field is not set
2539 correctly until the section has been read. */
2541 static bfd_size_type
2542 dwarf2_section_size (struct objfile *objfile,
2543 struct dwarf2_section_info *info)
2546 dwarf2_read_section (objfile, info);
2550 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2554 dwarf2_get_section_info (struct objfile *objfile,
2555 enum dwarf2_section_enum sect,
2556 asection **sectp, const gdb_byte **bufp,
2557 bfd_size_type *sizep)
2559 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2560 struct dwarf2_section_info *info;
2562 /* We may see an objfile without any DWARF, in which case we just
2573 case DWARF2_DEBUG_FRAME:
2574 info = &data->frame;
2576 case DWARF2_EH_FRAME:
2577 info = &data->eh_frame;
2580 gdb_assert_not_reached ("unexpected section");
2583 dwarf2_read_section (objfile, info);
2585 *sectp = get_section_bfd_section (info);
2586 *bufp = info->buffer;
2587 *sizep = info->size;
2590 /* A helper function to find the sections for a .dwz file. */
2593 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2595 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2597 /* Note that we only support the standard ELF names, because .dwz
2598 is ELF-only (at the time of writing). */
2599 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2601 dwz_file->abbrev.s.section = sectp;
2602 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2604 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2606 dwz_file->info.s.section = sectp;
2607 dwz_file->info.size = bfd_get_section_size (sectp);
2609 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2611 dwz_file->str.s.section = sectp;
2612 dwz_file->str.size = bfd_get_section_size (sectp);
2614 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2616 dwz_file->line.s.section = sectp;
2617 dwz_file->line.size = bfd_get_section_size (sectp);
2619 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2621 dwz_file->macro.s.section = sectp;
2622 dwz_file->macro.size = bfd_get_section_size (sectp);
2624 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2626 dwz_file->gdb_index.s.section = sectp;
2627 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2629 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2631 dwz_file->debug_names.s.section = sectp;
2632 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2636 /* See dwarf2read.h. */
2639 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2641 const char *filename;
2642 bfd_size_type buildid_len_arg;
2646 if (dwarf2_per_objfile->dwz_file != NULL)
2647 return dwarf2_per_objfile->dwz_file.get ();
2649 bfd_set_error (bfd_error_no_error);
2650 gdb::unique_xmalloc_ptr<char> data
2651 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2652 &buildid_len_arg, &buildid));
2655 if (bfd_get_error () == bfd_error_no_error)
2657 error (_("could not read '.gnu_debugaltlink' section: %s"),
2658 bfd_errmsg (bfd_get_error ()));
2661 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2663 buildid_len = (size_t) buildid_len_arg;
2665 filename = data.get ();
2667 std::string abs_storage;
2668 if (!IS_ABSOLUTE_PATH (filename))
2670 gdb::unique_xmalloc_ptr<char> abs
2671 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2673 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2674 filename = abs_storage.c_str ();
2677 /* First try the file name given in the section. If that doesn't
2678 work, try to use the build-id instead. */
2679 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2680 if (dwz_bfd != NULL)
2682 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2683 dwz_bfd.reset (nullptr);
2686 if (dwz_bfd == NULL)
2687 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2689 if (dwz_bfd == NULL)
2690 error (_("could not find '.gnu_debugaltlink' file for %s"),
2691 objfile_name (dwarf2_per_objfile->objfile));
2693 std::unique_ptr<struct dwz_file> result
2694 (new struct dwz_file (std::move (dwz_bfd)));
2696 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2699 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2700 result->dwz_bfd.get ());
2701 dwarf2_per_objfile->dwz_file = std::move (result);
2702 return dwarf2_per_objfile->dwz_file.get ();
2705 /* DWARF quick_symbols_functions support. */
2707 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2708 unique line tables, so we maintain a separate table of all .debug_line
2709 derived entries to support the sharing.
2710 All the quick functions need is the list of file names. We discard the
2711 line_header when we're done and don't need to record it here. */
2712 struct quick_file_names
2714 /* The data used to construct the hash key. */
2715 struct stmt_list_hash hash;
2717 /* The number of entries in file_names, real_names. */
2718 unsigned int num_file_names;
2720 /* The file names from the line table, after being run through
2722 const char **file_names;
2724 /* The file names from the line table after being run through
2725 gdb_realpath. These are computed lazily. */
2726 const char **real_names;
2729 /* When using the index (and thus not using psymtabs), each CU has an
2730 object of this type. This is used to hold information needed by
2731 the various "quick" methods. */
2732 struct dwarf2_per_cu_quick_data
2734 /* The file table. This can be NULL if there was no file table
2735 or it's currently not read in.
2736 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2737 struct quick_file_names *file_names;
2739 /* The corresponding symbol table. This is NULL if symbols for this
2740 CU have not yet been read. */
2741 struct compunit_symtab *compunit_symtab;
2743 /* A temporary mark bit used when iterating over all CUs in
2744 expand_symtabs_matching. */
2745 unsigned int mark : 1;
2747 /* True if we've tried to read the file table and found there isn't one.
2748 There will be no point in trying to read it again next time. */
2749 unsigned int no_file_data : 1;
2752 /* Utility hash function for a stmt_list_hash. */
2755 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2759 if (stmt_list_hash->dwo_unit != NULL)
2760 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2761 v += to_underlying (stmt_list_hash->line_sect_off);
2765 /* Utility equality function for a stmt_list_hash. */
2768 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2769 const struct stmt_list_hash *rhs)
2771 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2773 if (lhs->dwo_unit != NULL
2774 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2777 return lhs->line_sect_off == rhs->line_sect_off;
2780 /* Hash function for a quick_file_names. */
2783 hash_file_name_entry (const void *e)
2785 const struct quick_file_names *file_data
2786 = (const struct quick_file_names *) e;
2788 return hash_stmt_list_entry (&file_data->hash);
2791 /* Equality function for a quick_file_names. */
2794 eq_file_name_entry (const void *a, const void *b)
2796 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2797 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2799 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2802 /* Delete function for a quick_file_names. */
2805 delete_file_name_entry (void *e)
2807 struct quick_file_names *file_data = (struct quick_file_names *) e;
2810 for (i = 0; i < file_data->num_file_names; ++i)
2812 xfree ((void*) file_data->file_names[i]);
2813 if (file_data->real_names)
2814 xfree ((void*) file_data->real_names[i]);
2817 /* The space for the struct itself lives on objfile_obstack,
2818 so we don't free it here. */
2821 /* Create a quick_file_names hash table. */
2824 create_quick_file_names_table (unsigned int nr_initial_entries)
2826 return htab_create_alloc (nr_initial_entries,
2827 hash_file_name_entry, eq_file_name_entry,
2828 delete_file_name_entry, xcalloc, xfree);
2831 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2832 have to be created afterwards. You should call age_cached_comp_units after
2833 processing PER_CU->CU. dw2_setup must have been already called. */
2836 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2838 if (per_cu->is_debug_types)
2839 load_full_type_unit (per_cu);
2841 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2843 if (per_cu->cu == NULL)
2844 return; /* Dummy CU. */
2846 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2849 /* Read in the symbols for PER_CU. */
2852 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2854 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2856 /* Skip type_unit_groups, reading the type units they contain
2857 is handled elsewhere. */
2858 if (IS_TYPE_UNIT_GROUP (per_cu))
2861 /* The destructor of dwarf2_queue_guard frees any entries left on
2862 the queue. After this point we're guaranteed to leave this function
2863 with the dwarf queue empty. */
2864 dwarf2_queue_guard q_guard;
2866 if (dwarf2_per_objfile->using_index
2867 ? per_cu->v.quick->compunit_symtab == NULL
2868 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2870 queue_comp_unit (per_cu, language_minimal);
2871 load_cu (per_cu, skip_partial);
2873 /* If we just loaded a CU from a DWO, and we're working with an index
2874 that may badly handle TUs, load all the TUs in that DWO as well.
2875 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2876 if (!per_cu->is_debug_types
2877 && per_cu->cu != NULL
2878 && per_cu->cu->dwo_unit != NULL
2879 && dwarf2_per_objfile->index_table != NULL
2880 && dwarf2_per_objfile->index_table->version <= 7
2881 /* DWP files aren't supported yet. */
2882 && get_dwp_file (dwarf2_per_objfile) == NULL)
2883 queue_and_load_all_dwo_tus (per_cu);
2886 process_queue (dwarf2_per_objfile);
2888 /* Age the cache, releasing compilation units that have not
2889 been used recently. */
2890 age_cached_comp_units (dwarf2_per_objfile);
2893 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2894 the objfile from which this CU came. Returns the resulting symbol
2897 static struct compunit_symtab *
2898 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2900 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2902 gdb_assert (dwarf2_per_objfile->using_index);
2903 if (!per_cu->v.quick->compunit_symtab)
2905 free_cached_comp_units freer (dwarf2_per_objfile);
2906 scoped_restore decrementer = increment_reading_symtab ();
2907 dw2_do_instantiate_symtab (per_cu, skip_partial);
2908 process_cu_includes (dwarf2_per_objfile);
2911 return per_cu->v.quick->compunit_symtab;
2914 /* See declaration. */
2916 dwarf2_per_cu_data *
2917 dwarf2_per_objfile::get_cutu (int index)
2919 if (index >= this->all_comp_units.size ())
2921 index -= this->all_comp_units.size ();
2922 gdb_assert (index < this->all_type_units.size ());
2923 return &this->all_type_units[index]->per_cu;
2926 return this->all_comp_units[index];
2929 /* See declaration. */
2931 dwarf2_per_cu_data *
2932 dwarf2_per_objfile::get_cu (int index)
2934 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2936 return this->all_comp_units[index];
2939 /* See declaration. */
2942 dwarf2_per_objfile::get_tu (int index)
2944 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2946 return this->all_type_units[index];
2949 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2950 objfile_obstack, and constructed with the specified field
2953 static dwarf2_per_cu_data *
2954 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2955 struct dwarf2_section_info *section,
2957 sect_offset sect_off, ULONGEST length)
2959 struct objfile *objfile = dwarf2_per_objfile->objfile;
2960 dwarf2_per_cu_data *the_cu
2961 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2962 struct dwarf2_per_cu_data);
2963 the_cu->sect_off = sect_off;
2964 the_cu->length = length;
2965 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2966 the_cu->section = section;
2967 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2968 struct dwarf2_per_cu_quick_data);
2969 the_cu->is_dwz = is_dwz;
2973 /* A helper for create_cus_from_index that handles a given list of
2977 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2978 const gdb_byte *cu_list, offset_type n_elements,
2979 struct dwarf2_section_info *section,
2982 for (offset_type i = 0; i < n_elements; i += 2)
2984 gdb_static_assert (sizeof (ULONGEST) >= 8);
2986 sect_offset sect_off
2987 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2988 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2991 dwarf2_per_cu_data *per_cu
2992 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2994 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2998 /* Read the CU list from the mapped index, and use it to create all
2999 the CU objects for this objfile. */
3002 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3003 const gdb_byte *cu_list, offset_type cu_list_elements,
3004 const gdb_byte *dwz_list, offset_type dwz_elements)
3006 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3007 dwarf2_per_objfile->all_comp_units.reserve
3008 ((cu_list_elements + dwz_elements) / 2);
3010 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3011 &dwarf2_per_objfile->info, 0);
3013 if (dwz_elements == 0)
3016 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3017 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3021 /* Create the signatured type hash table from the index. */
3024 create_signatured_type_table_from_index
3025 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3026 struct dwarf2_section_info *section,
3027 const gdb_byte *bytes,
3028 offset_type elements)
3030 struct objfile *objfile = dwarf2_per_objfile->objfile;
3032 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3033 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3035 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3037 for (offset_type i = 0; i < elements; i += 3)
3039 struct signatured_type *sig_type;
3042 cu_offset type_offset_in_tu;
3044 gdb_static_assert (sizeof (ULONGEST) >= 8);
3045 sect_offset sect_off
3046 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3048 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3050 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3053 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3054 struct signatured_type);
3055 sig_type->signature = signature;
3056 sig_type->type_offset_in_tu = type_offset_in_tu;
3057 sig_type->per_cu.is_debug_types = 1;
3058 sig_type->per_cu.section = section;
3059 sig_type->per_cu.sect_off = sect_off;
3060 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3061 sig_type->per_cu.v.quick
3062 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3063 struct dwarf2_per_cu_quick_data);
3065 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3068 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3071 dwarf2_per_objfile->signatured_types = sig_types_hash;
3074 /* Create the signatured type hash table from .debug_names. */
3077 create_signatured_type_table_from_debug_names
3078 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3079 const mapped_debug_names &map,
3080 struct dwarf2_section_info *section,
3081 struct dwarf2_section_info *abbrev_section)
3083 struct objfile *objfile = dwarf2_per_objfile->objfile;
3085 dwarf2_read_section (objfile, section);
3086 dwarf2_read_section (objfile, abbrev_section);
3088 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3089 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3091 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3093 for (uint32_t i = 0; i < map.tu_count; ++i)
3095 struct signatured_type *sig_type;
3098 sect_offset sect_off
3099 = (sect_offset) (extract_unsigned_integer
3100 (map.tu_table_reordered + i * map.offset_size,
3102 map.dwarf5_byte_order));
3104 comp_unit_head cu_header;
3105 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3107 section->buffer + to_underlying (sect_off),
3110 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3111 struct signatured_type);
3112 sig_type->signature = cu_header.signature;
3113 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3114 sig_type->per_cu.is_debug_types = 1;
3115 sig_type->per_cu.section = section;
3116 sig_type->per_cu.sect_off = sect_off;
3117 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3118 sig_type->per_cu.v.quick
3119 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3120 struct dwarf2_per_cu_quick_data);
3122 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3125 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3128 dwarf2_per_objfile->signatured_types = sig_types_hash;
3131 /* Read the address map data from the mapped index, and use it to
3132 populate the objfile's psymtabs_addrmap. */
3135 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3136 struct mapped_index *index)
3138 struct objfile *objfile = dwarf2_per_objfile->objfile;
3139 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3140 const gdb_byte *iter, *end;
3141 struct addrmap *mutable_map;
3144 auto_obstack temp_obstack;
3146 mutable_map = addrmap_create_mutable (&temp_obstack);
3148 iter = index->address_table.data ();
3149 end = iter + index->address_table.size ();
3151 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3155 ULONGEST hi, lo, cu_index;
3156 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3158 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3160 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3165 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3166 hex_string (lo), hex_string (hi));
3170 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3172 complaint (_(".gdb_index address table has invalid CU number %u"),
3173 (unsigned) cu_index);
3177 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3178 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3179 addrmap_set_empty (mutable_map, lo, hi - 1,
3180 dwarf2_per_objfile->get_cu (cu_index));
3183 objfile->partial_symtabs->psymtabs_addrmap
3184 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3187 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3188 populate the objfile's psymtabs_addrmap. */
3191 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3192 struct dwarf2_section_info *section)
3194 struct objfile *objfile = dwarf2_per_objfile->objfile;
3195 bfd *abfd = objfile->obfd;
3196 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3197 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3198 SECT_OFF_TEXT (objfile));
3200 auto_obstack temp_obstack;
3201 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3203 std::unordered_map<sect_offset,
3204 dwarf2_per_cu_data *,
3205 gdb::hash_enum<sect_offset>>
3206 debug_info_offset_to_per_cu;
3207 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3209 const auto insertpair
3210 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3211 if (!insertpair.second)
3213 warning (_("Section .debug_aranges in %s has duplicate "
3214 "debug_info_offset %s, ignoring .debug_aranges."),
3215 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3220 dwarf2_read_section (objfile, section);
3222 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3224 const gdb_byte *addr = section->buffer;
3226 while (addr < section->buffer + section->size)
3228 const gdb_byte *const entry_addr = addr;
3229 unsigned int bytes_read;
3231 const LONGEST entry_length = read_initial_length (abfd, addr,
3235 const gdb_byte *const entry_end = addr + entry_length;
3236 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3237 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3238 if (addr + entry_length > section->buffer + section->size)
3240 warning (_("Section .debug_aranges in %s entry at offset %zu "
3241 "length %s exceeds section length %s, "
3242 "ignoring .debug_aranges."),
3243 objfile_name (objfile), entry_addr - section->buffer,
3244 plongest (bytes_read + entry_length),
3245 pulongest (section->size));
3249 /* The version number. */
3250 const uint16_t version = read_2_bytes (abfd, addr);
3254 warning (_("Section .debug_aranges in %s entry at offset %zu "
3255 "has unsupported version %d, ignoring .debug_aranges."),
3256 objfile_name (objfile), entry_addr - section->buffer,
3261 const uint64_t debug_info_offset
3262 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3263 addr += offset_size;
3264 const auto per_cu_it
3265 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3266 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3268 warning (_("Section .debug_aranges in %s entry at offset %zu "
3269 "debug_info_offset %s does not exists, "
3270 "ignoring .debug_aranges."),
3271 objfile_name (objfile), entry_addr - section->buffer,
3272 pulongest (debug_info_offset));
3275 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3277 const uint8_t address_size = *addr++;
3278 if (address_size < 1 || address_size > 8)
3280 warning (_("Section .debug_aranges in %s entry at offset %zu "
3281 "address_size %u is invalid, ignoring .debug_aranges."),
3282 objfile_name (objfile), entry_addr - section->buffer,
3287 const uint8_t segment_selector_size = *addr++;
3288 if (segment_selector_size != 0)
3290 warning (_("Section .debug_aranges in %s entry at offset %zu "
3291 "segment_selector_size %u is not supported, "
3292 "ignoring .debug_aranges."),
3293 objfile_name (objfile), entry_addr - section->buffer,
3294 segment_selector_size);
3298 /* Must pad to an alignment boundary that is twice the address
3299 size. It is undocumented by the DWARF standard but GCC does
3301 for (size_t padding = ((-(addr - section->buffer))
3302 & (2 * address_size - 1));
3303 padding > 0; padding--)
3306 warning (_("Section .debug_aranges in %s entry at offset %zu "
3307 "padding is not zero, ignoring .debug_aranges."),
3308 objfile_name (objfile), entry_addr - section->buffer);
3314 if (addr + 2 * address_size > entry_end)
3316 warning (_("Section .debug_aranges in %s entry at offset %zu "
3317 "address list is not properly terminated, "
3318 "ignoring .debug_aranges."),
3319 objfile_name (objfile), entry_addr - section->buffer);
3322 ULONGEST start = extract_unsigned_integer (addr, address_size,
3324 addr += address_size;
3325 ULONGEST length = extract_unsigned_integer (addr, address_size,
3327 addr += address_size;
3328 if (start == 0 && length == 0)
3330 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3332 /* Symbol was eliminated due to a COMDAT group. */
3335 ULONGEST end = start + length;
3336 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3338 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3340 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3344 objfile->partial_symtabs->psymtabs_addrmap
3345 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3348 /* Find a slot in the mapped index INDEX for the object named NAME.
3349 If NAME is found, set *VEC_OUT to point to the CU vector in the
3350 constant pool and return true. If NAME cannot be found, return
3354 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3355 offset_type **vec_out)
3358 offset_type slot, step;
3359 int (*cmp) (const char *, const char *);
3361 gdb::unique_xmalloc_ptr<char> without_params;
3362 if (current_language->la_language == language_cplus
3363 || current_language->la_language == language_fortran
3364 || current_language->la_language == language_d)
3366 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3369 if (strchr (name, '(') != NULL)
3371 without_params = cp_remove_params (name);
3373 if (without_params != NULL)
3374 name = without_params.get ();
3378 /* Index version 4 did not support case insensitive searches. But the
3379 indices for case insensitive languages are built in lowercase, therefore
3380 simulate our NAME being searched is also lowercased. */
3381 hash = mapped_index_string_hash ((index->version == 4
3382 && case_sensitivity == case_sensitive_off
3383 ? 5 : index->version),
3386 slot = hash & (index->symbol_table.size () - 1);
3387 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3388 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3394 const auto &bucket = index->symbol_table[slot];
3395 if (bucket.name == 0 && bucket.vec == 0)
3398 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3399 if (!cmp (name, str))
3401 *vec_out = (offset_type *) (index->constant_pool
3402 + MAYBE_SWAP (bucket.vec));
3406 slot = (slot + step) & (index->symbol_table.size () - 1);
3410 /* A helper function that reads the .gdb_index from BUFFER and fills
3411 in MAP. FILENAME is the name of the file containing the data;
3412 it is used for error reporting. DEPRECATED_OK is true if it is
3413 ok to use deprecated sections.
3415 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3416 out parameters that are filled in with information about the CU and
3417 TU lists in the section.
3419 Returns true if all went well, false otherwise. */
3422 read_gdb_index_from_buffer (struct objfile *objfile,
3423 const char *filename,
3425 gdb::array_view<const gdb_byte> buffer,
3426 struct mapped_index *map,
3427 const gdb_byte **cu_list,
3428 offset_type *cu_list_elements,
3429 const gdb_byte **types_list,
3430 offset_type *types_list_elements)
3432 const gdb_byte *addr = &buffer[0];
3434 /* Version check. */
3435 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3436 /* Versions earlier than 3 emitted every copy of a psymbol. This
3437 causes the index to behave very poorly for certain requests. Version 3
3438 contained incomplete addrmap. So, it seems better to just ignore such
3442 static int warning_printed = 0;
3443 if (!warning_printed)
3445 warning (_("Skipping obsolete .gdb_index section in %s."),
3447 warning_printed = 1;
3451 /* Index version 4 uses a different hash function than index version
3454 Versions earlier than 6 did not emit psymbols for inlined
3455 functions. Using these files will cause GDB not to be able to
3456 set breakpoints on inlined functions by name, so we ignore these
3457 indices unless the user has done
3458 "set use-deprecated-index-sections on". */
3459 if (version < 6 && !deprecated_ok)
3461 static int warning_printed = 0;
3462 if (!warning_printed)
3465 Skipping deprecated .gdb_index section in %s.\n\
3466 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3467 to use the section anyway."),
3469 warning_printed = 1;
3473 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3474 of the TU (for symbols coming from TUs),
3475 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3476 Plus gold-generated indices can have duplicate entries for global symbols,
3477 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3478 These are just performance bugs, and we can't distinguish gdb-generated
3479 indices from gold-generated ones, so issue no warning here. */
3481 /* Indexes with higher version than the one supported by GDB may be no
3482 longer backward compatible. */
3486 map->version = version;
3488 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3491 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3492 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3496 *types_list = addr + MAYBE_SWAP (metadata[i]);
3497 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3498 - MAYBE_SWAP (metadata[i]))
3502 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3503 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3505 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3508 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3509 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3511 = gdb::array_view<mapped_index::symbol_table_slot>
3512 ((mapped_index::symbol_table_slot *) symbol_table,
3513 (mapped_index::symbol_table_slot *) symbol_table_end);
3516 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3521 /* Callback types for dwarf2_read_gdb_index. */
3523 typedef gdb::function_view
3524 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3525 get_gdb_index_contents_ftype;
3526 typedef gdb::function_view
3527 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3528 get_gdb_index_contents_dwz_ftype;
3530 /* Read .gdb_index. If everything went ok, initialize the "quick"
3531 elements of all the CUs and return 1. Otherwise, return 0. */
3534 dwarf2_read_gdb_index
3535 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3536 get_gdb_index_contents_ftype get_gdb_index_contents,
3537 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3539 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3540 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3541 struct dwz_file *dwz;
3542 struct objfile *objfile = dwarf2_per_objfile->objfile;
3544 gdb::array_view<const gdb_byte> main_index_contents
3545 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3547 if (main_index_contents.empty ())
3550 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3551 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3552 use_deprecated_index_sections,
3553 main_index_contents, map.get (), &cu_list,
3554 &cu_list_elements, &types_list,
3555 &types_list_elements))
3558 /* Don't use the index if it's empty. */
3559 if (map->symbol_table.empty ())
3562 /* If there is a .dwz file, read it so we can get its CU list as
3564 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3567 struct mapped_index dwz_map;
3568 const gdb_byte *dwz_types_ignore;
3569 offset_type dwz_types_elements_ignore;
3571 gdb::array_view<const gdb_byte> dwz_index_content
3572 = get_gdb_index_contents_dwz (objfile, dwz);
3574 if (dwz_index_content.empty ())
3577 if (!read_gdb_index_from_buffer (objfile,
3578 bfd_get_filename (dwz->dwz_bfd), 1,
3579 dwz_index_content, &dwz_map,
3580 &dwz_list, &dwz_list_elements,
3582 &dwz_types_elements_ignore))
3584 warning (_("could not read '.gdb_index' section from %s; skipping"),
3585 bfd_get_filename (dwz->dwz_bfd));
3590 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3591 dwz_list, dwz_list_elements);
3593 if (types_list_elements)
3595 struct dwarf2_section_info *section;
3597 /* We can only handle a single .debug_types when we have an
3599 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3602 section = VEC_index (dwarf2_section_info_def,
3603 dwarf2_per_objfile->types, 0);
3605 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3606 types_list, types_list_elements);
3609 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3611 dwarf2_per_objfile->index_table = std::move (map);
3612 dwarf2_per_objfile->using_index = 1;
3613 dwarf2_per_objfile->quick_file_names_table =
3614 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3619 /* die_reader_func for dw2_get_file_names. */
3622 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3623 const gdb_byte *info_ptr,
3624 struct die_info *comp_unit_die,
3628 struct dwarf2_cu *cu = reader->cu;
3629 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3630 struct dwarf2_per_objfile *dwarf2_per_objfile
3631 = cu->per_cu->dwarf2_per_objfile;
3632 struct objfile *objfile = dwarf2_per_objfile->objfile;
3633 struct dwarf2_per_cu_data *lh_cu;
3634 struct attribute *attr;
3637 struct quick_file_names *qfn;
3639 gdb_assert (! this_cu->is_debug_types);
3641 /* Our callers never want to match partial units -- instead they
3642 will match the enclosing full CU. */
3643 if (comp_unit_die->tag == DW_TAG_partial_unit)
3645 this_cu->v.quick->no_file_data = 1;
3653 sect_offset line_offset {};
3655 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3658 struct quick_file_names find_entry;
3660 line_offset = (sect_offset) DW_UNSND (attr);
3662 /* We may have already read in this line header (TU line header sharing).
3663 If we have we're done. */
3664 find_entry.hash.dwo_unit = cu->dwo_unit;
3665 find_entry.hash.line_sect_off = line_offset;
3666 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3667 &find_entry, INSERT);
3670 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3674 lh = dwarf_decode_line_header (line_offset, cu);
3678 lh_cu->v.quick->no_file_data = 1;
3682 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3683 qfn->hash.dwo_unit = cu->dwo_unit;
3684 qfn->hash.line_sect_off = line_offset;
3685 gdb_assert (slot != NULL);
3688 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3690 qfn->num_file_names = lh->file_names.size ();
3692 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3693 for (i = 0; i < lh->file_names.size (); ++i)
3694 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3695 qfn->real_names = NULL;
3697 lh_cu->v.quick->file_names = qfn;
3700 /* A helper for the "quick" functions which attempts to read the line
3701 table for THIS_CU. */
3703 static struct quick_file_names *
3704 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3706 /* This should never be called for TUs. */
3707 gdb_assert (! this_cu->is_debug_types);
3708 /* Nor type unit groups. */
3709 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3711 if (this_cu->v.quick->file_names != NULL)
3712 return this_cu->v.quick->file_names;
3713 /* If we know there is no line data, no point in looking again. */
3714 if (this_cu->v.quick->no_file_data)
3717 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3719 if (this_cu->v.quick->no_file_data)
3721 return this_cu->v.quick->file_names;
3724 /* A helper for the "quick" functions which computes and caches the
3725 real path for a given file name from the line table. */
3728 dw2_get_real_path (struct objfile *objfile,
3729 struct quick_file_names *qfn, int index)
3731 if (qfn->real_names == NULL)
3732 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3733 qfn->num_file_names, const char *);
3735 if (qfn->real_names[index] == NULL)
3736 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3738 return qfn->real_names[index];
3741 static struct symtab *
3742 dw2_find_last_source_symtab (struct objfile *objfile)
3744 struct dwarf2_per_objfile *dwarf2_per_objfile
3745 = get_dwarf2_per_objfile (objfile);
3746 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3747 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3752 return compunit_primary_filetab (cust);
3755 /* Traversal function for dw2_forget_cached_source_info. */
3758 dw2_free_cached_file_names (void **slot, void *info)
3760 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3762 if (file_data->real_names)
3766 for (i = 0; i < file_data->num_file_names; ++i)
3768 xfree ((void*) file_data->real_names[i]);
3769 file_data->real_names[i] = NULL;
3777 dw2_forget_cached_source_info (struct objfile *objfile)
3779 struct dwarf2_per_objfile *dwarf2_per_objfile
3780 = get_dwarf2_per_objfile (objfile);
3782 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3783 dw2_free_cached_file_names, NULL);
3786 /* Helper function for dw2_map_symtabs_matching_filename that expands
3787 the symtabs and calls the iterator. */
3790 dw2_map_expand_apply (struct objfile *objfile,
3791 struct dwarf2_per_cu_data *per_cu,
3792 const char *name, const char *real_path,
3793 gdb::function_view<bool (symtab *)> callback)
3795 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3797 /* Don't visit already-expanded CUs. */
3798 if (per_cu->v.quick->compunit_symtab)
3801 /* This may expand more than one symtab, and we want to iterate over
3803 dw2_instantiate_symtab (per_cu, false);
3805 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3806 last_made, callback);
3809 /* Implementation of the map_symtabs_matching_filename method. */
3812 dw2_map_symtabs_matching_filename
3813 (struct objfile *objfile, const char *name, const char *real_path,
3814 gdb::function_view<bool (symtab *)> callback)
3816 const char *name_basename = lbasename (name);
3817 struct dwarf2_per_objfile *dwarf2_per_objfile
3818 = get_dwarf2_per_objfile (objfile);
3820 /* The rule is CUs specify all the files, including those used by
3821 any TU, so there's no need to scan TUs here. */
3823 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3825 /* We only need to look at symtabs not already expanded. */
3826 if (per_cu->v.quick->compunit_symtab)
3829 quick_file_names *file_data = dw2_get_file_names (per_cu);
3830 if (file_data == NULL)
3833 for (int j = 0; j < file_data->num_file_names; ++j)
3835 const char *this_name = file_data->file_names[j];
3836 const char *this_real_name;
3838 if (compare_filenames_for_search (this_name, name))
3840 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3846 /* Before we invoke realpath, which can get expensive when many
3847 files are involved, do a quick comparison of the basenames. */
3848 if (! basenames_may_differ
3849 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3852 this_real_name = dw2_get_real_path (objfile, file_data, j);
3853 if (compare_filenames_for_search (this_real_name, name))
3855 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3861 if (real_path != NULL)
3863 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3864 gdb_assert (IS_ABSOLUTE_PATH (name));
3865 if (this_real_name != NULL
3866 && FILENAME_CMP (real_path, this_real_name) == 0)
3868 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3880 /* Struct used to manage iterating over all CUs looking for a symbol. */
3882 struct dw2_symtab_iterator
3884 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3885 struct dwarf2_per_objfile *dwarf2_per_objfile;
3886 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3887 int want_specific_block;
3888 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3889 Unused if !WANT_SPECIFIC_BLOCK. */
3891 /* The kind of symbol we're looking for. */
3893 /* The list of CUs from the index entry of the symbol,
3894 or NULL if not found. */
3896 /* The next element in VEC to look at. */
3898 /* The number of elements in VEC, or zero if there is no match. */
3900 /* Have we seen a global version of the symbol?
3901 If so we can ignore all further global instances.
3902 This is to work around gold/15646, inefficient gold-generated
3907 /* Initialize the index symtab iterator ITER.
3908 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3909 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3912 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3913 struct dwarf2_per_objfile *dwarf2_per_objfile,
3914 int want_specific_block,
3919 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3920 iter->want_specific_block = want_specific_block;
3921 iter->block_index = block_index;
3922 iter->domain = domain;
3924 iter->global_seen = 0;
3926 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3928 /* index is NULL if OBJF_READNOW. */
3929 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3930 iter->length = MAYBE_SWAP (*iter->vec);
3938 /* Return the next matching CU or NULL if there are no more. */
3940 static struct dwarf2_per_cu_data *
3941 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3943 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3945 for ( ; iter->next < iter->length; ++iter->next)
3947 offset_type cu_index_and_attrs =
3948 MAYBE_SWAP (iter->vec[iter->next + 1]);
3949 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3950 int want_static = iter->block_index != GLOBAL_BLOCK;
3951 /* This value is only valid for index versions >= 7. */
3952 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3953 gdb_index_symbol_kind symbol_kind =
3954 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3955 /* Only check the symbol attributes if they're present.
3956 Indices prior to version 7 don't record them,
3957 and indices >= 7 may elide them for certain symbols
3958 (gold does this). */
3960 (dwarf2_per_objfile->index_table->version >= 7
3961 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3963 /* Don't crash on bad data. */
3964 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3965 + dwarf2_per_objfile->all_type_units.size ()))
3967 complaint (_(".gdb_index entry has bad CU index"
3969 objfile_name (dwarf2_per_objfile->objfile));
3973 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3975 /* Skip if already read in. */
3976 if (per_cu->v.quick->compunit_symtab)
3979 /* Check static vs global. */
3982 if (iter->want_specific_block
3983 && want_static != is_static)
3985 /* Work around gold/15646. */
3986 if (!is_static && iter->global_seen)
3989 iter->global_seen = 1;
3992 /* Only check the symbol's kind if it has one. */
3995 switch (iter->domain)
3998 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3999 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4000 /* Some types are also in VAR_DOMAIN. */
4001 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4005 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4009 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4024 static struct compunit_symtab *
4025 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4026 const char *name, domain_enum domain)
4028 struct compunit_symtab *stab_best = NULL;
4029 struct dwarf2_per_objfile *dwarf2_per_objfile
4030 = get_dwarf2_per_objfile (objfile);
4032 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4034 struct dw2_symtab_iterator iter;
4035 struct dwarf2_per_cu_data *per_cu;
4037 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4039 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4041 struct symbol *sym, *with_opaque = NULL;
4042 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4043 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4044 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4046 sym = block_find_symbol (block, name, domain,
4047 block_find_non_opaque_type_preferred,
4050 /* Some caution must be observed with overloaded functions
4051 and methods, since the index will not contain any overload
4052 information (but NAME might contain it). */
4055 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4057 if (with_opaque != NULL
4058 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4061 /* Keep looking through other CUs. */
4068 dw2_print_stats (struct objfile *objfile)
4070 struct dwarf2_per_objfile *dwarf2_per_objfile
4071 = get_dwarf2_per_objfile (objfile);
4072 int total = (dwarf2_per_objfile->all_comp_units.size ()
4073 + dwarf2_per_objfile->all_type_units.size ());
4076 for (int i = 0; i < total; ++i)
4078 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4080 if (!per_cu->v.quick->compunit_symtab)
4083 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4084 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4087 /* This dumps minimal information about the index.
4088 It is called via "mt print objfiles".
4089 One use is to verify .gdb_index has been loaded by the
4090 gdb.dwarf2/gdb-index.exp testcase. */
4093 dw2_dump (struct objfile *objfile)
4095 struct dwarf2_per_objfile *dwarf2_per_objfile
4096 = get_dwarf2_per_objfile (objfile);
4098 gdb_assert (dwarf2_per_objfile->using_index);
4099 printf_filtered (".gdb_index:");
4100 if (dwarf2_per_objfile->index_table != NULL)
4102 printf_filtered (" version %d\n",
4103 dwarf2_per_objfile->index_table->version);
4106 printf_filtered (" faked for \"readnow\"\n");
4107 printf_filtered ("\n");
4111 dw2_expand_symtabs_for_function (struct objfile *objfile,
4112 const char *func_name)
4114 struct dwarf2_per_objfile *dwarf2_per_objfile
4115 = get_dwarf2_per_objfile (objfile);
4117 struct dw2_symtab_iterator iter;
4118 struct dwarf2_per_cu_data *per_cu;
4120 /* Note: It doesn't matter what we pass for block_index here. */
4121 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4124 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4125 dw2_instantiate_symtab (per_cu, false);
4130 dw2_expand_all_symtabs (struct objfile *objfile)
4132 struct dwarf2_per_objfile *dwarf2_per_objfile
4133 = get_dwarf2_per_objfile (objfile);
4134 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4135 + dwarf2_per_objfile->all_type_units.size ());
4137 for (int i = 0; i < total_units; ++i)
4139 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4141 /* We don't want to directly expand a partial CU, because if we
4142 read it with the wrong language, then assertion failures can
4143 be triggered later on. See PR symtab/23010. So, tell
4144 dw2_instantiate_symtab to skip partial CUs -- any important
4145 partial CU will be read via DW_TAG_imported_unit anyway. */
4146 dw2_instantiate_symtab (per_cu, true);
4151 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4152 const char *fullname)
4154 struct dwarf2_per_objfile *dwarf2_per_objfile
4155 = get_dwarf2_per_objfile (objfile);
4157 /* We don't need to consider type units here.
4158 This is only called for examining code, e.g. expand_line_sal.
4159 There can be an order of magnitude (or more) more type units
4160 than comp units, and we avoid them if we can. */
4162 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4164 /* We only need to look at symtabs not already expanded. */
4165 if (per_cu->v.quick->compunit_symtab)
4168 quick_file_names *file_data = dw2_get_file_names (per_cu);
4169 if (file_data == NULL)
4172 for (int j = 0; j < file_data->num_file_names; ++j)
4174 const char *this_fullname = file_data->file_names[j];
4176 if (filename_cmp (this_fullname, fullname) == 0)
4178 dw2_instantiate_symtab (per_cu, false);
4186 dw2_map_matching_symbols (struct objfile *objfile,
4187 const char * name, domain_enum domain,
4189 int (*callback) (const struct block *,
4190 struct symbol *, void *),
4191 void *data, symbol_name_match_type match,
4192 symbol_compare_ftype *ordered_compare)
4194 /* Currently unimplemented; used for Ada. The function can be called if the
4195 current language is Ada for a non-Ada objfile using GNU index. As Ada
4196 does not look for non-Ada symbols this function should just return. */
4199 /* Symbol name matcher for .gdb_index names.
4201 Symbol names in .gdb_index have a few particularities:
4203 - There's no indication of which is the language of each symbol.
4205 Since each language has its own symbol name matching algorithm,
4206 and we don't know which language is the right one, we must match
4207 each symbol against all languages. This would be a potential
4208 performance problem if it were not mitigated by the
4209 mapped_index::name_components lookup table, which significantly
4210 reduces the number of times we need to call into this matcher,
4211 making it a non-issue.
4213 - Symbol names in the index have no overload (parameter)
4214 information. I.e., in C++, "foo(int)" and "foo(long)" both
4215 appear as "foo" in the index, for example.
4217 This means that the lookup names passed to the symbol name
4218 matcher functions must have no parameter information either
4219 because (e.g.) symbol search name "foo" does not match
4220 lookup-name "foo(int)" [while swapping search name for lookup
4223 class gdb_index_symbol_name_matcher
4226 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4227 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4229 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4230 Returns true if any matcher matches. */
4231 bool matches (const char *symbol_name);
4234 /* A reference to the lookup name we're matching against. */
4235 const lookup_name_info &m_lookup_name;
4237 /* A vector holding all the different symbol name matchers, for all
4239 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4242 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4243 (const lookup_name_info &lookup_name)
4244 : m_lookup_name (lookup_name)
4246 /* Prepare the vector of comparison functions upfront, to avoid
4247 doing the same work for each symbol. Care is taken to avoid
4248 matching with the same matcher more than once if/when multiple
4249 languages use the same matcher function. */
4250 auto &matchers = m_symbol_name_matcher_funcs;
4251 matchers.reserve (nr_languages);
4253 matchers.push_back (default_symbol_name_matcher);
4255 for (int i = 0; i < nr_languages; i++)
4257 const language_defn *lang = language_def ((enum language) i);
4258 symbol_name_matcher_ftype *name_matcher
4259 = get_symbol_name_matcher (lang, m_lookup_name);
4261 /* Don't insert the same comparison routine more than once.
4262 Note that we do this linear walk instead of a seemingly
4263 cheaper sorted insert, or use a std::set or something like
4264 that, because relative order of function addresses is not
4265 stable. This is not a problem in practice because the number
4266 of supported languages is low, and the cost here is tiny
4267 compared to the number of searches we'll do afterwards using
4269 if (name_matcher != default_symbol_name_matcher
4270 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4271 == matchers.end ()))
4272 matchers.push_back (name_matcher);
4277 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4279 for (auto matches_name : m_symbol_name_matcher_funcs)
4280 if (matches_name (symbol_name, m_lookup_name, NULL))
4286 /* Starting from a search name, return the string that finds the upper
4287 bound of all strings that start with SEARCH_NAME in a sorted name
4288 list. Returns the empty string to indicate that the upper bound is
4289 the end of the list. */
4292 make_sort_after_prefix_name (const char *search_name)
4294 /* When looking to complete "func", we find the upper bound of all
4295 symbols that start with "func" by looking for where we'd insert
4296 the closest string that would follow "func" in lexicographical
4297 order. Usually, that's "func"-with-last-character-incremented,
4298 i.e. "fund". Mind non-ASCII characters, though. Usually those
4299 will be UTF-8 multi-byte sequences, but we can't be certain.
4300 Especially mind the 0xff character, which is a valid character in
4301 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4302 rule out compilers allowing it in identifiers. Note that
4303 conveniently, strcmp/strcasecmp are specified to compare
4304 characters interpreted as unsigned char. So what we do is treat
4305 the whole string as a base 256 number composed of a sequence of
4306 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4307 to 0, and carries 1 to the following more-significant position.
4308 If the very first character in SEARCH_NAME ends up incremented
4309 and carries/overflows, then the upper bound is the end of the
4310 list. The string after the empty string is also the empty
4313 Some examples of this operation:
4315 SEARCH_NAME => "+1" RESULT
4319 "\xff" "a" "\xff" => "\xff" "b"
4324 Then, with these symbols for example:
4330 completing "func" looks for symbols between "func" and
4331 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4332 which finds "func" and "func1", but not "fund".
4336 funcÿ (Latin1 'ÿ' [0xff])
4340 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4341 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4345 ÿÿ (Latin1 'ÿ' [0xff])
4348 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4349 the end of the list.
4351 std::string after = search_name;
4352 while (!after.empty () && (unsigned char) after.back () == 0xff)
4354 if (!after.empty ())
4355 after.back () = (unsigned char) after.back () + 1;
4359 /* See declaration. */
4361 std::pair<std::vector<name_component>::const_iterator,
4362 std::vector<name_component>::const_iterator>
4363 mapped_index_base::find_name_components_bounds
4364 (const lookup_name_info &lookup_name_without_params) const
4367 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4370 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4372 /* Comparison function object for lower_bound that matches against a
4373 given symbol name. */
4374 auto lookup_compare_lower = [&] (const name_component &elem,
4377 const char *elem_qualified = this->symbol_name_at (elem.idx);
4378 const char *elem_name = elem_qualified + elem.name_offset;
4379 return name_cmp (elem_name, name) < 0;
4382 /* Comparison function object for upper_bound that matches against a
4383 given symbol name. */
4384 auto lookup_compare_upper = [&] (const char *name,
4385 const name_component &elem)
4387 const char *elem_qualified = this->symbol_name_at (elem.idx);
4388 const char *elem_name = elem_qualified + elem.name_offset;
4389 return name_cmp (name, elem_name) < 0;
4392 auto begin = this->name_components.begin ();
4393 auto end = this->name_components.end ();
4395 /* Find the lower bound. */
4398 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4401 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4404 /* Find the upper bound. */
4407 if (lookup_name_without_params.completion_mode ())
4409 /* In completion mode, we want UPPER to point past all
4410 symbols names that have the same prefix. I.e., with
4411 these symbols, and completing "func":
4413 function << lower bound
4415 other_function << upper bound
4417 We find the upper bound by looking for the insertion
4418 point of "func"-with-last-character-incremented,
4420 std::string after = make_sort_after_prefix_name (cplus);
4423 return std::lower_bound (lower, end, after.c_str (),
4424 lookup_compare_lower);
4427 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4430 return {lower, upper};
4433 /* See declaration. */
4436 mapped_index_base::build_name_components ()
4438 if (!this->name_components.empty ())
4441 this->name_components_casing = case_sensitivity;
4443 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4445 /* The code below only knows how to break apart components of C++
4446 symbol names (and other languages that use '::' as
4447 namespace/module separator). If we add support for wild matching
4448 to some language that uses some other operator (E.g., Ada, Go and
4449 D use '.'), then we'll need to try splitting the symbol name
4450 according to that language too. Note that Ada does support wild
4451 matching, but doesn't currently support .gdb_index. */
4452 auto count = this->symbol_name_count ();
4453 for (offset_type idx = 0; idx < count; idx++)
4455 if (this->symbol_name_slot_invalid (idx))
4458 const char *name = this->symbol_name_at (idx);
4460 /* Add each name component to the name component table. */
4461 unsigned int previous_len = 0;
4462 for (unsigned int current_len = cp_find_first_component (name);
4463 name[current_len] != '\0';
4464 current_len += cp_find_first_component (name + current_len))
4466 gdb_assert (name[current_len] == ':');
4467 this->name_components.push_back ({previous_len, idx});
4468 /* Skip the '::'. */
4470 previous_len = current_len;
4472 this->name_components.push_back ({previous_len, idx});
4475 /* Sort name_components elements by name. */
4476 auto name_comp_compare = [&] (const name_component &left,
4477 const name_component &right)
4479 const char *left_qualified = this->symbol_name_at (left.idx);
4480 const char *right_qualified = this->symbol_name_at (right.idx);
4482 const char *left_name = left_qualified + left.name_offset;
4483 const char *right_name = right_qualified + right.name_offset;
4485 return name_cmp (left_name, right_name) < 0;
4488 std::sort (this->name_components.begin (),
4489 this->name_components.end (),
4493 /* Helper for dw2_expand_symtabs_matching that works with a
4494 mapped_index_base instead of the containing objfile. This is split
4495 to a separate function in order to be able to unit test the
4496 name_components matching using a mock mapped_index_base. For each
4497 symbol name that matches, calls MATCH_CALLBACK, passing it the
4498 symbol's index in the mapped_index_base symbol table. */
4501 dw2_expand_symtabs_matching_symbol
4502 (mapped_index_base &index,
4503 const lookup_name_info &lookup_name_in,
4504 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4505 enum search_domain kind,
4506 gdb::function_view<void (offset_type)> match_callback)
4508 lookup_name_info lookup_name_without_params
4509 = lookup_name_in.make_ignore_params ();
4510 gdb_index_symbol_name_matcher lookup_name_matcher
4511 (lookup_name_without_params);
4513 /* Build the symbol name component sorted vector, if we haven't
4515 index.build_name_components ();
4517 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4522 /* The same symbol may appear more than once in the range though.
4523 E.g., if we're looking for symbols that complete "w", and we have
4524 a symbol named "w1::w2", we'll find the two name components for
4525 that same symbol in the range. To be sure we only call the
4526 callback once per symbol, we first collect the symbol name
4527 indexes that matched in a temporary vector and ignore
4529 std::vector<offset_type> matches;
4530 matches.reserve (std::distance (bounds.first, bounds.second));
4532 for (; bounds.first != bounds.second; ++bounds.first)
4534 const char *qualified = index.symbol_name_at (bounds.first->idx);
4536 if (!lookup_name_matcher.matches (qualified)
4537 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4540 matches.push_back (bounds.first->idx);
4543 std::sort (matches.begin (), matches.end ());
4545 /* Finally call the callback, once per match. */
4547 for (offset_type idx : matches)
4551 match_callback (idx);
4556 /* Above we use a type wider than idx's for 'prev', since 0 and
4557 (offset_type)-1 are both possible values. */
4558 static_assert (sizeof (prev) > sizeof (offset_type), "");
4563 namespace selftests { namespace dw2_expand_symtabs_matching {
4565 /* A mock .gdb_index/.debug_names-like name index table, enough to
4566 exercise dw2_expand_symtabs_matching_symbol, which works with the
4567 mapped_index_base interface. Builds an index from the symbol list
4568 passed as parameter to the constructor. */
4569 class mock_mapped_index : public mapped_index_base
4572 mock_mapped_index (gdb::array_view<const char *> symbols)
4573 : m_symbol_table (symbols)
4576 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4578 /* Return the number of names in the symbol table. */
4579 size_t symbol_name_count () const override
4581 return m_symbol_table.size ();
4584 /* Get the name of the symbol at IDX in the symbol table. */
4585 const char *symbol_name_at (offset_type idx) const override
4587 return m_symbol_table[idx];
4591 gdb::array_view<const char *> m_symbol_table;
4594 /* Convenience function that converts a NULL pointer to a "<null>"
4595 string, to pass to print routines. */
4598 string_or_null (const char *str)
4600 return str != NULL ? str : "<null>";
4603 /* Check if a lookup_name_info built from
4604 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4605 index. EXPECTED_LIST is the list of expected matches, in expected
4606 matching order. If no match expected, then an empty list is
4607 specified. Returns true on success. On failure prints a warning
4608 indicating the file:line that failed, and returns false. */
4611 check_match (const char *file, int line,
4612 mock_mapped_index &mock_index,
4613 const char *name, symbol_name_match_type match_type,
4614 bool completion_mode,
4615 std::initializer_list<const char *> expected_list)
4617 lookup_name_info lookup_name (name, match_type, completion_mode);
4619 bool matched = true;
4621 auto mismatch = [&] (const char *expected_str,
4624 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4625 "expected=\"%s\", got=\"%s\"\n"),
4627 (match_type == symbol_name_match_type::FULL
4629 name, string_or_null (expected_str), string_or_null (got));
4633 auto expected_it = expected_list.begin ();
4634 auto expected_end = expected_list.end ();
4636 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4638 [&] (offset_type idx)
4640 const char *matched_name = mock_index.symbol_name_at (idx);
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4644 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4645 mismatch (expected_str, matched_name);
4648 const char *expected_str
4649 = expected_it == expected_end ? NULL : *expected_it++;
4650 if (expected_str != NULL)
4651 mismatch (expected_str, NULL);
4656 /* The symbols added to the mock mapped_index for testing (in
4658 static const char *test_symbols[] = {
4667 "ns2::tmpl<int>::foo2",
4668 "(anonymous namespace)::A::B::C",
4670 /* These are used to check that the increment-last-char in the
4671 matching algorithm for completion doesn't match "t1_fund" when
4672 completing "t1_func". */
4678 /* A UTF-8 name with multi-byte sequences to make sure that
4679 cp-name-parser understands this as a single identifier ("função"
4680 is "function" in PT). */
4683 /* \377 (0xff) is Latin1 'ÿ'. */
4686 /* \377 (0xff) is Latin1 'ÿ'. */
4690 /* A name with all sorts of complications. Starts with "z" to make
4691 it easier for the completion tests below. */
4692 #define Z_SYM_NAME \
4693 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4694 "::tuple<(anonymous namespace)::ui*, " \
4695 "std::default_delete<(anonymous namespace)::ui>, void>"
4700 /* Returns true if the mapped_index_base::find_name_component_bounds
4701 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4702 in completion mode. */
4705 check_find_bounds_finds (mapped_index_base &index,
4706 const char *search_name,
4707 gdb::array_view<const char *> expected_syms)
4709 lookup_name_info lookup_name (search_name,
4710 symbol_name_match_type::FULL, true);
4712 auto bounds = index.find_name_components_bounds (lookup_name);
4714 size_t distance = std::distance (bounds.first, bounds.second);
4715 if (distance != expected_syms.size ())
4718 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4720 auto nc_elem = bounds.first + exp_elem;
4721 const char *qualified = index.symbol_name_at (nc_elem->idx);
4722 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4729 /* Test the lower-level mapped_index::find_name_component_bounds
4733 test_mapped_index_find_name_component_bounds ()
4735 mock_mapped_index mock_index (test_symbols);
4737 mock_index.build_name_components ();
4739 /* Test the lower-level mapped_index::find_name_component_bounds
4740 method in completion mode. */
4742 static const char *expected_syms[] = {
4747 SELF_CHECK (check_find_bounds_finds (mock_index,
4748 "t1_func", expected_syms));
4751 /* Check that the increment-last-char in the name matching algorithm
4752 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4754 static const char *expected_syms1[] = {
4758 SELF_CHECK (check_find_bounds_finds (mock_index,
4759 "\377", expected_syms1));
4761 static const char *expected_syms2[] = {
4764 SELF_CHECK (check_find_bounds_finds (mock_index,
4765 "\377\377", expected_syms2));
4769 /* Test dw2_expand_symtabs_matching_symbol. */
4772 test_dw2_expand_symtabs_matching_symbol ()
4774 mock_mapped_index mock_index (test_symbols);
4776 /* We let all tests run until the end even if some fails, for debug
4778 bool any_mismatch = false;
4780 /* Create the expected symbols list (an initializer_list). Needed
4781 because lists have commas, and we need to pass them to CHECK,
4782 which is a macro. */
4783 #define EXPECT(...) { __VA_ARGS__ }
4785 /* Wrapper for check_match that passes down the current
4786 __FILE__/__LINE__. */
4787 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4788 any_mismatch |= !check_match (__FILE__, __LINE__, \
4790 NAME, MATCH_TYPE, COMPLETION_MODE, \
4793 /* Identity checks. */
4794 for (const char *sym : test_symbols)
4796 /* Should be able to match all existing symbols. */
4797 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4800 /* Should be able to match all existing symbols with
4802 std::string with_params = std::string (sym) + "(int)";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4806 /* Should be able to match all existing symbols with
4807 parameters and qualifiers. */
4808 with_params = std::string (sym) + " ( int ) const";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4812 /* This should really find sym, but cp-name-parser.y doesn't
4813 know about lvalue/rvalue qualifiers yet. */
4814 with_params = std::string (sym) + " ( int ) &&";
4815 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4819 /* Check that the name matching algorithm for completion doesn't get
4820 confused with Latin1 'ÿ' / 0xff. */
4822 static const char str[] = "\377";
4823 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4824 EXPECT ("\377", "\377\377123"));
4827 /* Check that the increment-last-char in the matching algorithm for
4828 completion doesn't match "t1_fund" when completing "t1_func". */
4830 static const char str[] = "t1_func";
4831 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4832 EXPECT ("t1_func", "t1_func1"));
4835 /* Check that completion mode works at each prefix of the expected
4838 static const char str[] = "function(int)";
4839 size_t len = strlen (str);
4842 for (size_t i = 1; i < len; i++)
4844 lookup.assign (str, i);
4845 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4846 EXPECT ("function"));
4850 /* While "w" is a prefix of both components, the match function
4851 should still only be called once. */
4853 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4855 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4859 /* Same, with a "complicated" symbol. */
4861 static const char str[] = Z_SYM_NAME;
4862 size_t len = strlen (str);
4865 for (size_t i = 1; i < len; i++)
4867 lookup.assign (str, i);
4868 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4869 EXPECT (Z_SYM_NAME));
4873 /* In FULL mode, an incomplete symbol doesn't match. */
4875 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4879 /* A complete symbol with parameters matches any overload, since the
4880 index has no overload info. */
4882 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
4884 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
4886 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
4890 /* Check that whitespace is ignored appropriately. A symbol with a
4891 template argument list. */
4893 static const char expected[] = "ns::foo<int>";
4894 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4896 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4900 /* Check that whitespace is ignored appropriately. A symbol with a
4901 template argument list that includes a pointer. */
4903 static const char expected[] = "ns::foo<char*>";
4904 /* Try both completion and non-completion modes. */
4905 static const bool completion_mode[2] = {false, true};
4906 for (size_t i = 0; i < 2; i++)
4908 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4909 completion_mode[i], EXPECT (expected));
4910 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4911 completion_mode[i], EXPECT (expected));
4913 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4914 completion_mode[i], EXPECT (expected));
4915 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4916 completion_mode[i], EXPECT (expected));
4921 /* Check method qualifiers are ignored. */
4922 static const char expected[] = "ns::foo<char*>";
4923 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4924 symbol_name_match_type::FULL, true, EXPECT (expected));
4925 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4926 symbol_name_match_type::FULL, true, EXPECT (expected));
4927 CHECK_MATCH ("foo < char * > ( int ) const",
4928 symbol_name_match_type::WILD, true, EXPECT (expected));
4929 CHECK_MATCH ("foo < char * > ( int ) &&",
4930 symbol_name_match_type::WILD, true, EXPECT (expected));
4933 /* Test lookup names that don't match anything. */
4935 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4938 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4942 /* Some wild matching tests, exercising "(anonymous namespace)",
4943 which should not be confused with a parameter list. */
4945 static const char *syms[] = {
4949 "A :: B :: C ( int )",
4954 for (const char *s : syms)
4956 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4957 EXPECT ("(anonymous namespace)::A::B::C"));
4962 static const char expected[] = "ns2::tmpl<int>::foo2";
4963 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4965 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4969 SELF_CHECK (!any_mismatch);
4978 test_mapped_index_find_name_component_bounds ();
4979 test_dw2_expand_symtabs_matching_symbol ();
4982 }} // namespace selftests::dw2_expand_symtabs_matching
4984 #endif /* GDB_SELF_TEST */
4986 /* If FILE_MATCHER is NULL or if PER_CU has
4987 dwarf2_per_cu_quick_data::MARK set (see
4988 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4989 EXPANSION_NOTIFY on it. */
4992 dw2_expand_symtabs_matching_one
4993 (struct dwarf2_per_cu_data *per_cu,
4994 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4995 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4997 if (file_matcher == NULL || per_cu->v.quick->mark)
4999 bool symtab_was_null
5000 = (per_cu->v.quick->compunit_symtab == NULL);
5002 dw2_instantiate_symtab (per_cu, false);
5004 if (expansion_notify != NULL
5006 && per_cu->v.quick->compunit_symtab != NULL)
5007 expansion_notify (per_cu->v.quick->compunit_symtab);
5011 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5012 matched, to expand corresponding CUs that were marked. IDX is the
5013 index of the symbol name that matched. */
5016 dw2_expand_marked_cus
5017 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5018 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5019 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5022 offset_type *vec, vec_len, vec_idx;
5023 bool global_seen = false;
5024 mapped_index &index = *dwarf2_per_objfile->index_table;
5026 vec = (offset_type *) (index.constant_pool
5027 + MAYBE_SWAP (index.symbol_table[idx].vec));
5028 vec_len = MAYBE_SWAP (vec[0]);
5029 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5031 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5032 /* This value is only valid for index versions >= 7. */
5033 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5034 gdb_index_symbol_kind symbol_kind =
5035 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5036 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5037 /* Only check the symbol attributes if they're present.
5038 Indices prior to version 7 don't record them,
5039 and indices >= 7 may elide them for certain symbols
5040 (gold does this). */
5043 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5045 /* Work around gold/15646. */
5048 if (!is_static && global_seen)
5054 /* Only check the symbol's kind if it has one. */
5059 case VARIABLES_DOMAIN:
5060 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5063 case FUNCTIONS_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5076 /* Don't crash on bad data. */
5077 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5078 + dwarf2_per_objfile->all_type_units.size ()))
5080 complaint (_(".gdb_index entry has bad CU index"
5082 objfile_name (dwarf2_per_objfile->objfile));
5086 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5087 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5092 /* If FILE_MATCHER is non-NULL, set all the
5093 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5094 that match FILE_MATCHER. */
5097 dw_expand_symtabs_matching_file_matcher
5098 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5099 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5101 if (file_matcher == NULL)
5104 objfile *const objfile = dwarf2_per_objfile->objfile;
5106 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5108 NULL, xcalloc, xfree));
5109 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5111 NULL, xcalloc, xfree));
5113 /* The rule is CUs specify all the files, including those used by
5114 any TU, so there's no need to scan TUs here. */
5116 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5120 per_cu->v.quick->mark = 0;
5122 /* We only need to look at symtabs not already expanded. */
5123 if (per_cu->v.quick->compunit_symtab)
5126 quick_file_names *file_data = dw2_get_file_names (per_cu);
5127 if (file_data == NULL)
5130 if (htab_find (visited_not_found.get (), file_data) != NULL)
5132 else if (htab_find (visited_found.get (), file_data) != NULL)
5134 per_cu->v.quick->mark = 1;
5138 for (int j = 0; j < file_data->num_file_names; ++j)
5140 const char *this_real_name;
5142 if (file_matcher (file_data->file_names[j], false))
5144 per_cu->v.quick->mark = 1;
5148 /* Before we invoke realpath, which can get expensive when many
5149 files are involved, do a quick comparison of the basenames. */
5150 if (!basenames_may_differ
5151 && !file_matcher (lbasename (file_data->file_names[j]),
5155 this_real_name = dw2_get_real_path (objfile, file_data, j);
5156 if (file_matcher (this_real_name, false))
5158 per_cu->v.quick->mark = 1;
5163 void **slot = htab_find_slot (per_cu->v.quick->mark
5164 ? visited_found.get ()
5165 : visited_not_found.get (),
5172 dw2_expand_symtabs_matching
5173 (struct objfile *objfile,
5174 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5175 const lookup_name_info &lookup_name,
5176 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5177 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5178 enum search_domain kind)
5180 struct dwarf2_per_objfile *dwarf2_per_objfile
5181 = get_dwarf2_per_objfile (objfile);
5183 /* index_table is NULL if OBJF_READNOW. */
5184 if (!dwarf2_per_objfile->index_table)
5187 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5189 mapped_index &index = *dwarf2_per_objfile->index_table;
5191 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5193 kind, [&] (offset_type idx)
5195 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5196 expansion_notify, kind);
5200 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5203 static struct compunit_symtab *
5204 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5209 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5210 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5213 if (cust->includes == NULL)
5216 for (i = 0; cust->includes[i]; ++i)
5218 struct compunit_symtab *s = cust->includes[i];
5220 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5228 static struct compunit_symtab *
5229 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5230 struct bound_minimal_symbol msymbol,
5232 struct obj_section *section,
5235 struct dwarf2_per_cu_data *data;
5236 struct compunit_symtab *result;
5238 if (!objfile->partial_symtabs->psymtabs_addrmap)
5241 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5242 SECT_OFF_TEXT (objfile));
5243 data = (struct dwarf2_per_cu_data *) addrmap_find
5244 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5248 if (warn_if_readin && data->v.quick->compunit_symtab)
5249 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5250 paddress (get_objfile_arch (objfile), pc));
5253 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5256 gdb_assert (result != NULL);
5261 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5262 void *data, int need_fullname)
5264 struct dwarf2_per_objfile *dwarf2_per_objfile
5265 = get_dwarf2_per_objfile (objfile);
5267 if (!dwarf2_per_objfile->filenames_cache)
5269 dwarf2_per_objfile->filenames_cache.emplace ();
5271 htab_up visited (htab_create_alloc (10,
5272 htab_hash_pointer, htab_eq_pointer,
5273 NULL, xcalloc, xfree));
5275 /* The rule is CUs specify all the files, including those used
5276 by any TU, so there's no need to scan TUs here. We can
5277 ignore file names coming from already-expanded CUs. */
5279 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5281 if (per_cu->v.quick->compunit_symtab)
5283 void **slot = htab_find_slot (visited.get (),
5284 per_cu->v.quick->file_names,
5287 *slot = per_cu->v.quick->file_names;
5291 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5293 /* We only need to look at symtabs not already expanded. */
5294 if (per_cu->v.quick->compunit_symtab)
5297 quick_file_names *file_data = dw2_get_file_names (per_cu);
5298 if (file_data == NULL)
5301 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5304 /* Already visited. */
5309 for (int j = 0; j < file_data->num_file_names; ++j)
5311 const char *filename = file_data->file_names[j];
5312 dwarf2_per_objfile->filenames_cache->seen (filename);
5317 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5319 gdb::unique_xmalloc_ptr<char> this_real_name;
5322 this_real_name = gdb_realpath (filename);
5323 (*fun) (filename, this_real_name.get (), data);
5328 dw2_has_symbols (struct objfile *objfile)
5333 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5336 dw2_find_last_source_symtab,
5337 dw2_forget_cached_source_info,
5338 dw2_map_symtabs_matching_filename,
5342 dw2_expand_symtabs_for_function,
5343 dw2_expand_all_symtabs,
5344 dw2_expand_symtabs_with_fullname,
5345 dw2_map_matching_symbols,
5346 dw2_expand_symtabs_matching,
5347 dw2_find_pc_sect_compunit_symtab,
5349 dw2_map_symbol_filenames
5352 /* DWARF-5 debug_names reader. */
5354 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5355 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5357 /* A helper function that reads the .debug_names section in SECTION
5358 and fills in MAP. FILENAME is the name of the file containing the
5359 section; it is used for error reporting.
5361 Returns true if all went well, false otherwise. */
5364 read_debug_names_from_section (struct objfile *objfile,
5365 const char *filename,
5366 struct dwarf2_section_info *section,
5367 mapped_debug_names &map)
5369 if (dwarf2_section_empty_p (section))
5372 /* Older elfutils strip versions could keep the section in the main
5373 executable while splitting it for the separate debug info file. */
5374 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5377 dwarf2_read_section (objfile, section);
5379 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5381 const gdb_byte *addr = section->buffer;
5383 bfd *const abfd = get_section_bfd_owner (section);
5385 unsigned int bytes_read;
5386 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5389 map.dwarf5_is_dwarf64 = bytes_read != 4;
5390 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5391 if (bytes_read + length != section->size)
5393 /* There may be multiple per-CU indices. */
5394 warning (_("Section .debug_names in %s length %s does not match "
5395 "section length %s, ignoring .debug_names."),
5396 filename, plongest (bytes_read + length),
5397 pulongest (section->size));
5401 /* The version number. */
5402 uint16_t version = read_2_bytes (abfd, addr);
5406 warning (_("Section .debug_names in %s has unsupported version %d, "
5407 "ignoring .debug_names."),
5413 uint16_t padding = read_2_bytes (abfd, addr);
5417 warning (_("Section .debug_names in %s has unsupported padding %d, "
5418 "ignoring .debug_names."),
5423 /* comp_unit_count - The number of CUs in the CU list. */
5424 map.cu_count = read_4_bytes (abfd, addr);
5427 /* local_type_unit_count - The number of TUs in the local TU
5429 map.tu_count = read_4_bytes (abfd, addr);
5432 /* foreign_type_unit_count - The number of TUs in the foreign TU
5434 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5436 if (foreign_tu_count != 0)
5438 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5439 "ignoring .debug_names."),
5440 filename, static_cast<unsigned long> (foreign_tu_count));
5444 /* bucket_count - The number of hash buckets in the hash lookup
5446 map.bucket_count = read_4_bytes (abfd, addr);
5449 /* name_count - The number of unique names in the index. */
5450 map.name_count = read_4_bytes (abfd, addr);
5453 /* abbrev_table_size - The size in bytes of the abbreviations
5455 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5458 /* augmentation_string_size - The size in bytes of the augmentation
5459 string. This value is rounded up to a multiple of 4. */
5460 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5462 map.augmentation_is_gdb = ((augmentation_string_size
5463 == sizeof (dwarf5_augmentation))
5464 && memcmp (addr, dwarf5_augmentation,
5465 sizeof (dwarf5_augmentation)) == 0);
5466 augmentation_string_size += (-augmentation_string_size) & 3;
5467 addr += augmentation_string_size;
5470 map.cu_table_reordered = addr;
5471 addr += map.cu_count * map.offset_size;
5473 /* List of Local TUs */
5474 map.tu_table_reordered = addr;
5475 addr += map.tu_count * map.offset_size;
5477 /* Hash Lookup Table */
5478 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.bucket_count * 4;
5480 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5481 addr += map.name_count * 4;
5484 map.name_table_string_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486 map.name_table_entry_offs_reordered = addr;
5487 addr += map.name_count * map.offset_size;
5489 const gdb_byte *abbrev_table_start = addr;
5492 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5497 const auto insertpair
5498 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5499 if (!insertpair.second)
5501 warning (_("Section .debug_names in %s has duplicate index %s, "
5502 "ignoring .debug_names."),
5503 filename, pulongest (index_num));
5506 mapped_debug_names::index_val &indexval = insertpair.first->second;
5507 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5512 mapped_debug_names::index_val::attr attr;
5513 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5515 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 if (attr.form == DW_FORM_implicit_const)
5519 attr.implicit_const = read_signed_leb128 (abfd, addr,
5523 if (attr.dw_idx == 0 && attr.form == 0)
5525 indexval.attr_vec.push_back (std::move (attr));
5528 if (addr != abbrev_table_start + abbrev_table_size)
5530 warning (_("Section .debug_names in %s has abbreviation_table "
5531 "of size %zu vs. written as %u, ignoring .debug_names."),
5532 filename, addr - abbrev_table_start, abbrev_table_size);
5535 map.entry_pool = addr;
5540 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5544 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5545 const mapped_debug_names &map,
5546 dwarf2_section_info §ion,
5549 sect_offset sect_off_prev;
5550 for (uint32_t i = 0; i <= map.cu_count; ++i)
5552 sect_offset sect_off_next;
5553 if (i < map.cu_count)
5556 = (sect_offset) (extract_unsigned_integer
5557 (map.cu_table_reordered + i * map.offset_size,
5559 map.dwarf5_byte_order));
5562 sect_off_next = (sect_offset) section.size;
5565 const ULONGEST length = sect_off_next - sect_off_prev;
5566 dwarf2_per_cu_data *per_cu
5567 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
5568 sect_off_prev, length);
5569 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5571 sect_off_prev = sect_off_next;
5575 /* Read the CU list from the mapped index, and use it to create all
5576 the CU objects for this dwarf2_per_objfile. */
5579 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5580 const mapped_debug_names &map,
5581 const mapped_debug_names &dwz_map)
5583 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5584 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5586 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5587 dwarf2_per_objfile->info,
5588 false /* is_dwz */);
5590 if (dwz_map.cu_count == 0)
5593 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5594 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5598 /* Read .debug_names. If everything went ok, initialize the "quick"
5599 elements of all the CUs and return true. Otherwise, return false. */
5602 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5604 std::unique_ptr<mapped_debug_names> map
5605 (new mapped_debug_names (dwarf2_per_objfile));
5606 mapped_debug_names dwz_map (dwarf2_per_objfile);
5607 struct objfile *objfile = dwarf2_per_objfile->objfile;
5609 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5610 &dwarf2_per_objfile->debug_names,
5614 /* Don't use the index if it's empty. */
5615 if (map->name_count == 0)
5618 /* If there is a .dwz file, read it so we can get its CU list as
5620 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5623 if (!read_debug_names_from_section (objfile,
5624 bfd_get_filename (dwz->dwz_bfd),
5625 &dwz->debug_names, dwz_map))
5627 warning (_("could not read '.debug_names' section from %s; skipping"),
5628 bfd_get_filename (dwz->dwz_bfd));
5633 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5635 if (map->tu_count != 0)
5637 /* We can only handle a single .debug_types when we have an
5639 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5642 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5643 dwarf2_per_objfile->types, 0);
5645 create_signatured_type_table_from_debug_names
5646 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5649 create_addrmap_from_aranges (dwarf2_per_objfile,
5650 &dwarf2_per_objfile->debug_aranges);
5652 dwarf2_per_objfile->debug_names_table = std::move (map);
5653 dwarf2_per_objfile->using_index = 1;
5654 dwarf2_per_objfile->quick_file_names_table =
5655 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5660 /* Type used to manage iterating over all CUs looking for a symbol for
5663 class dw2_debug_names_iterator
5666 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5667 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5668 dw2_debug_names_iterator (const mapped_debug_names &map,
5669 bool want_specific_block,
5670 block_enum block_index, domain_enum domain,
5672 : m_map (map), m_want_specific_block (want_specific_block),
5673 m_block_index (block_index), m_domain (domain),
5674 m_addr (find_vec_in_debug_names (map, name))
5677 dw2_debug_names_iterator (const mapped_debug_names &map,
5678 search_domain search, uint32_t namei)
5681 m_addr (find_vec_in_debug_names (map, namei))
5684 /* Return the next matching CU or NULL if there are no more. */
5685 dwarf2_per_cu_data *next ();
5688 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5693 /* The internalized form of .debug_names. */
5694 const mapped_debug_names &m_map;
5696 /* If true, only look for symbols that match BLOCK_INDEX. */
5697 const bool m_want_specific_block = false;
5699 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5700 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5702 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5704 /* The kind of symbol we're looking for. */
5705 const domain_enum m_domain = UNDEF_DOMAIN;
5706 const search_domain m_search = ALL_DOMAIN;
5708 /* The list of CUs from the index entry of the symbol, or NULL if
5710 const gdb_byte *m_addr;
5714 mapped_debug_names::namei_to_name (uint32_t namei) const
5716 const ULONGEST namei_string_offs
5717 = extract_unsigned_integer ((name_table_string_offs_reordered
5718 + namei * offset_size),
5721 return read_indirect_string_at_offset
5722 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5725 /* Find a slot in .debug_names for the object named NAME. If NAME is
5726 found, return pointer to its pool data. If NAME cannot be found,
5730 dw2_debug_names_iterator::find_vec_in_debug_names
5731 (const mapped_debug_names &map, const char *name)
5733 int (*cmp) (const char *, const char *);
5735 if (current_language->la_language == language_cplus
5736 || current_language->la_language == language_fortran
5737 || current_language->la_language == language_d)
5739 /* NAME is already canonical. Drop any qualifiers as
5740 .debug_names does not contain any. */
5742 if (strchr (name, '(') != NULL)
5744 gdb::unique_xmalloc_ptr<char> without_params
5745 = cp_remove_params (name);
5747 if (without_params != NULL)
5749 name = without_params.get();
5754 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5756 const uint32_t full_hash = dwarf5_djb_hash (name);
5758 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5759 (map.bucket_table_reordered
5760 + (full_hash % map.bucket_count)), 4,
5761 map.dwarf5_byte_order);
5765 if (namei >= map.name_count)
5767 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5769 namei, map.name_count,
5770 objfile_name (map.dwarf2_per_objfile->objfile));
5776 const uint32_t namei_full_hash
5777 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5778 (map.hash_table_reordered + namei), 4,
5779 map.dwarf5_byte_order);
5780 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5783 if (full_hash == namei_full_hash)
5785 const char *const namei_string = map.namei_to_name (namei);
5787 #if 0 /* An expensive sanity check. */
5788 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5790 complaint (_("Wrong .debug_names hash for string at index %u "
5792 namei, objfile_name (dwarf2_per_objfile->objfile));
5797 if (cmp (namei_string, name) == 0)
5799 const ULONGEST namei_entry_offs
5800 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5801 + namei * map.offset_size),
5802 map.offset_size, map.dwarf5_byte_order);
5803 return map.entry_pool + namei_entry_offs;
5808 if (namei >= map.name_count)
5814 dw2_debug_names_iterator::find_vec_in_debug_names
5815 (const mapped_debug_names &map, uint32_t namei)
5817 if (namei >= map.name_count)
5819 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5821 namei, map.name_count,
5822 objfile_name (map.dwarf2_per_objfile->objfile));
5826 const ULONGEST namei_entry_offs
5827 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5828 + namei * map.offset_size),
5829 map.offset_size, map.dwarf5_byte_order);
5830 return map.entry_pool + namei_entry_offs;
5833 /* See dw2_debug_names_iterator. */
5835 dwarf2_per_cu_data *
5836 dw2_debug_names_iterator::next ()
5841 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5842 struct objfile *objfile = dwarf2_per_objfile->objfile;
5843 bfd *const abfd = objfile->obfd;
5847 unsigned int bytes_read;
5848 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5849 m_addr += bytes_read;
5853 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5854 if (indexval_it == m_map.abbrev_map.cend ())
5856 complaint (_("Wrong .debug_names undefined abbrev code %s "
5858 pulongest (abbrev), objfile_name (objfile));
5861 const mapped_debug_names::index_val &indexval = indexval_it->second;
5862 bool have_is_static = false;
5864 dwarf2_per_cu_data *per_cu = NULL;
5865 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5870 case DW_FORM_implicit_const:
5871 ull = attr.implicit_const;
5873 case DW_FORM_flag_present:
5877 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5878 m_addr += bytes_read;
5881 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5882 dwarf_form_name (attr.form),
5883 objfile_name (objfile));
5886 switch (attr.dw_idx)
5888 case DW_IDX_compile_unit:
5889 /* Don't crash on bad data. */
5890 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5892 complaint (_(".debug_names entry has bad CU index %s"
5895 objfile_name (dwarf2_per_objfile->objfile));
5898 per_cu = dwarf2_per_objfile->get_cutu (ull);
5900 case DW_IDX_type_unit:
5901 /* Don't crash on bad data. */
5902 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5904 complaint (_(".debug_names entry has bad TU index %s"
5907 objfile_name (dwarf2_per_objfile->objfile));
5910 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5912 case DW_IDX_GNU_internal:
5913 if (!m_map.augmentation_is_gdb)
5915 have_is_static = true;
5918 case DW_IDX_GNU_external:
5919 if (!m_map.augmentation_is_gdb)
5921 have_is_static = true;
5927 /* Skip if already read in. */
5928 if (per_cu->v.quick->compunit_symtab)
5931 /* Check static vs global. */
5934 const bool want_static = m_block_index != GLOBAL_BLOCK;
5935 if (m_want_specific_block && want_static != is_static)
5939 /* Match dw2_symtab_iter_next, symbol_kind
5940 and debug_names::psymbol_tag. */
5944 switch (indexval.dwarf_tag)
5946 case DW_TAG_variable:
5947 case DW_TAG_subprogram:
5948 /* Some types are also in VAR_DOMAIN. */
5949 case DW_TAG_typedef:
5950 case DW_TAG_structure_type:
5957 switch (indexval.dwarf_tag)
5959 case DW_TAG_typedef:
5960 case DW_TAG_structure_type:
5967 switch (indexval.dwarf_tag)
5970 case DW_TAG_variable:
5980 /* Match dw2_expand_symtabs_matching, symbol_kind and
5981 debug_names::psymbol_tag. */
5984 case VARIABLES_DOMAIN:
5985 switch (indexval.dwarf_tag)
5987 case DW_TAG_variable:
5993 case FUNCTIONS_DOMAIN:
5994 switch (indexval.dwarf_tag)
5996 case DW_TAG_subprogram:
6003 switch (indexval.dwarf_tag)
6005 case DW_TAG_typedef:
6006 case DW_TAG_structure_type:
6019 static struct compunit_symtab *
6020 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6021 const char *name, domain_enum domain)
6023 const block_enum block_index = static_cast<block_enum> (block_index_int);
6024 struct dwarf2_per_objfile *dwarf2_per_objfile
6025 = get_dwarf2_per_objfile (objfile);
6027 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6030 /* index is NULL if OBJF_READNOW. */
6033 const auto &map = *mapp;
6035 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6036 block_index, domain, name);
6038 struct compunit_symtab *stab_best = NULL;
6039 struct dwarf2_per_cu_data *per_cu;
6040 while ((per_cu = iter.next ()) != NULL)
6042 struct symbol *sym, *with_opaque = NULL;
6043 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6044 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6045 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6047 sym = block_find_symbol (block, name, domain,
6048 block_find_non_opaque_type_preferred,
6051 /* Some caution must be observed with overloaded functions and
6052 methods, since the index will not contain any overload
6053 information (but NAME might contain it). */
6056 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6058 if (with_opaque != NULL
6059 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6062 /* Keep looking through other CUs. */
6068 /* This dumps minimal information about .debug_names. It is called
6069 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6070 uses this to verify that .debug_names has been loaded. */
6073 dw2_debug_names_dump (struct objfile *objfile)
6075 struct dwarf2_per_objfile *dwarf2_per_objfile
6076 = get_dwarf2_per_objfile (objfile);
6078 gdb_assert (dwarf2_per_objfile->using_index);
6079 printf_filtered (".debug_names:");
6080 if (dwarf2_per_objfile->debug_names_table)
6081 printf_filtered (" exists\n");
6083 printf_filtered (" faked for \"readnow\"\n");
6084 printf_filtered ("\n");
6088 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6089 const char *func_name)
6091 struct dwarf2_per_objfile *dwarf2_per_objfile
6092 = get_dwarf2_per_objfile (objfile);
6094 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6095 if (dwarf2_per_objfile->debug_names_table)
6097 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6099 /* Note: It doesn't matter what we pass for block_index here. */
6100 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6101 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6103 struct dwarf2_per_cu_data *per_cu;
6104 while ((per_cu = iter.next ()) != NULL)
6105 dw2_instantiate_symtab (per_cu, false);
6110 dw2_debug_names_expand_symtabs_matching
6111 (struct objfile *objfile,
6112 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6113 const lookup_name_info &lookup_name,
6114 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6115 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6116 enum search_domain kind)
6118 struct dwarf2_per_objfile *dwarf2_per_objfile
6119 = get_dwarf2_per_objfile (objfile);
6121 /* debug_names_table is NULL if OBJF_READNOW. */
6122 if (!dwarf2_per_objfile->debug_names_table)
6125 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6127 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6129 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6131 kind, [&] (offset_type namei)
6133 /* The name was matched, now expand corresponding CUs that were
6135 dw2_debug_names_iterator iter (map, kind, namei);
6137 struct dwarf2_per_cu_data *per_cu;
6138 while ((per_cu = iter.next ()) != NULL)
6139 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6144 const struct quick_symbol_functions dwarf2_debug_names_functions =
6147 dw2_find_last_source_symtab,
6148 dw2_forget_cached_source_info,
6149 dw2_map_symtabs_matching_filename,
6150 dw2_debug_names_lookup_symbol,
6152 dw2_debug_names_dump,
6153 dw2_debug_names_expand_symtabs_for_function,
6154 dw2_expand_all_symtabs,
6155 dw2_expand_symtabs_with_fullname,
6156 dw2_map_matching_symbols,
6157 dw2_debug_names_expand_symtabs_matching,
6158 dw2_find_pc_sect_compunit_symtab,
6160 dw2_map_symbol_filenames
6163 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6164 to either a dwarf2_per_objfile or dwz_file object. */
6166 template <typename T>
6167 static gdb::array_view<const gdb_byte>
6168 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6170 dwarf2_section_info *section = §ion_owner->gdb_index;
6172 if (dwarf2_section_empty_p (section))
6175 /* Older elfutils strip versions could keep the section in the main
6176 executable while splitting it for the separate debug info file. */
6177 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6180 dwarf2_read_section (obj, section);
6182 /* dwarf2_section_info::size is a bfd_size_type, while
6183 gdb::array_view works with size_t. On 32-bit hosts, with
6184 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6185 is 32-bit. So we need an explicit narrowing conversion here.
6186 This is fine, because it's impossible to allocate or mmap an
6187 array/buffer larger than what size_t can represent. */
6188 return gdb::make_array_view (section->buffer, section->size);
6191 /* Lookup the index cache for the contents of the index associated to
6194 static gdb::array_view<const gdb_byte>
6195 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6197 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6198 if (build_id == nullptr)
6201 return global_index_cache.lookup_gdb_index (build_id,
6202 &dwarf2_obj->index_cache_res);
6205 /* Same as the above, but for DWZ. */
6207 static gdb::array_view<const gdb_byte>
6208 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6210 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6211 if (build_id == nullptr)
6214 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6217 /* See symfile.h. */
6220 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6222 struct dwarf2_per_objfile *dwarf2_per_objfile
6223 = get_dwarf2_per_objfile (objfile);
6225 /* If we're about to read full symbols, don't bother with the
6226 indices. In this case we also don't care if some other debug
6227 format is making psymtabs, because they are all about to be
6229 if ((objfile->flags & OBJF_READNOW))
6231 dwarf2_per_objfile->using_index = 1;
6232 create_all_comp_units (dwarf2_per_objfile);
6233 create_all_type_units (dwarf2_per_objfile);
6234 dwarf2_per_objfile->quick_file_names_table
6235 = create_quick_file_names_table
6236 (dwarf2_per_objfile->all_comp_units.size ());
6238 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6239 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6241 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6243 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6244 struct dwarf2_per_cu_quick_data);
6247 /* Return 1 so that gdb sees the "quick" functions. However,
6248 these functions will be no-ops because we will have expanded
6250 *index_kind = dw_index_kind::GDB_INDEX;
6254 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6256 *index_kind = dw_index_kind::DEBUG_NAMES;
6260 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6261 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6262 get_gdb_index_contents_from_section<dwz_file>))
6264 *index_kind = dw_index_kind::GDB_INDEX;
6268 /* ... otherwise, try to find the index in the index cache. */
6269 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6270 get_gdb_index_contents_from_cache,
6271 get_gdb_index_contents_from_cache_dwz))
6273 global_index_cache.hit ();
6274 *index_kind = dw_index_kind::GDB_INDEX;
6278 global_index_cache.miss ();
6284 /* Build a partial symbol table. */
6287 dwarf2_build_psymtabs (struct objfile *objfile)
6289 struct dwarf2_per_objfile *dwarf2_per_objfile
6290 = get_dwarf2_per_objfile (objfile);
6292 init_psymbol_list (objfile, 1024);
6296 /* This isn't really ideal: all the data we allocate on the
6297 objfile's obstack is still uselessly kept around. However,
6298 freeing it seems unsafe. */
6299 psymtab_discarder psymtabs (objfile);
6300 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6303 /* (maybe) store an index in the cache. */
6304 global_index_cache.store (dwarf2_per_objfile);
6306 catch (const gdb_exception_error &except)
6308 exception_print (gdb_stderr, except);
6312 /* Return the total length of the CU described by HEADER. */
6315 get_cu_length (const struct comp_unit_head *header)
6317 return header->initial_length_size + header->length;
6320 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6323 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6325 sect_offset bottom = cu_header->sect_off;
6326 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6328 return sect_off >= bottom && sect_off < top;
6331 /* Find the base address of the compilation unit for range lists and
6332 location lists. It will normally be specified by DW_AT_low_pc.
6333 In DWARF-3 draft 4, the base address could be overridden by
6334 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6335 compilation units with discontinuous ranges. */
6338 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6340 struct attribute *attr;
6343 cu->base_address = 0;
6345 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6348 cu->base_address = attr_value_as_address (attr);
6353 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6356 cu->base_address = attr_value_as_address (attr);
6362 /* Read in the comp unit header information from the debug_info at info_ptr.
6363 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6364 NOTE: This leaves members offset, first_die_offset to be filled in
6367 static const gdb_byte *
6368 read_comp_unit_head (struct comp_unit_head *cu_header,
6369 const gdb_byte *info_ptr,
6370 struct dwarf2_section_info *section,
6371 rcuh_kind section_kind)
6374 unsigned int bytes_read;
6375 const char *filename = get_section_file_name (section);
6376 bfd *abfd = get_section_bfd_owner (section);
6378 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6379 cu_header->initial_length_size = bytes_read;
6380 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6381 info_ptr += bytes_read;
6382 cu_header->version = read_2_bytes (abfd, info_ptr);
6383 if (cu_header->version < 2 || cu_header->version > 5)
6384 error (_("Dwarf Error: wrong version in compilation unit header "
6385 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6386 cu_header->version, filename);
6388 if (cu_header->version < 5)
6389 switch (section_kind)
6391 case rcuh_kind::COMPILE:
6392 cu_header->unit_type = DW_UT_compile;
6394 case rcuh_kind::TYPE:
6395 cu_header->unit_type = DW_UT_type;
6398 internal_error (__FILE__, __LINE__,
6399 _("read_comp_unit_head: invalid section_kind"));
6403 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6404 (read_1_byte (abfd, info_ptr));
6406 switch (cu_header->unit_type)
6409 if (section_kind != rcuh_kind::COMPILE)
6410 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6411 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6415 section_kind = rcuh_kind::TYPE;
6418 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6419 "(is %d, should be %d or %d) [in module %s]"),
6420 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6423 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6426 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6429 info_ptr += bytes_read;
6430 if (cu_header->version < 5)
6432 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6435 signed_addr = bfd_get_sign_extend_vma (abfd);
6436 if (signed_addr < 0)
6437 internal_error (__FILE__, __LINE__,
6438 _("read_comp_unit_head: dwarf from non elf file"));
6439 cu_header->signed_addr_p = signed_addr;
6441 if (section_kind == rcuh_kind::TYPE)
6443 LONGEST type_offset;
6445 cu_header->signature = read_8_bytes (abfd, info_ptr);
6448 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6449 info_ptr += bytes_read;
6450 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6451 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6452 error (_("Dwarf Error: Too big type_offset in compilation unit "
6453 "header (is %s) [in module %s]"), plongest (type_offset),
6460 /* Helper function that returns the proper abbrev section for
6463 static struct dwarf2_section_info *
6464 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6466 struct dwarf2_section_info *abbrev;
6467 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6469 if (this_cu->is_dwz)
6470 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6472 abbrev = &dwarf2_per_objfile->abbrev;
6477 /* Subroutine of read_and_check_comp_unit_head and
6478 read_and_check_type_unit_head to simplify them.
6479 Perform various error checking on the header. */
6482 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6483 struct comp_unit_head *header,
6484 struct dwarf2_section_info *section,
6485 struct dwarf2_section_info *abbrev_section)
6487 const char *filename = get_section_file_name (section);
6489 if (to_underlying (header->abbrev_sect_off)
6490 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6491 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6492 "(offset %s + 6) [in module %s]"),
6493 sect_offset_str (header->abbrev_sect_off),
6494 sect_offset_str (header->sect_off),
6497 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6498 avoid potential 32-bit overflow. */
6499 if (((ULONGEST) header->sect_off + get_cu_length (header))
6501 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6502 "(offset %s + 0) [in module %s]"),
6503 header->length, sect_offset_str (header->sect_off),
6507 /* Read in a CU/TU header and perform some basic error checking.
6508 The contents of the header are stored in HEADER.
6509 The result is a pointer to the start of the first DIE. */
6511 static const gdb_byte *
6512 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6513 struct comp_unit_head *header,
6514 struct dwarf2_section_info *section,
6515 struct dwarf2_section_info *abbrev_section,
6516 const gdb_byte *info_ptr,
6517 rcuh_kind section_kind)
6519 const gdb_byte *beg_of_comp_unit = info_ptr;
6521 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6523 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6525 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6527 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6533 /* Fetch the abbreviation table offset from a comp or type unit header. */
6536 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6537 struct dwarf2_section_info *section,
6538 sect_offset sect_off)
6540 bfd *abfd = get_section_bfd_owner (section);
6541 const gdb_byte *info_ptr;
6542 unsigned int initial_length_size, offset_size;
6545 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6546 info_ptr = section->buffer + to_underlying (sect_off);
6547 read_initial_length (abfd, info_ptr, &initial_length_size);
6548 offset_size = initial_length_size == 4 ? 4 : 8;
6549 info_ptr += initial_length_size;
6551 version = read_2_bytes (abfd, info_ptr);
6555 /* Skip unit type and address size. */
6559 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6562 /* Allocate a new partial symtab for file named NAME and mark this new
6563 partial symtab as being an include of PST. */
6566 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6567 struct objfile *objfile)
6569 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6571 if (!IS_ABSOLUTE_PATH (subpst->filename))
6573 /* It shares objfile->objfile_obstack. */
6574 subpst->dirname = pst->dirname;
6577 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6578 subpst->dependencies[0] = pst;
6579 subpst->number_of_dependencies = 1;
6581 subpst->read_symtab = pst->read_symtab;
6583 /* No private part is necessary for include psymtabs. This property
6584 can be used to differentiate between such include psymtabs and
6585 the regular ones. */
6586 subpst->read_symtab_private = NULL;
6589 /* Read the Line Number Program data and extract the list of files
6590 included by the source file represented by PST. Build an include
6591 partial symtab for each of these included files. */
6594 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6595 struct die_info *die,
6596 struct partial_symtab *pst)
6599 struct attribute *attr;
6601 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6603 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6605 return; /* No linetable, so no includes. */
6607 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6608 that we pass in the raw text_low here; that is ok because we're
6609 only decoding the line table to make include partial symtabs, and
6610 so the addresses aren't really used. */
6611 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6612 pst->raw_text_low (), 1);
6616 hash_signatured_type (const void *item)
6618 const struct signatured_type *sig_type
6619 = (const struct signatured_type *) item;
6621 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6622 return sig_type->signature;
6626 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6628 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6629 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6631 return lhs->signature == rhs->signature;
6634 /* Allocate a hash table for signatured types. */
6637 allocate_signatured_type_table (struct objfile *objfile)
6639 return htab_create_alloc_ex (41,
6640 hash_signatured_type,
6643 &objfile->objfile_obstack,
6644 hashtab_obstack_allocate,
6645 dummy_obstack_deallocate);
6648 /* A helper function to add a signatured type CU to a table. */
6651 add_signatured_type_cu_to_table (void **slot, void *datum)
6653 struct signatured_type *sigt = (struct signatured_type *) *slot;
6654 std::vector<signatured_type *> *all_type_units
6655 = (std::vector<signatured_type *> *) datum;
6657 all_type_units->push_back (sigt);
6662 /* A helper for create_debug_types_hash_table. Read types from SECTION
6663 and fill them into TYPES_HTAB. It will process only type units,
6664 therefore DW_UT_type. */
6667 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6668 struct dwo_file *dwo_file,
6669 dwarf2_section_info *section, htab_t &types_htab,
6670 rcuh_kind section_kind)
6672 struct objfile *objfile = dwarf2_per_objfile->objfile;
6673 struct dwarf2_section_info *abbrev_section;
6675 const gdb_byte *info_ptr, *end_ptr;
6677 abbrev_section = (dwo_file != NULL
6678 ? &dwo_file->sections.abbrev
6679 : &dwarf2_per_objfile->abbrev);
6681 if (dwarf_read_debug)
6682 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6683 get_section_name (section),
6684 get_section_file_name (abbrev_section));
6686 dwarf2_read_section (objfile, section);
6687 info_ptr = section->buffer;
6689 if (info_ptr == NULL)
6692 /* We can't set abfd until now because the section may be empty or
6693 not present, in which case the bfd is unknown. */
6694 abfd = get_section_bfd_owner (section);
6696 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6697 because we don't need to read any dies: the signature is in the
6700 end_ptr = info_ptr + section->size;
6701 while (info_ptr < end_ptr)
6703 struct signatured_type *sig_type;
6704 struct dwo_unit *dwo_tu;
6706 const gdb_byte *ptr = info_ptr;
6707 struct comp_unit_head header;
6708 unsigned int length;
6710 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6712 /* Initialize it due to a false compiler warning. */
6713 header.signature = -1;
6714 header.type_cu_offset_in_tu = (cu_offset) -1;
6716 /* We need to read the type's signature in order to build the hash
6717 table, but we don't need anything else just yet. */
6719 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6720 abbrev_section, ptr, section_kind);
6722 length = get_cu_length (&header);
6724 /* Skip dummy type units. */
6725 if (ptr >= info_ptr + length
6726 || peek_abbrev_code (abfd, ptr) == 0
6727 || header.unit_type != DW_UT_type)
6733 if (types_htab == NULL)
6736 types_htab = allocate_dwo_unit_table (objfile);
6738 types_htab = allocate_signatured_type_table (objfile);
6744 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6746 dwo_tu->dwo_file = dwo_file;
6747 dwo_tu->signature = header.signature;
6748 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6749 dwo_tu->section = section;
6750 dwo_tu->sect_off = sect_off;
6751 dwo_tu->length = length;
6755 /* N.B.: type_offset is not usable if this type uses a DWO file.
6756 The real type_offset is in the DWO file. */
6758 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6759 struct signatured_type);
6760 sig_type->signature = header.signature;
6761 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6762 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6763 sig_type->per_cu.is_debug_types = 1;
6764 sig_type->per_cu.section = section;
6765 sig_type->per_cu.sect_off = sect_off;
6766 sig_type->per_cu.length = length;
6769 slot = htab_find_slot (types_htab,
6770 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6772 gdb_assert (slot != NULL);
6775 sect_offset dup_sect_off;
6779 const struct dwo_unit *dup_tu
6780 = (const struct dwo_unit *) *slot;
6782 dup_sect_off = dup_tu->sect_off;
6786 const struct signatured_type *dup_tu
6787 = (const struct signatured_type *) *slot;
6789 dup_sect_off = dup_tu->per_cu.sect_off;
6792 complaint (_("debug type entry at offset %s is duplicate to"
6793 " the entry at offset %s, signature %s"),
6794 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6795 hex_string (header.signature));
6797 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6799 if (dwarf_read_debug > 1)
6800 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6801 sect_offset_str (sect_off),
6802 hex_string (header.signature));
6808 /* Create the hash table of all entries in the .debug_types
6809 (or .debug_types.dwo) section(s).
6810 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6811 otherwise it is NULL.
6813 The result is a pointer to the hash table or NULL if there are no types.
6815 Note: This function processes DWO files only, not DWP files. */
6818 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6819 struct dwo_file *dwo_file,
6820 VEC (dwarf2_section_info_def) *types,
6824 struct dwarf2_section_info *section;
6826 if (VEC_empty (dwarf2_section_info_def, types))
6830 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6832 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6833 types_htab, rcuh_kind::TYPE);
6836 /* Create the hash table of all entries in the .debug_types section,
6837 and initialize all_type_units.
6838 The result is zero if there is an error (e.g. missing .debug_types section),
6839 otherwise non-zero. */
6842 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6844 htab_t types_htab = NULL;
6846 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6847 &dwarf2_per_objfile->info, types_htab,
6848 rcuh_kind::COMPILE);
6849 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6850 dwarf2_per_objfile->types, types_htab);
6851 if (types_htab == NULL)
6853 dwarf2_per_objfile->signatured_types = NULL;
6857 dwarf2_per_objfile->signatured_types = types_htab;
6859 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6860 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6862 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6863 &dwarf2_per_objfile->all_type_units);
6868 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6869 If SLOT is non-NULL, it is the entry to use in the hash table.
6870 Otherwise we find one. */
6872 static struct signatured_type *
6873 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6876 struct objfile *objfile = dwarf2_per_objfile->objfile;
6878 if (dwarf2_per_objfile->all_type_units.size ()
6879 == dwarf2_per_objfile->all_type_units.capacity ())
6880 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6882 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6883 struct signatured_type);
6885 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6886 sig_type->signature = sig;
6887 sig_type->per_cu.is_debug_types = 1;
6888 if (dwarf2_per_objfile->using_index)
6890 sig_type->per_cu.v.quick =
6891 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6892 struct dwarf2_per_cu_quick_data);
6897 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6900 gdb_assert (*slot == NULL);
6902 /* The rest of sig_type must be filled in by the caller. */
6906 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6907 Fill in SIG_ENTRY with DWO_ENTRY. */
6910 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6911 struct signatured_type *sig_entry,
6912 struct dwo_unit *dwo_entry)
6914 /* Make sure we're not clobbering something we don't expect to. */
6915 gdb_assert (! sig_entry->per_cu.queued);
6916 gdb_assert (sig_entry->per_cu.cu == NULL);
6917 if (dwarf2_per_objfile->using_index)
6919 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6920 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6923 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6924 gdb_assert (sig_entry->signature == dwo_entry->signature);
6925 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6926 gdb_assert (sig_entry->type_unit_group == NULL);
6927 gdb_assert (sig_entry->dwo_unit == NULL);
6929 sig_entry->per_cu.section = dwo_entry->section;
6930 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6931 sig_entry->per_cu.length = dwo_entry->length;
6932 sig_entry->per_cu.reading_dwo_directly = 1;
6933 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6934 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6935 sig_entry->dwo_unit = dwo_entry;
6938 /* Subroutine of lookup_signatured_type.
6939 If we haven't read the TU yet, create the signatured_type data structure
6940 for a TU to be read in directly from a DWO file, bypassing the stub.
6941 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6942 using .gdb_index, then when reading a CU we want to stay in the DWO file
6943 containing that CU. Otherwise we could end up reading several other DWO
6944 files (due to comdat folding) to process the transitive closure of all the
6945 mentioned TUs, and that can be slow. The current DWO file will have every
6946 type signature that it needs.
6947 We only do this for .gdb_index because in the psymtab case we already have
6948 to read all the DWOs to build the type unit groups. */
6950 static struct signatured_type *
6951 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6953 struct dwarf2_per_objfile *dwarf2_per_objfile
6954 = cu->per_cu->dwarf2_per_objfile;
6955 struct objfile *objfile = dwarf2_per_objfile->objfile;
6956 struct dwo_file *dwo_file;
6957 struct dwo_unit find_dwo_entry, *dwo_entry;
6958 struct signatured_type find_sig_entry, *sig_entry;
6961 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6963 /* If TU skeletons have been removed then we may not have read in any
6965 if (dwarf2_per_objfile->signatured_types == NULL)
6967 dwarf2_per_objfile->signatured_types
6968 = allocate_signatured_type_table (objfile);
6971 /* We only ever need to read in one copy of a signatured type.
6972 Use the global signatured_types array to do our own comdat-folding
6973 of types. If this is the first time we're reading this TU, and
6974 the TU has an entry in .gdb_index, replace the recorded data from
6975 .gdb_index with this TU. */
6977 find_sig_entry.signature = sig;
6978 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6979 &find_sig_entry, INSERT);
6980 sig_entry = (struct signatured_type *) *slot;
6982 /* We can get here with the TU already read, *or* in the process of being
6983 read. Don't reassign the global entry to point to this DWO if that's
6984 the case. Also note that if the TU is already being read, it may not
6985 have come from a DWO, the program may be a mix of Fission-compiled
6986 code and non-Fission-compiled code. */
6988 /* Have we already tried to read this TU?
6989 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6990 needn't exist in the global table yet). */
6991 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6994 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6995 dwo_unit of the TU itself. */
6996 dwo_file = cu->dwo_unit->dwo_file;
6998 /* Ok, this is the first time we're reading this TU. */
6999 if (dwo_file->tus == NULL)
7001 find_dwo_entry.signature = sig;
7002 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7003 if (dwo_entry == NULL)
7006 /* If the global table doesn't have an entry for this TU, add one. */
7007 if (sig_entry == NULL)
7008 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7010 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7011 sig_entry->per_cu.tu_read = 1;
7015 /* Subroutine of lookup_signatured_type.
7016 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7017 then try the DWP file. If the TU stub (skeleton) has been removed then
7018 it won't be in .gdb_index. */
7020 static struct signatured_type *
7021 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7023 struct dwarf2_per_objfile *dwarf2_per_objfile
7024 = cu->per_cu->dwarf2_per_objfile;
7025 struct objfile *objfile = dwarf2_per_objfile->objfile;
7026 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7027 struct dwo_unit *dwo_entry;
7028 struct signatured_type find_sig_entry, *sig_entry;
7031 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7032 gdb_assert (dwp_file != NULL);
7034 /* If TU skeletons have been removed then we may not have read in any
7036 if (dwarf2_per_objfile->signatured_types == NULL)
7038 dwarf2_per_objfile->signatured_types
7039 = allocate_signatured_type_table (objfile);
7042 find_sig_entry.signature = sig;
7043 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7044 &find_sig_entry, INSERT);
7045 sig_entry = (struct signatured_type *) *slot;
7047 /* Have we already tried to read this TU?
7048 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7049 needn't exist in the global table yet). */
7050 if (sig_entry != NULL)
7053 if (dwp_file->tus == NULL)
7055 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7056 sig, 1 /* is_debug_types */);
7057 if (dwo_entry == NULL)
7060 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7061 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7066 /* Lookup a signature based type for DW_FORM_ref_sig8.
7067 Returns NULL if signature SIG is not present in the table.
7068 It is up to the caller to complain about this. */
7070 static struct signatured_type *
7071 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7073 struct dwarf2_per_objfile *dwarf2_per_objfile
7074 = cu->per_cu->dwarf2_per_objfile;
7077 && dwarf2_per_objfile->using_index)
7079 /* We're in a DWO/DWP file, and we're using .gdb_index.
7080 These cases require special processing. */
7081 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7082 return lookup_dwo_signatured_type (cu, sig);
7084 return lookup_dwp_signatured_type (cu, sig);
7088 struct signatured_type find_entry, *entry;
7090 if (dwarf2_per_objfile->signatured_types == NULL)
7092 find_entry.signature = sig;
7093 entry = ((struct signatured_type *)
7094 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7099 /* Low level DIE reading support. */
7101 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7104 init_cu_die_reader (struct die_reader_specs *reader,
7105 struct dwarf2_cu *cu,
7106 struct dwarf2_section_info *section,
7107 struct dwo_file *dwo_file,
7108 struct abbrev_table *abbrev_table)
7110 gdb_assert (section->readin && section->buffer != NULL);
7111 reader->abfd = get_section_bfd_owner (section);
7113 reader->dwo_file = dwo_file;
7114 reader->die_section = section;
7115 reader->buffer = section->buffer;
7116 reader->buffer_end = section->buffer + section->size;
7117 reader->comp_dir = NULL;
7118 reader->abbrev_table = abbrev_table;
7121 /* Subroutine of init_cutu_and_read_dies to simplify it.
7122 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7123 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7126 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7127 from it to the DIE in the DWO. If NULL we are skipping the stub.
7128 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7129 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7130 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7131 STUB_COMP_DIR may be non-NULL.
7132 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7133 are filled in with the info of the DIE from the DWO file.
7134 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7135 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7136 kept around for at least as long as *RESULT_READER.
7138 The result is non-zero if a valid (non-dummy) DIE was found. */
7141 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7142 struct dwo_unit *dwo_unit,
7143 struct die_info *stub_comp_unit_die,
7144 const char *stub_comp_dir,
7145 struct die_reader_specs *result_reader,
7146 const gdb_byte **result_info_ptr,
7147 struct die_info **result_comp_unit_die,
7148 int *result_has_children,
7149 abbrev_table_up *result_dwo_abbrev_table)
7151 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7152 struct objfile *objfile = dwarf2_per_objfile->objfile;
7153 struct dwarf2_cu *cu = this_cu->cu;
7155 const gdb_byte *begin_info_ptr, *info_ptr;
7156 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7157 int i,num_extra_attrs;
7158 struct dwarf2_section_info *dwo_abbrev_section;
7159 struct attribute *attr;
7160 struct die_info *comp_unit_die;
7162 /* At most one of these may be provided. */
7163 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7165 /* These attributes aren't processed until later:
7166 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7167 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7168 referenced later. However, these attributes are found in the stub
7169 which we won't have later. In order to not impose this complication
7170 on the rest of the code, we read them here and copy them to the
7179 if (stub_comp_unit_die != NULL)
7181 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7183 if (! this_cu->is_debug_types)
7184 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7185 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7186 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7187 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7188 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7190 /* There should be a DW_AT_addr_base attribute here (if needed).
7191 We need the value before we can process DW_FORM_GNU_addr_index
7192 or DW_FORM_addrx. */
7194 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7196 cu->addr_base = DW_UNSND (attr);
7198 /* There should be a DW_AT_ranges_base attribute here (if needed).
7199 We need the value before we can process DW_AT_ranges. */
7200 cu->ranges_base = 0;
7201 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7203 cu->ranges_base = DW_UNSND (attr);
7205 else if (stub_comp_dir != NULL)
7207 /* Reconstruct the comp_dir attribute to simplify the code below. */
7208 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7209 comp_dir->name = DW_AT_comp_dir;
7210 comp_dir->form = DW_FORM_string;
7211 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7212 DW_STRING (comp_dir) = stub_comp_dir;
7215 /* Set up for reading the DWO CU/TU. */
7216 cu->dwo_unit = dwo_unit;
7217 dwarf2_section_info *section = dwo_unit->section;
7218 dwarf2_read_section (objfile, section);
7219 abfd = get_section_bfd_owner (section);
7220 begin_info_ptr = info_ptr = (section->buffer
7221 + to_underlying (dwo_unit->sect_off));
7222 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7224 if (this_cu->is_debug_types)
7226 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7228 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7229 &cu->header, section,
7231 info_ptr, rcuh_kind::TYPE);
7232 /* This is not an assert because it can be caused by bad debug info. */
7233 if (sig_type->signature != cu->header.signature)
7235 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7236 " TU at offset %s [in module %s]"),
7237 hex_string (sig_type->signature),
7238 hex_string (cu->header.signature),
7239 sect_offset_str (dwo_unit->sect_off),
7240 bfd_get_filename (abfd));
7242 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7243 /* For DWOs coming from DWP files, we don't know the CU length
7244 nor the type's offset in the TU until now. */
7245 dwo_unit->length = get_cu_length (&cu->header);
7246 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7248 /* Establish the type offset that can be used to lookup the type.
7249 For DWO files, we don't know it until now. */
7250 sig_type->type_offset_in_section
7251 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7255 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7256 &cu->header, section,
7258 info_ptr, rcuh_kind::COMPILE);
7259 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7260 /* For DWOs coming from DWP files, we don't know the CU length
7262 dwo_unit->length = get_cu_length (&cu->header);
7265 *result_dwo_abbrev_table
7266 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7267 cu->header.abbrev_sect_off);
7268 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7269 result_dwo_abbrev_table->get ());
7271 /* Read in the die, but leave space to copy over the attributes
7272 from the stub. This has the benefit of simplifying the rest of
7273 the code - all the work to maintain the illusion of a single
7274 DW_TAG_{compile,type}_unit DIE is done here. */
7275 num_extra_attrs = ((stmt_list != NULL)
7279 + (comp_dir != NULL));
7280 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7281 result_has_children, num_extra_attrs);
7283 /* Copy over the attributes from the stub to the DIE we just read in. */
7284 comp_unit_die = *result_comp_unit_die;
7285 i = comp_unit_die->num_attrs;
7286 if (stmt_list != NULL)
7287 comp_unit_die->attrs[i++] = *stmt_list;
7289 comp_unit_die->attrs[i++] = *low_pc;
7290 if (high_pc != NULL)
7291 comp_unit_die->attrs[i++] = *high_pc;
7293 comp_unit_die->attrs[i++] = *ranges;
7294 if (comp_dir != NULL)
7295 comp_unit_die->attrs[i++] = *comp_dir;
7296 comp_unit_die->num_attrs += num_extra_attrs;
7298 if (dwarf_die_debug)
7300 fprintf_unfiltered (gdb_stdlog,
7301 "Read die from %s@0x%x of %s:\n",
7302 get_section_name (section),
7303 (unsigned) (begin_info_ptr - section->buffer),
7304 bfd_get_filename (abfd));
7305 dump_die (comp_unit_die, dwarf_die_debug);
7308 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7309 TUs by skipping the stub and going directly to the entry in the DWO file.
7310 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7311 to get it via circuitous means. Blech. */
7312 if (comp_dir != NULL)
7313 result_reader->comp_dir = DW_STRING (comp_dir);
7315 /* Skip dummy compilation units. */
7316 if (info_ptr >= begin_info_ptr + dwo_unit->length
7317 || peek_abbrev_code (abfd, info_ptr) == 0)
7320 *result_info_ptr = info_ptr;
7324 /* Subroutine of init_cutu_and_read_dies to simplify it.
7325 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7326 Returns NULL if the specified DWO unit cannot be found. */
7328 static struct dwo_unit *
7329 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7330 struct die_info *comp_unit_die)
7332 struct dwarf2_cu *cu = this_cu->cu;
7334 struct dwo_unit *dwo_unit;
7335 const char *comp_dir, *dwo_name;
7337 gdb_assert (cu != NULL);
7339 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7340 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7341 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7343 if (this_cu->is_debug_types)
7345 struct signatured_type *sig_type;
7347 /* Since this_cu is the first member of struct signatured_type,
7348 we can go from a pointer to one to a pointer to the other. */
7349 sig_type = (struct signatured_type *) this_cu;
7350 signature = sig_type->signature;
7351 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7355 struct attribute *attr;
7357 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7359 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7361 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7362 signature = DW_UNSND (attr);
7363 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7370 /* Subroutine of init_cutu_and_read_dies to simplify it.
7371 See it for a description of the parameters.
7372 Read a TU directly from a DWO file, bypassing the stub. */
7375 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7376 int use_existing_cu, int keep,
7377 die_reader_func_ftype *die_reader_func,
7380 std::unique_ptr<dwarf2_cu> new_cu;
7381 struct signatured_type *sig_type;
7382 struct die_reader_specs reader;
7383 const gdb_byte *info_ptr;
7384 struct die_info *comp_unit_die;
7386 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7388 /* Verify we can do the following downcast, and that we have the
7390 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7391 sig_type = (struct signatured_type *) this_cu;
7392 gdb_assert (sig_type->dwo_unit != NULL);
7394 if (use_existing_cu && this_cu->cu != NULL)
7396 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7397 /* There's no need to do the rereading_dwo_cu handling that
7398 init_cutu_and_read_dies does since we don't read the stub. */
7402 /* If !use_existing_cu, this_cu->cu must be NULL. */
7403 gdb_assert (this_cu->cu == NULL);
7404 new_cu.reset (new dwarf2_cu (this_cu));
7407 /* A future optimization, if needed, would be to use an existing
7408 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7409 could share abbrev tables. */
7411 /* The abbreviation table used by READER, this must live at least as long as
7413 abbrev_table_up dwo_abbrev_table;
7415 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7416 NULL /* stub_comp_unit_die */,
7417 sig_type->dwo_unit->dwo_file->comp_dir,
7419 &comp_unit_die, &has_children,
7420 &dwo_abbrev_table) == 0)
7426 /* All the "real" work is done here. */
7427 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7429 /* This duplicates the code in init_cutu_and_read_dies,
7430 but the alternative is making the latter more complex.
7431 This function is only for the special case of using DWO files directly:
7432 no point in overly complicating the general case just to handle this. */
7433 if (new_cu != NULL && keep)
7435 /* Link this CU into read_in_chain. */
7436 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7437 dwarf2_per_objfile->read_in_chain = this_cu;
7438 /* The chain owns it now. */
7443 /* Initialize a CU (or TU) and read its DIEs.
7444 If the CU defers to a DWO file, read the DWO file as well.
7446 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7447 Otherwise the table specified in the comp unit header is read in and used.
7448 This is an optimization for when we already have the abbrev table.
7450 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7451 Otherwise, a new CU is allocated with xmalloc.
7453 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7454 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7456 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7457 linker) then DIE_READER_FUNC will not get called. */
7460 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7461 struct abbrev_table *abbrev_table,
7462 int use_existing_cu, int keep,
7464 die_reader_func_ftype *die_reader_func,
7467 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7468 struct objfile *objfile = dwarf2_per_objfile->objfile;
7469 struct dwarf2_section_info *section = this_cu->section;
7470 bfd *abfd = get_section_bfd_owner (section);
7471 struct dwarf2_cu *cu;
7472 const gdb_byte *begin_info_ptr, *info_ptr;
7473 struct die_reader_specs reader;
7474 struct die_info *comp_unit_die;
7476 struct attribute *attr;
7477 struct signatured_type *sig_type = NULL;
7478 struct dwarf2_section_info *abbrev_section;
7479 /* Non-zero if CU currently points to a DWO file and we need to
7480 reread it. When this happens we need to reread the skeleton die
7481 before we can reread the DWO file (this only applies to CUs, not TUs). */
7482 int rereading_dwo_cu = 0;
7484 if (dwarf_die_debug)
7485 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7486 this_cu->is_debug_types ? "type" : "comp",
7487 sect_offset_str (this_cu->sect_off));
7489 if (use_existing_cu)
7492 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7493 file (instead of going through the stub), short-circuit all of this. */
7494 if (this_cu->reading_dwo_directly)
7496 /* Narrow down the scope of possibilities to have to understand. */
7497 gdb_assert (this_cu->is_debug_types);
7498 gdb_assert (abbrev_table == NULL);
7499 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7500 die_reader_func, data);
7504 /* This is cheap if the section is already read in. */
7505 dwarf2_read_section (objfile, section);
7507 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7509 abbrev_section = get_abbrev_section_for_cu (this_cu);
7511 std::unique_ptr<dwarf2_cu> new_cu;
7512 if (use_existing_cu && this_cu->cu != NULL)
7515 /* If this CU is from a DWO file we need to start over, we need to
7516 refetch the attributes from the skeleton CU.
7517 This could be optimized by retrieving those attributes from when we
7518 were here the first time: the previous comp_unit_die was stored in
7519 comp_unit_obstack. But there's no data yet that we need this
7521 if (cu->dwo_unit != NULL)
7522 rereading_dwo_cu = 1;
7526 /* If !use_existing_cu, this_cu->cu must be NULL. */
7527 gdb_assert (this_cu->cu == NULL);
7528 new_cu.reset (new dwarf2_cu (this_cu));
7532 /* Get the header. */
7533 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7535 /* We already have the header, there's no need to read it in again. */
7536 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7540 if (this_cu->is_debug_types)
7542 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7543 &cu->header, section,
7544 abbrev_section, info_ptr,
7547 /* Since per_cu is the first member of struct signatured_type,
7548 we can go from a pointer to one to a pointer to the other. */
7549 sig_type = (struct signatured_type *) this_cu;
7550 gdb_assert (sig_type->signature == cu->header.signature);
7551 gdb_assert (sig_type->type_offset_in_tu
7552 == cu->header.type_cu_offset_in_tu);
7553 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7555 /* LENGTH has not been set yet for type units if we're
7556 using .gdb_index. */
7557 this_cu->length = get_cu_length (&cu->header);
7559 /* Establish the type offset that can be used to lookup the type. */
7560 sig_type->type_offset_in_section =
7561 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7563 this_cu->dwarf_version = cu->header.version;
7567 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7568 &cu->header, section,
7571 rcuh_kind::COMPILE);
7573 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7574 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7575 this_cu->dwarf_version = cu->header.version;
7579 /* Skip dummy compilation units. */
7580 if (info_ptr >= begin_info_ptr + this_cu->length
7581 || peek_abbrev_code (abfd, info_ptr) == 0)
7584 /* If we don't have them yet, read the abbrevs for this compilation unit.
7585 And if we need to read them now, make sure they're freed when we're
7586 done (own the table through ABBREV_TABLE_HOLDER). */
7587 abbrev_table_up abbrev_table_holder;
7588 if (abbrev_table != NULL)
7589 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7593 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7594 cu->header.abbrev_sect_off);
7595 abbrev_table = abbrev_table_holder.get ();
7598 /* Read the top level CU/TU die. */
7599 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7600 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7602 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7605 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7606 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7607 table from the DWO file and pass the ownership over to us. It will be
7608 referenced from READER, so we must make sure to free it after we're done
7611 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7612 DWO CU, that this test will fail (the attribute will not be present). */
7613 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7614 abbrev_table_up dwo_abbrev_table;
7617 struct dwo_unit *dwo_unit;
7618 struct die_info *dwo_comp_unit_die;
7622 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7623 " has children (offset %s) [in module %s]"),
7624 sect_offset_str (this_cu->sect_off),
7625 bfd_get_filename (abfd));
7627 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7628 if (dwo_unit != NULL)
7630 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7631 comp_unit_die, NULL,
7633 &dwo_comp_unit_die, &has_children,
7634 &dwo_abbrev_table) == 0)
7639 comp_unit_die = dwo_comp_unit_die;
7643 /* Yikes, we couldn't find the rest of the DIE, we only have
7644 the stub. A complaint has already been logged. There's
7645 not much more we can do except pass on the stub DIE to
7646 die_reader_func. We don't want to throw an error on bad
7651 /* All of the above is setup for this call. Yikes. */
7652 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7654 /* Done, clean up. */
7655 if (new_cu != NULL && keep)
7657 /* Link this CU into read_in_chain. */
7658 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7659 dwarf2_per_objfile->read_in_chain = this_cu;
7660 /* The chain owns it now. */
7665 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7666 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7667 to have already done the lookup to find the DWO file).
7669 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7670 THIS_CU->is_debug_types, but nothing else.
7672 We fill in THIS_CU->length.
7674 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7675 linker) then DIE_READER_FUNC will not get called.
7677 THIS_CU->cu is always freed when done.
7678 This is done in order to not leave THIS_CU->cu in a state where we have
7679 to care whether it refers to the "main" CU or the DWO CU. */
7682 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7683 struct dwo_file *dwo_file,
7684 die_reader_func_ftype *die_reader_func,
7687 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7688 struct objfile *objfile = dwarf2_per_objfile->objfile;
7689 struct dwarf2_section_info *section = this_cu->section;
7690 bfd *abfd = get_section_bfd_owner (section);
7691 struct dwarf2_section_info *abbrev_section;
7692 const gdb_byte *begin_info_ptr, *info_ptr;
7693 struct die_reader_specs reader;
7694 struct die_info *comp_unit_die;
7697 if (dwarf_die_debug)
7698 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7699 this_cu->is_debug_types ? "type" : "comp",
7700 sect_offset_str (this_cu->sect_off));
7702 gdb_assert (this_cu->cu == NULL);
7704 abbrev_section = (dwo_file != NULL
7705 ? &dwo_file->sections.abbrev
7706 : get_abbrev_section_for_cu (this_cu));
7708 /* This is cheap if the section is already read in. */
7709 dwarf2_read_section (objfile, section);
7711 struct dwarf2_cu cu (this_cu);
7713 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7714 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7715 &cu.header, section,
7716 abbrev_section, info_ptr,
7717 (this_cu->is_debug_types
7719 : rcuh_kind::COMPILE));
7721 this_cu->length = get_cu_length (&cu.header);
7723 /* Skip dummy compilation units. */
7724 if (info_ptr >= begin_info_ptr + this_cu->length
7725 || peek_abbrev_code (abfd, info_ptr) == 0)
7728 abbrev_table_up abbrev_table
7729 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7730 cu.header.abbrev_sect_off);
7732 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7733 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7735 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7738 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7739 does not lookup the specified DWO file.
7740 This cannot be used to read DWO files.
7742 THIS_CU->cu is always freed when done.
7743 This is done in order to not leave THIS_CU->cu in a state where we have
7744 to care whether it refers to the "main" CU or the DWO CU.
7745 We can revisit this if the data shows there's a performance issue. */
7748 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7749 die_reader_func_ftype *die_reader_func,
7752 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7755 /* Type Unit Groups.
7757 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7758 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7759 so that all types coming from the same compilation (.o file) are grouped
7760 together. A future step could be to put the types in the same symtab as
7761 the CU the types ultimately came from. */
7764 hash_type_unit_group (const void *item)
7766 const struct type_unit_group *tu_group
7767 = (const struct type_unit_group *) item;
7769 return hash_stmt_list_entry (&tu_group->hash);
7773 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7775 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7776 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7778 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7781 /* Allocate a hash table for type unit groups. */
7784 allocate_type_unit_groups_table (struct objfile *objfile)
7786 return htab_create_alloc_ex (3,
7787 hash_type_unit_group,
7790 &objfile->objfile_obstack,
7791 hashtab_obstack_allocate,
7792 dummy_obstack_deallocate);
7795 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7796 partial symtabs. We combine several TUs per psymtab to not let the size
7797 of any one psymtab grow too big. */
7798 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7799 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7801 /* Helper routine for get_type_unit_group.
7802 Create the type_unit_group object used to hold one or more TUs. */
7804 static struct type_unit_group *
7805 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7807 struct dwarf2_per_objfile *dwarf2_per_objfile
7808 = cu->per_cu->dwarf2_per_objfile;
7809 struct objfile *objfile = dwarf2_per_objfile->objfile;
7810 struct dwarf2_per_cu_data *per_cu;
7811 struct type_unit_group *tu_group;
7813 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7814 struct type_unit_group);
7815 per_cu = &tu_group->per_cu;
7816 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7818 if (dwarf2_per_objfile->using_index)
7820 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7821 struct dwarf2_per_cu_quick_data);
7825 unsigned int line_offset = to_underlying (line_offset_struct);
7826 struct partial_symtab *pst;
7829 /* Give the symtab a useful name for debug purposes. */
7830 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7831 name = string_printf ("<type_units_%d>",
7832 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7834 name = string_printf ("<type_units_at_0x%x>", line_offset);
7836 pst = create_partial_symtab (per_cu, name.c_str ());
7840 tu_group->hash.dwo_unit = cu->dwo_unit;
7841 tu_group->hash.line_sect_off = line_offset_struct;
7846 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7847 STMT_LIST is a DW_AT_stmt_list attribute. */
7849 static struct type_unit_group *
7850 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7852 struct dwarf2_per_objfile *dwarf2_per_objfile
7853 = cu->per_cu->dwarf2_per_objfile;
7854 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7855 struct type_unit_group *tu_group;
7857 unsigned int line_offset;
7858 struct type_unit_group type_unit_group_for_lookup;
7860 if (dwarf2_per_objfile->type_unit_groups == NULL)
7862 dwarf2_per_objfile->type_unit_groups =
7863 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7866 /* Do we need to create a new group, or can we use an existing one? */
7870 line_offset = DW_UNSND (stmt_list);
7871 ++tu_stats->nr_symtab_sharers;
7875 /* Ugh, no stmt_list. Rare, but we have to handle it.
7876 We can do various things here like create one group per TU or
7877 spread them over multiple groups to split up the expansion work.
7878 To avoid worst case scenarios (too many groups or too large groups)
7879 we, umm, group them in bunches. */
7880 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7881 | (tu_stats->nr_stmt_less_type_units
7882 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7883 ++tu_stats->nr_stmt_less_type_units;
7886 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7887 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7888 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7889 &type_unit_group_for_lookup, INSERT);
7892 tu_group = (struct type_unit_group *) *slot;
7893 gdb_assert (tu_group != NULL);
7897 sect_offset line_offset_struct = (sect_offset) line_offset;
7898 tu_group = create_type_unit_group (cu, line_offset_struct);
7900 ++tu_stats->nr_symtabs;
7906 /* Partial symbol tables. */
7908 /* Create a psymtab named NAME and assign it to PER_CU.
7910 The caller must fill in the following details:
7911 dirname, textlow, texthigh. */
7913 static struct partial_symtab *
7914 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7916 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7917 struct partial_symtab *pst;
7919 pst = start_psymtab_common (objfile, name, 0);
7921 pst->psymtabs_addrmap_supported = 1;
7923 /* This is the glue that links PST into GDB's symbol API. */
7924 pst->read_symtab_private = per_cu;
7925 pst->read_symtab = dwarf2_read_symtab;
7926 per_cu->v.psymtab = pst;
7931 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7934 struct process_psymtab_comp_unit_data
7936 /* True if we are reading a DW_TAG_partial_unit. */
7938 int want_partial_unit;
7940 /* The "pretend" language that is used if the CU doesn't declare a
7943 enum language pretend_language;
7946 /* die_reader_func for process_psymtab_comp_unit. */
7949 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7950 const gdb_byte *info_ptr,
7951 struct die_info *comp_unit_die,
7955 struct dwarf2_cu *cu = reader->cu;
7956 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7957 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7958 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7960 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7961 struct partial_symtab *pst;
7962 enum pc_bounds_kind cu_bounds_kind;
7963 const char *filename;
7964 struct process_psymtab_comp_unit_data *info
7965 = (struct process_psymtab_comp_unit_data *) data;
7967 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7970 gdb_assert (! per_cu->is_debug_types);
7972 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7974 /* Allocate a new partial symbol table structure. */
7975 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7976 if (filename == NULL)
7979 pst = create_partial_symtab (per_cu, filename);
7981 /* This must be done before calling dwarf2_build_include_psymtabs. */
7982 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7984 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7986 dwarf2_find_base_address (comp_unit_die, cu);
7988 /* Possibly set the default values of LOWPC and HIGHPC from
7990 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7991 &best_highpc, cu, pst);
7992 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7995 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7998 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8000 /* Store the contiguous range if it is not empty; it can be
8001 empty for CUs with no code. */
8002 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8006 /* Check if comp unit has_children.
8007 If so, read the rest of the partial symbols from this comp unit.
8008 If not, there's no more debug_info for this comp unit. */
8011 struct partial_die_info *first_die;
8012 CORE_ADDR lowpc, highpc;
8014 lowpc = ((CORE_ADDR) -1);
8015 highpc = ((CORE_ADDR) 0);
8017 first_die = load_partial_dies (reader, info_ptr, 1);
8019 scan_partial_symbols (first_die, &lowpc, &highpc,
8020 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8022 /* If we didn't find a lowpc, set it to highpc to avoid
8023 complaints from `maint check'. */
8024 if (lowpc == ((CORE_ADDR) -1))
8027 /* If the compilation unit didn't have an explicit address range,
8028 then use the information extracted from its child dies. */
8029 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8032 best_highpc = highpc;
8035 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8036 best_lowpc + baseaddr)
8038 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8039 best_highpc + baseaddr)
8042 end_psymtab_common (objfile, pst);
8044 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8047 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8048 struct dwarf2_per_cu_data *iter;
8050 /* Fill in 'dependencies' here; we fill in 'users' in a
8052 pst->number_of_dependencies = len;
8054 = objfile->partial_symtabs->allocate_dependencies (len);
8056 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8059 pst->dependencies[i] = iter->v.psymtab;
8061 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8064 /* Get the list of files included in the current compilation unit,
8065 and build a psymtab for each of them. */
8066 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8068 if (dwarf_read_debug)
8069 fprintf_unfiltered (gdb_stdlog,
8070 "Psymtab for %s unit @%s: %s - %s"
8071 ", %d global, %d static syms\n",
8072 per_cu->is_debug_types ? "type" : "comp",
8073 sect_offset_str (per_cu->sect_off),
8074 paddress (gdbarch, pst->text_low (objfile)),
8075 paddress (gdbarch, pst->text_high (objfile)),
8076 pst->n_global_syms, pst->n_static_syms);
8079 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8080 Process compilation unit THIS_CU for a psymtab. */
8083 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8084 int want_partial_unit,
8085 enum language pretend_language)
8087 /* If this compilation unit was already read in, free the
8088 cached copy in order to read it in again. This is
8089 necessary because we skipped some symbols when we first
8090 read in the compilation unit (see load_partial_dies).
8091 This problem could be avoided, but the benefit is unclear. */
8092 if (this_cu->cu != NULL)
8093 free_one_cached_comp_unit (this_cu);
8095 if (this_cu->is_debug_types)
8096 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8097 build_type_psymtabs_reader, NULL);
8100 process_psymtab_comp_unit_data info;
8101 info.want_partial_unit = want_partial_unit;
8102 info.pretend_language = pretend_language;
8103 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8104 process_psymtab_comp_unit_reader, &info);
8107 /* Age out any secondary CUs. */
8108 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8111 /* Reader function for build_type_psymtabs. */
8114 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8115 const gdb_byte *info_ptr,
8116 struct die_info *type_unit_die,
8120 struct dwarf2_per_objfile *dwarf2_per_objfile
8121 = reader->cu->per_cu->dwarf2_per_objfile;
8122 struct objfile *objfile = dwarf2_per_objfile->objfile;
8123 struct dwarf2_cu *cu = reader->cu;
8124 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8125 struct signatured_type *sig_type;
8126 struct type_unit_group *tu_group;
8127 struct attribute *attr;
8128 struct partial_die_info *first_die;
8129 CORE_ADDR lowpc, highpc;
8130 struct partial_symtab *pst;
8132 gdb_assert (data == NULL);
8133 gdb_assert (per_cu->is_debug_types);
8134 sig_type = (struct signatured_type *) per_cu;
8139 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8140 tu_group = get_type_unit_group (cu, attr);
8142 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8144 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8145 pst = create_partial_symtab (per_cu, "");
8148 first_die = load_partial_dies (reader, info_ptr, 1);
8150 lowpc = (CORE_ADDR) -1;
8151 highpc = (CORE_ADDR) 0;
8152 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8154 end_psymtab_common (objfile, pst);
8157 /* Struct used to sort TUs by their abbreviation table offset. */
8159 struct tu_abbrev_offset
8161 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8162 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8165 signatured_type *sig_type;
8166 sect_offset abbrev_offset;
8169 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8172 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8173 const struct tu_abbrev_offset &b)
8175 return a.abbrev_offset < b.abbrev_offset;
8178 /* Efficiently read all the type units.
8179 This does the bulk of the work for build_type_psymtabs.
8181 The efficiency is because we sort TUs by the abbrev table they use and
8182 only read each abbrev table once. In one program there are 200K TUs
8183 sharing 8K abbrev tables.
8185 The main purpose of this function is to support building the
8186 dwarf2_per_objfile->type_unit_groups table.
8187 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8188 can collapse the search space by grouping them by stmt_list.
8189 The savings can be significant, in the same program from above the 200K TUs
8190 share 8K stmt_list tables.
8192 FUNC is expected to call get_type_unit_group, which will create the
8193 struct type_unit_group if necessary and add it to
8194 dwarf2_per_objfile->type_unit_groups. */
8197 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8199 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8200 abbrev_table_up abbrev_table;
8201 sect_offset abbrev_offset;
8203 /* It's up to the caller to not call us multiple times. */
8204 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8206 if (dwarf2_per_objfile->all_type_units.empty ())
8209 /* TUs typically share abbrev tables, and there can be way more TUs than
8210 abbrev tables. Sort by abbrev table to reduce the number of times we
8211 read each abbrev table in.
8212 Alternatives are to punt or to maintain a cache of abbrev tables.
8213 This is simpler and efficient enough for now.
8215 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8216 symtab to use). Typically TUs with the same abbrev offset have the same
8217 stmt_list value too so in practice this should work well.
8219 The basic algorithm here is:
8221 sort TUs by abbrev table
8222 for each TU with same abbrev table:
8223 read abbrev table if first user
8224 read TU top level DIE
8225 [IWBN if DWO skeletons had DW_AT_stmt_list]
8228 if (dwarf_read_debug)
8229 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8231 /* Sort in a separate table to maintain the order of all_type_units
8232 for .gdb_index: TU indices directly index all_type_units. */
8233 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8234 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8236 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8237 sorted_by_abbrev.emplace_back
8238 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8239 sig_type->per_cu.section,
8240 sig_type->per_cu.sect_off));
8242 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8243 sort_tu_by_abbrev_offset);
8245 abbrev_offset = (sect_offset) ~(unsigned) 0;
8247 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8249 /* Switch to the next abbrev table if necessary. */
8250 if (abbrev_table == NULL
8251 || tu.abbrev_offset != abbrev_offset)
8253 abbrev_offset = tu.abbrev_offset;
8255 abbrev_table_read_table (dwarf2_per_objfile,
8256 &dwarf2_per_objfile->abbrev,
8258 ++tu_stats->nr_uniq_abbrev_tables;
8261 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8262 0, 0, false, build_type_psymtabs_reader, NULL);
8266 /* Print collected type unit statistics. */
8269 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8271 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8273 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8274 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8275 dwarf2_per_objfile->all_type_units.size ());
8276 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8277 tu_stats->nr_uniq_abbrev_tables);
8278 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8279 tu_stats->nr_symtabs);
8280 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8281 tu_stats->nr_symtab_sharers);
8282 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8283 tu_stats->nr_stmt_less_type_units);
8284 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8285 tu_stats->nr_all_type_units_reallocs);
8288 /* Traversal function for build_type_psymtabs. */
8291 build_type_psymtab_dependencies (void **slot, void *info)
8293 struct dwarf2_per_objfile *dwarf2_per_objfile
8294 = (struct dwarf2_per_objfile *) info;
8295 struct objfile *objfile = dwarf2_per_objfile->objfile;
8296 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8297 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8298 struct partial_symtab *pst = per_cu->v.psymtab;
8299 int len = VEC_length (sig_type_ptr, tu_group->tus);
8300 struct signatured_type *iter;
8303 gdb_assert (len > 0);
8304 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8306 pst->number_of_dependencies = len;
8307 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8309 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8312 gdb_assert (iter->per_cu.is_debug_types);
8313 pst->dependencies[i] = iter->per_cu.v.psymtab;
8314 iter->type_unit_group = tu_group;
8317 VEC_free (sig_type_ptr, tu_group->tus);
8322 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8323 Build partial symbol tables for the .debug_types comp-units. */
8326 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8328 if (! create_all_type_units (dwarf2_per_objfile))
8331 build_type_psymtabs_1 (dwarf2_per_objfile);
8334 /* Traversal function for process_skeletonless_type_unit.
8335 Read a TU in a DWO file and build partial symbols for it. */
8338 process_skeletonless_type_unit (void **slot, void *info)
8340 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8341 struct dwarf2_per_objfile *dwarf2_per_objfile
8342 = (struct dwarf2_per_objfile *) info;
8343 struct signatured_type find_entry, *entry;
8345 /* If this TU doesn't exist in the global table, add it and read it in. */
8347 if (dwarf2_per_objfile->signatured_types == NULL)
8349 dwarf2_per_objfile->signatured_types
8350 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8353 find_entry.signature = dwo_unit->signature;
8354 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8356 /* If we've already seen this type there's nothing to do. What's happening
8357 is we're doing our own version of comdat-folding here. */
8361 /* This does the job that create_all_type_units would have done for
8363 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8364 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8367 /* This does the job that build_type_psymtabs_1 would have done. */
8368 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8369 build_type_psymtabs_reader, NULL);
8374 /* Traversal function for process_skeletonless_type_units. */
8377 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8379 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8381 if (dwo_file->tus != NULL)
8383 htab_traverse_noresize (dwo_file->tus,
8384 process_skeletonless_type_unit, info);
8390 /* Scan all TUs of DWO files, verifying we've processed them.
8391 This is needed in case a TU was emitted without its skeleton.
8392 Note: This can't be done until we know what all the DWO files are. */
8395 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8397 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8398 if (get_dwp_file (dwarf2_per_objfile) == NULL
8399 && dwarf2_per_objfile->dwo_files != NULL)
8401 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8402 process_dwo_file_for_skeletonless_type_units,
8403 dwarf2_per_objfile);
8407 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8410 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8412 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8414 struct partial_symtab *pst = per_cu->v.psymtab;
8419 for (int j = 0; j < pst->number_of_dependencies; ++j)
8421 /* Set the 'user' field only if it is not already set. */
8422 if (pst->dependencies[j]->user == NULL)
8423 pst->dependencies[j]->user = pst;
8428 /* Build the partial symbol table by doing a quick pass through the
8429 .debug_info and .debug_abbrev sections. */
8432 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8434 struct objfile *objfile = dwarf2_per_objfile->objfile;
8436 if (dwarf_read_debug)
8438 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8439 objfile_name (objfile));
8442 dwarf2_per_objfile->reading_partial_symbols = 1;
8444 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8446 /* Any cached compilation units will be linked by the per-objfile
8447 read_in_chain. Make sure to free them when we're done. */
8448 free_cached_comp_units freer (dwarf2_per_objfile);
8450 build_type_psymtabs (dwarf2_per_objfile);
8452 create_all_comp_units (dwarf2_per_objfile);
8454 /* Create a temporary address map on a temporary obstack. We later
8455 copy this to the final obstack. */
8456 auto_obstack temp_obstack;
8458 scoped_restore save_psymtabs_addrmap
8459 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8460 addrmap_create_mutable (&temp_obstack));
8462 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8463 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8465 /* This has to wait until we read the CUs, we need the list of DWOs. */
8466 process_skeletonless_type_units (dwarf2_per_objfile);
8468 /* Now that all TUs have been processed we can fill in the dependencies. */
8469 if (dwarf2_per_objfile->type_unit_groups != NULL)
8471 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8472 build_type_psymtab_dependencies, dwarf2_per_objfile);
8475 if (dwarf_read_debug)
8476 print_tu_stats (dwarf2_per_objfile);
8478 set_partial_user (dwarf2_per_objfile);
8480 objfile->partial_symtabs->psymtabs_addrmap
8481 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8482 objfile->partial_symtabs->obstack ());
8483 /* At this point we want to keep the address map. */
8484 save_psymtabs_addrmap.release ();
8486 if (dwarf_read_debug)
8487 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8488 objfile_name (objfile));
8491 /* die_reader_func for load_partial_comp_unit. */
8494 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8495 const gdb_byte *info_ptr,
8496 struct die_info *comp_unit_die,
8500 struct dwarf2_cu *cu = reader->cu;
8502 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8504 /* Check if comp unit has_children.
8505 If so, read the rest of the partial symbols from this comp unit.
8506 If not, there's no more debug_info for this comp unit. */
8508 load_partial_dies (reader, info_ptr, 0);
8511 /* Load the partial DIEs for a secondary CU into memory.
8512 This is also used when rereading a primary CU with load_all_dies. */
8515 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8517 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8518 load_partial_comp_unit_reader, NULL);
8522 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8523 struct dwarf2_section_info *section,
8524 struct dwarf2_section_info *abbrev_section,
8525 unsigned int is_dwz)
8527 const gdb_byte *info_ptr;
8528 struct objfile *objfile = dwarf2_per_objfile->objfile;
8530 if (dwarf_read_debug)
8531 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8532 get_section_name (section),
8533 get_section_file_name (section));
8535 dwarf2_read_section (objfile, section);
8537 info_ptr = section->buffer;
8539 while (info_ptr < section->buffer + section->size)
8541 struct dwarf2_per_cu_data *this_cu;
8543 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8545 comp_unit_head cu_header;
8546 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8547 abbrev_section, info_ptr,
8548 rcuh_kind::COMPILE);
8550 /* Save the compilation unit for later lookup. */
8551 if (cu_header.unit_type != DW_UT_type)
8553 this_cu = XOBNEW (&objfile->objfile_obstack,
8554 struct dwarf2_per_cu_data);
8555 memset (this_cu, 0, sizeof (*this_cu));
8559 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8560 struct signatured_type);
8561 memset (sig_type, 0, sizeof (*sig_type));
8562 sig_type->signature = cu_header.signature;
8563 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8564 this_cu = &sig_type->per_cu;
8566 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8567 this_cu->sect_off = sect_off;
8568 this_cu->length = cu_header.length + cu_header.initial_length_size;
8569 this_cu->is_dwz = is_dwz;
8570 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8571 this_cu->section = section;
8573 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8575 info_ptr = info_ptr + this_cu->length;
8579 /* Create a list of all compilation units in OBJFILE.
8580 This is only done for -readnow and building partial symtabs. */
8583 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8585 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8586 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8587 &dwarf2_per_objfile->abbrev, 0);
8589 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8591 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8595 /* Process all loaded DIEs for compilation unit CU, starting at
8596 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8597 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8598 DW_AT_ranges). See the comments of add_partial_subprogram on how
8599 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8602 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8603 CORE_ADDR *highpc, int set_addrmap,
8604 struct dwarf2_cu *cu)
8606 struct partial_die_info *pdi;
8608 /* Now, march along the PDI's, descending into ones which have
8609 interesting children but skipping the children of the other ones,
8610 until we reach the end of the compilation unit. */
8618 /* Anonymous namespaces or modules have no name but have interesting
8619 children, so we need to look at them. Ditto for anonymous
8622 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8623 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8624 || pdi->tag == DW_TAG_imported_unit
8625 || pdi->tag == DW_TAG_inlined_subroutine)
8629 case DW_TAG_subprogram:
8630 case DW_TAG_inlined_subroutine:
8631 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8633 case DW_TAG_constant:
8634 case DW_TAG_variable:
8635 case DW_TAG_typedef:
8636 case DW_TAG_union_type:
8637 if (!pdi->is_declaration)
8639 add_partial_symbol (pdi, cu);
8642 case DW_TAG_class_type:
8643 case DW_TAG_interface_type:
8644 case DW_TAG_structure_type:
8645 if (!pdi->is_declaration)
8647 add_partial_symbol (pdi, cu);
8649 if ((cu->language == language_rust
8650 || cu->language == language_cplus) && pdi->has_children)
8651 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8654 case DW_TAG_enumeration_type:
8655 if (!pdi->is_declaration)
8656 add_partial_enumeration (pdi, cu);
8658 case DW_TAG_base_type:
8659 case DW_TAG_subrange_type:
8660 /* File scope base type definitions are added to the partial
8662 add_partial_symbol (pdi, cu);
8664 case DW_TAG_namespace:
8665 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8668 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8670 case DW_TAG_imported_unit:
8672 struct dwarf2_per_cu_data *per_cu;
8674 /* For now we don't handle imported units in type units. */
8675 if (cu->per_cu->is_debug_types)
8677 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8678 " supported in type units [in module %s]"),
8679 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8682 per_cu = dwarf2_find_containing_comp_unit
8683 (pdi->d.sect_off, pdi->is_dwz,
8684 cu->per_cu->dwarf2_per_objfile);
8686 /* Go read the partial unit, if needed. */
8687 if (per_cu->v.psymtab == NULL)
8688 process_psymtab_comp_unit (per_cu, 1, cu->language);
8690 VEC_safe_push (dwarf2_per_cu_ptr,
8691 cu->per_cu->imported_symtabs, per_cu);
8694 case DW_TAG_imported_declaration:
8695 add_partial_symbol (pdi, cu);
8702 /* If the die has a sibling, skip to the sibling. */
8704 pdi = pdi->die_sibling;
8708 /* Functions used to compute the fully scoped name of a partial DIE.
8710 Normally, this is simple. For C++, the parent DIE's fully scoped
8711 name is concatenated with "::" and the partial DIE's name.
8712 Enumerators are an exception; they use the scope of their parent
8713 enumeration type, i.e. the name of the enumeration type is not
8714 prepended to the enumerator.
8716 There are two complexities. One is DW_AT_specification; in this
8717 case "parent" means the parent of the target of the specification,
8718 instead of the direct parent of the DIE. The other is compilers
8719 which do not emit DW_TAG_namespace; in this case we try to guess
8720 the fully qualified name of structure types from their members'
8721 linkage names. This must be done using the DIE's children rather
8722 than the children of any DW_AT_specification target. We only need
8723 to do this for structures at the top level, i.e. if the target of
8724 any DW_AT_specification (if any; otherwise the DIE itself) does not
8727 /* Compute the scope prefix associated with PDI's parent, in
8728 compilation unit CU. The result will be allocated on CU's
8729 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8730 field. NULL is returned if no prefix is necessary. */
8732 partial_die_parent_scope (struct partial_die_info *pdi,
8733 struct dwarf2_cu *cu)
8735 const char *grandparent_scope;
8736 struct partial_die_info *parent, *real_pdi;
8738 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8739 then this means the parent of the specification DIE. */
8742 while (real_pdi->has_specification)
8744 auto res = find_partial_die (real_pdi->spec_offset,
8745 real_pdi->spec_is_dwz, cu);
8750 parent = real_pdi->die_parent;
8754 if (parent->scope_set)
8755 return parent->scope;
8759 grandparent_scope = partial_die_parent_scope (parent, cu);
8761 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8762 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8763 Work around this problem here. */
8764 if (cu->language == language_cplus
8765 && parent->tag == DW_TAG_namespace
8766 && strcmp (parent->name, "::") == 0
8767 && grandparent_scope == NULL)
8769 parent->scope = NULL;
8770 parent->scope_set = 1;
8774 if (pdi->tag == DW_TAG_enumerator)
8775 /* Enumerators should not get the name of the enumeration as a prefix. */
8776 parent->scope = grandparent_scope;
8777 else if (parent->tag == DW_TAG_namespace
8778 || parent->tag == DW_TAG_module
8779 || parent->tag == DW_TAG_structure_type
8780 || parent->tag == DW_TAG_class_type
8781 || parent->tag == DW_TAG_interface_type
8782 || parent->tag == DW_TAG_union_type
8783 || parent->tag == DW_TAG_enumeration_type)
8785 if (grandparent_scope == NULL)
8786 parent->scope = parent->name;
8788 parent->scope = typename_concat (&cu->comp_unit_obstack,
8790 parent->name, 0, cu);
8794 /* FIXME drow/2004-04-01: What should we be doing with
8795 function-local names? For partial symbols, we should probably be
8797 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8798 dwarf_tag_name (parent->tag),
8799 sect_offset_str (pdi->sect_off));
8800 parent->scope = grandparent_scope;
8803 parent->scope_set = 1;
8804 return parent->scope;
8807 /* Return the fully scoped name associated with PDI, from compilation unit
8808 CU. The result will be allocated with malloc. */
8811 partial_die_full_name (struct partial_die_info *pdi,
8812 struct dwarf2_cu *cu)
8814 const char *parent_scope;
8816 /* If this is a template instantiation, we can not work out the
8817 template arguments from partial DIEs. So, unfortunately, we have
8818 to go through the full DIEs. At least any work we do building
8819 types here will be reused if full symbols are loaded later. */
8820 if (pdi->has_template_arguments)
8824 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8826 struct die_info *die;
8827 struct attribute attr;
8828 struct dwarf2_cu *ref_cu = cu;
8830 /* DW_FORM_ref_addr is using section offset. */
8831 attr.name = (enum dwarf_attribute) 0;
8832 attr.form = DW_FORM_ref_addr;
8833 attr.u.unsnd = to_underlying (pdi->sect_off);
8834 die = follow_die_ref (NULL, &attr, &ref_cu);
8836 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8840 parent_scope = partial_die_parent_scope (pdi, cu);
8841 if (parent_scope == NULL)
8844 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8848 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8850 struct dwarf2_per_objfile *dwarf2_per_objfile
8851 = cu->per_cu->dwarf2_per_objfile;
8852 struct objfile *objfile = dwarf2_per_objfile->objfile;
8853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8855 const char *actual_name = NULL;
8857 char *built_actual_name;
8859 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8861 built_actual_name = partial_die_full_name (pdi, cu);
8862 if (built_actual_name != NULL)
8863 actual_name = built_actual_name;
8865 if (actual_name == NULL)
8866 actual_name = pdi->name;
8870 case DW_TAG_inlined_subroutine:
8871 case DW_TAG_subprogram:
8872 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8874 if (pdi->is_external || cu->language == language_ada)
8876 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8877 of the global scope. But in Ada, we want to be able to access
8878 nested procedures globally. So all Ada subprograms are stored
8879 in the global scope. */
8880 add_psymbol_to_list (actual_name, strlen (actual_name),
8881 built_actual_name != NULL,
8882 VAR_DOMAIN, LOC_BLOCK,
8883 SECT_OFF_TEXT (objfile),
8884 psymbol_placement::GLOBAL,
8886 cu->language, objfile);
8890 add_psymbol_to_list (actual_name, strlen (actual_name),
8891 built_actual_name != NULL,
8892 VAR_DOMAIN, LOC_BLOCK,
8893 SECT_OFF_TEXT (objfile),
8894 psymbol_placement::STATIC,
8895 addr, cu->language, objfile);
8898 if (pdi->main_subprogram && actual_name != NULL)
8899 set_objfile_main_name (objfile, actual_name, cu->language);
8901 case DW_TAG_constant:
8902 add_psymbol_to_list (actual_name, strlen (actual_name),
8903 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8904 -1, (pdi->is_external
8905 ? psymbol_placement::GLOBAL
8906 : psymbol_placement::STATIC),
8907 0, cu->language, objfile);
8909 case DW_TAG_variable:
8911 addr = decode_locdesc (pdi->d.locdesc, cu);
8915 && !dwarf2_per_objfile->has_section_at_zero)
8917 /* A global or static variable may also have been stripped
8918 out by the linker if unused, in which case its address
8919 will be nullified; do not add such variables into partial
8920 symbol table then. */
8922 else if (pdi->is_external)
8925 Don't enter into the minimal symbol tables as there is
8926 a minimal symbol table entry from the ELF symbols already.
8927 Enter into partial symbol table if it has a location
8928 descriptor or a type.
8929 If the location descriptor is missing, new_symbol will create
8930 a LOC_UNRESOLVED symbol, the address of the variable will then
8931 be determined from the minimal symbol table whenever the variable
8933 The address for the partial symbol table entry is not
8934 used by GDB, but it comes in handy for debugging partial symbol
8937 if (pdi->d.locdesc || pdi->has_type)
8938 add_psymbol_to_list (actual_name, strlen (actual_name),
8939 built_actual_name != NULL,
8940 VAR_DOMAIN, LOC_STATIC,
8941 SECT_OFF_TEXT (objfile),
8942 psymbol_placement::GLOBAL,
8943 addr, cu->language, objfile);
8947 int has_loc = pdi->d.locdesc != NULL;
8949 /* Static Variable. Skip symbols whose value we cannot know (those
8950 without location descriptors or constant values). */
8951 if (!has_loc && !pdi->has_const_value)
8953 xfree (built_actual_name);
8957 add_psymbol_to_list (actual_name, strlen (actual_name),
8958 built_actual_name != NULL,
8959 VAR_DOMAIN, LOC_STATIC,
8960 SECT_OFF_TEXT (objfile),
8961 psymbol_placement::STATIC,
8963 cu->language, objfile);
8966 case DW_TAG_typedef:
8967 case DW_TAG_base_type:
8968 case DW_TAG_subrange_type:
8969 add_psymbol_to_list (actual_name, strlen (actual_name),
8970 built_actual_name != NULL,
8971 VAR_DOMAIN, LOC_TYPEDEF, -1,
8972 psymbol_placement::STATIC,
8973 0, cu->language, objfile);
8975 case DW_TAG_imported_declaration:
8976 case DW_TAG_namespace:
8977 add_psymbol_to_list (actual_name, strlen (actual_name),
8978 built_actual_name != NULL,
8979 VAR_DOMAIN, LOC_TYPEDEF, -1,
8980 psymbol_placement::GLOBAL,
8981 0, cu->language, objfile);
8984 /* With Fortran 77 there might be a "BLOCK DATA" module
8985 available without any name. If so, we skip the module as it
8986 doesn't bring any value. */
8987 if (actual_name != nullptr)
8988 add_psymbol_to_list (actual_name, strlen (actual_name),
8989 built_actual_name != NULL,
8990 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8991 psymbol_placement::GLOBAL,
8992 0, cu->language, objfile);
8994 case DW_TAG_class_type:
8995 case DW_TAG_interface_type:
8996 case DW_TAG_structure_type:
8997 case DW_TAG_union_type:
8998 case DW_TAG_enumeration_type:
8999 /* Skip external references. The DWARF standard says in the section
9000 about "Structure, Union, and Class Type Entries": "An incomplete
9001 structure, union or class type is represented by a structure,
9002 union or class entry that does not have a byte size attribute
9003 and that has a DW_AT_declaration attribute." */
9004 if (!pdi->has_byte_size && pdi->is_declaration)
9006 xfree (built_actual_name);
9010 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9011 static vs. global. */
9012 add_psymbol_to_list (actual_name, strlen (actual_name),
9013 built_actual_name != NULL,
9014 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9015 cu->language == language_cplus
9016 ? psymbol_placement::GLOBAL
9017 : psymbol_placement::STATIC,
9018 0, cu->language, objfile);
9021 case DW_TAG_enumerator:
9022 add_psymbol_to_list (actual_name, strlen (actual_name),
9023 built_actual_name != NULL,
9024 VAR_DOMAIN, LOC_CONST, -1,
9025 cu->language == language_cplus
9026 ? psymbol_placement::GLOBAL
9027 : psymbol_placement::STATIC,
9028 0, cu->language, objfile);
9034 xfree (built_actual_name);
9037 /* Read a partial die corresponding to a namespace; also, add a symbol
9038 corresponding to that namespace to the symbol table. NAMESPACE is
9039 the name of the enclosing namespace. */
9042 add_partial_namespace (struct partial_die_info *pdi,
9043 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9044 int set_addrmap, struct dwarf2_cu *cu)
9046 /* Add a symbol for the namespace. */
9048 add_partial_symbol (pdi, cu);
9050 /* Now scan partial symbols in that namespace. */
9052 if (pdi->has_children)
9053 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9056 /* Read a partial die corresponding to a Fortran module. */
9059 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9060 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9062 /* Add a symbol for the namespace. */
9064 add_partial_symbol (pdi, cu);
9066 /* Now scan partial symbols in that module. */
9068 if (pdi->has_children)
9069 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9072 /* Read a partial die corresponding to a subprogram or an inlined
9073 subprogram and create a partial symbol for that subprogram.
9074 When the CU language allows it, this routine also defines a partial
9075 symbol for each nested subprogram that this subprogram contains.
9076 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9077 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9079 PDI may also be a lexical block, in which case we simply search
9080 recursively for subprograms defined inside that lexical block.
9081 Again, this is only performed when the CU language allows this
9082 type of definitions. */
9085 add_partial_subprogram (struct partial_die_info *pdi,
9086 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9087 int set_addrmap, struct dwarf2_cu *cu)
9089 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9091 if (pdi->has_pc_info)
9093 if (pdi->lowpc < *lowpc)
9094 *lowpc = pdi->lowpc;
9095 if (pdi->highpc > *highpc)
9096 *highpc = pdi->highpc;
9099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9100 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9102 CORE_ADDR this_highpc;
9103 CORE_ADDR this_lowpc;
9105 baseaddr = ANOFFSET (objfile->section_offsets,
9106 SECT_OFF_TEXT (objfile));
9108 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9109 pdi->lowpc + baseaddr)
9112 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9113 pdi->highpc + baseaddr)
9115 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9116 this_lowpc, this_highpc - 1,
9117 cu->per_cu->v.psymtab);
9121 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9123 if (!pdi->is_declaration)
9124 /* Ignore subprogram DIEs that do not have a name, they are
9125 illegal. Do not emit a complaint at this point, we will
9126 do so when we convert this psymtab into a symtab. */
9128 add_partial_symbol (pdi, cu);
9132 if (! pdi->has_children)
9135 if (cu->language == language_ada)
9137 pdi = pdi->die_child;
9141 if (pdi->tag == DW_TAG_subprogram
9142 || pdi->tag == DW_TAG_inlined_subroutine
9143 || pdi->tag == DW_TAG_lexical_block)
9144 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9145 pdi = pdi->die_sibling;
9150 /* Read a partial die corresponding to an enumeration type. */
9153 add_partial_enumeration (struct partial_die_info *enum_pdi,
9154 struct dwarf2_cu *cu)
9156 struct partial_die_info *pdi;
9158 if (enum_pdi->name != NULL)
9159 add_partial_symbol (enum_pdi, cu);
9161 pdi = enum_pdi->die_child;
9164 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9165 complaint (_("malformed enumerator DIE ignored"));
9167 add_partial_symbol (pdi, cu);
9168 pdi = pdi->die_sibling;
9172 /* Return the initial uleb128 in the die at INFO_PTR. */
9175 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9177 unsigned int bytes_read;
9179 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9182 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9183 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9185 Return the corresponding abbrev, or NULL if the number is zero (indicating
9186 an empty DIE). In either case *BYTES_READ will be set to the length of
9187 the initial number. */
9189 static struct abbrev_info *
9190 peek_die_abbrev (const die_reader_specs &reader,
9191 const gdb_byte *info_ptr, unsigned int *bytes_read)
9193 dwarf2_cu *cu = reader.cu;
9194 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9195 unsigned int abbrev_number
9196 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9198 if (abbrev_number == 0)
9201 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9204 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9205 " at offset %s [in module %s]"),
9206 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9207 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9213 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9214 Returns a pointer to the end of a series of DIEs, terminated by an empty
9215 DIE. Any children of the skipped DIEs will also be skipped. */
9217 static const gdb_byte *
9218 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9222 unsigned int bytes_read;
9223 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9226 return info_ptr + bytes_read;
9228 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9232 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9233 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9234 abbrev corresponding to that skipped uleb128 should be passed in
9235 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9238 static const gdb_byte *
9239 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9240 struct abbrev_info *abbrev)
9242 unsigned int bytes_read;
9243 struct attribute attr;
9244 bfd *abfd = reader->abfd;
9245 struct dwarf2_cu *cu = reader->cu;
9246 const gdb_byte *buffer = reader->buffer;
9247 const gdb_byte *buffer_end = reader->buffer_end;
9248 unsigned int form, i;
9250 for (i = 0; i < abbrev->num_attrs; i++)
9252 /* The only abbrev we care about is DW_AT_sibling. */
9253 if (abbrev->attrs[i].name == DW_AT_sibling)
9255 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9256 if (attr.form == DW_FORM_ref_addr)
9257 complaint (_("ignoring absolute DW_AT_sibling"));
9260 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9261 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9263 if (sibling_ptr < info_ptr)
9264 complaint (_("DW_AT_sibling points backwards"));
9265 else if (sibling_ptr > reader->buffer_end)
9266 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9272 /* If it isn't DW_AT_sibling, skip this attribute. */
9273 form = abbrev->attrs[i].form;
9277 case DW_FORM_ref_addr:
9278 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9279 and later it is offset sized. */
9280 if (cu->header.version == 2)
9281 info_ptr += cu->header.addr_size;
9283 info_ptr += cu->header.offset_size;
9285 case DW_FORM_GNU_ref_alt:
9286 info_ptr += cu->header.offset_size;
9289 info_ptr += cu->header.addr_size;
9296 case DW_FORM_flag_present:
9297 case DW_FORM_implicit_const:
9309 case DW_FORM_ref_sig8:
9312 case DW_FORM_data16:
9315 case DW_FORM_string:
9316 read_direct_string (abfd, info_ptr, &bytes_read);
9317 info_ptr += bytes_read;
9319 case DW_FORM_sec_offset:
9321 case DW_FORM_GNU_strp_alt:
9322 info_ptr += cu->header.offset_size;
9324 case DW_FORM_exprloc:
9326 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9327 info_ptr += bytes_read;
9329 case DW_FORM_block1:
9330 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9332 case DW_FORM_block2:
9333 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9335 case DW_FORM_block4:
9336 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9342 case DW_FORM_ref_udata:
9343 case DW_FORM_GNU_addr_index:
9344 case DW_FORM_GNU_str_index:
9345 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9347 case DW_FORM_indirect:
9348 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9349 info_ptr += bytes_read;
9350 /* We need to continue parsing from here, so just go back to
9352 goto skip_attribute;
9355 error (_("Dwarf Error: Cannot handle %s "
9356 "in DWARF reader [in module %s]"),
9357 dwarf_form_name (form),
9358 bfd_get_filename (abfd));
9362 if (abbrev->has_children)
9363 return skip_children (reader, info_ptr);
9368 /* Locate ORIG_PDI's sibling.
9369 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9371 static const gdb_byte *
9372 locate_pdi_sibling (const struct die_reader_specs *reader,
9373 struct partial_die_info *orig_pdi,
9374 const gdb_byte *info_ptr)
9376 /* Do we know the sibling already? */
9378 if (orig_pdi->sibling)
9379 return orig_pdi->sibling;
9381 /* Are there any children to deal with? */
9383 if (!orig_pdi->has_children)
9386 /* Skip the children the long way. */
9388 return skip_children (reader, info_ptr);
9391 /* Expand this partial symbol table into a full symbol table. SELF is
9395 dwarf2_read_symtab (struct partial_symtab *self,
9396 struct objfile *objfile)
9398 struct dwarf2_per_objfile *dwarf2_per_objfile
9399 = get_dwarf2_per_objfile (objfile);
9403 warning (_("bug: psymtab for %s is already read in."),
9410 printf_filtered (_("Reading in symbols for %s..."),
9412 gdb_flush (gdb_stdout);
9415 /* If this psymtab is constructed from a debug-only objfile, the
9416 has_section_at_zero flag will not necessarily be correct. We
9417 can get the correct value for this flag by looking at the data
9418 associated with the (presumably stripped) associated objfile. */
9419 if (objfile->separate_debug_objfile_backlink)
9421 struct dwarf2_per_objfile *dpo_backlink
9422 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9424 dwarf2_per_objfile->has_section_at_zero
9425 = dpo_backlink->has_section_at_zero;
9428 dwarf2_per_objfile->reading_partial_symbols = 0;
9430 psymtab_to_symtab_1 (self);
9432 /* Finish up the debug error message. */
9434 printf_filtered (_("done.\n"));
9437 process_cu_includes (dwarf2_per_objfile);
9440 /* Reading in full CUs. */
9442 /* Add PER_CU to the queue. */
9445 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9446 enum language pretend_language)
9448 struct dwarf2_queue_item *item;
9451 item = XNEW (struct dwarf2_queue_item);
9452 item->per_cu = per_cu;
9453 item->pretend_language = pretend_language;
9456 if (dwarf2_queue == NULL)
9457 dwarf2_queue = item;
9459 dwarf2_queue_tail->next = item;
9461 dwarf2_queue_tail = item;
9464 /* If PER_CU is not yet queued, add it to the queue.
9465 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9467 The result is non-zero if PER_CU was queued, otherwise the result is zero
9468 meaning either PER_CU is already queued or it is already loaded.
9470 N.B. There is an invariant here that if a CU is queued then it is loaded.
9471 The caller is required to load PER_CU if we return non-zero. */
9474 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9475 struct dwarf2_per_cu_data *per_cu,
9476 enum language pretend_language)
9478 /* We may arrive here during partial symbol reading, if we need full
9479 DIEs to process an unusual case (e.g. template arguments). Do
9480 not queue PER_CU, just tell our caller to load its DIEs. */
9481 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9483 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9488 /* Mark the dependence relation so that we don't flush PER_CU
9490 if (dependent_cu != NULL)
9491 dwarf2_add_dependence (dependent_cu, per_cu);
9493 /* If it's already on the queue, we have nothing to do. */
9497 /* If the compilation unit is already loaded, just mark it as
9499 if (per_cu->cu != NULL)
9501 per_cu->cu->last_used = 0;
9505 /* Add it to the queue. */
9506 queue_comp_unit (per_cu, pretend_language);
9511 /* Process the queue. */
9514 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9516 struct dwarf2_queue_item *item, *next_item;
9518 if (dwarf_read_debug)
9520 fprintf_unfiltered (gdb_stdlog,
9521 "Expanding one or more symtabs of objfile %s ...\n",
9522 objfile_name (dwarf2_per_objfile->objfile));
9525 /* The queue starts out with one item, but following a DIE reference
9526 may load a new CU, adding it to the end of the queue. */
9527 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9529 if ((dwarf2_per_objfile->using_index
9530 ? !item->per_cu->v.quick->compunit_symtab
9531 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9532 /* Skip dummy CUs. */
9533 && item->per_cu->cu != NULL)
9535 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9536 unsigned int debug_print_threshold;
9539 if (per_cu->is_debug_types)
9541 struct signatured_type *sig_type =
9542 (struct signatured_type *) per_cu;
9544 sprintf (buf, "TU %s at offset %s",
9545 hex_string (sig_type->signature),
9546 sect_offset_str (per_cu->sect_off));
9547 /* There can be 100s of TUs.
9548 Only print them in verbose mode. */
9549 debug_print_threshold = 2;
9553 sprintf (buf, "CU at offset %s",
9554 sect_offset_str (per_cu->sect_off));
9555 debug_print_threshold = 1;
9558 if (dwarf_read_debug >= debug_print_threshold)
9559 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9561 if (per_cu->is_debug_types)
9562 process_full_type_unit (per_cu, item->pretend_language);
9564 process_full_comp_unit (per_cu, item->pretend_language);
9566 if (dwarf_read_debug >= debug_print_threshold)
9567 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9570 item->per_cu->queued = 0;
9571 next_item = item->next;
9575 dwarf2_queue_tail = NULL;
9577 if (dwarf_read_debug)
9579 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9580 objfile_name (dwarf2_per_objfile->objfile));
9584 /* Read in full symbols for PST, and anything it depends on. */
9587 psymtab_to_symtab_1 (struct partial_symtab *pst)
9589 struct dwarf2_per_cu_data *per_cu;
9595 for (i = 0; i < pst->number_of_dependencies; i++)
9596 if (!pst->dependencies[i]->readin
9597 && pst->dependencies[i]->user == NULL)
9599 /* Inform about additional files that need to be read in. */
9602 /* FIXME: i18n: Need to make this a single string. */
9603 fputs_filtered (" ", gdb_stdout);
9605 fputs_filtered ("and ", gdb_stdout);
9607 printf_filtered ("%s...", pst->dependencies[i]->filename);
9608 wrap_here (""); /* Flush output. */
9609 gdb_flush (gdb_stdout);
9611 psymtab_to_symtab_1 (pst->dependencies[i]);
9614 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9618 /* It's an include file, no symbols to read for it.
9619 Everything is in the parent symtab. */
9624 dw2_do_instantiate_symtab (per_cu, false);
9627 /* Trivial hash function for die_info: the hash value of a DIE
9628 is its offset in .debug_info for this objfile. */
9631 die_hash (const void *item)
9633 const struct die_info *die = (const struct die_info *) item;
9635 return to_underlying (die->sect_off);
9638 /* Trivial comparison function for die_info structures: two DIEs
9639 are equal if they have the same offset. */
9642 die_eq (const void *item_lhs, const void *item_rhs)
9644 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9645 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9647 return die_lhs->sect_off == die_rhs->sect_off;
9650 /* die_reader_func for load_full_comp_unit.
9651 This is identical to read_signatured_type_reader,
9652 but is kept separate for now. */
9655 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9656 const gdb_byte *info_ptr,
9657 struct die_info *comp_unit_die,
9661 struct dwarf2_cu *cu = reader->cu;
9662 enum language *language_ptr = (enum language *) data;
9664 gdb_assert (cu->die_hash == NULL);
9666 htab_create_alloc_ex (cu->header.length / 12,
9670 &cu->comp_unit_obstack,
9671 hashtab_obstack_allocate,
9672 dummy_obstack_deallocate);
9675 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9676 &info_ptr, comp_unit_die);
9677 cu->dies = comp_unit_die;
9678 /* comp_unit_die is not stored in die_hash, no need. */
9680 /* We try not to read any attributes in this function, because not
9681 all CUs needed for references have been loaded yet, and symbol
9682 table processing isn't initialized. But we have to set the CU language,
9683 or we won't be able to build types correctly.
9684 Similarly, if we do not read the producer, we can not apply
9685 producer-specific interpretation. */
9686 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9689 /* Load the DIEs associated with PER_CU into memory. */
9692 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9694 enum language pretend_language)
9696 gdb_assert (! this_cu->is_debug_types);
9698 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9699 load_full_comp_unit_reader, &pretend_language);
9702 /* Add a DIE to the delayed physname list. */
9705 add_to_method_list (struct type *type, int fnfield_index, int index,
9706 const char *name, struct die_info *die,
9707 struct dwarf2_cu *cu)
9709 struct delayed_method_info mi;
9711 mi.fnfield_index = fnfield_index;
9715 cu->method_list.push_back (mi);
9718 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9719 "const" / "volatile". If so, decrements LEN by the length of the
9720 modifier and return true. Otherwise return false. */
9724 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9726 size_t mod_len = sizeof (mod) - 1;
9727 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9735 /* Compute the physnames of any methods on the CU's method list.
9737 The computation of method physnames is delayed in order to avoid the
9738 (bad) condition that one of the method's formal parameters is of an as yet
9742 compute_delayed_physnames (struct dwarf2_cu *cu)
9744 /* Only C++ delays computing physnames. */
9745 if (cu->method_list.empty ())
9747 gdb_assert (cu->language == language_cplus);
9749 for (const delayed_method_info &mi : cu->method_list)
9751 const char *physname;
9752 struct fn_fieldlist *fn_flp
9753 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9754 physname = dwarf2_physname (mi.name, mi.die, cu);
9755 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9756 = physname ? physname : "";
9758 /* Since there's no tag to indicate whether a method is a
9759 const/volatile overload, extract that information out of the
9761 if (physname != NULL)
9763 size_t len = strlen (physname);
9767 if (physname[len] == ')') /* shortcut */
9769 else if (check_modifier (physname, len, " const"))
9770 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9771 else if (check_modifier (physname, len, " volatile"))
9772 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9779 /* The list is no longer needed. */
9780 cu->method_list.clear ();
9783 /* Go objects should be embedded in a DW_TAG_module DIE,
9784 and it's not clear if/how imported objects will appear.
9785 To keep Go support simple until that's worked out,
9786 go back through what we've read and create something usable.
9787 We could do this while processing each DIE, and feels kinda cleaner,
9788 but that way is more invasive.
9789 This is to, for example, allow the user to type "p var" or "b main"
9790 without having to specify the package name, and allow lookups
9791 of module.object to work in contexts that use the expression
9795 fixup_go_packaging (struct dwarf2_cu *cu)
9797 char *package_name = NULL;
9798 struct pending *list;
9801 for (list = *cu->get_builder ()->get_global_symbols ();
9805 for (i = 0; i < list->nsyms; ++i)
9807 struct symbol *sym = list->symbol[i];
9809 if (SYMBOL_LANGUAGE (sym) == language_go
9810 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9812 char *this_package_name = go_symbol_package_name (sym);
9814 if (this_package_name == NULL)
9816 if (package_name == NULL)
9817 package_name = this_package_name;
9820 struct objfile *objfile
9821 = cu->per_cu->dwarf2_per_objfile->objfile;
9822 if (strcmp (package_name, this_package_name) != 0)
9823 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9824 (symbol_symtab (sym) != NULL
9825 ? symtab_to_filename_for_display
9826 (symbol_symtab (sym))
9827 : objfile_name (objfile)),
9828 this_package_name, package_name);
9829 xfree (this_package_name);
9835 if (package_name != NULL)
9837 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9838 const char *saved_package_name
9839 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9841 strlen (package_name));
9842 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9843 saved_package_name);
9846 sym = allocate_symbol (objfile);
9847 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9848 SYMBOL_SET_NAMES (sym, saved_package_name,
9849 strlen (saved_package_name), 0, objfile);
9850 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9851 e.g., "main" finds the "main" module and not C's main(). */
9852 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9853 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9854 SYMBOL_TYPE (sym) = type;
9856 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9858 xfree (package_name);
9862 /* Allocate a fully-qualified name consisting of the two parts on the
9866 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9868 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9871 /* A helper that allocates a struct discriminant_info to attach to a
9874 static struct discriminant_info *
9875 alloc_discriminant_info (struct type *type, int discriminant_index,
9878 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9879 gdb_assert (discriminant_index == -1
9880 || (discriminant_index >= 0
9881 && discriminant_index < TYPE_NFIELDS (type)));
9882 gdb_assert (default_index == -1
9883 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9885 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9887 struct discriminant_info *disc
9888 = ((struct discriminant_info *)
9890 offsetof (struct discriminant_info, discriminants)
9891 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9892 disc->default_index = default_index;
9893 disc->discriminant_index = discriminant_index;
9895 struct dynamic_prop prop;
9896 prop.kind = PROP_UNDEFINED;
9897 prop.data.baton = disc;
9899 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9904 /* Some versions of rustc emitted enums in an unusual way.
9906 Ordinary enums were emitted as unions. The first element of each
9907 structure in the union was named "RUST$ENUM$DISR". This element
9908 held the discriminant.
9910 These versions of Rust also implemented the "non-zero"
9911 optimization. When the enum had two values, and one is empty and
9912 the other holds a pointer that cannot be zero, the pointer is used
9913 as the discriminant, with a zero value meaning the empty variant.
9914 Here, the union's first member is of the form
9915 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9916 where the fieldnos are the indices of the fields that should be
9917 traversed in order to find the field (which may be several fields deep)
9918 and the variantname is the name of the variant of the case when the
9921 This function recognizes whether TYPE is of one of these forms,
9922 and, if so, smashes it to be a variant type. */
9925 quirk_rust_enum (struct type *type, struct objfile *objfile)
9927 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9929 /* We don't need to deal with empty enums. */
9930 if (TYPE_NFIELDS (type) == 0)
9933 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9934 if (TYPE_NFIELDS (type) == 1
9935 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9937 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9939 /* Decode the field name to find the offset of the
9941 ULONGEST bit_offset = 0;
9942 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9943 while (name[0] >= '0' && name[0] <= '9')
9946 unsigned long index = strtoul (name, &tail, 10);
9949 || index >= TYPE_NFIELDS (field_type)
9950 || (TYPE_FIELD_LOC_KIND (field_type, index)
9951 != FIELD_LOC_KIND_BITPOS))
9953 complaint (_("Could not parse Rust enum encoding string \"%s\""
9955 TYPE_FIELD_NAME (type, 0),
9956 objfile_name (objfile));
9961 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9962 field_type = TYPE_FIELD_TYPE (field_type, index);
9965 /* Make a union to hold the variants. */
9966 struct type *union_type = alloc_type (objfile);
9967 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9968 TYPE_NFIELDS (union_type) = 3;
9969 TYPE_FIELDS (union_type)
9970 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9971 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9972 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9974 /* Put the discriminant must at index 0. */
9975 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9976 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9977 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9978 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9980 /* The order of fields doesn't really matter, so put the real
9981 field at index 1 and the data-less field at index 2. */
9982 struct discriminant_info *disc
9983 = alloc_discriminant_info (union_type, 0, 1);
9984 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9985 TYPE_FIELD_NAME (union_type, 1)
9986 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9987 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9988 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9989 TYPE_FIELD_NAME (union_type, 1));
9991 const char *dataless_name
9992 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9994 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9996 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9997 /* NAME points into the original discriminant name, which
9998 already has the correct lifetime. */
9999 TYPE_FIELD_NAME (union_type, 2) = name;
10000 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10001 disc->discriminants[2] = 0;
10003 /* Smash this type to be a structure type. We have to do this
10004 because the type has already been recorded. */
10005 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10006 TYPE_NFIELDS (type) = 1;
10008 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10010 /* Install the variant part. */
10011 TYPE_FIELD_TYPE (type, 0) = union_type;
10012 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10013 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10015 else if (TYPE_NFIELDS (type) == 1)
10017 /* We assume that a union with a single field is a univariant
10019 /* Smash this type to be a structure type. We have to do this
10020 because the type has already been recorded. */
10021 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10023 /* Make a union to hold the variants. */
10024 struct type *union_type = alloc_type (objfile);
10025 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10026 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10027 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10028 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10029 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10031 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10032 const char *variant_name
10033 = rust_last_path_segment (TYPE_NAME (field_type));
10034 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10035 TYPE_NAME (field_type)
10036 = rust_fully_qualify (&objfile->objfile_obstack,
10037 TYPE_NAME (type), variant_name);
10039 /* Install the union in the outer struct type. */
10040 TYPE_NFIELDS (type) = 1;
10042 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10043 TYPE_FIELD_TYPE (type, 0) = union_type;
10044 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10045 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10047 alloc_discriminant_info (union_type, -1, 0);
10051 struct type *disr_type = nullptr;
10052 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10054 disr_type = TYPE_FIELD_TYPE (type, i);
10056 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10058 /* All fields of a true enum will be structs. */
10061 else if (TYPE_NFIELDS (disr_type) == 0)
10063 /* Could be data-less variant, so keep going. */
10064 disr_type = nullptr;
10066 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10067 "RUST$ENUM$DISR") != 0)
10069 /* Not a Rust enum. */
10079 /* If we got here without a discriminant, then it's probably
10081 if (disr_type == nullptr)
10084 /* Smash this type to be a structure type. We have to do this
10085 because the type has already been recorded. */
10086 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10088 /* Make a union to hold the variants. */
10089 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10090 struct type *union_type = alloc_type (objfile);
10091 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10092 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10093 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10094 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10095 TYPE_FIELDS (union_type)
10096 = (struct field *) TYPE_ZALLOC (union_type,
10097 (TYPE_NFIELDS (union_type)
10098 * sizeof (struct field)));
10100 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10101 TYPE_NFIELDS (type) * sizeof (struct field));
10103 /* Install the discriminant at index 0 in the union. */
10104 TYPE_FIELD (union_type, 0) = *disr_field;
10105 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10106 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10108 /* Install the union in the outer struct type. */
10109 TYPE_FIELD_TYPE (type, 0) = union_type;
10110 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10111 TYPE_NFIELDS (type) = 1;
10113 /* Set the size and offset of the union type. */
10114 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10116 /* We need a way to find the correct discriminant given a
10117 variant name. For convenience we build a map here. */
10118 struct type *enum_type = FIELD_TYPE (*disr_field);
10119 std::unordered_map<std::string, ULONGEST> discriminant_map;
10120 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10122 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10125 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10126 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10130 int n_fields = TYPE_NFIELDS (union_type);
10131 struct discriminant_info *disc
10132 = alloc_discriminant_info (union_type, 0, -1);
10133 /* Skip the discriminant here. */
10134 for (int i = 1; i < n_fields; ++i)
10136 /* Find the final word in the name of this variant's type.
10137 That name can be used to look up the correct
10139 const char *variant_name
10140 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10143 auto iter = discriminant_map.find (variant_name);
10144 if (iter != discriminant_map.end ())
10145 disc->discriminants[i] = iter->second;
10147 /* Remove the discriminant field, if it exists. */
10148 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10149 if (TYPE_NFIELDS (sub_type) > 0)
10151 --TYPE_NFIELDS (sub_type);
10152 ++TYPE_FIELDS (sub_type);
10154 TYPE_FIELD_NAME (union_type, i) = variant_name;
10155 TYPE_NAME (sub_type)
10156 = rust_fully_qualify (&objfile->objfile_obstack,
10157 TYPE_NAME (type), variant_name);
10162 /* Rewrite some Rust unions to be structures with variants parts. */
10165 rust_union_quirks (struct dwarf2_cu *cu)
10167 gdb_assert (cu->language == language_rust);
10168 for (type *type_ : cu->rust_unions)
10169 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10170 /* We don't need this any more. */
10171 cu->rust_unions.clear ();
10174 /* Return the symtab for PER_CU. This works properly regardless of
10175 whether we're using the index or psymtabs. */
10177 static struct compunit_symtab *
10178 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10180 return (per_cu->dwarf2_per_objfile->using_index
10181 ? per_cu->v.quick->compunit_symtab
10182 : per_cu->v.psymtab->compunit_symtab);
10185 /* A helper function for computing the list of all symbol tables
10186 included by PER_CU. */
10189 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10190 htab_t all_children, htab_t all_type_symtabs,
10191 struct dwarf2_per_cu_data *per_cu,
10192 struct compunit_symtab *immediate_parent)
10196 struct compunit_symtab *cust;
10197 struct dwarf2_per_cu_data *iter;
10199 slot = htab_find_slot (all_children, per_cu, INSERT);
10202 /* This inclusion and its children have been processed. */
10207 /* Only add a CU if it has a symbol table. */
10208 cust = get_compunit_symtab (per_cu);
10211 /* If this is a type unit only add its symbol table if we haven't
10212 seen it yet (type unit per_cu's can share symtabs). */
10213 if (per_cu->is_debug_types)
10215 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10219 result->push_back (cust);
10220 if (cust->user == NULL)
10221 cust->user = immediate_parent;
10226 result->push_back (cust);
10227 if (cust->user == NULL)
10228 cust->user = immediate_parent;
10233 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10236 recursively_compute_inclusions (result, all_children,
10237 all_type_symtabs, iter, cust);
10241 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10245 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10247 gdb_assert (! per_cu->is_debug_types);
10249 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10252 struct dwarf2_per_cu_data *per_cu_iter;
10253 std::vector<compunit_symtab *> result_symtabs;
10254 htab_t all_children, all_type_symtabs;
10255 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10257 /* If we don't have a symtab, we can just skip this case. */
10261 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10262 NULL, xcalloc, xfree);
10263 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10264 NULL, xcalloc, xfree);
10267 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10271 recursively_compute_inclusions (&result_symtabs, all_children,
10272 all_type_symtabs, per_cu_iter,
10276 /* Now we have a transitive closure of all the included symtabs. */
10277 len = result_symtabs.size ();
10279 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10280 struct compunit_symtab *, len + 1);
10281 memcpy (cust->includes, result_symtabs.data (),
10282 len * sizeof (compunit_symtab *));
10283 cust->includes[len] = NULL;
10285 htab_delete (all_children);
10286 htab_delete (all_type_symtabs);
10290 /* Compute the 'includes' field for the symtabs of all the CUs we just
10294 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10296 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10298 if (! iter->is_debug_types)
10299 compute_compunit_symtab_includes (iter);
10302 dwarf2_per_objfile->just_read_cus.clear ();
10305 /* Generate full symbol information for PER_CU, whose DIEs have
10306 already been loaded into memory. */
10309 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10310 enum language pretend_language)
10312 struct dwarf2_cu *cu = per_cu->cu;
10313 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10314 struct objfile *objfile = dwarf2_per_objfile->objfile;
10315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10316 CORE_ADDR lowpc, highpc;
10317 struct compunit_symtab *cust;
10318 CORE_ADDR baseaddr;
10319 struct block *static_block;
10322 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10324 /* Clear the list here in case something was left over. */
10325 cu->method_list.clear ();
10327 cu->language = pretend_language;
10328 cu->language_defn = language_def (cu->language);
10330 /* Do line number decoding in read_file_scope () */
10331 process_die (cu->dies, cu);
10333 /* For now fudge the Go package. */
10334 if (cu->language == language_go)
10335 fixup_go_packaging (cu);
10337 /* Now that we have processed all the DIEs in the CU, all the types
10338 should be complete, and it should now be safe to compute all of the
10340 compute_delayed_physnames (cu);
10342 if (cu->language == language_rust)
10343 rust_union_quirks (cu);
10345 /* Some compilers don't define a DW_AT_high_pc attribute for the
10346 compilation unit. If the DW_AT_high_pc is missing, synthesize
10347 it, by scanning the DIE's below the compilation unit. */
10348 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10350 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10351 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10353 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10354 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10355 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10356 addrmap to help ensure it has an accurate map of pc values belonging to
10358 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10360 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10361 SECT_OFF_TEXT (objfile),
10366 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10368 /* Set symtab language to language from DW_AT_language. If the
10369 compilation is from a C file generated by language preprocessors, do
10370 not set the language if it was already deduced by start_subfile. */
10371 if (!(cu->language == language_c
10372 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10373 COMPUNIT_FILETABS (cust)->language = cu->language;
10375 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10376 produce DW_AT_location with location lists but it can be possibly
10377 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10378 there were bugs in prologue debug info, fixed later in GCC-4.5
10379 by "unwind info for epilogues" patch (which is not directly related).
10381 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10382 needed, it would be wrong due to missing DW_AT_producer there.
10384 Still one can confuse GDB by using non-standard GCC compilation
10385 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10387 if (cu->has_loclist && gcc_4_minor >= 5)
10388 cust->locations_valid = 1;
10390 if (gcc_4_minor >= 5)
10391 cust->epilogue_unwind_valid = 1;
10393 cust->call_site_htab = cu->call_site_htab;
10396 if (dwarf2_per_objfile->using_index)
10397 per_cu->v.quick->compunit_symtab = cust;
10400 struct partial_symtab *pst = per_cu->v.psymtab;
10401 pst->compunit_symtab = cust;
10405 /* Push it for inclusion processing later. */
10406 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10408 /* Not needed any more. */
10409 cu->reset_builder ();
10412 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10413 already been loaded into memory. */
10416 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10417 enum language pretend_language)
10419 struct dwarf2_cu *cu = per_cu->cu;
10420 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10421 struct objfile *objfile = dwarf2_per_objfile->objfile;
10422 struct compunit_symtab *cust;
10423 struct signatured_type *sig_type;
10425 gdb_assert (per_cu->is_debug_types);
10426 sig_type = (struct signatured_type *) per_cu;
10428 /* Clear the list here in case something was left over. */
10429 cu->method_list.clear ();
10431 cu->language = pretend_language;
10432 cu->language_defn = language_def (cu->language);
10434 /* The symbol tables are set up in read_type_unit_scope. */
10435 process_die (cu->dies, cu);
10437 /* For now fudge the Go package. */
10438 if (cu->language == language_go)
10439 fixup_go_packaging (cu);
10441 /* Now that we have processed all the DIEs in the CU, all the types
10442 should be complete, and it should now be safe to compute all of the
10444 compute_delayed_physnames (cu);
10446 if (cu->language == language_rust)
10447 rust_union_quirks (cu);
10449 /* TUs share symbol tables.
10450 If this is the first TU to use this symtab, complete the construction
10451 of it with end_expandable_symtab. Otherwise, complete the addition of
10452 this TU's symbols to the existing symtab. */
10453 if (sig_type->type_unit_group->compunit_symtab == NULL)
10455 buildsym_compunit *builder = cu->get_builder ();
10456 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10457 sig_type->type_unit_group->compunit_symtab = cust;
10461 /* Set symtab language to language from DW_AT_language. If the
10462 compilation is from a C file generated by language preprocessors,
10463 do not set the language if it was already deduced by
10465 if (!(cu->language == language_c
10466 && COMPUNIT_FILETABS (cust)->language != language_c))
10467 COMPUNIT_FILETABS (cust)->language = cu->language;
10472 cu->get_builder ()->augment_type_symtab ();
10473 cust = sig_type->type_unit_group->compunit_symtab;
10476 if (dwarf2_per_objfile->using_index)
10477 per_cu->v.quick->compunit_symtab = cust;
10480 struct partial_symtab *pst = per_cu->v.psymtab;
10481 pst->compunit_symtab = cust;
10485 /* Not needed any more. */
10486 cu->reset_builder ();
10489 /* Process an imported unit DIE. */
10492 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10494 struct attribute *attr;
10496 /* For now we don't handle imported units in type units. */
10497 if (cu->per_cu->is_debug_types)
10499 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10500 " supported in type units [in module %s]"),
10501 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10504 attr = dwarf2_attr (die, DW_AT_import, cu);
10507 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10508 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10509 dwarf2_per_cu_data *per_cu
10510 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10511 cu->per_cu->dwarf2_per_objfile);
10513 /* If necessary, add it to the queue and load its DIEs. */
10514 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10515 load_full_comp_unit (per_cu, false, cu->language);
10517 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10522 /* RAII object that represents a process_die scope: i.e.,
10523 starts/finishes processing a DIE. */
10524 class process_die_scope
10527 process_die_scope (die_info *die, dwarf2_cu *cu)
10528 : m_die (die), m_cu (cu)
10530 /* We should only be processing DIEs not already in process. */
10531 gdb_assert (!m_die->in_process);
10532 m_die->in_process = true;
10535 ~process_die_scope ()
10537 m_die->in_process = false;
10539 /* If we're done processing the DIE for the CU that owns the line
10540 header, we don't need the line header anymore. */
10541 if (m_cu->line_header_die_owner == m_die)
10543 delete m_cu->line_header;
10544 m_cu->line_header = NULL;
10545 m_cu->line_header_die_owner = NULL;
10554 /* Process a die and its children. */
10557 process_die (struct die_info *die, struct dwarf2_cu *cu)
10559 process_die_scope scope (die, cu);
10563 case DW_TAG_padding:
10565 case DW_TAG_compile_unit:
10566 case DW_TAG_partial_unit:
10567 read_file_scope (die, cu);
10569 case DW_TAG_type_unit:
10570 read_type_unit_scope (die, cu);
10572 case DW_TAG_subprogram:
10573 case DW_TAG_inlined_subroutine:
10574 read_func_scope (die, cu);
10576 case DW_TAG_lexical_block:
10577 case DW_TAG_try_block:
10578 case DW_TAG_catch_block:
10579 read_lexical_block_scope (die, cu);
10581 case DW_TAG_call_site:
10582 case DW_TAG_GNU_call_site:
10583 read_call_site_scope (die, cu);
10585 case DW_TAG_class_type:
10586 case DW_TAG_interface_type:
10587 case DW_TAG_structure_type:
10588 case DW_TAG_union_type:
10589 process_structure_scope (die, cu);
10591 case DW_TAG_enumeration_type:
10592 process_enumeration_scope (die, cu);
10595 /* These dies have a type, but processing them does not create
10596 a symbol or recurse to process the children. Therefore we can
10597 read them on-demand through read_type_die. */
10598 case DW_TAG_subroutine_type:
10599 case DW_TAG_set_type:
10600 case DW_TAG_array_type:
10601 case DW_TAG_pointer_type:
10602 case DW_TAG_ptr_to_member_type:
10603 case DW_TAG_reference_type:
10604 case DW_TAG_rvalue_reference_type:
10605 case DW_TAG_string_type:
10608 case DW_TAG_base_type:
10609 case DW_TAG_subrange_type:
10610 case DW_TAG_typedef:
10611 /* Add a typedef symbol for the type definition, if it has a
10613 new_symbol (die, read_type_die (die, cu), cu);
10615 case DW_TAG_common_block:
10616 read_common_block (die, cu);
10618 case DW_TAG_common_inclusion:
10620 case DW_TAG_namespace:
10621 cu->processing_has_namespace_info = true;
10622 read_namespace (die, cu);
10624 case DW_TAG_module:
10625 cu->processing_has_namespace_info = true;
10626 read_module (die, cu);
10628 case DW_TAG_imported_declaration:
10629 cu->processing_has_namespace_info = true;
10630 if (read_namespace_alias (die, cu))
10632 /* The declaration is not a global namespace alias. */
10633 /* Fall through. */
10634 case DW_TAG_imported_module:
10635 cu->processing_has_namespace_info = true;
10636 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10637 || cu->language != language_fortran))
10638 complaint (_("Tag '%s' has unexpected children"),
10639 dwarf_tag_name (die->tag));
10640 read_import_statement (die, cu);
10643 case DW_TAG_imported_unit:
10644 process_imported_unit_die (die, cu);
10647 case DW_TAG_variable:
10648 read_variable (die, cu);
10652 new_symbol (die, NULL, cu);
10657 /* DWARF name computation. */
10659 /* A helper function for dwarf2_compute_name which determines whether DIE
10660 needs to have the name of the scope prepended to the name listed in the
10664 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10666 struct attribute *attr;
10670 case DW_TAG_namespace:
10671 case DW_TAG_typedef:
10672 case DW_TAG_class_type:
10673 case DW_TAG_interface_type:
10674 case DW_TAG_structure_type:
10675 case DW_TAG_union_type:
10676 case DW_TAG_enumeration_type:
10677 case DW_TAG_enumerator:
10678 case DW_TAG_subprogram:
10679 case DW_TAG_inlined_subroutine:
10680 case DW_TAG_member:
10681 case DW_TAG_imported_declaration:
10684 case DW_TAG_variable:
10685 case DW_TAG_constant:
10686 /* We only need to prefix "globally" visible variables. These include
10687 any variable marked with DW_AT_external or any variable that
10688 lives in a namespace. [Variables in anonymous namespaces
10689 require prefixing, but they are not DW_AT_external.] */
10691 if (dwarf2_attr (die, DW_AT_specification, cu))
10693 struct dwarf2_cu *spec_cu = cu;
10695 return die_needs_namespace (die_specification (die, &spec_cu),
10699 attr = dwarf2_attr (die, DW_AT_external, cu);
10700 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10701 && die->parent->tag != DW_TAG_module)
10703 /* A variable in a lexical block of some kind does not need a
10704 namespace, even though in C++ such variables may be external
10705 and have a mangled name. */
10706 if (die->parent->tag == DW_TAG_lexical_block
10707 || die->parent->tag == DW_TAG_try_block
10708 || die->parent->tag == DW_TAG_catch_block
10709 || die->parent->tag == DW_TAG_subprogram)
10718 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10719 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10720 defined for the given DIE. */
10722 static struct attribute *
10723 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10725 struct attribute *attr;
10727 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10729 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10734 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10735 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10736 defined for the given DIE. */
10738 static const char *
10739 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10741 const char *linkage_name;
10743 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10744 if (linkage_name == NULL)
10745 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10747 return linkage_name;
10750 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10751 compute the physname for the object, which include a method's:
10752 - formal parameters (C++),
10753 - receiver type (Go),
10755 The term "physname" is a bit confusing.
10756 For C++, for example, it is the demangled name.
10757 For Go, for example, it's the mangled name.
10759 For Ada, return the DIE's linkage name rather than the fully qualified
10760 name. PHYSNAME is ignored..
10762 The result is allocated on the objfile_obstack and canonicalized. */
10764 static const char *
10765 dwarf2_compute_name (const char *name,
10766 struct die_info *die, struct dwarf2_cu *cu,
10769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10772 name = dwarf2_name (die, cu);
10774 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10775 but otherwise compute it by typename_concat inside GDB.
10776 FIXME: Actually this is not really true, or at least not always true.
10777 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10778 Fortran names because there is no mangling standard. So new_symbol
10779 will set the demangled name to the result of dwarf2_full_name, and it is
10780 the demangled name that GDB uses if it exists. */
10781 if (cu->language == language_ada
10782 || (cu->language == language_fortran && physname))
10784 /* For Ada unit, we prefer the linkage name over the name, as
10785 the former contains the exported name, which the user expects
10786 to be able to reference. Ideally, we want the user to be able
10787 to reference this entity using either natural or linkage name,
10788 but we haven't started looking at this enhancement yet. */
10789 const char *linkage_name = dw2_linkage_name (die, cu);
10791 if (linkage_name != NULL)
10792 return linkage_name;
10795 /* These are the only languages we know how to qualify names in. */
10797 && (cu->language == language_cplus
10798 || cu->language == language_fortran || cu->language == language_d
10799 || cu->language == language_rust))
10801 if (die_needs_namespace (die, cu))
10803 const char *prefix;
10804 const char *canonical_name = NULL;
10808 prefix = determine_prefix (die, cu);
10809 if (*prefix != '\0')
10811 char *prefixed_name = typename_concat (NULL, prefix, name,
10814 buf.puts (prefixed_name);
10815 xfree (prefixed_name);
10820 /* Template parameters may be specified in the DIE's DW_AT_name, or
10821 as children with DW_TAG_template_type_param or
10822 DW_TAG_value_type_param. If the latter, add them to the name
10823 here. If the name already has template parameters, then
10824 skip this step; some versions of GCC emit both, and
10825 it is more efficient to use the pre-computed name.
10827 Something to keep in mind about this process: it is very
10828 unlikely, or in some cases downright impossible, to produce
10829 something that will match the mangled name of a function.
10830 If the definition of the function has the same debug info,
10831 we should be able to match up with it anyway. But fallbacks
10832 using the minimal symbol, for instance to find a method
10833 implemented in a stripped copy of libstdc++, will not work.
10834 If we do not have debug info for the definition, we will have to
10835 match them up some other way.
10837 When we do name matching there is a related problem with function
10838 templates; two instantiated function templates are allowed to
10839 differ only by their return types, which we do not add here. */
10841 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10843 struct attribute *attr;
10844 struct die_info *child;
10847 die->building_fullname = 1;
10849 for (child = die->child; child != NULL; child = child->sibling)
10853 const gdb_byte *bytes;
10854 struct dwarf2_locexpr_baton *baton;
10857 if (child->tag != DW_TAG_template_type_param
10858 && child->tag != DW_TAG_template_value_param)
10869 attr = dwarf2_attr (child, DW_AT_type, cu);
10872 complaint (_("template parameter missing DW_AT_type"));
10873 buf.puts ("UNKNOWN_TYPE");
10876 type = die_type (child, cu);
10878 if (child->tag == DW_TAG_template_type_param)
10880 c_print_type (type, "", &buf, -1, 0, cu->language,
10881 &type_print_raw_options);
10885 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10888 complaint (_("template parameter missing "
10889 "DW_AT_const_value"));
10890 buf.puts ("UNKNOWN_VALUE");
10894 dwarf2_const_value_attr (attr, type, name,
10895 &cu->comp_unit_obstack, cu,
10896 &value, &bytes, &baton);
10898 if (TYPE_NOSIGN (type))
10899 /* GDB prints characters as NUMBER 'CHAR'. If that's
10900 changed, this can use value_print instead. */
10901 c_printchar (value, type, &buf);
10904 struct value_print_options opts;
10907 v = dwarf2_evaluate_loc_desc (type, NULL,
10911 else if (bytes != NULL)
10913 v = allocate_value (type);
10914 memcpy (value_contents_writeable (v), bytes,
10915 TYPE_LENGTH (type));
10918 v = value_from_longest (type, value);
10920 /* Specify decimal so that we do not depend on
10922 get_formatted_print_options (&opts, 'd');
10924 value_print (v, &buf, &opts);
10929 die->building_fullname = 0;
10933 /* Close the argument list, with a space if necessary
10934 (nested templates). */
10935 if (!buf.empty () && buf.string ().back () == '>')
10942 /* For C++ methods, append formal parameter type
10943 information, if PHYSNAME. */
10945 if (physname && die->tag == DW_TAG_subprogram
10946 && cu->language == language_cplus)
10948 struct type *type = read_type_die (die, cu);
10950 c_type_print_args (type, &buf, 1, cu->language,
10951 &type_print_raw_options);
10953 if (cu->language == language_cplus)
10955 /* Assume that an artificial first parameter is
10956 "this", but do not crash if it is not. RealView
10957 marks unnamed (and thus unused) parameters as
10958 artificial; there is no way to differentiate
10960 if (TYPE_NFIELDS (type) > 0
10961 && TYPE_FIELD_ARTIFICIAL (type, 0)
10962 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10963 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10965 buf.puts (" const");
10969 const std::string &intermediate_name = buf.string ();
10971 if (cu->language == language_cplus)
10973 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10974 &objfile->per_bfd->storage_obstack);
10976 /* If we only computed INTERMEDIATE_NAME, or if
10977 INTERMEDIATE_NAME is already canonical, then we need to
10978 copy it to the appropriate obstack. */
10979 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10980 name = ((const char *)
10981 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10982 intermediate_name.c_str (),
10983 intermediate_name.length ()));
10985 name = canonical_name;
10992 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10993 If scope qualifiers are appropriate they will be added. The result
10994 will be allocated on the storage_obstack, or NULL if the DIE does
10995 not have a name. NAME may either be from a previous call to
10996 dwarf2_name or NULL.
10998 The output string will be canonicalized (if C++). */
11000 static const char *
11001 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11003 return dwarf2_compute_name (name, die, cu, 0);
11006 /* Construct a physname for the given DIE in CU. NAME may either be
11007 from a previous call to dwarf2_name or NULL. The result will be
11008 allocated on the objfile_objstack or NULL if the DIE does not have a
11011 The output string will be canonicalized (if C++). */
11013 static const char *
11014 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11017 const char *retval, *mangled = NULL, *canon = NULL;
11020 /* In this case dwarf2_compute_name is just a shortcut not building anything
11022 if (!die_needs_namespace (die, cu))
11023 return dwarf2_compute_name (name, die, cu, 1);
11025 mangled = dw2_linkage_name (die, cu);
11027 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11028 See https://github.com/rust-lang/rust/issues/32925. */
11029 if (cu->language == language_rust && mangled != NULL
11030 && strchr (mangled, '{') != NULL)
11033 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11035 gdb::unique_xmalloc_ptr<char> demangled;
11036 if (mangled != NULL)
11039 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11041 /* Do nothing (do not demangle the symbol name). */
11043 else if (cu->language == language_go)
11045 /* This is a lie, but we already lie to the caller new_symbol.
11046 new_symbol assumes we return the mangled name.
11047 This just undoes that lie until things are cleaned up. */
11051 /* Use DMGL_RET_DROP for C++ template functions to suppress
11052 their return type. It is easier for GDB users to search
11053 for such functions as `name(params)' than `long name(params)'.
11054 In such case the minimal symbol names do not match the full
11055 symbol names but for template functions there is never a need
11056 to look up their definition from their declaration so
11057 the only disadvantage remains the minimal symbol variant
11058 `long name(params)' does not have the proper inferior type. */
11059 demangled.reset (gdb_demangle (mangled,
11060 (DMGL_PARAMS | DMGL_ANSI
11061 | DMGL_RET_DROP)));
11064 canon = demangled.get ();
11072 if (canon == NULL || check_physname)
11074 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11076 if (canon != NULL && strcmp (physname, canon) != 0)
11078 /* It may not mean a bug in GDB. The compiler could also
11079 compute DW_AT_linkage_name incorrectly. But in such case
11080 GDB would need to be bug-to-bug compatible. */
11082 complaint (_("Computed physname <%s> does not match demangled <%s> "
11083 "(from linkage <%s>) - DIE at %s [in module %s]"),
11084 physname, canon, mangled, sect_offset_str (die->sect_off),
11085 objfile_name (objfile));
11087 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11088 is available here - over computed PHYSNAME. It is safer
11089 against both buggy GDB and buggy compilers. */
11103 retval = ((const char *)
11104 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11105 retval, strlen (retval)));
11110 /* Inspect DIE in CU for a namespace alias. If one exists, record
11111 a new symbol for it.
11113 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11116 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11118 struct attribute *attr;
11120 /* If the die does not have a name, this is not a namespace
11122 attr = dwarf2_attr (die, DW_AT_name, cu);
11126 struct die_info *d = die;
11127 struct dwarf2_cu *imported_cu = cu;
11129 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11130 keep inspecting DIEs until we hit the underlying import. */
11131 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11132 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11134 attr = dwarf2_attr (d, DW_AT_import, cu);
11138 d = follow_die_ref (d, attr, &imported_cu);
11139 if (d->tag != DW_TAG_imported_declaration)
11143 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11145 complaint (_("DIE at %s has too many recursively imported "
11146 "declarations"), sect_offset_str (d->sect_off));
11153 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11155 type = get_die_type_at_offset (sect_off, cu->per_cu);
11156 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11158 /* This declaration is a global namespace alias. Add
11159 a symbol for it whose type is the aliased namespace. */
11160 new_symbol (die, type, cu);
11169 /* Return the using directives repository (global or local?) to use in the
11170 current context for CU.
11172 For Ada, imported declarations can materialize renamings, which *may* be
11173 global. However it is impossible (for now?) in DWARF to distinguish
11174 "external" imported declarations and "static" ones. As all imported
11175 declarations seem to be static in all other languages, make them all CU-wide
11176 global only in Ada. */
11178 static struct using_direct **
11179 using_directives (struct dwarf2_cu *cu)
11181 if (cu->language == language_ada
11182 && cu->get_builder ()->outermost_context_p ())
11183 return cu->get_builder ()->get_global_using_directives ();
11185 return cu->get_builder ()->get_local_using_directives ();
11188 /* Read the import statement specified by the given die and record it. */
11191 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11194 struct attribute *import_attr;
11195 struct die_info *imported_die, *child_die;
11196 struct dwarf2_cu *imported_cu;
11197 const char *imported_name;
11198 const char *imported_name_prefix;
11199 const char *canonical_name;
11200 const char *import_alias;
11201 const char *imported_declaration = NULL;
11202 const char *import_prefix;
11203 std::vector<const char *> excludes;
11205 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11206 if (import_attr == NULL)
11208 complaint (_("Tag '%s' has no DW_AT_import"),
11209 dwarf_tag_name (die->tag));
11214 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11215 imported_name = dwarf2_name (imported_die, imported_cu);
11216 if (imported_name == NULL)
11218 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11220 The import in the following code:
11234 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11235 <52> DW_AT_decl_file : 1
11236 <53> DW_AT_decl_line : 6
11237 <54> DW_AT_import : <0x75>
11238 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11239 <59> DW_AT_name : B
11240 <5b> DW_AT_decl_file : 1
11241 <5c> DW_AT_decl_line : 2
11242 <5d> DW_AT_type : <0x6e>
11244 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11245 <76> DW_AT_byte_size : 4
11246 <77> DW_AT_encoding : 5 (signed)
11248 imports the wrong die ( 0x75 instead of 0x58 ).
11249 This case will be ignored until the gcc bug is fixed. */
11253 /* Figure out the local name after import. */
11254 import_alias = dwarf2_name (die, cu);
11256 /* Figure out where the statement is being imported to. */
11257 import_prefix = determine_prefix (die, cu);
11259 /* Figure out what the scope of the imported die is and prepend it
11260 to the name of the imported die. */
11261 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11263 if (imported_die->tag != DW_TAG_namespace
11264 && imported_die->tag != DW_TAG_module)
11266 imported_declaration = imported_name;
11267 canonical_name = imported_name_prefix;
11269 else if (strlen (imported_name_prefix) > 0)
11270 canonical_name = obconcat (&objfile->objfile_obstack,
11271 imported_name_prefix,
11272 (cu->language == language_d ? "." : "::"),
11273 imported_name, (char *) NULL);
11275 canonical_name = imported_name;
11277 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11278 for (child_die = die->child; child_die && child_die->tag;
11279 child_die = sibling_die (child_die))
11281 /* DWARF-4: A Fortran use statement with a “rename list” may be
11282 represented by an imported module entry with an import attribute
11283 referring to the module and owned entries corresponding to those
11284 entities that are renamed as part of being imported. */
11286 if (child_die->tag != DW_TAG_imported_declaration)
11288 complaint (_("child DW_TAG_imported_declaration expected "
11289 "- DIE at %s [in module %s]"),
11290 sect_offset_str (child_die->sect_off),
11291 objfile_name (objfile));
11295 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11296 if (import_attr == NULL)
11298 complaint (_("Tag '%s' has no DW_AT_import"),
11299 dwarf_tag_name (child_die->tag));
11304 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11306 imported_name = dwarf2_name (imported_die, imported_cu);
11307 if (imported_name == NULL)
11309 complaint (_("child DW_TAG_imported_declaration has unknown "
11310 "imported name - DIE at %s [in module %s]"),
11311 sect_offset_str (child_die->sect_off),
11312 objfile_name (objfile));
11316 excludes.push_back (imported_name);
11318 process_die (child_die, cu);
11321 add_using_directive (using_directives (cu),
11325 imported_declaration,
11328 &objfile->objfile_obstack);
11331 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11332 types, but gives them a size of zero. Starting with version 14,
11333 ICC is compatible with GCC. */
11336 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11338 if (!cu->checked_producer)
11339 check_producer (cu);
11341 return cu->producer_is_icc_lt_14;
11344 /* ICC generates a DW_AT_type for C void functions. This was observed on
11345 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11346 which says that void functions should not have a DW_AT_type. */
11349 producer_is_icc (struct dwarf2_cu *cu)
11351 if (!cu->checked_producer)
11352 check_producer (cu);
11354 return cu->producer_is_icc;
11357 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11358 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11359 this, it was first present in GCC release 4.3.0. */
11362 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11364 if (!cu->checked_producer)
11365 check_producer (cu);
11367 return cu->producer_is_gcc_lt_4_3;
11370 static file_and_directory
11371 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11373 file_and_directory res;
11375 /* Find the filename. Do not use dwarf2_name here, since the filename
11376 is not a source language identifier. */
11377 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11378 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11380 if (res.comp_dir == NULL
11381 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11382 && IS_ABSOLUTE_PATH (res.name))
11384 res.comp_dir_storage = ldirname (res.name);
11385 if (!res.comp_dir_storage.empty ())
11386 res.comp_dir = res.comp_dir_storage.c_str ();
11388 if (res.comp_dir != NULL)
11390 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11391 directory, get rid of it. */
11392 const char *cp = strchr (res.comp_dir, ':');
11394 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11395 res.comp_dir = cp + 1;
11398 if (res.name == NULL)
11399 res.name = "<unknown>";
11404 /* Handle DW_AT_stmt_list for a compilation unit.
11405 DIE is the DW_TAG_compile_unit die for CU.
11406 COMP_DIR is the compilation directory. LOWPC is passed to
11407 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11410 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11411 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11413 struct dwarf2_per_objfile *dwarf2_per_objfile
11414 = cu->per_cu->dwarf2_per_objfile;
11415 struct objfile *objfile = dwarf2_per_objfile->objfile;
11416 struct attribute *attr;
11417 struct line_header line_header_local;
11418 hashval_t line_header_local_hash;
11420 int decode_mapping;
11422 gdb_assert (! cu->per_cu->is_debug_types);
11424 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11428 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11430 /* The line header hash table is only created if needed (it exists to
11431 prevent redundant reading of the line table for partial_units).
11432 If we're given a partial_unit, we'll need it. If we're given a
11433 compile_unit, then use the line header hash table if it's already
11434 created, but don't create one just yet. */
11436 if (dwarf2_per_objfile->line_header_hash == NULL
11437 && die->tag == DW_TAG_partial_unit)
11439 dwarf2_per_objfile->line_header_hash
11440 = htab_create_alloc_ex (127, line_header_hash_voidp,
11441 line_header_eq_voidp,
11442 free_line_header_voidp,
11443 &objfile->objfile_obstack,
11444 hashtab_obstack_allocate,
11445 dummy_obstack_deallocate);
11448 line_header_local.sect_off = line_offset;
11449 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11450 line_header_local_hash = line_header_hash (&line_header_local);
11451 if (dwarf2_per_objfile->line_header_hash != NULL)
11453 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11454 &line_header_local,
11455 line_header_local_hash, NO_INSERT);
11457 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11458 is not present in *SLOT (since if there is something in *SLOT then
11459 it will be for a partial_unit). */
11460 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11462 gdb_assert (*slot != NULL);
11463 cu->line_header = (struct line_header *) *slot;
11468 /* dwarf_decode_line_header does not yet provide sufficient information.
11469 We always have to call also dwarf_decode_lines for it. */
11470 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11474 cu->line_header = lh.release ();
11475 cu->line_header_die_owner = die;
11477 if (dwarf2_per_objfile->line_header_hash == NULL)
11481 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11482 &line_header_local,
11483 line_header_local_hash, INSERT);
11484 gdb_assert (slot != NULL);
11486 if (slot != NULL && *slot == NULL)
11488 /* This newly decoded line number information unit will be owned
11489 by line_header_hash hash table. */
11490 *slot = cu->line_header;
11491 cu->line_header_die_owner = NULL;
11495 /* We cannot free any current entry in (*slot) as that struct line_header
11496 may be already used by multiple CUs. Create only temporary decoded
11497 line_header for this CU - it may happen at most once for each line
11498 number information unit. And if we're not using line_header_hash
11499 then this is what we want as well. */
11500 gdb_assert (die->tag != DW_TAG_partial_unit);
11502 decode_mapping = (die->tag != DW_TAG_partial_unit);
11503 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11508 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11511 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11513 struct dwarf2_per_objfile *dwarf2_per_objfile
11514 = cu->per_cu->dwarf2_per_objfile;
11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
11516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11517 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11518 CORE_ADDR highpc = ((CORE_ADDR) 0);
11519 struct attribute *attr;
11520 struct die_info *child_die;
11521 CORE_ADDR baseaddr;
11523 prepare_one_comp_unit (cu, die, cu->language);
11524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11526 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11528 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11529 from finish_block. */
11530 if (lowpc == ((CORE_ADDR) -1))
11532 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11534 file_and_directory fnd = find_file_and_directory (die, cu);
11536 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11537 standardised yet. As a workaround for the language detection we fall
11538 back to the DW_AT_producer string. */
11539 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11540 cu->language = language_opencl;
11542 /* Similar hack for Go. */
11543 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11544 set_cu_language (DW_LANG_Go, cu);
11546 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11548 /* Decode line number information if present. We do this before
11549 processing child DIEs, so that the line header table is available
11550 for DW_AT_decl_file. */
11551 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11553 /* Process all dies in compilation unit. */
11554 if (die->child != NULL)
11556 child_die = die->child;
11557 while (child_die && child_die->tag)
11559 process_die (child_die, cu);
11560 child_die = sibling_die (child_die);
11564 /* Decode macro information, if present. Dwarf 2 macro information
11565 refers to information in the line number info statement program
11566 header, so we can only read it if we've read the header
11568 attr = dwarf2_attr (die, DW_AT_macros, cu);
11570 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11571 if (attr && cu->line_header)
11573 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11574 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11576 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11580 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11581 if (attr && cu->line_header)
11583 unsigned int macro_offset = DW_UNSND (attr);
11585 dwarf_decode_macros (cu, macro_offset, 0);
11591 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11593 struct type_unit_group *tu_group;
11595 struct attribute *attr;
11597 struct signatured_type *sig_type;
11599 gdb_assert (per_cu->is_debug_types);
11600 sig_type = (struct signatured_type *) per_cu;
11602 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11604 /* If we're using .gdb_index (includes -readnow) then
11605 per_cu->type_unit_group may not have been set up yet. */
11606 if (sig_type->type_unit_group == NULL)
11607 sig_type->type_unit_group = get_type_unit_group (this, attr);
11608 tu_group = sig_type->type_unit_group;
11610 /* If we've already processed this stmt_list there's no real need to
11611 do it again, we could fake it and just recreate the part we need
11612 (file name,index -> symtab mapping). If data shows this optimization
11613 is useful we can do it then. */
11614 first_time = tu_group->compunit_symtab == NULL;
11616 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11621 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11622 lh = dwarf_decode_line_header (line_offset, this);
11627 start_symtab ("", NULL, 0);
11630 gdb_assert (tu_group->symtabs == NULL);
11631 gdb_assert (m_builder == nullptr);
11632 struct compunit_symtab *cust = tu_group->compunit_symtab;
11633 m_builder.reset (new struct buildsym_compunit
11634 (COMPUNIT_OBJFILE (cust), "",
11635 COMPUNIT_DIRNAME (cust),
11636 compunit_language (cust),
11642 line_header = lh.release ();
11643 line_header_die_owner = die;
11647 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11649 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11650 still initializing it, and our caller (a few levels up)
11651 process_full_type_unit still needs to know if this is the first
11654 tu_group->num_symtabs = line_header->file_names.size ();
11655 tu_group->symtabs = XNEWVEC (struct symtab *,
11656 line_header->file_names.size ());
11658 for (i = 0; i < line_header->file_names.size (); ++i)
11660 file_entry &fe = line_header->file_names[i];
11662 dwarf2_start_subfile (this, fe.name,
11663 fe.include_dir (line_header));
11664 buildsym_compunit *b = get_builder ();
11665 if (b->get_current_subfile ()->symtab == NULL)
11667 /* NOTE: start_subfile will recognize when it's been
11668 passed a file it has already seen. So we can't
11669 assume there's a simple mapping from
11670 cu->line_header->file_names to subfiles, plus
11671 cu->line_header->file_names may contain dups. */
11672 b->get_current_subfile ()->symtab
11673 = allocate_symtab (cust, b->get_current_subfile ()->name);
11676 fe.symtab = b->get_current_subfile ()->symtab;
11677 tu_group->symtabs[i] = fe.symtab;
11682 gdb_assert (m_builder == nullptr);
11683 struct compunit_symtab *cust = tu_group->compunit_symtab;
11684 m_builder.reset (new struct buildsym_compunit
11685 (COMPUNIT_OBJFILE (cust), "",
11686 COMPUNIT_DIRNAME (cust),
11687 compunit_language (cust),
11690 for (i = 0; i < line_header->file_names.size (); ++i)
11692 file_entry &fe = line_header->file_names[i];
11694 fe.symtab = tu_group->symtabs[i];
11698 /* The main symtab is allocated last. Type units don't have DW_AT_name
11699 so they don't have a "real" (so to speak) symtab anyway.
11700 There is later code that will assign the main symtab to all symbols
11701 that don't have one. We need to handle the case of a symbol with a
11702 missing symtab (DW_AT_decl_file) anyway. */
11705 /* Process DW_TAG_type_unit.
11706 For TUs we want to skip the first top level sibling if it's not the
11707 actual type being defined by this TU. In this case the first top
11708 level sibling is there to provide context only. */
11711 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11713 struct die_info *child_die;
11715 prepare_one_comp_unit (cu, die, language_minimal);
11717 /* Initialize (or reinitialize) the machinery for building symtabs.
11718 We do this before processing child DIEs, so that the line header table
11719 is available for DW_AT_decl_file. */
11720 cu->setup_type_unit_groups (die);
11722 if (die->child != NULL)
11724 child_die = die->child;
11725 while (child_die && child_die->tag)
11727 process_die (child_die, cu);
11728 child_die = sibling_die (child_die);
11735 http://gcc.gnu.org/wiki/DebugFission
11736 http://gcc.gnu.org/wiki/DebugFissionDWP
11738 To simplify handling of both DWO files ("object" files with the DWARF info)
11739 and DWP files (a file with the DWOs packaged up into one file), we treat
11740 DWP files as having a collection of virtual DWO files. */
11743 hash_dwo_file (const void *item)
11745 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11748 hash = htab_hash_string (dwo_file->dwo_name);
11749 if (dwo_file->comp_dir != NULL)
11750 hash += htab_hash_string (dwo_file->comp_dir);
11755 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11757 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11758 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11760 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11762 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11763 return lhs->comp_dir == rhs->comp_dir;
11764 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11767 /* Allocate a hash table for DWO files. */
11770 allocate_dwo_file_hash_table (struct objfile *objfile)
11772 auto delete_dwo_file = [] (void *item)
11774 struct dwo_file *dwo_file = (struct dwo_file *) item;
11779 return htab_up (htab_create_alloc_ex (41,
11783 &objfile->objfile_obstack,
11784 hashtab_obstack_allocate,
11785 dummy_obstack_deallocate));
11788 /* Lookup DWO file DWO_NAME. */
11791 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11792 const char *dwo_name,
11793 const char *comp_dir)
11795 struct dwo_file find_entry;
11798 if (dwarf2_per_objfile->dwo_files == NULL)
11799 dwarf2_per_objfile->dwo_files
11800 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11802 find_entry.dwo_name = dwo_name;
11803 find_entry.comp_dir = comp_dir;
11804 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11811 hash_dwo_unit (const void *item)
11813 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11815 /* This drops the top 32 bits of the id, but is ok for a hash. */
11816 return dwo_unit->signature;
11820 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11822 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11823 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11825 /* The signature is assumed to be unique within the DWO file.
11826 So while object file CU dwo_id's always have the value zero,
11827 that's OK, assuming each object file DWO file has only one CU,
11828 and that's the rule for now. */
11829 return lhs->signature == rhs->signature;
11832 /* Allocate a hash table for DWO CUs,TUs.
11833 There is one of these tables for each of CUs,TUs for each DWO file. */
11836 allocate_dwo_unit_table (struct objfile *objfile)
11838 /* Start out with a pretty small number.
11839 Generally DWO files contain only one CU and maybe some TUs. */
11840 return htab_create_alloc_ex (3,
11844 &objfile->objfile_obstack,
11845 hashtab_obstack_allocate,
11846 dummy_obstack_deallocate);
11849 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11851 struct create_dwo_cu_data
11853 struct dwo_file *dwo_file;
11854 struct dwo_unit dwo_unit;
11857 /* die_reader_func for create_dwo_cu. */
11860 create_dwo_cu_reader (const struct die_reader_specs *reader,
11861 const gdb_byte *info_ptr,
11862 struct die_info *comp_unit_die,
11866 struct dwarf2_cu *cu = reader->cu;
11867 sect_offset sect_off = cu->per_cu->sect_off;
11868 struct dwarf2_section_info *section = cu->per_cu->section;
11869 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11870 struct dwo_file *dwo_file = data->dwo_file;
11871 struct dwo_unit *dwo_unit = &data->dwo_unit;
11872 struct attribute *attr;
11874 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11877 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11878 " its dwo_id [in module %s]"),
11879 sect_offset_str (sect_off), dwo_file->dwo_name);
11883 dwo_unit->dwo_file = dwo_file;
11884 dwo_unit->signature = DW_UNSND (attr);
11885 dwo_unit->section = section;
11886 dwo_unit->sect_off = sect_off;
11887 dwo_unit->length = cu->per_cu->length;
11889 if (dwarf_read_debug)
11890 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11891 sect_offset_str (sect_off),
11892 hex_string (dwo_unit->signature));
11895 /* Create the dwo_units for the CUs in a DWO_FILE.
11896 Note: This function processes DWO files only, not DWP files. */
11899 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11900 struct dwo_file &dwo_file, dwarf2_section_info §ion,
11903 struct objfile *objfile = dwarf2_per_objfile->objfile;
11904 const gdb_byte *info_ptr, *end_ptr;
11906 dwarf2_read_section (objfile, §ion);
11907 info_ptr = section.buffer;
11909 if (info_ptr == NULL)
11912 if (dwarf_read_debug)
11914 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11915 get_section_name (§ion),
11916 get_section_file_name (§ion));
11919 end_ptr = info_ptr + section.size;
11920 while (info_ptr < end_ptr)
11922 struct dwarf2_per_cu_data per_cu;
11923 struct create_dwo_cu_data create_dwo_cu_data;
11924 struct dwo_unit *dwo_unit;
11926 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11928 memset (&create_dwo_cu_data.dwo_unit, 0,
11929 sizeof (create_dwo_cu_data.dwo_unit));
11930 memset (&per_cu, 0, sizeof (per_cu));
11931 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11932 per_cu.is_debug_types = 0;
11933 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11934 per_cu.section = §ion;
11935 create_dwo_cu_data.dwo_file = &dwo_file;
11937 init_cutu_and_read_dies_no_follow (
11938 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11939 info_ptr += per_cu.length;
11941 // If the unit could not be parsed, skip it.
11942 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11945 if (cus_htab == NULL)
11946 cus_htab = allocate_dwo_unit_table (objfile);
11948 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11949 *dwo_unit = create_dwo_cu_data.dwo_unit;
11950 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11951 gdb_assert (slot != NULL);
11954 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11955 sect_offset dup_sect_off = dup_cu->sect_off;
11957 complaint (_("debug cu entry at offset %s is duplicate to"
11958 " the entry at offset %s, signature %s"),
11959 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11960 hex_string (dwo_unit->signature));
11962 *slot = (void *)dwo_unit;
11966 /* DWP file .debug_{cu,tu}_index section format:
11967 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11971 Both index sections have the same format, and serve to map a 64-bit
11972 signature to a set of section numbers. Each section begins with a header,
11973 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11974 indexes, and a pool of 32-bit section numbers. The index sections will be
11975 aligned at 8-byte boundaries in the file.
11977 The index section header consists of:
11979 V, 32 bit version number
11981 N, 32 bit number of compilation units or type units in the index
11982 M, 32 bit number of slots in the hash table
11984 Numbers are recorded using the byte order of the application binary.
11986 The hash table begins at offset 16 in the section, and consists of an array
11987 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11988 order of the application binary). Unused slots in the hash table are 0.
11989 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11991 The parallel table begins immediately after the hash table
11992 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11993 array of 32-bit indexes (using the byte order of the application binary),
11994 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11995 table contains a 32-bit index into the pool of section numbers. For unused
11996 hash table slots, the corresponding entry in the parallel table will be 0.
11998 The pool of section numbers begins immediately following the hash table
11999 (at offset 16 + 12 * M from the beginning of the section). The pool of
12000 section numbers consists of an array of 32-bit words (using the byte order
12001 of the application binary). Each item in the array is indexed starting
12002 from 0. The hash table entry provides the index of the first section
12003 number in the set. Additional section numbers in the set follow, and the
12004 set is terminated by a 0 entry (section number 0 is not used in ELF).
12006 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12007 section must be the first entry in the set, and the .debug_abbrev.dwo must
12008 be the second entry. Other members of the set may follow in any order.
12014 DWP Version 2 combines all the .debug_info, etc. sections into one,
12015 and the entries in the index tables are now offsets into these sections.
12016 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12019 Index Section Contents:
12021 Hash Table of Signatures dwp_hash_table.hash_table
12022 Parallel Table of Indices dwp_hash_table.unit_table
12023 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12024 Table of Section Sizes dwp_hash_table.v2.sizes
12026 The index section header consists of:
12028 V, 32 bit version number
12029 L, 32 bit number of columns in the table of section offsets
12030 N, 32 bit number of compilation units or type units in the index
12031 M, 32 bit number of slots in the hash table
12033 Numbers are recorded using the byte order of the application binary.
12035 The hash table has the same format as version 1.
12036 The parallel table of indices has the same format as version 1,
12037 except that the entries are origin-1 indices into the table of sections
12038 offsets and the table of section sizes.
12040 The table of offsets begins immediately following the parallel table
12041 (at offset 16 + 12 * M from the beginning of the section). The table is
12042 a two-dimensional array of 32-bit words (using the byte order of the
12043 application binary), with L columns and N+1 rows, in row-major order.
12044 Each row in the array is indexed starting from 0. The first row provides
12045 a key to the remaining rows: each column in this row provides an identifier
12046 for a debug section, and the offsets in the same column of subsequent rows
12047 refer to that section. The section identifiers are:
12049 DW_SECT_INFO 1 .debug_info.dwo
12050 DW_SECT_TYPES 2 .debug_types.dwo
12051 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12052 DW_SECT_LINE 4 .debug_line.dwo
12053 DW_SECT_LOC 5 .debug_loc.dwo
12054 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12055 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12056 DW_SECT_MACRO 8 .debug_macro.dwo
12058 The offsets provided by the CU and TU index sections are the base offsets
12059 for the contributions made by each CU or TU to the corresponding section
12060 in the package file. Each CU and TU header contains an abbrev_offset
12061 field, used to find the abbreviations table for that CU or TU within the
12062 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12063 be interpreted as relative to the base offset given in the index section.
12064 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12065 should be interpreted as relative to the base offset for .debug_line.dwo,
12066 and offsets into other debug sections obtained from DWARF attributes should
12067 also be interpreted as relative to the corresponding base offset.
12069 The table of sizes begins immediately following the table of offsets.
12070 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12071 with L columns and N rows, in row-major order. Each row in the array is
12072 indexed starting from 1 (row 0 is shared by the two tables).
12076 Hash table lookup is handled the same in version 1 and 2:
12078 We assume that N and M will not exceed 2^32 - 1.
12079 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12081 Given a 64-bit compilation unit signature or a type signature S, an entry
12082 in the hash table is located as follows:
12084 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12085 the low-order k bits all set to 1.
12087 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12089 3) If the hash table entry at index H matches the signature, use that
12090 entry. If the hash table entry at index H is unused (all zeroes),
12091 terminate the search: the signature is not present in the table.
12093 4) Let H = (H + H') modulo M. Repeat at Step 3.
12095 Because M > N and H' and M are relatively prime, the search is guaranteed
12096 to stop at an unused slot or find the match. */
12098 /* Create a hash table to map DWO IDs to their CU/TU entry in
12099 .debug_{info,types}.dwo in DWP_FILE.
12100 Returns NULL if there isn't one.
12101 Note: This function processes DWP files only, not DWO files. */
12103 static struct dwp_hash_table *
12104 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12105 struct dwp_file *dwp_file, int is_debug_types)
12107 struct objfile *objfile = dwarf2_per_objfile->objfile;
12108 bfd *dbfd = dwp_file->dbfd.get ();
12109 const gdb_byte *index_ptr, *index_end;
12110 struct dwarf2_section_info *index;
12111 uint32_t version, nr_columns, nr_units, nr_slots;
12112 struct dwp_hash_table *htab;
12114 if (is_debug_types)
12115 index = &dwp_file->sections.tu_index;
12117 index = &dwp_file->sections.cu_index;
12119 if (dwarf2_section_empty_p (index))
12121 dwarf2_read_section (objfile, index);
12123 index_ptr = index->buffer;
12124 index_end = index_ptr + index->size;
12126 version = read_4_bytes (dbfd, index_ptr);
12129 nr_columns = read_4_bytes (dbfd, index_ptr);
12133 nr_units = read_4_bytes (dbfd, index_ptr);
12135 nr_slots = read_4_bytes (dbfd, index_ptr);
12138 if (version != 1 && version != 2)
12140 error (_("Dwarf Error: unsupported DWP file version (%s)"
12141 " [in module %s]"),
12142 pulongest (version), dwp_file->name);
12144 if (nr_slots != (nr_slots & -nr_slots))
12146 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12147 " is not power of 2 [in module %s]"),
12148 pulongest (nr_slots), dwp_file->name);
12151 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12152 htab->version = version;
12153 htab->nr_columns = nr_columns;
12154 htab->nr_units = nr_units;
12155 htab->nr_slots = nr_slots;
12156 htab->hash_table = index_ptr;
12157 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12159 /* Exit early if the table is empty. */
12160 if (nr_slots == 0 || nr_units == 0
12161 || (version == 2 && nr_columns == 0))
12163 /* All must be zero. */
12164 if (nr_slots != 0 || nr_units != 0
12165 || (version == 2 && nr_columns != 0))
12167 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12168 " all zero [in modules %s]"),
12176 htab->section_pool.v1.indices =
12177 htab->unit_table + sizeof (uint32_t) * nr_slots;
12178 /* It's harder to decide whether the section is too small in v1.
12179 V1 is deprecated anyway so we punt. */
12183 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12184 int *ids = htab->section_pool.v2.section_ids;
12185 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12186 /* Reverse map for error checking. */
12187 int ids_seen[DW_SECT_MAX + 1];
12190 if (nr_columns < 2)
12192 error (_("Dwarf Error: bad DWP hash table, too few columns"
12193 " in section table [in module %s]"),
12196 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12198 error (_("Dwarf Error: bad DWP hash table, too many columns"
12199 " in section table [in module %s]"),
12202 memset (ids, 255, sizeof_ids);
12203 memset (ids_seen, 255, sizeof (ids_seen));
12204 for (i = 0; i < nr_columns; ++i)
12206 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12208 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12210 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12211 " in section table [in module %s]"),
12212 id, dwp_file->name);
12214 if (ids_seen[id] != -1)
12216 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12217 " id %d in section table [in module %s]"),
12218 id, dwp_file->name);
12223 /* Must have exactly one info or types section. */
12224 if (((ids_seen[DW_SECT_INFO] != -1)
12225 + (ids_seen[DW_SECT_TYPES] != -1))
12228 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12229 " DWO info/types section [in module %s]"),
12232 /* Must have an abbrev section. */
12233 if (ids_seen[DW_SECT_ABBREV] == -1)
12235 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12236 " section [in module %s]"),
12239 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12240 htab->section_pool.v2.sizes =
12241 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12242 * nr_units * nr_columns);
12243 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12244 * nr_units * nr_columns))
12247 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12248 " [in module %s]"),
12256 /* Update SECTIONS with the data from SECTP.
12258 This function is like the other "locate" section routines that are
12259 passed to bfd_map_over_sections, but in this context the sections to
12260 read comes from the DWP V1 hash table, not the full ELF section table.
12262 The result is non-zero for success, or zero if an error was found. */
12265 locate_v1_virtual_dwo_sections (asection *sectp,
12266 struct virtual_v1_dwo_sections *sections)
12268 const struct dwop_section_names *names = &dwop_section_names;
12270 if (section_is_p (sectp->name, &names->abbrev_dwo))
12272 /* There can be only one. */
12273 if (sections->abbrev.s.section != NULL)
12275 sections->abbrev.s.section = sectp;
12276 sections->abbrev.size = bfd_get_section_size (sectp);
12278 else if (section_is_p (sectp->name, &names->info_dwo)
12279 || section_is_p (sectp->name, &names->types_dwo))
12281 /* There can be only one. */
12282 if (sections->info_or_types.s.section != NULL)
12284 sections->info_or_types.s.section = sectp;
12285 sections->info_or_types.size = bfd_get_section_size (sectp);
12287 else if (section_is_p (sectp->name, &names->line_dwo))
12289 /* There can be only one. */
12290 if (sections->line.s.section != NULL)
12292 sections->line.s.section = sectp;
12293 sections->line.size = bfd_get_section_size (sectp);
12295 else if (section_is_p (sectp->name, &names->loc_dwo))
12297 /* There can be only one. */
12298 if (sections->loc.s.section != NULL)
12300 sections->loc.s.section = sectp;
12301 sections->loc.size = bfd_get_section_size (sectp);
12303 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12305 /* There can be only one. */
12306 if (sections->macinfo.s.section != NULL)
12308 sections->macinfo.s.section = sectp;
12309 sections->macinfo.size = bfd_get_section_size (sectp);
12311 else if (section_is_p (sectp->name, &names->macro_dwo))
12313 /* There can be only one. */
12314 if (sections->macro.s.section != NULL)
12316 sections->macro.s.section = sectp;
12317 sections->macro.size = bfd_get_section_size (sectp);
12319 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12321 /* There can be only one. */
12322 if (sections->str_offsets.s.section != NULL)
12324 sections->str_offsets.s.section = sectp;
12325 sections->str_offsets.size = bfd_get_section_size (sectp);
12329 /* No other kind of section is valid. */
12336 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12337 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12338 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12339 This is for DWP version 1 files. */
12341 static struct dwo_unit *
12342 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12343 struct dwp_file *dwp_file,
12344 uint32_t unit_index,
12345 const char *comp_dir,
12346 ULONGEST signature, int is_debug_types)
12348 struct objfile *objfile = dwarf2_per_objfile->objfile;
12349 const struct dwp_hash_table *dwp_htab =
12350 is_debug_types ? dwp_file->tus : dwp_file->cus;
12351 bfd *dbfd = dwp_file->dbfd.get ();
12352 const char *kind = is_debug_types ? "TU" : "CU";
12353 struct dwo_file *dwo_file;
12354 struct dwo_unit *dwo_unit;
12355 struct virtual_v1_dwo_sections sections;
12356 void **dwo_file_slot;
12359 gdb_assert (dwp_file->version == 1);
12361 if (dwarf_read_debug)
12363 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12365 pulongest (unit_index), hex_string (signature),
12369 /* Fetch the sections of this DWO unit.
12370 Put a limit on the number of sections we look for so that bad data
12371 doesn't cause us to loop forever. */
12373 #define MAX_NR_V1_DWO_SECTIONS \
12374 (1 /* .debug_info or .debug_types */ \
12375 + 1 /* .debug_abbrev */ \
12376 + 1 /* .debug_line */ \
12377 + 1 /* .debug_loc */ \
12378 + 1 /* .debug_str_offsets */ \
12379 + 1 /* .debug_macro or .debug_macinfo */ \
12380 + 1 /* trailing zero */)
12382 memset (§ions, 0, sizeof (sections));
12384 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12387 uint32_t section_nr =
12388 read_4_bytes (dbfd,
12389 dwp_htab->section_pool.v1.indices
12390 + (unit_index + i) * sizeof (uint32_t));
12392 if (section_nr == 0)
12394 if (section_nr >= dwp_file->num_sections)
12396 error (_("Dwarf Error: bad DWP hash table, section number too large"
12397 " [in module %s]"),
12401 sectp = dwp_file->elf_sections[section_nr];
12402 if (! locate_v1_virtual_dwo_sections (sectp, §ions))
12404 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12405 " [in module %s]"),
12411 || dwarf2_section_empty_p (§ions.info_or_types)
12412 || dwarf2_section_empty_p (§ions.abbrev))
12414 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12415 " [in module %s]"),
12418 if (i == MAX_NR_V1_DWO_SECTIONS)
12420 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12421 " [in module %s]"),
12425 /* It's easier for the rest of the code if we fake a struct dwo_file and
12426 have dwo_unit "live" in that. At least for now.
12428 The DWP file can be made up of a random collection of CUs and TUs.
12429 However, for each CU + set of TUs that came from the same original DWO
12430 file, we can combine them back into a virtual DWO file to save space
12431 (fewer struct dwo_file objects to allocate). Remember that for really
12432 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12434 std::string virtual_dwo_name =
12435 string_printf ("virtual-dwo/%d-%d-%d-%d",
12436 get_section_id (§ions.abbrev),
12437 get_section_id (§ions.line),
12438 get_section_id (§ions.loc),
12439 get_section_id (§ions.str_offsets));
12440 /* Can we use an existing virtual DWO file? */
12441 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12442 virtual_dwo_name.c_str (),
12444 /* Create one if necessary. */
12445 if (*dwo_file_slot == NULL)
12447 if (dwarf_read_debug)
12449 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12450 virtual_dwo_name.c_str ());
12452 dwo_file = new struct dwo_file;
12454 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12455 virtual_dwo_name.c_str (),
12456 virtual_dwo_name.size ());
12457 dwo_file->comp_dir = comp_dir;
12458 dwo_file->sections.abbrev = sections.abbrev;
12459 dwo_file->sections.line = sections.line;
12460 dwo_file->sections.loc = sections.loc;
12461 dwo_file->sections.macinfo = sections.macinfo;
12462 dwo_file->sections.macro = sections.macro;
12463 dwo_file->sections.str_offsets = sections.str_offsets;
12464 /* The "str" section is global to the entire DWP file. */
12465 dwo_file->sections.str = dwp_file->sections.str;
12466 /* The info or types section is assigned below to dwo_unit,
12467 there's no need to record it in dwo_file.
12468 Also, we can't simply record type sections in dwo_file because
12469 we record a pointer into the vector in dwo_unit. As we collect more
12470 types we'll grow the vector and eventually have to reallocate space
12471 for it, invalidating all copies of pointers into the previous
12473 *dwo_file_slot = dwo_file;
12477 if (dwarf_read_debug)
12479 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12480 virtual_dwo_name.c_str ());
12482 dwo_file = (struct dwo_file *) *dwo_file_slot;
12485 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12486 dwo_unit->dwo_file = dwo_file;
12487 dwo_unit->signature = signature;
12488 dwo_unit->section =
12489 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12490 *dwo_unit->section = sections.info_or_types;
12491 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12496 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12497 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12498 piece within that section used by a TU/CU, return a virtual section
12499 of just that piece. */
12501 static struct dwarf2_section_info
12502 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12503 struct dwarf2_section_info *section,
12504 bfd_size_type offset, bfd_size_type size)
12506 struct dwarf2_section_info result;
12509 gdb_assert (section != NULL);
12510 gdb_assert (!section->is_virtual);
12512 memset (&result, 0, sizeof (result));
12513 result.s.containing_section = section;
12514 result.is_virtual = true;
12519 sectp = get_section_bfd_section (section);
12521 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12522 bounds of the real section. This is a pretty-rare event, so just
12523 flag an error (easier) instead of a warning and trying to cope. */
12525 || offset + size > bfd_get_section_size (sectp))
12527 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12528 " in section %s [in module %s]"),
12529 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12530 objfile_name (dwarf2_per_objfile->objfile));
12533 result.virtual_offset = offset;
12534 result.size = size;
12538 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12539 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12540 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12541 This is for DWP version 2 files. */
12543 static struct dwo_unit *
12544 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12545 struct dwp_file *dwp_file,
12546 uint32_t unit_index,
12547 const char *comp_dir,
12548 ULONGEST signature, int is_debug_types)
12550 struct objfile *objfile = dwarf2_per_objfile->objfile;
12551 const struct dwp_hash_table *dwp_htab =
12552 is_debug_types ? dwp_file->tus : dwp_file->cus;
12553 bfd *dbfd = dwp_file->dbfd.get ();
12554 const char *kind = is_debug_types ? "TU" : "CU";
12555 struct dwo_file *dwo_file;
12556 struct dwo_unit *dwo_unit;
12557 struct virtual_v2_dwo_sections sections;
12558 void **dwo_file_slot;
12561 gdb_assert (dwp_file->version == 2);
12563 if (dwarf_read_debug)
12565 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12567 pulongest (unit_index), hex_string (signature),
12571 /* Fetch the section offsets of this DWO unit. */
12573 memset (§ions, 0, sizeof (sections));
12575 for (i = 0; i < dwp_htab->nr_columns; ++i)
12577 uint32_t offset = read_4_bytes (dbfd,
12578 dwp_htab->section_pool.v2.offsets
12579 + (((unit_index - 1) * dwp_htab->nr_columns
12581 * sizeof (uint32_t)));
12582 uint32_t size = read_4_bytes (dbfd,
12583 dwp_htab->section_pool.v2.sizes
12584 + (((unit_index - 1) * dwp_htab->nr_columns
12586 * sizeof (uint32_t)));
12588 switch (dwp_htab->section_pool.v2.section_ids[i])
12591 case DW_SECT_TYPES:
12592 sections.info_or_types_offset = offset;
12593 sections.info_or_types_size = size;
12595 case DW_SECT_ABBREV:
12596 sections.abbrev_offset = offset;
12597 sections.abbrev_size = size;
12600 sections.line_offset = offset;
12601 sections.line_size = size;
12604 sections.loc_offset = offset;
12605 sections.loc_size = size;
12607 case DW_SECT_STR_OFFSETS:
12608 sections.str_offsets_offset = offset;
12609 sections.str_offsets_size = size;
12611 case DW_SECT_MACINFO:
12612 sections.macinfo_offset = offset;
12613 sections.macinfo_size = size;
12615 case DW_SECT_MACRO:
12616 sections.macro_offset = offset;
12617 sections.macro_size = size;
12622 /* It's easier for the rest of the code if we fake a struct dwo_file and
12623 have dwo_unit "live" in that. At least for now.
12625 The DWP file can be made up of a random collection of CUs and TUs.
12626 However, for each CU + set of TUs that came from the same original DWO
12627 file, we can combine them back into a virtual DWO file to save space
12628 (fewer struct dwo_file objects to allocate). Remember that for really
12629 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12631 std::string virtual_dwo_name =
12632 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12633 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12634 (long) (sections.line_size ? sections.line_offset : 0),
12635 (long) (sections.loc_size ? sections.loc_offset : 0),
12636 (long) (sections.str_offsets_size
12637 ? sections.str_offsets_offset : 0));
12638 /* Can we use an existing virtual DWO file? */
12639 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12640 virtual_dwo_name.c_str (),
12642 /* Create one if necessary. */
12643 if (*dwo_file_slot == NULL)
12645 if (dwarf_read_debug)
12647 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12648 virtual_dwo_name.c_str ());
12650 dwo_file = new struct dwo_file;
12652 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12653 virtual_dwo_name.c_str (),
12654 virtual_dwo_name.size ());
12655 dwo_file->comp_dir = comp_dir;
12656 dwo_file->sections.abbrev =
12657 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12658 sections.abbrev_offset, sections.abbrev_size);
12659 dwo_file->sections.line =
12660 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12661 sections.line_offset, sections.line_size);
12662 dwo_file->sections.loc =
12663 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12664 sections.loc_offset, sections.loc_size);
12665 dwo_file->sections.macinfo =
12666 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12667 sections.macinfo_offset, sections.macinfo_size);
12668 dwo_file->sections.macro =
12669 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12670 sections.macro_offset, sections.macro_size);
12671 dwo_file->sections.str_offsets =
12672 create_dwp_v2_section (dwarf2_per_objfile,
12673 &dwp_file->sections.str_offsets,
12674 sections.str_offsets_offset,
12675 sections.str_offsets_size);
12676 /* The "str" section is global to the entire DWP file. */
12677 dwo_file->sections.str = dwp_file->sections.str;
12678 /* The info or types section is assigned below to dwo_unit,
12679 there's no need to record it in dwo_file.
12680 Also, we can't simply record type sections in dwo_file because
12681 we record a pointer into the vector in dwo_unit. As we collect more
12682 types we'll grow the vector and eventually have to reallocate space
12683 for it, invalidating all copies of pointers into the previous
12685 *dwo_file_slot = dwo_file;
12689 if (dwarf_read_debug)
12691 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12692 virtual_dwo_name.c_str ());
12694 dwo_file = (struct dwo_file *) *dwo_file_slot;
12697 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12698 dwo_unit->dwo_file = dwo_file;
12699 dwo_unit->signature = signature;
12700 dwo_unit->section =
12701 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12702 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12704 ? &dwp_file->sections.types
12705 : &dwp_file->sections.info,
12706 sections.info_or_types_offset,
12707 sections.info_or_types_size);
12708 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12713 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12714 Returns NULL if the signature isn't found. */
12716 static struct dwo_unit *
12717 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12718 struct dwp_file *dwp_file, const char *comp_dir,
12719 ULONGEST signature, int is_debug_types)
12721 const struct dwp_hash_table *dwp_htab =
12722 is_debug_types ? dwp_file->tus : dwp_file->cus;
12723 bfd *dbfd = dwp_file->dbfd.get ();
12724 uint32_t mask = dwp_htab->nr_slots - 1;
12725 uint32_t hash = signature & mask;
12726 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12729 struct dwo_unit find_dwo_cu;
12731 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12732 find_dwo_cu.signature = signature;
12733 slot = htab_find_slot (is_debug_types
12734 ? dwp_file->loaded_tus
12735 : dwp_file->loaded_cus,
12736 &find_dwo_cu, INSERT);
12739 return (struct dwo_unit *) *slot;
12741 /* Use a for loop so that we don't loop forever on bad debug info. */
12742 for (i = 0; i < dwp_htab->nr_slots; ++i)
12744 ULONGEST signature_in_table;
12746 signature_in_table =
12747 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12748 if (signature_in_table == signature)
12750 uint32_t unit_index =
12751 read_4_bytes (dbfd,
12752 dwp_htab->unit_table + hash * sizeof (uint32_t));
12754 if (dwp_file->version == 1)
12756 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12757 dwp_file, unit_index,
12758 comp_dir, signature,
12763 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12764 dwp_file, unit_index,
12765 comp_dir, signature,
12768 return (struct dwo_unit *) *slot;
12770 if (signature_in_table == 0)
12772 hash = (hash + hash2) & mask;
12775 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12776 " [in module %s]"),
12780 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12781 Open the file specified by FILE_NAME and hand it off to BFD for
12782 preliminary analysis. Return a newly initialized bfd *, which
12783 includes a canonicalized copy of FILE_NAME.
12784 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12785 SEARCH_CWD is true if the current directory is to be searched.
12786 It will be searched before debug-file-directory.
12787 If successful, the file is added to the bfd include table of the
12788 objfile's bfd (see gdb_bfd_record_inclusion).
12789 If unable to find/open the file, return NULL.
12790 NOTE: This function is derived from symfile_bfd_open. */
12792 static gdb_bfd_ref_ptr
12793 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12794 const char *file_name, int is_dwp, int search_cwd)
12797 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12798 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12799 to debug_file_directory. */
12800 const char *search_path;
12801 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12803 gdb::unique_xmalloc_ptr<char> search_path_holder;
12806 if (*debug_file_directory != '\0')
12808 search_path_holder.reset (concat (".", dirname_separator_string,
12809 debug_file_directory,
12811 search_path = search_path_holder.get ();
12817 search_path = debug_file_directory;
12819 openp_flags flags = OPF_RETURN_REALPATH;
12821 flags |= OPF_SEARCH_IN_PATH;
12823 gdb::unique_xmalloc_ptr<char> absolute_name;
12824 desc = openp (search_path, flags, file_name,
12825 O_RDONLY | O_BINARY, &absolute_name);
12829 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12831 if (sym_bfd == NULL)
12833 bfd_set_cacheable (sym_bfd.get (), 1);
12835 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12838 /* Success. Record the bfd as having been included by the objfile's bfd.
12839 This is important because things like demangled_names_hash lives in the
12840 objfile's per_bfd space and may have references to things like symbol
12841 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12842 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12847 /* Try to open DWO file FILE_NAME.
12848 COMP_DIR is the DW_AT_comp_dir attribute.
12849 The result is the bfd handle of the file.
12850 If there is a problem finding or opening the file, return NULL.
12851 Upon success, the canonicalized path of the file is stored in the bfd,
12852 same as symfile_bfd_open. */
12854 static gdb_bfd_ref_ptr
12855 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12856 const char *file_name, const char *comp_dir)
12858 if (IS_ABSOLUTE_PATH (file_name))
12859 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12860 0 /*is_dwp*/, 0 /*search_cwd*/);
12862 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12864 if (comp_dir != NULL)
12866 char *path_to_try = concat (comp_dir, SLASH_STRING,
12867 file_name, (char *) NULL);
12869 /* NOTE: If comp_dir is a relative path, this will also try the
12870 search path, which seems useful. */
12871 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12874 1 /*search_cwd*/));
12875 xfree (path_to_try);
12880 /* That didn't work, try debug-file-directory, which, despite its name,
12881 is a list of paths. */
12883 if (*debug_file_directory == '\0')
12886 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12887 0 /*is_dwp*/, 1 /*search_cwd*/);
12890 /* This function is mapped across the sections and remembers the offset and
12891 size of each of the DWO debugging sections we are interested in. */
12894 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12896 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12897 const struct dwop_section_names *names = &dwop_section_names;
12899 if (section_is_p (sectp->name, &names->abbrev_dwo))
12901 dwo_sections->abbrev.s.section = sectp;
12902 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12904 else if (section_is_p (sectp->name, &names->info_dwo))
12906 dwo_sections->info.s.section = sectp;
12907 dwo_sections->info.size = bfd_get_section_size (sectp);
12909 else if (section_is_p (sectp->name, &names->line_dwo))
12911 dwo_sections->line.s.section = sectp;
12912 dwo_sections->line.size = bfd_get_section_size (sectp);
12914 else if (section_is_p (sectp->name, &names->loc_dwo))
12916 dwo_sections->loc.s.section = sectp;
12917 dwo_sections->loc.size = bfd_get_section_size (sectp);
12919 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12921 dwo_sections->macinfo.s.section = sectp;
12922 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12924 else if (section_is_p (sectp->name, &names->macro_dwo))
12926 dwo_sections->macro.s.section = sectp;
12927 dwo_sections->macro.size = bfd_get_section_size (sectp);
12929 else if (section_is_p (sectp->name, &names->str_dwo))
12931 dwo_sections->str.s.section = sectp;
12932 dwo_sections->str.size = bfd_get_section_size (sectp);
12934 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12936 dwo_sections->str_offsets.s.section = sectp;
12937 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12939 else if (section_is_p (sectp->name, &names->types_dwo))
12941 struct dwarf2_section_info type_section;
12943 memset (&type_section, 0, sizeof (type_section));
12944 type_section.s.section = sectp;
12945 type_section.size = bfd_get_section_size (sectp);
12946 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12951 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12952 by PER_CU. This is for the non-DWP case.
12953 The result is NULL if DWO_NAME can't be found. */
12955 static struct dwo_file *
12956 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12957 const char *dwo_name, const char *comp_dir)
12959 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12961 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12964 if (dwarf_read_debug)
12965 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12969 dwo_file_up dwo_file (new struct dwo_file);
12970 dwo_file->dwo_name = dwo_name;
12971 dwo_file->comp_dir = comp_dir;
12972 dwo_file->dbfd = std::move (dbfd);
12974 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12975 &dwo_file->sections);
12977 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12980 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12981 dwo_file->sections.types, dwo_file->tus);
12983 if (dwarf_read_debug)
12984 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12986 return dwo_file.release ();
12989 /* This function is mapped across the sections and remembers the offset and
12990 size of each of the DWP debugging sections common to version 1 and 2 that
12991 we are interested in. */
12994 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12995 void *dwp_file_ptr)
12997 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12998 const struct dwop_section_names *names = &dwop_section_names;
12999 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13001 /* Record the ELF section number for later lookup: this is what the
13002 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13003 gdb_assert (elf_section_nr < dwp_file->num_sections);
13004 dwp_file->elf_sections[elf_section_nr] = sectp;
13006 /* Look for specific sections that we need. */
13007 if (section_is_p (sectp->name, &names->str_dwo))
13009 dwp_file->sections.str.s.section = sectp;
13010 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13012 else if (section_is_p (sectp->name, &names->cu_index))
13014 dwp_file->sections.cu_index.s.section = sectp;
13015 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13017 else if (section_is_p (sectp->name, &names->tu_index))
13019 dwp_file->sections.tu_index.s.section = sectp;
13020 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13024 /* This function is mapped across the sections and remembers the offset and
13025 size of each of the DWP version 2 debugging sections that we are interested
13026 in. This is split into a separate function because we don't know if we
13027 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13030 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13032 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13033 const struct dwop_section_names *names = &dwop_section_names;
13034 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13036 /* Record the ELF section number for later lookup: this is what the
13037 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13038 gdb_assert (elf_section_nr < dwp_file->num_sections);
13039 dwp_file->elf_sections[elf_section_nr] = sectp;
13041 /* Look for specific sections that we need. */
13042 if (section_is_p (sectp->name, &names->abbrev_dwo))
13044 dwp_file->sections.abbrev.s.section = sectp;
13045 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13047 else if (section_is_p (sectp->name, &names->info_dwo))
13049 dwp_file->sections.info.s.section = sectp;
13050 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13052 else if (section_is_p (sectp->name, &names->line_dwo))
13054 dwp_file->sections.line.s.section = sectp;
13055 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13057 else if (section_is_p (sectp->name, &names->loc_dwo))
13059 dwp_file->sections.loc.s.section = sectp;
13060 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13062 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13064 dwp_file->sections.macinfo.s.section = sectp;
13065 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13067 else if (section_is_p (sectp->name, &names->macro_dwo))
13069 dwp_file->sections.macro.s.section = sectp;
13070 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13072 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13074 dwp_file->sections.str_offsets.s.section = sectp;
13075 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13077 else if (section_is_p (sectp->name, &names->types_dwo))
13079 dwp_file->sections.types.s.section = sectp;
13080 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13084 /* Hash function for dwp_file loaded CUs/TUs. */
13087 hash_dwp_loaded_cutus (const void *item)
13089 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13091 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13092 return dwo_unit->signature;
13095 /* Equality function for dwp_file loaded CUs/TUs. */
13098 eq_dwp_loaded_cutus (const void *a, const void *b)
13100 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13101 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13103 return dua->signature == dub->signature;
13106 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13109 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13111 return htab_create_alloc_ex (3,
13112 hash_dwp_loaded_cutus,
13113 eq_dwp_loaded_cutus,
13115 &objfile->objfile_obstack,
13116 hashtab_obstack_allocate,
13117 dummy_obstack_deallocate);
13120 /* Try to open DWP file FILE_NAME.
13121 The result is the bfd handle of the file.
13122 If there is a problem finding or opening the file, return NULL.
13123 Upon success, the canonicalized path of the file is stored in the bfd,
13124 same as symfile_bfd_open. */
13126 static gdb_bfd_ref_ptr
13127 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13128 const char *file_name)
13130 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13132 1 /*search_cwd*/));
13136 /* Work around upstream bug 15652.
13137 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13138 [Whether that's a "bug" is debatable, but it is getting in our way.]
13139 We have no real idea where the dwp file is, because gdb's realpath-ing
13140 of the executable's path may have discarded the needed info.
13141 [IWBN if the dwp file name was recorded in the executable, akin to
13142 .gnu_debuglink, but that doesn't exist yet.]
13143 Strip the directory from FILE_NAME and search again. */
13144 if (*debug_file_directory != '\0')
13146 /* Don't implicitly search the current directory here.
13147 If the user wants to search "." to handle this case,
13148 it must be added to debug-file-directory. */
13149 return try_open_dwop_file (dwarf2_per_objfile,
13150 lbasename (file_name), 1 /*is_dwp*/,
13157 /* Initialize the use of the DWP file for the current objfile.
13158 By convention the name of the DWP file is ${objfile}.dwp.
13159 The result is NULL if it can't be found. */
13161 static std::unique_ptr<struct dwp_file>
13162 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13164 struct objfile *objfile = dwarf2_per_objfile->objfile;
13166 /* Try to find first .dwp for the binary file before any symbolic links
13169 /* If the objfile is a debug file, find the name of the real binary
13170 file and get the name of dwp file from there. */
13171 std::string dwp_name;
13172 if (objfile->separate_debug_objfile_backlink != NULL)
13174 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13175 const char *backlink_basename = lbasename (backlink->original_name);
13177 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13180 dwp_name = objfile->original_name;
13182 dwp_name += ".dwp";
13184 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13186 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13188 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13189 dwp_name = objfile_name (objfile);
13190 dwp_name += ".dwp";
13191 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13196 if (dwarf_read_debug)
13197 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13198 return std::unique_ptr<dwp_file> ();
13201 const char *name = bfd_get_filename (dbfd.get ());
13202 std::unique_ptr<struct dwp_file> dwp_file
13203 (new struct dwp_file (name, std::move (dbfd)));
13205 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13206 dwp_file->elf_sections =
13207 OBSTACK_CALLOC (&objfile->objfile_obstack,
13208 dwp_file->num_sections, asection *);
13210 bfd_map_over_sections (dwp_file->dbfd.get (),
13211 dwarf2_locate_common_dwp_sections,
13214 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13217 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13220 /* The DWP file version is stored in the hash table. Oh well. */
13221 if (dwp_file->cus && dwp_file->tus
13222 && dwp_file->cus->version != dwp_file->tus->version)
13224 /* Technically speaking, we should try to limp along, but this is
13225 pretty bizarre. We use pulongest here because that's the established
13226 portability solution (e.g, we cannot use %u for uint32_t). */
13227 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13228 " TU version %s [in DWP file %s]"),
13229 pulongest (dwp_file->cus->version),
13230 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13234 dwp_file->version = dwp_file->cus->version;
13235 else if (dwp_file->tus)
13236 dwp_file->version = dwp_file->tus->version;
13238 dwp_file->version = 2;
13240 if (dwp_file->version == 2)
13241 bfd_map_over_sections (dwp_file->dbfd.get (),
13242 dwarf2_locate_v2_dwp_sections,
13245 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13246 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13248 if (dwarf_read_debug)
13250 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13251 fprintf_unfiltered (gdb_stdlog,
13252 " %s CUs, %s TUs\n",
13253 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13254 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13260 /* Wrapper around open_and_init_dwp_file, only open it once. */
13262 static struct dwp_file *
13263 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13265 if (! dwarf2_per_objfile->dwp_checked)
13267 dwarf2_per_objfile->dwp_file
13268 = open_and_init_dwp_file (dwarf2_per_objfile);
13269 dwarf2_per_objfile->dwp_checked = 1;
13271 return dwarf2_per_objfile->dwp_file.get ();
13274 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13275 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13276 or in the DWP file for the objfile, referenced by THIS_UNIT.
13277 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13278 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13280 This is called, for example, when wanting to read a variable with a
13281 complex location. Therefore we don't want to do file i/o for every call.
13282 Therefore we don't want to look for a DWO file on every call.
13283 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13284 then we check if we've already seen DWO_NAME, and only THEN do we check
13287 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13288 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13290 static struct dwo_unit *
13291 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13292 const char *dwo_name, const char *comp_dir,
13293 ULONGEST signature, int is_debug_types)
13295 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13296 struct objfile *objfile = dwarf2_per_objfile->objfile;
13297 const char *kind = is_debug_types ? "TU" : "CU";
13298 void **dwo_file_slot;
13299 struct dwo_file *dwo_file;
13300 struct dwp_file *dwp_file;
13302 /* First see if there's a DWP file.
13303 If we have a DWP file but didn't find the DWO inside it, don't
13304 look for the original DWO file. It makes gdb behave differently
13305 depending on whether one is debugging in the build tree. */
13307 dwp_file = get_dwp_file (dwarf2_per_objfile);
13308 if (dwp_file != NULL)
13310 const struct dwp_hash_table *dwp_htab =
13311 is_debug_types ? dwp_file->tus : dwp_file->cus;
13313 if (dwp_htab != NULL)
13315 struct dwo_unit *dwo_cutu =
13316 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13317 signature, is_debug_types);
13319 if (dwo_cutu != NULL)
13321 if (dwarf_read_debug)
13323 fprintf_unfiltered (gdb_stdlog,
13324 "Virtual DWO %s %s found: @%s\n",
13325 kind, hex_string (signature),
13326 host_address_to_string (dwo_cutu));
13334 /* No DWP file, look for the DWO file. */
13336 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13337 dwo_name, comp_dir);
13338 if (*dwo_file_slot == NULL)
13340 /* Read in the file and build a table of the CUs/TUs it contains. */
13341 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13343 /* NOTE: This will be NULL if unable to open the file. */
13344 dwo_file = (struct dwo_file *) *dwo_file_slot;
13346 if (dwo_file != NULL)
13348 struct dwo_unit *dwo_cutu = NULL;
13350 if (is_debug_types && dwo_file->tus)
13352 struct dwo_unit find_dwo_cutu;
13354 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13355 find_dwo_cutu.signature = signature;
13357 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13359 else if (!is_debug_types && dwo_file->cus)
13361 struct dwo_unit find_dwo_cutu;
13363 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13364 find_dwo_cutu.signature = signature;
13365 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13369 if (dwo_cutu != NULL)
13371 if (dwarf_read_debug)
13373 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13374 kind, dwo_name, hex_string (signature),
13375 host_address_to_string (dwo_cutu));
13382 /* We didn't find it. This could mean a dwo_id mismatch, or
13383 someone deleted the DWO/DWP file, or the search path isn't set up
13384 correctly to find the file. */
13386 if (dwarf_read_debug)
13388 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13389 kind, dwo_name, hex_string (signature));
13392 /* This is a warning and not a complaint because it can be caused by
13393 pilot error (e.g., user accidentally deleting the DWO). */
13395 /* Print the name of the DWP file if we looked there, helps the user
13396 better diagnose the problem. */
13397 std::string dwp_text;
13399 if (dwp_file != NULL)
13400 dwp_text = string_printf (" [in DWP file %s]",
13401 lbasename (dwp_file->name));
13403 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13404 " [in module %s]"),
13405 kind, dwo_name, hex_string (signature),
13407 this_unit->is_debug_types ? "TU" : "CU",
13408 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13413 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13414 See lookup_dwo_cutu_unit for details. */
13416 static struct dwo_unit *
13417 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13418 const char *dwo_name, const char *comp_dir,
13419 ULONGEST signature)
13421 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13424 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13425 See lookup_dwo_cutu_unit for details. */
13427 static struct dwo_unit *
13428 lookup_dwo_type_unit (struct signatured_type *this_tu,
13429 const char *dwo_name, const char *comp_dir)
13431 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13434 /* Traversal function for queue_and_load_all_dwo_tus. */
13437 queue_and_load_dwo_tu (void **slot, void *info)
13439 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13440 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13441 ULONGEST signature = dwo_unit->signature;
13442 struct signatured_type *sig_type =
13443 lookup_dwo_signatured_type (per_cu->cu, signature);
13445 if (sig_type != NULL)
13447 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13449 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13450 a real dependency of PER_CU on SIG_TYPE. That is detected later
13451 while processing PER_CU. */
13452 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13453 load_full_type_unit (sig_cu);
13454 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13460 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13461 The DWO may have the only definition of the type, though it may not be
13462 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13463 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13466 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13468 struct dwo_unit *dwo_unit;
13469 struct dwo_file *dwo_file;
13471 gdb_assert (!per_cu->is_debug_types);
13472 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13473 gdb_assert (per_cu->cu != NULL);
13475 dwo_unit = per_cu->cu->dwo_unit;
13476 gdb_assert (dwo_unit != NULL);
13478 dwo_file = dwo_unit->dwo_file;
13479 if (dwo_file->tus != NULL)
13480 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13483 /* Read in various DIEs. */
13485 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13486 Inherit only the children of the DW_AT_abstract_origin DIE not being
13487 already referenced by DW_AT_abstract_origin from the children of the
13491 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13493 struct die_info *child_die;
13494 sect_offset *offsetp;
13495 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13496 struct die_info *origin_die;
13497 /* Iterator of the ORIGIN_DIE children. */
13498 struct die_info *origin_child_die;
13499 struct attribute *attr;
13500 struct dwarf2_cu *origin_cu;
13501 struct pending **origin_previous_list_in_scope;
13503 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13507 /* Note that following die references may follow to a die in a
13511 origin_die = follow_die_ref (die, attr, &origin_cu);
13513 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13515 origin_previous_list_in_scope = origin_cu->list_in_scope;
13516 origin_cu->list_in_scope = cu->list_in_scope;
13518 if (die->tag != origin_die->tag
13519 && !(die->tag == DW_TAG_inlined_subroutine
13520 && origin_die->tag == DW_TAG_subprogram))
13521 complaint (_("DIE %s and its abstract origin %s have different tags"),
13522 sect_offset_str (die->sect_off),
13523 sect_offset_str (origin_die->sect_off));
13525 std::vector<sect_offset> offsets;
13527 for (child_die = die->child;
13528 child_die && child_die->tag;
13529 child_die = sibling_die (child_die))
13531 struct die_info *child_origin_die;
13532 struct dwarf2_cu *child_origin_cu;
13534 /* We are trying to process concrete instance entries:
13535 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13536 it's not relevant to our analysis here. i.e. detecting DIEs that are
13537 present in the abstract instance but not referenced in the concrete
13539 if (child_die->tag == DW_TAG_call_site
13540 || child_die->tag == DW_TAG_GNU_call_site)
13543 /* For each CHILD_DIE, find the corresponding child of
13544 ORIGIN_DIE. If there is more than one layer of
13545 DW_AT_abstract_origin, follow them all; there shouldn't be,
13546 but GCC versions at least through 4.4 generate this (GCC PR
13548 child_origin_die = child_die;
13549 child_origin_cu = cu;
13552 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13556 child_origin_die = follow_die_ref (child_origin_die, attr,
13560 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13561 counterpart may exist. */
13562 if (child_origin_die != child_die)
13564 if (child_die->tag != child_origin_die->tag
13565 && !(child_die->tag == DW_TAG_inlined_subroutine
13566 && child_origin_die->tag == DW_TAG_subprogram))
13567 complaint (_("Child DIE %s and its abstract origin %s have "
13569 sect_offset_str (child_die->sect_off),
13570 sect_offset_str (child_origin_die->sect_off));
13571 if (child_origin_die->parent != origin_die)
13572 complaint (_("Child DIE %s and its abstract origin %s have "
13573 "different parents"),
13574 sect_offset_str (child_die->sect_off),
13575 sect_offset_str (child_origin_die->sect_off));
13577 offsets.push_back (child_origin_die->sect_off);
13580 std::sort (offsets.begin (), offsets.end ());
13581 sect_offset *offsets_end = offsets.data () + offsets.size ();
13582 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13583 if (offsetp[-1] == *offsetp)
13584 complaint (_("Multiple children of DIE %s refer "
13585 "to DIE %s as their abstract origin"),
13586 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13588 offsetp = offsets.data ();
13589 origin_child_die = origin_die->child;
13590 while (origin_child_die && origin_child_die->tag)
13592 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13593 while (offsetp < offsets_end
13594 && *offsetp < origin_child_die->sect_off)
13596 if (offsetp >= offsets_end
13597 || *offsetp > origin_child_die->sect_off)
13599 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13600 Check whether we're already processing ORIGIN_CHILD_DIE.
13601 This can happen with mutually referenced abstract_origins.
13603 if (!origin_child_die->in_process)
13604 process_die (origin_child_die, origin_cu);
13606 origin_child_die = sibling_die (origin_child_die);
13608 origin_cu->list_in_scope = origin_previous_list_in_scope;
13612 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13614 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13615 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13616 struct context_stack *newobj;
13619 struct die_info *child_die;
13620 struct attribute *attr, *call_line, *call_file;
13622 CORE_ADDR baseaddr;
13623 struct block *block;
13624 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13625 std::vector<struct symbol *> template_args;
13626 struct template_symbol *templ_func = NULL;
13630 /* If we do not have call site information, we can't show the
13631 caller of this inlined function. That's too confusing, so
13632 only use the scope for local variables. */
13633 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13634 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13635 if (call_line == NULL || call_file == NULL)
13637 read_lexical_block_scope (die, cu);
13642 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13644 name = dwarf2_name (die, cu);
13646 /* Ignore functions with missing or empty names. These are actually
13647 illegal according to the DWARF standard. */
13650 complaint (_("missing name for subprogram DIE at %s"),
13651 sect_offset_str (die->sect_off));
13655 /* Ignore functions with missing or invalid low and high pc attributes. */
13656 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13657 <= PC_BOUNDS_INVALID)
13659 attr = dwarf2_attr (die, DW_AT_external, cu);
13660 if (!attr || !DW_UNSND (attr))
13661 complaint (_("cannot get low and high bounds "
13662 "for subprogram DIE at %s"),
13663 sect_offset_str (die->sect_off));
13667 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13668 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13670 /* If we have any template arguments, then we must allocate a
13671 different sort of symbol. */
13672 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13674 if (child_die->tag == DW_TAG_template_type_param
13675 || child_die->tag == DW_TAG_template_value_param)
13677 templ_func = allocate_template_symbol (objfile);
13678 templ_func->subclass = SYMBOL_TEMPLATE;
13683 newobj = cu->get_builder ()->push_context (0, lowpc);
13684 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13685 (struct symbol *) templ_func);
13687 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13688 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13691 /* If there is a location expression for DW_AT_frame_base, record
13693 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13695 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13697 /* If there is a location for the static link, record it. */
13698 newobj->static_link = NULL;
13699 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13702 newobj->static_link
13703 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13704 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13707 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13709 if (die->child != NULL)
13711 child_die = die->child;
13712 while (child_die && child_die->tag)
13714 if (child_die->tag == DW_TAG_template_type_param
13715 || child_die->tag == DW_TAG_template_value_param)
13717 struct symbol *arg = new_symbol (child_die, NULL, cu);
13720 template_args.push_back (arg);
13723 process_die (child_die, cu);
13724 child_die = sibling_die (child_die);
13728 inherit_abstract_dies (die, cu);
13730 /* If we have a DW_AT_specification, we might need to import using
13731 directives from the context of the specification DIE. See the
13732 comment in determine_prefix. */
13733 if (cu->language == language_cplus
13734 && dwarf2_attr (die, DW_AT_specification, cu))
13736 struct dwarf2_cu *spec_cu = cu;
13737 struct die_info *spec_die = die_specification (die, &spec_cu);
13741 child_die = spec_die->child;
13742 while (child_die && child_die->tag)
13744 if (child_die->tag == DW_TAG_imported_module)
13745 process_die (child_die, spec_cu);
13746 child_die = sibling_die (child_die);
13749 /* In some cases, GCC generates specification DIEs that
13750 themselves contain DW_AT_specification attributes. */
13751 spec_die = die_specification (spec_die, &spec_cu);
13755 struct context_stack cstk = cu->get_builder ()->pop_context ();
13756 /* Make a block for the local symbols within. */
13757 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13758 cstk.static_link, lowpc, highpc);
13760 /* For C++, set the block's scope. */
13761 if ((cu->language == language_cplus
13762 || cu->language == language_fortran
13763 || cu->language == language_d
13764 || cu->language == language_rust)
13765 && cu->processing_has_namespace_info)
13766 block_set_scope (block, determine_prefix (die, cu),
13767 &objfile->objfile_obstack);
13769 /* If we have address ranges, record them. */
13770 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13772 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13774 /* Attach template arguments to function. */
13775 if (!template_args.empty ())
13777 gdb_assert (templ_func != NULL);
13779 templ_func->n_template_arguments = template_args.size ();
13780 templ_func->template_arguments
13781 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13782 templ_func->n_template_arguments);
13783 memcpy (templ_func->template_arguments,
13784 template_args.data (),
13785 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13787 /* Make sure that the symtab is set on the new symbols. Even
13788 though they don't appear in this symtab directly, other parts
13789 of gdb assume that symbols do, and this is reasonably
13791 for (symbol *sym : template_args)
13792 symbol_set_symtab (sym, symbol_symtab (templ_func));
13795 /* In C++, we can have functions nested inside functions (e.g., when
13796 a function declares a class that has methods). This means that
13797 when we finish processing a function scope, we may need to go
13798 back to building a containing block's symbol lists. */
13799 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13800 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13802 /* If we've finished processing a top-level function, subsequent
13803 symbols go in the file symbol list. */
13804 if (cu->get_builder ()->outermost_context_p ())
13805 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13808 /* Process all the DIES contained within a lexical block scope. Start
13809 a new scope, process the dies, and then close the scope. */
13812 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13814 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13815 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13816 CORE_ADDR lowpc, highpc;
13817 struct die_info *child_die;
13818 CORE_ADDR baseaddr;
13820 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13822 /* Ignore blocks with missing or invalid low and high pc attributes. */
13823 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13824 as multiple lexical blocks? Handling children in a sane way would
13825 be nasty. Might be easier to properly extend generic blocks to
13826 describe ranges. */
13827 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13829 case PC_BOUNDS_NOT_PRESENT:
13830 /* DW_TAG_lexical_block has no attributes, process its children as if
13831 there was no wrapping by that DW_TAG_lexical_block.
13832 GCC does no longer produces such DWARF since GCC r224161. */
13833 for (child_die = die->child;
13834 child_die != NULL && child_die->tag;
13835 child_die = sibling_die (child_die))
13836 process_die (child_die, cu);
13838 case PC_BOUNDS_INVALID:
13841 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13842 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13844 cu->get_builder ()->push_context (0, lowpc);
13845 if (die->child != NULL)
13847 child_die = die->child;
13848 while (child_die && child_die->tag)
13850 process_die (child_die, cu);
13851 child_die = sibling_die (child_die);
13854 inherit_abstract_dies (die, cu);
13855 struct context_stack cstk = cu->get_builder ()->pop_context ();
13857 if (*cu->get_builder ()->get_local_symbols () != NULL
13858 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13860 struct block *block
13861 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13862 cstk.start_addr, highpc);
13864 /* Note that recording ranges after traversing children, as we
13865 do here, means that recording a parent's ranges entails
13866 walking across all its children's ranges as they appear in
13867 the address map, which is quadratic behavior.
13869 It would be nicer to record the parent's ranges before
13870 traversing its children, simply overriding whatever you find
13871 there. But since we don't even decide whether to create a
13872 block until after we've traversed its children, that's hard
13874 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13876 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13877 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13880 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13883 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13885 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13886 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13887 CORE_ADDR pc, baseaddr;
13888 struct attribute *attr;
13889 struct call_site *call_site, call_site_local;
13892 struct die_info *child_die;
13894 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13896 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13899 /* This was a pre-DWARF-5 GNU extension alias
13900 for DW_AT_call_return_pc. */
13901 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13905 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13906 "DIE %s [in module %s]"),
13907 sect_offset_str (die->sect_off), objfile_name (objfile));
13910 pc = attr_value_as_address (attr) + baseaddr;
13911 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13913 if (cu->call_site_htab == NULL)
13914 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13915 NULL, &objfile->objfile_obstack,
13916 hashtab_obstack_allocate, NULL);
13917 call_site_local.pc = pc;
13918 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13921 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13922 "DIE %s [in module %s]"),
13923 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13924 objfile_name (objfile));
13928 /* Count parameters at the caller. */
13931 for (child_die = die->child; child_die && child_die->tag;
13932 child_die = sibling_die (child_die))
13934 if (child_die->tag != DW_TAG_call_site_parameter
13935 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13937 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13938 "DW_TAG_call_site child DIE %s [in module %s]"),
13939 child_die->tag, sect_offset_str (child_die->sect_off),
13940 objfile_name (objfile));
13948 = ((struct call_site *)
13949 obstack_alloc (&objfile->objfile_obstack,
13950 sizeof (*call_site)
13951 + (sizeof (*call_site->parameter) * (nparams - 1))));
13953 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13954 call_site->pc = pc;
13956 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13957 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13959 struct die_info *func_die;
13961 /* Skip also over DW_TAG_inlined_subroutine. */
13962 for (func_die = die->parent;
13963 func_die && func_die->tag != DW_TAG_subprogram
13964 && func_die->tag != DW_TAG_subroutine_type;
13965 func_die = func_die->parent);
13967 /* DW_AT_call_all_calls is a superset
13968 of DW_AT_call_all_tail_calls. */
13970 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13971 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13972 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13973 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13975 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13976 not complete. But keep CALL_SITE for look ups via call_site_htab,
13977 both the initial caller containing the real return address PC and
13978 the final callee containing the current PC of a chain of tail
13979 calls do not need to have the tail call list complete. But any
13980 function candidate for a virtual tail call frame searched via
13981 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13982 determined unambiguously. */
13986 struct type *func_type = NULL;
13989 func_type = get_die_type (func_die, cu);
13990 if (func_type != NULL)
13992 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13994 /* Enlist this call site to the function. */
13995 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13996 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13999 complaint (_("Cannot find function owning DW_TAG_call_site "
14000 "DIE %s [in module %s]"),
14001 sect_offset_str (die->sect_off), objfile_name (objfile));
14005 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14007 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14009 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14012 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14013 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14015 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14016 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14017 /* Keep NULL DWARF_BLOCK. */;
14018 else if (attr_form_is_block (attr))
14020 struct dwarf2_locexpr_baton *dlbaton;
14022 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14023 dlbaton->data = DW_BLOCK (attr)->data;
14024 dlbaton->size = DW_BLOCK (attr)->size;
14025 dlbaton->per_cu = cu->per_cu;
14027 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14029 else if (attr_form_is_ref (attr))
14031 struct dwarf2_cu *target_cu = cu;
14032 struct die_info *target_die;
14034 target_die = follow_die_ref (die, attr, &target_cu);
14035 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14036 if (die_is_declaration (target_die, target_cu))
14038 const char *target_physname;
14040 /* Prefer the mangled name; otherwise compute the demangled one. */
14041 target_physname = dw2_linkage_name (target_die, target_cu);
14042 if (target_physname == NULL)
14043 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14044 if (target_physname == NULL)
14045 complaint (_("DW_AT_call_target target DIE has invalid "
14046 "physname, for referencing DIE %s [in module %s]"),
14047 sect_offset_str (die->sect_off), objfile_name (objfile));
14049 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14055 /* DW_AT_entry_pc should be preferred. */
14056 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14057 <= PC_BOUNDS_INVALID)
14058 complaint (_("DW_AT_call_target target DIE has invalid "
14059 "low pc, for referencing DIE %s [in module %s]"),
14060 sect_offset_str (die->sect_off), objfile_name (objfile));
14063 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14064 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14069 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14070 "block nor reference, for DIE %s [in module %s]"),
14071 sect_offset_str (die->sect_off), objfile_name (objfile));
14073 call_site->per_cu = cu->per_cu;
14075 for (child_die = die->child;
14076 child_die && child_die->tag;
14077 child_die = sibling_die (child_die))
14079 struct call_site_parameter *parameter;
14080 struct attribute *loc, *origin;
14082 if (child_die->tag != DW_TAG_call_site_parameter
14083 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14085 /* Already printed the complaint above. */
14089 gdb_assert (call_site->parameter_count < nparams);
14090 parameter = &call_site->parameter[call_site->parameter_count];
14092 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14093 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14094 register is contained in DW_AT_call_value. */
14096 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14097 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14098 if (origin == NULL)
14100 /* This was a pre-DWARF-5 GNU extension alias
14101 for DW_AT_call_parameter. */
14102 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14104 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14106 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14108 sect_offset sect_off
14109 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14110 if (!offset_in_cu_p (&cu->header, sect_off))
14112 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14113 binding can be done only inside one CU. Such referenced DIE
14114 therefore cannot be even moved to DW_TAG_partial_unit. */
14115 complaint (_("DW_AT_call_parameter offset is not in CU for "
14116 "DW_TAG_call_site child DIE %s [in module %s]"),
14117 sect_offset_str (child_die->sect_off),
14118 objfile_name (objfile));
14121 parameter->u.param_cu_off
14122 = (cu_offset) (sect_off - cu->header.sect_off);
14124 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14126 complaint (_("No DW_FORM_block* DW_AT_location for "
14127 "DW_TAG_call_site child DIE %s [in module %s]"),
14128 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14133 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14134 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14135 if (parameter->u.dwarf_reg != -1)
14136 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14137 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14138 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14139 ¶meter->u.fb_offset))
14140 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14143 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14144 "for DW_FORM_block* DW_AT_location is supported for "
14145 "DW_TAG_call_site child DIE %s "
14147 sect_offset_str (child_die->sect_off),
14148 objfile_name (objfile));
14153 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14155 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14156 if (!attr_form_is_block (attr))
14158 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14159 "DW_TAG_call_site child DIE %s [in module %s]"),
14160 sect_offset_str (child_die->sect_off),
14161 objfile_name (objfile));
14164 parameter->value = DW_BLOCK (attr)->data;
14165 parameter->value_size = DW_BLOCK (attr)->size;
14167 /* Parameters are not pre-cleared by memset above. */
14168 parameter->data_value = NULL;
14169 parameter->data_value_size = 0;
14170 call_site->parameter_count++;
14172 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14174 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14177 if (!attr_form_is_block (attr))
14178 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14179 "DW_TAG_call_site child DIE %s [in module %s]"),
14180 sect_offset_str (child_die->sect_off),
14181 objfile_name (objfile));
14184 parameter->data_value = DW_BLOCK (attr)->data;
14185 parameter->data_value_size = DW_BLOCK (attr)->size;
14191 /* Helper function for read_variable. If DIE represents a virtual
14192 table, then return the type of the concrete object that is
14193 associated with the virtual table. Otherwise, return NULL. */
14195 static struct type *
14196 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14198 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14202 /* Find the type DIE. */
14203 struct die_info *type_die = NULL;
14204 struct dwarf2_cu *type_cu = cu;
14206 if (attr_form_is_ref (attr))
14207 type_die = follow_die_ref (die, attr, &type_cu);
14208 if (type_die == NULL)
14211 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14213 return die_containing_type (type_die, type_cu);
14216 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14219 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14221 struct rust_vtable_symbol *storage = NULL;
14223 if (cu->language == language_rust)
14225 struct type *containing_type = rust_containing_type (die, cu);
14227 if (containing_type != NULL)
14229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14231 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14232 struct rust_vtable_symbol);
14233 initialize_objfile_symbol (storage);
14234 storage->concrete_type = containing_type;
14235 storage->subclass = SYMBOL_RUST_VTABLE;
14239 struct symbol *res = new_symbol (die, NULL, cu, storage);
14240 struct attribute *abstract_origin
14241 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14242 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14243 if (res == NULL && loc && abstract_origin)
14245 /* We have a variable without a name, but with a location and an abstract
14246 origin. This may be a concrete instance of an abstract variable
14247 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14249 struct dwarf2_cu *origin_cu = cu;
14250 struct die_info *origin_die
14251 = follow_die_ref (die, abstract_origin, &origin_cu);
14252 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14253 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14257 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14258 reading .debug_rnglists.
14259 Callback's type should be:
14260 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14261 Return true if the attributes are present and valid, otherwise,
14264 template <typename Callback>
14266 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14267 Callback &&callback)
14269 struct dwarf2_per_objfile *dwarf2_per_objfile
14270 = cu->per_cu->dwarf2_per_objfile;
14271 struct objfile *objfile = dwarf2_per_objfile->objfile;
14272 bfd *obfd = objfile->obfd;
14273 /* Base address selection entry. */
14276 const gdb_byte *buffer;
14277 CORE_ADDR baseaddr;
14278 bool overflow = false;
14280 found_base = cu->base_known;
14281 base = cu->base_address;
14283 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14284 if (offset >= dwarf2_per_objfile->rnglists.size)
14286 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14290 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14292 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14296 /* Initialize it due to a false compiler warning. */
14297 CORE_ADDR range_beginning = 0, range_end = 0;
14298 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14299 + dwarf2_per_objfile->rnglists.size);
14300 unsigned int bytes_read;
14302 if (buffer == buf_end)
14307 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14310 case DW_RLE_end_of_list:
14312 case DW_RLE_base_address:
14313 if (buffer + cu->header.addr_size > buf_end)
14318 base = read_address (obfd, buffer, cu, &bytes_read);
14320 buffer += bytes_read;
14322 case DW_RLE_start_length:
14323 if (buffer + cu->header.addr_size > buf_end)
14328 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14329 buffer += bytes_read;
14330 range_end = (range_beginning
14331 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14332 buffer += bytes_read;
14333 if (buffer > buf_end)
14339 case DW_RLE_offset_pair:
14340 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14341 buffer += bytes_read;
14342 if (buffer > buf_end)
14347 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14348 buffer += bytes_read;
14349 if (buffer > buf_end)
14355 case DW_RLE_start_end:
14356 if (buffer + 2 * cu->header.addr_size > buf_end)
14361 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14362 buffer += bytes_read;
14363 range_end = read_address (obfd, buffer, cu, &bytes_read);
14364 buffer += bytes_read;
14367 complaint (_("Invalid .debug_rnglists data (no base address)"));
14370 if (rlet == DW_RLE_end_of_list || overflow)
14372 if (rlet == DW_RLE_base_address)
14377 /* We have no valid base address for the ranges
14379 complaint (_("Invalid .debug_rnglists data (no base address)"));
14383 if (range_beginning > range_end)
14385 /* Inverted range entries are invalid. */
14386 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14390 /* Empty range entries have no effect. */
14391 if (range_beginning == range_end)
14394 range_beginning += base;
14397 /* A not-uncommon case of bad debug info.
14398 Don't pollute the addrmap with bad data. */
14399 if (range_beginning + baseaddr == 0
14400 && !dwarf2_per_objfile->has_section_at_zero)
14402 complaint (_(".debug_rnglists entry has start address of zero"
14403 " [in module %s]"), objfile_name (objfile));
14407 callback (range_beginning, range_end);
14412 complaint (_("Offset %d is not terminated "
14413 "for DW_AT_ranges attribute"),
14421 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14422 Callback's type should be:
14423 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14424 Return 1 if the attributes are present and valid, otherwise, return 0. */
14426 template <typename Callback>
14428 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14429 Callback &&callback)
14431 struct dwarf2_per_objfile *dwarf2_per_objfile
14432 = cu->per_cu->dwarf2_per_objfile;
14433 struct objfile *objfile = dwarf2_per_objfile->objfile;
14434 struct comp_unit_head *cu_header = &cu->header;
14435 bfd *obfd = objfile->obfd;
14436 unsigned int addr_size = cu_header->addr_size;
14437 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14438 /* Base address selection entry. */
14441 unsigned int dummy;
14442 const gdb_byte *buffer;
14443 CORE_ADDR baseaddr;
14445 if (cu_header->version >= 5)
14446 return dwarf2_rnglists_process (offset, cu, callback);
14448 found_base = cu->base_known;
14449 base = cu->base_address;
14451 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14452 if (offset >= dwarf2_per_objfile->ranges.size)
14454 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14458 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14460 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14464 CORE_ADDR range_beginning, range_end;
14466 range_beginning = read_address (obfd, buffer, cu, &dummy);
14467 buffer += addr_size;
14468 range_end = read_address (obfd, buffer, cu, &dummy);
14469 buffer += addr_size;
14470 offset += 2 * addr_size;
14472 /* An end of list marker is a pair of zero addresses. */
14473 if (range_beginning == 0 && range_end == 0)
14474 /* Found the end of list entry. */
14477 /* Each base address selection entry is a pair of 2 values.
14478 The first is the largest possible address, the second is
14479 the base address. Check for a base address here. */
14480 if ((range_beginning & mask) == mask)
14482 /* If we found the largest possible address, then we already
14483 have the base address in range_end. */
14491 /* We have no valid base address for the ranges
14493 complaint (_("Invalid .debug_ranges data (no base address)"));
14497 if (range_beginning > range_end)
14499 /* Inverted range entries are invalid. */
14500 complaint (_("Invalid .debug_ranges data (inverted range)"));
14504 /* Empty range entries have no effect. */
14505 if (range_beginning == range_end)
14508 range_beginning += base;
14511 /* A not-uncommon case of bad debug info.
14512 Don't pollute the addrmap with bad data. */
14513 if (range_beginning + baseaddr == 0
14514 && !dwarf2_per_objfile->has_section_at_zero)
14516 complaint (_(".debug_ranges entry has start address of zero"
14517 " [in module %s]"), objfile_name (objfile));
14521 callback (range_beginning, range_end);
14527 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14528 Return 1 if the attributes are present and valid, otherwise, return 0.
14529 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14532 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14533 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14534 struct partial_symtab *ranges_pst)
14536 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14538 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14539 SECT_OFF_TEXT (objfile));
14542 CORE_ADDR high = 0;
14545 retval = dwarf2_ranges_process (offset, cu,
14546 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14548 if (ranges_pst != NULL)
14553 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14554 range_beginning + baseaddr)
14556 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14557 range_end + baseaddr)
14559 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14560 lowpc, highpc - 1, ranges_pst);
14563 /* FIXME: This is recording everything as a low-high
14564 segment of consecutive addresses. We should have a
14565 data structure for discontiguous block ranges
14569 low = range_beginning;
14575 if (range_beginning < low)
14576 low = range_beginning;
14577 if (range_end > high)
14585 /* If the first entry is an end-of-list marker, the range
14586 describes an empty scope, i.e. no instructions. */
14592 *high_return = high;
14596 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14597 definition for the return value. *LOWPC and *HIGHPC are set iff
14598 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14600 static enum pc_bounds_kind
14601 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14602 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14603 struct partial_symtab *pst)
14605 struct dwarf2_per_objfile *dwarf2_per_objfile
14606 = cu->per_cu->dwarf2_per_objfile;
14607 struct attribute *attr;
14608 struct attribute *attr_high;
14610 CORE_ADDR high = 0;
14611 enum pc_bounds_kind ret;
14613 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14616 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14619 low = attr_value_as_address (attr);
14620 high = attr_value_as_address (attr_high);
14621 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14625 /* Found high w/o low attribute. */
14626 return PC_BOUNDS_INVALID;
14628 /* Found consecutive range of addresses. */
14629 ret = PC_BOUNDS_HIGH_LOW;
14633 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14636 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14637 We take advantage of the fact that DW_AT_ranges does not appear
14638 in DW_TAG_compile_unit of DWO files. */
14639 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14640 unsigned int ranges_offset = (DW_UNSND (attr)
14641 + (need_ranges_base
14645 /* Value of the DW_AT_ranges attribute is the offset in the
14646 .debug_ranges section. */
14647 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14648 return PC_BOUNDS_INVALID;
14649 /* Found discontinuous range of addresses. */
14650 ret = PC_BOUNDS_RANGES;
14653 return PC_BOUNDS_NOT_PRESENT;
14656 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14658 return PC_BOUNDS_INVALID;
14660 /* When using the GNU linker, .gnu.linkonce. sections are used to
14661 eliminate duplicate copies of functions and vtables and such.
14662 The linker will arbitrarily choose one and discard the others.
14663 The AT_*_pc values for such functions refer to local labels in
14664 these sections. If the section from that file was discarded, the
14665 labels are not in the output, so the relocs get a value of 0.
14666 If this is a discarded function, mark the pc bounds as invalid,
14667 so that GDB will ignore it. */
14668 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14669 return PC_BOUNDS_INVALID;
14677 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14678 its low and high PC addresses. Do nothing if these addresses could not
14679 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14680 and HIGHPC to the high address if greater than HIGHPC. */
14683 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14684 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14685 struct dwarf2_cu *cu)
14687 CORE_ADDR low, high;
14688 struct die_info *child = die->child;
14690 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14692 *lowpc = std::min (*lowpc, low);
14693 *highpc = std::max (*highpc, high);
14696 /* If the language does not allow nested subprograms (either inside
14697 subprograms or lexical blocks), we're done. */
14698 if (cu->language != language_ada)
14701 /* Check all the children of the given DIE. If it contains nested
14702 subprograms, then check their pc bounds. Likewise, we need to
14703 check lexical blocks as well, as they may also contain subprogram
14705 while (child && child->tag)
14707 if (child->tag == DW_TAG_subprogram
14708 || child->tag == DW_TAG_lexical_block)
14709 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14710 child = sibling_die (child);
14714 /* Get the low and high pc's represented by the scope DIE, and store
14715 them in *LOWPC and *HIGHPC. If the correct values can't be
14716 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14719 get_scope_pc_bounds (struct die_info *die,
14720 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14721 struct dwarf2_cu *cu)
14723 CORE_ADDR best_low = (CORE_ADDR) -1;
14724 CORE_ADDR best_high = (CORE_ADDR) 0;
14725 CORE_ADDR current_low, current_high;
14727 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14728 >= PC_BOUNDS_RANGES)
14730 best_low = current_low;
14731 best_high = current_high;
14735 struct die_info *child = die->child;
14737 while (child && child->tag)
14739 switch (child->tag) {
14740 case DW_TAG_subprogram:
14741 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14743 case DW_TAG_namespace:
14744 case DW_TAG_module:
14745 /* FIXME: carlton/2004-01-16: Should we do this for
14746 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14747 that current GCC's always emit the DIEs corresponding
14748 to definitions of methods of classes as children of a
14749 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14750 the DIEs giving the declarations, which could be
14751 anywhere). But I don't see any reason why the
14752 standards says that they have to be there. */
14753 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14755 if (current_low != ((CORE_ADDR) -1))
14757 best_low = std::min (best_low, current_low);
14758 best_high = std::max (best_high, current_high);
14766 child = sibling_die (child);
14771 *highpc = best_high;
14774 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14778 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14779 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14781 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14783 struct attribute *attr;
14784 struct attribute *attr_high;
14786 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14789 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14792 CORE_ADDR low = attr_value_as_address (attr);
14793 CORE_ADDR high = attr_value_as_address (attr_high);
14795 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14798 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14799 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14800 cu->get_builder ()->record_block_range (block, low, high - 1);
14804 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14807 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14808 We take advantage of the fact that DW_AT_ranges does not appear
14809 in DW_TAG_compile_unit of DWO files. */
14810 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14812 /* The value of the DW_AT_ranges attribute is the offset of the
14813 address range list in the .debug_ranges section. */
14814 unsigned long offset = (DW_UNSND (attr)
14815 + (need_ranges_base ? cu->ranges_base : 0));
14817 std::vector<blockrange> blockvec;
14818 dwarf2_ranges_process (offset, cu,
14819 [&] (CORE_ADDR start, CORE_ADDR end)
14823 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14824 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14825 cu->get_builder ()->record_block_range (block, start, end - 1);
14826 blockvec.emplace_back (start, end);
14829 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14833 /* Check whether the producer field indicates either of GCC < 4.6, or the
14834 Intel C/C++ compiler, and cache the result in CU. */
14837 check_producer (struct dwarf2_cu *cu)
14841 if (cu->producer == NULL)
14843 /* For unknown compilers expect their behavior is DWARF version
14846 GCC started to support .debug_types sections by -gdwarf-4 since
14847 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14848 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14849 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14850 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14852 else if (producer_is_gcc (cu->producer, &major, &minor))
14854 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14855 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14857 else if (producer_is_icc (cu->producer, &major, &minor))
14859 cu->producer_is_icc = true;
14860 cu->producer_is_icc_lt_14 = major < 14;
14862 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14863 cu->producer_is_codewarrior = true;
14866 /* For other non-GCC compilers, expect their behavior is DWARF version
14870 cu->checked_producer = true;
14873 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14874 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14875 during 4.6.0 experimental. */
14878 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14880 if (!cu->checked_producer)
14881 check_producer (cu);
14883 return cu->producer_is_gxx_lt_4_6;
14887 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14888 with incorrect is_stmt attributes. */
14891 producer_is_codewarrior (struct dwarf2_cu *cu)
14893 if (!cu->checked_producer)
14894 check_producer (cu);
14896 return cu->producer_is_codewarrior;
14899 /* Return the default accessibility type if it is not overriden by
14900 DW_AT_accessibility. */
14902 static enum dwarf_access_attribute
14903 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14905 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14907 /* The default DWARF 2 accessibility for members is public, the default
14908 accessibility for inheritance is private. */
14910 if (die->tag != DW_TAG_inheritance)
14911 return DW_ACCESS_public;
14913 return DW_ACCESS_private;
14917 /* DWARF 3+ defines the default accessibility a different way. The same
14918 rules apply now for DW_TAG_inheritance as for the members and it only
14919 depends on the container kind. */
14921 if (die->parent->tag == DW_TAG_class_type)
14922 return DW_ACCESS_private;
14924 return DW_ACCESS_public;
14928 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14929 offset. If the attribute was not found return 0, otherwise return
14930 1. If it was found but could not properly be handled, set *OFFSET
14934 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14937 struct attribute *attr;
14939 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14944 /* Note that we do not check for a section offset first here.
14945 This is because DW_AT_data_member_location is new in DWARF 4,
14946 so if we see it, we can assume that a constant form is really
14947 a constant and not a section offset. */
14948 if (attr_form_is_constant (attr))
14949 *offset = dwarf2_get_attr_constant_value (attr, 0);
14950 else if (attr_form_is_section_offset (attr))
14951 dwarf2_complex_location_expr_complaint ();
14952 else if (attr_form_is_block (attr))
14953 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14955 dwarf2_complex_location_expr_complaint ();
14963 /* Add an aggregate field to the field list. */
14966 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14967 struct dwarf2_cu *cu)
14969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14970 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14971 struct nextfield *new_field;
14972 struct attribute *attr;
14974 const char *fieldname = "";
14976 if (die->tag == DW_TAG_inheritance)
14978 fip->baseclasses.emplace_back ();
14979 new_field = &fip->baseclasses.back ();
14983 fip->fields.emplace_back ();
14984 new_field = &fip->fields.back ();
14989 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14991 new_field->accessibility = DW_UNSND (attr);
14993 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14994 if (new_field->accessibility != DW_ACCESS_public)
14995 fip->non_public_fields = 1;
14997 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14999 new_field->virtuality = DW_UNSND (attr);
15001 new_field->virtuality = DW_VIRTUALITY_none;
15003 fp = &new_field->field;
15005 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15009 /* Data member other than a C++ static data member. */
15011 /* Get type of field. */
15012 fp->type = die_type (die, cu);
15014 SET_FIELD_BITPOS (*fp, 0);
15016 /* Get bit size of field (zero if none). */
15017 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15020 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15024 FIELD_BITSIZE (*fp) = 0;
15027 /* Get bit offset of field. */
15028 if (handle_data_member_location (die, cu, &offset))
15029 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15030 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15033 if (gdbarch_bits_big_endian (gdbarch))
15035 /* For big endian bits, the DW_AT_bit_offset gives the
15036 additional bit offset from the MSB of the containing
15037 anonymous object to the MSB of the field. We don't
15038 have to do anything special since we don't need to
15039 know the size of the anonymous object. */
15040 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15044 /* For little endian bits, compute the bit offset to the
15045 MSB of the anonymous object, subtract off the number of
15046 bits from the MSB of the field to the MSB of the
15047 object, and then subtract off the number of bits of
15048 the field itself. The result is the bit offset of
15049 the LSB of the field. */
15050 int anonymous_size;
15051 int bit_offset = DW_UNSND (attr);
15053 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15056 /* The size of the anonymous object containing
15057 the bit field is explicit, so use the
15058 indicated size (in bytes). */
15059 anonymous_size = DW_UNSND (attr);
15063 /* The size of the anonymous object containing
15064 the bit field must be inferred from the type
15065 attribute of the data member containing the
15067 anonymous_size = TYPE_LENGTH (fp->type);
15069 SET_FIELD_BITPOS (*fp,
15070 (FIELD_BITPOS (*fp)
15071 + anonymous_size * bits_per_byte
15072 - bit_offset - FIELD_BITSIZE (*fp)));
15075 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15077 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15078 + dwarf2_get_attr_constant_value (attr, 0)));
15080 /* Get name of field. */
15081 fieldname = dwarf2_name (die, cu);
15082 if (fieldname == NULL)
15085 /* The name is already allocated along with this objfile, so we don't
15086 need to duplicate it for the type. */
15087 fp->name = fieldname;
15089 /* Change accessibility for artificial fields (e.g. virtual table
15090 pointer or virtual base class pointer) to private. */
15091 if (dwarf2_attr (die, DW_AT_artificial, cu))
15093 FIELD_ARTIFICIAL (*fp) = 1;
15094 new_field->accessibility = DW_ACCESS_private;
15095 fip->non_public_fields = 1;
15098 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15100 /* C++ static member. */
15102 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15103 is a declaration, but all versions of G++ as of this writing
15104 (so through at least 3.2.1) incorrectly generate
15105 DW_TAG_variable tags. */
15107 const char *physname;
15109 /* Get name of field. */
15110 fieldname = dwarf2_name (die, cu);
15111 if (fieldname == NULL)
15114 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15116 /* Only create a symbol if this is an external value.
15117 new_symbol checks this and puts the value in the global symbol
15118 table, which we want. If it is not external, new_symbol
15119 will try to put the value in cu->list_in_scope which is wrong. */
15120 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15122 /* A static const member, not much different than an enum as far as
15123 we're concerned, except that we can support more types. */
15124 new_symbol (die, NULL, cu);
15127 /* Get physical name. */
15128 physname = dwarf2_physname (fieldname, die, cu);
15130 /* The name is already allocated along with this objfile, so we don't
15131 need to duplicate it for the type. */
15132 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15133 FIELD_TYPE (*fp) = die_type (die, cu);
15134 FIELD_NAME (*fp) = fieldname;
15136 else if (die->tag == DW_TAG_inheritance)
15140 /* C++ base class field. */
15141 if (handle_data_member_location (die, cu, &offset))
15142 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15143 FIELD_BITSIZE (*fp) = 0;
15144 FIELD_TYPE (*fp) = die_type (die, cu);
15145 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15147 else if (die->tag == DW_TAG_variant_part)
15149 /* process_structure_scope will treat this DIE as a union. */
15150 process_structure_scope (die, cu);
15152 /* The variant part is relative to the start of the enclosing
15154 SET_FIELD_BITPOS (*fp, 0);
15155 fp->type = get_die_type (die, cu);
15156 fp->artificial = 1;
15157 fp->name = "<<variant>>";
15159 /* Normally a DW_TAG_variant_part won't have a size, but our
15160 representation requires one, so set it to the maximum of the
15162 if (TYPE_LENGTH (fp->type) == 0)
15165 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15166 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15167 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15168 TYPE_LENGTH (fp->type) = max;
15172 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15175 /* Can the type given by DIE define another type? */
15178 type_can_define_types (const struct die_info *die)
15182 case DW_TAG_typedef:
15183 case DW_TAG_class_type:
15184 case DW_TAG_structure_type:
15185 case DW_TAG_union_type:
15186 case DW_TAG_enumeration_type:
15194 /* Add a type definition defined in the scope of the FIP's class. */
15197 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15198 struct dwarf2_cu *cu)
15200 struct decl_field fp;
15201 memset (&fp, 0, sizeof (fp));
15203 gdb_assert (type_can_define_types (die));
15205 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15206 fp.name = dwarf2_name (die, cu);
15207 fp.type = read_type_die (die, cu);
15209 /* Save accessibility. */
15210 enum dwarf_access_attribute accessibility;
15211 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15213 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15215 accessibility = dwarf2_default_access_attribute (die, cu);
15216 switch (accessibility)
15218 case DW_ACCESS_public:
15219 /* The assumed value if neither private nor protected. */
15221 case DW_ACCESS_private:
15224 case DW_ACCESS_protected:
15225 fp.is_protected = 1;
15228 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15231 if (die->tag == DW_TAG_typedef)
15232 fip->typedef_field_list.push_back (fp);
15234 fip->nested_types_list.push_back (fp);
15237 /* Create the vector of fields, and attach it to the type. */
15240 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15241 struct dwarf2_cu *cu)
15243 int nfields = fip->nfields;
15245 /* Record the field count, allocate space for the array of fields,
15246 and create blank accessibility bitfields if necessary. */
15247 TYPE_NFIELDS (type) = nfields;
15248 TYPE_FIELDS (type) = (struct field *)
15249 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15251 if (fip->non_public_fields && cu->language != language_ada)
15253 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15255 TYPE_FIELD_PRIVATE_BITS (type) =
15256 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15257 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15259 TYPE_FIELD_PROTECTED_BITS (type) =
15260 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15261 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15263 TYPE_FIELD_IGNORE_BITS (type) =
15264 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15265 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15268 /* If the type has baseclasses, allocate and clear a bit vector for
15269 TYPE_FIELD_VIRTUAL_BITS. */
15270 if (!fip->baseclasses.empty () && cu->language != language_ada)
15272 int num_bytes = B_BYTES (fip->baseclasses.size ());
15273 unsigned char *pointer;
15275 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15276 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15277 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15278 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15279 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15282 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15284 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15286 for (int index = 0; index < nfields; ++index)
15288 struct nextfield &field = fip->fields[index];
15290 if (field.variant.is_discriminant)
15291 di->discriminant_index = index;
15292 else if (field.variant.default_branch)
15293 di->default_index = index;
15295 di->discriminants[index] = field.variant.discriminant_value;
15299 /* Copy the saved-up fields into the field vector. */
15300 for (int i = 0; i < nfields; ++i)
15302 struct nextfield &field
15303 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15304 : fip->fields[i - fip->baseclasses.size ()]);
15306 TYPE_FIELD (type, i) = field.field;
15307 switch (field.accessibility)
15309 case DW_ACCESS_private:
15310 if (cu->language != language_ada)
15311 SET_TYPE_FIELD_PRIVATE (type, i);
15314 case DW_ACCESS_protected:
15315 if (cu->language != language_ada)
15316 SET_TYPE_FIELD_PROTECTED (type, i);
15319 case DW_ACCESS_public:
15323 /* Unknown accessibility. Complain and treat it as public. */
15325 complaint (_("unsupported accessibility %d"),
15326 field.accessibility);
15330 if (i < fip->baseclasses.size ())
15332 switch (field.virtuality)
15334 case DW_VIRTUALITY_virtual:
15335 case DW_VIRTUALITY_pure_virtual:
15336 if (cu->language == language_ada)
15337 error (_("unexpected virtuality in component of Ada type"));
15338 SET_TYPE_FIELD_VIRTUAL (type, i);
15345 /* Return true if this member function is a constructor, false
15349 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15351 const char *fieldname;
15352 const char *type_name;
15355 if (die->parent == NULL)
15358 if (die->parent->tag != DW_TAG_structure_type
15359 && die->parent->tag != DW_TAG_union_type
15360 && die->parent->tag != DW_TAG_class_type)
15363 fieldname = dwarf2_name (die, cu);
15364 type_name = dwarf2_name (die->parent, cu);
15365 if (fieldname == NULL || type_name == NULL)
15368 len = strlen (fieldname);
15369 return (strncmp (fieldname, type_name, len) == 0
15370 && (type_name[len] == '\0' || type_name[len] == '<'));
15373 /* Add a member function to the proper fieldlist. */
15376 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15377 struct type *type, struct dwarf2_cu *cu)
15379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15380 struct attribute *attr;
15382 struct fnfieldlist *flp = nullptr;
15383 struct fn_field *fnp;
15384 const char *fieldname;
15385 struct type *this_type;
15386 enum dwarf_access_attribute accessibility;
15388 if (cu->language == language_ada)
15389 error (_("unexpected member function in Ada type"));
15391 /* Get name of member function. */
15392 fieldname = dwarf2_name (die, cu);
15393 if (fieldname == NULL)
15396 /* Look up member function name in fieldlist. */
15397 for (i = 0; i < fip->fnfieldlists.size (); i++)
15399 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15401 flp = &fip->fnfieldlists[i];
15406 /* Create a new fnfieldlist if necessary. */
15407 if (flp == nullptr)
15409 fip->fnfieldlists.emplace_back ();
15410 flp = &fip->fnfieldlists.back ();
15411 flp->name = fieldname;
15412 i = fip->fnfieldlists.size () - 1;
15415 /* Create a new member function field and add it to the vector of
15417 flp->fnfields.emplace_back ();
15418 fnp = &flp->fnfields.back ();
15420 /* Delay processing of the physname until later. */
15421 if (cu->language == language_cplus)
15422 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15426 const char *physname = dwarf2_physname (fieldname, die, cu);
15427 fnp->physname = physname ? physname : "";
15430 fnp->type = alloc_type (objfile);
15431 this_type = read_type_die (die, cu);
15432 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15434 int nparams = TYPE_NFIELDS (this_type);
15436 /* TYPE is the domain of this method, and THIS_TYPE is the type
15437 of the method itself (TYPE_CODE_METHOD). */
15438 smash_to_method_type (fnp->type, type,
15439 TYPE_TARGET_TYPE (this_type),
15440 TYPE_FIELDS (this_type),
15441 TYPE_NFIELDS (this_type),
15442 TYPE_VARARGS (this_type));
15444 /* Handle static member functions.
15445 Dwarf2 has no clean way to discern C++ static and non-static
15446 member functions. G++ helps GDB by marking the first
15447 parameter for non-static member functions (which is the this
15448 pointer) as artificial. We obtain this information from
15449 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15450 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15451 fnp->voffset = VOFFSET_STATIC;
15454 complaint (_("member function type missing for '%s'"),
15455 dwarf2_full_name (fieldname, die, cu));
15457 /* Get fcontext from DW_AT_containing_type if present. */
15458 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15459 fnp->fcontext = die_containing_type (die, cu);
15461 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15462 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15464 /* Get accessibility. */
15465 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15467 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15469 accessibility = dwarf2_default_access_attribute (die, cu);
15470 switch (accessibility)
15472 case DW_ACCESS_private:
15473 fnp->is_private = 1;
15475 case DW_ACCESS_protected:
15476 fnp->is_protected = 1;
15480 /* Check for artificial methods. */
15481 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15482 if (attr && DW_UNSND (attr) != 0)
15483 fnp->is_artificial = 1;
15485 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15487 /* Get index in virtual function table if it is a virtual member
15488 function. For older versions of GCC, this is an offset in the
15489 appropriate virtual table, as specified by DW_AT_containing_type.
15490 For everyone else, it is an expression to be evaluated relative
15491 to the object address. */
15493 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15496 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15498 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15500 /* Old-style GCC. */
15501 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15503 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15504 || (DW_BLOCK (attr)->size > 1
15505 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15506 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15508 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15509 if ((fnp->voffset % cu->header.addr_size) != 0)
15510 dwarf2_complex_location_expr_complaint ();
15512 fnp->voffset /= cu->header.addr_size;
15516 dwarf2_complex_location_expr_complaint ();
15518 if (!fnp->fcontext)
15520 /* If there is no `this' field and no DW_AT_containing_type,
15521 we cannot actually find a base class context for the
15523 if (TYPE_NFIELDS (this_type) == 0
15524 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15526 complaint (_("cannot determine context for virtual member "
15527 "function \"%s\" (offset %s)"),
15528 fieldname, sect_offset_str (die->sect_off));
15533 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15537 else if (attr_form_is_section_offset (attr))
15539 dwarf2_complex_location_expr_complaint ();
15543 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15549 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15550 if (attr && DW_UNSND (attr))
15552 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15553 complaint (_("Member function \"%s\" (offset %s) is virtual "
15554 "but the vtable offset is not specified"),
15555 fieldname, sect_offset_str (die->sect_off));
15556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15557 TYPE_CPLUS_DYNAMIC (type) = 1;
15562 /* Create the vector of member function fields, and attach it to the type. */
15565 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15566 struct dwarf2_cu *cu)
15568 if (cu->language == language_ada)
15569 error (_("unexpected member functions in Ada type"));
15571 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15572 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15574 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15576 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15578 struct fnfieldlist &nf = fip->fnfieldlists[i];
15579 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15581 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15582 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15583 fn_flp->fn_fields = (struct fn_field *)
15584 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15586 for (int k = 0; k < nf.fnfields.size (); ++k)
15587 fn_flp->fn_fields[k] = nf.fnfields[k];
15590 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15593 /* Returns non-zero if NAME is the name of a vtable member in CU's
15594 language, zero otherwise. */
15596 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15598 static const char vptr[] = "_vptr";
15600 /* Look for the C++ form of the vtable. */
15601 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15607 /* GCC outputs unnamed structures that are really pointers to member
15608 functions, with the ABI-specified layout. If TYPE describes
15609 such a structure, smash it into a member function type.
15611 GCC shouldn't do this; it should just output pointer to member DIEs.
15612 This is GCC PR debug/28767. */
15615 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15617 struct type *pfn_type, *self_type, *new_type;
15619 /* Check for a structure with no name and two children. */
15620 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15623 /* Check for __pfn and __delta members. */
15624 if (TYPE_FIELD_NAME (type, 0) == NULL
15625 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15626 || TYPE_FIELD_NAME (type, 1) == NULL
15627 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15630 /* Find the type of the method. */
15631 pfn_type = TYPE_FIELD_TYPE (type, 0);
15632 if (pfn_type == NULL
15633 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15634 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15637 /* Look for the "this" argument. */
15638 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15639 if (TYPE_NFIELDS (pfn_type) == 0
15640 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15641 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15644 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15645 new_type = alloc_type (objfile);
15646 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15647 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15648 TYPE_VARARGS (pfn_type));
15649 smash_to_methodptr_type (type, new_type);
15652 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15653 appropriate error checking and issuing complaints if there is a
15657 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15659 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15661 if (attr == nullptr)
15664 if (!attr_form_is_constant (attr))
15666 complaint (_("DW_AT_alignment must have constant form"
15667 " - DIE at %s [in module %s]"),
15668 sect_offset_str (die->sect_off),
15669 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15674 if (attr->form == DW_FORM_sdata)
15676 LONGEST val = DW_SND (attr);
15679 complaint (_("DW_AT_alignment value must not be negative"
15680 " - DIE at %s [in module %s]"),
15681 sect_offset_str (die->sect_off),
15682 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15688 align = DW_UNSND (attr);
15692 complaint (_("DW_AT_alignment value must not be zero"
15693 " - DIE at %s [in module %s]"),
15694 sect_offset_str (die->sect_off),
15695 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15698 if ((align & (align - 1)) != 0)
15700 complaint (_("DW_AT_alignment value must be a power of 2"
15701 " - DIE at %s [in module %s]"),
15702 sect_offset_str (die->sect_off),
15703 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15710 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15711 the alignment for TYPE. */
15714 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15717 if (!set_type_align (type, get_alignment (cu, die)))
15718 complaint (_("DW_AT_alignment value too large"
15719 " - DIE at %s [in module %s]"),
15720 sect_offset_str (die->sect_off),
15721 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15724 /* Called when we find the DIE that starts a structure or union scope
15725 (definition) to create a type for the structure or union. Fill in
15726 the type's name and general properties; the members will not be
15727 processed until process_structure_scope. A symbol table entry for
15728 the type will also not be done until process_structure_scope (assuming
15729 the type has a name).
15731 NOTE: we need to call these functions regardless of whether or not the
15732 DIE has a DW_AT_name attribute, since it might be an anonymous
15733 structure or union. This gets the type entered into our set of
15734 user defined types. */
15736 static struct type *
15737 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15741 struct attribute *attr;
15744 /* If the definition of this type lives in .debug_types, read that type.
15745 Don't follow DW_AT_specification though, that will take us back up
15746 the chain and we want to go down. */
15747 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15750 type = get_DW_AT_signature_type (die, attr, cu);
15752 /* The type's CU may not be the same as CU.
15753 Ensure TYPE is recorded with CU in die_type_hash. */
15754 return set_die_type (die, type, cu);
15757 type = alloc_type (objfile);
15758 INIT_CPLUS_SPECIFIC (type);
15760 name = dwarf2_name (die, cu);
15763 if (cu->language == language_cplus
15764 || cu->language == language_d
15765 || cu->language == language_rust)
15767 const char *full_name = dwarf2_full_name (name, die, cu);
15769 /* dwarf2_full_name might have already finished building the DIE's
15770 type. If so, there is no need to continue. */
15771 if (get_die_type (die, cu) != NULL)
15772 return get_die_type (die, cu);
15774 TYPE_NAME (type) = full_name;
15778 /* The name is already allocated along with this objfile, so
15779 we don't need to duplicate it for the type. */
15780 TYPE_NAME (type) = name;
15784 if (die->tag == DW_TAG_structure_type)
15786 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15788 else if (die->tag == DW_TAG_union_type)
15790 TYPE_CODE (type) = TYPE_CODE_UNION;
15792 else if (die->tag == DW_TAG_variant_part)
15794 TYPE_CODE (type) = TYPE_CODE_UNION;
15795 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15799 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15802 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15803 TYPE_DECLARED_CLASS (type) = 1;
15805 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15808 if (attr_form_is_constant (attr))
15809 TYPE_LENGTH (type) = DW_UNSND (attr);
15812 /* For the moment, dynamic type sizes are not supported
15813 by GDB's struct type. The actual size is determined
15814 on-demand when resolving the type of a given object,
15815 so set the type's length to zero for now. Otherwise,
15816 we record an expression as the length, and that expression
15817 could lead to a very large value, which could eventually
15818 lead to us trying to allocate that much memory when creating
15819 a value of that type. */
15820 TYPE_LENGTH (type) = 0;
15825 TYPE_LENGTH (type) = 0;
15828 maybe_set_alignment (cu, die, type);
15830 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15832 /* ICC<14 does not output the required DW_AT_declaration on
15833 incomplete types, but gives them a size of zero. */
15834 TYPE_STUB (type) = 1;
15837 TYPE_STUB_SUPPORTED (type) = 1;
15839 if (die_is_declaration (die, cu))
15840 TYPE_STUB (type) = 1;
15841 else if (attr == NULL && die->child == NULL
15842 && producer_is_realview (cu->producer))
15843 /* RealView does not output the required DW_AT_declaration
15844 on incomplete types. */
15845 TYPE_STUB (type) = 1;
15847 /* We need to add the type field to the die immediately so we don't
15848 infinitely recurse when dealing with pointers to the structure
15849 type within the structure itself. */
15850 set_die_type (die, type, cu);
15852 /* set_die_type should be already done. */
15853 set_descriptive_type (type, die, cu);
15858 /* A helper for process_structure_scope that handles a single member
15862 handle_struct_member_die (struct die_info *child_die, struct type *type,
15863 struct field_info *fi,
15864 std::vector<struct symbol *> *template_args,
15865 struct dwarf2_cu *cu)
15867 if (child_die->tag == DW_TAG_member
15868 || child_die->tag == DW_TAG_variable
15869 || child_die->tag == DW_TAG_variant_part)
15871 /* NOTE: carlton/2002-11-05: A C++ static data member
15872 should be a DW_TAG_member that is a declaration, but
15873 all versions of G++ as of this writing (so through at
15874 least 3.2.1) incorrectly generate DW_TAG_variable
15875 tags for them instead. */
15876 dwarf2_add_field (fi, child_die, cu);
15878 else if (child_die->tag == DW_TAG_subprogram)
15880 /* Rust doesn't have member functions in the C++ sense.
15881 However, it does emit ordinary functions as children
15882 of a struct DIE. */
15883 if (cu->language == language_rust)
15884 read_func_scope (child_die, cu);
15887 /* C++ member function. */
15888 dwarf2_add_member_fn (fi, child_die, type, cu);
15891 else if (child_die->tag == DW_TAG_inheritance)
15893 /* C++ base class field. */
15894 dwarf2_add_field (fi, child_die, cu);
15896 else if (type_can_define_types (child_die))
15897 dwarf2_add_type_defn (fi, child_die, cu);
15898 else if (child_die->tag == DW_TAG_template_type_param
15899 || child_die->tag == DW_TAG_template_value_param)
15901 struct symbol *arg = new_symbol (child_die, NULL, cu);
15904 template_args->push_back (arg);
15906 else if (child_die->tag == DW_TAG_variant)
15908 /* In a variant we want to get the discriminant and also add a
15909 field for our sole member child. */
15910 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15912 for (die_info *variant_child = child_die->child;
15913 variant_child != NULL;
15914 variant_child = sibling_die (variant_child))
15916 if (variant_child->tag == DW_TAG_member)
15918 handle_struct_member_die (variant_child, type, fi,
15919 template_args, cu);
15920 /* Only handle the one. */
15925 /* We don't handle this but we might as well report it if we see
15927 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15928 complaint (_("DW_AT_discr_list is not supported yet"
15929 " - DIE at %s [in module %s]"),
15930 sect_offset_str (child_die->sect_off),
15931 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15933 /* The first field was just added, so we can stash the
15934 discriminant there. */
15935 gdb_assert (!fi->fields.empty ());
15937 fi->fields.back ().variant.default_branch = true;
15939 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15943 /* Finish creating a structure or union type, including filling in
15944 its members and creating a symbol for it. */
15947 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15949 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15950 struct die_info *child_die;
15953 type = get_die_type (die, cu);
15955 type = read_structure_type (die, cu);
15957 /* When reading a DW_TAG_variant_part, we need to notice when we
15958 read the discriminant member, so we can record it later in the
15959 discriminant_info. */
15960 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15961 sect_offset discr_offset;
15962 bool has_template_parameters = false;
15964 if (is_variant_part)
15966 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15969 /* Maybe it's a univariant form, an extension we support.
15970 In this case arrange not to check the offset. */
15971 is_variant_part = false;
15973 else if (attr_form_is_ref (discr))
15975 struct dwarf2_cu *target_cu = cu;
15976 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15978 discr_offset = target_die->sect_off;
15982 complaint (_("DW_AT_discr does not have DIE reference form"
15983 " - DIE at %s [in module %s]"),
15984 sect_offset_str (die->sect_off),
15985 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15986 is_variant_part = false;
15990 if (die->child != NULL && ! die_is_declaration (die, cu))
15992 struct field_info fi;
15993 std::vector<struct symbol *> template_args;
15995 child_die = die->child;
15997 while (child_die && child_die->tag)
15999 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16001 if (is_variant_part && discr_offset == child_die->sect_off)
16002 fi.fields.back ().variant.is_discriminant = true;
16004 child_die = sibling_die (child_die);
16007 /* Attach template arguments to type. */
16008 if (!template_args.empty ())
16010 has_template_parameters = true;
16011 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16012 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16013 TYPE_TEMPLATE_ARGUMENTS (type)
16014 = XOBNEWVEC (&objfile->objfile_obstack,
16016 TYPE_N_TEMPLATE_ARGUMENTS (type));
16017 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16018 template_args.data (),
16019 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16020 * sizeof (struct symbol *)));
16023 /* Attach fields and member functions to the type. */
16025 dwarf2_attach_fields_to_type (&fi, type, cu);
16026 if (!fi.fnfieldlists.empty ())
16028 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16030 /* Get the type which refers to the base class (possibly this
16031 class itself) which contains the vtable pointer for the current
16032 class from the DW_AT_containing_type attribute. This use of
16033 DW_AT_containing_type is a GNU extension. */
16035 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16037 struct type *t = die_containing_type (die, cu);
16039 set_type_vptr_basetype (type, t);
16044 /* Our own class provides vtbl ptr. */
16045 for (i = TYPE_NFIELDS (t) - 1;
16046 i >= TYPE_N_BASECLASSES (t);
16049 const char *fieldname = TYPE_FIELD_NAME (t, i);
16051 if (is_vtable_name (fieldname, cu))
16053 set_type_vptr_fieldno (type, i);
16058 /* Complain if virtual function table field not found. */
16059 if (i < TYPE_N_BASECLASSES (t))
16060 complaint (_("virtual function table pointer "
16061 "not found when defining class '%s'"),
16062 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16066 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16069 else if (cu->producer
16070 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16072 /* The IBM XLC compiler does not provide direct indication
16073 of the containing type, but the vtable pointer is
16074 always named __vfp. */
16078 for (i = TYPE_NFIELDS (type) - 1;
16079 i >= TYPE_N_BASECLASSES (type);
16082 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16084 set_type_vptr_fieldno (type, i);
16085 set_type_vptr_basetype (type, type);
16092 /* Copy fi.typedef_field_list linked list elements content into the
16093 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16094 if (!fi.typedef_field_list.empty ())
16096 int count = fi.typedef_field_list.size ();
16098 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16099 TYPE_TYPEDEF_FIELD_ARRAY (type)
16100 = ((struct decl_field *)
16102 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16103 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16105 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16106 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16109 /* Copy fi.nested_types_list linked list elements content into the
16110 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16111 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16113 int count = fi.nested_types_list.size ();
16115 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16116 TYPE_NESTED_TYPES_ARRAY (type)
16117 = ((struct decl_field *)
16118 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16119 TYPE_NESTED_TYPES_COUNT (type) = count;
16121 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16122 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16126 quirk_gcc_member_function_pointer (type, objfile);
16127 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16128 cu->rust_unions.push_back (type);
16130 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16131 snapshots) has been known to create a die giving a declaration
16132 for a class that has, as a child, a die giving a definition for a
16133 nested class. So we have to process our children even if the
16134 current die is a declaration. Normally, of course, a declaration
16135 won't have any children at all. */
16137 child_die = die->child;
16139 while (child_die != NULL && child_die->tag)
16141 if (child_die->tag == DW_TAG_member
16142 || child_die->tag == DW_TAG_variable
16143 || child_die->tag == DW_TAG_inheritance
16144 || child_die->tag == DW_TAG_template_value_param
16145 || child_die->tag == DW_TAG_template_type_param)
16150 process_die (child_die, cu);
16152 child_die = sibling_die (child_die);
16155 /* Do not consider external references. According to the DWARF standard,
16156 these DIEs are identified by the fact that they have no byte_size
16157 attribute, and a declaration attribute. */
16158 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16159 || !die_is_declaration (die, cu))
16161 struct symbol *sym = new_symbol (die, type, cu);
16163 if (has_template_parameters)
16165 struct symtab *symtab;
16166 if (sym != nullptr)
16167 symtab = symbol_symtab (sym);
16168 else if (cu->line_header != nullptr)
16170 /* Any related symtab will do. */
16172 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16177 complaint (_("could not find suitable "
16178 "symtab for template parameter"
16179 " - DIE at %s [in module %s]"),
16180 sect_offset_str (die->sect_off),
16181 objfile_name (objfile));
16184 if (symtab != nullptr)
16186 /* Make sure that the symtab is set on the new symbols.
16187 Even though they don't appear in this symtab directly,
16188 other parts of gdb assume that symbols do, and this is
16189 reasonably true. */
16190 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16191 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16197 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16198 update TYPE using some information only available in DIE's children. */
16201 update_enumeration_type_from_children (struct die_info *die,
16203 struct dwarf2_cu *cu)
16205 struct die_info *child_die;
16206 int unsigned_enum = 1;
16210 auto_obstack obstack;
16212 for (child_die = die->child;
16213 child_die != NULL && child_die->tag;
16214 child_die = sibling_die (child_die))
16216 struct attribute *attr;
16218 const gdb_byte *bytes;
16219 struct dwarf2_locexpr_baton *baton;
16222 if (child_die->tag != DW_TAG_enumerator)
16225 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16229 name = dwarf2_name (child_die, cu);
16231 name = "<anonymous enumerator>";
16233 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16234 &value, &bytes, &baton);
16240 else if ((mask & value) != 0)
16245 /* If we already know that the enum type is neither unsigned, nor
16246 a flag type, no need to look at the rest of the enumerates. */
16247 if (!unsigned_enum && !flag_enum)
16252 TYPE_UNSIGNED (type) = 1;
16254 TYPE_FLAG_ENUM (type) = 1;
16257 /* Given a DW_AT_enumeration_type die, set its type. We do not
16258 complete the type's fields yet, or create any symbols. */
16260 static struct type *
16261 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16263 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16265 struct attribute *attr;
16268 /* If the definition of this type lives in .debug_types, read that type.
16269 Don't follow DW_AT_specification though, that will take us back up
16270 the chain and we want to go down. */
16271 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16274 type = get_DW_AT_signature_type (die, attr, cu);
16276 /* The type's CU may not be the same as CU.
16277 Ensure TYPE is recorded with CU in die_type_hash. */
16278 return set_die_type (die, type, cu);
16281 type = alloc_type (objfile);
16283 TYPE_CODE (type) = TYPE_CODE_ENUM;
16284 name = dwarf2_full_name (NULL, die, cu);
16286 TYPE_NAME (type) = name;
16288 attr = dwarf2_attr (die, DW_AT_type, cu);
16291 struct type *underlying_type = die_type (die, cu);
16293 TYPE_TARGET_TYPE (type) = underlying_type;
16296 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16299 TYPE_LENGTH (type) = DW_UNSND (attr);
16303 TYPE_LENGTH (type) = 0;
16306 maybe_set_alignment (cu, die, type);
16308 /* The enumeration DIE can be incomplete. In Ada, any type can be
16309 declared as private in the package spec, and then defined only
16310 inside the package body. Such types are known as Taft Amendment
16311 Types. When another package uses such a type, an incomplete DIE
16312 may be generated by the compiler. */
16313 if (die_is_declaration (die, cu))
16314 TYPE_STUB (type) = 1;
16316 /* Finish the creation of this type by using the enum's children.
16317 We must call this even when the underlying type has been provided
16318 so that we can determine if we're looking at a "flag" enum. */
16319 update_enumeration_type_from_children (die, type, cu);
16321 /* If this type has an underlying type that is not a stub, then we
16322 may use its attributes. We always use the "unsigned" attribute
16323 in this situation, because ordinarily we guess whether the type
16324 is unsigned -- but the guess can be wrong and the underlying type
16325 can tell us the reality. However, we defer to a local size
16326 attribute if one exists, because this lets the compiler override
16327 the underlying type if needed. */
16328 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16330 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16331 if (TYPE_LENGTH (type) == 0)
16332 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16333 if (TYPE_RAW_ALIGN (type) == 0
16334 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16335 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16338 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16340 return set_die_type (die, type, cu);
16343 /* Given a pointer to a die which begins an enumeration, process all
16344 the dies that define the members of the enumeration, and create the
16345 symbol for the enumeration type.
16347 NOTE: We reverse the order of the element list. */
16350 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16352 struct type *this_type;
16354 this_type = get_die_type (die, cu);
16355 if (this_type == NULL)
16356 this_type = read_enumeration_type (die, cu);
16358 if (die->child != NULL)
16360 struct die_info *child_die;
16361 struct symbol *sym;
16362 struct field *fields = NULL;
16363 int num_fields = 0;
16366 child_die = die->child;
16367 while (child_die && child_die->tag)
16369 if (child_die->tag != DW_TAG_enumerator)
16371 process_die (child_die, cu);
16375 name = dwarf2_name (child_die, cu);
16378 sym = new_symbol (child_die, this_type, cu);
16380 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16382 fields = (struct field *)
16384 (num_fields + DW_FIELD_ALLOC_CHUNK)
16385 * sizeof (struct field));
16388 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16389 FIELD_TYPE (fields[num_fields]) = NULL;
16390 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16391 FIELD_BITSIZE (fields[num_fields]) = 0;
16397 child_die = sibling_die (child_die);
16402 TYPE_NFIELDS (this_type) = num_fields;
16403 TYPE_FIELDS (this_type) = (struct field *)
16404 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16405 memcpy (TYPE_FIELDS (this_type), fields,
16406 sizeof (struct field) * num_fields);
16411 /* If we are reading an enum from a .debug_types unit, and the enum
16412 is a declaration, and the enum is not the signatured type in the
16413 unit, then we do not want to add a symbol for it. Adding a
16414 symbol would in some cases obscure the true definition of the
16415 enum, giving users an incomplete type when the definition is
16416 actually available. Note that we do not want to do this for all
16417 enums which are just declarations, because C++0x allows forward
16418 enum declarations. */
16419 if (cu->per_cu->is_debug_types
16420 && die_is_declaration (die, cu))
16422 struct signatured_type *sig_type;
16424 sig_type = (struct signatured_type *) cu->per_cu;
16425 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16426 if (sig_type->type_offset_in_section != die->sect_off)
16430 new_symbol (die, this_type, cu);
16433 /* Extract all information from a DW_TAG_array_type DIE and put it in
16434 the DIE's type field. For now, this only handles one dimensional
16437 static struct type *
16438 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16441 struct die_info *child_die;
16443 struct type *element_type, *range_type, *index_type;
16444 struct attribute *attr;
16446 struct dynamic_prop *byte_stride_prop = NULL;
16447 unsigned int bit_stride = 0;
16449 element_type = die_type (die, cu);
16451 /* The die_type call above may have already set the type for this DIE. */
16452 type = get_die_type (die, cu);
16456 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16462 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16463 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16466 complaint (_("unable to read array DW_AT_byte_stride "
16467 " - DIE at %s [in module %s]"),
16468 sect_offset_str (die->sect_off),
16469 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16470 /* Ignore this attribute. We will likely not be able to print
16471 arrays of this type correctly, but there is little we can do
16472 to help if we cannot read the attribute's value. */
16473 byte_stride_prop = NULL;
16477 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16479 bit_stride = DW_UNSND (attr);
16481 /* Irix 6.2 native cc creates array types without children for
16482 arrays with unspecified length. */
16483 if (die->child == NULL)
16485 index_type = objfile_type (objfile)->builtin_int;
16486 range_type = create_static_range_type (NULL, index_type, 0, -1);
16487 type = create_array_type_with_stride (NULL, element_type, range_type,
16488 byte_stride_prop, bit_stride);
16489 return set_die_type (die, type, cu);
16492 std::vector<struct type *> range_types;
16493 child_die = die->child;
16494 while (child_die && child_die->tag)
16496 if (child_die->tag == DW_TAG_subrange_type)
16498 struct type *child_type = read_type_die (child_die, cu);
16500 if (child_type != NULL)
16502 /* The range type was succesfully read. Save it for the
16503 array type creation. */
16504 range_types.push_back (child_type);
16507 child_die = sibling_die (child_die);
16510 /* Dwarf2 dimensions are output from left to right, create the
16511 necessary array types in backwards order. */
16513 type = element_type;
16515 if (read_array_order (die, cu) == DW_ORD_col_major)
16519 while (i < range_types.size ())
16520 type = create_array_type_with_stride (NULL, type, range_types[i++],
16521 byte_stride_prop, bit_stride);
16525 size_t ndim = range_types.size ();
16527 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16528 byte_stride_prop, bit_stride);
16531 /* Understand Dwarf2 support for vector types (like they occur on
16532 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16533 array type. This is not part of the Dwarf2/3 standard yet, but a
16534 custom vendor extension. The main difference between a regular
16535 array and the vector variant is that vectors are passed by value
16537 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16539 make_vector_type (type);
16541 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16542 implementation may choose to implement triple vectors using this
16544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16547 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16548 TYPE_LENGTH (type) = DW_UNSND (attr);
16550 complaint (_("DW_AT_byte_size for array type smaller "
16551 "than the total size of elements"));
16554 name = dwarf2_name (die, cu);
16556 TYPE_NAME (type) = name;
16558 maybe_set_alignment (cu, die, type);
16560 /* Install the type in the die. */
16561 set_die_type (die, type, cu);
16563 /* set_die_type should be already done. */
16564 set_descriptive_type (type, die, cu);
16569 static enum dwarf_array_dim_ordering
16570 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16572 struct attribute *attr;
16574 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16577 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16579 /* GNU F77 is a special case, as at 08/2004 array type info is the
16580 opposite order to the dwarf2 specification, but data is still
16581 laid out as per normal fortran.
16583 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16584 version checking. */
16586 if (cu->language == language_fortran
16587 && cu->producer && strstr (cu->producer, "GNU F77"))
16589 return DW_ORD_row_major;
16592 switch (cu->language_defn->la_array_ordering)
16594 case array_column_major:
16595 return DW_ORD_col_major;
16596 case array_row_major:
16598 return DW_ORD_row_major;
16602 /* Extract all information from a DW_TAG_set_type DIE and put it in
16603 the DIE's type field. */
16605 static struct type *
16606 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16608 struct type *domain_type, *set_type;
16609 struct attribute *attr;
16611 domain_type = die_type (die, cu);
16613 /* The die_type call above may have already set the type for this DIE. */
16614 set_type = get_die_type (die, cu);
16618 set_type = create_set_type (NULL, domain_type);
16620 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16622 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16624 maybe_set_alignment (cu, die, set_type);
16626 return set_die_type (die, set_type, cu);
16629 /* A helper for read_common_block that creates a locexpr baton.
16630 SYM is the symbol which we are marking as computed.
16631 COMMON_DIE is the DIE for the common block.
16632 COMMON_LOC is the location expression attribute for the common
16634 MEMBER_LOC is the location expression attribute for the particular
16635 member of the common block that we are processing.
16636 CU is the CU from which the above come. */
16639 mark_common_block_symbol_computed (struct symbol *sym,
16640 struct die_info *common_die,
16641 struct attribute *common_loc,
16642 struct attribute *member_loc,
16643 struct dwarf2_cu *cu)
16645 struct dwarf2_per_objfile *dwarf2_per_objfile
16646 = cu->per_cu->dwarf2_per_objfile;
16647 struct objfile *objfile = dwarf2_per_objfile->objfile;
16648 struct dwarf2_locexpr_baton *baton;
16650 unsigned int cu_off;
16651 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16652 LONGEST offset = 0;
16654 gdb_assert (common_loc && member_loc);
16655 gdb_assert (attr_form_is_block (common_loc));
16656 gdb_assert (attr_form_is_block (member_loc)
16657 || attr_form_is_constant (member_loc));
16659 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16660 baton->per_cu = cu->per_cu;
16661 gdb_assert (baton->per_cu);
16663 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16665 if (attr_form_is_constant (member_loc))
16667 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16668 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16671 baton->size += DW_BLOCK (member_loc)->size;
16673 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16676 *ptr++ = DW_OP_call4;
16677 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16678 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16681 if (attr_form_is_constant (member_loc))
16683 *ptr++ = DW_OP_addr;
16684 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16685 ptr += cu->header.addr_size;
16689 /* We have to copy the data here, because DW_OP_call4 will only
16690 use a DW_AT_location attribute. */
16691 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16692 ptr += DW_BLOCK (member_loc)->size;
16695 *ptr++ = DW_OP_plus;
16696 gdb_assert (ptr - baton->data == baton->size);
16698 SYMBOL_LOCATION_BATON (sym) = baton;
16699 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16702 /* Create appropriate locally-scoped variables for all the
16703 DW_TAG_common_block entries. Also create a struct common_block
16704 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16705 is used to sepate the common blocks name namespace from regular
16709 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16711 struct attribute *attr;
16713 attr = dwarf2_attr (die, DW_AT_location, cu);
16716 /* Support the .debug_loc offsets. */
16717 if (attr_form_is_block (attr))
16721 else if (attr_form_is_section_offset (attr))
16723 dwarf2_complex_location_expr_complaint ();
16728 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16729 "common block member");
16734 if (die->child != NULL)
16736 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16737 struct die_info *child_die;
16738 size_t n_entries = 0, size;
16739 struct common_block *common_block;
16740 struct symbol *sym;
16742 for (child_die = die->child;
16743 child_die && child_die->tag;
16744 child_die = sibling_die (child_die))
16747 size = (sizeof (struct common_block)
16748 + (n_entries - 1) * sizeof (struct symbol *));
16750 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16752 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16753 common_block->n_entries = 0;
16755 for (child_die = die->child;
16756 child_die && child_die->tag;
16757 child_die = sibling_die (child_die))
16759 /* Create the symbol in the DW_TAG_common_block block in the current
16761 sym = new_symbol (child_die, NULL, cu);
16764 struct attribute *member_loc;
16766 common_block->contents[common_block->n_entries++] = sym;
16768 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16772 /* GDB has handled this for a long time, but it is
16773 not specified by DWARF. It seems to have been
16774 emitted by gfortran at least as recently as:
16775 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16776 complaint (_("Variable in common block has "
16777 "DW_AT_data_member_location "
16778 "- DIE at %s [in module %s]"),
16779 sect_offset_str (child_die->sect_off),
16780 objfile_name (objfile));
16782 if (attr_form_is_section_offset (member_loc))
16783 dwarf2_complex_location_expr_complaint ();
16784 else if (attr_form_is_constant (member_loc)
16785 || attr_form_is_block (member_loc))
16788 mark_common_block_symbol_computed (sym, die, attr,
16792 dwarf2_complex_location_expr_complaint ();
16797 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16798 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16802 /* Create a type for a C++ namespace. */
16804 static struct type *
16805 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16807 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16808 const char *previous_prefix, *name;
16812 /* For extensions, reuse the type of the original namespace. */
16813 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16815 struct die_info *ext_die;
16816 struct dwarf2_cu *ext_cu = cu;
16818 ext_die = dwarf2_extension (die, &ext_cu);
16819 type = read_type_die (ext_die, ext_cu);
16821 /* EXT_CU may not be the same as CU.
16822 Ensure TYPE is recorded with CU in die_type_hash. */
16823 return set_die_type (die, type, cu);
16826 name = namespace_name (die, &is_anonymous, cu);
16828 /* Now build the name of the current namespace. */
16830 previous_prefix = determine_prefix (die, cu);
16831 if (previous_prefix[0] != '\0')
16832 name = typename_concat (&objfile->objfile_obstack,
16833 previous_prefix, name, 0, cu);
16835 /* Create the type. */
16836 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16838 return set_die_type (die, type, cu);
16841 /* Read a namespace scope. */
16844 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16849 /* Add a symbol associated to this if we haven't seen the namespace
16850 before. Also, add a using directive if it's an anonymous
16853 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16857 type = read_type_die (die, cu);
16858 new_symbol (die, type, cu);
16860 namespace_name (die, &is_anonymous, cu);
16863 const char *previous_prefix = determine_prefix (die, cu);
16865 std::vector<const char *> excludes;
16866 add_using_directive (using_directives (cu),
16867 previous_prefix, TYPE_NAME (type), NULL,
16868 NULL, excludes, 0, &objfile->objfile_obstack);
16872 if (die->child != NULL)
16874 struct die_info *child_die = die->child;
16876 while (child_die && child_die->tag)
16878 process_die (child_die, cu);
16879 child_die = sibling_die (child_die);
16884 /* Read a Fortran module as type. This DIE can be only a declaration used for
16885 imported module. Still we need that type as local Fortran "use ... only"
16886 declaration imports depend on the created type in determine_prefix. */
16888 static struct type *
16889 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16892 const char *module_name;
16895 module_name = dwarf2_name (die, cu);
16896 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16898 return set_die_type (die, type, cu);
16901 /* Read a Fortran module. */
16904 read_module (struct die_info *die, struct dwarf2_cu *cu)
16906 struct die_info *child_die = die->child;
16909 type = read_type_die (die, cu);
16910 new_symbol (die, type, cu);
16912 while (child_die && child_die->tag)
16914 process_die (child_die, cu);
16915 child_die = sibling_die (child_die);
16919 /* Return the name of the namespace represented by DIE. Set
16920 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16923 static const char *
16924 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16926 struct die_info *current_die;
16927 const char *name = NULL;
16929 /* Loop through the extensions until we find a name. */
16931 for (current_die = die;
16932 current_die != NULL;
16933 current_die = dwarf2_extension (die, &cu))
16935 /* We don't use dwarf2_name here so that we can detect the absence
16936 of a name -> anonymous namespace. */
16937 name = dwarf2_string_attr (die, DW_AT_name, cu);
16943 /* Is it an anonymous namespace? */
16945 *is_anonymous = (name == NULL);
16947 name = CP_ANONYMOUS_NAMESPACE_STR;
16952 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16953 the user defined type vector. */
16955 static struct type *
16956 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16958 struct gdbarch *gdbarch
16959 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16960 struct comp_unit_head *cu_header = &cu->header;
16962 struct attribute *attr_byte_size;
16963 struct attribute *attr_address_class;
16964 int byte_size, addr_class;
16965 struct type *target_type;
16967 target_type = die_type (die, cu);
16969 /* The die_type call above may have already set the type for this DIE. */
16970 type = get_die_type (die, cu);
16974 type = lookup_pointer_type (target_type);
16976 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16977 if (attr_byte_size)
16978 byte_size = DW_UNSND (attr_byte_size);
16980 byte_size = cu_header->addr_size;
16982 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16983 if (attr_address_class)
16984 addr_class = DW_UNSND (attr_address_class);
16986 addr_class = DW_ADDR_none;
16988 ULONGEST alignment = get_alignment (cu, die);
16990 /* If the pointer size, alignment, or address class is different
16991 than the default, create a type variant marked as such and set
16992 the length accordingly. */
16993 if (TYPE_LENGTH (type) != byte_size
16994 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16995 && alignment != TYPE_RAW_ALIGN (type))
16996 || addr_class != DW_ADDR_none)
16998 if (gdbarch_address_class_type_flags_p (gdbarch))
17002 type_flags = gdbarch_address_class_type_flags
17003 (gdbarch, byte_size, addr_class);
17004 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17006 type = make_type_with_address_space (type, type_flags);
17008 else if (TYPE_LENGTH (type) != byte_size)
17010 complaint (_("invalid pointer size %d"), byte_size);
17012 else if (TYPE_RAW_ALIGN (type) != alignment)
17014 complaint (_("Invalid DW_AT_alignment"
17015 " - DIE at %s [in module %s]"),
17016 sect_offset_str (die->sect_off),
17017 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17021 /* Should we also complain about unhandled address classes? */
17025 TYPE_LENGTH (type) = byte_size;
17026 set_type_align (type, alignment);
17027 return set_die_type (die, type, cu);
17030 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17031 the user defined type vector. */
17033 static struct type *
17034 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17037 struct type *to_type;
17038 struct type *domain;
17040 to_type = die_type (die, cu);
17041 domain = die_containing_type (die, cu);
17043 /* The calls above may have already set the type for this DIE. */
17044 type = get_die_type (die, cu);
17048 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17049 type = lookup_methodptr_type (to_type);
17050 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17052 struct type *new_type
17053 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17055 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17056 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17057 TYPE_VARARGS (to_type));
17058 type = lookup_methodptr_type (new_type);
17061 type = lookup_memberptr_type (to_type, domain);
17063 return set_die_type (die, type, cu);
17066 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17067 the user defined type vector. */
17069 static struct type *
17070 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17071 enum type_code refcode)
17073 struct comp_unit_head *cu_header = &cu->header;
17074 struct type *type, *target_type;
17075 struct attribute *attr;
17077 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17079 target_type = die_type (die, cu);
17081 /* The die_type call above may have already set the type for this DIE. */
17082 type = get_die_type (die, cu);
17086 type = lookup_reference_type (target_type, refcode);
17087 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17090 TYPE_LENGTH (type) = DW_UNSND (attr);
17094 TYPE_LENGTH (type) = cu_header->addr_size;
17096 maybe_set_alignment (cu, die, type);
17097 return set_die_type (die, type, cu);
17100 /* Add the given cv-qualifiers to the element type of the array. GCC
17101 outputs DWARF type qualifiers that apply to an array, not the
17102 element type. But GDB relies on the array element type to carry
17103 the cv-qualifiers. This mimics section 6.7.3 of the C99
17106 static struct type *
17107 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17108 struct type *base_type, int cnst, int voltl)
17110 struct type *el_type, *inner_array;
17112 base_type = copy_type (base_type);
17113 inner_array = base_type;
17115 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17117 TYPE_TARGET_TYPE (inner_array) =
17118 copy_type (TYPE_TARGET_TYPE (inner_array));
17119 inner_array = TYPE_TARGET_TYPE (inner_array);
17122 el_type = TYPE_TARGET_TYPE (inner_array);
17123 cnst |= TYPE_CONST (el_type);
17124 voltl |= TYPE_VOLATILE (el_type);
17125 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17127 return set_die_type (die, base_type, cu);
17130 static struct type *
17131 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17133 struct type *base_type, *cv_type;
17135 base_type = die_type (die, cu);
17137 /* The die_type call above may have already set the type for this DIE. */
17138 cv_type = get_die_type (die, cu);
17142 /* In case the const qualifier is applied to an array type, the element type
17143 is so qualified, not the array type (section 6.7.3 of C99). */
17144 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17145 return add_array_cv_type (die, cu, base_type, 1, 0);
17147 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17148 return set_die_type (die, cv_type, cu);
17151 static struct type *
17152 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17154 struct type *base_type, *cv_type;
17156 base_type = die_type (die, cu);
17158 /* The die_type call above may have already set the type for this DIE. */
17159 cv_type = get_die_type (die, cu);
17163 /* In case the volatile qualifier is applied to an array type, the
17164 element type is so qualified, not the array type (section 6.7.3
17166 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17167 return add_array_cv_type (die, cu, base_type, 0, 1);
17169 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17170 return set_die_type (die, cv_type, cu);
17173 /* Handle DW_TAG_restrict_type. */
17175 static struct type *
17176 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17178 struct type *base_type, *cv_type;
17180 base_type = die_type (die, cu);
17182 /* The die_type call above may have already set the type for this DIE. */
17183 cv_type = get_die_type (die, cu);
17187 cv_type = make_restrict_type (base_type);
17188 return set_die_type (die, cv_type, cu);
17191 /* Handle DW_TAG_atomic_type. */
17193 static struct type *
17194 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17196 struct type *base_type, *cv_type;
17198 base_type = die_type (die, cu);
17200 /* The die_type call above may have already set the type for this DIE. */
17201 cv_type = get_die_type (die, cu);
17205 cv_type = make_atomic_type (base_type);
17206 return set_die_type (die, cv_type, cu);
17209 /* Extract all information from a DW_TAG_string_type DIE and add to
17210 the user defined type vector. It isn't really a user defined type,
17211 but it behaves like one, with other DIE's using an AT_user_def_type
17212 attribute to reference it. */
17214 static struct type *
17215 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17217 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17219 struct type *type, *range_type, *index_type, *char_type;
17220 struct attribute *attr;
17221 unsigned int length;
17223 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17226 length = DW_UNSND (attr);
17230 /* Check for the DW_AT_byte_size attribute. */
17231 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17234 length = DW_UNSND (attr);
17242 index_type = objfile_type (objfile)->builtin_int;
17243 range_type = create_static_range_type (NULL, index_type, 1, length);
17244 char_type = language_string_char_type (cu->language_defn, gdbarch);
17245 type = create_string_type (NULL, char_type, range_type);
17247 return set_die_type (die, type, cu);
17250 /* Assuming that DIE corresponds to a function, returns nonzero
17251 if the function is prototyped. */
17254 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17256 struct attribute *attr;
17258 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17259 if (attr && (DW_UNSND (attr) != 0))
17262 /* The DWARF standard implies that the DW_AT_prototyped attribute
17263 is only meaninful for C, but the concept also extends to other
17264 languages that allow unprototyped functions (Eg: Objective C).
17265 For all other languages, assume that functions are always
17267 if (cu->language != language_c
17268 && cu->language != language_objc
17269 && cu->language != language_opencl)
17272 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17273 prototyped and unprototyped functions; default to prototyped,
17274 since that is more common in modern code (and RealView warns
17275 about unprototyped functions). */
17276 if (producer_is_realview (cu->producer))
17282 /* Handle DIES due to C code like:
17286 int (*funcp)(int a, long l);
17290 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17292 static struct type *
17293 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17296 struct type *type; /* Type that this function returns. */
17297 struct type *ftype; /* Function that returns above type. */
17298 struct attribute *attr;
17300 type = die_type (die, cu);
17302 /* The die_type call above may have already set the type for this DIE. */
17303 ftype = get_die_type (die, cu);
17307 ftype = lookup_function_type (type);
17309 if (prototyped_function_p (die, cu))
17310 TYPE_PROTOTYPED (ftype) = 1;
17312 /* Store the calling convention in the type if it's available in
17313 the subroutine die. Otherwise set the calling convention to
17314 the default value DW_CC_normal. */
17315 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17317 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17318 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17319 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17321 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17323 /* Record whether the function returns normally to its caller or not
17324 if the DWARF producer set that information. */
17325 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17326 if (attr && (DW_UNSND (attr) != 0))
17327 TYPE_NO_RETURN (ftype) = 1;
17329 /* We need to add the subroutine type to the die immediately so
17330 we don't infinitely recurse when dealing with parameters
17331 declared as the same subroutine type. */
17332 set_die_type (die, ftype, cu);
17334 if (die->child != NULL)
17336 struct type *void_type = objfile_type (objfile)->builtin_void;
17337 struct die_info *child_die;
17338 int nparams, iparams;
17340 /* Count the number of parameters.
17341 FIXME: GDB currently ignores vararg functions, but knows about
17342 vararg member functions. */
17344 child_die = die->child;
17345 while (child_die && child_die->tag)
17347 if (child_die->tag == DW_TAG_formal_parameter)
17349 else if (child_die->tag == DW_TAG_unspecified_parameters)
17350 TYPE_VARARGS (ftype) = 1;
17351 child_die = sibling_die (child_die);
17354 /* Allocate storage for parameters and fill them in. */
17355 TYPE_NFIELDS (ftype) = nparams;
17356 TYPE_FIELDS (ftype) = (struct field *)
17357 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17359 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17360 even if we error out during the parameters reading below. */
17361 for (iparams = 0; iparams < nparams; iparams++)
17362 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17365 child_die = die->child;
17366 while (child_die && child_die->tag)
17368 if (child_die->tag == DW_TAG_formal_parameter)
17370 struct type *arg_type;
17372 /* DWARF version 2 has no clean way to discern C++
17373 static and non-static member functions. G++ helps
17374 GDB by marking the first parameter for non-static
17375 member functions (which is the this pointer) as
17376 artificial. We pass this information to
17377 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17379 DWARF version 3 added DW_AT_object_pointer, which GCC
17380 4.5 does not yet generate. */
17381 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17383 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17385 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17386 arg_type = die_type (child_die, cu);
17388 /* RealView does not mark THIS as const, which the testsuite
17389 expects. GCC marks THIS as const in method definitions,
17390 but not in the class specifications (GCC PR 43053). */
17391 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17392 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17395 struct dwarf2_cu *arg_cu = cu;
17396 const char *name = dwarf2_name (child_die, cu);
17398 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17401 /* If the compiler emits this, use it. */
17402 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17405 else if (name && strcmp (name, "this") == 0)
17406 /* Function definitions will have the argument names. */
17408 else if (name == NULL && iparams == 0)
17409 /* Declarations may not have the names, so like
17410 elsewhere in GDB, assume an artificial first
17411 argument is "this". */
17415 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17419 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17422 child_die = sibling_die (child_die);
17429 static struct type *
17430 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17433 const char *name = NULL;
17434 struct type *this_type, *target_type;
17436 name = dwarf2_full_name (NULL, die, cu);
17437 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17438 TYPE_TARGET_STUB (this_type) = 1;
17439 set_die_type (die, this_type, cu);
17440 target_type = die_type (die, cu);
17441 if (target_type != this_type)
17442 TYPE_TARGET_TYPE (this_type) = target_type;
17445 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17446 spec and cause infinite loops in GDB. */
17447 complaint (_("Self-referential DW_TAG_typedef "
17448 "- DIE at %s [in module %s]"),
17449 sect_offset_str (die->sect_off), objfile_name (objfile));
17450 TYPE_TARGET_TYPE (this_type) = NULL;
17455 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17456 (which may be different from NAME) to the architecture back-end to allow
17457 it to guess the correct format if necessary. */
17459 static struct type *
17460 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17461 const char *name_hint)
17463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17464 const struct floatformat **format;
17467 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17469 type = init_float_type (objfile, bits, name, format);
17471 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17476 /* Allocate an integer type of size BITS and name NAME. */
17478 static struct type *
17479 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17480 int bits, int unsigned_p, const char *name)
17484 /* Versions of Intel's C Compiler generate an integer type called "void"
17485 instead of using DW_TAG_unspecified_type. This has been seen on
17486 at least versions 14, 17, and 18. */
17487 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17488 && strcmp (name, "void") == 0)
17489 type = objfile_type (objfile)->builtin_void;
17491 type = init_integer_type (objfile, bits, unsigned_p, name);
17496 /* Initialise and return a floating point type of size BITS suitable for
17497 use as a component of a complex number. The NAME_HINT is passed through
17498 when initialising the floating point type and is the name of the complex
17501 As DWARF doesn't currently provide an explicit name for the components
17502 of a complex number, but it can be helpful to have these components
17503 named, we try to select a suitable name based on the size of the
17505 static struct type *
17506 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17507 struct objfile *objfile,
17508 int bits, const char *name_hint)
17510 gdbarch *gdbarch = get_objfile_arch (objfile);
17511 struct type *tt = nullptr;
17513 /* Try to find a suitable floating point builtin type of size BITS.
17514 We're going to use the name of this type as the name for the complex
17515 target type that we are about to create. */
17516 switch (cu->language)
17518 case language_fortran:
17522 tt = builtin_f_type (gdbarch)->builtin_real;
17525 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17527 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17529 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17537 tt = builtin_type (gdbarch)->builtin_float;
17540 tt = builtin_type (gdbarch)->builtin_double;
17542 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17544 tt = builtin_type (gdbarch)->builtin_long_double;
17550 /* If the type we found doesn't match the size we were looking for, then
17551 pretend we didn't find a type at all, the complex target type we
17552 create will then be nameless. */
17553 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17556 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17557 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17560 /* Find a representation of a given base type and install
17561 it in the TYPE field of the die. */
17563 static struct type *
17564 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17566 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17568 struct attribute *attr;
17569 int encoding = 0, bits = 0;
17572 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17575 encoding = DW_UNSND (attr);
17577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17580 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17582 name = dwarf2_name (die, cu);
17585 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17590 case DW_ATE_address:
17591 /* Turn DW_ATE_address into a void * pointer. */
17592 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17593 type = init_pointer_type (objfile, bits, name, type);
17595 case DW_ATE_boolean:
17596 type = init_boolean_type (objfile, bits, 1, name);
17598 case DW_ATE_complex_float:
17599 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17600 type = init_complex_type (objfile, name, type);
17602 case DW_ATE_decimal_float:
17603 type = init_decfloat_type (objfile, bits, name);
17606 type = dwarf2_init_float_type (objfile, bits, name, name);
17608 case DW_ATE_signed:
17609 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17611 case DW_ATE_unsigned:
17612 if (cu->language == language_fortran
17614 && startswith (name, "character("))
17615 type = init_character_type (objfile, bits, 1, name);
17617 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17619 case DW_ATE_signed_char:
17620 if (cu->language == language_ada || cu->language == language_m2
17621 || cu->language == language_pascal
17622 || cu->language == language_fortran)
17623 type = init_character_type (objfile, bits, 0, name);
17625 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17627 case DW_ATE_unsigned_char:
17628 if (cu->language == language_ada || cu->language == language_m2
17629 || cu->language == language_pascal
17630 || cu->language == language_fortran
17631 || cu->language == language_rust)
17632 type = init_character_type (objfile, bits, 1, name);
17634 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17638 gdbarch *arch = get_objfile_arch (objfile);
17641 type = builtin_type (arch)->builtin_char16;
17642 else if (bits == 32)
17643 type = builtin_type (arch)->builtin_char32;
17646 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17648 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17650 return set_die_type (die, type, cu);
17655 complaint (_("unsupported DW_AT_encoding: '%s'"),
17656 dwarf_type_encoding_name (encoding));
17657 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17661 if (name && strcmp (name, "char") == 0)
17662 TYPE_NOSIGN (type) = 1;
17664 maybe_set_alignment (cu, die, type);
17666 return set_die_type (die, type, cu);
17669 /* Parse dwarf attribute if it's a block, reference or constant and put the
17670 resulting value of the attribute into struct bound_prop.
17671 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17674 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17675 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17677 struct dwarf2_property_baton *baton;
17678 struct obstack *obstack
17679 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17681 if (attr == NULL || prop == NULL)
17684 if (attr_form_is_block (attr))
17686 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17687 baton->referenced_type = NULL;
17688 baton->locexpr.per_cu = cu->per_cu;
17689 baton->locexpr.size = DW_BLOCK (attr)->size;
17690 baton->locexpr.data = DW_BLOCK (attr)->data;
17691 prop->data.baton = baton;
17692 prop->kind = PROP_LOCEXPR;
17693 gdb_assert (prop->data.baton != NULL);
17695 else if (attr_form_is_ref (attr))
17697 struct dwarf2_cu *target_cu = cu;
17698 struct die_info *target_die;
17699 struct attribute *target_attr;
17701 target_die = follow_die_ref (die, attr, &target_cu);
17702 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17703 if (target_attr == NULL)
17704 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17706 if (target_attr == NULL)
17709 switch (target_attr->name)
17711 case DW_AT_location:
17712 if (attr_form_is_section_offset (target_attr))
17714 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17715 baton->referenced_type = die_type (target_die, target_cu);
17716 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17717 prop->data.baton = baton;
17718 prop->kind = PROP_LOCLIST;
17719 gdb_assert (prop->data.baton != NULL);
17721 else if (attr_form_is_block (target_attr))
17723 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17724 baton->referenced_type = die_type (target_die, target_cu);
17725 baton->locexpr.per_cu = cu->per_cu;
17726 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17727 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17728 prop->data.baton = baton;
17729 prop->kind = PROP_LOCEXPR;
17730 gdb_assert (prop->data.baton != NULL);
17734 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17735 "dynamic property");
17739 case DW_AT_data_member_location:
17743 if (!handle_data_member_location (target_die, target_cu,
17747 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17748 baton->referenced_type = read_type_die (target_die->parent,
17750 baton->offset_info.offset = offset;
17751 baton->offset_info.type = die_type (target_die, target_cu);
17752 prop->data.baton = baton;
17753 prop->kind = PROP_ADDR_OFFSET;
17758 else if (attr_form_is_constant (attr))
17760 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17761 prop->kind = PROP_CONST;
17765 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17766 dwarf2_name (die, cu));
17773 /* Read the given DW_AT_subrange DIE. */
17775 static struct type *
17776 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17778 struct type *base_type, *orig_base_type;
17779 struct type *range_type;
17780 struct attribute *attr;
17781 struct dynamic_prop low, high;
17782 int low_default_is_valid;
17783 int high_bound_is_count = 0;
17785 ULONGEST negative_mask;
17787 orig_base_type = die_type (die, cu);
17788 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17789 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17790 creating the range type, but we use the result of check_typedef
17791 when examining properties of the type. */
17792 base_type = check_typedef (orig_base_type);
17794 /* The die_type call above may have already set the type for this DIE. */
17795 range_type = get_die_type (die, cu);
17799 low.kind = PROP_CONST;
17800 high.kind = PROP_CONST;
17801 high.data.const_val = 0;
17803 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17804 omitting DW_AT_lower_bound. */
17805 switch (cu->language)
17808 case language_cplus:
17809 low.data.const_val = 0;
17810 low_default_is_valid = 1;
17812 case language_fortran:
17813 low.data.const_val = 1;
17814 low_default_is_valid = 1;
17817 case language_objc:
17818 case language_rust:
17819 low.data.const_val = 0;
17820 low_default_is_valid = (cu->header.version >= 4);
17824 case language_pascal:
17825 low.data.const_val = 1;
17826 low_default_is_valid = (cu->header.version >= 4);
17829 low.data.const_val = 0;
17830 low_default_is_valid = 0;
17834 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17836 attr_to_dynamic_prop (attr, die, cu, &low);
17837 else if (!low_default_is_valid)
17838 complaint (_("Missing DW_AT_lower_bound "
17839 "- DIE at %s [in module %s]"),
17840 sect_offset_str (die->sect_off),
17841 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17843 struct attribute *attr_ub, *attr_count;
17844 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17845 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17847 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17848 if (attr_to_dynamic_prop (attr, die, cu, &high))
17850 /* If bounds are constant do the final calculation here. */
17851 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17852 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17854 high_bound_is_count = 1;
17858 if (attr_ub != NULL)
17859 complaint (_("Unresolved DW_AT_upper_bound "
17860 "- DIE at %s [in module %s]"),
17861 sect_offset_str (die->sect_off),
17862 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17863 if (attr_count != NULL)
17864 complaint (_("Unresolved DW_AT_count "
17865 "- DIE at %s [in module %s]"),
17866 sect_offset_str (die->sect_off),
17867 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17872 /* Dwarf-2 specifications explicitly allows to create subrange types
17873 without specifying a base type.
17874 In that case, the base type must be set to the type of
17875 the lower bound, upper bound or count, in that order, if any of these
17876 three attributes references an object that has a type.
17877 If no base type is found, the Dwarf-2 specifications say that
17878 a signed integer type of size equal to the size of an address should
17880 For the following C code: `extern char gdb_int [];'
17881 GCC produces an empty range DIE.
17882 FIXME: muller/2010-05-28: Possible references to object for low bound,
17883 high bound or count are not yet handled by this code. */
17884 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17887 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17888 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17889 struct type *int_type = objfile_type (objfile)->builtin_int;
17891 /* Test "int", "long int", and "long long int" objfile types,
17892 and select the first one having a size above or equal to the
17893 architecture address size. */
17894 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17895 base_type = int_type;
17898 int_type = objfile_type (objfile)->builtin_long;
17899 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17900 base_type = int_type;
17903 int_type = objfile_type (objfile)->builtin_long_long;
17904 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17905 base_type = int_type;
17910 /* Normally, the DWARF producers are expected to use a signed
17911 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17912 But this is unfortunately not always the case, as witnessed
17913 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17914 is used instead. To work around that ambiguity, we treat
17915 the bounds as signed, and thus sign-extend their values, when
17916 the base type is signed. */
17918 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17919 if (low.kind == PROP_CONST
17920 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17921 low.data.const_val |= negative_mask;
17922 if (high.kind == PROP_CONST
17923 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17924 high.data.const_val |= negative_mask;
17926 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17928 if (high_bound_is_count)
17929 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17931 /* Ada expects an empty array on no boundary attributes. */
17932 if (attr == NULL && cu->language != language_ada)
17933 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17935 name = dwarf2_name (die, cu);
17937 TYPE_NAME (range_type) = name;
17939 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17941 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17943 maybe_set_alignment (cu, die, range_type);
17945 set_die_type (die, range_type, cu);
17947 /* set_die_type should be already done. */
17948 set_descriptive_type (range_type, die, cu);
17953 static struct type *
17954 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17958 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17960 TYPE_NAME (type) = dwarf2_name (die, cu);
17962 /* In Ada, an unspecified type is typically used when the description
17963 of the type is defered to a different unit. When encountering
17964 such a type, we treat it as a stub, and try to resolve it later on,
17966 if (cu->language == language_ada)
17967 TYPE_STUB (type) = 1;
17969 return set_die_type (die, type, cu);
17972 /* Read a single die and all its descendents. Set the die's sibling
17973 field to NULL; set other fields in the die correctly, and set all
17974 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17975 location of the info_ptr after reading all of those dies. PARENT
17976 is the parent of the die in question. */
17978 static struct die_info *
17979 read_die_and_children (const struct die_reader_specs *reader,
17980 const gdb_byte *info_ptr,
17981 const gdb_byte **new_info_ptr,
17982 struct die_info *parent)
17984 struct die_info *die;
17985 const gdb_byte *cur_ptr;
17988 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17991 *new_info_ptr = cur_ptr;
17994 store_in_ref_table (die, reader->cu);
17997 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18001 *new_info_ptr = cur_ptr;
18004 die->sibling = NULL;
18005 die->parent = parent;
18009 /* Read a die, all of its descendents, and all of its siblings; set
18010 all of the fields of all of the dies correctly. Arguments are as
18011 in read_die_and_children. */
18013 static struct die_info *
18014 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18015 const gdb_byte *info_ptr,
18016 const gdb_byte **new_info_ptr,
18017 struct die_info *parent)
18019 struct die_info *first_die, *last_sibling;
18020 const gdb_byte *cur_ptr;
18022 cur_ptr = info_ptr;
18023 first_die = last_sibling = NULL;
18027 struct die_info *die
18028 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18032 *new_info_ptr = cur_ptr;
18039 last_sibling->sibling = die;
18041 last_sibling = die;
18045 /* Read a die, all of its descendents, and all of its siblings; set
18046 all of the fields of all of the dies correctly. Arguments are as
18047 in read_die_and_children.
18048 This the main entry point for reading a DIE and all its children. */
18050 static struct die_info *
18051 read_die_and_siblings (const struct die_reader_specs *reader,
18052 const gdb_byte *info_ptr,
18053 const gdb_byte **new_info_ptr,
18054 struct die_info *parent)
18056 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18057 new_info_ptr, parent);
18059 if (dwarf_die_debug)
18061 fprintf_unfiltered (gdb_stdlog,
18062 "Read die from %s@0x%x of %s:\n",
18063 get_section_name (reader->die_section),
18064 (unsigned) (info_ptr - reader->die_section->buffer),
18065 bfd_get_filename (reader->abfd));
18066 dump_die (die, dwarf_die_debug);
18072 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18074 The caller is responsible for filling in the extra attributes
18075 and updating (*DIEP)->num_attrs.
18076 Set DIEP to point to a newly allocated die with its information,
18077 except for its child, sibling, and parent fields.
18078 Set HAS_CHILDREN to tell whether the die has children or not. */
18080 static const gdb_byte *
18081 read_full_die_1 (const struct die_reader_specs *reader,
18082 struct die_info **diep, const gdb_byte *info_ptr,
18083 int *has_children, int num_extra_attrs)
18085 unsigned int abbrev_number, bytes_read, i;
18086 struct abbrev_info *abbrev;
18087 struct die_info *die;
18088 struct dwarf2_cu *cu = reader->cu;
18089 bfd *abfd = reader->abfd;
18091 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18092 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18093 info_ptr += bytes_read;
18094 if (!abbrev_number)
18101 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18103 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18105 bfd_get_filename (abfd));
18107 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18108 die->sect_off = sect_off;
18109 die->tag = abbrev->tag;
18110 die->abbrev = abbrev_number;
18112 /* Make the result usable.
18113 The caller needs to update num_attrs after adding the extra
18115 die->num_attrs = abbrev->num_attrs;
18117 for (i = 0; i < abbrev->num_attrs; ++i)
18118 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18122 *has_children = abbrev->has_children;
18126 /* Read a die and all its attributes.
18127 Set DIEP to point to a newly allocated die with its information,
18128 except for its child, sibling, and parent fields.
18129 Set HAS_CHILDREN to tell whether the die has children or not. */
18131 static const gdb_byte *
18132 read_full_die (const struct die_reader_specs *reader,
18133 struct die_info **diep, const gdb_byte *info_ptr,
18136 const gdb_byte *result;
18138 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18140 if (dwarf_die_debug)
18142 fprintf_unfiltered (gdb_stdlog,
18143 "Read die from %s@0x%x of %s:\n",
18144 get_section_name (reader->die_section),
18145 (unsigned) (info_ptr - reader->die_section->buffer),
18146 bfd_get_filename (reader->abfd));
18147 dump_die (*diep, dwarf_die_debug);
18153 /* Abbreviation tables.
18155 In DWARF version 2, the description of the debugging information is
18156 stored in a separate .debug_abbrev section. Before we read any
18157 dies from a section we read in all abbreviations and install them
18158 in a hash table. */
18160 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18162 struct abbrev_info *
18163 abbrev_table::alloc_abbrev ()
18165 struct abbrev_info *abbrev;
18167 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18168 memset (abbrev, 0, sizeof (struct abbrev_info));
18173 /* Add an abbreviation to the table. */
18176 abbrev_table::add_abbrev (unsigned int abbrev_number,
18177 struct abbrev_info *abbrev)
18179 unsigned int hash_number;
18181 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18182 abbrev->next = m_abbrevs[hash_number];
18183 m_abbrevs[hash_number] = abbrev;
18186 /* Look up an abbrev in the table.
18187 Returns NULL if the abbrev is not found. */
18189 struct abbrev_info *
18190 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18192 unsigned int hash_number;
18193 struct abbrev_info *abbrev;
18195 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18196 abbrev = m_abbrevs[hash_number];
18200 if (abbrev->number == abbrev_number)
18202 abbrev = abbrev->next;
18207 /* Read in an abbrev table. */
18209 static abbrev_table_up
18210 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18211 struct dwarf2_section_info *section,
18212 sect_offset sect_off)
18214 struct objfile *objfile = dwarf2_per_objfile->objfile;
18215 bfd *abfd = get_section_bfd_owner (section);
18216 const gdb_byte *abbrev_ptr;
18217 struct abbrev_info *cur_abbrev;
18218 unsigned int abbrev_number, bytes_read, abbrev_name;
18219 unsigned int abbrev_form;
18220 struct attr_abbrev *cur_attrs;
18221 unsigned int allocated_attrs;
18223 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18225 dwarf2_read_section (objfile, section);
18226 abbrev_ptr = section->buffer + to_underlying (sect_off);
18227 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18228 abbrev_ptr += bytes_read;
18230 allocated_attrs = ATTR_ALLOC_CHUNK;
18231 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18233 /* Loop until we reach an abbrev number of 0. */
18234 while (abbrev_number)
18236 cur_abbrev = abbrev_table->alloc_abbrev ();
18238 /* read in abbrev header */
18239 cur_abbrev->number = abbrev_number;
18241 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18242 abbrev_ptr += bytes_read;
18243 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18246 /* now read in declarations */
18249 LONGEST implicit_const;
18251 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18252 abbrev_ptr += bytes_read;
18253 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18254 abbrev_ptr += bytes_read;
18255 if (abbrev_form == DW_FORM_implicit_const)
18257 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18259 abbrev_ptr += bytes_read;
18263 /* Initialize it due to a false compiler warning. */
18264 implicit_const = -1;
18267 if (abbrev_name == 0)
18270 if (cur_abbrev->num_attrs == allocated_attrs)
18272 allocated_attrs += ATTR_ALLOC_CHUNK;
18274 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18277 cur_attrs[cur_abbrev->num_attrs].name
18278 = (enum dwarf_attribute) abbrev_name;
18279 cur_attrs[cur_abbrev->num_attrs].form
18280 = (enum dwarf_form) abbrev_form;
18281 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18282 ++cur_abbrev->num_attrs;
18285 cur_abbrev->attrs =
18286 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18287 cur_abbrev->num_attrs);
18288 memcpy (cur_abbrev->attrs, cur_attrs,
18289 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18291 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18293 /* Get next abbreviation.
18294 Under Irix6 the abbreviations for a compilation unit are not
18295 always properly terminated with an abbrev number of 0.
18296 Exit loop if we encounter an abbreviation which we have
18297 already read (which means we are about to read the abbreviations
18298 for the next compile unit) or if the end of the abbreviation
18299 table is reached. */
18300 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18302 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18303 abbrev_ptr += bytes_read;
18304 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18309 return abbrev_table;
18312 /* Returns nonzero if TAG represents a type that we might generate a partial
18316 is_type_tag_for_partial (int tag)
18321 /* Some types that would be reasonable to generate partial symbols for,
18322 that we don't at present. */
18323 case DW_TAG_array_type:
18324 case DW_TAG_file_type:
18325 case DW_TAG_ptr_to_member_type:
18326 case DW_TAG_set_type:
18327 case DW_TAG_string_type:
18328 case DW_TAG_subroutine_type:
18330 case DW_TAG_base_type:
18331 case DW_TAG_class_type:
18332 case DW_TAG_interface_type:
18333 case DW_TAG_enumeration_type:
18334 case DW_TAG_structure_type:
18335 case DW_TAG_subrange_type:
18336 case DW_TAG_typedef:
18337 case DW_TAG_union_type:
18344 /* Load all DIEs that are interesting for partial symbols into memory. */
18346 static struct partial_die_info *
18347 load_partial_dies (const struct die_reader_specs *reader,
18348 const gdb_byte *info_ptr, int building_psymtab)
18350 struct dwarf2_cu *cu = reader->cu;
18351 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18352 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18353 unsigned int bytes_read;
18354 unsigned int load_all = 0;
18355 int nesting_level = 1;
18360 gdb_assert (cu->per_cu != NULL);
18361 if (cu->per_cu->load_all_dies)
18365 = htab_create_alloc_ex (cu->header.length / 12,
18369 &cu->comp_unit_obstack,
18370 hashtab_obstack_allocate,
18371 dummy_obstack_deallocate);
18375 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18377 /* A NULL abbrev means the end of a series of children. */
18378 if (abbrev == NULL)
18380 if (--nesting_level == 0)
18383 info_ptr += bytes_read;
18384 last_die = parent_die;
18385 parent_die = parent_die->die_parent;
18389 /* Check for template arguments. We never save these; if
18390 they're seen, we just mark the parent, and go on our way. */
18391 if (parent_die != NULL
18392 && cu->language == language_cplus
18393 && (abbrev->tag == DW_TAG_template_type_param
18394 || abbrev->tag == DW_TAG_template_value_param))
18396 parent_die->has_template_arguments = 1;
18400 /* We don't need a partial DIE for the template argument. */
18401 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18406 /* We only recurse into c++ subprograms looking for template arguments.
18407 Skip their other children. */
18409 && cu->language == language_cplus
18410 && parent_die != NULL
18411 && parent_die->tag == DW_TAG_subprogram)
18413 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18417 /* Check whether this DIE is interesting enough to save. Normally
18418 we would not be interested in members here, but there may be
18419 later variables referencing them via DW_AT_specification (for
18420 static members). */
18422 && !is_type_tag_for_partial (abbrev->tag)
18423 && abbrev->tag != DW_TAG_constant
18424 && abbrev->tag != DW_TAG_enumerator
18425 && abbrev->tag != DW_TAG_subprogram
18426 && abbrev->tag != DW_TAG_inlined_subroutine
18427 && abbrev->tag != DW_TAG_lexical_block
18428 && abbrev->tag != DW_TAG_variable
18429 && abbrev->tag != DW_TAG_namespace
18430 && abbrev->tag != DW_TAG_module
18431 && abbrev->tag != DW_TAG_member
18432 && abbrev->tag != DW_TAG_imported_unit
18433 && abbrev->tag != DW_TAG_imported_declaration)
18435 /* Otherwise we skip to the next sibling, if any. */
18436 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18440 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18443 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18445 /* This two-pass algorithm for processing partial symbols has a
18446 high cost in cache pressure. Thus, handle some simple cases
18447 here which cover the majority of C partial symbols. DIEs
18448 which neither have specification tags in them, nor could have
18449 specification tags elsewhere pointing at them, can simply be
18450 processed and discarded.
18452 This segment is also optional; scan_partial_symbols and
18453 add_partial_symbol will handle these DIEs if we chain
18454 them in normally. When compilers which do not emit large
18455 quantities of duplicate debug information are more common,
18456 this code can probably be removed. */
18458 /* Any complete simple types at the top level (pretty much all
18459 of them, for a language without namespaces), can be processed
18461 if (parent_die == NULL
18462 && pdi.has_specification == 0
18463 && pdi.is_declaration == 0
18464 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18465 || pdi.tag == DW_TAG_base_type
18466 || pdi.tag == DW_TAG_subrange_type))
18468 if (building_psymtab && pdi.name != NULL)
18469 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18470 VAR_DOMAIN, LOC_TYPEDEF, -1,
18471 psymbol_placement::STATIC,
18472 0, cu->language, objfile);
18473 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18477 /* The exception for DW_TAG_typedef with has_children above is
18478 a workaround of GCC PR debug/47510. In the case of this complaint
18479 type_name_or_error will error on such types later.
18481 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18482 it could not find the child DIEs referenced later, this is checked
18483 above. In correct DWARF DW_TAG_typedef should have no children. */
18485 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18486 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18487 "- DIE at %s [in module %s]"),
18488 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18490 /* If we're at the second level, and we're an enumerator, and
18491 our parent has no specification (meaning possibly lives in a
18492 namespace elsewhere), then we can add the partial symbol now
18493 instead of queueing it. */
18494 if (pdi.tag == DW_TAG_enumerator
18495 && parent_die != NULL
18496 && parent_die->die_parent == NULL
18497 && parent_die->tag == DW_TAG_enumeration_type
18498 && parent_die->has_specification == 0)
18500 if (pdi.name == NULL)
18501 complaint (_("malformed enumerator DIE ignored"));
18502 else if (building_psymtab)
18503 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18504 VAR_DOMAIN, LOC_CONST, -1,
18505 cu->language == language_cplus
18506 ? psymbol_placement::GLOBAL
18507 : psymbol_placement::STATIC,
18508 0, cu->language, objfile);
18510 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18514 struct partial_die_info *part_die
18515 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18517 /* We'll save this DIE so link it in. */
18518 part_die->die_parent = parent_die;
18519 part_die->die_sibling = NULL;
18520 part_die->die_child = NULL;
18522 if (last_die && last_die == parent_die)
18523 last_die->die_child = part_die;
18525 last_die->die_sibling = part_die;
18527 last_die = part_die;
18529 if (first_die == NULL)
18530 first_die = part_die;
18532 /* Maybe add the DIE to the hash table. Not all DIEs that we
18533 find interesting need to be in the hash table, because we
18534 also have the parent/sibling/child chains; only those that we
18535 might refer to by offset later during partial symbol reading.
18537 For now this means things that might have be the target of a
18538 DW_AT_specification, DW_AT_abstract_origin, or
18539 DW_AT_extension. DW_AT_extension will refer only to
18540 namespaces; DW_AT_abstract_origin refers to functions (and
18541 many things under the function DIE, but we do not recurse
18542 into function DIEs during partial symbol reading) and
18543 possibly variables as well; DW_AT_specification refers to
18544 declarations. Declarations ought to have the DW_AT_declaration
18545 flag. It happens that GCC forgets to put it in sometimes, but
18546 only for functions, not for types.
18548 Adding more things than necessary to the hash table is harmless
18549 except for the performance cost. Adding too few will result in
18550 wasted time in find_partial_die, when we reread the compilation
18551 unit with load_all_dies set. */
18554 || abbrev->tag == DW_TAG_constant
18555 || abbrev->tag == DW_TAG_subprogram
18556 || abbrev->tag == DW_TAG_variable
18557 || abbrev->tag == DW_TAG_namespace
18558 || part_die->is_declaration)
18562 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18563 to_underlying (part_die->sect_off),
18568 /* For some DIEs we want to follow their children (if any). For C
18569 we have no reason to follow the children of structures; for other
18570 languages we have to, so that we can get at method physnames
18571 to infer fully qualified class names, for DW_AT_specification,
18572 and for C++ template arguments. For C++, we also look one level
18573 inside functions to find template arguments (if the name of the
18574 function does not already contain the template arguments).
18576 For Ada, we need to scan the children of subprograms and lexical
18577 blocks as well because Ada allows the definition of nested
18578 entities that could be interesting for the debugger, such as
18579 nested subprograms for instance. */
18580 if (last_die->has_children
18582 || last_die->tag == DW_TAG_namespace
18583 || last_die->tag == DW_TAG_module
18584 || last_die->tag == DW_TAG_enumeration_type
18585 || (cu->language == language_cplus
18586 && last_die->tag == DW_TAG_subprogram
18587 && (last_die->name == NULL
18588 || strchr (last_die->name, '<') == NULL))
18589 || (cu->language != language_c
18590 && (last_die->tag == DW_TAG_class_type
18591 || last_die->tag == DW_TAG_interface_type
18592 || last_die->tag == DW_TAG_structure_type
18593 || last_die->tag == DW_TAG_union_type))
18594 || (cu->language == language_ada
18595 && (last_die->tag == DW_TAG_subprogram
18596 || last_die->tag == DW_TAG_lexical_block))))
18599 parent_die = last_die;
18603 /* Otherwise we skip to the next sibling, if any. */
18604 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18606 /* Back to the top, do it again. */
18610 partial_die_info::partial_die_info (sect_offset sect_off_,
18611 struct abbrev_info *abbrev)
18612 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18616 /* Read a minimal amount of information into the minimal die structure.
18617 INFO_PTR should point just after the initial uleb128 of a DIE. */
18620 partial_die_info::read (const struct die_reader_specs *reader,
18621 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18623 struct dwarf2_cu *cu = reader->cu;
18624 struct dwarf2_per_objfile *dwarf2_per_objfile
18625 = cu->per_cu->dwarf2_per_objfile;
18627 int has_low_pc_attr = 0;
18628 int has_high_pc_attr = 0;
18629 int high_pc_relative = 0;
18631 for (i = 0; i < abbrev.num_attrs; ++i)
18633 struct attribute attr;
18635 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18637 /* Store the data if it is of an attribute we want to keep in a
18638 partial symbol table. */
18644 case DW_TAG_compile_unit:
18645 case DW_TAG_partial_unit:
18646 case DW_TAG_type_unit:
18647 /* Compilation units have a DW_AT_name that is a filename, not
18648 a source language identifier. */
18649 case DW_TAG_enumeration_type:
18650 case DW_TAG_enumerator:
18651 /* These tags always have simple identifiers already; no need
18652 to canonicalize them. */
18653 name = DW_STRING (&attr);
18657 struct objfile *objfile = dwarf2_per_objfile->objfile;
18660 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18661 &objfile->per_bfd->storage_obstack);
18666 case DW_AT_linkage_name:
18667 case DW_AT_MIPS_linkage_name:
18668 /* Note that both forms of linkage name might appear. We
18669 assume they will be the same, and we only store the last
18671 if (cu->language == language_ada)
18672 name = DW_STRING (&attr);
18673 linkage_name = DW_STRING (&attr);
18676 has_low_pc_attr = 1;
18677 lowpc = attr_value_as_address (&attr);
18679 case DW_AT_high_pc:
18680 has_high_pc_attr = 1;
18681 highpc = attr_value_as_address (&attr);
18682 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18683 high_pc_relative = 1;
18685 case DW_AT_location:
18686 /* Support the .debug_loc offsets. */
18687 if (attr_form_is_block (&attr))
18689 d.locdesc = DW_BLOCK (&attr);
18691 else if (attr_form_is_section_offset (&attr))
18693 dwarf2_complex_location_expr_complaint ();
18697 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18698 "partial symbol information");
18701 case DW_AT_external:
18702 is_external = DW_UNSND (&attr);
18704 case DW_AT_declaration:
18705 is_declaration = DW_UNSND (&attr);
18710 case DW_AT_abstract_origin:
18711 case DW_AT_specification:
18712 case DW_AT_extension:
18713 has_specification = 1;
18714 spec_offset = dwarf2_get_ref_die_offset (&attr);
18715 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18716 || cu->per_cu->is_dwz);
18718 case DW_AT_sibling:
18719 /* Ignore absolute siblings, they might point outside of
18720 the current compile unit. */
18721 if (attr.form == DW_FORM_ref_addr)
18722 complaint (_("ignoring absolute DW_AT_sibling"));
18725 const gdb_byte *buffer = reader->buffer;
18726 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18727 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18729 if (sibling_ptr < info_ptr)
18730 complaint (_("DW_AT_sibling points backwards"));
18731 else if (sibling_ptr > reader->buffer_end)
18732 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18734 sibling = sibling_ptr;
18737 case DW_AT_byte_size:
18740 case DW_AT_const_value:
18741 has_const_value = 1;
18743 case DW_AT_calling_convention:
18744 /* DWARF doesn't provide a way to identify a program's source-level
18745 entry point. DW_AT_calling_convention attributes are only meant
18746 to describe functions' calling conventions.
18748 However, because it's a necessary piece of information in
18749 Fortran, and before DWARF 4 DW_CC_program was the only
18750 piece of debugging information whose definition refers to
18751 a 'main program' at all, several compilers marked Fortran
18752 main programs with DW_CC_program --- even when those
18753 functions use the standard calling conventions.
18755 Although DWARF now specifies a way to provide this
18756 information, we support this practice for backward
18758 if (DW_UNSND (&attr) == DW_CC_program
18759 && cu->language == language_fortran)
18760 main_subprogram = 1;
18763 if (DW_UNSND (&attr) == DW_INL_inlined
18764 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18765 may_be_inlined = 1;
18769 if (tag == DW_TAG_imported_unit)
18771 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18772 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18773 || cu->per_cu->is_dwz);
18777 case DW_AT_main_subprogram:
18778 main_subprogram = DW_UNSND (&attr);
18783 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18784 but that requires a full DIE, so instead we just
18786 int need_ranges_base = tag != DW_TAG_compile_unit;
18787 unsigned int ranges_offset = (DW_UNSND (&attr)
18788 + (need_ranges_base
18792 /* Value of the DW_AT_ranges attribute is the offset in the
18793 .debug_ranges section. */
18794 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18805 if (high_pc_relative)
18808 if (has_low_pc_attr && has_high_pc_attr)
18810 /* When using the GNU linker, .gnu.linkonce. sections are used to
18811 eliminate duplicate copies of functions and vtables and such.
18812 The linker will arbitrarily choose one and discard the others.
18813 The AT_*_pc values for such functions refer to local labels in
18814 these sections. If the section from that file was discarded, the
18815 labels are not in the output, so the relocs get a value of 0.
18816 If this is a discarded function, mark the pc bounds as invalid,
18817 so that GDB will ignore it. */
18818 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18820 struct objfile *objfile = dwarf2_per_objfile->objfile;
18821 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18823 complaint (_("DW_AT_low_pc %s is zero "
18824 "for DIE at %s [in module %s]"),
18825 paddress (gdbarch, lowpc),
18826 sect_offset_str (sect_off),
18827 objfile_name (objfile));
18829 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18830 else if (lowpc >= highpc)
18832 struct objfile *objfile = dwarf2_per_objfile->objfile;
18833 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18835 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18836 "for DIE at %s [in module %s]"),
18837 paddress (gdbarch, lowpc),
18838 paddress (gdbarch, highpc),
18839 sect_offset_str (sect_off),
18840 objfile_name (objfile));
18849 /* Find a cached partial DIE at OFFSET in CU. */
18851 struct partial_die_info *
18852 dwarf2_cu::find_partial_die (sect_offset sect_off)
18854 struct partial_die_info *lookup_die = NULL;
18855 struct partial_die_info part_die (sect_off);
18857 lookup_die = ((struct partial_die_info *)
18858 htab_find_with_hash (partial_dies, &part_die,
18859 to_underlying (sect_off)));
18864 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18865 except in the case of .debug_types DIEs which do not reference
18866 outside their CU (they do however referencing other types via
18867 DW_FORM_ref_sig8). */
18869 static const struct cu_partial_die_info
18870 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18872 struct dwarf2_per_objfile *dwarf2_per_objfile
18873 = cu->per_cu->dwarf2_per_objfile;
18874 struct objfile *objfile = dwarf2_per_objfile->objfile;
18875 struct dwarf2_per_cu_data *per_cu = NULL;
18876 struct partial_die_info *pd = NULL;
18878 if (offset_in_dwz == cu->per_cu->is_dwz
18879 && offset_in_cu_p (&cu->header, sect_off))
18881 pd = cu->find_partial_die (sect_off);
18884 /* We missed recording what we needed.
18885 Load all dies and try again. */
18886 per_cu = cu->per_cu;
18890 /* TUs don't reference other CUs/TUs (except via type signatures). */
18891 if (cu->per_cu->is_debug_types)
18893 error (_("Dwarf Error: Type Unit at offset %s contains"
18894 " external reference to offset %s [in module %s].\n"),
18895 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18896 bfd_get_filename (objfile->obfd));
18898 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18899 dwarf2_per_objfile);
18901 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18902 load_partial_comp_unit (per_cu);
18904 per_cu->cu->last_used = 0;
18905 pd = per_cu->cu->find_partial_die (sect_off);
18908 /* If we didn't find it, and not all dies have been loaded,
18909 load them all and try again. */
18911 if (pd == NULL && per_cu->load_all_dies == 0)
18913 per_cu->load_all_dies = 1;
18915 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18916 THIS_CU->cu may already be in use. So we can't just free it and
18917 replace its DIEs with the ones we read in. Instead, we leave those
18918 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18919 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18921 load_partial_comp_unit (per_cu);
18923 pd = per_cu->cu->find_partial_die (sect_off);
18927 internal_error (__FILE__, __LINE__,
18928 _("could not find partial DIE %s "
18929 "in cache [from module %s]\n"),
18930 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18931 return { per_cu->cu, pd };
18934 /* See if we can figure out if the class lives in a namespace. We do
18935 this by looking for a member function; its demangled name will
18936 contain namespace info, if there is any. */
18939 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18940 struct dwarf2_cu *cu)
18942 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18943 what template types look like, because the demangler
18944 frequently doesn't give the same name as the debug info. We
18945 could fix this by only using the demangled name to get the
18946 prefix (but see comment in read_structure_type). */
18948 struct partial_die_info *real_pdi;
18949 struct partial_die_info *child_pdi;
18951 /* If this DIE (this DIE's specification, if any) has a parent, then
18952 we should not do this. We'll prepend the parent's fully qualified
18953 name when we create the partial symbol. */
18955 real_pdi = struct_pdi;
18956 while (real_pdi->has_specification)
18958 auto res = find_partial_die (real_pdi->spec_offset,
18959 real_pdi->spec_is_dwz, cu);
18960 real_pdi = res.pdi;
18964 if (real_pdi->die_parent != NULL)
18967 for (child_pdi = struct_pdi->die_child;
18969 child_pdi = child_pdi->die_sibling)
18971 if (child_pdi->tag == DW_TAG_subprogram
18972 && child_pdi->linkage_name != NULL)
18974 char *actual_class_name
18975 = language_class_name_from_physname (cu->language_defn,
18976 child_pdi->linkage_name);
18977 if (actual_class_name != NULL)
18979 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18982 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18984 strlen (actual_class_name)));
18985 xfree (actual_class_name);
18993 partial_die_info::fixup (struct dwarf2_cu *cu)
18995 /* Once we've fixed up a die, there's no point in doing so again.
18996 This also avoids a memory leak if we were to call
18997 guess_partial_die_structure_name multiple times. */
19001 /* If we found a reference attribute and the DIE has no name, try
19002 to find a name in the referred to DIE. */
19004 if (name == NULL && has_specification)
19006 struct partial_die_info *spec_die;
19008 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19009 spec_die = res.pdi;
19012 spec_die->fixup (cu);
19014 if (spec_die->name)
19016 name = spec_die->name;
19018 /* Copy DW_AT_external attribute if it is set. */
19019 if (spec_die->is_external)
19020 is_external = spec_die->is_external;
19024 /* Set default names for some unnamed DIEs. */
19026 if (name == NULL && tag == DW_TAG_namespace)
19027 name = CP_ANONYMOUS_NAMESPACE_STR;
19029 /* If there is no parent die to provide a namespace, and there are
19030 children, see if we can determine the namespace from their linkage
19032 if (cu->language == language_cplus
19033 && !VEC_empty (dwarf2_section_info_def,
19034 cu->per_cu->dwarf2_per_objfile->types)
19035 && die_parent == NULL
19037 && (tag == DW_TAG_class_type
19038 || tag == DW_TAG_structure_type
19039 || tag == DW_TAG_union_type))
19040 guess_partial_die_structure_name (this, cu);
19042 /* GCC might emit a nameless struct or union that has a linkage
19043 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19045 && (tag == DW_TAG_class_type
19046 || tag == DW_TAG_interface_type
19047 || tag == DW_TAG_structure_type
19048 || tag == DW_TAG_union_type)
19049 && linkage_name != NULL)
19053 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19058 /* Strip any leading namespaces/classes, keep only the base name.
19059 DW_AT_name for named DIEs does not contain the prefixes. */
19060 base = strrchr (demangled, ':');
19061 if (base && base > demangled && base[-1] == ':')
19066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19069 obstack_copy0 (&objfile->per_bfd->storage_obstack,
19070 base, strlen (base)));
19078 /* Read an attribute value described by an attribute form. */
19080 static const gdb_byte *
19081 read_attribute_value (const struct die_reader_specs *reader,
19082 struct attribute *attr, unsigned form,
19083 LONGEST implicit_const, const gdb_byte *info_ptr)
19085 struct dwarf2_cu *cu = reader->cu;
19086 struct dwarf2_per_objfile *dwarf2_per_objfile
19087 = cu->per_cu->dwarf2_per_objfile;
19088 struct objfile *objfile = dwarf2_per_objfile->objfile;
19089 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19090 bfd *abfd = reader->abfd;
19091 struct comp_unit_head *cu_header = &cu->header;
19092 unsigned int bytes_read;
19093 struct dwarf_block *blk;
19095 attr->form = (enum dwarf_form) form;
19098 case DW_FORM_ref_addr:
19099 if (cu->header.version == 2)
19100 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19102 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19103 &cu->header, &bytes_read);
19104 info_ptr += bytes_read;
19106 case DW_FORM_GNU_ref_alt:
19107 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19108 info_ptr += bytes_read;
19111 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19112 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19113 info_ptr += bytes_read;
19115 case DW_FORM_block2:
19116 blk = dwarf_alloc_block (cu);
19117 blk->size = read_2_bytes (abfd, info_ptr);
19119 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19120 info_ptr += blk->size;
19121 DW_BLOCK (attr) = blk;
19123 case DW_FORM_block4:
19124 blk = dwarf_alloc_block (cu);
19125 blk->size = read_4_bytes (abfd, info_ptr);
19127 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19128 info_ptr += blk->size;
19129 DW_BLOCK (attr) = blk;
19131 case DW_FORM_data2:
19132 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19135 case DW_FORM_data4:
19136 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19139 case DW_FORM_data8:
19140 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19143 case DW_FORM_data16:
19144 blk = dwarf_alloc_block (cu);
19146 blk->data = read_n_bytes (abfd, info_ptr, 16);
19148 DW_BLOCK (attr) = blk;
19150 case DW_FORM_sec_offset:
19151 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19152 info_ptr += bytes_read;
19154 case DW_FORM_string:
19155 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19156 DW_STRING_IS_CANONICAL (attr) = 0;
19157 info_ptr += bytes_read;
19160 if (!cu->per_cu->is_dwz)
19162 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19163 abfd, info_ptr, cu_header,
19165 DW_STRING_IS_CANONICAL (attr) = 0;
19166 info_ptr += bytes_read;
19170 case DW_FORM_line_strp:
19171 if (!cu->per_cu->is_dwz)
19173 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19175 cu_header, &bytes_read);
19176 DW_STRING_IS_CANONICAL (attr) = 0;
19177 info_ptr += bytes_read;
19181 case DW_FORM_GNU_strp_alt:
19183 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19184 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19187 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19189 DW_STRING_IS_CANONICAL (attr) = 0;
19190 info_ptr += bytes_read;
19193 case DW_FORM_exprloc:
19194 case DW_FORM_block:
19195 blk = dwarf_alloc_block (cu);
19196 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19197 info_ptr += bytes_read;
19198 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19199 info_ptr += blk->size;
19200 DW_BLOCK (attr) = blk;
19202 case DW_FORM_block1:
19203 blk = dwarf_alloc_block (cu);
19204 blk->size = read_1_byte (abfd, info_ptr);
19206 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19207 info_ptr += blk->size;
19208 DW_BLOCK (attr) = blk;
19210 case DW_FORM_data1:
19211 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19215 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19218 case DW_FORM_flag_present:
19219 DW_UNSND (attr) = 1;
19221 case DW_FORM_sdata:
19222 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19223 info_ptr += bytes_read;
19225 case DW_FORM_udata:
19226 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19227 info_ptr += bytes_read;
19230 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19231 + read_1_byte (abfd, info_ptr));
19235 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19236 + read_2_bytes (abfd, info_ptr));
19240 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19241 + read_4_bytes (abfd, info_ptr));
19245 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19246 + read_8_bytes (abfd, info_ptr));
19249 case DW_FORM_ref_sig8:
19250 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19253 case DW_FORM_ref_udata:
19254 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19255 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19256 info_ptr += bytes_read;
19258 case DW_FORM_indirect:
19259 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19260 info_ptr += bytes_read;
19261 if (form == DW_FORM_implicit_const)
19263 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19264 info_ptr += bytes_read;
19266 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19269 case DW_FORM_implicit_const:
19270 DW_SND (attr) = implicit_const;
19272 case DW_FORM_addrx:
19273 case DW_FORM_GNU_addr_index:
19274 if (reader->dwo_file == NULL)
19276 /* For now flag a hard error.
19277 Later we can turn this into a complaint. */
19278 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19279 dwarf_form_name (form),
19280 bfd_get_filename (abfd));
19282 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19283 info_ptr += bytes_read;
19286 case DW_FORM_strx1:
19287 case DW_FORM_strx2:
19288 case DW_FORM_strx3:
19289 case DW_FORM_strx4:
19290 case DW_FORM_GNU_str_index:
19291 if (reader->dwo_file == NULL)
19293 /* For now flag a hard error.
19294 Later we can turn this into a complaint if warranted. */
19295 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19296 dwarf_form_name (form),
19297 bfd_get_filename (abfd));
19300 ULONGEST str_index;
19301 if (form == DW_FORM_strx1)
19303 str_index = read_1_byte (abfd, info_ptr);
19306 else if (form == DW_FORM_strx2)
19308 str_index = read_2_bytes (abfd, info_ptr);
19311 else if (form == DW_FORM_strx3)
19313 str_index = read_3_bytes (abfd, info_ptr);
19316 else if (form == DW_FORM_strx4)
19318 str_index = read_4_bytes (abfd, info_ptr);
19323 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19324 info_ptr += bytes_read;
19326 DW_STRING (attr) = read_str_index (reader, str_index);
19327 DW_STRING_IS_CANONICAL (attr) = 0;
19331 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19332 dwarf_form_name (form),
19333 bfd_get_filename (abfd));
19337 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19338 attr->form = DW_FORM_GNU_ref_alt;
19340 /* We have seen instances where the compiler tried to emit a byte
19341 size attribute of -1 which ended up being encoded as an unsigned
19342 0xffffffff. Although 0xffffffff is technically a valid size value,
19343 an object of this size seems pretty unlikely so we can relatively
19344 safely treat these cases as if the size attribute was invalid and
19345 treat them as zero by default. */
19346 if (attr->name == DW_AT_byte_size
19347 && form == DW_FORM_data4
19348 && DW_UNSND (attr) >= 0xffffffff)
19351 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19352 hex_string (DW_UNSND (attr)));
19353 DW_UNSND (attr) = 0;
19359 /* Read an attribute described by an abbreviated attribute. */
19361 static const gdb_byte *
19362 read_attribute (const struct die_reader_specs *reader,
19363 struct attribute *attr, struct attr_abbrev *abbrev,
19364 const gdb_byte *info_ptr)
19366 attr->name = abbrev->name;
19367 return read_attribute_value (reader, attr, abbrev->form,
19368 abbrev->implicit_const, info_ptr);
19371 /* Read dwarf information from a buffer. */
19373 static unsigned int
19374 read_1_byte (bfd *abfd, const gdb_byte *buf)
19376 return bfd_get_8 (abfd, buf);
19380 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19382 return bfd_get_signed_8 (abfd, buf);
19385 static unsigned int
19386 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19388 return bfd_get_16 (abfd, buf);
19392 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19394 return bfd_get_signed_16 (abfd, buf);
19397 static unsigned int
19398 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19400 unsigned int result = 0;
19401 for (int i = 0; i < 3; ++i)
19403 unsigned char byte = bfd_get_8 (abfd, buf);
19405 result |= ((unsigned int) byte << (i * 8));
19410 static unsigned int
19411 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19413 return bfd_get_32 (abfd, buf);
19417 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19419 return bfd_get_signed_32 (abfd, buf);
19423 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19425 return bfd_get_64 (abfd, buf);
19429 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19430 unsigned int *bytes_read)
19432 struct comp_unit_head *cu_header = &cu->header;
19433 CORE_ADDR retval = 0;
19435 if (cu_header->signed_addr_p)
19437 switch (cu_header->addr_size)
19440 retval = bfd_get_signed_16 (abfd, buf);
19443 retval = bfd_get_signed_32 (abfd, buf);
19446 retval = bfd_get_signed_64 (abfd, buf);
19449 internal_error (__FILE__, __LINE__,
19450 _("read_address: bad switch, signed [in module %s]"),
19451 bfd_get_filename (abfd));
19456 switch (cu_header->addr_size)
19459 retval = bfd_get_16 (abfd, buf);
19462 retval = bfd_get_32 (abfd, buf);
19465 retval = bfd_get_64 (abfd, buf);
19468 internal_error (__FILE__, __LINE__,
19469 _("read_address: bad switch, "
19470 "unsigned [in module %s]"),
19471 bfd_get_filename (abfd));
19475 *bytes_read = cu_header->addr_size;
19479 /* Read the initial length from a section. The (draft) DWARF 3
19480 specification allows the initial length to take up either 4 bytes
19481 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19482 bytes describe the length and all offsets will be 8 bytes in length
19485 An older, non-standard 64-bit format is also handled by this
19486 function. The older format in question stores the initial length
19487 as an 8-byte quantity without an escape value. Lengths greater
19488 than 2^32 aren't very common which means that the initial 4 bytes
19489 is almost always zero. Since a length value of zero doesn't make
19490 sense for the 32-bit format, this initial zero can be considered to
19491 be an escape value which indicates the presence of the older 64-bit
19492 format. As written, the code can't detect (old format) lengths
19493 greater than 4GB. If it becomes necessary to handle lengths
19494 somewhat larger than 4GB, we could allow other small values (such
19495 as the non-sensical values of 1, 2, and 3) to also be used as
19496 escape values indicating the presence of the old format.
19498 The value returned via bytes_read should be used to increment the
19499 relevant pointer after calling read_initial_length().
19501 [ Note: read_initial_length() and read_offset() are based on the
19502 document entitled "DWARF Debugging Information Format", revision
19503 3, draft 8, dated November 19, 2001. This document was obtained
19506 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19508 This document is only a draft and is subject to change. (So beware.)
19510 Details regarding the older, non-standard 64-bit format were
19511 determined empirically by examining 64-bit ELF files produced by
19512 the SGI toolchain on an IRIX 6.5 machine.
19514 - Kevin, July 16, 2002
19518 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19520 LONGEST length = bfd_get_32 (abfd, buf);
19522 if (length == 0xffffffff)
19524 length = bfd_get_64 (abfd, buf + 4);
19527 else if (length == 0)
19529 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19530 length = bfd_get_64 (abfd, buf);
19541 /* Cover function for read_initial_length.
19542 Returns the length of the object at BUF, and stores the size of the
19543 initial length in *BYTES_READ and stores the size that offsets will be in
19545 If the initial length size is not equivalent to that specified in
19546 CU_HEADER then issue a complaint.
19547 This is useful when reading non-comp-unit headers. */
19550 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19551 const struct comp_unit_head *cu_header,
19552 unsigned int *bytes_read,
19553 unsigned int *offset_size)
19555 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19557 gdb_assert (cu_header->initial_length_size == 4
19558 || cu_header->initial_length_size == 8
19559 || cu_header->initial_length_size == 12);
19561 if (cu_header->initial_length_size != *bytes_read)
19562 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19564 *offset_size = (*bytes_read == 4) ? 4 : 8;
19568 /* Read an offset from the data stream. The size of the offset is
19569 given by cu_header->offset_size. */
19572 read_offset (bfd *abfd, const gdb_byte *buf,
19573 const struct comp_unit_head *cu_header,
19574 unsigned int *bytes_read)
19576 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19578 *bytes_read = cu_header->offset_size;
19582 /* Read an offset from the data stream. */
19585 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19587 LONGEST retval = 0;
19589 switch (offset_size)
19592 retval = bfd_get_32 (abfd, buf);
19595 retval = bfd_get_64 (abfd, buf);
19598 internal_error (__FILE__, __LINE__,
19599 _("read_offset_1: bad switch [in module %s]"),
19600 bfd_get_filename (abfd));
19606 static const gdb_byte *
19607 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19609 /* If the size of a host char is 8 bits, we can return a pointer
19610 to the buffer, otherwise we have to copy the data to a buffer
19611 allocated on the temporary obstack. */
19612 gdb_assert (HOST_CHAR_BIT == 8);
19616 static const char *
19617 read_direct_string (bfd *abfd, const gdb_byte *buf,
19618 unsigned int *bytes_read_ptr)
19620 /* If the size of a host char is 8 bits, we can return a pointer
19621 to the string, otherwise we have to copy the string to a buffer
19622 allocated on the temporary obstack. */
19623 gdb_assert (HOST_CHAR_BIT == 8);
19626 *bytes_read_ptr = 1;
19629 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19630 return (const char *) buf;
19633 /* Return pointer to string at section SECT offset STR_OFFSET with error
19634 reporting strings FORM_NAME and SECT_NAME. */
19636 static const char *
19637 read_indirect_string_at_offset_from (struct objfile *objfile,
19638 bfd *abfd, LONGEST str_offset,
19639 struct dwarf2_section_info *sect,
19640 const char *form_name,
19641 const char *sect_name)
19643 dwarf2_read_section (objfile, sect);
19644 if (sect->buffer == NULL)
19645 error (_("%s used without %s section [in module %s]"),
19646 form_name, sect_name, bfd_get_filename (abfd));
19647 if (str_offset >= sect->size)
19648 error (_("%s pointing outside of %s section [in module %s]"),
19649 form_name, sect_name, bfd_get_filename (abfd));
19650 gdb_assert (HOST_CHAR_BIT == 8);
19651 if (sect->buffer[str_offset] == '\0')
19653 return (const char *) (sect->buffer + str_offset);
19656 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19658 static const char *
19659 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19660 bfd *abfd, LONGEST str_offset)
19662 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19664 &dwarf2_per_objfile->str,
19665 "DW_FORM_strp", ".debug_str");
19668 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19670 static const char *
19671 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19672 bfd *abfd, LONGEST str_offset)
19674 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19676 &dwarf2_per_objfile->line_str,
19677 "DW_FORM_line_strp",
19678 ".debug_line_str");
19681 /* Read a string at offset STR_OFFSET in the .debug_str section from
19682 the .dwz file DWZ. Throw an error if the offset is too large. If
19683 the string consists of a single NUL byte, return NULL; otherwise
19684 return a pointer to the string. */
19686 static const char *
19687 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19688 LONGEST str_offset)
19690 dwarf2_read_section (objfile, &dwz->str);
19692 if (dwz->str.buffer == NULL)
19693 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19694 "section [in module %s]"),
19695 bfd_get_filename (dwz->dwz_bfd));
19696 if (str_offset >= dwz->str.size)
19697 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19698 ".debug_str section [in module %s]"),
19699 bfd_get_filename (dwz->dwz_bfd));
19700 gdb_assert (HOST_CHAR_BIT == 8);
19701 if (dwz->str.buffer[str_offset] == '\0')
19703 return (const char *) (dwz->str.buffer + str_offset);
19706 /* Return pointer to string at .debug_str offset as read from BUF.
19707 BUF is assumed to be in a compilation unit described by CU_HEADER.
19708 Return *BYTES_READ_PTR count of bytes read from BUF. */
19710 static const char *
19711 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19712 const gdb_byte *buf,
19713 const struct comp_unit_head *cu_header,
19714 unsigned int *bytes_read_ptr)
19716 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19718 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19721 /* Return pointer to string at .debug_line_str offset as read from BUF.
19722 BUF is assumed to be in a compilation unit described by CU_HEADER.
19723 Return *BYTES_READ_PTR count of bytes read from BUF. */
19725 static const char *
19726 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19727 bfd *abfd, const gdb_byte *buf,
19728 const struct comp_unit_head *cu_header,
19729 unsigned int *bytes_read_ptr)
19731 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19733 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19738 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19739 unsigned int *bytes_read_ptr)
19742 unsigned int num_read;
19744 unsigned char byte;
19751 byte = bfd_get_8 (abfd, buf);
19754 result |= ((ULONGEST) (byte & 127) << shift);
19755 if ((byte & 128) == 0)
19761 *bytes_read_ptr = num_read;
19766 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19767 unsigned int *bytes_read_ptr)
19770 int shift, num_read;
19771 unsigned char byte;
19778 byte = bfd_get_8 (abfd, buf);
19781 result |= ((ULONGEST) (byte & 127) << shift);
19783 if ((byte & 128) == 0)
19788 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19789 result |= -(((ULONGEST) 1) << shift);
19790 *bytes_read_ptr = num_read;
19794 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19795 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19796 ADDR_SIZE is the size of addresses from the CU header. */
19799 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19800 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19802 struct objfile *objfile = dwarf2_per_objfile->objfile;
19803 bfd *abfd = objfile->obfd;
19804 const gdb_byte *info_ptr;
19806 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19807 if (dwarf2_per_objfile->addr.buffer == NULL)
19808 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19809 objfile_name (objfile));
19810 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19811 error (_("DW_FORM_addr_index pointing outside of "
19812 ".debug_addr section [in module %s]"),
19813 objfile_name (objfile));
19814 info_ptr = (dwarf2_per_objfile->addr.buffer
19815 + addr_base + addr_index * addr_size);
19816 if (addr_size == 4)
19817 return bfd_get_32 (abfd, info_ptr);
19819 return bfd_get_64 (abfd, info_ptr);
19822 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19825 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19827 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19828 cu->addr_base, cu->header.addr_size);
19831 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19834 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19835 unsigned int *bytes_read)
19837 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19838 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19840 return read_addr_index (cu, addr_index);
19843 /* Data structure to pass results from dwarf2_read_addr_index_reader
19844 back to dwarf2_read_addr_index. */
19846 struct dwarf2_read_addr_index_data
19848 ULONGEST addr_base;
19852 /* die_reader_func for dwarf2_read_addr_index. */
19855 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19856 const gdb_byte *info_ptr,
19857 struct die_info *comp_unit_die,
19861 struct dwarf2_cu *cu = reader->cu;
19862 struct dwarf2_read_addr_index_data *aidata =
19863 (struct dwarf2_read_addr_index_data *) data;
19865 aidata->addr_base = cu->addr_base;
19866 aidata->addr_size = cu->header.addr_size;
19869 /* Given an index in .debug_addr, fetch the value.
19870 NOTE: This can be called during dwarf expression evaluation,
19871 long after the debug information has been read, and thus per_cu->cu
19872 may no longer exist. */
19875 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19876 unsigned int addr_index)
19878 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19879 struct dwarf2_cu *cu = per_cu->cu;
19880 ULONGEST addr_base;
19883 /* We need addr_base and addr_size.
19884 If we don't have PER_CU->cu, we have to get it.
19885 Nasty, but the alternative is storing the needed info in PER_CU,
19886 which at this point doesn't seem justified: it's not clear how frequently
19887 it would get used and it would increase the size of every PER_CU.
19888 Entry points like dwarf2_per_cu_addr_size do a similar thing
19889 so we're not in uncharted territory here.
19890 Alas we need to be a bit more complicated as addr_base is contained
19893 We don't need to read the entire CU(/TU).
19894 We just need the header and top level die.
19896 IWBN to use the aging mechanism to let us lazily later discard the CU.
19897 For now we skip this optimization. */
19901 addr_base = cu->addr_base;
19902 addr_size = cu->header.addr_size;
19906 struct dwarf2_read_addr_index_data aidata;
19908 /* Note: We can't use init_cutu_and_read_dies_simple here,
19909 we need addr_base. */
19910 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19911 dwarf2_read_addr_index_reader, &aidata);
19912 addr_base = aidata.addr_base;
19913 addr_size = aidata.addr_size;
19916 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19920 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
19921 This is only used by the Fission support. */
19923 static const char *
19924 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19926 struct dwarf2_cu *cu = reader->cu;
19927 struct dwarf2_per_objfile *dwarf2_per_objfile
19928 = cu->per_cu->dwarf2_per_objfile;
19929 struct objfile *objfile = dwarf2_per_objfile->objfile;
19930 const char *objf_name = objfile_name (objfile);
19931 bfd *abfd = objfile->obfd;
19932 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19933 struct dwarf2_section_info *str_offsets_section =
19934 &reader->dwo_file->sections.str_offsets;
19935 const gdb_byte *info_ptr;
19936 ULONGEST str_offset;
19937 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19939 dwarf2_read_section (objfile, str_section);
19940 dwarf2_read_section (objfile, str_offsets_section);
19941 if (str_section->buffer == NULL)
19942 error (_("%s used without .debug_str.dwo section"
19943 " in CU at offset %s [in module %s]"),
19944 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19945 if (str_offsets_section->buffer == NULL)
19946 error (_("%s used without .debug_str_offsets.dwo section"
19947 " in CU at offset %s [in module %s]"),
19948 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19949 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19950 error (_("%s pointing outside of .debug_str_offsets.dwo"
19951 " section in CU at offset %s [in module %s]"),
19952 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19953 info_ptr = (str_offsets_section->buffer
19954 + str_index * cu->header.offset_size);
19955 if (cu->header.offset_size == 4)
19956 str_offset = bfd_get_32 (abfd, info_ptr);
19958 str_offset = bfd_get_64 (abfd, info_ptr);
19959 if (str_offset >= str_section->size)
19960 error (_("Offset from %s pointing outside of"
19961 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19962 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19963 return (const char *) (str_section->buffer + str_offset);
19966 /* Return the length of an LEB128 number in BUF. */
19969 leb128_size (const gdb_byte *buf)
19971 const gdb_byte *begin = buf;
19977 if ((byte & 128) == 0)
19978 return buf - begin;
19983 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19992 cu->language = language_c;
19995 case DW_LANG_C_plus_plus:
19996 case DW_LANG_C_plus_plus_11:
19997 case DW_LANG_C_plus_plus_14:
19998 cu->language = language_cplus;
20001 cu->language = language_d;
20003 case DW_LANG_Fortran77:
20004 case DW_LANG_Fortran90:
20005 case DW_LANG_Fortran95:
20006 case DW_LANG_Fortran03:
20007 case DW_LANG_Fortran08:
20008 cu->language = language_fortran;
20011 cu->language = language_go;
20013 case DW_LANG_Mips_Assembler:
20014 cu->language = language_asm;
20016 case DW_LANG_Ada83:
20017 case DW_LANG_Ada95:
20018 cu->language = language_ada;
20020 case DW_LANG_Modula2:
20021 cu->language = language_m2;
20023 case DW_LANG_Pascal83:
20024 cu->language = language_pascal;
20027 cu->language = language_objc;
20030 case DW_LANG_Rust_old:
20031 cu->language = language_rust;
20033 case DW_LANG_Cobol74:
20034 case DW_LANG_Cobol85:
20036 cu->language = language_minimal;
20039 cu->language_defn = language_def (cu->language);
20042 /* Return the named attribute or NULL if not there. */
20044 static struct attribute *
20045 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20050 struct attribute *spec = NULL;
20052 for (i = 0; i < die->num_attrs; ++i)
20054 if (die->attrs[i].name == name)
20055 return &die->attrs[i];
20056 if (die->attrs[i].name == DW_AT_specification
20057 || die->attrs[i].name == DW_AT_abstract_origin)
20058 spec = &die->attrs[i];
20064 die = follow_die_ref (die, spec, &cu);
20070 /* Return the named attribute or NULL if not there,
20071 but do not follow DW_AT_specification, etc.
20072 This is for use in contexts where we're reading .debug_types dies.
20073 Following DW_AT_specification, DW_AT_abstract_origin will take us
20074 back up the chain, and we want to go down. */
20076 static struct attribute *
20077 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20081 for (i = 0; i < die->num_attrs; ++i)
20082 if (die->attrs[i].name == name)
20083 return &die->attrs[i];
20088 /* Return the string associated with a string-typed attribute, or NULL if it
20089 is either not found or is of an incorrect type. */
20091 static const char *
20092 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20094 struct attribute *attr;
20095 const char *str = NULL;
20097 attr = dwarf2_attr (die, name, cu);
20101 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20102 || attr->form == DW_FORM_string
20103 || attr->form == DW_FORM_strx
20104 || attr->form == DW_FORM_GNU_str_index
20105 || attr->form == DW_FORM_GNU_strp_alt)
20106 str = DW_STRING (attr);
20108 complaint (_("string type expected for attribute %s for "
20109 "DIE at %s in module %s"),
20110 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20111 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20117 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20118 and holds a non-zero value. This function should only be used for
20119 DW_FORM_flag or DW_FORM_flag_present attributes. */
20122 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20124 struct attribute *attr = dwarf2_attr (die, name, cu);
20126 return (attr && DW_UNSND (attr));
20130 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20132 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20133 which value is non-zero. However, we have to be careful with
20134 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20135 (via dwarf2_flag_true_p) follows this attribute. So we may
20136 end up accidently finding a declaration attribute that belongs
20137 to a different DIE referenced by the specification attribute,
20138 even though the given DIE does not have a declaration attribute. */
20139 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20140 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20143 /* Return the die giving the specification for DIE, if there is
20144 one. *SPEC_CU is the CU containing DIE on input, and the CU
20145 containing the return value on output. If there is no
20146 specification, but there is an abstract origin, that is
20149 static struct die_info *
20150 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20152 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20155 if (spec_attr == NULL)
20156 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20158 if (spec_attr == NULL)
20161 return follow_die_ref (die, spec_attr, spec_cu);
20164 /* Stub for free_line_header to match void * callback types. */
20167 free_line_header_voidp (void *arg)
20169 struct line_header *lh = (struct line_header *) arg;
20175 line_header::add_include_dir (const char *include_dir)
20177 if (dwarf_line_debug >= 2)
20178 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20179 include_dirs.size () + 1, include_dir);
20181 include_dirs.push_back (include_dir);
20185 line_header::add_file_name (const char *name,
20187 unsigned int mod_time,
20188 unsigned int length)
20190 if (dwarf_line_debug >= 2)
20191 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20192 (unsigned) file_names.size () + 1, name);
20194 file_names.emplace_back (name, d_index, mod_time, length);
20197 /* A convenience function to find the proper .debug_line section for a CU. */
20199 static struct dwarf2_section_info *
20200 get_debug_line_section (struct dwarf2_cu *cu)
20202 struct dwarf2_section_info *section;
20203 struct dwarf2_per_objfile *dwarf2_per_objfile
20204 = cu->per_cu->dwarf2_per_objfile;
20206 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20208 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20209 section = &cu->dwo_unit->dwo_file->sections.line;
20210 else if (cu->per_cu->is_dwz)
20212 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20214 section = &dwz->line;
20217 section = &dwarf2_per_objfile->line;
20222 /* Read directory or file name entry format, starting with byte of
20223 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20224 entries count and the entries themselves in the described entry
20228 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20229 bfd *abfd, const gdb_byte **bufp,
20230 struct line_header *lh,
20231 const struct comp_unit_head *cu_header,
20232 void (*callback) (struct line_header *lh,
20235 unsigned int mod_time,
20236 unsigned int length))
20238 gdb_byte format_count, formati;
20239 ULONGEST data_count, datai;
20240 const gdb_byte *buf = *bufp;
20241 const gdb_byte *format_header_data;
20242 unsigned int bytes_read;
20244 format_count = read_1_byte (abfd, buf);
20246 format_header_data = buf;
20247 for (formati = 0; formati < format_count; formati++)
20249 read_unsigned_leb128 (abfd, buf, &bytes_read);
20251 read_unsigned_leb128 (abfd, buf, &bytes_read);
20255 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20257 for (datai = 0; datai < data_count; datai++)
20259 const gdb_byte *format = format_header_data;
20260 struct file_entry fe;
20262 for (formati = 0; formati < format_count; formati++)
20264 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20265 format += bytes_read;
20267 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20268 format += bytes_read;
20270 gdb::optional<const char *> string;
20271 gdb::optional<unsigned int> uint;
20275 case DW_FORM_string:
20276 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20280 case DW_FORM_line_strp:
20281 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20288 case DW_FORM_data1:
20289 uint.emplace (read_1_byte (abfd, buf));
20293 case DW_FORM_data2:
20294 uint.emplace (read_2_bytes (abfd, buf));
20298 case DW_FORM_data4:
20299 uint.emplace (read_4_bytes (abfd, buf));
20303 case DW_FORM_data8:
20304 uint.emplace (read_8_bytes (abfd, buf));
20308 case DW_FORM_udata:
20309 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20313 case DW_FORM_block:
20314 /* It is valid only for DW_LNCT_timestamp which is ignored by
20319 switch (content_type)
20322 if (string.has_value ())
20325 case DW_LNCT_directory_index:
20326 if (uint.has_value ())
20327 fe.d_index = (dir_index) *uint;
20329 case DW_LNCT_timestamp:
20330 if (uint.has_value ())
20331 fe.mod_time = *uint;
20334 if (uint.has_value ())
20340 complaint (_("Unknown format content type %s"),
20341 pulongest (content_type));
20345 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20351 /* Read the statement program header starting at OFFSET in
20352 .debug_line, or .debug_line.dwo. Return a pointer
20353 to a struct line_header, allocated using xmalloc.
20354 Returns NULL if there is a problem reading the header, e.g., if it
20355 has a version we don't understand.
20357 NOTE: the strings in the include directory and file name tables of
20358 the returned object point into the dwarf line section buffer,
20359 and must not be freed. */
20361 static line_header_up
20362 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20364 const gdb_byte *line_ptr;
20365 unsigned int bytes_read, offset_size;
20367 const char *cur_dir, *cur_file;
20368 struct dwarf2_section_info *section;
20370 struct dwarf2_per_objfile *dwarf2_per_objfile
20371 = cu->per_cu->dwarf2_per_objfile;
20373 section = get_debug_line_section (cu);
20374 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20375 if (section->buffer == NULL)
20377 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20378 complaint (_("missing .debug_line.dwo section"));
20380 complaint (_("missing .debug_line section"));
20384 /* We can't do this until we know the section is non-empty.
20385 Only then do we know we have such a section. */
20386 abfd = get_section_bfd_owner (section);
20388 /* Make sure that at least there's room for the total_length field.
20389 That could be 12 bytes long, but we're just going to fudge that. */
20390 if (to_underlying (sect_off) + 4 >= section->size)
20392 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20396 line_header_up lh (new line_header ());
20398 lh->sect_off = sect_off;
20399 lh->offset_in_dwz = cu->per_cu->is_dwz;
20401 line_ptr = section->buffer + to_underlying (sect_off);
20403 /* Read in the header. */
20405 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20406 &bytes_read, &offset_size);
20407 line_ptr += bytes_read;
20408 if (line_ptr + lh->total_length > (section->buffer + section->size))
20410 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20413 lh->statement_program_end = line_ptr + lh->total_length;
20414 lh->version = read_2_bytes (abfd, line_ptr);
20416 if (lh->version > 5)
20418 /* This is a version we don't understand. The format could have
20419 changed in ways we don't handle properly so just punt. */
20420 complaint (_("unsupported version in .debug_line section"));
20423 if (lh->version >= 5)
20425 gdb_byte segment_selector_size;
20427 /* Skip address size. */
20428 read_1_byte (abfd, line_ptr);
20431 segment_selector_size = read_1_byte (abfd, line_ptr);
20433 if (segment_selector_size != 0)
20435 complaint (_("unsupported segment selector size %u "
20436 "in .debug_line section"),
20437 segment_selector_size);
20441 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20442 line_ptr += offset_size;
20443 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20445 if (lh->version >= 4)
20447 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20451 lh->maximum_ops_per_instruction = 1;
20453 if (lh->maximum_ops_per_instruction == 0)
20455 lh->maximum_ops_per_instruction = 1;
20456 complaint (_("invalid maximum_ops_per_instruction "
20457 "in `.debug_line' section"));
20460 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20462 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20464 lh->line_range = read_1_byte (abfd, line_ptr);
20466 lh->opcode_base = read_1_byte (abfd, line_ptr);
20468 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20470 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20471 for (i = 1; i < lh->opcode_base; ++i)
20473 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20477 if (lh->version >= 5)
20479 /* Read directory table. */
20480 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20482 [] (struct line_header *header, const char *name,
20483 dir_index d_index, unsigned int mod_time,
20484 unsigned int length)
20486 header->add_include_dir (name);
20489 /* Read file name table. */
20490 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20492 [] (struct line_header *header, const char *name,
20493 dir_index d_index, unsigned int mod_time,
20494 unsigned int length)
20496 header->add_file_name (name, d_index, mod_time, length);
20501 /* Read directory table. */
20502 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20504 line_ptr += bytes_read;
20505 lh->add_include_dir (cur_dir);
20507 line_ptr += bytes_read;
20509 /* Read file name table. */
20510 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20512 unsigned int mod_time, length;
20515 line_ptr += bytes_read;
20516 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20517 line_ptr += bytes_read;
20518 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20519 line_ptr += bytes_read;
20520 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20521 line_ptr += bytes_read;
20523 lh->add_file_name (cur_file, d_index, mod_time, length);
20525 line_ptr += bytes_read;
20527 lh->statement_program_start = line_ptr;
20529 if (line_ptr > (section->buffer + section->size))
20530 complaint (_("line number info header doesn't "
20531 "fit in `.debug_line' section"));
20536 /* Subroutine of dwarf_decode_lines to simplify it.
20537 Return the file name of the psymtab for included file FILE_INDEX
20538 in line header LH of PST.
20539 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20540 If space for the result is malloc'd, *NAME_HOLDER will be set.
20541 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20543 static const char *
20544 psymtab_include_file_name (const struct line_header *lh, int file_index,
20545 const struct partial_symtab *pst,
20546 const char *comp_dir,
20547 gdb::unique_xmalloc_ptr<char> *name_holder)
20549 const file_entry &fe = lh->file_names[file_index];
20550 const char *include_name = fe.name;
20551 const char *include_name_to_compare = include_name;
20552 const char *pst_filename;
20555 const char *dir_name = fe.include_dir (lh);
20557 gdb::unique_xmalloc_ptr<char> hold_compare;
20558 if (!IS_ABSOLUTE_PATH (include_name)
20559 && (dir_name != NULL || comp_dir != NULL))
20561 /* Avoid creating a duplicate psymtab for PST.
20562 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20563 Before we do the comparison, however, we need to account
20564 for DIR_NAME and COMP_DIR.
20565 First prepend dir_name (if non-NULL). If we still don't
20566 have an absolute path prepend comp_dir (if non-NULL).
20567 However, the directory we record in the include-file's
20568 psymtab does not contain COMP_DIR (to match the
20569 corresponding symtab(s)).
20574 bash$ gcc -g ./hello.c
20575 include_name = "hello.c"
20577 DW_AT_comp_dir = comp_dir = "/tmp"
20578 DW_AT_name = "./hello.c"
20582 if (dir_name != NULL)
20584 name_holder->reset (concat (dir_name, SLASH_STRING,
20585 include_name, (char *) NULL));
20586 include_name = name_holder->get ();
20587 include_name_to_compare = include_name;
20589 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20591 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20592 include_name, (char *) NULL));
20593 include_name_to_compare = hold_compare.get ();
20597 pst_filename = pst->filename;
20598 gdb::unique_xmalloc_ptr<char> copied_name;
20599 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20601 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20602 pst_filename, (char *) NULL));
20603 pst_filename = copied_name.get ();
20606 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20610 return include_name;
20613 /* State machine to track the state of the line number program. */
20615 class lnp_state_machine
20618 /* Initialize a machine state for the start of a line number
20620 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20621 bool record_lines_p);
20623 file_entry *current_file ()
20625 /* lh->file_names is 0-based, but the file name numbers in the
20626 statement program are 1-based. */
20627 return m_line_header->file_name_at (m_file);
20630 /* Record the line in the state machine. END_SEQUENCE is true if
20631 we're processing the end of a sequence. */
20632 void record_line (bool end_sequence);
20634 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20635 nop-out rest of the lines in this sequence. */
20636 void check_line_address (struct dwarf2_cu *cu,
20637 const gdb_byte *line_ptr,
20638 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20640 void handle_set_discriminator (unsigned int discriminator)
20642 m_discriminator = discriminator;
20643 m_line_has_non_zero_discriminator |= discriminator != 0;
20646 /* Handle DW_LNE_set_address. */
20647 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20650 address += baseaddr;
20651 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20654 /* Handle DW_LNS_advance_pc. */
20655 void handle_advance_pc (CORE_ADDR adjust);
20657 /* Handle a special opcode. */
20658 void handle_special_opcode (unsigned char op_code);
20660 /* Handle DW_LNS_advance_line. */
20661 void handle_advance_line (int line_delta)
20663 advance_line (line_delta);
20666 /* Handle DW_LNS_set_file. */
20667 void handle_set_file (file_name_index file);
20669 /* Handle DW_LNS_negate_stmt. */
20670 void handle_negate_stmt ()
20672 m_is_stmt = !m_is_stmt;
20675 /* Handle DW_LNS_const_add_pc. */
20676 void handle_const_add_pc ();
20678 /* Handle DW_LNS_fixed_advance_pc. */
20679 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20681 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20685 /* Handle DW_LNS_copy. */
20686 void handle_copy ()
20688 record_line (false);
20689 m_discriminator = 0;
20692 /* Handle DW_LNE_end_sequence. */
20693 void handle_end_sequence ()
20695 m_currently_recording_lines = true;
20699 /* Advance the line by LINE_DELTA. */
20700 void advance_line (int line_delta)
20702 m_line += line_delta;
20704 if (line_delta != 0)
20705 m_line_has_non_zero_discriminator = m_discriminator != 0;
20708 struct dwarf2_cu *m_cu;
20710 gdbarch *m_gdbarch;
20712 /* True if we're recording lines.
20713 Otherwise we're building partial symtabs and are just interested in
20714 finding include files mentioned by the line number program. */
20715 bool m_record_lines_p;
20717 /* The line number header. */
20718 line_header *m_line_header;
20720 /* These are part of the standard DWARF line number state machine,
20721 and initialized according to the DWARF spec. */
20723 unsigned char m_op_index = 0;
20724 /* The line table index (1-based) of the current file. */
20725 file_name_index m_file = (file_name_index) 1;
20726 unsigned int m_line = 1;
20728 /* These are initialized in the constructor. */
20730 CORE_ADDR m_address;
20732 unsigned int m_discriminator;
20734 /* Additional bits of state we need to track. */
20736 /* The last file that we called dwarf2_start_subfile for.
20737 This is only used for TLLs. */
20738 unsigned int m_last_file = 0;
20739 /* The last file a line number was recorded for. */
20740 struct subfile *m_last_subfile = NULL;
20742 /* When true, record the lines we decode. */
20743 bool m_currently_recording_lines = false;
20745 /* The last line number that was recorded, used to coalesce
20746 consecutive entries for the same line. This can happen, for
20747 example, when discriminators are present. PR 17276. */
20748 unsigned int m_last_line = 0;
20749 bool m_line_has_non_zero_discriminator = false;
20753 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20755 CORE_ADDR addr_adj = (((m_op_index + adjust)
20756 / m_line_header->maximum_ops_per_instruction)
20757 * m_line_header->minimum_instruction_length);
20758 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20759 m_op_index = ((m_op_index + adjust)
20760 % m_line_header->maximum_ops_per_instruction);
20764 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20766 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20767 CORE_ADDR addr_adj = (((m_op_index
20768 + (adj_opcode / m_line_header->line_range))
20769 / m_line_header->maximum_ops_per_instruction)
20770 * m_line_header->minimum_instruction_length);
20771 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20772 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20773 % m_line_header->maximum_ops_per_instruction);
20775 int line_delta = (m_line_header->line_base
20776 + (adj_opcode % m_line_header->line_range));
20777 advance_line (line_delta);
20778 record_line (false);
20779 m_discriminator = 0;
20783 lnp_state_machine::handle_set_file (file_name_index file)
20787 const file_entry *fe = current_file ();
20789 dwarf2_debug_line_missing_file_complaint ();
20790 else if (m_record_lines_p)
20792 const char *dir = fe->include_dir (m_line_header);
20794 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20795 m_line_has_non_zero_discriminator = m_discriminator != 0;
20796 dwarf2_start_subfile (m_cu, fe->name, dir);
20801 lnp_state_machine::handle_const_add_pc ()
20804 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20807 = (((m_op_index + adjust)
20808 / m_line_header->maximum_ops_per_instruction)
20809 * m_line_header->minimum_instruction_length);
20811 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20812 m_op_index = ((m_op_index + adjust)
20813 % m_line_header->maximum_ops_per_instruction);
20816 /* Return non-zero if we should add LINE to the line number table.
20817 LINE is the line to add, LAST_LINE is the last line that was added,
20818 LAST_SUBFILE is the subfile for LAST_LINE.
20819 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20820 had a non-zero discriminator.
20822 We have to be careful in the presence of discriminators.
20823 E.g., for this line:
20825 for (i = 0; i < 100000; i++);
20827 clang can emit four line number entries for that one line,
20828 each with a different discriminator.
20829 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20831 However, we want gdb to coalesce all four entries into one.
20832 Otherwise the user could stepi into the middle of the line and
20833 gdb would get confused about whether the pc really was in the
20834 middle of the line.
20836 Things are further complicated by the fact that two consecutive
20837 line number entries for the same line is a heuristic used by gcc
20838 to denote the end of the prologue. So we can't just discard duplicate
20839 entries, we have to be selective about it. The heuristic we use is
20840 that we only collapse consecutive entries for the same line if at least
20841 one of those entries has a non-zero discriminator. PR 17276.
20843 Note: Addresses in the line number state machine can never go backwards
20844 within one sequence, thus this coalescing is ok. */
20847 dwarf_record_line_p (struct dwarf2_cu *cu,
20848 unsigned int line, unsigned int last_line,
20849 int line_has_non_zero_discriminator,
20850 struct subfile *last_subfile)
20852 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20854 if (line != last_line)
20856 /* Same line for the same file that we've seen already.
20857 As a last check, for pr 17276, only record the line if the line
20858 has never had a non-zero discriminator. */
20859 if (!line_has_non_zero_discriminator)
20864 /* Use the CU's builder to record line number LINE beginning at
20865 address ADDRESS in the line table of subfile SUBFILE. */
20868 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20869 unsigned int line, CORE_ADDR address,
20870 struct dwarf2_cu *cu)
20872 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20874 if (dwarf_line_debug)
20876 fprintf_unfiltered (gdb_stdlog,
20877 "Recording line %u, file %s, address %s\n",
20878 line, lbasename (subfile->name),
20879 paddress (gdbarch, address));
20883 cu->get_builder ()->record_line (subfile, line, addr);
20886 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20887 Mark the end of a set of line number records.
20888 The arguments are the same as for dwarf_record_line_1.
20889 If SUBFILE is NULL the request is ignored. */
20892 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20893 CORE_ADDR address, struct dwarf2_cu *cu)
20895 if (subfile == NULL)
20898 if (dwarf_line_debug)
20900 fprintf_unfiltered (gdb_stdlog,
20901 "Finishing current line, file %s, address %s\n",
20902 lbasename (subfile->name),
20903 paddress (gdbarch, address));
20906 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20910 lnp_state_machine::record_line (bool end_sequence)
20912 if (dwarf_line_debug)
20914 fprintf_unfiltered (gdb_stdlog,
20915 "Processing actual line %u: file %u,"
20916 " address %s, is_stmt %u, discrim %u\n",
20917 m_line, to_underlying (m_file),
20918 paddress (m_gdbarch, m_address),
20919 m_is_stmt, m_discriminator);
20922 file_entry *fe = current_file ();
20925 dwarf2_debug_line_missing_file_complaint ();
20926 /* For now we ignore lines not starting on an instruction boundary.
20927 But not when processing end_sequence for compatibility with the
20928 previous version of the code. */
20929 else if (m_op_index == 0 || end_sequence)
20931 fe->included_p = 1;
20932 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
20934 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20937 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20938 m_currently_recording_lines ? m_cu : nullptr);
20943 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20944 m_line_has_non_zero_discriminator,
20947 buildsym_compunit *builder = m_cu->get_builder ();
20948 dwarf_record_line_1 (m_gdbarch,
20949 builder->get_current_subfile (),
20951 m_currently_recording_lines ? m_cu : nullptr);
20953 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20954 m_last_line = m_line;
20960 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20961 line_header *lh, bool record_lines_p)
20965 m_record_lines_p = record_lines_p;
20966 m_line_header = lh;
20968 m_currently_recording_lines = true;
20970 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20971 was a line entry for it so that the backend has a chance to adjust it
20972 and also record it in case it needs it. This is currently used by MIPS
20973 code, cf. `mips_adjust_dwarf2_line'. */
20974 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20975 m_is_stmt = lh->default_is_stmt;
20976 m_discriminator = 0;
20980 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20981 const gdb_byte *line_ptr,
20982 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20984 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20985 the pc range of the CU. However, we restrict the test to only ADDRESS
20986 values of zero to preserve GDB's previous behaviour which is to handle
20987 the specific case of a function being GC'd by the linker. */
20989 if (address == 0 && address < unrelocated_lowpc)
20991 /* This line table is for a function which has been
20992 GCd by the linker. Ignore it. PR gdb/12528 */
20994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20995 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20997 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20998 line_offset, objfile_name (objfile));
20999 m_currently_recording_lines = false;
21000 /* Note: m_currently_recording_lines is left as false until we see
21001 DW_LNE_end_sequence. */
21005 /* Subroutine of dwarf_decode_lines to simplify it.
21006 Process the line number information in LH.
21007 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21008 program in order to set included_p for every referenced header. */
21011 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21012 const int decode_for_pst_p, CORE_ADDR lowpc)
21014 const gdb_byte *line_ptr, *extended_end;
21015 const gdb_byte *line_end;
21016 unsigned int bytes_read, extended_len;
21017 unsigned char op_code, extended_op;
21018 CORE_ADDR baseaddr;
21019 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21020 bfd *abfd = objfile->obfd;
21021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21022 /* True if we're recording line info (as opposed to building partial
21023 symtabs and just interested in finding include files mentioned by
21024 the line number program). */
21025 bool record_lines_p = !decode_for_pst_p;
21027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21029 line_ptr = lh->statement_program_start;
21030 line_end = lh->statement_program_end;
21032 /* Read the statement sequences until there's nothing left. */
21033 while (line_ptr < line_end)
21035 /* The DWARF line number program state machine. Reset the state
21036 machine at the start of each sequence. */
21037 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21038 bool end_sequence = false;
21040 if (record_lines_p)
21042 /* Start a subfile for the current file of the state
21044 const file_entry *fe = state_machine.current_file ();
21047 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21050 /* Decode the table. */
21051 while (line_ptr < line_end && !end_sequence)
21053 op_code = read_1_byte (abfd, line_ptr);
21056 if (op_code >= lh->opcode_base)
21058 /* Special opcode. */
21059 state_machine.handle_special_opcode (op_code);
21061 else switch (op_code)
21063 case DW_LNS_extended_op:
21064 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21066 line_ptr += bytes_read;
21067 extended_end = line_ptr + extended_len;
21068 extended_op = read_1_byte (abfd, line_ptr);
21070 switch (extended_op)
21072 case DW_LNE_end_sequence:
21073 state_machine.handle_end_sequence ();
21074 end_sequence = true;
21076 case DW_LNE_set_address:
21079 = read_address (abfd, line_ptr, cu, &bytes_read);
21080 line_ptr += bytes_read;
21082 state_machine.check_line_address (cu, line_ptr,
21083 lowpc - baseaddr, address);
21084 state_machine.handle_set_address (baseaddr, address);
21087 case DW_LNE_define_file:
21089 const char *cur_file;
21090 unsigned int mod_time, length;
21093 cur_file = read_direct_string (abfd, line_ptr,
21095 line_ptr += bytes_read;
21096 dindex = (dir_index)
21097 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21098 line_ptr += bytes_read;
21100 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21101 line_ptr += bytes_read;
21103 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21104 line_ptr += bytes_read;
21105 lh->add_file_name (cur_file, dindex, mod_time, length);
21108 case DW_LNE_set_discriminator:
21110 /* The discriminator is not interesting to the
21111 debugger; just ignore it. We still need to
21112 check its value though:
21113 if there are consecutive entries for the same
21114 (non-prologue) line we want to coalesce them.
21117 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21118 line_ptr += bytes_read;
21120 state_machine.handle_set_discriminator (discr);
21124 complaint (_("mangled .debug_line section"));
21127 /* Make sure that we parsed the extended op correctly. If e.g.
21128 we expected a different address size than the producer used,
21129 we may have read the wrong number of bytes. */
21130 if (line_ptr != extended_end)
21132 complaint (_("mangled .debug_line section"));
21137 state_machine.handle_copy ();
21139 case DW_LNS_advance_pc:
21142 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21143 line_ptr += bytes_read;
21145 state_machine.handle_advance_pc (adjust);
21148 case DW_LNS_advance_line:
21151 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21152 line_ptr += bytes_read;
21154 state_machine.handle_advance_line (line_delta);
21157 case DW_LNS_set_file:
21159 file_name_index file
21160 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21162 line_ptr += bytes_read;
21164 state_machine.handle_set_file (file);
21167 case DW_LNS_set_column:
21168 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21169 line_ptr += bytes_read;
21171 case DW_LNS_negate_stmt:
21172 state_machine.handle_negate_stmt ();
21174 case DW_LNS_set_basic_block:
21176 /* Add to the address register of the state machine the
21177 address increment value corresponding to special opcode
21178 255. I.e., this value is scaled by the minimum
21179 instruction length since special opcode 255 would have
21180 scaled the increment. */
21181 case DW_LNS_const_add_pc:
21182 state_machine.handle_const_add_pc ();
21184 case DW_LNS_fixed_advance_pc:
21186 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21189 state_machine.handle_fixed_advance_pc (addr_adj);
21194 /* Unknown standard opcode, ignore it. */
21197 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21199 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21200 line_ptr += bytes_read;
21207 dwarf2_debug_line_missing_end_sequence_complaint ();
21209 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21210 in which case we still finish recording the last line). */
21211 state_machine.record_line (true);
21215 /* Decode the Line Number Program (LNP) for the given line_header
21216 structure and CU. The actual information extracted and the type
21217 of structures created from the LNP depends on the value of PST.
21219 1. If PST is NULL, then this procedure uses the data from the program
21220 to create all necessary symbol tables, and their linetables.
21222 2. If PST is not NULL, this procedure reads the program to determine
21223 the list of files included by the unit represented by PST, and
21224 builds all the associated partial symbol tables.
21226 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21227 It is used for relative paths in the line table.
21228 NOTE: When processing partial symtabs (pst != NULL),
21229 comp_dir == pst->dirname.
21231 NOTE: It is important that psymtabs have the same file name (via strcmp)
21232 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21233 symtab we don't use it in the name of the psymtabs we create.
21234 E.g. expand_line_sal requires this when finding psymtabs to expand.
21235 A good testcase for this is mb-inline.exp.
21237 LOWPC is the lowest address in CU (or 0 if not known).
21239 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21240 for its PC<->lines mapping information. Otherwise only the filename
21241 table is read in. */
21244 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21245 struct dwarf2_cu *cu, struct partial_symtab *pst,
21246 CORE_ADDR lowpc, int decode_mapping)
21248 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21249 const int decode_for_pst_p = (pst != NULL);
21251 if (decode_mapping)
21252 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21254 if (decode_for_pst_p)
21258 /* Now that we're done scanning the Line Header Program, we can
21259 create the psymtab of each included file. */
21260 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21261 if (lh->file_names[file_index].included_p == 1)
21263 gdb::unique_xmalloc_ptr<char> name_holder;
21264 const char *include_name =
21265 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21267 if (include_name != NULL)
21268 dwarf2_create_include_psymtab (include_name, pst, objfile);
21273 /* Make sure a symtab is created for every file, even files
21274 which contain only variables (i.e. no code with associated
21276 buildsym_compunit *builder = cu->get_builder ();
21277 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21280 for (i = 0; i < lh->file_names.size (); i++)
21282 file_entry &fe = lh->file_names[i];
21284 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21286 if (builder->get_current_subfile ()->symtab == NULL)
21288 builder->get_current_subfile ()->symtab
21289 = allocate_symtab (cust,
21290 builder->get_current_subfile ()->name);
21292 fe.symtab = builder->get_current_subfile ()->symtab;
21297 /* Start a subfile for DWARF. FILENAME is the name of the file and
21298 DIRNAME the name of the source directory which contains FILENAME
21299 or NULL if not known.
21300 This routine tries to keep line numbers from identical absolute and
21301 relative file names in a common subfile.
21303 Using the `list' example from the GDB testsuite, which resides in
21304 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21305 of /srcdir/list0.c yields the following debugging information for list0.c:
21307 DW_AT_name: /srcdir/list0.c
21308 DW_AT_comp_dir: /compdir
21309 files.files[0].name: list0.h
21310 files.files[0].dir: /srcdir
21311 files.files[1].name: list0.c
21312 files.files[1].dir: /srcdir
21314 The line number information for list0.c has to end up in a single
21315 subfile, so that `break /srcdir/list0.c:1' works as expected.
21316 start_subfile will ensure that this happens provided that we pass the
21317 concatenation of files.files[1].dir and files.files[1].name as the
21321 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21322 const char *dirname)
21326 /* In order not to lose the line information directory,
21327 we concatenate it to the filename when it makes sense.
21328 Note that the Dwarf3 standard says (speaking of filenames in line
21329 information): ``The directory index is ignored for file names
21330 that represent full path names''. Thus ignoring dirname in the
21331 `else' branch below isn't an issue. */
21333 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21335 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21339 cu->get_builder ()->start_subfile (filename);
21345 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21346 buildsym_compunit constructor. */
21348 struct compunit_symtab *
21349 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21352 gdb_assert (m_builder == nullptr);
21354 m_builder.reset (new struct buildsym_compunit
21355 (per_cu->dwarf2_per_objfile->objfile,
21356 name, comp_dir, language, low_pc));
21358 list_in_scope = get_builder ()->get_file_symbols ();
21360 get_builder ()->record_debugformat ("DWARF 2");
21361 get_builder ()->record_producer (producer);
21363 processing_has_namespace_info = false;
21365 return get_builder ()->get_compunit_symtab ();
21369 var_decode_location (struct attribute *attr, struct symbol *sym,
21370 struct dwarf2_cu *cu)
21372 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21373 struct comp_unit_head *cu_header = &cu->header;
21375 /* NOTE drow/2003-01-30: There used to be a comment and some special
21376 code here to turn a symbol with DW_AT_external and a
21377 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21378 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21379 with some versions of binutils) where shared libraries could have
21380 relocations against symbols in their debug information - the
21381 minimal symbol would have the right address, but the debug info
21382 would not. It's no longer necessary, because we will explicitly
21383 apply relocations when we read in the debug information now. */
21385 /* A DW_AT_location attribute with no contents indicates that a
21386 variable has been optimized away. */
21387 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21389 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21393 /* Handle one degenerate form of location expression specially, to
21394 preserve GDB's previous behavior when section offsets are
21395 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21396 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21398 if (attr_form_is_block (attr)
21399 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21400 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21401 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21402 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21403 && (DW_BLOCK (attr)->size
21404 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21406 unsigned int dummy;
21408 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21409 SYMBOL_VALUE_ADDRESS (sym) =
21410 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21412 SYMBOL_VALUE_ADDRESS (sym) =
21413 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21414 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21415 fixup_symbol_section (sym, objfile);
21416 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21417 SYMBOL_SECTION (sym));
21421 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21422 expression evaluator, and use LOC_COMPUTED only when necessary
21423 (i.e. when the value of a register or memory location is
21424 referenced, or a thread-local block, etc.). Then again, it might
21425 not be worthwhile. I'm assuming that it isn't unless performance
21426 or memory numbers show me otherwise. */
21428 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21430 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21431 cu->has_loclist = true;
21434 /* Given a pointer to a DWARF information entry, figure out if we need
21435 to make a symbol table entry for it, and if so, create a new entry
21436 and return a pointer to it.
21437 If TYPE is NULL, determine symbol type from the die, otherwise
21438 used the passed type.
21439 If SPACE is not NULL, use it to hold the new symbol. If it is
21440 NULL, allocate a new symbol on the objfile's obstack. */
21442 static struct symbol *
21443 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21444 struct symbol *space)
21446 struct dwarf2_per_objfile *dwarf2_per_objfile
21447 = cu->per_cu->dwarf2_per_objfile;
21448 struct objfile *objfile = dwarf2_per_objfile->objfile;
21449 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21450 struct symbol *sym = NULL;
21452 struct attribute *attr = NULL;
21453 struct attribute *attr2 = NULL;
21454 CORE_ADDR baseaddr;
21455 struct pending **list_to_add = NULL;
21457 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21459 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21461 name = dwarf2_name (die, cu);
21464 const char *linkagename;
21465 int suppress_add = 0;
21470 sym = allocate_symbol (objfile);
21471 OBJSTAT (objfile, n_syms++);
21473 /* Cache this symbol's name and the name's demangled form (if any). */
21474 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21475 linkagename = dwarf2_physname (name, die, cu);
21476 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21478 /* Fortran does not have mangling standard and the mangling does differ
21479 between gfortran, iFort etc. */
21480 if (cu->language == language_fortran
21481 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21482 symbol_set_demangled_name (&(sym->ginfo),
21483 dwarf2_full_name (name, die, cu),
21486 /* Default assumptions.
21487 Use the passed type or decode it from the die. */
21488 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21489 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21491 SYMBOL_TYPE (sym) = type;
21493 SYMBOL_TYPE (sym) = die_type (die, cu);
21494 attr = dwarf2_attr (die,
21495 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21499 SYMBOL_LINE (sym) = DW_UNSND (attr);
21502 attr = dwarf2_attr (die,
21503 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21507 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21508 struct file_entry *fe;
21510 if (cu->line_header != NULL)
21511 fe = cu->line_header->file_name_at (file_index);
21516 complaint (_("file index out of range"));
21518 symbol_set_symtab (sym, fe->symtab);
21524 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21529 addr = attr_value_as_address (attr);
21530 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21531 SYMBOL_VALUE_ADDRESS (sym) = addr;
21533 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21534 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21535 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21536 add_symbol_to_list (sym, cu->list_in_scope);
21538 case DW_TAG_subprogram:
21539 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21541 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21542 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21543 if ((attr2 && (DW_UNSND (attr2) != 0))
21544 || cu->language == language_ada)
21546 /* Subprograms marked external are stored as a global symbol.
21547 Ada subprograms, whether marked external or not, are always
21548 stored as a global symbol, because we want to be able to
21549 access them globally. For instance, we want to be able
21550 to break on a nested subprogram without having to
21551 specify the context. */
21552 list_to_add = cu->get_builder ()->get_global_symbols ();
21556 list_to_add = cu->list_in_scope;
21559 case DW_TAG_inlined_subroutine:
21560 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21562 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21563 SYMBOL_INLINED (sym) = 1;
21564 list_to_add = cu->list_in_scope;
21566 case DW_TAG_template_value_param:
21568 /* Fall through. */
21569 case DW_TAG_constant:
21570 case DW_TAG_variable:
21571 case DW_TAG_member:
21572 /* Compilation with minimal debug info may result in
21573 variables with missing type entries. Change the
21574 misleading `void' type to something sensible. */
21575 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21576 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21578 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21579 /* In the case of DW_TAG_member, we should only be called for
21580 static const members. */
21581 if (die->tag == DW_TAG_member)
21583 /* dwarf2_add_field uses die_is_declaration,
21584 so we do the same. */
21585 gdb_assert (die_is_declaration (die, cu));
21590 dwarf2_const_value (attr, sym, cu);
21591 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21594 if (attr2 && (DW_UNSND (attr2) != 0))
21595 list_to_add = cu->get_builder ()->get_global_symbols ();
21597 list_to_add = cu->list_in_scope;
21601 attr = dwarf2_attr (die, DW_AT_location, cu);
21604 var_decode_location (attr, sym, cu);
21605 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21607 /* Fortran explicitly imports any global symbols to the local
21608 scope by DW_TAG_common_block. */
21609 if (cu->language == language_fortran && die->parent
21610 && die->parent->tag == DW_TAG_common_block)
21613 if (SYMBOL_CLASS (sym) == LOC_STATIC
21614 && SYMBOL_VALUE_ADDRESS (sym) == 0
21615 && !dwarf2_per_objfile->has_section_at_zero)
21617 /* When a static variable is eliminated by the linker,
21618 the corresponding debug information is not stripped
21619 out, but the variable address is set to null;
21620 do not add such variables into symbol table. */
21622 else if (attr2 && (DW_UNSND (attr2) != 0))
21624 /* Workaround gfortran PR debug/40040 - it uses
21625 DW_AT_location for variables in -fPIC libraries which may
21626 get overriden by other libraries/executable and get
21627 a different address. Resolve it by the minimal symbol
21628 which may come from inferior's executable using copy
21629 relocation. Make this workaround only for gfortran as for
21630 other compilers GDB cannot guess the minimal symbol
21631 Fortran mangling kind. */
21632 if (cu->language == language_fortran && die->parent
21633 && die->parent->tag == DW_TAG_module
21635 && startswith (cu->producer, "GNU Fortran"))
21636 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21638 /* A variable with DW_AT_external is never static,
21639 but it may be block-scoped. */
21641 = ((cu->list_in_scope
21642 == cu->get_builder ()->get_file_symbols ())
21643 ? cu->get_builder ()->get_global_symbols ()
21644 : cu->list_in_scope);
21647 list_to_add = cu->list_in_scope;
21651 /* We do not know the address of this symbol.
21652 If it is an external symbol and we have type information
21653 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21654 The address of the variable will then be determined from
21655 the minimal symbol table whenever the variable is
21657 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21659 /* Fortran explicitly imports any global symbols to the local
21660 scope by DW_TAG_common_block. */
21661 if (cu->language == language_fortran && die->parent
21662 && die->parent->tag == DW_TAG_common_block)
21664 /* SYMBOL_CLASS doesn't matter here because
21665 read_common_block is going to reset it. */
21667 list_to_add = cu->list_in_scope;
21669 else if (attr2 && (DW_UNSND (attr2) != 0)
21670 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21672 /* A variable with DW_AT_external is never static, but it
21673 may be block-scoped. */
21675 = ((cu->list_in_scope
21676 == cu->get_builder ()->get_file_symbols ())
21677 ? cu->get_builder ()->get_global_symbols ()
21678 : cu->list_in_scope);
21680 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21682 else if (!die_is_declaration (die, cu))
21684 /* Use the default LOC_OPTIMIZED_OUT class. */
21685 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21687 list_to_add = cu->list_in_scope;
21691 case DW_TAG_formal_parameter:
21693 /* If we are inside a function, mark this as an argument. If
21694 not, we might be looking at an argument to an inlined function
21695 when we do not have enough information to show inlined frames;
21696 pretend it's a local variable in that case so that the user can
21698 struct context_stack *curr
21699 = cu->get_builder ()->get_current_context_stack ();
21700 if (curr != nullptr && curr->name != nullptr)
21701 SYMBOL_IS_ARGUMENT (sym) = 1;
21702 attr = dwarf2_attr (die, DW_AT_location, cu);
21705 var_decode_location (attr, sym, cu);
21707 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21710 dwarf2_const_value (attr, sym, cu);
21713 list_to_add = cu->list_in_scope;
21716 case DW_TAG_unspecified_parameters:
21717 /* From varargs functions; gdb doesn't seem to have any
21718 interest in this information, so just ignore it for now.
21721 case DW_TAG_template_type_param:
21723 /* Fall through. */
21724 case DW_TAG_class_type:
21725 case DW_TAG_interface_type:
21726 case DW_TAG_structure_type:
21727 case DW_TAG_union_type:
21728 case DW_TAG_set_type:
21729 case DW_TAG_enumeration_type:
21730 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21731 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21734 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21735 really ever be static objects: otherwise, if you try
21736 to, say, break of a class's method and you're in a file
21737 which doesn't mention that class, it won't work unless
21738 the check for all static symbols in lookup_symbol_aux
21739 saves you. See the OtherFileClass tests in
21740 gdb.c++/namespace.exp. */
21744 buildsym_compunit *builder = cu->get_builder ();
21746 = (cu->list_in_scope == builder->get_file_symbols ()
21747 && cu->language == language_cplus
21748 ? builder->get_global_symbols ()
21749 : cu->list_in_scope);
21751 /* The semantics of C++ state that "struct foo {
21752 ... }" also defines a typedef for "foo". */
21753 if (cu->language == language_cplus
21754 || cu->language == language_ada
21755 || cu->language == language_d
21756 || cu->language == language_rust)
21758 /* The symbol's name is already allocated along
21759 with this objfile, so we don't need to
21760 duplicate it for the type. */
21761 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21762 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21767 case DW_TAG_typedef:
21768 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21769 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21770 list_to_add = cu->list_in_scope;
21772 case DW_TAG_base_type:
21773 case DW_TAG_subrange_type:
21774 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21775 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21776 list_to_add = cu->list_in_scope;
21778 case DW_TAG_enumerator:
21779 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21782 dwarf2_const_value (attr, sym, cu);
21785 /* NOTE: carlton/2003-11-10: See comment above in the
21786 DW_TAG_class_type, etc. block. */
21789 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21790 && cu->language == language_cplus
21791 ? cu->get_builder ()->get_global_symbols ()
21792 : cu->list_in_scope);
21795 case DW_TAG_imported_declaration:
21796 case DW_TAG_namespace:
21797 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21798 list_to_add = cu->get_builder ()->get_global_symbols ();
21800 case DW_TAG_module:
21801 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21802 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21803 list_to_add = cu->get_builder ()->get_global_symbols ();
21805 case DW_TAG_common_block:
21806 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21807 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21808 add_symbol_to_list (sym, cu->list_in_scope);
21811 /* Not a tag we recognize. Hopefully we aren't processing
21812 trash data, but since we must specifically ignore things
21813 we don't recognize, there is nothing else we should do at
21815 complaint (_("unsupported tag: '%s'"),
21816 dwarf_tag_name (die->tag));
21822 sym->hash_next = objfile->template_symbols;
21823 objfile->template_symbols = sym;
21824 list_to_add = NULL;
21827 if (list_to_add != NULL)
21828 add_symbol_to_list (sym, list_to_add);
21830 /* For the benefit of old versions of GCC, check for anonymous
21831 namespaces based on the demangled name. */
21832 if (!cu->processing_has_namespace_info
21833 && cu->language == language_cplus)
21834 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21839 /* Given an attr with a DW_FORM_dataN value in host byte order,
21840 zero-extend it as appropriate for the symbol's type. The DWARF
21841 standard (v4) is not entirely clear about the meaning of using
21842 DW_FORM_dataN for a constant with a signed type, where the type is
21843 wider than the data. The conclusion of a discussion on the DWARF
21844 list was that this is unspecified. We choose to always zero-extend
21845 because that is the interpretation long in use by GCC. */
21848 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21849 struct dwarf2_cu *cu, LONGEST *value, int bits)
21851 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21852 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21853 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21854 LONGEST l = DW_UNSND (attr);
21856 if (bits < sizeof (*value) * 8)
21858 l &= ((LONGEST) 1 << bits) - 1;
21861 else if (bits == sizeof (*value) * 8)
21865 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21866 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21873 /* Read a constant value from an attribute. Either set *VALUE, or if
21874 the value does not fit in *VALUE, set *BYTES - either already
21875 allocated on the objfile obstack, or newly allocated on OBSTACK,
21876 or, set *BATON, if we translated the constant to a location
21880 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21881 const char *name, struct obstack *obstack,
21882 struct dwarf2_cu *cu,
21883 LONGEST *value, const gdb_byte **bytes,
21884 struct dwarf2_locexpr_baton **baton)
21886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21887 struct comp_unit_head *cu_header = &cu->header;
21888 struct dwarf_block *blk;
21889 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21890 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21896 switch (attr->form)
21899 case DW_FORM_addrx:
21900 case DW_FORM_GNU_addr_index:
21904 if (TYPE_LENGTH (type) != cu_header->addr_size)
21905 dwarf2_const_value_length_mismatch_complaint (name,
21906 cu_header->addr_size,
21907 TYPE_LENGTH (type));
21908 /* Symbols of this form are reasonably rare, so we just
21909 piggyback on the existing location code rather than writing
21910 a new implementation of symbol_computed_ops. */
21911 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21912 (*baton)->per_cu = cu->per_cu;
21913 gdb_assert ((*baton)->per_cu);
21915 (*baton)->size = 2 + cu_header->addr_size;
21916 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21917 (*baton)->data = data;
21919 data[0] = DW_OP_addr;
21920 store_unsigned_integer (&data[1], cu_header->addr_size,
21921 byte_order, DW_ADDR (attr));
21922 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21925 case DW_FORM_string:
21928 case DW_FORM_GNU_str_index:
21929 case DW_FORM_GNU_strp_alt:
21930 /* DW_STRING is already allocated on the objfile obstack, point
21932 *bytes = (const gdb_byte *) DW_STRING (attr);
21934 case DW_FORM_block1:
21935 case DW_FORM_block2:
21936 case DW_FORM_block4:
21937 case DW_FORM_block:
21938 case DW_FORM_exprloc:
21939 case DW_FORM_data16:
21940 blk = DW_BLOCK (attr);
21941 if (TYPE_LENGTH (type) != blk->size)
21942 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21943 TYPE_LENGTH (type));
21944 *bytes = blk->data;
21947 /* The DW_AT_const_value attributes are supposed to carry the
21948 symbol's value "represented as it would be on the target
21949 architecture." By the time we get here, it's already been
21950 converted to host endianness, so we just need to sign- or
21951 zero-extend it as appropriate. */
21952 case DW_FORM_data1:
21953 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21955 case DW_FORM_data2:
21956 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21958 case DW_FORM_data4:
21959 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21961 case DW_FORM_data8:
21962 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21965 case DW_FORM_sdata:
21966 case DW_FORM_implicit_const:
21967 *value = DW_SND (attr);
21970 case DW_FORM_udata:
21971 *value = DW_UNSND (attr);
21975 complaint (_("unsupported const value attribute form: '%s'"),
21976 dwarf_form_name (attr->form));
21983 /* Copy constant value from an attribute to a symbol. */
21986 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21987 struct dwarf2_cu *cu)
21989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21991 const gdb_byte *bytes;
21992 struct dwarf2_locexpr_baton *baton;
21994 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21995 SYMBOL_PRINT_NAME (sym),
21996 &objfile->objfile_obstack, cu,
21997 &value, &bytes, &baton);
22001 SYMBOL_LOCATION_BATON (sym) = baton;
22002 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22004 else if (bytes != NULL)
22006 SYMBOL_VALUE_BYTES (sym) = bytes;
22007 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22011 SYMBOL_VALUE (sym) = value;
22012 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22016 /* Return the type of the die in question using its DW_AT_type attribute. */
22018 static struct type *
22019 die_type (struct die_info *die, struct dwarf2_cu *cu)
22021 struct attribute *type_attr;
22023 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22027 /* A missing DW_AT_type represents a void type. */
22028 return objfile_type (objfile)->builtin_void;
22031 return lookup_die_type (die, type_attr, cu);
22034 /* True iff CU's producer generates GNAT Ada auxiliary information
22035 that allows to find parallel types through that information instead
22036 of having to do expensive parallel lookups by type name. */
22039 need_gnat_info (struct dwarf2_cu *cu)
22041 /* Assume that the Ada compiler was GNAT, which always produces
22042 the auxiliary information. */
22043 return (cu->language == language_ada);
22046 /* Return the auxiliary type of the die in question using its
22047 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22048 attribute is not present. */
22050 static struct type *
22051 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22053 struct attribute *type_attr;
22055 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22059 return lookup_die_type (die, type_attr, cu);
22062 /* If DIE has a descriptive_type attribute, then set the TYPE's
22063 descriptive type accordingly. */
22066 set_descriptive_type (struct type *type, struct die_info *die,
22067 struct dwarf2_cu *cu)
22069 struct type *descriptive_type = die_descriptive_type (die, cu);
22071 if (descriptive_type)
22073 ALLOCATE_GNAT_AUX_TYPE (type);
22074 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22078 /* Return the containing type of the die in question using its
22079 DW_AT_containing_type attribute. */
22081 static struct type *
22082 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22084 struct attribute *type_attr;
22085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22087 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22089 error (_("Dwarf Error: Problem turning containing type into gdb type "
22090 "[in module %s]"), objfile_name (objfile));
22092 return lookup_die_type (die, type_attr, cu);
22095 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22097 static struct type *
22098 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22100 struct dwarf2_per_objfile *dwarf2_per_objfile
22101 = cu->per_cu->dwarf2_per_objfile;
22102 struct objfile *objfile = dwarf2_per_objfile->objfile;
22105 std::string message
22106 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22107 objfile_name (objfile),
22108 sect_offset_str (cu->header.sect_off),
22109 sect_offset_str (die->sect_off));
22110 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
22111 message.c_str (), message.length ());
22113 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22116 /* Look up the type of DIE in CU using its type attribute ATTR.
22117 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22118 DW_AT_containing_type.
22119 If there is no type substitute an error marker. */
22121 static struct type *
22122 lookup_die_type (struct die_info *die, const struct attribute *attr,
22123 struct dwarf2_cu *cu)
22125 struct dwarf2_per_objfile *dwarf2_per_objfile
22126 = cu->per_cu->dwarf2_per_objfile;
22127 struct objfile *objfile = dwarf2_per_objfile->objfile;
22128 struct type *this_type;
22130 gdb_assert (attr->name == DW_AT_type
22131 || attr->name == DW_AT_GNAT_descriptive_type
22132 || attr->name == DW_AT_containing_type);
22134 /* First see if we have it cached. */
22136 if (attr->form == DW_FORM_GNU_ref_alt)
22138 struct dwarf2_per_cu_data *per_cu;
22139 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22141 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22142 dwarf2_per_objfile);
22143 this_type = get_die_type_at_offset (sect_off, per_cu);
22145 else if (attr_form_is_ref (attr))
22147 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22149 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22151 else if (attr->form == DW_FORM_ref_sig8)
22153 ULONGEST signature = DW_SIGNATURE (attr);
22155 return get_signatured_type (die, signature, cu);
22159 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22160 " at %s [in module %s]"),
22161 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22162 objfile_name (objfile));
22163 return build_error_marker_type (cu, die);
22166 /* If not cached we need to read it in. */
22168 if (this_type == NULL)
22170 struct die_info *type_die = NULL;
22171 struct dwarf2_cu *type_cu = cu;
22173 if (attr_form_is_ref (attr))
22174 type_die = follow_die_ref (die, attr, &type_cu);
22175 if (type_die == NULL)
22176 return build_error_marker_type (cu, die);
22177 /* If we find the type now, it's probably because the type came
22178 from an inter-CU reference and the type's CU got expanded before
22180 this_type = read_type_die (type_die, type_cu);
22183 /* If we still don't have a type use an error marker. */
22185 if (this_type == NULL)
22186 return build_error_marker_type (cu, die);
22191 /* Return the type in DIE, CU.
22192 Returns NULL for invalid types.
22194 This first does a lookup in die_type_hash,
22195 and only reads the die in if necessary.
22197 NOTE: This can be called when reading in partial or full symbols. */
22199 static struct type *
22200 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22202 struct type *this_type;
22204 this_type = get_die_type (die, cu);
22208 return read_type_die_1 (die, cu);
22211 /* Read the type in DIE, CU.
22212 Returns NULL for invalid types. */
22214 static struct type *
22215 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22217 struct type *this_type = NULL;
22221 case DW_TAG_class_type:
22222 case DW_TAG_interface_type:
22223 case DW_TAG_structure_type:
22224 case DW_TAG_union_type:
22225 this_type = read_structure_type (die, cu);
22227 case DW_TAG_enumeration_type:
22228 this_type = read_enumeration_type (die, cu);
22230 case DW_TAG_subprogram:
22231 case DW_TAG_subroutine_type:
22232 case DW_TAG_inlined_subroutine:
22233 this_type = read_subroutine_type (die, cu);
22235 case DW_TAG_array_type:
22236 this_type = read_array_type (die, cu);
22238 case DW_TAG_set_type:
22239 this_type = read_set_type (die, cu);
22241 case DW_TAG_pointer_type:
22242 this_type = read_tag_pointer_type (die, cu);
22244 case DW_TAG_ptr_to_member_type:
22245 this_type = read_tag_ptr_to_member_type (die, cu);
22247 case DW_TAG_reference_type:
22248 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22250 case DW_TAG_rvalue_reference_type:
22251 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22253 case DW_TAG_const_type:
22254 this_type = read_tag_const_type (die, cu);
22256 case DW_TAG_volatile_type:
22257 this_type = read_tag_volatile_type (die, cu);
22259 case DW_TAG_restrict_type:
22260 this_type = read_tag_restrict_type (die, cu);
22262 case DW_TAG_string_type:
22263 this_type = read_tag_string_type (die, cu);
22265 case DW_TAG_typedef:
22266 this_type = read_typedef (die, cu);
22268 case DW_TAG_subrange_type:
22269 this_type = read_subrange_type (die, cu);
22271 case DW_TAG_base_type:
22272 this_type = read_base_type (die, cu);
22274 case DW_TAG_unspecified_type:
22275 this_type = read_unspecified_type (die, cu);
22277 case DW_TAG_namespace:
22278 this_type = read_namespace_type (die, cu);
22280 case DW_TAG_module:
22281 this_type = read_module_type (die, cu);
22283 case DW_TAG_atomic_type:
22284 this_type = read_tag_atomic_type (die, cu);
22287 complaint (_("unexpected tag in read_type_die: '%s'"),
22288 dwarf_tag_name (die->tag));
22295 /* See if we can figure out if the class lives in a namespace. We do
22296 this by looking for a member function; its demangled name will
22297 contain namespace info, if there is any.
22298 Return the computed name or NULL.
22299 Space for the result is allocated on the objfile's obstack.
22300 This is the full-die version of guess_partial_die_structure_name.
22301 In this case we know DIE has no useful parent. */
22304 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22306 struct die_info *spec_die;
22307 struct dwarf2_cu *spec_cu;
22308 struct die_info *child;
22309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22312 spec_die = die_specification (die, &spec_cu);
22313 if (spec_die != NULL)
22319 for (child = die->child;
22321 child = child->sibling)
22323 if (child->tag == DW_TAG_subprogram)
22325 const char *linkage_name = dw2_linkage_name (child, cu);
22327 if (linkage_name != NULL)
22330 = language_class_name_from_physname (cu->language_defn,
22334 if (actual_name != NULL)
22336 const char *die_name = dwarf2_name (die, cu);
22338 if (die_name != NULL
22339 && strcmp (die_name, actual_name) != 0)
22341 /* Strip off the class name from the full name.
22342 We want the prefix. */
22343 int die_name_len = strlen (die_name);
22344 int actual_name_len = strlen (actual_name);
22346 /* Test for '::' as a sanity check. */
22347 if (actual_name_len > die_name_len + 2
22348 && actual_name[actual_name_len
22349 - die_name_len - 1] == ':')
22350 name = (char *) obstack_copy0 (
22351 &objfile->per_bfd->storage_obstack,
22352 actual_name, actual_name_len - die_name_len - 2);
22355 xfree (actual_name);
22364 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22365 prefix part in such case. See
22366 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22368 static const char *
22369 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22371 struct attribute *attr;
22374 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22375 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22378 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22381 attr = dw2_linkage_name_attr (die, cu);
22382 if (attr == NULL || DW_STRING (attr) == NULL)
22385 /* dwarf2_name had to be already called. */
22386 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22388 /* Strip the base name, keep any leading namespaces/classes. */
22389 base = strrchr (DW_STRING (attr), ':');
22390 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22393 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22394 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22396 &base[-1] - DW_STRING (attr));
22399 /* Return the name of the namespace/class that DIE is defined within,
22400 or "" if we can't tell. The caller should not xfree the result.
22402 For example, if we're within the method foo() in the following
22412 then determine_prefix on foo's die will return "N::C". */
22414 static const char *
22415 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22417 struct dwarf2_per_objfile *dwarf2_per_objfile
22418 = cu->per_cu->dwarf2_per_objfile;
22419 struct die_info *parent, *spec_die;
22420 struct dwarf2_cu *spec_cu;
22421 struct type *parent_type;
22422 const char *retval;
22424 if (cu->language != language_cplus
22425 && cu->language != language_fortran && cu->language != language_d
22426 && cu->language != language_rust)
22429 retval = anonymous_struct_prefix (die, cu);
22433 /* We have to be careful in the presence of DW_AT_specification.
22434 For example, with GCC 3.4, given the code
22438 // Definition of N::foo.
22442 then we'll have a tree of DIEs like this:
22444 1: DW_TAG_compile_unit
22445 2: DW_TAG_namespace // N
22446 3: DW_TAG_subprogram // declaration of N::foo
22447 4: DW_TAG_subprogram // definition of N::foo
22448 DW_AT_specification // refers to die #3
22450 Thus, when processing die #4, we have to pretend that we're in
22451 the context of its DW_AT_specification, namely the contex of die
22454 spec_die = die_specification (die, &spec_cu);
22455 if (spec_die == NULL)
22456 parent = die->parent;
22459 parent = spec_die->parent;
22463 if (parent == NULL)
22465 else if (parent->building_fullname)
22468 const char *parent_name;
22470 /* It has been seen on RealView 2.2 built binaries,
22471 DW_TAG_template_type_param types actually _defined_ as
22472 children of the parent class:
22475 template class <class Enum> Class{};
22476 Class<enum E> class_e;
22478 1: DW_TAG_class_type (Class)
22479 2: DW_TAG_enumeration_type (E)
22480 3: DW_TAG_enumerator (enum1:0)
22481 3: DW_TAG_enumerator (enum2:1)
22483 2: DW_TAG_template_type_param
22484 DW_AT_type DW_FORM_ref_udata (E)
22486 Besides being broken debug info, it can put GDB into an
22487 infinite loop. Consider:
22489 When we're building the full name for Class<E>, we'll start
22490 at Class, and go look over its template type parameters,
22491 finding E. We'll then try to build the full name of E, and
22492 reach here. We're now trying to build the full name of E,
22493 and look over the parent DIE for containing scope. In the
22494 broken case, if we followed the parent DIE of E, we'd again
22495 find Class, and once again go look at its template type
22496 arguments, etc., etc. Simply don't consider such parent die
22497 as source-level parent of this die (it can't be, the language
22498 doesn't allow it), and break the loop here. */
22499 name = dwarf2_name (die, cu);
22500 parent_name = dwarf2_name (parent, cu);
22501 complaint (_("template param type '%s' defined within parent '%s'"),
22502 name ? name : "<unknown>",
22503 parent_name ? parent_name : "<unknown>");
22507 switch (parent->tag)
22509 case DW_TAG_namespace:
22510 parent_type = read_type_die (parent, cu);
22511 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22512 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22513 Work around this problem here. */
22514 if (cu->language == language_cplus
22515 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22517 /* We give a name to even anonymous namespaces. */
22518 return TYPE_NAME (parent_type);
22519 case DW_TAG_class_type:
22520 case DW_TAG_interface_type:
22521 case DW_TAG_structure_type:
22522 case DW_TAG_union_type:
22523 case DW_TAG_module:
22524 parent_type = read_type_die (parent, cu);
22525 if (TYPE_NAME (parent_type) != NULL)
22526 return TYPE_NAME (parent_type);
22528 /* An anonymous structure is only allowed non-static data
22529 members; no typedefs, no member functions, et cetera.
22530 So it does not need a prefix. */
22532 case DW_TAG_compile_unit:
22533 case DW_TAG_partial_unit:
22534 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22535 if (cu->language == language_cplus
22536 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22537 && die->child != NULL
22538 && (die->tag == DW_TAG_class_type
22539 || die->tag == DW_TAG_structure_type
22540 || die->tag == DW_TAG_union_type))
22542 char *name = guess_full_die_structure_name (die, cu);
22547 case DW_TAG_enumeration_type:
22548 parent_type = read_type_die (parent, cu);
22549 if (TYPE_DECLARED_CLASS (parent_type))
22551 if (TYPE_NAME (parent_type) != NULL)
22552 return TYPE_NAME (parent_type);
22555 /* Fall through. */
22557 return determine_prefix (parent, cu);
22561 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22562 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22563 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22564 an obconcat, otherwise allocate storage for the result. The CU argument is
22565 used to determine the language and hence, the appropriate separator. */
22567 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22570 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22571 int physname, struct dwarf2_cu *cu)
22573 const char *lead = "";
22576 if (suffix == NULL || suffix[0] == '\0'
22577 || prefix == NULL || prefix[0] == '\0')
22579 else if (cu->language == language_d)
22581 /* For D, the 'main' function could be defined in any module, but it
22582 should never be prefixed. */
22583 if (strcmp (suffix, "D main") == 0)
22591 else if (cu->language == language_fortran && physname)
22593 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22594 DW_AT_MIPS_linkage_name is preferred and used instead. */
22602 if (prefix == NULL)
22604 if (suffix == NULL)
22611 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22613 strcpy (retval, lead);
22614 strcat (retval, prefix);
22615 strcat (retval, sep);
22616 strcat (retval, suffix);
22621 /* We have an obstack. */
22622 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22626 /* Return sibling of die, NULL if no sibling. */
22628 static struct die_info *
22629 sibling_die (struct die_info *die)
22631 return die->sibling;
22634 /* Get name of a die, return NULL if not found. */
22636 static const char *
22637 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22638 struct obstack *obstack)
22640 if (name && cu->language == language_cplus)
22642 std::string canon_name = cp_canonicalize_string (name);
22644 if (!canon_name.empty ())
22646 if (canon_name != name)
22647 name = (const char *) obstack_copy0 (obstack,
22648 canon_name.c_str (),
22649 canon_name.length ());
22656 /* Get name of a die, return NULL if not found.
22657 Anonymous namespaces are converted to their magic string. */
22659 static const char *
22660 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22662 struct attribute *attr;
22663 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22665 attr = dwarf2_attr (die, DW_AT_name, cu);
22666 if ((!attr || !DW_STRING (attr))
22667 && die->tag != DW_TAG_namespace
22668 && die->tag != DW_TAG_class_type
22669 && die->tag != DW_TAG_interface_type
22670 && die->tag != DW_TAG_structure_type
22671 && die->tag != DW_TAG_union_type)
22676 case DW_TAG_compile_unit:
22677 case DW_TAG_partial_unit:
22678 /* Compilation units have a DW_AT_name that is a filename, not
22679 a source language identifier. */
22680 case DW_TAG_enumeration_type:
22681 case DW_TAG_enumerator:
22682 /* These tags always have simple identifiers already; no need
22683 to canonicalize them. */
22684 return DW_STRING (attr);
22686 case DW_TAG_namespace:
22687 if (attr != NULL && DW_STRING (attr) != NULL)
22688 return DW_STRING (attr);
22689 return CP_ANONYMOUS_NAMESPACE_STR;
22691 case DW_TAG_class_type:
22692 case DW_TAG_interface_type:
22693 case DW_TAG_structure_type:
22694 case DW_TAG_union_type:
22695 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22696 structures or unions. These were of the form "._%d" in GCC 4.1,
22697 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22698 and GCC 4.4. We work around this problem by ignoring these. */
22699 if (attr && DW_STRING (attr)
22700 && (startswith (DW_STRING (attr), "._")
22701 || startswith (DW_STRING (attr), "<anonymous")))
22704 /* GCC might emit a nameless typedef that has a linkage name. See
22705 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22706 if (!attr || DW_STRING (attr) == NULL)
22708 char *demangled = NULL;
22710 attr = dw2_linkage_name_attr (die, cu);
22711 if (attr == NULL || DW_STRING (attr) == NULL)
22714 /* Avoid demangling DW_STRING (attr) the second time on a second
22715 call for the same DIE. */
22716 if (!DW_STRING_IS_CANONICAL (attr))
22717 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22723 /* FIXME: we already did this for the partial symbol... */
22726 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22727 demangled, strlen (demangled)));
22728 DW_STRING_IS_CANONICAL (attr) = 1;
22731 /* Strip any leading namespaces/classes, keep only the base name.
22732 DW_AT_name for named DIEs does not contain the prefixes. */
22733 base = strrchr (DW_STRING (attr), ':');
22734 if (base && base > DW_STRING (attr) && base[-1] == ':')
22737 return DW_STRING (attr);
22746 if (!DW_STRING_IS_CANONICAL (attr))
22749 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22750 &objfile->per_bfd->storage_obstack);
22751 DW_STRING_IS_CANONICAL (attr) = 1;
22753 return DW_STRING (attr);
22756 /* Return the die that this die in an extension of, or NULL if there
22757 is none. *EXT_CU is the CU containing DIE on input, and the CU
22758 containing the return value on output. */
22760 static struct die_info *
22761 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22763 struct attribute *attr;
22765 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22769 return follow_die_ref (die, attr, ext_cu);
22772 /* A convenience function that returns an "unknown" DWARF name,
22773 including the value of V. STR is the name of the entity being
22774 printed, e.g., "TAG". */
22776 static const char *
22777 dwarf_unknown (const char *str, unsigned v)
22779 char *cell = get_print_cell ();
22780 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22784 /* Convert a DIE tag into its string name. */
22786 static const char *
22787 dwarf_tag_name (unsigned tag)
22789 const char *name = get_DW_TAG_name (tag);
22792 return dwarf_unknown ("TAG", tag);
22797 /* Convert a DWARF attribute code into its string name. */
22799 static const char *
22800 dwarf_attr_name (unsigned attr)
22804 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22805 if (attr == DW_AT_MIPS_fde)
22806 return "DW_AT_MIPS_fde";
22808 if (attr == DW_AT_HP_block_index)
22809 return "DW_AT_HP_block_index";
22812 name = get_DW_AT_name (attr);
22815 return dwarf_unknown ("AT", attr);
22820 /* Convert a DWARF value form code into its string name. */
22822 static const char *
22823 dwarf_form_name (unsigned form)
22825 const char *name = get_DW_FORM_name (form);
22828 return dwarf_unknown ("FORM", form);
22833 static const char *
22834 dwarf_bool_name (unsigned mybool)
22842 /* Convert a DWARF type code into its string name. */
22844 static const char *
22845 dwarf_type_encoding_name (unsigned enc)
22847 const char *name = get_DW_ATE_name (enc);
22850 return dwarf_unknown ("ATE", enc);
22856 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22860 print_spaces (indent, f);
22861 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22862 dwarf_tag_name (die->tag), die->abbrev,
22863 sect_offset_str (die->sect_off));
22865 if (die->parent != NULL)
22867 print_spaces (indent, f);
22868 fprintf_unfiltered (f, " parent at offset: %s\n",
22869 sect_offset_str (die->parent->sect_off));
22872 print_spaces (indent, f);
22873 fprintf_unfiltered (f, " has children: %s\n",
22874 dwarf_bool_name (die->child != NULL));
22876 print_spaces (indent, f);
22877 fprintf_unfiltered (f, " attributes:\n");
22879 for (i = 0; i < die->num_attrs; ++i)
22881 print_spaces (indent, f);
22882 fprintf_unfiltered (f, " %s (%s) ",
22883 dwarf_attr_name (die->attrs[i].name),
22884 dwarf_form_name (die->attrs[i].form));
22886 switch (die->attrs[i].form)
22889 case DW_FORM_addrx:
22890 case DW_FORM_GNU_addr_index:
22891 fprintf_unfiltered (f, "address: ");
22892 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22894 case DW_FORM_block2:
22895 case DW_FORM_block4:
22896 case DW_FORM_block:
22897 case DW_FORM_block1:
22898 fprintf_unfiltered (f, "block: size %s",
22899 pulongest (DW_BLOCK (&die->attrs[i])->size));
22901 case DW_FORM_exprloc:
22902 fprintf_unfiltered (f, "expression: size %s",
22903 pulongest (DW_BLOCK (&die->attrs[i])->size));
22905 case DW_FORM_data16:
22906 fprintf_unfiltered (f, "constant of 16 bytes");
22908 case DW_FORM_ref_addr:
22909 fprintf_unfiltered (f, "ref address: ");
22910 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22912 case DW_FORM_GNU_ref_alt:
22913 fprintf_unfiltered (f, "alt ref address: ");
22914 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22920 case DW_FORM_ref_udata:
22921 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22922 (long) (DW_UNSND (&die->attrs[i])));
22924 case DW_FORM_data1:
22925 case DW_FORM_data2:
22926 case DW_FORM_data4:
22927 case DW_FORM_data8:
22928 case DW_FORM_udata:
22929 case DW_FORM_sdata:
22930 fprintf_unfiltered (f, "constant: %s",
22931 pulongest (DW_UNSND (&die->attrs[i])));
22933 case DW_FORM_sec_offset:
22934 fprintf_unfiltered (f, "section offset: %s",
22935 pulongest (DW_UNSND (&die->attrs[i])));
22937 case DW_FORM_ref_sig8:
22938 fprintf_unfiltered (f, "signature: %s",
22939 hex_string (DW_SIGNATURE (&die->attrs[i])));
22941 case DW_FORM_string:
22943 case DW_FORM_line_strp:
22945 case DW_FORM_GNU_str_index:
22946 case DW_FORM_GNU_strp_alt:
22947 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22948 DW_STRING (&die->attrs[i])
22949 ? DW_STRING (&die->attrs[i]) : "",
22950 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22953 if (DW_UNSND (&die->attrs[i]))
22954 fprintf_unfiltered (f, "flag: TRUE");
22956 fprintf_unfiltered (f, "flag: FALSE");
22958 case DW_FORM_flag_present:
22959 fprintf_unfiltered (f, "flag: TRUE");
22961 case DW_FORM_indirect:
22962 /* The reader will have reduced the indirect form to
22963 the "base form" so this form should not occur. */
22964 fprintf_unfiltered (f,
22965 "unexpected attribute form: DW_FORM_indirect");
22967 case DW_FORM_implicit_const:
22968 fprintf_unfiltered (f, "constant: %s",
22969 plongest (DW_SND (&die->attrs[i])));
22972 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22973 die->attrs[i].form);
22976 fprintf_unfiltered (f, "\n");
22981 dump_die_for_error (struct die_info *die)
22983 dump_die_shallow (gdb_stderr, 0, die);
22987 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22989 int indent = level * 4;
22991 gdb_assert (die != NULL);
22993 if (level >= max_level)
22996 dump_die_shallow (f, indent, die);
22998 if (die->child != NULL)
23000 print_spaces (indent, f);
23001 fprintf_unfiltered (f, " Children:");
23002 if (level + 1 < max_level)
23004 fprintf_unfiltered (f, "\n");
23005 dump_die_1 (f, level + 1, max_level, die->child);
23009 fprintf_unfiltered (f,
23010 " [not printed, max nesting level reached]\n");
23014 if (die->sibling != NULL && level > 0)
23016 dump_die_1 (f, level, max_level, die->sibling);
23020 /* This is called from the pdie macro in gdbinit.in.
23021 It's not static so gcc will keep a copy callable from gdb. */
23024 dump_die (struct die_info *die, int max_level)
23026 dump_die_1 (gdb_stdlog, 0, max_level, die);
23030 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23034 slot = htab_find_slot_with_hash (cu->die_hash, die,
23035 to_underlying (die->sect_off),
23041 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23045 dwarf2_get_ref_die_offset (const struct attribute *attr)
23047 if (attr_form_is_ref (attr))
23048 return (sect_offset) DW_UNSND (attr);
23050 complaint (_("unsupported die ref attribute form: '%s'"),
23051 dwarf_form_name (attr->form));
23055 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23056 * the value held by the attribute is not constant. */
23059 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23061 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23062 return DW_SND (attr);
23063 else if (attr->form == DW_FORM_udata
23064 || attr->form == DW_FORM_data1
23065 || attr->form == DW_FORM_data2
23066 || attr->form == DW_FORM_data4
23067 || attr->form == DW_FORM_data8)
23068 return DW_UNSND (attr);
23071 /* For DW_FORM_data16 see attr_form_is_constant. */
23072 complaint (_("Attribute value is not a constant (%s)"),
23073 dwarf_form_name (attr->form));
23074 return default_value;
23078 /* Follow reference or signature attribute ATTR of SRC_DIE.
23079 On entry *REF_CU is the CU of SRC_DIE.
23080 On exit *REF_CU is the CU of the result. */
23082 static struct die_info *
23083 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23084 struct dwarf2_cu **ref_cu)
23086 struct die_info *die;
23088 if (attr_form_is_ref (attr))
23089 die = follow_die_ref (src_die, attr, ref_cu);
23090 else if (attr->form == DW_FORM_ref_sig8)
23091 die = follow_die_sig (src_die, attr, ref_cu);
23094 dump_die_for_error (src_die);
23095 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23096 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23102 /* Follow reference OFFSET.
23103 On entry *REF_CU is the CU of the source die referencing OFFSET.
23104 On exit *REF_CU is the CU of the result.
23105 Returns NULL if OFFSET is invalid. */
23107 static struct die_info *
23108 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23109 struct dwarf2_cu **ref_cu)
23111 struct die_info temp_die;
23112 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23113 struct dwarf2_per_objfile *dwarf2_per_objfile
23114 = cu->per_cu->dwarf2_per_objfile;
23116 gdb_assert (cu->per_cu != NULL);
23120 if (cu->per_cu->is_debug_types)
23122 /* .debug_types CUs cannot reference anything outside their CU.
23123 If they need to, they have to reference a signatured type via
23124 DW_FORM_ref_sig8. */
23125 if (!offset_in_cu_p (&cu->header, sect_off))
23128 else if (offset_in_dwz != cu->per_cu->is_dwz
23129 || !offset_in_cu_p (&cu->header, sect_off))
23131 struct dwarf2_per_cu_data *per_cu;
23133 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23134 dwarf2_per_objfile);
23136 /* If necessary, add it to the queue and load its DIEs. */
23137 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23138 load_full_comp_unit (per_cu, false, cu->language);
23140 target_cu = per_cu->cu;
23142 else if (cu->dies == NULL)
23144 /* We're loading full DIEs during partial symbol reading. */
23145 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23146 load_full_comp_unit (cu->per_cu, false, language_minimal);
23149 *ref_cu = target_cu;
23150 temp_die.sect_off = sect_off;
23152 if (target_cu != cu)
23153 target_cu->ancestor = cu;
23155 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23157 to_underlying (sect_off));
23160 /* Follow reference attribute ATTR of SRC_DIE.
23161 On entry *REF_CU is the CU of SRC_DIE.
23162 On exit *REF_CU is the CU of the result. */
23164 static struct die_info *
23165 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23166 struct dwarf2_cu **ref_cu)
23168 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23169 struct dwarf2_cu *cu = *ref_cu;
23170 struct die_info *die;
23172 die = follow_die_offset (sect_off,
23173 (attr->form == DW_FORM_GNU_ref_alt
23174 || cu->per_cu->is_dwz),
23177 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23178 "at %s [in module %s]"),
23179 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23180 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23185 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23186 Returned value is intended for DW_OP_call*. Returned
23187 dwarf2_locexpr_baton->data has lifetime of
23188 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23190 struct dwarf2_locexpr_baton
23191 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23192 struct dwarf2_per_cu_data *per_cu,
23193 CORE_ADDR (*get_frame_pc) (void *baton),
23194 void *baton, bool resolve_abstract_p)
23196 struct dwarf2_cu *cu;
23197 struct die_info *die;
23198 struct attribute *attr;
23199 struct dwarf2_locexpr_baton retval;
23200 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23201 struct objfile *objfile = dwarf2_per_objfile->objfile;
23203 if (per_cu->cu == NULL)
23204 load_cu (per_cu, false);
23208 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23209 Instead just throw an error, not much else we can do. */
23210 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23211 sect_offset_str (sect_off), objfile_name (objfile));
23214 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23216 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23217 sect_offset_str (sect_off), objfile_name (objfile));
23219 attr = dwarf2_attr (die, DW_AT_location, cu);
23220 if (!attr && resolve_abstract_p
23221 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23222 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23224 CORE_ADDR pc = (*get_frame_pc) (baton);
23226 for (const auto &cand_off
23227 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23229 struct dwarf2_cu *cand_cu = cu;
23230 struct die_info *cand
23231 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23234 || cand->parent->tag != DW_TAG_subprogram)
23237 CORE_ADDR pc_low, pc_high;
23238 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23239 if (pc_low == ((CORE_ADDR) -1)
23240 || !(pc_low <= pc && pc < pc_high))
23244 attr = dwarf2_attr (die, DW_AT_location, cu);
23251 /* DWARF: "If there is no such attribute, then there is no effect.".
23252 DATA is ignored if SIZE is 0. */
23254 retval.data = NULL;
23257 else if (attr_form_is_section_offset (attr))
23259 struct dwarf2_loclist_baton loclist_baton;
23260 CORE_ADDR pc = (*get_frame_pc) (baton);
23263 fill_in_loclist_baton (cu, &loclist_baton, attr);
23265 retval.data = dwarf2_find_location_expression (&loclist_baton,
23267 retval.size = size;
23271 if (!attr_form_is_block (attr))
23272 error (_("Dwarf Error: DIE at %s referenced in module %s "
23273 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23274 sect_offset_str (sect_off), objfile_name (objfile));
23276 retval.data = DW_BLOCK (attr)->data;
23277 retval.size = DW_BLOCK (attr)->size;
23279 retval.per_cu = cu->per_cu;
23281 age_cached_comp_units (dwarf2_per_objfile);
23286 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23289 struct dwarf2_locexpr_baton
23290 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23291 struct dwarf2_per_cu_data *per_cu,
23292 CORE_ADDR (*get_frame_pc) (void *baton),
23295 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23297 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23300 /* Write a constant of a given type as target-ordered bytes into
23303 static const gdb_byte *
23304 write_constant_as_bytes (struct obstack *obstack,
23305 enum bfd_endian byte_order,
23312 *len = TYPE_LENGTH (type);
23313 result = (gdb_byte *) obstack_alloc (obstack, *len);
23314 store_unsigned_integer (result, *len, byte_order, value);
23319 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23320 pointer to the constant bytes and set LEN to the length of the
23321 data. If memory is needed, allocate it on OBSTACK. If the DIE
23322 does not have a DW_AT_const_value, return NULL. */
23325 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23326 struct dwarf2_per_cu_data *per_cu,
23327 struct obstack *obstack,
23330 struct dwarf2_cu *cu;
23331 struct die_info *die;
23332 struct attribute *attr;
23333 const gdb_byte *result = NULL;
23336 enum bfd_endian byte_order;
23337 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23339 if (per_cu->cu == NULL)
23340 load_cu (per_cu, false);
23344 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23345 Instead just throw an error, not much else we can do. */
23346 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23347 sect_offset_str (sect_off), objfile_name (objfile));
23350 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23352 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23353 sect_offset_str (sect_off), objfile_name (objfile));
23355 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23359 byte_order = (bfd_big_endian (objfile->obfd)
23360 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23362 switch (attr->form)
23365 case DW_FORM_addrx:
23366 case DW_FORM_GNU_addr_index:
23370 *len = cu->header.addr_size;
23371 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23372 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23376 case DW_FORM_string:
23379 case DW_FORM_GNU_str_index:
23380 case DW_FORM_GNU_strp_alt:
23381 /* DW_STRING is already allocated on the objfile obstack, point
23383 result = (const gdb_byte *) DW_STRING (attr);
23384 *len = strlen (DW_STRING (attr));
23386 case DW_FORM_block1:
23387 case DW_FORM_block2:
23388 case DW_FORM_block4:
23389 case DW_FORM_block:
23390 case DW_FORM_exprloc:
23391 case DW_FORM_data16:
23392 result = DW_BLOCK (attr)->data;
23393 *len = DW_BLOCK (attr)->size;
23396 /* The DW_AT_const_value attributes are supposed to carry the
23397 symbol's value "represented as it would be on the target
23398 architecture." By the time we get here, it's already been
23399 converted to host endianness, so we just need to sign- or
23400 zero-extend it as appropriate. */
23401 case DW_FORM_data1:
23402 type = die_type (die, cu);
23403 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23404 if (result == NULL)
23405 result = write_constant_as_bytes (obstack, byte_order,
23408 case DW_FORM_data2:
23409 type = die_type (die, cu);
23410 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23411 if (result == NULL)
23412 result = write_constant_as_bytes (obstack, byte_order,
23415 case DW_FORM_data4:
23416 type = die_type (die, cu);
23417 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23418 if (result == NULL)
23419 result = write_constant_as_bytes (obstack, byte_order,
23422 case DW_FORM_data8:
23423 type = die_type (die, cu);
23424 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23425 if (result == NULL)
23426 result = write_constant_as_bytes (obstack, byte_order,
23430 case DW_FORM_sdata:
23431 case DW_FORM_implicit_const:
23432 type = die_type (die, cu);
23433 result = write_constant_as_bytes (obstack, byte_order,
23434 type, DW_SND (attr), len);
23437 case DW_FORM_udata:
23438 type = die_type (die, cu);
23439 result = write_constant_as_bytes (obstack, byte_order,
23440 type, DW_UNSND (attr), len);
23444 complaint (_("unsupported const value attribute form: '%s'"),
23445 dwarf_form_name (attr->form));
23452 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23453 valid type for this die is found. */
23456 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23457 struct dwarf2_per_cu_data *per_cu)
23459 struct dwarf2_cu *cu;
23460 struct die_info *die;
23462 if (per_cu->cu == NULL)
23463 load_cu (per_cu, false);
23468 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23472 return die_type (die, cu);
23475 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23479 dwarf2_get_die_type (cu_offset die_offset,
23480 struct dwarf2_per_cu_data *per_cu)
23482 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23483 return get_die_type_at_offset (die_offset_sect, per_cu);
23486 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23487 On entry *REF_CU is the CU of SRC_DIE.
23488 On exit *REF_CU is the CU of the result.
23489 Returns NULL if the referenced DIE isn't found. */
23491 static struct die_info *
23492 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23493 struct dwarf2_cu **ref_cu)
23495 struct die_info temp_die;
23496 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23497 struct die_info *die;
23499 /* While it might be nice to assert sig_type->type == NULL here,
23500 we can get here for DW_AT_imported_declaration where we need
23501 the DIE not the type. */
23503 /* If necessary, add it to the queue and load its DIEs. */
23505 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23506 read_signatured_type (sig_type);
23508 sig_cu = sig_type->per_cu.cu;
23509 gdb_assert (sig_cu != NULL);
23510 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23511 temp_die.sect_off = sig_type->type_offset_in_section;
23512 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23513 to_underlying (temp_die.sect_off));
23516 struct dwarf2_per_objfile *dwarf2_per_objfile
23517 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23519 /* For .gdb_index version 7 keep track of included TUs.
23520 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23521 if (dwarf2_per_objfile->index_table != NULL
23522 && dwarf2_per_objfile->index_table->version <= 7)
23524 VEC_safe_push (dwarf2_per_cu_ptr,
23525 (*ref_cu)->per_cu->imported_symtabs,
23531 sig_cu->ancestor = cu;
23539 /* Follow signatured type referenced by ATTR in SRC_DIE.
23540 On entry *REF_CU is the CU of SRC_DIE.
23541 On exit *REF_CU is the CU of the result.
23542 The result is the DIE of the type.
23543 If the referenced type cannot be found an error is thrown. */
23545 static struct die_info *
23546 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23547 struct dwarf2_cu **ref_cu)
23549 ULONGEST signature = DW_SIGNATURE (attr);
23550 struct signatured_type *sig_type;
23551 struct die_info *die;
23553 gdb_assert (attr->form == DW_FORM_ref_sig8);
23555 sig_type = lookup_signatured_type (*ref_cu, signature);
23556 /* sig_type will be NULL if the signatured type is missing from
23558 if (sig_type == NULL)
23560 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23561 " from DIE at %s [in module %s]"),
23562 hex_string (signature), sect_offset_str (src_die->sect_off),
23563 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23566 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23569 dump_die_for_error (src_die);
23570 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23571 " from DIE at %s [in module %s]"),
23572 hex_string (signature), sect_offset_str (src_die->sect_off),
23573 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23579 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23580 reading in and processing the type unit if necessary. */
23582 static struct type *
23583 get_signatured_type (struct die_info *die, ULONGEST signature,
23584 struct dwarf2_cu *cu)
23586 struct dwarf2_per_objfile *dwarf2_per_objfile
23587 = cu->per_cu->dwarf2_per_objfile;
23588 struct signatured_type *sig_type;
23589 struct dwarf2_cu *type_cu;
23590 struct die_info *type_die;
23593 sig_type = lookup_signatured_type (cu, signature);
23594 /* sig_type will be NULL if the signatured type is missing from
23596 if (sig_type == NULL)
23598 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23599 " from DIE at %s [in module %s]"),
23600 hex_string (signature), sect_offset_str (die->sect_off),
23601 objfile_name (dwarf2_per_objfile->objfile));
23602 return build_error_marker_type (cu, die);
23605 /* If we already know the type we're done. */
23606 if (sig_type->type != NULL)
23607 return sig_type->type;
23610 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23611 if (type_die != NULL)
23613 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23614 is created. This is important, for example, because for c++ classes
23615 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23616 type = read_type_die (type_die, type_cu);
23619 complaint (_("Dwarf Error: Cannot build signatured type %s"
23620 " referenced from DIE at %s [in module %s]"),
23621 hex_string (signature), sect_offset_str (die->sect_off),
23622 objfile_name (dwarf2_per_objfile->objfile));
23623 type = build_error_marker_type (cu, die);
23628 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23629 " from DIE at %s [in module %s]"),
23630 hex_string (signature), sect_offset_str (die->sect_off),
23631 objfile_name (dwarf2_per_objfile->objfile));
23632 type = build_error_marker_type (cu, die);
23634 sig_type->type = type;
23639 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23640 reading in and processing the type unit if necessary. */
23642 static struct type *
23643 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23644 struct dwarf2_cu *cu) /* ARI: editCase function */
23646 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23647 if (attr_form_is_ref (attr))
23649 struct dwarf2_cu *type_cu = cu;
23650 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23652 return read_type_die (type_die, type_cu);
23654 else if (attr->form == DW_FORM_ref_sig8)
23656 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23660 struct dwarf2_per_objfile *dwarf2_per_objfile
23661 = cu->per_cu->dwarf2_per_objfile;
23663 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23664 " at %s [in module %s]"),
23665 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23666 objfile_name (dwarf2_per_objfile->objfile));
23667 return build_error_marker_type (cu, die);
23671 /* Load the DIEs associated with type unit PER_CU into memory. */
23674 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23676 struct signatured_type *sig_type;
23678 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23679 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23681 /* We have the per_cu, but we need the signatured_type.
23682 Fortunately this is an easy translation. */
23683 gdb_assert (per_cu->is_debug_types);
23684 sig_type = (struct signatured_type *) per_cu;
23686 gdb_assert (per_cu->cu == NULL);
23688 read_signatured_type (sig_type);
23690 gdb_assert (per_cu->cu != NULL);
23693 /* die_reader_func for read_signatured_type.
23694 This is identical to load_full_comp_unit_reader,
23695 but is kept separate for now. */
23698 read_signatured_type_reader (const struct die_reader_specs *reader,
23699 const gdb_byte *info_ptr,
23700 struct die_info *comp_unit_die,
23704 struct dwarf2_cu *cu = reader->cu;
23706 gdb_assert (cu->die_hash == NULL);
23708 htab_create_alloc_ex (cu->header.length / 12,
23712 &cu->comp_unit_obstack,
23713 hashtab_obstack_allocate,
23714 dummy_obstack_deallocate);
23717 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23718 &info_ptr, comp_unit_die);
23719 cu->dies = comp_unit_die;
23720 /* comp_unit_die is not stored in die_hash, no need. */
23722 /* We try not to read any attributes in this function, because not
23723 all CUs needed for references have been loaded yet, and symbol
23724 table processing isn't initialized. But we have to set the CU language,
23725 or we won't be able to build types correctly.
23726 Similarly, if we do not read the producer, we can not apply
23727 producer-specific interpretation. */
23728 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23731 /* Read in a signatured type and build its CU and DIEs.
23732 If the type is a stub for the real type in a DWO file,
23733 read in the real type from the DWO file as well. */
23736 read_signatured_type (struct signatured_type *sig_type)
23738 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23740 gdb_assert (per_cu->is_debug_types);
23741 gdb_assert (per_cu->cu == NULL);
23743 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23744 read_signatured_type_reader, NULL);
23745 sig_type->per_cu.tu_read = 1;
23748 /* Decode simple location descriptions.
23749 Given a pointer to a dwarf block that defines a location, compute
23750 the location and return the value.
23752 NOTE drow/2003-11-18: This function is called in two situations
23753 now: for the address of static or global variables (partial symbols
23754 only) and for offsets into structures which are expected to be
23755 (more or less) constant. The partial symbol case should go away,
23756 and only the constant case should remain. That will let this
23757 function complain more accurately. A few special modes are allowed
23758 without complaint for global variables (for instance, global
23759 register values and thread-local values).
23761 A location description containing no operations indicates that the
23762 object is optimized out. The return value is 0 for that case.
23763 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23764 callers will only want a very basic result and this can become a
23767 Note that stack[0] is unused except as a default error return. */
23770 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23772 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23774 size_t size = blk->size;
23775 const gdb_byte *data = blk->data;
23776 CORE_ADDR stack[64];
23778 unsigned int bytes_read, unsnd;
23784 stack[++stacki] = 0;
23823 stack[++stacki] = op - DW_OP_lit0;
23858 stack[++stacki] = op - DW_OP_reg0;
23860 dwarf2_complex_location_expr_complaint ();
23864 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23866 stack[++stacki] = unsnd;
23868 dwarf2_complex_location_expr_complaint ();
23872 stack[++stacki] = read_address (objfile->obfd, &data[i],
23877 case DW_OP_const1u:
23878 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23882 case DW_OP_const1s:
23883 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23887 case DW_OP_const2u:
23888 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23892 case DW_OP_const2s:
23893 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23897 case DW_OP_const4u:
23898 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23902 case DW_OP_const4s:
23903 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23907 case DW_OP_const8u:
23908 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23913 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23919 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23924 stack[stacki + 1] = stack[stacki];
23929 stack[stacki - 1] += stack[stacki];
23933 case DW_OP_plus_uconst:
23934 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23940 stack[stacki - 1] -= stack[stacki];
23945 /* If we're not the last op, then we definitely can't encode
23946 this using GDB's address_class enum. This is valid for partial
23947 global symbols, although the variable's address will be bogus
23950 dwarf2_complex_location_expr_complaint ();
23953 case DW_OP_GNU_push_tls_address:
23954 case DW_OP_form_tls_address:
23955 /* The top of the stack has the offset from the beginning
23956 of the thread control block at which the variable is located. */
23957 /* Nothing should follow this operator, so the top of stack would
23959 /* This is valid for partial global symbols, but the variable's
23960 address will be bogus in the psymtab. Make it always at least
23961 non-zero to not look as a variable garbage collected by linker
23962 which have DW_OP_addr 0. */
23964 dwarf2_complex_location_expr_complaint ();
23968 case DW_OP_GNU_uninit:
23972 case DW_OP_GNU_addr_index:
23973 case DW_OP_GNU_const_index:
23974 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23981 const char *name = get_DW_OP_name (op);
23984 complaint (_("unsupported stack op: '%s'"),
23987 complaint (_("unsupported stack op: '%02x'"),
23991 return (stack[stacki]);
23994 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23995 outside of the allocated space. Also enforce minimum>0. */
23996 if (stacki >= ARRAY_SIZE (stack) - 1)
23998 complaint (_("location description stack overflow"));
24004 complaint (_("location description stack underflow"));
24008 return (stack[stacki]);
24011 /* memory allocation interface */
24013 static struct dwarf_block *
24014 dwarf_alloc_block (struct dwarf2_cu *cu)
24016 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24019 static struct die_info *
24020 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24022 struct die_info *die;
24023 size_t size = sizeof (struct die_info);
24026 size += (num_attrs - 1) * sizeof (struct attribute);
24028 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24029 memset (die, 0, sizeof (struct die_info));
24034 /* Macro support. */
24036 /* Return file name relative to the compilation directory of file number I in
24037 *LH's file name table. The result is allocated using xmalloc; the caller is
24038 responsible for freeing it. */
24041 file_file_name (int file, struct line_header *lh)
24043 /* Is the file number a valid index into the line header's file name
24044 table? Remember that file numbers start with one, not zero. */
24045 if (1 <= file && file <= lh->file_names.size ())
24047 const file_entry &fe = lh->file_names[file - 1];
24049 if (!IS_ABSOLUTE_PATH (fe.name))
24051 const char *dir = fe.include_dir (lh);
24053 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24055 return xstrdup (fe.name);
24059 /* The compiler produced a bogus file number. We can at least
24060 record the macro definitions made in the file, even if we
24061 won't be able to find the file by name. */
24062 char fake_name[80];
24064 xsnprintf (fake_name, sizeof (fake_name),
24065 "<bad macro file number %d>", file);
24067 complaint (_("bad file number in macro information (%d)"),
24070 return xstrdup (fake_name);
24074 /* Return the full name of file number I in *LH's file name table.
24075 Use COMP_DIR as the name of the current directory of the
24076 compilation. The result is allocated using xmalloc; the caller is
24077 responsible for freeing it. */
24079 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24081 /* Is the file number a valid index into the line header's file name
24082 table? Remember that file numbers start with one, not zero. */
24083 if (1 <= file && file <= lh->file_names.size ())
24085 char *relative = file_file_name (file, lh);
24087 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24089 return reconcat (relative, comp_dir, SLASH_STRING,
24090 relative, (char *) NULL);
24093 return file_file_name (file, lh);
24097 static struct macro_source_file *
24098 macro_start_file (struct dwarf2_cu *cu,
24099 int file, int line,
24100 struct macro_source_file *current_file,
24101 struct line_header *lh)
24103 /* File name relative to the compilation directory of this source file. */
24104 char *file_name = file_file_name (file, lh);
24106 if (! current_file)
24108 /* Note: We don't create a macro table for this compilation unit
24109 at all until we actually get a filename. */
24110 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24112 /* If we have no current file, then this must be the start_file
24113 directive for the compilation unit's main source file. */
24114 current_file = macro_set_main (macro_table, file_name);
24115 macro_define_special (macro_table);
24118 current_file = macro_include (current_file, line, file_name);
24122 return current_file;
24125 static const char *
24126 consume_improper_spaces (const char *p, const char *body)
24130 complaint (_("macro definition contains spaces "
24131 "in formal argument list:\n`%s'"),
24143 parse_macro_definition (struct macro_source_file *file, int line,
24148 /* The body string takes one of two forms. For object-like macro
24149 definitions, it should be:
24151 <macro name> " " <definition>
24153 For function-like macro definitions, it should be:
24155 <macro name> "() " <definition>
24157 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24159 Spaces may appear only where explicitly indicated, and in the
24162 The Dwarf 2 spec says that an object-like macro's name is always
24163 followed by a space, but versions of GCC around March 2002 omit
24164 the space when the macro's definition is the empty string.
24166 The Dwarf 2 spec says that there should be no spaces between the
24167 formal arguments in a function-like macro's formal argument list,
24168 but versions of GCC around March 2002 include spaces after the
24172 /* Find the extent of the macro name. The macro name is terminated
24173 by either a space or null character (for an object-like macro) or
24174 an opening paren (for a function-like macro). */
24175 for (p = body; *p; p++)
24176 if (*p == ' ' || *p == '(')
24179 if (*p == ' ' || *p == '\0')
24181 /* It's an object-like macro. */
24182 int name_len = p - body;
24183 char *name = savestring (body, name_len);
24184 const char *replacement;
24187 replacement = body + name_len + 1;
24190 dwarf2_macro_malformed_definition_complaint (body);
24191 replacement = body + name_len;
24194 macro_define_object (file, line, name, replacement);
24198 else if (*p == '(')
24200 /* It's a function-like macro. */
24201 char *name = savestring (body, p - body);
24204 char **argv = XNEWVEC (char *, argv_size);
24208 p = consume_improper_spaces (p, body);
24210 /* Parse the formal argument list. */
24211 while (*p && *p != ')')
24213 /* Find the extent of the current argument name. */
24214 const char *arg_start = p;
24216 while (*p && *p != ',' && *p != ')' && *p != ' ')
24219 if (! *p || p == arg_start)
24220 dwarf2_macro_malformed_definition_complaint (body);
24223 /* Make sure argv has room for the new argument. */
24224 if (argc >= argv_size)
24227 argv = XRESIZEVEC (char *, argv, argv_size);
24230 argv[argc++] = savestring (arg_start, p - arg_start);
24233 p = consume_improper_spaces (p, body);
24235 /* Consume the comma, if present. */
24240 p = consume_improper_spaces (p, body);
24249 /* Perfectly formed definition, no complaints. */
24250 macro_define_function (file, line, name,
24251 argc, (const char **) argv,
24253 else if (*p == '\0')
24255 /* Complain, but do define it. */
24256 dwarf2_macro_malformed_definition_complaint (body);
24257 macro_define_function (file, line, name,
24258 argc, (const char **) argv,
24262 /* Just complain. */
24263 dwarf2_macro_malformed_definition_complaint (body);
24266 /* Just complain. */
24267 dwarf2_macro_malformed_definition_complaint (body);
24273 for (i = 0; i < argc; i++)
24279 dwarf2_macro_malformed_definition_complaint (body);
24282 /* Skip some bytes from BYTES according to the form given in FORM.
24283 Returns the new pointer. */
24285 static const gdb_byte *
24286 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24287 enum dwarf_form form,
24288 unsigned int offset_size,
24289 struct dwarf2_section_info *section)
24291 unsigned int bytes_read;
24295 case DW_FORM_data1:
24300 case DW_FORM_data2:
24304 case DW_FORM_data4:
24308 case DW_FORM_data8:
24312 case DW_FORM_data16:
24316 case DW_FORM_string:
24317 read_direct_string (abfd, bytes, &bytes_read);
24318 bytes += bytes_read;
24321 case DW_FORM_sec_offset:
24323 case DW_FORM_GNU_strp_alt:
24324 bytes += offset_size;
24327 case DW_FORM_block:
24328 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24329 bytes += bytes_read;
24332 case DW_FORM_block1:
24333 bytes += 1 + read_1_byte (abfd, bytes);
24335 case DW_FORM_block2:
24336 bytes += 2 + read_2_bytes (abfd, bytes);
24338 case DW_FORM_block4:
24339 bytes += 4 + read_4_bytes (abfd, bytes);
24342 case DW_FORM_addrx:
24343 case DW_FORM_sdata:
24345 case DW_FORM_udata:
24346 case DW_FORM_GNU_addr_index:
24347 case DW_FORM_GNU_str_index:
24348 bytes = gdb_skip_leb128 (bytes, buffer_end);
24351 dwarf2_section_buffer_overflow_complaint (section);
24356 case DW_FORM_implicit_const:
24361 complaint (_("invalid form 0x%x in `%s'"),
24362 form, get_section_name (section));
24370 /* A helper for dwarf_decode_macros that handles skipping an unknown
24371 opcode. Returns an updated pointer to the macro data buffer; or,
24372 on error, issues a complaint and returns NULL. */
24374 static const gdb_byte *
24375 skip_unknown_opcode (unsigned int opcode,
24376 const gdb_byte **opcode_definitions,
24377 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24379 unsigned int offset_size,
24380 struct dwarf2_section_info *section)
24382 unsigned int bytes_read, i;
24384 const gdb_byte *defn;
24386 if (opcode_definitions[opcode] == NULL)
24388 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24393 defn = opcode_definitions[opcode];
24394 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24395 defn += bytes_read;
24397 for (i = 0; i < arg; ++i)
24399 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24400 (enum dwarf_form) defn[i], offset_size,
24402 if (mac_ptr == NULL)
24404 /* skip_form_bytes already issued the complaint. */
24412 /* A helper function which parses the header of a macro section.
24413 If the macro section is the extended (for now called "GNU") type,
24414 then this updates *OFFSET_SIZE. Returns a pointer to just after
24415 the header, or issues a complaint and returns NULL on error. */
24417 static const gdb_byte *
24418 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24420 const gdb_byte *mac_ptr,
24421 unsigned int *offset_size,
24422 int section_is_gnu)
24424 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24426 if (section_is_gnu)
24428 unsigned int version, flags;
24430 version = read_2_bytes (abfd, mac_ptr);
24431 if (version != 4 && version != 5)
24433 complaint (_("unrecognized version `%d' in .debug_macro section"),
24439 flags = read_1_byte (abfd, mac_ptr);
24441 *offset_size = (flags & 1) ? 8 : 4;
24443 if ((flags & 2) != 0)
24444 /* We don't need the line table offset. */
24445 mac_ptr += *offset_size;
24447 /* Vendor opcode descriptions. */
24448 if ((flags & 4) != 0)
24450 unsigned int i, count;
24452 count = read_1_byte (abfd, mac_ptr);
24454 for (i = 0; i < count; ++i)
24456 unsigned int opcode, bytes_read;
24459 opcode = read_1_byte (abfd, mac_ptr);
24461 opcode_definitions[opcode] = mac_ptr;
24462 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24463 mac_ptr += bytes_read;
24472 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24473 including DW_MACRO_import. */
24476 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24478 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24479 struct macro_source_file *current_file,
24480 struct line_header *lh,
24481 struct dwarf2_section_info *section,
24482 int section_is_gnu, int section_is_dwz,
24483 unsigned int offset_size,
24484 htab_t include_hash)
24486 struct dwarf2_per_objfile *dwarf2_per_objfile
24487 = cu->per_cu->dwarf2_per_objfile;
24488 struct objfile *objfile = dwarf2_per_objfile->objfile;
24489 enum dwarf_macro_record_type macinfo_type;
24490 int at_commandline;
24491 const gdb_byte *opcode_definitions[256];
24493 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24494 &offset_size, section_is_gnu);
24495 if (mac_ptr == NULL)
24497 /* We already issued a complaint. */
24501 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24502 GDB is still reading the definitions from command line. First
24503 DW_MACINFO_start_file will need to be ignored as it was already executed
24504 to create CURRENT_FILE for the main source holding also the command line
24505 definitions. On first met DW_MACINFO_start_file this flag is reset to
24506 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24508 at_commandline = 1;
24512 /* Do we at least have room for a macinfo type byte? */
24513 if (mac_ptr >= mac_end)
24515 dwarf2_section_buffer_overflow_complaint (section);
24519 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24522 /* Note that we rely on the fact that the corresponding GNU and
24523 DWARF constants are the same. */
24525 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24526 switch (macinfo_type)
24528 /* A zero macinfo type indicates the end of the macro
24533 case DW_MACRO_define:
24534 case DW_MACRO_undef:
24535 case DW_MACRO_define_strp:
24536 case DW_MACRO_undef_strp:
24537 case DW_MACRO_define_sup:
24538 case DW_MACRO_undef_sup:
24540 unsigned int bytes_read;
24545 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24546 mac_ptr += bytes_read;
24548 if (macinfo_type == DW_MACRO_define
24549 || macinfo_type == DW_MACRO_undef)
24551 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24552 mac_ptr += bytes_read;
24556 LONGEST str_offset;
24558 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24559 mac_ptr += offset_size;
24561 if (macinfo_type == DW_MACRO_define_sup
24562 || macinfo_type == DW_MACRO_undef_sup
24565 struct dwz_file *dwz
24566 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24568 body = read_indirect_string_from_dwz (objfile,
24572 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24576 is_define = (macinfo_type == DW_MACRO_define
24577 || macinfo_type == DW_MACRO_define_strp
24578 || macinfo_type == DW_MACRO_define_sup);
24579 if (! current_file)
24581 /* DWARF violation as no main source is present. */
24582 complaint (_("debug info with no main source gives macro %s "
24584 is_define ? _("definition") : _("undefinition"),
24588 if ((line == 0 && !at_commandline)
24589 || (line != 0 && at_commandline))
24590 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24591 at_commandline ? _("command-line") : _("in-file"),
24592 is_define ? _("definition") : _("undefinition"),
24593 line == 0 ? _("zero") : _("non-zero"), line, body);
24597 /* Fedora's rpm-build's "debugedit" binary
24598 corrupted .debug_macro sections.
24601 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24602 complaint (_("debug info gives %s invalid macro %s "
24603 "without body (corrupted?) at line %d "
24605 at_commandline ? _("command-line") : _("in-file"),
24606 is_define ? _("definition") : _("undefinition"),
24607 line, current_file->filename);
24609 else if (is_define)
24610 parse_macro_definition (current_file, line, body);
24613 gdb_assert (macinfo_type == DW_MACRO_undef
24614 || macinfo_type == DW_MACRO_undef_strp
24615 || macinfo_type == DW_MACRO_undef_sup);
24616 macro_undef (current_file, line, body);
24621 case DW_MACRO_start_file:
24623 unsigned int bytes_read;
24626 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24627 mac_ptr += bytes_read;
24628 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24629 mac_ptr += bytes_read;
24631 if ((line == 0 && !at_commandline)
24632 || (line != 0 && at_commandline))
24633 complaint (_("debug info gives source %d included "
24634 "from %s at %s line %d"),
24635 file, at_commandline ? _("command-line") : _("file"),
24636 line == 0 ? _("zero") : _("non-zero"), line);
24638 if (at_commandline)
24640 /* This DW_MACRO_start_file was executed in the
24642 at_commandline = 0;
24645 current_file = macro_start_file (cu, file, line, current_file,
24650 case DW_MACRO_end_file:
24651 if (! current_file)
24652 complaint (_("macro debug info has an unmatched "
24653 "`close_file' directive"));
24656 current_file = current_file->included_by;
24657 if (! current_file)
24659 enum dwarf_macro_record_type next_type;
24661 /* GCC circa March 2002 doesn't produce the zero
24662 type byte marking the end of the compilation
24663 unit. Complain if it's not there, but exit no
24666 /* Do we at least have room for a macinfo type byte? */
24667 if (mac_ptr >= mac_end)
24669 dwarf2_section_buffer_overflow_complaint (section);
24673 /* We don't increment mac_ptr here, so this is just
24676 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24678 if (next_type != 0)
24679 complaint (_("no terminating 0-type entry for "
24680 "macros in `.debug_macinfo' section"));
24687 case DW_MACRO_import:
24688 case DW_MACRO_import_sup:
24692 bfd *include_bfd = abfd;
24693 struct dwarf2_section_info *include_section = section;
24694 const gdb_byte *include_mac_end = mac_end;
24695 int is_dwz = section_is_dwz;
24696 const gdb_byte *new_mac_ptr;
24698 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24699 mac_ptr += offset_size;
24701 if (macinfo_type == DW_MACRO_import_sup)
24703 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24705 dwarf2_read_section (objfile, &dwz->macro);
24707 include_section = &dwz->macro;
24708 include_bfd = get_section_bfd_owner (include_section);
24709 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24713 new_mac_ptr = include_section->buffer + offset;
24714 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24718 /* This has actually happened; see
24719 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24720 complaint (_("recursive DW_MACRO_import in "
24721 ".debug_macro section"));
24725 *slot = (void *) new_mac_ptr;
24727 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24728 include_mac_end, current_file, lh,
24729 section, section_is_gnu, is_dwz,
24730 offset_size, include_hash);
24732 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24737 case DW_MACINFO_vendor_ext:
24738 if (!section_is_gnu)
24740 unsigned int bytes_read;
24742 /* This reads the constant, but since we don't recognize
24743 any vendor extensions, we ignore it. */
24744 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24745 mac_ptr += bytes_read;
24746 read_direct_string (abfd, mac_ptr, &bytes_read);
24747 mac_ptr += bytes_read;
24749 /* We don't recognize any vendor extensions. */
24755 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24756 mac_ptr, mac_end, abfd, offset_size,
24758 if (mac_ptr == NULL)
24763 } while (macinfo_type != 0);
24767 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24768 int section_is_gnu)
24770 struct dwarf2_per_objfile *dwarf2_per_objfile
24771 = cu->per_cu->dwarf2_per_objfile;
24772 struct objfile *objfile = dwarf2_per_objfile->objfile;
24773 struct line_header *lh = cu->line_header;
24775 const gdb_byte *mac_ptr, *mac_end;
24776 struct macro_source_file *current_file = 0;
24777 enum dwarf_macro_record_type macinfo_type;
24778 unsigned int offset_size = cu->header.offset_size;
24779 const gdb_byte *opcode_definitions[256];
24781 struct dwarf2_section_info *section;
24782 const char *section_name;
24784 if (cu->dwo_unit != NULL)
24786 if (section_is_gnu)
24788 section = &cu->dwo_unit->dwo_file->sections.macro;
24789 section_name = ".debug_macro.dwo";
24793 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24794 section_name = ".debug_macinfo.dwo";
24799 if (section_is_gnu)
24801 section = &dwarf2_per_objfile->macro;
24802 section_name = ".debug_macro";
24806 section = &dwarf2_per_objfile->macinfo;
24807 section_name = ".debug_macinfo";
24811 dwarf2_read_section (objfile, section);
24812 if (section->buffer == NULL)
24814 complaint (_("missing %s section"), section_name);
24817 abfd = get_section_bfd_owner (section);
24819 /* First pass: Find the name of the base filename.
24820 This filename is needed in order to process all macros whose definition
24821 (or undefinition) comes from the command line. These macros are defined
24822 before the first DW_MACINFO_start_file entry, and yet still need to be
24823 associated to the base file.
24825 To determine the base file name, we scan the macro definitions until we
24826 reach the first DW_MACINFO_start_file entry. We then initialize
24827 CURRENT_FILE accordingly so that any macro definition found before the
24828 first DW_MACINFO_start_file can still be associated to the base file. */
24830 mac_ptr = section->buffer + offset;
24831 mac_end = section->buffer + section->size;
24833 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24834 &offset_size, section_is_gnu);
24835 if (mac_ptr == NULL)
24837 /* We already issued a complaint. */
24843 /* Do we at least have room for a macinfo type byte? */
24844 if (mac_ptr >= mac_end)
24846 /* Complaint is printed during the second pass as GDB will probably
24847 stop the first pass earlier upon finding
24848 DW_MACINFO_start_file. */
24852 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24855 /* Note that we rely on the fact that the corresponding GNU and
24856 DWARF constants are the same. */
24858 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24859 switch (macinfo_type)
24861 /* A zero macinfo type indicates the end of the macro
24866 case DW_MACRO_define:
24867 case DW_MACRO_undef:
24868 /* Only skip the data by MAC_PTR. */
24870 unsigned int bytes_read;
24872 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24873 mac_ptr += bytes_read;
24874 read_direct_string (abfd, mac_ptr, &bytes_read);
24875 mac_ptr += bytes_read;
24879 case DW_MACRO_start_file:
24881 unsigned int bytes_read;
24884 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24885 mac_ptr += bytes_read;
24886 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24887 mac_ptr += bytes_read;
24889 current_file = macro_start_file (cu, file, line, current_file, lh);
24893 case DW_MACRO_end_file:
24894 /* No data to skip by MAC_PTR. */
24897 case DW_MACRO_define_strp:
24898 case DW_MACRO_undef_strp:
24899 case DW_MACRO_define_sup:
24900 case DW_MACRO_undef_sup:
24902 unsigned int bytes_read;
24904 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24905 mac_ptr += bytes_read;
24906 mac_ptr += offset_size;
24910 case DW_MACRO_import:
24911 case DW_MACRO_import_sup:
24912 /* Note that, according to the spec, a transparent include
24913 chain cannot call DW_MACRO_start_file. So, we can just
24914 skip this opcode. */
24915 mac_ptr += offset_size;
24918 case DW_MACINFO_vendor_ext:
24919 /* Only skip the data by MAC_PTR. */
24920 if (!section_is_gnu)
24922 unsigned int bytes_read;
24924 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24925 mac_ptr += bytes_read;
24926 read_direct_string (abfd, mac_ptr, &bytes_read);
24927 mac_ptr += bytes_read;
24932 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24933 mac_ptr, mac_end, abfd, offset_size,
24935 if (mac_ptr == NULL)
24940 } while (macinfo_type != 0 && current_file == NULL);
24942 /* Second pass: Process all entries.
24944 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24945 command-line macro definitions/undefinitions. This flag is unset when we
24946 reach the first DW_MACINFO_start_file entry. */
24948 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24950 NULL, xcalloc, xfree));
24951 mac_ptr = section->buffer + offset;
24952 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24953 *slot = (void *) mac_ptr;
24954 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
24955 current_file, lh, section,
24956 section_is_gnu, 0, offset_size,
24957 include_hash.get ());
24960 /* Check if the attribute's form is a DW_FORM_block*
24961 if so return true else false. */
24964 attr_form_is_block (const struct attribute *attr)
24966 return (attr == NULL ? 0 :
24967 attr->form == DW_FORM_block1
24968 || attr->form == DW_FORM_block2
24969 || attr->form == DW_FORM_block4
24970 || attr->form == DW_FORM_block
24971 || attr->form == DW_FORM_exprloc);
24974 /* Return non-zero if ATTR's value is a section offset --- classes
24975 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24976 You may use DW_UNSND (attr) to retrieve such offsets.
24978 Section 7.5.4, "Attribute Encodings", explains that no attribute
24979 may have a value that belongs to more than one of these classes; it
24980 would be ambiguous if we did, because we use the same forms for all
24984 attr_form_is_section_offset (const struct attribute *attr)
24986 return (attr->form == DW_FORM_data4
24987 || attr->form == DW_FORM_data8
24988 || attr->form == DW_FORM_sec_offset);
24991 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24992 zero otherwise. When this function returns true, you can apply
24993 dwarf2_get_attr_constant_value to it.
24995 However, note that for some attributes you must check
24996 attr_form_is_section_offset before using this test. DW_FORM_data4
24997 and DW_FORM_data8 are members of both the constant class, and of
24998 the classes that contain offsets into other debug sections
24999 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25000 that, if an attribute's can be either a constant or one of the
25001 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25002 taken as section offsets, not constants.
25004 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25005 cannot handle that. */
25008 attr_form_is_constant (const struct attribute *attr)
25010 switch (attr->form)
25012 case DW_FORM_sdata:
25013 case DW_FORM_udata:
25014 case DW_FORM_data1:
25015 case DW_FORM_data2:
25016 case DW_FORM_data4:
25017 case DW_FORM_data8:
25018 case DW_FORM_implicit_const:
25026 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25027 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25030 attr_form_is_ref (const struct attribute *attr)
25032 switch (attr->form)
25034 case DW_FORM_ref_addr:
25039 case DW_FORM_ref_udata:
25040 case DW_FORM_GNU_ref_alt:
25047 /* Return the .debug_loc section to use for CU.
25048 For DWO files use .debug_loc.dwo. */
25050 static struct dwarf2_section_info *
25051 cu_debug_loc_section (struct dwarf2_cu *cu)
25053 struct dwarf2_per_objfile *dwarf2_per_objfile
25054 = cu->per_cu->dwarf2_per_objfile;
25058 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25060 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
25062 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25063 : &dwarf2_per_objfile->loc);
25066 /* A helper function that fills in a dwarf2_loclist_baton. */
25069 fill_in_loclist_baton (struct dwarf2_cu *cu,
25070 struct dwarf2_loclist_baton *baton,
25071 const struct attribute *attr)
25073 struct dwarf2_per_objfile *dwarf2_per_objfile
25074 = cu->per_cu->dwarf2_per_objfile;
25075 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25077 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25079 baton->per_cu = cu->per_cu;
25080 gdb_assert (baton->per_cu);
25081 /* We don't know how long the location list is, but make sure we
25082 don't run off the edge of the section. */
25083 baton->size = section->size - DW_UNSND (attr);
25084 baton->data = section->buffer + DW_UNSND (attr);
25085 baton->base_address = cu->base_address;
25086 baton->from_dwo = cu->dwo_unit != NULL;
25090 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25091 struct dwarf2_cu *cu, int is_block)
25093 struct dwarf2_per_objfile *dwarf2_per_objfile
25094 = cu->per_cu->dwarf2_per_objfile;
25095 struct objfile *objfile = dwarf2_per_objfile->objfile;
25096 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25098 if (attr_form_is_section_offset (attr)
25099 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25100 the section. If so, fall through to the complaint in the
25102 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25104 struct dwarf2_loclist_baton *baton;
25106 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25108 fill_in_loclist_baton (cu, baton, attr);
25110 if (cu->base_known == 0)
25111 complaint (_("Location list used without "
25112 "specifying the CU base address."));
25114 SYMBOL_ACLASS_INDEX (sym) = (is_block
25115 ? dwarf2_loclist_block_index
25116 : dwarf2_loclist_index);
25117 SYMBOL_LOCATION_BATON (sym) = baton;
25121 struct dwarf2_locexpr_baton *baton;
25123 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25124 baton->per_cu = cu->per_cu;
25125 gdb_assert (baton->per_cu);
25127 if (attr_form_is_block (attr))
25129 /* Note that we're just copying the block's data pointer
25130 here, not the actual data. We're still pointing into the
25131 info_buffer for SYM's objfile; right now we never release
25132 that buffer, but when we do clean up properly this may
25134 baton->size = DW_BLOCK (attr)->size;
25135 baton->data = DW_BLOCK (attr)->data;
25139 dwarf2_invalid_attrib_class_complaint ("location description",
25140 SYMBOL_NATURAL_NAME (sym));
25144 SYMBOL_ACLASS_INDEX (sym) = (is_block
25145 ? dwarf2_locexpr_block_index
25146 : dwarf2_locexpr_index);
25147 SYMBOL_LOCATION_BATON (sym) = baton;
25151 /* Return the OBJFILE associated with the compilation unit CU. If CU
25152 came from a separate debuginfo file, then the master objfile is
25156 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25158 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25160 /* Return the master objfile, so that we can report and look up the
25161 correct file containing this variable. */
25162 if (objfile->separate_debug_objfile_backlink)
25163 objfile = objfile->separate_debug_objfile_backlink;
25168 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25169 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25170 CU_HEADERP first. */
25172 static const struct comp_unit_head *
25173 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25174 struct dwarf2_per_cu_data *per_cu)
25176 const gdb_byte *info_ptr;
25179 return &per_cu->cu->header;
25181 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25183 memset (cu_headerp, 0, sizeof (*cu_headerp));
25184 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25185 rcuh_kind::COMPILE);
25190 /* Return the address size given in the compilation unit header for CU. */
25193 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25195 struct comp_unit_head cu_header_local;
25196 const struct comp_unit_head *cu_headerp;
25198 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25200 return cu_headerp->addr_size;
25203 /* Return the offset size given in the compilation unit header for CU. */
25206 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25208 struct comp_unit_head cu_header_local;
25209 const struct comp_unit_head *cu_headerp;
25211 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25213 return cu_headerp->offset_size;
25216 /* See its dwarf2loc.h declaration. */
25219 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25221 struct comp_unit_head cu_header_local;
25222 const struct comp_unit_head *cu_headerp;
25224 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25226 if (cu_headerp->version == 2)
25227 return cu_headerp->addr_size;
25229 return cu_headerp->offset_size;
25232 /* Return the text offset of the CU. The returned offset comes from
25233 this CU's objfile. If this objfile came from a separate debuginfo
25234 file, then the offset may be different from the corresponding
25235 offset in the parent objfile. */
25238 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25240 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25242 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25245 /* Return DWARF version number of PER_CU. */
25248 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25250 return per_cu->dwarf_version;
25253 /* Locate the .debug_info compilation unit from CU's objfile which contains
25254 the DIE at OFFSET. Raises an error on failure. */
25256 static struct dwarf2_per_cu_data *
25257 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25258 unsigned int offset_in_dwz,
25259 struct dwarf2_per_objfile *dwarf2_per_objfile)
25261 struct dwarf2_per_cu_data *this_cu;
25265 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25268 struct dwarf2_per_cu_data *mid_cu;
25269 int mid = low + (high - low) / 2;
25271 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25272 if (mid_cu->is_dwz > offset_in_dwz
25273 || (mid_cu->is_dwz == offset_in_dwz
25274 && mid_cu->sect_off + mid_cu->length >= sect_off))
25279 gdb_assert (low == high);
25280 this_cu = dwarf2_per_objfile->all_comp_units[low];
25281 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25283 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25284 error (_("Dwarf Error: could not find partial DIE containing "
25285 "offset %s [in module %s]"),
25286 sect_offset_str (sect_off),
25287 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25289 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25291 return dwarf2_per_objfile->all_comp_units[low-1];
25295 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25296 && sect_off >= this_cu->sect_off + this_cu->length)
25297 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25298 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25303 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25305 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25306 : per_cu (per_cu_),
25308 has_loclist (false),
25309 checked_producer (false),
25310 producer_is_gxx_lt_4_6 (false),
25311 producer_is_gcc_lt_4_3 (false),
25312 producer_is_icc (false),
25313 producer_is_icc_lt_14 (false),
25314 producer_is_codewarrior (false),
25315 processing_has_namespace_info (false)
25320 /* Destroy a dwarf2_cu. */
25322 dwarf2_cu::~dwarf2_cu ()
25327 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25330 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25331 enum language pretend_language)
25333 struct attribute *attr;
25335 /* Set the language we're debugging. */
25336 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25338 set_cu_language (DW_UNSND (attr), cu);
25341 cu->language = pretend_language;
25342 cu->language_defn = language_def (cu->language);
25345 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25348 /* Increase the age counter on each cached compilation unit, and free
25349 any that are too old. */
25352 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25354 struct dwarf2_per_cu_data *per_cu, **last_chain;
25356 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25357 per_cu = dwarf2_per_objfile->read_in_chain;
25358 while (per_cu != NULL)
25360 per_cu->cu->last_used ++;
25361 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25362 dwarf2_mark (per_cu->cu);
25363 per_cu = per_cu->cu->read_in_chain;
25366 per_cu = dwarf2_per_objfile->read_in_chain;
25367 last_chain = &dwarf2_per_objfile->read_in_chain;
25368 while (per_cu != NULL)
25370 struct dwarf2_per_cu_data *next_cu;
25372 next_cu = per_cu->cu->read_in_chain;
25374 if (!per_cu->cu->mark)
25377 *last_chain = next_cu;
25380 last_chain = &per_cu->cu->read_in_chain;
25386 /* Remove a single compilation unit from the cache. */
25389 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25391 struct dwarf2_per_cu_data *per_cu, **last_chain;
25392 struct dwarf2_per_objfile *dwarf2_per_objfile
25393 = target_per_cu->dwarf2_per_objfile;
25395 per_cu = dwarf2_per_objfile->read_in_chain;
25396 last_chain = &dwarf2_per_objfile->read_in_chain;
25397 while (per_cu != NULL)
25399 struct dwarf2_per_cu_data *next_cu;
25401 next_cu = per_cu->cu->read_in_chain;
25403 if (per_cu == target_per_cu)
25407 *last_chain = next_cu;
25411 last_chain = &per_cu->cu->read_in_chain;
25417 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25418 We store these in a hash table separate from the DIEs, and preserve them
25419 when the DIEs are flushed out of cache.
25421 The CU "per_cu" pointer is needed because offset alone is not enough to
25422 uniquely identify the type. A file may have multiple .debug_types sections,
25423 or the type may come from a DWO file. Furthermore, while it's more logical
25424 to use per_cu->section+offset, with Fission the section with the data is in
25425 the DWO file but we don't know that section at the point we need it.
25426 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25427 because we can enter the lookup routine, get_die_type_at_offset, from
25428 outside this file, and thus won't necessarily have PER_CU->cu.
25429 Fortunately, PER_CU is stable for the life of the objfile. */
25431 struct dwarf2_per_cu_offset_and_type
25433 const struct dwarf2_per_cu_data *per_cu;
25434 sect_offset sect_off;
25438 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25441 per_cu_offset_and_type_hash (const void *item)
25443 const struct dwarf2_per_cu_offset_and_type *ofs
25444 = (const struct dwarf2_per_cu_offset_and_type *) item;
25446 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25449 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25452 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25454 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25455 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25456 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25457 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25459 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25460 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25463 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25464 table if necessary. For convenience, return TYPE.
25466 The DIEs reading must have careful ordering to:
25467 * Not cause infite loops trying to read in DIEs as a prerequisite for
25468 reading current DIE.
25469 * Not trying to dereference contents of still incompletely read in types
25470 while reading in other DIEs.
25471 * Enable referencing still incompletely read in types just by a pointer to
25472 the type without accessing its fields.
25474 Therefore caller should follow these rules:
25475 * Try to fetch any prerequisite types we may need to build this DIE type
25476 before building the type and calling set_die_type.
25477 * After building type call set_die_type for current DIE as soon as
25478 possible before fetching more types to complete the current type.
25479 * Make the type as complete as possible before fetching more types. */
25481 static struct type *
25482 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25484 struct dwarf2_per_objfile *dwarf2_per_objfile
25485 = cu->per_cu->dwarf2_per_objfile;
25486 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25487 struct objfile *objfile = dwarf2_per_objfile->objfile;
25488 struct attribute *attr;
25489 struct dynamic_prop prop;
25491 /* For Ada types, make sure that the gnat-specific data is always
25492 initialized (if not already set). There are a few types where
25493 we should not be doing so, because the type-specific area is
25494 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25495 where the type-specific area is used to store the floatformat).
25496 But this is not a problem, because the gnat-specific information
25497 is actually not needed for these types. */
25498 if (need_gnat_info (cu)
25499 && TYPE_CODE (type) != TYPE_CODE_FUNC
25500 && TYPE_CODE (type) != TYPE_CODE_FLT
25501 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25502 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25503 && TYPE_CODE (type) != TYPE_CODE_METHOD
25504 && !HAVE_GNAT_AUX_INFO (type))
25505 INIT_GNAT_SPECIFIC (type);
25507 /* Read DW_AT_allocated and set in type. */
25508 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25509 if (attr_form_is_block (attr))
25511 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25512 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25514 else if (attr != NULL)
25516 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25517 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25518 sect_offset_str (die->sect_off));
25521 /* Read DW_AT_associated and set in type. */
25522 attr = dwarf2_attr (die, DW_AT_associated, cu);
25523 if (attr_form_is_block (attr))
25525 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25526 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25528 else if (attr != NULL)
25530 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25531 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25532 sect_offset_str (die->sect_off));
25535 /* Read DW_AT_data_location and set in type. */
25536 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25537 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25538 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25540 if (dwarf2_per_objfile->die_type_hash == NULL)
25542 dwarf2_per_objfile->die_type_hash =
25543 htab_create_alloc_ex (127,
25544 per_cu_offset_and_type_hash,
25545 per_cu_offset_and_type_eq,
25547 &objfile->objfile_obstack,
25548 hashtab_obstack_allocate,
25549 dummy_obstack_deallocate);
25552 ofs.per_cu = cu->per_cu;
25553 ofs.sect_off = die->sect_off;
25555 slot = (struct dwarf2_per_cu_offset_and_type **)
25556 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25558 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25559 sect_offset_str (die->sect_off));
25560 *slot = XOBNEW (&objfile->objfile_obstack,
25561 struct dwarf2_per_cu_offset_and_type);
25566 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25567 or return NULL if the die does not have a saved type. */
25569 static struct type *
25570 get_die_type_at_offset (sect_offset sect_off,
25571 struct dwarf2_per_cu_data *per_cu)
25573 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25574 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25576 if (dwarf2_per_objfile->die_type_hash == NULL)
25579 ofs.per_cu = per_cu;
25580 ofs.sect_off = sect_off;
25581 slot = ((struct dwarf2_per_cu_offset_and_type *)
25582 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25589 /* Look up the type for DIE in CU in die_type_hash,
25590 or return NULL if DIE does not have a saved type. */
25592 static struct type *
25593 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25595 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25598 /* Add a dependence relationship from CU to REF_PER_CU. */
25601 dwarf2_add_dependence (struct dwarf2_cu *cu,
25602 struct dwarf2_per_cu_data *ref_per_cu)
25606 if (cu->dependencies == NULL)
25608 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25609 NULL, &cu->comp_unit_obstack,
25610 hashtab_obstack_allocate,
25611 dummy_obstack_deallocate);
25613 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25615 *slot = ref_per_cu;
25618 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25619 Set the mark field in every compilation unit in the
25620 cache that we must keep because we are keeping CU. */
25623 dwarf2_mark_helper (void **slot, void *data)
25625 struct dwarf2_per_cu_data *per_cu;
25627 per_cu = (struct dwarf2_per_cu_data *) *slot;
25629 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25630 reading of the chain. As such dependencies remain valid it is not much
25631 useful to track and undo them during QUIT cleanups. */
25632 if (per_cu->cu == NULL)
25635 if (per_cu->cu->mark)
25637 per_cu->cu->mark = true;
25639 if (per_cu->cu->dependencies != NULL)
25640 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25645 /* Set the mark field in CU and in every other compilation unit in the
25646 cache that we must keep because we are keeping CU. */
25649 dwarf2_mark (struct dwarf2_cu *cu)
25654 if (cu->dependencies != NULL)
25655 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25659 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25663 per_cu->cu->mark = false;
25664 per_cu = per_cu->cu->read_in_chain;
25668 /* Trivial hash function for partial_die_info: the hash value of a DIE
25669 is its offset in .debug_info for this objfile. */
25672 partial_die_hash (const void *item)
25674 const struct partial_die_info *part_die
25675 = (const struct partial_die_info *) item;
25677 return to_underlying (part_die->sect_off);
25680 /* Trivial comparison function for partial_die_info structures: two DIEs
25681 are equal if they have the same offset. */
25684 partial_die_eq (const void *item_lhs, const void *item_rhs)
25686 const struct partial_die_info *part_die_lhs
25687 = (const struct partial_die_info *) item_lhs;
25688 const struct partial_die_info *part_die_rhs
25689 = (const struct partial_die_info *) item_rhs;
25691 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25694 struct cmd_list_element *set_dwarf_cmdlist;
25695 struct cmd_list_element *show_dwarf_cmdlist;
25698 set_dwarf_cmd (const char *args, int from_tty)
25700 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25705 show_dwarf_cmd (const char *args, int from_tty)
25707 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25710 int dwarf_always_disassemble;
25713 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25714 struct cmd_list_element *c, const char *value)
25716 fprintf_filtered (file,
25717 _("Whether to always disassemble "
25718 "DWARF expressions is %s.\n"),
25723 show_check_physname (struct ui_file *file, int from_tty,
25724 struct cmd_list_element *c, const char *value)
25726 fprintf_filtered (file,
25727 _("Whether to check \"physname\" is %s.\n"),
25732 _initialize_dwarf2_read (void)
25734 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25735 Set DWARF specific variables.\n\
25736 Configure DWARF variables such as the cache size"),
25737 &set_dwarf_cmdlist, "maintenance set dwarf ",
25738 0/*allow-unknown*/, &maintenance_set_cmdlist);
25740 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25741 Show DWARF specific variables\n\
25742 Show DWARF variables such as the cache size"),
25743 &show_dwarf_cmdlist, "maintenance show dwarf ",
25744 0/*allow-unknown*/, &maintenance_show_cmdlist);
25746 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25747 &dwarf_max_cache_age, _("\
25748 Set the upper bound on the age of cached DWARF compilation units."), _("\
25749 Show the upper bound on the age of cached DWARF compilation units."), _("\
25750 A higher limit means that cached compilation units will be stored\n\
25751 in memory longer, and more total memory will be used. Zero disables\n\
25752 caching, which can slow down startup."),
25754 show_dwarf_max_cache_age,
25755 &set_dwarf_cmdlist,
25756 &show_dwarf_cmdlist);
25758 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25759 &dwarf_always_disassemble, _("\
25760 Set whether `info address' always disassembles DWARF expressions."), _("\
25761 Show whether `info address' always disassembles DWARF expressions."), _("\
25762 When enabled, DWARF expressions are always printed in an assembly-like\n\
25763 syntax. When disabled, expressions will be printed in a more\n\
25764 conversational style, when possible."),
25766 show_dwarf_always_disassemble,
25767 &set_dwarf_cmdlist,
25768 &show_dwarf_cmdlist);
25770 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25771 Set debugging of the DWARF reader."), _("\
25772 Show debugging of the DWARF reader."), _("\
25773 When enabled (non-zero), debugging messages are printed during DWARF\n\
25774 reading and symtab expansion. A value of 1 (one) provides basic\n\
25775 information. A value greater than 1 provides more verbose information."),
25778 &setdebuglist, &showdebuglist);
25780 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25781 Set debugging of the DWARF DIE reader."), _("\
25782 Show debugging of the DWARF DIE reader."), _("\
25783 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25784 The value is the maximum depth to print."),
25787 &setdebuglist, &showdebuglist);
25789 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25790 Set debugging of the dwarf line reader."), _("\
25791 Show debugging of the dwarf line reader."), _("\
25792 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25793 A value of 1 (one) provides basic information.\n\
25794 A value greater than 1 provides more verbose information."),
25797 &setdebuglist, &showdebuglist);
25799 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25800 Set cross-checking of \"physname\" code against demangler."), _("\
25801 Show cross-checking of \"physname\" code against demangler."), _("\
25802 When enabled, GDB's internal \"physname\" code is checked against\n\
25804 NULL, show_check_physname,
25805 &setdebuglist, &showdebuglist);
25807 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25808 no_class, &use_deprecated_index_sections, _("\
25809 Set whether to use deprecated gdb_index sections."), _("\
25810 Show whether to use deprecated gdb_index sections."), _("\
25811 When enabled, deprecated .gdb_index sections are used anyway.\n\
25812 Normally they are ignored either because of a missing feature or\n\
25813 performance issue.\n\
25814 Warning: This option must be enabled before gdb reads the file."),
25817 &setlist, &showlist);
25819 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25820 &dwarf2_locexpr_funcs);
25821 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25822 &dwarf2_loclist_funcs);
25824 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25825 &dwarf2_block_frame_base_locexpr_funcs);
25826 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25827 &dwarf2_block_frame_base_loclist_funcs);
25830 selftests::register_test ("dw2_expand_symtabs_matching",
25831 selftests::dw2_expand_symtabs_matching::run_test);