1 /* DWARF 2 debugging format support for GDB.
3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
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 "gdbsupport/vec.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
71 #include "gdbsupport/filestuff.h"
73 #include "namespace.h"
74 #include "gdbsupport/gdb_unlinker.h"
75 #include "gdbsupport/function-view.h"
76 #include "gdbsupport/gdb_optional.h"
77 #include "gdbsupport/underlying.h"
78 #include "gdbsupport/byte-vector.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
83 #include <sys/types.h>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "gdbsupport/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;
377 /* 64-bit signature of this unit. For type units, it denotes the signature of
378 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
379 Also used in DWARF 5, to denote the dwo id when the unit type is
380 DW_UT_skeleton or DW_UT_split_compile. */
383 /* For types, offset in the type's DIE of the type defined by this TU. */
384 cu_offset type_cu_offset_in_tu;
387 /* Type used for delaying computation of method physnames.
388 See comments for compute_delayed_physnames. */
389 struct delayed_method_info
391 /* The type to which the method is attached, i.e., its parent class. */
394 /* The index of the method in the type's function fieldlists. */
397 /* The index of the method in the fieldlist. */
400 /* The name of the DIE. */
403 /* The DIE associated with this method. */
404 struct die_info *die;
407 /* Internal state when decoding a particular compilation unit. */
410 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
413 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
415 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
416 Create the set of symtabs used by this TU, or if this TU is sharing
417 symtabs with another TU and the symtabs have already been created
418 then restore those symtabs in the line header.
419 We don't need the pc/line-number mapping for type units. */
420 void setup_type_unit_groups (struct die_info *die);
422 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
423 buildsym_compunit constructor. */
424 struct compunit_symtab *start_symtab (const char *name,
425 const char *comp_dir,
428 /* Reset the builder. */
429 void reset_builder () { m_builder.reset (); }
431 /* The header of the compilation unit. */
432 struct comp_unit_head header {};
434 /* Base address of this compilation unit. */
435 CORE_ADDR base_address = 0;
437 /* Non-zero if base_address has been set. */
440 /* The language we are debugging. */
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
444 const char *producer = nullptr;
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
449 std::unique_ptr<buildsym_compunit> m_builder;
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
461 struct pending **list_in_scope = nullptr;
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
465 htab_t partial_dies = nullptr;
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
469 auto_obstack comp_unit_obstack;
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
477 /* Backlink to our per_cu entry. */
478 struct dwarf2_per_cu_data *per_cu;
480 /* How many compilation units ago was this CU last referenced? */
483 /* A hash table of DIE cu_offset for following references with
484 die_info->offset.sect_off as hash. */
485 htab_t die_hash = nullptr;
487 /* Full DIEs if read in. */
488 struct die_info *dies = nullptr;
490 /* A set of pointers to dwarf2_per_cu_data objects for compilation
491 units referenced by this one. Only set during full symbol processing;
492 partial symbol tables do not have dependencies. */
493 htab_t dependencies = nullptr;
495 /* Header data from the line table, during full symbol processing. */
496 struct line_header *line_header = nullptr;
497 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
498 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
499 this is the DW_TAG_compile_unit die for this CU. We'll hold on
500 to the line header as long as this DIE is being processed. See
501 process_die_scope. */
502 die_info *line_header_die_owner = nullptr;
504 /* A list of methods which need to have physnames computed
505 after all type information has been read. */
506 std::vector<delayed_method_info> method_list;
508 /* To be copied to symtab->call_site_htab. */
509 htab_t call_site_htab = nullptr;
511 /* Non-NULL if this CU came from a DWO file.
512 There is an invariant here that is important to remember:
513 Except for attributes copied from the top level DIE in the "main"
514 (or "stub") file in preparation for reading the DWO file
515 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
516 Either there isn't a DWO file (in which case this is NULL and the point
517 is moot), or there is and either we're not going to read it (in which
518 case this is NULL) or there is and we are reading it (in which case this
520 struct dwo_unit *dwo_unit = nullptr;
522 /* The DW_AT_addr_base attribute if present, zero otherwise
523 (zero is a valid value though).
524 Note this value comes from the Fission stub CU/TU's DIE. */
525 ULONGEST addr_base = 0;
527 /* The DW_AT_ranges_base attribute if present, zero otherwise
528 (zero is a valid value though).
529 Note this value comes from the Fission stub CU/TU's DIE.
530 Also note that the value is zero in the non-DWO case so this value can
531 be used without needing to know whether DWO files are in use or not.
532 N.B. This does not apply to DW_AT_ranges appearing in
533 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
534 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
535 DW_AT_ranges_base *would* have to be applied, and we'd have to care
536 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
537 ULONGEST ranges_base = 0;
539 /* When reading debug info generated by older versions of rustc, we
540 have to rewrite some union types to be struct types with a
541 variant part. This rewriting must be done after the CU is fully
542 read in, because otherwise at the point of rewriting some struct
543 type might not have been fully processed. So, we keep a list of
544 all such types here and process them after expansion. */
545 std::vector<struct type *> rust_unions;
547 /* Mark used when releasing cached dies. */
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
571 bool processing_has_namespace_info : 1;
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
594 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
597 struct stmt_list_hash
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
606 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
607 an object of this type. */
609 struct type_unit_group
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab.
615 This PER_CU is recognized by having no section. */
616 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
617 struct dwarf2_per_cu_data per_cu;
619 /* The TUs that share this DW_AT_stmt_list entry.
620 This is added to while parsing type units to build partial symtabs,
621 and is deleted afterwards and not used again. */
622 VEC (sig_type_ptr) *tus;
624 /* The compunit symtab.
625 Type units in a group needn't all be defined in the same source file,
626 so we create an essentially anonymous symtab as the compunit symtab. */
627 struct compunit_symtab *compunit_symtab;
629 /* The data used to construct the hash key. */
630 struct stmt_list_hash hash;
632 /* The number of symtabs from the line header.
633 The value here must match line_header.num_file_names. */
634 unsigned int num_symtabs;
636 /* The symbol tables for this TU (obtained from the files listed in
638 WARNING: The order of entries here must match the order of entries
639 in the line header. After the first TU using this type_unit_group, the
640 line header for the subsequent TUs is recreated from this. This is done
641 because we need to use the same symtabs for each TU using the same
642 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
643 there's no guarantee the line header doesn't have duplicate entries. */
644 struct symtab **symtabs;
647 /* These sections are what may appear in a (real or virtual) DWO file. */
651 struct dwarf2_section_info abbrev;
652 struct dwarf2_section_info line;
653 struct dwarf2_section_info loc;
654 struct dwarf2_section_info loclists;
655 struct dwarf2_section_info macinfo;
656 struct dwarf2_section_info macro;
657 struct dwarf2_section_info str;
658 struct dwarf2_section_info str_offsets;
659 /* In the case of a virtual DWO file, these two are unused. */
660 struct dwarf2_section_info info;
661 std::vector<dwarf2_section_info> types;
664 /* CUs/TUs in DWP/DWO files. */
668 /* Backlink to the containing struct dwo_file. */
669 struct dwo_file *dwo_file;
671 /* The "id" that distinguishes this CU/TU.
672 .debug_info calls this "dwo_id", .debug_types calls this "signature".
673 Since signatures came first, we stick with it for consistency. */
676 /* The section this CU/TU lives in, in the DWO file. */
677 struct dwarf2_section_info *section;
679 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
680 sect_offset sect_off;
683 /* For types, offset in the type's DIE of the type defined by this TU. */
684 cu_offset type_offset_in_tu;
687 /* include/dwarf2.h defines the DWP section codes.
688 It defines a max value but it doesn't define a min value, which we
689 use for error checking, so provide one. */
691 enum dwp_v2_section_ids
696 /* Data for one DWO file.
698 This includes virtual DWO files (a virtual DWO file is a DWO file as it
699 appears in a DWP file). DWP files don't really have DWO files per se -
700 comdat folding of types "loses" the DWO file they came from, and from
701 a high level view DWP files appear to contain a mass of random types.
702 However, to maintain consistency with the non-DWP case we pretend DWP
703 files contain virtual DWO files, and we assign each TU with one virtual
704 DWO file (generally based on the line and abbrev section offsets -
705 a heuristic that seems to work in practice). */
709 dwo_file () = default;
710 DISABLE_COPY_AND_ASSIGN (dwo_file);
712 /* The DW_AT_GNU_dwo_name attribute.
713 For virtual DWO files the name is constructed from the section offsets
714 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
715 from related CU+TUs. */
716 const char *dwo_name = nullptr;
718 /* The DW_AT_comp_dir attribute. */
719 const char *comp_dir = nullptr;
721 /* The bfd, when the file is open. Otherwise this is NULL.
722 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
723 gdb_bfd_ref_ptr dbfd;
725 /* The sections that make up this DWO file.
726 Remember that for virtual DWO files in DWP V2, these are virtual
727 sections (for lack of a better name). */
728 struct dwo_sections sections {};
730 /* The CUs in the file.
731 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
732 an extension to handle LLVM's Link Time Optimization output (where
733 multiple source files may be compiled into a single object/dwo pair). */
736 /* Table of TUs in the file.
737 Each element is a struct dwo_unit. */
741 /* These sections are what may appear in a DWP file. */
745 /* These are used by both DWP version 1 and 2. */
746 struct dwarf2_section_info str;
747 struct dwarf2_section_info cu_index;
748 struct dwarf2_section_info tu_index;
750 /* These are only used by DWP version 2 files.
751 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
752 sections are referenced by section number, and are not recorded here.
753 In DWP version 2 there is at most one copy of all these sections, each
754 section being (effectively) comprised of the concatenation of all of the
755 individual sections that exist in the version 1 format.
756 To keep the code simple we treat each of these concatenated pieces as a
757 section itself (a virtual section?). */
758 struct dwarf2_section_info abbrev;
759 struct dwarf2_section_info info;
760 struct dwarf2_section_info line;
761 struct dwarf2_section_info loc;
762 struct dwarf2_section_info macinfo;
763 struct dwarf2_section_info macro;
764 struct dwarf2_section_info str_offsets;
765 struct dwarf2_section_info types;
768 /* These sections are what may appear in a virtual DWO file in DWP version 1.
769 A virtual DWO file is a DWO file as it appears in a DWP file. */
771 struct virtual_v1_dwo_sections
773 struct dwarf2_section_info abbrev;
774 struct dwarf2_section_info line;
775 struct dwarf2_section_info loc;
776 struct dwarf2_section_info macinfo;
777 struct dwarf2_section_info macro;
778 struct dwarf2_section_info str_offsets;
779 /* Each DWP hash table entry records one CU or one TU.
780 That is recorded here, and copied to dwo_unit.section. */
781 struct dwarf2_section_info info_or_types;
784 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
785 In version 2, the sections of the DWO files are concatenated together
786 and stored in one section of that name. Thus each ELF section contains
787 several "virtual" sections. */
789 struct virtual_v2_dwo_sections
791 bfd_size_type abbrev_offset;
792 bfd_size_type abbrev_size;
794 bfd_size_type line_offset;
795 bfd_size_type line_size;
797 bfd_size_type loc_offset;
798 bfd_size_type loc_size;
800 bfd_size_type macinfo_offset;
801 bfd_size_type macinfo_size;
803 bfd_size_type macro_offset;
804 bfd_size_type macro_size;
806 bfd_size_type str_offsets_offset;
807 bfd_size_type str_offsets_size;
809 /* Each DWP hash table entry records one CU or one TU.
810 That is recorded here, and copied to dwo_unit.section. */
811 bfd_size_type info_or_types_offset;
812 bfd_size_type info_or_types_size;
815 /* Contents of DWP hash tables. */
817 struct dwp_hash_table
819 uint32_t version, nr_columns;
820 uint32_t nr_units, nr_slots;
821 const gdb_byte *hash_table, *unit_table;
826 const gdb_byte *indices;
830 /* This is indexed by column number and gives the id of the section
832 #define MAX_NR_V2_DWO_SECTIONS \
833 (1 /* .debug_info or .debug_types */ \
834 + 1 /* .debug_abbrev */ \
835 + 1 /* .debug_line */ \
836 + 1 /* .debug_loc */ \
837 + 1 /* .debug_str_offsets */ \
838 + 1 /* .debug_macro or .debug_macinfo */)
839 int section_ids[MAX_NR_V2_DWO_SECTIONS];
840 const gdb_byte *offsets;
841 const gdb_byte *sizes;
846 /* Data for one DWP file. */
850 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
852 dbfd (std::move (abfd))
856 /* Name of the file. */
859 /* File format version. */
863 gdb_bfd_ref_ptr dbfd;
865 /* Section info for this file. */
866 struct dwp_sections sections {};
868 /* Table of CUs in the file. */
869 const struct dwp_hash_table *cus = nullptr;
871 /* Table of TUs in the file. */
872 const struct dwp_hash_table *tus = nullptr;
874 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
875 htab_t loaded_cus {};
876 htab_t loaded_tus {};
878 /* Table to map ELF section numbers to their sections.
879 This is only needed for the DWP V1 file format. */
880 unsigned int num_sections = 0;
881 asection **elf_sections = nullptr;
884 /* Struct used to pass misc. parameters to read_die_and_children, et
885 al. which are used for both .debug_info and .debug_types dies.
886 All parameters here are unchanging for the life of the call. This
887 struct exists to abstract away the constant parameters of die reading. */
889 struct die_reader_specs
891 /* The bfd of die_section. */
894 /* The CU of the DIE we are parsing. */
895 struct dwarf2_cu *cu;
897 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
898 struct dwo_file *dwo_file;
900 /* The section the die comes from.
901 This is either .debug_info or .debug_types, or the .dwo variants. */
902 struct dwarf2_section_info *die_section;
904 /* die_section->buffer. */
905 const gdb_byte *buffer;
907 /* The end of the buffer. */
908 const gdb_byte *buffer_end;
910 /* The value of the DW_AT_comp_dir attribute. */
911 const char *comp_dir;
913 /* The abbreviation table to use when reading the DIEs. */
914 struct abbrev_table *abbrev_table;
917 /* Type of function passed to init_cutu_and_read_dies, et.al. */
918 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
919 const gdb_byte *info_ptr,
920 struct die_info *comp_unit_die,
924 /* A 1-based directory index. This is a strong typedef to prevent
925 accidentally using a directory index as a 0-based index into an
927 enum class dir_index : unsigned int {};
929 /* Likewise, a 1-based file name index. */
930 enum class file_name_index : unsigned int {};
934 file_entry () = default;
936 file_entry (const char *name_, dir_index d_index_,
937 unsigned int mod_time_, unsigned int length_)
940 mod_time (mod_time_),
944 /* Return the include directory at D_INDEX stored in LH. Returns
945 NULL if D_INDEX is out of bounds. */
946 const char *include_dir (const line_header *lh) const;
948 /* The file name. Note this is an observing pointer. The memory is
949 owned by debug_line_buffer. */
952 /* The directory index (1-based). */
953 dir_index d_index {};
955 unsigned int mod_time {};
957 unsigned int length {};
959 /* True if referenced by the Line Number Program. */
962 /* The associated symbol table, if any. */
963 struct symtab *symtab {};
966 /* The line number information for a compilation unit (found in the
967 .debug_line section) begins with a "statement program header",
968 which contains the following information. */
975 /* Add an entry to the include directory table. */
976 void add_include_dir (const char *include_dir);
978 /* Add an entry to the file name table. */
979 void add_file_name (const char *name, dir_index d_index,
980 unsigned int mod_time, unsigned int length);
982 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
984 const char *include_dir_at (dir_index index) const
986 /* Convert directory index number (1-based) to vector index
988 size_t vec_index = to_underlying (index) - 1;
990 if (vec_index >= include_dirs.size ())
992 return include_dirs[vec_index];
995 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
997 file_entry *file_name_at (file_name_index index)
999 /* Convert file name index number (1-based) to vector index
1001 size_t vec_index = to_underlying (index) - 1;
1003 if (vec_index >= file_names.size ())
1005 return &file_names[vec_index];
1008 /* Offset of line number information in .debug_line section. */
1009 sect_offset sect_off {};
1011 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1012 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1014 unsigned int total_length {};
1015 unsigned short version {};
1016 unsigned int header_length {};
1017 unsigned char minimum_instruction_length {};
1018 unsigned char maximum_ops_per_instruction {};
1019 unsigned char default_is_stmt {};
1021 unsigned char line_range {};
1022 unsigned char opcode_base {};
1024 /* standard_opcode_lengths[i] is the number of operands for the
1025 standard opcode whose value is i. This means that
1026 standard_opcode_lengths[0] is unused, and the last meaningful
1027 element is standard_opcode_lengths[opcode_base - 1]. */
1028 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1030 /* The include_directories table. Note these are observing
1031 pointers. The memory is owned by debug_line_buffer. */
1032 std::vector<const char *> include_dirs;
1034 /* The file_names table. */
1035 std::vector<file_entry> file_names;
1037 /* The start and end of the statement program following this
1038 header. These point into dwarf2_per_objfile->line_buffer. */
1039 const gdb_byte *statement_program_start {}, *statement_program_end {};
1042 typedef std::unique_ptr<line_header> line_header_up;
1045 file_entry::include_dir (const line_header *lh) const
1047 return lh->include_dir_at (d_index);
1050 /* When we construct a partial symbol table entry we only
1051 need this much information. */
1052 struct partial_die_info : public allocate_on_obstack
1054 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1056 /* Disable assign but still keep copy ctor, which is needed
1057 load_partial_dies. */
1058 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1060 /* Adjust the partial die before generating a symbol for it. This
1061 function may set the is_external flag or change the DIE's
1063 void fixup (struct dwarf2_cu *cu);
1065 /* Read a minimal amount of information into the minimal die
1067 const gdb_byte *read (const struct die_reader_specs *reader,
1068 const struct abbrev_info &abbrev,
1069 const gdb_byte *info_ptr);
1071 /* Offset of this DIE. */
1072 const sect_offset sect_off;
1074 /* DWARF-2 tag for this DIE. */
1075 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1077 /* Assorted flags describing the data found in this DIE. */
1078 const unsigned int has_children : 1;
1080 unsigned int is_external : 1;
1081 unsigned int is_declaration : 1;
1082 unsigned int has_type : 1;
1083 unsigned int has_specification : 1;
1084 unsigned int has_pc_info : 1;
1085 unsigned int may_be_inlined : 1;
1087 /* This DIE has been marked DW_AT_main_subprogram. */
1088 unsigned int main_subprogram : 1;
1090 /* Flag set if the SCOPE field of this structure has been
1092 unsigned int scope_set : 1;
1094 /* Flag set if the DIE has a byte_size attribute. */
1095 unsigned int has_byte_size : 1;
1097 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1098 unsigned int has_const_value : 1;
1100 /* Flag set if any of the DIE's children are template arguments. */
1101 unsigned int has_template_arguments : 1;
1103 /* Flag set if fixup has been called on this die. */
1104 unsigned int fixup_called : 1;
1106 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1107 unsigned int is_dwz : 1;
1109 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1110 unsigned int spec_is_dwz : 1;
1112 /* The name of this DIE. Normally the value of DW_AT_name, but
1113 sometimes a default name for unnamed DIEs. */
1114 const char *name = nullptr;
1116 /* The linkage name, if present. */
1117 const char *linkage_name = nullptr;
1119 /* The scope to prepend to our children. This is generally
1120 allocated on the comp_unit_obstack, so will disappear
1121 when this compilation unit leaves the cache. */
1122 const char *scope = nullptr;
1124 /* Some data associated with the partial DIE. The tag determines
1125 which field is live. */
1128 /* The location description associated with this DIE, if any. */
1129 struct dwarf_block *locdesc;
1130 /* The offset of an import, for DW_TAG_imported_unit. */
1131 sect_offset sect_off;
1134 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1135 CORE_ADDR lowpc = 0;
1136 CORE_ADDR highpc = 0;
1138 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1139 DW_AT_sibling, if any. */
1140 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1141 could return DW_AT_sibling values to its caller load_partial_dies. */
1142 const gdb_byte *sibling = nullptr;
1144 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1145 DW_AT_specification (or DW_AT_abstract_origin or
1146 DW_AT_extension). */
1147 sect_offset spec_offset {};
1149 /* Pointers to this DIE's parent, first child, and next sibling,
1151 struct partial_die_info *die_parent = nullptr;
1152 struct partial_die_info *die_child = nullptr;
1153 struct partial_die_info *die_sibling = nullptr;
1155 friend struct partial_die_info *
1156 dwarf2_cu::find_partial_die (sect_offset sect_off);
1159 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1160 partial_die_info (sect_offset sect_off)
1161 : partial_die_info (sect_off, DW_TAG_padding, 0)
1165 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1167 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1172 has_specification = 0;
1175 main_subprogram = 0;
1178 has_const_value = 0;
1179 has_template_arguments = 0;
1186 /* This data structure holds the information of an abbrev. */
1189 unsigned int number; /* number identifying abbrev */
1190 enum dwarf_tag tag; /* dwarf tag */
1191 unsigned short has_children; /* boolean */
1192 unsigned short num_attrs; /* number of attributes */
1193 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1194 struct abbrev_info *next; /* next in chain */
1199 ENUM_BITFIELD(dwarf_attribute) name : 16;
1200 ENUM_BITFIELD(dwarf_form) form : 16;
1202 /* It is valid only if FORM is DW_FORM_implicit_const. */
1203 LONGEST implicit_const;
1206 /* Size of abbrev_table.abbrev_hash_table. */
1207 #define ABBREV_HASH_SIZE 121
1209 /* Top level data structure to contain an abbreviation table. */
1213 explicit abbrev_table (sect_offset off)
1217 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1218 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1221 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1223 /* Allocate space for a struct abbrev_info object in
1225 struct abbrev_info *alloc_abbrev ();
1227 /* Add an abbreviation to the table. */
1228 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1230 /* Look up an abbrev in the table.
1231 Returns NULL if the abbrev is not found. */
1233 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1236 /* Where the abbrev table came from.
1237 This is used as a sanity check when the table is used. */
1238 const sect_offset sect_off;
1240 /* Storage for the abbrev table. */
1241 auto_obstack abbrev_obstack;
1245 /* Hash table of abbrevs.
1246 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1247 It could be statically allocated, but the previous code didn't so we
1249 struct abbrev_info **m_abbrevs;
1252 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1254 /* Attributes have a name and a value. */
1257 ENUM_BITFIELD(dwarf_attribute) name : 16;
1258 ENUM_BITFIELD(dwarf_form) form : 15;
1260 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1261 field should be in u.str (existing only for DW_STRING) but it is kept
1262 here for better struct attribute alignment. */
1263 unsigned int string_is_canonical : 1;
1268 struct dwarf_block *blk;
1277 /* This data structure holds a complete die structure. */
1280 /* DWARF-2 tag for this DIE. */
1281 ENUM_BITFIELD(dwarf_tag) tag : 16;
1283 /* Number of attributes */
1284 unsigned char num_attrs;
1286 /* True if we're presently building the full type name for the
1287 type derived from this DIE. */
1288 unsigned char building_fullname : 1;
1290 /* True if this die is in process. PR 16581. */
1291 unsigned char in_process : 1;
1294 unsigned int abbrev;
1296 /* Offset in .debug_info or .debug_types section. */
1297 sect_offset sect_off;
1299 /* The dies in a compilation unit form an n-ary tree. PARENT
1300 points to this die's parent; CHILD points to the first child of
1301 this node; and all the children of a given node are chained
1302 together via their SIBLING fields. */
1303 struct die_info *child; /* Its first child, if any. */
1304 struct die_info *sibling; /* Its next sibling, if any. */
1305 struct die_info *parent; /* Its parent, if any. */
1307 /* An array of attributes, with NUM_ATTRS elements. There may be
1308 zero, but it's not common and zero-sized arrays are not
1309 sufficiently portable C. */
1310 struct attribute attrs[1];
1313 /* Get at parts of an attribute structure. */
1315 #define DW_STRING(attr) ((attr)->u.str)
1316 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1317 #define DW_UNSND(attr) ((attr)->u.unsnd)
1318 #define DW_BLOCK(attr) ((attr)->u.blk)
1319 #define DW_SND(attr) ((attr)->u.snd)
1320 #define DW_ADDR(attr) ((attr)->u.addr)
1321 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1323 /* Blocks are a bunch of untyped bytes. */
1328 /* Valid only if SIZE is not zero. */
1329 const gdb_byte *data;
1332 #ifndef ATTR_ALLOC_CHUNK
1333 #define ATTR_ALLOC_CHUNK 4
1336 /* Allocate fields for structs, unions and enums in this size. */
1337 #ifndef DW_FIELD_ALLOC_CHUNK
1338 #define DW_FIELD_ALLOC_CHUNK 4
1341 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1342 but this would require a corresponding change in unpack_field_as_long
1344 static int bits_per_byte = 8;
1346 /* When reading a variant or variant part, we track a bit more
1347 information about the field, and store it in an object of this
1350 struct variant_field
1352 /* If we see a DW_TAG_variant, then this will be the discriminant
1354 ULONGEST discriminant_value;
1355 /* If we see a DW_TAG_variant, then this will be set if this is the
1357 bool default_branch;
1358 /* While reading a DW_TAG_variant_part, this will be set if this
1359 field is the discriminant. */
1360 bool is_discriminant;
1365 int accessibility = 0;
1367 /* Extra information to describe a variant or variant part. */
1368 struct variant_field variant {};
1369 struct field field {};
1374 const char *name = nullptr;
1375 std::vector<struct fn_field> fnfields;
1378 /* The routines that read and process dies for a C struct or C++ class
1379 pass lists of data member fields and lists of member function fields
1380 in an instance of a field_info structure, as defined below. */
1383 /* List of data member and baseclasses fields. */
1384 std::vector<struct nextfield> fields;
1385 std::vector<struct nextfield> baseclasses;
1387 /* Number of fields (including baseclasses). */
1390 /* Set if the accesibility of one of the fields is not public. */
1391 int non_public_fields = 0;
1393 /* Member function fieldlist array, contains name of possibly overloaded
1394 member function, number of overloaded member functions and a pointer
1395 to the head of the member function field chain. */
1396 std::vector<struct fnfieldlist> fnfieldlists;
1398 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1399 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1400 std::vector<struct decl_field> typedef_field_list;
1402 /* Nested types defined by this class and the number of elements in this
1404 std::vector<struct decl_field> nested_types_list;
1407 /* One item on the queue of compilation units to read in full symbols
1409 struct dwarf2_queue_item
1411 struct dwarf2_per_cu_data *per_cu;
1412 enum language pretend_language;
1413 struct dwarf2_queue_item *next;
1416 /* The current queue. */
1417 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1419 /* Loaded secondary compilation units are kept in memory until they
1420 have not been referenced for the processing of this many
1421 compilation units. Set this to zero to disable caching. Cache
1422 sizes of up to at least twenty will improve startup time for
1423 typical inter-CU-reference binaries, at an obvious memory cost. */
1424 static int dwarf_max_cache_age = 5;
1426 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1427 struct cmd_list_element *c, const char *value)
1429 fprintf_filtered (file, _("The upper bound on the age of cached "
1430 "DWARF compilation units is %s.\n"),
1434 /* local function prototypes */
1436 static const char *get_section_name (const struct dwarf2_section_info *);
1438 static const char *get_section_file_name (const struct dwarf2_section_info *);
1440 static void dwarf2_find_base_address (struct die_info *die,
1441 struct dwarf2_cu *cu);
1443 static struct partial_symtab *create_partial_symtab
1444 (struct dwarf2_per_cu_data *per_cu, const char *name);
1446 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1447 const gdb_byte *info_ptr,
1448 struct die_info *type_unit_die,
1449 int has_children, void *data);
1451 static void dwarf2_build_psymtabs_hard
1452 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1454 static void scan_partial_symbols (struct partial_die_info *,
1455 CORE_ADDR *, CORE_ADDR *,
1456 int, struct dwarf2_cu *);
1458 static void add_partial_symbol (struct partial_die_info *,
1459 struct dwarf2_cu *);
1461 static void add_partial_namespace (struct partial_die_info *pdi,
1462 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1463 int set_addrmap, struct dwarf2_cu *cu);
1465 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1466 CORE_ADDR *highpc, int set_addrmap,
1467 struct dwarf2_cu *cu);
1469 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1470 struct dwarf2_cu *cu);
1472 static void add_partial_subprogram (struct partial_die_info *pdi,
1473 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1474 int need_pc, struct dwarf2_cu *cu);
1476 static void dwarf2_read_symtab (struct partial_symtab *,
1479 static void psymtab_to_symtab_1 (struct partial_symtab *);
1481 static abbrev_table_up abbrev_table_read_table
1482 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1485 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1487 static struct partial_die_info *load_partial_dies
1488 (const struct die_reader_specs *, const gdb_byte *, int);
1490 /* A pair of partial_die_info and compilation unit. */
1491 struct cu_partial_die_info
1493 /* The compilation unit of the partial_die_info. */
1494 struct dwarf2_cu *cu;
1495 /* A partial_die_info. */
1496 struct partial_die_info *pdi;
1498 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1504 cu_partial_die_info () = delete;
1507 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1508 struct dwarf2_cu *);
1510 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1511 struct attribute *, struct attr_abbrev *,
1514 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1516 static int read_1_signed_byte (bfd *, const gdb_byte *);
1518 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1520 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1521 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1523 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1525 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1527 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1530 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1532 static LONGEST read_checked_initial_length_and_offset
1533 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1534 unsigned int *, unsigned int *);
1536 static LONGEST read_offset (bfd *, const gdb_byte *,
1537 const struct comp_unit_head *,
1540 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1542 static sect_offset read_abbrev_offset
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1544 struct dwarf2_section_info *, sect_offset);
1546 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1548 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1550 static const char *read_indirect_string
1551 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1552 const struct comp_unit_head *, unsigned int *);
1554 static const char *read_indirect_line_string
1555 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1556 const struct comp_unit_head *, unsigned int *);
1558 static const char *read_indirect_string_at_offset
1559 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1560 LONGEST str_offset);
1562 static const char *read_indirect_string_from_dwz
1563 (struct objfile *objfile, struct dwz_file *, LONGEST);
1565 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1567 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1571 static const char *read_str_index (const struct die_reader_specs *reader,
1572 ULONGEST str_index);
1574 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1576 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1577 struct dwarf2_cu *);
1579 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1582 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1583 struct dwarf2_cu *cu);
1585 static const char *dwarf2_dwo_name (struct die_info *die, 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_unit_type_name (int unit_type);
1771 static const char *dwarf_form_name (unsigned int);
1773 static const char *dwarf_bool_name (unsigned int);
1775 static const char *dwarf_type_encoding_name (unsigned int);
1777 static struct die_info *sibling_die (struct die_info *);
1779 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1781 static void dump_die_for_error (struct die_info *);
1783 static void dump_die_1 (struct ui_file *, int level, int max_level,
1786 /*static*/ void dump_die (struct die_info *, int max_level);
1788 static void store_in_ref_table (struct die_info *,
1789 struct dwarf2_cu *);
1791 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1793 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1795 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1796 const struct attribute *,
1797 struct dwarf2_cu **);
1799 static struct die_info *follow_die_ref (struct die_info *,
1800 const struct attribute *,
1801 struct dwarf2_cu **);
1803 static struct die_info *follow_die_sig (struct die_info *,
1804 const struct attribute *,
1805 struct dwarf2_cu **);
1807 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1808 struct dwarf2_cu *);
1810 static struct type *get_DW_AT_signature_type (struct die_info *,
1811 const struct attribute *,
1812 struct dwarf2_cu *);
1814 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1816 static void read_signatured_type (struct signatured_type *);
1818 static int attr_to_dynamic_prop (const struct attribute *attr,
1819 struct die_info *die, struct dwarf2_cu *cu,
1820 struct dynamic_prop *prop, struct type *type);
1822 /* memory allocation interface */
1824 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1826 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1828 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1830 static int attr_form_is_block (const struct attribute *);
1832 static int attr_form_is_section_offset (const struct attribute *);
1834 static int attr_form_is_constant (const struct attribute *);
1836 static int attr_form_is_ref (const struct attribute *);
1838 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1839 struct dwarf2_loclist_baton *baton,
1840 const struct attribute *attr);
1842 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1844 struct dwarf2_cu *cu,
1847 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1848 const gdb_byte *info_ptr,
1849 struct abbrev_info *abbrev);
1851 static hashval_t partial_die_hash (const void *item);
1853 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1855 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1856 (sect_offset sect_off, unsigned int offset_in_dwz,
1857 struct dwarf2_per_objfile *dwarf2_per_objfile);
1859 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1860 struct die_info *comp_unit_die,
1861 enum language pretend_language);
1863 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1865 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1867 static struct type *set_die_type (struct die_info *, struct type *,
1868 struct dwarf2_cu *);
1870 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1872 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1874 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1877 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1880 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1883 static void dwarf2_add_dependence (struct dwarf2_cu *,
1884 struct dwarf2_per_cu_data *);
1886 static void dwarf2_mark (struct dwarf2_cu *);
1888 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1890 static struct type *get_die_type_at_offset (sect_offset,
1891 struct dwarf2_per_cu_data *);
1893 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1895 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1896 enum language pretend_language);
1898 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1900 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1901 static struct type *dwarf2_per_cu_addr_sized_int_type
1902 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1904 /* Class, the destructor of which frees all allocated queue entries. This
1905 will only have work to do if an error was thrown while processing the
1906 dwarf. If no error was thrown then the queue entries should have all
1907 been processed, and freed, as we went along. */
1909 class dwarf2_queue_guard
1912 dwarf2_queue_guard () = default;
1914 /* Free any entries remaining on the queue. There should only be
1915 entries left if we hit an error while processing the dwarf. */
1916 ~dwarf2_queue_guard ()
1918 struct dwarf2_queue_item *item, *last;
1920 item = dwarf2_queue;
1923 /* Anything still marked queued is likely to be in an
1924 inconsistent state, so discard it. */
1925 if (item->per_cu->queued)
1927 if (item->per_cu->cu != NULL)
1928 free_one_cached_comp_unit (item->per_cu);
1929 item->per_cu->queued = 0;
1937 dwarf2_queue = dwarf2_queue_tail = NULL;
1941 /* The return type of find_file_and_directory. Note, the enclosed
1942 string pointers are only valid while this object is valid. */
1944 struct file_and_directory
1946 /* The filename. This is never NULL. */
1949 /* The compilation directory. NULL if not known. If we needed to
1950 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1951 points directly to the DW_AT_comp_dir string attribute owned by
1952 the obstack that owns the DIE. */
1953 const char *comp_dir;
1955 /* If we needed to build a new string for comp_dir, this is what
1956 owns the storage. */
1957 std::string comp_dir_storage;
1960 static file_and_directory find_file_and_directory (struct die_info *die,
1961 struct dwarf2_cu *cu);
1963 static char *file_full_name (int file, struct line_header *lh,
1964 const char *comp_dir);
1966 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1967 enum class rcuh_kind { COMPILE, TYPE };
1969 static const gdb_byte *read_and_check_comp_unit_head
1970 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1971 struct comp_unit_head *header,
1972 struct dwarf2_section_info *section,
1973 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1974 rcuh_kind section_kind);
1976 static void init_cutu_and_read_dies
1977 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1978 int use_existing_cu, int keep, bool skip_partial,
1979 die_reader_func_ftype *die_reader_func, void *data);
1981 static void init_cutu_and_read_dies_simple
1982 (struct dwarf2_per_cu_data *this_cu,
1983 die_reader_func_ftype *die_reader_func, void *data);
1985 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1987 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1989 static struct dwo_unit *lookup_dwo_unit_in_dwp
1990 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1991 struct dwp_file *dwp_file, const char *comp_dir,
1992 ULONGEST signature, int is_debug_types);
1994 static struct dwp_file *get_dwp_file
1995 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1997 static struct dwo_unit *lookup_dwo_comp_unit
1998 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2000 static struct dwo_unit *lookup_dwo_type_unit
2001 (struct signatured_type *, const char *, const char *);
2003 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2005 /* A unique pointer to a dwo_file. */
2007 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2009 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2011 static void check_producer (struct dwarf2_cu *cu);
2013 static void free_line_header_voidp (void *arg);
2015 /* Various complaints about symbol reading that don't abort the process. */
2018 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2020 complaint (_("statement list doesn't fit in .debug_line section"));
2024 dwarf2_debug_line_missing_file_complaint (void)
2026 complaint (_(".debug_line section has line data without a file"));
2030 dwarf2_debug_line_missing_end_sequence_complaint (void)
2032 complaint (_(".debug_line section has line "
2033 "program sequence without an end"));
2037 dwarf2_complex_location_expr_complaint (void)
2039 complaint (_("location expression too complex"));
2043 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2046 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2051 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2053 complaint (_("debug info runs off end of %s section"
2055 get_section_name (section),
2056 get_section_file_name (section));
2060 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2062 complaint (_("macro debug info contains a "
2063 "malformed macro definition:\n`%s'"),
2068 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2070 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2074 /* Hash function for line_header_hash. */
2077 line_header_hash (const struct line_header *ofs)
2079 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2082 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2085 line_header_hash_voidp (const void *item)
2087 const struct line_header *ofs = (const struct line_header *) item;
2089 return line_header_hash (ofs);
2092 /* Equality function for line_header_hash. */
2095 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2097 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2098 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2100 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2101 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2106 /* Read the given attribute value as an address, taking the attribute's
2107 form into account. */
2110 attr_value_as_address (struct attribute *attr)
2114 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2115 && attr->form != DW_FORM_GNU_addr_index)
2117 /* Aside from a few clearly defined exceptions, attributes that
2118 contain an address must always be in DW_FORM_addr form.
2119 Unfortunately, some compilers happen to be violating this
2120 requirement by encoding addresses using other forms, such
2121 as DW_FORM_data4 for example. For those broken compilers,
2122 we try to do our best, without any guarantee of success,
2123 to interpret the address correctly. It would also be nice
2124 to generate a complaint, but that would require us to maintain
2125 a list of legitimate cases where a non-address form is allowed,
2126 as well as update callers to pass in at least the CU's DWARF
2127 version. This is more overhead than what we're willing to
2128 expand for a pretty rare case. */
2129 addr = DW_UNSND (attr);
2132 addr = DW_ADDR (attr);
2137 /* See declaration. */
2139 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2140 const dwarf2_debug_sections *names)
2141 : objfile (objfile_)
2144 names = &dwarf2_elf_names;
2146 bfd *obfd = objfile->obfd;
2148 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2149 locate_sections (obfd, sec, *names);
2152 dwarf2_per_objfile::~dwarf2_per_objfile ()
2154 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2155 free_cached_comp_units ();
2157 if (quick_file_names_table)
2158 htab_delete (quick_file_names_table);
2160 if (line_header_hash)
2161 htab_delete (line_header_hash);
2163 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2164 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
2166 for (signatured_type *sig_type : all_type_units)
2167 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
2169 /* Everything else should be on the objfile obstack. */
2172 /* See declaration. */
2175 dwarf2_per_objfile::free_cached_comp_units ()
2177 dwarf2_per_cu_data *per_cu = read_in_chain;
2178 dwarf2_per_cu_data **last_chain = &read_in_chain;
2179 while (per_cu != NULL)
2181 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2184 *last_chain = next_cu;
2189 /* A helper class that calls free_cached_comp_units on
2192 class free_cached_comp_units
2196 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2197 : m_per_objfile (per_objfile)
2201 ~free_cached_comp_units ()
2203 m_per_objfile->free_cached_comp_units ();
2206 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2210 dwarf2_per_objfile *m_per_objfile;
2213 /* Try to locate the sections we need for DWARF 2 debugging
2214 information and return true if we have enough to do something.
2215 NAMES points to the dwarf2 section names, or is NULL if the standard
2216 ELF names are used. */
2219 dwarf2_has_info (struct objfile *objfile,
2220 const struct dwarf2_debug_sections *names)
2222 if (objfile->flags & OBJF_READNEVER)
2225 struct dwarf2_per_objfile *dwarf2_per_objfile
2226 = get_dwarf2_per_objfile (objfile);
2228 if (dwarf2_per_objfile == NULL)
2229 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2232 return (!dwarf2_per_objfile->info.is_virtual
2233 && dwarf2_per_objfile->info.s.section != NULL
2234 && !dwarf2_per_objfile->abbrev.is_virtual
2235 && dwarf2_per_objfile->abbrev.s.section != NULL);
2238 /* Return the containing section of virtual section SECTION. */
2240 static struct dwarf2_section_info *
2241 get_containing_section (const struct dwarf2_section_info *section)
2243 gdb_assert (section->is_virtual);
2244 return section->s.containing_section;
2247 /* Return the bfd owner of SECTION. */
2250 get_section_bfd_owner (const struct dwarf2_section_info *section)
2252 if (section->is_virtual)
2254 section = get_containing_section (section);
2255 gdb_assert (!section->is_virtual);
2257 return section->s.section->owner;
2260 /* Return the bfd section of SECTION.
2261 Returns NULL if the section is not present. */
2264 get_section_bfd_section (const struct dwarf2_section_info *section)
2266 if (section->is_virtual)
2268 section = get_containing_section (section);
2269 gdb_assert (!section->is_virtual);
2271 return section->s.section;
2274 /* Return the name of SECTION. */
2277 get_section_name (const struct dwarf2_section_info *section)
2279 asection *sectp = get_section_bfd_section (section);
2281 gdb_assert (sectp != NULL);
2282 return bfd_section_name (get_section_bfd_owner (section), sectp);
2285 /* Return the name of the file SECTION is in. */
2288 get_section_file_name (const struct dwarf2_section_info *section)
2290 bfd *abfd = get_section_bfd_owner (section);
2292 return bfd_get_filename (abfd);
2295 /* Return the id of SECTION.
2296 Returns 0 if SECTION doesn't exist. */
2299 get_section_id (const struct dwarf2_section_info *section)
2301 asection *sectp = get_section_bfd_section (section);
2308 /* Return the flags of SECTION.
2309 SECTION (or containing section if this is a virtual section) must exist. */
2312 get_section_flags (const struct dwarf2_section_info *section)
2314 asection *sectp = get_section_bfd_section (section);
2316 gdb_assert (sectp != NULL);
2317 return bfd_get_section_flags (sectp->owner, sectp);
2320 /* When loading sections, we look either for uncompressed section or for
2321 compressed section names. */
2324 section_is_p (const char *section_name,
2325 const struct dwarf2_section_names *names)
2327 if (names->normal != NULL
2328 && strcmp (section_name, names->normal) == 0)
2330 if (names->compressed != NULL
2331 && strcmp (section_name, names->compressed) == 0)
2336 /* See declaration. */
2339 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2340 const dwarf2_debug_sections &names)
2342 flagword aflag = bfd_get_section_flags (abfd, sectp);
2344 if ((aflag & SEC_HAS_CONTENTS) == 0)
2347 else if (section_is_p (sectp->name, &names.info))
2349 this->info.s.section = sectp;
2350 this->info.size = bfd_get_section_size (sectp);
2352 else if (section_is_p (sectp->name, &names.abbrev))
2354 this->abbrev.s.section = sectp;
2355 this->abbrev.size = bfd_get_section_size (sectp);
2357 else if (section_is_p (sectp->name, &names.line))
2359 this->line.s.section = sectp;
2360 this->line.size = bfd_get_section_size (sectp);
2362 else if (section_is_p (sectp->name, &names.loc))
2364 this->loc.s.section = sectp;
2365 this->loc.size = bfd_get_section_size (sectp);
2367 else if (section_is_p (sectp->name, &names.loclists))
2369 this->loclists.s.section = sectp;
2370 this->loclists.size = bfd_get_section_size (sectp);
2372 else if (section_is_p (sectp->name, &names.macinfo))
2374 this->macinfo.s.section = sectp;
2375 this->macinfo.size = bfd_get_section_size (sectp);
2377 else if (section_is_p (sectp->name, &names.macro))
2379 this->macro.s.section = sectp;
2380 this->macro.size = bfd_get_section_size (sectp);
2382 else if (section_is_p (sectp->name, &names.str))
2384 this->str.s.section = sectp;
2385 this->str.size = bfd_get_section_size (sectp);
2387 else if (section_is_p (sectp->name, &names.line_str))
2389 this->line_str.s.section = sectp;
2390 this->line_str.size = bfd_get_section_size (sectp);
2392 else if (section_is_p (sectp->name, &names.addr))
2394 this->addr.s.section = sectp;
2395 this->addr.size = bfd_get_section_size (sectp);
2397 else if (section_is_p (sectp->name, &names.frame))
2399 this->frame.s.section = sectp;
2400 this->frame.size = bfd_get_section_size (sectp);
2402 else if (section_is_p (sectp->name, &names.eh_frame))
2404 this->eh_frame.s.section = sectp;
2405 this->eh_frame.size = bfd_get_section_size (sectp);
2407 else if (section_is_p (sectp->name, &names.ranges))
2409 this->ranges.s.section = sectp;
2410 this->ranges.size = bfd_get_section_size (sectp);
2412 else if (section_is_p (sectp->name, &names.rnglists))
2414 this->rnglists.s.section = sectp;
2415 this->rnglists.size = bfd_get_section_size (sectp);
2417 else if (section_is_p (sectp->name, &names.types))
2419 struct dwarf2_section_info type_section;
2421 memset (&type_section, 0, sizeof (type_section));
2422 type_section.s.section = sectp;
2423 type_section.size = bfd_get_section_size (sectp);
2425 this->types.push_back (type_section);
2427 else if (section_is_p (sectp->name, &names.gdb_index))
2429 this->gdb_index.s.section = sectp;
2430 this->gdb_index.size = bfd_get_section_size (sectp);
2432 else if (section_is_p (sectp->name, &names.debug_names))
2434 this->debug_names.s.section = sectp;
2435 this->debug_names.size = bfd_get_section_size (sectp);
2437 else if (section_is_p (sectp->name, &names.debug_aranges))
2439 this->debug_aranges.s.section = sectp;
2440 this->debug_aranges.size = bfd_get_section_size (sectp);
2443 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2444 && bfd_section_vma (abfd, sectp) == 0)
2445 this->has_section_at_zero = true;
2448 /* A helper function that decides whether a section is empty,
2452 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2454 if (section->is_virtual)
2455 return section->size == 0;
2456 return section->s.section == NULL || section->size == 0;
2459 /* See dwarf2read.h. */
2462 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2466 gdb_byte *buf, *retbuf;
2470 info->buffer = NULL;
2471 info->readin = true;
2473 if (dwarf2_section_empty_p (info))
2476 sectp = get_section_bfd_section (info);
2478 /* If this is a virtual section we need to read in the real one first. */
2479 if (info->is_virtual)
2481 struct dwarf2_section_info *containing_section =
2482 get_containing_section (info);
2484 gdb_assert (sectp != NULL);
2485 if ((sectp->flags & SEC_RELOC) != 0)
2487 error (_("Dwarf Error: DWP format V2 with relocations is not"
2488 " supported in section %s [in module %s]"),
2489 get_section_name (info), get_section_file_name (info));
2491 dwarf2_read_section (objfile, containing_section);
2492 /* Other code should have already caught virtual sections that don't
2494 gdb_assert (info->virtual_offset + info->size
2495 <= containing_section->size);
2496 /* If the real section is empty or there was a problem reading the
2497 section we shouldn't get here. */
2498 gdb_assert (containing_section->buffer != NULL);
2499 info->buffer = containing_section->buffer + info->virtual_offset;
2503 /* If the section has relocations, we must read it ourselves.
2504 Otherwise we attach it to the BFD. */
2505 if ((sectp->flags & SEC_RELOC) == 0)
2507 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2511 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2514 /* When debugging .o files, we may need to apply relocations; see
2515 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2516 We never compress sections in .o files, so we only need to
2517 try this when the section is not compressed. */
2518 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2521 info->buffer = retbuf;
2525 abfd = get_section_bfd_owner (info);
2526 gdb_assert (abfd != NULL);
2528 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2529 || bfd_bread (buf, info->size, abfd) != info->size)
2531 error (_("Dwarf Error: Can't read DWARF data"
2532 " in section %s [in module %s]"),
2533 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2537 /* A helper function that returns the size of a section in a safe way.
2538 If you are positive that the section has been read before using the
2539 size, then it is safe to refer to the dwarf2_section_info object's
2540 "size" field directly. In other cases, you must call this
2541 function, because for compressed sections the size field is not set
2542 correctly until the section has been read. */
2544 static bfd_size_type
2545 dwarf2_section_size (struct objfile *objfile,
2546 struct dwarf2_section_info *info)
2549 dwarf2_read_section (objfile, info);
2553 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2557 dwarf2_get_section_info (struct objfile *objfile,
2558 enum dwarf2_section_enum sect,
2559 asection **sectp, const gdb_byte **bufp,
2560 bfd_size_type *sizep)
2562 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2563 struct dwarf2_section_info *info;
2565 /* We may see an objfile without any DWARF, in which case we just
2576 case DWARF2_DEBUG_FRAME:
2577 info = &data->frame;
2579 case DWARF2_EH_FRAME:
2580 info = &data->eh_frame;
2583 gdb_assert_not_reached ("unexpected section");
2586 dwarf2_read_section (objfile, info);
2588 *sectp = get_section_bfd_section (info);
2589 *bufp = info->buffer;
2590 *sizep = info->size;
2593 /* A helper function to find the sections for a .dwz file. */
2596 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2598 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2600 /* Note that we only support the standard ELF names, because .dwz
2601 is ELF-only (at the time of writing). */
2602 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2604 dwz_file->abbrev.s.section = sectp;
2605 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2607 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2609 dwz_file->info.s.section = sectp;
2610 dwz_file->info.size = bfd_get_section_size (sectp);
2612 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2614 dwz_file->str.s.section = sectp;
2615 dwz_file->str.size = bfd_get_section_size (sectp);
2617 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2619 dwz_file->line.s.section = sectp;
2620 dwz_file->line.size = bfd_get_section_size (sectp);
2622 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2624 dwz_file->macro.s.section = sectp;
2625 dwz_file->macro.size = bfd_get_section_size (sectp);
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2629 dwz_file->gdb_index.s.section = sectp;
2630 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2634 dwz_file->debug_names.s.section = sectp;
2635 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2639 /* See dwarf2read.h. */
2642 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2644 const char *filename;
2645 bfd_size_type buildid_len_arg;
2649 if (dwarf2_per_objfile->dwz_file != NULL)
2650 return dwarf2_per_objfile->dwz_file.get ();
2652 bfd_set_error (bfd_error_no_error);
2653 gdb::unique_xmalloc_ptr<char> data
2654 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2655 &buildid_len_arg, &buildid));
2658 if (bfd_get_error () == bfd_error_no_error)
2660 error (_("could not read '.gnu_debugaltlink' section: %s"),
2661 bfd_errmsg (bfd_get_error ()));
2664 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2666 buildid_len = (size_t) buildid_len_arg;
2668 filename = data.get ();
2670 std::string abs_storage;
2671 if (!IS_ABSOLUTE_PATH (filename))
2673 gdb::unique_xmalloc_ptr<char> abs
2674 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2676 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2677 filename = abs_storage.c_str ();
2680 /* First try the file name given in the section. If that doesn't
2681 work, try to use the build-id instead. */
2682 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2683 if (dwz_bfd != NULL)
2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2686 dwz_bfd.reset (nullptr);
2689 if (dwz_bfd == NULL)
2690 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2692 if (dwz_bfd == NULL)
2693 error (_("could not find '.gnu_debugaltlink' file for %s"),
2694 objfile_name (dwarf2_per_objfile->objfile));
2696 std::unique_ptr<struct dwz_file> result
2697 (new struct dwz_file (std::move (dwz_bfd)));
2699 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2702 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2703 result->dwz_bfd.get ());
2704 dwarf2_per_objfile->dwz_file = std::move (result);
2705 return dwarf2_per_objfile->dwz_file.get ();
2708 /* DWARF quick_symbols_functions support. */
2710 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2711 unique line tables, so we maintain a separate table of all .debug_line
2712 derived entries to support the sharing.
2713 All the quick functions need is the list of file names. We discard the
2714 line_header when we're done and don't need to record it here. */
2715 struct quick_file_names
2717 /* The data used to construct the hash key. */
2718 struct stmt_list_hash hash;
2720 /* The number of entries in file_names, real_names. */
2721 unsigned int num_file_names;
2723 /* The file names from the line table, after being run through
2725 const char **file_names;
2727 /* The file names from the line table after being run through
2728 gdb_realpath. These are computed lazily. */
2729 const char **real_names;
2732 /* When using the index (and thus not using psymtabs), each CU has an
2733 object of this type. This is used to hold information needed by
2734 the various "quick" methods. */
2735 struct dwarf2_per_cu_quick_data
2737 /* The file table. This can be NULL if there was no file table
2738 or it's currently not read in.
2739 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2740 struct quick_file_names *file_names;
2742 /* The corresponding symbol table. This is NULL if symbols for this
2743 CU have not yet been read. */
2744 struct compunit_symtab *compunit_symtab;
2746 /* A temporary mark bit used when iterating over all CUs in
2747 expand_symtabs_matching. */
2748 unsigned int mark : 1;
2750 /* True if we've tried to read the file table and found there isn't one.
2751 There will be no point in trying to read it again next time. */
2752 unsigned int no_file_data : 1;
2755 /* Utility hash function for a stmt_list_hash. */
2758 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2762 if (stmt_list_hash->dwo_unit != NULL)
2763 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2764 v += to_underlying (stmt_list_hash->line_sect_off);
2768 /* Utility equality function for a stmt_list_hash. */
2771 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2772 const struct stmt_list_hash *rhs)
2774 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2776 if (lhs->dwo_unit != NULL
2777 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2780 return lhs->line_sect_off == rhs->line_sect_off;
2783 /* Hash function for a quick_file_names. */
2786 hash_file_name_entry (const void *e)
2788 const struct quick_file_names *file_data
2789 = (const struct quick_file_names *) e;
2791 return hash_stmt_list_entry (&file_data->hash);
2794 /* Equality function for a quick_file_names. */
2797 eq_file_name_entry (const void *a, const void *b)
2799 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2800 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2802 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2805 /* Delete function for a quick_file_names. */
2808 delete_file_name_entry (void *e)
2810 struct quick_file_names *file_data = (struct quick_file_names *) e;
2813 for (i = 0; i < file_data->num_file_names; ++i)
2815 xfree ((void*) file_data->file_names[i]);
2816 if (file_data->real_names)
2817 xfree ((void*) file_data->real_names[i]);
2820 /* The space for the struct itself lives on objfile_obstack,
2821 so we don't free it here. */
2824 /* Create a quick_file_names hash table. */
2827 create_quick_file_names_table (unsigned int nr_initial_entries)
2829 return htab_create_alloc (nr_initial_entries,
2830 hash_file_name_entry, eq_file_name_entry,
2831 delete_file_name_entry, xcalloc, xfree);
2834 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2835 have to be created afterwards. You should call age_cached_comp_units after
2836 processing PER_CU->CU. dw2_setup must have been already called. */
2839 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2841 if (per_cu->is_debug_types)
2842 load_full_type_unit (per_cu);
2844 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2846 if (per_cu->cu == NULL)
2847 return; /* Dummy CU. */
2849 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2852 /* Read in the symbols for PER_CU. */
2855 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2859 /* Skip type_unit_groups, reading the type units they contain
2860 is handled elsewhere. */
2861 if (IS_TYPE_UNIT_GROUP (per_cu))
2864 /* The destructor of dwarf2_queue_guard frees any entries left on
2865 the queue. After this point we're guaranteed to leave this function
2866 with the dwarf queue empty. */
2867 dwarf2_queue_guard q_guard;
2869 if (dwarf2_per_objfile->using_index
2870 ? per_cu->v.quick->compunit_symtab == NULL
2871 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2873 queue_comp_unit (per_cu, language_minimal);
2874 load_cu (per_cu, skip_partial);
2876 /* If we just loaded a CU from a DWO, and we're working with an index
2877 that may badly handle TUs, load all the TUs in that DWO as well.
2878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2879 if (!per_cu->is_debug_types
2880 && per_cu->cu != NULL
2881 && per_cu->cu->dwo_unit != NULL
2882 && dwarf2_per_objfile->index_table != NULL
2883 && dwarf2_per_objfile->index_table->version <= 7
2884 /* DWP files aren't supported yet. */
2885 && get_dwp_file (dwarf2_per_objfile) == NULL)
2886 queue_and_load_all_dwo_tus (per_cu);
2889 process_queue (dwarf2_per_objfile);
2891 /* Age the cache, releasing compilation units that have not
2892 been used recently. */
2893 age_cached_comp_units (dwarf2_per_objfile);
2896 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2897 the objfile from which this CU came. Returns the resulting symbol
2900 static struct compunit_symtab *
2901 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2903 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2905 gdb_assert (dwarf2_per_objfile->using_index);
2906 if (!per_cu->v.quick->compunit_symtab)
2908 free_cached_comp_units freer (dwarf2_per_objfile);
2909 scoped_restore decrementer = increment_reading_symtab ();
2910 dw2_do_instantiate_symtab (per_cu, skip_partial);
2911 process_cu_includes (dwarf2_per_objfile);
2914 return per_cu->v.quick->compunit_symtab;
2917 /* See declaration. */
2919 dwarf2_per_cu_data *
2920 dwarf2_per_objfile::get_cutu (int index)
2922 if (index >= this->all_comp_units.size ())
2924 index -= this->all_comp_units.size ();
2925 gdb_assert (index < this->all_type_units.size ());
2926 return &this->all_type_units[index]->per_cu;
2929 return this->all_comp_units[index];
2932 /* See declaration. */
2934 dwarf2_per_cu_data *
2935 dwarf2_per_objfile::get_cu (int index)
2937 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2939 return this->all_comp_units[index];
2942 /* See declaration. */
2945 dwarf2_per_objfile::get_tu (int index)
2947 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2949 return this->all_type_units[index];
2952 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2953 objfile_obstack, and constructed with the specified field
2956 static dwarf2_per_cu_data *
2957 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2958 struct dwarf2_section_info *section,
2960 sect_offset sect_off, ULONGEST length)
2962 struct objfile *objfile = dwarf2_per_objfile->objfile;
2963 dwarf2_per_cu_data *the_cu
2964 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_data);
2966 the_cu->sect_off = sect_off;
2967 the_cu->length = length;
2968 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2969 the_cu->section = section;
2970 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2971 struct dwarf2_per_cu_quick_data);
2972 the_cu->is_dwz = is_dwz;
2976 /* A helper for create_cus_from_index that handles a given list of
2980 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2981 const gdb_byte *cu_list, offset_type n_elements,
2982 struct dwarf2_section_info *section,
2985 for (offset_type i = 0; i < n_elements; i += 2)
2987 gdb_static_assert (sizeof (ULONGEST) >= 8);
2989 sect_offset sect_off
2990 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2991 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2994 dwarf2_per_cu_data *per_cu
2995 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2997 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3001 /* Read the CU list from the mapped index, and use it to create all
3002 the CU objects for this objfile. */
3005 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 const gdb_byte *cu_list, offset_type cu_list_elements,
3007 const gdb_byte *dwz_list, offset_type dwz_elements)
3009 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3010 dwarf2_per_objfile->all_comp_units.reserve
3011 ((cu_list_elements + dwz_elements) / 2);
3013 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3014 &dwarf2_per_objfile->info, 0);
3016 if (dwz_elements == 0)
3019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3020 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3024 /* Create the signatured type hash table from the index. */
3027 create_signatured_type_table_from_index
3028 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3029 struct dwarf2_section_info *section,
3030 const gdb_byte *bytes,
3031 offset_type elements)
3033 struct objfile *objfile = dwarf2_per_objfile->objfile;
3035 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3036 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3038 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3040 for (offset_type i = 0; i < elements; i += 3)
3042 struct signatured_type *sig_type;
3045 cu_offset type_offset_in_tu;
3047 gdb_static_assert (sizeof (ULONGEST) >= 8);
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3051 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3053 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3056 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3057 struct signatured_type);
3058 sig_type->signature = signature;
3059 sig_type->type_offset_in_tu = type_offset_in_tu;
3060 sig_type->per_cu.is_debug_types = 1;
3061 sig_type->per_cu.section = section;
3062 sig_type->per_cu.sect_off = sect_off;
3063 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3064 sig_type->per_cu.v.quick
3065 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3066 struct dwarf2_per_cu_quick_data);
3068 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3071 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3074 dwarf2_per_objfile->signatured_types = sig_types_hash;
3077 /* Create the signatured type hash table from .debug_names. */
3080 create_signatured_type_table_from_debug_names
3081 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3082 const mapped_debug_names &map,
3083 struct dwarf2_section_info *section,
3084 struct dwarf2_section_info *abbrev_section)
3086 struct objfile *objfile = dwarf2_per_objfile->objfile;
3088 dwarf2_read_section (objfile, section);
3089 dwarf2_read_section (objfile, abbrev_section);
3091 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3092 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3094 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3096 for (uint32_t i = 0; i < map.tu_count; ++i)
3098 struct signatured_type *sig_type;
3101 sect_offset sect_off
3102 = (sect_offset) (extract_unsigned_integer
3103 (map.tu_table_reordered + i * map.offset_size,
3105 map.dwarf5_byte_order));
3107 comp_unit_head cu_header;
3108 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3110 section->buffer + to_underlying (sect_off),
3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3114 struct signatured_type);
3115 sig_type->signature = cu_header.signature;
3116 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3117 sig_type->per_cu.is_debug_types = 1;
3118 sig_type->per_cu.section = section;
3119 sig_type->per_cu.sect_off = sect_off;
3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3121 sig_type->per_cu.v.quick
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
3134 /* Read the address map data from the mapped index, and use it to
3135 populate the objfile's psymtabs_addrmap. */
3138 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3139 struct mapped_index *index)
3141 struct objfile *objfile = dwarf2_per_objfile->objfile;
3142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3143 const gdb_byte *iter, *end;
3144 struct addrmap *mutable_map;
3147 auto_obstack temp_obstack;
3149 mutable_map = addrmap_create_mutable (&temp_obstack);
3151 iter = index->address_table.data ();
3152 end = iter + index->address_table.size ();
3154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3158 ULONGEST hi, lo, cu_index;
3159 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3161 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3163 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3168 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3169 hex_string (lo), hex_string (hi));
3173 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3175 complaint (_(".gdb_index address table has invalid CU number %u"),
3176 (unsigned) cu_index);
3180 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3181 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3182 addrmap_set_empty (mutable_map, lo, hi - 1,
3183 dwarf2_per_objfile->get_cu (cu_index));
3186 objfile->partial_symtabs->psymtabs_addrmap
3187 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3190 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3191 populate the objfile's psymtabs_addrmap. */
3194 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3195 struct dwarf2_section_info *section)
3197 struct objfile *objfile = dwarf2_per_objfile->objfile;
3198 bfd *abfd = objfile->obfd;
3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3200 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3201 SECT_OFF_TEXT (objfile));
3203 auto_obstack temp_obstack;
3204 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3206 std::unordered_map<sect_offset,
3207 dwarf2_per_cu_data *,
3208 gdb::hash_enum<sect_offset>>
3209 debug_info_offset_to_per_cu;
3210 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3212 const auto insertpair
3213 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3214 if (!insertpair.second)
3216 warning (_("Section .debug_aranges in %s has duplicate "
3217 "debug_info_offset %s, ignoring .debug_aranges."),
3218 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3223 dwarf2_read_section (objfile, section);
3225 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3227 const gdb_byte *addr = section->buffer;
3229 while (addr < section->buffer + section->size)
3231 const gdb_byte *const entry_addr = addr;
3232 unsigned int bytes_read;
3234 const LONGEST entry_length = read_initial_length (abfd, addr,
3238 const gdb_byte *const entry_end = addr + entry_length;
3239 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3240 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3241 if (addr + entry_length > section->buffer + section->size)
3243 warning (_("Section .debug_aranges in %s entry at offset %s "
3244 "length %s exceeds section length %s, "
3245 "ignoring .debug_aranges."),
3246 objfile_name (objfile),
3247 plongest (entry_addr - section->buffer),
3248 plongest (bytes_read + entry_length),
3249 pulongest (section->size));
3253 /* The version number. */
3254 const uint16_t version = read_2_bytes (abfd, addr);
3258 warning (_("Section .debug_aranges in %s entry at offset %s "
3259 "has unsupported version %d, ignoring .debug_aranges."),
3260 objfile_name (objfile),
3261 plongest (entry_addr - section->buffer), version);
3265 const uint64_t debug_info_offset
3266 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3267 addr += offset_size;
3268 const auto per_cu_it
3269 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3270 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3272 warning (_("Section .debug_aranges in %s entry at offset %s "
3273 "debug_info_offset %s does not exists, "
3274 "ignoring .debug_aranges."),
3275 objfile_name (objfile),
3276 plongest (entry_addr - section->buffer),
3277 pulongest (debug_info_offset));
3280 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3282 const uint8_t address_size = *addr++;
3283 if (address_size < 1 || address_size > 8)
3285 warning (_("Section .debug_aranges in %s entry at offset %s "
3286 "address_size %u is invalid, ignoring .debug_aranges."),
3287 objfile_name (objfile),
3288 plongest (entry_addr - section->buffer), address_size);
3292 const uint8_t segment_selector_size = *addr++;
3293 if (segment_selector_size != 0)
3295 warning (_("Section .debug_aranges in %s entry at offset %s "
3296 "segment_selector_size %u is not supported, "
3297 "ignoring .debug_aranges."),
3298 objfile_name (objfile),
3299 plongest (entry_addr - section->buffer),
3300 segment_selector_size);
3304 /* Must pad to an alignment boundary that is twice the address
3305 size. It is undocumented by the DWARF standard but GCC does
3307 for (size_t padding = ((-(addr - section->buffer))
3308 & (2 * address_size - 1));
3309 padding > 0; padding--)
3312 warning (_("Section .debug_aranges in %s entry at offset %s "
3313 "padding is not zero, ignoring .debug_aranges."),
3314 objfile_name (objfile),
3315 plongest (entry_addr - section->buffer));
3321 if (addr + 2 * address_size > entry_end)
3323 warning (_("Section .debug_aranges in %s entry at offset %s "
3324 "address list is not properly terminated, "
3325 "ignoring .debug_aranges."),
3326 objfile_name (objfile),
3327 plongest (entry_addr - section->buffer));
3330 ULONGEST start = extract_unsigned_integer (addr, address_size,
3332 addr += address_size;
3333 ULONGEST length = extract_unsigned_integer (addr, address_size,
3335 addr += address_size;
3336 if (start == 0 && length == 0)
3338 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3340 /* Symbol was eliminated due to a COMDAT group. */
3343 ULONGEST end = start + length;
3344 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3346 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3348 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3352 objfile->partial_symtabs->psymtabs_addrmap
3353 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3356 /* Find a slot in the mapped index INDEX for the object named NAME.
3357 If NAME is found, set *VEC_OUT to point to the CU vector in the
3358 constant pool and return true. If NAME cannot be found, return
3362 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3363 offset_type **vec_out)
3366 offset_type slot, step;
3367 int (*cmp) (const char *, const char *);
3369 gdb::unique_xmalloc_ptr<char> without_params;
3370 if (current_language->la_language == language_cplus
3371 || current_language->la_language == language_fortran
3372 || current_language->la_language == language_d)
3374 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3377 if (strchr (name, '(') != NULL)
3379 without_params = cp_remove_params (name);
3381 if (without_params != NULL)
3382 name = without_params.get ();
3386 /* Index version 4 did not support case insensitive searches. But the
3387 indices for case insensitive languages are built in lowercase, therefore
3388 simulate our NAME being searched is also lowercased. */
3389 hash = mapped_index_string_hash ((index->version == 4
3390 && case_sensitivity == case_sensitive_off
3391 ? 5 : index->version),
3394 slot = hash & (index->symbol_table.size () - 1);
3395 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3396 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3402 const auto &bucket = index->symbol_table[slot];
3403 if (bucket.name == 0 && bucket.vec == 0)
3406 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3407 if (!cmp (name, str))
3409 *vec_out = (offset_type *) (index->constant_pool
3410 + MAYBE_SWAP (bucket.vec));
3414 slot = (slot + step) & (index->symbol_table.size () - 1);
3418 /* A helper function that reads the .gdb_index from BUFFER and fills
3419 in MAP. FILENAME is the name of the file containing the data;
3420 it is used for error reporting. DEPRECATED_OK is true if it is
3421 ok to use deprecated sections.
3423 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3424 out parameters that are filled in with information about the CU and
3425 TU lists in the section.
3427 Returns true if all went well, false otherwise. */
3430 read_gdb_index_from_buffer (struct objfile *objfile,
3431 const char *filename,
3433 gdb::array_view<const gdb_byte> buffer,
3434 struct mapped_index *map,
3435 const gdb_byte **cu_list,
3436 offset_type *cu_list_elements,
3437 const gdb_byte **types_list,
3438 offset_type *types_list_elements)
3440 const gdb_byte *addr = &buffer[0];
3442 /* Version check. */
3443 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3444 /* Versions earlier than 3 emitted every copy of a psymbol. This
3445 causes the index to behave very poorly for certain requests. Version 3
3446 contained incomplete addrmap. So, it seems better to just ignore such
3450 static int warning_printed = 0;
3451 if (!warning_printed)
3453 warning (_("Skipping obsolete .gdb_index section in %s."),
3455 warning_printed = 1;
3459 /* Index version 4 uses a different hash function than index version
3462 Versions earlier than 6 did not emit psymbols for inlined
3463 functions. Using these files will cause GDB not to be able to
3464 set breakpoints on inlined functions by name, so we ignore these
3465 indices unless the user has done
3466 "set use-deprecated-index-sections on". */
3467 if (version < 6 && !deprecated_ok)
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3473 Skipping deprecated .gdb_index section in %s.\n\
3474 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3475 to use the section anyway."),
3477 warning_printed = 1;
3481 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3482 of the TU (for symbols coming from TUs),
3483 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3484 Plus gold-generated indices can have duplicate entries for global symbols,
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3486 These are just performance bugs, and we can't distinguish gdb-generated
3487 indices from gold-generated ones, so issue no warning here. */
3489 /* Indexes with higher version than the one supported by GDB may be no
3490 longer backward compatible. */
3494 map->version = version;
3496 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3499 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3500 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3504 *types_list = addr + MAYBE_SWAP (metadata[i]);
3505 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3506 - MAYBE_SWAP (metadata[i]))
3510 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3511 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3513 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3516 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3517 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3519 = gdb::array_view<mapped_index::symbol_table_slot>
3520 ((mapped_index::symbol_table_slot *) symbol_table,
3521 (mapped_index::symbol_table_slot *) symbol_table_end);
3524 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3529 /* Callback types for dwarf2_read_gdb_index. */
3531 typedef gdb::function_view
3532 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3533 get_gdb_index_contents_ftype;
3534 typedef gdb::function_view
3535 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3536 get_gdb_index_contents_dwz_ftype;
3538 /* Read .gdb_index. If everything went ok, initialize the "quick"
3539 elements of all the CUs and return 1. Otherwise, return 0. */
3542 dwarf2_read_gdb_index
3543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3544 get_gdb_index_contents_ftype get_gdb_index_contents,
3545 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3547 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3548 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3549 struct dwz_file *dwz;
3550 struct objfile *objfile = dwarf2_per_objfile->objfile;
3552 gdb::array_view<const gdb_byte> main_index_contents
3553 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3555 if (main_index_contents.empty ())
3558 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3559 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3560 use_deprecated_index_sections,
3561 main_index_contents, map.get (), &cu_list,
3562 &cu_list_elements, &types_list,
3563 &types_list_elements))
3566 /* Don't use the index if it's empty. */
3567 if (map->symbol_table.empty ())
3570 /* If there is a .dwz file, read it so we can get its CU list as
3572 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3575 struct mapped_index dwz_map;
3576 const gdb_byte *dwz_types_ignore;
3577 offset_type dwz_types_elements_ignore;
3579 gdb::array_view<const gdb_byte> dwz_index_content
3580 = get_gdb_index_contents_dwz (objfile, dwz);
3582 if (dwz_index_content.empty ())
3585 if (!read_gdb_index_from_buffer (objfile,
3586 bfd_get_filename (dwz->dwz_bfd), 1,
3587 dwz_index_content, &dwz_map,
3588 &dwz_list, &dwz_list_elements,
3590 &dwz_types_elements_ignore))
3592 warning (_("could not read '.gdb_index' section from %s; skipping"),
3593 bfd_get_filename (dwz->dwz_bfd));
3598 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3599 dwz_list, dwz_list_elements);
3601 if (types_list_elements)
3603 /* We can only handle a single .debug_types when we have an
3605 if (dwarf2_per_objfile->types.size () != 1)
3608 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3610 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3611 types_list, types_list_elements);
3614 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3616 dwarf2_per_objfile->index_table = std::move (map);
3617 dwarf2_per_objfile->using_index = 1;
3618 dwarf2_per_objfile->quick_file_names_table =
3619 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3624 /* die_reader_func for dw2_get_file_names. */
3627 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3628 const gdb_byte *info_ptr,
3629 struct die_info *comp_unit_die,
3633 struct dwarf2_cu *cu = reader->cu;
3634 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3635 struct dwarf2_per_objfile *dwarf2_per_objfile
3636 = cu->per_cu->dwarf2_per_objfile;
3637 struct objfile *objfile = dwarf2_per_objfile->objfile;
3638 struct dwarf2_per_cu_data *lh_cu;
3639 struct attribute *attr;
3642 struct quick_file_names *qfn;
3644 gdb_assert (! this_cu->is_debug_types);
3646 /* Our callers never want to match partial units -- instead they
3647 will match the enclosing full CU. */
3648 if (comp_unit_die->tag == DW_TAG_partial_unit)
3650 this_cu->v.quick->no_file_data = 1;
3658 sect_offset line_offset {};
3660 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3663 struct quick_file_names find_entry;
3665 line_offset = (sect_offset) DW_UNSND (attr);
3667 /* We may have already read in this line header (TU line header sharing).
3668 If we have we're done. */
3669 find_entry.hash.dwo_unit = cu->dwo_unit;
3670 find_entry.hash.line_sect_off = line_offset;
3671 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3672 &find_entry, INSERT);
3675 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3679 lh = dwarf_decode_line_header (line_offset, cu);
3683 lh_cu->v.quick->no_file_data = 1;
3687 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3688 qfn->hash.dwo_unit = cu->dwo_unit;
3689 qfn->hash.line_sect_off = line_offset;
3690 gdb_assert (slot != NULL);
3693 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3695 qfn->num_file_names = lh->file_names.size ();
3697 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3698 for (i = 0; i < lh->file_names.size (); ++i)
3699 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3700 qfn->real_names = NULL;
3702 lh_cu->v.quick->file_names = qfn;
3705 /* A helper for the "quick" functions which attempts to read the line
3706 table for THIS_CU. */
3708 static struct quick_file_names *
3709 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3711 /* This should never be called for TUs. */
3712 gdb_assert (! this_cu->is_debug_types);
3713 /* Nor type unit groups. */
3714 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3716 if (this_cu->v.quick->file_names != NULL)
3717 return this_cu->v.quick->file_names;
3718 /* If we know there is no line data, no point in looking again. */
3719 if (this_cu->v.quick->no_file_data)
3722 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3724 if (this_cu->v.quick->no_file_data)
3726 return this_cu->v.quick->file_names;
3729 /* A helper for the "quick" functions which computes and caches the
3730 real path for a given file name from the line table. */
3733 dw2_get_real_path (struct objfile *objfile,
3734 struct quick_file_names *qfn, int index)
3736 if (qfn->real_names == NULL)
3737 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3738 qfn->num_file_names, const char *);
3740 if (qfn->real_names[index] == NULL)
3741 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3743 return qfn->real_names[index];
3746 static struct symtab *
3747 dw2_find_last_source_symtab (struct objfile *objfile)
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
3751 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3752 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3757 return compunit_primary_filetab (cust);
3760 /* Traversal function for dw2_forget_cached_source_info. */
3763 dw2_free_cached_file_names (void **slot, void *info)
3765 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3767 if (file_data->real_names)
3771 for (i = 0; i < file_data->num_file_names; ++i)
3773 xfree ((void*) file_data->real_names[i]);
3774 file_data->real_names[i] = NULL;
3782 dw2_forget_cached_source_info (struct objfile *objfile)
3784 struct dwarf2_per_objfile *dwarf2_per_objfile
3785 = get_dwarf2_per_objfile (objfile);
3787 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3788 dw2_free_cached_file_names, NULL);
3791 /* Helper function for dw2_map_symtabs_matching_filename that expands
3792 the symtabs and calls the iterator. */
3795 dw2_map_expand_apply (struct objfile *objfile,
3796 struct dwarf2_per_cu_data *per_cu,
3797 const char *name, const char *real_path,
3798 gdb::function_view<bool (symtab *)> callback)
3800 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3802 /* Don't visit already-expanded CUs. */
3803 if (per_cu->v.quick->compunit_symtab)
3806 /* This may expand more than one symtab, and we want to iterate over
3808 dw2_instantiate_symtab (per_cu, false);
3810 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3811 last_made, callback);
3814 /* Implementation of the map_symtabs_matching_filename method. */
3817 dw2_map_symtabs_matching_filename
3818 (struct objfile *objfile, const char *name, const char *real_path,
3819 gdb::function_view<bool (symtab *)> callback)
3821 const char *name_basename = lbasename (name);
3822 struct dwarf2_per_objfile *dwarf2_per_objfile
3823 = get_dwarf2_per_objfile (objfile);
3825 /* The rule is CUs specify all the files, including those used by
3826 any TU, so there's no need to scan TUs here. */
3828 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3830 /* We only need to look at symtabs not already expanded. */
3831 if (per_cu->v.quick->compunit_symtab)
3834 quick_file_names *file_data = dw2_get_file_names (per_cu);
3835 if (file_data == NULL)
3838 for (int j = 0; j < file_data->num_file_names; ++j)
3840 const char *this_name = file_data->file_names[j];
3841 const char *this_real_name;
3843 if (compare_filenames_for_search (this_name, name))
3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3851 /* Before we invoke realpath, which can get expensive when many
3852 files are involved, do a quick comparison of the basenames. */
3853 if (! basenames_may_differ
3854 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3857 this_real_name = dw2_get_real_path (objfile, file_data, j);
3858 if (compare_filenames_for_search (this_real_name, name))
3860 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3866 if (real_path != NULL)
3868 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3869 gdb_assert (IS_ABSOLUTE_PATH (name));
3870 if (this_real_name != NULL
3871 && FILENAME_CMP (real_path, this_real_name) == 0)
3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3885 /* Struct used to manage iterating over all CUs looking for a symbol. */
3887 struct dw2_symtab_iterator
3889 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3890 struct dwarf2_per_objfile *dwarf2_per_objfile;
3891 /* If set, only look for symbols that match that block. Valid values are
3892 GLOBAL_BLOCK and STATIC_BLOCK. */
3893 gdb::optional<block_enum> block_index;
3894 /* The kind of symbol we're looking for. */
3896 /* The list of CUs from the index entry of the symbol,
3897 or NULL if not found. */
3899 /* The next element in VEC to look at. */
3901 /* The number of elements in VEC, or zero if there is no match. */
3903 /* Have we seen a global version of the symbol?
3904 If so we can ignore all further global instances.
3905 This is to work around gold/15646, inefficient gold-generated
3910 /* Initialize the index symtab iterator ITER. */
3913 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3914 struct dwarf2_per_objfile *dwarf2_per_objfile,
3915 gdb::optional<block_enum> block_index,
3919 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3920 iter->block_index = block_index;
3921 iter->domain = domain;
3923 iter->global_seen = 0;
3925 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3927 /* index is NULL if OBJF_READNOW. */
3928 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3929 iter->length = MAYBE_SWAP (*iter->vec);
3937 /* Return the next matching CU or NULL if there are no more. */
3939 static struct dwarf2_per_cu_data *
3940 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3942 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3944 for ( ; iter->next < iter->length; ++iter->next)
3946 offset_type cu_index_and_attrs =
3947 MAYBE_SWAP (iter->vec[iter->next + 1]);
3948 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3949 gdb_index_symbol_kind symbol_kind =
3950 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3951 /* Only check the symbol attributes if they're present.
3952 Indices prior to version 7 don't record them,
3953 and indices >= 7 may elide them for certain symbols
3954 (gold does this). */
3956 (dwarf2_per_objfile->index_table->version >= 7
3957 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3959 /* Don't crash on bad data. */
3960 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3961 + dwarf2_per_objfile->all_type_units.size ()))
3963 complaint (_(".gdb_index entry has bad CU index"
3965 objfile_name (dwarf2_per_objfile->objfile));
3969 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3971 /* Skip if already read in. */
3972 if (per_cu->v.quick->compunit_symtab)
3975 /* Check static vs global. */
3978 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3980 if (iter->block_index.has_value ())
3982 bool want_static = *iter->block_index == STATIC_BLOCK;
3984 if (is_static != want_static)
3988 /* Work around gold/15646. */
3989 if (!is_static && iter->global_seen)
3992 iter->global_seen = 1;
3995 /* Only check the symbol's kind if it has one. */
3998 switch (iter->domain)
4001 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4002 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4003 /* Some types are also in VAR_DOMAIN. */
4004 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4027 static struct compunit_symtab *
4028 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4029 const char *name, domain_enum domain)
4031 struct compunit_symtab *stab_best = NULL;
4032 struct dwarf2_per_objfile *dwarf2_per_objfile
4033 = get_dwarf2_per_objfile (objfile);
4035 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
4040 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4042 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4044 struct symbol *sym, *with_opaque = NULL;
4045 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4046 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4047 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4049 sym = block_find_symbol (block, name, domain,
4050 block_find_non_opaque_type_preferred,
4053 /* Some caution must be observed with overloaded functions
4054 and methods, since the index will not contain any overload
4055 information (but NAME might contain it). */
4058 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4060 if (with_opaque != NULL
4061 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4064 /* Keep looking through other CUs. */
4071 dw2_print_stats (struct objfile *objfile)
4073 struct dwarf2_per_objfile *dwarf2_per_objfile
4074 = get_dwarf2_per_objfile (objfile);
4075 int total = (dwarf2_per_objfile->all_comp_units.size ()
4076 + dwarf2_per_objfile->all_type_units.size ());
4079 for (int i = 0; i < total; ++i)
4081 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4083 if (!per_cu->v.quick->compunit_symtab)
4086 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4087 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4090 /* This dumps minimal information about the index.
4091 It is called via "mt print objfiles".
4092 One use is to verify .gdb_index has been loaded by the
4093 gdb.dwarf2/gdb-index.exp testcase. */
4096 dw2_dump (struct objfile *objfile)
4098 struct dwarf2_per_objfile *dwarf2_per_objfile
4099 = get_dwarf2_per_objfile (objfile);
4101 gdb_assert (dwarf2_per_objfile->using_index);
4102 printf_filtered (".gdb_index:");
4103 if (dwarf2_per_objfile->index_table != NULL)
4105 printf_filtered (" version %d\n",
4106 dwarf2_per_objfile->index_table->version);
4109 printf_filtered (" faked for \"readnow\"\n");
4110 printf_filtered ("\n");
4114 dw2_expand_symtabs_for_function (struct objfile *objfile,
4115 const char *func_name)
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
4120 struct dw2_symtab_iterator iter;
4121 struct dwarf2_per_cu_data *per_cu;
4123 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4125 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4126 dw2_instantiate_symtab (per_cu, false);
4131 dw2_expand_all_symtabs (struct objfile *objfile)
4133 struct dwarf2_per_objfile *dwarf2_per_objfile
4134 = get_dwarf2_per_objfile (objfile);
4135 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4136 + dwarf2_per_objfile->all_type_units.size ());
4138 for (int i = 0; i < total_units; ++i)
4140 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4142 /* We don't want to directly expand a partial CU, because if we
4143 read it with the wrong language, then assertion failures can
4144 be triggered later on. See PR symtab/23010. So, tell
4145 dw2_instantiate_symtab to skip partial CUs -- any important
4146 partial CU will be read via DW_TAG_imported_unit anyway. */
4147 dw2_instantiate_symtab (per_cu, true);
4152 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4153 const char *fullname)
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
4158 /* We don't need to consider type units here.
4159 This is only called for examining code, e.g. expand_line_sal.
4160 There can be an order of magnitude (or more) more type units
4161 than comp units, and we avoid them if we can. */
4163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4165 /* We only need to look at symtabs not already expanded. */
4166 if (per_cu->v.quick->compunit_symtab)
4169 quick_file_names *file_data = dw2_get_file_names (per_cu);
4170 if (file_data == NULL)
4173 for (int j = 0; j < file_data->num_file_names; ++j)
4175 const char *this_fullname = file_data->file_names[j];
4177 if (filename_cmp (this_fullname, fullname) == 0)
4179 dw2_instantiate_symtab (per_cu, false);
4187 dw2_map_matching_symbols
4188 (struct objfile *objfile,
4189 const char * name, domain_enum domain,
4191 gdb::function_view<symbol_found_callback_ftype> callback,
4192 symbol_name_match_type match,
4193 symbol_compare_ftype *ordered_compare)
4195 /* Currently unimplemented; used for Ada. The function can be called if the
4196 current language is Ada for a non-Ada objfile using GNU index. As Ada
4197 does not look for non-Ada symbols this function should just return. */
4200 /* Symbol name matcher for .gdb_index names.
4202 Symbol names in .gdb_index have a few particularities:
4204 - There's no indication of which is the language of each symbol.
4206 Since each language has its own symbol name matching algorithm,
4207 and we don't know which language is the right one, we must match
4208 each symbol against all languages. This would be a potential
4209 performance problem if it were not mitigated by the
4210 mapped_index::name_components lookup table, which significantly
4211 reduces the number of times we need to call into this matcher,
4212 making it a non-issue.
4214 - Symbol names in the index have no overload (parameter)
4215 information. I.e., in C++, "foo(int)" and "foo(long)" both
4216 appear as "foo" in the index, for example.
4218 This means that the lookup names passed to the symbol name
4219 matcher functions must have no parameter information either
4220 because (e.g.) symbol search name "foo" does not match
4221 lookup-name "foo(int)" [while swapping search name for lookup
4224 class gdb_index_symbol_name_matcher
4227 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4228 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4230 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4231 Returns true if any matcher matches. */
4232 bool matches (const char *symbol_name);
4235 /* A reference to the lookup name we're matching against. */
4236 const lookup_name_info &m_lookup_name;
4238 /* A vector holding all the different symbol name matchers, for all
4240 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4243 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4244 (const lookup_name_info &lookup_name)
4245 : m_lookup_name (lookup_name)
4247 /* Prepare the vector of comparison functions upfront, to avoid
4248 doing the same work for each symbol. Care is taken to avoid
4249 matching with the same matcher more than once if/when multiple
4250 languages use the same matcher function. */
4251 auto &matchers = m_symbol_name_matcher_funcs;
4252 matchers.reserve (nr_languages);
4254 matchers.push_back (default_symbol_name_matcher);
4256 for (int i = 0; i < nr_languages; i++)
4258 const language_defn *lang = language_def ((enum language) i);
4259 symbol_name_matcher_ftype *name_matcher
4260 = get_symbol_name_matcher (lang, m_lookup_name);
4262 /* Don't insert the same comparison routine more than once.
4263 Note that we do this linear walk instead of a seemingly
4264 cheaper sorted insert, or use a std::set or something like
4265 that, because relative order of function addresses is not
4266 stable. This is not a problem in practice because the number
4267 of supported languages is low, and the cost here is tiny
4268 compared to the number of searches we'll do afterwards using
4270 if (name_matcher != default_symbol_name_matcher
4271 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4272 == matchers.end ()))
4273 matchers.push_back (name_matcher);
4278 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4280 for (auto matches_name : m_symbol_name_matcher_funcs)
4281 if (matches_name (symbol_name, m_lookup_name, NULL))
4287 /* Starting from a search name, return the string that finds the upper
4288 bound of all strings that start with SEARCH_NAME in a sorted name
4289 list. Returns the empty string to indicate that the upper bound is
4290 the end of the list. */
4293 make_sort_after_prefix_name (const char *search_name)
4295 /* When looking to complete "func", we find the upper bound of all
4296 symbols that start with "func" by looking for where we'd insert
4297 the closest string that would follow "func" in lexicographical
4298 order. Usually, that's "func"-with-last-character-incremented,
4299 i.e. "fund". Mind non-ASCII characters, though. Usually those
4300 will be UTF-8 multi-byte sequences, but we can't be certain.
4301 Especially mind the 0xff character, which is a valid character in
4302 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4303 rule out compilers allowing it in identifiers. Note that
4304 conveniently, strcmp/strcasecmp are specified to compare
4305 characters interpreted as unsigned char. So what we do is treat
4306 the whole string as a base 256 number composed of a sequence of
4307 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4308 to 0, and carries 1 to the following more-significant position.
4309 If the very first character in SEARCH_NAME ends up incremented
4310 and carries/overflows, then the upper bound is the end of the
4311 list. The string after the empty string is also the empty
4314 Some examples of this operation:
4316 SEARCH_NAME => "+1" RESULT
4320 "\xff" "a" "\xff" => "\xff" "b"
4325 Then, with these symbols for example:
4331 completing "func" looks for symbols between "func" and
4332 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4333 which finds "func" and "func1", but not "fund".
4337 funcÿ (Latin1 'ÿ' [0xff])
4341 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4342 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4346 ÿÿ (Latin1 'ÿ' [0xff])
4349 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4350 the end of the list.
4352 std::string after = search_name;
4353 while (!after.empty () && (unsigned char) after.back () == 0xff)
4355 if (!after.empty ())
4356 after.back () = (unsigned char) after.back () + 1;
4360 /* See declaration. */
4362 std::pair<std::vector<name_component>::const_iterator,
4363 std::vector<name_component>::const_iterator>
4364 mapped_index_base::find_name_components_bounds
4365 (const lookup_name_info &lookup_name_without_params) const
4368 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4371 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4373 /* Comparison function object for lower_bound that matches against a
4374 given symbol name. */
4375 auto lookup_compare_lower = [&] (const name_component &elem,
4378 const char *elem_qualified = this->symbol_name_at (elem.idx);
4379 const char *elem_name = elem_qualified + elem.name_offset;
4380 return name_cmp (elem_name, name) < 0;
4383 /* Comparison function object for upper_bound that matches against a
4384 given symbol name. */
4385 auto lookup_compare_upper = [&] (const char *name,
4386 const name_component &elem)
4388 const char *elem_qualified = this->symbol_name_at (elem.idx);
4389 const char *elem_name = elem_qualified + elem.name_offset;
4390 return name_cmp (name, elem_name) < 0;
4393 auto begin = this->name_components.begin ();
4394 auto end = this->name_components.end ();
4396 /* Find the lower bound. */
4399 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4402 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4405 /* Find the upper bound. */
4408 if (lookup_name_without_params.completion_mode ())
4410 /* In completion mode, we want UPPER to point past all
4411 symbols names that have the same prefix. I.e., with
4412 these symbols, and completing "func":
4414 function << lower bound
4416 other_function << upper bound
4418 We find the upper bound by looking for the insertion
4419 point of "func"-with-last-character-incremented,
4421 std::string after = make_sort_after_prefix_name (cplus);
4424 return std::lower_bound (lower, end, after.c_str (),
4425 lookup_compare_lower);
4428 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4431 return {lower, upper};
4434 /* See declaration. */
4437 mapped_index_base::build_name_components ()
4439 if (!this->name_components.empty ())
4442 this->name_components_casing = case_sensitivity;
4444 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4446 /* The code below only knows how to break apart components of C++
4447 symbol names (and other languages that use '::' as
4448 namespace/module separator). If we add support for wild matching
4449 to some language that uses some other operator (E.g., Ada, Go and
4450 D use '.'), then we'll need to try splitting the symbol name
4451 according to that language too. Note that Ada does support wild
4452 matching, but doesn't currently support .gdb_index. */
4453 auto count = this->symbol_name_count ();
4454 for (offset_type idx = 0; idx < count; idx++)
4456 if (this->symbol_name_slot_invalid (idx))
4459 const char *name = this->symbol_name_at (idx);
4461 /* Add each name component to the name component table. */
4462 unsigned int previous_len = 0;
4463 for (unsigned int current_len = cp_find_first_component (name);
4464 name[current_len] != '\0';
4465 current_len += cp_find_first_component (name + current_len))
4467 gdb_assert (name[current_len] == ':');
4468 this->name_components.push_back ({previous_len, idx});
4469 /* Skip the '::'. */
4471 previous_len = current_len;
4473 this->name_components.push_back ({previous_len, idx});
4476 /* Sort name_components elements by name. */
4477 auto name_comp_compare = [&] (const name_component &left,
4478 const name_component &right)
4480 const char *left_qualified = this->symbol_name_at (left.idx);
4481 const char *right_qualified = this->symbol_name_at (right.idx);
4483 const char *left_name = left_qualified + left.name_offset;
4484 const char *right_name = right_qualified + right.name_offset;
4486 return name_cmp (left_name, right_name) < 0;
4489 std::sort (this->name_components.begin (),
4490 this->name_components.end (),
4494 /* Helper for dw2_expand_symtabs_matching that works with a
4495 mapped_index_base instead of the containing objfile. This is split
4496 to a separate function in order to be able to unit test the
4497 name_components matching using a mock mapped_index_base. For each
4498 symbol name that matches, calls MATCH_CALLBACK, passing it the
4499 symbol's index in the mapped_index_base symbol table. */
4502 dw2_expand_symtabs_matching_symbol
4503 (mapped_index_base &index,
4504 const lookup_name_info &lookup_name_in,
4505 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4506 enum search_domain kind,
4507 gdb::function_view<void (offset_type)> match_callback)
4509 lookup_name_info lookup_name_without_params
4510 = lookup_name_in.make_ignore_params ();
4511 gdb_index_symbol_name_matcher lookup_name_matcher
4512 (lookup_name_without_params);
4514 /* Build the symbol name component sorted vector, if we haven't
4516 index.build_name_components ();
4518 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4520 /* Now for each symbol name in range, check to see if we have a name
4521 match, and if so, call the MATCH_CALLBACK callback. */
4523 /* The same symbol may appear more than once in the range though.
4524 E.g., if we're looking for symbols that complete "w", and we have
4525 a symbol named "w1::w2", we'll find the two name components for
4526 that same symbol in the range. To be sure we only call the
4527 callback once per symbol, we first collect the symbol name
4528 indexes that matched in a temporary vector and ignore
4530 std::vector<offset_type> matches;
4531 matches.reserve (std::distance (bounds.first, bounds.second));
4533 for (; bounds.first != bounds.second; ++bounds.first)
4535 const char *qualified = index.symbol_name_at (bounds.first->idx);
4537 if (!lookup_name_matcher.matches (qualified)
4538 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4541 matches.push_back (bounds.first->idx);
4544 std::sort (matches.begin (), matches.end ());
4546 /* Finally call the callback, once per match. */
4548 for (offset_type idx : matches)
4552 match_callback (idx);
4557 /* Above we use a type wider than idx's for 'prev', since 0 and
4558 (offset_type)-1 are both possible values. */
4559 static_assert (sizeof (prev) > sizeof (offset_type), "");
4564 namespace selftests { namespace dw2_expand_symtabs_matching {
4566 /* A mock .gdb_index/.debug_names-like name index table, enough to
4567 exercise dw2_expand_symtabs_matching_symbol, which works with the
4568 mapped_index_base interface. Builds an index from the symbol list
4569 passed as parameter to the constructor. */
4570 class mock_mapped_index : public mapped_index_base
4573 mock_mapped_index (gdb::array_view<const char *> symbols)
4574 : m_symbol_table (symbols)
4577 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4579 /* Return the number of names in the symbol table. */
4580 size_t symbol_name_count () const override
4582 return m_symbol_table.size ();
4585 /* Get the name of the symbol at IDX in the symbol table. */
4586 const char *symbol_name_at (offset_type idx) const override
4588 return m_symbol_table[idx];
4592 gdb::array_view<const char *> m_symbol_table;
4595 /* Convenience function that converts a NULL pointer to a "<null>"
4596 string, to pass to print routines. */
4599 string_or_null (const char *str)
4601 return str != NULL ? str : "<null>";
4604 /* Check if a lookup_name_info built from
4605 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4606 index. EXPECTED_LIST is the list of expected matches, in expected
4607 matching order. If no match expected, then an empty list is
4608 specified. Returns true on success. On failure prints a warning
4609 indicating the file:line that failed, and returns false. */
4612 check_match (const char *file, int line,
4613 mock_mapped_index &mock_index,
4614 const char *name, symbol_name_match_type match_type,
4615 bool completion_mode,
4616 std::initializer_list<const char *> expected_list)
4618 lookup_name_info lookup_name (name, match_type, completion_mode);
4620 bool matched = true;
4622 auto mismatch = [&] (const char *expected_str,
4625 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4626 "expected=\"%s\", got=\"%s\"\n"),
4628 (match_type == symbol_name_match_type::FULL
4630 name, string_or_null (expected_str), string_or_null (got));
4634 auto expected_it = expected_list.begin ();
4635 auto expected_end = expected_list.end ();
4637 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4639 [&] (offset_type idx)
4641 const char *matched_name = mock_index.symbol_name_at (idx);
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4645 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4646 mismatch (expected_str, matched_name);
4649 const char *expected_str
4650 = expected_it == expected_end ? NULL : *expected_it++;
4651 if (expected_str != NULL)
4652 mismatch (expected_str, NULL);
4657 /* The symbols added to the mock mapped_index for testing (in
4659 static const char *test_symbols[] = {
4668 "ns2::tmpl<int>::foo2",
4669 "(anonymous namespace)::A::B::C",
4671 /* These are used to check that the increment-last-char in the
4672 matching algorithm for completion doesn't match "t1_fund" when
4673 completing "t1_func". */
4679 /* A UTF-8 name with multi-byte sequences to make sure that
4680 cp-name-parser understands this as a single identifier ("função"
4681 is "function" in PT). */
4684 /* \377 (0xff) is Latin1 'ÿ'. */
4687 /* \377 (0xff) is Latin1 'ÿ'. */
4691 /* A name with all sorts of complications. Starts with "z" to make
4692 it easier for the completion tests below. */
4693 #define Z_SYM_NAME \
4694 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4695 "::tuple<(anonymous namespace)::ui*, " \
4696 "std::default_delete<(anonymous namespace)::ui>, void>"
4701 /* Returns true if the mapped_index_base::find_name_component_bounds
4702 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4703 in completion mode. */
4706 check_find_bounds_finds (mapped_index_base &index,
4707 const char *search_name,
4708 gdb::array_view<const char *> expected_syms)
4710 lookup_name_info lookup_name (search_name,
4711 symbol_name_match_type::FULL, true);
4713 auto bounds = index.find_name_components_bounds (lookup_name);
4715 size_t distance = std::distance (bounds.first, bounds.second);
4716 if (distance != expected_syms.size ())
4719 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4721 auto nc_elem = bounds.first + exp_elem;
4722 const char *qualified = index.symbol_name_at (nc_elem->idx);
4723 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4730 /* Test the lower-level mapped_index::find_name_component_bounds
4734 test_mapped_index_find_name_component_bounds ()
4736 mock_mapped_index mock_index (test_symbols);
4738 mock_index.build_name_components ();
4740 /* Test the lower-level mapped_index::find_name_component_bounds
4741 method in completion mode. */
4743 static const char *expected_syms[] = {
4748 SELF_CHECK (check_find_bounds_finds (mock_index,
4749 "t1_func", expected_syms));
4752 /* Check that the increment-last-char in the name matching algorithm
4753 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4755 static const char *expected_syms1[] = {
4759 SELF_CHECK (check_find_bounds_finds (mock_index,
4760 "\377", expected_syms1));
4762 static const char *expected_syms2[] = {
4765 SELF_CHECK (check_find_bounds_finds (mock_index,
4766 "\377\377", expected_syms2));
4770 /* Test dw2_expand_symtabs_matching_symbol. */
4773 test_dw2_expand_symtabs_matching_symbol ()
4775 mock_mapped_index mock_index (test_symbols);
4777 /* We let all tests run until the end even if some fails, for debug
4779 bool any_mismatch = false;
4781 /* Create the expected symbols list (an initializer_list). Needed
4782 because lists have commas, and we need to pass them to CHECK,
4783 which is a macro. */
4784 #define EXPECT(...) { __VA_ARGS__ }
4786 /* Wrapper for check_match that passes down the current
4787 __FILE__/__LINE__. */
4788 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4789 any_mismatch |= !check_match (__FILE__, __LINE__, \
4791 NAME, MATCH_TYPE, COMPLETION_MODE, \
4794 /* Identity checks. */
4795 for (const char *sym : test_symbols)
4797 /* Should be able to match all existing symbols. */
4798 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4801 /* Should be able to match all existing symbols with
4803 std::string with_params = std::string (sym) + "(int)";
4804 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4807 /* Should be able to match all existing symbols with
4808 parameters and qualifiers. */
4809 with_params = std::string (sym) + " ( int ) const";
4810 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4813 /* This should really find sym, but cp-name-parser.y doesn't
4814 know about lvalue/rvalue qualifiers yet. */
4815 with_params = std::string (sym) + " ( int ) &&";
4816 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4820 /* Check that the name matching algorithm for completion doesn't get
4821 confused with Latin1 'ÿ' / 0xff. */
4823 static const char str[] = "\377";
4824 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4825 EXPECT ("\377", "\377\377123"));
4828 /* Check that the increment-last-char in the matching algorithm for
4829 completion doesn't match "t1_fund" when completing "t1_func". */
4831 static const char str[] = "t1_func";
4832 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4833 EXPECT ("t1_func", "t1_func1"));
4836 /* Check that completion mode works at each prefix of the expected
4839 static const char str[] = "function(int)";
4840 size_t len = strlen (str);
4843 for (size_t i = 1; i < len; i++)
4845 lookup.assign (str, i);
4846 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4847 EXPECT ("function"));
4851 /* While "w" is a prefix of both components, the match function
4852 should still only be called once. */
4854 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4856 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4860 /* Same, with a "complicated" symbol. */
4862 static const char str[] = Z_SYM_NAME;
4863 size_t len = strlen (str);
4866 for (size_t i = 1; i < len; i++)
4868 lookup.assign (str, i);
4869 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4870 EXPECT (Z_SYM_NAME));
4874 /* In FULL mode, an incomplete symbol doesn't match. */
4876 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4880 /* A complete symbol with parameters matches any overload, since the
4881 index has no overload info. */
4883 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4884 EXPECT ("std::zfunction", "std::zfunction2"));
4885 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4886 EXPECT ("std::zfunction", "std::zfunction2"));
4887 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4888 EXPECT ("std::zfunction", "std::zfunction2"));
4891 /* Check that whitespace is ignored appropriately. A symbol with a
4892 template argument list. */
4894 static const char expected[] = "ns::foo<int>";
4895 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4897 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4901 /* Check that whitespace is ignored appropriately. A symbol with a
4902 template argument list that includes a pointer. */
4904 static const char expected[] = "ns::foo<char*>";
4905 /* Try both completion and non-completion modes. */
4906 static const bool completion_mode[2] = {false, true};
4907 for (size_t i = 0; i < 2; i++)
4909 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4910 completion_mode[i], EXPECT (expected));
4911 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4912 completion_mode[i], EXPECT (expected));
4914 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4915 completion_mode[i], EXPECT (expected));
4916 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4917 completion_mode[i], EXPECT (expected));
4922 /* Check method qualifiers are ignored. */
4923 static const char expected[] = "ns::foo<char*>";
4924 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4925 symbol_name_match_type::FULL, true, EXPECT (expected));
4926 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4927 symbol_name_match_type::FULL, true, EXPECT (expected));
4928 CHECK_MATCH ("foo < char * > ( int ) const",
4929 symbol_name_match_type::WILD, true, EXPECT (expected));
4930 CHECK_MATCH ("foo < char * > ( int ) &&",
4931 symbol_name_match_type::WILD, true, EXPECT (expected));
4934 /* Test lookup names that don't match anything. */
4936 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4939 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4943 /* Some wild matching tests, exercising "(anonymous namespace)",
4944 which should not be confused with a parameter list. */
4946 static const char *syms[] = {
4950 "A :: B :: C ( int )",
4955 for (const char *s : syms)
4957 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4958 EXPECT ("(anonymous namespace)::A::B::C"));
4963 static const char expected[] = "ns2::tmpl<int>::foo2";
4964 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4966 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4970 SELF_CHECK (!any_mismatch);
4979 test_mapped_index_find_name_component_bounds ();
4980 test_dw2_expand_symtabs_matching_symbol ();
4983 }} // namespace selftests::dw2_expand_symtabs_matching
4985 #endif /* GDB_SELF_TEST */
4987 /* If FILE_MATCHER is NULL or if PER_CU has
4988 dwarf2_per_cu_quick_data::MARK set (see
4989 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4990 EXPANSION_NOTIFY on it. */
4993 dw2_expand_symtabs_matching_one
4994 (struct dwarf2_per_cu_data *per_cu,
4995 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4996 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4998 if (file_matcher == NULL || per_cu->v.quick->mark)
5000 bool symtab_was_null
5001 = (per_cu->v.quick->compunit_symtab == NULL);
5003 dw2_instantiate_symtab (per_cu, false);
5005 if (expansion_notify != NULL
5007 && per_cu->v.quick->compunit_symtab != NULL)
5008 expansion_notify (per_cu->v.quick->compunit_symtab);
5012 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5013 matched, to expand corresponding CUs that were marked. IDX is the
5014 index of the symbol name that matched. */
5017 dw2_expand_marked_cus
5018 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5019 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5020 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5023 offset_type *vec, vec_len, vec_idx;
5024 bool global_seen = false;
5025 mapped_index &index = *dwarf2_per_objfile->index_table;
5027 vec = (offset_type *) (index.constant_pool
5028 + MAYBE_SWAP (index.symbol_table[idx].vec));
5029 vec_len = MAYBE_SWAP (vec[0]);
5030 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5032 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5033 /* This value is only valid for index versions >= 7. */
5034 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5035 gdb_index_symbol_kind symbol_kind =
5036 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5037 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5038 /* Only check the symbol attributes if they're present.
5039 Indices prior to version 7 don't record them,
5040 and indices >= 7 may elide them for certain symbols
5041 (gold does this). */
5044 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5046 /* Work around gold/15646. */
5049 if (!is_static && global_seen)
5055 /* Only check the symbol's kind if it has one. */
5060 case VARIABLES_DOMAIN:
5061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5064 case FUNCTIONS_DOMAIN:
5065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5077 /* Don't crash on bad data. */
5078 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5079 + dwarf2_per_objfile->all_type_units.size ()))
5081 complaint (_(".gdb_index entry has bad CU index"
5083 objfile_name (dwarf2_per_objfile->objfile));
5087 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5088 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5093 /* If FILE_MATCHER is non-NULL, set all the
5094 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5095 that match FILE_MATCHER. */
5098 dw_expand_symtabs_matching_file_matcher
5099 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5100 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5102 if (file_matcher == NULL)
5105 objfile *const objfile = dwarf2_per_objfile->objfile;
5107 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5109 NULL, xcalloc, xfree));
5110 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5112 NULL, xcalloc, xfree));
5114 /* The rule is CUs specify all the files, including those used by
5115 any TU, so there's no need to scan TUs here. */
5117 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5121 per_cu->v.quick->mark = 0;
5123 /* We only need to look at symtabs not already expanded. */
5124 if (per_cu->v.quick->compunit_symtab)
5127 quick_file_names *file_data = dw2_get_file_names (per_cu);
5128 if (file_data == NULL)
5131 if (htab_find (visited_not_found.get (), file_data) != NULL)
5133 else if (htab_find (visited_found.get (), file_data) != NULL)
5135 per_cu->v.quick->mark = 1;
5139 for (int j = 0; j < file_data->num_file_names; ++j)
5141 const char *this_real_name;
5143 if (file_matcher (file_data->file_names[j], false))
5145 per_cu->v.quick->mark = 1;
5149 /* Before we invoke realpath, which can get expensive when many
5150 files are involved, do a quick comparison of the basenames. */
5151 if (!basenames_may_differ
5152 && !file_matcher (lbasename (file_data->file_names[j]),
5156 this_real_name = dw2_get_real_path (objfile, file_data, j);
5157 if (file_matcher (this_real_name, false))
5159 per_cu->v.quick->mark = 1;
5164 void **slot = htab_find_slot (per_cu->v.quick->mark
5165 ? visited_found.get ()
5166 : visited_not_found.get (),
5173 dw2_expand_symtabs_matching
5174 (struct objfile *objfile,
5175 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5176 const lookup_name_info &lookup_name,
5177 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5178 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5179 enum search_domain kind)
5181 struct dwarf2_per_objfile *dwarf2_per_objfile
5182 = get_dwarf2_per_objfile (objfile);
5184 /* index_table is NULL if OBJF_READNOW. */
5185 if (!dwarf2_per_objfile->index_table)
5188 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5190 mapped_index &index = *dwarf2_per_objfile->index_table;
5192 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5194 kind, [&] (offset_type idx)
5196 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5197 expansion_notify, kind);
5201 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5204 static struct compunit_symtab *
5205 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5210 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5211 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5214 if (cust->includes == NULL)
5217 for (i = 0; cust->includes[i]; ++i)
5219 struct compunit_symtab *s = cust->includes[i];
5221 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5229 static struct compunit_symtab *
5230 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5231 struct bound_minimal_symbol msymbol,
5233 struct obj_section *section,
5236 struct dwarf2_per_cu_data *data;
5237 struct compunit_symtab *result;
5239 if (!objfile->partial_symtabs->psymtabs_addrmap)
5242 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5243 SECT_OFF_TEXT (objfile));
5244 data = (struct dwarf2_per_cu_data *) addrmap_find
5245 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5249 if (warn_if_readin && data->v.quick->compunit_symtab)
5250 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5251 paddress (get_objfile_arch (objfile), pc));
5254 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5257 gdb_assert (result != NULL);
5262 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5263 void *data, int need_fullname)
5265 struct dwarf2_per_objfile *dwarf2_per_objfile
5266 = get_dwarf2_per_objfile (objfile);
5268 if (!dwarf2_per_objfile->filenames_cache)
5270 dwarf2_per_objfile->filenames_cache.emplace ();
5272 htab_up visited (htab_create_alloc (10,
5273 htab_hash_pointer, htab_eq_pointer,
5274 NULL, xcalloc, xfree));
5276 /* The rule is CUs specify all the files, including those used
5277 by any TU, so there's no need to scan TUs here. We can
5278 ignore file names coming from already-expanded CUs. */
5280 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5282 if (per_cu->v.quick->compunit_symtab)
5284 void **slot = htab_find_slot (visited.get (),
5285 per_cu->v.quick->file_names,
5288 *slot = per_cu->v.quick->file_names;
5292 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5294 /* We only need to look at symtabs not already expanded. */
5295 if (per_cu->v.quick->compunit_symtab)
5298 quick_file_names *file_data = dw2_get_file_names (per_cu);
5299 if (file_data == NULL)
5302 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5305 /* Already visited. */
5310 for (int j = 0; j < file_data->num_file_names; ++j)
5312 const char *filename = file_data->file_names[j];
5313 dwarf2_per_objfile->filenames_cache->seen (filename);
5318 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5320 gdb::unique_xmalloc_ptr<char> this_real_name;
5323 this_real_name = gdb_realpath (filename);
5324 (*fun) (filename, this_real_name.get (), data);
5329 dw2_has_symbols (struct objfile *objfile)
5334 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5337 dw2_find_last_source_symtab,
5338 dw2_forget_cached_source_info,
5339 dw2_map_symtabs_matching_filename,
5343 dw2_expand_symtabs_for_function,
5344 dw2_expand_all_symtabs,
5345 dw2_expand_symtabs_with_fullname,
5346 dw2_map_matching_symbols,
5347 dw2_expand_symtabs_matching,
5348 dw2_find_pc_sect_compunit_symtab,
5350 dw2_map_symbol_filenames
5353 /* DWARF-5 debug_names reader. */
5355 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5356 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5358 /* A helper function that reads the .debug_names section in SECTION
5359 and fills in MAP. FILENAME is the name of the file containing the
5360 section; it is used for error reporting.
5362 Returns true if all went well, false otherwise. */
5365 read_debug_names_from_section (struct objfile *objfile,
5366 const char *filename,
5367 struct dwarf2_section_info *section,
5368 mapped_debug_names &map)
5370 if (dwarf2_section_empty_p (section))
5373 /* Older elfutils strip versions could keep the section in the main
5374 executable while splitting it for the separate debug info file. */
5375 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5378 dwarf2_read_section (objfile, section);
5380 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5382 const gdb_byte *addr = section->buffer;
5384 bfd *const abfd = get_section_bfd_owner (section);
5386 unsigned int bytes_read;
5387 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5390 map.dwarf5_is_dwarf64 = bytes_read != 4;
5391 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5392 if (bytes_read + length != section->size)
5394 /* There may be multiple per-CU indices. */
5395 warning (_("Section .debug_names in %s length %s does not match "
5396 "section length %s, ignoring .debug_names."),
5397 filename, plongest (bytes_read + length),
5398 pulongest (section->size));
5402 /* The version number. */
5403 uint16_t version = read_2_bytes (abfd, addr);
5407 warning (_("Section .debug_names in %s has unsupported version %d, "
5408 "ignoring .debug_names."),
5414 uint16_t padding = read_2_bytes (abfd, addr);
5418 warning (_("Section .debug_names in %s has unsupported padding %d, "
5419 "ignoring .debug_names."),
5424 /* comp_unit_count - The number of CUs in the CU list. */
5425 map.cu_count = read_4_bytes (abfd, addr);
5428 /* local_type_unit_count - The number of TUs in the local TU
5430 map.tu_count = read_4_bytes (abfd, addr);
5433 /* foreign_type_unit_count - The number of TUs in the foreign TU
5435 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5437 if (foreign_tu_count != 0)
5439 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5440 "ignoring .debug_names."),
5441 filename, static_cast<unsigned long> (foreign_tu_count));
5445 /* bucket_count - The number of hash buckets in the hash lookup
5447 map.bucket_count = read_4_bytes (abfd, addr);
5450 /* name_count - The number of unique names in the index. */
5451 map.name_count = read_4_bytes (abfd, addr);
5454 /* abbrev_table_size - The size in bytes of the abbreviations
5456 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5459 /* augmentation_string_size - The size in bytes of the augmentation
5460 string. This value is rounded up to a multiple of 4. */
5461 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5463 map.augmentation_is_gdb = ((augmentation_string_size
5464 == sizeof (dwarf5_augmentation))
5465 && memcmp (addr, dwarf5_augmentation,
5466 sizeof (dwarf5_augmentation)) == 0);
5467 augmentation_string_size += (-augmentation_string_size) & 3;
5468 addr += augmentation_string_size;
5471 map.cu_table_reordered = addr;
5472 addr += map.cu_count * map.offset_size;
5474 /* List of Local TUs */
5475 map.tu_table_reordered = addr;
5476 addr += map.tu_count * map.offset_size;
5478 /* Hash Lookup Table */
5479 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5480 addr += map.bucket_count * 4;
5481 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5482 addr += map.name_count * 4;
5485 map.name_table_string_offs_reordered = addr;
5486 addr += map.name_count * map.offset_size;
5487 map.name_table_entry_offs_reordered = addr;
5488 addr += map.name_count * map.offset_size;
5490 const gdb_byte *abbrev_table_start = addr;
5493 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5498 const auto insertpair
5499 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5500 if (!insertpair.second)
5502 warning (_("Section .debug_names in %s has duplicate index %s, "
5503 "ignoring .debug_names."),
5504 filename, pulongest (index_num));
5507 mapped_debug_names::index_val &indexval = insertpair.first->second;
5508 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 mapped_debug_names::index_val::attr attr;
5514 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5518 if (attr.form == DW_FORM_implicit_const)
5520 attr.implicit_const = read_signed_leb128 (abfd, addr,
5524 if (attr.dw_idx == 0 && attr.form == 0)
5526 indexval.attr_vec.push_back (std::move (attr));
5529 if (addr != abbrev_table_start + abbrev_table_size)
5531 warning (_("Section .debug_names in %s has abbreviation_table "
5532 "of size %s vs. written as %u, ignoring .debug_names."),
5533 filename, plongest (addr - abbrev_table_start),
5537 map.entry_pool = addr;
5542 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5546 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5547 const mapped_debug_names &map,
5548 dwarf2_section_info §ion,
5551 sect_offset sect_off_prev;
5552 for (uint32_t i = 0; i <= map.cu_count; ++i)
5554 sect_offset sect_off_next;
5555 if (i < map.cu_count)
5558 = (sect_offset) (extract_unsigned_integer
5559 (map.cu_table_reordered + i * map.offset_size,
5561 map.dwarf5_byte_order));
5564 sect_off_next = (sect_offset) section.size;
5567 const ULONGEST length = sect_off_next - sect_off_prev;
5568 dwarf2_per_cu_data *per_cu
5569 = create_cu_from_index_list (dwarf2_per_objfile, §ion, is_dwz,
5570 sect_off_prev, length);
5571 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5573 sect_off_prev = sect_off_next;
5577 /* Read the CU list from the mapped index, and use it to create all
5578 the CU objects for this dwarf2_per_objfile. */
5581 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5582 const mapped_debug_names &map,
5583 const mapped_debug_names &dwz_map)
5585 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5586 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5588 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5589 dwarf2_per_objfile->info,
5590 false /* is_dwz */);
5592 if (dwz_map.cu_count == 0)
5595 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5596 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5600 /* Read .debug_names. If everything went ok, initialize the "quick"
5601 elements of all the CUs and return true. Otherwise, return false. */
5604 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5606 std::unique_ptr<mapped_debug_names> map
5607 (new mapped_debug_names (dwarf2_per_objfile));
5608 mapped_debug_names dwz_map (dwarf2_per_objfile);
5609 struct objfile *objfile = dwarf2_per_objfile->objfile;
5611 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5612 &dwarf2_per_objfile->debug_names,
5616 /* Don't use the index if it's empty. */
5617 if (map->name_count == 0)
5620 /* If there is a .dwz file, read it so we can get its CU list as
5622 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5625 if (!read_debug_names_from_section (objfile,
5626 bfd_get_filename (dwz->dwz_bfd),
5627 &dwz->debug_names, dwz_map))
5629 warning (_("could not read '.debug_names' section from %s; skipping"),
5630 bfd_get_filename (dwz->dwz_bfd));
5635 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5637 if (map->tu_count != 0)
5639 /* We can only handle a single .debug_types when we have an
5641 if (dwarf2_per_objfile->types.size () != 1)
5644 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5646 create_signatured_type_table_from_debug_names
5647 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5650 create_addrmap_from_aranges (dwarf2_per_objfile,
5651 &dwarf2_per_objfile->debug_aranges);
5653 dwarf2_per_objfile->debug_names_table = std::move (map);
5654 dwarf2_per_objfile->using_index = 1;
5655 dwarf2_per_objfile->quick_file_names_table =
5656 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5661 /* Type used to manage iterating over all CUs looking for a symbol for
5664 class dw2_debug_names_iterator
5667 dw2_debug_names_iterator (const mapped_debug_names &map,
5668 gdb::optional<block_enum> block_index,
5671 : m_map (map), m_block_index (block_index), m_domain (domain),
5672 m_addr (find_vec_in_debug_names (map, name))
5675 dw2_debug_names_iterator (const mapped_debug_names &map,
5676 search_domain search, uint32_t namei)
5679 m_addr (find_vec_in_debug_names (map, namei))
5682 /* Return the next matching CU or NULL if there are no more. */
5683 dwarf2_per_cu_data *next ();
5686 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5688 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5691 /* The internalized form of .debug_names. */
5692 const mapped_debug_names &m_map;
5694 /* If set, only look for symbols that match that block. Valid values are
5695 GLOBAL_BLOCK and STATIC_BLOCK. */
5696 const gdb::optional<block_enum> m_block_index;
5698 /* The kind of symbol we're looking for. */
5699 const domain_enum m_domain = UNDEF_DOMAIN;
5700 const search_domain m_search = ALL_DOMAIN;
5702 /* The list of CUs from the index entry of the symbol, or NULL if
5704 const gdb_byte *m_addr;
5708 mapped_debug_names::namei_to_name (uint32_t namei) const
5710 const ULONGEST namei_string_offs
5711 = extract_unsigned_integer ((name_table_string_offs_reordered
5712 + namei * offset_size),
5715 return read_indirect_string_at_offset
5716 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5719 /* Find a slot in .debug_names for the object named NAME. If NAME is
5720 found, return pointer to its pool data. If NAME cannot be found,
5724 dw2_debug_names_iterator::find_vec_in_debug_names
5725 (const mapped_debug_names &map, const char *name)
5727 int (*cmp) (const char *, const char *);
5729 gdb::unique_xmalloc_ptr<char> without_params;
5730 if (current_language->la_language == language_cplus
5731 || current_language->la_language == language_fortran
5732 || current_language->la_language == language_d)
5734 /* NAME is already canonical. Drop any qualifiers as
5735 .debug_names does not contain any. */
5737 if (strchr (name, '(') != NULL)
5739 without_params = cp_remove_params (name);
5740 if (without_params != NULL)
5741 name = without_params.get ();
5745 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5747 const uint32_t full_hash = dwarf5_djb_hash (name);
5749 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5750 (map.bucket_table_reordered
5751 + (full_hash % map.bucket_count)), 4,
5752 map.dwarf5_byte_order);
5756 if (namei >= map.name_count)
5758 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5760 namei, map.name_count,
5761 objfile_name (map.dwarf2_per_objfile->objfile));
5767 const uint32_t namei_full_hash
5768 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5769 (map.hash_table_reordered + namei), 4,
5770 map.dwarf5_byte_order);
5771 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5774 if (full_hash == namei_full_hash)
5776 const char *const namei_string = map.namei_to_name (namei);
5778 #if 0 /* An expensive sanity check. */
5779 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5781 complaint (_("Wrong .debug_names hash for string at index %u "
5783 namei, objfile_name (dwarf2_per_objfile->objfile));
5788 if (cmp (namei_string, name) == 0)
5790 const ULONGEST namei_entry_offs
5791 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5792 + namei * map.offset_size),
5793 map.offset_size, map.dwarf5_byte_order);
5794 return map.entry_pool + namei_entry_offs;
5799 if (namei >= map.name_count)
5805 dw2_debug_names_iterator::find_vec_in_debug_names
5806 (const mapped_debug_names &map, uint32_t namei)
5808 if (namei >= map.name_count)
5810 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5812 namei, map.name_count,
5813 objfile_name (map.dwarf2_per_objfile->objfile));
5817 const ULONGEST namei_entry_offs
5818 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5819 + namei * map.offset_size),
5820 map.offset_size, map.dwarf5_byte_order);
5821 return map.entry_pool + namei_entry_offs;
5824 /* See dw2_debug_names_iterator. */
5826 dwarf2_per_cu_data *
5827 dw2_debug_names_iterator::next ()
5832 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5833 struct objfile *objfile = dwarf2_per_objfile->objfile;
5834 bfd *const abfd = objfile->obfd;
5838 unsigned int bytes_read;
5839 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5840 m_addr += bytes_read;
5844 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5845 if (indexval_it == m_map.abbrev_map.cend ())
5847 complaint (_("Wrong .debug_names undefined abbrev code %s "
5849 pulongest (abbrev), objfile_name (objfile));
5852 const mapped_debug_names::index_val &indexval = indexval_it->second;
5853 enum class symbol_linkage {
5857 } symbol_linkage_ = symbol_linkage::unknown;
5858 dwarf2_per_cu_data *per_cu = NULL;
5859 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5864 case DW_FORM_implicit_const:
5865 ull = attr.implicit_const;
5867 case DW_FORM_flag_present:
5871 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5872 m_addr += bytes_read;
5875 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5876 dwarf_form_name (attr.form),
5877 objfile_name (objfile));
5880 switch (attr.dw_idx)
5882 case DW_IDX_compile_unit:
5883 /* Don't crash on bad data. */
5884 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5886 complaint (_(".debug_names entry has bad CU index %s"
5889 objfile_name (dwarf2_per_objfile->objfile));
5892 per_cu = dwarf2_per_objfile->get_cutu (ull);
5894 case DW_IDX_type_unit:
5895 /* Don't crash on bad data. */
5896 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5898 complaint (_(".debug_names entry has bad TU index %s"
5901 objfile_name (dwarf2_per_objfile->objfile));
5904 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5906 case DW_IDX_GNU_internal:
5907 if (!m_map.augmentation_is_gdb)
5909 symbol_linkage_ = symbol_linkage::static_;
5911 case DW_IDX_GNU_external:
5912 if (!m_map.augmentation_is_gdb)
5914 symbol_linkage_ = symbol_linkage::extern_;
5919 /* Skip if already read in. */
5920 if (per_cu->v.quick->compunit_symtab)
5923 /* Check static vs global. */
5924 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5926 const bool want_static = *m_block_index == STATIC_BLOCK;
5927 const bool symbol_is_static =
5928 symbol_linkage_ == symbol_linkage::static_;
5929 if (want_static != symbol_is_static)
5933 /* Match dw2_symtab_iter_next, symbol_kind
5934 and debug_names::psymbol_tag. */
5938 switch (indexval.dwarf_tag)
5940 case DW_TAG_variable:
5941 case DW_TAG_subprogram:
5942 /* Some types are also in VAR_DOMAIN. */
5943 case DW_TAG_typedef:
5944 case DW_TAG_structure_type:
5951 switch (indexval.dwarf_tag)
5953 case DW_TAG_typedef:
5954 case DW_TAG_structure_type:
5961 switch (indexval.dwarf_tag)
5964 case DW_TAG_variable:
5974 /* Match dw2_expand_symtabs_matching, symbol_kind and
5975 debug_names::psymbol_tag. */
5978 case VARIABLES_DOMAIN:
5979 switch (indexval.dwarf_tag)
5981 case DW_TAG_variable:
5987 case FUNCTIONS_DOMAIN:
5988 switch (indexval.dwarf_tag)
5990 case DW_TAG_subprogram:
5997 switch (indexval.dwarf_tag)
5999 case DW_TAG_typedef:
6000 case DW_TAG_structure_type:
6013 static struct compunit_symtab *
6014 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6015 const char *name, domain_enum domain)
6017 struct dwarf2_per_objfile *dwarf2_per_objfile
6018 = get_dwarf2_per_objfile (objfile);
6020 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6023 /* index is NULL if OBJF_READNOW. */
6026 const auto &map = *mapp;
6028 dw2_debug_names_iterator iter (map, block_index, domain, name);
6030 struct compunit_symtab *stab_best = NULL;
6031 struct dwarf2_per_cu_data *per_cu;
6032 while ((per_cu = iter.next ()) != NULL)
6034 struct symbol *sym, *with_opaque = NULL;
6035 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6036 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6037 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6039 sym = block_find_symbol (block, name, domain,
6040 block_find_non_opaque_type_preferred,
6043 /* Some caution must be observed with overloaded functions and
6044 methods, since the index will not contain any overload
6045 information (but NAME might contain it). */
6048 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6050 if (with_opaque != NULL
6051 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6054 /* Keep looking through other CUs. */
6060 /* This dumps minimal information about .debug_names. It is called
6061 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6062 uses this to verify that .debug_names has been loaded. */
6065 dw2_debug_names_dump (struct objfile *objfile)
6067 struct dwarf2_per_objfile *dwarf2_per_objfile
6068 = get_dwarf2_per_objfile (objfile);
6070 gdb_assert (dwarf2_per_objfile->using_index);
6071 printf_filtered (".debug_names:");
6072 if (dwarf2_per_objfile->debug_names_table)
6073 printf_filtered (" exists\n");
6075 printf_filtered (" faked for \"readnow\"\n");
6076 printf_filtered ("\n");
6080 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6081 const char *func_name)
6083 struct dwarf2_per_objfile *dwarf2_per_objfile
6084 = get_dwarf2_per_objfile (objfile);
6086 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6087 if (dwarf2_per_objfile->debug_names_table)
6089 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6091 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6093 struct dwarf2_per_cu_data *per_cu;
6094 while ((per_cu = iter.next ()) != NULL)
6095 dw2_instantiate_symtab (per_cu, false);
6100 dw2_debug_names_expand_symtabs_matching
6101 (struct objfile *objfile,
6102 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6103 const lookup_name_info &lookup_name,
6104 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6105 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6106 enum search_domain kind)
6108 struct dwarf2_per_objfile *dwarf2_per_objfile
6109 = get_dwarf2_per_objfile (objfile);
6111 /* debug_names_table is NULL if OBJF_READNOW. */
6112 if (!dwarf2_per_objfile->debug_names_table)
6115 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6117 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6119 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6121 kind, [&] (offset_type namei)
6123 /* The name was matched, now expand corresponding CUs that were
6125 dw2_debug_names_iterator iter (map, kind, namei);
6127 struct dwarf2_per_cu_data *per_cu;
6128 while ((per_cu = iter.next ()) != NULL)
6129 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6134 const struct quick_symbol_functions dwarf2_debug_names_functions =
6137 dw2_find_last_source_symtab,
6138 dw2_forget_cached_source_info,
6139 dw2_map_symtabs_matching_filename,
6140 dw2_debug_names_lookup_symbol,
6142 dw2_debug_names_dump,
6143 dw2_debug_names_expand_symtabs_for_function,
6144 dw2_expand_all_symtabs,
6145 dw2_expand_symtabs_with_fullname,
6146 dw2_map_matching_symbols,
6147 dw2_debug_names_expand_symtabs_matching,
6148 dw2_find_pc_sect_compunit_symtab,
6150 dw2_map_symbol_filenames
6153 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6154 to either a dwarf2_per_objfile or dwz_file object. */
6156 template <typename T>
6157 static gdb::array_view<const gdb_byte>
6158 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6160 dwarf2_section_info *section = §ion_owner->gdb_index;
6162 if (dwarf2_section_empty_p (section))
6165 /* Older elfutils strip versions could keep the section in the main
6166 executable while splitting it for the separate debug info file. */
6167 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6170 dwarf2_read_section (obj, section);
6172 /* dwarf2_section_info::size is a bfd_size_type, while
6173 gdb::array_view works with size_t. On 32-bit hosts, with
6174 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6175 is 32-bit. So we need an explicit narrowing conversion here.
6176 This is fine, because it's impossible to allocate or mmap an
6177 array/buffer larger than what size_t can represent. */
6178 return gdb::make_array_view (section->buffer, section->size);
6181 /* Lookup the index cache for the contents of the index associated to
6184 static gdb::array_view<const gdb_byte>
6185 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6187 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6188 if (build_id == nullptr)
6191 return global_index_cache.lookup_gdb_index (build_id,
6192 &dwarf2_obj->index_cache_res);
6195 /* Same as the above, but for DWZ. */
6197 static gdb::array_view<const gdb_byte>
6198 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6200 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6201 if (build_id == nullptr)
6204 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6207 /* See symfile.h. */
6210 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6212 struct dwarf2_per_objfile *dwarf2_per_objfile
6213 = get_dwarf2_per_objfile (objfile);
6215 /* If we're about to read full symbols, don't bother with the
6216 indices. In this case we also don't care if some other debug
6217 format is making psymtabs, because they are all about to be
6219 if ((objfile->flags & OBJF_READNOW))
6221 dwarf2_per_objfile->using_index = 1;
6222 create_all_comp_units (dwarf2_per_objfile);
6223 create_all_type_units (dwarf2_per_objfile);
6224 dwarf2_per_objfile->quick_file_names_table
6225 = create_quick_file_names_table
6226 (dwarf2_per_objfile->all_comp_units.size ());
6228 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6229 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6231 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6233 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6234 struct dwarf2_per_cu_quick_data);
6237 /* Return 1 so that gdb sees the "quick" functions. However,
6238 these functions will be no-ops because we will have expanded
6240 *index_kind = dw_index_kind::GDB_INDEX;
6244 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6246 *index_kind = dw_index_kind::DEBUG_NAMES;
6250 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6251 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6252 get_gdb_index_contents_from_section<dwz_file>))
6254 *index_kind = dw_index_kind::GDB_INDEX;
6258 /* ... otherwise, try to find the index in the index cache. */
6259 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6260 get_gdb_index_contents_from_cache,
6261 get_gdb_index_contents_from_cache_dwz))
6263 global_index_cache.hit ();
6264 *index_kind = dw_index_kind::GDB_INDEX;
6268 global_index_cache.miss ();
6274 /* Build a partial symbol table. */
6277 dwarf2_build_psymtabs (struct objfile *objfile)
6279 struct dwarf2_per_objfile *dwarf2_per_objfile
6280 = get_dwarf2_per_objfile (objfile);
6282 init_psymbol_list (objfile, 1024);
6286 /* This isn't really ideal: all the data we allocate on the
6287 objfile's obstack is still uselessly kept around. However,
6288 freeing it seems unsafe. */
6289 psymtab_discarder psymtabs (objfile);
6290 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6293 /* (maybe) store an index in the cache. */
6294 global_index_cache.store (dwarf2_per_objfile);
6296 catch (const gdb_exception_error &except)
6298 exception_print (gdb_stderr, except);
6302 /* Return the total length of the CU described by HEADER. */
6305 get_cu_length (const struct comp_unit_head *header)
6307 return header->initial_length_size + header->length;
6310 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6313 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6315 sect_offset bottom = cu_header->sect_off;
6316 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6318 return sect_off >= bottom && sect_off < top;
6321 /* Find the base address of the compilation unit for range lists and
6322 location lists. It will normally be specified by DW_AT_low_pc.
6323 In DWARF-3 draft 4, the base address could be overridden by
6324 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6325 compilation units with discontinuous ranges. */
6328 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6330 struct attribute *attr;
6333 cu->base_address = 0;
6335 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6338 cu->base_address = attr_value_as_address (attr);
6343 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6346 cu->base_address = attr_value_as_address (attr);
6352 /* Read in the comp unit header information from the debug_info at info_ptr.
6353 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6354 NOTE: This leaves members offset, first_die_offset to be filled in
6357 static const gdb_byte *
6358 read_comp_unit_head (struct comp_unit_head *cu_header,
6359 const gdb_byte *info_ptr,
6360 struct dwarf2_section_info *section,
6361 rcuh_kind section_kind)
6364 unsigned int bytes_read;
6365 const char *filename = get_section_file_name (section);
6366 bfd *abfd = get_section_bfd_owner (section);
6368 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6369 cu_header->initial_length_size = bytes_read;
6370 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6371 info_ptr += bytes_read;
6372 cu_header->version = read_2_bytes (abfd, info_ptr);
6373 if (cu_header->version < 2 || cu_header->version > 5)
6374 error (_("Dwarf Error: wrong version in compilation unit header "
6375 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6376 cu_header->version, filename);
6378 if (cu_header->version < 5)
6379 switch (section_kind)
6381 case rcuh_kind::COMPILE:
6382 cu_header->unit_type = DW_UT_compile;
6384 case rcuh_kind::TYPE:
6385 cu_header->unit_type = DW_UT_type;
6388 internal_error (__FILE__, __LINE__,
6389 _("read_comp_unit_head: invalid section_kind"));
6393 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6394 (read_1_byte (abfd, info_ptr));
6396 switch (cu_header->unit_type)
6400 case DW_UT_skeleton:
6401 case DW_UT_split_compile:
6402 if (section_kind != rcuh_kind::COMPILE)
6403 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6404 "(is %s, should be %s) [in module %s]"),
6405 dwarf_unit_type_name (cu_header->unit_type),
6406 dwarf_unit_type_name (DW_UT_type), filename);
6409 case DW_UT_split_type:
6410 section_kind = rcuh_kind::TYPE;
6413 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6414 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6415 "[in module %s]"), cu_header->unit_type,
6416 dwarf_unit_type_name (DW_UT_compile),
6417 dwarf_unit_type_name (DW_UT_skeleton),
6418 dwarf_unit_type_name (DW_UT_split_compile),
6419 dwarf_unit_type_name (DW_UT_type),
6420 dwarf_unit_type_name (DW_UT_split_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 bool header_has_signature = section_kind == rcuh_kind::TYPE
6442 || cu_header->unit_type == DW_UT_skeleton
6443 || cu_header->unit_type == DW_UT_split_compile;
6445 if (header_has_signature)
6447 cu_header->signature = read_8_bytes (abfd, info_ptr);
6451 if (section_kind == rcuh_kind::TYPE)
6453 LONGEST type_offset;
6454 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6455 info_ptr += bytes_read;
6456 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6457 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6458 error (_("Dwarf Error: Too big type_offset in compilation unit "
6459 "header (is %s) [in module %s]"), plongest (type_offset),
6466 /* Helper function that returns the proper abbrev section for
6469 static struct dwarf2_section_info *
6470 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6472 struct dwarf2_section_info *abbrev;
6473 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6475 if (this_cu->is_dwz)
6476 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6478 abbrev = &dwarf2_per_objfile->abbrev;
6483 /* Subroutine of read_and_check_comp_unit_head and
6484 read_and_check_type_unit_head to simplify them.
6485 Perform various error checking on the header. */
6488 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6489 struct comp_unit_head *header,
6490 struct dwarf2_section_info *section,
6491 struct dwarf2_section_info *abbrev_section)
6493 const char *filename = get_section_file_name (section);
6495 if (to_underlying (header->abbrev_sect_off)
6496 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6497 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6498 "(offset %s + 6) [in module %s]"),
6499 sect_offset_str (header->abbrev_sect_off),
6500 sect_offset_str (header->sect_off),
6503 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6504 avoid potential 32-bit overflow. */
6505 if (((ULONGEST) header->sect_off + get_cu_length (header))
6507 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6508 "(offset %s + 0) [in module %s]"),
6509 header->length, sect_offset_str (header->sect_off),
6513 /* Read in a CU/TU header and perform some basic error checking.
6514 The contents of the header are stored in HEADER.
6515 The result is a pointer to the start of the first DIE. */
6517 static const gdb_byte *
6518 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6519 struct comp_unit_head *header,
6520 struct dwarf2_section_info *section,
6521 struct dwarf2_section_info *abbrev_section,
6522 const gdb_byte *info_ptr,
6523 rcuh_kind section_kind)
6525 const gdb_byte *beg_of_comp_unit = info_ptr;
6527 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6529 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6531 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6533 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6539 /* Fetch the abbreviation table offset from a comp or type unit header. */
6542 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6543 struct dwarf2_section_info *section,
6544 sect_offset sect_off)
6546 bfd *abfd = get_section_bfd_owner (section);
6547 const gdb_byte *info_ptr;
6548 unsigned int initial_length_size, offset_size;
6551 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6552 info_ptr = section->buffer + to_underlying (sect_off);
6553 read_initial_length (abfd, info_ptr, &initial_length_size);
6554 offset_size = initial_length_size == 4 ? 4 : 8;
6555 info_ptr += initial_length_size;
6557 version = read_2_bytes (abfd, info_ptr);
6561 /* Skip unit type and address size. */
6565 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6568 /* Allocate a new partial symtab for file named NAME and mark this new
6569 partial symtab as being an include of PST. */
6572 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6573 struct objfile *objfile)
6575 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6577 if (!IS_ABSOLUTE_PATH (subpst->filename))
6579 /* It shares objfile->objfile_obstack. */
6580 subpst->dirname = pst->dirname;
6583 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6584 subpst->dependencies[0] = pst;
6585 subpst->number_of_dependencies = 1;
6587 subpst->read_symtab = pst->read_symtab;
6589 /* No private part is necessary for include psymtabs. This property
6590 can be used to differentiate between such include psymtabs and
6591 the regular ones. */
6592 subpst->read_symtab_private = NULL;
6595 /* Read the Line Number Program data and extract the list of files
6596 included by the source file represented by PST. Build an include
6597 partial symtab for each of these included files. */
6600 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6601 struct die_info *die,
6602 struct partial_symtab *pst)
6605 struct attribute *attr;
6607 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6609 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6611 return; /* No linetable, so no includes. */
6613 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6614 that we pass in the raw text_low here; that is ok because we're
6615 only decoding the line table to make include partial symtabs, and
6616 so the addresses aren't really used. */
6617 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6618 pst->raw_text_low (), 1);
6622 hash_signatured_type (const void *item)
6624 const struct signatured_type *sig_type
6625 = (const struct signatured_type *) item;
6627 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6628 return sig_type->signature;
6632 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6634 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6635 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6637 return lhs->signature == rhs->signature;
6640 /* Allocate a hash table for signatured types. */
6643 allocate_signatured_type_table (struct objfile *objfile)
6645 return htab_create_alloc_ex (41,
6646 hash_signatured_type,
6649 &objfile->objfile_obstack,
6650 hashtab_obstack_allocate,
6651 dummy_obstack_deallocate);
6654 /* A helper function to add a signatured type CU to a table. */
6657 add_signatured_type_cu_to_table (void **slot, void *datum)
6659 struct signatured_type *sigt = (struct signatured_type *) *slot;
6660 std::vector<signatured_type *> *all_type_units
6661 = (std::vector<signatured_type *> *) datum;
6663 all_type_units->push_back (sigt);
6668 /* A helper for create_debug_types_hash_table. Read types from SECTION
6669 and fill them into TYPES_HTAB. It will process only type units,
6670 therefore DW_UT_type. */
6673 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6674 struct dwo_file *dwo_file,
6675 dwarf2_section_info *section, htab_t &types_htab,
6676 rcuh_kind section_kind)
6678 struct objfile *objfile = dwarf2_per_objfile->objfile;
6679 struct dwarf2_section_info *abbrev_section;
6681 const gdb_byte *info_ptr, *end_ptr;
6683 abbrev_section = (dwo_file != NULL
6684 ? &dwo_file->sections.abbrev
6685 : &dwarf2_per_objfile->abbrev);
6687 if (dwarf_read_debug)
6688 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6689 get_section_name (section),
6690 get_section_file_name (abbrev_section));
6692 dwarf2_read_section (objfile, section);
6693 info_ptr = section->buffer;
6695 if (info_ptr == NULL)
6698 /* We can't set abfd until now because the section may be empty or
6699 not present, in which case the bfd is unknown. */
6700 abfd = get_section_bfd_owner (section);
6702 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6703 because we don't need to read any dies: the signature is in the
6706 end_ptr = info_ptr + section->size;
6707 while (info_ptr < end_ptr)
6709 struct signatured_type *sig_type;
6710 struct dwo_unit *dwo_tu;
6712 const gdb_byte *ptr = info_ptr;
6713 struct comp_unit_head header;
6714 unsigned int length;
6716 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6718 /* Initialize it due to a false compiler warning. */
6719 header.signature = -1;
6720 header.type_cu_offset_in_tu = (cu_offset) -1;
6722 /* We need to read the type's signature in order to build the hash
6723 table, but we don't need anything else just yet. */
6725 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6726 abbrev_section, ptr, section_kind);
6728 length = get_cu_length (&header);
6730 /* Skip dummy type units. */
6731 if (ptr >= info_ptr + length
6732 || peek_abbrev_code (abfd, ptr) == 0
6733 || header.unit_type != DW_UT_type)
6739 if (types_htab == NULL)
6742 types_htab = allocate_dwo_unit_table (objfile);
6744 types_htab = allocate_signatured_type_table (objfile);
6750 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6752 dwo_tu->dwo_file = dwo_file;
6753 dwo_tu->signature = header.signature;
6754 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6755 dwo_tu->section = section;
6756 dwo_tu->sect_off = sect_off;
6757 dwo_tu->length = length;
6761 /* N.B.: type_offset is not usable if this type uses a DWO file.
6762 The real type_offset is in the DWO file. */
6764 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6765 struct signatured_type);
6766 sig_type->signature = header.signature;
6767 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6768 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6769 sig_type->per_cu.is_debug_types = 1;
6770 sig_type->per_cu.section = section;
6771 sig_type->per_cu.sect_off = sect_off;
6772 sig_type->per_cu.length = length;
6775 slot = htab_find_slot (types_htab,
6776 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6778 gdb_assert (slot != NULL);
6781 sect_offset dup_sect_off;
6785 const struct dwo_unit *dup_tu
6786 = (const struct dwo_unit *) *slot;
6788 dup_sect_off = dup_tu->sect_off;
6792 const struct signatured_type *dup_tu
6793 = (const struct signatured_type *) *slot;
6795 dup_sect_off = dup_tu->per_cu.sect_off;
6798 complaint (_("debug type entry at offset %s is duplicate to"
6799 " the entry at offset %s, signature %s"),
6800 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6801 hex_string (header.signature));
6803 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6805 if (dwarf_read_debug > 1)
6806 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6807 sect_offset_str (sect_off),
6808 hex_string (header.signature));
6814 /* Create the hash table of all entries in the .debug_types
6815 (or .debug_types.dwo) section(s).
6816 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6817 otherwise it is NULL.
6819 The result is a pointer to the hash table or NULL if there are no types.
6821 Note: This function processes DWO files only, not DWP files. */
6824 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6825 struct dwo_file *dwo_file,
6826 gdb::array_view<dwarf2_section_info> type_sections,
6829 for (dwarf2_section_info §ion : type_sections)
6830 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, §ion,
6831 types_htab, rcuh_kind::TYPE);
6834 /* Create the hash table of all entries in the .debug_types section,
6835 and initialize all_type_units.
6836 The result is zero if there is an error (e.g. missing .debug_types section),
6837 otherwise non-zero. */
6840 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6842 htab_t types_htab = NULL;
6844 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6845 &dwarf2_per_objfile->info, types_htab,
6846 rcuh_kind::COMPILE);
6847 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6848 dwarf2_per_objfile->types, types_htab);
6849 if (types_htab == NULL)
6851 dwarf2_per_objfile->signatured_types = NULL;
6855 dwarf2_per_objfile->signatured_types = types_htab;
6857 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6858 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6860 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6861 &dwarf2_per_objfile->all_type_units);
6866 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6867 If SLOT is non-NULL, it is the entry to use in the hash table.
6868 Otherwise we find one. */
6870 static struct signatured_type *
6871 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6874 struct objfile *objfile = dwarf2_per_objfile->objfile;
6876 if (dwarf2_per_objfile->all_type_units.size ()
6877 == dwarf2_per_objfile->all_type_units.capacity ())
6878 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6880 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6881 struct signatured_type);
6883 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6884 sig_type->signature = sig;
6885 sig_type->per_cu.is_debug_types = 1;
6886 if (dwarf2_per_objfile->using_index)
6888 sig_type->per_cu.v.quick =
6889 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6890 struct dwarf2_per_cu_quick_data);
6895 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6898 gdb_assert (*slot == NULL);
6900 /* The rest of sig_type must be filled in by the caller. */
6904 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6905 Fill in SIG_ENTRY with DWO_ENTRY. */
6908 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6909 struct signatured_type *sig_entry,
6910 struct dwo_unit *dwo_entry)
6912 /* Make sure we're not clobbering something we don't expect to. */
6913 gdb_assert (! sig_entry->per_cu.queued);
6914 gdb_assert (sig_entry->per_cu.cu == NULL);
6915 if (dwarf2_per_objfile->using_index)
6917 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6918 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6921 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6922 gdb_assert (sig_entry->signature == dwo_entry->signature);
6923 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6924 gdb_assert (sig_entry->type_unit_group == NULL);
6925 gdb_assert (sig_entry->dwo_unit == NULL);
6927 sig_entry->per_cu.section = dwo_entry->section;
6928 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6929 sig_entry->per_cu.length = dwo_entry->length;
6930 sig_entry->per_cu.reading_dwo_directly = 1;
6931 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6932 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6933 sig_entry->dwo_unit = dwo_entry;
6936 /* Subroutine of lookup_signatured_type.
6937 If we haven't read the TU yet, create the signatured_type data structure
6938 for a TU to be read in directly from a DWO file, bypassing the stub.
6939 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6940 using .gdb_index, then when reading a CU we want to stay in the DWO file
6941 containing that CU. Otherwise we could end up reading several other DWO
6942 files (due to comdat folding) to process the transitive closure of all the
6943 mentioned TUs, and that can be slow. The current DWO file will have every
6944 type signature that it needs.
6945 We only do this for .gdb_index because in the psymtab case we already have
6946 to read all the DWOs to build the type unit groups. */
6948 static struct signatured_type *
6949 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6951 struct dwarf2_per_objfile *dwarf2_per_objfile
6952 = cu->per_cu->dwarf2_per_objfile;
6953 struct objfile *objfile = dwarf2_per_objfile->objfile;
6954 struct dwo_file *dwo_file;
6955 struct dwo_unit find_dwo_entry, *dwo_entry;
6956 struct signatured_type find_sig_entry, *sig_entry;
6959 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6961 /* If TU skeletons have been removed then we may not have read in any
6963 if (dwarf2_per_objfile->signatured_types == NULL)
6965 dwarf2_per_objfile->signatured_types
6966 = allocate_signatured_type_table (objfile);
6969 /* We only ever need to read in one copy of a signatured type.
6970 Use the global signatured_types array to do our own comdat-folding
6971 of types. If this is the first time we're reading this TU, and
6972 the TU has an entry in .gdb_index, replace the recorded data from
6973 .gdb_index with this TU. */
6975 find_sig_entry.signature = sig;
6976 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6977 &find_sig_entry, INSERT);
6978 sig_entry = (struct signatured_type *) *slot;
6980 /* We can get here with the TU already read, *or* in the process of being
6981 read. Don't reassign the global entry to point to this DWO if that's
6982 the case. Also note that if the TU is already being read, it may not
6983 have come from a DWO, the program may be a mix of Fission-compiled
6984 code and non-Fission-compiled code. */
6986 /* Have we already tried to read this TU?
6987 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6988 needn't exist in the global table yet). */
6989 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6992 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6993 dwo_unit of the TU itself. */
6994 dwo_file = cu->dwo_unit->dwo_file;
6996 /* Ok, this is the first time we're reading this TU. */
6997 if (dwo_file->tus == NULL)
6999 find_dwo_entry.signature = sig;
7000 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7001 if (dwo_entry == NULL)
7004 /* If the global table doesn't have an entry for this TU, add one. */
7005 if (sig_entry == NULL)
7006 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7008 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7009 sig_entry->per_cu.tu_read = 1;
7013 /* Subroutine of lookup_signatured_type.
7014 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7015 then try the DWP file. If the TU stub (skeleton) has been removed then
7016 it won't be in .gdb_index. */
7018 static struct signatured_type *
7019 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7021 struct dwarf2_per_objfile *dwarf2_per_objfile
7022 = cu->per_cu->dwarf2_per_objfile;
7023 struct objfile *objfile = dwarf2_per_objfile->objfile;
7024 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7025 struct dwo_unit *dwo_entry;
7026 struct signatured_type find_sig_entry, *sig_entry;
7029 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7030 gdb_assert (dwp_file != NULL);
7032 /* If TU skeletons have been removed then we may not have read in any
7034 if (dwarf2_per_objfile->signatured_types == NULL)
7036 dwarf2_per_objfile->signatured_types
7037 = allocate_signatured_type_table (objfile);
7040 find_sig_entry.signature = sig;
7041 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7042 &find_sig_entry, INSERT);
7043 sig_entry = (struct signatured_type *) *slot;
7045 /* Have we already tried to read this TU?
7046 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7047 needn't exist in the global table yet). */
7048 if (sig_entry != NULL)
7051 if (dwp_file->tus == NULL)
7053 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7054 sig, 1 /* is_debug_types */);
7055 if (dwo_entry == NULL)
7058 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7059 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7064 /* Lookup a signature based type for DW_FORM_ref_sig8.
7065 Returns NULL if signature SIG is not present in the table.
7066 It is up to the caller to complain about this. */
7068 static struct signatured_type *
7069 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7071 struct dwarf2_per_objfile *dwarf2_per_objfile
7072 = cu->per_cu->dwarf2_per_objfile;
7075 && dwarf2_per_objfile->using_index)
7077 /* We're in a DWO/DWP file, and we're using .gdb_index.
7078 These cases require special processing. */
7079 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7080 return lookup_dwo_signatured_type (cu, sig);
7082 return lookup_dwp_signatured_type (cu, sig);
7086 struct signatured_type find_entry, *entry;
7088 if (dwarf2_per_objfile->signatured_types == NULL)
7090 find_entry.signature = sig;
7091 entry = ((struct signatured_type *)
7092 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7097 /* Low level DIE reading support. */
7099 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7102 init_cu_die_reader (struct die_reader_specs *reader,
7103 struct dwarf2_cu *cu,
7104 struct dwarf2_section_info *section,
7105 struct dwo_file *dwo_file,
7106 struct abbrev_table *abbrev_table)
7108 gdb_assert (section->readin && section->buffer != NULL);
7109 reader->abfd = get_section_bfd_owner (section);
7111 reader->dwo_file = dwo_file;
7112 reader->die_section = section;
7113 reader->buffer = section->buffer;
7114 reader->buffer_end = section->buffer + section->size;
7115 reader->comp_dir = NULL;
7116 reader->abbrev_table = abbrev_table;
7119 /* Subroutine of init_cutu_and_read_dies to simplify it.
7120 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7121 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7124 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7125 from it to the DIE in the DWO. If NULL we are skipping the stub.
7126 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7127 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7128 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7129 STUB_COMP_DIR may be non-NULL.
7130 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7131 are filled in with the info of the DIE from the DWO file.
7132 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7133 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7134 kept around for at least as long as *RESULT_READER.
7136 The result is non-zero if a valid (non-dummy) DIE was found. */
7139 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7140 struct dwo_unit *dwo_unit,
7141 struct die_info *stub_comp_unit_die,
7142 const char *stub_comp_dir,
7143 struct die_reader_specs *result_reader,
7144 const gdb_byte **result_info_ptr,
7145 struct die_info **result_comp_unit_die,
7146 int *result_has_children,
7147 abbrev_table_up *result_dwo_abbrev_table)
7149 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7150 struct objfile *objfile = dwarf2_per_objfile->objfile;
7151 struct dwarf2_cu *cu = this_cu->cu;
7153 const gdb_byte *begin_info_ptr, *info_ptr;
7154 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7155 int i,num_extra_attrs;
7156 struct dwarf2_section_info *dwo_abbrev_section;
7157 struct attribute *attr;
7158 struct die_info *comp_unit_die;
7160 /* At most one of these may be provided. */
7161 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7163 /* These attributes aren't processed until later:
7164 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7165 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7166 referenced later. However, these attributes are found in the stub
7167 which we won't have later. In order to not impose this complication
7168 on the rest of the code, we read them here and copy them to the
7177 if (stub_comp_unit_die != NULL)
7179 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7181 if (! this_cu->is_debug_types)
7182 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7183 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7184 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7185 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7186 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7188 /* There should be a DW_AT_addr_base attribute here (if needed).
7189 We need the value before we can process DW_FORM_GNU_addr_index
7190 or DW_FORM_addrx. */
7192 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7194 cu->addr_base = DW_UNSND (attr);
7196 /* There should be a DW_AT_ranges_base attribute here (if needed).
7197 We need the value before we can process DW_AT_ranges. */
7198 cu->ranges_base = 0;
7199 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7201 cu->ranges_base = DW_UNSND (attr);
7203 else if (stub_comp_dir != NULL)
7205 /* Reconstruct the comp_dir attribute to simplify the code below. */
7206 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7207 comp_dir->name = DW_AT_comp_dir;
7208 comp_dir->form = DW_FORM_string;
7209 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7210 DW_STRING (comp_dir) = stub_comp_dir;
7213 /* Set up for reading the DWO CU/TU. */
7214 cu->dwo_unit = dwo_unit;
7215 dwarf2_section_info *section = dwo_unit->section;
7216 dwarf2_read_section (objfile, section);
7217 abfd = get_section_bfd_owner (section);
7218 begin_info_ptr = info_ptr = (section->buffer
7219 + to_underlying (dwo_unit->sect_off));
7220 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7222 if (this_cu->is_debug_types)
7224 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7226 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7227 &cu->header, section,
7229 info_ptr, rcuh_kind::TYPE);
7230 /* This is not an assert because it can be caused by bad debug info. */
7231 if (sig_type->signature != cu->header.signature)
7233 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7234 " TU at offset %s [in module %s]"),
7235 hex_string (sig_type->signature),
7236 hex_string (cu->header.signature),
7237 sect_offset_str (dwo_unit->sect_off),
7238 bfd_get_filename (abfd));
7240 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7241 /* For DWOs coming from DWP files, we don't know the CU length
7242 nor the type's offset in the TU until now. */
7243 dwo_unit->length = get_cu_length (&cu->header);
7244 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7246 /* Establish the type offset that can be used to lookup the type.
7247 For DWO files, we don't know it until now. */
7248 sig_type->type_offset_in_section
7249 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7253 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7254 &cu->header, section,
7256 info_ptr, rcuh_kind::COMPILE);
7257 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7258 /* For DWOs coming from DWP files, we don't know the CU length
7260 dwo_unit->length = get_cu_length (&cu->header);
7263 *result_dwo_abbrev_table
7264 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7265 cu->header.abbrev_sect_off);
7266 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7267 result_dwo_abbrev_table->get ());
7269 /* Read in the die, but leave space to copy over the attributes
7270 from the stub. This has the benefit of simplifying the rest of
7271 the code - all the work to maintain the illusion of a single
7272 DW_TAG_{compile,type}_unit DIE is done here. */
7273 num_extra_attrs = ((stmt_list != NULL)
7277 + (comp_dir != NULL));
7278 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7279 result_has_children, num_extra_attrs);
7281 /* Copy over the attributes from the stub to the DIE we just read in. */
7282 comp_unit_die = *result_comp_unit_die;
7283 i = comp_unit_die->num_attrs;
7284 if (stmt_list != NULL)
7285 comp_unit_die->attrs[i++] = *stmt_list;
7287 comp_unit_die->attrs[i++] = *low_pc;
7288 if (high_pc != NULL)
7289 comp_unit_die->attrs[i++] = *high_pc;
7291 comp_unit_die->attrs[i++] = *ranges;
7292 if (comp_dir != NULL)
7293 comp_unit_die->attrs[i++] = *comp_dir;
7294 comp_unit_die->num_attrs += num_extra_attrs;
7296 if (dwarf_die_debug)
7298 fprintf_unfiltered (gdb_stdlog,
7299 "Read die from %s@0x%x of %s:\n",
7300 get_section_name (section),
7301 (unsigned) (begin_info_ptr - section->buffer),
7302 bfd_get_filename (abfd));
7303 dump_die (comp_unit_die, dwarf_die_debug);
7306 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7307 TUs by skipping the stub and going directly to the entry in the DWO file.
7308 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7309 to get it via circuitous means. Blech. */
7310 if (comp_dir != NULL)
7311 result_reader->comp_dir = DW_STRING (comp_dir);
7313 /* Skip dummy compilation units. */
7314 if (info_ptr >= begin_info_ptr + dwo_unit->length
7315 || peek_abbrev_code (abfd, info_ptr) == 0)
7318 *result_info_ptr = info_ptr;
7322 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7323 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7324 signature is part of the header. */
7325 static gdb::optional<ULONGEST>
7326 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7328 if (cu->header.version >= 5)
7329 return cu->header.signature;
7330 struct attribute *attr;
7331 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7332 if (attr == nullptr)
7333 return gdb::optional<ULONGEST> ();
7334 return DW_UNSND (attr);
7337 /* Subroutine of init_cutu_and_read_dies to simplify it.
7338 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7339 Returns NULL if the specified DWO unit cannot be found. */
7341 static struct dwo_unit *
7342 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7343 struct die_info *comp_unit_die)
7345 struct dwarf2_cu *cu = this_cu->cu;
7346 struct dwo_unit *dwo_unit;
7347 const char *comp_dir, *dwo_name;
7349 gdb_assert (cu != NULL);
7351 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7352 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7353 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7355 if (this_cu->is_debug_types)
7357 struct signatured_type *sig_type;
7359 /* Since this_cu is the first member of struct signatured_type,
7360 we can go from a pointer to one to a pointer to the other. */
7361 sig_type = (struct signatured_type *) this_cu;
7362 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7366 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7367 if (!signature.has_value ())
7368 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7370 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7371 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7378 /* Subroutine of init_cutu_and_read_dies to simplify it.
7379 See it for a description of the parameters.
7380 Read a TU directly from a DWO file, bypassing the stub. */
7383 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7384 int use_existing_cu, int keep,
7385 die_reader_func_ftype *die_reader_func,
7388 std::unique_ptr<dwarf2_cu> new_cu;
7389 struct signatured_type *sig_type;
7390 struct die_reader_specs reader;
7391 const gdb_byte *info_ptr;
7392 struct die_info *comp_unit_die;
7394 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7396 /* Verify we can do the following downcast, and that we have the
7398 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7399 sig_type = (struct signatured_type *) this_cu;
7400 gdb_assert (sig_type->dwo_unit != NULL);
7402 if (use_existing_cu && this_cu->cu != NULL)
7404 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7405 /* There's no need to do the rereading_dwo_cu handling that
7406 init_cutu_and_read_dies does since we don't read the stub. */
7410 /* If !use_existing_cu, this_cu->cu must be NULL. */
7411 gdb_assert (this_cu->cu == NULL);
7412 new_cu.reset (new dwarf2_cu (this_cu));
7415 /* A future optimization, if needed, would be to use an existing
7416 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7417 could share abbrev tables. */
7419 /* The abbreviation table used by READER, this must live at least as long as
7421 abbrev_table_up dwo_abbrev_table;
7423 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7424 NULL /* stub_comp_unit_die */,
7425 sig_type->dwo_unit->dwo_file->comp_dir,
7427 &comp_unit_die, &has_children,
7428 &dwo_abbrev_table) == 0)
7434 /* All the "real" work is done here. */
7435 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7437 /* This duplicates the code in init_cutu_and_read_dies,
7438 but the alternative is making the latter more complex.
7439 This function is only for the special case of using DWO files directly:
7440 no point in overly complicating the general case just to handle this. */
7441 if (new_cu != NULL && keep)
7443 /* Link this CU into read_in_chain. */
7444 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7445 dwarf2_per_objfile->read_in_chain = this_cu;
7446 /* The chain owns it now. */
7451 /* Initialize a CU (or TU) and read its DIEs.
7452 If the CU defers to a DWO file, read the DWO file as well.
7454 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7455 Otherwise the table specified in the comp unit header is read in and used.
7456 This is an optimization for when we already have the abbrev table.
7458 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7459 Otherwise, a new CU is allocated with xmalloc.
7461 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7462 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7464 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7465 linker) then DIE_READER_FUNC will not get called. */
7468 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7469 struct abbrev_table *abbrev_table,
7470 int use_existing_cu, int keep,
7472 die_reader_func_ftype *die_reader_func,
7475 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7476 struct objfile *objfile = dwarf2_per_objfile->objfile;
7477 struct dwarf2_section_info *section = this_cu->section;
7478 bfd *abfd = get_section_bfd_owner (section);
7479 struct dwarf2_cu *cu;
7480 const gdb_byte *begin_info_ptr, *info_ptr;
7481 struct die_reader_specs reader;
7482 struct die_info *comp_unit_die;
7484 struct signatured_type *sig_type = NULL;
7485 struct dwarf2_section_info *abbrev_section;
7486 /* Non-zero if CU currently points to a DWO file and we need to
7487 reread it. When this happens we need to reread the skeleton die
7488 before we can reread the DWO file (this only applies to CUs, not TUs). */
7489 int rereading_dwo_cu = 0;
7491 if (dwarf_die_debug)
7492 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7493 this_cu->is_debug_types ? "type" : "comp",
7494 sect_offset_str (this_cu->sect_off));
7496 if (use_existing_cu)
7499 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7500 file (instead of going through the stub), short-circuit all of this. */
7501 if (this_cu->reading_dwo_directly)
7503 /* Narrow down the scope of possibilities to have to understand. */
7504 gdb_assert (this_cu->is_debug_types);
7505 gdb_assert (abbrev_table == NULL);
7506 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7507 die_reader_func, data);
7511 /* This is cheap if the section is already read in. */
7512 dwarf2_read_section (objfile, section);
7514 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7516 abbrev_section = get_abbrev_section_for_cu (this_cu);
7518 std::unique_ptr<dwarf2_cu> new_cu;
7519 if (use_existing_cu && this_cu->cu != NULL)
7522 /* If this CU is from a DWO file we need to start over, we need to
7523 refetch the attributes from the skeleton CU.
7524 This could be optimized by retrieving those attributes from when we
7525 were here the first time: the previous comp_unit_die was stored in
7526 comp_unit_obstack. But there's no data yet that we need this
7528 if (cu->dwo_unit != NULL)
7529 rereading_dwo_cu = 1;
7533 /* If !use_existing_cu, this_cu->cu must be NULL. */
7534 gdb_assert (this_cu->cu == NULL);
7535 new_cu.reset (new dwarf2_cu (this_cu));
7539 /* Get the header. */
7540 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7542 /* We already have the header, there's no need to read it in again. */
7543 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7547 if (this_cu->is_debug_types)
7549 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7550 &cu->header, section,
7551 abbrev_section, info_ptr,
7554 /* Since per_cu is the first member of struct signatured_type,
7555 we can go from a pointer to one to a pointer to the other. */
7556 sig_type = (struct signatured_type *) this_cu;
7557 gdb_assert (sig_type->signature == cu->header.signature);
7558 gdb_assert (sig_type->type_offset_in_tu
7559 == cu->header.type_cu_offset_in_tu);
7560 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7562 /* LENGTH has not been set yet for type units if we're
7563 using .gdb_index. */
7564 this_cu->length = get_cu_length (&cu->header);
7566 /* Establish the type offset that can be used to lookup the type. */
7567 sig_type->type_offset_in_section =
7568 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7570 this_cu->dwarf_version = cu->header.version;
7574 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7575 &cu->header, section,
7578 rcuh_kind::COMPILE);
7580 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7581 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7582 this_cu->dwarf_version = cu->header.version;
7586 /* Skip dummy compilation units. */
7587 if (info_ptr >= begin_info_ptr + this_cu->length
7588 || peek_abbrev_code (abfd, info_ptr) == 0)
7591 /* If we don't have them yet, read the abbrevs for this compilation unit.
7592 And if we need to read them now, make sure they're freed when we're
7593 done (own the table through ABBREV_TABLE_HOLDER). */
7594 abbrev_table_up abbrev_table_holder;
7595 if (abbrev_table != NULL)
7596 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7600 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7601 cu->header.abbrev_sect_off);
7602 abbrev_table = abbrev_table_holder.get ();
7605 /* Read the top level CU/TU die. */
7606 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7607 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7609 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7612 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7613 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7614 table from the DWO file and pass the ownership over to us. It will be
7615 referenced from READER, so we must make sure to free it after we're done
7618 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7619 DWO CU, that this test will fail (the attribute will not be present). */
7620 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7621 abbrev_table_up dwo_abbrev_table;
7622 if (dwo_name != nullptr)
7624 struct dwo_unit *dwo_unit;
7625 struct die_info *dwo_comp_unit_die;
7629 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7630 " has children (offset %s) [in module %s]"),
7631 sect_offset_str (this_cu->sect_off),
7632 bfd_get_filename (abfd));
7634 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7635 if (dwo_unit != NULL)
7637 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7638 comp_unit_die, NULL,
7640 &dwo_comp_unit_die, &has_children,
7641 &dwo_abbrev_table) == 0)
7646 comp_unit_die = dwo_comp_unit_die;
7650 /* Yikes, we couldn't find the rest of the DIE, we only have
7651 the stub. A complaint has already been logged. There's
7652 not much more we can do except pass on the stub DIE to
7653 die_reader_func. We don't want to throw an error on bad
7658 /* All of the above is setup for this call. Yikes. */
7659 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7661 /* Done, clean up. */
7662 if (new_cu != NULL && keep)
7664 /* Link this CU into read_in_chain. */
7665 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7666 dwarf2_per_objfile->read_in_chain = this_cu;
7667 /* The chain owns it now. */
7672 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7673 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7674 to have already done the lookup to find the DWO file).
7676 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7677 THIS_CU->is_debug_types, but nothing else.
7679 We fill in THIS_CU->length.
7681 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7682 linker) then DIE_READER_FUNC will not get called.
7684 THIS_CU->cu is always freed when done.
7685 This is done in order to not leave THIS_CU->cu in a state where we have
7686 to care whether it refers to the "main" CU or the DWO CU. */
7689 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7690 struct dwo_file *dwo_file,
7691 die_reader_func_ftype *die_reader_func,
7694 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7695 struct objfile *objfile = dwarf2_per_objfile->objfile;
7696 struct dwarf2_section_info *section = this_cu->section;
7697 bfd *abfd = get_section_bfd_owner (section);
7698 struct dwarf2_section_info *abbrev_section;
7699 const gdb_byte *begin_info_ptr, *info_ptr;
7700 struct die_reader_specs reader;
7701 struct die_info *comp_unit_die;
7704 if (dwarf_die_debug)
7705 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7706 this_cu->is_debug_types ? "type" : "comp",
7707 sect_offset_str (this_cu->sect_off));
7709 gdb_assert (this_cu->cu == NULL);
7711 abbrev_section = (dwo_file != NULL
7712 ? &dwo_file->sections.abbrev
7713 : get_abbrev_section_for_cu (this_cu));
7715 /* This is cheap if the section is already read in. */
7716 dwarf2_read_section (objfile, section);
7718 struct dwarf2_cu cu (this_cu);
7720 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7721 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7722 &cu.header, section,
7723 abbrev_section, info_ptr,
7724 (this_cu->is_debug_types
7726 : rcuh_kind::COMPILE));
7728 this_cu->length = get_cu_length (&cu.header);
7730 /* Skip dummy compilation units. */
7731 if (info_ptr >= begin_info_ptr + this_cu->length
7732 || peek_abbrev_code (abfd, info_ptr) == 0)
7735 abbrev_table_up abbrev_table
7736 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7737 cu.header.abbrev_sect_off);
7739 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7740 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7742 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7745 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7746 does not lookup the specified DWO file.
7747 This cannot be used to read DWO files.
7749 THIS_CU->cu is always freed when done.
7750 This is done in order to not leave THIS_CU->cu in a state where we have
7751 to care whether it refers to the "main" CU or the DWO CU.
7752 We can revisit this if the data shows there's a performance issue. */
7755 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7756 die_reader_func_ftype *die_reader_func,
7759 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7762 /* Type Unit Groups.
7764 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7765 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7766 so that all types coming from the same compilation (.o file) are grouped
7767 together. A future step could be to put the types in the same symtab as
7768 the CU the types ultimately came from. */
7771 hash_type_unit_group (const void *item)
7773 const struct type_unit_group *tu_group
7774 = (const struct type_unit_group *) item;
7776 return hash_stmt_list_entry (&tu_group->hash);
7780 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7782 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7783 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7785 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7788 /* Allocate a hash table for type unit groups. */
7791 allocate_type_unit_groups_table (struct objfile *objfile)
7793 return htab_create_alloc_ex (3,
7794 hash_type_unit_group,
7797 &objfile->objfile_obstack,
7798 hashtab_obstack_allocate,
7799 dummy_obstack_deallocate);
7802 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7803 partial symtabs. We combine several TUs per psymtab to not let the size
7804 of any one psymtab grow too big. */
7805 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7806 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7808 /* Helper routine for get_type_unit_group.
7809 Create the type_unit_group object used to hold one or more TUs. */
7811 static struct type_unit_group *
7812 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7814 struct dwarf2_per_objfile *dwarf2_per_objfile
7815 = cu->per_cu->dwarf2_per_objfile;
7816 struct objfile *objfile = dwarf2_per_objfile->objfile;
7817 struct dwarf2_per_cu_data *per_cu;
7818 struct type_unit_group *tu_group;
7820 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7821 struct type_unit_group);
7822 per_cu = &tu_group->per_cu;
7823 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7825 if (dwarf2_per_objfile->using_index)
7827 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7828 struct dwarf2_per_cu_quick_data);
7832 unsigned int line_offset = to_underlying (line_offset_struct);
7833 struct partial_symtab *pst;
7836 /* Give the symtab a useful name for debug purposes. */
7837 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7838 name = string_printf ("<type_units_%d>",
7839 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7841 name = string_printf ("<type_units_at_0x%x>", line_offset);
7843 pst = create_partial_symtab (per_cu, name.c_str ());
7847 tu_group->hash.dwo_unit = cu->dwo_unit;
7848 tu_group->hash.line_sect_off = line_offset_struct;
7853 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7854 STMT_LIST is a DW_AT_stmt_list attribute. */
7856 static struct type_unit_group *
7857 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7859 struct dwarf2_per_objfile *dwarf2_per_objfile
7860 = cu->per_cu->dwarf2_per_objfile;
7861 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7862 struct type_unit_group *tu_group;
7864 unsigned int line_offset;
7865 struct type_unit_group type_unit_group_for_lookup;
7867 if (dwarf2_per_objfile->type_unit_groups == NULL)
7869 dwarf2_per_objfile->type_unit_groups =
7870 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7873 /* Do we need to create a new group, or can we use an existing one? */
7877 line_offset = DW_UNSND (stmt_list);
7878 ++tu_stats->nr_symtab_sharers;
7882 /* Ugh, no stmt_list. Rare, but we have to handle it.
7883 We can do various things here like create one group per TU or
7884 spread them over multiple groups to split up the expansion work.
7885 To avoid worst case scenarios (too many groups or too large groups)
7886 we, umm, group them in bunches. */
7887 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7888 | (tu_stats->nr_stmt_less_type_units
7889 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7890 ++tu_stats->nr_stmt_less_type_units;
7893 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7894 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7895 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7896 &type_unit_group_for_lookup, INSERT);
7899 tu_group = (struct type_unit_group *) *slot;
7900 gdb_assert (tu_group != NULL);
7904 sect_offset line_offset_struct = (sect_offset) line_offset;
7905 tu_group = create_type_unit_group (cu, line_offset_struct);
7907 ++tu_stats->nr_symtabs;
7913 /* Partial symbol tables. */
7915 /* Create a psymtab named NAME and assign it to PER_CU.
7917 The caller must fill in the following details:
7918 dirname, textlow, texthigh. */
7920 static struct partial_symtab *
7921 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7923 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7924 struct partial_symtab *pst;
7926 pst = start_psymtab_common (objfile, name, 0);
7928 pst->psymtabs_addrmap_supported = 1;
7930 /* This is the glue that links PST into GDB's symbol API. */
7931 pst->read_symtab_private = per_cu;
7932 pst->read_symtab = dwarf2_read_symtab;
7933 per_cu->v.psymtab = pst;
7938 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7941 struct process_psymtab_comp_unit_data
7943 /* True if we are reading a DW_TAG_partial_unit. */
7945 int want_partial_unit;
7947 /* The "pretend" language that is used if the CU doesn't declare a
7950 enum language pretend_language;
7953 /* die_reader_func for process_psymtab_comp_unit. */
7956 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7957 const gdb_byte *info_ptr,
7958 struct die_info *comp_unit_die,
7962 struct dwarf2_cu *cu = reader->cu;
7963 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7964 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7965 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7967 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7968 struct partial_symtab *pst;
7969 enum pc_bounds_kind cu_bounds_kind;
7970 const char *filename;
7971 struct process_psymtab_comp_unit_data *info
7972 = (struct process_psymtab_comp_unit_data *) data;
7974 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7977 gdb_assert (! per_cu->is_debug_types);
7979 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7981 /* Allocate a new partial symbol table structure. */
7982 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7983 if (filename == NULL)
7986 pst = create_partial_symtab (per_cu, filename);
7988 /* This must be done before calling dwarf2_build_include_psymtabs. */
7989 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7991 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7993 dwarf2_find_base_address (comp_unit_die, cu);
7995 /* Possibly set the default values of LOWPC and HIGHPC from
7997 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7998 &best_highpc, cu, pst);
7999 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8002 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8005 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8007 /* Store the contiguous range if it is not empty; it can be
8008 empty for CUs with no code. */
8009 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8013 /* Check if comp unit has_children.
8014 If so, read the rest of the partial symbols from this comp unit.
8015 If not, there's no more debug_info for this comp unit. */
8018 struct partial_die_info *first_die;
8019 CORE_ADDR lowpc, highpc;
8021 lowpc = ((CORE_ADDR) -1);
8022 highpc = ((CORE_ADDR) 0);
8024 first_die = load_partial_dies (reader, info_ptr, 1);
8026 scan_partial_symbols (first_die, &lowpc, &highpc,
8027 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8029 /* If we didn't find a lowpc, set it to highpc to avoid
8030 complaints from `maint check'. */
8031 if (lowpc == ((CORE_ADDR) -1))
8034 /* If the compilation unit didn't have an explicit address range,
8035 then use the information extracted from its child dies. */
8036 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8039 best_highpc = highpc;
8042 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8043 best_lowpc + baseaddr)
8045 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8046 best_highpc + baseaddr)
8049 end_psymtab_common (objfile, pst);
8051 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8054 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8055 struct dwarf2_per_cu_data *iter;
8057 /* Fill in 'dependencies' here; we fill in 'users' in a
8059 pst->number_of_dependencies = len;
8061 = objfile->partial_symtabs->allocate_dependencies (len);
8063 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8066 pst->dependencies[i] = iter->v.psymtab;
8068 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8071 /* Get the list of files included in the current compilation unit,
8072 and build a psymtab for each of them. */
8073 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8075 if (dwarf_read_debug)
8076 fprintf_unfiltered (gdb_stdlog,
8077 "Psymtab for %s unit @%s: %s - %s"
8078 ", %d global, %d static syms\n",
8079 per_cu->is_debug_types ? "type" : "comp",
8080 sect_offset_str (per_cu->sect_off),
8081 paddress (gdbarch, pst->text_low (objfile)),
8082 paddress (gdbarch, pst->text_high (objfile)),
8083 pst->n_global_syms, pst->n_static_syms);
8086 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8087 Process compilation unit THIS_CU for a psymtab. */
8090 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8091 int want_partial_unit,
8092 enum language pretend_language)
8094 /* If this compilation unit was already read in, free the
8095 cached copy in order to read it in again. This is
8096 necessary because we skipped some symbols when we first
8097 read in the compilation unit (see load_partial_dies).
8098 This problem could be avoided, but the benefit is unclear. */
8099 if (this_cu->cu != NULL)
8100 free_one_cached_comp_unit (this_cu);
8102 if (this_cu->is_debug_types)
8103 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8104 build_type_psymtabs_reader, NULL);
8107 process_psymtab_comp_unit_data info;
8108 info.want_partial_unit = want_partial_unit;
8109 info.pretend_language = pretend_language;
8110 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8111 process_psymtab_comp_unit_reader, &info);
8114 /* Age out any secondary CUs. */
8115 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8118 /* Reader function for build_type_psymtabs. */
8121 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8122 const gdb_byte *info_ptr,
8123 struct die_info *type_unit_die,
8127 struct dwarf2_per_objfile *dwarf2_per_objfile
8128 = reader->cu->per_cu->dwarf2_per_objfile;
8129 struct objfile *objfile = dwarf2_per_objfile->objfile;
8130 struct dwarf2_cu *cu = reader->cu;
8131 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8132 struct signatured_type *sig_type;
8133 struct type_unit_group *tu_group;
8134 struct attribute *attr;
8135 struct partial_die_info *first_die;
8136 CORE_ADDR lowpc, highpc;
8137 struct partial_symtab *pst;
8139 gdb_assert (data == NULL);
8140 gdb_assert (per_cu->is_debug_types);
8141 sig_type = (struct signatured_type *) per_cu;
8146 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8147 tu_group = get_type_unit_group (cu, attr);
8149 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8151 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8152 pst = create_partial_symtab (per_cu, "");
8155 first_die = load_partial_dies (reader, info_ptr, 1);
8157 lowpc = (CORE_ADDR) -1;
8158 highpc = (CORE_ADDR) 0;
8159 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8161 end_psymtab_common (objfile, pst);
8164 /* Struct used to sort TUs by their abbreviation table offset. */
8166 struct tu_abbrev_offset
8168 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8169 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8172 signatured_type *sig_type;
8173 sect_offset abbrev_offset;
8176 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8179 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8180 const struct tu_abbrev_offset &b)
8182 return a.abbrev_offset < b.abbrev_offset;
8185 /* Efficiently read all the type units.
8186 This does the bulk of the work for build_type_psymtabs.
8188 The efficiency is because we sort TUs by the abbrev table they use and
8189 only read each abbrev table once. In one program there are 200K TUs
8190 sharing 8K abbrev tables.
8192 The main purpose of this function is to support building the
8193 dwarf2_per_objfile->type_unit_groups table.
8194 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8195 can collapse the search space by grouping them by stmt_list.
8196 The savings can be significant, in the same program from above the 200K TUs
8197 share 8K stmt_list tables.
8199 FUNC is expected to call get_type_unit_group, which will create the
8200 struct type_unit_group if necessary and add it to
8201 dwarf2_per_objfile->type_unit_groups. */
8204 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8206 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8207 abbrev_table_up abbrev_table;
8208 sect_offset abbrev_offset;
8210 /* It's up to the caller to not call us multiple times. */
8211 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8213 if (dwarf2_per_objfile->all_type_units.empty ())
8216 /* TUs typically share abbrev tables, and there can be way more TUs than
8217 abbrev tables. Sort by abbrev table to reduce the number of times we
8218 read each abbrev table in.
8219 Alternatives are to punt or to maintain a cache of abbrev tables.
8220 This is simpler and efficient enough for now.
8222 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8223 symtab to use). Typically TUs with the same abbrev offset have the same
8224 stmt_list value too so in practice this should work well.
8226 The basic algorithm here is:
8228 sort TUs by abbrev table
8229 for each TU with same abbrev table:
8230 read abbrev table if first user
8231 read TU top level DIE
8232 [IWBN if DWO skeletons had DW_AT_stmt_list]
8235 if (dwarf_read_debug)
8236 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8238 /* Sort in a separate table to maintain the order of all_type_units
8239 for .gdb_index: TU indices directly index all_type_units. */
8240 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8241 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8243 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8244 sorted_by_abbrev.emplace_back
8245 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8246 sig_type->per_cu.section,
8247 sig_type->per_cu.sect_off));
8249 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8250 sort_tu_by_abbrev_offset);
8252 abbrev_offset = (sect_offset) ~(unsigned) 0;
8254 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8256 /* Switch to the next abbrev table if necessary. */
8257 if (abbrev_table == NULL
8258 || tu.abbrev_offset != abbrev_offset)
8260 abbrev_offset = tu.abbrev_offset;
8262 abbrev_table_read_table (dwarf2_per_objfile,
8263 &dwarf2_per_objfile->abbrev,
8265 ++tu_stats->nr_uniq_abbrev_tables;
8268 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8269 0, 0, false, build_type_psymtabs_reader, NULL);
8273 /* Print collected type unit statistics. */
8276 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8278 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8280 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8281 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8282 dwarf2_per_objfile->all_type_units.size ());
8283 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8284 tu_stats->nr_uniq_abbrev_tables);
8285 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8286 tu_stats->nr_symtabs);
8287 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8288 tu_stats->nr_symtab_sharers);
8289 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8290 tu_stats->nr_stmt_less_type_units);
8291 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8292 tu_stats->nr_all_type_units_reallocs);
8295 /* Traversal function for build_type_psymtabs. */
8298 build_type_psymtab_dependencies (void **slot, void *info)
8300 struct dwarf2_per_objfile *dwarf2_per_objfile
8301 = (struct dwarf2_per_objfile *) info;
8302 struct objfile *objfile = dwarf2_per_objfile->objfile;
8303 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8304 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8305 struct partial_symtab *pst = per_cu->v.psymtab;
8306 int len = VEC_length (sig_type_ptr, tu_group->tus);
8307 struct signatured_type *iter;
8310 gdb_assert (len > 0);
8311 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8313 pst->number_of_dependencies = len;
8314 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8316 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8319 gdb_assert (iter->per_cu.is_debug_types);
8320 pst->dependencies[i] = iter->per_cu.v.psymtab;
8321 iter->type_unit_group = tu_group;
8324 VEC_free (sig_type_ptr, tu_group->tus);
8329 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8330 Build partial symbol tables for the .debug_types comp-units. */
8333 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8335 if (! create_all_type_units (dwarf2_per_objfile))
8338 build_type_psymtabs_1 (dwarf2_per_objfile);
8341 /* Traversal function for process_skeletonless_type_unit.
8342 Read a TU in a DWO file and build partial symbols for it. */
8345 process_skeletonless_type_unit (void **slot, void *info)
8347 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8348 struct dwarf2_per_objfile *dwarf2_per_objfile
8349 = (struct dwarf2_per_objfile *) info;
8350 struct signatured_type find_entry, *entry;
8352 /* If this TU doesn't exist in the global table, add it and read it in. */
8354 if (dwarf2_per_objfile->signatured_types == NULL)
8356 dwarf2_per_objfile->signatured_types
8357 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8360 find_entry.signature = dwo_unit->signature;
8361 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8363 /* If we've already seen this type there's nothing to do. What's happening
8364 is we're doing our own version of comdat-folding here. */
8368 /* This does the job that create_all_type_units would have done for
8370 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8371 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8374 /* This does the job that build_type_psymtabs_1 would have done. */
8375 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8376 build_type_psymtabs_reader, NULL);
8381 /* Traversal function for process_skeletonless_type_units. */
8384 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8386 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8388 if (dwo_file->tus != NULL)
8390 htab_traverse_noresize (dwo_file->tus,
8391 process_skeletonless_type_unit, info);
8397 /* Scan all TUs of DWO files, verifying we've processed them.
8398 This is needed in case a TU was emitted without its skeleton.
8399 Note: This can't be done until we know what all the DWO files are. */
8402 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8404 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8405 if (get_dwp_file (dwarf2_per_objfile) == NULL
8406 && dwarf2_per_objfile->dwo_files != NULL)
8408 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8409 process_dwo_file_for_skeletonless_type_units,
8410 dwarf2_per_objfile);
8414 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8417 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8419 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8421 struct partial_symtab *pst = per_cu->v.psymtab;
8426 for (int j = 0; j < pst->number_of_dependencies; ++j)
8428 /* Set the 'user' field only if it is not already set. */
8429 if (pst->dependencies[j]->user == NULL)
8430 pst->dependencies[j]->user = pst;
8435 /* Build the partial symbol table by doing a quick pass through the
8436 .debug_info and .debug_abbrev sections. */
8439 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8441 struct objfile *objfile = dwarf2_per_objfile->objfile;
8443 if (dwarf_read_debug)
8445 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8446 objfile_name (objfile));
8449 dwarf2_per_objfile->reading_partial_symbols = 1;
8451 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8453 /* Any cached compilation units will be linked by the per-objfile
8454 read_in_chain. Make sure to free them when we're done. */
8455 free_cached_comp_units freer (dwarf2_per_objfile);
8457 build_type_psymtabs (dwarf2_per_objfile);
8459 create_all_comp_units (dwarf2_per_objfile);
8461 /* Create a temporary address map on a temporary obstack. We later
8462 copy this to the final obstack. */
8463 auto_obstack temp_obstack;
8465 scoped_restore save_psymtabs_addrmap
8466 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8467 addrmap_create_mutable (&temp_obstack));
8469 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8470 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8472 /* This has to wait until we read the CUs, we need the list of DWOs. */
8473 process_skeletonless_type_units (dwarf2_per_objfile);
8475 /* Now that all TUs have been processed we can fill in the dependencies. */
8476 if (dwarf2_per_objfile->type_unit_groups != NULL)
8478 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8479 build_type_psymtab_dependencies, dwarf2_per_objfile);
8482 if (dwarf_read_debug)
8483 print_tu_stats (dwarf2_per_objfile);
8485 set_partial_user (dwarf2_per_objfile);
8487 objfile->partial_symtabs->psymtabs_addrmap
8488 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8489 objfile->partial_symtabs->obstack ());
8490 /* At this point we want to keep the address map. */
8491 save_psymtabs_addrmap.release ();
8493 if (dwarf_read_debug)
8494 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8495 objfile_name (objfile));
8498 /* die_reader_func for load_partial_comp_unit. */
8501 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8502 const gdb_byte *info_ptr,
8503 struct die_info *comp_unit_die,
8507 struct dwarf2_cu *cu = reader->cu;
8509 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8511 /* Check if comp unit has_children.
8512 If so, read the rest of the partial symbols from this comp unit.
8513 If not, there's no more debug_info for this comp unit. */
8515 load_partial_dies (reader, info_ptr, 0);
8518 /* Load the partial DIEs for a secondary CU into memory.
8519 This is also used when rereading a primary CU with load_all_dies. */
8522 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8524 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8525 load_partial_comp_unit_reader, NULL);
8529 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8530 struct dwarf2_section_info *section,
8531 struct dwarf2_section_info *abbrev_section,
8532 unsigned int is_dwz)
8534 const gdb_byte *info_ptr;
8535 struct objfile *objfile = dwarf2_per_objfile->objfile;
8537 if (dwarf_read_debug)
8538 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8539 get_section_name (section),
8540 get_section_file_name (section));
8542 dwarf2_read_section (objfile, section);
8544 info_ptr = section->buffer;
8546 while (info_ptr < section->buffer + section->size)
8548 struct dwarf2_per_cu_data *this_cu;
8550 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8552 comp_unit_head cu_header;
8553 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8554 abbrev_section, info_ptr,
8555 rcuh_kind::COMPILE);
8557 /* Save the compilation unit for later lookup. */
8558 if (cu_header.unit_type != DW_UT_type)
8560 this_cu = XOBNEW (&objfile->objfile_obstack,
8561 struct dwarf2_per_cu_data);
8562 memset (this_cu, 0, sizeof (*this_cu));
8566 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8567 struct signatured_type);
8568 memset (sig_type, 0, sizeof (*sig_type));
8569 sig_type->signature = cu_header.signature;
8570 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8571 this_cu = &sig_type->per_cu;
8573 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8574 this_cu->sect_off = sect_off;
8575 this_cu->length = cu_header.length + cu_header.initial_length_size;
8576 this_cu->is_dwz = is_dwz;
8577 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8578 this_cu->section = section;
8580 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8582 info_ptr = info_ptr + this_cu->length;
8586 /* Create a list of all compilation units in OBJFILE.
8587 This is only done for -readnow and building partial symtabs. */
8590 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8592 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8593 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8594 &dwarf2_per_objfile->abbrev, 0);
8596 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8598 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8602 /* Process all loaded DIEs for compilation unit CU, starting at
8603 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8604 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8605 DW_AT_ranges). See the comments of add_partial_subprogram on how
8606 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8609 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8610 CORE_ADDR *highpc, int set_addrmap,
8611 struct dwarf2_cu *cu)
8613 struct partial_die_info *pdi;
8615 /* Now, march along the PDI's, descending into ones which have
8616 interesting children but skipping the children of the other ones,
8617 until we reach the end of the compilation unit. */
8625 /* Anonymous namespaces or modules have no name but have interesting
8626 children, so we need to look at them. Ditto for anonymous
8629 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8630 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8631 || pdi->tag == DW_TAG_imported_unit
8632 || pdi->tag == DW_TAG_inlined_subroutine)
8636 case DW_TAG_subprogram:
8637 case DW_TAG_inlined_subroutine:
8638 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8640 case DW_TAG_constant:
8641 case DW_TAG_variable:
8642 case DW_TAG_typedef:
8643 case DW_TAG_union_type:
8644 if (!pdi->is_declaration)
8646 add_partial_symbol (pdi, cu);
8649 case DW_TAG_class_type:
8650 case DW_TAG_interface_type:
8651 case DW_TAG_structure_type:
8652 if (!pdi->is_declaration)
8654 add_partial_symbol (pdi, cu);
8656 if ((cu->language == language_rust
8657 || cu->language == language_cplus) && pdi->has_children)
8658 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8661 case DW_TAG_enumeration_type:
8662 if (!pdi->is_declaration)
8663 add_partial_enumeration (pdi, cu);
8665 case DW_TAG_base_type:
8666 case DW_TAG_subrange_type:
8667 /* File scope base type definitions are added to the partial
8669 add_partial_symbol (pdi, cu);
8671 case DW_TAG_namespace:
8672 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8675 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8677 case DW_TAG_imported_unit:
8679 struct dwarf2_per_cu_data *per_cu;
8681 /* For now we don't handle imported units in type units. */
8682 if (cu->per_cu->is_debug_types)
8684 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8685 " supported in type units [in module %s]"),
8686 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8689 per_cu = dwarf2_find_containing_comp_unit
8690 (pdi->d.sect_off, pdi->is_dwz,
8691 cu->per_cu->dwarf2_per_objfile);
8693 /* Go read the partial unit, if needed. */
8694 if (per_cu->v.psymtab == NULL)
8695 process_psymtab_comp_unit (per_cu, 1, cu->language);
8697 VEC_safe_push (dwarf2_per_cu_ptr,
8698 cu->per_cu->imported_symtabs, per_cu);
8701 case DW_TAG_imported_declaration:
8702 add_partial_symbol (pdi, cu);
8709 /* If the die has a sibling, skip to the sibling. */
8711 pdi = pdi->die_sibling;
8715 /* Functions used to compute the fully scoped name of a partial DIE.
8717 Normally, this is simple. For C++, the parent DIE's fully scoped
8718 name is concatenated with "::" and the partial DIE's name.
8719 Enumerators are an exception; they use the scope of their parent
8720 enumeration type, i.e. the name of the enumeration type is not
8721 prepended to the enumerator.
8723 There are two complexities. One is DW_AT_specification; in this
8724 case "parent" means the parent of the target of the specification,
8725 instead of the direct parent of the DIE. The other is compilers
8726 which do not emit DW_TAG_namespace; in this case we try to guess
8727 the fully qualified name of structure types from their members'
8728 linkage names. This must be done using the DIE's children rather
8729 than the children of any DW_AT_specification target. We only need
8730 to do this for structures at the top level, i.e. if the target of
8731 any DW_AT_specification (if any; otherwise the DIE itself) does not
8734 /* Compute the scope prefix associated with PDI's parent, in
8735 compilation unit CU. The result will be allocated on CU's
8736 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8737 field. NULL is returned if no prefix is necessary. */
8739 partial_die_parent_scope (struct partial_die_info *pdi,
8740 struct dwarf2_cu *cu)
8742 const char *grandparent_scope;
8743 struct partial_die_info *parent, *real_pdi;
8745 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8746 then this means the parent of the specification DIE. */
8749 while (real_pdi->has_specification)
8751 auto res = find_partial_die (real_pdi->spec_offset,
8752 real_pdi->spec_is_dwz, cu);
8757 parent = real_pdi->die_parent;
8761 if (parent->scope_set)
8762 return parent->scope;
8766 grandparent_scope = partial_die_parent_scope (parent, cu);
8768 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8769 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8770 Work around this problem here. */
8771 if (cu->language == language_cplus
8772 && parent->tag == DW_TAG_namespace
8773 && strcmp (parent->name, "::") == 0
8774 && grandparent_scope == NULL)
8776 parent->scope = NULL;
8777 parent->scope_set = 1;
8781 if (pdi->tag == DW_TAG_enumerator)
8782 /* Enumerators should not get the name of the enumeration as a prefix. */
8783 parent->scope = grandparent_scope;
8784 else if (parent->tag == DW_TAG_namespace
8785 || parent->tag == DW_TAG_module
8786 || parent->tag == DW_TAG_structure_type
8787 || parent->tag == DW_TAG_class_type
8788 || parent->tag == DW_TAG_interface_type
8789 || parent->tag == DW_TAG_union_type
8790 || parent->tag == DW_TAG_enumeration_type)
8792 if (grandparent_scope == NULL)
8793 parent->scope = parent->name;
8795 parent->scope = typename_concat (&cu->comp_unit_obstack,
8797 parent->name, 0, cu);
8801 /* FIXME drow/2004-04-01: What should we be doing with
8802 function-local names? For partial symbols, we should probably be
8804 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8805 dwarf_tag_name (parent->tag),
8806 sect_offset_str (pdi->sect_off));
8807 parent->scope = grandparent_scope;
8810 parent->scope_set = 1;
8811 return parent->scope;
8814 /* Return the fully scoped name associated with PDI, from compilation unit
8815 CU. The result will be allocated with malloc. */
8818 partial_die_full_name (struct partial_die_info *pdi,
8819 struct dwarf2_cu *cu)
8821 const char *parent_scope;
8823 /* If this is a template instantiation, we can not work out the
8824 template arguments from partial DIEs. So, unfortunately, we have
8825 to go through the full DIEs. At least any work we do building
8826 types here will be reused if full symbols are loaded later. */
8827 if (pdi->has_template_arguments)
8831 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8833 struct die_info *die;
8834 struct attribute attr;
8835 struct dwarf2_cu *ref_cu = cu;
8837 /* DW_FORM_ref_addr is using section offset. */
8838 attr.name = (enum dwarf_attribute) 0;
8839 attr.form = DW_FORM_ref_addr;
8840 attr.u.unsnd = to_underlying (pdi->sect_off);
8841 die = follow_die_ref (NULL, &attr, &ref_cu);
8843 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8847 parent_scope = partial_die_parent_scope (pdi, cu);
8848 if (parent_scope == NULL)
8851 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8855 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8857 struct dwarf2_per_objfile *dwarf2_per_objfile
8858 = cu->per_cu->dwarf2_per_objfile;
8859 struct objfile *objfile = dwarf2_per_objfile->objfile;
8860 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8862 const char *actual_name = NULL;
8864 char *built_actual_name;
8866 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8868 built_actual_name = partial_die_full_name (pdi, cu);
8869 if (built_actual_name != NULL)
8870 actual_name = built_actual_name;
8872 if (actual_name == NULL)
8873 actual_name = pdi->name;
8877 case DW_TAG_inlined_subroutine:
8878 case DW_TAG_subprogram:
8879 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8881 if (pdi->is_external || cu->language == language_ada)
8883 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8884 of the global scope. But in Ada, we want to be able to access
8885 nested procedures globally. So all Ada subprograms are stored
8886 in the global scope. */
8887 add_psymbol_to_list (actual_name, strlen (actual_name),
8888 built_actual_name != NULL,
8889 VAR_DOMAIN, LOC_BLOCK,
8890 SECT_OFF_TEXT (objfile),
8891 psymbol_placement::GLOBAL,
8893 cu->language, objfile);
8897 add_psymbol_to_list (actual_name, strlen (actual_name),
8898 built_actual_name != NULL,
8899 VAR_DOMAIN, LOC_BLOCK,
8900 SECT_OFF_TEXT (objfile),
8901 psymbol_placement::STATIC,
8902 addr, cu->language, objfile);
8905 if (pdi->main_subprogram && actual_name != NULL)
8906 set_objfile_main_name (objfile, actual_name, cu->language);
8908 case DW_TAG_constant:
8909 add_psymbol_to_list (actual_name, strlen (actual_name),
8910 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8911 -1, (pdi->is_external
8912 ? psymbol_placement::GLOBAL
8913 : psymbol_placement::STATIC),
8914 0, cu->language, objfile);
8916 case DW_TAG_variable:
8918 addr = decode_locdesc (pdi->d.locdesc, cu);
8922 && !dwarf2_per_objfile->has_section_at_zero)
8924 /* A global or static variable may also have been stripped
8925 out by the linker if unused, in which case its address
8926 will be nullified; do not add such variables into partial
8927 symbol table then. */
8929 else if (pdi->is_external)
8932 Don't enter into the minimal symbol tables as there is
8933 a minimal symbol table entry from the ELF symbols already.
8934 Enter into partial symbol table if it has a location
8935 descriptor or a type.
8936 If the location descriptor is missing, new_symbol will create
8937 a LOC_UNRESOLVED symbol, the address of the variable will then
8938 be determined from the minimal symbol table whenever the variable
8940 The address for the partial symbol table entry is not
8941 used by GDB, but it comes in handy for debugging partial symbol
8944 if (pdi->d.locdesc || pdi->has_type)
8945 add_psymbol_to_list (actual_name, strlen (actual_name),
8946 built_actual_name != NULL,
8947 VAR_DOMAIN, LOC_STATIC,
8948 SECT_OFF_TEXT (objfile),
8949 psymbol_placement::GLOBAL,
8950 addr, cu->language, objfile);
8954 int has_loc = pdi->d.locdesc != NULL;
8956 /* Static Variable. Skip symbols whose value we cannot know (those
8957 without location descriptors or constant values). */
8958 if (!has_loc && !pdi->has_const_value)
8960 xfree (built_actual_name);
8964 add_psymbol_to_list (actual_name, strlen (actual_name),
8965 built_actual_name != NULL,
8966 VAR_DOMAIN, LOC_STATIC,
8967 SECT_OFF_TEXT (objfile),
8968 psymbol_placement::STATIC,
8970 cu->language, objfile);
8973 case DW_TAG_typedef:
8974 case DW_TAG_base_type:
8975 case DW_TAG_subrange_type:
8976 add_psymbol_to_list (actual_name, strlen (actual_name),
8977 built_actual_name != NULL,
8978 VAR_DOMAIN, LOC_TYPEDEF, -1,
8979 psymbol_placement::STATIC,
8980 0, cu->language, objfile);
8982 case DW_TAG_imported_declaration:
8983 case DW_TAG_namespace:
8984 add_psymbol_to_list (actual_name, strlen (actual_name),
8985 built_actual_name != NULL,
8986 VAR_DOMAIN, LOC_TYPEDEF, -1,
8987 psymbol_placement::GLOBAL,
8988 0, cu->language, objfile);
8991 /* With Fortran 77 there might be a "BLOCK DATA" module
8992 available without any name. If so, we skip the module as it
8993 doesn't bring any value. */
8994 if (actual_name != nullptr)
8995 add_psymbol_to_list (actual_name, strlen (actual_name),
8996 built_actual_name != NULL,
8997 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8998 psymbol_placement::GLOBAL,
8999 0, cu->language, objfile);
9001 case DW_TAG_class_type:
9002 case DW_TAG_interface_type:
9003 case DW_TAG_structure_type:
9004 case DW_TAG_union_type:
9005 case DW_TAG_enumeration_type:
9006 /* Skip external references. The DWARF standard says in the section
9007 about "Structure, Union, and Class Type Entries": "An incomplete
9008 structure, union or class type is represented by a structure,
9009 union or class entry that does not have a byte size attribute
9010 and that has a DW_AT_declaration attribute." */
9011 if (!pdi->has_byte_size && pdi->is_declaration)
9013 xfree (built_actual_name);
9017 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9018 static vs. global. */
9019 add_psymbol_to_list (actual_name, strlen (actual_name),
9020 built_actual_name != NULL,
9021 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9022 cu->language == language_cplus
9023 ? psymbol_placement::GLOBAL
9024 : psymbol_placement::STATIC,
9025 0, cu->language, objfile);
9028 case DW_TAG_enumerator:
9029 add_psymbol_to_list (actual_name, strlen (actual_name),
9030 built_actual_name != NULL,
9031 VAR_DOMAIN, LOC_CONST, -1,
9032 cu->language == language_cplus
9033 ? psymbol_placement::GLOBAL
9034 : psymbol_placement::STATIC,
9035 0, cu->language, objfile);
9041 xfree (built_actual_name);
9044 /* Read a partial die corresponding to a namespace; also, add a symbol
9045 corresponding to that namespace to the symbol table. NAMESPACE is
9046 the name of the enclosing namespace. */
9049 add_partial_namespace (struct partial_die_info *pdi,
9050 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9051 int set_addrmap, struct dwarf2_cu *cu)
9053 /* Add a symbol for the namespace. */
9055 add_partial_symbol (pdi, cu);
9057 /* Now scan partial symbols in that namespace. */
9059 if (pdi->has_children)
9060 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9063 /* Read a partial die corresponding to a Fortran module. */
9066 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9067 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9069 /* Add a symbol for the namespace. */
9071 add_partial_symbol (pdi, cu);
9073 /* Now scan partial symbols in that module. */
9075 if (pdi->has_children)
9076 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9079 /* Read a partial die corresponding to a subprogram or an inlined
9080 subprogram and create a partial symbol for that subprogram.
9081 When the CU language allows it, this routine also defines a partial
9082 symbol for each nested subprogram that this subprogram contains.
9083 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9084 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9086 PDI may also be a lexical block, in which case we simply search
9087 recursively for subprograms defined inside that lexical block.
9088 Again, this is only performed when the CU language allows this
9089 type of definitions. */
9092 add_partial_subprogram (struct partial_die_info *pdi,
9093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9094 int set_addrmap, struct dwarf2_cu *cu)
9096 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9098 if (pdi->has_pc_info)
9100 if (pdi->lowpc < *lowpc)
9101 *lowpc = pdi->lowpc;
9102 if (pdi->highpc > *highpc)
9103 *highpc = pdi->highpc;
9106 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9109 CORE_ADDR this_highpc;
9110 CORE_ADDR this_lowpc;
9112 baseaddr = ANOFFSET (objfile->section_offsets,
9113 SECT_OFF_TEXT (objfile));
9115 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9116 pdi->lowpc + baseaddr)
9119 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9120 pdi->highpc + baseaddr)
9122 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9123 this_lowpc, this_highpc - 1,
9124 cu->per_cu->v.psymtab);
9128 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9130 if (!pdi->is_declaration)
9131 /* Ignore subprogram DIEs that do not have a name, they are
9132 illegal. Do not emit a complaint at this point, we will
9133 do so when we convert this psymtab into a symtab. */
9135 add_partial_symbol (pdi, cu);
9139 if (! pdi->has_children)
9142 if (cu->language == language_ada)
9144 pdi = pdi->die_child;
9148 if (pdi->tag == DW_TAG_subprogram
9149 || pdi->tag == DW_TAG_inlined_subroutine
9150 || pdi->tag == DW_TAG_lexical_block)
9151 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9152 pdi = pdi->die_sibling;
9157 /* Read a partial die corresponding to an enumeration type. */
9160 add_partial_enumeration (struct partial_die_info *enum_pdi,
9161 struct dwarf2_cu *cu)
9163 struct partial_die_info *pdi;
9165 if (enum_pdi->name != NULL)
9166 add_partial_symbol (enum_pdi, cu);
9168 pdi = enum_pdi->die_child;
9171 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9172 complaint (_("malformed enumerator DIE ignored"));
9174 add_partial_symbol (pdi, cu);
9175 pdi = pdi->die_sibling;
9179 /* Return the initial uleb128 in the die at INFO_PTR. */
9182 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9184 unsigned int bytes_read;
9186 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9189 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9190 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9192 Return the corresponding abbrev, or NULL if the number is zero (indicating
9193 an empty DIE). In either case *BYTES_READ will be set to the length of
9194 the initial number. */
9196 static struct abbrev_info *
9197 peek_die_abbrev (const die_reader_specs &reader,
9198 const gdb_byte *info_ptr, unsigned int *bytes_read)
9200 dwarf2_cu *cu = reader.cu;
9201 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9202 unsigned int abbrev_number
9203 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9205 if (abbrev_number == 0)
9208 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9211 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9212 " at offset %s [in module %s]"),
9213 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9214 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9220 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9221 Returns a pointer to the end of a series of DIEs, terminated by an empty
9222 DIE. Any children of the skipped DIEs will also be skipped. */
9224 static const gdb_byte *
9225 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9229 unsigned int bytes_read;
9230 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9233 return info_ptr + bytes_read;
9235 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9239 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9240 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9241 abbrev corresponding to that skipped uleb128 should be passed in
9242 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9245 static const gdb_byte *
9246 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9247 struct abbrev_info *abbrev)
9249 unsigned int bytes_read;
9250 struct attribute attr;
9251 bfd *abfd = reader->abfd;
9252 struct dwarf2_cu *cu = reader->cu;
9253 const gdb_byte *buffer = reader->buffer;
9254 const gdb_byte *buffer_end = reader->buffer_end;
9255 unsigned int form, i;
9257 for (i = 0; i < abbrev->num_attrs; i++)
9259 /* The only abbrev we care about is DW_AT_sibling. */
9260 if (abbrev->attrs[i].name == DW_AT_sibling)
9262 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9263 if (attr.form == DW_FORM_ref_addr)
9264 complaint (_("ignoring absolute DW_AT_sibling"));
9267 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9268 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9270 if (sibling_ptr < info_ptr)
9271 complaint (_("DW_AT_sibling points backwards"));
9272 else if (sibling_ptr > reader->buffer_end)
9273 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9279 /* If it isn't DW_AT_sibling, skip this attribute. */
9280 form = abbrev->attrs[i].form;
9284 case DW_FORM_ref_addr:
9285 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9286 and later it is offset sized. */
9287 if (cu->header.version == 2)
9288 info_ptr += cu->header.addr_size;
9290 info_ptr += cu->header.offset_size;
9292 case DW_FORM_GNU_ref_alt:
9293 info_ptr += cu->header.offset_size;
9296 info_ptr += cu->header.addr_size;
9303 case DW_FORM_flag_present:
9304 case DW_FORM_implicit_const:
9316 case DW_FORM_ref_sig8:
9319 case DW_FORM_data16:
9322 case DW_FORM_string:
9323 read_direct_string (abfd, info_ptr, &bytes_read);
9324 info_ptr += bytes_read;
9326 case DW_FORM_sec_offset:
9328 case DW_FORM_GNU_strp_alt:
9329 info_ptr += cu->header.offset_size;
9331 case DW_FORM_exprloc:
9333 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9334 info_ptr += bytes_read;
9336 case DW_FORM_block1:
9337 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9339 case DW_FORM_block2:
9340 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9342 case DW_FORM_block4:
9343 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9349 case DW_FORM_ref_udata:
9350 case DW_FORM_GNU_addr_index:
9351 case DW_FORM_GNU_str_index:
9352 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9354 case DW_FORM_indirect:
9355 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9356 info_ptr += bytes_read;
9357 /* We need to continue parsing from here, so just go back to
9359 goto skip_attribute;
9362 error (_("Dwarf Error: Cannot handle %s "
9363 "in DWARF reader [in module %s]"),
9364 dwarf_form_name (form),
9365 bfd_get_filename (abfd));
9369 if (abbrev->has_children)
9370 return skip_children (reader, info_ptr);
9375 /* Locate ORIG_PDI's sibling.
9376 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9378 static const gdb_byte *
9379 locate_pdi_sibling (const struct die_reader_specs *reader,
9380 struct partial_die_info *orig_pdi,
9381 const gdb_byte *info_ptr)
9383 /* Do we know the sibling already? */
9385 if (orig_pdi->sibling)
9386 return orig_pdi->sibling;
9388 /* Are there any children to deal with? */
9390 if (!orig_pdi->has_children)
9393 /* Skip the children the long way. */
9395 return skip_children (reader, info_ptr);
9398 /* Expand this partial symbol table into a full symbol table. SELF is
9402 dwarf2_read_symtab (struct partial_symtab *self,
9403 struct objfile *objfile)
9405 struct dwarf2_per_objfile *dwarf2_per_objfile
9406 = get_dwarf2_per_objfile (objfile);
9410 warning (_("bug: psymtab for %s is already read in."),
9417 printf_filtered (_("Reading in symbols for %s..."),
9419 gdb_flush (gdb_stdout);
9422 /* If this psymtab is constructed from a debug-only objfile, the
9423 has_section_at_zero flag will not necessarily be correct. We
9424 can get the correct value for this flag by looking at the data
9425 associated with the (presumably stripped) associated objfile. */
9426 if (objfile->separate_debug_objfile_backlink)
9428 struct dwarf2_per_objfile *dpo_backlink
9429 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9431 dwarf2_per_objfile->has_section_at_zero
9432 = dpo_backlink->has_section_at_zero;
9435 dwarf2_per_objfile->reading_partial_symbols = 0;
9437 psymtab_to_symtab_1 (self);
9439 /* Finish up the debug error message. */
9441 printf_filtered (_("done.\n"));
9444 process_cu_includes (dwarf2_per_objfile);
9447 /* Reading in full CUs. */
9449 /* Add PER_CU to the queue. */
9452 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9453 enum language pretend_language)
9455 struct dwarf2_queue_item *item;
9458 item = XNEW (struct dwarf2_queue_item);
9459 item->per_cu = per_cu;
9460 item->pretend_language = pretend_language;
9463 if (dwarf2_queue == NULL)
9464 dwarf2_queue = item;
9466 dwarf2_queue_tail->next = item;
9468 dwarf2_queue_tail = item;
9471 /* If PER_CU is not yet queued, add it to the queue.
9472 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9474 The result is non-zero if PER_CU was queued, otherwise the result is zero
9475 meaning either PER_CU is already queued or it is already loaded.
9477 N.B. There is an invariant here that if a CU is queued then it is loaded.
9478 The caller is required to load PER_CU if we return non-zero. */
9481 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9482 struct dwarf2_per_cu_data *per_cu,
9483 enum language pretend_language)
9485 /* We may arrive here during partial symbol reading, if we need full
9486 DIEs to process an unusual case (e.g. template arguments). Do
9487 not queue PER_CU, just tell our caller to load its DIEs. */
9488 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9490 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9495 /* Mark the dependence relation so that we don't flush PER_CU
9497 if (dependent_cu != NULL)
9498 dwarf2_add_dependence (dependent_cu, per_cu);
9500 /* If it's already on the queue, we have nothing to do. */
9504 /* If the compilation unit is already loaded, just mark it as
9506 if (per_cu->cu != NULL)
9508 per_cu->cu->last_used = 0;
9512 /* Add it to the queue. */
9513 queue_comp_unit (per_cu, pretend_language);
9518 /* Process the queue. */
9521 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9523 struct dwarf2_queue_item *item, *next_item;
9525 if (dwarf_read_debug)
9527 fprintf_unfiltered (gdb_stdlog,
9528 "Expanding one or more symtabs of objfile %s ...\n",
9529 objfile_name (dwarf2_per_objfile->objfile));
9532 /* The queue starts out with one item, but following a DIE reference
9533 may load a new CU, adding it to the end of the queue. */
9534 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9536 if ((dwarf2_per_objfile->using_index
9537 ? !item->per_cu->v.quick->compunit_symtab
9538 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9539 /* Skip dummy CUs. */
9540 && item->per_cu->cu != NULL)
9542 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9543 unsigned int debug_print_threshold;
9546 if (per_cu->is_debug_types)
9548 struct signatured_type *sig_type =
9549 (struct signatured_type *) per_cu;
9551 sprintf (buf, "TU %s at offset %s",
9552 hex_string (sig_type->signature),
9553 sect_offset_str (per_cu->sect_off));
9554 /* There can be 100s of TUs.
9555 Only print them in verbose mode. */
9556 debug_print_threshold = 2;
9560 sprintf (buf, "CU at offset %s",
9561 sect_offset_str (per_cu->sect_off));
9562 debug_print_threshold = 1;
9565 if (dwarf_read_debug >= debug_print_threshold)
9566 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9568 if (per_cu->is_debug_types)
9569 process_full_type_unit (per_cu, item->pretend_language);
9571 process_full_comp_unit (per_cu, item->pretend_language);
9573 if (dwarf_read_debug >= debug_print_threshold)
9574 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9577 item->per_cu->queued = 0;
9578 next_item = item->next;
9582 dwarf2_queue_tail = NULL;
9584 if (dwarf_read_debug)
9586 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9587 objfile_name (dwarf2_per_objfile->objfile));
9591 /* Read in full symbols for PST, and anything it depends on. */
9594 psymtab_to_symtab_1 (struct partial_symtab *pst)
9596 struct dwarf2_per_cu_data *per_cu;
9602 for (i = 0; i < pst->number_of_dependencies; i++)
9603 if (!pst->dependencies[i]->readin
9604 && pst->dependencies[i]->user == NULL)
9606 /* Inform about additional files that need to be read in. */
9609 /* FIXME: i18n: Need to make this a single string. */
9610 fputs_filtered (" ", gdb_stdout);
9612 fputs_filtered ("and ", gdb_stdout);
9614 printf_filtered ("%s...", pst->dependencies[i]->filename);
9615 wrap_here (""); /* Flush output. */
9616 gdb_flush (gdb_stdout);
9618 psymtab_to_symtab_1 (pst->dependencies[i]);
9621 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9625 /* It's an include file, no symbols to read for it.
9626 Everything is in the parent symtab. */
9631 dw2_do_instantiate_symtab (per_cu, false);
9634 /* Trivial hash function for die_info: the hash value of a DIE
9635 is its offset in .debug_info for this objfile. */
9638 die_hash (const void *item)
9640 const struct die_info *die = (const struct die_info *) item;
9642 return to_underlying (die->sect_off);
9645 /* Trivial comparison function for die_info structures: two DIEs
9646 are equal if they have the same offset. */
9649 die_eq (const void *item_lhs, const void *item_rhs)
9651 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9652 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9654 return die_lhs->sect_off == die_rhs->sect_off;
9657 /* die_reader_func for load_full_comp_unit.
9658 This is identical to read_signatured_type_reader,
9659 but is kept separate for now. */
9662 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9663 const gdb_byte *info_ptr,
9664 struct die_info *comp_unit_die,
9668 struct dwarf2_cu *cu = reader->cu;
9669 enum language *language_ptr = (enum language *) data;
9671 gdb_assert (cu->die_hash == NULL);
9673 htab_create_alloc_ex (cu->header.length / 12,
9677 &cu->comp_unit_obstack,
9678 hashtab_obstack_allocate,
9679 dummy_obstack_deallocate);
9682 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9683 &info_ptr, comp_unit_die);
9684 cu->dies = comp_unit_die;
9685 /* comp_unit_die is not stored in die_hash, no need. */
9687 /* We try not to read any attributes in this function, because not
9688 all CUs needed for references have been loaded yet, and symbol
9689 table processing isn't initialized. But we have to set the CU language,
9690 or we won't be able to build types correctly.
9691 Similarly, if we do not read the producer, we can not apply
9692 producer-specific interpretation. */
9693 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9696 /* Load the DIEs associated with PER_CU into memory. */
9699 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9701 enum language pretend_language)
9703 gdb_assert (! this_cu->is_debug_types);
9705 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9706 load_full_comp_unit_reader, &pretend_language);
9709 /* Add a DIE to the delayed physname list. */
9712 add_to_method_list (struct type *type, int fnfield_index, int index,
9713 const char *name, struct die_info *die,
9714 struct dwarf2_cu *cu)
9716 struct delayed_method_info mi;
9718 mi.fnfield_index = fnfield_index;
9722 cu->method_list.push_back (mi);
9725 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9726 "const" / "volatile". If so, decrements LEN by the length of the
9727 modifier and return true. Otherwise return false. */
9731 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9733 size_t mod_len = sizeof (mod) - 1;
9734 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9742 /* Compute the physnames of any methods on the CU's method list.
9744 The computation of method physnames is delayed in order to avoid the
9745 (bad) condition that one of the method's formal parameters is of an as yet
9749 compute_delayed_physnames (struct dwarf2_cu *cu)
9751 /* Only C++ delays computing physnames. */
9752 if (cu->method_list.empty ())
9754 gdb_assert (cu->language == language_cplus);
9756 for (const delayed_method_info &mi : cu->method_list)
9758 const char *physname;
9759 struct fn_fieldlist *fn_flp
9760 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9761 physname = dwarf2_physname (mi.name, mi.die, cu);
9762 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9763 = physname ? physname : "";
9765 /* Since there's no tag to indicate whether a method is a
9766 const/volatile overload, extract that information out of the
9768 if (physname != NULL)
9770 size_t len = strlen (physname);
9774 if (physname[len] == ')') /* shortcut */
9776 else if (check_modifier (physname, len, " const"))
9777 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9778 else if (check_modifier (physname, len, " volatile"))
9779 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9786 /* The list is no longer needed. */
9787 cu->method_list.clear ();
9790 /* Go objects should be embedded in a DW_TAG_module DIE,
9791 and it's not clear if/how imported objects will appear.
9792 To keep Go support simple until that's worked out,
9793 go back through what we've read and create something usable.
9794 We could do this while processing each DIE, and feels kinda cleaner,
9795 but that way is more invasive.
9796 This is to, for example, allow the user to type "p var" or "b main"
9797 without having to specify the package name, and allow lookups
9798 of module.object to work in contexts that use the expression
9802 fixup_go_packaging (struct dwarf2_cu *cu)
9804 char *package_name = NULL;
9805 struct pending *list;
9808 for (list = *cu->get_builder ()->get_global_symbols ();
9812 for (i = 0; i < list->nsyms; ++i)
9814 struct symbol *sym = list->symbol[i];
9816 if (SYMBOL_LANGUAGE (sym) == language_go
9817 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9819 char *this_package_name = go_symbol_package_name (sym);
9821 if (this_package_name == NULL)
9823 if (package_name == NULL)
9824 package_name = this_package_name;
9827 struct objfile *objfile
9828 = cu->per_cu->dwarf2_per_objfile->objfile;
9829 if (strcmp (package_name, this_package_name) != 0)
9830 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9831 (symbol_symtab (sym) != NULL
9832 ? symtab_to_filename_for_display
9833 (symbol_symtab (sym))
9834 : objfile_name (objfile)),
9835 this_package_name, package_name);
9836 xfree (this_package_name);
9842 if (package_name != NULL)
9844 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9845 const char *saved_package_name
9846 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9847 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9848 saved_package_name);
9851 sym = allocate_symbol (objfile);
9852 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9853 SYMBOL_SET_NAMES (sym, saved_package_name,
9854 strlen (saved_package_name), 0, objfile);
9855 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9856 e.g., "main" finds the "main" module and not C's main(). */
9857 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9858 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9859 SYMBOL_TYPE (sym) = type;
9861 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9863 xfree (package_name);
9867 /* Allocate a fully-qualified name consisting of the two parts on the
9871 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9873 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9876 /* A helper that allocates a struct discriminant_info to attach to a
9879 static struct discriminant_info *
9880 alloc_discriminant_info (struct type *type, int discriminant_index,
9883 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9884 gdb_assert (discriminant_index == -1
9885 || (discriminant_index >= 0
9886 && discriminant_index < TYPE_NFIELDS (type)));
9887 gdb_assert (default_index == -1
9888 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9890 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9892 struct discriminant_info *disc
9893 = ((struct discriminant_info *)
9895 offsetof (struct discriminant_info, discriminants)
9896 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9897 disc->default_index = default_index;
9898 disc->discriminant_index = discriminant_index;
9900 struct dynamic_prop prop;
9901 prop.kind = PROP_UNDEFINED;
9902 prop.data.baton = disc;
9904 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9909 /* Some versions of rustc emitted enums in an unusual way.
9911 Ordinary enums were emitted as unions. The first element of each
9912 structure in the union was named "RUST$ENUM$DISR". This element
9913 held the discriminant.
9915 These versions of Rust also implemented the "non-zero"
9916 optimization. When the enum had two values, and one is empty and
9917 the other holds a pointer that cannot be zero, the pointer is used
9918 as the discriminant, with a zero value meaning the empty variant.
9919 Here, the union's first member is of the form
9920 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9921 where the fieldnos are the indices of the fields that should be
9922 traversed in order to find the field (which may be several fields deep)
9923 and the variantname is the name of the variant of the case when the
9926 This function recognizes whether TYPE is of one of these forms,
9927 and, if so, smashes it to be a variant type. */
9930 quirk_rust_enum (struct type *type, struct objfile *objfile)
9932 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9934 /* We don't need to deal with empty enums. */
9935 if (TYPE_NFIELDS (type) == 0)
9938 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9939 if (TYPE_NFIELDS (type) == 1
9940 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9942 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9944 /* Decode the field name to find the offset of the
9946 ULONGEST bit_offset = 0;
9947 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9948 while (name[0] >= '0' && name[0] <= '9')
9951 unsigned long index = strtoul (name, &tail, 10);
9954 || index >= TYPE_NFIELDS (field_type)
9955 || (TYPE_FIELD_LOC_KIND (field_type, index)
9956 != FIELD_LOC_KIND_BITPOS))
9958 complaint (_("Could not parse Rust enum encoding string \"%s\""
9960 TYPE_FIELD_NAME (type, 0),
9961 objfile_name (objfile));
9966 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9967 field_type = TYPE_FIELD_TYPE (field_type, index);
9970 /* Make a union to hold the variants. */
9971 struct type *union_type = alloc_type (objfile);
9972 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9973 TYPE_NFIELDS (union_type) = 3;
9974 TYPE_FIELDS (union_type)
9975 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9976 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9977 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9979 /* Put the discriminant must at index 0. */
9980 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9981 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9982 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9983 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9985 /* The order of fields doesn't really matter, so put the real
9986 field at index 1 and the data-less field at index 2. */
9987 struct discriminant_info *disc
9988 = alloc_discriminant_info (union_type, 0, 1);
9989 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9990 TYPE_FIELD_NAME (union_type, 1)
9991 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9992 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9993 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9994 TYPE_FIELD_NAME (union_type, 1));
9996 const char *dataless_name
9997 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9999 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10001 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10002 /* NAME points into the original discriminant name, which
10003 already has the correct lifetime. */
10004 TYPE_FIELD_NAME (union_type, 2) = name;
10005 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10006 disc->discriminants[2] = 0;
10008 /* Smash this type to be a structure type. We have to do this
10009 because the type has already been recorded. */
10010 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10011 TYPE_NFIELDS (type) = 1;
10013 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10015 /* Install the variant part. */
10016 TYPE_FIELD_TYPE (type, 0) = union_type;
10017 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10018 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10020 else if (TYPE_NFIELDS (type) == 1)
10022 /* We assume that a union with a single field is a univariant
10024 /* Smash this type to be a structure type. We have to do this
10025 because the type has already been recorded. */
10026 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10028 /* Make a union to hold the variants. */
10029 struct type *union_type = alloc_type (objfile);
10030 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10031 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10032 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10033 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10034 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10036 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10037 const char *variant_name
10038 = rust_last_path_segment (TYPE_NAME (field_type));
10039 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10040 TYPE_NAME (field_type)
10041 = rust_fully_qualify (&objfile->objfile_obstack,
10042 TYPE_NAME (type), variant_name);
10044 /* Install the union in the outer struct type. */
10045 TYPE_NFIELDS (type) = 1;
10047 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10048 TYPE_FIELD_TYPE (type, 0) = union_type;
10049 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10050 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10052 alloc_discriminant_info (union_type, -1, 0);
10056 struct type *disr_type = nullptr;
10057 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10059 disr_type = TYPE_FIELD_TYPE (type, i);
10061 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10063 /* All fields of a true enum will be structs. */
10066 else if (TYPE_NFIELDS (disr_type) == 0)
10068 /* Could be data-less variant, so keep going. */
10069 disr_type = nullptr;
10071 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10072 "RUST$ENUM$DISR") != 0)
10074 /* Not a Rust enum. */
10084 /* If we got here without a discriminant, then it's probably
10086 if (disr_type == nullptr)
10089 /* Smash this type to be a structure type. We have to do this
10090 because the type has already been recorded. */
10091 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10093 /* Make a union to hold the variants. */
10094 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10095 struct type *union_type = alloc_type (objfile);
10096 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10097 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10098 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10099 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10100 TYPE_FIELDS (union_type)
10101 = (struct field *) TYPE_ZALLOC (union_type,
10102 (TYPE_NFIELDS (union_type)
10103 * sizeof (struct field)));
10105 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10106 TYPE_NFIELDS (type) * sizeof (struct field));
10108 /* Install the discriminant at index 0 in the union. */
10109 TYPE_FIELD (union_type, 0) = *disr_field;
10110 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10111 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10113 /* Install the union in the outer struct type. */
10114 TYPE_FIELD_TYPE (type, 0) = union_type;
10115 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10116 TYPE_NFIELDS (type) = 1;
10118 /* Set the size and offset of the union type. */
10119 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10121 /* We need a way to find the correct discriminant given a
10122 variant name. For convenience we build a map here. */
10123 struct type *enum_type = FIELD_TYPE (*disr_field);
10124 std::unordered_map<std::string, ULONGEST> discriminant_map;
10125 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10127 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10130 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10131 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10135 int n_fields = TYPE_NFIELDS (union_type);
10136 struct discriminant_info *disc
10137 = alloc_discriminant_info (union_type, 0, -1);
10138 /* Skip the discriminant here. */
10139 for (int i = 1; i < n_fields; ++i)
10141 /* Find the final word in the name of this variant's type.
10142 That name can be used to look up the correct
10144 const char *variant_name
10145 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10148 auto iter = discriminant_map.find (variant_name);
10149 if (iter != discriminant_map.end ())
10150 disc->discriminants[i] = iter->second;
10152 /* Remove the discriminant field, if it exists. */
10153 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10154 if (TYPE_NFIELDS (sub_type) > 0)
10156 --TYPE_NFIELDS (sub_type);
10157 ++TYPE_FIELDS (sub_type);
10159 TYPE_FIELD_NAME (union_type, i) = variant_name;
10160 TYPE_NAME (sub_type)
10161 = rust_fully_qualify (&objfile->objfile_obstack,
10162 TYPE_NAME (type), variant_name);
10167 /* Rewrite some Rust unions to be structures with variants parts. */
10170 rust_union_quirks (struct dwarf2_cu *cu)
10172 gdb_assert (cu->language == language_rust);
10173 for (type *type_ : cu->rust_unions)
10174 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10175 /* We don't need this any more. */
10176 cu->rust_unions.clear ();
10179 /* Return the symtab for PER_CU. This works properly regardless of
10180 whether we're using the index or psymtabs. */
10182 static struct compunit_symtab *
10183 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10185 return (per_cu->dwarf2_per_objfile->using_index
10186 ? per_cu->v.quick->compunit_symtab
10187 : per_cu->v.psymtab->compunit_symtab);
10190 /* A helper function for computing the list of all symbol tables
10191 included by PER_CU. */
10194 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10195 htab_t all_children, htab_t all_type_symtabs,
10196 struct dwarf2_per_cu_data *per_cu,
10197 struct compunit_symtab *immediate_parent)
10201 struct compunit_symtab *cust;
10202 struct dwarf2_per_cu_data *iter;
10204 slot = htab_find_slot (all_children, per_cu, INSERT);
10207 /* This inclusion and its children have been processed. */
10212 /* Only add a CU if it has a symbol table. */
10213 cust = get_compunit_symtab (per_cu);
10216 /* If this is a type unit only add its symbol table if we haven't
10217 seen it yet (type unit per_cu's can share symtabs). */
10218 if (per_cu->is_debug_types)
10220 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10224 result->push_back (cust);
10225 if (cust->user == NULL)
10226 cust->user = immediate_parent;
10231 result->push_back (cust);
10232 if (cust->user == NULL)
10233 cust->user = immediate_parent;
10238 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10241 recursively_compute_inclusions (result, all_children,
10242 all_type_symtabs, iter, cust);
10246 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10250 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10252 gdb_assert (! per_cu->is_debug_types);
10254 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10257 struct dwarf2_per_cu_data *per_cu_iter;
10258 std::vector<compunit_symtab *> result_symtabs;
10259 htab_t all_children, all_type_symtabs;
10260 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10262 /* If we don't have a symtab, we can just skip this case. */
10266 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10267 NULL, xcalloc, xfree);
10268 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10269 NULL, xcalloc, xfree);
10272 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10276 recursively_compute_inclusions (&result_symtabs, all_children,
10277 all_type_symtabs, per_cu_iter,
10281 /* Now we have a transitive closure of all the included symtabs. */
10282 len = result_symtabs.size ();
10284 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10285 struct compunit_symtab *, len + 1);
10286 memcpy (cust->includes, result_symtabs.data (),
10287 len * sizeof (compunit_symtab *));
10288 cust->includes[len] = NULL;
10290 htab_delete (all_children);
10291 htab_delete (all_type_symtabs);
10295 /* Compute the 'includes' field for the symtabs of all the CUs we just
10299 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10301 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10303 if (! iter->is_debug_types)
10304 compute_compunit_symtab_includes (iter);
10307 dwarf2_per_objfile->just_read_cus.clear ();
10310 /* Generate full symbol information for PER_CU, whose DIEs have
10311 already been loaded into memory. */
10314 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10315 enum language pretend_language)
10317 struct dwarf2_cu *cu = per_cu->cu;
10318 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10319 struct objfile *objfile = dwarf2_per_objfile->objfile;
10320 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10321 CORE_ADDR lowpc, highpc;
10322 struct compunit_symtab *cust;
10323 CORE_ADDR baseaddr;
10324 struct block *static_block;
10327 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10329 /* Clear the list here in case something was left over. */
10330 cu->method_list.clear ();
10332 cu->language = pretend_language;
10333 cu->language_defn = language_def (cu->language);
10335 /* Do line number decoding in read_file_scope () */
10336 process_die (cu->dies, cu);
10338 /* For now fudge the Go package. */
10339 if (cu->language == language_go)
10340 fixup_go_packaging (cu);
10342 /* Now that we have processed all the DIEs in the CU, all the types
10343 should be complete, and it should now be safe to compute all of the
10345 compute_delayed_physnames (cu);
10347 if (cu->language == language_rust)
10348 rust_union_quirks (cu);
10350 /* Some compilers don't define a DW_AT_high_pc attribute for the
10351 compilation unit. If the DW_AT_high_pc is missing, synthesize
10352 it, by scanning the DIE's below the compilation unit. */
10353 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10355 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10356 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10358 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10359 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10360 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10361 addrmap to help ensure it has an accurate map of pc values belonging to
10363 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10365 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10366 SECT_OFF_TEXT (objfile),
10371 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10373 /* Set symtab language to language from DW_AT_language. If the
10374 compilation is from a C file generated by language preprocessors, do
10375 not set the language if it was already deduced by start_subfile. */
10376 if (!(cu->language == language_c
10377 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10378 COMPUNIT_FILETABS (cust)->language = cu->language;
10380 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10381 produce DW_AT_location with location lists but it can be possibly
10382 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10383 there were bugs in prologue debug info, fixed later in GCC-4.5
10384 by "unwind info for epilogues" patch (which is not directly related).
10386 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10387 needed, it would be wrong due to missing DW_AT_producer there.
10389 Still one can confuse GDB by using non-standard GCC compilation
10390 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10392 if (cu->has_loclist && gcc_4_minor >= 5)
10393 cust->locations_valid = 1;
10395 if (gcc_4_minor >= 5)
10396 cust->epilogue_unwind_valid = 1;
10398 cust->call_site_htab = cu->call_site_htab;
10401 if (dwarf2_per_objfile->using_index)
10402 per_cu->v.quick->compunit_symtab = cust;
10405 struct partial_symtab *pst = per_cu->v.psymtab;
10406 pst->compunit_symtab = cust;
10410 /* Push it for inclusion processing later. */
10411 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10413 /* Not needed any more. */
10414 cu->reset_builder ();
10417 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10418 already been loaded into memory. */
10421 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10422 enum language pretend_language)
10424 struct dwarf2_cu *cu = per_cu->cu;
10425 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10426 struct objfile *objfile = dwarf2_per_objfile->objfile;
10427 struct compunit_symtab *cust;
10428 struct signatured_type *sig_type;
10430 gdb_assert (per_cu->is_debug_types);
10431 sig_type = (struct signatured_type *) per_cu;
10433 /* Clear the list here in case something was left over. */
10434 cu->method_list.clear ();
10436 cu->language = pretend_language;
10437 cu->language_defn = language_def (cu->language);
10439 /* The symbol tables are set up in read_type_unit_scope. */
10440 process_die (cu->dies, cu);
10442 /* For now fudge the Go package. */
10443 if (cu->language == language_go)
10444 fixup_go_packaging (cu);
10446 /* Now that we have processed all the DIEs in the CU, all the types
10447 should be complete, and it should now be safe to compute all of the
10449 compute_delayed_physnames (cu);
10451 if (cu->language == language_rust)
10452 rust_union_quirks (cu);
10454 /* TUs share symbol tables.
10455 If this is the first TU to use this symtab, complete the construction
10456 of it with end_expandable_symtab. Otherwise, complete the addition of
10457 this TU's symbols to the existing symtab. */
10458 if (sig_type->type_unit_group->compunit_symtab == NULL)
10460 buildsym_compunit *builder = cu->get_builder ();
10461 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10462 sig_type->type_unit_group->compunit_symtab = cust;
10466 /* Set symtab language to language from DW_AT_language. If the
10467 compilation is from a C file generated by language preprocessors,
10468 do not set the language if it was already deduced by
10470 if (!(cu->language == language_c
10471 && COMPUNIT_FILETABS (cust)->language != language_c))
10472 COMPUNIT_FILETABS (cust)->language = cu->language;
10477 cu->get_builder ()->augment_type_symtab ();
10478 cust = sig_type->type_unit_group->compunit_symtab;
10481 if (dwarf2_per_objfile->using_index)
10482 per_cu->v.quick->compunit_symtab = cust;
10485 struct partial_symtab *pst = per_cu->v.psymtab;
10486 pst->compunit_symtab = cust;
10490 /* Not needed any more. */
10491 cu->reset_builder ();
10494 /* Process an imported unit DIE. */
10497 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10499 struct attribute *attr;
10501 /* For now we don't handle imported units in type units. */
10502 if (cu->per_cu->is_debug_types)
10504 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10505 " supported in type units [in module %s]"),
10506 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10509 attr = dwarf2_attr (die, DW_AT_import, cu);
10512 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10513 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10514 dwarf2_per_cu_data *per_cu
10515 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10516 cu->per_cu->dwarf2_per_objfile);
10518 /* If necessary, add it to the queue and load its DIEs. */
10519 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10520 load_full_comp_unit (per_cu, false, cu->language);
10522 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10527 /* RAII object that represents a process_die scope: i.e.,
10528 starts/finishes processing a DIE. */
10529 class process_die_scope
10532 process_die_scope (die_info *die, dwarf2_cu *cu)
10533 : m_die (die), m_cu (cu)
10535 /* We should only be processing DIEs not already in process. */
10536 gdb_assert (!m_die->in_process);
10537 m_die->in_process = true;
10540 ~process_die_scope ()
10542 m_die->in_process = false;
10544 /* If we're done processing the DIE for the CU that owns the line
10545 header, we don't need the line header anymore. */
10546 if (m_cu->line_header_die_owner == m_die)
10548 delete m_cu->line_header;
10549 m_cu->line_header = NULL;
10550 m_cu->line_header_die_owner = NULL;
10559 /* Process a die and its children. */
10562 process_die (struct die_info *die, struct dwarf2_cu *cu)
10564 process_die_scope scope (die, cu);
10568 case DW_TAG_padding:
10570 case DW_TAG_compile_unit:
10571 case DW_TAG_partial_unit:
10572 read_file_scope (die, cu);
10574 case DW_TAG_type_unit:
10575 read_type_unit_scope (die, cu);
10577 case DW_TAG_subprogram:
10578 case DW_TAG_inlined_subroutine:
10579 read_func_scope (die, cu);
10581 case DW_TAG_lexical_block:
10582 case DW_TAG_try_block:
10583 case DW_TAG_catch_block:
10584 read_lexical_block_scope (die, cu);
10586 case DW_TAG_call_site:
10587 case DW_TAG_GNU_call_site:
10588 read_call_site_scope (die, cu);
10590 case DW_TAG_class_type:
10591 case DW_TAG_interface_type:
10592 case DW_TAG_structure_type:
10593 case DW_TAG_union_type:
10594 process_structure_scope (die, cu);
10596 case DW_TAG_enumeration_type:
10597 process_enumeration_scope (die, cu);
10600 /* These dies have a type, but processing them does not create
10601 a symbol or recurse to process the children. Therefore we can
10602 read them on-demand through read_type_die. */
10603 case DW_TAG_subroutine_type:
10604 case DW_TAG_set_type:
10605 case DW_TAG_array_type:
10606 case DW_TAG_pointer_type:
10607 case DW_TAG_ptr_to_member_type:
10608 case DW_TAG_reference_type:
10609 case DW_TAG_rvalue_reference_type:
10610 case DW_TAG_string_type:
10613 case DW_TAG_base_type:
10614 case DW_TAG_subrange_type:
10615 case DW_TAG_typedef:
10616 /* Add a typedef symbol for the type definition, if it has a
10618 new_symbol (die, read_type_die (die, cu), cu);
10620 case DW_TAG_common_block:
10621 read_common_block (die, cu);
10623 case DW_TAG_common_inclusion:
10625 case DW_TAG_namespace:
10626 cu->processing_has_namespace_info = true;
10627 read_namespace (die, cu);
10629 case DW_TAG_module:
10630 cu->processing_has_namespace_info = true;
10631 read_module (die, cu);
10633 case DW_TAG_imported_declaration:
10634 cu->processing_has_namespace_info = true;
10635 if (read_namespace_alias (die, cu))
10637 /* The declaration is not a global namespace alias. */
10638 /* Fall through. */
10639 case DW_TAG_imported_module:
10640 cu->processing_has_namespace_info = true;
10641 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10642 || cu->language != language_fortran))
10643 complaint (_("Tag '%s' has unexpected children"),
10644 dwarf_tag_name (die->tag));
10645 read_import_statement (die, cu);
10648 case DW_TAG_imported_unit:
10649 process_imported_unit_die (die, cu);
10652 case DW_TAG_variable:
10653 read_variable (die, cu);
10657 new_symbol (die, NULL, cu);
10662 /* DWARF name computation. */
10664 /* A helper function for dwarf2_compute_name which determines whether DIE
10665 needs to have the name of the scope prepended to the name listed in the
10669 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10671 struct attribute *attr;
10675 case DW_TAG_namespace:
10676 case DW_TAG_typedef:
10677 case DW_TAG_class_type:
10678 case DW_TAG_interface_type:
10679 case DW_TAG_structure_type:
10680 case DW_TAG_union_type:
10681 case DW_TAG_enumeration_type:
10682 case DW_TAG_enumerator:
10683 case DW_TAG_subprogram:
10684 case DW_TAG_inlined_subroutine:
10685 case DW_TAG_member:
10686 case DW_TAG_imported_declaration:
10689 case DW_TAG_variable:
10690 case DW_TAG_constant:
10691 /* We only need to prefix "globally" visible variables. These include
10692 any variable marked with DW_AT_external or any variable that
10693 lives in a namespace. [Variables in anonymous namespaces
10694 require prefixing, but they are not DW_AT_external.] */
10696 if (dwarf2_attr (die, DW_AT_specification, cu))
10698 struct dwarf2_cu *spec_cu = cu;
10700 return die_needs_namespace (die_specification (die, &spec_cu),
10704 attr = dwarf2_attr (die, DW_AT_external, cu);
10705 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10706 && die->parent->tag != DW_TAG_module)
10708 /* A variable in a lexical block of some kind does not need a
10709 namespace, even though in C++ such variables may be external
10710 and have a mangled name. */
10711 if (die->parent->tag == DW_TAG_lexical_block
10712 || die->parent->tag == DW_TAG_try_block
10713 || die->parent->tag == DW_TAG_catch_block
10714 || die->parent->tag == DW_TAG_subprogram)
10723 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10724 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10725 defined for the given DIE. */
10727 static struct attribute *
10728 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10730 struct attribute *attr;
10732 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10734 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10739 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10740 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10741 defined for the given DIE. */
10743 static const char *
10744 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10746 const char *linkage_name;
10748 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10749 if (linkage_name == NULL)
10750 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10752 return linkage_name;
10755 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10756 compute the physname for the object, which include a method's:
10757 - formal parameters (C++),
10758 - receiver type (Go),
10760 The term "physname" is a bit confusing.
10761 For C++, for example, it is the demangled name.
10762 For Go, for example, it's the mangled name.
10764 For Ada, return the DIE's linkage name rather than the fully qualified
10765 name. PHYSNAME is ignored..
10767 The result is allocated on the objfile_obstack and canonicalized. */
10769 static const char *
10770 dwarf2_compute_name (const char *name,
10771 struct die_info *die, struct dwarf2_cu *cu,
10774 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10777 name = dwarf2_name (die, cu);
10779 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10780 but otherwise compute it by typename_concat inside GDB.
10781 FIXME: Actually this is not really true, or at least not always true.
10782 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10783 Fortran names because there is no mangling standard. So new_symbol
10784 will set the demangled name to the result of dwarf2_full_name, and it is
10785 the demangled name that GDB uses if it exists. */
10786 if (cu->language == language_ada
10787 || (cu->language == language_fortran && physname))
10789 /* For Ada unit, we prefer the linkage name over the name, as
10790 the former contains the exported name, which the user expects
10791 to be able to reference. Ideally, we want the user to be able
10792 to reference this entity using either natural or linkage name,
10793 but we haven't started looking at this enhancement yet. */
10794 const char *linkage_name = dw2_linkage_name (die, cu);
10796 if (linkage_name != NULL)
10797 return linkage_name;
10800 /* These are the only languages we know how to qualify names in. */
10802 && (cu->language == language_cplus
10803 || cu->language == language_fortran || cu->language == language_d
10804 || cu->language == language_rust))
10806 if (die_needs_namespace (die, cu))
10808 const char *prefix;
10809 const char *canonical_name = NULL;
10813 prefix = determine_prefix (die, cu);
10814 if (*prefix != '\0')
10816 char *prefixed_name = typename_concat (NULL, prefix, name,
10819 buf.puts (prefixed_name);
10820 xfree (prefixed_name);
10825 /* Template parameters may be specified in the DIE's DW_AT_name, or
10826 as children with DW_TAG_template_type_param or
10827 DW_TAG_value_type_param. If the latter, add them to the name
10828 here. If the name already has template parameters, then
10829 skip this step; some versions of GCC emit both, and
10830 it is more efficient to use the pre-computed name.
10832 Something to keep in mind about this process: it is very
10833 unlikely, or in some cases downright impossible, to produce
10834 something that will match the mangled name of a function.
10835 If the definition of the function has the same debug info,
10836 we should be able to match up with it anyway. But fallbacks
10837 using the minimal symbol, for instance to find a method
10838 implemented in a stripped copy of libstdc++, will not work.
10839 If we do not have debug info for the definition, we will have to
10840 match them up some other way.
10842 When we do name matching there is a related problem with function
10843 templates; two instantiated function templates are allowed to
10844 differ only by their return types, which we do not add here. */
10846 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10848 struct attribute *attr;
10849 struct die_info *child;
10852 die->building_fullname = 1;
10854 for (child = die->child; child != NULL; child = child->sibling)
10858 const gdb_byte *bytes;
10859 struct dwarf2_locexpr_baton *baton;
10862 if (child->tag != DW_TAG_template_type_param
10863 && child->tag != DW_TAG_template_value_param)
10874 attr = dwarf2_attr (child, DW_AT_type, cu);
10877 complaint (_("template parameter missing DW_AT_type"));
10878 buf.puts ("UNKNOWN_TYPE");
10881 type = die_type (child, cu);
10883 if (child->tag == DW_TAG_template_type_param)
10885 c_print_type (type, "", &buf, -1, 0, cu->language,
10886 &type_print_raw_options);
10890 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10893 complaint (_("template parameter missing "
10894 "DW_AT_const_value"));
10895 buf.puts ("UNKNOWN_VALUE");
10899 dwarf2_const_value_attr (attr, type, name,
10900 &cu->comp_unit_obstack, cu,
10901 &value, &bytes, &baton);
10903 if (TYPE_NOSIGN (type))
10904 /* GDB prints characters as NUMBER 'CHAR'. If that's
10905 changed, this can use value_print instead. */
10906 c_printchar (value, type, &buf);
10909 struct value_print_options opts;
10912 v = dwarf2_evaluate_loc_desc (type, NULL,
10916 else if (bytes != NULL)
10918 v = allocate_value (type);
10919 memcpy (value_contents_writeable (v), bytes,
10920 TYPE_LENGTH (type));
10923 v = value_from_longest (type, value);
10925 /* Specify decimal so that we do not depend on
10927 get_formatted_print_options (&opts, 'd');
10929 value_print (v, &buf, &opts);
10934 die->building_fullname = 0;
10938 /* Close the argument list, with a space if necessary
10939 (nested templates). */
10940 if (!buf.empty () && buf.string ().back () == '>')
10947 /* For C++ methods, append formal parameter type
10948 information, if PHYSNAME. */
10950 if (physname && die->tag == DW_TAG_subprogram
10951 && cu->language == language_cplus)
10953 struct type *type = read_type_die (die, cu);
10955 c_type_print_args (type, &buf, 1, cu->language,
10956 &type_print_raw_options);
10958 if (cu->language == language_cplus)
10960 /* Assume that an artificial first parameter is
10961 "this", but do not crash if it is not. RealView
10962 marks unnamed (and thus unused) parameters as
10963 artificial; there is no way to differentiate
10965 if (TYPE_NFIELDS (type) > 0
10966 && TYPE_FIELD_ARTIFICIAL (type, 0)
10967 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10968 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10970 buf.puts (" const");
10974 const std::string &intermediate_name = buf.string ();
10976 if (cu->language == language_cplus)
10978 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10979 &objfile->per_bfd->storage_obstack);
10981 /* If we only computed INTERMEDIATE_NAME, or if
10982 INTERMEDIATE_NAME is already canonical, then we need to
10983 copy it to the appropriate obstack. */
10984 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10985 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10986 intermediate_name);
10988 name = canonical_name;
10995 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10996 If scope qualifiers are appropriate they will be added. The result
10997 will be allocated on the storage_obstack, or NULL if the DIE does
10998 not have a name. NAME may either be from a previous call to
10999 dwarf2_name or NULL.
11001 The output string will be canonicalized (if C++). */
11003 static const char *
11004 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11006 return dwarf2_compute_name (name, die, cu, 0);
11009 /* Construct a physname for the given DIE in CU. NAME may either be
11010 from a previous call to dwarf2_name or NULL. The result will be
11011 allocated on the objfile_objstack or NULL if the DIE does not have a
11014 The output string will be canonicalized (if C++). */
11016 static const char *
11017 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11019 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11020 const char *retval, *mangled = NULL, *canon = NULL;
11023 /* In this case dwarf2_compute_name is just a shortcut not building anything
11025 if (!die_needs_namespace (die, cu))
11026 return dwarf2_compute_name (name, die, cu, 1);
11028 mangled = dw2_linkage_name (die, cu);
11030 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11031 See https://github.com/rust-lang/rust/issues/32925. */
11032 if (cu->language == language_rust && mangled != NULL
11033 && strchr (mangled, '{') != NULL)
11036 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11038 gdb::unique_xmalloc_ptr<char> demangled;
11039 if (mangled != NULL)
11042 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11044 /* Do nothing (do not demangle the symbol name). */
11046 else if (cu->language == language_go)
11048 /* This is a lie, but we already lie to the caller new_symbol.
11049 new_symbol assumes we return the mangled name.
11050 This just undoes that lie until things are cleaned up. */
11054 /* Use DMGL_RET_DROP for C++ template functions to suppress
11055 their return type. It is easier for GDB users to search
11056 for such functions as `name(params)' than `long name(params)'.
11057 In such case the minimal symbol names do not match the full
11058 symbol names but for template functions there is never a need
11059 to look up their definition from their declaration so
11060 the only disadvantage remains the minimal symbol variant
11061 `long name(params)' does not have the proper inferior type. */
11062 demangled.reset (gdb_demangle (mangled,
11063 (DMGL_PARAMS | DMGL_ANSI
11064 | DMGL_RET_DROP)));
11067 canon = demangled.get ();
11075 if (canon == NULL || check_physname)
11077 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11079 if (canon != NULL && strcmp (physname, canon) != 0)
11081 /* It may not mean a bug in GDB. The compiler could also
11082 compute DW_AT_linkage_name incorrectly. But in such case
11083 GDB would need to be bug-to-bug compatible. */
11085 complaint (_("Computed physname <%s> does not match demangled <%s> "
11086 "(from linkage <%s>) - DIE at %s [in module %s]"),
11087 physname, canon, mangled, sect_offset_str (die->sect_off),
11088 objfile_name (objfile));
11090 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11091 is available here - over computed PHYSNAME. It is safer
11092 against both buggy GDB and buggy compilers. */
11106 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11111 /* Inspect DIE in CU for a namespace alias. If one exists, record
11112 a new symbol for it.
11114 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11117 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11119 struct attribute *attr;
11121 /* If the die does not have a name, this is not a namespace
11123 attr = dwarf2_attr (die, DW_AT_name, cu);
11127 struct die_info *d = die;
11128 struct dwarf2_cu *imported_cu = cu;
11130 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11131 keep inspecting DIEs until we hit the underlying import. */
11132 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11133 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11135 attr = dwarf2_attr (d, DW_AT_import, cu);
11139 d = follow_die_ref (d, attr, &imported_cu);
11140 if (d->tag != DW_TAG_imported_declaration)
11144 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11146 complaint (_("DIE at %s has too many recursively imported "
11147 "declarations"), sect_offset_str (d->sect_off));
11154 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11156 type = get_die_type_at_offset (sect_off, cu->per_cu);
11157 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11159 /* This declaration is a global namespace alias. Add
11160 a symbol for it whose type is the aliased namespace. */
11161 new_symbol (die, type, cu);
11170 /* Return the using directives repository (global or local?) to use in the
11171 current context for CU.
11173 For Ada, imported declarations can materialize renamings, which *may* be
11174 global. However it is impossible (for now?) in DWARF to distinguish
11175 "external" imported declarations and "static" ones. As all imported
11176 declarations seem to be static in all other languages, make them all CU-wide
11177 global only in Ada. */
11179 static struct using_direct **
11180 using_directives (struct dwarf2_cu *cu)
11182 if (cu->language == language_ada
11183 && cu->get_builder ()->outermost_context_p ())
11184 return cu->get_builder ()->get_global_using_directives ();
11186 return cu->get_builder ()->get_local_using_directives ();
11189 /* Read the import statement specified by the given die and record it. */
11192 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11195 struct attribute *import_attr;
11196 struct die_info *imported_die, *child_die;
11197 struct dwarf2_cu *imported_cu;
11198 const char *imported_name;
11199 const char *imported_name_prefix;
11200 const char *canonical_name;
11201 const char *import_alias;
11202 const char *imported_declaration = NULL;
11203 const char *import_prefix;
11204 std::vector<const char *> excludes;
11206 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11207 if (import_attr == NULL)
11209 complaint (_("Tag '%s' has no DW_AT_import"),
11210 dwarf_tag_name (die->tag));
11215 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11216 imported_name = dwarf2_name (imported_die, imported_cu);
11217 if (imported_name == NULL)
11219 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11221 The import in the following code:
11235 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11236 <52> DW_AT_decl_file : 1
11237 <53> DW_AT_decl_line : 6
11238 <54> DW_AT_import : <0x75>
11239 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11240 <59> DW_AT_name : B
11241 <5b> DW_AT_decl_file : 1
11242 <5c> DW_AT_decl_line : 2
11243 <5d> DW_AT_type : <0x6e>
11245 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11246 <76> DW_AT_byte_size : 4
11247 <77> DW_AT_encoding : 5 (signed)
11249 imports the wrong die ( 0x75 instead of 0x58 ).
11250 This case will be ignored until the gcc bug is fixed. */
11254 /* Figure out the local name after import. */
11255 import_alias = dwarf2_name (die, cu);
11257 /* Figure out where the statement is being imported to. */
11258 import_prefix = determine_prefix (die, cu);
11260 /* Figure out what the scope of the imported die is and prepend it
11261 to the name of the imported die. */
11262 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11264 if (imported_die->tag != DW_TAG_namespace
11265 && imported_die->tag != DW_TAG_module)
11267 imported_declaration = imported_name;
11268 canonical_name = imported_name_prefix;
11270 else if (strlen (imported_name_prefix) > 0)
11271 canonical_name = obconcat (&objfile->objfile_obstack,
11272 imported_name_prefix,
11273 (cu->language == language_d ? "." : "::"),
11274 imported_name, (char *) NULL);
11276 canonical_name = imported_name;
11278 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11279 for (child_die = die->child; child_die && child_die->tag;
11280 child_die = sibling_die (child_die))
11282 /* DWARF-4: A Fortran use statement with a “rename list” may be
11283 represented by an imported module entry with an import attribute
11284 referring to the module and owned entries corresponding to those
11285 entities that are renamed as part of being imported. */
11287 if (child_die->tag != DW_TAG_imported_declaration)
11289 complaint (_("child DW_TAG_imported_declaration expected "
11290 "- DIE at %s [in module %s]"),
11291 sect_offset_str (child_die->sect_off),
11292 objfile_name (objfile));
11296 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11297 if (import_attr == NULL)
11299 complaint (_("Tag '%s' has no DW_AT_import"),
11300 dwarf_tag_name (child_die->tag));
11305 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11307 imported_name = dwarf2_name (imported_die, imported_cu);
11308 if (imported_name == NULL)
11310 complaint (_("child DW_TAG_imported_declaration has unknown "
11311 "imported name - DIE at %s [in module %s]"),
11312 sect_offset_str (child_die->sect_off),
11313 objfile_name (objfile));
11317 excludes.push_back (imported_name);
11319 process_die (child_die, cu);
11322 add_using_directive (using_directives (cu),
11326 imported_declaration,
11329 &objfile->objfile_obstack);
11332 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11333 types, but gives them a size of zero. Starting with version 14,
11334 ICC is compatible with GCC. */
11337 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11339 if (!cu->checked_producer)
11340 check_producer (cu);
11342 return cu->producer_is_icc_lt_14;
11345 /* ICC generates a DW_AT_type for C void functions. This was observed on
11346 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11347 which says that void functions should not have a DW_AT_type. */
11350 producer_is_icc (struct dwarf2_cu *cu)
11352 if (!cu->checked_producer)
11353 check_producer (cu);
11355 return cu->producer_is_icc;
11358 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11359 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11360 this, it was first present in GCC release 4.3.0. */
11363 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11365 if (!cu->checked_producer)
11366 check_producer (cu);
11368 return cu->producer_is_gcc_lt_4_3;
11371 static file_and_directory
11372 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11374 file_and_directory res;
11376 /* Find the filename. Do not use dwarf2_name here, since the filename
11377 is not a source language identifier. */
11378 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11379 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11381 if (res.comp_dir == NULL
11382 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11383 && IS_ABSOLUTE_PATH (res.name))
11385 res.comp_dir_storage = ldirname (res.name);
11386 if (!res.comp_dir_storage.empty ())
11387 res.comp_dir = res.comp_dir_storage.c_str ();
11389 if (res.comp_dir != NULL)
11391 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11392 directory, get rid of it. */
11393 const char *cp = strchr (res.comp_dir, ':');
11395 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11396 res.comp_dir = cp + 1;
11399 if (res.name == NULL)
11400 res.name = "<unknown>";
11405 /* Handle DW_AT_stmt_list for a compilation unit.
11406 DIE is the DW_TAG_compile_unit die for CU.
11407 COMP_DIR is the compilation directory. LOWPC is passed to
11408 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11411 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11412 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11414 struct dwarf2_per_objfile *dwarf2_per_objfile
11415 = cu->per_cu->dwarf2_per_objfile;
11416 struct objfile *objfile = dwarf2_per_objfile->objfile;
11417 struct attribute *attr;
11418 struct line_header line_header_local;
11419 hashval_t line_header_local_hash;
11421 int decode_mapping;
11423 gdb_assert (! cu->per_cu->is_debug_types);
11425 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11429 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11431 /* The line header hash table is only created if needed (it exists to
11432 prevent redundant reading of the line table for partial_units).
11433 If we're given a partial_unit, we'll need it. If we're given a
11434 compile_unit, then use the line header hash table if it's already
11435 created, but don't create one just yet. */
11437 if (dwarf2_per_objfile->line_header_hash == NULL
11438 && die->tag == DW_TAG_partial_unit)
11440 dwarf2_per_objfile->line_header_hash
11441 = htab_create_alloc_ex (127, line_header_hash_voidp,
11442 line_header_eq_voidp,
11443 free_line_header_voidp,
11444 &objfile->objfile_obstack,
11445 hashtab_obstack_allocate,
11446 dummy_obstack_deallocate);
11449 line_header_local.sect_off = line_offset;
11450 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11451 line_header_local_hash = line_header_hash (&line_header_local);
11452 if (dwarf2_per_objfile->line_header_hash != NULL)
11454 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11455 &line_header_local,
11456 line_header_local_hash, NO_INSERT);
11458 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11459 is not present in *SLOT (since if there is something in *SLOT then
11460 it will be for a partial_unit). */
11461 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11463 gdb_assert (*slot != NULL);
11464 cu->line_header = (struct line_header *) *slot;
11469 /* dwarf_decode_line_header does not yet provide sufficient information.
11470 We always have to call also dwarf_decode_lines for it. */
11471 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11475 cu->line_header = lh.release ();
11476 cu->line_header_die_owner = die;
11478 if (dwarf2_per_objfile->line_header_hash == NULL)
11482 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11483 &line_header_local,
11484 line_header_local_hash, INSERT);
11485 gdb_assert (slot != NULL);
11487 if (slot != NULL && *slot == NULL)
11489 /* This newly decoded line number information unit will be owned
11490 by line_header_hash hash table. */
11491 *slot = cu->line_header;
11492 cu->line_header_die_owner = NULL;
11496 /* We cannot free any current entry in (*slot) as that struct line_header
11497 may be already used by multiple CUs. Create only temporary decoded
11498 line_header for this CU - it may happen at most once for each line
11499 number information unit. And if we're not using line_header_hash
11500 then this is what we want as well. */
11501 gdb_assert (die->tag != DW_TAG_partial_unit);
11503 decode_mapping = (die->tag != DW_TAG_partial_unit);
11504 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11509 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11512 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11514 struct dwarf2_per_objfile *dwarf2_per_objfile
11515 = cu->per_cu->dwarf2_per_objfile;
11516 struct objfile *objfile = dwarf2_per_objfile->objfile;
11517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11518 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11519 CORE_ADDR highpc = ((CORE_ADDR) 0);
11520 struct attribute *attr;
11521 struct die_info *child_die;
11522 CORE_ADDR baseaddr;
11524 prepare_one_comp_unit (cu, die, cu->language);
11525 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11527 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11529 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11530 from finish_block. */
11531 if (lowpc == ((CORE_ADDR) -1))
11533 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11535 file_and_directory fnd = find_file_and_directory (die, cu);
11537 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11538 standardised yet. As a workaround for the language detection we fall
11539 back to the DW_AT_producer string. */
11540 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11541 cu->language = language_opencl;
11543 /* Similar hack for Go. */
11544 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11545 set_cu_language (DW_LANG_Go, cu);
11547 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11549 /* Decode line number information if present. We do this before
11550 processing child DIEs, so that the line header table is available
11551 for DW_AT_decl_file. */
11552 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11554 /* Process all dies in compilation unit. */
11555 if (die->child != NULL)
11557 child_die = die->child;
11558 while (child_die && child_die->tag)
11560 process_die (child_die, cu);
11561 child_die = sibling_die (child_die);
11565 /* Decode macro information, if present. Dwarf 2 macro information
11566 refers to information in the line number info statement program
11567 header, so we can only read it if we've read the header
11569 attr = dwarf2_attr (die, DW_AT_macros, cu);
11571 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11572 if (attr && cu->line_header)
11574 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11575 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11577 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11581 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11582 if (attr && cu->line_header)
11584 unsigned int macro_offset = DW_UNSND (attr);
11586 dwarf_decode_macros (cu, macro_offset, 0);
11592 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11594 struct type_unit_group *tu_group;
11596 struct attribute *attr;
11598 struct signatured_type *sig_type;
11600 gdb_assert (per_cu->is_debug_types);
11601 sig_type = (struct signatured_type *) per_cu;
11603 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11605 /* If we're using .gdb_index (includes -readnow) then
11606 per_cu->type_unit_group may not have been set up yet. */
11607 if (sig_type->type_unit_group == NULL)
11608 sig_type->type_unit_group = get_type_unit_group (this, attr);
11609 tu_group = sig_type->type_unit_group;
11611 /* If we've already processed this stmt_list there's no real need to
11612 do it again, we could fake it and just recreate the part we need
11613 (file name,index -> symtab mapping). If data shows this optimization
11614 is useful we can do it then. */
11615 first_time = tu_group->compunit_symtab == NULL;
11617 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11622 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11623 lh = dwarf_decode_line_header (line_offset, this);
11628 start_symtab ("", NULL, 0);
11631 gdb_assert (tu_group->symtabs == NULL);
11632 gdb_assert (m_builder == nullptr);
11633 struct compunit_symtab *cust = tu_group->compunit_symtab;
11634 m_builder.reset (new struct buildsym_compunit
11635 (COMPUNIT_OBJFILE (cust), "",
11636 COMPUNIT_DIRNAME (cust),
11637 compunit_language (cust),
11643 line_header = lh.release ();
11644 line_header_die_owner = die;
11648 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11650 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11651 still initializing it, and our caller (a few levels up)
11652 process_full_type_unit still needs to know if this is the first
11655 tu_group->num_symtabs = line_header->file_names.size ();
11656 tu_group->symtabs = XNEWVEC (struct symtab *,
11657 line_header->file_names.size ());
11659 for (i = 0; i < line_header->file_names.size (); ++i)
11661 file_entry &fe = line_header->file_names[i];
11663 dwarf2_start_subfile (this, fe.name,
11664 fe.include_dir (line_header));
11665 buildsym_compunit *b = get_builder ();
11666 if (b->get_current_subfile ()->symtab == NULL)
11668 /* NOTE: start_subfile will recognize when it's been
11669 passed a file it has already seen. So we can't
11670 assume there's a simple mapping from
11671 cu->line_header->file_names to subfiles, plus
11672 cu->line_header->file_names may contain dups. */
11673 b->get_current_subfile ()->symtab
11674 = allocate_symtab (cust, b->get_current_subfile ()->name);
11677 fe.symtab = b->get_current_subfile ()->symtab;
11678 tu_group->symtabs[i] = fe.symtab;
11683 gdb_assert (m_builder == nullptr);
11684 struct compunit_symtab *cust = tu_group->compunit_symtab;
11685 m_builder.reset (new struct buildsym_compunit
11686 (COMPUNIT_OBJFILE (cust), "",
11687 COMPUNIT_DIRNAME (cust),
11688 compunit_language (cust),
11691 for (i = 0; i < line_header->file_names.size (); ++i)
11693 file_entry &fe = line_header->file_names[i];
11695 fe.symtab = tu_group->symtabs[i];
11699 /* The main symtab is allocated last. Type units don't have DW_AT_name
11700 so they don't have a "real" (so to speak) symtab anyway.
11701 There is later code that will assign the main symtab to all symbols
11702 that don't have one. We need to handle the case of a symbol with a
11703 missing symtab (DW_AT_decl_file) anyway. */
11706 /* Process DW_TAG_type_unit.
11707 For TUs we want to skip the first top level sibling if it's not the
11708 actual type being defined by this TU. In this case the first top
11709 level sibling is there to provide context only. */
11712 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11714 struct die_info *child_die;
11716 prepare_one_comp_unit (cu, die, language_minimal);
11718 /* Initialize (or reinitialize) the machinery for building symtabs.
11719 We do this before processing child DIEs, so that the line header table
11720 is available for DW_AT_decl_file. */
11721 cu->setup_type_unit_groups (die);
11723 if (die->child != NULL)
11725 child_die = die->child;
11726 while (child_die && child_die->tag)
11728 process_die (child_die, cu);
11729 child_die = sibling_die (child_die);
11736 http://gcc.gnu.org/wiki/DebugFission
11737 http://gcc.gnu.org/wiki/DebugFissionDWP
11739 To simplify handling of both DWO files ("object" files with the DWARF info)
11740 and DWP files (a file with the DWOs packaged up into one file), we treat
11741 DWP files as having a collection of virtual DWO files. */
11744 hash_dwo_file (const void *item)
11746 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11749 hash = htab_hash_string (dwo_file->dwo_name);
11750 if (dwo_file->comp_dir != NULL)
11751 hash += htab_hash_string (dwo_file->comp_dir);
11756 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11758 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11759 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11761 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11763 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11764 return lhs->comp_dir == rhs->comp_dir;
11765 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11768 /* Allocate a hash table for DWO files. */
11771 allocate_dwo_file_hash_table (struct objfile *objfile)
11773 auto delete_dwo_file = [] (void *item)
11775 struct dwo_file *dwo_file = (struct dwo_file *) item;
11780 return htab_up (htab_create_alloc_ex (41,
11784 &objfile->objfile_obstack,
11785 hashtab_obstack_allocate,
11786 dummy_obstack_deallocate));
11789 /* Lookup DWO file DWO_NAME. */
11792 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11793 const char *dwo_name,
11794 const char *comp_dir)
11796 struct dwo_file find_entry;
11799 if (dwarf2_per_objfile->dwo_files == NULL)
11800 dwarf2_per_objfile->dwo_files
11801 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11803 find_entry.dwo_name = dwo_name;
11804 find_entry.comp_dir = comp_dir;
11805 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11812 hash_dwo_unit (const void *item)
11814 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11816 /* This drops the top 32 bits of the id, but is ok for a hash. */
11817 return dwo_unit->signature;
11821 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11823 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11824 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11826 /* The signature is assumed to be unique within the DWO file.
11827 So while object file CU dwo_id's always have the value zero,
11828 that's OK, assuming each object file DWO file has only one CU,
11829 and that's the rule for now. */
11830 return lhs->signature == rhs->signature;
11833 /* Allocate a hash table for DWO CUs,TUs.
11834 There is one of these tables for each of CUs,TUs for each DWO file. */
11837 allocate_dwo_unit_table (struct objfile *objfile)
11839 /* Start out with a pretty small number.
11840 Generally DWO files contain only one CU and maybe some TUs. */
11841 return htab_create_alloc_ex (3,
11845 &objfile->objfile_obstack,
11846 hashtab_obstack_allocate,
11847 dummy_obstack_deallocate);
11850 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11852 struct create_dwo_cu_data
11854 struct dwo_file *dwo_file;
11855 struct dwo_unit dwo_unit;
11858 /* die_reader_func for create_dwo_cu. */
11861 create_dwo_cu_reader (const struct die_reader_specs *reader,
11862 const gdb_byte *info_ptr,
11863 struct die_info *comp_unit_die,
11867 struct dwarf2_cu *cu = reader->cu;
11868 sect_offset sect_off = cu->per_cu->sect_off;
11869 struct dwarf2_section_info *section = cu->per_cu->section;
11870 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11871 struct dwo_file *dwo_file = data->dwo_file;
11872 struct dwo_unit *dwo_unit = &data->dwo_unit;
11874 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11875 if (!signature.has_value ())
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 = *signature;
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;
12453 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12455 dwo_file->comp_dir = comp_dir;
12456 dwo_file->sections.abbrev = sections.abbrev;
12457 dwo_file->sections.line = sections.line;
12458 dwo_file->sections.loc = sections.loc;
12459 dwo_file->sections.macinfo = sections.macinfo;
12460 dwo_file->sections.macro = sections.macro;
12461 dwo_file->sections.str_offsets = sections.str_offsets;
12462 /* The "str" section is global to the entire DWP file. */
12463 dwo_file->sections.str = dwp_file->sections.str;
12464 /* The info or types section is assigned below to dwo_unit,
12465 there's no need to record it in dwo_file.
12466 Also, we can't simply record type sections in dwo_file because
12467 we record a pointer into the vector in dwo_unit. As we collect more
12468 types we'll grow the vector and eventually have to reallocate space
12469 for it, invalidating all copies of pointers into the previous
12471 *dwo_file_slot = dwo_file;
12475 if (dwarf_read_debug)
12477 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12478 virtual_dwo_name.c_str ());
12480 dwo_file = (struct dwo_file *) *dwo_file_slot;
12483 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12484 dwo_unit->dwo_file = dwo_file;
12485 dwo_unit->signature = signature;
12486 dwo_unit->section =
12487 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12488 *dwo_unit->section = sections.info_or_types;
12489 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12494 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12495 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12496 piece within that section used by a TU/CU, return a virtual section
12497 of just that piece. */
12499 static struct dwarf2_section_info
12500 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12501 struct dwarf2_section_info *section,
12502 bfd_size_type offset, bfd_size_type size)
12504 struct dwarf2_section_info result;
12507 gdb_assert (section != NULL);
12508 gdb_assert (!section->is_virtual);
12510 memset (&result, 0, sizeof (result));
12511 result.s.containing_section = section;
12512 result.is_virtual = true;
12517 sectp = get_section_bfd_section (section);
12519 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12520 bounds of the real section. This is a pretty-rare event, so just
12521 flag an error (easier) instead of a warning and trying to cope. */
12523 || offset + size > bfd_get_section_size (sectp))
12525 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12526 " in section %s [in module %s]"),
12527 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12528 objfile_name (dwarf2_per_objfile->objfile));
12531 result.virtual_offset = offset;
12532 result.size = size;
12536 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12537 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12538 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12539 This is for DWP version 2 files. */
12541 static struct dwo_unit *
12542 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12543 struct dwp_file *dwp_file,
12544 uint32_t unit_index,
12545 const char *comp_dir,
12546 ULONGEST signature, int is_debug_types)
12548 struct objfile *objfile = dwarf2_per_objfile->objfile;
12549 const struct dwp_hash_table *dwp_htab =
12550 is_debug_types ? dwp_file->tus : dwp_file->cus;
12551 bfd *dbfd = dwp_file->dbfd.get ();
12552 const char *kind = is_debug_types ? "TU" : "CU";
12553 struct dwo_file *dwo_file;
12554 struct dwo_unit *dwo_unit;
12555 struct virtual_v2_dwo_sections sections;
12556 void **dwo_file_slot;
12559 gdb_assert (dwp_file->version == 2);
12561 if (dwarf_read_debug)
12563 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12565 pulongest (unit_index), hex_string (signature),
12569 /* Fetch the section offsets of this DWO unit. */
12571 memset (§ions, 0, sizeof (sections));
12573 for (i = 0; i < dwp_htab->nr_columns; ++i)
12575 uint32_t offset = read_4_bytes (dbfd,
12576 dwp_htab->section_pool.v2.offsets
12577 + (((unit_index - 1) * dwp_htab->nr_columns
12579 * sizeof (uint32_t)));
12580 uint32_t size = read_4_bytes (dbfd,
12581 dwp_htab->section_pool.v2.sizes
12582 + (((unit_index - 1) * dwp_htab->nr_columns
12584 * sizeof (uint32_t)));
12586 switch (dwp_htab->section_pool.v2.section_ids[i])
12589 case DW_SECT_TYPES:
12590 sections.info_or_types_offset = offset;
12591 sections.info_or_types_size = size;
12593 case DW_SECT_ABBREV:
12594 sections.abbrev_offset = offset;
12595 sections.abbrev_size = size;
12598 sections.line_offset = offset;
12599 sections.line_size = size;
12602 sections.loc_offset = offset;
12603 sections.loc_size = size;
12605 case DW_SECT_STR_OFFSETS:
12606 sections.str_offsets_offset = offset;
12607 sections.str_offsets_size = size;
12609 case DW_SECT_MACINFO:
12610 sections.macinfo_offset = offset;
12611 sections.macinfo_size = size;
12613 case DW_SECT_MACRO:
12614 sections.macro_offset = offset;
12615 sections.macro_size = size;
12620 /* It's easier for the rest of the code if we fake a struct dwo_file and
12621 have dwo_unit "live" in that. At least for now.
12623 The DWP file can be made up of a random collection of CUs and TUs.
12624 However, for each CU + set of TUs that came from the same original DWO
12625 file, we can combine them back into a virtual DWO file to save space
12626 (fewer struct dwo_file objects to allocate). Remember that for really
12627 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12629 std::string virtual_dwo_name =
12630 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12631 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12632 (long) (sections.line_size ? sections.line_offset : 0),
12633 (long) (sections.loc_size ? sections.loc_offset : 0),
12634 (long) (sections.str_offsets_size
12635 ? sections.str_offsets_offset : 0));
12636 /* Can we use an existing virtual DWO file? */
12637 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12638 virtual_dwo_name.c_str (),
12640 /* Create one if necessary. */
12641 if (*dwo_file_slot == NULL)
12643 if (dwarf_read_debug)
12645 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12646 virtual_dwo_name.c_str ());
12648 dwo_file = new struct dwo_file;
12649 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12651 dwo_file->comp_dir = comp_dir;
12652 dwo_file->sections.abbrev =
12653 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12654 sections.abbrev_offset, sections.abbrev_size);
12655 dwo_file->sections.line =
12656 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12657 sections.line_offset, sections.line_size);
12658 dwo_file->sections.loc =
12659 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12660 sections.loc_offset, sections.loc_size);
12661 dwo_file->sections.macinfo =
12662 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12663 sections.macinfo_offset, sections.macinfo_size);
12664 dwo_file->sections.macro =
12665 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12666 sections.macro_offset, sections.macro_size);
12667 dwo_file->sections.str_offsets =
12668 create_dwp_v2_section (dwarf2_per_objfile,
12669 &dwp_file->sections.str_offsets,
12670 sections.str_offsets_offset,
12671 sections.str_offsets_size);
12672 /* The "str" section is global to the entire DWP file. */
12673 dwo_file->sections.str = dwp_file->sections.str;
12674 /* The info or types section is assigned below to dwo_unit,
12675 there's no need to record it in dwo_file.
12676 Also, we can't simply record type sections in dwo_file because
12677 we record a pointer into the vector in dwo_unit. As we collect more
12678 types we'll grow the vector and eventually have to reallocate space
12679 for it, invalidating all copies of pointers into the previous
12681 *dwo_file_slot = dwo_file;
12685 if (dwarf_read_debug)
12687 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12688 virtual_dwo_name.c_str ());
12690 dwo_file = (struct dwo_file *) *dwo_file_slot;
12693 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12694 dwo_unit->dwo_file = dwo_file;
12695 dwo_unit->signature = signature;
12696 dwo_unit->section =
12697 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12698 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12700 ? &dwp_file->sections.types
12701 : &dwp_file->sections.info,
12702 sections.info_or_types_offset,
12703 sections.info_or_types_size);
12704 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12709 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12710 Returns NULL if the signature isn't found. */
12712 static struct dwo_unit *
12713 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12714 struct dwp_file *dwp_file, const char *comp_dir,
12715 ULONGEST signature, int is_debug_types)
12717 const struct dwp_hash_table *dwp_htab =
12718 is_debug_types ? dwp_file->tus : dwp_file->cus;
12719 bfd *dbfd = dwp_file->dbfd.get ();
12720 uint32_t mask = dwp_htab->nr_slots - 1;
12721 uint32_t hash = signature & mask;
12722 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12725 struct dwo_unit find_dwo_cu;
12727 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12728 find_dwo_cu.signature = signature;
12729 slot = htab_find_slot (is_debug_types
12730 ? dwp_file->loaded_tus
12731 : dwp_file->loaded_cus,
12732 &find_dwo_cu, INSERT);
12735 return (struct dwo_unit *) *slot;
12737 /* Use a for loop so that we don't loop forever on bad debug info. */
12738 for (i = 0; i < dwp_htab->nr_slots; ++i)
12740 ULONGEST signature_in_table;
12742 signature_in_table =
12743 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12744 if (signature_in_table == signature)
12746 uint32_t unit_index =
12747 read_4_bytes (dbfd,
12748 dwp_htab->unit_table + hash * sizeof (uint32_t));
12750 if (dwp_file->version == 1)
12752 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12753 dwp_file, unit_index,
12754 comp_dir, signature,
12759 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12760 dwp_file, unit_index,
12761 comp_dir, signature,
12764 return (struct dwo_unit *) *slot;
12766 if (signature_in_table == 0)
12768 hash = (hash + hash2) & mask;
12771 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12772 " [in module %s]"),
12776 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12777 Open the file specified by FILE_NAME and hand it off to BFD for
12778 preliminary analysis. Return a newly initialized bfd *, which
12779 includes a canonicalized copy of FILE_NAME.
12780 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12781 SEARCH_CWD is true if the current directory is to be searched.
12782 It will be searched before debug-file-directory.
12783 If successful, the file is added to the bfd include table of the
12784 objfile's bfd (see gdb_bfd_record_inclusion).
12785 If unable to find/open the file, return NULL.
12786 NOTE: This function is derived from symfile_bfd_open. */
12788 static gdb_bfd_ref_ptr
12789 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12790 const char *file_name, int is_dwp, int search_cwd)
12793 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12794 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12795 to debug_file_directory. */
12796 const char *search_path;
12797 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12799 gdb::unique_xmalloc_ptr<char> search_path_holder;
12802 if (*debug_file_directory != '\0')
12804 search_path_holder.reset (concat (".", dirname_separator_string,
12805 debug_file_directory,
12807 search_path = search_path_holder.get ();
12813 search_path = debug_file_directory;
12815 openp_flags flags = OPF_RETURN_REALPATH;
12817 flags |= OPF_SEARCH_IN_PATH;
12819 gdb::unique_xmalloc_ptr<char> absolute_name;
12820 desc = openp (search_path, flags, file_name,
12821 O_RDONLY | O_BINARY, &absolute_name);
12825 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12827 if (sym_bfd == NULL)
12829 bfd_set_cacheable (sym_bfd.get (), 1);
12831 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12834 /* Success. Record the bfd as having been included by the objfile's bfd.
12835 This is important because things like demangled_names_hash lives in the
12836 objfile's per_bfd space and may have references to things like symbol
12837 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12838 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12843 /* Try to open DWO file FILE_NAME.
12844 COMP_DIR is the DW_AT_comp_dir attribute.
12845 The result is the bfd handle of the file.
12846 If there is a problem finding or opening the file, return NULL.
12847 Upon success, the canonicalized path of the file is stored in the bfd,
12848 same as symfile_bfd_open. */
12850 static gdb_bfd_ref_ptr
12851 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12852 const char *file_name, const char *comp_dir)
12854 if (IS_ABSOLUTE_PATH (file_name))
12855 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12856 0 /*is_dwp*/, 0 /*search_cwd*/);
12858 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12860 if (comp_dir != NULL)
12862 char *path_to_try = concat (comp_dir, SLASH_STRING,
12863 file_name, (char *) NULL);
12865 /* NOTE: If comp_dir is a relative path, this will also try the
12866 search path, which seems useful. */
12867 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12870 1 /*search_cwd*/));
12871 xfree (path_to_try);
12876 /* That didn't work, try debug-file-directory, which, despite its name,
12877 is a list of paths. */
12879 if (*debug_file_directory == '\0')
12882 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12883 0 /*is_dwp*/, 1 /*search_cwd*/);
12886 /* This function is mapped across the sections and remembers the offset and
12887 size of each of the DWO debugging sections we are interested in. */
12890 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12892 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12893 const struct dwop_section_names *names = &dwop_section_names;
12895 if (section_is_p (sectp->name, &names->abbrev_dwo))
12897 dwo_sections->abbrev.s.section = sectp;
12898 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12900 else if (section_is_p (sectp->name, &names->info_dwo))
12902 dwo_sections->info.s.section = sectp;
12903 dwo_sections->info.size = bfd_get_section_size (sectp);
12905 else if (section_is_p (sectp->name, &names->line_dwo))
12907 dwo_sections->line.s.section = sectp;
12908 dwo_sections->line.size = bfd_get_section_size (sectp);
12910 else if (section_is_p (sectp->name, &names->loc_dwo))
12912 dwo_sections->loc.s.section = sectp;
12913 dwo_sections->loc.size = bfd_get_section_size (sectp);
12915 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12917 dwo_sections->macinfo.s.section = sectp;
12918 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12920 else if (section_is_p (sectp->name, &names->macro_dwo))
12922 dwo_sections->macro.s.section = sectp;
12923 dwo_sections->macro.size = bfd_get_section_size (sectp);
12925 else if (section_is_p (sectp->name, &names->str_dwo))
12927 dwo_sections->str.s.section = sectp;
12928 dwo_sections->str.size = bfd_get_section_size (sectp);
12930 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12932 dwo_sections->str_offsets.s.section = sectp;
12933 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12935 else if (section_is_p (sectp->name, &names->types_dwo))
12937 struct dwarf2_section_info type_section;
12939 memset (&type_section, 0, sizeof (type_section));
12940 type_section.s.section = sectp;
12941 type_section.size = bfd_get_section_size (sectp);
12942 dwo_sections->types.push_back (type_section);
12946 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12947 by PER_CU. This is for the non-DWP case.
12948 The result is NULL if DWO_NAME can't be found. */
12950 static struct dwo_file *
12951 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12952 const char *dwo_name, const char *comp_dir)
12954 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12956 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12959 if (dwarf_read_debug)
12960 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12964 dwo_file_up dwo_file (new struct dwo_file);
12965 dwo_file->dwo_name = dwo_name;
12966 dwo_file->comp_dir = comp_dir;
12967 dwo_file->dbfd = std::move (dbfd);
12969 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12970 &dwo_file->sections);
12972 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12975 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12976 dwo_file->sections.types, dwo_file->tus);
12978 if (dwarf_read_debug)
12979 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12981 return dwo_file.release ();
12984 /* This function is mapped across the sections and remembers the offset and
12985 size of each of the DWP debugging sections common to version 1 and 2 that
12986 we are interested in. */
12989 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12990 void *dwp_file_ptr)
12992 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12993 const struct dwop_section_names *names = &dwop_section_names;
12994 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12996 /* Record the ELF section number for later lookup: this is what the
12997 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12998 gdb_assert (elf_section_nr < dwp_file->num_sections);
12999 dwp_file->elf_sections[elf_section_nr] = sectp;
13001 /* Look for specific sections that we need. */
13002 if (section_is_p (sectp->name, &names->str_dwo))
13004 dwp_file->sections.str.s.section = sectp;
13005 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13007 else if (section_is_p (sectp->name, &names->cu_index))
13009 dwp_file->sections.cu_index.s.section = sectp;
13010 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13012 else if (section_is_p (sectp->name, &names->tu_index))
13014 dwp_file->sections.tu_index.s.section = sectp;
13015 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13019 /* This function is mapped across the sections and remembers the offset and
13020 size of each of the DWP version 2 debugging sections that we are interested
13021 in. This is split into a separate function because we don't know if we
13022 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13025 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13027 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13028 const struct dwop_section_names *names = &dwop_section_names;
13029 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13031 /* Record the ELF section number for later lookup: this is what the
13032 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13033 gdb_assert (elf_section_nr < dwp_file->num_sections);
13034 dwp_file->elf_sections[elf_section_nr] = sectp;
13036 /* Look for specific sections that we need. */
13037 if (section_is_p (sectp->name, &names->abbrev_dwo))
13039 dwp_file->sections.abbrev.s.section = sectp;
13040 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13042 else if (section_is_p (sectp->name, &names->info_dwo))
13044 dwp_file->sections.info.s.section = sectp;
13045 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13047 else if (section_is_p (sectp->name, &names->line_dwo))
13049 dwp_file->sections.line.s.section = sectp;
13050 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13052 else if (section_is_p (sectp->name, &names->loc_dwo))
13054 dwp_file->sections.loc.s.section = sectp;
13055 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13057 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13059 dwp_file->sections.macinfo.s.section = sectp;
13060 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13062 else if (section_is_p (sectp->name, &names->macro_dwo))
13064 dwp_file->sections.macro.s.section = sectp;
13065 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13067 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13069 dwp_file->sections.str_offsets.s.section = sectp;
13070 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13072 else if (section_is_p (sectp->name, &names->types_dwo))
13074 dwp_file->sections.types.s.section = sectp;
13075 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13079 /* Hash function for dwp_file loaded CUs/TUs. */
13082 hash_dwp_loaded_cutus (const void *item)
13084 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13086 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13087 return dwo_unit->signature;
13090 /* Equality function for dwp_file loaded CUs/TUs. */
13093 eq_dwp_loaded_cutus (const void *a, const void *b)
13095 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13096 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13098 return dua->signature == dub->signature;
13101 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13104 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13106 return htab_create_alloc_ex (3,
13107 hash_dwp_loaded_cutus,
13108 eq_dwp_loaded_cutus,
13110 &objfile->objfile_obstack,
13111 hashtab_obstack_allocate,
13112 dummy_obstack_deallocate);
13115 /* Try to open DWP file FILE_NAME.
13116 The result is the bfd handle of the file.
13117 If there is a problem finding or opening the file, return NULL.
13118 Upon success, the canonicalized path of the file is stored in the bfd,
13119 same as symfile_bfd_open. */
13121 static gdb_bfd_ref_ptr
13122 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13123 const char *file_name)
13125 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13127 1 /*search_cwd*/));
13131 /* Work around upstream bug 15652.
13132 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13133 [Whether that's a "bug" is debatable, but it is getting in our way.]
13134 We have no real idea where the dwp file is, because gdb's realpath-ing
13135 of the executable's path may have discarded the needed info.
13136 [IWBN if the dwp file name was recorded in the executable, akin to
13137 .gnu_debuglink, but that doesn't exist yet.]
13138 Strip the directory from FILE_NAME and search again. */
13139 if (*debug_file_directory != '\0')
13141 /* Don't implicitly search the current directory here.
13142 If the user wants to search "." to handle this case,
13143 it must be added to debug-file-directory. */
13144 return try_open_dwop_file (dwarf2_per_objfile,
13145 lbasename (file_name), 1 /*is_dwp*/,
13152 /* Initialize the use of the DWP file for the current objfile.
13153 By convention the name of the DWP file is ${objfile}.dwp.
13154 The result is NULL if it can't be found. */
13156 static std::unique_ptr<struct dwp_file>
13157 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13159 struct objfile *objfile = dwarf2_per_objfile->objfile;
13161 /* Try to find first .dwp for the binary file before any symbolic links
13164 /* If the objfile is a debug file, find the name of the real binary
13165 file and get the name of dwp file from there. */
13166 std::string dwp_name;
13167 if (objfile->separate_debug_objfile_backlink != NULL)
13169 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13170 const char *backlink_basename = lbasename (backlink->original_name);
13172 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13175 dwp_name = objfile->original_name;
13177 dwp_name += ".dwp";
13179 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13181 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13183 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13184 dwp_name = objfile_name (objfile);
13185 dwp_name += ".dwp";
13186 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13191 if (dwarf_read_debug)
13192 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13193 return std::unique_ptr<dwp_file> ();
13196 const char *name = bfd_get_filename (dbfd.get ());
13197 std::unique_ptr<struct dwp_file> dwp_file
13198 (new struct dwp_file (name, std::move (dbfd)));
13200 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13201 dwp_file->elf_sections =
13202 OBSTACK_CALLOC (&objfile->objfile_obstack,
13203 dwp_file->num_sections, asection *);
13205 bfd_map_over_sections (dwp_file->dbfd.get (),
13206 dwarf2_locate_common_dwp_sections,
13209 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13212 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13215 /* The DWP file version is stored in the hash table. Oh well. */
13216 if (dwp_file->cus && dwp_file->tus
13217 && dwp_file->cus->version != dwp_file->tus->version)
13219 /* Technically speaking, we should try to limp along, but this is
13220 pretty bizarre. We use pulongest here because that's the established
13221 portability solution (e.g, we cannot use %u for uint32_t). */
13222 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13223 " TU version %s [in DWP file %s]"),
13224 pulongest (dwp_file->cus->version),
13225 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13229 dwp_file->version = dwp_file->cus->version;
13230 else if (dwp_file->tus)
13231 dwp_file->version = dwp_file->tus->version;
13233 dwp_file->version = 2;
13235 if (dwp_file->version == 2)
13236 bfd_map_over_sections (dwp_file->dbfd.get (),
13237 dwarf2_locate_v2_dwp_sections,
13240 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13241 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13243 if (dwarf_read_debug)
13245 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13246 fprintf_unfiltered (gdb_stdlog,
13247 " %s CUs, %s TUs\n",
13248 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13249 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13255 /* Wrapper around open_and_init_dwp_file, only open it once. */
13257 static struct dwp_file *
13258 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13260 if (! dwarf2_per_objfile->dwp_checked)
13262 dwarf2_per_objfile->dwp_file
13263 = open_and_init_dwp_file (dwarf2_per_objfile);
13264 dwarf2_per_objfile->dwp_checked = 1;
13266 return dwarf2_per_objfile->dwp_file.get ();
13269 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13270 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13271 or in the DWP file for the objfile, referenced by THIS_UNIT.
13272 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13273 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13275 This is called, for example, when wanting to read a variable with a
13276 complex location. Therefore we don't want to do file i/o for every call.
13277 Therefore we don't want to look for a DWO file on every call.
13278 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13279 then we check if we've already seen DWO_NAME, and only THEN do we check
13282 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13283 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13285 static struct dwo_unit *
13286 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13287 const char *dwo_name, const char *comp_dir,
13288 ULONGEST signature, int is_debug_types)
13290 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13291 struct objfile *objfile = dwarf2_per_objfile->objfile;
13292 const char *kind = is_debug_types ? "TU" : "CU";
13293 void **dwo_file_slot;
13294 struct dwo_file *dwo_file;
13295 struct dwp_file *dwp_file;
13297 /* First see if there's a DWP file.
13298 If we have a DWP file but didn't find the DWO inside it, don't
13299 look for the original DWO file. It makes gdb behave differently
13300 depending on whether one is debugging in the build tree. */
13302 dwp_file = get_dwp_file (dwarf2_per_objfile);
13303 if (dwp_file != NULL)
13305 const struct dwp_hash_table *dwp_htab =
13306 is_debug_types ? dwp_file->tus : dwp_file->cus;
13308 if (dwp_htab != NULL)
13310 struct dwo_unit *dwo_cutu =
13311 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13312 signature, is_debug_types);
13314 if (dwo_cutu != NULL)
13316 if (dwarf_read_debug)
13318 fprintf_unfiltered (gdb_stdlog,
13319 "Virtual DWO %s %s found: @%s\n",
13320 kind, hex_string (signature),
13321 host_address_to_string (dwo_cutu));
13329 /* No DWP file, look for the DWO file. */
13331 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13332 dwo_name, comp_dir);
13333 if (*dwo_file_slot == NULL)
13335 /* Read in the file and build a table of the CUs/TUs it contains. */
13336 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13338 /* NOTE: This will be NULL if unable to open the file. */
13339 dwo_file = (struct dwo_file *) *dwo_file_slot;
13341 if (dwo_file != NULL)
13343 struct dwo_unit *dwo_cutu = NULL;
13345 if (is_debug_types && dwo_file->tus)
13347 struct dwo_unit find_dwo_cutu;
13349 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13350 find_dwo_cutu.signature = signature;
13352 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13354 else if (!is_debug_types && dwo_file->cus)
13356 struct dwo_unit find_dwo_cutu;
13358 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13359 find_dwo_cutu.signature = signature;
13360 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13364 if (dwo_cutu != NULL)
13366 if (dwarf_read_debug)
13368 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13369 kind, dwo_name, hex_string (signature),
13370 host_address_to_string (dwo_cutu));
13377 /* We didn't find it. This could mean a dwo_id mismatch, or
13378 someone deleted the DWO/DWP file, or the search path isn't set up
13379 correctly to find the file. */
13381 if (dwarf_read_debug)
13383 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13384 kind, dwo_name, hex_string (signature));
13387 /* This is a warning and not a complaint because it can be caused by
13388 pilot error (e.g., user accidentally deleting the DWO). */
13390 /* Print the name of the DWP file if we looked there, helps the user
13391 better diagnose the problem. */
13392 std::string dwp_text;
13394 if (dwp_file != NULL)
13395 dwp_text = string_printf (" [in DWP file %s]",
13396 lbasename (dwp_file->name));
13398 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13399 " [in module %s]"),
13400 kind, dwo_name, hex_string (signature),
13402 this_unit->is_debug_types ? "TU" : "CU",
13403 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13408 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13409 See lookup_dwo_cutu_unit for details. */
13411 static struct dwo_unit *
13412 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13413 const char *dwo_name, const char *comp_dir,
13414 ULONGEST signature)
13416 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13419 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13420 See lookup_dwo_cutu_unit for details. */
13422 static struct dwo_unit *
13423 lookup_dwo_type_unit (struct signatured_type *this_tu,
13424 const char *dwo_name, const char *comp_dir)
13426 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13429 /* Traversal function for queue_and_load_all_dwo_tus. */
13432 queue_and_load_dwo_tu (void **slot, void *info)
13434 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13435 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13436 ULONGEST signature = dwo_unit->signature;
13437 struct signatured_type *sig_type =
13438 lookup_dwo_signatured_type (per_cu->cu, signature);
13440 if (sig_type != NULL)
13442 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13444 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13445 a real dependency of PER_CU on SIG_TYPE. That is detected later
13446 while processing PER_CU. */
13447 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13448 load_full_type_unit (sig_cu);
13449 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13455 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13456 The DWO may have the only definition of the type, though it may not be
13457 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13458 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13461 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13463 struct dwo_unit *dwo_unit;
13464 struct dwo_file *dwo_file;
13466 gdb_assert (!per_cu->is_debug_types);
13467 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13468 gdb_assert (per_cu->cu != NULL);
13470 dwo_unit = per_cu->cu->dwo_unit;
13471 gdb_assert (dwo_unit != NULL);
13473 dwo_file = dwo_unit->dwo_file;
13474 if (dwo_file->tus != NULL)
13475 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13478 /* Read in various DIEs. */
13480 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13481 Inherit only the children of the DW_AT_abstract_origin DIE not being
13482 already referenced by DW_AT_abstract_origin from the children of the
13486 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13488 struct die_info *child_die;
13489 sect_offset *offsetp;
13490 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13491 struct die_info *origin_die;
13492 /* Iterator of the ORIGIN_DIE children. */
13493 struct die_info *origin_child_die;
13494 struct attribute *attr;
13495 struct dwarf2_cu *origin_cu;
13496 struct pending **origin_previous_list_in_scope;
13498 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13502 /* Note that following die references may follow to a die in a
13506 origin_die = follow_die_ref (die, attr, &origin_cu);
13508 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13510 origin_previous_list_in_scope = origin_cu->list_in_scope;
13511 origin_cu->list_in_scope = cu->list_in_scope;
13513 if (die->tag != origin_die->tag
13514 && !(die->tag == DW_TAG_inlined_subroutine
13515 && origin_die->tag == DW_TAG_subprogram))
13516 complaint (_("DIE %s and its abstract origin %s have different tags"),
13517 sect_offset_str (die->sect_off),
13518 sect_offset_str (origin_die->sect_off));
13520 std::vector<sect_offset> offsets;
13522 for (child_die = die->child;
13523 child_die && child_die->tag;
13524 child_die = sibling_die (child_die))
13526 struct die_info *child_origin_die;
13527 struct dwarf2_cu *child_origin_cu;
13529 /* We are trying to process concrete instance entries:
13530 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13531 it's not relevant to our analysis here. i.e. detecting DIEs that are
13532 present in the abstract instance but not referenced in the concrete
13534 if (child_die->tag == DW_TAG_call_site
13535 || child_die->tag == DW_TAG_GNU_call_site)
13538 /* For each CHILD_DIE, find the corresponding child of
13539 ORIGIN_DIE. If there is more than one layer of
13540 DW_AT_abstract_origin, follow them all; there shouldn't be,
13541 but GCC versions at least through 4.4 generate this (GCC PR
13543 child_origin_die = child_die;
13544 child_origin_cu = cu;
13547 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13551 child_origin_die = follow_die_ref (child_origin_die, attr,
13555 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13556 counterpart may exist. */
13557 if (child_origin_die != child_die)
13559 if (child_die->tag != child_origin_die->tag
13560 && !(child_die->tag == DW_TAG_inlined_subroutine
13561 && child_origin_die->tag == DW_TAG_subprogram))
13562 complaint (_("Child DIE %s and its abstract origin %s have "
13564 sect_offset_str (child_die->sect_off),
13565 sect_offset_str (child_origin_die->sect_off));
13566 if (child_origin_die->parent != origin_die)
13567 complaint (_("Child DIE %s and its abstract origin %s have "
13568 "different parents"),
13569 sect_offset_str (child_die->sect_off),
13570 sect_offset_str (child_origin_die->sect_off));
13572 offsets.push_back (child_origin_die->sect_off);
13575 std::sort (offsets.begin (), offsets.end ());
13576 sect_offset *offsets_end = offsets.data () + offsets.size ();
13577 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13578 if (offsetp[-1] == *offsetp)
13579 complaint (_("Multiple children of DIE %s refer "
13580 "to DIE %s as their abstract origin"),
13581 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13583 offsetp = offsets.data ();
13584 origin_child_die = origin_die->child;
13585 while (origin_child_die && origin_child_die->tag)
13587 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13588 while (offsetp < offsets_end
13589 && *offsetp < origin_child_die->sect_off)
13591 if (offsetp >= offsets_end
13592 || *offsetp > origin_child_die->sect_off)
13594 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13595 Check whether we're already processing ORIGIN_CHILD_DIE.
13596 This can happen with mutually referenced abstract_origins.
13598 if (!origin_child_die->in_process)
13599 process_die (origin_child_die, origin_cu);
13601 origin_child_die = sibling_die (origin_child_die);
13603 origin_cu->list_in_scope = origin_previous_list_in_scope;
13607 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13609 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13610 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13611 struct context_stack *newobj;
13614 struct die_info *child_die;
13615 struct attribute *attr, *call_line, *call_file;
13617 CORE_ADDR baseaddr;
13618 struct block *block;
13619 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13620 std::vector<struct symbol *> template_args;
13621 struct template_symbol *templ_func = NULL;
13625 /* If we do not have call site information, we can't show the
13626 caller of this inlined function. That's too confusing, so
13627 only use the scope for local variables. */
13628 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13629 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13630 if (call_line == NULL || call_file == NULL)
13632 read_lexical_block_scope (die, cu);
13637 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13639 name = dwarf2_name (die, cu);
13641 /* Ignore functions with missing or empty names. These are actually
13642 illegal according to the DWARF standard. */
13645 complaint (_("missing name for subprogram DIE at %s"),
13646 sect_offset_str (die->sect_off));
13650 /* Ignore functions with missing or invalid low and high pc attributes. */
13651 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13652 <= PC_BOUNDS_INVALID)
13654 attr = dwarf2_attr (die, DW_AT_external, cu);
13655 if (!attr || !DW_UNSND (attr))
13656 complaint (_("cannot get low and high bounds "
13657 "for subprogram DIE at %s"),
13658 sect_offset_str (die->sect_off));
13662 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13663 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13665 /* If we have any template arguments, then we must allocate a
13666 different sort of symbol. */
13667 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13669 if (child_die->tag == DW_TAG_template_type_param
13670 || child_die->tag == DW_TAG_template_value_param)
13672 templ_func = allocate_template_symbol (objfile);
13673 templ_func->subclass = SYMBOL_TEMPLATE;
13678 newobj = cu->get_builder ()->push_context (0, lowpc);
13679 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13680 (struct symbol *) templ_func);
13682 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13683 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13686 /* If there is a location expression for DW_AT_frame_base, record
13688 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13690 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13692 /* If there is a location for the static link, record it. */
13693 newobj->static_link = NULL;
13694 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13697 newobj->static_link
13698 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13699 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13700 dwarf2_per_cu_addr_type (cu->per_cu));
13703 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13705 if (die->child != NULL)
13707 child_die = die->child;
13708 while (child_die && child_die->tag)
13710 if (child_die->tag == DW_TAG_template_type_param
13711 || child_die->tag == DW_TAG_template_value_param)
13713 struct symbol *arg = new_symbol (child_die, NULL, cu);
13716 template_args.push_back (arg);
13719 process_die (child_die, cu);
13720 child_die = sibling_die (child_die);
13724 inherit_abstract_dies (die, cu);
13726 /* If we have a DW_AT_specification, we might need to import using
13727 directives from the context of the specification DIE. See the
13728 comment in determine_prefix. */
13729 if (cu->language == language_cplus
13730 && dwarf2_attr (die, DW_AT_specification, cu))
13732 struct dwarf2_cu *spec_cu = cu;
13733 struct die_info *spec_die = die_specification (die, &spec_cu);
13737 child_die = spec_die->child;
13738 while (child_die && child_die->tag)
13740 if (child_die->tag == DW_TAG_imported_module)
13741 process_die (child_die, spec_cu);
13742 child_die = sibling_die (child_die);
13745 /* In some cases, GCC generates specification DIEs that
13746 themselves contain DW_AT_specification attributes. */
13747 spec_die = die_specification (spec_die, &spec_cu);
13751 struct context_stack cstk = cu->get_builder ()->pop_context ();
13752 /* Make a block for the local symbols within. */
13753 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13754 cstk.static_link, lowpc, highpc);
13756 /* For C++, set the block's scope. */
13757 if ((cu->language == language_cplus
13758 || cu->language == language_fortran
13759 || cu->language == language_d
13760 || cu->language == language_rust)
13761 && cu->processing_has_namespace_info)
13762 block_set_scope (block, determine_prefix (die, cu),
13763 &objfile->objfile_obstack);
13765 /* If we have address ranges, record them. */
13766 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13768 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13770 /* Attach template arguments to function. */
13771 if (!template_args.empty ())
13773 gdb_assert (templ_func != NULL);
13775 templ_func->n_template_arguments = template_args.size ();
13776 templ_func->template_arguments
13777 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13778 templ_func->n_template_arguments);
13779 memcpy (templ_func->template_arguments,
13780 template_args.data (),
13781 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13783 /* Make sure that the symtab is set on the new symbols. Even
13784 though they don't appear in this symtab directly, other parts
13785 of gdb assume that symbols do, and this is reasonably
13787 for (symbol *sym : template_args)
13788 symbol_set_symtab (sym, symbol_symtab (templ_func));
13791 /* In C++, we can have functions nested inside functions (e.g., when
13792 a function declares a class that has methods). This means that
13793 when we finish processing a function scope, we may need to go
13794 back to building a containing block's symbol lists. */
13795 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13796 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13798 /* If we've finished processing a top-level function, subsequent
13799 symbols go in the file symbol list. */
13800 if (cu->get_builder ()->outermost_context_p ())
13801 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13804 /* Process all the DIES contained within a lexical block scope. Start
13805 a new scope, process the dies, and then close the scope. */
13808 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13811 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13812 CORE_ADDR lowpc, highpc;
13813 struct die_info *child_die;
13814 CORE_ADDR baseaddr;
13816 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13818 /* Ignore blocks with missing or invalid low and high pc attributes. */
13819 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13820 as multiple lexical blocks? Handling children in a sane way would
13821 be nasty. Might be easier to properly extend generic blocks to
13822 describe ranges. */
13823 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13825 case PC_BOUNDS_NOT_PRESENT:
13826 /* DW_TAG_lexical_block has no attributes, process its children as if
13827 there was no wrapping by that DW_TAG_lexical_block.
13828 GCC does no longer produces such DWARF since GCC r224161. */
13829 for (child_die = die->child;
13830 child_die != NULL && child_die->tag;
13831 child_die = sibling_die (child_die))
13832 process_die (child_die, cu);
13834 case PC_BOUNDS_INVALID:
13837 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13838 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13840 cu->get_builder ()->push_context (0, lowpc);
13841 if (die->child != NULL)
13843 child_die = die->child;
13844 while (child_die && child_die->tag)
13846 process_die (child_die, cu);
13847 child_die = sibling_die (child_die);
13850 inherit_abstract_dies (die, cu);
13851 struct context_stack cstk = cu->get_builder ()->pop_context ();
13853 if (*cu->get_builder ()->get_local_symbols () != NULL
13854 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13856 struct block *block
13857 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13858 cstk.start_addr, highpc);
13860 /* Note that recording ranges after traversing children, as we
13861 do here, means that recording a parent's ranges entails
13862 walking across all its children's ranges as they appear in
13863 the address map, which is quadratic behavior.
13865 It would be nicer to record the parent's ranges before
13866 traversing its children, simply overriding whatever you find
13867 there. But since we don't even decide whether to create a
13868 block until after we've traversed its children, that's hard
13870 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13872 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13873 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13876 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13879 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13881 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13882 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13883 CORE_ADDR pc, baseaddr;
13884 struct attribute *attr;
13885 struct call_site *call_site, call_site_local;
13888 struct die_info *child_die;
13890 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13892 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13895 /* This was a pre-DWARF-5 GNU extension alias
13896 for DW_AT_call_return_pc. */
13897 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13901 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13902 "DIE %s [in module %s]"),
13903 sect_offset_str (die->sect_off), objfile_name (objfile));
13906 pc = attr_value_as_address (attr) + baseaddr;
13907 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13909 if (cu->call_site_htab == NULL)
13910 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13911 NULL, &objfile->objfile_obstack,
13912 hashtab_obstack_allocate, NULL);
13913 call_site_local.pc = pc;
13914 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13917 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13918 "DIE %s [in module %s]"),
13919 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13920 objfile_name (objfile));
13924 /* Count parameters at the caller. */
13927 for (child_die = die->child; child_die && child_die->tag;
13928 child_die = sibling_die (child_die))
13930 if (child_die->tag != DW_TAG_call_site_parameter
13931 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13933 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13934 "DW_TAG_call_site child DIE %s [in module %s]"),
13935 child_die->tag, sect_offset_str (child_die->sect_off),
13936 objfile_name (objfile));
13944 = ((struct call_site *)
13945 obstack_alloc (&objfile->objfile_obstack,
13946 sizeof (*call_site)
13947 + (sizeof (*call_site->parameter) * (nparams - 1))));
13949 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13950 call_site->pc = pc;
13952 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13953 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13955 struct die_info *func_die;
13957 /* Skip also over DW_TAG_inlined_subroutine. */
13958 for (func_die = die->parent;
13959 func_die && func_die->tag != DW_TAG_subprogram
13960 && func_die->tag != DW_TAG_subroutine_type;
13961 func_die = func_die->parent);
13963 /* DW_AT_call_all_calls is a superset
13964 of DW_AT_call_all_tail_calls. */
13966 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13967 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13968 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13969 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13971 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13972 not complete. But keep CALL_SITE for look ups via call_site_htab,
13973 both the initial caller containing the real return address PC and
13974 the final callee containing the current PC of a chain of tail
13975 calls do not need to have the tail call list complete. But any
13976 function candidate for a virtual tail call frame searched via
13977 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13978 determined unambiguously. */
13982 struct type *func_type = NULL;
13985 func_type = get_die_type (func_die, cu);
13986 if (func_type != NULL)
13988 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13990 /* Enlist this call site to the function. */
13991 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13992 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13995 complaint (_("Cannot find function owning DW_TAG_call_site "
13996 "DIE %s [in module %s]"),
13997 sect_offset_str (die->sect_off), objfile_name (objfile));
14001 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14003 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14005 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14008 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14009 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14011 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14012 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14013 /* Keep NULL DWARF_BLOCK. */;
14014 else if (attr_form_is_block (attr))
14016 struct dwarf2_locexpr_baton *dlbaton;
14018 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14019 dlbaton->data = DW_BLOCK (attr)->data;
14020 dlbaton->size = DW_BLOCK (attr)->size;
14021 dlbaton->per_cu = cu->per_cu;
14023 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14025 else if (attr_form_is_ref (attr))
14027 struct dwarf2_cu *target_cu = cu;
14028 struct die_info *target_die;
14030 target_die = follow_die_ref (die, attr, &target_cu);
14031 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14032 if (die_is_declaration (target_die, target_cu))
14034 const char *target_physname;
14036 /* Prefer the mangled name; otherwise compute the demangled one. */
14037 target_physname = dw2_linkage_name (target_die, target_cu);
14038 if (target_physname == NULL)
14039 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14040 if (target_physname == NULL)
14041 complaint (_("DW_AT_call_target target DIE has invalid "
14042 "physname, for referencing DIE %s [in module %s]"),
14043 sect_offset_str (die->sect_off), objfile_name (objfile));
14045 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14051 /* DW_AT_entry_pc should be preferred. */
14052 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14053 <= PC_BOUNDS_INVALID)
14054 complaint (_("DW_AT_call_target target DIE has invalid "
14055 "low pc, for referencing DIE %s [in module %s]"),
14056 sect_offset_str (die->sect_off), objfile_name (objfile));
14059 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14060 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14065 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14066 "block nor reference, for DIE %s [in module %s]"),
14067 sect_offset_str (die->sect_off), objfile_name (objfile));
14069 call_site->per_cu = cu->per_cu;
14071 for (child_die = die->child;
14072 child_die && child_die->tag;
14073 child_die = sibling_die (child_die))
14075 struct call_site_parameter *parameter;
14076 struct attribute *loc, *origin;
14078 if (child_die->tag != DW_TAG_call_site_parameter
14079 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14081 /* Already printed the complaint above. */
14085 gdb_assert (call_site->parameter_count < nparams);
14086 parameter = &call_site->parameter[call_site->parameter_count];
14088 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14089 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14090 register is contained in DW_AT_call_value. */
14092 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14093 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14094 if (origin == NULL)
14096 /* This was a pre-DWARF-5 GNU extension alias
14097 for DW_AT_call_parameter. */
14098 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14100 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14102 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14104 sect_offset sect_off
14105 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14106 if (!offset_in_cu_p (&cu->header, sect_off))
14108 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14109 binding can be done only inside one CU. Such referenced DIE
14110 therefore cannot be even moved to DW_TAG_partial_unit. */
14111 complaint (_("DW_AT_call_parameter offset is not in CU for "
14112 "DW_TAG_call_site child DIE %s [in module %s]"),
14113 sect_offset_str (child_die->sect_off),
14114 objfile_name (objfile));
14117 parameter->u.param_cu_off
14118 = (cu_offset) (sect_off - cu->header.sect_off);
14120 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14122 complaint (_("No DW_FORM_block* DW_AT_location for "
14123 "DW_TAG_call_site child DIE %s [in module %s]"),
14124 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14129 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14130 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14131 if (parameter->u.dwarf_reg != -1)
14132 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14133 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14134 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14135 ¶meter->u.fb_offset))
14136 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14139 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14140 "for DW_FORM_block* DW_AT_location is supported for "
14141 "DW_TAG_call_site child DIE %s "
14143 sect_offset_str (child_die->sect_off),
14144 objfile_name (objfile));
14149 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14151 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14152 if (!attr_form_is_block (attr))
14154 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14155 "DW_TAG_call_site child DIE %s [in module %s]"),
14156 sect_offset_str (child_die->sect_off),
14157 objfile_name (objfile));
14160 parameter->value = DW_BLOCK (attr)->data;
14161 parameter->value_size = DW_BLOCK (attr)->size;
14163 /* Parameters are not pre-cleared by memset above. */
14164 parameter->data_value = NULL;
14165 parameter->data_value_size = 0;
14166 call_site->parameter_count++;
14168 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14170 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14173 if (!attr_form_is_block (attr))
14174 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14175 "DW_TAG_call_site child DIE %s [in module %s]"),
14176 sect_offset_str (child_die->sect_off),
14177 objfile_name (objfile));
14180 parameter->data_value = DW_BLOCK (attr)->data;
14181 parameter->data_value_size = DW_BLOCK (attr)->size;
14187 /* Helper function for read_variable. If DIE represents a virtual
14188 table, then return the type of the concrete object that is
14189 associated with the virtual table. Otherwise, return NULL. */
14191 static struct type *
14192 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14194 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14198 /* Find the type DIE. */
14199 struct die_info *type_die = NULL;
14200 struct dwarf2_cu *type_cu = cu;
14202 if (attr_form_is_ref (attr))
14203 type_die = follow_die_ref (die, attr, &type_cu);
14204 if (type_die == NULL)
14207 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14209 return die_containing_type (type_die, type_cu);
14212 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14215 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14217 struct rust_vtable_symbol *storage = NULL;
14219 if (cu->language == language_rust)
14221 struct type *containing_type = rust_containing_type (die, cu);
14223 if (containing_type != NULL)
14225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14227 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14228 struct rust_vtable_symbol);
14229 initialize_objfile_symbol (storage);
14230 storage->concrete_type = containing_type;
14231 storage->subclass = SYMBOL_RUST_VTABLE;
14235 struct symbol *res = new_symbol (die, NULL, cu, storage);
14236 struct attribute *abstract_origin
14237 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14238 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14239 if (res == NULL && loc && abstract_origin)
14241 /* We have a variable without a name, but with a location and an abstract
14242 origin. This may be a concrete instance of an abstract variable
14243 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14245 struct dwarf2_cu *origin_cu = cu;
14246 struct die_info *origin_die
14247 = follow_die_ref (die, abstract_origin, &origin_cu);
14248 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14249 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14253 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14254 reading .debug_rnglists.
14255 Callback's type should be:
14256 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14257 Return true if the attributes are present and valid, otherwise,
14260 template <typename Callback>
14262 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14263 Callback &&callback)
14265 struct dwarf2_per_objfile *dwarf2_per_objfile
14266 = cu->per_cu->dwarf2_per_objfile;
14267 struct objfile *objfile = dwarf2_per_objfile->objfile;
14268 bfd *obfd = objfile->obfd;
14269 /* Base address selection entry. */
14272 const gdb_byte *buffer;
14273 CORE_ADDR baseaddr;
14274 bool overflow = false;
14276 found_base = cu->base_known;
14277 base = cu->base_address;
14279 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14280 if (offset >= dwarf2_per_objfile->rnglists.size)
14282 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14286 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14288 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14292 /* Initialize it due to a false compiler warning. */
14293 CORE_ADDR range_beginning = 0, range_end = 0;
14294 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14295 + dwarf2_per_objfile->rnglists.size);
14296 unsigned int bytes_read;
14298 if (buffer == buf_end)
14303 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14306 case DW_RLE_end_of_list:
14308 case DW_RLE_base_address:
14309 if (buffer + cu->header.addr_size > buf_end)
14314 base = read_address (obfd, buffer, cu, &bytes_read);
14316 buffer += bytes_read;
14318 case DW_RLE_start_length:
14319 if (buffer + cu->header.addr_size > buf_end)
14324 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14325 buffer += bytes_read;
14326 range_end = (range_beginning
14327 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14328 buffer += bytes_read;
14329 if (buffer > buf_end)
14335 case DW_RLE_offset_pair:
14336 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14337 buffer += bytes_read;
14338 if (buffer > buf_end)
14343 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14344 buffer += bytes_read;
14345 if (buffer > buf_end)
14351 case DW_RLE_start_end:
14352 if (buffer + 2 * cu->header.addr_size > buf_end)
14357 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14358 buffer += bytes_read;
14359 range_end = read_address (obfd, buffer, cu, &bytes_read);
14360 buffer += bytes_read;
14363 complaint (_("Invalid .debug_rnglists data (no base address)"));
14366 if (rlet == DW_RLE_end_of_list || overflow)
14368 if (rlet == DW_RLE_base_address)
14373 /* We have no valid base address for the ranges
14375 complaint (_("Invalid .debug_rnglists data (no base address)"));
14379 if (range_beginning > range_end)
14381 /* Inverted range entries are invalid. */
14382 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14386 /* Empty range entries have no effect. */
14387 if (range_beginning == range_end)
14390 range_beginning += base;
14393 /* A not-uncommon case of bad debug info.
14394 Don't pollute the addrmap with bad data. */
14395 if (range_beginning + baseaddr == 0
14396 && !dwarf2_per_objfile->has_section_at_zero)
14398 complaint (_(".debug_rnglists entry has start address of zero"
14399 " [in module %s]"), objfile_name (objfile));
14403 callback (range_beginning, range_end);
14408 complaint (_("Offset %d is not terminated "
14409 "for DW_AT_ranges attribute"),
14417 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14418 Callback's type should be:
14419 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14420 Return 1 if the attributes are present and valid, otherwise, return 0. */
14422 template <typename Callback>
14424 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14425 Callback &&callback)
14427 struct dwarf2_per_objfile *dwarf2_per_objfile
14428 = cu->per_cu->dwarf2_per_objfile;
14429 struct objfile *objfile = dwarf2_per_objfile->objfile;
14430 struct comp_unit_head *cu_header = &cu->header;
14431 bfd *obfd = objfile->obfd;
14432 unsigned int addr_size = cu_header->addr_size;
14433 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14434 /* Base address selection entry. */
14437 unsigned int dummy;
14438 const gdb_byte *buffer;
14439 CORE_ADDR baseaddr;
14441 if (cu_header->version >= 5)
14442 return dwarf2_rnglists_process (offset, cu, callback);
14444 found_base = cu->base_known;
14445 base = cu->base_address;
14447 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14448 if (offset >= dwarf2_per_objfile->ranges.size)
14450 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14454 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14456 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14460 CORE_ADDR range_beginning, range_end;
14462 range_beginning = read_address (obfd, buffer, cu, &dummy);
14463 buffer += addr_size;
14464 range_end = read_address (obfd, buffer, cu, &dummy);
14465 buffer += addr_size;
14466 offset += 2 * addr_size;
14468 /* An end of list marker is a pair of zero addresses. */
14469 if (range_beginning == 0 && range_end == 0)
14470 /* Found the end of list entry. */
14473 /* Each base address selection entry is a pair of 2 values.
14474 The first is the largest possible address, the second is
14475 the base address. Check for a base address here. */
14476 if ((range_beginning & mask) == mask)
14478 /* If we found the largest possible address, then we already
14479 have the base address in range_end. */
14487 /* We have no valid base address for the ranges
14489 complaint (_("Invalid .debug_ranges data (no base address)"));
14493 if (range_beginning > range_end)
14495 /* Inverted range entries are invalid. */
14496 complaint (_("Invalid .debug_ranges data (inverted range)"));
14500 /* Empty range entries have no effect. */
14501 if (range_beginning == range_end)
14504 range_beginning += base;
14507 /* A not-uncommon case of bad debug info.
14508 Don't pollute the addrmap with bad data. */
14509 if (range_beginning + baseaddr == 0
14510 && !dwarf2_per_objfile->has_section_at_zero)
14512 complaint (_(".debug_ranges entry has start address of zero"
14513 " [in module %s]"), objfile_name (objfile));
14517 callback (range_beginning, range_end);
14523 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14524 Return 1 if the attributes are present and valid, otherwise, return 0.
14525 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14528 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14529 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14530 struct partial_symtab *ranges_pst)
14532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14533 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14534 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14535 SECT_OFF_TEXT (objfile));
14538 CORE_ADDR high = 0;
14541 retval = dwarf2_ranges_process (offset, cu,
14542 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14544 if (ranges_pst != NULL)
14549 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14550 range_beginning + baseaddr)
14552 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14553 range_end + baseaddr)
14555 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14556 lowpc, highpc - 1, ranges_pst);
14559 /* FIXME: This is recording everything as a low-high
14560 segment of consecutive addresses. We should have a
14561 data structure for discontiguous block ranges
14565 low = range_beginning;
14571 if (range_beginning < low)
14572 low = range_beginning;
14573 if (range_end > high)
14581 /* If the first entry is an end-of-list marker, the range
14582 describes an empty scope, i.e. no instructions. */
14588 *high_return = high;
14592 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14593 definition for the return value. *LOWPC and *HIGHPC are set iff
14594 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14596 static enum pc_bounds_kind
14597 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14598 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14599 struct partial_symtab *pst)
14601 struct dwarf2_per_objfile *dwarf2_per_objfile
14602 = cu->per_cu->dwarf2_per_objfile;
14603 struct attribute *attr;
14604 struct attribute *attr_high;
14606 CORE_ADDR high = 0;
14607 enum pc_bounds_kind ret;
14609 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14612 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14615 low = attr_value_as_address (attr);
14616 high = attr_value_as_address (attr_high);
14617 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14621 /* Found high w/o low attribute. */
14622 return PC_BOUNDS_INVALID;
14624 /* Found consecutive range of addresses. */
14625 ret = PC_BOUNDS_HIGH_LOW;
14629 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14632 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14633 We take advantage of the fact that DW_AT_ranges does not appear
14634 in DW_TAG_compile_unit of DWO files. */
14635 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14636 unsigned int ranges_offset = (DW_UNSND (attr)
14637 + (need_ranges_base
14641 /* Value of the DW_AT_ranges attribute is the offset in the
14642 .debug_ranges section. */
14643 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14644 return PC_BOUNDS_INVALID;
14645 /* Found discontinuous range of addresses. */
14646 ret = PC_BOUNDS_RANGES;
14649 return PC_BOUNDS_NOT_PRESENT;
14652 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14654 return PC_BOUNDS_INVALID;
14656 /* When using the GNU linker, .gnu.linkonce. sections are used to
14657 eliminate duplicate copies of functions and vtables and such.
14658 The linker will arbitrarily choose one and discard the others.
14659 The AT_*_pc values for such functions refer to local labels in
14660 these sections. If the section from that file was discarded, the
14661 labels are not in the output, so the relocs get a value of 0.
14662 If this is a discarded function, mark the pc bounds as invalid,
14663 so that GDB will ignore it. */
14664 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14665 return PC_BOUNDS_INVALID;
14673 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14674 its low and high PC addresses. Do nothing if these addresses could not
14675 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14676 and HIGHPC to the high address if greater than HIGHPC. */
14679 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14680 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14681 struct dwarf2_cu *cu)
14683 CORE_ADDR low, high;
14684 struct die_info *child = die->child;
14686 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14688 *lowpc = std::min (*lowpc, low);
14689 *highpc = std::max (*highpc, high);
14692 /* If the language does not allow nested subprograms (either inside
14693 subprograms or lexical blocks), we're done. */
14694 if (cu->language != language_ada)
14697 /* Check all the children of the given DIE. If it contains nested
14698 subprograms, then check their pc bounds. Likewise, we need to
14699 check lexical blocks as well, as they may also contain subprogram
14701 while (child && child->tag)
14703 if (child->tag == DW_TAG_subprogram
14704 || child->tag == DW_TAG_lexical_block)
14705 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14706 child = sibling_die (child);
14710 /* Get the low and high pc's represented by the scope DIE, and store
14711 them in *LOWPC and *HIGHPC. If the correct values can't be
14712 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14715 get_scope_pc_bounds (struct die_info *die,
14716 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14717 struct dwarf2_cu *cu)
14719 CORE_ADDR best_low = (CORE_ADDR) -1;
14720 CORE_ADDR best_high = (CORE_ADDR) 0;
14721 CORE_ADDR current_low, current_high;
14723 if (dwarf2_get_pc_bounds (die, ¤t_low, ¤t_high, cu, NULL)
14724 >= PC_BOUNDS_RANGES)
14726 best_low = current_low;
14727 best_high = current_high;
14731 struct die_info *child = die->child;
14733 while (child && child->tag)
14735 switch (child->tag) {
14736 case DW_TAG_subprogram:
14737 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14739 case DW_TAG_namespace:
14740 case DW_TAG_module:
14741 /* FIXME: carlton/2004-01-16: Should we do this for
14742 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14743 that current GCC's always emit the DIEs corresponding
14744 to definitions of methods of classes as children of a
14745 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14746 the DIEs giving the declarations, which could be
14747 anywhere). But I don't see any reason why the
14748 standards says that they have to be there. */
14749 get_scope_pc_bounds (child, ¤t_low, ¤t_high, cu);
14751 if (current_low != ((CORE_ADDR) -1))
14753 best_low = std::min (best_low, current_low);
14754 best_high = std::max (best_high, current_high);
14762 child = sibling_die (child);
14767 *highpc = best_high;
14770 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14774 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14775 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14777 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14778 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14779 struct attribute *attr;
14780 struct attribute *attr_high;
14782 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14785 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14788 CORE_ADDR low = attr_value_as_address (attr);
14789 CORE_ADDR high = attr_value_as_address (attr_high);
14791 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14794 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14795 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14796 cu->get_builder ()->record_block_range (block, low, high - 1);
14800 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14803 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14804 We take advantage of the fact that DW_AT_ranges does not appear
14805 in DW_TAG_compile_unit of DWO files. */
14806 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14808 /* The value of the DW_AT_ranges attribute is the offset of the
14809 address range list in the .debug_ranges section. */
14810 unsigned long offset = (DW_UNSND (attr)
14811 + (need_ranges_base ? cu->ranges_base : 0));
14813 std::vector<blockrange> blockvec;
14814 dwarf2_ranges_process (offset, cu,
14815 [&] (CORE_ADDR start, CORE_ADDR end)
14819 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14820 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14821 cu->get_builder ()->record_block_range (block, start, end - 1);
14822 blockvec.emplace_back (start, end);
14825 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14829 /* Check whether the producer field indicates either of GCC < 4.6, or the
14830 Intel C/C++ compiler, and cache the result in CU. */
14833 check_producer (struct dwarf2_cu *cu)
14837 if (cu->producer == NULL)
14839 /* For unknown compilers expect their behavior is DWARF version
14842 GCC started to support .debug_types sections by -gdwarf-4 since
14843 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14844 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14845 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14846 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14848 else if (producer_is_gcc (cu->producer, &major, &minor))
14850 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14851 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14853 else if (producer_is_icc (cu->producer, &major, &minor))
14855 cu->producer_is_icc = true;
14856 cu->producer_is_icc_lt_14 = major < 14;
14858 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14859 cu->producer_is_codewarrior = true;
14862 /* For other non-GCC compilers, expect their behavior is DWARF version
14866 cu->checked_producer = true;
14869 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14870 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14871 during 4.6.0 experimental. */
14874 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14876 if (!cu->checked_producer)
14877 check_producer (cu);
14879 return cu->producer_is_gxx_lt_4_6;
14883 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14884 with incorrect is_stmt attributes. */
14887 producer_is_codewarrior (struct dwarf2_cu *cu)
14889 if (!cu->checked_producer)
14890 check_producer (cu);
14892 return cu->producer_is_codewarrior;
14895 /* Return the default accessibility type if it is not overriden by
14896 DW_AT_accessibility. */
14898 static enum dwarf_access_attribute
14899 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14901 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14903 /* The default DWARF 2 accessibility for members is public, the default
14904 accessibility for inheritance is private. */
14906 if (die->tag != DW_TAG_inheritance)
14907 return DW_ACCESS_public;
14909 return DW_ACCESS_private;
14913 /* DWARF 3+ defines the default accessibility a different way. The same
14914 rules apply now for DW_TAG_inheritance as for the members and it only
14915 depends on the container kind. */
14917 if (die->parent->tag == DW_TAG_class_type)
14918 return DW_ACCESS_private;
14920 return DW_ACCESS_public;
14924 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14925 offset. If the attribute was not found return 0, otherwise return
14926 1. If it was found but could not properly be handled, set *OFFSET
14930 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14933 struct attribute *attr;
14935 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14940 /* Note that we do not check for a section offset first here.
14941 This is because DW_AT_data_member_location is new in DWARF 4,
14942 so if we see it, we can assume that a constant form is really
14943 a constant and not a section offset. */
14944 if (attr_form_is_constant (attr))
14945 *offset = dwarf2_get_attr_constant_value (attr, 0);
14946 else if (attr_form_is_section_offset (attr))
14947 dwarf2_complex_location_expr_complaint ();
14948 else if (attr_form_is_block (attr))
14949 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14951 dwarf2_complex_location_expr_complaint ();
14959 /* Add an aggregate field to the field list. */
14962 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14963 struct dwarf2_cu *cu)
14965 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14966 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14967 struct nextfield *new_field;
14968 struct attribute *attr;
14970 const char *fieldname = "";
14972 if (die->tag == DW_TAG_inheritance)
14974 fip->baseclasses.emplace_back ();
14975 new_field = &fip->baseclasses.back ();
14979 fip->fields.emplace_back ();
14980 new_field = &fip->fields.back ();
14985 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14987 new_field->accessibility = DW_UNSND (attr);
14989 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14990 if (new_field->accessibility != DW_ACCESS_public)
14991 fip->non_public_fields = 1;
14993 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14995 new_field->virtuality = DW_UNSND (attr);
14997 new_field->virtuality = DW_VIRTUALITY_none;
14999 fp = &new_field->field;
15001 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15005 /* Data member other than a C++ static data member. */
15007 /* Get type of field. */
15008 fp->type = die_type (die, cu);
15010 SET_FIELD_BITPOS (*fp, 0);
15012 /* Get bit size of field (zero if none). */
15013 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15016 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15020 FIELD_BITSIZE (*fp) = 0;
15023 /* Get bit offset of field. */
15024 if (handle_data_member_location (die, cu, &offset))
15025 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15026 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15029 if (gdbarch_bits_big_endian (gdbarch))
15031 /* For big endian bits, the DW_AT_bit_offset gives the
15032 additional bit offset from the MSB of the containing
15033 anonymous object to the MSB of the field. We don't
15034 have to do anything special since we don't need to
15035 know the size of the anonymous object. */
15036 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15040 /* For little endian bits, compute the bit offset to the
15041 MSB of the anonymous object, subtract off the number of
15042 bits from the MSB of the field to the MSB of the
15043 object, and then subtract off the number of bits of
15044 the field itself. The result is the bit offset of
15045 the LSB of the field. */
15046 int anonymous_size;
15047 int bit_offset = DW_UNSND (attr);
15049 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15052 /* The size of the anonymous object containing
15053 the bit field is explicit, so use the
15054 indicated size (in bytes). */
15055 anonymous_size = DW_UNSND (attr);
15059 /* The size of the anonymous object containing
15060 the bit field must be inferred from the type
15061 attribute of the data member containing the
15063 anonymous_size = TYPE_LENGTH (fp->type);
15065 SET_FIELD_BITPOS (*fp,
15066 (FIELD_BITPOS (*fp)
15067 + anonymous_size * bits_per_byte
15068 - bit_offset - FIELD_BITSIZE (*fp)));
15071 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15073 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15074 + dwarf2_get_attr_constant_value (attr, 0)));
15076 /* Get name of field. */
15077 fieldname = dwarf2_name (die, cu);
15078 if (fieldname == NULL)
15081 /* The name is already allocated along with this objfile, so we don't
15082 need to duplicate it for the type. */
15083 fp->name = fieldname;
15085 /* Change accessibility for artificial fields (e.g. virtual table
15086 pointer or virtual base class pointer) to private. */
15087 if (dwarf2_attr (die, DW_AT_artificial, cu))
15089 FIELD_ARTIFICIAL (*fp) = 1;
15090 new_field->accessibility = DW_ACCESS_private;
15091 fip->non_public_fields = 1;
15094 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15096 /* C++ static member. */
15098 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15099 is a declaration, but all versions of G++ as of this writing
15100 (so through at least 3.2.1) incorrectly generate
15101 DW_TAG_variable tags. */
15103 const char *physname;
15105 /* Get name of field. */
15106 fieldname = dwarf2_name (die, cu);
15107 if (fieldname == NULL)
15110 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15112 /* Only create a symbol if this is an external value.
15113 new_symbol checks this and puts the value in the global symbol
15114 table, which we want. If it is not external, new_symbol
15115 will try to put the value in cu->list_in_scope which is wrong. */
15116 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15118 /* A static const member, not much different than an enum as far as
15119 we're concerned, except that we can support more types. */
15120 new_symbol (die, NULL, cu);
15123 /* Get physical name. */
15124 physname = dwarf2_physname (fieldname, die, cu);
15126 /* The name is already allocated along with this objfile, so we don't
15127 need to duplicate it for the type. */
15128 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15129 FIELD_TYPE (*fp) = die_type (die, cu);
15130 FIELD_NAME (*fp) = fieldname;
15132 else if (die->tag == DW_TAG_inheritance)
15136 /* C++ base class field. */
15137 if (handle_data_member_location (die, cu, &offset))
15138 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15139 FIELD_BITSIZE (*fp) = 0;
15140 FIELD_TYPE (*fp) = die_type (die, cu);
15141 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15143 else if (die->tag == DW_TAG_variant_part)
15145 /* process_structure_scope will treat this DIE as a union. */
15146 process_structure_scope (die, cu);
15148 /* The variant part is relative to the start of the enclosing
15150 SET_FIELD_BITPOS (*fp, 0);
15151 fp->type = get_die_type (die, cu);
15152 fp->artificial = 1;
15153 fp->name = "<<variant>>";
15155 /* Normally a DW_TAG_variant_part won't have a size, but our
15156 representation requires one, so set it to the maximum of the
15158 if (TYPE_LENGTH (fp->type) == 0)
15161 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15162 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15163 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15164 TYPE_LENGTH (fp->type) = max;
15168 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15171 /* Can the type given by DIE define another type? */
15174 type_can_define_types (const struct die_info *die)
15178 case DW_TAG_typedef:
15179 case DW_TAG_class_type:
15180 case DW_TAG_structure_type:
15181 case DW_TAG_union_type:
15182 case DW_TAG_enumeration_type:
15190 /* Add a type definition defined in the scope of the FIP's class. */
15193 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15194 struct dwarf2_cu *cu)
15196 struct decl_field fp;
15197 memset (&fp, 0, sizeof (fp));
15199 gdb_assert (type_can_define_types (die));
15201 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15202 fp.name = dwarf2_name (die, cu);
15203 fp.type = read_type_die (die, cu);
15205 /* Save accessibility. */
15206 enum dwarf_access_attribute accessibility;
15207 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15209 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15211 accessibility = dwarf2_default_access_attribute (die, cu);
15212 switch (accessibility)
15214 case DW_ACCESS_public:
15215 /* The assumed value if neither private nor protected. */
15217 case DW_ACCESS_private:
15220 case DW_ACCESS_protected:
15221 fp.is_protected = 1;
15224 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15227 if (die->tag == DW_TAG_typedef)
15228 fip->typedef_field_list.push_back (fp);
15230 fip->nested_types_list.push_back (fp);
15233 /* Create the vector of fields, and attach it to the type. */
15236 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15237 struct dwarf2_cu *cu)
15239 int nfields = fip->nfields;
15241 /* Record the field count, allocate space for the array of fields,
15242 and create blank accessibility bitfields if necessary. */
15243 TYPE_NFIELDS (type) = nfields;
15244 TYPE_FIELDS (type) = (struct field *)
15245 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15247 if (fip->non_public_fields && cu->language != language_ada)
15249 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15251 TYPE_FIELD_PRIVATE_BITS (type) =
15252 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15253 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15255 TYPE_FIELD_PROTECTED_BITS (type) =
15256 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15257 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15259 TYPE_FIELD_IGNORE_BITS (type) =
15260 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15261 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15264 /* If the type has baseclasses, allocate and clear a bit vector for
15265 TYPE_FIELD_VIRTUAL_BITS. */
15266 if (!fip->baseclasses.empty () && cu->language != language_ada)
15268 int num_bytes = B_BYTES (fip->baseclasses.size ());
15269 unsigned char *pointer;
15271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15272 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15273 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15274 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15275 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15278 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15280 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15282 for (int index = 0; index < nfields; ++index)
15284 struct nextfield &field = fip->fields[index];
15286 if (field.variant.is_discriminant)
15287 di->discriminant_index = index;
15288 else if (field.variant.default_branch)
15289 di->default_index = index;
15291 di->discriminants[index] = field.variant.discriminant_value;
15295 /* Copy the saved-up fields into the field vector. */
15296 for (int i = 0; i < nfields; ++i)
15298 struct nextfield &field
15299 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15300 : fip->fields[i - fip->baseclasses.size ()]);
15302 TYPE_FIELD (type, i) = field.field;
15303 switch (field.accessibility)
15305 case DW_ACCESS_private:
15306 if (cu->language != language_ada)
15307 SET_TYPE_FIELD_PRIVATE (type, i);
15310 case DW_ACCESS_protected:
15311 if (cu->language != language_ada)
15312 SET_TYPE_FIELD_PROTECTED (type, i);
15315 case DW_ACCESS_public:
15319 /* Unknown accessibility. Complain and treat it as public. */
15321 complaint (_("unsupported accessibility %d"),
15322 field.accessibility);
15326 if (i < fip->baseclasses.size ())
15328 switch (field.virtuality)
15330 case DW_VIRTUALITY_virtual:
15331 case DW_VIRTUALITY_pure_virtual:
15332 if (cu->language == language_ada)
15333 error (_("unexpected virtuality in component of Ada type"));
15334 SET_TYPE_FIELD_VIRTUAL (type, i);
15341 /* Return true if this member function is a constructor, false
15345 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15347 const char *fieldname;
15348 const char *type_name;
15351 if (die->parent == NULL)
15354 if (die->parent->tag != DW_TAG_structure_type
15355 && die->parent->tag != DW_TAG_union_type
15356 && die->parent->tag != DW_TAG_class_type)
15359 fieldname = dwarf2_name (die, cu);
15360 type_name = dwarf2_name (die->parent, cu);
15361 if (fieldname == NULL || type_name == NULL)
15364 len = strlen (fieldname);
15365 return (strncmp (fieldname, type_name, len) == 0
15366 && (type_name[len] == '\0' || type_name[len] == '<'));
15369 /* Add a member function to the proper fieldlist. */
15372 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15373 struct type *type, struct dwarf2_cu *cu)
15375 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15376 struct attribute *attr;
15378 struct fnfieldlist *flp = nullptr;
15379 struct fn_field *fnp;
15380 const char *fieldname;
15381 struct type *this_type;
15382 enum dwarf_access_attribute accessibility;
15384 if (cu->language == language_ada)
15385 error (_("unexpected member function in Ada type"));
15387 /* Get name of member function. */
15388 fieldname = dwarf2_name (die, cu);
15389 if (fieldname == NULL)
15392 /* Look up member function name in fieldlist. */
15393 for (i = 0; i < fip->fnfieldlists.size (); i++)
15395 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15397 flp = &fip->fnfieldlists[i];
15402 /* Create a new fnfieldlist if necessary. */
15403 if (flp == nullptr)
15405 fip->fnfieldlists.emplace_back ();
15406 flp = &fip->fnfieldlists.back ();
15407 flp->name = fieldname;
15408 i = fip->fnfieldlists.size () - 1;
15411 /* Create a new member function field and add it to the vector of
15413 flp->fnfields.emplace_back ();
15414 fnp = &flp->fnfields.back ();
15416 /* Delay processing of the physname until later. */
15417 if (cu->language == language_cplus)
15418 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15422 const char *physname = dwarf2_physname (fieldname, die, cu);
15423 fnp->physname = physname ? physname : "";
15426 fnp->type = alloc_type (objfile);
15427 this_type = read_type_die (die, cu);
15428 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15430 int nparams = TYPE_NFIELDS (this_type);
15432 /* TYPE is the domain of this method, and THIS_TYPE is the type
15433 of the method itself (TYPE_CODE_METHOD). */
15434 smash_to_method_type (fnp->type, type,
15435 TYPE_TARGET_TYPE (this_type),
15436 TYPE_FIELDS (this_type),
15437 TYPE_NFIELDS (this_type),
15438 TYPE_VARARGS (this_type));
15440 /* Handle static member functions.
15441 Dwarf2 has no clean way to discern C++ static and non-static
15442 member functions. G++ helps GDB by marking the first
15443 parameter for non-static member functions (which is the this
15444 pointer) as artificial. We obtain this information from
15445 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15446 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15447 fnp->voffset = VOFFSET_STATIC;
15450 complaint (_("member function type missing for '%s'"),
15451 dwarf2_full_name (fieldname, die, cu));
15453 /* Get fcontext from DW_AT_containing_type if present. */
15454 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15455 fnp->fcontext = die_containing_type (die, cu);
15457 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15458 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15460 /* Get accessibility. */
15461 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15463 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15465 accessibility = dwarf2_default_access_attribute (die, cu);
15466 switch (accessibility)
15468 case DW_ACCESS_private:
15469 fnp->is_private = 1;
15471 case DW_ACCESS_protected:
15472 fnp->is_protected = 1;
15476 /* Check for artificial methods. */
15477 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15478 if (attr && DW_UNSND (attr) != 0)
15479 fnp->is_artificial = 1;
15481 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15483 /* Get index in virtual function table if it is a virtual member
15484 function. For older versions of GCC, this is an offset in the
15485 appropriate virtual table, as specified by DW_AT_containing_type.
15486 For everyone else, it is an expression to be evaluated relative
15487 to the object address. */
15489 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15492 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15494 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15496 /* Old-style GCC. */
15497 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15499 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15500 || (DW_BLOCK (attr)->size > 1
15501 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15502 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15504 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15505 if ((fnp->voffset % cu->header.addr_size) != 0)
15506 dwarf2_complex_location_expr_complaint ();
15508 fnp->voffset /= cu->header.addr_size;
15512 dwarf2_complex_location_expr_complaint ();
15514 if (!fnp->fcontext)
15516 /* If there is no `this' field and no DW_AT_containing_type,
15517 we cannot actually find a base class context for the
15519 if (TYPE_NFIELDS (this_type) == 0
15520 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15522 complaint (_("cannot determine context for virtual member "
15523 "function \"%s\" (offset %s)"),
15524 fieldname, sect_offset_str (die->sect_off));
15529 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15533 else if (attr_form_is_section_offset (attr))
15535 dwarf2_complex_location_expr_complaint ();
15539 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15545 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15546 if (attr && DW_UNSND (attr))
15548 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15549 complaint (_("Member function \"%s\" (offset %s) is virtual "
15550 "but the vtable offset is not specified"),
15551 fieldname, sect_offset_str (die->sect_off));
15552 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15553 TYPE_CPLUS_DYNAMIC (type) = 1;
15558 /* Create the vector of member function fields, and attach it to the type. */
15561 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15562 struct dwarf2_cu *cu)
15564 if (cu->language == language_ada)
15565 error (_("unexpected member functions in Ada type"));
15567 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15568 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15570 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15572 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15574 struct fnfieldlist &nf = fip->fnfieldlists[i];
15575 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15577 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15578 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15579 fn_flp->fn_fields = (struct fn_field *)
15580 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15582 for (int k = 0; k < nf.fnfields.size (); ++k)
15583 fn_flp->fn_fields[k] = nf.fnfields[k];
15586 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15589 /* Returns non-zero if NAME is the name of a vtable member in CU's
15590 language, zero otherwise. */
15592 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15594 static const char vptr[] = "_vptr";
15596 /* Look for the C++ form of the vtable. */
15597 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15603 /* GCC outputs unnamed structures that are really pointers to member
15604 functions, with the ABI-specified layout. If TYPE describes
15605 such a structure, smash it into a member function type.
15607 GCC shouldn't do this; it should just output pointer to member DIEs.
15608 This is GCC PR debug/28767. */
15611 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15613 struct type *pfn_type, *self_type, *new_type;
15615 /* Check for a structure with no name and two children. */
15616 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15619 /* Check for __pfn and __delta members. */
15620 if (TYPE_FIELD_NAME (type, 0) == NULL
15621 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15622 || TYPE_FIELD_NAME (type, 1) == NULL
15623 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15626 /* Find the type of the method. */
15627 pfn_type = TYPE_FIELD_TYPE (type, 0);
15628 if (pfn_type == NULL
15629 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15630 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15633 /* Look for the "this" argument. */
15634 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15635 if (TYPE_NFIELDS (pfn_type) == 0
15636 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15637 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15640 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15641 new_type = alloc_type (objfile);
15642 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15643 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15644 TYPE_VARARGS (pfn_type));
15645 smash_to_methodptr_type (type, new_type);
15648 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15649 appropriate error checking and issuing complaints if there is a
15653 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15655 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15657 if (attr == nullptr)
15660 if (!attr_form_is_constant (attr))
15662 complaint (_("DW_AT_alignment must have constant form"
15663 " - DIE at %s [in module %s]"),
15664 sect_offset_str (die->sect_off),
15665 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15670 if (attr->form == DW_FORM_sdata)
15672 LONGEST val = DW_SND (attr);
15675 complaint (_("DW_AT_alignment value must not be negative"
15676 " - DIE at %s [in module %s]"),
15677 sect_offset_str (die->sect_off),
15678 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15684 align = DW_UNSND (attr);
15688 complaint (_("DW_AT_alignment value must not be zero"
15689 " - DIE at %s [in module %s]"),
15690 sect_offset_str (die->sect_off),
15691 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15694 if ((align & (align - 1)) != 0)
15696 complaint (_("DW_AT_alignment value must be a power of 2"
15697 " - DIE at %s [in module %s]"),
15698 sect_offset_str (die->sect_off),
15699 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15706 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15707 the alignment for TYPE. */
15710 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15713 if (!set_type_align (type, get_alignment (cu, die)))
15714 complaint (_("DW_AT_alignment value too large"
15715 " - DIE at %s [in module %s]"),
15716 sect_offset_str (die->sect_off),
15717 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15720 /* Called when we find the DIE that starts a structure or union scope
15721 (definition) to create a type for the structure or union. Fill in
15722 the type's name and general properties; the members will not be
15723 processed until process_structure_scope. A symbol table entry for
15724 the type will also not be done until process_structure_scope (assuming
15725 the type has a name).
15727 NOTE: we need to call these functions regardless of whether or not the
15728 DIE has a DW_AT_name attribute, since it might be an anonymous
15729 structure or union. This gets the type entered into our set of
15730 user defined types. */
15732 static struct type *
15733 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15737 struct attribute *attr;
15740 /* If the definition of this type lives in .debug_types, read that type.
15741 Don't follow DW_AT_specification though, that will take us back up
15742 the chain and we want to go down. */
15743 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15746 type = get_DW_AT_signature_type (die, attr, cu);
15748 /* The type's CU may not be the same as CU.
15749 Ensure TYPE is recorded with CU in die_type_hash. */
15750 return set_die_type (die, type, cu);
15753 type = alloc_type (objfile);
15754 INIT_CPLUS_SPECIFIC (type);
15756 name = dwarf2_name (die, cu);
15759 if (cu->language == language_cplus
15760 || cu->language == language_d
15761 || cu->language == language_rust)
15763 const char *full_name = dwarf2_full_name (name, die, cu);
15765 /* dwarf2_full_name might have already finished building the DIE's
15766 type. If so, there is no need to continue. */
15767 if (get_die_type (die, cu) != NULL)
15768 return get_die_type (die, cu);
15770 TYPE_NAME (type) = full_name;
15774 /* The name is already allocated along with this objfile, so
15775 we don't need to duplicate it for the type. */
15776 TYPE_NAME (type) = name;
15780 if (die->tag == DW_TAG_structure_type)
15782 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15784 else if (die->tag == DW_TAG_union_type)
15786 TYPE_CODE (type) = TYPE_CODE_UNION;
15788 else if (die->tag == DW_TAG_variant_part)
15790 TYPE_CODE (type) = TYPE_CODE_UNION;
15791 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15795 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15798 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15799 TYPE_DECLARED_CLASS (type) = 1;
15801 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15804 if (attr_form_is_constant (attr))
15805 TYPE_LENGTH (type) = DW_UNSND (attr);
15808 /* For the moment, dynamic type sizes are not supported
15809 by GDB's struct type. The actual size is determined
15810 on-demand when resolving the type of a given object,
15811 so set the type's length to zero for now. Otherwise,
15812 we record an expression as the length, and that expression
15813 could lead to a very large value, which could eventually
15814 lead to us trying to allocate that much memory when creating
15815 a value of that type. */
15816 TYPE_LENGTH (type) = 0;
15821 TYPE_LENGTH (type) = 0;
15824 maybe_set_alignment (cu, die, type);
15826 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15828 /* ICC<14 does not output the required DW_AT_declaration on
15829 incomplete types, but gives them a size of zero. */
15830 TYPE_STUB (type) = 1;
15833 TYPE_STUB_SUPPORTED (type) = 1;
15835 if (die_is_declaration (die, cu))
15836 TYPE_STUB (type) = 1;
15837 else if (attr == NULL && die->child == NULL
15838 && producer_is_realview (cu->producer))
15839 /* RealView does not output the required DW_AT_declaration
15840 on incomplete types. */
15841 TYPE_STUB (type) = 1;
15843 /* We need to add the type field to the die immediately so we don't
15844 infinitely recurse when dealing with pointers to the structure
15845 type within the structure itself. */
15846 set_die_type (die, type, cu);
15848 /* set_die_type should be already done. */
15849 set_descriptive_type (type, die, cu);
15854 /* A helper for process_structure_scope that handles a single member
15858 handle_struct_member_die (struct die_info *child_die, struct type *type,
15859 struct field_info *fi,
15860 std::vector<struct symbol *> *template_args,
15861 struct dwarf2_cu *cu)
15863 if (child_die->tag == DW_TAG_member
15864 || child_die->tag == DW_TAG_variable
15865 || child_die->tag == DW_TAG_variant_part)
15867 /* NOTE: carlton/2002-11-05: A C++ static data member
15868 should be a DW_TAG_member that is a declaration, but
15869 all versions of G++ as of this writing (so through at
15870 least 3.2.1) incorrectly generate DW_TAG_variable
15871 tags for them instead. */
15872 dwarf2_add_field (fi, child_die, cu);
15874 else if (child_die->tag == DW_TAG_subprogram)
15876 /* Rust doesn't have member functions in the C++ sense.
15877 However, it does emit ordinary functions as children
15878 of a struct DIE. */
15879 if (cu->language == language_rust)
15880 read_func_scope (child_die, cu);
15883 /* C++ member function. */
15884 dwarf2_add_member_fn (fi, child_die, type, cu);
15887 else if (child_die->tag == DW_TAG_inheritance)
15889 /* C++ base class field. */
15890 dwarf2_add_field (fi, child_die, cu);
15892 else if (type_can_define_types (child_die))
15893 dwarf2_add_type_defn (fi, child_die, cu);
15894 else if (child_die->tag == DW_TAG_template_type_param
15895 || child_die->tag == DW_TAG_template_value_param)
15897 struct symbol *arg = new_symbol (child_die, NULL, cu);
15900 template_args->push_back (arg);
15902 else if (child_die->tag == DW_TAG_variant)
15904 /* In a variant we want to get the discriminant and also add a
15905 field for our sole member child. */
15906 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15908 for (die_info *variant_child = child_die->child;
15909 variant_child != NULL;
15910 variant_child = sibling_die (variant_child))
15912 if (variant_child->tag == DW_TAG_member)
15914 handle_struct_member_die (variant_child, type, fi,
15915 template_args, cu);
15916 /* Only handle the one. */
15921 /* We don't handle this but we might as well report it if we see
15923 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15924 complaint (_("DW_AT_discr_list is not supported yet"
15925 " - DIE at %s [in module %s]"),
15926 sect_offset_str (child_die->sect_off),
15927 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15929 /* The first field was just added, so we can stash the
15930 discriminant there. */
15931 gdb_assert (!fi->fields.empty ());
15933 fi->fields.back ().variant.default_branch = true;
15935 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15939 /* Finish creating a structure or union type, including filling in
15940 its members and creating a symbol for it. */
15943 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15946 struct die_info *child_die;
15949 type = get_die_type (die, cu);
15951 type = read_structure_type (die, cu);
15953 /* When reading a DW_TAG_variant_part, we need to notice when we
15954 read the discriminant member, so we can record it later in the
15955 discriminant_info. */
15956 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15957 sect_offset discr_offset;
15958 bool has_template_parameters = false;
15960 if (is_variant_part)
15962 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15965 /* Maybe it's a univariant form, an extension we support.
15966 In this case arrange not to check the offset. */
15967 is_variant_part = false;
15969 else if (attr_form_is_ref (discr))
15971 struct dwarf2_cu *target_cu = cu;
15972 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15974 discr_offset = target_die->sect_off;
15978 complaint (_("DW_AT_discr does not have DIE reference form"
15979 " - DIE at %s [in module %s]"),
15980 sect_offset_str (die->sect_off),
15981 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15982 is_variant_part = false;
15986 if (die->child != NULL && ! die_is_declaration (die, cu))
15988 struct field_info fi;
15989 std::vector<struct symbol *> template_args;
15991 child_die = die->child;
15993 while (child_die && child_die->tag)
15995 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15997 if (is_variant_part && discr_offset == child_die->sect_off)
15998 fi.fields.back ().variant.is_discriminant = true;
16000 child_die = sibling_die (child_die);
16003 /* Attach template arguments to type. */
16004 if (!template_args.empty ())
16006 has_template_parameters = true;
16007 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16008 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16009 TYPE_TEMPLATE_ARGUMENTS (type)
16010 = XOBNEWVEC (&objfile->objfile_obstack,
16012 TYPE_N_TEMPLATE_ARGUMENTS (type));
16013 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16014 template_args.data (),
16015 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16016 * sizeof (struct symbol *)));
16019 /* Attach fields and member functions to the type. */
16021 dwarf2_attach_fields_to_type (&fi, type, cu);
16022 if (!fi.fnfieldlists.empty ())
16024 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16026 /* Get the type which refers to the base class (possibly this
16027 class itself) which contains the vtable pointer for the current
16028 class from the DW_AT_containing_type attribute. This use of
16029 DW_AT_containing_type is a GNU extension. */
16031 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16033 struct type *t = die_containing_type (die, cu);
16035 set_type_vptr_basetype (type, t);
16040 /* Our own class provides vtbl ptr. */
16041 for (i = TYPE_NFIELDS (t) - 1;
16042 i >= TYPE_N_BASECLASSES (t);
16045 const char *fieldname = TYPE_FIELD_NAME (t, i);
16047 if (is_vtable_name (fieldname, cu))
16049 set_type_vptr_fieldno (type, i);
16054 /* Complain if virtual function table field not found. */
16055 if (i < TYPE_N_BASECLASSES (t))
16056 complaint (_("virtual function table pointer "
16057 "not found when defining class '%s'"),
16058 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16062 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16065 else if (cu->producer
16066 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16068 /* The IBM XLC compiler does not provide direct indication
16069 of the containing type, but the vtable pointer is
16070 always named __vfp. */
16074 for (i = TYPE_NFIELDS (type) - 1;
16075 i >= TYPE_N_BASECLASSES (type);
16078 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16080 set_type_vptr_fieldno (type, i);
16081 set_type_vptr_basetype (type, type);
16088 /* Copy fi.typedef_field_list linked list elements content into the
16089 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16090 if (!fi.typedef_field_list.empty ())
16092 int count = fi.typedef_field_list.size ();
16094 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16095 TYPE_TYPEDEF_FIELD_ARRAY (type)
16096 = ((struct decl_field *)
16098 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16099 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16101 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16102 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16105 /* Copy fi.nested_types_list linked list elements content into the
16106 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16107 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16109 int count = fi.nested_types_list.size ();
16111 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16112 TYPE_NESTED_TYPES_ARRAY (type)
16113 = ((struct decl_field *)
16114 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16115 TYPE_NESTED_TYPES_COUNT (type) = count;
16117 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16118 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16122 quirk_gcc_member_function_pointer (type, objfile);
16123 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16124 cu->rust_unions.push_back (type);
16126 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16127 snapshots) has been known to create a die giving a declaration
16128 for a class that has, as a child, a die giving a definition for a
16129 nested class. So we have to process our children even if the
16130 current die is a declaration. Normally, of course, a declaration
16131 won't have any children at all. */
16133 child_die = die->child;
16135 while (child_die != NULL && child_die->tag)
16137 if (child_die->tag == DW_TAG_member
16138 || child_die->tag == DW_TAG_variable
16139 || child_die->tag == DW_TAG_inheritance
16140 || child_die->tag == DW_TAG_template_value_param
16141 || child_die->tag == DW_TAG_template_type_param)
16146 process_die (child_die, cu);
16148 child_die = sibling_die (child_die);
16151 /* Do not consider external references. According to the DWARF standard,
16152 these DIEs are identified by the fact that they have no byte_size
16153 attribute, and a declaration attribute. */
16154 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16155 || !die_is_declaration (die, cu))
16157 struct symbol *sym = new_symbol (die, type, cu);
16159 if (has_template_parameters)
16161 struct symtab *symtab;
16162 if (sym != nullptr)
16163 symtab = symbol_symtab (sym);
16164 else if (cu->line_header != nullptr)
16166 /* Any related symtab will do. */
16168 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16173 complaint (_("could not find suitable "
16174 "symtab for template parameter"
16175 " - DIE at %s [in module %s]"),
16176 sect_offset_str (die->sect_off),
16177 objfile_name (objfile));
16180 if (symtab != nullptr)
16182 /* Make sure that the symtab is set on the new symbols.
16183 Even though they don't appear in this symtab directly,
16184 other parts of gdb assume that symbols do, and this is
16185 reasonably true. */
16186 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16187 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16193 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16194 update TYPE using some information only available in DIE's children. */
16197 update_enumeration_type_from_children (struct die_info *die,
16199 struct dwarf2_cu *cu)
16201 struct die_info *child_die;
16202 int unsigned_enum = 1;
16206 auto_obstack obstack;
16208 for (child_die = die->child;
16209 child_die != NULL && child_die->tag;
16210 child_die = sibling_die (child_die))
16212 struct attribute *attr;
16214 const gdb_byte *bytes;
16215 struct dwarf2_locexpr_baton *baton;
16218 if (child_die->tag != DW_TAG_enumerator)
16221 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16225 name = dwarf2_name (child_die, cu);
16227 name = "<anonymous enumerator>";
16229 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16230 &value, &bytes, &baton);
16236 else if ((mask & value) != 0)
16241 /* If we already know that the enum type is neither unsigned, nor
16242 a flag type, no need to look at the rest of the enumerates. */
16243 if (!unsigned_enum && !flag_enum)
16248 TYPE_UNSIGNED (type) = 1;
16250 TYPE_FLAG_ENUM (type) = 1;
16253 /* Given a DW_AT_enumeration_type die, set its type. We do not
16254 complete the type's fields yet, or create any symbols. */
16256 static struct type *
16257 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16261 struct attribute *attr;
16264 /* If the definition of this type lives in .debug_types, read that type.
16265 Don't follow DW_AT_specification though, that will take us back up
16266 the chain and we want to go down. */
16267 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16270 type = get_DW_AT_signature_type (die, attr, cu);
16272 /* The type's CU may not be the same as CU.
16273 Ensure TYPE is recorded with CU in die_type_hash. */
16274 return set_die_type (die, type, cu);
16277 type = alloc_type (objfile);
16279 TYPE_CODE (type) = TYPE_CODE_ENUM;
16280 name = dwarf2_full_name (NULL, die, cu);
16282 TYPE_NAME (type) = name;
16284 attr = dwarf2_attr (die, DW_AT_type, cu);
16287 struct type *underlying_type = die_type (die, cu);
16289 TYPE_TARGET_TYPE (type) = underlying_type;
16292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16295 TYPE_LENGTH (type) = DW_UNSND (attr);
16299 TYPE_LENGTH (type) = 0;
16302 maybe_set_alignment (cu, die, type);
16304 /* The enumeration DIE can be incomplete. In Ada, any type can be
16305 declared as private in the package spec, and then defined only
16306 inside the package body. Such types are known as Taft Amendment
16307 Types. When another package uses such a type, an incomplete DIE
16308 may be generated by the compiler. */
16309 if (die_is_declaration (die, cu))
16310 TYPE_STUB (type) = 1;
16312 /* Finish the creation of this type by using the enum's children.
16313 We must call this even when the underlying type has been provided
16314 so that we can determine if we're looking at a "flag" enum. */
16315 update_enumeration_type_from_children (die, type, cu);
16317 /* If this type has an underlying type that is not a stub, then we
16318 may use its attributes. We always use the "unsigned" attribute
16319 in this situation, because ordinarily we guess whether the type
16320 is unsigned -- but the guess can be wrong and the underlying type
16321 can tell us the reality. However, we defer to a local size
16322 attribute if one exists, because this lets the compiler override
16323 the underlying type if needed. */
16324 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16326 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16327 if (TYPE_LENGTH (type) == 0)
16328 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16329 if (TYPE_RAW_ALIGN (type) == 0
16330 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16331 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16334 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16336 return set_die_type (die, type, cu);
16339 /* Given a pointer to a die which begins an enumeration, process all
16340 the dies that define the members of the enumeration, and create the
16341 symbol for the enumeration type.
16343 NOTE: We reverse the order of the element list. */
16346 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16348 struct type *this_type;
16350 this_type = get_die_type (die, cu);
16351 if (this_type == NULL)
16352 this_type = read_enumeration_type (die, cu);
16354 if (die->child != NULL)
16356 struct die_info *child_die;
16357 struct symbol *sym;
16358 struct field *fields = NULL;
16359 int num_fields = 0;
16362 child_die = die->child;
16363 while (child_die && child_die->tag)
16365 if (child_die->tag != DW_TAG_enumerator)
16367 process_die (child_die, cu);
16371 name = dwarf2_name (child_die, cu);
16374 sym = new_symbol (child_die, this_type, cu);
16376 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16378 fields = (struct field *)
16380 (num_fields + DW_FIELD_ALLOC_CHUNK)
16381 * sizeof (struct field));
16384 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16385 FIELD_TYPE (fields[num_fields]) = NULL;
16386 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16387 FIELD_BITSIZE (fields[num_fields]) = 0;
16393 child_die = sibling_die (child_die);
16398 TYPE_NFIELDS (this_type) = num_fields;
16399 TYPE_FIELDS (this_type) = (struct field *)
16400 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16401 memcpy (TYPE_FIELDS (this_type), fields,
16402 sizeof (struct field) * num_fields);
16407 /* If we are reading an enum from a .debug_types unit, and the enum
16408 is a declaration, and the enum is not the signatured type in the
16409 unit, then we do not want to add a symbol for it. Adding a
16410 symbol would in some cases obscure the true definition of the
16411 enum, giving users an incomplete type when the definition is
16412 actually available. Note that we do not want to do this for all
16413 enums which are just declarations, because C++0x allows forward
16414 enum declarations. */
16415 if (cu->per_cu->is_debug_types
16416 && die_is_declaration (die, cu))
16418 struct signatured_type *sig_type;
16420 sig_type = (struct signatured_type *) cu->per_cu;
16421 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16422 if (sig_type->type_offset_in_section != die->sect_off)
16426 new_symbol (die, this_type, cu);
16429 /* Extract all information from a DW_TAG_array_type DIE and put it in
16430 the DIE's type field. For now, this only handles one dimensional
16433 static struct type *
16434 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16436 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16437 struct die_info *child_die;
16439 struct type *element_type, *range_type, *index_type;
16440 struct attribute *attr;
16442 struct dynamic_prop *byte_stride_prop = NULL;
16443 unsigned int bit_stride = 0;
16445 element_type = die_type (die, cu);
16447 /* The die_type call above may have already set the type for this DIE. */
16448 type = get_die_type (die, cu);
16452 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16456 struct type *prop_type
16457 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16460 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16461 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16465 complaint (_("unable to read array DW_AT_byte_stride "
16466 " - DIE at %s [in module %s]"),
16467 sect_offset_str (die->sect_off),
16468 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16469 /* Ignore this attribute. We will likely not be able to print
16470 arrays of this type correctly, but there is little we can do
16471 to help if we cannot read the attribute's value. */
16472 byte_stride_prop = NULL;
16476 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16478 bit_stride = DW_UNSND (attr);
16480 /* Irix 6.2 native cc creates array types without children for
16481 arrays with unspecified length. */
16482 if (die->child == NULL)
16484 index_type = objfile_type (objfile)->builtin_int;
16485 range_type = create_static_range_type (NULL, index_type, 0, -1);
16486 type = create_array_type_with_stride (NULL, element_type, range_type,
16487 byte_stride_prop, bit_stride);
16488 return set_die_type (die, type, cu);
16491 std::vector<struct type *> range_types;
16492 child_die = die->child;
16493 while (child_die && child_die->tag)
16495 if (child_die->tag == DW_TAG_subrange_type)
16497 struct type *child_type = read_type_die (child_die, cu);
16499 if (child_type != NULL)
16501 /* The range type was succesfully read. Save it for the
16502 array type creation. */
16503 range_types.push_back (child_type);
16506 child_die = sibling_die (child_die);
16509 /* Dwarf2 dimensions are output from left to right, create the
16510 necessary array types in backwards order. */
16512 type = element_type;
16514 if (read_array_order (die, cu) == DW_ORD_col_major)
16518 while (i < range_types.size ())
16519 type = create_array_type_with_stride (NULL, type, range_types[i++],
16520 byte_stride_prop, bit_stride);
16524 size_t ndim = range_types.size ();
16526 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16527 byte_stride_prop, bit_stride);
16530 /* Understand Dwarf2 support for vector types (like they occur on
16531 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16532 array type. This is not part of the Dwarf2/3 standard yet, but a
16533 custom vendor extension. The main difference between a regular
16534 array and the vector variant is that vectors are passed by value
16536 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16538 make_vector_type (type);
16540 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16541 implementation may choose to implement triple vectors using this
16543 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16546 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16547 TYPE_LENGTH (type) = DW_UNSND (attr);
16549 complaint (_("DW_AT_byte_size for array type smaller "
16550 "than the total size of elements"));
16553 name = dwarf2_name (die, cu);
16555 TYPE_NAME (type) = name;
16557 maybe_set_alignment (cu, die, type);
16559 /* Install the type in the die. */
16560 set_die_type (die, type, cu);
16562 /* set_die_type should be already done. */
16563 set_descriptive_type (type, die, cu);
16568 static enum dwarf_array_dim_ordering
16569 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16571 struct attribute *attr;
16573 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16576 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16578 /* GNU F77 is a special case, as at 08/2004 array type info is the
16579 opposite order to the dwarf2 specification, but data is still
16580 laid out as per normal fortran.
16582 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16583 version checking. */
16585 if (cu->language == language_fortran
16586 && cu->producer && strstr (cu->producer, "GNU F77"))
16588 return DW_ORD_row_major;
16591 switch (cu->language_defn->la_array_ordering)
16593 case array_column_major:
16594 return DW_ORD_col_major;
16595 case array_row_major:
16597 return DW_ORD_row_major;
16601 /* Extract all information from a DW_TAG_set_type DIE and put it in
16602 the DIE's type field. */
16604 static struct type *
16605 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16607 struct type *domain_type, *set_type;
16608 struct attribute *attr;
16610 domain_type = die_type (die, cu);
16612 /* The die_type call above may have already set the type for this DIE. */
16613 set_type = get_die_type (die, cu);
16617 set_type = create_set_type (NULL, domain_type);
16619 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16621 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16623 maybe_set_alignment (cu, die, set_type);
16625 return set_die_type (die, set_type, cu);
16628 /* A helper for read_common_block that creates a locexpr baton.
16629 SYM is the symbol which we are marking as computed.
16630 COMMON_DIE is the DIE for the common block.
16631 COMMON_LOC is the location expression attribute for the common
16633 MEMBER_LOC is the location expression attribute for the particular
16634 member of the common block that we are processing.
16635 CU is the CU from which the above come. */
16638 mark_common_block_symbol_computed (struct symbol *sym,
16639 struct die_info *common_die,
16640 struct attribute *common_loc,
16641 struct attribute *member_loc,
16642 struct dwarf2_cu *cu)
16644 struct dwarf2_per_objfile *dwarf2_per_objfile
16645 = cu->per_cu->dwarf2_per_objfile;
16646 struct objfile *objfile = dwarf2_per_objfile->objfile;
16647 struct dwarf2_locexpr_baton *baton;
16649 unsigned int cu_off;
16650 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16651 LONGEST offset = 0;
16653 gdb_assert (common_loc && member_loc);
16654 gdb_assert (attr_form_is_block (common_loc));
16655 gdb_assert (attr_form_is_block (member_loc)
16656 || attr_form_is_constant (member_loc));
16658 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16659 baton->per_cu = cu->per_cu;
16660 gdb_assert (baton->per_cu);
16662 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16664 if (attr_form_is_constant (member_loc))
16666 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16667 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16670 baton->size += DW_BLOCK (member_loc)->size;
16672 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16675 *ptr++ = DW_OP_call4;
16676 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16677 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16680 if (attr_form_is_constant (member_loc))
16682 *ptr++ = DW_OP_addr;
16683 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16684 ptr += cu->header.addr_size;
16688 /* We have to copy the data here, because DW_OP_call4 will only
16689 use a DW_AT_location attribute. */
16690 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16691 ptr += DW_BLOCK (member_loc)->size;
16694 *ptr++ = DW_OP_plus;
16695 gdb_assert (ptr - baton->data == baton->size);
16697 SYMBOL_LOCATION_BATON (sym) = baton;
16698 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16701 /* Create appropriate locally-scoped variables for all the
16702 DW_TAG_common_block entries. Also create a struct common_block
16703 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16704 is used to sepate the common blocks name namespace from regular
16708 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16710 struct attribute *attr;
16712 attr = dwarf2_attr (die, DW_AT_location, cu);
16715 /* Support the .debug_loc offsets. */
16716 if (attr_form_is_block (attr))
16720 else if (attr_form_is_section_offset (attr))
16722 dwarf2_complex_location_expr_complaint ();
16727 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16728 "common block member");
16733 if (die->child != NULL)
16735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16736 struct die_info *child_die;
16737 size_t n_entries = 0, size;
16738 struct common_block *common_block;
16739 struct symbol *sym;
16741 for (child_die = die->child;
16742 child_die && child_die->tag;
16743 child_die = sibling_die (child_die))
16746 size = (sizeof (struct common_block)
16747 + (n_entries - 1) * sizeof (struct symbol *));
16749 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16751 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16752 common_block->n_entries = 0;
16754 for (child_die = die->child;
16755 child_die && child_die->tag;
16756 child_die = sibling_die (child_die))
16758 /* Create the symbol in the DW_TAG_common_block block in the current
16760 sym = new_symbol (child_die, NULL, cu);
16763 struct attribute *member_loc;
16765 common_block->contents[common_block->n_entries++] = sym;
16767 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16771 /* GDB has handled this for a long time, but it is
16772 not specified by DWARF. It seems to have been
16773 emitted by gfortran at least as recently as:
16774 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16775 complaint (_("Variable in common block has "
16776 "DW_AT_data_member_location "
16777 "- DIE at %s [in module %s]"),
16778 sect_offset_str (child_die->sect_off),
16779 objfile_name (objfile));
16781 if (attr_form_is_section_offset (member_loc))
16782 dwarf2_complex_location_expr_complaint ();
16783 else if (attr_form_is_constant (member_loc)
16784 || attr_form_is_block (member_loc))
16787 mark_common_block_symbol_computed (sym, die, attr,
16791 dwarf2_complex_location_expr_complaint ();
16796 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16797 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16801 /* Create a type for a C++ namespace. */
16803 static struct type *
16804 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16806 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16807 const char *previous_prefix, *name;
16811 /* For extensions, reuse the type of the original namespace. */
16812 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16814 struct die_info *ext_die;
16815 struct dwarf2_cu *ext_cu = cu;
16817 ext_die = dwarf2_extension (die, &ext_cu);
16818 type = read_type_die (ext_die, ext_cu);
16820 /* EXT_CU may not be the same as CU.
16821 Ensure TYPE is recorded with CU in die_type_hash. */
16822 return set_die_type (die, type, cu);
16825 name = namespace_name (die, &is_anonymous, cu);
16827 /* Now build the name of the current namespace. */
16829 previous_prefix = determine_prefix (die, cu);
16830 if (previous_prefix[0] != '\0')
16831 name = typename_concat (&objfile->objfile_obstack,
16832 previous_prefix, name, 0, cu);
16834 /* Create the type. */
16835 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16837 return set_die_type (die, type, cu);
16840 /* Read a namespace scope. */
16843 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16848 /* Add a symbol associated to this if we haven't seen the namespace
16849 before. Also, add a using directive if it's an anonymous
16852 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16856 type = read_type_die (die, cu);
16857 new_symbol (die, type, cu);
16859 namespace_name (die, &is_anonymous, cu);
16862 const char *previous_prefix = determine_prefix (die, cu);
16864 std::vector<const char *> excludes;
16865 add_using_directive (using_directives (cu),
16866 previous_prefix, TYPE_NAME (type), NULL,
16867 NULL, excludes, 0, &objfile->objfile_obstack);
16871 if (die->child != NULL)
16873 struct die_info *child_die = die->child;
16875 while (child_die && child_die->tag)
16877 process_die (child_die, cu);
16878 child_die = sibling_die (child_die);
16883 /* Read a Fortran module as type. This DIE can be only a declaration used for
16884 imported module. Still we need that type as local Fortran "use ... only"
16885 declaration imports depend on the created type in determine_prefix. */
16887 static struct type *
16888 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16891 const char *module_name;
16894 module_name = dwarf2_name (die, cu);
16895 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16897 return set_die_type (die, type, cu);
16900 /* Read a Fortran module. */
16903 read_module (struct die_info *die, struct dwarf2_cu *cu)
16905 struct die_info *child_die = die->child;
16908 type = read_type_die (die, cu);
16909 new_symbol (die, type, cu);
16911 while (child_die && child_die->tag)
16913 process_die (child_die, cu);
16914 child_die = sibling_die (child_die);
16918 /* Return the name of the namespace represented by DIE. Set
16919 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16922 static const char *
16923 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16925 struct die_info *current_die;
16926 const char *name = NULL;
16928 /* Loop through the extensions until we find a name. */
16930 for (current_die = die;
16931 current_die != NULL;
16932 current_die = dwarf2_extension (die, &cu))
16934 /* We don't use dwarf2_name here so that we can detect the absence
16935 of a name -> anonymous namespace. */
16936 name = dwarf2_string_attr (die, DW_AT_name, cu);
16942 /* Is it an anonymous namespace? */
16944 *is_anonymous = (name == NULL);
16946 name = CP_ANONYMOUS_NAMESPACE_STR;
16951 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16952 the user defined type vector. */
16954 static struct type *
16955 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16957 struct gdbarch *gdbarch
16958 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16959 struct comp_unit_head *cu_header = &cu->header;
16961 struct attribute *attr_byte_size;
16962 struct attribute *attr_address_class;
16963 int byte_size, addr_class;
16964 struct type *target_type;
16966 target_type = die_type (die, cu);
16968 /* The die_type call above may have already set the type for this DIE. */
16969 type = get_die_type (die, cu);
16973 type = lookup_pointer_type (target_type);
16975 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16976 if (attr_byte_size)
16977 byte_size = DW_UNSND (attr_byte_size);
16979 byte_size = cu_header->addr_size;
16981 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16982 if (attr_address_class)
16983 addr_class = DW_UNSND (attr_address_class);
16985 addr_class = DW_ADDR_none;
16987 ULONGEST alignment = get_alignment (cu, die);
16989 /* If the pointer size, alignment, or address class is different
16990 than the default, create a type variant marked as such and set
16991 the length accordingly. */
16992 if (TYPE_LENGTH (type) != byte_size
16993 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16994 && alignment != TYPE_RAW_ALIGN (type))
16995 || addr_class != DW_ADDR_none)
16997 if (gdbarch_address_class_type_flags_p (gdbarch))
17001 type_flags = gdbarch_address_class_type_flags
17002 (gdbarch, byte_size, addr_class);
17003 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17005 type = make_type_with_address_space (type, type_flags);
17007 else if (TYPE_LENGTH (type) != byte_size)
17009 complaint (_("invalid pointer size %d"), byte_size);
17011 else if (TYPE_RAW_ALIGN (type) != alignment)
17013 complaint (_("Invalid DW_AT_alignment"
17014 " - DIE at %s [in module %s]"),
17015 sect_offset_str (die->sect_off),
17016 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17020 /* Should we also complain about unhandled address classes? */
17024 TYPE_LENGTH (type) = byte_size;
17025 set_type_align (type, alignment);
17026 return set_die_type (die, type, cu);
17029 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17030 the user defined type vector. */
17032 static struct type *
17033 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17036 struct type *to_type;
17037 struct type *domain;
17039 to_type = die_type (die, cu);
17040 domain = die_containing_type (die, cu);
17042 /* The calls above may have already set the type for this DIE. */
17043 type = get_die_type (die, cu);
17047 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17048 type = lookup_methodptr_type (to_type);
17049 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17051 struct type *new_type
17052 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17054 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17055 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17056 TYPE_VARARGS (to_type));
17057 type = lookup_methodptr_type (new_type);
17060 type = lookup_memberptr_type (to_type, domain);
17062 return set_die_type (die, type, cu);
17065 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17066 the user defined type vector. */
17068 static struct type *
17069 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17070 enum type_code refcode)
17072 struct comp_unit_head *cu_header = &cu->header;
17073 struct type *type, *target_type;
17074 struct attribute *attr;
17076 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17078 target_type = die_type (die, cu);
17080 /* The die_type call above may have already set the type for this DIE. */
17081 type = get_die_type (die, cu);
17085 type = lookup_reference_type (target_type, refcode);
17086 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17089 TYPE_LENGTH (type) = DW_UNSND (attr);
17093 TYPE_LENGTH (type) = cu_header->addr_size;
17095 maybe_set_alignment (cu, die, type);
17096 return set_die_type (die, type, cu);
17099 /* Add the given cv-qualifiers to the element type of the array. GCC
17100 outputs DWARF type qualifiers that apply to an array, not the
17101 element type. But GDB relies on the array element type to carry
17102 the cv-qualifiers. This mimics section 6.7.3 of the C99
17105 static struct type *
17106 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17107 struct type *base_type, int cnst, int voltl)
17109 struct type *el_type, *inner_array;
17111 base_type = copy_type (base_type);
17112 inner_array = base_type;
17114 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17116 TYPE_TARGET_TYPE (inner_array) =
17117 copy_type (TYPE_TARGET_TYPE (inner_array));
17118 inner_array = TYPE_TARGET_TYPE (inner_array);
17121 el_type = TYPE_TARGET_TYPE (inner_array);
17122 cnst |= TYPE_CONST (el_type);
17123 voltl |= TYPE_VOLATILE (el_type);
17124 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17126 return set_die_type (die, base_type, cu);
17129 static struct type *
17130 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17132 struct type *base_type, *cv_type;
17134 base_type = die_type (die, cu);
17136 /* The die_type call above may have already set the type for this DIE. */
17137 cv_type = get_die_type (die, cu);
17141 /* In case the const qualifier is applied to an array type, the element type
17142 is so qualified, not the array type (section 6.7.3 of C99). */
17143 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17144 return add_array_cv_type (die, cu, base_type, 1, 0);
17146 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17147 return set_die_type (die, cv_type, cu);
17150 static struct type *
17151 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17153 struct type *base_type, *cv_type;
17155 base_type = die_type (die, cu);
17157 /* The die_type call above may have already set the type for this DIE. */
17158 cv_type = get_die_type (die, cu);
17162 /* In case the volatile qualifier is applied to an array type, the
17163 element type is so qualified, not the array type (section 6.7.3
17165 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17166 return add_array_cv_type (die, cu, base_type, 0, 1);
17168 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17169 return set_die_type (die, cv_type, cu);
17172 /* Handle DW_TAG_restrict_type. */
17174 static struct type *
17175 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17177 struct type *base_type, *cv_type;
17179 base_type = die_type (die, cu);
17181 /* The die_type call above may have already set the type for this DIE. */
17182 cv_type = get_die_type (die, cu);
17186 cv_type = make_restrict_type (base_type);
17187 return set_die_type (die, cv_type, cu);
17190 /* Handle DW_TAG_atomic_type. */
17192 static struct type *
17193 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17195 struct type *base_type, *cv_type;
17197 base_type = die_type (die, cu);
17199 /* The die_type call above may have already set the type for this DIE. */
17200 cv_type = get_die_type (die, cu);
17204 cv_type = make_atomic_type (base_type);
17205 return set_die_type (die, cv_type, cu);
17208 /* Extract all information from a DW_TAG_string_type DIE and add to
17209 the user defined type vector. It isn't really a user defined type,
17210 but it behaves like one, with other DIE's using an AT_user_def_type
17211 attribute to reference it. */
17213 static struct type *
17214 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17216 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17218 struct type *type, *range_type, *index_type, *char_type;
17219 struct attribute *attr;
17220 unsigned int length;
17222 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17225 length = DW_UNSND (attr);
17229 /* Check for the DW_AT_byte_size attribute. */
17230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17233 length = DW_UNSND (attr);
17241 index_type = objfile_type (objfile)->builtin_int;
17242 range_type = create_static_range_type (NULL, index_type, 1, length);
17243 char_type = language_string_char_type (cu->language_defn, gdbarch);
17244 type = create_string_type (NULL, char_type, range_type);
17246 return set_die_type (die, type, cu);
17249 /* Assuming that DIE corresponds to a function, returns nonzero
17250 if the function is prototyped. */
17253 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17255 struct attribute *attr;
17257 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17258 if (attr && (DW_UNSND (attr) != 0))
17261 /* The DWARF standard implies that the DW_AT_prototyped attribute
17262 is only meaninful for C, but the concept also extends to other
17263 languages that allow unprototyped functions (Eg: Objective C).
17264 For all other languages, assume that functions are always
17266 if (cu->language != language_c
17267 && cu->language != language_objc
17268 && cu->language != language_opencl)
17271 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17272 prototyped and unprototyped functions; default to prototyped,
17273 since that is more common in modern code (and RealView warns
17274 about unprototyped functions). */
17275 if (producer_is_realview (cu->producer))
17281 /* Handle DIES due to C code like:
17285 int (*funcp)(int a, long l);
17289 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17291 static struct type *
17292 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17295 struct type *type; /* Type that this function returns. */
17296 struct type *ftype; /* Function that returns above type. */
17297 struct attribute *attr;
17299 type = die_type (die, cu);
17301 /* The die_type call above may have already set the type for this DIE. */
17302 ftype = get_die_type (die, cu);
17306 ftype = lookup_function_type (type);
17308 if (prototyped_function_p (die, cu))
17309 TYPE_PROTOTYPED (ftype) = 1;
17311 /* Store the calling convention in the type if it's available in
17312 the subroutine die. Otherwise set the calling convention to
17313 the default value DW_CC_normal. */
17314 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17316 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17317 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17318 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17320 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17322 /* Record whether the function returns normally to its caller or not
17323 if the DWARF producer set that information. */
17324 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17325 if (attr && (DW_UNSND (attr) != 0))
17326 TYPE_NO_RETURN (ftype) = 1;
17328 /* We need to add the subroutine type to the die immediately so
17329 we don't infinitely recurse when dealing with parameters
17330 declared as the same subroutine type. */
17331 set_die_type (die, ftype, cu);
17333 if (die->child != NULL)
17335 struct type *void_type = objfile_type (objfile)->builtin_void;
17336 struct die_info *child_die;
17337 int nparams, iparams;
17339 /* Count the number of parameters.
17340 FIXME: GDB currently ignores vararg functions, but knows about
17341 vararg member functions. */
17343 child_die = die->child;
17344 while (child_die && child_die->tag)
17346 if (child_die->tag == DW_TAG_formal_parameter)
17348 else if (child_die->tag == DW_TAG_unspecified_parameters)
17349 TYPE_VARARGS (ftype) = 1;
17350 child_die = sibling_die (child_die);
17353 /* Allocate storage for parameters and fill them in. */
17354 TYPE_NFIELDS (ftype) = nparams;
17355 TYPE_FIELDS (ftype) = (struct field *)
17356 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17358 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17359 even if we error out during the parameters reading below. */
17360 for (iparams = 0; iparams < nparams; iparams++)
17361 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17364 child_die = die->child;
17365 while (child_die && child_die->tag)
17367 if (child_die->tag == DW_TAG_formal_parameter)
17369 struct type *arg_type;
17371 /* DWARF version 2 has no clean way to discern C++
17372 static and non-static member functions. G++ helps
17373 GDB by marking the first parameter for non-static
17374 member functions (which is the this pointer) as
17375 artificial. We pass this information to
17376 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17378 DWARF version 3 added DW_AT_object_pointer, which GCC
17379 4.5 does not yet generate. */
17380 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17382 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17384 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17385 arg_type = die_type (child_die, cu);
17387 /* RealView does not mark THIS as const, which the testsuite
17388 expects. GCC marks THIS as const in method definitions,
17389 but not in the class specifications (GCC PR 43053). */
17390 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17391 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17394 struct dwarf2_cu *arg_cu = cu;
17395 const char *name = dwarf2_name (child_die, cu);
17397 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17400 /* If the compiler emits this, use it. */
17401 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17404 else if (name && strcmp (name, "this") == 0)
17405 /* Function definitions will have the argument names. */
17407 else if (name == NULL && iparams == 0)
17408 /* Declarations may not have the names, so like
17409 elsewhere in GDB, assume an artificial first
17410 argument is "this". */
17414 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17418 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17421 child_die = sibling_die (child_die);
17428 static struct type *
17429 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17432 const char *name = NULL;
17433 struct type *this_type, *target_type;
17435 name = dwarf2_full_name (NULL, die, cu);
17436 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17437 TYPE_TARGET_STUB (this_type) = 1;
17438 set_die_type (die, this_type, cu);
17439 target_type = die_type (die, cu);
17440 if (target_type != this_type)
17441 TYPE_TARGET_TYPE (this_type) = target_type;
17444 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17445 spec and cause infinite loops in GDB. */
17446 complaint (_("Self-referential DW_TAG_typedef "
17447 "- DIE at %s [in module %s]"),
17448 sect_offset_str (die->sect_off), objfile_name (objfile));
17449 TYPE_TARGET_TYPE (this_type) = NULL;
17454 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17455 (which may be different from NAME) to the architecture back-end to allow
17456 it to guess the correct format if necessary. */
17458 static struct type *
17459 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17460 const char *name_hint)
17462 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17463 const struct floatformat **format;
17466 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17468 type = init_float_type (objfile, bits, name, format);
17470 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17475 /* Allocate an integer type of size BITS and name NAME. */
17477 static struct type *
17478 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17479 int bits, int unsigned_p, const char *name)
17483 /* Versions of Intel's C Compiler generate an integer type called "void"
17484 instead of using DW_TAG_unspecified_type. This has been seen on
17485 at least versions 14, 17, and 18. */
17486 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17487 && strcmp (name, "void") == 0)
17488 type = objfile_type (objfile)->builtin_void;
17490 type = init_integer_type (objfile, bits, unsigned_p, name);
17495 /* Initialise and return a floating point type of size BITS suitable for
17496 use as a component of a complex number. The NAME_HINT is passed through
17497 when initialising the floating point type and is the name of the complex
17500 As DWARF doesn't currently provide an explicit name for the components
17501 of a complex number, but it can be helpful to have these components
17502 named, we try to select a suitable name based on the size of the
17504 static struct type *
17505 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17506 struct objfile *objfile,
17507 int bits, const char *name_hint)
17509 gdbarch *gdbarch = get_objfile_arch (objfile);
17510 struct type *tt = nullptr;
17512 /* Try to find a suitable floating point builtin type of size BITS.
17513 We're going to use the name of this type as the name for the complex
17514 target type that we are about to create. */
17515 switch (cu->language)
17517 case language_fortran:
17521 tt = builtin_f_type (gdbarch)->builtin_real;
17524 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17526 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17528 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17536 tt = builtin_type (gdbarch)->builtin_float;
17539 tt = builtin_type (gdbarch)->builtin_double;
17541 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17543 tt = builtin_type (gdbarch)->builtin_long_double;
17549 /* If the type we found doesn't match the size we were looking for, then
17550 pretend we didn't find a type at all, the complex target type we
17551 create will then be nameless. */
17552 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17555 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17556 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17559 /* Find a representation of a given base type and install
17560 it in the TYPE field of the die. */
17562 static struct type *
17563 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17565 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17567 struct attribute *attr;
17568 int encoding = 0, bits = 0;
17571 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17574 encoding = DW_UNSND (attr);
17576 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17579 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17581 name = dwarf2_name (die, cu);
17584 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17589 case DW_ATE_address:
17590 /* Turn DW_ATE_address into a void * pointer. */
17591 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17592 type = init_pointer_type (objfile, bits, name, type);
17594 case DW_ATE_boolean:
17595 type = init_boolean_type (objfile, bits, 1, name);
17597 case DW_ATE_complex_float:
17598 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17599 type = init_complex_type (objfile, name, type);
17601 case DW_ATE_decimal_float:
17602 type = init_decfloat_type (objfile, bits, name);
17605 type = dwarf2_init_float_type (objfile, bits, name, name);
17607 case DW_ATE_signed:
17608 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17610 case DW_ATE_unsigned:
17611 if (cu->language == language_fortran
17613 && startswith (name, "character("))
17614 type = init_character_type (objfile, bits, 1, name);
17616 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17618 case DW_ATE_signed_char:
17619 if (cu->language == language_ada || cu->language == language_m2
17620 || cu->language == language_pascal
17621 || cu->language == language_fortran)
17622 type = init_character_type (objfile, bits, 0, name);
17624 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17626 case DW_ATE_unsigned_char:
17627 if (cu->language == language_ada || cu->language == language_m2
17628 || cu->language == language_pascal
17629 || cu->language == language_fortran
17630 || cu->language == language_rust)
17631 type = init_character_type (objfile, bits, 1, name);
17633 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17637 gdbarch *arch = get_objfile_arch (objfile);
17640 type = builtin_type (arch)->builtin_char16;
17641 else if (bits == 32)
17642 type = builtin_type (arch)->builtin_char32;
17645 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17647 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17649 return set_die_type (die, type, cu);
17654 complaint (_("unsupported DW_AT_encoding: '%s'"),
17655 dwarf_type_encoding_name (encoding));
17656 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17660 if (name && strcmp (name, "char") == 0)
17661 TYPE_NOSIGN (type) = 1;
17663 maybe_set_alignment (cu, die, type);
17665 return set_die_type (die, type, cu);
17668 /* Parse dwarf attribute if it's a block, reference or constant and put the
17669 resulting value of the attribute into struct bound_prop.
17670 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17673 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17674 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17675 struct type *default_type)
17677 struct dwarf2_property_baton *baton;
17678 struct obstack *obstack
17679 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17681 gdb_assert (default_type != NULL);
17683 if (attr == NULL || prop == NULL)
17686 if (attr_form_is_block (attr))
17688 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17689 baton->property_type = default_type;
17690 baton->locexpr.per_cu = cu->per_cu;
17691 baton->locexpr.size = DW_BLOCK (attr)->size;
17692 baton->locexpr.data = DW_BLOCK (attr)->data;
17693 baton->locexpr.is_reference = false;
17694 prop->data.baton = baton;
17695 prop->kind = PROP_LOCEXPR;
17696 gdb_assert (prop->data.baton != NULL);
17698 else if (attr_form_is_ref (attr))
17700 struct dwarf2_cu *target_cu = cu;
17701 struct die_info *target_die;
17702 struct attribute *target_attr;
17704 target_die = follow_die_ref (die, attr, &target_cu);
17705 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17706 if (target_attr == NULL)
17707 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17709 if (target_attr == NULL)
17712 switch (target_attr->name)
17714 case DW_AT_location:
17715 if (attr_form_is_section_offset (target_attr))
17717 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17718 baton->property_type = die_type (target_die, target_cu);
17719 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17720 prop->data.baton = baton;
17721 prop->kind = PROP_LOCLIST;
17722 gdb_assert (prop->data.baton != NULL);
17724 else if (attr_form_is_block (target_attr))
17726 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17727 baton->property_type = die_type (target_die, target_cu);
17728 baton->locexpr.per_cu = cu->per_cu;
17729 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17730 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17731 baton->locexpr.is_reference = true;
17732 prop->data.baton = baton;
17733 prop->kind = PROP_LOCEXPR;
17734 gdb_assert (prop->data.baton != NULL);
17738 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17739 "dynamic property");
17743 case DW_AT_data_member_location:
17747 if (!handle_data_member_location (target_die, target_cu,
17751 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17752 baton->property_type = read_type_die (target_die->parent,
17754 baton->offset_info.offset = offset;
17755 baton->offset_info.type = die_type (target_die, target_cu);
17756 prop->data.baton = baton;
17757 prop->kind = PROP_ADDR_OFFSET;
17762 else if (attr_form_is_constant (attr))
17764 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17765 prop->kind = PROP_CONST;
17769 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17770 dwarf2_name (die, cu));
17777 /* Find an integer type the same size as the address size given in the
17778 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17779 is unsigned or not. */
17781 static struct type *
17782 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17785 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17786 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17787 struct type *int_type;
17789 /* Helper macro to examine the various builtin types. */
17790 #define TRY_TYPE(F) \
17791 int_type = (unsigned_p \
17792 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17793 : objfile_type (objfile)->builtin_ ## F); \
17794 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17801 TRY_TYPE (long_long);
17805 gdb_assert_not_reached ("unable to find suitable integer type");
17808 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17809 present (which is valid) then compute the default type based on the
17810 compilation units address size. */
17812 static struct type *
17813 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17815 struct type *index_type = die_type (die, cu);
17817 /* Dwarf-2 specifications explicitly allows to create subrange types
17818 without specifying a base type.
17819 In that case, the base type must be set to the type of
17820 the lower bound, upper bound or count, in that order, if any of these
17821 three attributes references an object that has a type.
17822 If no base type is found, the Dwarf-2 specifications say that
17823 a signed integer type of size equal to the size of an address should
17825 For the following C code: `extern char gdb_int [];'
17826 GCC produces an empty range DIE.
17827 FIXME: muller/2010-05-28: Possible references to object for low bound,
17828 high bound or count are not yet handled by this code. */
17829 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17830 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17835 /* Read the given DW_AT_subrange DIE. */
17837 static struct type *
17838 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17840 struct type *base_type, *orig_base_type;
17841 struct type *range_type;
17842 struct attribute *attr;
17843 struct dynamic_prop low, high;
17844 int low_default_is_valid;
17845 int high_bound_is_count = 0;
17847 ULONGEST negative_mask;
17849 orig_base_type = read_subrange_index_type (die, cu);
17851 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17852 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17853 creating the range type, but we use the result of check_typedef
17854 when examining properties of the type. */
17855 base_type = check_typedef (orig_base_type);
17857 /* The die_type call above may have already set the type for this DIE. */
17858 range_type = get_die_type (die, cu);
17862 low.kind = PROP_CONST;
17863 high.kind = PROP_CONST;
17864 high.data.const_val = 0;
17866 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17867 omitting DW_AT_lower_bound. */
17868 switch (cu->language)
17871 case language_cplus:
17872 low.data.const_val = 0;
17873 low_default_is_valid = 1;
17875 case language_fortran:
17876 low.data.const_val = 1;
17877 low_default_is_valid = 1;
17880 case language_objc:
17881 case language_rust:
17882 low.data.const_val = 0;
17883 low_default_is_valid = (cu->header.version >= 4);
17887 case language_pascal:
17888 low.data.const_val = 1;
17889 low_default_is_valid = (cu->header.version >= 4);
17892 low.data.const_val = 0;
17893 low_default_is_valid = 0;
17897 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17899 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17900 else if (!low_default_is_valid)
17901 complaint (_("Missing DW_AT_lower_bound "
17902 "- DIE at %s [in module %s]"),
17903 sect_offset_str (die->sect_off),
17904 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17906 struct attribute *attr_ub, *attr_count;
17907 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17908 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17910 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17911 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17913 /* If bounds are constant do the final calculation here. */
17914 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17915 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17917 high_bound_is_count = 1;
17921 if (attr_ub != NULL)
17922 complaint (_("Unresolved DW_AT_upper_bound "
17923 "- DIE at %s [in module %s]"),
17924 sect_offset_str (die->sect_off),
17925 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17926 if (attr_count != NULL)
17927 complaint (_("Unresolved DW_AT_count "
17928 "- DIE at %s [in module %s]"),
17929 sect_offset_str (die->sect_off),
17930 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17935 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17936 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17937 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17939 /* Normally, the DWARF producers are expected to use a signed
17940 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17941 But this is unfortunately not always the case, as witnessed
17942 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17943 is used instead. To work around that ambiguity, we treat
17944 the bounds as signed, and thus sign-extend their values, when
17945 the base type is signed. */
17947 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17948 if (low.kind == PROP_CONST
17949 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17950 low.data.const_val |= negative_mask;
17951 if (high.kind == PROP_CONST
17952 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17953 high.data.const_val |= negative_mask;
17955 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17957 if (high_bound_is_count)
17958 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17960 /* Ada expects an empty array on no boundary attributes. */
17961 if (attr == NULL && cu->language != language_ada)
17962 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17964 name = dwarf2_name (die, cu);
17966 TYPE_NAME (range_type) = name;
17968 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17970 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17972 maybe_set_alignment (cu, die, range_type);
17974 set_die_type (die, range_type, cu);
17976 /* set_die_type should be already done. */
17977 set_descriptive_type (range_type, die, cu);
17982 static struct type *
17983 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17987 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17989 TYPE_NAME (type) = dwarf2_name (die, cu);
17991 /* In Ada, an unspecified type is typically used when the description
17992 of the type is defered to a different unit. When encountering
17993 such a type, we treat it as a stub, and try to resolve it later on,
17995 if (cu->language == language_ada)
17996 TYPE_STUB (type) = 1;
17998 return set_die_type (die, type, cu);
18001 /* Read a single die and all its descendents. Set the die's sibling
18002 field to NULL; set other fields in the die correctly, and set all
18003 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18004 location of the info_ptr after reading all of those dies. PARENT
18005 is the parent of the die in question. */
18007 static struct die_info *
18008 read_die_and_children (const struct die_reader_specs *reader,
18009 const gdb_byte *info_ptr,
18010 const gdb_byte **new_info_ptr,
18011 struct die_info *parent)
18013 struct die_info *die;
18014 const gdb_byte *cur_ptr;
18017 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18020 *new_info_ptr = cur_ptr;
18023 store_in_ref_table (die, reader->cu);
18026 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18030 *new_info_ptr = cur_ptr;
18033 die->sibling = NULL;
18034 die->parent = parent;
18038 /* Read a die, all of its descendents, and all of its siblings; set
18039 all of the fields of all of the dies correctly. Arguments are as
18040 in read_die_and_children. */
18042 static struct die_info *
18043 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18044 const gdb_byte *info_ptr,
18045 const gdb_byte **new_info_ptr,
18046 struct die_info *parent)
18048 struct die_info *first_die, *last_sibling;
18049 const gdb_byte *cur_ptr;
18051 cur_ptr = info_ptr;
18052 first_die = last_sibling = NULL;
18056 struct die_info *die
18057 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18061 *new_info_ptr = cur_ptr;
18068 last_sibling->sibling = die;
18070 last_sibling = die;
18074 /* Read a die, all of its descendents, and all of its siblings; set
18075 all of the fields of all of the dies correctly. Arguments are as
18076 in read_die_and_children.
18077 This the main entry point for reading a DIE and all its children. */
18079 static struct die_info *
18080 read_die_and_siblings (const struct die_reader_specs *reader,
18081 const gdb_byte *info_ptr,
18082 const gdb_byte **new_info_ptr,
18083 struct die_info *parent)
18085 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18086 new_info_ptr, parent);
18088 if (dwarf_die_debug)
18090 fprintf_unfiltered (gdb_stdlog,
18091 "Read die from %s@0x%x of %s:\n",
18092 get_section_name (reader->die_section),
18093 (unsigned) (info_ptr - reader->die_section->buffer),
18094 bfd_get_filename (reader->abfd));
18095 dump_die (die, dwarf_die_debug);
18101 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18103 The caller is responsible for filling in the extra attributes
18104 and updating (*DIEP)->num_attrs.
18105 Set DIEP to point to a newly allocated die with its information,
18106 except for its child, sibling, and parent fields.
18107 Set HAS_CHILDREN to tell whether the die has children or not. */
18109 static const gdb_byte *
18110 read_full_die_1 (const struct die_reader_specs *reader,
18111 struct die_info **diep, const gdb_byte *info_ptr,
18112 int *has_children, int num_extra_attrs)
18114 unsigned int abbrev_number, bytes_read, i;
18115 struct abbrev_info *abbrev;
18116 struct die_info *die;
18117 struct dwarf2_cu *cu = reader->cu;
18118 bfd *abfd = reader->abfd;
18120 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18121 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18122 info_ptr += bytes_read;
18123 if (!abbrev_number)
18130 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18132 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18134 bfd_get_filename (abfd));
18136 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18137 die->sect_off = sect_off;
18138 die->tag = abbrev->tag;
18139 die->abbrev = abbrev_number;
18141 /* Make the result usable.
18142 The caller needs to update num_attrs after adding the extra
18144 die->num_attrs = abbrev->num_attrs;
18146 for (i = 0; i < abbrev->num_attrs; ++i)
18147 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18151 *has_children = abbrev->has_children;
18155 /* Read a die and all its attributes.
18156 Set DIEP to point to a newly allocated die with its information,
18157 except for its child, sibling, and parent fields.
18158 Set HAS_CHILDREN to tell whether the die has children or not. */
18160 static const gdb_byte *
18161 read_full_die (const struct die_reader_specs *reader,
18162 struct die_info **diep, const gdb_byte *info_ptr,
18165 const gdb_byte *result;
18167 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18169 if (dwarf_die_debug)
18171 fprintf_unfiltered (gdb_stdlog,
18172 "Read die from %s@0x%x of %s:\n",
18173 get_section_name (reader->die_section),
18174 (unsigned) (info_ptr - reader->die_section->buffer),
18175 bfd_get_filename (reader->abfd));
18176 dump_die (*diep, dwarf_die_debug);
18182 /* Abbreviation tables.
18184 In DWARF version 2, the description of the debugging information is
18185 stored in a separate .debug_abbrev section. Before we read any
18186 dies from a section we read in all abbreviations and install them
18187 in a hash table. */
18189 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18191 struct abbrev_info *
18192 abbrev_table::alloc_abbrev ()
18194 struct abbrev_info *abbrev;
18196 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18197 memset (abbrev, 0, sizeof (struct abbrev_info));
18202 /* Add an abbreviation to the table. */
18205 abbrev_table::add_abbrev (unsigned int abbrev_number,
18206 struct abbrev_info *abbrev)
18208 unsigned int hash_number;
18210 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18211 abbrev->next = m_abbrevs[hash_number];
18212 m_abbrevs[hash_number] = abbrev;
18215 /* Look up an abbrev in the table.
18216 Returns NULL if the abbrev is not found. */
18218 struct abbrev_info *
18219 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18221 unsigned int hash_number;
18222 struct abbrev_info *abbrev;
18224 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18225 abbrev = m_abbrevs[hash_number];
18229 if (abbrev->number == abbrev_number)
18231 abbrev = abbrev->next;
18236 /* Read in an abbrev table. */
18238 static abbrev_table_up
18239 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18240 struct dwarf2_section_info *section,
18241 sect_offset sect_off)
18243 struct objfile *objfile = dwarf2_per_objfile->objfile;
18244 bfd *abfd = get_section_bfd_owner (section);
18245 const gdb_byte *abbrev_ptr;
18246 struct abbrev_info *cur_abbrev;
18247 unsigned int abbrev_number, bytes_read, abbrev_name;
18248 unsigned int abbrev_form;
18249 struct attr_abbrev *cur_attrs;
18250 unsigned int allocated_attrs;
18252 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18254 dwarf2_read_section (objfile, section);
18255 abbrev_ptr = section->buffer + to_underlying (sect_off);
18256 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18257 abbrev_ptr += bytes_read;
18259 allocated_attrs = ATTR_ALLOC_CHUNK;
18260 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18262 /* Loop until we reach an abbrev number of 0. */
18263 while (abbrev_number)
18265 cur_abbrev = abbrev_table->alloc_abbrev ();
18267 /* read in abbrev header */
18268 cur_abbrev->number = abbrev_number;
18270 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18271 abbrev_ptr += bytes_read;
18272 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18275 /* now read in declarations */
18278 LONGEST implicit_const;
18280 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18281 abbrev_ptr += bytes_read;
18282 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18283 abbrev_ptr += bytes_read;
18284 if (abbrev_form == DW_FORM_implicit_const)
18286 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18288 abbrev_ptr += bytes_read;
18292 /* Initialize it due to a false compiler warning. */
18293 implicit_const = -1;
18296 if (abbrev_name == 0)
18299 if (cur_abbrev->num_attrs == allocated_attrs)
18301 allocated_attrs += ATTR_ALLOC_CHUNK;
18303 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18306 cur_attrs[cur_abbrev->num_attrs].name
18307 = (enum dwarf_attribute) abbrev_name;
18308 cur_attrs[cur_abbrev->num_attrs].form
18309 = (enum dwarf_form) abbrev_form;
18310 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18311 ++cur_abbrev->num_attrs;
18314 cur_abbrev->attrs =
18315 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18316 cur_abbrev->num_attrs);
18317 memcpy (cur_abbrev->attrs, cur_attrs,
18318 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18320 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18322 /* Get next abbreviation.
18323 Under Irix6 the abbreviations for a compilation unit are not
18324 always properly terminated with an abbrev number of 0.
18325 Exit loop if we encounter an abbreviation which we have
18326 already read (which means we are about to read the abbreviations
18327 for the next compile unit) or if the end of the abbreviation
18328 table is reached. */
18329 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18331 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18332 abbrev_ptr += bytes_read;
18333 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18338 return abbrev_table;
18341 /* Returns nonzero if TAG represents a type that we might generate a partial
18345 is_type_tag_for_partial (int tag)
18350 /* Some types that would be reasonable to generate partial symbols for,
18351 that we don't at present. */
18352 case DW_TAG_array_type:
18353 case DW_TAG_file_type:
18354 case DW_TAG_ptr_to_member_type:
18355 case DW_TAG_set_type:
18356 case DW_TAG_string_type:
18357 case DW_TAG_subroutine_type:
18359 case DW_TAG_base_type:
18360 case DW_TAG_class_type:
18361 case DW_TAG_interface_type:
18362 case DW_TAG_enumeration_type:
18363 case DW_TAG_structure_type:
18364 case DW_TAG_subrange_type:
18365 case DW_TAG_typedef:
18366 case DW_TAG_union_type:
18373 /* Load all DIEs that are interesting for partial symbols into memory. */
18375 static struct partial_die_info *
18376 load_partial_dies (const struct die_reader_specs *reader,
18377 const gdb_byte *info_ptr, int building_psymtab)
18379 struct dwarf2_cu *cu = reader->cu;
18380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18381 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18382 unsigned int bytes_read;
18383 unsigned int load_all = 0;
18384 int nesting_level = 1;
18389 gdb_assert (cu->per_cu != NULL);
18390 if (cu->per_cu->load_all_dies)
18394 = htab_create_alloc_ex (cu->header.length / 12,
18398 &cu->comp_unit_obstack,
18399 hashtab_obstack_allocate,
18400 dummy_obstack_deallocate);
18404 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18406 /* A NULL abbrev means the end of a series of children. */
18407 if (abbrev == NULL)
18409 if (--nesting_level == 0)
18412 info_ptr += bytes_read;
18413 last_die = parent_die;
18414 parent_die = parent_die->die_parent;
18418 /* Check for template arguments. We never save these; if
18419 they're seen, we just mark the parent, and go on our way. */
18420 if (parent_die != NULL
18421 && cu->language == language_cplus
18422 && (abbrev->tag == DW_TAG_template_type_param
18423 || abbrev->tag == DW_TAG_template_value_param))
18425 parent_die->has_template_arguments = 1;
18429 /* We don't need a partial DIE for the template argument. */
18430 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18435 /* We only recurse into c++ subprograms looking for template arguments.
18436 Skip their other children. */
18438 && cu->language == language_cplus
18439 && parent_die != NULL
18440 && parent_die->tag == DW_TAG_subprogram)
18442 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18446 /* Check whether this DIE is interesting enough to save. Normally
18447 we would not be interested in members here, but there may be
18448 later variables referencing them via DW_AT_specification (for
18449 static members). */
18451 && !is_type_tag_for_partial (abbrev->tag)
18452 && abbrev->tag != DW_TAG_constant
18453 && abbrev->tag != DW_TAG_enumerator
18454 && abbrev->tag != DW_TAG_subprogram
18455 && abbrev->tag != DW_TAG_inlined_subroutine
18456 && abbrev->tag != DW_TAG_lexical_block
18457 && abbrev->tag != DW_TAG_variable
18458 && abbrev->tag != DW_TAG_namespace
18459 && abbrev->tag != DW_TAG_module
18460 && abbrev->tag != DW_TAG_member
18461 && abbrev->tag != DW_TAG_imported_unit
18462 && abbrev->tag != DW_TAG_imported_declaration)
18464 /* Otherwise we skip to the next sibling, if any. */
18465 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18469 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18472 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18474 /* This two-pass algorithm for processing partial symbols has a
18475 high cost in cache pressure. Thus, handle some simple cases
18476 here which cover the majority of C partial symbols. DIEs
18477 which neither have specification tags in them, nor could have
18478 specification tags elsewhere pointing at them, can simply be
18479 processed and discarded.
18481 This segment is also optional; scan_partial_symbols and
18482 add_partial_symbol will handle these DIEs if we chain
18483 them in normally. When compilers which do not emit large
18484 quantities of duplicate debug information are more common,
18485 this code can probably be removed. */
18487 /* Any complete simple types at the top level (pretty much all
18488 of them, for a language without namespaces), can be processed
18490 if (parent_die == NULL
18491 && pdi.has_specification == 0
18492 && pdi.is_declaration == 0
18493 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18494 || pdi.tag == DW_TAG_base_type
18495 || pdi.tag == DW_TAG_subrange_type))
18497 if (building_psymtab && pdi.name != NULL)
18498 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18499 VAR_DOMAIN, LOC_TYPEDEF, -1,
18500 psymbol_placement::STATIC,
18501 0, cu->language, objfile);
18502 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18506 /* The exception for DW_TAG_typedef with has_children above is
18507 a workaround of GCC PR debug/47510. In the case of this complaint
18508 type_name_or_error will error on such types later.
18510 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18511 it could not find the child DIEs referenced later, this is checked
18512 above. In correct DWARF DW_TAG_typedef should have no children. */
18514 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18515 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18516 "- DIE at %s [in module %s]"),
18517 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18519 /* If we're at the second level, and we're an enumerator, and
18520 our parent has no specification (meaning possibly lives in a
18521 namespace elsewhere), then we can add the partial symbol now
18522 instead of queueing it. */
18523 if (pdi.tag == DW_TAG_enumerator
18524 && parent_die != NULL
18525 && parent_die->die_parent == NULL
18526 && parent_die->tag == DW_TAG_enumeration_type
18527 && parent_die->has_specification == 0)
18529 if (pdi.name == NULL)
18530 complaint (_("malformed enumerator DIE ignored"));
18531 else if (building_psymtab)
18532 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18533 VAR_DOMAIN, LOC_CONST, -1,
18534 cu->language == language_cplus
18535 ? psymbol_placement::GLOBAL
18536 : psymbol_placement::STATIC,
18537 0, cu->language, objfile);
18539 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18543 struct partial_die_info *part_die
18544 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18546 /* We'll save this DIE so link it in. */
18547 part_die->die_parent = parent_die;
18548 part_die->die_sibling = NULL;
18549 part_die->die_child = NULL;
18551 if (last_die && last_die == parent_die)
18552 last_die->die_child = part_die;
18554 last_die->die_sibling = part_die;
18556 last_die = part_die;
18558 if (first_die == NULL)
18559 first_die = part_die;
18561 /* Maybe add the DIE to the hash table. Not all DIEs that we
18562 find interesting need to be in the hash table, because we
18563 also have the parent/sibling/child chains; only those that we
18564 might refer to by offset later during partial symbol reading.
18566 For now this means things that might have be the target of a
18567 DW_AT_specification, DW_AT_abstract_origin, or
18568 DW_AT_extension. DW_AT_extension will refer only to
18569 namespaces; DW_AT_abstract_origin refers to functions (and
18570 many things under the function DIE, but we do not recurse
18571 into function DIEs during partial symbol reading) and
18572 possibly variables as well; DW_AT_specification refers to
18573 declarations. Declarations ought to have the DW_AT_declaration
18574 flag. It happens that GCC forgets to put it in sometimes, but
18575 only for functions, not for types.
18577 Adding more things than necessary to the hash table is harmless
18578 except for the performance cost. Adding too few will result in
18579 wasted time in find_partial_die, when we reread the compilation
18580 unit with load_all_dies set. */
18583 || abbrev->tag == DW_TAG_constant
18584 || abbrev->tag == DW_TAG_subprogram
18585 || abbrev->tag == DW_TAG_variable
18586 || abbrev->tag == DW_TAG_namespace
18587 || part_die->is_declaration)
18591 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18592 to_underlying (part_die->sect_off),
18597 /* For some DIEs we want to follow their children (if any). For C
18598 we have no reason to follow the children of structures; for other
18599 languages we have to, so that we can get at method physnames
18600 to infer fully qualified class names, for DW_AT_specification,
18601 and for C++ template arguments. For C++, we also look one level
18602 inside functions to find template arguments (if the name of the
18603 function does not already contain the template arguments).
18605 For Ada, we need to scan the children of subprograms and lexical
18606 blocks as well because Ada allows the definition of nested
18607 entities that could be interesting for the debugger, such as
18608 nested subprograms for instance. */
18609 if (last_die->has_children
18611 || last_die->tag == DW_TAG_namespace
18612 || last_die->tag == DW_TAG_module
18613 || last_die->tag == DW_TAG_enumeration_type
18614 || (cu->language == language_cplus
18615 && last_die->tag == DW_TAG_subprogram
18616 && (last_die->name == NULL
18617 || strchr (last_die->name, '<') == NULL))
18618 || (cu->language != language_c
18619 && (last_die->tag == DW_TAG_class_type
18620 || last_die->tag == DW_TAG_interface_type
18621 || last_die->tag == DW_TAG_structure_type
18622 || last_die->tag == DW_TAG_union_type))
18623 || (cu->language == language_ada
18624 && (last_die->tag == DW_TAG_subprogram
18625 || last_die->tag == DW_TAG_lexical_block))))
18628 parent_die = last_die;
18632 /* Otherwise we skip to the next sibling, if any. */
18633 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18635 /* Back to the top, do it again. */
18639 partial_die_info::partial_die_info (sect_offset sect_off_,
18640 struct abbrev_info *abbrev)
18641 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18645 /* Read a minimal amount of information into the minimal die structure.
18646 INFO_PTR should point just after the initial uleb128 of a DIE. */
18649 partial_die_info::read (const struct die_reader_specs *reader,
18650 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18652 struct dwarf2_cu *cu = reader->cu;
18653 struct dwarf2_per_objfile *dwarf2_per_objfile
18654 = cu->per_cu->dwarf2_per_objfile;
18656 int has_low_pc_attr = 0;
18657 int has_high_pc_attr = 0;
18658 int high_pc_relative = 0;
18660 for (i = 0; i < abbrev.num_attrs; ++i)
18662 struct attribute attr;
18664 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18666 /* Store the data if it is of an attribute we want to keep in a
18667 partial symbol table. */
18673 case DW_TAG_compile_unit:
18674 case DW_TAG_partial_unit:
18675 case DW_TAG_type_unit:
18676 /* Compilation units have a DW_AT_name that is a filename, not
18677 a source language identifier. */
18678 case DW_TAG_enumeration_type:
18679 case DW_TAG_enumerator:
18680 /* These tags always have simple identifiers already; no need
18681 to canonicalize them. */
18682 name = DW_STRING (&attr);
18686 struct objfile *objfile = dwarf2_per_objfile->objfile;
18689 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18690 &objfile->per_bfd->storage_obstack);
18695 case DW_AT_linkage_name:
18696 case DW_AT_MIPS_linkage_name:
18697 /* Note that both forms of linkage name might appear. We
18698 assume they will be the same, and we only store the last
18700 linkage_name = DW_STRING (&attr);
18703 has_low_pc_attr = 1;
18704 lowpc = attr_value_as_address (&attr);
18706 case DW_AT_high_pc:
18707 has_high_pc_attr = 1;
18708 highpc = attr_value_as_address (&attr);
18709 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18710 high_pc_relative = 1;
18712 case DW_AT_location:
18713 /* Support the .debug_loc offsets. */
18714 if (attr_form_is_block (&attr))
18716 d.locdesc = DW_BLOCK (&attr);
18718 else if (attr_form_is_section_offset (&attr))
18720 dwarf2_complex_location_expr_complaint ();
18724 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18725 "partial symbol information");
18728 case DW_AT_external:
18729 is_external = DW_UNSND (&attr);
18731 case DW_AT_declaration:
18732 is_declaration = DW_UNSND (&attr);
18737 case DW_AT_abstract_origin:
18738 case DW_AT_specification:
18739 case DW_AT_extension:
18740 has_specification = 1;
18741 spec_offset = dwarf2_get_ref_die_offset (&attr);
18742 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18743 || cu->per_cu->is_dwz);
18745 case DW_AT_sibling:
18746 /* Ignore absolute siblings, they might point outside of
18747 the current compile unit. */
18748 if (attr.form == DW_FORM_ref_addr)
18749 complaint (_("ignoring absolute DW_AT_sibling"));
18752 const gdb_byte *buffer = reader->buffer;
18753 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18754 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18756 if (sibling_ptr < info_ptr)
18757 complaint (_("DW_AT_sibling points backwards"));
18758 else if (sibling_ptr > reader->buffer_end)
18759 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18761 sibling = sibling_ptr;
18764 case DW_AT_byte_size:
18767 case DW_AT_const_value:
18768 has_const_value = 1;
18770 case DW_AT_calling_convention:
18771 /* DWARF doesn't provide a way to identify a program's source-level
18772 entry point. DW_AT_calling_convention attributes are only meant
18773 to describe functions' calling conventions.
18775 However, because it's a necessary piece of information in
18776 Fortran, and before DWARF 4 DW_CC_program was the only
18777 piece of debugging information whose definition refers to
18778 a 'main program' at all, several compilers marked Fortran
18779 main programs with DW_CC_program --- even when those
18780 functions use the standard calling conventions.
18782 Although DWARF now specifies a way to provide this
18783 information, we support this practice for backward
18785 if (DW_UNSND (&attr) == DW_CC_program
18786 && cu->language == language_fortran)
18787 main_subprogram = 1;
18790 if (DW_UNSND (&attr) == DW_INL_inlined
18791 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18792 may_be_inlined = 1;
18796 if (tag == DW_TAG_imported_unit)
18798 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18799 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18800 || cu->per_cu->is_dwz);
18804 case DW_AT_main_subprogram:
18805 main_subprogram = DW_UNSND (&attr);
18810 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18811 but that requires a full DIE, so instead we just
18813 int need_ranges_base = tag != DW_TAG_compile_unit;
18814 unsigned int ranges_offset = (DW_UNSND (&attr)
18815 + (need_ranges_base
18819 /* Value of the DW_AT_ranges attribute is the offset in the
18820 .debug_ranges section. */
18821 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18832 /* For Ada, if both the name and the linkage name appear, we prefer
18833 the latter. This lets "catch exception" work better, regardless
18834 of the order in which the name and linkage name were emitted.
18835 Really, though, this is just a workaround for the fact that gdb
18836 doesn't store both the name and the linkage name. */
18837 if (cu->language == language_ada && linkage_name != nullptr)
18838 name = linkage_name;
18840 if (high_pc_relative)
18843 if (has_low_pc_attr && has_high_pc_attr)
18845 /* When using the GNU linker, .gnu.linkonce. sections are used to
18846 eliminate duplicate copies of functions and vtables and such.
18847 The linker will arbitrarily choose one and discard the others.
18848 The AT_*_pc values for such functions refer to local labels in
18849 these sections. If the section from that file was discarded, the
18850 labels are not in the output, so the relocs get a value of 0.
18851 If this is a discarded function, mark the pc bounds as invalid,
18852 so that GDB will ignore it. */
18853 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18855 struct objfile *objfile = dwarf2_per_objfile->objfile;
18856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18858 complaint (_("DW_AT_low_pc %s is zero "
18859 "for DIE at %s [in module %s]"),
18860 paddress (gdbarch, lowpc),
18861 sect_offset_str (sect_off),
18862 objfile_name (objfile));
18864 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18865 else if (lowpc >= highpc)
18867 struct objfile *objfile = dwarf2_per_objfile->objfile;
18868 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18870 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18871 "for DIE at %s [in module %s]"),
18872 paddress (gdbarch, lowpc),
18873 paddress (gdbarch, highpc),
18874 sect_offset_str (sect_off),
18875 objfile_name (objfile));
18884 /* Find a cached partial DIE at OFFSET in CU. */
18886 struct partial_die_info *
18887 dwarf2_cu::find_partial_die (sect_offset sect_off)
18889 struct partial_die_info *lookup_die = NULL;
18890 struct partial_die_info part_die (sect_off);
18892 lookup_die = ((struct partial_die_info *)
18893 htab_find_with_hash (partial_dies, &part_die,
18894 to_underlying (sect_off)));
18899 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18900 except in the case of .debug_types DIEs which do not reference
18901 outside their CU (they do however referencing other types via
18902 DW_FORM_ref_sig8). */
18904 static const struct cu_partial_die_info
18905 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18907 struct dwarf2_per_objfile *dwarf2_per_objfile
18908 = cu->per_cu->dwarf2_per_objfile;
18909 struct objfile *objfile = dwarf2_per_objfile->objfile;
18910 struct dwarf2_per_cu_data *per_cu = NULL;
18911 struct partial_die_info *pd = NULL;
18913 if (offset_in_dwz == cu->per_cu->is_dwz
18914 && offset_in_cu_p (&cu->header, sect_off))
18916 pd = cu->find_partial_die (sect_off);
18919 /* We missed recording what we needed.
18920 Load all dies and try again. */
18921 per_cu = cu->per_cu;
18925 /* TUs don't reference other CUs/TUs (except via type signatures). */
18926 if (cu->per_cu->is_debug_types)
18928 error (_("Dwarf Error: Type Unit at offset %s contains"
18929 " external reference to offset %s [in module %s].\n"),
18930 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18931 bfd_get_filename (objfile->obfd));
18933 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18934 dwarf2_per_objfile);
18936 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18937 load_partial_comp_unit (per_cu);
18939 per_cu->cu->last_used = 0;
18940 pd = per_cu->cu->find_partial_die (sect_off);
18943 /* If we didn't find it, and not all dies have been loaded,
18944 load them all and try again. */
18946 if (pd == NULL && per_cu->load_all_dies == 0)
18948 per_cu->load_all_dies = 1;
18950 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18951 THIS_CU->cu may already be in use. So we can't just free it and
18952 replace its DIEs with the ones we read in. Instead, we leave those
18953 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18954 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18956 load_partial_comp_unit (per_cu);
18958 pd = per_cu->cu->find_partial_die (sect_off);
18962 internal_error (__FILE__, __LINE__,
18963 _("could not find partial DIE %s "
18964 "in cache [from module %s]\n"),
18965 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18966 return { per_cu->cu, pd };
18969 /* See if we can figure out if the class lives in a namespace. We do
18970 this by looking for a member function; its demangled name will
18971 contain namespace info, if there is any. */
18974 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18975 struct dwarf2_cu *cu)
18977 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18978 what template types look like, because the demangler
18979 frequently doesn't give the same name as the debug info. We
18980 could fix this by only using the demangled name to get the
18981 prefix (but see comment in read_structure_type). */
18983 struct partial_die_info *real_pdi;
18984 struct partial_die_info *child_pdi;
18986 /* If this DIE (this DIE's specification, if any) has a parent, then
18987 we should not do this. We'll prepend the parent's fully qualified
18988 name when we create the partial symbol. */
18990 real_pdi = struct_pdi;
18991 while (real_pdi->has_specification)
18993 auto res = find_partial_die (real_pdi->spec_offset,
18994 real_pdi->spec_is_dwz, cu);
18995 real_pdi = res.pdi;
18999 if (real_pdi->die_parent != NULL)
19002 for (child_pdi = struct_pdi->die_child;
19004 child_pdi = child_pdi->die_sibling)
19006 if (child_pdi->tag == DW_TAG_subprogram
19007 && child_pdi->linkage_name != NULL)
19009 char *actual_class_name
19010 = language_class_name_from_physname (cu->language_defn,
19011 child_pdi->linkage_name);
19012 if (actual_class_name != NULL)
19014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19016 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19017 actual_class_name);
19018 xfree (actual_class_name);
19026 partial_die_info::fixup (struct dwarf2_cu *cu)
19028 /* Once we've fixed up a die, there's no point in doing so again.
19029 This also avoids a memory leak if we were to call
19030 guess_partial_die_structure_name multiple times. */
19034 /* If we found a reference attribute and the DIE has no name, try
19035 to find a name in the referred to DIE. */
19037 if (name == NULL && has_specification)
19039 struct partial_die_info *spec_die;
19041 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19042 spec_die = res.pdi;
19045 spec_die->fixup (cu);
19047 if (spec_die->name)
19049 name = spec_die->name;
19051 /* Copy DW_AT_external attribute if it is set. */
19052 if (spec_die->is_external)
19053 is_external = spec_die->is_external;
19057 /* Set default names for some unnamed DIEs. */
19059 if (name == NULL && tag == DW_TAG_namespace)
19060 name = CP_ANONYMOUS_NAMESPACE_STR;
19062 /* If there is no parent die to provide a namespace, and there are
19063 children, see if we can determine the namespace from their linkage
19065 if (cu->language == language_cplus
19066 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19067 && die_parent == NULL
19069 && (tag == DW_TAG_class_type
19070 || tag == DW_TAG_structure_type
19071 || tag == DW_TAG_union_type))
19072 guess_partial_die_structure_name (this, cu);
19074 /* GCC might emit a nameless struct or union that has a linkage
19075 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19077 && (tag == DW_TAG_class_type
19078 || tag == DW_TAG_interface_type
19079 || tag == DW_TAG_structure_type
19080 || tag == DW_TAG_union_type)
19081 && linkage_name != NULL)
19085 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19090 /* Strip any leading namespaces/classes, keep only the base name.
19091 DW_AT_name for named DIEs does not contain the prefixes. */
19092 base = strrchr (demangled, ':');
19093 if (base && base > demangled && base[-1] == ':')
19098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19099 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19107 /* Read an attribute value described by an attribute form. */
19109 static const gdb_byte *
19110 read_attribute_value (const struct die_reader_specs *reader,
19111 struct attribute *attr, unsigned form,
19112 LONGEST implicit_const, const gdb_byte *info_ptr)
19114 struct dwarf2_cu *cu = reader->cu;
19115 struct dwarf2_per_objfile *dwarf2_per_objfile
19116 = cu->per_cu->dwarf2_per_objfile;
19117 struct objfile *objfile = dwarf2_per_objfile->objfile;
19118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19119 bfd *abfd = reader->abfd;
19120 struct comp_unit_head *cu_header = &cu->header;
19121 unsigned int bytes_read;
19122 struct dwarf_block *blk;
19124 attr->form = (enum dwarf_form) form;
19127 case DW_FORM_ref_addr:
19128 if (cu->header.version == 2)
19129 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19131 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19132 &cu->header, &bytes_read);
19133 info_ptr += bytes_read;
19135 case DW_FORM_GNU_ref_alt:
19136 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19137 info_ptr += bytes_read;
19140 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19141 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19142 info_ptr += bytes_read;
19144 case DW_FORM_block2:
19145 blk = dwarf_alloc_block (cu);
19146 blk->size = read_2_bytes (abfd, info_ptr);
19148 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19149 info_ptr += blk->size;
19150 DW_BLOCK (attr) = blk;
19152 case DW_FORM_block4:
19153 blk = dwarf_alloc_block (cu);
19154 blk->size = read_4_bytes (abfd, info_ptr);
19156 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19157 info_ptr += blk->size;
19158 DW_BLOCK (attr) = blk;
19160 case DW_FORM_data2:
19161 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19164 case DW_FORM_data4:
19165 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19168 case DW_FORM_data8:
19169 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19172 case DW_FORM_data16:
19173 blk = dwarf_alloc_block (cu);
19175 blk->data = read_n_bytes (abfd, info_ptr, 16);
19177 DW_BLOCK (attr) = blk;
19179 case DW_FORM_sec_offset:
19180 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19181 info_ptr += bytes_read;
19183 case DW_FORM_string:
19184 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19185 DW_STRING_IS_CANONICAL (attr) = 0;
19186 info_ptr += bytes_read;
19189 if (!cu->per_cu->is_dwz)
19191 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19192 abfd, info_ptr, cu_header,
19194 DW_STRING_IS_CANONICAL (attr) = 0;
19195 info_ptr += bytes_read;
19199 case DW_FORM_line_strp:
19200 if (!cu->per_cu->is_dwz)
19202 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19204 cu_header, &bytes_read);
19205 DW_STRING_IS_CANONICAL (attr) = 0;
19206 info_ptr += bytes_read;
19210 case DW_FORM_GNU_strp_alt:
19212 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19213 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19216 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19218 DW_STRING_IS_CANONICAL (attr) = 0;
19219 info_ptr += bytes_read;
19222 case DW_FORM_exprloc:
19223 case DW_FORM_block:
19224 blk = dwarf_alloc_block (cu);
19225 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19226 info_ptr += bytes_read;
19227 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19228 info_ptr += blk->size;
19229 DW_BLOCK (attr) = blk;
19231 case DW_FORM_block1:
19232 blk = dwarf_alloc_block (cu);
19233 blk->size = read_1_byte (abfd, info_ptr);
19235 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19236 info_ptr += blk->size;
19237 DW_BLOCK (attr) = blk;
19239 case DW_FORM_data1:
19240 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19244 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19247 case DW_FORM_flag_present:
19248 DW_UNSND (attr) = 1;
19250 case DW_FORM_sdata:
19251 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19252 info_ptr += bytes_read;
19254 case DW_FORM_udata:
19255 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19256 info_ptr += bytes_read;
19259 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19260 + read_1_byte (abfd, info_ptr));
19264 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19265 + read_2_bytes (abfd, info_ptr));
19269 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19270 + read_4_bytes (abfd, info_ptr));
19274 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19275 + read_8_bytes (abfd, info_ptr));
19278 case DW_FORM_ref_sig8:
19279 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19282 case DW_FORM_ref_udata:
19283 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19284 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19285 info_ptr += bytes_read;
19287 case DW_FORM_indirect:
19288 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 if (form == DW_FORM_implicit_const)
19292 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19293 info_ptr += bytes_read;
19295 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19298 case DW_FORM_implicit_const:
19299 DW_SND (attr) = implicit_const;
19301 case DW_FORM_addrx:
19302 case DW_FORM_GNU_addr_index:
19303 if (reader->dwo_file == NULL)
19305 /* For now flag a hard error.
19306 Later we can turn this into a complaint. */
19307 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19308 dwarf_form_name (form),
19309 bfd_get_filename (abfd));
19311 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19312 info_ptr += bytes_read;
19315 case DW_FORM_strx1:
19316 case DW_FORM_strx2:
19317 case DW_FORM_strx3:
19318 case DW_FORM_strx4:
19319 case DW_FORM_GNU_str_index:
19320 if (reader->dwo_file == NULL)
19322 /* For now flag a hard error.
19323 Later we can turn this into a complaint if warranted. */
19324 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19325 dwarf_form_name (form),
19326 bfd_get_filename (abfd));
19329 ULONGEST str_index;
19330 if (form == DW_FORM_strx1)
19332 str_index = read_1_byte (abfd, info_ptr);
19335 else if (form == DW_FORM_strx2)
19337 str_index = read_2_bytes (abfd, info_ptr);
19340 else if (form == DW_FORM_strx3)
19342 str_index = read_3_bytes (abfd, info_ptr);
19345 else if (form == DW_FORM_strx4)
19347 str_index = read_4_bytes (abfd, info_ptr);
19352 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19353 info_ptr += bytes_read;
19355 DW_STRING (attr) = read_str_index (reader, str_index);
19356 DW_STRING_IS_CANONICAL (attr) = 0;
19360 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19361 dwarf_form_name (form),
19362 bfd_get_filename (abfd));
19366 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19367 attr->form = DW_FORM_GNU_ref_alt;
19369 /* We have seen instances where the compiler tried to emit a byte
19370 size attribute of -1 which ended up being encoded as an unsigned
19371 0xffffffff. Although 0xffffffff is technically a valid size value,
19372 an object of this size seems pretty unlikely so we can relatively
19373 safely treat these cases as if the size attribute was invalid and
19374 treat them as zero by default. */
19375 if (attr->name == DW_AT_byte_size
19376 && form == DW_FORM_data4
19377 && DW_UNSND (attr) >= 0xffffffff)
19380 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19381 hex_string (DW_UNSND (attr)));
19382 DW_UNSND (attr) = 0;
19388 /* Read an attribute described by an abbreviated attribute. */
19390 static const gdb_byte *
19391 read_attribute (const struct die_reader_specs *reader,
19392 struct attribute *attr, struct attr_abbrev *abbrev,
19393 const gdb_byte *info_ptr)
19395 attr->name = abbrev->name;
19396 return read_attribute_value (reader, attr, abbrev->form,
19397 abbrev->implicit_const, info_ptr);
19400 /* Read dwarf information from a buffer. */
19402 static unsigned int
19403 read_1_byte (bfd *abfd, const gdb_byte *buf)
19405 return bfd_get_8 (abfd, buf);
19409 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19411 return bfd_get_signed_8 (abfd, buf);
19414 static unsigned int
19415 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19417 return bfd_get_16 (abfd, buf);
19421 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19423 return bfd_get_signed_16 (abfd, buf);
19426 static unsigned int
19427 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19429 unsigned int result = 0;
19430 for (int i = 0; i < 3; ++i)
19432 unsigned char byte = bfd_get_8 (abfd, buf);
19434 result |= ((unsigned int) byte << (i * 8));
19439 static unsigned int
19440 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19442 return bfd_get_32 (abfd, buf);
19446 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19448 return bfd_get_signed_32 (abfd, buf);
19452 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19454 return bfd_get_64 (abfd, buf);
19458 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19459 unsigned int *bytes_read)
19461 struct comp_unit_head *cu_header = &cu->header;
19462 CORE_ADDR retval = 0;
19464 if (cu_header->signed_addr_p)
19466 switch (cu_header->addr_size)
19469 retval = bfd_get_signed_16 (abfd, buf);
19472 retval = bfd_get_signed_32 (abfd, buf);
19475 retval = bfd_get_signed_64 (abfd, buf);
19478 internal_error (__FILE__, __LINE__,
19479 _("read_address: bad switch, signed [in module %s]"),
19480 bfd_get_filename (abfd));
19485 switch (cu_header->addr_size)
19488 retval = bfd_get_16 (abfd, buf);
19491 retval = bfd_get_32 (abfd, buf);
19494 retval = bfd_get_64 (abfd, buf);
19497 internal_error (__FILE__, __LINE__,
19498 _("read_address: bad switch, "
19499 "unsigned [in module %s]"),
19500 bfd_get_filename (abfd));
19504 *bytes_read = cu_header->addr_size;
19508 /* Read the initial length from a section. The (draft) DWARF 3
19509 specification allows the initial length to take up either 4 bytes
19510 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19511 bytes describe the length and all offsets will be 8 bytes in length
19514 An older, non-standard 64-bit format is also handled by this
19515 function. The older format in question stores the initial length
19516 as an 8-byte quantity without an escape value. Lengths greater
19517 than 2^32 aren't very common which means that the initial 4 bytes
19518 is almost always zero. Since a length value of zero doesn't make
19519 sense for the 32-bit format, this initial zero can be considered to
19520 be an escape value which indicates the presence of the older 64-bit
19521 format. As written, the code can't detect (old format) lengths
19522 greater than 4GB. If it becomes necessary to handle lengths
19523 somewhat larger than 4GB, we could allow other small values (such
19524 as the non-sensical values of 1, 2, and 3) to also be used as
19525 escape values indicating the presence of the old format.
19527 The value returned via bytes_read should be used to increment the
19528 relevant pointer after calling read_initial_length().
19530 [ Note: read_initial_length() and read_offset() are based on the
19531 document entitled "DWARF Debugging Information Format", revision
19532 3, draft 8, dated November 19, 2001. This document was obtained
19535 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19537 This document is only a draft and is subject to change. (So beware.)
19539 Details regarding the older, non-standard 64-bit format were
19540 determined empirically by examining 64-bit ELF files produced by
19541 the SGI toolchain on an IRIX 6.5 machine.
19543 - Kevin, July 16, 2002
19547 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19549 LONGEST length = bfd_get_32 (abfd, buf);
19551 if (length == 0xffffffff)
19553 length = bfd_get_64 (abfd, buf + 4);
19556 else if (length == 0)
19558 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19559 length = bfd_get_64 (abfd, buf);
19570 /* Cover function for read_initial_length.
19571 Returns the length of the object at BUF, and stores the size of the
19572 initial length in *BYTES_READ and stores the size that offsets will be in
19574 If the initial length size is not equivalent to that specified in
19575 CU_HEADER then issue a complaint.
19576 This is useful when reading non-comp-unit headers. */
19579 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19580 const struct comp_unit_head *cu_header,
19581 unsigned int *bytes_read,
19582 unsigned int *offset_size)
19584 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19586 gdb_assert (cu_header->initial_length_size == 4
19587 || cu_header->initial_length_size == 8
19588 || cu_header->initial_length_size == 12);
19590 if (cu_header->initial_length_size != *bytes_read)
19591 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19593 *offset_size = (*bytes_read == 4) ? 4 : 8;
19597 /* Read an offset from the data stream. The size of the offset is
19598 given by cu_header->offset_size. */
19601 read_offset (bfd *abfd, const gdb_byte *buf,
19602 const struct comp_unit_head *cu_header,
19603 unsigned int *bytes_read)
19605 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19607 *bytes_read = cu_header->offset_size;
19611 /* Read an offset from the data stream. */
19614 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19616 LONGEST retval = 0;
19618 switch (offset_size)
19621 retval = bfd_get_32 (abfd, buf);
19624 retval = bfd_get_64 (abfd, buf);
19627 internal_error (__FILE__, __LINE__,
19628 _("read_offset_1: bad switch [in module %s]"),
19629 bfd_get_filename (abfd));
19635 static const gdb_byte *
19636 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19638 /* If the size of a host char is 8 bits, we can return a pointer
19639 to the buffer, otherwise we have to copy the data to a buffer
19640 allocated on the temporary obstack. */
19641 gdb_assert (HOST_CHAR_BIT == 8);
19645 static const char *
19646 read_direct_string (bfd *abfd, const gdb_byte *buf,
19647 unsigned int *bytes_read_ptr)
19649 /* If the size of a host char is 8 bits, we can return a pointer
19650 to the string, otherwise we have to copy the string to a buffer
19651 allocated on the temporary obstack. */
19652 gdb_assert (HOST_CHAR_BIT == 8);
19655 *bytes_read_ptr = 1;
19658 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19659 return (const char *) buf;
19662 /* Return pointer to string at section SECT offset STR_OFFSET with error
19663 reporting strings FORM_NAME and SECT_NAME. */
19665 static const char *
19666 read_indirect_string_at_offset_from (struct objfile *objfile,
19667 bfd *abfd, LONGEST str_offset,
19668 struct dwarf2_section_info *sect,
19669 const char *form_name,
19670 const char *sect_name)
19672 dwarf2_read_section (objfile, sect);
19673 if (sect->buffer == NULL)
19674 error (_("%s used without %s section [in module %s]"),
19675 form_name, sect_name, bfd_get_filename (abfd));
19676 if (str_offset >= sect->size)
19677 error (_("%s pointing outside of %s section [in module %s]"),
19678 form_name, sect_name, bfd_get_filename (abfd));
19679 gdb_assert (HOST_CHAR_BIT == 8);
19680 if (sect->buffer[str_offset] == '\0')
19682 return (const char *) (sect->buffer + str_offset);
19685 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19687 static const char *
19688 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19689 bfd *abfd, LONGEST str_offset)
19691 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19693 &dwarf2_per_objfile->str,
19694 "DW_FORM_strp", ".debug_str");
19697 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19699 static const char *
19700 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19701 bfd *abfd, LONGEST str_offset)
19703 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19705 &dwarf2_per_objfile->line_str,
19706 "DW_FORM_line_strp",
19707 ".debug_line_str");
19710 /* Read a string at offset STR_OFFSET in the .debug_str section from
19711 the .dwz file DWZ. Throw an error if the offset is too large. If
19712 the string consists of a single NUL byte, return NULL; otherwise
19713 return a pointer to the string. */
19715 static const char *
19716 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19717 LONGEST str_offset)
19719 dwarf2_read_section (objfile, &dwz->str);
19721 if (dwz->str.buffer == NULL)
19722 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19723 "section [in module %s]"),
19724 bfd_get_filename (dwz->dwz_bfd));
19725 if (str_offset >= dwz->str.size)
19726 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19727 ".debug_str section [in module %s]"),
19728 bfd_get_filename (dwz->dwz_bfd));
19729 gdb_assert (HOST_CHAR_BIT == 8);
19730 if (dwz->str.buffer[str_offset] == '\0')
19732 return (const char *) (dwz->str.buffer + str_offset);
19735 /* Return pointer to string at .debug_str offset as read from BUF.
19736 BUF is assumed to be in a compilation unit described by CU_HEADER.
19737 Return *BYTES_READ_PTR count of bytes read from BUF. */
19739 static const char *
19740 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19741 const gdb_byte *buf,
19742 const struct comp_unit_head *cu_header,
19743 unsigned int *bytes_read_ptr)
19745 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19747 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19750 /* Return pointer to string at .debug_line_str offset as read from BUF.
19751 BUF is assumed to be in a compilation unit described by CU_HEADER.
19752 Return *BYTES_READ_PTR count of bytes read from BUF. */
19754 static const char *
19755 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19756 bfd *abfd, const gdb_byte *buf,
19757 const struct comp_unit_head *cu_header,
19758 unsigned int *bytes_read_ptr)
19760 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19762 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19767 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19768 unsigned int *bytes_read_ptr)
19771 unsigned int num_read;
19773 unsigned char byte;
19780 byte = bfd_get_8 (abfd, buf);
19783 result |= ((ULONGEST) (byte & 127) << shift);
19784 if ((byte & 128) == 0)
19790 *bytes_read_ptr = num_read;
19795 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19796 unsigned int *bytes_read_ptr)
19799 int shift, num_read;
19800 unsigned char byte;
19807 byte = bfd_get_8 (abfd, buf);
19810 result |= ((ULONGEST) (byte & 127) << shift);
19812 if ((byte & 128) == 0)
19817 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19818 result |= -(((ULONGEST) 1) << shift);
19819 *bytes_read_ptr = num_read;
19823 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19824 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19825 ADDR_SIZE is the size of addresses from the CU header. */
19828 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19829 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19831 struct objfile *objfile = dwarf2_per_objfile->objfile;
19832 bfd *abfd = objfile->obfd;
19833 const gdb_byte *info_ptr;
19835 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19836 if (dwarf2_per_objfile->addr.buffer == NULL)
19837 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19838 objfile_name (objfile));
19839 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19840 error (_("DW_FORM_addr_index pointing outside of "
19841 ".debug_addr section [in module %s]"),
19842 objfile_name (objfile));
19843 info_ptr = (dwarf2_per_objfile->addr.buffer
19844 + addr_base + addr_index * addr_size);
19845 if (addr_size == 4)
19846 return bfd_get_32 (abfd, info_ptr);
19848 return bfd_get_64 (abfd, info_ptr);
19851 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19854 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19856 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19857 cu->addr_base, cu->header.addr_size);
19860 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19863 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19864 unsigned int *bytes_read)
19866 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19867 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19869 return read_addr_index (cu, addr_index);
19872 /* Data structure to pass results from dwarf2_read_addr_index_reader
19873 back to dwarf2_read_addr_index. */
19875 struct dwarf2_read_addr_index_data
19877 ULONGEST addr_base;
19881 /* die_reader_func for dwarf2_read_addr_index. */
19884 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19885 const gdb_byte *info_ptr,
19886 struct die_info *comp_unit_die,
19890 struct dwarf2_cu *cu = reader->cu;
19891 struct dwarf2_read_addr_index_data *aidata =
19892 (struct dwarf2_read_addr_index_data *) data;
19894 aidata->addr_base = cu->addr_base;
19895 aidata->addr_size = cu->header.addr_size;
19898 /* Given an index in .debug_addr, fetch the value.
19899 NOTE: This can be called during dwarf expression evaluation,
19900 long after the debug information has been read, and thus per_cu->cu
19901 may no longer exist. */
19904 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19905 unsigned int addr_index)
19907 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19908 struct dwarf2_cu *cu = per_cu->cu;
19909 ULONGEST addr_base;
19912 /* We need addr_base and addr_size.
19913 If we don't have PER_CU->cu, we have to get it.
19914 Nasty, but the alternative is storing the needed info in PER_CU,
19915 which at this point doesn't seem justified: it's not clear how frequently
19916 it would get used and it would increase the size of every PER_CU.
19917 Entry points like dwarf2_per_cu_addr_size do a similar thing
19918 so we're not in uncharted territory here.
19919 Alas we need to be a bit more complicated as addr_base is contained
19922 We don't need to read the entire CU(/TU).
19923 We just need the header and top level die.
19925 IWBN to use the aging mechanism to let us lazily later discard the CU.
19926 For now we skip this optimization. */
19930 addr_base = cu->addr_base;
19931 addr_size = cu->header.addr_size;
19935 struct dwarf2_read_addr_index_data aidata;
19937 /* Note: We can't use init_cutu_and_read_dies_simple here,
19938 we need addr_base. */
19939 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19940 dwarf2_read_addr_index_reader, &aidata);
19941 addr_base = aidata.addr_base;
19942 addr_size = aidata.addr_size;
19945 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19949 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
19950 This is only used by the Fission support. */
19952 static const char *
19953 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19955 struct dwarf2_cu *cu = reader->cu;
19956 struct dwarf2_per_objfile *dwarf2_per_objfile
19957 = cu->per_cu->dwarf2_per_objfile;
19958 struct objfile *objfile = dwarf2_per_objfile->objfile;
19959 const char *objf_name = objfile_name (objfile);
19960 bfd *abfd = objfile->obfd;
19961 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19962 struct dwarf2_section_info *str_offsets_section =
19963 &reader->dwo_file->sections.str_offsets;
19964 const gdb_byte *info_ptr;
19965 ULONGEST str_offset;
19966 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19968 dwarf2_read_section (objfile, str_section);
19969 dwarf2_read_section (objfile, str_offsets_section);
19970 if (str_section->buffer == NULL)
19971 error (_("%s used without .debug_str.dwo section"
19972 " in CU at offset %s [in module %s]"),
19973 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19974 if (str_offsets_section->buffer == NULL)
19975 error (_("%s used without .debug_str_offsets.dwo section"
19976 " in CU at offset %s [in module %s]"),
19977 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19978 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19979 error (_("%s pointing outside of .debug_str_offsets.dwo"
19980 " section in CU at offset %s [in module %s]"),
19981 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19982 info_ptr = (str_offsets_section->buffer
19983 + str_index * cu->header.offset_size);
19984 if (cu->header.offset_size == 4)
19985 str_offset = bfd_get_32 (abfd, info_ptr);
19987 str_offset = bfd_get_64 (abfd, info_ptr);
19988 if (str_offset >= str_section->size)
19989 error (_("Offset from %s pointing outside of"
19990 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19991 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19992 return (const char *) (str_section->buffer + str_offset);
19995 /* Return the length of an LEB128 number in BUF. */
19998 leb128_size (const gdb_byte *buf)
20000 const gdb_byte *begin = buf;
20006 if ((byte & 128) == 0)
20007 return buf - begin;
20012 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20021 cu->language = language_c;
20024 case DW_LANG_C_plus_plus:
20025 case DW_LANG_C_plus_plus_11:
20026 case DW_LANG_C_plus_plus_14:
20027 cu->language = language_cplus;
20030 cu->language = language_d;
20032 case DW_LANG_Fortran77:
20033 case DW_LANG_Fortran90:
20034 case DW_LANG_Fortran95:
20035 case DW_LANG_Fortran03:
20036 case DW_LANG_Fortran08:
20037 cu->language = language_fortran;
20040 cu->language = language_go;
20042 case DW_LANG_Mips_Assembler:
20043 cu->language = language_asm;
20045 case DW_LANG_Ada83:
20046 case DW_LANG_Ada95:
20047 cu->language = language_ada;
20049 case DW_LANG_Modula2:
20050 cu->language = language_m2;
20052 case DW_LANG_Pascal83:
20053 cu->language = language_pascal;
20056 cu->language = language_objc;
20059 case DW_LANG_Rust_old:
20060 cu->language = language_rust;
20062 case DW_LANG_Cobol74:
20063 case DW_LANG_Cobol85:
20065 cu->language = language_minimal;
20068 cu->language_defn = language_def (cu->language);
20071 /* Return the named attribute or NULL if not there. */
20073 static struct attribute *
20074 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20079 struct attribute *spec = NULL;
20081 for (i = 0; i < die->num_attrs; ++i)
20083 if (die->attrs[i].name == name)
20084 return &die->attrs[i];
20085 if (die->attrs[i].name == DW_AT_specification
20086 || die->attrs[i].name == DW_AT_abstract_origin)
20087 spec = &die->attrs[i];
20093 die = follow_die_ref (die, spec, &cu);
20099 /* Return the named attribute or NULL if not there,
20100 but do not follow DW_AT_specification, etc.
20101 This is for use in contexts where we're reading .debug_types dies.
20102 Following DW_AT_specification, DW_AT_abstract_origin will take us
20103 back up the chain, and we want to go down. */
20105 static struct attribute *
20106 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20110 for (i = 0; i < die->num_attrs; ++i)
20111 if (die->attrs[i].name == name)
20112 return &die->attrs[i];
20117 /* Return the string associated with a string-typed attribute, or NULL if it
20118 is either not found or is of an incorrect type. */
20120 static const char *
20121 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20123 struct attribute *attr;
20124 const char *str = NULL;
20126 attr = dwarf2_attr (die, name, cu);
20130 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20131 || attr->form == DW_FORM_string
20132 || attr->form == DW_FORM_strx
20133 || attr->form == DW_FORM_GNU_str_index
20134 || attr->form == DW_FORM_GNU_strp_alt)
20135 str = DW_STRING (attr);
20137 complaint (_("string type expected for attribute %s for "
20138 "DIE at %s in module %s"),
20139 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20140 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20146 /* Return the dwo name or NULL if not present. If present, it is in either
20147 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20148 static const char *
20149 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20151 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20152 if (dwo_name == nullptr)
20153 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20157 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20158 and holds a non-zero value. This function should only be used for
20159 DW_FORM_flag or DW_FORM_flag_present attributes. */
20162 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20164 struct attribute *attr = dwarf2_attr (die, name, cu);
20166 return (attr && DW_UNSND (attr));
20170 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20172 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20173 which value is non-zero. However, we have to be careful with
20174 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20175 (via dwarf2_flag_true_p) follows this attribute. So we may
20176 end up accidently finding a declaration attribute that belongs
20177 to a different DIE referenced by the specification attribute,
20178 even though the given DIE does not have a declaration attribute. */
20179 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20180 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20183 /* Return the die giving the specification for DIE, if there is
20184 one. *SPEC_CU is the CU containing DIE on input, and the CU
20185 containing the return value on output. If there is no
20186 specification, but there is an abstract origin, that is
20189 static struct die_info *
20190 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20192 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20195 if (spec_attr == NULL)
20196 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20198 if (spec_attr == NULL)
20201 return follow_die_ref (die, spec_attr, spec_cu);
20204 /* Stub for free_line_header to match void * callback types. */
20207 free_line_header_voidp (void *arg)
20209 struct line_header *lh = (struct line_header *) arg;
20215 line_header::add_include_dir (const char *include_dir)
20217 if (dwarf_line_debug >= 2)
20218 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20219 include_dirs.size () + 1, include_dir);
20221 include_dirs.push_back (include_dir);
20225 line_header::add_file_name (const char *name,
20227 unsigned int mod_time,
20228 unsigned int length)
20230 if (dwarf_line_debug >= 2)
20231 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20232 (unsigned) file_names.size () + 1, name);
20234 file_names.emplace_back (name, d_index, mod_time, length);
20237 /* A convenience function to find the proper .debug_line section for a CU. */
20239 static struct dwarf2_section_info *
20240 get_debug_line_section (struct dwarf2_cu *cu)
20242 struct dwarf2_section_info *section;
20243 struct dwarf2_per_objfile *dwarf2_per_objfile
20244 = cu->per_cu->dwarf2_per_objfile;
20246 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20248 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20249 section = &cu->dwo_unit->dwo_file->sections.line;
20250 else if (cu->per_cu->is_dwz)
20252 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20254 section = &dwz->line;
20257 section = &dwarf2_per_objfile->line;
20262 /* Read directory or file name entry format, starting with byte of
20263 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20264 entries count and the entries themselves in the described entry
20268 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20269 bfd *abfd, const gdb_byte **bufp,
20270 struct line_header *lh,
20271 const struct comp_unit_head *cu_header,
20272 void (*callback) (struct line_header *lh,
20275 unsigned int mod_time,
20276 unsigned int length))
20278 gdb_byte format_count, formati;
20279 ULONGEST data_count, datai;
20280 const gdb_byte *buf = *bufp;
20281 const gdb_byte *format_header_data;
20282 unsigned int bytes_read;
20284 format_count = read_1_byte (abfd, buf);
20286 format_header_data = buf;
20287 for (formati = 0; formati < format_count; formati++)
20289 read_unsigned_leb128 (abfd, buf, &bytes_read);
20291 read_unsigned_leb128 (abfd, buf, &bytes_read);
20295 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20297 for (datai = 0; datai < data_count; datai++)
20299 const gdb_byte *format = format_header_data;
20300 struct file_entry fe;
20302 for (formati = 0; formati < format_count; formati++)
20304 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20305 format += bytes_read;
20307 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20308 format += bytes_read;
20310 gdb::optional<const char *> string;
20311 gdb::optional<unsigned int> uint;
20315 case DW_FORM_string:
20316 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20320 case DW_FORM_line_strp:
20321 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20328 case DW_FORM_data1:
20329 uint.emplace (read_1_byte (abfd, buf));
20333 case DW_FORM_data2:
20334 uint.emplace (read_2_bytes (abfd, buf));
20338 case DW_FORM_data4:
20339 uint.emplace (read_4_bytes (abfd, buf));
20343 case DW_FORM_data8:
20344 uint.emplace (read_8_bytes (abfd, buf));
20348 case DW_FORM_udata:
20349 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20353 case DW_FORM_block:
20354 /* It is valid only for DW_LNCT_timestamp which is ignored by
20359 switch (content_type)
20362 if (string.has_value ())
20365 case DW_LNCT_directory_index:
20366 if (uint.has_value ())
20367 fe.d_index = (dir_index) *uint;
20369 case DW_LNCT_timestamp:
20370 if (uint.has_value ())
20371 fe.mod_time = *uint;
20374 if (uint.has_value ())
20380 complaint (_("Unknown format content type %s"),
20381 pulongest (content_type));
20385 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20391 /* Read the statement program header starting at OFFSET in
20392 .debug_line, or .debug_line.dwo. Return a pointer
20393 to a struct line_header, allocated using xmalloc.
20394 Returns NULL if there is a problem reading the header, e.g., if it
20395 has a version we don't understand.
20397 NOTE: the strings in the include directory and file name tables of
20398 the returned object point into the dwarf line section buffer,
20399 and must not be freed. */
20401 static line_header_up
20402 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20404 const gdb_byte *line_ptr;
20405 unsigned int bytes_read, offset_size;
20407 const char *cur_dir, *cur_file;
20408 struct dwarf2_section_info *section;
20410 struct dwarf2_per_objfile *dwarf2_per_objfile
20411 = cu->per_cu->dwarf2_per_objfile;
20413 section = get_debug_line_section (cu);
20414 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20415 if (section->buffer == NULL)
20417 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20418 complaint (_("missing .debug_line.dwo section"));
20420 complaint (_("missing .debug_line section"));
20424 /* We can't do this until we know the section is non-empty.
20425 Only then do we know we have such a section. */
20426 abfd = get_section_bfd_owner (section);
20428 /* Make sure that at least there's room for the total_length field.
20429 That could be 12 bytes long, but we're just going to fudge that. */
20430 if (to_underlying (sect_off) + 4 >= section->size)
20432 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20436 line_header_up lh (new line_header ());
20438 lh->sect_off = sect_off;
20439 lh->offset_in_dwz = cu->per_cu->is_dwz;
20441 line_ptr = section->buffer + to_underlying (sect_off);
20443 /* Read in the header. */
20445 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20446 &bytes_read, &offset_size);
20447 line_ptr += bytes_read;
20448 if (line_ptr + lh->total_length > (section->buffer + section->size))
20450 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20453 lh->statement_program_end = line_ptr + lh->total_length;
20454 lh->version = read_2_bytes (abfd, line_ptr);
20456 if (lh->version > 5)
20458 /* This is a version we don't understand. The format could have
20459 changed in ways we don't handle properly so just punt. */
20460 complaint (_("unsupported version in .debug_line section"));
20463 if (lh->version >= 5)
20465 gdb_byte segment_selector_size;
20467 /* Skip address size. */
20468 read_1_byte (abfd, line_ptr);
20471 segment_selector_size = read_1_byte (abfd, line_ptr);
20473 if (segment_selector_size != 0)
20475 complaint (_("unsupported segment selector size %u "
20476 "in .debug_line section"),
20477 segment_selector_size);
20481 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20482 line_ptr += offset_size;
20483 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20485 if (lh->version >= 4)
20487 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20491 lh->maximum_ops_per_instruction = 1;
20493 if (lh->maximum_ops_per_instruction == 0)
20495 lh->maximum_ops_per_instruction = 1;
20496 complaint (_("invalid maximum_ops_per_instruction "
20497 "in `.debug_line' section"));
20500 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20502 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20504 lh->line_range = read_1_byte (abfd, line_ptr);
20506 lh->opcode_base = read_1_byte (abfd, line_ptr);
20508 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20510 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20511 for (i = 1; i < lh->opcode_base; ++i)
20513 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20517 if (lh->version >= 5)
20519 /* Read directory table. */
20520 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20522 [] (struct line_header *header, const char *name,
20523 dir_index d_index, unsigned int mod_time,
20524 unsigned int length)
20526 header->add_include_dir (name);
20529 /* Read file name table. */
20530 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20532 [] (struct line_header *header, const char *name,
20533 dir_index d_index, unsigned int mod_time,
20534 unsigned int length)
20536 header->add_file_name (name, d_index, mod_time, length);
20541 /* Read directory table. */
20542 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20544 line_ptr += bytes_read;
20545 lh->add_include_dir (cur_dir);
20547 line_ptr += bytes_read;
20549 /* Read file name table. */
20550 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20552 unsigned int mod_time, length;
20555 line_ptr += bytes_read;
20556 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20557 line_ptr += bytes_read;
20558 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20559 line_ptr += bytes_read;
20560 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20561 line_ptr += bytes_read;
20563 lh->add_file_name (cur_file, d_index, mod_time, length);
20565 line_ptr += bytes_read;
20567 lh->statement_program_start = line_ptr;
20569 if (line_ptr > (section->buffer + section->size))
20570 complaint (_("line number info header doesn't "
20571 "fit in `.debug_line' section"));
20576 /* Subroutine of dwarf_decode_lines to simplify it.
20577 Return the file name of the psymtab for included file FILE_INDEX
20578 in line header LH of PST.
20579 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20580 If space for the result is malloc'd, *NAME_HOLDER will be set.
20581 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20583 static const char *
20584 psymtab_include_file_name (const struct line_header *lh, int file_index,
20585 const struct partial_symtab *pst,
20586 const char *comp_dir,
20587 gdb::unique_xmalloc_ptr<char> *name_holder)
20589 const file_entry &fe = lh->file_names[file_index];
20590 const char *include_name = fe.name;
20591 const char *include_name_to_compare = include_name;
20592 const char *pst_filename;
20595 const char *dir_name = fe.include_dir (lh);
20597 gdb::unique_xmalloc_ptr<char> hold_compare;
20598 if (!IS_ABSOLUTE_PATH (include_name)
20599 && (dir_name != NULL || comp_dir != NULL))
20601 /* Avoid creating a duplicate psymtab for PST.
20602 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20603 Before we do the comparison, however, we need to account
20604 for DIR_NAME and COMP_DIR.
20605 First prepend dir_name (if non-NULL). If we still don't
20606 have an absolute path prepend comp_dir (if non-NULL).
20607 However, the directory we record in the include-file's
20608 psymtab does not contain COMP_DIR (to match the
20609 corresponding symtab(s)).
20614 bash$ gcc -g ./hello.c
20615 include_name = "hello.c"
20617 DW_AT_comp_dir = comp_dir = "/tmp"
20618 DW_AT_name = "./hello.c"
20622 if (dir_name != NULL)
20624 name_holder->reset (concat (dir_name, SLASH_STRING,
20625 include_name, (char *) NULL));
20626 include_name = name_holder->get ();
20627 include_name_to_compare = include_name;
20629 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20631 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20632 include_name, (char *) NULL));
20633 include_name_to_compare = hold_compare.get ();
20637 pst_filename = pst->filename;
20638 gdb::unique_xmalloc_ptr<char> copied_name;
20639 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20641 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20642 pst_filename, (char *) NULL));
20643 pst_filename = copied_name.get ();
20646 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20650 return include_name;
20653 /* State machine to track the state of the line number program. */
20655 class lnp_state_machine
20658 /* Initialize a machine state for the start of a line number
20660 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20661 bool record_lines_p);
20663 file_entry *current_file ()
20665 /* lh->file_names is 0-based, but the file name numbers in the
20666 statement program are 1-based. */
20667 return m_line_header->file_name_at (m_file);
20670 /* Record the line in the state machine. END_SEQUENCE is true if
20671 we're processing the end of a sequence. */
20672 void record_line (bool end_sequence);
20674 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20675 nop-out rest of the lines in this sequence. */
20676 void check_line_address (struct dwarf2_cu *cu,
20677 const gdb_byte *line_ptr,
20678 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20680 void handle_set_discriminator (unsigned int discriminator)
20682 m_discriminator = discriminator;
20683 m_line_has_non_zero_discriminator |= discriminator != 0;
20686 /* Handle DW_LNE_set_address. */
20687 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20690 address += baseaddr;
20691 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20694 /* Handle DW_LNS_advance_pc. */
20695 void handle_advance_pc (CORE_ADDR adjust);
20697 /* Handle a special opcode. */
20698 void handle_special_opcode (unsigned char op_code);
20700 /* Handle DW_LNS_advance_line. */
20701 void handle_advance_line (int line_delta)
20703 advance_line (line_delta);
20706 /* Handle DW_LNS_set_file. */
20707 void handle_set_file (file_name_index file);
20709 /* Handle DW_LNS_negate_stmt. */
20710 void handle_negate_stmt ()
20712 m_is_stmt = !m_is_stmt;
20715 /* Handle DW_LNS_const_add_pc. */
20716 void handle_const_add_pc ();
20718 /* Handle DW_LNS_fixed_advance_pc. */
20719 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20721 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20725 /* Handle DW_LNS_copy. */
20726 void handle_copy ()
20728 record_line (false);
20729 m_discriminator = 0;
20732 /* Handle DW_LNE_end_sequence. */
20733 void handle_end_sequence ()
20735 m_currently_recording_lines = true;
20739 /* Advance the line by LINE_DELTA. */
20740 void advance_line (int line_delta)
20742 m_line += line_delta;
20744 if (line_delta != 0)
20745 m_line_has_non_zero_discriminator = m_discriminator != 0;
20748 struct dwarf2_cu *m_cu;
20750 gdbarch *m_gdbarch;
20752 /* True if we're recording lines.
20753 Otherwise we're building partial symtabs and are just interested in
20754 finding include files mentioned by the line number program. */
20755 bool m_record_lines_p;
20757 /* The line number header. */
20758 line_header *m_line_header;
20760 /* These are part of the standard DWARF line number state machine,
20761 and initialized according to the DWARF spec. */
20763 unsigned char m_op_index = 0;
20764 /* The line table index (1-based) of the current file. */
20765 file_name_index m_file = (file_name_index) 1;
20766 unsigned int m_line = 1;
20768 /* These are initialized in the constructor. */
20770 CORE_ADDR m_address;
20772 unsigned int m_discriminator;
20774 /* Additional bits of state we need to track. */
20776 /* The last file that we called dwarf2_start_subfile for.
20777 This is only used for TLLs. */
20778 unsigned int m_last_file = 0;
20779 /* The last file a line number was recorded for. */
20780 struct subfile *m_last_subfile = NULL;
20782 /* When true, record the lines we decode. */
20783 bool m_currently_recording_lines = false;
20785 /* The last line number that was recorded, used to coalesce
20786 consecutive entries for the same line. This can happen, for
20787 example, when discriminators are present. PR 17276. */
20788 unsigned int m_last_line = 0;
20789 bool m_line_has_non_zero_discriminator = false;
20793 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20795 CORE_ADDR addr_adj = (((m_op_index + adjust)
20796 / m_line_header->maximum_ops_per_instruction)
20797 * m_line_header->minimum_instruction_length);
20798 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20799 m_op_index = ((m_op_index + adjust)
20800 % m_line_header->maximum_ops_per_instruction);
20804 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20806 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20807 CORE_ADDR addr_adj = (((m_op_index
20808 + (adj_opcode / m_line_header->line_range))
20809 / m_line_header->maximum_ops_per_instruction)
20810 * 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 + (adj_opcode / m_line_header->line_range))
20813 % m_line_header->maximum_ops_per_instruction);
20815 int line_delta = (m_line_header->line_base
20816 + (adj_opcode % m_line_header->line_range));
20817 advance_line (line_delta);
20818 record_line (false);
20819 m_discriminator = 0;
20823 lnp_state_machine::handle_set_file (file_name_index file)
20827 const file_entry *fe = current_file ();
20829 dwarf2_debug_line_missing_file_complaint ();
20830 else if (m_record_lines_p)
20832 const char *dir = fe->include_dir (m_line_header);
20834 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20835 m_line_has_non_zero_discriminator = m_discriminator != 0;
20836 dwarf2_start_subfile (m_cu, fe->name, dir);
20841 lnp_state_machine::handle_const_add_pc ()
20844 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20847 = (((m_op_index + adjust)
20848 / m_line_header->maximum_ops_per_instruction)
20849 * m_line_header->minimum_instruction_length);
20851 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20852 m_op_index = ((m_op_index + adjust)
20853 % m_line_header->maximum_ops_per_instruction);
20856 /* Return non-zero if we should add LINE to the line number table.
20857 LINE is the line to add, LAST_LINE is the last line that was added,
20858 LAST_SUBFILE is the subfile for LAST_LINE.
20859 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20860 had a non-zero discriminator.
20862 We have to be careful in the presence of discriminators.
20863 E.g., for this line:
20865 for (i = 0; i < 100000; i++);
20867 clang can emit four line number entries for that one line,
20868 each with a different discriminator.
20869 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20871 However, we want gdb to coalesce all four entries into one.
20872 Otherwise the user could stepi into the middle of the line and
20873 gdb would get confused about whether the pc really was in the
20874 middle of the line.
20876 Things are further complicated by the fact that two consecutive
20877 line number entries for the same line is a heuristic used by gcc
20878 to denote the end of the prologue. So we can't just discard duplicate
20879 entries, we have to be selective about it. The heuristic we use is
20880 that we only collapse consecutive entries for the same line if at least
20881 one of those entries has a non-zero discriminator. PR 17276.
20883 Note: Addresses in the line number state machine can never go backwards
20884 within one sequence, thus this coalescing is ok. */
20887 dwarf_record_line_p (struct dwarf2_cu *cu,
20888 unsigned int line, unsigned int last_line,
20889 int line_has_non_zero_discriminator,
20890 struct subfile *last_subfile)
20892 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20894 if (line != last_line)
20896 /* Same line for the same file that we've seen already.
20897 As a last check, for pr 17276, only record the line if the line
20898 has never had a non-zero discriminator. */
20899 if (!line_has_non_zero_discriminator)
20904 /* Use the CU's builder to record line number LINE beginning at
20905 address ADDRESS in the line table of subfile SUBFILE. */
20908 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20909 unsigned int line, CORE_ADDR address,
20910 struct dwarf2_cu *cu)
20912 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20914 if (dwarf_line_debug)
20916 fprintf_unfiltered (gdb_stdlog,
20917 "Recording line %u, file %s, address %s\n",
20918 line, lbasename (subfile->name),
20919 paddress (gdbarch, address));
20923 cu->get_builder ()->record_line (subfile, line, addr);
20926 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20927 Mark the end of a set of line number records.
20928 The arguments are the same as for dwarf_record_line_1.
20929 If SUBFILE is NULL the request is ignored. */
20932 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20933 CORE_ADDR address, struct dwarf2_cu *cu)
20935 if (subfile == NULL)
20938 if (dwarf_line_debug)
20940 fprintf_unfiltered (gdb_stdlog,
20941 "Finishing current line, file %s, address %s\n",
20942 lbasename (subfile->name),
20943 paddress (gdbarch, address));
20946 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
20950 lnp_state_machine::record_line (bool end_sequence)
20952 if (dwarf_line_debug)
20954 fprintf_unfiltered (gdb_stdlog,
20955 "Processing actual line %u: file %u,"
20956 " address %s, is_stmt %u, discrim %u\n",
20957 m_line, to_underlying (m_file),
20958 paddress (m_gdbarch, m_address),
20959 m_is_stmt, m_discriminator);
20962 file_entry *fe = current_file ();
20965 dwarf2_debug_line_missing_file_complaint ();
20966 /* For now we ignore lines not starting on an instruction boundary.
20967 But not when processing end_sequence for compatibility with the
20968 previous version of the code. */
20969 else if (m_op_index == 0 || end_sequence)
20971 fe->included_p = 1;
20972 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
20974 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20977 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20978 m_currently_recording_lines ? m_cu : nullptr);
20983 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20984 m_line_has_non_zero_discriminator,
20987 buildsym_compunit *builder = m_cu->get_builder ();
20988 dwarf_record_line_1 (m_gdbarch,
20989 builder->get_current_subfile (),
20991 m_currently_recording_lines ? m_cu : nullptr);
20993 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20994 m_last_line = m_line;
21000 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21001 line_header *lh, bool record_lines_p)
21005 m_record_lines_p = record_lines_p;
21006 m_line_header = lh;
21008 m_currently_recording_lines = true;
21010 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21011 was a line entry for it so that the backend has a chance to adjust it
21012 and also record it in case it needs it. This is currently used by MIPS
21013 code, cf. `mips_adjust_dwarf2_line'. */
21014 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21015 m_is_stmt = lh->default_is_stmt;
21016 m_discriminator = 0;
21020 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21021 const gdb_byte *line_ptr,
21022 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21024 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21025 the pc range of the CU. However, we restrict the test to only ADDRESS
21026 values of zero to preserve GDB's previous behaviour which is to handle
21027 the specific case of a function being GC'd by the linker. */
21029 if (address == 0 && address < unrelocated_lowpc)
21031 /* This line table is for a function which has been
21032 GCd by the linker. Ignore it. PR gdb/12528 */
21034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21035 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21037 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21038 line_offset, objfile_name (objfile));
21039 m_currently_recording_lines = false;
21040 /* Note: m_currently_recording_lines is left as false until we see
21041 DW_LNE_end_sequence. */
21045 /* Subroutine of dwarf_decode_lines to simplify it.
21046 Process the line number information in LH.
21047 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21048 program in order to set included_p for every referenced header. */
21051 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21052 const int decode_for_pst_p, CORE_ADDR lowpc)
21054 const gdb_byte *line_ptr, *extended_end;
21055 const gdb_byte *line_end;
21056 unsigned int bytes_read, extended_len;
21057 unsigned char op_code, extended_op;
21058 CORE_ADDR baseaddr;
21059 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21060 bfd *abfd = objfile->obfd;
21061 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21062 /* True if we're recording line info (as opposed to building partial
21063 symtabs and just interested in finding include files mentioned by
21064 the line number program). */
21065 bool record_lines_p = !decode_for_pst_p;
21067 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21069 line_ptr = lh->statement_program_start;
21070 line_end = lh->statement_program_end;
21072 /* Read the statement sequences until there's nothing left. */
21073 while (line_ptr < line_end)
21075 /* The DWARF line number program state machine. Reset the state
21076 machine at the start of each sequence. */
21077 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21078 bool end_sequence = false;
21080 if (record_lines_p)
21082 /* Start a subfile for the current file of the state
21084 const file_entry *fe = state_machine.current_file ();
21087 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21090 /* Decode the table. */
21091 while (line_ptr < line_end && !end_sequence)
21093 op_code = read_1_byte (abfd, line_ptr);
21096 if (op_code >= lh->opcode_base)
21098 /* Special opcode. */
21099 state_machine.handle_special_opcode (op_code);
21101 else switch (op_code)
21103 case DW_LNS_extended_op:
21104 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21106 line_ptr += bytes_read;
21107 extended_end = line_ptr + extended_len;
21108 extended_op = read_1_byte (abfd, line_ptr);
21110 switch (extended_op)
21112 case DW_LNE_end_sequence:
21113 state_machine.handle_end_sequence ();
21114 end_sequence = true;
21116 case DW_LNE_set_address:
21119 = read_address (abfd, line_ptr, cu, &bytes_read);
21120 line_ptr += bytes_read;
21122 state_machine.check_line_address (cu, line_ptr,
21123 lowpc - baseaddr, address);
21124 state_machine.handle_set_address (baseaddr, address);
21127 case DW_LNE_define_file:
21129 const char *cur_file;
21130 unsigned int mod_time, length;
21133 cur_file = read_direct_string (abfd, line_ptr,
21135 line_ptr += bytes_read;
21136 dindex = (dir_index)
21137 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21138 line_ptr += bytes_read;
21140 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21141 line_ptr += bytes_read;
21143 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21144 line_ptr += bytes_read;
21145 lh->add_file_name (cur_file, dindex, mod_time, length);
21148 case DW_LNE_set_discriminator:
21150 /* The discriminator is not interesting to the
21151 debugger; just ignore it. We still need to
21152 check its value though:
21153 if there are consecutive entries for the same
21154 (non-prologue) line we want to coalesce them.
21157 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21158 line_ptr += bytes_read;
21160 state_machine.handle_set_discriminator (discr);
21164 complaint (_("mangled .debug_line section"));
21167 /* Make sure that we parsed the extended op correctly. If e.g.
21168 we expected a different address size than the producer used,
21169 we may have read the wrong number of bytes. */
21170 if (line_ptr != extended_end)
21172 complaint (_("mangled .debug_line section"));
21177 state_machine.handle_copy ();
21179 case DW_LNS_advance_pc:
21182 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21183 line_ptr += bytes_read;
21185 state_machine.handle_advance_pc (adjust);
21188 case DW_LNS_advance_line:
21191 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21192 line_ptr += bytes_read;
21194 state_machine.handle_advance_line (line_delta);
21197 case DW_LNS_set_file:
21199 file_name_index file
21200 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21202 line_ptr += bytes_read;
21204 state_machine.handle_set_file (file);
21207 case DW_LNS_set_column:
21208 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21209 line_ptr += bytes_read;
21211 case DW_LNS_negate_stmt:
21212 state_machine.handle_negate_stmt ();
21214 case DW_LNS_set_basic_block:
21216 /* Add to the address register of the state machine the
21217 address increment value corresponding to special opcode
21218 255. I.e., this value is scaled by the minimum
21219 instruction length since special opcode 255 would have
21220 scaled the increment. */
21221 case DW_LNS_const_add_pc:
21222 state_machine.handle_const_add_pc ();
21224 case DW_LNS_fixed_advance_pc:
21226 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21229 state_machine.handle_fixed_advance_pc (addr_adj);
21234 /* Unknown standard opcode, ignore it. */
21237 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21239 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21240 line_ptr += bytes_read;
21247 dwarf2_debug_line_missing_end_sequence_complaint ();
21249 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21250 in which case we still finish recording the last line). */
21251 state_machine.record_line (true);
21255 /* Decode the Line Number Program (LNP) for the given line_header
21256 structure and CU. The actual information extracted and the type
21257 of structures created from the LNP depends on the value of PST.
21259 1. If PST is NULL, then this procedure uses the data from the program
21260 to create all necessary symbol tables, and their linetables.
21262 2. If PST is not NULL, this procedure reads the program to determine
21263 the list of files included by the unit represented by PST, and
21264 builds all the associated partial symbol tables.
21266 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21267 It is used for relative paths in the line table.
21268 NOTE: When processing partial symtabs (pst != NULL),
21269 comp_dir == pst->dirname.
21271 NOTE: It is important that psymtabs have the same file name (via strcmp)
21272 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21273 symtab we don't use it in the name of the psymtabs we create.
21274 E.g. expand_line_sal requires this when finding psymtabs to expand.
21275 A good testcase for this is mb-inline.exp.
21277 LOWPC is the lowest address in CU (or 0 if not known).
21279 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21280 for its PC<->lines mapping information. Otherwise only the filename
21281 table is read in. */
21284 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21285 struct dwarf2_cu *cu, struct partial_symtab *pst,
21286 CORE_ADDR lowpc, int decode_mapping)
21288 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21289 const int decode_for_pst_p = (pst != NULL);
21291 if (decode_mapping)
21292 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21294 if (decode_for_pst_p)
21298 /* Now that we're done scanning the Line Header Program, we can
21299 create the psymtab of each included file. */
21300 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21301 if (lh->file_names[file_index].included_p == 1)
21303 gdb::unique_xmalloc_ptr<char> name_holder;
21304 const char *include_name =
21305 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21307 if (include_name != NULL)
21308 dwarf2_create_include_psymtab (include_name, pst, objfile);
21313 /* Make sure a symtab is created for every file, even files
21314 which contain only variables (i.e. no code with associated
21316 buildsym_compunit *builder = cu->get_builder ();
21317 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21320 for (i = 0; i < lh->file_names.size (); i++)
21322 file_entry &fe = lh->file_names[i];
21324 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21326 if (builder->get_current_subfile ()->symtab == NULL)
21328 builder->get_current_subfile ()->symtab
21329 = allocate_symtab (cust,
21330 builder->get_current_subfile ()->name);
21332 fe.symtab = builder->get_current_subfile ()->symtab;
21337 /* Start a subfile for DWARF. FILENAME is the name of the file and
21338 DIRNAME the name of the source directory which contains FILENAME
21339 or NULL if not known.
21340 This routine tries to keep line numbers from identical absolute and
21341 relative file names in a common subfile.
21343 Using the `list' example from the GDB testsuite, which resides in
21344 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21345 of /srcdir/list0.c yields the following debugging information for list0.c:
21347 DW_AT_name: /srcdir/list0.c
21348 DW_AT_comp_dir: /compdir
21349 files.files[0].name: list0.h
21350 files.files[0].dir: /srcdir
21351 files.files[1].name: list0.c
21352 files.files[1].dir: /srcdir
21354 The line number information for list0.c has to end up in a single
21355 subfile, so that `break /srcdir/list0.c:1' works as expected.
21356 start_subfile will ensure that this happens provided that we pass the
21357 concatenation of files.files[1].dir and files.files[1].name as the
21361 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21362 const char *dirname)
21366 /* In order not to lose the line information directory,
21367 we concatenate it to the filename when it makes sense.
21368 Note that the Dwarf3 standard says (speaking of filenames in line
21369 information): ``The directory index is ignored for file names
21370 that represent full path names''. Thus ignoring dirname in the
21371 `else' branch below isn't an issue. */
21373 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21375 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21379 cu->get_builder ()->start_subfile (filename);
21385 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21386 buildsym_compunit constructor. */
21388 struct compunit_symtab *
21389 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21392 gdb_assert (m_builder == nullptr);
21394 m_builder.reset (new struct buildsym_compunit
21395 (per_cu->dwarf2_per_objfile->objfile,
21396 name, comp_dir, language, low_pc));
21398 list_in_scope = get_builder ()->get_file_symbols ();
21400 get_builder ()->record_debugformat ("DWARF 2");
21401 get_builder ()->record_producer (producer);
21403 processing_has_namespace_info = false;
21405 return get_builder ()->get_compunit_symtab ();
21409 var_decode_location (struct attribute *attr, struct symbol *sym,
21410 struct dwarf2_cu *cu)
21412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21413 struct comp_unit_head *cu_header = &cu->header;
21415 /* NOTE drow/2003-01-30: There used to be a comment and some special
21416 code here to turn a symbol with DW_AT_external and a
21417 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21418 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21419 with some versions of binutils) where shared libraries could have
21420 relocations against symbols in their debug information - the
21421 minimal symbol would have the right address, but the debug info
21422 would not. It's no longer necessary, because we will explicitly
21423 apply relocations when we read in the debug information now. */
21425 /* A DW_AT_location attribute with no contents indicates that a
21426 variable has been optimized away. */
21427 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21429 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21433 /* Handle one degenerate form of location expression specially, to
21434 preserve GDB's previous behavior when section offsets are
21435 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21436 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21438 if (attr_form_is_block (attr)
21439 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21440 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21441 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21442 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21443 && (DW_BLOCK (attr)->size
21444 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21446 unsigned int dummy;
21448 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21449 SYMBOL_VALUE_ADDRESS (sym) =
21450 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21452 SYMBOL_VALUE_ADDRESS (sym) =
21453 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21454 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21455 fixup_symbol_section (sym, objfile);
21456 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21457 SYMBOL_SECTION (sym));
21461 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21462 expression evaluator, and use LOC_COMPUTED only when necessary
21463 (i.e. when the value of a register or memory location is
21464 referenced, or a thread-local block, etc.). Then again, it might
21465 not be worthwhile. I'm assuming that it isn't unless performance
21466 or memory numbers show me otherwise. */
21468 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21470 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21471 cu->has_loclist = true;
21474 /* Given a pointer to a DWARF information entry, figure out if we need
21475 to make a symbol table entry for it, and if so, create a new entry
21476 and return a pointer to it.
21477 If TYPE is NULL, determine symbol type from the die, otherwise
21478 used the passed type.
21479 If SPACE is not NULL, use it to hold the new symbol. If it is
21480 NULL, allocate a new symbol on the objfile's obstack. */
21482 static struct symbol *
21483 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21484 struct symbol *space)
21486 struct dwarf2_per_objfile *dwarf2_per_objfile
21487 = cu->per_cu->dwarf2_per_objfile;
21488 struct objfile *objfile = dwarf2_per_objfile->objfile;
21489 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21490 struct symbol *sym = NULL;
21492 struct attribute *attr = NULL;
21493 struct attribute *attr2 = NULL;
21494 CORE_ADDR baseaddr;
21495 struct pending **list_to_add = NULL;
21497 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21501 name = dwarf2_name (die, cu);
21504 const char *linkagename;
21505 int suppress_add = 0;
21510 sym = allocate_symbol (objfile);
21511 OBJSTAT (objfile, n_syms++);
21513 /* Cache this symbol's name and the name's demangled form (if any). */
21514 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21515 linkagename = dwarf2_physname (name, die, cu);
21516 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21518 /* Fortran does not have mangling standard and the mangling does differ
21519 between gfortran, iFort etc. */
21520 if (cu->language == language_fortran
21521 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21522 symbol_set_demangled_name (&(sym->ginfo),
21523 dwarf2_full_name (name, die, cu),
21526 /* Default assumptions.
21527 Use the passed type or decode it from the die. */
21528 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21529 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21531 SYMBOL_TYPE (sym) = type;
21533 SYMBOL_TYPE (sym) = die_type (die, cu);
21534 attr = dwarf2_attr (die,
21535 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21539 SYMBOL_LINE (sym) = DW_UNSND (attr);
21542 attr = dwarf2_attr (die,
21543 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21547 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21548 struct file_entry *fe;
21550 if (cu->line_header != NULL)
21551 fe = cu->line_header->file_name_at (file_index);
21556 complaint (_("file index out of range"));
21558 symbol_set_symtab (sym, fe->symtab);
21564 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21569 addr = attr_value_as_address (attr);
21570 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21571 SYMBOL_VALUE_ADDRESS (sym) = addr;
21573 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21574 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21575 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21576 add_symbol_to_list (sym, cu->list_in_scope);
21578 case DW_TAG_subprogram:
21579 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21581 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21582 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21583 if ((attr2 && (DW_UNSND (attr2) != 0))
21584 || cu->language == language_ada)
21586 /* Subprograms marked external are stored as a global symbol.
21587 Ada subprograms, whether marked external or not, are always
21588 stored as a global symbol, because we want to be able to
21589 access them globally. For instance, we want to be able
21590 to break on a nested subprogram without having to
21591 specify the context. */
21592 list_to_add = cu->get_builder ()->get_global_symbols ();
21596 list_to_add = cu->list_in_scope;
21599 case DW_TAG_inlined_subroutine:
21600 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21602 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21603 SYMBOL_INLINED (sym) = 1;
21604 list_to_add = cu->list_in_scope;
21606 case DW_TAG_template_value_param:
21608 /* Fall through. */
21609 case DW_TAG_constant:
21610 case DW_TAG_variable:
21611 case DW_TAG_member:
21612 /* Compilation with minimal debug info may result in
21613 variables with missing type entries. Change the
21614 misleading `void' type to something sensible. */
21615 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21616 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21618 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21619 /* In the case of DW_TAG_member, we should only be called for
21620 static const members. */
21621 if (die->tag == DW_TAG_member)
21623 /* dwarf2_add_field uses die_is_declaration,
21624 so we do the same. */
21625 gdb_assert (die_is_declaration (die, cu));
21630 dwarf2_const_value (attr, sym, cu);
21631 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21634 if (attr2 && (DW_UNSND (attr2) != 0))
21635 list_to_add = cu->get_builder ()->get_global_symbols ();
21637 list_to_add = cu->list_in_scope;
21641 attr = dwarf2_attr (die, DW_AT_location, cu);
21644 var_decode_location (attr, sym, cu);
21645 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21647 /* Fortran explicitly imports any global symbols to the local
21648 scope by DW_TAG_common_block. */
21649 if (cu->language == language_fortran && die->parent
21650 && die->parent->tag == DW_TAG_common_block)
21653 if (SYMBOL_CLASS (sym) == LOC_STATIC
21654 && SYMBOL_VALUE_ADDRESS (sym) == 0
21655 && !dwarf2_per_objfile->has_section_at_zero)
21657 /* When a static variable is eliminated by the linker,
21658 the corresponding debug information is not stripped
21659 out, but the variable address is set to null;
21660 do not add such variables into symbol table. */
21662 else if (attr2 && (DW_UNSND (attr2) != 0))
21664 /* Workaround gfortran PR debug/40040 - it uses
21665 DW_AT_location for variables in -fPIC libraries which may
21666 get overriden by other libraries/executable and get
21667 a different address. Resolve it by the minimal symbol
21668 which may come from inferior's executable using copy
21669 relocation. Make this workaround only for gfortran as for
21670 other compilers GDB cannot guess the minimal symbol
21671 Fortran mangling kind. */
21672 if (cu->language == language_fortran && die->parent
21673 && die->parent->tag == DW_TAG_module
21675 && startswith (cu->producer, "GNU Fortran"))
21676 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21678 /* A variable with DW_AT_external is never static,
21679 but it may be block-scoped. */
21681 = ((cu->list_in_scope
21682 == cu->get_builder ()->get_file_symbols ())
21683 ? cu->get_builder ()->get_global_symbols ()
21684 : cu->list_in_scope);
21687 list_to_add = cu->list_in_scope;
21691 /* We do not know the address of this symbol.
21692 If it is an external symbol and we have type information
21693 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21694 The address of the variable will then be determined from
21695 the minimal symbol table whenever the variable is
21697 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21699 /* Fortran explicitly imports any global symbols to the local
21700 scope by DW_TAG_common_block. */
21701 if (cu->language == language_fortran && die->parent
21702 && die->parent->tag == DW_TAG_common_block)
21704 /* SYMBOL_CLASS doesn't matter here because
21705 read_common_block is going to reset it. */
21707 list_to_add = cu->list_in_scope;
21709 else if (attr2 && (DW_UNSND (attr2) != 0)
21710 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21712 /* A variable with DW_AT_external is never static, but it
21713 may be block-scoped. */
21715 = ((cu->list_in_scope
21716 == cu->get_builder ()->get_file_symbols ())
21717 ? cu->get_builder ()->get_global_symbols ()
21718 : cu->list_in_scope);
21720 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21722 else if (!die_is_declaration (die, cu))
21724 /* Use the default LOC_OPTIMIZED_OUT class. */
21725 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21727 list_to_add = cu->list_in_scope;
21731 case DW_TAG_formal_parameter:
21733 /* If we are inside a function, mark this as an argument. If
21734 not, we might be looking at an argument to an inlined function
21735 when we do not have enough information to show inlined frames;
21736 pretend it's a local variable in that case so that the user can
21738 struct context_stack *curr
21739 = cu->get_builder ()->get_current_context_stack ();
21740 if (curr != nullptr && curr->name != nullptr)
21741 SYMBOL_IS_ARGUMENT (sym) = 1;
21742 attr = dwarf2_attr (die, DW_AT_location, cu);
21745 var_decode_location (attr, sym, cu);
21747 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21750 dwarf2_const_value (attr, sym, cu);
21753 list_to_add = cu->list_in_scope;
21756 case DW_TAG_unspecified_parameters:
21757 /* From varargs functions; gdb doesn't seem to have any
21758 interest in this information, so just ignore it for now.
21761 case DW_TAG_template_type_param:
21763 /* Fall through. */
21764 case DW_TAG_class_type:
21765 case DW_TAG_interface_type:
21766 case DW_TAG_structure_type:
21767 case DW_TAG_union_type:
21768 case DW_TAG_set_type:
21769 case DW_TAG_enumeration_type:
21770 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21771 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21774 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21775 really ever be static objects: otherwise, if you try
21776 to, say, break of a class's method and you're in a file
21777 which doesn't mention that class, it won't work unless
21778 the check for all static symbols in lookup_symbol_aux
21779 saves you. See the OtherFileClass tests in
21780 gdb.c++/namespace.exp. */
21784 buildsym_compunit *builder = cu->get_builder ();
21786 = (cu->list_in_scope == builder->get_file_symbols ()
21787 && cu->language == language_cplus
21788 ? builder->get_global_symbols ()
21789 : cu->list_in_scope);
21791 /* The semantics of C++ state that "struct foo {
21792 ... }" also defines a typedef for "foo". */
21793 if (cu->language == language_cplus
21794 || cu->language == language_ada
21795 || cu->language == language_d
21796 || cu->language == language_rust)
21798 /* The symbol's name is already allocated along
21799 with this objfile, so we don't need to
21800 duplicate it for the type. */
21801 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21802 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21807 case DW_TAG_typedef:
21808 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21809 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21810 list_to_add = cu->list_in_scope;
21812 case DW_TAG_base_type:
21813 case DW_TAG_subrange_type:
21814 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21815 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21816 list_to_add = cu->list_in_scope;
21818 case DW_TAG_enumerator:
21819 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21822 dwarf2_const_value (attr, sym, cu);
21825 /* NOTE: carlton/2003-11-10: See comment above in the
21826 DW_TAG_class_type, etc. block. */
21829 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21830 && cu->language == language_cplus
21831 ? cu->get_builder ()->get_global_symbols ()
21832 : cu->list_in_scope);
21835 case DW_TAG_imported_declaration:
21836 case DW_TAG_namespace:
21837 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21838 list_to_add = cu->get_builder ()->get_global_symbols ();
21840 case DW_TAG_module:
21841 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21842 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21843 list_to_add = cu->get_builder ()->get_global_symbols ();
21845 case DW_TAG_common_block:
21846 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21847 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21848 add_symbol_to_list (sym, cu->list_in_scope);
21851 /* Not a tag we recognize. Hopefully we aren't processing
21852 trash data, but since we must specifically ignore things
21853 we don't recognize, there is nothing else we should do at
21855 complaint (_("unsupported tag: '%s'"),
21856 dwarf_tag_name (die->tag));
21862 sym->hash_next = objfile->template_symbols;
21863 objfile->template_symbols = sym;
21864 list_to_add = NULL;
21867 if (list_to_add != NULL)
21868 add_symbol_to_list (sym, list_to_add);
21870 /* For the benefit of old versions of GCC, check for anonymous
21871 namespaces based on the demangled name. */
21872 if (!cu->processing_has_namespace_info
21873 && cu->language == language_cplus)
21874 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21879 /* Given an attr with a DW_FORM_dataN value in host byte order,
21880 zero-extend it as appropriate for the symbol's type. The DWARF
21881 standard (v4) is not entirely clear about the meaning of using
21882 DW_FORM_dataN for a constant with a signed type, where the type is
21883 wider than the data. The conclusion of a discussion on the DWARF
21884 list was that this is unspecified. We choose to always zero-extend
21885 because that is the interpretation long in use by GCC. */
21888 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21889 struct dwarf2_cu *cu, LONGEST *value, int bits)
21891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21892 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21893 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21894 LONGEST l = DW_UNSND (attr);
21896 if (bits < sizeof (*value) * 8)
21898 l &= ((LONGEST) 1 << bits) - 1;
21901 else if (bits == sizeof (*value) * 8)
21905 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21906 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21913 /* Read a constant value from an attribute. Either set *VALUE, or if
21914 the value does not fit in *VALUE, set *BYTES - either already
21915 allocated on the objfile obstack, or newly allocated on OBSTACK,
21916 or, set *BATON, if we translated the constant to a location
21920 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21921 const char *name, struct obstack *obstack,
21922 struct dwarf2_cu *cu,
21923 LONGEST *value, const gdb_byte **bytes,
21924 struct dwarf2_locexpr_baton **baton)
21926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21927 struct comp_unit_head *cu_header = &cu->header;
21928 struct dwarf_block *blk;
21929 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21930 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21936 switch (attr->form)
21939 case DW_FORM_addrx:
21940 case DW_FORM_GNU_addr_index:
21944 if (TYPE_LENGTH (type) != cu_header->addr_size)
21945 dwarf2_const_value_length_mismatch_complaint (name,
21946 cu_header->addr_size,
21947 TYPE_LENGTH (type));
21948 /* Symbols of this form are reasonably rare, so we just
21949 piggyback on the existing location code rather than writing
21950 a new implementation of symbol_computed_ops. */
21951 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21952 (*baton)->per_cu = cu->per_cu;
21953 gdb_assert ((*baton)->per_cu);
21955 (*baton)->size = 2 + cu_header->addr_size;
21956 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21957 (*baton)->data = data;
21959 data[0] = DW_OP_addr;
21960 store_unsigned_integer (&data[1], cu_header->addr_size,
21961 byte_order, DW_ADDR (attr));
21962 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21965 case DW_FORM_string:
21968 case DW_FORM_GNU_str_index:
21969 case DW_FORM_GNU_strp_alt:
21970 /* DW_STRING is already allocated on the objfile obstack, point
21972 *bytes = (const gdb_byte *) DW_STRING (attr);
21974 case DW_FORM_block1:
21975 case DW_FORM_block2:
21976 case DW_FORM_block4:
21977 case DW_FORM_block:
21978 case DW_FORM_exprloc:
21979 case DW_FORM_data16:
21980 blk = DW_BLOCK (attr);
21981 if (TYPE_LENGTH (type) != blk->size)
21982 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21983 TYPE_LENGTH (type));
21984 *bytes = blk->data;
21987 /* The DW_AT_const_value attributes are supposed to carry the
21988 symbol's value "represented as it would be on the target
21989 architecture." By the time we get here, it's already been
21990 converted to host endianness, so we just need to sign- or
21991 zero-extend it as appropriate. */
21992 case DW_FORM_data1:
21993 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21995 case DW_FORM_data2:
21996 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21998 case DW_FORM_data4:
21999 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22001 case DW_FORM_data8:
22002 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22005 case DW_FORM_sdata:
22006 case DW_FORM_implicit_const:
22007 *value = DW_SND (attr);
22010 case DW_FORM_udata:
22011 *value = DW_UNSND (attr);
22015 complaint (_("unsupported const value attribute form: '%s'"),
22016 dwarf_form_name (attr->form));
22023 /* Copy constant value from an attribute to a symbol. */
22026 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22027 struct dwarf2_cu *cu)
22029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22031 const gdb_byte *bytes;
22032 struct dwarf2_locexpr_baton *baton;
22034 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22035 SYMBOL_PRINT_NAME (sym),
22036 &objfile->objfile_obstack, cu,
22037 &value, &bytes, &baton);
22041 SYMBOL_LOCATION_BATON (sym) = baton;
22042 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22044 else if (bytes != NULL)
22046 SYMBOL_VALUE_BYTES (sym) = bytes;
22047 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22051 SYMBOL_VALUE (sym) = value;
22052 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22056 /* Return the type of the die in question using its DW_AT_type attribute. */
22058 static struct type *
22059 die_type (struct die_info *die, struct dwarf2_cu *cu)
22061 struct attribute *type_attr;
22063 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22067 /* A missing DW_AT_type represents a void type. */
22068 return objfile_type (objfile)->builtin_void;
22071 return lookup_die_type (die, type_attr, cu);
22074 /* True iff CU's producer generates GNAT Ada auxiliary information
22075 that allows to find parallel types through that information instead
22076 of having to do expensive parallel lookups by type name. */
22079 need_gnat_info (struct dwarf2_cu *cu)
22081 /* Assume that the Ada compiler was GNAT, which always produces
22082 the auxiliary information. */
22083 return (cu->language == language_ada);
22086 /* Return the auxiliary type of the die in question using its
22087 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22088 attribute is not present. */
22090 static struct type *
22091 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22093 struct attribute *type_attr;
22095 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22099 return lookup_die_type (die, type_attr, cu);
22102 /* If DIE has a descriptive_type attribute, then set the TYPE's
22103 descriptive type accordingly. */
22106 set_descriptive_type (struct type *type, struct die_info *die,
22107 struct dwarf2_cu *cu)
22109 struct type *descriptive_type = die_descriptive_type (die, cu);
22111 if (descriptive_type)
22113 ALLOCATE_GNAT_AUX_TYPE (type);
22114 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22118 /* Return the containing type of the die in question using its
22119 DW_AT_containing_type attribute. */
22121 static struct type *
22122 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22124 struct attribute *type_attr;
22125 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22127 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22129 error (_("Dwarf Error: Problem turning containing type into gdb type "
22130 "[in module %s]"), objfile_name (objfile));
22132 return lookup_die_type (die, type_attr, cu);
22135 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22137 static struct type *
22138 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22140 struct dwarf2_per_objfile *dwarf2_per_objfile
22141 = cu->per_cu->dwarf2_per_objfile;
22142 struct objfile *objfile = dwarf2_per_objfile->objfile;
22145 std::string message
22146 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22147 objfile_name (objfile),
22148 sect_offset_str (cu->header.sect_off),
22149 sect_offset_str (die->sect_off));
22150 saved = obstack_strdup (&objfile->objfile_obstack, message);
22152 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22155 /* Look up the type of DIE in CU using its type attribute ATTR.
22156 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22157 DW_AT_containing_type.
22158 If there is no type substitute an error marker. */
22160 static struct type *
22161 lookup_die_type (struct die_info *die, const struct attribute *attr,
22162 struct dwarf2_cu *cu)
22164 struct dwarf2_per_objfile *dwarf2_per_objfile
22165 = cu->per_cu->dwarf2_per_objfile;
22166 struct objfile *objfile = dwarf2_per_objfile->objfile;
22167 struct type *this_type;
22169 gdb_assert (attr->name == DW_AT_type
22170 || attr->name == DW_AT_GNAT_descriptive_type
22171 || attr->name == DW_AT_containing_type);
22173 /* First see if we have it cached. */
22175 if (attr->form == DW_FORM_GNU_ref_alt)
22177 struct dwarf2_per_cu_data *per_cu;
22178 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22180 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22181 dwarf2_per_objfile);
22182 this_type = get_die_type_at_offset (sect_off, per_cu);
22184 else if (attr_form_is_ref (attr))
22186 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22188 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22190 else if (attr->form == DW_FORM_ref_sig8)
22192 ULONGEST signature = DW_SIGNATURE (attr);
22194 return get_signatured_type (die, signature, cu);
22198 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22199 " at %s [in module %s]"),
22200 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22201 objfile_name (objfile));
22202 return build_error_marker_type (cu, die);
22205 /* If not cached we need to read it in. */
22207 if (this_type == NULL)
22209 struct die_info *type_die = NULL;
22210 struct dwarf2_cu *type_cu = cu;
22212 if (attr_form_is_ref (attr))
22213 type_die = follow_die_ref (die, attr, &type_cu);
22214 if (type_die == NULL)
22215 return build_error_marker_type (cu, die);
22216 /* If we find the type now, it's probably because the type came
22217 from an inter-CU reference and the type's CU got expanded before
22219 this_type = read_type_die (type_die, type_cu);
22222 /* If we still don't have a type use an error marker. */
22224 if (this_type == NULL)
22225 return build_error_marker_type (cu, die);
22230 /* Return the type in DIE, CU.
22231 Returns NULL for invalid types.
22233 This first does a lookup in die_type_hash,
22234 and only reads the die in if necessary.
22236 NOTE: This can be called when reading in partial or full symbols. */
22238 static struct type *
22239 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22241 struct type *this_type;
22243 this_type = get_die_type (die, cu);
22247 return read_type_die_1 (die, cu);
22250 /* Read the type in DIE, CU.
22251 Returns NULL for invalid types. */
22253 static struct type *
22254 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22256 struct type *this_type = NULL;
22260 case DW_TAG_class_type:
22261 case DW_TAG_interface_type:
22262 case DW_TAG_structure_type:
22263 case DW_TAG_union_type:
22264 this_type = read_structure_type (die, cu);
22266 case DW_TAG_enumeration_type:
22267 this_type = read_enumeration_type (die, cu);
22269 case DW_TAG_subprogram:
22270 case DW_TAG_subroutine_type:
22271 case DW_TAG_inlined_subroutine:
22272 this_type = read_subroutine_type (die, cu);
22274 case DW_TAG_array_type:
22275 this_type = read_array_type (die, cu);
22277 case DW_TAG_set_type:
22278 this_type = read_set_type (die, cu);
22280 case DW_TAG_pointer_type:
22281 this_type = read_tag_pointer_type (die, cu);
22283 case DW_TAG_ptr_to_member_type:
22284 this_type = read_tag_ptr_to_member_type (die, cu);
22286 case DW_TAG_reference_type:
22287 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22289 case DW_TAG_rvalue_reference_type:
22290 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22292 case DW_TAG_const_type:
22293 this_type = read_tag_const_type (die, cu);
22295 case DW_TAG_volatile_type:
22296 this_type = read_tag_volatile_type (die, cu);
22298 case DW_TAG_restrict_type:
22299 this_type = read_tag_restrict_type (die, cu);
22301 case DW_TAG_string_type:
22302 this_type = read_tag_string_type (die, cu);
22304 case DW_TAG_typedef:
22305 this_type = read_typedef (die, cu);
22307 case DW_TAG_subrange_type:
22308 this_type = read_subrange_type (die, cu);
22310 case DW_TAG_base_type:
22311 this_type = read_base_type (die, cu);
22313 case DW_TAG_unspecified_type:
22314 this_type = read_unspecified_type (die, cu);
22316 case DW_TAG_namespace:
22317 this_type = read_namespace_type (die, cu);
22319 case DW_TAG_module:
22320 this_type = read_module_type (die, cu);
22322 case DW_TAG_atomic_type:
22323 this_type = read_tag_atomic_type (die, cu);
22326 complaint (_("unexpected tag in read_type_die: '%s'"),
22327 dwarf_tag_name (die->tag));
22334 /* See if we can figure out if the class lives in a namespace. We do
22335 this by looking for a member function; its demangled name will
22336 contain namespace info, if there is any.
22337 Return the computed name or NULL.
22338 Space for the result is allocated on the objfile's obstack.
22339 This is the full-die version of guess_partial_die_structure_name.
22340 In this case we know DIE has no useful parent. */
22343 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22345 struct die_info *spec_die;
22346 struct dwarf2_cu *spec_cu;
22347 struct die_info *child;
22348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22351 spec_die = die_specification (die, &spec_cu);
22352 if (spec_die != NULL)
22358 for (child = die->child;
22360 child = child->sibling)
22362 if (child->tag == DW_TAG_subprogram)
22364 const char *linkage_name = dw2_linkage_name (child, cu);
22366 if (linkage_name != NULL)
22369 = language_class_name_from_physname (cu->language_defn,
22373 if (actual_name != NULL)
22375 const char *die_name = dwarf2_name (die, cu);
22377 if (die_name != NULL
22378 && strcmp (die_name, actual_name) != 0)
22380 /* Strip off the class name from the full name.
22381 We want the prefix. */
22382 int die_name_len = strlen (die_name);
22383 int actual_name_len = strlen (actual_name);
22385 /* Test for '::' as a sanity check. */
22386 if (actual_name_len > die_name_len + 2
22387 && actual_name[actual_name_len
22388 - die_name_len - 1] == ':')
22389 name = obstack_strndup (
22390 &objfile->per_bfd->storage_obstack,
22391 actual_name, actual_name_len - die_name_len - 2);
22394 xfree (actual_name);
22403 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22404 prefix part in such case. See
22405 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22407 static const char *
22408 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22410 struct attribute *attr;
22413 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22414 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22417 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22420 attr = dw2_linkage_name_attr (die, cu);
22421 if (attr == NULL || DW_STRING (attr) == NULL)
22424 /* dwarf2_name had to be already called. */
22425 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22427 /* Strip the base name, keep any leading namespaces/classes. */
22428 base = strrchr (DW_STRING (attr), ':');
22429 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22432 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22433 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22435 &base[-1] - DW_STRING (attr));
22438 /* Return the name of the namespace/class that DIE is defined within,
22439 or "" if we can't tell. The caller should not xfree the result.
22441 For example, if we're within the method foo() in the following
22451 then determine_prefix on foo's die will return "N::C". */
22453 static const char *
22454 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22456 struct dwarf2_per_objfile *dwarf2_per_objfile
22457 = cu->per_cu->dwarf2_per_objfile;
22458 struct die_info *parent, *spec_die;
22459 struct dwarf2_cu *spec_cu;
22460 struct type *parent_type;
22461 const char *retval;
22463 if (cu->language != language_cplus
22464 && cu->language != language_fortran && cu->language != language_d
22465 && cu->language != language_rust)
22468 retval = anonymous_struct_prefix (die, cu);
22472 /* We have to be careful in the presence of DW_AT_specification.
22473 For example, with GCC 3.4, given the code
22477 // Definition of N::foo.
22481 then we'll have a tree of DIEs like this:
22483 1: DW_TAG_compile_unit
22484 2: DW_TAG_namespace // N
22485 3: DW_TAG_subprogram // declaration of N::foo
22486 4: DW_TAG_subprogram // definition of N::foo
22487 DW_AT_specification // refers to die #3
22489 Thus, when processing die #4, we have to pretend that we're in
22490 the context of its DW_AT_specification, namely the contex of die
22493 spec_die = die_specification (die, &spec_cu);
22494 if (spec_die == NULL)
22495 parent = die->parent;
22498 parent = spec_die->parent;
22502 if (parent == NULL)
22504 else if (parent->building_fullname)
22507 const char *parent_name;
22509 /* It has been seen on RealView 2.2 built binaries,
22510 DW_TAG_template_type_param types actually _defined_ as
22511 children of the parent class:
22514 template class <class Enum> Class{};
22515 Class<enum E> class_e;
22517 1: DW_TAG_class_type (Class)
22518 2: DW_TAG_enumeration_type (E)
22519 3: DW_TAG_enumerator (enum1:0)
22520 3: DW_TAG_enumerator (enum2:1)
22522 2: DW_TAG_template_type_param
22523 DW_AT_type DW_FORM_ref_udata (E)
22525 Besides being broken debug info, it can put GDB into an
22526 infinite loop. Consider:
22528 When we're building the full name for Class<E>, we'll start
22529 at Class, and go look over its template type parameters,
22530 finding E. We'll then try to build the full name of E, and
22531 reach here. We're now trying to build the full name of E,
22532 and look over the parent DIE for containing scope. In the
22533 broken case, if we followed the parent DIE of E, we'd again
22534 find Class, and once again go look at its template type
22535 arguments, etc., etc. Simply don't consider such parent die
22536 as source-level parent of this die (it can't be, the language
22537 doesn't allow it), and break the loop here. */
22538 name = dwarf2_name (die, cu);
22539 parent_name = dwarf2_name (parent, cu);
22540 complaint (_("template param type '%s' defined within parent '%s'"),
22541 name ? name : "<unknown>",
22542 parent_name ? parent_name : "<unknown>");
22546 switch (parent->tag)
22548 case DW_TAG_namespace:
22549 parent_type = read_type_die (parent, cu);
22550 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22551 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22552 Work around this problem here. */
22553 if (cu->language == language_cplus
22554 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22556 /* We give a name to even anonymous namespaces. */
22557 return TYPE_NAME (parent_type);
22558 case DW_TAG_class_type:
22559 case DW_TAG_interface_type:
22560 case DW_TAG_structure_type:
22561 case DW_TAG_union_type:
22562 case DW_TAG_module:
22563 parent_type = read_type_die (parent, cu);
22564 if (TYPE_NAME (parent_type) != NULL)
22565 return TYPE_NAME (parent_type);
22567 /* An anonymous structure is only allowed non-static data
22568 members; no typedefs, no member functions, et cetera.
22569 So it does not need a prefix. */
22571 case DW_TAG_compile_unit:
22572 case DW_TAG_partial_unit:
22573 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22574 if (cu->language == language_cplus
22575 && !dwarf2_per_objfile->types.empty ()
22576 && die->child != NULL
22577 && (die->tag == DW_TAG_class_type
22578 || die->tag == DW_TAG_structure_type
22579 || die->tag == DW_TAG_union_type))
22581 char *name = guess_full_die_structure_name (die, cu);
22586 case DW_TAG_enumeration_type:
22587 parent_type = read_type_die (parent, cu);
22588 if (TYPE_DECLARED_CLASS (parent_type))
22590 if (TYPE_NAME (parent_type) != NULL)
22591 return TYPE_NAME (parent_type);
22594 /* Fall through. */
22596 return determine_prefix (parent, cu);
22600 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22601 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22602 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22603 an obconcat, otherwise allocate storage for the result. The CU argument is
22604 used to determine the language and hence, the appropriate separator. */
22606 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22609 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22610 int physname, struct dwarf2_cu *cu)
22612 const char *lead = "";
22615 if (suffix == NULL || suffix[0] == '\0'
22616 || prefix == NULL || prefix[0] == '\0')
22618 else if (cu->language == language_d)
22620 /* For D, the 'main' function could be defined in any module, but it
22621 should never be prefixed. */
22622 if (strcmp (suffix, "D main") == 0)
22630 else if (cu->language == language_fortran && physname)
22632 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22633 DW_AT_MIPS_linkage_name is preferred and used instead. */
22641 if (prefix == NULL)
22643 if (suffix == NULL)
22650 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22652 strcpy (retval, lead);
22653 strcat (retval, prefix);
22654 strcat (retval, sep);
22655 strcat (retval, suffix);
22660 /* We have an obstack. */
22661 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22665 /* Return sibling of die, NULL if no sibling. */
22667 static struct die_info *
22668 sibling_die (struct die_info *die)
22670 return die->sibling;
22673 /* Get name of a die, return NULL if not found. */
22675 static const char *
22676 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22677 struct obstack *obstack)
22679 if (name && cu->language == language_cplus)
22681 std::string canon_name = cp_canonicalize_string (name);
22683 if (!canon_name.empty ())
22685 if (canon_name != name)
22686 name = obstack_strdup (obstack, canon_name);
22693 /* Get name of a die, return NULL if not found.
22694 Anonymous namespaces are converted to their magic string. */
22696 static const char *
22697 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22699 struct attribute *attr;
22700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22702 attr = dwarf2_attr (die, DW_AT_name, cu);
22703 if ((!attr || !DW_STRING (attr))
22704 && die->tag != DW_TAG_namespace
22705 && die->tag != DW_TAG_class_type
22706 && die->tag != DW_TAG_interface_type
22707 && die->tag != DW_TAG_structure_type
22708 && die->tag != DW_TAG_union_type)
22713 case DW_TAG_compile_unit:
22714 case DW_TAG_partial_unit:
22715 /* Compilation units have a DW_AT_name that is a filename, not
22716 a source language identifier. */
22717 case DW_TAG_enumeration_type:
22718 case DW_TAG_enumerator:
22719 /* These tags always have simple identifiers already; no need
22720 to canonicalize them. */
22721 return DW_STRING (attr);
22723 case DW_TAG_namespace:
22724 if (attr != NULL && DW_STRING (attr) != NULL)
22725 return DW_STRING (attr);
22726 return CP_ANONYMOUS_NAMESPACE_STR;
22728 case DW_TAG_class_type:
22729 case DW_TAG_interface_type:
22730 case DW_TAG_structure_type:
22731 case DW_TAG_union_type:
22732 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22733 structures or unions. These were of the form "._%d" in GCC 4.1,
22734 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22735 and GCC 4.4. We work around this problem by ignoring these. */
22736 if (attr && DW_STRING (attr)
22737 && (startswith (DW_STRING (attr), "._")
22738 || startswith (DW_STRING (attr), "<anonymous")))
22741 /* GCC might emit a nameless typedef that has a linkage name. See
22742 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22743 if (!attr || DW_STRING (attr) == NULL)
22745 char *demangled = NULL;
22747 attr = dw2_linkage_name_attr (die, cu);
22748 if (attr == NULL || DW_STRING (attr) == NULL)
22751 /* Avoid demangling DW_STRING (attr) the second time on a second
22752 call for the same DIE. */
22753 if (!DW_STRING_IS_CANONICAL (attr))
22754 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22760 /* FIXME: we already did this for the partial symbol... */
22762 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22764 DW_STRING_IS_CANONICAL (attr) = 1;
22767 /* Strip any leading namespaces/classes, keep only the base name.
22768 DW_AT_name for named DIEs does not contain the prefixes. */
22769 base = strrchr (DW_STRING (attr), ':');
22770 if (base && base > DW_STRING (attr) && base[-1] == ':')
22773 return DW_STRING (attr);
22782 if (!DW_STRING_IS_CANONICAL (attr))
22785 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22786 &objfile->per_bfd->storage_obstack);
22787 DW_STRING_IS_CANONICAL (attr) = 1;
22789 return DW_STRING (attr);
22792 /* Return the die that this die in an extension of, or NULL if there
22793 is none. *EXT_CU is the CU containing DIE on input, and the CU
22794 containing the return value on output. */
22796 static struct die_info *
22797 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22799 struct attribute *attr;
22801 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22805 return follow_die_ref (die, attr, ext_cu);
22808 /* A convenience function that returns an "unknown" DWARF name,
22809 including the value of V. STR is the name of the entity being
22810 printed, e.g., "TAG". */
22812 static const char *
22813 dwarf_unknown (const char *str, unsigned v)
22815 char *cell = get_print_cell ();
22816 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22820 /* Convert a DIE tag into its string name. */
22822 static const char *
22823 dwarf_tag_name (unsigned tag)
22825 const char *name = get_DW_TAG_name (tag);
22828 return dwarf_unknown ("TAG", tag);
22833 /* Convert a DWARF attribute code into its string name. */
22835 static const char *
22836 dwarf_attr_name (unsigned attr)
22840 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22841 if (attr == DW_AT_MIPS_fde)
22842 return "DW_AT_MIPS_fde";
22844 if (attr == DW_AT_HP_block_index)
22845 return "DW_AT_HP_block_index";
22848 name = get_DW_AT_name (attr);
22851 return dwarf_unknown ("AT", attr);
22856 /* Convert a unit type to corresponding DW_UT name. */
22858 static const char *
22859 dwarf_unit_type_name (int unit_type) {
22863 return "DW_UT_compile (0x01)";
22865 return "DW_UT_type (0x02)";
22867 return "DW_UT_partial (0x03)";
22869 return "DW_UT_skeleton (0x04)";
22871 return "DW_UT_split_compile (0x05)";
22873 return "DW_UT_split_type (0x06)";
22875 return "DW_UT_lo_user (0x80)";
22877 return "DW_UT_hi_user (0xff)";
22883 /* Convert a DWARF value form code into its string name. */
22885 static const char *
22886 dwarf_form_name (unsigned form)
22888 const char *name = get_DW_FORM_name (form);
22891 return dwarf_unknown ("FORM", form);
22896 static const char *
22897 dwarf_bool_name (unsigned mybool)
22905 /* Convert a DWARF type code into its string name. */
22907 static const char *
22908 dwarf_type_encoding_name (unsigned enc)
22910 const char *name = get_DW_ATE_name (enc);
22913 return dwarf_unknown ("ATE", enc);
22919 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22923 print_spaces (indent, f);
22924 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22925 dwarf_tag_name (die->tag), die->abbrev,
22926 sect_offset_str (die->sect_off));
22928 if (die->parent != NULL)
22930 print_spaces (indent, f);
22931 fprintf_unfiltered (f, " parent at offset: %s\n",
22932 sect_offset_str (die->parent->sect_off));
22935 print_spaces (indent, f);
22936 fprintf_unfiltered (f, " has children: %s\n",
22937 dwarf_bool_name (die->child != NULL));
22939 print_spaces (indent, f);
22940 fprintf_unfiltered (f, " attributes:\n");
22942 for (i = 0; i < die->num_attrs; ++i)
22944 print_spaces (indent, f);
22945 fprintf_unfiltered (f, " %s (%s) ",
22946 dwarf_attr_name (die->attrs[i].name),
22947 dwarf_form_name (die->attrs[i].form));
22949 switch (die->attrs[i].form)
22952 case DW_FORM_addrx:
22953 case DW_FORM_GNU_addr_index:
22954 fprintf_unfiltered (f, "address: ");
22955 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22957 case DW_FORM_block2:
22958 case DW_FORM_block4:
22959 case DW_FORM_block:
22960 case DW_FORM_block1:
22961 fprintf_unfiltered (f, "block: size %s",
22962 pulongest (DW_BLOCK (&die->attrs[i])->size));
22964 case DW_FORM_exprloc:
22965 fprintf_unfiltered (f, "expression: size %s",
22966 pulongest (DW_BLOCK (&die->attrs[i])->size));
22968 case DW_FORM_data16:
22969 fprintf_unfiltered (f, "constant of 16 bytes");
22971 case DW_FORM_ref_addr:
22972 fprintf_unfiltered (f, "ref address: ");
22973 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22975 case DW_FORM_GNU_ref_alt:
22976 fprintf_unfiltered (f, "alt ref address: ");
22977 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22983 case DW_FORM_ref_udata:
22984 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22985 (long) (DW_UNSND (&die->attrs[i])));
22987 case DW_FORM_data1:
22988 case DW_FORM_data2:
22989 case DW_FORM_data4:
22990 case DW_FORM_data8:
22991 case DW_FORM_udata:
22992 case DW_FORM_sdata:
22993 fprintf_unfiltered (f, "constant: %s",
22994 pulongest (DW_UNSND (&die->attrs[i])));
22996 case DW_FORM_sec_offset:
22997 fprintf_unfiltered (f, "section offset: %s",
22998 pulongest (DW_UNSND (&die->attrs[i])));
23000 case DW_FORM_ref_sig8:
23001 fprintf_unfiltered (f, "signature: %s",
23002 hex_string (DW_SIGNATURE (&die->attrs[i])));
23004 case DW_FORM_string:
23006 case DW_FORM_line_strp:
23008 case DW_FORM_GNU_str_index:
23009 case DW_FORM_GNU_strp_alt:
23010 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23011 DW_STRING (&die->attrs[i])
23012 ? DW_STRING (&die->attrs[i]) : "",
23013 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23016 if (DW_UNSND (&die->attrs[i]))
23017 fprintf_unfiltered (f, "flag: TRUE");
23019 fprintf_unfiltered (f, "flag: FALSE");
23021 case DW_FORM_flag_present:
23022 fprintf_unfiltered (f, "flag: TRUE");
23024 case DW_FORM_indirect:
23025 /* The reader will have reduced the indirect form to
23026 the "base form" so this form should not occur. */
23027 fprintf_unfiltered (f,
23028 "unexpected attribute form: DW_FORM_indirect");
23030 case DW_FORM_implicit_const:
23031 fprintf_unfiltered (f, "constant: %s",
23032 plongest (DW_SND (&die->attrs[i])));
23035 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23036 die->attrs[i].form);
23039 fprintf_unfiltered (f, "\n");
23044 dump_die_for_error (struct die_info *die)
23046 dump_die_shallow (gdb_stderr, 0, die);
23050 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23052 int indent = level * 4;
23054 gdb_assert (die != NULL);
23056 if (level >= max_level)
23059 dump_die_shallow (f, indent, die);
23061 if (die->child != NULL)
23063 print_spaces (indent, f);
23064 fprintf_unfiltered (f, " Children:");
23065 if (level + 1 < max_level)
23067 fprintf_unfiltered (f, "\n");
23068 dump_die_1 (f, level + 1, max_level, die->child);
23072 fprintf_unfiltered (f,
23073 " [not printed, max nesting level reached]\n");
23077 if (die->sibling != NULL && level > 0)
23079 dump_die_1 (f, level, max_level, die->sibling);
23083 /* This is called from the pdie macro in gdbinit.in.
23084 It's not static so gcc will keep a copy callable from gdb. */
23087 dump_die (struct die_info *die, int max_level)
23089 dump_die_1 (gdb_stdlog, 0, max_level, die);
23093 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23097 slot = htab_find_slot_with_hash (cu->die_hash, die,
23098 to_underlying (die->sect_off),
23104 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23108 dwarf2_get_ref_die_offset (const struct attribute *attr)
23110 if (attr_form_is_ref (attr))
23111 return (sect_offset) DW_UNSND (attr);
23113 complaint (_("unsupported die ref attribute form: '%s'"),
23114 dwarf_form_name (attr->form));
23118 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23119 * the value held by the attribute is not constant. */
23122 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23124 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23125 return DW_SND (attr);
23126 else if (attr->form == DW_FORM_udata
23127 || attr->form == DW_FORM_data1
23128 || attr->form == DW_FORM_data2
23129 || attr->form == DW_FORM_data4
23130 || attr->form == DW_FORM_data8)
23131 return DW_UNSND (attr);
23134 /* For DW_FORM_data16 see attr_form_is_constant. */
23135 complaint (_("Attribute value is not a constant (%s)"),
23136 dwarf_form_name (attr->form));
23137 return default_value;
23141 /* Follow reference or signature attribute ATTR of SRC_DIE.
23142 On entry *REF_CU is the CU of SRC_DIE.
23143 On exit *REF_CU is the CU of the result. */
23145 static struct die_info *
23146 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23147 struct dwarf2_cu **ref_cu)
23149 struct die_info *die;
23151 if (attr_form_is_ref (attr))
23152 die = follow_die_ref (src_die, attr, ref_cu);
23153 else if (attr->form == DW_FORM_ref_sig8)
23154 die = follow_die_sig (src_die, attr, ref_cu);
23157 dump_die_for_error (src_die);
23158 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23159 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23165 /* Follow reference OFFSET.
23166 On entry *REF_CU is the CU of the source die referencing OFFSET.
23167 On exit *REF_CU is the CU of the result.
23168 Returns NULL if OFFSET is invalid. */
23170 static struct die_info *
23171 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23172 struct dwarf2_cu **ref_cu)
23174 struct die_info temp_die;
23175 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23176 struct dwarf2_per_objfile *dwarf2_per_objfile
23177 = cu->per_cu->dwarf2_per_objfile;
23179 gdb_assert (cu->per_cu != NULL);
23183 if (cu->per_cu->is_debug_types)
23185 /* .debug_types CUs cannot reference anything outside their CU.
23186 If they need to, they have to reference a signatured type via
23187 DW_FORM_ref_sig8. */
23188 if (!offset_in_cu_p (&cu->header, sect_off))
23191 else if (offset_in_dwz != cu->per_cu->is_dwz
23192 || !offset_in_cu_p (&cu->header, sect_off))
23194 struct dwarf2_per_cu_data *per_cu;
23196 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23197 dwarf2_per_objfile);
23199 /* If necessary, add it to the queue and load its DIEs. */
23200 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23201 load_full_comp_unit (per_cu, false, cu->language);
23203 target_cu = per_cu->cu;
23205 else if (cu->dies == NULL)
23207 /* We're loading full DIEs during partial symbol reading. */
23208 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23209 load_full_comp_unit (cu->per_cu, false, language_minimal);
23212 *ref_cu = target_cu;
23213 temp_die.sect_off = sect_off;
23215 if (target_cu != cu)
23216 target_cu->ancestor = cu;
23218 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23220 to_underlying (sect_off));
23223 /* Follow reference attribute ATTR of SRC_DIE.
23224 On entry *REF_CU is the CU of SRC_DIE.
23225 On exit *REF_CU is the CU of the result. */
23227 static struct die_info *
23228 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23229 struct dwarf2_cu **ref_cu)
23231 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23232 struct dwarf2_cu *cu = *ref_cu;
23233 struct die_info *die;
23235 die = follow_die_offset (sect_off,
23236 (attr->form == DW_FORM_GNU_ref_alt
23237 || cu->per_cu->is_dwz),
23240 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23241 "at %s [in module %s]"),
23242 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23243 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23248 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23249 Returned value is intended for DW_OP_call*. Returned
23250 dwarf2_locexpr_baton->data has lifetime of
23251 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23253 struct dwarf2_locexpr_baton
23254 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23255 struct dwarf2_per_cu_data *per_cu,
23256 CORE_ADDR (*get_frame_pc) (void *baton),
23257 void *baton, bool resolve_abstract_p)
23259 struct dwarf2_cu *cu;
23260 struct die_info *die;
23261 struct attribute *attr;
23262 struct dwarf2_locexpr_baton retval;
23263 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23264 struct objfile *objfile = dwarf2_per_objfile->objfile;
23266 if (per_cu->cu == NULL)
23267 load_cu (per_cu, false);
23271 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23272 Instead just throw an error, not much else we can do. */
23273 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23274 sect_offset_str (sect_off), objfile_name (objfile));
23277 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23279 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23280 sect_offset_str (sect_off), objfile_name (objfile));
23282 attr = dwarf2_attr (die, DW_AT_location, cu);
23283 if (!attr && resolve_abstract_p
23284 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23285 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23287 CORE_ADDR pc = (*get_frame_pc) (baton);
23289 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23292 for (const auto &cand_off
23293 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23295 struct dwarf2_cu *cand_cu = cu;
23296 struct die_info *cand
23297 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23300 || cand->parent->tag != DW_TAG_subprogram)
23303 CORE_ADDR pc_low, pc_high;
23304 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23305 if (pc_low == ((CORE_ADDR) -1))
23307 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23308 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23309 if (!(pc_low <= pc && pc < pc_high))
23313 attr = dwarf2_attr (die, DW_AT_location, cu);
23320 /* DWARF: "If there is no such attribute, then there is no effect.".
23321 DATA is ignored if SIZE is 0. */
23323 retval.data = NULL;
23326 else if (attr_form_is_section_offset (attr))
23328 struct dwarf2_loclist_baton loclist_baton;
23329 CORE_ADDR pc = (*get_frame_pc) (baton);
23332 fill_in_loclist_baton (cu, &loclist_baton, attr);
23334 retval.data = dwarf2_find_location_expression (&loclist_baton,
23336 retval.size = size;
23340 if (!attr_form_is_block (attr))
23341 error (_("Dwarf Error: DIE at %s referenced in module %s "
23342 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23343 sect_offset_str (sect_off), objfile_name (objfile));
23345 retval.data = DW_BLOCK (attr)->data;
23346 retval.size = DW_BLOCK (attr)->size;
23348 retval.per_cu = cu->per_cu;
23350 age_cached_comp_units (dwarf2_per_objfile);
23355 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23358 struct dwarf2_locexpr_baton
23359 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23360 struct dwarf2_per_cu_data *per_cu,
23361 CORE_ADDR (*get_frame_pc) (void *baton),
23364 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23366 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23369 /* Write a constant of a given type as target-ordered bytes into
23372 static const gdb_byte *
23373 write_constant_as_bytes (struct obstack *obstack,
23374 enum bfd_endian byte_order,
23381 *len = TYPE_LENGTH (type);
23382 result = (gdb_byte *) obstack_alloc (obstack, *len);
23383 store_unsigned_integer (result, *len, byte_order, value);
23388 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23389 pointer to the constant bytes and set LEN to the length of the
23390 data. If memory is needed, allocate it on OBSTACK. If the DIE
23391 does not have a DW_AT_const_value, return NULL. */
23394 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23395 struct dwarf2_per_cu_data *per_cu,
23396 struct obstack *obstack,
23399 struct dwarf2_cu *cu;
23400 struct die_info *die;
23401 struct attribute *attr;
23402 const gdb_byte *result = NULL;
23405 enum bfd_endian byte_order;
23406 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23408 if (per_cu->cu == NULL)
23409 load_cu (per_cu, false);
23413 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23414 Instead just throw an error, not much else we can do. */
23415 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23416 sect_offset_str (sect_off), objfile_name (objfile));
23419 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23421 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23422 sect_offset_str (sect_off), objfile_name (objfile));
23424 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23428 byte_order = (bfd_big_endian (objfile->obfd)
23429 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23431 switch (attr->form)
23434 case DW_FORM_addrx:
23435 case DW_FORM_GNU_addr_index:
23439 *len = cu->header.addr_size;
23440 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23441 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23445 case DW_FORM_string:
23448 case DW_FORM_GNU_str_index:
23449 case DW_FORM_GNU_strp_alt:
23450 /* DW_STRING is already allocated on the objfile obstack, point
23452 result = (const gdb_byte *) DW_STRING (attr);
23453 *len = strlen (DW_STRING (attr));
23455 case DW_FORM_block1:
23456 case DW_FORM_block2:
23457 case DW_FORM_block4:
23458 case DW_FORM_block:
23459 case DW_FORM_exprloc:
23460 case DW_FORM_data16:
23461 result = DW_BLOCK (attr)->data;
23462 *len = DW_BLOCK (attr)->size;
23465 /* The DW_AT_const_value attributes are supposed to carry the
23466 symbol's value "represented as it would be on the target
23467 architecture." By the time we get here, it's already been
23468 converted to host endianness, so we just need to sign- or
23469 zero-extend it as appropriate. */
23470 case DW_FORM_data1:
23471 type = die_type (die, cu);
23472 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23473 if (result == NULL)
23474 result = write_constant_as_bytes (obstack, byte_order,
23477 case DW_FORM_data2:
23478 type = die_type (die, cu);
23479 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23480 if (result == NULL)
23481 result = write_constant_as_bytes (obstack, byte_order,
23484 case DW_FORM_data4:
23485 type = die_type (die, cu);
23486 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23487 if (result == NULL)
23488 result = write_constant_as_bytes (obstack, byte_order,
23491 case DW_FORM_data8:
23492 type = die_type (die, cu);
23493 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23494 if (result == NULL)
23495 result = write_constant_as_bytes (obstack, byte_order,
23499 case DW_FORM_sdata:
23500 case DW_FORM_implicit_const:
23501 type = die_type (die, cu);
23502 result = write_constant_as_bytes (obstack, byte_order,
23503 type, DW_SND (attr), len);
23506 case DW_FORM_udata:
23507 type = die_type (die, cu);
23508 result = write_constant_as_bytes (obstack, byte_order,
23509 type, DW_UNSND (attr), len);
23513 complaint (_("unsupported const value attribute form: '%s'"),
23514 dwarf_form_name (attr->form));
23521 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23522 valid type for this die is found. */
23525 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23526 struct dwarf2_per_cu_data *per_cu)
23528 struct dwarf2_cu *cu;
23529 struct die_info *die;
23531 if (per_cu->cu == NULL)
23532 load_cu (per_cu, false);
23537 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23541 return die_type (die, cu);
23544 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23548 dwarf2_get_die_type (cu_offset die_offset,
23549 struct dwarf2_per_cu_data *per_cu)
23551 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23552 return get_die_type_at_offset (die_offset_sect, per_cu);
23555 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23556 On entry *REF_CU is the CU of SRC_DIE.
23557 On exit *REF_CU is the CU of the result.
23558 Returns NULL if the referenced DIE isn't found. */
23560 static struct die_info *
23561 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23562 struct dwarf2_cu **ref_cu)
23564 struct die_info temp_die;
23565 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23566 struct die_info *die;
23568 /* While it might be nice to assert sig_type->type == NULL here,
23569 we can get here for DW_AT_imported_declaration where we need
23570 the DIE not the type. */
23572 /* If necessary, add it to the queue and load its DIEs. */
23574 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23575 read_signatured_type (sig_type);
23577 sig_cu = sig_type->per_cu.cu;
23578 gdb_assert (sig_cu != NULL);
23579 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23580 temp_die.sect_off = sig_type->type_offset_in_section;
23581 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23582 to_underlying (temp_die.sect_off));
23585 struct dwarf2_per_objfile *dwarf2_per_objfile
23586 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23588 /* For .gdb_index version 7 keep track of included TUs.
23589 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23590 if (dwarf2_per_objfile->index_table != NULL
23591 && dwarf2_per_objfile->index_table->version <= 7)
23593 VEC_safe_push (dwarf2_per_cu_ptr,
23594 (*ref_cu)->per_cu->imported_symtabs,
23600 sig_cu->ancestor = cu;
23608 /* Follow signatured type referenced by ATTR in SRC_DIE.
23609 On entry *REF_CU is the CU of SRC_DIE.
23610 On exit *REF_CU is the CU of the result.
23611 The result is the DIE of the type.
23612 If the referenced type cannot be found an error is thrown. */
23614 static struct die_info *
23615 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23616 struct dwarf2_cu **ref_cu)
23618 ULONGEST signature = DW_SIGNATURE (attr);
23619 struct signatured_type *sig_type;
23620 struct die_info *die;
23622 gdb_assert (attr->form == DW_FORM_ref_sig8);
23624 sig_type = lookup_signatured_type (*ref_cu, signature);
23625 /* sig_type will be NULL if the signatured type is missing from
23627 if (sig_type == NULL)
23629 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23630 " from DIE at %s [in module %s]"),
23631 hex_string (signature), sect_offset_str (src_die->sect_off),
23632 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23635 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23638 dump_die_for_error (src_die);
23639 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23640 " from DIE at %s [in module %s]"),
23641 hex_string (signature), sect_offset_str (src_die->sect_off),
23642 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23648 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23649 reading in and processing the type unit if necessary. */
23651 static struct type *
23652 get_signatured_type (struct die_info *die, ULONGEST signature,
23653 struct dwarf2_cu *cu)
23655 struct dwarf2_per_objfile *dwarf2_per_objfile
23656 = cu->per_cu->dwarf2_per_objfile;
23657 struct signatured_type *sig_type;
23658 struct dwarf2_cu *type_cu;
23659 struct die_info *type_die;
23662 sig_type = lookup_signatured_type (cu, signature);
23663 /* sig_type will be NULL if the signatured type is missing from
23665 if (sig_type == NULL)
23667 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23668 " from DIE at %s [in module %s]"),
23669 hex_string (signature), sect_offset_str (die->sect_off),
23670 objfile_name (dwarf2_per_objfile->objfile));
23671 return build_error_marker_type (cu, die);
23674 /* If we already know the type we're done. */
23675 if (sig_type->type != NULL)
23676 return sig_type->type;
23679 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23680 if (type_die != NULL)
23682 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23683 is created. This is important, for example, because for c++ classes
23684 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23685 type = read_type_die (type_die, type_cu);
23688 complaint (_("Dwarf Error: Cannot build signatured type %s"
23689 " referenced from DIE at %s [in module %s]"),
23690 hex_string (signature), sect_offset_str (die->sect_off),
23691 objfile_name (dwarf2_per_objfile->objfile));
23692 type = build_error_marker_type (cu, die);
23697 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23698 " from DIE at %s [in module %s]"),
23699 hex_string (signature), sect_offset_str (die->sect_off),
23700 objfile_name (dwarf2_per_objfile->objfile));
23701 type = build_error_marker_type (cu, die);
23703 sig_type->type = type;
23708 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23709 reading in and processing the type unit if necessary. */
23711 static struct type *
23712 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23713 struct dwarf2_cu *cu) /* ARI: editCase function */
23715 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23716 if (attr_form_is_ref (attr))
23718 struct dwarf2_cu *type_cu = cu;
23719 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23721 return read_type_die (type_die, type_cu);
23723 else if (attr->form == DW_FORM_ref_sig8)
23725 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23729 struct dwarf2_per_objfile *dwarf2_per_objfile
23730 = cu->per_cu->dwarf2_per_objfile;
23732 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23733 " at %s [in module %s]"),
23734 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23735 objfile_name (dwarf2_per_objfile->objfile));
23736 return build_error_marker_type (cu, die);
23740 /* Load the DIEs associated with type unit PER_CU into memory. */
23743 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23745 struct signatured_type *sig_type;
23747 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23748 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23750 /* We have the per_cu, but we need the signatured_type.
23751 Fortunately this is an easy translation. */
23752 gdb_assert (per_cu->is_debug_types);
23753 sig_type = (struct signatured_type *) per_cu;
23755 gdb_assert (per_cu->cu == NULL);
23757 read_signatured_type (sig_type);
23759 gdb_assert (per_cu->cu != NULL);
23762 /* die_reader_func for read_signatured_type.
23763 This is identical to load_full_comp_unit_reader,
23764 but is kept separate for now. */
23767 read_signatured_type_reader (const struct die_reader_specs *reader,
23768 const gdb_byte *info_ptr,
23769 struct die_info *comp_unit_die,
23773 struct dwarf2_cu *cu = reader->cu;
23775 gdb_assert (cu->die_hash == NULL);
23777 htab_create_alloc_ex (cu->header.length / 12,
23781 &cu->comp_unit_obstack,
23782 hashtab_obstack_allocate,
23783 dummy_obstack_deallocate);
23786 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23787 &info_ptr, comp_unit_die);
23788 cu->dies = comp_unit_die;
23789 /* comp_unit_die is not stored in die_hash, no need. */
23791 /* We try not to read any attributes in this function, because not
23792 all CUs needed for references have been loaded yet, and symbol
23793 table processing isn't initialized. But we have to set the CU language,
23794 or we won't be able to build types correctly.
23795 Similarly, if we do not read the producer, we can not apply
23796 producer-specific interpretation. */
23797 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23800 /* Read in a signatured type and build its CU and DIEs.
23801 If the type is a stub for the real type in a DWO file,
23802 read in the real type from the DWO file as well. */
23805 read_signatured_type (struct signatured_type *sig_type)
23807 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23809 gdb_assert (per_cu->is_debug_types);
23810 gdb_assert (per_cu->cu == NULL);
23812 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23813 read_signatured_type_reader, NULL);
23814 sig_type->per_cu.tu_read = 1;
23817 /* Decode simple location descriptions.
23818 Given a pointer to a dwarf block that defines a location, compute
23819 the location and return the value.
23821 NOTE drow/2003-11-18: This function is called in two situations
23822 now: for the address of static or global variables (partial symbols
23823 only) and for offsets into structures which are expected to be
23824 (more or less) constant. The partial symbol case should go away,
23825 and only the constant case should remain. That will let this
23826 function complain more accurately. A few special modes are allowed
23827 without complaint for global variables (for instance, global
23828 register values and thread-local values).
23830 A location description containing no operations indicates that the
23831 object is optimized out. The return value is 0 for that case.
23832 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23833 callers will only want a very basic result and this can become a
23836 Note that stack[0] is unused except as a default error return. */
23839 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23843 size_t size = blk->size;
23844 const gdb_byte *data = blk->data;
23845 CORE_ADDR stack[64];
23847 unsigned int bytes_read, unsnd;
23853 stack[++stacki] = 0;
23892 stack[++stacki] = op - DW_OP_lit0;
23927 stack[++stacki] = op - DW_OP_reg0;
23929 dwarf2_complex_location_expr_complaint ();
23933 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23935 stack[++stacki] = unsnd;
23937 dwarf2_complex_location_expr_complaint ();
23941 stack[++stacki] = read_address (objfile->obfd, &data[i],
23946 case DW_OP_const1u:
23947 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23951 case DW_OP_const1s:
23952 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23956 case DW_OP_const2u:
23957 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23961 case DW_OP_const2s:
23962 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23966 case DW_OP_const4u:
23967 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23971 case DW_OP_const4s:
23972 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23976 case DW_OP_const8u:
23977 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23982 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23988 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23993 stack[stacki + 1] = stack[stacki];
23998 stack[stacki - 1] += stack[stacki];
24002 case DW_OP_plus_uconst:
24003 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24009 stack[stacki - 1] -= stack[stacki];
24014 /* If we're not the last op, then we definitely can't encode
24015 this using GDB's address_class enum. This is valid for partial
24016 global symbols, although the variable's address will be bogus
24019 dwarf2_complex_location_expr_complaint ();
24022 case DW_OP_GNU_push_tls_address:
24023 case DW_OP_form_tls_address:
24024 /* The top of the stack has the offset from the beginning
24025 of the thread control block at which the variable is located. */
24026 /* Nothing should follow this operator, so the top of stack would
24028 /* This is valid for partial global symbols, but the variable's
24029 address will be bogus in the psymtab. Make it always at least
24030 non-zero to not look as a variable garbage collected by linker
24031 which have DW_OP_addr 0. */
24033 dwarf2_complex_location_expr_complaint ();
24037 case DW_OP_GNU_uninit:
24041 case DW_OP_GNU_addr_index:
24042 case DW_OP_GNU_const_index:
24043 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24050 const char *name = get_DW_OP_name (op);
24053 complaint (_("unsupported stack op: '%s'"),
24056 complaint (_("unsupported stack op: '%02x'"),
24060 return (stack[stacki]);
24063 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24064 outside of the allocated space. Also enforce minimum>0. */
24065 if (stacki >= ARRAY_SIZE (stack) - 1)
24067 complaint (_("location description stack overflow"));
24073 complaint (_("location description stack underflow"));
24077 return (stack[stacki]);
24080 /* memory allocation interface */
24082 static struct dwarf_block *
24083 dwarf_alloc_block (struct dwarf2_cu *cu)
24085 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24088 static struct die_info *
24089 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24091 struct die_info *die;
24092 size_t size = sizeof (struct die_info);
24095 size += (num_attrs - 1) * sizeof (struct attribute);
24097 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24098 memset (die, 0, sizeof (struct die_info));
24103 /* Macro support. */
24105 /* Return file name relative to the compilation directory of file number I in
24106 *LH's file name table. The result is allocated using xmalloc; the caller is
24107 responsible for freeing it. */
24110 file_file_name (int file, struct line_header *lh)
24112 /* Is the file number a valid index into the line header's file name
24113 table? Remember that file numbers start with one, not zero. */
24114 if (1 <= file && file <= lh->file_names.size ())
24116 const file_entry &fe = lh->file_names[file - 1];
24118 if (!IS_ABSOLUTE_PATH (fe.name))
24120 const char *dir = fe.include_dir (lh);
24122 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24124 return xstrdup (fe.name);
24128 /* The compiler produced a bogus file number. We can at least
24129 record the macro definitions made in the file, even if we
24130 won't be able to find the file by name. */
24131 char fake_name[80];
24133 xsnprintf (fake_name, sizeof (fake_name),
24134 "<bad macro file number %d>", file);
24136 complaint (_("bad file number in macro information (%d)"),
24139 return xstrdup (fake_name);
24143 /* Return the full name of file number I in *LH's file name table.
24144 Use COMP_DIR as the name of the current directory of the
24145 compilation. The result is allocated using xmalloc; the caller is
24146 responsible for freeing it. */
24148 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24150 /* Is the file number a valid index into the line header's file name
24151 table? Remember that file numbers start with one, not zero. */
24152 if (1 <= file && file <= lh->file_names.size ())
24154 char *relative = file_file_name (file, lh);
24156 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24158 return reconcat (relative, comp_dir, SLASH_STRING,
24159 relative, (char *) NULL);
24162 return file_file_name (file, lh);
24166 static struct macro_source_file *
24167 macro_start_file (struct dwarf2_cu *cu,
24168 int file, int line,
24169 struct macro_source_file *current_file,
24170 struct line_header *lh)
24172 /* File name relative to the compilation directory of this source file. */
24173 char *file_name = file_file_name (file, lh);
24175 if (! current_file)
24177 /* Note: We don't create a macro table for this compilation unit
24178 at all until we actually get a filename. */
24179 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24181 /* If we have no current file, then this must be the start_file
24182 directive for the compilation unit's main source file. */
24183 current_file = macro_set_main (macro_table, file_name);
24184 macro_define_special (macro_table);
24187 current_file = macro_include (current_file, line, file_name);
24191 return current_file;
24194 static const char *
24195 consume_improper_spaces (const char *p, const char *body)
24199 complaint (_("macro definition contains spaces "
24200 "in formal argument list:\n`%s'"),
24212 parse_macro_definition (struct macro_source_file *file, int line,
24217 /* The body string takes one of two forms. For object-like macro
24218 definitions, it should be:
24220 <macro name> " " <definition>
24222 For function-like macro definitions, it should be:
24224 <macro name> "() " <definition>
24226 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24228 Spaces may appear only where explicitly indicated, and in the
24231 The Dwarf 2 spec says that an object-like macro's name is always
24232 followed by a space, but versions of GCC around March 2002 omit
24233 the space when the macro's definition is the empty string.
24235 The Dwarf 2 spec says that there should be no spaces between the
24236 formal arguments in a function-like macro's formal argument list,
24237 but versions of GCC around March 2002 include spaces after the
24241 /* Find the extent of the macro name. The macro name is terminated
24242 by either a space or null character (for an object-like macro) or
24243 an opening paren (for a function-like macro). */
24244 for (p = body; *p; p++)
24245 if (*p == ' ' || *p == '(')
24248 if (*p == ' ' || *p == '\0')
24250 /* It's an object-like macro. */
24251 int name_len = p - body;
24252 char *name = savestring (body, name_len);
24253 const char *replacement;
24256 replacement = body + name_len + 1;
24259 dwarf2_macro_malformed_definition_complaint (body);
24260 replacement = body + name_len;
24263 macro_define_object (file, line, name, replacement);
24267 else if (*p == '(')
24269 /* It's a function-like macro. */
24270 char *name = savestring (body, p - body);
24273 char **argv = XNEWVEC (char *, argv_size);
24277 p = consume_improper_spaces (p, body);
24279 /* Parse the formal argument list. */
24280 while (*p && *p != ')')
24282 /* Find the extent of the current argument name. */
24283 const char *arg_start = p;
24285 while (*p && *p != ',' && *p != ')' && *p != ' ')
24288 if (! *p || p == arg_start)
24289 dwarf2_macro_malformed_definition_complaint (body);
24292 /* Make sure argv has room for the new argument. */
24293 if (argc >= argv_size)
24296 argv = XRESIZEVEC (char *, argv, argv_size);
24299 argv[argc++] = savestring (arg_start, p - arg_start);
24302 p = consume_improper_spaces (p, body);
24304 /* Consume the comma, if present. */
24309 p = consume_improper_spaces (p, body);
24318 /* Perfectly formed definition, no complaints. */
24319 macro_define_function (file, line, name,
24320 argc, (const char **) argv,
24322 else if (*p == '\0')
24324 /* Complain, but do define it. */
24325 dwarf2_macro_malformed_definition_complaint (body);
24326 macro_define_function (file, line, name,
24327 argc, (const char **) argv,
24331 /* Just complain. */
24332 dwarf2_macro_malformed_definition_complaint (body);
24335 /* Just complain. */
24336 dwarf2_macro_malformed_definition_complaint (body);
24342 for (i = 0; i < argc; i++)
24348 dwarf2_macro_malformed_definition_complaint (body);
24351 /* Skip some bytes from BYTES according to the form given in FORM.
24352 Returns the new pointer. */
24354 static const gdb_byte *
24355 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24356 enum dwarf_form form,
24357 unsigned int offset_size,
24358 struct dwarf2_section_info *section)
24360 unsigned int bytes_read;
24364 case DW_FORM_data1:
24369 case DW_FORM_data2:
24373 case DW_FORM_data4:
24377 case DW_FORM_data8:
24381 case DW_FORM_data16:
24385 case DW_FORM_string:
24386 read_direct_string (abfd, bytes, &bytes_read);
24387 bytes += bytes_read;
24390 case DW_FORM_sec_offset:
24392 case DW_FORM_GNU_strp_alt:
24393 bytes += offset_size;
24396 case DW_FORM_block:
24397 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24398 bytes += bytes_read;
24401 case DW_FORM_block1:
24402 bytes += 1 + read_1_byte (abfd, bytes);
24404 case DW_FORM_block2:
24405 bytes += 2 + read_2_bytes (abfd, bytes);
24407 case DW_FORM_block4:
24408 bytes += 4 + read_4_bytes (abfd, bytes);
24411 case DW_FORM_addrx:
24412 case DW_FORM_sdata:
24414 case DW_FORM_udata:
24415 case DW_FORM_GNU_addr_index:
24416 case DW_FORM_GNU_str_index:
24417 bytes = gdb_skip_leb128 (bytes, buffer_end);
24420 dwarf2_section_buffer_overflow_complaint (section);
24425 case DW_FORM_implicit_const:
24430 complaint (_("invalid form 0x%x in `%s'"),
24431 form, get_section_name (section));
24439 /* A helper for dwarf_decode_macros that handles skipping an unknown
24440 opcode. Returns an updated pointer to the macro data buffer; or,
24441 on error, issues a complaint and returns NULL. */
24443 static const gdb_byte *
24444 skip_unknown_opcode (unsigned int opcode,
24445 const gdb_byte **opcode_definitions,
24446 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24448 unsigned int offset_size,
24449 struct dwarf2_section_info *section)
24451 unsigned int bytes_read, i;
24453 const gdb_byte *defn;
24455 if (opcode_definitions[opcode] == NULL)
24457 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24462 defn = opcode_definitions[opcode];
24463 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24464 defn += bytes_read;
24466 for (i = 0; i < arg; ++i)
24468 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24469 (enum dwarf_form) defn[i], offset_size,
24471 if (mac_ptr == NULL)
24473 /* skip_form_bytes already issued the complaint. */
24481 /* A helper function which parses the header of a macro section.
24482 If the macro section is the extended (for now called "GNU") type,
24483 then this updates *OFFSET_SIZE. Returns a pointer to just after
24484 the header, or issues a complaint and returns NULL on error. */
24486 static const gdb_byte *
24487 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24489 const gdb_byte *mac_ptr,
24490 unsigned int *offset_size,
24491 int section_is_gnu)
24493 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24495 if (section_is_gnu)
24497 unsigned int version, flags;
24499 version = read_2_bytes (abfd, mac_ptr);
24500 if (version != 4 && version != 5)
24502 complaint (_("unrecognized version `%d' in .debug_macro section"),
24508 flags = read_1_byte (abfd, mac_ptr);
24510 *offset_size = (flags & 1) ? 8 : 4;
24512 if ((flags & 2) != 0)
24513 /* We don't need the line table offset. */
24514 mac_ptr += *offset_size;
24516 /* Vendor opcode descriptions. */
24517 if ((flags & 4) != 0)
24519 unsigned int i, count;
24521 count = read_1_byte (abfd, mac_ptr);
24523 for (i = 0; i < count; ++i)
24525 unsigned int opcode, bytes_read;
24528 opcode = read_1_byte (abfd, mac_ptr);
24530 opcode_definitions[opcode] = mac_ptr;
24531 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24532 mac_ptr += bytes_read;
24541 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24542 including DW_MACRO_import. */
24545 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24547 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24548 struct macro_source_file *current_file,
24549 struct line_header *lh,
24550 struct dwarf2_section_info *section,
24551 int section_is_gnu, int section_is_dwz,
24552 unsigned int offset_size,
24553 htab_t include_hash)
24555 struct dwarf2_per_objfile *dwarf2_per_objfile
24556 = cu->per_cu->dwarf2_per_objfile;
24557 struct objfile *objfile = dwarf2_per_objfile->objfile;
24558 enum dwarf_macro_record_type macinfo_type;
24559 int at_commandline;
24560 const gdb_byte *opcode_definitions[256];
24562 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24563 &offset_size, section_is_gnu);
24564 if (mac_ptr == NULL)
24566 /* We already issued a complaint. */
24570 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24571 GDB is still reading the definitions from command line. First
24572 DW_MACINFO_start_file will need to be ignored as it was already executed
24573 to create CURRENT_FILE for the main source holding also the command line
24574 definitions. On first met DW_MACINFO_start_file this flag is reset to
24575 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24577 at_commandline = 1;
24581 /* Do we at least have room for a macinfo type byte? */
24582 if (mac_ptr >= mac_end)
24584 dwarf2_section_buffer_overflow_complaint (section);
24588 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24591 /* Note that we rely on the fact that the corresponding GNU and
24592 DWARF constants are the same. */
24594 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24595 switch (macinfo_type)
24597 /* A zero macinfo type indicates the end of the macro
24602 case DW_MACRO_define:
24603 case DW_MACRO_undef:
24604 case DW_MACRO_define_strp:
24605 case DW_MACRO_undef_strp:
24606 case DW_MACRO_define_sup:
24607 case DW_MACRO_undef_sup:
24609 unsigned int bytes_read;
24614 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24615 mac_ptr += bytes_read;
24617 if (macinfo_type == DW_MACRO_define
24618 || macinfo_type == DW_MACRO_undef)
24620 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24621 mac_ptr += bytes_read;
24625 LONGEST str_offset;
24627 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24628 mac_ptr += offset_size;
24630 if (macinfo_type == DW_MACRO_define_sup
24631 || macinfo_type == DW_MACRO_undef_sup
24634 struct dwz_file *dwz
24635 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24637 body = read_indirect_string_from_dwz (objfile,
24641 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24645 is_define = (macinfo_type == DW_MACRO_define
24646 || macinfo_type == DW_MACRO_define_strp
24647 || macinfo_type == DW_MACRO_define_sup);
24648 if (! current_file)
24650 /* DWARF violation as no main source is present. */
24651 complaint (_("debug info with no main source gives macro %s "
24653 is_define ? _("definition") : _("undefinition"),
24657 if ((line == 0 && !at_commandline)
24658 || (line != 0 && at_commandline))
24659 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24660 at_commandline ? _("command-line") : _("in-file"),
24661 is_define ? _("definition") : _("undefinition"),
24662 line == 0 ? _("zero") : _("non-zero"), line, body);
24666 /* Fedora's rpm-build's "debugedit" binary
24667 corrupted .debug_macro sections.
24670 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24671 complaint (_("debug info gives %s invalid macro %s "
24672 "without body (corrupted?) at line %d "
24674 at_commandline ? _("command-line") : _("in-file"),
24675 is_define ? _("definition") : _("undefinition"),
24676 line, current_file->filename);
24678 else if (is_define)
24679 parse_macro_definition (current_file, line, body);
24682 gdb_assert (macinfo_type == DW_MACRO_undef
24683 || macinfo_type == DW_MACRO_undef_strp
24684 || macinfo_type == DW_MACRO_undef_sup);
24685 macro_undef (current_file, line, body);
24690 case DW_MACRO_start_file:
24692 unsigned int bytes_read;
24695 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24696 mac_ptr += bytes_read;
24697 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24698 mac_ptr += bytes_read;
24700 if ((line == 0 && !at_commandline)
24701 || (line != 0 && at_commandline))
24702 complaint (_("debug info gives source %d included "
24703 "from %s at %s line %d"),
24704 file, at_commandline ? _("command-line") : _("file"),
24705 line == 0 ? _("zero") : _("non-zero"), line);
24707 if (at_commandline)
24709 /* This DW_MACRO_start_file was executed in the
24711 at_commandline = 0;
24714 current_file = macro_start_file (cu, file, line, current_file,
24719 case DW_MACRO_end_file:
24720 if (! current_file)
24721 complaint (_("macro debug info has an unmatched "
24722 "`close_file' directive"));
24725 current_file = current_file->included_by;
24726 if (! current_file)
24728 enum dwarf_macro_record_type next_type;
24730 /* GCC circa March 2002 doesn't produce the zero
24731 type byte marking the end of the compilation
24732 unit. Complain if it's not there, but exit no
24735 /* Do we at least have room for a macinfo type byte? */
24736 if (mac_ptr >= mac_end)
24738 dwarf2_section_buffer_overflow_complaint (section);
24742 /* We don't increment mac_ptr here, so this is just
24745 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24747 if (next_type != 0)
24748 complaint (_("no terminating 0-type entry for "
24749 "macros in `.debug_macinfo' section"));
24756 case DW_MACRO_import:
24757 case DW_MACRO_import_sup:
24761 bfd *include_bfd = abfd;
24762 struct dwarf2_section_info *include_section = section;
24763 const gdb_byte *include_mac_end = mac_end;
24764 int is_dwz = section_is_dwz;
24765 const gdb_byte *new_mac_ptr;
24767 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24768 mac_ptr += offset_size;
24770 if (macinfo_type == DW_MACRO_import_sup)
24772 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24774 dwarf2_read_section (objfile, &dwz->macro);
24776 include_section = &dwz->macro;
24777 include_bfd = get_section_bfd_owner (include_section);
24778 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24782 new_mac_ptr = include_section->buffer + offset;
24783 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24787 /* This has actually happened; see
24788 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24789 complaint (_("recursive DW_MACRO_import in "
24790 ".debug_macro section"));
24794 *slot = (void *) new_mac_ptr;
24796 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24797 include_mac_end, current_file, lh,
24798 section, section_is_gnu, is_dwz,
24799 offset_size, include_hash);
24801 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24806 case DW_MACINFO_vendor_ext:
24807 if (!section_is_gnu)
24809 unsigned int bytes_read;
24811 /* This reads the constant, but since we don't recognize
24812 any vendor extensions, we ignore it. */
24813 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24814 mac_ptr += bytes_read;
24815 read_direct_string (abfd, mac_ptr, &bytes_read);
24816 mac_ptr += bytes_read;
24818 /* We don't recognize any vendor extensions. */
24824 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24825 mac_ptr, mac_end, abfd, offset_size,
24827 if (mac_ptr == NULL)
24832 } while (macinfo_type != 0);
24836 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24837 int section_is_gnu)
24839 struct dwarf2_per_objfile *dwarf2_per_objfile
24840 = cu->per_cu->dwarf2_per_objfile;
24841 struct objfile *objfile = dwarf2_per_objfile->objfile;
24842 struct line_header *lh = cu->line_header;
24844 const gdb_byte *mac_ptr, *mac_end;
24845 struct macro_source_file *current_file = 0;
24846 enum dwarf_macro_record_type macinfo_type;
24847 unsigned int offset_size = cu->header.offset_size;
24848 const gdb_byte *opcode_definitions[256];
24850 struct dwarf2_section_info *section;
24851 const char *section_name;
24853 if (cu->dwo_unit != NULL)
24855 if (section_is_gnu)
24857 section = &cu->dwo_unit->dwo_file->sections.macro;
24858 section_name = ".debug_macro.dwo";
24862 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24863 section_name = ".debug_macinfo.dwo";
24868 if (section_is_gnu)
24870 section = &dwarf2_per_objfile->macro;
24871 section_name = ".debug_macro";
24875 section = &dwarf2_per_objfile->macinfo;
24876 section_name = ".debug_macinfo";
24880 dwarf2_read_section (objfile, section);
24881 if (section->buffer == NULL)
24883 complaint (_("missing %s section"), section_name);
24886 abfd = get_section_bfd_owner (section);
24888 /* First pass: Find the name of the base filename.
24889 This filename is needed in order to process all macros whose definition
24890 (or undefinition) comes from the command line. These macros are defined
24891 before the first DW_MACINFO_start_file entry, and yet still need to be
24892 associated to the base file.
24894 To determine the base file name, we scan the macro definitions until we
24895 reach the first DW_MACINFO_start_file entry. We then initialize
24896 CURRENT_FILE accordingly so that any macro definition found before the
24897 first DW_MACINFO_start_file can still be associated to the base file. */
24899 mac_ptr = section->buffer + offset;
24900 mac_end = section->buffer + section->size;
24902 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24903 &offset_size, section_is_gnu);
24904 if (mac_ptr == NULL)
24906 /* We already issued a complaint. */
24912 /* Do we at least have room for a macinfo type byte? */
24913 if (mac_ptr >= mac_end)
24915 /* Complaint is printed during the second pass as GDB will probably
24916 stop the first pass earlier upon finding
24917 DW_MACINFO_start_file. */
24921 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24924 /* Note that we rely on the fact that the corresponding GNU and
24925 DWARF constants are the same. */
24927 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24928 switch (macinfo_type)
24930 /* A zero macinfo type indicates the end of the macro
24935 case DW_MACRO_define:
24936 case DW_MACRO_undef:
24937 /* Only skip the data by MAC_PTR. */
24939 unsigned int bytes_read;
24941 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24942 mac_ptr += bytes_read;
24943 read_direct_string (abfd, mac_ptr, &bytes_read);
24944 mac_ptr += bytes_read;
24948 case DW_MACRO_start_file:
24950 unsigned int bytes_read;
24953 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24954 mac_ptr += bytes_read;
24955 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24956 mac_ptr += bytes_read;
24958 current_file = macro_start_file (cu, file, line, current_file, lh);
24962 case DW_MACRO_end_file:
24963 /* No data to skip by MAC_PTR. */
24966 case DW_MACRO_define_strp:
24967 case DW_MACRO_undef_strp:
24968 case DW_MACRO_define_sup:
24969 case DW_MACRO_undef_sup:
24971 unsigned int bytes_read;
24973 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24974 mac_ptr += bytes_read;
24975 mac_ptr += offset_size;
24979 case DW_MACRO_import:
24980 case DW_MACRO_import_sup:
24981 /* Note that, according to the spec, a transparent include
24982 chain cannot call DW_MACRO_start_file. So, we can just
24983 skip this opcode. */
24984 mac_ptr += offset_size;
24987 case DW_MACINFO_vendor_ext:
24988 /* Only skip the data by MAC_PTR. */
24989 if (!section_is_gnu)
24991 unsigned int bytes_read;
24993 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24994 mac_ptr += bytes_read;
24995 read_direct_string (abfd, mac_ptr, &bytes_read);
24996 mac_ptr += bytes_read;
25001 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25002 mac_ptr, mac_end, abfd, offset_size,
25004 if (mac_ptr == NULL)
25009 } while (macinfo_type != 0 && current_file == NULL);
25011 /* Second pass: Process all entries.
25013 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25014 command-line macro definitions/undefinitions. This flag is unset when we
25015 reach the first DW_MACINFO_start_file entry. */
25017 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25019 NULL, xcalloc, xfree));
25020 mac_ptr = section->buffer + offset;
25021 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25022 *slot = (void *) mac_ptr;
25023 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25024 current_file, lh, section,
25025 section_is_gnu, 0, offset_size,
25026 include_hash.get ());
25029 /* Check if the attribute's form is a DW_FORM_block*
25030 if so return true else false. */
25033 attr_form_is_block (const struct attribute *attr)
25035 return (attr == NULL ? 0 :
25036 attr->form == DW_FORM_block1
25037 || attr->form == DW_FORM_block2
25038 || attr->form == DW_FORM_block4
25039 || attr->form == DW_FORM_block
25040 || attr->form == DW_FORM_exprloc);
25043 /* Return non-zero if ATTR's value is a section offset --- classes
25044 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25045 You may use DW_UNSND (attr) to retrieve such offsets.
25047 Section 7.5.4, "Attribute Encodings", explains that no attribute
25048 may have a value that belongs to more than one of these classes; it
25049 would be ambiguous if we did, because we use the same forms for all
25053 attr_form_is_section_offset (const struct attribute *attr)
25055 return (attr->form == DW_FORM_data4
25056 || attr->form == DW_FORM_data8
25057 || attr->form == DW_FORM_sec_offset);
25060 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25061 zero otherwise. When this function returns true, you can apply
25062 dwarf2_get_attr_constant_value to it.
25064 However, note that for some attributes you must check
25065 attr_form_is_section_offset before using this test. DW_FORM_data4
25066 and DW_FORM_data8 are members of both the constant class, and of
25067 the classes that contain offsets into other debug sections
25068 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25069 that, if an attribute's can be either a constant or one of the
25070 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25071 taken as section offsets, not constants.
25073 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25074 cannot handle that. */
25077 attr_form_is_constant (const struct attribute *attr)
25079 switch (attr->form)
25081 case DW_FORM_sdata:
25082 case DW_FORM_udata:
25083 case DW_FORM_data1:
25084 case DW_FORM_data2:
25085 case DW_FORM_data4:
25086 case DW_FORM_data8:
25087 case DW_FORM_implicit_const:
25095 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25096 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25099 attr_form_is_ref (const struct attribute *attr)
25101 switch (attr->form)
25103 case DW_FORM_ref_addr:
25108 case DW_FORM_ref_udata:
25109 case DW_FORM_GNU_ref_alt:
25116 /* Return the .debug_loc section to use for CU.
25117 For DWO files use .debug_loc.dwo. */
25119 static struct dwarf2_section_info *
25120 cu_debug_loc_section (struct dwarf2_cu *cu)
25122 struct dwarf2_per_objfile *dwarf2_per_objfile
25123 = cu->per_cu->dwarf2_per_objfile;
25127 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25129 return cu->header.version >= 5 ? §ions->loclists : §ions->loc;
25131 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25132 : &dwarf2_per_objfile->loc);
25135 /* A helper function that fills in a dwarf2_loclist_baton. */
25138 fill_in_loclist_baton (struct dwarf2_cu *cu,
25139 struct dwarf2_loclist_baton *baton,
25140 const struct attribute *attr)
25142 struct dwarf2_per_objfile *dwarf2_per_objfile
25143 = cu->per_cu->dwarf2_per_objfile;
25144 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25146 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25148 baton->per_cu = cu->per_cu;
25149 gdb_assert (baton->per_cu);
25150 /* We don't know how long the location list is, but make sure we
25151 don't run off the edge of the section. */
25152 baton->size = section->size - DW_UNSND (attr);
25153 baton->data = section->buffer + DW_UNSND (attr);
25154 baton->base_address = cu->base_address;
25155 baton->from_dwo = cu->dwo_unit != NULL;
25159 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25160 struct dwarf2_cu *cu, int is_block)
25162 struct dwarf2_per_objfile *dwarf2_per_objfile
25163 = cu->per_cu->dwarf2_per_objfile;
25164 struct objfile *objfile = dwarf2_per_objfile->objfile;
25165 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25167 if (attr_form_is_section_offset (attr)
25168 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25169 the section. If so, fall through to the complaint in the
25171 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25173 struct dwarf2_loclist_baton *baton;
25175 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25177 fill_in_loclist_baton (cu, baton, attr);
25179 if (cu->base_known == 0)
25180 complaint (_("Location list used without "
25181 "specifying the CU base address."));
25183 SYMBOL_ACLASS_INDEX (sym) = (is_block
25184 ? dwarf2_loclist_block_index
25185 : dwarf2_loclist_index);
25186 SYMBOL_LOCATION_BATON (sym) = baton;
25190 struct dwarf2_locexpr_baton *baton;
25192 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25193 baton->per_cu = cu->per_cu;
25194 gdb_assert (baton->per_cu);
25196 if (attr_form_is_block (attr))
25198 /* Note that we're just copying the block's data pointer
25199 here, not the actual data. We're still pointing into the
25200 info_buffer for SYM's objfile; right now we never release
25201 that buffer, but when we do clean up properly this may
25203 baton->size = DW_BLOCK (attr)->size;
25204 baton->data = DW_BLOCK (attr)->data;
25208 dwarf2_invalid_attrib_class_complaint ("location description",
25209 SYMBOL_NATURAL_NAME (sym));
25213 SYMBOL_ACLASS_INDEX (sym) = (is_block
25214 ? dwarf2_locexpr_block_index
25215 : dwarf2_locexpr_index);
25216 SYMBOL_LOCATION_BATON (sym) = baton;
25220 /* Return the OBJFILE associated with the compilation unit CU. If CU
25221 came from a separate debuginfo file, then the master objfile is
25225 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25227 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25229 /* Return the master objfile, so that we can report and look up the
25230 correct file containing this variable. */
25231 if (objfile->separate_debug_objfile_backlink)
25232 objfile = objfile->separate_debug_objfile_backlink;
25237 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25238 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25239 CU_HEADERP first. */
25241 static const struct comp_unit_head *
25242 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25243 struct dwarf2_per_cu_data *per_cu)
25245 const gdb_byte *info_ptr;
25248 return &per_cu->cu->header;
25250 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25252 memset (cu_headerp, 0, sizeof (*cu_headerp));
25253 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25254 rcuh_kind::COMPILE);
25259 /* Return the address size given in the compilation unit header for CU. */
25262 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25264 struct comp_unit_head cu_header_local;
25265 const struct comp_unit_head *cu_headerp;
25267 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25269 return cu_headerp->addr_size;
25272 /* Return the offset size given in the compilation unit header for CU. */
25275 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25277 struct comp_unit_head cu_header_local;
25278 const struct comp_unit_head *cu_headerp;
25280 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25282 return cu_headerp->offset_size;
25285 /* See its dwarf2loc.h declaration. */
25288 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25290 struct comp_unit_head cu_header_local;
25291 const struct comp_unit_head *cu_headerp;
25293 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25295 if (cu_headerp->version == 2)
25296 return cu_headerp->addr_size;
25298 return cu_headerp->offset_size;
25301 /* Return the text offset of the CU. The returned offset comes from
25302 this CU's objfile. If this objfile came from a separate debuginfo
25303 file, then the offset may be different from the corresponding
25304 offset in the parent objfile. */
25307 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25309 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25311 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25314 /* Return a type that is a generic pointer type, the size of which matches
25315 the address size given in the compilation unit header for PER_CU. */
25316 static struct type *
25317 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25319 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25320 struct type *void_type = objfile_type (objfile)->builtin_void;
25321 struct type *addr_type = lookup_pointer_type (void_type);
25322 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25324 if (TYPE_LENGTH (addr_type) == addr_size)
25328 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25332 /* Return DWARF version number of PER_CU. */
25335 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25337 return per_cu->dwarf_version;
25340 /* Locate the .debug_info compilation unit from CU's objfile which contains
25341 the DIE at OFFSET. Raises an error on failure. */
25343 static struct dwarf2_per_cu_data *
25344 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25345 unsigned int offset_in_dwz,
25346 struct dwarf2_per_objfile *dwarf2_per_objfile)
25348 struct dwarf2_per_cu_data *this_cu;
25352 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25355 struct dwarf2_per_cu_data *mid_cu;
25356 int mid = low + (high - low) / 2;
25358 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25359 if (mid_cu->is_dwz > offset_in_dwz
25360 || (mid_cu->is_dwz == offset_in_dwz
25361 && mid_cu->sect_off + mid_cu->length >= sect_off))
25366 gdb_assert (low == high);
25367 this_cu = dwarf2_per_objfile->all_comp_units[low];
25368 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25370 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25371 error (_("Dwarf Error: could not find partial DIE containing "
25372 "offset %s [in module %s]"),
25373 sect_offset_str (sect_off),
25374 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25376 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25378 return dwarf2_per_objfile->all_comp_units[low-1];
25382 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25383 && sect_off >= this_cu->sect_off + this_cu->length)
25384 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25385 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25390 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25392 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25393 : per_cu (per_cu_),
25395 has_loclist (false),
25396 checked_producer (false),
25397 producer_is_gxx_lt_4_6 (false),
25398 producer_is_gcc_lt_4_3 (false),
25399 producer_is_icc (false),
25400 producer_is_icc_lt_14 (false),
25401 producer_is_codewarrior (false),
25402 processing_has_namespace_info (false)
25407 /* Destroy a dwarf2_cu. */
25409 dwarf2_cu::~dwarf2_cu ()
25414 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25417 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25418 enum language pretend_language)
25420 struct attribute *attr;
25422 /* Set the language we're debugging. */
25423 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25425 set_cu_language (DW_UNSND (attr), cu);
25428 cu->language = pretend_language;
25429 cu->language_defn = language_def (cu->language);
25432 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25435 /* Increase the age counter on each cached compilation unit, and free
25436 any that are too old. */
25439 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25441 struct dwarf2_per_cu_data *per_cu, **last_chain;
25443 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25444 per_cu = dwarf2_per_objfile->read_in_chain;
25445 while (per_cu != NULL)
25447 per_cu->cu->last_used ++;
25448 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25449 dwarf2_mark (per_cu->cu);
25450 per_cu = per_cu->cu->read_in_chain;
25453 per_cu = dwarf2_per_objfile->read_in_chain;
25454 last_chain = &dwarf2_per_objfile->read_in_chain;
25455 while (per_cu != NULL)
25457 struct dwarf2_per_cu_data *next_cu;
25459 next_cu = per_cu->cu->read_in_chain;
25461 if (!per_cu->cu->mark)
25464 *last_chain = next_cu;
25467 last_chain = &per_cu->cu->read_in_chain;
25473 /* Remove a single compilation unit from the cache. */
25476 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25478 struct dwarf2_per_cu_data *per_cu, **last_chain;
25479 struct dwarf2_per_objfile *dwarf2_per_objfile
25480 = target_per_cu->dwarf2_per_objfile;
25482 per_cu = dwarf2_per_objfile->read_in_chain;
25483 last_chain = &dwarf2_per_objfile->read_in_chain;
25484 while (per_cu != NULL)
25486 struct dwarf2_per_cu_data *next_cu;
25488 next_cu = per_cu->cu->read_in_chain;
25490 if (per_cu == target_per_cu)
25494 *last_chain = next_cu;
25498 last_chain = &per_cu->cu->read_in_chain;
25504 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25505 We store these in a hash table separate from the DIEs, and preserve them
25506 when the DIEs are flushed out of cache.
25508 The CU "per_cu" pointer is needed because offset alone is not enough to
25509 uniquely identify the type. A file may have multiple .debug_types sections,
25510 or the type may come from a DWO file. Furthermore, while it's more logical
25511 to use per_cu->section+offset, with Fission the section with the data is in
25512 the DWO file but we don't know that section at the point we need it.
25513 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25514 because we can enter the lookup routine, get_die_type_at_offset, from
25515 outside this file, and thus won't necessarily have PER_CU->cu.
25516 Fortunately, PER_CU is stable for the life of the objfile. */
25518 struct dwarf2_per_cu_offset_and_type
25520 const struct dwarf2_per_cu_data *per_cu;
25521 sect_offset sect_off;
25525 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25528 per_cu_offset_and_type_hash (const void *item)
25530 const struct dwarf2_per_cu_offset_and_type *ofs
25531 = (const struct dwarf2_per_cu_offset_and_type *) item;
25533 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25536 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25539 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25541 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25542 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25543 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25544 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25546 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25547 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25550 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25551 table if necessary. For convenience, return TYPE.
25553 The DIEs reading must have careful ordering to:
25554 * Not cause infite loops trying to read in DIEs as a prerequisite for
25555 reading current DIE.
25556 * Not trying to dereference contents of still incompletely read in types
25557 while reading in other DIEs.
25558 * Enable referencing still incompletely read in types just by a pointer to
25559 the type without accessing its fields.
25561 Therefore caller should follow these rules:
25562 * Try to fetch any prerequisite types we may need to build this DIE type
25563 before building the type and calling set_die_type.
25564 * After building type call set_die_type for current DIE as soon as
25565 possible before fetching more types to complete the current type.
25566 * Make the type as complete as possible before fetching more types. */
25568 static struct type *
25569 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25571 struct dwarf2_per_objfile *dwarf2_per_objfile
25572 = cu->per_cu->dwarf2_per_objfile;
25573 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25574 struct objfile *objfile = dwarf2_per_objfile->objfile;
25575 struct attribute *attr;
25576 struct dynamic_prop prop;
25578 /* For Ada types, make sure that the gnat-specific data is always
25579 initialized (if not already set). There are a few types where
25580 we should not be doing so, because the type-specific area is
25581 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25582 where the type-specific area is used to store the floatformat).
25583 But this is not a problem, because the gnat-specific information
25584 is actually not needed for these types. */
25585 if (need_gnat_info (cu)
25586 && TYPE_CODE (type) != TYPE_CODE_FUNC
25587 && TYPE_CODE (type) != TYPE_CODE_FLT
25588 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25589 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25590 && TYPE_CODE (type) != TYPE_CODE_METHOD
25591 && !HAVE_GNAT_AUX_INFO (type))
25592 INIT_GNAT_SPECIFIC (type);
25594 /* Read DW_AT_allocated and set in type. */
25595 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25596 if (attr_form_is_block (attr))
25598 struct type *prop_type
25599 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25600 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25601 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25603 else if (attr != NULL)
25605 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25606 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25607 sect_offset_str (die->sect_off));
25610 /* Read DW_AT_associated and set in type. */
25611 attr = dwarf2_attr (die, DW_AT_associated, cu);
25612 if (attr_form_is_block (attr))
25614 struct type *prop_type
25615 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25616 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25617 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25619 else if (attr != NULL)
25621 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25622 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25623 sect_offset_str (die->sect_off));
25626 /* Read DW_AT_data_location and set in type. */
25627 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25628 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25629 dwarf2_per_cu_addr_type (cu->per_cu)))
25630 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25632 if (dwarf2_per_objfile->die_type_hash == NULL)
25634 dwarf2_per_objfile->die_type_hash =
25635 htab_create_alloc_ex (127,
25636 per_cu_offset_and_type_hash,
25637 per_cu_offset_and_type_eq,
25639 &objfile->objfile_obstack,
25640 hashtab_obstack_allocate,
25641 dummy_obstack_deallocate);
25644 ofs.per_cu = cu->per_cu;
25645 ofs.sect_off = die->sect_off;
25647 slot = (struct dwarf2_per_cu_offset_and_type **)
25648 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25650 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25651 sect_offset_str (die->sect_off));
25652 *slot = XOBNEW (&objfile->objfile_obstack,
25653 struct dwarf2_per_cu_offset_and_type);
25658 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25659 or return NULL if the die does not have a saved type. */
25661 static struct type *
25662 get_die_type_at_offset (sect_offset sect_off,
25663 struct dwarf2_per_cu_data *per_cu)
25665 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25666 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25668 if (dwarf2_per_objfile->die_type_hash == NULL)
25671 ofs.per_cu = per_cu;
25672 ofs.sect_off = sect_off;
25673 slot = ((struct dwarf2_per_cu_offset_and_type *)
25674 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25681 /* Look up the type for DIE in CU in die_type_hash,
25682 or return NULL if DIE does not have a saved type. */
25684 static struct type *
25685 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25687 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25690 /* Add a dependence relationship from CU to REF_PER_CU. */
25693 dwarf2_add_dependence (struct dwarf2_cu *cu,
25694 struct dwarf2_per_cu_data *ref_per_cu)
25698 if (cu->dependencies == NULL)
25700 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25701 NULL, &cu->comp_unit_obstack,
25702 hashtab_obstack_allocate,
25703 dummy_obstack_deallocate);
25705 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25707 *slot = ref_per_cu;
25710 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25711 Set the mark field in every compilation unit in the
25712 cache that we must keep because we are keeping CU. */
25715 dwarf2_mark_helper (void **slot, void *data)
25717 struct dwarf2_per_cu_data *per_cu;
25719 per_cu = (struct dwarf2_per_cu_data *) *slot;
25721 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25722 reading of the chain. As such dependencies remain valid it is not much
25723 useful to track and undo them during QUIT cleanups. */
25724 if (per_cu->cu == NULL)
25727 if (per_cu->cu->mark)
25729 per_cu->cu->mark = true;
25731 if (per_cu->cu->dependencies != NULL)
25732 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25737 /* Set the mark field in CU and in every other compilation unit in the
25738 cache that we must keep because we are keeping CU. */
25741 dwarf2_mark (struct dwarf2_cu *cu)
25746 if (cu->dependencies != NULL)
25747 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25751 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25755 per_cu->cu->mark = false;
25756 per_cu = per_cu->cu->read_in_chain;
25760 /* Trivial hash function for partial_die_info: the hash value of a DIE
25761 is its offset in .debug_info for this objfile. */
25764 partial_die_hash (const void *item)
25766 const struct partial_die_info *part_die
25767 = (const struct partial_die_info *) item;
25769 return to_underlying (part_die->sect_off);
25772 /* Trivial comparison function for partial_die_info structures: two DIEs
25773 are equal if they have the same offset. */
25776 partial_die_eq (const void *item_lhs, const void *item_rhs)
25778 const struct partial_die_info *part_die_lhs
25779 = (const struct partial_die_info *) item_lhs;
25780 const struct partial_die_info *part_die_rhs
25781 = (const struct partial_die_info *) item_rhs;
25783 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25786 struct cmd_list_element *set_dwarf_cmdlist;
25787 struct cmd_list_element *show_dwarf_cmdlist;
25790 set_dwarf_cmd (const char *args, int from_tty)
25792 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25797 show_dwarf_cmd (const char *args, int from_tty)
25799 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25802 int dwarf_always_disassemble;
25805 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25806 struct cmd_list_element *c, const char *value)
25808 fprintf_filtered (file,
25809 _("Whether to always disassemble "
25810 "DWARF expressions is %s.\n"),
25815 show_check_physname (struct ui_file *file, int from_tty,
25816 struct cmd_list_element *c, const char *value)
25818 fprintf_filtered (file,
25819 _("Whether to check \"physname\" is %s.\n"),
25824 _initialize_dwarf2_read (void)
25826 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25827 Set DWARF specific variables.\n\
25828 Configure DWARF variables such as the cache size."),
25829 &set_dwarf_cmdlist, "maintenance set dwarf ",
25830 0/*allow-unknown*/, &maintenance_set_cmdlist);
25832 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25833 Show DWARF specific variables.\n\
25834 Show DWARF variables such as the cache size."),
25835 &show_dwarf_cmdlist, "maintenance show dwarf ",
25836 0/*allow-unknown*/, &maintenance_show_cmdlist);
25838 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25839 &dwarf_max_cache_age, _("\
25840 Set the upper bound on the age of cached DWARF compilation units."), _("\
25841 Show the upper bound on the age of cached DWARF compilation units."), _("\
25842 A higher limit means that cached compilation units will be stored\n\
25843 in memory longer, and more total memory will be used. Zero disables\n\
25844 caching, which can slow down startup."),
25846 show_dwarf_max_cache_age,
25847 &set_dwarf_cmdlist,
25848 &show_dwarf_cmdlist);
25850 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25851 &dwarf_always_disassemble, _("\
25852 Set whether `info address' always disassembles DWARF expressions."), _("\
25853 Show whether `info address' always disassembles DWARF expressions."), _("\
25854 When enabled, DWARF expressions are always printed in an assembly-like\n\
25855 syntax. When disabled, expressions will be printed in a more\n\
25856 conversational style, when possible."),
25858 show_dwarf_always_disassemble,
25859 &set_dwarf_cmdlist,
25860 &show_dwarf_cmdlist);
25862 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25863 Set debugging of the DWARF reader."), _("\
25864 Show debugging of the DWARF reader."), _("\
25865 When enabled (non-zero), debugging messages are printed during DWARF\n\
25866 reading and symtab expansion. A value of 1 (one) provides basic\n\
25867 information. A value greater than 1 provides more verbose information."),
25870 &setdebuglist, &showdebuglist);
25872 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25873 Set debugging of the DWARF DIE reader."), _("\
25874 Show debugging of the DWARF DIE reader."), _("\
25875 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25876 The value is the maximum depth to print."),
25879 &setdebuglist, &showdebuglist);
25881 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25882 Set debugging of the dwarf line reader."), _("\
25883 Show debugging of the dwarf line reader."), _("\
25884 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25885 A value of 1 (one) provides basic information.\n\
25886 A value greater than 1 provides more verbose information."),
25889 &setdebuglist, &showdebuglist);
25891 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25892 Set cross-checking of \"physname\" code against demangler."), _("\
25893 Show cross-checking of \"physname\" code against demangler."), _("\
25894 When enabled, GDB's internal \"physname\" code is checked against\n\
25896 NULL, show_check_physname,
25897 &setdebuglist, &showdebuglist);
25899 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25900 no_class, &use_deprecated_index_sections, _("\
25901 Set whether to use deprecated gdb_index sections."), _("\
25902 Show whether to use deprecated gdb_index sections."), _("\
25903 When enabled, deprecated .gdb_index sections are used anyway.\n\
25904 Normally they are ignored either because of a missing feature or\n\
25905 performance issue.\n\
25906 Warning: This option must be enabled before gdb reads the file."),
25909 &setlist, &showlist);
25911 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25912 &dwarf2_locexpr_funcs);
25913 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25914 &dwarf2_loclist_funcs);
25916 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25917 &dwarf2_block_frame_base_locexpr_funcs);
25918 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25919 &dwarf2_block_frame_base_loclist_funcs);
25922 selftests::register_test ("dw2_expand_symtabs_matching",
25923 selftests::dw2_expand_symtabs_matching::run_test);